Update ooo320-m1
[ooovba.git] / xmloff / source / forms / controlpropertyhdl.cxx
blob390e50a105566b76dbb6bc7fd1c7bc87d52a108a
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: controlpropertyhdl.cxx,v $
10 * $Revision: 1.22 $
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 <xmloff/controlpropertyhdl.hxx>
34 #include <com/sun/star/awt/TextAlign.hpp>
35 #include <com/sun/star/awt/FontWidth.hpp>
36 #include <com/sun/star/awt/FontEmphasisMark.hpp>
37 #include <xmloff/xmltypes.hxx>
38 #include "xmloff/NamedBoolPropertyHdl.hxx"
39 #include "formenums.hxx"
40 #include <xmloff/xmluconv.hxx>
41 #include <xmloff/xmltoken.hxx>
42 #include <rtl/ustrbuf.hxx>
43 #include <osl/diagnose.h>
44 #include "callbacks.hxx"
45 #include <xmloff/XMLConstantsPropertyHandler.hxx>
47 //.........................................................................
48 namespace xmloff
50 //.........................................................................
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::awt;
54 using namespace ::com::sun::star::beans;
55 using namespace ::xmloff::token;
57 //=====================================================================
58 //= OControlPropertyHandlerFactory
59 //=====================================================================
60 //---------------------------------------------------------------------
61 OControlPropertyHandlerFactory::OControlPropertyHandlerFactory()
62 :m_pTextAlignHandler(NULL)
63 ,m_pControlBorderStyleHandler(NULL)
64 ,m_pControlBorderColorHandler(NULL)
65 ,m_pRotationAngleHandler(NULL)
66 ,m_pFontWidthHandler(NULL)
67 ,m_pFontEmphasisHandler(NULL)
68 ,m_pFontReliefHandler(NULL)
72 //---------------------------------------------------------------------
73 OControlPropertyHandlerFactory::~OControlPropertyHandlerFactory()
75 delete m_pTextAlignHandler;
76 delete m_pControlBorderStyleHandler;
77 delete m_pControlBorderColorHandler;
78 delete m_pRotationAngleHandler;
79 delete m_pFontWidthHandler;
80 delete m_pFontEmphasisHandler;
81 delete m_pFontReliefHandler;
84 //---------------------------------------------------------------------
85 const XMLPropertyHandler* OControlPropertyHandlerFactory::GetPropertyHandler(sal_Int32 _nType) const
87 const XMLPropertyHandler* pHandler = NULL;
89 switch (_nType)
91 case XML_TYPE_TEXT_ALIGN:
92 if (!m_pTextAlignHandler)
93 m_pTextAlignHandler = new XMLConstantsPropertyHandler(OEnumMapper::getEnumMap(OEnumMapper::epTextAlign), XML_TOKEN_INVALID );
94 pHandler = m_pTextAlignHandler;
95 break;
97 case XML_TYPE_CONTROL_BORDER:
98 if (!m_pControlBorderStyleHandler)
99 m_pControlBorderStyleHandler = new OControlBorderHandler( OControlBorderHandler::STYLE );
100 pHandler = m_pControlBorderStyleHandler;
101 break;
103 case XML_TYPE_CONTROL_BORDER_COLOR:
104 if ( !m_pControlBorderColorHandler )
105 m_pControlBorderColorHandler = new OControlBorderHandler( OControlBorderHandler::COLOR );
106 pHandler = m_pControlBorderColorHandler;
107 break;
109 case XML_TYPE_ROTATION_ANGLE:
110 if (!m_pRotationAngleHandler)
111 m_pRotationAngleHandler = new ORotationAngleHandler;
112 pHandler = m_pRotationAngleHandler;
113 break;
115 case XML_TYPE_FONT_WIDTH:
116 if (!m_pFontWidthHandler)
117 m_pFontWidthHandler = new OFontWidthHandler;
118 pHandler = m_pFontWidthHandler;
119 break;
121 case XML_TYPE_CONTROL_TEXT_EMPHASIZE:
122 if (!m_pFontEmphasisHandler)
123 m_pFontEmphasisHandler = new XMLConstantsPropertyHandler( OEnumMapper::getEnumMap(OEnumMapper::epFontEmphasis), XML_NONE );
124 pHandler = m_pFontEmphasisHandler;
125 break;
127 case XML_TYPE_TEXT_FONT_RELIEF:
128 if (!m_pFontReliefHandler)
129 m_pFontReliefHandler = new XMLConstantsPropertyHandler( OEnumMapper::getEnumMap(OEnumMapper::epFontRelief), XML_NONE );
130 pHandler = m_pFontReliefHandler;
131 break;
132 case XML_TYPE_TEXT_LINE_MODE:
133 pHandler = new XMLNamedBoolPropertyHdl(
134 ::xmloff::token::XML_SKIP_WHITE_SPACE,
135 ::xmloff::token::XML_CONTINUOUS);
136 break;
139 if (!pHandler)
140 pHandler = XMLPropertyHandlerFactory::GetPropertyHandler(_nType);
141 return pHandler;
144 //=====================================================================
145 //= OControlTextEmphasisHandler
146 //=====================================================================
147 OControlTextEmphasisHandler::OControlTextEmphasisHandler()
151 //---------------------------------------------------------------------
152 sal_Bool OControlTextEmphasisHandler::exportXML( ::rtl::OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
154 ::rtl::OUStringBuffer aReturn;
155 sal_Bool bSuccess = sal_False;
156 sal_Int16 nFontEmphasis = sal_Int16();
157 if (_rValue >>= nFontEmphasis)
159 // the type
160 sal_Int16 nType = nFontEmphasis & ~(FontEmphasisMark::ABOVE | FontEmphasisMark::BELOW);
161 // the position of the mark
162 sal_Bool bBelow = 0 != (nFontEmphasis & FontEmphasisMark::BELOW);
164 // convert
165 bSuccess = SvXMLUnitConverter::convertEnum(aReturn, nType, OEnumMapper::getEnumMap(OEnumMapper::epFontEmphasis), XML_NONE);
166 if (bSuccess)
168 aReturn.append( (sal_Unicode)' ' );
169 aReturn.append( GetXMLToken(bBelow ? XML_BELOW : XML_ABOVE) );
171 _rStrExpValue = aReturn.makeStringAndClear();
175 return bSuccess;
178 //---------------------------------------------------------------------
179 sal_Bool OControlTextEmphasisHandler::importXML( const ::rtl::OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
181 sal_Bool bSuccess = sal_True;
182 sal_uInt16 nEmphasis = FontEmphasisMark::NONE;
184 sal_Bool bBelow = sal_False;
185 sal_Bool bHasPos = sal_False, bHasType = sal_False;
187 ::rtl::OUString sToken;
188 SvXMLTokenEnumerator aTokenEnum(_rStrImpValue);
189 while (aTokenEnum.getNextToken(sToken))
191 if (!bHasPos)
193 if (IsXMLToken(sToken, XML_ABOVE))
195 bBelow = sal_False;
196 bHasPos = sal_True;
198 else if (IsXMLToken(sToken, XML_BELOW))
200 bBelow = sal_True;
201 bHasPos = sal_True;
204 if (!bHasType)
206 if (SvXMLUnitConverter::convertEnum(nEmphasis, sToken, OEnumMapper::getEnumMap(OEnumMapper::epFontEmphasis)))
208 bHasType = sal_True;
210 else
212 bSuccess = sal_False;
213 break;
218 if (bSuccess)
220 nEmphasis |= bBelow ? FontEmphasisMark::BELOW : FontEmphasisMark::ABOVE;
221 _rValue <<= (sal_Int16)nEmphasis;
224 return bSuccess;
227 //=====================================================================
228 //= OControlBorderHandlerBase
229 //=====================================================================
230 //---------------------------------------------------------------------
231 OControlBorderHandler::OControlBorderHandler( const OControlBorderHandler::BorderFacet _eFacet )
232 :m_eFacet( _eFacet )
236 //---------------------------------------------------------------------
237 sal_Bool OControlBorderHandler::importXML( const ::rtl::OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
239 ::rtl::OUString sToken;
240 SvXMLTokenEnumerator aTokens(_rStrImpValue);
242 sal_uInt16 nStyle = 1;
243 Color aColor;
245 while ( aTokens.getNextToken(sToken) // have a new token
246 && (0 != sToken.getLength()) // really have a new token
249 // try interpreting the token as border style
250 if ( m_eFacet == STYLE )
252 // is it a valid enum value?
253 if ( SvXMLUnitConverter::convertEnum( nStyle, sToken, OEnumMapper::getEnumMap( OEnumMapper::epBorderWidth ) ) )
255 _rValue <<= nStyle;
256 return sal_True;
260 // try interpreting it as color value
261 if ( m_eFacet == COLOR )
263 if ( SvXMLUnitConverter::convertColor( aColor, sToken ) )
265 _rValue <<= (sal_Int32)aColor.GetColor();
266 return sal_True;
271 return sal_False;
274 //---------------------------------------------------------------------
275 sal_Bool OControlBorderHandler::exportXML( ::rtl::OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
277 sal_Bool bSuccess = sal_False;
279 ::rtl::OUStringBuffer aOut;
280 switch ( m_eFacet )
282 case STYLE:
284 sal_Int16 nBorder = 0;
285 bSuccess = (_rValue >>= nBorder)
286 && SvXMLUnitConverter::convertEnum( aOut, nBorder, OEnumMapper::getEnumMap( OEnumMapper::epBorderWidth ) );
288 break;
289 case COLOR:
291 sal_Int32 nBorderColor = 0;
292 if ( _rValue >>= nBorderColor )
294 SvXMLUnitConverter::convertColor( aOut, Color( nBorderColor ) );
295 bSuccess = sal_True;
298 break;
299 } // switch ( m_eFacet )
301 if ( !bSuccess )
302 return sal_False;
304 if ( _rStrExpValue.getLength() )
305 _rStrExpValue += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " " ) );
306 _rStrExpValue += aOut.makeStringAndClear();
308 return sal_True;
311 //=====================================================================
312 //= OFontWidthHandler
313 //=====================================================================
314 //---------------------------------------------------------------------
315 OFontWidthHandler::OFontWidthHandler()
319 //---------------------------------------------------------------------
320 sal_Bool OFontWidthHandler::importXML( const ::rtl::OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
322 sal_Int32 nWidth = 0;
323 sal_Bool bSuccess = SvXMLUnitConverter::convertMeasure(nWidth, _rStrImpValue, MAP_POINT);
324 if (bSuccess)
325 _rValue <<= (sal_Int16)nWidth;
327 return bSuccess;
330 //---------------------------------------------------------------------
331 sal_Bool OFontWidthHandler::exportXML( ::rtl::OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
333 sal_Int16 nWidth = 0;
334 ::rtl::OUStringBuffer aResult;
335 if (_rValue >>= nWidth)
336 SvXMLUnitConverter::convertMeasure(aResult, nWidth, MAP_POINT, MAP_POINT);
337 _rStrExpValue = aResult.makeStringAndClear();
339 return _rStrExpValue.getLength() != 0;
342 //=====================================================================
343 //= ORotationAngleHandler
344 //=====================================================================
345 //---------------------------------------------------------------------
346 ORotationAngleHandler::ORotationAngleHandler()
350 //---------------------------------------------------------------------
351 sal_Bool ORotationAngleHandler::importXML( const ::rtl::OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
353 double fValue;
354 sal_Bool bSucces =
355 SvXMLUnitConverter::convertDouble(fValue, _rStrImpValue);
356 if (bSucces)
358 fValue *= 10;
359 _rValue <<= (float)fValue;
362 return bSucces;
365 //---------------------------------------------------------------------
366 sal_Bool ORotationAngleHandler::exportXML( ::rtl::OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
368 float fAngle = 0;
369 sal_Bool bSuccess = (_rValue >>= fAngle);
371 if (bSuccess)
373 rtl::OUStringBuffer sValue;
374 SvXMLUnitConverter::convertDouble(sValue, ((double)fAngle) / 10);
375 _rStrExpValue = sValue.makeStringAndClear();
378 return bSuccess;
381 //=====================================================================
382 //= ImageScaleModeHandler
383 //=====================================================================
384 //---------------------------------------------------------------------
385 ImageScaleModeHandler::ImageScaleModeHandler()
386 :XMLConstantsPropertyHandler( OEnumMapper::getEnumMap( OEnumMapper::epImageScaleMode ), XML_STRETCH )
390 //.........................................................................
391 } // namespace xmloff
392 //.........................................................................