merge the formfield patch from ooo-build
[ooovba.git] / vcl / source / gdi / wall.cxx
blobd60d436b0b318b671b114170e9e12dfd6c000c49
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: wall.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
33 #include <tools/stream.hxx>
34 #include <tools/vcompat.hxx>
35 #include <tools/debug.hxx>
36 #include <vcl/bitmapex.hxx>
37 #include <vcl/gradient.hxx>
38 #include <vcl/wall.hxx>
39 #include <vcl/wall2.hxx>
40 #include <vcl/svapp.hxx>
43 DBG_NAME( Wallpaper );
45 // -----------------------------------------------------------------------
47 ImplWallpaper::ImplWallpaper() :
48 maColor( COL_TRANSPARENT )
50 mnRefCount = 1;
51 mpBitmap = NULL;
52 mpCache = NULL;
53 mpGradient = NULL;
54 mpRect = NULL;
55 meStyle = WALLPAPER_NULL;
58 // -----------------------------------------------------------------------
60 ImplWallpaper::ImplWallpaper( const ImplWallpaper& rImplWallpaper ) :
61 maColor( rImplWallpaper.maColor )
63 mnRefCount = 1;
64 meStyle = rImplWallpaper.meStyle;
66 if ( rImplWallpaper.mpBitmap )
67 mpBitmap = new BitmapEx( *rImplWallpaper.mpBitmap );
68 else
69 mpBitmap = NULL;
70 if( rImplWallpaper.mpCache )
71 mpCache = new BitmapEx( *rImplWallpaper.mpCache );
72 else
73 mpCache = NULL;
74 if ( rImplWallpaper.mpGradient )
75 mpGradient = new Gradient( *rImplWallpaper.mpGradient );
76 else
77 mpGradient = NULL;
78 if ( rImplWallpaper.mpRect )
79 mpRect = new Rectangle( *rImplWallpaper.mpRect );
80 else
81 mpRect = NULL;
84 // -----------------------------------------------------------------------
86 ImplWallpaper::~ImplWallpaper()
88 delete mpBitmap;
89 delete mpCache;
90 delete mpGradient;
91 delete mpRect;
94 // -----------------------------------------------------------------------
96 void ImplWallpaper::ImplSetCachedBitmap( BitmapEx& rBmp )
98 if( !mpCache )
99 mpCache = new BitmapEx( rBmp );
100 else
101 *mpCache = rBmp;
104 // -----------------------------------------------------------------------
106 void ImplWallpaper::ImplReleaseCachedBitmap()
108 delete mpCache;
109 mpCache = NULL;
112 // -----------------------------------------------------------------------
114 SvStream& operator>>( SvStream& rIStm, ImplWallpaper& rImplWallpaper )
116 VersionCompat aCompat( rIStm, STREAM_READ );
117 UINT16 nTmp16;
119 delete rImplWallpaper.mpRect;
120 rImplWallpaper.mpRect = NULL;
122 delete rImplWallpaper.mpGradient;
123 rImplWallpaper.mpGradient = NULL;
125 delete rImplWallpaper.mpBitmap;
126 rImplWallpaper.mpBitmap = NULL;
128 // version 1
129 rIStm >> rImplWallpaper.maColor;
130 rIStm >> nTmp16; rImplWallpaper.meStyle = (WallpaperStyle) nTmp16;
132 // version 2
133 if( aCompat.GetVersion() >= 2 )
135 BOOL bRect, bGrad, bBmp, bDummy;
137 rIStm >> bRect >> bGrad >> bBmp >> bDummy >> bDummy >> bDummy;
139 if( bRect )
141 rImplWallpaper.mpRect = new Rectangle;
142 rIStm >> *rImplWallpaper.mpRect;
145 if( bGrad )
147 rImplWallpaper.mpGradient = new Gradient;
148 rIStm >> *rImplWallpaper.mpGradient;
151 if( bBmp )
153 rImplWallpaper.mpBitmap = new BitmapEx;
154 rIStm >> *rImplWallpaper.mpBitmap;
157 // version 3 (new color format)
158 if( aCompat.GetVersion() >= 3 )
160 rImplWallpaper.maColor.Read( rIStm, TRUE );
164 return rIStm;
167 // -----------------------------------------------------------------------
169 SvStream& operator<<( SvStream& rOStm, const ImplWallpaper& rImplWallpaper )
171 VersionCompat aCompat( rOStm, STREAM_WRITE, 3 );
172 BOOL bRect = ( rImplWallpaper.mpRect != NULL );
173 BOOL bGrad = ( rImplWallpaper.mpGradient != NULL );
174 BOOL bBmp = ( rImplWallpaper.mpBitmap != NULL );
175 BOOL bDummy = FALSE;
177 // version 1
178 rOStm << rImplWallpaper.maColor << (UINT16) rImplWallpaper.meStyle;
180 // version 2
181 rOStm << bRect << bGrad << bBmp << bDummy << bDummy << bDummy;
183 if( bRect )
184 rOStm << *rImplWallpaper.mpRect;
186 if( bGrad )
187 rOStm << *rImplWallpaper.mpGradient;
189 if( bBmp )
190 rOStm << *rImplWallpaper.mpBitmap;
192 // version 3 (new color format)
193 ( (Color&) rImplWallpaper.maColor ).Write( rOStm, TRUE );
195 return rOStm;
198 // -----------------------------------------------------------------------
200 inline void Wallpaper::ImplMakeUnique( BOOL bReleaseCache )
202 // Falls noch andere Referenzen bestehen, dann kopieren
203 if ( mpImplWallpaper->mnRefCount != 1 )
205 if ( mpImplWallpaper->mnRefCount )
206 mpImplWallpaper->mnRefCount--;
207 mpImplWallpaper = new ImplWallpaper( *(mpImplWallpaper) );
210 if( bReleaseCache )
211 mpImplWallpaper->ImplReleaseCachedBitmap();
214 // -----------------------------------------------------------------------
216 Wallpaper::Wallpaper()
218 DBG_CTOR( Wallpaper, NULL );
220 static ImplWallpaper aStaticImplWallpaper;
222 aStaticImplWallpaper.mnRefCount = 0;
223 mpImplWallpaper = &aStaticImplWallpaper;
226 // -----------------------------------------------------------------------
228 Wallpaper::Wallpaper( const Wallpaper& rWallpaper )
230 DBG_CTOR( Wallpaper, NULL );
231 DBG_CHKOBJ( &rWallpaper, Wallpaper, NULL );
232 DBG_ASSERT( rWallpaper.mpImplWallpaper->mnRefCount < 0xFFFFFFFE, "Wallpaper: RefCount overflow" );
234 // Instance Daten uebernehmen und Referenzcounter erhoehen
235 mpImplWallpaper = rWallpaper.mpImplWallpaper;
236 // RefCount == 0 fuer statische Objekte
237 if ( mpImplWallpaper->mnRefCount )
238 mpImplWallpaper->mnRefCount++;
241 // -----------------------------------------------------------------------
243 Wallpaper::Wallpaper( const Color& rColor )
245 DBG_CTOR( Wallpaper, NULL );
247 mpImplWallpaper = new ImplWallpaper;
248 mpImplWallpaper->maColor = rColor;
249 mpImplWallpaper->meStyle = WALLPAPER_TILE;
252 // -----------------------------------------------------------------------
254 Wallpaper::Wallpaper( const BitmapEx& rBmpEx )
256 DBG_CTOR( Wallpaper, NULL );
258 mpImplWallpaper = new ImplWallpaper;
259 mpImplWallpaper->mpBitmap = new BitmapEx( rBmpEx );
260 mpImplWallpaper->meStyle = WALLPAPER_TILE;
263 // -----------------------------------------------------------------------
265 Wallpaper::Wallpaper( const Gradient& rGradient )
267 DBG_CTOR( Wallpaper, NULL );
269 mpImplWallpaper = new ImplWallpaper;
270 mpImplWallpaper->mpGradient = new Gradient( rGradient );
271 mpImplWallpaper->meStyle = WALLPAPER_TILE;
274 // -----------------------------------------------------------------------
276 Wallpaper::~Wallpaper()
278 DBG_DTOR( Wallpaper, NULL );
280 // Wenn es keine statischen ImpDaten sind, dann loeschen, wenn es
281 // die letzte Referenz ist, sonst Referenzcounter decrementieren
282 if ( mpImplWallpaper->mnRefCount )
284 if ( mpImplWallpaper->mnRefCount == 1 )
285 delete mpImplWallpaper;
286 else
287 mpImplWallpaper->mnRefCount--;
291 // -----------------------------------------------------------------------
293 void Wallpaper::SetColor( const Color& rColor )
295 DBG_CHKTHIS( Wallpaper, NULL );
297 ImplMakeUnique();
298 mpImplWallpaper->maColor = rColor;
300 if( WALLPAPER_NULL == mpImplWallpaper->meStyle || WALLPAPER_APPLICATIONGRADIENT == mpImplWallpaper->meStyle )
301 mpImplWallpaper->meStyle = WALLPAPER_TILE;
304 // -----------------------------------------------------------------------
306 const Color& Wallpaper::GetColor() const
308 DBG_CHKTHIS( Wallpaper, NULL );
310 return mpImplWallpaper->maColor;
313 // -----------------------------------------------------------------------
315 void Wallpaper::SetStyle( WallpaperStyle eStyle )
317 DBG_CHKTHIS( Wallpaper, NULL );
319 ImplMakeUnique( FALSE );
321 if( eStyle == WALLPAPER_APPLICATIONGRADIENT )
322 // set a dummy gradient, the correct gradient
323 // will be created dynamically in GetGradient()
324 SetGradient( ImplGetApplicationGradient() );
326 mpImplWallpaper->meStyle = eStyle;
329 // -----------------------------------------------------------------------
331 WallpaperStyle Wallpaper::GetStyle() const
333 DBG_CHKTHIS( Wallpaper, NULL );
335 return mpImplWallpaper->meStyle;
338 // -----------------------------------------------------------------------
340 void Wallpaper::SetBitmap( const BitmapEx& rBitmap )
342 DBG_CHKTHIS( Wallpaper, NULL );
344 if ( !rBitmap )
346 if ( mpImplWallpaper->mpBitmap )
348 ImplMakeUnique();
349 delete mpImplWallpaper->mpBitmap;
350 mpImplWallpaper->mpBitmap = NULL;
353 else
355 ImplMakeUnique();
356 if ( mpImplWallpaper->mpBitmap )
357 *(mpImplWallpaper->mpBitmap) = rBitmap;
358 else
359 mpImplWallpaper->mpBitmap = new BitmapEx( rBitmap );
362 if( WALLPAPER_NULL == mpImplWallpaper->meStyle || WALLPAPER_APPLICATIONGRADIENT == mpImplWallpaper->meStyle)
363 mpImplWallpaper->meStyle = WALLPAPER_TILE;
366 // -----------------------------------------------------------------------
368 void Wallpaper::SetBitmap()
370 DBG_CHKTHIS( Wallpaper, NULL );
372 if ( mpImplWallpaper->mpBitmap )
374 ImplMakeUnique();
375 delete mpImplWallpaper->mpBitmap;
376 mpImplWallpaper->mpBitmap = NULL;
380 // -----------------------------------------------------------------------
382 BitmapEx Wallpaper::GetBitmap() const
384 DBG_CHKTHIS( Wallpaper, NULL );
386 if ( mpImplWallpaper->mpBitmap )
387 return *(mpImplWallpaper->mpBitmap);
388 else
390 BitmapEx aBmp;
391 return aBmp;
395 // -----------------------------------------------------------------------
397 BOOL Wallpaper::IsBitmap() const
399 DBG_CHKTHIS( Wallpaper, NULL );
401 return (mpImplWallpaper->mpBitmap != 0);
405 // -----------------------------------------------------------------------
407 void Wallpaper::SetGradient( const Gradient& rGradient )
409 DBG_CHKTHIS( Wallpaper, NULL );
411 ImplMakeUnique();
413 if ( mpImplWallpaper->mpGradient )
414 *(mpImplWallpaper->mpGradient) = rGradient;
415 else
416 mpImplWallpaper->mpGradient = new Gradient( rGradient );
418 if( WALLPAPER_NULL == mpImplWallpaper->meStyle || WALLPAPER_APPLICATIONGRADIENT == mpImplWallpaper->meStyle )
419 mpImplWallpaper->meStyle = WALLPAPER_TILE;
422 // -----------------------------------------------------------------------
424 void Wallpaper::SetGradient()
426 DBG_CHKTHIS( Wallpaper, NULL );
428 if ( mpImplWallpaper->mpGradient )
430 ImplMakeUnique();
431 delete mpImplWallpaper->mpGradient;
432 mpImplWallpaper->mpGradient = NULL;
436 // -----------------------------------------------------------------------
438 Gradient Wallpaper::GetGradient() const
440 DBG_CHKTHIS( Wallpaper, NULL );
442 if( WALLPAPER_APPLICATIONGRADIENT == mpImplWallpaper->meStyle )
443 return ImplGetApplicationGradient();
444 else if ( mpImplWallpaper->mpGradient )
445 return *(mpImplWallpaper->mpGradient);
446 else
448 Gradient aGradient;
449 return aGradient;
453 // -----------------------------------------------------------------------
455 BOOL Wallpaper::IsGradient() const
457 DBG_CHKTHIS( Wallpaper, NULL );
459 return (mpImplWallpaper->mpGradient != 0);
463 // -----------------------------------------------------------------------
465 Gradient Wallpaper::ImplGetApplicationGradient() const
467 Gradient g;
468 g.SetAngle( 900 );
469 g.SetStyle( GRADIENT_LINEAR );
470 g.SetStartColor( Application::GetSettings().GetStyleSettings().GetFaceColor() );
471 // no 'extreme' gradient when high contrast
472 if( Application::GetSettings().GetStyleSettings().GetHighContrastMode() )
473 g.SetEndColor( Application::GetSettings().GetStyleSettings().GetFaceColor() );
474 else
475 g.SetEndColor( Application::GetSettings().GetStyleSettings().GetFaceGradientColor() );
476 return g;
479 // -----------------------------------------------------------------------
481 void Wallpaper::SetRect( const Rectangle& rRect )
483 DBG_CHKTHIS( Wallpaper, NULL );
485 ImplMakeUnique( FALSE );
487 if ( rRect.IsEmpty() )
489 if ( mpImplWallpaper->mpRect )
491 delete mpImplWallpaper->mpRect;
492 mpImplWallpaper->mpRect = NULL;
495 else
497 if ( mpImplWallpaper->mpRect )
498 *(mpImplWallpaper->mpRect) = rRect;
499 else
500 mpImplWallpaper->mpRect = new Rectangle( rRect );
504 // -----------------------------------------------------------------------
506 void Wallpaper::SetRect()
508 DBG_CHKTHIS( Wallpaper, NULL );
510 if ( mpImplWallpaper->mpRect )
512 ImplMakeUnique( FALSE );
513 delete mpImplWallpaper->mpRect;
514 mpImplWallpaper->mpRect = NULL;
518 // -----------------------------------------------------------------------
520 Rectangle Wallpaper::GetRect() const
522 DBG_CHKTHIS( Wallpaper, NULL );
524 if ( mpImplWallpaper->mpRect )
525 return *(mpImplWallpaper->mpRect);
526 else
528 Rectangle aRect;
529 return aRect;
533 // -----------------------------------------------------------------------
535 BOOL Wallpaper::IsRect() const
537 DBG_CHKTHIS( Wallpaper, NULL );
539 return (mpImplWallpaper->mpRect != 0);
543 // -----------------------------------------------------------------------
545 BOOL Wallpaper::IsFixed() const
547 if ( mpImplWallpaper->meStyle == WALLPAPER_NULL )
548 return FALSE;
549 else
550 return (!mpImplWallpaper->mpBitmap && !mpImplWallpaper->mpGradient);
553 // -----------------------------------------------------------------------
555 BOOL Wallpaper::IsScrollable() const
557 if ( mpImplWallpaper->meStyle == WALLPAPER_NULL )
558 return FALSE;
559 else if ( !mpImplWallpaper->mpBitmap && !mpImplWallpaper->mpGradient )
560 return TRUE;
561 else if ( mpImplWallpaper->mpBitmap )
562 return (mpImplWallpaper->meStyle == WALLPAPER_TILE);
563 else
564 return FALSE;
567 // -----------------------------------------------------------------------
569 Wallpaper& Wallpaper::operator=( const Wallpaper& rWallpaper )
571 DBG_CHKTHIS( Wallpaper, NULL );
572 DBG_CHKOBJ( &rWallpaper, Wallpaper, NULL );
573 DBG_ASSERT( rWallpaper.mpImplWallpaper->mnRefCount < 0xFFFFFFFE, "Wallpaper: RefCount overflow" );
575 // Zuerst Referenzcounter erhoehen, damit man sich selbst zuweisen kann
576 if ( rWallpaper.mpImplWallpaper->mnRefCount )
577 rWallpaper.mpImplWallpaper->mnRefCount++;
579 // Wenn es keine statischen ImpDaten sind, dann loeschen, wenn es
580 // die letzte Referenz ist, sonst Referenzcounter decrementieren
581 if ( mpImplWallpaper->mnRefCount )
583 if ( mpImplWallpaper->mnRefCount == 1 )
584 delete mpImplWallpaper;
585 else
586 mpImplWallpaper->mnRefCount--;
589 mpImplWallpaper = rWallpaper.mpImplWallpaper;
591 return *this;
594 // -----------------------------------------------------------------------
596 BOOL Wallpaper::operator==( const Wallpaper& rWallpaper ) const
598 DBG_CHKTHIS( Wallpaper, NULL );
599 DBG_CHKOBJ( &rWallpaper, Wallpaper, NULL );
601 if ( mpImplWallpaper == rWallpaper.mpImplWallpaper )
602 return TRUE;
604 if ( ( mpImplWallpaper->meStyle != rWallpaper.mpImplWallpaper->meStyle ) ||
605 ( mpImplWallpaper->maColor != rWallpaper.mpImplWallpaper->maColor ) )
606 return FALSE;
608 if ( mpImplWallpaper->mpRect != rWallpaper.mpImplWallpaper->mpRect
609 && ( !mpImplWallpaper->mpRect
610 || !rWallpaper.mpImplWallpaper->mpRect
611 || *(mpImplWallpaper->mpRect) != *(rWallpaper.mpImplWallpaper->mpRect) ) )
612 return FALSE;
614 if ( mpImplWallpaper->mpBitmap != rWallpaper.mpImplWallpaper->mpBitmap
615 && ( !mpImplWallpaper->mpBitmap
616 || !rWallpaper.mpImplWallpaper->mpBitmap
617 || *(mpImplWallpaper->mpBitmap) != *(rWallpaper.mpImplWallpaper->mpBitmap) ) )
618 return FALSE;
620 if ( mpImplWallpaper->mpGradient != rWallpaper.mpImplWallpaper->mpGradient
621 && ( !mpImplWallpaper->mpGradient
622 || !rWallpaper.mpImplWallpaper->mpGradient
623 || *(mpImplWallpaper->mpGradient) != *(rWallpaper.mpImplWallpaper->mpGradient) ) )
624 return FALSE;
626 return TRUE;
629 // -----------------------------------------------------------------------
631 SvStream& operator>>( SvStream& rIStm, Wallpaper& rWallpaper )
633 rWallpaper.ImplMakeUnique();
634 return( rIStm >> *rWallpaper.mpImplWallpaper );
637 // -----------------------------------------------------------------------
639 SvStream& operator<<( SvStream& rOStm, const Wallpaper& rWallpaper )
641 return( rOStm << *rWallpaper.mpImplWallpaper );