bump product version to 4.1.6.2
[LibreOffice.git] / xmloff / source / forms / controlpropertyhdl.cxx
blobd89f2eb23bc60d487782e8b61fa7cac29f656e20
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <xmloff/controlpropertyhdl.hxx>
23 #include <com/sun/star/util/MeasureUnit.hpp>
24 #include <com/sun/star/awt/TextAlign.hpp>
25 #include <com/sun/star/awt/FontWidth.hpp>
26 #include <com/sun/star/awt/FontEmphasisMark.hpp>
28 #include <sax/tools/converter.hxx>
30 #include <xmloff/xmltypes.hxx>
31 #include "xmloff/NamedBoolPropertyHdl.hxx"
32 #include "formenums.hxx"
33 #include <xmloff/xmluconv.hxx>
34 #include <xmloff/xmltoken.hxx>
35 #include <rtl/ustrbuf.hxx>
36 #include <osl/diagnose.h>
37 #include "callbacks.hxx"
38 #include <xmloff/XMLConstantsPropertyHandler.hxx>
40 //.........................................................................
41 namespace xmloff
43 //.........................................................................
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::awt;
48 using namespace ::com::sun::star::beans;
49 using namespace ::xmloff::token;
51 //=====================================================================
52 //= OControlPropertyHandlerFactory
53 //=====================================================================
54 //---------------------------------------------------------------------
55 OControlPropertyHandlerFactory::OControlPropertyHandlerFactory()
56 :m_pTextAlignHandler(NULL)
57 ,m_pControlBorderStyleHandler(NULL)
58 ,m_pControlBorderColorHandler(NULL)
59 ,m_pRotationAngleHandler(NULL)
60 ,m_pFontWidthHandler(NULL)
61 ,m_pFontEmphasisHandler(NULL)
62 ,m_pFontReliefHandler(NULL)
66 //---------------------------------------------------------------------
67 OControlPropertyHandlerFactory::~OControlPropertyHandlerFactory()
69 delete m_pTextAlignHandler;
70 delete m_pControlBorderStyleHandler;
71 delete m_pControlBorderColorHandler;
72 delete m_pRotationAngleHandler;
73 delete m_pFontWidthHandler;
74 delete m_pFontEmphasisHandler;
75 delete m_pFontReliefHandler;
78 //---------------------------------------------------------------------
79 const XMLPropertyHandler* OControlPropertyHandlerFactory::GetPropertyHandler(sal_Int32 _nType) const
81 const XMLPropertyHandler* pHandler = NULL;
83 switch (_nType)
85 case XML_TYPE_TEXT_ALIGN:
86 if (!m_pTextAlignHandler)
87 m_pTextAlignHandler = new XMLConstantsPropertyHandler(OEnumMapper::getEnumMap(OEnumMapper::epTextAlign), XML_TOKEN_INVALID );
88 pHandler = m_pTextAlignHandler;
89 break;
91 case XML_TYPE_CONTROL_BORDER:
92 if (!m_pControlBorderStyleHandler)
93 m_pControlBorderStyleHandler = new OControlBorderHandler( OControlBorderHandler::STYLE );
94 pHandler = m_pControlBorderStyleHandler;
95 break;
97 case XML_TYPE_CONTROL_BORDER_COLOR:
98 if ( !m_pControlBorderColorHandler )
99 m_pControlBorderColorHandler = new OControlBorderHandler( OControlBorderHandler::COLOR );
100 pHandler = m_pControlBorderColorHandler;
101 break;
103 case XML_TYPE_ROTATION_ANGLE:
104 if (!m_pRotationAngleHandler)
105 m_pRotationAngleHandler = new ORotationAngleHandler;
106 pHandler = m_pRotationAngleHandler;
107 break;
109 case XML_TYPE_FONT_WIDTH:
110 if (!m_pFontWidthHandler)
111 m_pFontWidthHandler = new OFontWidthHandler;
112 pHandler = m_pFontWidthHandler;
113 break;
115 case XML_TYPE_CONTROL_TEXT_EMPHASIZE:
116 if (!m_pFontEmphasisHandler)
117 m_pFontEmphasisHandler = new XMLConstantsPropertyHandler( OEnumMapper::getEnumMap(OEnumMapper::epFontEmphasis), XML_NONE );
118 pHandler = m_pFontEmphasisHandler;
119 break;
121 case XML_TYPE_TEXT_FONT_RELIEF:
122 if (!m_pFontReliefHandler)
123 m_pFontReliefHandler = new XMLConstantsPropertyHandler( OEnumMapper::getEnumMap(OEnumMapper::epFontRelief), XML_NONE );
124 pHandler = m_pFontReliefHandler;
125 break;
126 case XML_TYPE_TEXT_LINE_MODE:
127 pHandler = new XMLNamedBoolPropertyHdl(
128 ::xmloff::token::XML_SKIP_WHITE_SPACE,
129 ::xmloff::token::XML_CONTINUOUS);
130 break;
133 if (!pHandler)
134 pHandler = XMLPropertyHandlerFactory::GetPropertyHandler(_nType);
135 return pHandler;
138 //=====================================================================
139 //= OControlTextEmphasisHandler
140 //=====================================================================
141 OControlTextEmphasisHandler::OControlTextEmphasisHandler()
145 //---------------------------------------------------------------------
146 sal_Bool OControlTextEmphasisHandler::exportXML( OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
148 OUStringBuffer aReturn;
149 sal_Bool bSuccess = sal_False;
150 sal_Int16 nFontEmphasis = sal_Int16();
151 if (_rValue >>= nFontEmphasis)
153 // the type
154 sal_Int16 nType = nFontEmphasis & ~(awt::FontEmphasisMark::ABOVE | awt::FontEmphasisMark::BELOW);
155 // the position of the mark
156 sal_Bool bBelow = 0 != (nFontEmphasis & awt::FontEmphasisMark::BELOW);
158 // convert
159 bSuccess = SvXMLUnitConverter::convertEnum(aReturn, nType, OEnumMapper::getEnumMap(OEnumMapper::epFontEmphasis), XML_NONE);
160 if (bSuccess)
162 aReturn.append( (sal_Unicode)' ' );
163 aReturn.append( GetXMLToken(bBelow ? XML_BELOW : XML_ABOVE) );
165 _rStrExpValue = aReturn.makeStringAndClear();
169 return bSuccess;
172 //---------------------------------------------------------------------
173 sal_Bool OControlTextEmphasisHandler::importXML( const OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
175 sal_Bool bSuccess = sal_True;
176 sal_uInt16 nEmphasis = awt::FontEmphasisMark::NONE;
178 sal_Bool bBelow = sal_False;
179 sal_Bool bHasPos = sal_False, bHasType = sal_False;
181 OUString sToken;
182 SvXMLTokenEnumerator aTokenEnum(_rStrImpValue);
183 while (aTokenEnum.getNextToken(sToken))
185 if (!bHasPos)
187 if (IsXMLToken(sToken, XML_ABOVE))
189 bBelow = sal_False;
190 bHasPos = sal_True;
192 else if (IsXMLToken(sToken, XML_BELOW))
194 bBelow = sal_True;
195 bHasPos = sal_True;
198 if (!bHasType)
200 if (SvXMLUnitConverter::convertEnum(nEmphasis, sToken, OEnumMapper::getEnumMap(OEnumMapper::epFontEmphasis)))
202 bHasType = sal_True;
204 else
206 bSuccess = sal_False;
207 break;
212 if (bSuccess)
214 nEmphasis |= bBelow ? awt::FontEmphasisMark::BELOW : awt::FontEmphasisMark::ABOVE;
215 _rValue <<= (sal_Int16)nEmphasis;
218 return bSuccess;
221 //=====================================================================
222 //= OControlBorderHandlerBase
223 //=====================================================================
224 //---------------------------------------------------------------------
225 OControlBorderHandler::OControlBorderHandler( const OControlBorderHandler::BorderFacet _eFacet )
226 :m_eFacet( _eFacet )
230 //---------------------------------------------------------------------
231 sal_Bool OControlBorderHandler::importXML( const OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
233 OUString sToken;
234 SvXMLTokenEnumerator aTokens(_rStrImpValue);
236 sal_uInt16 nStyle = 1;
238 while ( aTokens.getNextToken(sToken) // have a new token
239 && (!sToken.isEmpty()) // really have a new token
242 // try interpreting the token as border style
243 if ( m_eFacet == STYLE )
245 // is it a valid enum value?
246 if ( SvXMLUnitConverter::convertEnum( nStyle, sToken, OEnumMapper::getEnumMap( OEnumMapper::epBorderWidth ) ) )
248 _rValue <<= nStyle;
249 return sal_True;
253 // try interpreting it as color value
254 if ( m_eFacet == COLOR )
256 sal_Int32 nColor(0);
257 if (::sax::Converter::convertColor( nColor, sToken ))
259 _rValue <<= nColor;
260 return sal_True;
265 return sal_False;
268 //---------------------------------------------------------------------
269 sal_Bool OControlBorderHandler::exportXML( OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
271 sal_Bool bSuccess = sal_False;
273 OUStringBuffer aOut;
274 switch ( m_eFacet )
276 case STYLE:
278 sal_Int16 nBorder = 0;
279 bSuccess = (_rValue >>= nBorder)
280 && SvXMLUnitConverter::convertEnum( aOut, nBorder, OEnumMapper::getEnumMap( OEnumMapper::epBorderWidth ) );
282 break;
283 case COLOR:
285 sal_Int32 nBorderColor = 0;
286 if ( _rValue >>= nBorderColor )
288 ::sax::Converter::convertColor(aOut, nBorderColor);
289 bSuccess = sal_True;
292 break;
293 } // switch ( m_eFacet )
295 if ( !bSuccess )
296 return sal_False;
298 if ( !_rStrExpValue.isEmpty() )
299 _rStrExpValue += " ";
300 _rStrExpValue += aOut.makeStringAndClear();
302 return sal_True;
305 //=====================================================================
306 //= OFontWidthHandler
307 //=====================================================================
308 //---------------------------------------------------------------------
309 OFontWidthHandler::OFontWidthHandler()
313 //---------------------------------------------------------------------
314 sal_Bool OFontWidthHandler::importXML( const OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
316 sal_Int32 nWidth = 0;
317 bool const bSuccess = ::sax::Converter::convertMeasure(
318 nWidth, _rStrImpValue, util::MeasureUnit::POINT);
319 if (bSuccess)
320 _rValue <<= (sal_Int16)nWidth;
322 return bSuccess;
325 //---------------------------------------------------------------------
326 sal_Bool OFontWidthHandler::exportXML( OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
328 sal_Int16 nWidth = 0;
329 OUStringBuffer aResult;
330 if (_rValue >>= nWidth)
332 ::sax::Converter::convertMeasure(aResult, nWidth,
333 util::MeasureUnit::POINT, util::MeasureUnit::POINT);
335 _rStrExpValue = aResult.makeStringAndClear();
337 return !_rStrExpValue.isEmpty();
340 //=====================================================================
341 //= ORotationAngleHandler
342 //=====================================================================
343 //---------------------------------------------------------------------
344 ORotationAngleHandler::ORotationAngleHandler()
348 //---------------------------------------------------------------------
349 sal_Bool ORotationAngleHandler::importXML( const OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
351 double fValue;
352 bool const bSucces =
353 ::sax::Converter::convertDouble(fValue, _rStrImpValue);
354 if (bSucces)
356 fValue *= 10;
357 _rValue <<= (float)fValue;
360 return bSucces;
363 //---------------------------------------------------------------------
364 sal_Bool ORotationAngleHandler::exportXML( OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
366 float fAngle = 0;
367 sal_Bool bSuccess = (_rValue >>= fAngle);
369 if (bSuccess)
371 OUStringBuffer sValue;
372 ::sax::Converter::convertDouble(sValue, ((double)fAngle) / 10);
373 _rStrExpValue = sValue.makeStringAndClear();
376 return bSuccess;
379 //=====================================================================
380 //= ImageScaleModeHandler
381 //=====================================================================
382 //---------------------------------------------------------------------
383 ImageScaleModeHandler::ImageScaleModeHandler()
384 :XMLConstantsPropertyHandler( OEnumMapper::getEnumMap( OEnumMapper::epImageScaleMode ), XML_STRETCH )
388 //.........................................................................
389 } // namespace xmloff
390 //.........................................................................
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */