xref: /AOO41X/main/xmloff/source/forms/controlpropertyhdl.cxx (revision 63bba73cc51e0afb45f8a8d578158724bb5afee8)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 #include <xmloff/controlpropertyhdl.hxx>
27 #include <com/sun/star/awt/TextAlign.hpp>
28 #include <com/sun/star/awt/FontWidth.hpp>
29 #include <com/sun/star/awt/FontEmphasisMark.hpp>
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>
39 
40 //.........................................................................
41 namespace xmloff
42 {
43 //.........................................................................
44 
45     using namespace ::com::sun::star::uno;
46     using namespace ::com::sun::star::awt;
47     using namespace ::com::sun::star::beans;
48     using namespace ::xmloff::token;
49 
50     //=====================================================================
51     //= OControlPropertyHandlerFactory
52     //=====================================================================
53     //---------------------------------------------------------------------
OControlPropertyHandlerFactory()54     OControlPropertyHandlerFactory::OControlPropertyHandlerFactory()
55         :m_pTextAlignHandler(NULL)
56         ,m_pControlBorderStyleHandler(NULL)
57         ,m_pControlBorderColorHandler(NULL)
58         ,m_pRotationAngleHandler(NULL)
59         ,m_pFontWidthHandler(NULL)
60         ,m_pFontEmphasisHandler(NULL)
61         ,m_pFontReliefHandler(NULL)
62     {
63     }
64 
65     //---------------------------------------------------------------------
~OControlPropertyHandlerFactory()66     OControlPropertyHandlerFactory::~OControlPropertyHandlerFactory()
67     {
68         delete m_pTextAlignHandler;
69         delete m_pControlBorderStyleHandler;
70         delete m_pControlBorderColorHandler;
71         delete m_pRotationAngleHandler;
72         delete m_pFontWidthHandler;
73         delete m_pFontEmphasisHandler;
74         delete m_pFontReliefHandler;
75     }
76 
77     //---------------------------------------------------------------------
GetPropertyHandler(sal_Int32 _nType) const78     const XMLPropertyHandler* OControlPropertyHandlerFactory::GetPropertyHandler(sal_Int32 _nType) const
79     {
80         const XMLPropertyHandler* pHandler = NULL;
81 
82         switch (_nType)
83         {
84             case XML_TYPE_TEXT_ALIGN:
85                 if (!m_pTextAlignHandler)
86                     m_pTextAlignHandler = new XMLConstantsPropertyHandler(OEnumMapper::getEnumMap(OEnumMapper::epTextAlign), XML_TOKEN_INVALID );
87                 pHandler = m_pTextAlignHandler;
88                 break;
89 
90             case XML_TYPE_CONTROL_BORDER:
91                 if (!m_pControlBorderStyleHandler)
92                     m_pControlBorderStyleHandler = new OControlBorderHandler( OControlBorderHandler::STYLE );
93                 pHandler = m_pControlBorderStyleHandler;
94                 break;
95 
96             case XML_TYPE_CONTROL_BORDER_COLOR:
97                 if ( !m_pControlBorderColorHandler )
98                     m_pControlBorderColorHandler = new OControlBorderHandler( OControlBorderHandler::COLOR );
99                 pHandler = m_pControlBorderColorHandler;
100                 break;
101 
102             case XML_TYPE_ROTATION_ANGLE:
103                 if (!m_pRotationAngleHandler)
104                     m_pRotationAngleHandler = new ORotationAngleHandler;
105                 pHandler = m_pRotationAngleHandler;
106                 break;
107 
108             case XML_TYPE_FONT_WIDTH:
109                 if (!m_pFontWidthHandler)
110                     m_pFontWidthHandler = new OFontWidthHandler;
111                 pHandler = m_pFontWidthHandler;
112                 break;
113 
114             case XML_TYPE_CONTROL_TEXT_EMPHASIZE:
115                 if (!m_pFontEmphasisHandler)
116                     m_pFontEmphasisHandler = new XMLConstantsPropertyHandler( OEnumMapper::getEnumMap(OEnumMapper::epFontEmphasis), XML_NONE );
117                 pHandler = m_pFontEmphasisHandler;
118                 break;
119 
120             case XML_TYPE_TEXT_FONT_RELIEF:
121                 if (!m_pFontReliefHandler)
122                     m_pFontReliefHandler = new XMLConstantsPropertyHandler( OEnumMapper::getEnumMap(OEnumMapper::epFontRelief), XML_NONE );
123                 pHandler = m_pFontReliefHandler;
124                 break;
125             case XML_TYPE_TEXT_LINE_MODE:
126                 pHandler = new XMLNamedBoolPropertyHdl(
127                                             ::xmloff::token::XML_SKIP_WHITE_SPACE,
128                                             ::xmloff::token::XML_CONTINUOUS);
129                 break;
130         }
131 
132         if (!pHandler)
133             pHandler = XMLPropertyHandlerFactory::GetPropertyHandler(_nType);
134         return pHandler;
135     }
136 
137     //=====================================================================
138     //= OControlTextEmphasisHandler
139     //=====================================================================
OControlTextEmphasisHandler()140     OControlTextEmphasisHandler::OControlTextEmphasisHandler()
141     {
142     }
143 
144     //---------------------------------------------------------------------
exportXML(::rtl::OUString & _rStrExpValue,const Any & _rValue,const SvXMLUnitConverter &) const145     sal_Bool OControlTextEmphasisHandler::exportXML( ::rtl::OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
146     {
147         ::rtl::OUStringBuffer aReturn;
148         sal_Bool bSuccess = sal_False;
149         sal_Int16 nFontEmphasis = sal_Int16();
150         if (_rValue >>= nFontEmphasis)
151         {
152             // the type
153             sal_Int16 nType = nFontEmphasis & ~(FontEmphasisMark::ABOVE | FontEmphasisMark::BELOW);
154             // the position of the mark
155             sal_Bool bBelow = 0 != (nFontEmphasis & FontEmphasisMark::BELOW);
156 
157             // convert
158             bSuccess = SvXMLUnitConverter::convertEnum(aReturn, nType, OEnumMapper::getEnumMap(OEnumMapper::epFontEmphasis), XML_NONE);
159             if (bSuccess)
160             {
161                 aReturn.append( (sal_Unicode)' ' );
162                 aReturn.append( GetXMLToken(bBelow ? XML_BELOW : XML_ABOVE) );
163 
164                 _rStrExpValue = aReturn.makeStringAndClear();
165             }
166         }
167 
168         return bSuccess;
169     }
170 
171     //---------------------------------------------------------------------
importXML(const::rtl::OUString & _rStrImpValue,Any & _rValue,const SvXMLUnitConverter &) const172     sal_Bool OControlTextEmphasisHandler::importXML( const ::rtl::OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
173     {
174         sal_Bool bSuccess = sal_True;
175         sal_uInt16 nEmphasis = FontEmphasisMark::NONE;
176 
177         sal_Bool bBelow = sal_False;
178         sal_Bool bHasPos = sal_False, bHasType = sal_False;
179 
180         ::rtl::OUString sToken;
181         SvXMLTokenEnumerator aTokenEnum(_rStrImpValue);
182         while (aTokenEnum.getNextToken(sToken))
183         {
184             if (!bHasPos)
185             {
186                 if (IsXMLToken(sToken, XML_ABOVE))
187                 {
188                     bBelow = sal_False;
189                     bHasPos = sal_True;
190                 }
191                 else if (IsXMLToken(sToken, XML_BELOW))
192                 {
193                     bBelow = sal_True;
194                     bHasPos = sal_True;
195                 }
196             }
197             if (!bHasType)
198             {
199                 if (SvXMLUnitConverter::convertEnum(nEmphasis, sToken, OEnumMapper::getEnumMap(OEnumMapper::epFontEmphasis)))
200                 {
201                     bHasType = sal_True;
202                 }
203                 else
204                 {
205                     bSuccess = sal_False;
206                     break;
207                 }
208             }
209         }
210 
211         if (bSuccess)
212         {
213             nEmphasis |= bBelow ? FontEmphasisMark::BELOW : FontEmphasisMark::ABOVE;
214             _rValue <<= (sal_Int16)nEmphasis;
215         }
216 
217         return bSuccess;
218     }
219 
220     //=====================================================================
221     //= OControlBorderHandlerBase
222     //=====================================================================
223     //---------------------------------------------------------------------
OControlBorderHandler(const OControlBorderHandler::BorderFacet _eFacet)224     OControlBorderHandler::OControlBorderHandler( const OControlBorderHandler::BorderFacet _eFacet )
225         :m_eFacet( _eFacet )
226     {
227     }
228 
229     //---------------------------------------------------------------------
importXML(const::rtl::OUString & _rStrImpValue,Any & _rValue,const SvXMLUnitConverter &) const230     sal_Bool OControlBorderHandler::importXML( const ::rtl::OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
231     {
232         ::rtl::OUString sToken;
233         SvXMLTokenEnumerator aTokens(_rStrImpValue);
234 
235         sal_uInt16 nStyle = 1;
236         Color aColor;
237 
238         while   (   aTokens.getNextToken(sToken)    // have a new token
239                 &&  (0 != sToken.getLength())       // really have a new token
240                 )
241         {
242             // try interpreting the token as border style
243             if ( m_eFacet == STYLE )
244             {
245                 // is it a valid enum value?
246                 if ( SvXMLUnitConverter::convertEnum( nStyle, sToken, OEnumMapper::getEnumMap( OEnumMapper::epBorderWidth ) ) )
247                 {
248                     _rValue <<= nStyle;
249                     return sal_True;
250                 }
251             }
252 
253             // try interpreting it as color value
254             if ( m_eFacet == COLOR )
255             {
256                 if ( SvXMLUnitConverter::convertColor( aColor, sToken ) )
257                 {
258                     _rValue <<= (sal_Int32)aColor.GetColor();
259                     return sal_True;
260                 }
261             }
262         }
263 
264         return sal_False;
265     }
266 
267     //---------------------------------------------------------------------
exportXML(::rtl::OUString & _rStrExpValue,const Any & _rValue,const SvXMLUnitConverter &) const268     sal_Bool OControlBorderHandler::exportXML( ::rtl::OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
269     {
270         sal_Bool bSuccess = sal_False;
271 
272         ::rtl::OUStringBuffer aOut;
273         switch ( m_eFacet )
274         {
275         case STYLE:
276         {
277             sal_Int16 nBorder = 0;
278             bSuccess =  (_rValue >>= nBorder)
279                     &&  SvXMLUnitConverter::convertEnum( aOut, nBorder, OEnumMapper::getEnumMap( OEnumMapper::epBorderWidth ) );
280         }
281         break;
282         case COLOR:
283         {
284             sal_Int32 nBorderColor = 0;
285             if ( _rValue >>= nBorderColor )
286             {
287                 SvXMLUnitConverter::convertColor( aOut, Color( nBorderColor ) );
288                 bSuccess = sal_True;
289             }
290         }
291         break;
292         }   // switch ( m_eFacet )
293 
294         if ( !bSuccess )
295             return sal_False;
296 
297         if ( _rStrExpValue.getLength() )
298             _rStrExpValue += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " " ) );
299         _rStrExpValue += aOut.makeStringAndClear();
300 
301         return sal_True;
302     }
303 
304     //=====================================================================
305     //= OFontWidthHandler
306     //=====================================================================
307     //---------------------------------------------------------------------
OFontWidthHandler()308     OFontWidthHandler::OFontWidthHandler()
309     {
310     }
311 
312     //---------------------------------------------------------------------
importXML(const::rtl::OUString & _rStrImpValue,Any & _rValue,const SvXMLUnitConverter &) const313     sal_Bool OFontWidthHandler::importXML( const ::rtl::OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
314     {
315         sal_Int32 nWidth = 0;
316         sal_Bool bSuccess = SvXMLUnitConverter::convertMeasure(nWidth, _rStrImpValue, MAP_POINT);
317         if (bSuccess)
318             _rValue <<= (sal_Int16)nWidth;
319 
320         return bSuccess;
321     }
322 
323     //---------------------------------------------------------------------
exportXML(::rtl::OUString & _rStrExpValue,const Any & _rValue,const SvXMLUnitConverter &) const324     sal_Bool OFontWidthHandler::exportXML( ::rtl::OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
325     {
326         sal_Int16 nWidth = 0;
327         ::rtl::OUStringBuffer aResult;
328         if (_rValue >>= nWidth)
329             SvXMLUnitConverter::convertMeasure(aResult, nWidth, MAP_POINT, MAP_POINT);
330         _rStrExpValue = aResult.makeStringAndClear();
331 
332         return _rStrExpValue.getLength() != 0;
333     }
334 
335     //=====================================================================
336     //= ORotationAngleHandler
337     //=====================================================================
338     //---------------------------------------------------------------------
ORotationAngleHandler()339     ORotationAngleHandler::ORotationAngleHandler()
340     {
341     }
342 
343     //---------------------------------------------------------------------
importXML(const::rtl::OUString & _rStrImpValue,Any & _rValue,const SvXMLUnitConverter &) const344     sal_Bool ORotationAngleHandler::importXML( const ::rtl::OUString& _rStrImpValue, Any& _rValue, const SvXMLUnitConverter& ) const
345     {
346         double fValue;
347         sal_Bool bSucces =
348             SvXMLUnitConverter::convertDouble(fValue, _rStrImpValue);
349         if (bSucces)
350         {
351             fValue *= 10;
352             _rValue <<= (float)fValue;
353         }
354 
355         return bSucces;
356     }
357 
358     //---------------------------------------------------------------------
exportXML(::rtl::OUString & _rStrExpValue,const Any & _rValue,const SvXMLUnitConverter &) const359     sal_Bool ORotationAngleHandler::exportXML( ::rtl::OUString& _rStrExpValue, const Any& _rValue, const SvXMLUnitConverter& ) const
360     {
361         float fAngle = 0;
362         sal_Bool bSuccess = (_rValue >>= fAngle);
363 
364         if (bSuccess)
365         {
366             rtl::OUStringBuffer sValue;
367             SvXMLUnitConverter::convertDouble(sValue, ((double)fAngle) / 10);
368             _rStrExpValue = sValue.makeStringAndClear();
369         }
370 
371         return bSuccess;
372     }
373 
374     //=====================================================================
375     //= ImageScaleModeHandler
376     //=====================================================================
377     //---------------------------------------------------------------------
ImageScaleModeHandler()378     ImageScaleModeHandler::ImageScaleModeHandler()
379         :XMLConstantsPropertyHandler( OEnumMapper::getEnumMap( OEnumMapper::epImageScaleMode ), XML_STRETCH )
380     {
381     }
382 
383 //.........................................................................
384 }   // namespace xmloff
385 //.........................................................................
386 
387