update credits
[LibreOffice.git] / vcl / source / gdi / wall.cxx
blob5c2522de02db7f3e1a193a6c06fe928b304d44a8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <tools/stream.hxx>
22 #include <tools/vcompat.hxx>
23 #include <tools/debug.hxx>
25 #include <vcl/bitmapex.hxx>
26 #include <vcl/gradient.hxx>
27 #include <vcl/wall.hxx>
28 #include <vcl/svapp.hxx>
30 #include <wall2.hxx>
33 DBG_NAME( Wallpaper )
35 // -----------------------------------------------------------------------
37 ImplWallpaper::ImplWallpaper() :
38 maColor( COL_TRANSPARENT )
40 mnRefCount = 1;
41 mpBitmap = NULL;
42 mpCache = NULL;
43 mpGradient = NULL;
44 mpRect = NULL;
45 meStyle = WALLPAPER_NULL;
48 // -----------------------------------------------------------------------
50 ImplWallpaper::ImplWallpaper( const ImplWallpaper& rImplWallpaper ) :
51 maColor( rImplWallpaper.maColor )
53 mnRefCount = 1;
54 meStyle = rImplWallpaper.meStyle;
56 if ( rImplWallpaper.mpBitmap )
57 mpBitmap = new BitmapEx( *rImplWallpaper.mpBitmap );
58 else
59 mpBitmap = NULL;
60 if( rImplWallpaper.mpCache )
61 mpCache = new BitmapEx( *rImplWallpaper.mpCache );
62 else
63 mpCache = NULL;
64 if ( rImplWallpaper.mpGradient )
65 mpGradient = new Gradient( *rImplWallpaper.mpGradient );
66 else
67 mpGradient = NULL;
68 if ( rImplWallpaper.mpRect )
69 mpRect = new Rectangle( *rImplWallpaper.mpRect );
70 else
71 mpRect = NULL;
74 // -----------------------------------------------------------------------
76 ImplWallpaper::~ImplWallpaper()
78 delete mpBitmap;
79 delete mpCache;
80 delete mpGradient;
81 delete mpRect;
84 // -----------------------------------------------------------------------
86 void ImplWallpaper::ImplSetCachedBitmap( BitmapEx& rBmp )
88 if( !mpCache )
89 mpCache = new BitmapEx( rBmp );
90 else
91 *mpCache = rBmp;
94 // -----------------------------------------------------------------------
96 void ImplWallpaper::ImplReleaseCachedBitmap()
98 delete mpCache;
99 mpCache = NULL;
102 // -----------------------------------------------------------------------
104 SvStream& operator>>( SvStream& rIStm, ImplWallpaper& rImplWallpaper )
106 VersionCompat aCompat( rIStm, STREAM_READ );
107 sal_uInt16 nTmp16;
109 delete rImplWallpaper.mpRect;
110 rImplWallpaper.mpRect = NULL;
112 delete rImplWallpaper.mpGradient;
113 rImplWallpaper.mpGradient = NULL;
115 delete rImplWallpaper.mpBitmap;
116 rImplWallpaper.mpBitmap = NULL;
118 // version 1
119 rIStm >> rImplWallpaper.maColor;
120 rIStm >> nTmp16; rImplWallpaper.meStyle = (WallpaperStyle) nTmp16;
122 // version 2
123 if( aCompat.GetVersion() >= 2 )
125 sal_Bool bRect, bGrad, bBmp, bDummy;
127 rIStm >> bRect >> bGrad >> bBmp >> bDummy >> bDummy >> bDummy;
129 if( bRect )
131 rImplWallpaper.mpRect = new Rectangle;
132 rIStm >> *rImplWallpaper.mpRect;
135 if( bGrad )
137 rImplWallpaper.mpGradient = new Gradient;
138 rIStm >> *rImplWallpaper.mpGradient;
141 if( bBmp )
143 rImplWallpaper.mpBitmap = new BitmapEx;
144 rIStm >> *rImplWallpaper.mpBitmap;
147 // version 3 (new color format)
148 if( aCompat.GetVersion() >= 3 )
150 rImplWallpaper.maColor.Read( rIStm, sal_True );
154 return rIStm;
157 // -----------------------------------------------------------------------
159 SvStream& operator<<( SvStream& rOStm, const ImplWallpaper& rImplWallpaper )
161 VersionCompat aCompat( rOStm, STREAM_WRITE, 3 );
162 sal_Bool bRect = ( rImplWallpaper.mpRect != NULL );
163 sal_Bool bGrad = ( rImplWallpaper.mpGradient != NULL );
164 sal_Bool bBmp = ( rImplWallpaper.mpBitmap != NULL );
165 sal_Bool bDummy = sal_False;
167 // version 1
168 rOStm << rImplWallpaper.maColor << (sal_uInt16) rImplWallpaper.meStyle;
170 // version 2
171 rOStm << bRect << bGrad << bBmp << bDummy << bDummy << bDummy;
173 if( bRect )
174 rOStm << *rImplWallpaper.mpRect;
176 if( bGrad )
177 rOStm << *rImplWallpaper.mpGradient;
179 if( bBmp )
180 rOStm << *rImplWallpaper.mpBitmap;
182 // version 3 (new color format)
183 ( (Color&) rImplWallpaper.maColor ).Write( rOStm, sal_True );
185 return rOStm;
188 // -----------------------------------------------------------------------
190 inline void Wallpaper::ImplMakeUnique( sal_Bool bReleaseCache )
192 // Falls noch andere Referenzen bestehen, dann kopieren
193 if ( mpImplWallpaper->mnRefCount != 1 )
195 if ( mpImplWallpaper->mnRefCount )
196 mpImplWallpaper->mnRefCount--;
197 mpImplWallpaper = new ImplWallpaper( *(mpImplWallpaper) );
200 if( bReleaseCache )
201 mpImplWallpaper->ImplReleaseCachedBitmap();
204 // -----------------------------------------------------------------------
206 Wallpaper::Wallpaper()
208 DBG_CTOR( Wallpaper, NULL );
210 static ImplWallpaper aStaticImplWallpaper;
212 aStaticImplWallpaper.mnRefCount = 0;
213 mpImplWallpaper = &aStaticImplWallpaper;
216 // -----------------------------------------------------------------------
218 Wallpaper::Wallpaper( const Wallpaper& rWallpaper )
220 DBG_CTOR( Wallpaper, NULL );
221 DBG_CHKOBJ( &rWallpaper, Wallpaper, NULL );
222 DBG_ASSERT( rWallpaper.mpImplWallpaper->mnRefCount < 0xFFFFFFFE, "Wallpaper: RefCount overflow" );
224 // Instance Daten uebernehmen und Referenzcounter erhoehen
225 mpImplWallpaper = rWallpaper.mpImplWallpaper;
226 // RefCount == 0 fuer statische Objekte
227 if ( mpImplWallpaper->mnRefCount )
228 mpImplWallpaper->mnRefCount++;
231 // -----------------------------------------------------------------------
233 Wallpaper::Wallpaper( const Color& rColor )
235 DBG_CTOR( Wallpaper, NULL );
237 mpImplWallpaper = new ImplWallpaper;
238 mpImplWallpaper->maColor = rColor;
239 mpImplWallpaper->meStyle = WALLPAPER_TILE;
242 // -----------------------------------------------------------------------
244 Wallpaper::Wallpaper( const BitmapEx& rBmpEx )
246 DBG_CTOR( Wallpaper, NULL );
248 mpImplWallpaper = new ImplWallpaper;
249 mpImplWallpaper->mpBitmap = new BitmapEx( rBmpEx );
250 mpImplWallpaper->meStyle = WALLPAPER_TILE;
253 // -----------------------------------------------------------------------
255 Wallpaper::Wallpaper( const Gradient& rGradient )
257 DBG_CTOR( Wallpaper, NULL );
259 mpImplWallpaper = new ImplWallpaper;
260 mpImplWallpaper->mpGradient = new Gradient( rGradient );
261 mpImplWallpaper->meStyle = WALLPAPER_TILE;
264 // -----------------------------------------------------------------------
266 Wallpaper::~Wallpaper()
268 DBG_DTOR( Wallpaper, NULL );
270 // Wenn es keine statischen ImpDaten sind, dann loeschen, wenn es
271 // die letzte Referenz ist, sonst Referenzcounter decrementieren
272 if ( mpImplWallpaper->mnRefCount )
274 if ( mpImplWallpaper->mnRefCount == 1 )
275 delete mpImplWallpaper;
276 else
277 mpImplWallpaper->mnRefCount--;
281 // -----------------------------------------------------------------------
283 void Wallpaper::SetColor( const Color& rColor )
285 DBG_CHKTHIS( Wallpaper, NULL );
287 ImplMakeUnique();
288 mpImplWallpaper->maColor = rColor;
290 if( WALLPAPER_NULL == mpImplWallpaper->meStyle || WALLPAPER_APPLICATIONGRADIENT == mpImplWallpaper->meStyle )
291 mpImplWallpaper->meStyle = WALLPAPER_TILE;
294 // -----------------------------------------------------------------------
296 const Color& Wallpaper::GetColor() const
298 DBG_CHKTHIS( Wallpaper, NULL );
300 return mpImplWallpaper->maColor;
303 // -----------------------------------------------------------------------
305 void Wallpaper::SetStyle( WallpaperStyle eStyle )
307 DBG_CHKTHIS( Wallpaper, NULL );
309 ImplMakeUnique( sal_False );
311 if( eStyle == WALLPAPER_APPLICATIONGRADIENT )
312 // set a dummy gradient, the correct gradient
313 // will be created dynamically in GetGradient()
314 SetGradient( ImplGetApplicationGradient() );
316 mpImplWallpaper->meStyle = eStyle;
319 // -----------------------------------------------------------------------
321 WallpaperStyle Wallpaper::GetStyle() const
323 DBG_CHKTHIS( Wallpaper, NULL );
325 return mpImplWallpaper->meStyle;
328 // -----------------------------------------------------------------------
330 void Wallpaper::SetBitmap( const BitmapEx& rBitmap )
332 DBG_CHKTHIS( Wallpaper, NULL );
334 if ( !rBitmap )
336 if ( mpImplWallpaper->mpBitmap )
338 ImplMakeUnique();
339 delete mpImplWallpaper->mpBitmap;
340 mpImplWallpaper->mpBitmap = NULL;
343 else
345 ImplMakeUnique();
346 if ( mpImplWallpaper->mpBitmap )
347 *(mpImplWallpaper->mpBitmap) = rBitmap;
348 else
349 mpImplWallpaper->mpBitmap = new BitmapEx( rBitmap );
352 if( WALLPAPER_NULL == mpImplWallpaper->meStyle || WALLPAPER_APPLICATIONGRADIENT == mpImplWallpaper->meStyle)
353 mpImplWallpaper->meStyle = WALLPAPER_TILE;
356 // -----------------------------------------------------------------------
358 BitmapEx Wallpaper::GetBitmap() const
360 DBG_CHKTHIS( Wallpaper, NULL );
362 if ( mpImplWallpaper->mpBitmap )
363 return *(mpImplWallpaper->mpBitmap);
364 else
366 BitmapEx aBmp;
367 return aBmp;
371 // -----------------------------------------------------------------------
373 sal_Bool Wallpaper::IsBitmap() const
375 DBG_CHKTHIS( Wallpaper, NULL );
377 return (mpImplWallpaper->mpBitmap != 0);
381 // -----------------------------------------------------------------------
383 void Wallpaper::SetGradient( const Gradient& rGradient )
385 DBG_CHKTHIS( Wallpaper, NULL );
387 ImplMakeUnique();
389 if ( mpImplWallpaper->mpGradient )
390 *(mpImplWallpaper->mpGradient) = rGradient;
391 else
392 mpImplWallpaper->mpGradient = new Gradient( rGradient );
394 if( WALLPAPER_NULL == mpImplWallpaper->meStyle || WALLPAPER_APPLICATIONGRADIENT == mpImplWallpaper->meStyle )
395 mpImplWallpaper->meStyle = WALLPAPER_TILE;
398 // -----------------------------------------------------------------------
400 Gradient Wallpaper::GetGradient() const
402 DBG_CHKTHIS( Wallpaper, NULL );
404 if( WALLPAPER_APPLICATIONGRADIENT == mpImplWallpaper->meStyle )
405 return ImplGetApplicationGradient();
406 else if ( mpImplWallpaper->mpGradient )
407 return *(mpImplWallpaper->mpGradient);
408 else
410 Gradient aGradient;
411 return aGradient;
415 // -----------------------------------------------------------------------
417 sal_Bool Wallpaper::IsGradient() const
419 DBG_CHKTHIS( Wallpaper, NULL );
421 return (mpImplWallpaper->mpGradient != 0);
425 // -----------------------------------------------------------------------
427 Gradient Wallpaper::ImplGetApplicationGradient() const
429 Gradient g;
430 g.SetAngle( 900 );
431 g.SetStyle( GradientStyle_LINEAR );
432 g.SetStartColor( Application::GetSettings().GetStyleSettings().GetFaceColor() );
433 // no 'extreme' gradient when high contrast
434 if( Application::GetSettings().GetStyleSettings().GetHighContrastMode() )
435 g.SetEndColor( Application::GetSettings().GetStyleSettings().GetFaceColor() );
436 else
437 g.SetEndColor( Application::GetSettings().GetStyleSettings().GetFaceGradientColor() );
438 return g;
441 // -----------------------------------------------------------------------
443 void Wallpaper::SetRect( const Rectangle& rRect )
445 DBG_CHKTHIS( Wallpaper, NULL );
447 ImplMakeUnique( sal_False );
449 if ( rRect.IsEmpty() )
451 if ( mpImplWallpaper->mpRect )
453 delete mpImplWallpaper->mpRect;
454 mpImplWallpaper->mpRect = NULL;
457 else
459 if ( mpImplWallpaper->mpRect )
460 *(mpImplWallpaper->mpRect) = rRect;
461 else
462 mpImplWallpaper->mpRect = new Rectangle( rRect );
466 // -----------------------------------------------------------------------
468 Rectangle Wallpaper::GetRect() const
470 DBG_CHKTHIS( Wallpaper, NULL );
472 if ( mpImplWallpaper->mpRect )
473 return *(mpImplWallpaper->mpRect);
474 else
476 Rectangle aRect;
477 return aRect;
481 // -----------------------------------------------------------------------
483 sal_Bool Wallpaper::IsRect() const
485 DBG_CHKTHIS( Wallpaper, NULL );
487 return (mpImplWallpaper->mpRect != 0);
491 // -----------------------------------------------------------------------
493 sal_Bool Wallpaper::IsFixed() const
495 if ( mpImplWallpaper->meStyle == WALLPAPER_NULL )
496 return sal_False;
497 else
498 return (!mpImplWallpaper->mpBitmap && !mpImplWallpaper->mpGradient);
501 // -----------------------------------------------------------------------
503 sal_Bool Wallpaper::IsScrollable() const
505 if ( mpImplWallpaper->meStyle == WALLPAPER_NULL )
506 return sal_False;
507 else if ( !mpImplWallpaper->mpBitmap && !mpImplWallpaper->mpGradient )
508 return sal_True;
509 else if ( mpImplWallpaper->mpBitmap )
510 return (mpImplWallpaper->meStyle == WALLPAPER_TILE);
511 else
512 return sal_False;
515 // -----------------------------------------------------------------------
517 Wallpaper& Wallpaper::operator=( const Wallpaper& rWallpaper )
519 DBG_CHKTHIS( Wallpaper, NULL );
520 DBG_CHKOBJ( &rWallpaper, Wallpaper, NULL );
521 DBG_ASSERT( rWallpaper.mpImplWallpaper->mnRefCount < 0xFFFFFFFE, "Wallpaper: RefCount overflow" );
523 // Zuerst Referenzcounter erhoehen, damit man sich selbst zuweisen kann
524 if ( rWallpaper.mpImplWallpaper->mnRefCount )
525 rWallpaper.mpImplWallpaper->mnRefCount++;
527 // Wenn es keine statischen ImpDaten sind, dann loeschen, wenn es
528 // die letzte Referenz ist, sonst Referenzcounter decrementieren
529 if ( mpImplWallpaper->mnRefCount )
531 if ( mpImplWallpaper->mnRefCount == 1 )
532 delete mpImplWallpaper;
533 else
534 mpImplWallpaper->mnRefCount--;
537 mpImplWallpaper = rWallpaper.mpImplWallpaper;
539 return *this;
542 // -----------------------------------------------------------------------
544 sal_Bool Wallpaper::operator==( const Wallpaper& rWallpaper ) const
546 DBG_CHKTHIS( Wallpaper, NULL );
547 DBG_CHKOBJ( &rWallpaper, Wallpaper, NULL );
549 if ( mpImplWallpaper == rWallpaper.mpImplWallpaper )
550 return sal_True;
552 if ( ( mpImplWallpaper->meStyle != rWallpaper.mpImplWallpaper->meStyle ) ||
553 ( mpImplWallpaper->maColor != rWallpaper.mpImplWallpaper->maColor ) )
554 return sal_False;
556 if ( mpImplWallpaper->mpRect != rWallpaper.mpImplWallpaper->mpRect
557 && ( !mpImplWallpaper->mpRect
558 || !rWallpaper.mpImplWallpaper->mpRect
559 || *(mpImplWallpaper->mpRect) != *(rWallpaper.mpImplWallpaper->mpRect) ) )
560 return sal_False;
562 if ( mpImplWallpaper->mpBitmap != rWallpaper.mpImplWallpaper->mpBitmap
563 && ( !mpImplWallpaper->mpBitmap
564 || !rWallpaper.mpImplWallpaper->mpBitmap
565 || *(mpImplWallpaper->mpBitmap) != *(rWallpaper.mpImplWallpaper->mpBitmap) ) )
566 return sal_False;
568 if ( mpImplWallpaper->mpGradient != rWallpaper.mpImplWallpaper->mpGradient
569 && ( !mpImplWallpaper->mpGradient
570 || !rWallpaper.mpImplWallpaper->mpGradient
571 || *(mpImplWallpaper->mpGradient) != *(rWallpaper.mpImplWallpaper->mpGradient) ) )
572 return sal_False;
574 return sal_True;
577 // -----------------------------------------------------------------------
579 SvStream& operator>>( SvStream& rIStm, Wallpaper& rWallpaper )
581 rWallpaper.ImplMakeUnique();
582 return( rIStm >> *rWallpaper.mpImplWallpaper );
585 // -----------------------------------------------------------------------
587 SvStream& operator<<( SvStream& rOStm, const Wallpaper& rWallpaper )
589 return( rOStm << *rWallpaper.mpImplWallpaper );
592 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */