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 <tools/gen.hxx>
24 #include <vcl/gradient.hxx>
26 Impl_Gradient::Impl_Gradient() :
27 maStartColor( COL_BLACK
),
28 maEndColor( COL_WHITE
)
31 meStyle
= GradientStyle_LINEAR
;
36 mnIntensityStart
= 100;
41 Impl_Gradient::Impl_Gradient( const Impl_Gradient
& rImplGradient
) :
42 maStartColor( rImplGradient
.maStartColor
),
43 maEndColor( rImplGradient
.maEndColor
)
46 meStyle
= rImplGradient
.meStyle
;
47 mnAngle
= rImplGradient
.mnAngle
;
48 mnBorder
= rImplGradient
.mnBorder
;
49 mnOfsX
= rImplGradient
.mnOfsX
;
50 mnOfsY
= rImplGradient
.mnOfsY
;
51 mnIntensityStart
= rImplGradient
.mnIntensityStart
;
52 mnIntensityEnd
= rImplGradient
.mnIntensityEnd
;
53 mnStepCount
= rImplGradient
.mnStepCount
;
56 void Gradient::MakeUnique()
58 // If there are still other references, copy
59 if ( mpImplGradient
->mnRefCount
!= 1 )
61 if( mpImplGradient
->mnRefCount
)
62 mpImplGradient
->mnRefCount
--;
64 mpImplGradient
= new Impl_Gradient( *mpImplGradient
);
71 mpImplGradient
= new Impl_Gradient
;
74 Gradient::Gradient( const Gradient
& rGradient
)
77 // Take over instance data and increment refcount
78 mpImplGradient
= rGradient
.mpImplGradient
;
79 mpImplGradient
->mnRefCount
++;
82 Gradient::Gradient( GradientStyle eStyle
,
83 const Color
& rStartColor
, const Color
& rEndColor
)
86 mpImplGradient
= new Impl_Gradient
;
87 mpImplGradient
->meStyle
= eStyle
;
88 mpImplGradient
->maStartColor
= rStartColor
;
89 mpImplGradient
->maEndColor
= rEndColor
;
95 // If it's the last reference, delete it, otherwise
97 if ( mpImplGradient
->mnRefCount
== 1 )
98 delete mpImplGradient
;
100 mpImplGradient
->mnRefCount
--;
103 void Gradient::SetStyle( GradientStyle eStyle
)
107 mpImplGradient
->meStyle
= eStyle
;
110 void Gradient::SetStartColor( const Color
& rColor
)
114 mpImplGradient
->maStartColor
= rColor
;
117 void Gradient::SetEndColor( const Color
& rColor
)
121 mpImplGradient
->maEndColor
= rColor
;
124 void Gradient::SetAngle( sal_uInt16 nAngle
)
128 mpImplGradient
->mnAngle
= nAngle
;
131 void Gradient::SetBorder( sal_uInt16 nBorder
)
135 mpImplGradient
->mnBorder
= nBorder
;
138 void Gradient::SetOfsX( sal_uInt16 nOfsX
)
142 mpImplGradient
->mnOfsX
= nOfsX
;
145 void Gradient::SetOfsY( sal_uInt16 nOfsY
)
149 mpImplGradient
->mnOfsY
= nOfsY
;
152 void Gradient::SetStartIntensity( sal_uInt16 nIntens
)
156 mpImplGradient
->mnIntensityStart
= nIntens
;
159 void Gradient::SetEndIntensity( sal_uInt16 nIntens
)
163 mpImplGradient
->mnIntensityEnd
= nIntens
;
166 void Gradient::SetSteps( sal_uInt16 nSteps
)
170 mpImplGradient
->mnStepCount
= nSteps
;
173 void Gradient::GetBoundRect( const Rectangle
& rRect
, Rectangle
& rBoundRect
, Point
& rCenter
) const
175 Rectangle
aRect( rRect
);
176 sal_uInt16 nAngle
= GetAngle() % 3600;
178 if( GetStyle() == GradientStyle_LINEAR
|| GetStyle() == GradientStyle_AXIAL
)
180 const double fAngle
= nAngle
* F_PI1800
;
181 const double fWidth
= aRect
.GetWidth();
182 const double fHeight
= aRect
.GetHeight();
183 double fDX
= fWidth
* fabs( cos( fAngle
) ) +
184 fHeight
* fabs( sin( fAngle
) );
185 double fDY
= fHeight
* fabs( cos( fAngle
) ) +
186 fWidth
* fabs( sin( fAngle
) );
187 fDX
= (fDX
- fWidth
) * 0.5 + 0.5;
188 fDY
= (fDY
- fHeight
) * 0.5 + 0.5;
189 aRect
.Left() -= (long) fDX
;
190 aRect
.Right() += (long) fDX
;
191 aRect
.Top() -= (long) fDY
;
192 aRect
.Bottom() += (long) fDY
;
195 rCenter
= rRect
.Center();
199 if( GetStyle() == GradientStyle_SQUARE
|| GetStyle() == GradientStyle_RECT
)
201 const double fAngle
= nAngle
* F_PI1800
;
202 const double fWidth
= aRect
.GetWidth();
203 const double fHeight
= aRect
.GetHeight();
204 double fDX
= fWidth
* fabs( cos( fAngle
) ) + fHeight
* fabs( sin( fAngle
) );
205 double fDY
= fHeight
* fabs( cos( fAngle
) ) + fWidth
* fabs( sin( fAngle
) );
207 fDX
= ( fDX
- fWidth
) * 0.5 + 0.5;
208 fDY
= ( fDY
- fHeight
) * 0.5 + 0.5;
210 aRect
.Left() -= (long) fDX
;
211 aRect
.Right() += (long) fDX
;
212 aRect
.Top() -= (long) fDY
;
213 aRect
.Bottom() += (long) fDY
;
216 Size
aSize( aRect
.GetSize() );
218 if( GetStyle() == GradientStyle_RADIAL
)
220 // Calculation of radii for circle
221 aSize
.Width() = (long)(0.5 + sqrt((double)aSize
.Width()*(double)aSize
.Width() + (double)aSize
.Height()*(double)aSize
.Height()));
222 aSize
.Height() = aSize
.Width();
224 else if( GetStyle() == GradientStyle_ELLIPTICAL
)
226 // Calculation of radii for ellipse
227 aSize
.Width() = (long)( 0.5 + (double) aSize
.Width() * 1.4142 );
228 aSize
.Height() = (long)( 0.5 + (double) aSize
.Height() * 1.4142 );
231 // Calculate new centers
232 long nZWidth
= aRect
.GetWidth() * (long) GetOfsX() / 100;
233 long nZHeight
= aRect
.GetHeight() * (long) GetOfsY() / 100;
234 long nBorderX
= (long) GetBorder() * aSize
.Width() / 100;
235 long nBorderY
= (long) GetBorder() * aSize
.Height() / 100;
236 rCenter
= Point( aRect
.Left() + nZWidth
, aRect
.Top() + nZHeight
);
239 aSize
.Width() -= nBorderX
;
240 aSize
.Height() -= nBorderY
;
242 // Recalculate output rectangle
243 aRect
.Left() = rCenter
.X() - ( aSize
.Width() >> 1 );
244 aRect
.Top() = rCenter
.Y() - ( aSize
.Height() >> 1 );
246 aRect
.SetSize( aSize
);
251 Gradient
& Gradient::operator=( const Gradient
& rGradient
)
254 // Increment refcount first so that we can reference ourselves
255 rGradient
.mpImplGradient
->mnRefCount
++;
257 // If it's the last reference, delete it, otherwise decrement
258 if ( mpImplGradient
->mnRefCount
== 1 )
259 delete mpImplGradient
;
261 mpImplGradient
->mnRefCount
--;
262 mpImplGradient
= rGradient
.mpImplGradient
;
267 bool Gradient::operator==( const Gradient
& rGradient
) const
270 if ( mpImplGradient
== rGradient
.mpImplGradient
)
273 if ( (mpImplGradient
->meStyle
== rGradient
.mpImplGradient
->meStyle
) &&
274 (mpImplGradient
->mnAngle
== rGradient
.mpImplGradient
->mnAngle
) &&
275 (mpImplGradient
->mnBorder
== rGradient
.mpImplGradient
->mnBorder
) &&
276 (mpImplGradient
->mnOfsX
== rGradient
.mpImplGradient
->mnOfsX
) &&
277 (mpImplGradient
->mnOfsY
== rGradient
.mpImplGradient
->mnOfsY
) &&
278 (mpImplGradient
->mnStepCount
== rGradient
.mpImplGradient
->mnStepCount
) &&
279 (mpImplGradient
->mnIntensityStart
== rGradient
.mpImplGradient
->mnIntensityStart
) &&
280 (mpImplGradient
->mnIntensityEnd
== rGradient
.mpImplGradient
->mnIntensityEnd
) &&
281 (mpImplGradient
->maStartColor
== rGradient
.mpImplGradient
->maStartColor
) &&
282 (mpImplGradient
->maEndColor
== rGradient
.mpImplGradient
->maEndColor
) )
288 SvStream
& ReadImpl_Gradient( SvStream
& rIStm
, Impl_Gradient
& rImpl_Gradient
)
290 VersionCompat
aCompat( rIStm
, StreamMode::READ
);
293 rIStm
.ReadUInt16( nTmp16
); rImpl_Gradient
.meStyle
= (GradientStyle
) nTmp16
;
295 ReadColor( rIStm
, rImpl_Gradient
.maStartColor
);
296 ReadColor( rIStm
, rImpl_Gradient
.maEndColor
);
297 rIStm
.ReadUInt16( rImpl_Gradient
.mnAngle
). ReadUInt16( rImpl_Gradient
.mnBorder
). ReadUInt16( rImpl_Gradient
.mnOfsX
). ReadUInt16( rImpl_Gradient
.mnOfsY
). ReadUInt16( rImpl_Gradient
.mnIntensityStart
). ReadUInt16( rImpl_Gradient
.mnIntensityEnd
). ReadUInt16( rImpl_Gradient
.mnStepCount
);
302 SvStream
& WriteImpl_Gradient( SvStream
& rOStm
, const Impl_Gradient
& rImpl_Gradient
)
304 VersionCompat
aCompat( rOStm
, StreamMode::WRITE
, 1 );
306 rOStm
.WriteUInt16( rImpl_Gradient
.meStyle
);
307 WriteColor( rOStm
, rImpl_Gradient
.maStartColor
);
308 WriteColor( rOStm
, rImpl_Gradient
.maEndColor
);
309 rOStm
.WriteUInt16( rImpl_Gradient
.mnAngle
)
310 .WriteUInt16( rImpl_Gradient
.mnBorder
)
311 .WriteUInt16( rImpl_Gradient
.mnOfsX
)
312 .WriteUInt16( rImpl_Gradient
.mnOfsY
)
313 .WriteUInt16( rImpl_Gradient
.mnIntensityStart
)
314 .WriteUInt16( rImpl_Gradient
.mnIntensityEnd
)
315 .WriteUInt16( rImpl_Gradient
.mnStepCount
);
320 SvStream
& ReadGradient( SvStream
& rIStm
, Gradient
& rGradient
)
322 rGradient
.MakeUnique();
323 return ReadImpl_Gradient( rIStm
, *rGradient
.mpImplGradient
);
326 SvStream
& WriteGradient( SvStream
& rOStm
, const Gradient
& rGradient
)
328 return WriteImpl_Gradient( rOStm
, *rGradient
.mpImplGradient
);
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */