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 <tools/debug.hxx>
23 #include <vcl/bitmapex.hxx>
24 #include <vcl/gradient.hxx>
25 #include <vcl/wall.hxx>
26 #include <vcl/svapp.hxx>
28 #include <vcl/dibtools.hxx>
29 #include <vcl/settings.hxx>
31 ImplWallpaper::ImplWallpaper() :
32 maColor( COL_TRANSPARENT
)
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
);
50 if( rImplWallpaper
.mpCache
)
51 mpCache
= new BitmapEx( *rImplWallpaper
.mpCache
);
54 if ( rImplWallpaper
.mpGradient
)
55 mpGradient
= new Gradient( *rImplWallpaper
.mpGradient
);
58 if ( rImplWallpaper
.mpRect
)
59 mpRect
= new Rectangle( *rImplWallpaper
.mpRect
);
64 ImplWallpaper::~ImplWallpaper()
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
)
83 SvStream
& ReadImplWallpaper( SvStream
& rIStm
, ImplWallpaper
& rImplWallpaper
)
85 VersionCompat
aCompat( rIStm
, StreamMode::READ
);
88 delete rImplWallpaper
.mpRect
;
89 rImplWallpaper
.mpRect
= nullptr;
91 delete rImplWallpaper
.mpGradient
;
92 rImplWallpaper
.mpGradient
= nullptr;
94 delete rImplWallpaper
.mpBitmap
;
95 rImplWallpaper
.mpBitmap
= nullptr;
98 ReadColor( rIStm
, rImplWallpaper
.maColor
);
99 rIStm
.ReadUInt16( nTmp16
); rImplWallpaper
.meStyle
= (WallpaperStyle
) nTmp16
;
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
);
110 rImplWallpaper
.mpRect
= new Rectangle
;
111 ReadRectangle( rIStm
, *rImplWallpaper
.mpRect
);
116 rImplWallpaper
.mpGradient
= new Gradient
;
117 ReadGradient( rIStm
, *rImplWallpaper
.mpGradient
);
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
);
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 );
145 WriteColor( rOStm
, rImplWallpaper
.maColor
);
146 rOStm
.WriteUInt16( static_cast<sal_uInt16
>(rImplWallpaper
.meStyle
) );
149 rOStm
.WriteBool( bRect
).WriteBool( bGrad
).WriteBool( bBmp
).WriteBool( bDummy
).WriteBool( bDummy
).WriteBool( bDummy
);
152 WriteRectangle( rOStm
, *rImplWallpaper
.mpRect
);
155 WriteGradient( rOStm
, *rImplWallpaper
.mpGradient
);
158 WriteDIBBitmapEx(*rImplWallpaper
.mpBitmap
, rOStm
);
160 // version 3 (new color format)
161 ( (Color
&) rImplWallpaper
.maColor
).Write( rOStm
);
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
);
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
)
256 if ( mpImplWallpaper
->mpBitmap
)
258 ImplReleaseCachedBitmap();
259 delete mpImplWallpaper
->mpBitmap
;
260 mpImplWallpaper
->mpBitmap
= nullptr;
265 ImplReleaseCachedBitmap();
266 if ( mpImplWallpaper
->mpBitmap
)
267 *(mpImplWallpaper
->mpBitmap
) = rBitmap
;
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
);
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
;
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
);
318 bool Wallpaper::IsGradient() const
320 return (mpImplWallpaper
->mpGradient
!= nullptr);
323 Gradient
Wallpaper::ImplGetApplicationGradient() const
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() );
333 g
.SetEndColor( Application::GetSettings().GetStyleSettings().GetFaceGradientColor() );
337 void Wallpaper::SetRect( const Rectangle
& rRect
)
339 if ( rRect
.IsEmpty() )
341 if ( mpImplWallpaper
->mpRect
)
343 delete mpImplWallpaper
->mpRect
;
344 mpImplWallpaper
->mpRect
= nullptr;
349 if ( mpImplWallpaper
->mpRect
)
350 *(mpImplWallpaper
->mpRect
) = rRect
;
352 mpImplWallpaper
->mpRect
= new Rectangle( rRect
);
356 Rectangle
Wallpaper::GetRect() const
358 if ( mpImplWallpaper
->mpRect
)
359 return *(mpImplWallpaper
->mpRect
);
367 bool Wallpaper::IsRect() const
370 return (mpImplWallpaper
->mpRect
!= nullptr);
373 bool Wallpaper::IsFixed() const
375 if ( mpImplWallpaper
->meStyle
== WallpaperStyle::NONE
)
378 return (!mpImplWallpaper
->mpBitmap
&& !mpImplWallpaper
->mpGradient
);
381 bool Wallpaper::IsScrollable() const
383 if ( mpImplWallpaper
->meStyle
== WallpaperStyle::NONE
)
385 else if ( !mpImplWallpaper
->mpBitmap
&& !mpImplWallpaper
->mpGradient
)
387 else if ( mpImplWallpaper
->mpBitmap
)
388 return (mpImplWallpaper
->meStyle
== WallpaperStyle::Tile
);
393 Wallpaper
& Wallpaper::operator=( const Wallpaper
& rWallpaper
)
395 mpImplWallpaper
= rWallpaper
.mpImplWallpaper
;
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: */