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 "PageMasterPropHdl.hxx"
27 #include <xmloff/xmltoken.hxx>
28 #include <xmloff/xmluconv.hxx>
29 #include <xmloff/xmlnumi.hxx>
30 #include <xmloff/xmlnume.hxx>
31 #include <rtl/ustrbuf.hxx>
32 #include <com/sun/star/uno/Any.hxx>
33 #include <com/sun/star/style/PageStyleLayout.hpp>
34 #include <comphelper/types.hxx>
35 #include <comphelper/extract.hxx>
36 
37 using ::rtl::OUString;
38 using ::rtl::OUStringBuffer;
39 
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::style;
43 using namespace ::comphelper;
44 using namespace ::xmloff::token;
45 
46 
47 //______________________________________________________________________________
48 
49 #define DEFAULT_PAPERTRAY   (sal_Int32(-1))
50 
51 
52 //______________________________________________________________________________
53 // property handler for style:page-usage (style::PageStyleLayout)
54 
~XMLPMPropHdl_PageStyleLayout()55 XMLPMPropHdl_PageStyleLayout::~XMLPMPropHdl_PageStyleLayout()
56 {
57 }
58 
equals(const Any & rAny1,const Any & rAny2) const59 bool XMLPMPropHdl_PageStyleLayout::equals( const Any& rAny1, const Any& rAny2 ) const
60 {
61     style::PageStyleLayout eLayout1, eLayout2;
62     return ((rAny1 >>= eLayout1) && (rAny2 >>= eLayout2)) ? (eLayout1 == eLayout2) : sal_False;
63 }
64 
importXML(const OUString & rStrImpValue,Any & rValue,const SvXMLUnitConverter &) const65 sal_Bool XMLPMPropHdl_PageStyleLayout::importXML(
66         const OUString& rStrImpValue,
67         Any& rValue,
68         const SvXMLUnitConverter& ) const
69 {
70     sal_Bool bRet = sal_True;
71 
72     if( IsXMLToken( rStrImpValue, XML_ALL ) )
73         rValue <<= PageStyleLayout_ALL;
74     else if( IsXMLToken( rStrImpValue, XML_LEFT ) )
75         rValue <<= PageStyleLayout_LEFT;
76     else if( IsXMLToken( rStrImpValue, XML_RIGHT ) )
77         rValue <<= PageStyleLayout_RIGHT;
78     else if( IsXMLToken( rStrImpValue, XML_MIRRORED ) )
79         rValue <<= PageStyleLayout_MIRRORED;
80     else
81         bRet = sal_False;
82 
83     return bRet;
84 }
85 
exportXML(OUString & rStrExpValue,const Any & rValue,const SvXMLUnitConverter &) const86 sal_Bool XMLPMPropHdl_PageStyleLayout::exportXML(
87         OUString& rStrExpValue,
88         const Any& rValue,
89         const SvXMLUnitConverter& ) const
90 {
91     sal_Bool        bRet = sal_False;
92     PageStyleLayout eLayout;
93 
94     if( rValue >>= eLayout )
95     {
96         bRet = sal_True;
97         switch( eLayout )
98         {
99             case PageStyleLayout_ALL:
100                 rStrExpValue = GetXMLToken( XML_ALL );
101             break;
102             case PageStyleLayout_LEFT:
103                 rStrExpValue = GetXMLToken( XML_LEFT );
104             break;
105             case PageStyleLayout_RIGHT:
106                 rStrExpValue = GetXMLToken( XML_RIGHT );
107             break;
108             case PageStyleLayout_MIRRORED:
109                 rStrExpValue = GetXMLToken( XML_MIRRORED );
110             break;
111             default:
112                 bRet = sal_False;
113         }
114     }
115 
116     return bRet;
117 }
118 
119 
120 //______________________________________________________________________________
121 // property handler for style:num-format (style::NumberingType)
122 
~XMLPMPropHdl_NumFormat()123 XMLPMPropHdl_NumFormat::~XMLPMPropHdl_NumFormat()
124 {
125 }
126 
importXML(const OUString & rStrImpValue,Any & rValue,const SvXMLUnitConverter & rUnitConverter) const127 sal_Bool XMLPMPropHdl_NumFormat::importXML(
128         const OUString& rStrImpValue,
129         Any& rValue,
130         const SvXMLUnitConverter& rUnitConverter ) const
131 {
132     sal_Int16 nSync = sal_Int16();
133     sal_Int16 nNumType = NumberingType::NUMBER_NONE;
134     rUnitConverter.convertNumFormat( nNumType, rStrImpValue, OUString(),
135                                      sal_True );
136 
137     if( !(rValue >>= nSync) )
138         nSync = NumberingType::NUMBER_NONE;
139 
140     // if num-letter-sync appears before num-format, the function
141     // XMLPMPropHdl_NumLetterSync::importXML() sets the value
142     // NumberingType::CHARS_LOWER_LETTER_N
143     if( nSync == NumberingType::CHARS_LOWER_LETTER_N )
144     {
145         switch( nNumType )
146         {
147             case NumberingType::CHARS_LOWER_LETTER:
148                 nNumType = NumberingType::CHARS_LOWER_LETTER_N;
149             break;
150             case NumberingType::CHARS_UPPER_LETTER:
151                 nNumType = NumberingType::CHARS_UPPER_LETTER_N;
152             break;
153         }
154     }
155     rValue <<= nNumType;
156 
157     return sal_True;
158 }
159 
exportXML(OUString & rStrExpValue,const Any & rValue,const SvXMLUnitConverter & rUnitConverter) const160 sal_Bool XMLPMPropHdl_NumFormat::exportXML(
161         OUString& rStrExpValue,
162         const Any& rValue,
163         const SvXMLUnitConverter& rUnitConverter ) const
164 {
165     sal_Bool    bRet = sal_False;
166     sal_Int16   nNumType = sal_Int16();
167 
168     if( rValue >>= nNumType )
169     {
170         OUStringBuffer aBuffer( 10 );
171         rUnitConverter.convertNumFormat( aBuffer, nNumType );
172         rStrExpValue = aBuffer.makeStringAndClear();
173         bRet = sal_True;
174     }
175     return bRet;
176 }
177 
178 
179 //______________________________________________________________________________
180 // property handler for style:num-letter-sync (style::NumberingType)
181 
~XMLPMPropHdl_NumLetterSync()182 XMLPMPropHdl_NumLetterSync::~XMLPMPropHdl_NumLetterSync()
183 {
184 }
185 
importXML(const OUString & rStrImpValue,Any & rValue,const SvXMLUnitConverter & rUnitConverter) const186 sal_Bool XMLPMPropHdl_NumLetterSync::importXML(
187         const OUString& rStrImpValue,
188         Any& rValue,
189         const SvXMLUnitConverter& rUnitConverter ) const
190 {
191     sal_Int16 nNumType;
192     sal_Int16 nSync = NumberingType::NUMBER_NONE;
193     rUnitConverter.convertNumFormat( nSync, rStrImpValue,
194                                      GetXMLToken( XML_A ), sal_True );
195 
196     if( !(rValue >>= nNumType) )
197         nNumType = NumberingType::NUMBER_NONE;
198 
199     if( nSync == NumberingType::CHARS_LOWER_LETTER_N )
200     {
201         switch( nNumType )
202         {
203             case NumberingType::CHARS_LOWER_LETTER:
204                 nNumType = NumberingType::CHARS_LOWER_LETTER_N;
205             break;
206             case NumberingType::CHARS_UPPER_LETTER:
207                 nNumType = NumberingType::CHARS_UPPER_LETTER_N;
208             break;
209         }
210     }
211     rValue <<= nNumType;
212 
213     return sal_True;
214 }
215 
exportXML(OUString & rStrExpValue,const Any & rValue,const SvXMLUnitConverter & rUnitConverter) const216 sal_Bool XMLPMPropHdl_NumLetterSync::exportXML(
217         OUString& rStrExpValue,
218         const Any& rValue,
219         const SvXMLUnitConverter& rUnitConverter ) const
220 {
221     sal_Bool    bRet = sal_False;
222     sal_Int16   nNumType = sal_Int16();
223 
224     if( rValue >>= nNumType )
225     {
226         OUStringBuffer aBuffer( 5 );
227         rUnitConverter.convertNumLetterSync( aBuffer, nNumType );
228         rStrExpValue = aBuffer.makeStringAndClear();
229         bRet = rStrExpValue.getLength() > 0;
230     }
231     return bRet;
232 }
233 
234 
235 //______________________________________________________________________________
236 // property handler for style:paper-tray-number
237 
~XMLPMPropHdl_PaperTrayNumber()238 XMLPMPropHdl_PaperTrayNumber::~XMLPMPropHdl_PaperTrayNumber()
239 {
240 }
241 
importXML(const OUString & rStrImpValue,Any & rValue,const SvXMLUnitConverter &) const242 sal_Bool XMLPMPropHdl_PaperTrayNumber::importXML(
243         const OUString& rStrImpValue,
244         Any& rValue,
245         const SvXMLUnitConverter& ) const
246 {
247     sal_Bool bRet = sal_False;
248 
249     if( IsXMLToken( rStrImpValue, XML_DEFAULT ) )
250     {
251         rValue <<= DEFAULT_PAPERTRAY;
252         bRet = sal_True;
253     }
254     else
255     {
256         sal_Int32 nPaperTray;
257         if( SvXMLUnitConverter::convertNumber( nPaperTray, rStrImpValue, 0 ) )
258         {
259             rValue <<= nPaperTray;
260             bRet = sal_True;
261         }
262     }
263 
264     return bRet;
265 }
266 
exportXML(OUString & rStrExpValue,const Any & rValue,const SvXMLUnitConverter &) const267 sal_Bool XMLPMPropHdl_PaperTrayNumber::exportXML(
268         OUString& rStrExpValue,
269         const Any& rValue,
270         const SvXMLUnitConverter& ) const
271 {
272     sal_Bool    bRet = sal_False;
273     sal_Int32   nPaperTray = 0;
274 
275     if( rValue >>= nPaperTray )
276     {
277         if( nPaperTray == DEFAULT_PAPERTRAY )
278             rStrExpValue = GetXMLToken( XML_DEFAULT );
279         else
280         {
281             OUStringBuffer aBuffer;
282             SvXMLUnitConverter::convertNumber( aBuffer, nPaperTray );
283             rStrExpValue = aBuffer.makeStringAndClear();
284         }
285         bRet = sal_True;
286     }
287     return bRet;
288 }
289 
290 
291 //______________________________________________________________________________
292 // property handler for style:print
293 
XMLPMPropHdl_Print(enum XMLTokenEnum eValue)294 XMLPMPropHdl_Print::XMLPMPropHdl_Print( enum XMLTokenEnum eValue ) :
295     sAttrValue( GetXMLToken( eValue ) )
296 {
297 }
298 
~XMLPMPropHdl_Print()299 XMLPMPropHdl_Print::~XMLPMPropHdl_Print()
300 {
301 }
302 
importXML(const OUString & rStrImpValue,Any & rValue,const SvXMLUnitConverter &) const303 sal_Bool XMLPMPropHdl_Print::importXML(
304         const OUString& rStrImpValue,
305         Any& rValue,
306         const SvXMLUnitConverter& ) const
307 {
308     sal_Unicode cToken  = ' ';
309     sal_Int32   nTokenIndex = 0;
310     sal_Bool    bFound  = sal_False;
311 
312     do
313     {
314         bFound = (sAttrValue == rStrImpValue.getToken( 0, cToken, nTokenIndex ));
315     }
316     while ( (nTokenIndex >= 0) && !bFound );
317 
318     setBOOL( rValue, bFound );
319     return sal_True;
320 }
321 
exportXML(OUString & rStrExpValue,const Any & rValue,const SvXMLUnitConverter &) const322 sal_Bool XMLPMPropHdl_Print::exportXML(
323         OUString& rStrExpValue,
324         const Any& rValue,
325         const SvXMLUnitConverter& ) const
326 {
327     if( getBOOL( rValue ) )
328     {
329         if( rStrExpValue.getLength() )
330             rStrExpValue += OUString( RTL_CONSTASCII_USTRINGPARAM( " " ) );
331         rStrExpValue += sAttrValue;
332     }
333 
334     return sal_True;
335 }
336 
337 //______________________________________________________________________________
338 // property handler for style:table-centering
339 
~XMLPMPropHdl_CenterHorizontal()340 XMLPMPropHdl_CenterHorizontal::~XMLPMPropHdl_CenterHorizontal()
341 {
342 }
343 
importXML(const OUString & rStrImpValue,Any & rValue,const SvXMLUnitConverter &) const344 sal_Bool XMLPMPropHdl_CenterHorizontal::importXML(
345         const OUString& rStrImpValue,
346         Any& rValue,
347         const SvXMLUnitConverter& ) const
348 {
349     sal_Bool bRet = sal_False;
350 
351     if (rStrImpValue.getLength())
352         if (IsXMLToken( rStrImpValue, XML_BOTH) ||
353             IsXMLToken( rStrImpValue, XML_HORIZONTAL))
354         {
355             rValue = ::cppu::bool2any(sal_True);
356             bRet = sal_True;
357         }
358 
359 
360     return bRet;
361 }
362 
exportXML(OUString & rStrExpValue,const Any & rValue,const SvXMLUnitConverter &) const363 sal_Bool XMLPMPropHdl_CenterHorizontal::exportXML(
364         OUString& rStrExpValue,
365         const Any& rValue,
366         const SvXMLUnitConverter& ) const
367 {
368     sal_Bool    bRet = sal_False;
369 
370     if ( ::cppu::any2bool( rValue ) )
371     {
372         bRet = sal_True;
373         if (rStrExpValue.getLength())
374             rStrExpValue = GetXMLToken(XML_BOTH);
375         else
376             rStrExpValue = GetXMLToken(XML_HORIZONTAL);
377     }
378 
379     return bRet;
380 }
381 
~XMLPMPropHdl_CenterVertical()382 XMLPMPropHdl_CenterVertical::~XMLPMPropHdl_CenterVertical()
383 {
384 }
385 
importXML(const OUString & rStrImpValue,Any & rValue,const SvXMLUnitConverter &) const386 sal_Bool XMLPMPropHdl_CenterVertical::importXML(
387         const OUString& rStrImpValue,
388         Any& rValue,
389         const SvXMLUnitConverter& ) const
390 {
391     sal_Bool bRet = sal_False;
392 
393     if (rStrImpValue.getLength())
394         if (IsXMLToken(rStrImpValue, XML_BOTH) ||
395             IsXMLToken(rStrImpValue, XML_VERTICAL) )
396         {
397             rValue = ::cppu::bool2any(sal_True);
398             bRet = sal_True;
399         }
400 
401     return bRet;
402 }
403 
exportXML(OUString & rStrExpValue,const Any & rValue,const SvXMLUnitConverter &) const404 sal_Bool XMLPMPropHdl_CenterVertical::exportXML(
405         OUString& rStrExpValue,
406         const Any& rValue,
407         const SvXMLUnitConverter& ) const
408 {
409     sal_Bool    bRet = sal_False;
410 
411     if ( ::cppu::any2bool( rValue ) )
412     {
413         bRet = sal_True;
414         if (rStrExpValue.getLength())
415             rStrExpValue = GetXMLToken(XML_BOTH);
416         else
417             rStrExpValue = GetXMLToken(XML_VERTICAL);
418     }
419 
420     return bRet;
421 }
422 
423