Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <tools/stream.hxx>
21 : #include <tools/vcompat.hxx>
22 : #include <tools/debug.hxx>
23 : #include <vcl/bitmapex.hxx>
24 : #include <vcl/gradient.hxx>
25 : #include <vcl/wall.hxx>
26 : #include <vcl/svapp.hxx>
27 : #include <wall2.hxx>
28 : #include <vcl/dibtools.hxx>
29 :
30 : DBG_NAME( Wallpaper )
31 :
32 : // -----------------------------------------------------------------------
33 :
34 199859 : ImplWallpaper::ImplWallpaper() :
35 199859 : maColor( COL_TRANSPARENT )
36 : {
37 199859 : mnRefCount = 1;
38 199859 : mpBitmap = NULL;
39 199859 : mpCache = NULL;
40 199859 : mpGradient = NULL;
41 199859 : mpRect = NULL;
42 199859 : meStyle = WALLPAPER_NULL;
43 199859 : }
44 :
45 : // -----------------------------------------------------------------------
46 :
47 27427 : ImplWallpaper::ImplWallpaper( const ImplWallpaper& rImplWallpaper ) :
48 27427 : maColor( rImplWallpaper.maColor )
49 : {
50 27427 : mnRefCount = 1;
51 27427 : meStyle = rImplWallpaper.meStyle;
52 :
53 27427 : if ( rImplWallpaper.mpBitmap )
54 0 : mpBitmap = new BitmapEx( *rImplWallpaper.mpBitmap );
55 : else
56 27427 : mpBitmap = NULL;
57 27427 : if( rImplWallpaper.mpCache )
58 0 : mpCache = new BitmapEx( *rImplWallpaper.mpCache );
59 : else
60 27427 : mpCache = NULL;
61 27427 : if ( rImplWallpaper.mpGradient )
62 0 : mpGradient = new Gradient( *rImplWallpaper.mpGradient );
63 : else
64 27427 : mpGradient = NULL;
65 27427 : if ( rImplWallpaper.mpRect )
66 0 : mpRect = new Rectangle( *rImplWallpaper.mpRect );
67 : else
68 27427 : mpRect = NULL;
69 27427 : }
70 :
71 : // -----------------------------------------------------------------------
72 :
73 226909 : ImplWallpaper::~ImplWallpaper()
74 : {
75 226909 : delete mpBitmap;
76 226909 : delete mpCache;
77 226909 : delete mpGradient;
78 226909 : delete mpRect;
79 226909 : }
80 :
81 : // -----------------------------------------------------------------------
82 :
83 0 : void ImplWallpaper::ImplSetCachedBitmap( BitmapEx& rBmp )
84 : {
85 0 : if( !mpCache )
86 0 : mpCache = new BitmapEx( rBmp );
87 : else
88 0 : *mpCache = rBmp;
89 0 : }
90 :
91 : // -----------------------------------------------------------------------
92 :
93 27427 : void ImplWallpaper::ImplReleaseCachedBitmap()
94 : {
95 27427 : delete mpCache;
96 27427 : mpCache = NULL;
97 27427 : }
98 :
99 : // -----------------------------------------------------------------------
100 :
101 0 : SvStream& operator>>( SvStream& rIStm, ImplWallpaper& rImplWallpaper )
102 : {
103 0 : VersionCompat aCompat( rIStm, STREAM_READ );
104 : sal_uInt16 nTmp16;
105 :
106 0 : delete rImplWallpaper.mpRect;
107 0 : rImplWallpaper.mpRect = NULL;
108 :
109 0 : delete rImplWallpaper.mpGradient;
110 0 : rImplWallpaper.mpGradient = NULL;
111 :
112 0 : delete rImplWallpaper.mpBitmap;
113 0 : rImplWallpaper.mpBitmap = NULL;
114 :
115 : // version 1
116 0 : rIStm >> rImplWallpaper.maColor;
117 0 : rIStm >> nTmp16; rImplWallpaper.meStyle = (WallpaperStyle) nTmp16;
118 :
119 : // version 2
120 0 : if( aCompat.GetVersion() >= 2 )
121 : {
122 : sal_Bool bRect, bGrad, bBmp, bDummy;
123 :
124 0 : rIStm >> bRect >> bGrad >> bBmp >> bDummy >> bDummy >> bDummy;
125 :
126 0 : if( bRect )
127 : {
128 0 : rImplWallpaper.mpRect = new Rectangle;
129 0 : rIStm >> *rImplWallpaper.mpRect;
130 : }
131 :
132 0 : if( bGrad )
133 : {
134 0 : rImplWallpaper.mpGradient = new Gradient;
135 0 : rIStm >> *rImplWallpaper.mpGradient;
136 : }
137 :
138 0 : if( bBmp )
139 : {
140 0 : rImplWallpaper.mpBitmap = new BitmapEx;
141 0 : ReadDIBBitmapEx(*rImplWallpaper.mpBitmap, rIStm);
142 : }
143 :
144 : // version 3 (new color format)
145 0 : if( aCompat.GetVersion() >= 3 )
146 : {
147 0 : rImplWallpaper.maColor.Read( rIStm, sal_True );
148 : }
149 : }
150 :
151 0 : return rIStm;
152 : }
153 :
154 : // -----------------------------------------------------------------------
155 :
156 0 : SvStream& operator<<( SvStream& rOStm, const ImplWallpaper& rImplWallpaper )
157 : {
158 0 : VersionCompat aCompat( rOStm, STREAM_WRITE, 3 );
159 0 : sal_Bool bRect = ( rImplWallpaper.mpRect != NULL );
160 0 : sal_Bool bGrad = ( rImplWallpaper.mpGradient != NULL );
161 0 : sal_Bool bBmp = ( rImplWallpaper.mpBitmap != NULL );
162 0 : sal_Bool bDummy = sal_False;
163 :
164 : // version 1
165 0 : rOStm << rImplWallpaper.maColor << (sal_uInt16) rImplWallpaper.meStyle;
166 :
167 : // version 2
168 0 : rOStm << bRect << bGrad << bBmp << bDummy << bDummy << bDummy;
169 :
170 0 : if( bRect )
171 0 : rOStm << *rImplWallpaper.mpRect;
172 :
173 0 : if( bGrad )
174 0 : rOStm << *rImplWallpaper.mpGradient;
175 :
176 0 : if( bBmp )
177 0 : WriteDIBBitmapEx(*rImplWallpaper.mpBitmap, rOStm);
178 :
179 : // version 3 (new color format)
180 0 : ( (Color&) rImplWallpaper.maColor ).Write( rOStm, sal_True );
181 :
182 0 : return rOStm;
183 : }
184 :
185 : // -----------------------------------------------------------------------
186 :
187 54593 : inline void Wallpaper::ImplMakeUnique( sal_Bool bReleaseCache )
188 : {
189 : // Falls noch andere Referenzen bestehen, dann kopieren
190 54593 : if ( mpImplWallpaper->mnRefCount != 1 )
191 : {
192 27427 : if ( mpImplWallpaper->mnRefCount )
193 261 : mpImplWallpaper->mnRefCount--;
194 27427 : mpImplWallpaper = new ImplWallpaper( *(mpImplWallpaper) );
195 : }
196 :
197 54593 : if( bReleaseCache )
198 27427 : mpImplWallpaper->ImplReleaseCachedBitmap();
199 54593 : }
200 :
201 : // -----------------------------------------------------------------------
202 :
203 205662 : Wallpaper::Wallpaper()
204 : {
205 : DBG_CTOR( Wallpaper, NULL );
206 :
207 205662 : static ImplWallpaper aStaticImplWallpaper;
208 :
209 205662 : aStaticImplWallpaper.mnRefCount = 0;
210 205662 : mpImplWallpaper = &aStaticImplWallpaper;
211 205662 : }
212 :
213 : // -----------------------------------------------------------------------
214 :
215 26304 : Wallpaper::Wallpaper( const Wallpaper& rWallpaper )
216 : {
217 : DBG_CTOR( Wallpaper, NULL );
218 : DBG_CHKOBJ( &rWallpaper, Wallpaper, NULL );
219 : DBG_ASSERT( rWallpaper.mpImplWallpaper->mnRefCount < 0xFFFFFFFE, "Wallpaper: RefCount overflow" );
220 :
221 : // Instance Daten uebernehmen und Referenzcounter erhoehen
222 26304 : mpImplWallpaper = rWallpaper.mpImplWallpaper;
223 : // RefCount == 0 fuer statische Objekte
224 26304 : if ( mpImplWallpaper->mnRefCount )
225 26284 : mpImplWallpaper->mnRefCount++;
226 26304 : }
227 :
228 : // -----------------------------------------------------------------------
229 :
230 199208 : Wallpaper::Wallpaper( const Color& rColor )
231 : {
232 : DBG_CTOR( Wallpaper, NULL );
233 :
234 199208 : mpImplWallpaper = new ImplWallpaper;
235 199208 : mpImplWallpaper->maColor = rColor;
236 199208 : mpImplWallpaper->meStyle = WALLPAPER_TILE;
237 199208 : }
238 :
239 : // -----------------------------------------------------------------------
240 :
241 0 : Wallpaper::Wallpaper( const BitmapEx& rBmpEx )
242 : {
243 : DBG_CTOR( Wallpaper, NULL );
244 :
245 0 : mpImplWallpaper = new ImplWallpaper;
246 0 : mpImplWallpaper->mpBitmap = new BitmapEx( rBmpEx );
247 0 : mpImplWallpaper->meStyle = WALLPAPER_TILE;
248 0 : }
249 :
250 : // -----------------------------------------------------------------------
251 :
252 507 : Wallpaper::Wallpaper( const Gradient& rGradient )
253 : {
254 : DBG_CTOR( Wallpaper, NULL );
255 :
256 507 : mpImplWallpaper = new ImplWallpaper;
257 507 : mpImplWallpaper->mpGradient = new Gradient( rGradient );
258 507 : mpImplWallpaper->meStyle = WALLPAPER_TILE;
259 507 : }
260 :
261 : // -----------------------------------------------------------------------
262 :
263 431153 : Wallpaper::~Wallpaper()
264 : {
265 : DBG_DTOR( Wallpaper, NULL );
266 :
267 : // Wenn es keine statischen ImpDaten sind, dann loeschen, wenn es
268 : // die letzte Referenz ist, sonst Referenzcounter decrementieren
269 431153 : if ( mpImplWallpaper->mnRefCount )
270 : {
271 366948 : if ( mpImplWallpaper->mnRefCount == 1 )
272 110105 : delete mpImplWallpaper;
273 : else
274 256843 : mpImplWallpaper->mnRefCount--;
275 : }
276 431153 : }
277 :
278 : // -----------------------------------------------------------------------
279 :
280 261 : void Wallpaper::SetColor( const Color& rColor )
281 : {
282 : DBG_CHKTHIS( Wallpaper, NULL );
283 :
284 261 : ImplMakeUnique();
285 261 : mpImplWallpaper->maColor = rColor;
286 :
287 261 : if( WALLPAPER_NULL == mpImplWallpaper->meStyle || WALLPAPER_APPLICATIONGRADIENT == mpImplWallpaper->meStyle )
288 0 : mpImplWallpaper->meStyle = WALLPAPER_TILE;
289 261 : }
290 :
291 : // -----------------------------------------------------------------------
292 :
293 183752 : const Color& Wallpaper::GetColor() const
294 : {
295 : DBG_CHKTHIS( Wallpaper, NULL );
296 :
297 183752 : return mpImplWallpaper->maColor;
298 : }
299 :
300 : // -----------------------------------------------------------------------
301 :
302 27166 : void Wallpaper::SetStyle( WallpaperStyle eStyle )
303 : {
304 : DBG_CHKTHIS( Wallpaper, NULL );
305 :
306 27166 : ImplMakeUnique( sal_False );
307 :
308 27166 : if( eStyle == WALLPAPER_APPLICATIONGRADIENT )
309 : // set a dummy gradient, the correct gradient
310 : // will be created dynamically in GetGradient()
311 27166 : SetGradient( ImplGetApplicationGradient() );
312 :
313 27166 : mpImplWallpaper->meStyle = eStyle;
314 27166 : }
315 :
316 : // -----------------------------------------------------------------------
317 :
318 259883 : WallpaperStyle Wallpaper::GetStyle() const
319 : {
320 : DBG_CHKTHIS( Wallpaper, NULL );
321 :
322 259883 : return mpImplWallpaper->meStyle;
323 : }
324 :
325 : // -----------------------------------------------------------------------
326 :
327 0 : void Wallpaper::SetBitmap( const BitmapEx& rBitmap )
328 : {
329 : DBG_CHKTHIS( Wallpaper, NULL );
330 :
331 0 : if ( !rBitmap )
332 : {
333 0 : if ( mpImplWallpaper->mpBitmap )
334 : {
335 0 : ImplMakeUnique();
336 0 : delete mpImplWallpaper->mpBitmap;
337 0 : mpImplWallpaper->mpBitmap = NULL;
338 : }
339 : }
340 : else
341 : {
342 0 : ImplMakeUnique();
343 0 : if ( mpImplWallpaper->mpBitmap )
344 0 : *(mpImplWallpaper->mpBitmap) = rBitmap;
345 : else
346 0 : mpImplWallpaper->mpBitmap = new BitmapEx( rBitmap );
347 : }
348 :
349 0 : if( WALLPAPER_NULL == mpImplWallpaper->meStyle || WALLPAPER_APPLICATIONGRADIENT == mpImplWallpaper->meStyle)
350 0 : mpImplWallpaper->meStyle = WALLPAPER_TILE;
351 0 : }
352 :
353 : // -----------------------------------------------------------------------
354 :
355 0 : BitmapEx Wallpaper::GetBitmap() const
356 : {
357 : DBG_CHKTHIS( Wallpaper, NULL );
358 :
359 0 : if ( mpImplWallpaper->mpBitmap )
360 0 : return *(mpImplWallpaper->mpBitmap);
361 : else
362 : {
363 0 : BitmapEx aBmp;
364 0 : return aBmp;
365 : }
366 : }
367 :
368 : // -----------------------------------------------------------------------
369 :
370 141781 : sal_Bool Wallpaper::IsBitmap() const
371 : {
372 : DBG_CHKTHIS( Wallpaper, NULL );
373 :
374 141781 : return (mpImplWallpaper->mpBitmap != 0);
375 : }
376 :
377 :
378 : // -----------------------------------------------------------------------
379 :
380 27166 : void Wallpaper::SetGradient( const Gradient& rGradient )
381 : {
382 : DBG_CHKTHIS( Wallpaper, NULL );
383 :
384 27166 : ImplMakeUnique();
385 :
386 27166 : if ( mpImplWallpaper->mpGradient )
387 0 : *(mpImplWallpaper->mpGradient) = rGradient;
388 : else
389 27166 : mpImplWallpaper->mpGradient = new Gradient( rGradient );
390 :
391 27166 : if( WALLPAPER_NULL == mpImplWallpaper->meStyle || WALLPAPER_APPLICATIONGRADIENT == mpImplWallpaper->meStyle )
392 27166 : mpImplWallpaper->meStyle = WALLPAPER_TILE;
393 27166 : }
394 :
395 : // -----------------------------------------------------------------------
396 :
397 2873 : Gradient Wallpaper::GetGradient() const
398 : {
399 : DBG_CHKTHIS( Wallpaper, NULL );
400 :
401 2873 : if( WALLPAPER_APPLICATIONGRADIENT == mpImplWallpaper->meStyle )
402 2873 : return ImplGetApplicationGradient();
403 0 : else if ( mpImplWallpaper->mpGradient )
404 0 : return *(mpImplWallpaper->mpGradient);
405 : else
406 : {
407 0 : Gradient aGradient;
408 0 : return aGradient;
409 : }
410 : }
411 :
412 : // -----------------------------------------------------------------------
413 :
414 233993 : sal_Bool Wallpaper::IsGradient() const
415 : {
416 : DBG_CHKTHIS( Wallpaper, NULL );
417 :
418 233993 : return (mpImplWallpaper->mpGradient != 0);
419 : }
420 :
421 :
422 : // -----------------------------------------------------------------------
423 :
424 30039 : Gradient Wallpaper::ImplGetApplicationGradient() const
425 : {
426 30039 : Gradient g;
427 30039 : g.SetAngle( 900 );
428 30039 : g.SetStyle( GradientStyle_LINEAR );
429 30039 : g.SetStartColor( Application::GetSettings().GetStyleSettings().GetFaceColor() );
430 : // no 'extreme' gradient when high contrast
431 30039 : if( Application::GetSettings().GetStyleSettings().GetHighContrastMode() )
432 0 : g.SetEndColor( Application::GetSettings().GetStyleSettings().GetFaceColor() );
433 : else
434 30039 : g.SetEndColor( Application::GetSettings().GetStyleSettings().GetFaceGradientColor() );
435 30039 : return g;
436 : }
437 :
438 : // -----------------------------------------------------------------------
439 :
440 0 : void Wallpaper::SetRect( const Rectangle& rRect )
441 : {
442 : DBG_CHKTHIS( Wallpaper, NULL );
443 :
444 0 : ImplMakeUnique( sal_False );
445 :
446 0 : if ( rRect.IsEmpty() )
447 : {
448 0 : if ( mpImplWallpaper->mpRect )
449 : {
450 0 : delete mpImplWallpaper->mpRect;
451 0 : mpImplWallpaper->mpRect = NULL;
452 : }
453 : }
454 : else
455 : {
456 0 : if ( mpImplWallpaper->mpRect )
457 0 : *(mpImplWallpaper->mpRect) = rRect;
458 : else
459 0 : mpImplWallpaper->mpRect = new Rectangle( rRect );
460 : }
461 0 : }
462 :
463 : // -----------------------------------------------------------------------
464 :
465 0 : Rectangle Wallpaper::GetRect() const
466 : {
467 : DBG_CHKTHIS( Wallpaper, NULL );
468 :
469 0 : if ( mpImplWallpaper->mpRect )
470 0 : return *(mpImplWallpaper->mpRect);
471 : else
472 : {
473 0 : Rectangle aRect;
474 0 : return aRect;
475 : }
476 : }
477 :
478 : // -----------------------------------------------------------------------
479 :
480 0 : sal_Bool Wallpaper::IsRect() const
481 : {
482 : DBG_CHKTHIS( Wallpaper, NULL );
483 :
484 0 : return (mpImplWallpaper->mpRect != 0);
485 : }
486 :
487 :
488 : // -----------------------------------------------------------------------
489 :
490 0 : sal_Bool Wallpaper::IsFixed() const
491 : {
492 0 : if ( mpImplWallpaper->meStyle == WALLPAPER_NULL )
493 0 : return sal_False;
494 : else
495 0 : return (!mpImplWallpaper->mpBitmap && !mpImplWallpaper->mpGradient);
496 : }
497 :
498 : // -----------------------------------------------------------------------
499 :
500 0 : sal_Bool Wallpaper::IsScrollable() const
501 : {
502 0 : if ( mpImplWallpaper->meStyle == WALLPAPER_NULL )
503 0 : return sal_False;
504 0 : else if ( !mpImplWallpaper->mpBitmap && !mpImplWallpaper->mpGradient )
505 0 : return sal_True;
506 0 : else if ( mpImplWallpaper->mpBitmap )
507 0 : return (mpImplWallpaper->meStyle == WALLPAPER_TILE);
508 : else
509 0 : return sal_False;
510 : }
511 :
512 : // -----------------------------------------------------------------------
513 :
514 263203 : Wallpaper& Wallpaper::operator=( const Wallpaper& rWallpaper )
515 : {
516 : DBG_CHKTHIS( Wallpaper, NULL );
517 : DBG_CHKOBJ( &rWallpaper, Wallpaper, NULL );
518 : DBG_ASSERT( rWallpaper.mpImplWallpaper->mnRefCount < 0xFFFFFFFE, "Wallpaper: RefCount overflow" );
519 :
520 : // Zuerst Referenzcounter erhoehen, damit man sich selbst zuweisen kann
521 263203 : if ( rWallpaper.mpImplWallpaper->mnRefCount )
522 231928 : rWallpaper.mpImplWallpaper->mnRefCount++;
523 :
524 : // Wenn es keine statischen ImpDaten sind, dann loeschen, wenn es
525 : // die letzte Referenz ist, sonst Referenzcounter decrementieren
526 263203 : if ( mpImplWallpaper->mnRefCount )
527 : {
528 117756 : if ( mpImplWallpaper->mnRefCount == 1 )
529 116660 : delete mpImplWallpaper;
530 : else
531 1096 : mpImplWallpaper->mnRefCount--;
532 : }
533 :
534 263203 : mpImplWallpaper = rWallpaper.mpImplWallpaper;
535 :
536 263203 : return *this;
537 : }
538 :
539 : // -----------------------------------------------------------------------
540 :
541 0 : sal_Bool Wallpaper::operator==( const Wallpaper& rWallpaper ) const
542 : {
543 : DBG_CHKTHIS( Wallpaper, NULL );
544 : DBG_CHKOBJ( &rWallpaper, Wallpaper, NULL );
545 :
546 0 : if ( mpImplWallpaper == rWallpaper.mpImplWallpaper )
547 0 : return sal_True;
548 :
549 0 : if ( ( mpImplWallpaper->meStyle != rWallpaper.mpImplWallpaper->meStyle ) ||
550 0 : ( mpImplWallpaper->maColor != rWallpaper.mpImplWallpaper->maColor ) )
551 0 : return sal_False;
552 :
553 0 : if ( mpImplWallpaper->mpRect != rWallpaper.mpImplWallpaper->mpRect
554 0 : && ( !mpImplWallpaper->mpRect
555 0 : || !rWallpaper.mpImplWallpaper->mpRect
556 0 : || *(mpImplWallpaper->mpRect) != *(rWallpaper.mpImplWallpaper->mpRect) ) )
557 0 : return sal_False;
558 :
559 0 : if ( mpImplWallpaper->mpBitmap != rWallpaper.mpImplWallpaper->mpBitmap
560 0 : && ( !mpImplWallpaper->mpBitmap
561 0 : || !rWallpaper.mpImplWallpaper->mpBitmap
562 0 : || *(mpImplWallpaper->mpBitmap) != *(rWallpaper.mpImplWallpaper->mpBitmap) ) )
563 0 : return sal_False;
564 :
565 0 : if ( mpImplWallpaper->mpGradient != rWallpaper.mpImplWallpaper->mpGradient
566 0 : && ( !mpImplWallpaper->mpGradient
567 0 : || !rWallpaper.mpImplWallpaper->mpGradient
568 0 : || *(mpImplWallpaper->mpGradient) != *(rWallpaper.mpImplWallpaper->mpGradient) ) )
569 0 : return sal_False;
570 :
571 0 : return sal_True;
572 : }
573 :
574 : // -----------------------------------------------------------------------
575 :
576 0 : SvStream& operator>>( SvStream& rIStm, Wallpaper& rWallpaper )
577 : {
578 0 : rWallpaper.ImplMakeUnique();
579 0 : return( rIStm >> *rWallpaper.mpImplWallpaper );
580 : }
581 :
582 : // -----------------------------------------------------------------------
583 :
584 0 : SvStream& operator<<( SvStream& rOStm, const Wallpaper& rWallpaper )
585 : {
586 0 : return( rOStm << *rWallpaper.mpImplWallpaper );
587 465 : }
588 :
589 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|