xref: /trunk/main/vcl/source/gdi/gradient.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 #include <tools/stream.hxx>
31 #include <tools/vcompat.hxx>
32 #include <tools/debug.hxx>
33 #include <vcl/gradient.hxx>
34 
35 // =======================================================================
36 
37 DBG_NAME( Gradient )
38 
39 // -----------------------------------------------------------------------
40 
41 Impl_Gradient::Impl_Gradient() :
42     maStartColor( COL_BLACK ),
43     maEndColor( COL_WHITE )
44 {
45     mnRefCount          = 1;
46     meStyle             = GRADIENT_LINEAR;
47     mnAngle             = 0;
48     mnBorder            = 0;
49     mnOfsX              = 50;
50     mnOfsY              = 50;
51     mnIntensityStart    = 100;
52     mnIntensityEnd      = 100;
53     mnStepCount         = 0;
54 }
55 
56 // -----------------------------------------------------------------------
57 
58 Impl_Gradient::Impl_Gradient( const Impl_Gradient& rImplGradient ) :
59     maStartColor( rImplGradient.maStartColor ),
60     maEndColor( rImplGradient.maEndColor )
61 {
62     mnRefCount          = 1;
63     meStyle             = rImplGradient.meStyle;
64     mnAngle             = rImplGradient.mnAngle;
65     mnBorder            = rImplGradient.mnBorder;
66     mnOfsX              = rImplGradient.mnOfsX;
67     mnOfsY              = rImplGradient.mnOfsY;
68     mnIntensityStart    = rImplGradient.mnIntensityStart;
69     mnIntensityEnd      = rImplGradient.mnIntensityEnd;
70     mnStepCount         = rImplGradient.mnStepCount;
71 }
72 
73 // -----------------------------------------------------------------------
74 
75 void Gradient::MakeUnique()
76 {
77     // Falls noch andere Referenzen bestehen, dann kopieren
78     if ( mpImplGradient->mnRefCount != 1 )
79 	{
80 		if( mpImplGradient->mnRefCount )
81 			mpImplGradient->mnRefCount--;
82 
83         mpImplGradient = new Impl_Gradient( *mpImplGradient );
84 	}
85 }
86 
87 // -----------------------------------------------------------------------
88 
89 Gradient::Gradient()
90 {
91     DBG_CTOR( Gradient, NULL );
92 
93     mpImplGradient = new Impl_Gradient;
94 }
95 
96 // -----------------------------------------------------------------------
97 
98 Gradient::Gradient( const Gradient& rGradient )
99 {
100     DBG_CTOR( Gradient, NULL );
101     DBG_CHKOBJ( &rGradient, Gradient, NULL );
102 
103     // Instance Daten uebernehmen und Referenzcounter erhoehen
104     mpImplGradient = rGradient.mpImplGradient;
105     mpImplGradient->mnRefCount++;
106 }
107 
108 // -----------------------------------------------------------------------
109 
110 Gradient::Gradient( GradientStyle eStyle )
111 {
112     DBG_CTOR( Gradient, NULL );
113 
114     mpImplGradient          = new Impl_Gradient;
115     mpImplGradient->meStyle = eStyle;
116 }
117 
118 // -----------------------------------------------------------------------
119 
120 Gradient::Gradient( GradientStyle eStyle,
121                     const Color& rStartColor, const Color& rEndColor )
122 {
123     DBG_CTOR( Gradient, NULL );
124 
125     mpImplGradient                  = new Impl_Gradient;
126     mpImplGradient->meStyle         = eStyle;
127     mpImplGradient->maStartColor    = rStartColor;
128     mpImplGradient->maEndColor      = rEndColor;
129 }
130 
131 // -----------------------------------------------------------------------
132 
133 Gradient::~Gradient()
134 {
135     DBG_DTOR( Gradient, NULL );
136 
137     // Wenn es die letzte Referenz ist, loeschen,
138     // sonst Referenzcounter decrementieren
139     if ( mpImplGradient->mnRefCount == 1 )
140         delete mpImplGradient;
141     else
142         mpImplGradient->mnRefCount--;
143 }
144 
145 // -----------------------------------------------------------------------
146 
147 void Gradient::SetStyle( GradientStyle eStyle )
148 {
149     DBG_CHKTHIS( Gradient, NULL );
150 
151     MakeUnique();
152     mpImplGradient->meStyle = eStyle;
153 }
154 
155 // -----------------------------------------------------------------------
156 
157 void Gradient::SetStartColor( const Color& rColor )
158 {
159     DBG_CHKTHIS( Gradient, NULL );
160 
161     MakeUnique();
162     mpImplGradient->maStartColor = rColor;
163 }
164 
165 // -----------------------------------------------------------------------
166 
167 void Gradient::SetEndColor( const Color& rColor )
168 {
169     DBG_CHKTHIS( Gradient, NULL );
170 
171     MakeUnique();
172     mpImplGradient->maEndColor = rColor;
173 }
174 
175 // -----------------------------------------------------------------------
176 
177 void Gradient::SetAngle( sal_uInt16 nAngle )
178 {
179     DBG_CHKTHIS( Gradient, NULL );
180 
181     MakeUnique();
182     mpImplGradient->mnAngle = nAngle;
183 }
184 
185 // -----------------------------------------------------------------------
186 
187 void Gradient::SetBorder( sal_uInt16 nBorder )
188 {
189     DBG_CHKTHIS( Gradient, NULL );
190 
191     MakeUnique();
192     mpImplGradient->mnBorder = nBorder;
193 }
194 
195 // -----------------------------------------------------------------------
196 
197 void Gradient::SetOfsX( sal_uInt16 nOfsX )
198 {
199     DBG_CHKTHIS( Gradient, NULL );
200 
201     MakeUnique();
202     mpImplGradient->mnOfsX = nOfsX;
203 }
204 
205 // -----------------------------------------------------------------------
206 
207 void Gradient::SetOfsY( sal_uInt16 nOfsY )
208 {
209     DBG_CHKTHIS( Gradient, NULL );
210 
211     MakeUnique();
212     mpImplGradient->mnOfsY = nOfsY;
213 }
214 
215 // -----------------------------------------------------------------------
216 
217 void Gradient::SetStartIntensity( sal_uInt16 nIntens )
218 {
219     DBG_CHKTHIS( Gradient, NULL );
220 
221     MakeUnique();
222     mpImplGradient->mnIntensityStart = nIntens;
223 }
224 
225 // -----------------------------------------------------------------------
226 
227 void Gradient::SetEndIntensity( sal_uInt16 nIntens )
228 {
229     DBG_CHKTHIS( Gradient, NULL );
230 
231     MakeUnique();
232     mpImplGradient->mnIntensityEnd = nIntens;
233 }
234 
235 // -----------------------------------------------------------------------
236 
237 void Gradient::SetSteps( sal_uInt16 nSteps )
238 {
239     DBG_CHKTHIS( Gradient, NULL );
240 
241     MakeUnique();
242     mpImplGradient->mnStepCount = nSteps;
243 }
244 
245 // -----------------------------------------------------------------------
246 
247 Gradient& Gradient::operator=( const Gradient& rGradient )
248 {
249     DBG_CHKTHIS( Gradient, NULL );
250     DBG_CHKOBJ( &rGradient, Gradient, NULL );
251 
252     // Zuerst Referenzcounter erhoehen, damit man sich selbst zuweisen kann
253     rGradient.mpImplGradient->mnRefCount++;
254 
255     // Wenn es die letzte Referenz ist, loeschen,
256     // sonst Referenzcounter decrementieren
257     if ( mpImplGradient->mnRefCount == 1 )
258         delete mpImplGradient;
259     else
260         mpImplGradient->mnRefCount--;
261     mpImplGradient = rGradient.mpImplGradient;
262 
263     return *this;
264 }
265 
266 // -----------------------------------------------------------------------
267 
268 sal_Bool Gradient::operator==( const Gradient& rGradient ) const
269 {
270     DBG_CHKTHIS( Gradient, NULL );
271     DBG_CHKOBJ( &rGradient, Gradient, NULL );
272 
273     if ( mpImplGradient == rGradient.mpImplGradient )
274         return sal_True;
275 
276     if ( (mpImplGradient->meStyle           == rGradient.mpImplGradient->meStyle)           ||
277          (mpImplGradient->mnAngle           == rGradient.mpImplGradient->mnAngle)           ||
278          (mpImplGradient->mnBorder          == rGradient.mpImplGradient->mnBorder)          ||
279          (mpImplGradient->mnOfsX            == rGradient.mpImplGradient->mnOfsX)            ||
280          (mpImplGradient->mnOfsY            == rGradient.mpImplGradient->mnOfsY)            ||
281          (mpImplGradient->mnStepCount       == rGradient.mpImplGradient->mnStepCount)       ||
282          (mpImplGradient->mnIntensityStart  == rGradient.mpImplGradient->mnIntensityStart)  ||
283          (mpImplGradient->mnIntensityEnd    == rGradient.mpImplGradient->mnIntensityEnd)    ||
284          (mpImplGradient->maStartColor      == rGradient.mpImplGradient->maStartColor)      ||
285          (mpImplGradient->maEndColor        == rGradient.mpImplGradient->maEndColor) )
286          return sal_True;
287     else
288         return sal_False;
289 }
290 
291 SvStream& operator>>( SvStream& rIStm, Impl_Gradient& rImpl_Gradient )
292 {
293     VersionCompat	aCompat( rIStm, STREAM_READ );
294     sal_uInt16			nTmp16;
295 
296     rIStm >> nTmp16; rImpl_Gradient.meStyle = (GradientStyle) nTmp16;
297 
298     rIStm >> rImpl_Gradient.maStartColor >>
299              rImpl_Gradient.maEndColor >>
300              rImpl_Gradient.mnAngle >>
301              rImpl_Gradient.mnBorder >>
302              rImpl_Gradient.mnOfsX >>
303              rImpl_Gradient.mnOfsY >>
304              rImpl_Gradient.mnIntensityStart >>
305              rImpl_Gradient.mnIntensityEnd >>
306              rImpl_Gradient.mnStepCount;
307 
308     return rIStm;
309 }
310 
311 // -----------------------------------------------------------------------
312 
313 SvStream& operator<<( SvStream& rOStm, const Impl_Gradient& rImpl_Gradient )
314 {
315     VersionCompat aCompat( rOStm, STREAM_WRITE, 1 );
316 
317     rOStm << (sal_uInt16) rImpl_Gradient.meStyle <<
318              rImpl_Gradient.maStartColor <<
319              rImpl_Gradient.maEndColor <<
320              rImpl_Gradient.mnAngle <<
321              rImpl_Gradient.mnBorder <<
322              rImpl_Gradient.mnOfsX <<
323              rImpl_Gradient.mnOfsY <<
324              rImpl_Gradient.mnIntensityStart <<
325              rImpl_Gradient.mnIntensityEnd <<
326              rImpl_Gradient.mnStepCount;
327 
328     return rOStm;
329 }
330 
331 // -----------------------------------------------------------------------
332 
333 SvStream& operator>>( SvStream& rIStm, Gradient& rGradient )
334 {
335     rGradient.MakeUnique();
336     return( rIStm >> *rGradient.mpImplGradient );
337 }
338 
339 // -----------------------------------------------------------------------
340 
341 SvStream& operator<<( SvStream& rOStm, const Gradient& rGradient )
342 {
343     return( rOStm << *rGradient.mpImplGradient );
344 }
345