Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / xmloff / source / forms / controlpropertyhdl.cxx
blob855df152f336eb002712c57809478f559cccc333
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 .
20 #include <xmloff/controlpropertyhdl.hxx>
22 #include <com/sun/star/util/MeasureUnit.hpp>
23 #include <com/sun/star/awt/TextAlign.hpp>
24 #include <com/sun/star/awt/FontWidth.hpp>
25 #include <com/sun/star/awt/FontEmphasisMark.hpp>
27 #include <sax/tools/converter.hxx>
29 #include <xmloff/xmltypes.hxx>
30 #include <xmloff/NamedBoolPropertyHdl.hxx>
31 #include "formenums.hxx"
32 #include <xmloff/xmluconv.hxx>
33 #include <xmloff/xmltoken.hxx>
34 #include <rtl/ustrbuf.hxx>
35 #include <osl/diagnose.h>
36 #include "callbacks.hxx"
37 #include <xmloff/XMLConstantsPropertyHandler.hxx>
39 namespace xmloff
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::awt;
45 using namespace ::com::sun::star::beans;
46 using namespace ::xmloff::token;
48 //= OControlPropertyHandlerFactory
49 OControlPropertyHandlerFactory::OControlPropertyHandlerFactory()
50 :m_pTextAlignHandler(nullptr)
51 ,m_pControlBorderStyleHandler(nullptr)
52 ,m_pControlBorderColorHandler(nullptr)
53 ,m_pRotationAngleHandler(nullptr)
54 ,m_pFontWidthHandler(nullptr)
55 ,m_pFontEmphasisHandler(nullptr)
56 ,m_pFontReliefHandler(nullptr)
60 OControlPropertyHandlerFactory::~OControlPropertyHandlerFactory()
62 delete m_pTextAlignHandler;
63 delete m_pControlBorderStyleHandler;
64 delete m_pControlBorderColorHandler;
65 delete m_pRotationAngleHandler;
66 delete m_pFontWidthHandler;
67 delete m_pFontEmphasisHandler;
68 delete m_pFontReliefHandler;
71 const XMLPropertyHandler* OControlPropertyHandlerFactory::GetPropertyHandler(sal_Int32 _nType) const
73 const XMLPropertyHandler* pHandler = nullptr;
75 switch (_nType)
77 case XML_TYPE_TEXT_ALIGN:
78 if (!m_pTextAlignHandler)
79 m_pTextAlignHandler = new XMLConstantsPropertyHandler(OEnumMapper::getEnumMap(OEnumMapper::epTextAlign), XML_TOKEN_INVALID );
80 pHandler = m_pTextAlignHandler;
81 break;
83 case XML_TYPE_CONTROL_BORDER:
84 if (!m_pControlBorderStyleHandler)
85 m_pControlBorderStyleHandler = new OControlBorderHandler( OControlBorderHandler::STYLE );
86 pHandler = m_pControlBorderStyleHandler;
87 break;
89 case XML_TYPE_CONTROL_BORDER_COLOR:
90 if ( !m_pControlBorderColorHandler )
91 m_pControlBorderColorHandler = new OControlBorderHandler( OControlBorderHandler::COLOR );
92 pHandler = m_pControlBorderColorHandler;
93 break;
95 case XML_TYPE_ROTATION_ANGLE:
96 if (!m_pRotationAngleHandler)
97 m_pRotationAngleHandler = new ORotationAngleHandler;
98 pHandler = m_pRotationAngleHandler;
99 break;
101 case XML_TYPE_FONT_WIDTH:
102 if (!m_pFontWidthHandler)
103 m_pFontWidthHandler = new OFontWidthHandler;
104 pHandler = m_pFontWidthHandler;
105 break;
107 case XML_TYPE_CONTROL_TEXT_EMPHASIZE:
108 if (!m_pFontEmphasisHandler)
109 m_pFontEmphasisHandler = new XMLConstantsPropertyHandler( OEnumMapper::getEnumMap(OEnumMapper::epFontEmphasis), XML_NONE );
110 pHandler = m_pFontEmphasisHandler;
111 break;
113 case XML_TYPE_TEXT_FONT_RELIEF:
114 if (!m_pFontReliefHandler)
115 m_pFontReliefHandler = new XMLConstantsPropertyHandler( OEnumMapper::getEnumMap(OEnumMapper::epFontRelief), XML_NONE );
116 pHandler = m_pFontReliefHandler;
117 break;
118 case XML_TYPE_TEXT_LINE_MODE:
119 pHandler = new XMLNamedBoolPropertyHdl(
120 ::xmloff::token::XML_SKIP_WHITE_SPACE,
121 ::xmloff::token::XML_CONTINUOUS);
122 break;
125 if (!pHandler)
126 pHandler = XMLPropertyHandlerFactory::GetPropertyHandler(_nType);
127 return pHandler;
130 //= OControlTextEmphasisHandler
131 OControlTextEmphasisHandler::OControlTextEmphasisHandler()
135 bool OControlTextEmphasisHandler::exportXML( OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
137 OUStringBuffer aReturn;
138 bool bSuccess = false;
139 sal_Int16 nFontEmphasis = sal_Int16();
140 if (_rValue >>= nFontEmphasis)
142 // the type
143 sal_Int16 nType = nFontEmphasis & ~(awt::FontEmphasisMark::ABOVE | awt::FontEmphasisMark::BELOW);
144 // the position of the mark
145 bool bBelow = 0 != (nFontEmphasis & awt::FontEmphasisMark::BELOW);
147 // convert
148 bSuccess = SvXMLUnitConverter::convertEnum(aReturn, nType, OEnumMapper::getEnumMap(OEnumMapper::epFontEmphasis), XML_NONE);
149 if (bSuccess)
151 aReturn.append( ' ' );
152 aReturn.append( GetXMLToken(bBelow ? XML_BELOW : XML_ABOVE) );
154 _rStrExpValue = aReturn.makeStringAndClear();
158 return bSuccess;
161 bool OControlTextEmphasisHandler::importXML( const OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
163 bool bSuccess = true;
164 sal_uInt16 nEmphasis = awt::FontEmphasisMark::NONE;
166 bool bBelow = false;
167 bool bHasPos = false, bHasType = false;
169 OUString sToken;
170 SvXMLTokenEnumerator aTokenEnum(_rStrImpValue);
171 while (aTokenEnum.getNextToken(sToken))
173 if (!bHasPos)
175 if (IsXMLToken(sToken, XML_ABOVE))
177 bBelow = false;
178 bHasPos = true;
180 else if (IsXMLToken(sToken, XML_BELOW))
182 bBelow = true;
183 bHasPos = true;
186 if (!bHasType)
188 if (SvXMLUnitConverter::convertEnum(nEmphasis, sToken, OEnumMapper::getEnumMap(OEnumMapper::epFontEmphasis)))
190 bHasType = true;
192 else
194 bSuccess = false;
195 break;
200 if (bSuccess)
202 nEmphasis |= bBelow ? awt::FontEmphasisMark::BELOW : awt::FontEmphasisMark::ABOVE;
203 _rValue <<= (sal_Int16)nEmphasis;
206 return bSuccess;
209 //= OControlBorderHandlerBase
210 OControlBorderHandler::OControlBorderHandler( const OControlBorderHandler::BorderFacet _eFacet )
211 :m_eFacet( _eFacet )
215 bool OControlBorderHandler::importXML( const OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
217 OUString sToken;
218 SvXMLTokenEnumerator aTokens(_rStrImpValue);
220 sal_uInt16 nStyle = 1;
222 while ( aTokens.getNextToken(sToken) // have a new token
223 && (!sToken.isEmpty()) // really have a new token
226 // try interpreting the token as border style
227 if ( m_eFacet == STYLE )
229 // is it a valid enum value?
230 if ( SvXMLUnitConverter::convertEnum( nStyle, sToken, OEnumMapper::getEnumMap( OEnumMapper::epBorderWidth ) ) )
232 _rValue <<= nStyle;
233 return true;
237 // try interpreting it as color value
238 if ( m_eFacet == COLOR )
240 sal_Int32 nColor(0);
241 if (::sax::Converter::convertColor( nColor, sToken ))
243 _rValue <<= nColor;
244 return true;
249 return false;
252 bool OControlBorderHandler::exportXML( OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
254 bool bSuccess = false;
256 OUStringBuffer aOut;
257 switch ( m_eFacet )
259 case STYLE:
261 sal_Int16 nBorder = 0;
262 bSuccess = (_rValue >>= nBorder)
263 && SvXMLUnitConverter::convertEnum( aOut, nBorder, OEnumMapper::getEnumMap( OEnumMapper::epBorderWidth ) );
265 break;
266 case COLOR:
268 sal_Int32 nBorderColor = 0;
269 if ( _rValue >>= nBorderColor )
271 ::sax::Converter::convertColor(aOut, nBorderColor);
272 bSuccess = true;
275 break;
276 } // switch ( m_eFacet )
278 if ( !bSuccess )
279 return false;
281 if ( !_rStrExpValue.isEmpty() )
282 _rStrExpValue += " ";
283 _rStrExpValue += aOut.makeStringAndClear();
285 return true;
288 //= OFontWidthHandler
289 OFontWidthHandler::OFontWidthHandler()
293 bool OFontWidthHandler::importXML( const OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
295 sal_Int32 nWidth = 0;
296 bool const bSuccess = ::sax::Converter::convertMeasure(
297 nWidth, _rStrImpValue, util::MeasureUnit::POINT);
298 if (bSuccess)
299 _rValue <<= (sal_Int16)nWidth;
301 return bSuccess;
304 bool OFontWidthHandler::exportXML( OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
306 sal_Int16 nWidth = 0;
307 OUStringBuffer aResult;
308 if (_rValue >>= nWidth)
310 ::sax::Converter::convertMeasure(aResult, nWidth,
311 util::MeasureUnit::POINT, util::MeasureUnit::POINT);
313 _rStrExpValue = aResult.makeStringAndClear();
315 return !_rStrExpValue.isEmpty();
318 //= ORotationAngleHandler
319 ORotationAngleHandler::ORotationAngleHandler()
323 bool ORotationAngleHandler::importXML( const OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
325 double fValue;
326 bool const bSucces =
327 ::sax::Converter::convertDouble(fValue, _rStrImpValue);
328 if (bSucces)
330 fValue *= 10;
331 _rValue <<= (float)fValue;
334 return bSucces;
337 bool ORotationAngleHandler::exportXML( OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
339 float fAngle = 0;
340 bool bSuccess = (_rValue >>= fAngle);
342 if (bSuccess)
344 OUStringBuffer sValue;
345 ::sax::Converter::convertDouble(sValue, ((double)fAngle) / 10);
346 _rStrExpValue = sValue.makeStringAndClear();
349 return bSuccess;
352 //= ImageScaleModeHandler
353 ImageScaleModeHandler::ImageScaleModeHandler()
354 :XMLConstantsPropertyHandler( OEnumMapper::getEnumMap( OEnumMapper::epImageScaleMode ), XML_STRETCH )
358 } // namespace xmloff
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */