merge the formfield patch from ooo-build
[ooovba.git] / xmloff / source / style / TransGradientStyle.cxx
blob68a55971337098b86f42ea8e8da0e898a8e3c4a3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TransGradientStyle.cxx,v $
10 * $Revision: 1.18 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include "TransGradientStyle.hxx"
34 #include <com/sun/star/awt/Gradient.hpp>
35 #include <xmloff/attrlist.hxx>
36 #include <xmloff/nmspmap.hxx>
37 #include <xmloff/xmluconv.hxx>
38 #include "xmlnmspe.hxx"
39 #include <rtl/ustrbuf.hxx>
40 #include <rtl/ustring.hxx>
41 #include <tools/debug.hxx>
42 #include <xmloff/xmltkmap.hxx>
43 #include <xmloff/xmlexp.hxx>
44 #include <xmloff/xmlimp.hxx>
47 using namespace ::com::sun::star;
48 using ::rtl::OUString;
49 using ::rtl::OUStringBuffer;
51 using namespace ::xmloff::token;
53 enum SvXMLTokenMapAttrs
55 XML_TOK_GRADIENT_NAME,
56 XML_TOK_GRADIENT_DISPLAY_NAME,
57 XML_TOK_GRADIENT_STYLE,
58 XML_TOK_GRADIENT_CX,
59 XML_TOK_GRADIENT_CY,
60 XML_TOK_GRADIENT_START,
61 XML_TOK_GRADIENT_END,
62 XML_TOK_GRADIENT_ANGLE,
63 XML_TOK_GRADIENT_BORDER,
64 XML_TOK_TABSTOP_END=XML_TOK_UNKNOWN
68 SvXMLEnumMapEntry __READONLY_DATA pXML_GradientStyle_Enum[] =
70 { XML_GRADIENTSTYLE_LINEAR, awt::GradientStyle_LINEAR },
71 { XML_GRADIENTSTYLE_AXIAL, awt::GradientStyle_AXIAL },
72 { XML_GRADIENTSTYLE_RADIAL, awt::GradientStyle_RADIAL },
73 { XML_GRADIENTSTYLE_ELLIPSOID, awt::GradientStyle_ELLIPTICAL },
74 { XML_GRADIENTSTYLE_SQUARE, awt::GradientStyle_SQUARE },
75 { XML_GRADIENTSTYLE_RECTANGULAR, awt::GradientStyle_RECT },
76 { XML_TOKEN_INVALID, 0 }
80 //-------------------------------------------------------------
81 // Import
82 //-------------------------------------------------------------
84 XMLTransGradientStyleImport::XMLTransGradientStyleImport( SvXMLImport& rImp )
85 : rImport(rImp)
89 XMLTransGradientStyleImport::~XMLTransGradientStyleImport()
93 sal_Bool XMLTransGradientStyleImport::importXML(
94 const uno::Reference< xml::sax::XAttributeList >& xAttrList,
95 uno::Any& rValue,
96 OUString& rStrName )
98 sal_Bool bRet = sal_False;
99 sal_Bool bHasName = sal_False;
100 sal_Bool bHasStyle = sal_False;
101 OUString aDisplayName;
103 awt::Gradient aGradient;
104 aGradient.XOffset = 0;
105 aGradient.YOffset = 0;
106 aGradient.StartIntensity = 100;
107 aGradient.EndIntensity = 100;
108 aGradient.Angle = 0;
109 aGradient.Border = 0;
112 static __FAR_DATA SvXMLTokenMapEntry aTrGradientAttrTokenMap[] =
114 { XML_NAMESPACE_DRAW, XML_NAME, XML_TOK_GRADIENT_NAME },
115 { XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, XML_TOK_GRADIENT_DISPLAY_NAME },
116 { XML_NAMESPACE_DRAW, XML_STYLE, XML_TOK_GRADIENT_STYLE },
117 { XML_NAMESPACE_DRAW, XML_CX, XML_TOK_GRADIENT_CX },
118 { XML_NAMESPACE_DRAW, XML_CY, XML_TOK_GRADIENT_CY },
119 { XML_NAMESPACE_DRAW, XML_START, XML_TOK_GRADIENT_START },
120 { XML_NAMESPACE_DRAW, XML_END, XML_TOK_GRADIENT_END },
121 { XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, XML_TOK_GRADIENT_ANGLE },
122 { XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, XML_TOK_GRADIENT_BORDER },
123 XML_TOKEN_MAP_END
126 SvXMLTokenMap aTokenMap( aTrGradientAttrTokenMap );
127 SvXMLNamespaceMap& rNamespaceMap = rImport.GetNamespaceMap();
129 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
130 for( sal_Int16 i=0; i < nAttrCount; i++ )
132 const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
133 OUString aStrAttrName;
134 sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
135 const OUString& rStrValue = xAttrList->getValueByIndex( i );
137 sal_Int32 nTmpValue;
139 switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
141 case XML_TOK_GRADIENT_NAME:
143 rStrName = rStrValue;
144 bHasName = sal_True;
146 break;
147 case XML_TOK_GRADIENT_DISPLAY_NAME:
149 aDisplayName = rStrValue;
151 break;
152 case XML_TOK_GRADIENT_STYLE:
154 sal_uInt16 eValue;
155 if( SvXMLUnitConverter::convertEnum( eValue, rStrValue, pXML_GradientStyle_Enum ) )
157 aGradient.Style = (awt::GradientStyle) eValue;
158 bHasStyle = sal_True;
161 break;
162 case XML_TOK_GRADIENT_CX:
163 SvXMLUnitConverter::convertPercent( nTmpValue, rStrValue );
164 aGradient.XOffset = sal::static_int_cast< sal_Int16 >(nTmpValue);
165 break;
166 case XML_TOK_GRADIENT_CY:
167 SvXMLUnitConverter::convertPercent( nTmpValue, rStrValue );
168 aGradient.YOffset = sal::static_int_cast< sal_Int16 >(nTmpValue);
169 break;
170 case XML_TOK_GRADIENT_START:
172 sal_Int32 aStartTransparency;
173 SvXMLUnitConverter::convertPercent( aStartTransparency, rStrValue );
175 UINT8 n = sal::static_int_cast< UINT8 >(
176 ( (100 - aStartTransparency) * 255 ) / 100 );
178 Color aColor( n, n, n );
179 aGradient.StartColor = (sal_Int32)( aColor.GetColor() );
181 break;
182 case XML_TOK_GRADIENT_END:
184 sal_Int32 aEndTransparency;
185 SvXMLUnitConverter::convertPercent( aEndTransparency, rStrValue );
187 UINT8 n = sal::static_int_cast< UINT8 >(
188 ( (100 - aEndTransparency) * 255 ) / 100 );
190 Color aColor( n, n, n );
191 aGradient.EndColor = (sal_Int32)( aColor.GetColor() );
193 break;
194 case XML_TOK_GRADIENT_ANGLE:
196 sal_Int32 nValue;
197 SvXMLUnitConverter::convertNumber( nValue, rStrValue, 0, 3600 );
198 aGradient.Angle = sal_Int16( nValue );
200 break;
201 case XML_TOK_GRADIENT_BORDER:
202 SvXMLUnitConverter::convertPercent( nTmpValue, rStrValue );
203 aGradient.Border = sal::static_int_cast< sal_Int16 >(nTmpValue);
204 break;
206 default:
207 DBG_WARNING( "Unknown token at import transparency gradient style" )
212 rValue <<= aGradient;
214 if( aDisplayName.getLength() )
216 rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_GRADIENT_ID, rStrName,
217 aDisplayName );
218 rStrName = aDisplayName;
221 bRet = bHasName && bHasStyle;
225 return bRet;
229 //-------------------------------------------------------------
230 // Export
231 //-------------------------------------------------------------
233 #ifndef SVX_LIGHT
235 XMLTransGradientStyleExport::XMLTransGradientStyleExport( SvXMLExport& rExp )
236 : rExport(rExp)
240 XMLTransGradientStyleExport::~XMLTransGradientStyleExport()
245 sal_Bool XMLTransGradientStyleExport::exportXML(
246 const OUString& rStrName,
247 const uno::Any& rValue )
249 sal_Bool bRet = sal_False;
250 awt::Gradient aGradient;
252 if( rStrName.getLength() )
254 if( rValue >>= aGradient )
256 OUString aStrValue;
257 OUStringBuffer aOut;
259 // Style
260 if( !SvXMLUnitConverter::convertEnum( aOut, aGradient.Style, pXML_GradientStyle_Enum ) )
262 bRet = sal_False;
264 else
266 // Name
267 sal_Bool bEncoded = sal_False;
268 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
269 rExport.EncodeStyleName( rStrName,
270 &bEncoded ) );
271 if( bEncoded )
272 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
273 rStrName );
275 aStrValue = aOut.makeStringAndClear();
276 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
278 // Center x/y
279 if( aGradient.Style != awt::GradientStyle_LINEAR &&
280 aGradient.Style != awt::GradientStyle_AXIAL )
282 SvXMLUnitConverter::convertPercent( aOut, aGradient.XOffset );
283 aStrValue = aOut.makeStringAndClear();
284 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_CX, aStrValue );
286 SvXMLUnitConverter::convertPercent( aOut, aGradient.YOffset );
287 aStrValue = aOut.makeStringAndClear();
288 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_CY, aStrValue );
292 Color aColor;
294 // Transparency start
295 aColor.SetColor( aGradient.StartColor );
296 sal_Int32 aStartValue = 100 - (sal_Int32)(((aColor.GetRed() + 1) * 100) / 255);
297 SvXMLUnitConverter::convertPercent( aOut, aStartValue );
298 aStrValue = aOut.makeStringAndClear();
299 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_START, aStrValue );
301 // Transparency end
302 aColor.SetColor( aGradient.EndColor );
303 sal_Int32 aEndValue = 100 - (sal_Int32)(((aColor.GetRed() + 1) * 100) / 255);
304 SvXMLUnitConverter::convertPercent( aOut, aEndValue );
305 aStrValue = aOut.makeStringAndClear();
306 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_END, aStrValue );
308 // Angle
309 if( aGradient.Style != awt::GradientStyle_RADIAL )
311 SvXMLUnitConverter::convertNumber( aOut, sal_Int32( aGradient.Angle ) );
312 aStrValue = aOut.makeStringAndClear();
313 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, aStrValue );
316 // Border
317 SvXMLUnitConverter::convertPercent( aOut, aGradient.Border );
318 aStrValue = aOut.makeStringAndClear();
319 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, aStrValue );
321 // Do Write
322 SvXMLElementExport rElem( rExport,
323 XML_NAMESPACE_DRAW, XML_OPACITY,
324 sal_True, sal_False );
329 return bRet;
332 #endif // #ifndef SVX_LIGHT