1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
27 #include <vcl/dibtools.hxx>
28 #include <vcl/settings.hxx>
29 #include <o3tl/make_unique.hxx>
31 ImplWallpaper::ImplWallpaper() :
32 maColor( COL_TRANSPARENT
), meStyle( WallpaperStyle::NONE
)
36 ImplWallpaper::ImplWallpaper( const ImplWallpaper
& rImplWallpaper
) :
37 maColor( rImplWallpaper
.maColor
), meStyle(rImplWallpaper
.meStyle
)
39 if ( rImplWallpaper
.mpBitmap
)
40 mpBitmap
= o3tl::make_unique
<BitmapEx
>( *rImplWallpaper
.mpBitmap
);
42 if( rImplWallpaper
.mpCache
)
43 mpCache
= o3tl::make_unique
<BitmapEx
>( *rImplWallpaper
.mpCache
);
45 if ( rImplWallpaper
.mpGradient
)
46 mpGradient
= o3tl::make_unique
<Gradient
>( *rImplWallpaper
.mpGradient
);
48 if ( rImplWallpaper
.mpRect
)
49 mpRect
= o3tl::make_unique
<tools::Rectangle
>( *rImplWallpaper
.mpRect
);
53 ImplWallpaper::~ImplWallpaper()
57 SvStream
& ReadImplWallpaper( SvStream
& rIStm
, ImplWallpaper
& rImplWallpaper
)
59 VersionCompat
aCompat( rIStm
, StreamMode::READ
);
62 rImplWallpaper
.mpRect
.reset();
63 rImplWallpaper
.mpGradient
.reset();
64 rImplWallpaper
.mpBitmap
.reset();
67 ReadColor( rIStm
, rImplWallpaper
.maColor
);
68 rIStm
.ReadUInt16( nTmp16
); rImplWallpaper
.meStyle
= (WallpaperStyle
) nTmp16
;
71 if( aCompat
.GetVersion() >= 2 )
73 bool bRect
, bGrad
, bBmp
, bDummy
;
75 rIStm
.ReadCharAsBool( bRect
).ReadCharAsBool( bGrad
).ReadCharAsBool( bBmp
).ReadCharAsBool( bDummy
).ReadCharAsBool( bDummy
).ReadCharAsBool( bDummy
);
79 rImplWallpaper
.mpRect
= o3tl::make_unique
<tools::Rectangle
>();
80 ReadRectangle( rIStm
, *rImplWallpaper
.mpRect
);
85 rImplWallpaper
.mpGradient
= o3tl::make_unique
<Gradient
>();
86 ReadGradient( rIStm
, *rImplWallpaper
.mpGradient
);
91 rImplWallpaper
.mpBitmap
= o3tl::make_unique
<BitmapEx
>();
92 ReadDIBBitmapEx(*rImplWallpaper
.mpBitmap
, rIStm
);
95 // version 3 (new color format)
96 if( aCompat
.GetVersion() >= 3 )
98 rImplWallpaper
.maColor
.Read( rIStm
);
105 SvStream
& WriteImplWallpaper( SvStream
& rOStm
, const ImplWallpaper
& rImplWallpaper
)
107 VersionCompat
aCompat( rOStm
, StreamMode::WRITE
, 3 );
108 bool bRect
= bool(rImplWallpaper
.mpRect
);
109 bool bGrad
= bool(rImplWallpaper
.mpGradient
);
110 bool bBmp
= bool(rImplWallpaper
.mpBitmap
);
114 WriteColor( rOStm
, rImplWallpaper
.maColor
);
115 rOStm
.WriteUInt16( static_cast<sal_uInt16
>(rImplWallpaper
.meStyle
) );
118 rOStm
.WriteBool( bRect
).WriteBool( bGrad
).WriteBool( bBmp
).WriteBool( bDummy
).WriteBool( bDummy
).WriteBool( bDummy
);
121 WriteRectangle( rOStm
, *rImplWallpaper
.mpRect
);
124 WriteGradient( rOStm
, *rImplWallpaper
.mpGradient
);
127 WriteDIBBitmapEx(*rImplWallpaper
.mpBitmap
, rOStm
);
129 // version 3 (new color format)
130 ( (Color
&) rImplWallpaper
.maColor
).Write( rOStm
);
137 struct theGlobalDefault
:
138 public rtl::Static
< Wallpaper::ImplType
, theGlobalDefault
> {};
141 Wallpaper::Wallpaper() : mpImplWallpaper(theGlobalDefault::get())
145 Wallpaper::Wallpaper( const Wallpaper
& rWallpaper
)
146 : mpImplWallpaper( rWallpaper
.mpImplWallpaper
)
150 Wallpaper::Wallpaper( Wallpaper
&& rWallpaper
)
151 : mpImplWallpaper( std::move(rWallpaper
.mpImplWallpaper
) )
155 Wallpaper::Wallpaper( const Color
& rColor
) : mpImplWallpaper()
157 mpImplWallpaper
->maColor
= rColor
;
158 mpImplWallpaper
->meStyle
= WallpaperStyle::Tile
;
161 Wallpaper::Wallpaper( const BitmapEx
& rBmpEx
) : mpImplWallpaper()
163 mpImplWallpaper
->mpBitmap
= o3tl::make_unique
<BitmapEx
>( rBmpEx
);
164 mpImplWallpaper
->meStyle
= WallpaperStyle::Tile
;
167 Wallpaper::Wallpaper( const Gradient
& rGradient
) : mpImplWallpaper()
169 mpImplWallpaper
->mpGradient
= o3tl::make_unique
<Gradient
>( rGradient
);
170 mpImplWallpaper
->meStyle
= WallpaperStyle::Tile
;
173 Wallpaper::~Wallpaper()
177 void Wallpaper::ImplSetCachedBitmap( BitmapEx
& rBmp
) const
179 if( !mpImplWallpaper
->mpCache
)
180 const_cast< ImplWallpaper
* >(mpImplWallpaper
.get())->mpCache
= o3tl::make_unique
<BitmapEx
>( rBmp
);
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
)
228 if ( mpImplWallpaper
->mpBitmap
)
230 ImplReleaseCachedBitmap();
231 mpImplWallpaper
->mpBitmap
.reset();
236 ImplReleaseCachedBitmap();
237 if ( mpImplWallpaper
->mpBitmap
)
238 *(mpImplWallpaper
->mpBitmap
) = rBitmap
;
240 mpImplWallpaper
->mpBitmap
= o3tl::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
);
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
;
267 mpImplWallpaper
->mpGradient
= o3tl::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
);
283 bool Wallpaper::IsGradient() const
285 return bool(mpImplWallpaper
->mpGradient
);
288 Gradient
Wallpaper::ImplGetApplicationGradient()
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() );
298 g
.SetEndColor( Application::GetSettings().GetStyleSettings().GetFaceGradientColor() );
302 void Wallpaper::SetRect( const tools::Rectangle
& rRect
)
304 if ( rRect
.IsEmpty() )
306 mpImplWallpaper
->mpRect
.reset();
310 if ( mpImplWallpaper
->mpRect
)
311 *(mpImplWallpaper
->mpRect
) = rRect
;
313 mpImplWallpaper
->mpRect
= o3tl::make_unique
<tools::Rectangle
>( rRect
);
317 tools::Rectangle
Wallpaper::GetRect() const
319 if ( mpImplWallpaper
->mpRect
)
320 return *(mpImplWallpaper
->mpRect
);
322 return tools::Rectangle();
325 bool Wallpaper::IsRect() const
327 return bool(mpImplWallpaper
->mpRect
);
330 bool Wallpaper::IsFixed() const
332 if ( mpImplWallpaper
->meStyle
== WallpaperStyle::NONE
)
335 return (!mpImplWallpaper
->mpBitmap
&& !mpImplWallpaper
->mpGradient
);
338 bool Wallpaper::IsScrollable() const
340 if ( mpImplWallpaper
->meStyle
== WallpaperStyle::NONE
)
342 else if ( !mpImplWallpaper
->mpBitmap
&& !mpImplWallpaper
->mpGradient
)
344 else if ( mpImplWallpaper
->mpBitmap
)
345 return (mpImplWallpaper
->meStyle
== WallpaperStyle::Tile
);
350 Wallpaper
& Wallpaper::operator=( const Wallpaper
& rWallpaper
)
352 mpImplWallpaper
= rWallpaper
.mpImplWallpaper
;
356 Wallpaper
& Wallpaper::operator=( Wallpaper
&& rWallpaper
)
358 mpImplWallpaper
= std::move(rWallpaper
.mpImplWallpaper
);
362 bool Wallpaper::operator==( const Wallpaper
& rWallpaper
) const
364 return mpImplWallpaper
.same_object(rWallpaper
.mpImplWallpaper
);
367 SvStream
& ReadWallpaper( SvStream
& rIStm
, Wallpaper
& rWallpaper
)
369 return ReadImplWallpaper( rIStm
, *rWallpaper
.mpImplWallpaper
);
372 SvStream
& WriteWallpaper( SvStream
& rOStm
, const Wallpaper
& rWallpaper
)
374 return WriteImplWallpaper( rOStm
, *rWallpaper
.mpImplWallpaper
);
377 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */