update dev300-m58
[ooovba.git] / xmloff / source / style / HatchStyle.cxx
blobf0893b80779d35e20affe56efab8e5f520424d69
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: HatchStyle.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 "HatchStyle.hxx"
34 #include <com/sun/star/drawing/Hatch.hpp>
35 #include <xmloff/nmspmap.hxx>
36 #include <xmloff/xmluconv.hxx>
37 #include "xmlnmspe.hxx"
38 #include <xmloff/xmltoken.hxx>
39 #include <xmloff/xmlexp.hxx>
40 #include <xmloff/xmlimp.hxx>
41 #include <rtl/ustrbuf.hxx>
42 #include <rtl/ustring.hxx>
43 #include <tools/debug.hxx>
44 #include <xmloff/xmltkmap.hxx>
46 using namespace ::com::sun::star;
47 using ::rtl::OUString;
48 using ::rtl::OUStringBuffer;
50 using namespace ::xmloff::token;
52 enum SvXMLTokenMapAttrs
54 XML_TOK_HATCH_NAME,
55 XML_TOK_HATCH_DISPLAY_NAME,
56 XML_TOK_HATCH_STYLE,
57 XML_TOK_HATCH_COLOR,
58 XML_TOK_HATCH_DISTANCE,
59 XML_TOK_HATCH_ROTATION,
60 XML_TOK_TABSTOP_END=XML_TOK_UNKNOWN
64 SvXMLEnumMapEntry __READONLY_DATA pXML_HatchStyle_Enum[] =
66 { XML_HATCHSTYLE_SINGLE, drawing::HatchStyle_SINGLE },
67 { XML_HATCHSTYLE_DOUBLE, drawing::HatchStyle_DOUBLE },
68 { XML_HATCHSTYLE_TRIPLE, drawing::HatchStyle_TRIPLE },
69 { XML_TOKEN_INVALID, 0 }
73 //-------------------------------------------------------------
74 // Import
75 //-------------------------------------------------------------
77 XMLHatchStyleImport::XMLHatchStyleImport( SvXMLImport& rImp )
78 : rImport(rImp)
82 XMLHatchStyleImport::~XMLHatchStyleImport()
86 sal_Bool XMLHatchStyleImport::importXML(
87 const uno::Reference< xml::sax::XAttributeList >& xAttrList,
88 uno::Any& rValue,
89 OUString& rStrName )
91 sal_Bool bRet = sal_False;
93 sal_Bool bHasName = sal_False;
94 sal_Bool bHasStyle = sal_False;
95 sal_Bool bHasColor = sal_False;
96 sal_Bool bHasDist = sal_False;
97 OUString aDisplayName;
99 drawing::Hatch aHatch;
100 aHatch.Style = drawing::HatchStyle_SINGLE;
101 aHatch.Color = 0;
102 aHatch.Distance = 0;
103 aHatch.Angle = 0;
106 static __FAR_DATA SvXMLTokenMapEntry aHatchAttrTokenMap[] =
108 { XML_NAMESPACE_DRAW, XML_NAME, XML_TOK_HATCH_NAME },
109 { XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, XML_TOK_HATCH_DISPLAY_NAME },
110 { XML_NAMESPACE_DRAW, XML_STYLE, XML_TOK_HATCH_STYLE },
111 { XML_NAMESPACE_DRAW, XML_COLOR, XML_TOK_HATCH_COLOR },
112 { XML_NAMESPACE_DRAW, XML_HATCH_DISTANCE, XML_TOK_HATCH_DISTANCE },
113 { XML_NAMESPACE_DRAW, XML_ROTATION, XML_TOK_HATCH_ROTATION },
114 XML_TOKEN_MAP_END
117 SvXMLTokenMap aTokenMap( aHatchAttrTokenMap );
118 SvXMLNamespaceMap rNamespaceMap = rImport.GetNamespaceMap();
119 SvXMLUnitConverter& rUnitConverter = rImport.GetMM100UnitConverter();
121 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
122 for( sal_Int16 i=0; i < nAttrCount; i++ )
124 const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
125 OUString aStrAttrName;
126 sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
127 const OUString& rStrValue = xAttrList->getValueByIndex( i );
129 switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
131 case XML_TOK_HATCH_NAME:
133 rStrName = rStrValue;
134 bHasName = sal_True;
136 break;
137 case XML_TOK_HATCH_DISPLAY_NAME:
138 aDisplayName = rStrValue;
139 break;
140 case XML_TOK_HATCH_STYLE:
142 sal_uInt16 eValue;
143 bHasStyle = rUnitConverter.convertEnum( eValue, rStrValue, pXML_HatchStyle_Enum );
144 if( bHasStyle )
145 aHatch.Style = (drawing::HatchStyle) eValue;
147 break;
148 case XML_TOK_HATCH_COLOR:
150 Color aColor;
151 bHasColor = rUnitConverter.convertColor( aColor, rStrValue );
152 if( bHasColor )
153 aHatch.Color = (sal_Int32)( aColor.GetColor() );
155 break;
156 case XML_TOK_HATCH_DISTANCE:
157 bHasDist = rUnitConverter.convertMeasure( (sal_Int32&)aHatch.Distance, rStrValue );
158 break;
159 case XML_TOK_HATCH_ROTATION:
161 sal_Int32 nValue;
162 rUnitConverter.convertNumber( nValue, rStrValue, 0, 360 );
163 aHatch.Angle = sal_Int16( nValue );
165 break;
167 default:
168 DBG_WARNING( "Unknown token at import hatch style" )
173 rValue <<= aHatch;
175 if( aDisplayName.getLength() )
177 rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_HATCH_ID, rStrName,
178 aDisplayName );
179 rStrName = aDisplayName;
182 bRet = bHasName && bHasStyle && bHasColor && bHasDist;
186 return bRet;
190 //-------------------------------------------------------------
191 // Export
192 //-------------------------------------------------------------
194 #ifndef SVX_LIGHT
196 XMLHatchStyleExport::XMLHatchStyleExport( SvXMLExport& rExp )
197 : rExport(rExp)
201 XMLHatchStyleExport::~XMLHatchStyleExport()
205 sal_Bool XMLHatchStyleExport::exportXML(
206 const OUString& rStrName,
207 const uno::Any& rValue )
209 sal_Bool bRet = sal_False;
210 drawing::Hatch aHatch;
212 if( rStrName.getLength() )
214 if( rValue >>= aHatch )
216 OUString aStrValue;
217 OUStringBuffer aOut;
219 SvXMLUnitConverter& rUnitConverter =
220 rExport.GetMM100UnitConverter();
222 // Style
223 if( !rUnitConverter.convertEnum( aOut, aHatch.Style, pXML_HatchStyle_Enum ) )
225 bRet = sal_False;
227 else
229 // Name
230 sal_Bool bEncoded = sal_False;
231 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
232 rExport.EncodeStyleName( rStrName,
233 &bEncoded ) );
234 if( bEncoded )
235 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
236 rStrName );
238 aStrValue = aOut.makeStringAndClear();
239 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
241 // Color
242 rUnitConverter.convertColor( aOut, Color( aHatch.Color ) );
243 aStrValue = aOut.makeStringAndClear();
244 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_COLOR, aStrValue );
246 // Distance
247 rUnitConverter.convertMeasure( aOut, aHatch.Distance );
248 aStrValue = aOut.makeStringAndClear();
249 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_HATCH_DISTANCE, aStrValue );
251 // Angle
252 rUnitConverter.convertNumber( aOut, sal_Int32( aHatch.Angle ) );
253 aStrValue = aOut.makeStringAndClear();
254 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_ROTATION, aStrValue );
256 // Do Write
257 SvXMLElementExport rElem( rExport, XML_NAMESPACE_DRAW, XML_HATCH,
258 sal_True, sal_False );
263 return bRet;
266 #endif // #ifndef SVX_LIGHT