Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <tools/stream.hxx>
21 : #include <tools/vcompat.hxx>
22 : #include <tools/debug.hxx>
23 : #include <tools/gen.hxx>
24 : #include <vcl/gradient.hxx>
25 :
26 : DBG_NAME( Gradient )
27 :
28 106827 : Impl_Gradient::Impl_Gradient() :
29 : maStartColor( COL_BLACK ),
30 106827 : maEndColor( COL_WHITE )
31 : {
32 106827 : mnRefCount = 1;
33 106827 : meStyle = GradientStyle_LINEAR;
34 106827 : mnAngle = 0;
35 106827 : mnBorder = 0;
36 106827 : mnOfsX = 50;
37 106827 : mnOfsY = 50;
38 106827 : mnIntensityStart = 100;
39 106827 : mnIntensityEnd = 100;
40 106827 : mnStepCount = 0;
41 106827 : }
42 :
43 69547 : Impl_Gradient::Impl_Gradient( const Impl_Gradient& rImplGradient ) :
44 : maStartColor( rImplGradient.maStartColor ),
45 69547 : maEndColor( rImplGradient.maEndColor )
46 : {
47 69547 : mnRefCount = 1;
48 69547 : meStyle = rImplGradient.meStyle;
49 69547 : mnAngle = rImplGradient.mnAngle;
50 69547 : mnBorder = rImplGradient.mnBorder;
51 69547 : mnOfsX = rImplGradient.mnOfsX;
52 69547 : mnOfsY = rImplGradient.mnOfsY;
53 69547 : mnIntensityStart = rImplGradient.mnIntensityStart;
54 69547 : mnIntensityEnd = rImplGradient.mnIntensityEnd;
55 69547 : mnStepCount = rImplGradient.mnStepCount;
56 69547 : }
57 :
58 494827 : void Gradient::MakeUnique()
59 : {
60 : // If there are still other references, copy
61 494827 : if ( mpImplGradient->mnRefCount != 1 )
62 : {
63 69547 : if( mpImplGradient->mnRefCount )
64 69547 : mpImplGradient->mnRefCount--;
65 :
66 69547 : mpImplGradient = new Impl_Gradient( *mpImplGradient );
67 : }
68 494827 : }
69 :
70 106320 : Gradient::Gradient()
71 : {
72 : DBG_CTOR( Gradient, NULL );
73 :
74 106320 : mpImplGradient = new Impl_Gradient;
75 106320 : }
76 :
77 183086 : Gradient::Gradient( const Gradient& rGradient )
78 : {
79 : DBG_CTOR( Gradient, NULL );
80 : DBG_CHKOBJ( &rGradient, Gradient, NULL );
81 :
82 : // Take over instance data and increment refcount
83 183086 : mpImplGradient = rGradient.mpImplGradient;
84 183086 : mpImplGradient->mnRefCount++;
85 183086 : }
86 :
87 507 : Gradient::Gradient( GradientStyle eStyle,
88 : const Color& rStartColor, const Color& rEndColor )
89 : {
90 : DBG_CTOR( Gradient, NULL );
91 :
92 507 : mpImplGradient = new Impl_Gradient;
93 507 : mpImplGradient->meStyle = eStyle;
94 507 : mpImplGradient->maStartColor = rStartColor;
95 507 : mpImplGradient->maEndColor = rEndColor;
96 507 : }
97 :
98 289784 : Gradient::~Gradient()
99 : {
100 : DBG_DTOR( Gradient, NULL );
101 :
102 : // If it's the last reference, delete it, otherwise
103 : // decrement refcount
104 289784 : if ( mpImplGradient->mnRefCount == 1 )
105 176245 : delete mpImplGradient;
106 : else
107 113539 : mpImplGradient->mnRefCount--;
108 289784 : }
109 :
110 106320 : void Gradient::SetStyle( GradientStyle eStyle )
111 : {
112 : DBG_CHKTHIS( Gradient, NULL );
113 :
114 106320 : MakeUnique();
115 106320 : mpImplGradient->meStyle = eStyle;
116 106320 : }
117 :
118 106320 : void Gradient::SetStartColor( const Color& rColor )
119 : {
120 : DBG_CHKTHIS( Gradient, NULL );
121 :
122 106320 : MakeUnique();
123 106320 : mpImplGradient->maStartColor = rColor;
124 106320 : }
125 :
126 106320 : void Gradient::SetEndColor( const Color& rColor )
127 : {
128 : DBG_CHKTHIS( Gradient, NULL );
129 :
130 106320 : MakeUnique();
131 106320 : mpImplGradient->maEndColor = rColor;
132 106320 : }
133 :
134 106320 : void Gradient::SetAngle( sal_uInt16 nAngle )
135 : {
136 : DBG_CHKTHIS( Gradient, NULL );
137 :
138 106320 : MakeUnique();
139 106320 : mpImplGradient->mnAngle = nAngle;
140 106320 : }
141 :
142 0 : void Gradient::SetBorder( sal_uInt16 nBorder )
143 : {
144 : DBG_CHKTHIS( Gradient, NULL );
145 :
146 0 : MakeUnique();
147 0 : mpImplGradient->mnBorder = nBorder;
148 0 : }
149 :
150 0 : void Gradient::SetOfsX( sal_uInt16 nOfsX )
151 : {
152 : DBG_CHKTHIS( Gradient, NULL );
153 :
154 0 : MakeUnique();
155 0 : mpImplGradient->mnOfsX = nOfsX;
156 0 : }
157 :
158 0 : void Gradient::SetOfsY( sal_uInt16 nOfsY )
159 : {
160 : DBG_CHKTHIS( Gradient, NULL );
161 :
162 0 : MakeUnique();
163 0 : mpImplGradient->mnOfsY = nOfsY;
164 0 : }
165 :
166 0 : void Gradient::SetStartIntensity( sal_uInt16 nIntens )
167 : {
168 : DBG_CHKTHIS( Gradient, NULL );
169 :
170 0 : MakeUnique();
171 0 : mpImplGradient->mnIntensityStart = nIntens;
172 0 : }
173 :
174 0 : void Gradient::SetEndIntensity( sal_uInt16 nIntens )
175 : {
176 : DBG_CHKTHIS( Gradient, NULL );
177 :
178 0 : MakeUnique();
179 0 : mpImplGradient->mnIntensityEnd = nIntens;
180 0 : }
181 :
182 69547 : void Gradient::SetSteps( sal_uInt16 nSteps )
183 : {
184 : DBG_CHKTHIS( Gradient, NULL );
185 :
186 69547 : MakeUnique();
187 69547 : mpImplGradient->mnStepCount = nSteps;
188 69547 : }
189 :
190 69547 : void Gradient::GetBoundRect( const Rectangle& rRect, Rectangle& rBoundRect, Point& rCenter ) const
191 : {
192 69547 : Rectangle aRect( rRect );
193 69547 : sal_uInt16 nAngle = GetAngle() % 3600;
194 :
195 69547 : if( GetStyle() == GradientStyle_LINEAR || GetStyle() == GradientStyle_AXIAL )
196 : {
197 69547 : aRect.Left()--;
198 69547 : aRect.Top()--;
199 69547 : aRect.Right()++;
200 69547 : aRect.Bottom()++;
201 :
202 69547 : const double fAngle = nAngle * F_PI1800;
203 69547 : const double fWidth = aRect.GetWidth();
204 69547 : const double fHeight = aRect.GetHeight();
205 69547 : double fDX = fWidth * fabs( cos( fAngle ) ) + fHeight * fabs( sin( fAngle ) );
206 69547 : double fDY = fHeight * fabs( cos( fAngle ) ) + fWidth * fabs( sin( fAngle ) );
207 :
208 69547 : fDX = ( fDX - fWidth ) * 0.5 + 0.5;
209 69547 : fDY = ( fDY - fHeight ) * 0.5 + 0.5;
210 :
211 69547 : aRect.Left() -= (long) fDX;
212 69547 : aRect.Right() += (long) fDX;
213 69547 : aRect.Top() -= (long) fDY;
214 69547 : aRect.Bottom() += (long) fDY;
215 :
216 69547 : rBoundRect = aRect;
217 69547 : rCenter = rRect.Center();
218 : }
219 : else
220 : {
221 0 : if( GetStyle() == GradientStyle_SQUARE || GetStyle() == GradientStyle_RECT )
222 : {
223 0 : const double fAngle = nAngle * F_PI1800;
224 0 : const double fWidth = aRect.GetWidth();
225 0 : const double fHeight = aRect.GetHeight();
226 0 : double fDX = fWidth * fabs( cos( fAngle ) ) + fHeight * fabs( sin( fAngle ) );
227 0 : double fDY = fHeight * fabs( cos( fAngle ) ) + fWidth * fabs( sin( fAngle ) );
228 :
229 0 : fDX = ( fDX - fWidth ) * 0.5 + 0.5;
230 0 : fDY = ( fDY - fHeight ) * 0.5 + 0.5;
231 :
232 0 : aRect.Left() -= (long) fDX;
233 0 : aRect.Right() += (long) fDX;
234 0 : aRect.Top() -= (long) fDY;
235 0 : aRect.Bottom() += (long) fDY;
236 : }
237 :
238 0 : Size aSize( aRect.GetSize() );
239 :
240 0 : if( GetStyle() == GradientStyle_RADIAL )
241 : {
242 : // Calculation of radii for circle
243 0 : aSize.Width() = (long)(0.5 + sqrt((double)aSize.Width()*(double)aSize.Width() + (double)aSize.Height()*(double)aSize.Height()));
244 0 : aSize.Height() = aSize.Width();
245 : }
246 0 : else if( GetStyle() == GradientStyle_ELLIPTICAL )
247 : {
248 : // Calculation of radii for ellipse
249 0 : aSize.Width() = (long)( 0.5 + (double) aSize.Width() * 1.4142 );
250 0 : aSize.Height() = (long)( 0.5 + (double) aSize.Height() * 1.4142 );
251 : }
252 :
253 : // Calculate new centers
254 0 : long nZWidth = aRect.GetWidth() * (long) GetOfsX() / 100;
255 0 : long nZHeight = aRect.GetHeight() * (long) GetOfsY() / 100;
256 0 : long nBorderX = (long) GetBorder() * aSize.Width() / 100;
257 0 : long nBorderY = (long) GetBorder() * aSize.Height() / 100;
258 0 : rCenter = Point( aRect.Left() + nZWidth, aRect.Top() + nZHeight );
259 :
260 : // Respect borders
261 0 : aSize.Width() -= nBorderX;
262 0 : aSize.Height() -= nBorderY;
263 :
264 : // Recalculate output rectangle
265 0 : aRect.Left() = rCenter.X() - ( aSize.Width() >> 1 );
266 0 : aRect.Top() = rCenter.Y() - ( aSize.Height() >> 1 );
267 :
268 0 : aRect.SetSize( aSize );
269 0 : rBoundRect = aRect;
270 : }
271 69547 : }
272 :
273 0 : Gradient& Gradient::operator=( const Gradient& rGradient )
274 : {
275 : DBG_CHKTHIS( Gradient, NULL );
276 : DBG_CHKOBJ( &rGradient, Gradient, NULL );
277 :
278 : // Increment refcount first so that we can reference ourselves
279 0 : rGradient.mpImplGradient->mnRefCount++;
280 :
281 : // If it's the last reference, delete it, otherwise decrement
282 0 : if ( mpImplGradient->mnRefCount == 1 )
283 0 : delete mpImplGradient;
284 : else
285 0 : mpImplGradient->mnRefCount--;
286 0 : mpImplGradient = rGradient.mpImplGradient;
287 :
288 0 : return *this;
289 : }
290 :
291 0 : sal_Bool Gradient::operator==( const Gradient& rGradient ) const
292 : {
293 : DBG_CHKTHIS( Gradient, NULL );
294 : DBG_CHKOBJ( &rGradient, Gradient, NULL );
295 :
296 0 : if ( mpImplGradient == rGradient.mpImplGradient )
297 0 : return sal_True;
298 :
299 0 : if ( (mpImplGradient->meStyle == rGradient.mpImplGradient->meStyle) &&
300 0 : (mpImplGradient->mnAngle == rGradient.mpImplGradient->mnAngle) &&
301 0 : (mpImplGradient->mnBorder == rGradient.mpImplGradient->mnBorder) &&
302 0 : (mpImplGradient->mnOfsX == rGradient.mpImplGradient->mnOfsX) &&
303 0 : (mpImplGradient->mnOfsY == rGradient.mpImplGradient->mnOfsY) &&
304 0 : (mpImplGradient->mnStepCount == rGradient.mpImplGradient->mnStepCount) &&
305 0 : (mpImplGradient->mnIntensityStart == rGradient.mpImplGradient->mnIntensityStart) &&
306 0 : (mpImplGradient->mnIntensityEnd == rGradient.mpImplGradient->mnIntensityEnd) &&
307 0 : (mpImplGradient->maStartColor == rGradient.mpImplGradient->maStartColor) &&
308 0 : (mpImplGradient->maEndColor == rGradient.mpImplGradient->maEndColor) )
309 0 : return sal_True;
310 : else
311 0 : return sal_False;
312 : }
313 :
314 0 : SvStream& operator>>( SvStream& rIStm, Impl_Gradient& rImpl_Gradient )
315 : {
316 0 : VersionCompat aCompat( rIStm, STREAM_READ );
317 : sal_uInt16 nTmp16;
318 :
319 0 : rIStm >> nTmp16; rImpl_Gradient.meStyle = (GradientStyle) nTmp16;
320 :
321 0 : rIStm >> rImpl_Gradient.maStartColor >>
322 0 : rImpl_Gradient.maEndColor >>
323 0 : rImpl_Gradient.mnAngle >>
324 0 : rImpl_Gradient.mnBorder >>
325 0 : rImpl_Gradient.mnOfsX >>
326 0 : rImpl_Gradient.mnOfsY >>
327 0 : rImpl_Gradient.mnIntensityStart >>
328 0 : rImpl_Gradient.mnIntensityEnd >>
329 0 : rImpl_Gradient.mnStepCount;
330 :
331 0 : return rIStm;
332 : }
333 :
334 0 : SvStream& operator<<( SvStream& rOStm, const Impl_Gradient& rImpl_Gradient )
335 : {
336 0 : VersionCompat aCompat( rOStm, STREAM_WRITE, 1 );
337 :
338 0 : rOStm << (sal_uInt16) rImpl_Gradient.meStyle <<
339 0 : rImpl_Gradient.maStartColor <<
340 0 : rImpl_Gradient.maEndColor <<
341 0 : rImpl_Gradient.mnAngle <<
342 0 : rImpl_Gradient.mnBorder <<
343 0 : rImpl_Gradient.mnOfsX <<
344 0 : rImpl_Gradient.mnOfsY <<
345 0 : rImpl_Gradient.mnIntensityStart <<
346 0 : rImpl_Gradient.mnIntensityEnd <<
347 0 : rImpl_Gradient.mnStepCount;
348 :
349 0 : return rOStm;
350 : }
351 :
352 0 : SvStream& operator>>( SvStream& rIStm, Gradient& rGradient )
353 : {
354 0 : rGradient.MakeUnique();
355 0 : return( rIStm >> *rGradient.mpImplGradient );
356 : }
357 :
358 0 : SvStream& operator<<( SvStream& rOStm, const Gradient& rGradient )
359 : {
360 0 : return( rOStm << *rGradient.mpImplGradient );
361 : }
362 :
363 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|