Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / source / gdi / wall.cxx
blob5170134cc24cdf834118066cca029b7688ce37ab
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 <vcl/bitmapex.hxx>
23 #include <vcl/gradient.hxx>
24 #include <vcl/wall.hxx>
25 #include <vcl/svapp.hxx>
26 #include <wall2.hxx>
27 #include <vcl/dibtools.hxx>
28 #include <vcl/settings.hxx>
30 #include <TypeSerializer.hxx>
32 ImplWallpaper::ImplWallpaper() :
33 maColor( COL_TRANSPARENT ), meStyle( WallpaperStyle::NONE )
37 ImplWallpaper::ImplWallpaper( const ImplWallpaper& rImplWallpaper ) :
38 maColor( rImplWallpaper.maColor ), meStyle(rImplWallpaper.meStyle)
40 if ( rImplWallpaper.mpBitmap )
41 mpBitmap = std::make_unique<BitmapEx>( *rImplWallpaper.mpBitmap );
43 if( rImplWallpaper.mpCache )
44 mpCache = std::make_unique<BitmapEx>( *rImplWallpaper.mpCache );
46 if ( rImplWallpaper.mpGradient )
47 mpGradient = std::make_unique<Gradient>( *rImplWallpaper.mpGradient );
49 if ( rImplWallpaper.mpRect )
50 mpRect = *rImplWallpaper.mpRect;
53 ImplWallpaper::~ImplWallpaper()
57 SvStream& ReadImplWallpaper( SvStream& rIStm, ImplWallpaper& rImplWallpaper )
59 VersionCompat aCompat( rIStm, StreamMode::READ );
61 rImplWallpaper.mpRect.reset();
62 rImplWallpaper.mpGradient.reset();
63 rImplWallpaper.mpBitmap.reset();
65 // version 1
66 TypeSerializer aSerializer(rIStm);
67 aSerializer.readColor(rImplWallpaper.maColor);
68 sal_uInt16 nTmp16(0);
69 rIStm.ReadUInt16(nTmp16);
70 rImplWallpaper.meStyle = static_cast<WallpaperStyle>(nTmp16);
72 // version 2
73 if( aCompat.GetVersion() >= 2 )
75 bool bRect(false), bGrad(false), bBmp(false), bDummy;
77 rIStm.ReadCharAsBool( bRect ).ReadCharAsBool( bGrad ).ReadCharAsBool( bBmp ).ReadCharAsBool( bDummy ).ReadCharAsBool( bDummy ).ReadCharAsBool( bDummy );
79 if( bRect )
81 rImplWallpaper.mpRect = tools::Rectangle();
82 aSerializer.readRectangle(*rImplWallpaper.mpRect);
85 if( bGrad )
87 rImplWallpaper.mpGradient = std::make_unique<Gradient>();
88 aSerializer.readGradient(*rImplWallpaper.mpGradient);
91 if( bBmp )
93 rImplWallpaper.mpBitmap = std::make_unique<BitmapEx>();
94 ReadDIBBitmapEx(*rImplWallpaper.mpBitmap, rIStm);
97 // version 3 (new color format)
98 if( aCompat.GetVersion() >= 3 )
100 rIStm.ReadUInt32(rImplWallpaper.maColor.mValue);
104 return rIStm;
107 SvStream& WriteImplWallpaper( SvStream& rOStm, const ImplWallpaper& rImplWallpaper )
109 VersionCompat aCompat( rOStm, StreamMode::WRITE, 3 );
110 bool bRect = bool(rImplWallpaper.mpRect);
111 bool bGrad = bool(rImplWallpaper.mpGradient);
112 bool bBmp = bool(rImplWallpaper.mpBitmap);
113 bool bDummy = false;
115 // version 1
116 TypeSerializer aSerializer(rOStm);
117 aSerializer.writeColor(rImplWallpaper.maColor);
119 rOStm.WriteUInt16( static_cast<sal_uInt16>(rImplWallpaper.meStyle) );
121 // version 2
122 rOStm.WriteBool( bRect ).WriteBool( bGrad ).WriteBool( bBmp ).WriteBool( bDummy ).WriteBool( bDummy ).WriteBool( bDummy );
124 if( bRect )
126 aSerializer.writeRectangle(*rImplWallpaper.mpRect);
129 if (bGrad)
131 aSerializer.writeGradient(*rImplWallpaper.mpGradient);
134 if( bBmp )
135 WriteDIBBitmapEx(*rImplWallpaper.mpBitmap, rOStm);
137 // version 3 (new color format)
138 rOStm.WriteUInt32(rImplWallpaper.maColor.mValue);
140 return rOStm;
143 namespace
145 struct theGlobalDefault :
146 public rtl::Static< Wallpaper::ImplType, theGlobalDefault > {};
149 Wallpaper::Wallpaper() : mpImplWallpaper(theGlobalDefault::get())
153 Wallpaper::Wallpaper( const Wallpaper& ) = default;
155 Wallpaper::Wallpaper( Wallpaper&& ) = default;
157 Wallpaper::Wallpaper( const Color& rColor ) : mpImplWallpaper()
159 mpImplWallpaper->maColor = rColor;
160 mpImplWallpaper->meStyle = WallpaperStyle::Tile;
163 Wallpaper::Wallpaper( const BitmapEx& rBmpEx ) : mpImplWallpaper()
165 mpImplWallpaper->mpBitmap = std::make_unique<BitmapEx>( rBmpEx );
166 mpImplWallpaper->meStyle = WallpaperStyle::Tile;
169 Wallpaper::Wallpaper( const Gradient& rGradient ) : mpImplWallpaper()
171 mpImplWallpaper->mpGradient = std::make_unique<Gradient>( rGradient );
172 mpImplWallpaper->meStyle = WallpaperStyle::Tile;
175 Wallpaper::~Wallpaper() = default;
177 void Wallpaper::ImplSetCachedBitmap( BitmapEx& rBmp ) const
179 if( !mpImplWallpaper->mpCache )
180 const_cast< ImplWallpaper* >(mpImplWallpaper.get())->mpCache = std::make_unique<BitmapEx>( rBmp );
181 else
182 *const_cast< ImplWallpaper* >(mpImplWallpaper.get())->mpCache = rBmp;
185 const BitmapEx* Wallpaper::ImplGetCachedBitmap() const
187 return mpImplWallpaper->mpCache.get();
190 void Wallpaper::ImplReleaseCachedBitmap() const
192 const_cast< ImplWallpaper* >(mpImplWallpaper.get())->mpCache.reset();
195 void Wallpaper::SetColor( const Color& rColor )
197 ImplReleaseCachedBitmap();
198 mpImplWallpaper->maColor = rColor;
200 if( WallpaperStyle::NONE == mpImplWallpaper->meStyle || WallpaperStyle::ApplicationGradient == mpImplWallpaper->meStyle )
201 mpImplWallpaper->meStyle = WallpaperStyle::Tile;
204 const Color& Wallpaper::GetColor() const
206 return mpImplWallpaper->maColor;
209 void Wallpaper::SetStyle( WallpaperStyle eStyle )
211 if( eStyle == WallpaperStyle::ApplicationGradient )
212 // set a dummy gradient, the correct gradient
213 // will be created dynamically in GetGradient()
214 SetGradient( ImplGetApplicationGradient() );
216 mpImplWallpaper->meStyle = eStyle;
219 WallpaperStyle Wallpaper::GetStyle() const
221 return mpImplWallpaper->meStyle;
224 void Wallpaper::SetBitmap( const BitmapEx& rBitmap )
226 if ( !rBitmap )
228 if ( mpImplWallpaper->mpBitmap )
230 ImplReleaseCachedBitmap();
231 mpImplWallpaper->mpBitmap.reset();
234 else
236 ImplReleaseCachedBitmap();
237 if ( mpImplWallpaper->mpBitmap )
238 *(mpImplWallpaper->mpBitmap) = rBitmap;
239 else
240 mpImplWallpaper->mpBitmap = std::make_unique<BitmapEx>( rBitmap );
243 if( WallpaperStyle::NONE == mpImplWallpaper->meStyle || WallpaperStyle::ApplicationGradient == mpImplWallpaper->meStyle)
244 mpImplWallpaper->meStyle = WallpaperStyle::Tile;
247 BitmapEx Wallpaper::GetBitmap() const
249 if ( mpImplWallpaper->mpBitmap )
250 return *(mpImplWallpaper->mpBitmap);
251 else
252 return BitmapEx();
255 bool Wallpaper::IsBitmap() const
257 return bool(mpImplWallpaper->mpBitmap);
260 void Wallpaper::SetGradient( const Gradient& rGradient )
262 ImplReleaseCachedBitmap();
264 if ( mpImplWallpaper->mpGradient )
265 *(mpImplWallpaper->mpGradient) = rGradient;
266 else
267 mpImplWallpaper->mpGradient = std::make_unique<Gradient>( rGradient );
269 if( WallpaperStyle::NONE == mpImplWallpaper->meStyle || WallpaperStyle::ApplicationGradient == mpImplWallpaper->meStyle )
270 mpImplWallpaper->meStyle = WallpaperStyle::Tile;
273 Gradient Wallpaper::GetGradient() const
275 if( WallpaperStyle::ApplicationGradient == mpImplWallpaper->meStyle )
276 return ImplGetApplicationGradient();
277 else if ( mpImplWallpaper->mpGradient )
278 return *(mpImplWallpaper->mpGradient);
279 else
280 return Gradient();
283 bool Wallpaper::IsGradient() const
285 return bool(mpImplWallpaper->mpGradient);
288 Gradient Wallpaper::ImplGetApplicationGradient()
290 Gradient g;
291 g.SetAngle( 900 );
292 g.SetStyle( GradientStyle::Linear );
293 g.SetStartColor( Application::GetSettings().GetStyleSettings().GetFaceColor() );
294 // no 'extreme' gradient when high contrast
295 if( Application::GetSettings().GetStyleSettings().GetHighContrastMode() )
296 g.SetEndColor( Application::GetSettings().GetStyleSettings().GetFaceColor() );
297 else
298 g.SetEndColor( Application::GetSettings().GetStyleSettings().GetFaceGradientColor() );
299 return g;
302 void Wallpaper::SetRect( const tools::Rectangle& rRect )
304 if ( rRect.IsEmpty() )
306 mpImplWallpaper->mpRect.reset();
308 else
310 mpImplWallpaper->mpRect = rRect;
314 tools::Rectangle Wallpaper::GetRect() const
316 if ( mpImplWallpaper->mpRect )
317 return *mpImplWallpaper->mpRect;
318 else
319 return tools::Rectangle();
322 bool Wallpaper::IsRect() const
324 return bool(mpImplWallpaper->mpRect);
327 bool Wallpaper::IsFixed() const
329 if ( mpImplWallpaper->meStyle == WallpaperStyle::NONE )
330 return false;
331 else
332 return (!mpImplWallpaper->mpBitmap && !mpImplWallpaper->mpGradient);
335 bool Wallpaper::IsScrollable() const
337 if ( mpImplWallpaper->meStyle == WallpaperStyle::NONE )
338 return false;
339 else if ( !mpImplWallpaper->mpBitmap && !mpImplWallpaper->mpGradient )
340 return true;
341 else if ( mpImplWallpaper->mpBitmap )
342 return (mpImplWallpaper->meStyle == WallpaperStyle::Tile);
343 else
344 return false;
347 Wallpaper& Wallpaper::operator=( const Wallpaper& ) = default;
349 Wallpaper& Wallpaper::operator=( Wallpaper&& ) = default;
351 bool Wallpaper::operator==( const Wallpaper& rWallpaper ) const
353 return mpImplWallpaper.same_object(rWallpaper.mpImplWallpaper);
356 SvStream& ReadWallpaper( SvStream& rIStm, Wallpaper& rWallpaper )
358 return ReadImplWallpaper( rIStm, *rWallpaper.mpImplWallpaper );
361 SvStream& WriteWallpaper( SvStream& rOStm, const Wallpaper& rWallpaper )
363 return WriteImplWallpaper( rOStm, *rWallpaper.mpImplWallpaper );
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */