merge the formfield patch from ooo-build
[ooovba.git] / xmloff / source / style / GradientStyle.cxx
blobac8dff99e38fbf1311f04bc3da883d3b0e85a600
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: GradientStyle.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 "GradientStyle.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 <xmloff/xmltoken.hxx>
40 #include <rtl/ustrbuf.hxx>
41 #include <rtl/ustring.hxx>
42 #include <tools/debug.hxx>
43 #include <xmloff/xmltkmap.hxx>
44 #include <xmloff/xmlexp.hxx>
45 #include <xmloff/xmlimp.hxx>
49 using namespace ::com::sun::star;
50 using ::rtl::OUString;
51 using ::rtl::OUStringBuffer;
53 using namespace ::xmloff::token;
55 enum SvXMLTokenMapAttrs
57 XML_TOK_GRADIENT_NAME,
58 XML_TOK_GRADIENT_DISPLAY_NAME,
59 XML_TOK_GRADIENT_STYLE,
60 XML_TOK_GRADIENT_CX,
61 XML_TOK_GRADIENT_CY,
62 XML_TOK_GRADIENT_STARTCOLOR,
63 XML_TOK_GRADIENT_ENDCOLOR,
64 XML_TOK_GRADIENT_STARTINT,
65 XML_TOK_GRADIENT_ENDINT,
66 XML_TOK_GRADIENT_ANGLE,
67 XML_TOK_GRADIENT_BORDER,
68 XML_TOK_TABSTOP_END=XML_TOK_UNKNOWN
72 SvXMLEnumMapEntry __READONLY_DATA pXML_GradientStyle_Enum[] =
74 { XML_GRADIENTSTYLE_LINEAR, awt::GradientStyle_LINEAR },
75 { XML_GRADIENTSTYLE_AXIAL, awt::GradientStyle_AXIAL },
76 { XML_GRADIENTSTYLE_RADIAL, awt::GradientStyle_RADIAL },
77 { XML_GRADIENTSTYLE_ELLIPSOID, awt::GradientStyle_ELLIPTICAL },
78 { XML_GRADIENTSTYLE_SQUARE, awt::GradientStyle_SQUARE },
79 { XML_GRADIENTSTYLE_RECTANGULAR, awt::GradientStyle_RECT },
80 { XML_TOKEN_INVALID, 0 }
83 //-------------------------------------------------------------
84 // Import
85 //-------------------------------------------------------------
86 XMLGradientStyleImport::XMLGradientStyleImport(
87 SvXMLImport& rImp )
88 : rImport(rImp)
92 XMLGradientStyleImport::~XMLGradientStyleImport()
96 sal_Bool XMLGradientStyleImport::importXML(
97 const uno::Reference< xml::sax::XAttributeList >& xAttrList,
98 uno::Any& rValue,
99 OUString& rStrName )
101 sal_Bool bRet = sal_False;
102 sal_Bool bHasName = sal_False;
103 sal_Bool bHasStyle = sal_False;
104 sal_Bool bHasStartColor = sal_False;
105 sal_Bool bHasEndColor = sal_False;
106 OUString aDisplayName;
108 awt::Gradient aGradient;
109 aGradient.XOffset = 0;
110 aGradient.YOffset = 0;
111 aGradient.StartIntensity = 100;
112 aGradient.EndIntensity = 100;
113 aGradient.Angle = 0;
114 aGradient.Border = 0;
117 static __FAR_DATA SvXMLTokenMapEntry aGradientAttrTokenMap[] =
119 { XML_NAMESPACE_DRAW, XML_NAME, XML_TOK_GRADIENT_NAME },
120 { XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, XML_TOK_GRADIENT_DISPLAY_NAME },
121 { XML_NAMESPACE_DRAW, XML_STYLE, XML_TOK_GRADIENT_STYLE },
122 { XML_NAMESPACE_DRAW, XML_CX, XML_TOK_GRADIENT_CX },
123 { XML_NAMESPACE_DRAW, XML_CY, XML_TOK_GRADIENT_CY },
124 { XML_NAMESPACE_DRAW, XML_START_COLOR, XML_TOK_GRADIENT_STARTCOLOR },
125 { XML_NAMESPACE_DRAW, XML_END_COLOR, XML_TOK_GRADIENT_ENDCOLOR },
126 { XML_NAMESPACE_DRAW, XML_START_INTENSITY, XML_TOK_GRADIENT_STARTINT },
127 { XML_NAMESPACE_DRAW, XML_END_INTENSITY, XML_TOK_GRADIENT_ENDINT },
128 { XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, XML_TOK_GRADIENT_ANGLE },
129 { XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, XML_TOK_GRADIENT_BORDER },
130 XML_TOKEN_MAP_END
133 SvXMLTokenMap aTokenMap( aGradientAttrTokenMap );
134 SvXMLNamespaceMap& rNamespaceMap = rImport.GetNamespaceMap();
136 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
137 for( sal_Int16 i=0; i < nAttrCount; i++ )
139 const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
140 OUString aStrAttrName;
141 sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
142 const OUString& rStrValue = xAttrList->getValueByIndex( i );
144 sal_Int32 nTmpValue;
146 switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
148 case XML_TOK_GRADIENT_NAME:
150 rStrName = rStrValue;
151 bHasName = sal_True;
153 break;
154 case XML_TOK_GRADIENT_DISPLAY_NAME:
156 aDisplayName = rStrValue;
158 break;
159 case XML_TOK_GRADIENT_STYLE:
161 sal_uInt16 eValue;
162 if( SvXMLUnitConverter::convertEnum( eValue, rStrValue, pXML_GradientStyle_Enum ) )
164 aGradient.Style = (awt::GradientStyle) eValue;
165 bHasStyle = sal_True;
168 break;
169 case XML_TOK_GRADIENT_CX:
170 SvXMLUnitConverter::convertPercent( nTmpValue, rStrValue );
171 aGradient.XOffset = static_cast< sal_Int16 >( nTmpValue );
172 break;
173 case XML_TOK_GRADIENT_CY:
174 SvXMLUnitConverter::convertPercent( nTmpValue, rStrValue );
175 aGradient.YOffset = static_cast< sal_Int16 >( nTmpValue );
176 break;
177 case XML_TOK_GRADIENT_STARTCOLOR:
179 Color aColor;
180 bHasStartColor = SvXMLUnitConverter::convertColor( aColor, rStrValue );
181 if( bHasStartColor )
182 aGradient.StartColor = (sal_Int32)( aColor.GetColor() );
184 break;
185 case XML_TOK_GRADIENT_ENDCOLOR:
187 Color aColor;
188 bHasStartColor = SvXMLUnitConverter::convertColor( aColor, rStrValue );
189 if( bHasStartColor )
190 aGradient.EndColor = (sal_Int32)( aColor.GetColor() );
192 break;
193 case XML_TOK_GRADIENT_STARTINT:
194 SvXMLUnitConverter::convertPercent( nTmpValue, rStrValue );
195 aGradient.StartIntensity = static_cast< sal_Int16 >( nTmpValue );
196 break;
197 case XML_TOK_GRADIENT_ENDINT:
198 SvXMLUnitConverter::convertPercent( nTmpValue, rStrValue );
199 aGradient.EndIntensity = static_cast< sal_Int16 >( nTmpValue );
200 break;
201 case XML_TOK_GRADIENT_ANGLE:
203 sal_Int32 nValue;
204 SvXMLUnitConverter::convertNumber( nValue, rStrValue, 0, 3600 );
205 aGradient.Angle = sal_Int16( nValue );
207 break;
208 case XML_TOK_GRADIENT_BORDER:
209 SvXMLUnitConverter::convertPercent( nTmpValue, rStrValue );
210 aGradient.Border = static_cast< sal_Int16 >( nTmpValue );
211 break;
213 default:
214 DBG_WARNING( "Unknown token at import gradient style" )
219 rValue <<= aGradient;
221 if( aDisplayName.getLength() )
223 rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_GRADIENT_ID, rStrName,
224 aDisplayName );
225 rStrName = aDisplayName;
228 bRet = bHasName && bHasStyle && bHasStartColor && bHasEndColor;
232 return bRet;
236 //-------------------------------------------------------------
237 // Export
238 //-------------------------------------------------------------
240 #ifndef SVX_LIGHT
242 XMLGradientStyleExport::XMLGradientStyleExport(
243 SvXMLExport& rExp )
244 : rExport(rExp)
248 XMLGradientStyleExport::~XMLGradientStyleExport()
252 sal_Bool XMLGradientStyleExport::exportXML(
253 const OUString& rStrName,
254 const uno::Any& rValue )
256 sal_Bool bRet = sal_False;
257 awt::Gradient aGradient;
259 if( rStrName.getLength() )
261 if( rValue >>= aGradient )
263 OUString aStrValue;
264 OUStringBuffer aOut;
266 // Style
267 if( !SvXMLUnitConverter::convertEnum( aOut, aGradient.Style, pXML_GradientStyle_Enum ) )
269 bRet = sal_False;
271 else
273 // Name
274 sal_Bool bEncoded = sal_False;
275 OUString aStrName( rStrName );
276 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
277 rExport.EncodeStyleName( aStrName,
278 &bEncoded ) );
279 if( bEncoded )
280 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
281 aStrName );
284 aStrValue = aOut.makeStringAndClear();
285 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
287 // Center x/y
288 if( aGradient.Style != awt::GradientStyle_LINEAR &&
289 aGradient.Style != awt::GradientStyle_AXIAL )
291 SvXMLUnitConverter::convertPercent( aOut, aGradient.XOffset );
292 aStrValue = aOut.makeStringAndClear();
293 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_CX, aStrValue );
295 SvXMLUnitConverter::convertPercent( aOut, aGradient.YOffset );
296 aStrValue = aOut.makeStringAndClear();
297 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_CY, aStrValue );
300 Color aColor;
302 // Color start
303 aColor.SetColor( aGradient.StartColor );
304 SvXMLUnitConverter::convertColor( aOut, aColor );
305 aStrValue = aOut.makeStringAndClear();
306 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_START_COLOR, aStrValue );
308 // Color end
309 aColor.SetColor( aGradient.EndColor );
310 SvXMLUnitConverter::convertColor( aOut, aColor );
311 aStrValue = aOut.makeStringAndClear();
312 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_END_COLOR, aStrValue );
314 // Intensity start
315 SvXMLUnitConverter::convertPercent( aOut, aGradient.StartIntensity );
316 aStrValue = aOut.makeStringAndClear();
317 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_START_INTENSITY, aStrValue );
319 // Intensity end
320 SvXMLUnitConverter::convertPercent( aOut, aGradient.EndIntensity );
321 aStrValue = aOut.makeStringAndClear();
322 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_END_INTENSITY, aStrValue );
324 // Angle
325 if( aGradient.Style != awt::GradientStyle_RADIAL )
327 SvXMLUnitConverter::convertNumber( aOut, sal_Int32( aGradient.Angle ) );
328 aStrValue = aOut.makeStringAndClear();
329 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, aStrValue );
332 // Border
333 SvXMLUnitConverter::convertPercent( aOut, aGradient.Border );
334 aStrValue = aOut.makeStringAndClear();
335 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, aStrValue );
337 // Do Write
338 SvXMLElementExport aElem( rExport, XML_NAMESPACE_DRAW, XML_GRADIENT,
339 sal_True, sal_False );
344 return bRet;
347 #endif // #ifndef SVX_LIGHT