Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / vcl / source / gdi / wall.cxx
blob26b3448c50a054d90b41bc69c2d4444055d5a91d
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 .
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 #include <vcl/settings.hxx>
31 ImplWallpaper::ImplWallpaper() :
32 maColor( COL_TRANSPARENT )
34 mpBitmap = nullptr;
35 mpCache = nullptr;
36 mpGradient = nullptr;
37 mpRect = nullptr;
38 meStyle = WallpaperStyle::NONE;
41 ImplWallpaper::ImplWallpaper( const ImplWallpaper& rImplWallpaper ) :
42 maColor( rImplWallpaper.maColor )
44 meStyle = rImplWallpaper.meStyle;
46 if ( rImplWallpaper.mpBitmap )
47 mpBitmap = new BitmapEx( *rImplWallpaper.mpBitmap );
48 else
49 mpBitmap = nullptr;
50 if( rImplWallpaper.mpCache )
51 mpCache = new BitmapEx( *rImplWallpaper.mpCache );
52 else
53 mpCache = nullptr;
54 if ( rImplWallpaper.mpGradient )
55 mpGradient = new Gradient( *rImplWallpaper.mpGradient );
56 else
57 mpGradient = nullptr;
58 if ( rImplWallpaper.mpRect )
59 mpRect = new Rectangle( *rImplWallpaper.mpRect );
60 else
61 mpRect = nullptr;
64 ImplWallpaper::~ImplWallpaper()
66 delete mpBitmap;
67 delete mpCache;
68 delete mpGradient;
69 delete mpRect;
72 bool ImplWallpaper::operator==( const ImplWallpaper& rImplWallpaper ) const
74 if ( meStyle == rImplWallpaper.meStyle &&
75 maColor == rImplWallpaper.maColor &&
76 mpRect == rImplWallpaper.mpRect &&
77 mpBitmap == rImplWallpaper.mpBitmap &&
78 mpGradient == rImplWallpaper.mpGradient )
79 return true;
80 return false;
83 SvStream& ReadImplWallpaper( SvStream& rIStm, ImplWallpaper& rImplWallpaper )
85 VersionCompat aCompat( rIStm, StreamMode::READ );
86 sal_uInt16 nTmp16;
88 delete rImplWallpaper.mpRect;
89 rImplWallpaper.mpRect = nullptr;
91 delete rImplWallpaper.mpGradient;
92 rImplWallpaper.mpGradient = nullptr;
94 delete rImplWallpaper.mpBitmap;
95 rImplWallpaper.mpBitmap = nullptr;
97 // version 1
98 ReadColor( rIStm, rImplWallpaper.maColor );
99 rIStm.ReadUInt16( nTmp16 ); rImplWallpaper.meStyle = (WallpaperStyle) nTmp16;
101 // version 2
102 if( aCompat.GetVersion() >= 2 )
104 bool bRect, bGrad, bBmp, bDummy;
106 rIStm.ReadCharAsBool( bRect ).ReadCharAsBool( bGrad ).ReadCharAsBool( bBmp ).ReadCharAsBool( bDummy ).ReadCharAsBool( bDummy ).ReadCharAsBool( bDummy );
108 if( bRect )
110 rImplWallpaper.mpRect = new Rectangle;
111 ReadRectangle( rIStm, *rImplWallpaper.mpRect );
114 if( bGrad )
116 rImplWallpaper.mpGradient = new Gradient;
117 ReadGradient( rIStm, *rImplWallpaper.mpGradient );
120 if( bBmp )
122 rImplWallpaper.mpBitmap = new BitmapEx;
123 ReadDIBBitmapEx(*rImplWallpaper.mpBitmap, rIStm);
126 // version 3 (new color format)
127 if( aCompat.GetVersion() >= 3 )
129 rImplWallpaper.maColor.Read( rIStm );
133 return rIStm;
136 SvStream& WriteImplWallpaper( SvStream& rOStm, const ImplWallpaper& rImplWallpaper )
138 VersionCompat aCompat( rOStm, StreamMode::WRITE, 3 );
139 bool bRect = ( rImplWallpaper.mpRect != nullptr );
140 bool bGrad = ( rImplWallpaper.mpGradient != nullptr );
141 bool bBmp = ( rImplWallpaper.mpBitmap != nullptr );
142 bool bDummy = false;
144 // version 1
145 WriteColor( rOStm, rImplWallpaper.maColor );
146 rOStm.WriteUInt16( static_cast<sal_uInt16>(rImplWallpaper.meStyle) );
148 // version 2
149 rOStm.WriteBool( bRect ).WriteBool( bGrad ).WriteBool( bBmp ).WriteBool( bDummy ).WriteBool( bDummy ).WriteBool( bDummy );
151 if( bRect )
152 WriteRectangle( rOStm, *rImplWallpaper.mpRect );
154 if( bGrad )
155 WriteGradient( rOStm, *rImplWallpaper.mpGradient );
157 if( bBmp )
158 WriteDIBBitmapEx(*rImplWallpaper.mpBitmap, rOStm);
160 // version 3 (new color format)
161 ( (Color&) rImplWallpaper.maColor ).Write( rOStm );
163 return rOStm;
166 namespace
168 struct theGlobalDefault :
169 public rtl::Static< Wallpaper::ImplType, theGlobalDefault > {};
172 Wallpaper::Wallpaper() : mpImplWallpaper(theGlobalDefault::get())
176 Wallpaper::Wallpaper( const Wallpaper& rWallpaper )
177 : mpImplWallpaper( rWallpaper.mpImplWallpaper)
181 Wallpaper::Wallpaper( const Color& rColor ) : mpImplWallpaper()
183 mpImplWallpaper->maColor = rColor;
184 mpImplWallpaper->meStyle = WallpaperStyle::Tile;
187 Wallpaper::Wallpaper( const BitmapEx& rBmpEx ) : mpImplWallpaper()
189 mpImplWallpaper->mpBitmap = new BitmapEx( rBmpEx );
190 mpImplWallpaper->meStyle = WallpaperStyle::Tile;
193 Wallpaper::Wallpaper( const Gradient& rGradient ) : mpImplWallpaper()
195 mpImplWallpaper->mpGradient = new Gradient( rGradient );
196 mpImplWallpaper->meStyle = WallpaperStyle::Tile;
199 Wallpaper::~Wallpaper()
203 void Wallpaper::ImplSetCachedBitmap( BitmapEx& rBmp ) const
205 if( !mpImplWallpaper->mpCache )
206 const_cast< ImplWallpaper* >(mpImplWallpaper.get())->mpCache = new BitmapEx( rBmp );
207 else
208 *const_cast< ImplWallpaper* >(mpImplWallpaper.get())->mpCache = rBmp;
211 const BitmapEx* Wallpaper::ImplGetCachedBitmap() const
213 return mpImplWallpaper->mpCache;
216 void Wallpaper::ImplReleaseCachedBitmap() const
218 delete mpImplWallpaper->mpCache;
219 const_cast< ImplWallpaper* >(mpImplWallpaper.get())->mpCache = nullptr;
223 void Wallpaper::SetColor( const Color& rColor )
225 ImplReleaseCachedBitmap();
226 mpImplWallpaper->maColor = rColor;
228 if( WallpaperStyle::NONE == mpImplWallpaper->meStyle || WallpaperStyle::ApplicationGradient == mpImplWallpaper->meStyle )
229 mpImplWallpaper->meStyle = WallpaperStyle::Tile;
232 const Color& Wallpaper::GetColor() const
234 return mpImplWallpaper->maColor;
237 void Wallpaper::SetStyle( WallpaperStyle eStyle )
239 if( eStyle == WallpaperStyle::ApplicationGradient )
240 // set a dummy gradient, the correct gradient
241 // will be created dynamically in GetGradient()
242 SetGradient( ImplGetApplicationGradient() );
244 mpImplWallpaper->meStyle = eStyle;
247 WallpaperStyle Wallpaper::GetStyle() const
249 return mpImplWallpaper->meStyle;
252 void Wallpaper::SetBitmap( const BitmapEx& rBitmap )
254 if ( !rBitmap )
256 if ( mpImplWallpaper->mpBitmap )
258 ImplReleaseCachedBitmap();
259 delete mpImplWallpaper->mpBitmap;
260 mpImplWallpaper->mpBitmap = nullptr;
263 else
265 ImplReleaseCachedBitmap();
266 if ( mpImplWallpaper->mpBitmap )
267 *(mpImplWallpaper->mpBitmap) = rBitmap;
268 else
269 mpImplWallpaper->mpBitmap = new BitmapEx( rBitmap );
272 if( WallpaperStyle::NONE == mpImplWallpaper->meStyle || WallpaperStyle::ApplicationGradient == mpImplWallpaper->meStyle)
273 mpImplWallpaper->meStyle = WallpaperStyle::Tile;
276 BitmapEx Wallpaper::GetBitmap() const
278 if ( mpImplWallpaper->mpBitmap )
279 return *(mpImplWallpaper->mpBitmap);
280 else
282 BitmapEx aBmp;
283 return aBmp;
287 bool Wallpaper::IsBitmap() const
289 return (mpImplWallpaper->mpBitmap != nullptr);
292 void Wallpaper::SetGradient( const Gradient& rGradient )
294 ImplReleaseCachedBitmap();
296 if ( mpImplWallpaper->mpGradient )
297 *(mpImplWallpaper->mpGradient) = rGradient;
298 else
299 mpImplWallpaper->mpGradient = new Gradient( rGradient );
301 if( WallpaperStyle::NONE == mpImplWallpaper->meStyle || WallpaperStyle::ApplicationGradient == mpImplWallpaper->meStyle )
302 mpImplWallpaper->meStyle = WallpaperStyle::Tile;
305 Gradient Wallpaper::GetGradient() const
307 if( WallpaperStyle::ApplicationGradient == mpImplWallpaper->meStyle )
308 return ImplGetApplicationGradient();
309 else if ( mpImplWallpaper->mpGradient )
310 return *(mpImplWallpaper->mpGradient);
311 else
313 Gradient aGradient;
314 return aGradient;
318 bool Wallpaper::IsGradient() const
320 return (mpImplWallpaper->mpGradient != nullptr);
323 Gradient Wallpaper::ImplGetApplicationGradient() const
325 Gradient g;
326 g.SetAngle( 900 );
327 g.SetStyle( GradientStyle_LINEAR );
328 g.SetStartColor( Application::GetSettings().GetStyleSettings().GetFaceColor() );
329 // no 'extreme' gradient when high contrast
330 if( Application::GetSettings().GetStyleSettings().GetHighContrastMode() )
331 g.SetEndColor( Application::GetSettings().GetStyleSettings().GetFaceColor() );
332 else
333 g.SetEndColor( Application::GetSettings().GetStyleSettings().GetFaceGradientColor() );
334 return g;
337 void Wallpaper::SetRect( const Rectangle& rRect )
339 if ( rRect.IsEmpty() )
341 if ( mpImplWallpaper->mpRect )
343 delete mpImplWallpaper->mpRect;
344 mpImplWallpaper->mpRect = nullptr;
347 else
349 if ( mpImplWallpaper->mpRect )
350 *(mpImplWallpaper->mpRect) = rRect;
351 else
352 mpImplWallpaper->mpRect = new Rectangle( rRect );
356 Rectangle Wallpaper::GetRect() const
358 if ( mpImplWallpaper->mpRect )
359 return *(mpImplWallpaper->mpRect);
360 else
362 Rectangle aRect;
363 return aRect;
367 bool Wallpaper::IsRect() const
370 return (mpImplWallpaper->mpRect != nullptr);
373 bool Wallpaper::IsFixed() const
375 if ( mpImplWallpaper->meStyle == WallpaperStyle::NONE )
376 return false;
377 else
378 return (!mpImplWallpaper->mpBitmap && !mpImplWallpaper->mpGradient);
381 bool Wallpaper::IsScrollable() const
383 if ( mpImplWallpaper->meStyle == WallpaperStyle::NONE )
384 return false;
385 else if ( !mpImplWallpaper->mpBitmap && !mpImplWallpaper->mpGradient )
386 return true;
387 else if ( mpImplWallpaper->mpBitmap )
388 return (mpImplWallpaper->meStyle == WallpaperStyle::Tile);
389 else
390 return false;
393 Wallpaper& Wallpaper::operator=( const Wallpaper& rWallpaper )
395 mpImplWallpaper = rWallpaper.mpImplWallpaper;
396 return *this;
399 bool Wallpaper::operator==( const Wallpaper& rWallpaper ) const
401 return mpImplWallpaper == rWallpaper.mpImplWallpaper;
404 SvStream& ReadWallpaper( SvStream& rIStm, Wallpaper& rWallpaper )
406 return ReadImplWallpaper( rIStm, *rWallpaper.mpImplWallpaper );
409 SvStream& WriteWallpaper( SvStream& rOStm, const Wallpaper& rWallpaper )
411 return WriteImplWallpaper( rOStm, *rWallpaper.mpImplWallpaper );
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */