1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include "cdouthdl.hxx"
21 #include <xmloff/xmltoken.hxx>
22 #include <xmloff/xmluconv.hxx>
23 #include <xmloff/xmlement.hxx>
24 #include <rtl/ustrbuf.hxx>
25 #include <osl/diagnose.h>
26 
27 #include <com/sun/star/awt/FontStrikeout.hpp>
28 #include <com/sun/star/uno/Any.hxx>
29 
30 using namespace ::com::sun::star;
31 using namespace ::com::sun::star::awt;
32 using namespace ::xmloff::token;
33 
34 SvXMLEnumMapEntry<sal_uInt16> const pXML_CrossedoutType_Enum[] =
35 {
36     { XML_NONE,             awt::FontStrikeout::NONE },
37     { XML_SINGLE,           awt::FontStrikeout::SINGLE },
38     { XML_DOUBLE,           awt::FontStrikeout::DOUBLE },
39     { XML_SINGLE,           awt::FontStrikeout::BOLD },
40     { XML_SINGLE,           awt::FontStrikeout::SLASH },
41     { XML_SINGLE,           awt::FontStrikeout::X },
42     { XML_TOKEN_INVALID,    0 }
43 };
44 
45 SvXMLEnumMapEntry<sal_uInt16> const pXML_CrossedoutStyle_Enum[] =
46 {
47     { XML_NONE,             awt::FontStrikeout::NONE },
48     { XML_SOLID,            awt::FontStrikeout::SINGLE },
49     { XML_SOLID,            awt::FontStrikeout::DOUBLE },
50     { XML_SOLID,            awt::FontStrikeout::BOLD },
51     { XML_SOLID,            awt::FontStrikeout::SLASH },
52     { XML_SOLID,            awt::FontStrikeout::X },
53     { XML_DOTTED,           awt::FontStrikeout::SINGLE },
54     { XML_DASH,             awt::FontStrikeout::SINGLE },
55     { XML_LONG_DASH,        awt::FontStrikeout::SINGLE },
56     { XML_DOT_DASH,         awt::FontStrikeout::SINGLE },
57     { XML_DOT_DOT_DASH,     awt::FontStrikeout::SINGLE },
58     { XML_WAVE,             awt::FontStrikeout::SINGLE },
59     { XML_TOKEN_INVALID,    0 }
60 };
61 
62 SvXMLEnumMapEntry<sal_uInt16> const pXML_CrossedoutWidth_Enum[] =
63 {
64     { XML_AUTO,                 awt::FontStrikeout::NONE },
65     { XML_AUTO,                 awt::FontStrikeout::SINGLE },
66     { XML_AUTO,                 awt::FontStrikeout::DOUBLE },
67     { XML_BOLD,                 awt::FontStrikeout::BOLD },
68     { XML_AUTO,                 awt::FontStrikeout::SLASH },
69     { XML_AUTO,                 awt::FontStrikeout::X },
70     { XML_THIN,                 awt::FontStrikeout::NONE },
71     { XML_MEDIUM,               awt::FontStrikeout::NONE },
72     { XML_THICK,                awt::FontStrikeout::NONE },
73     { XML_TOKEN_INVALID,        0 }
74 };
75 
76 // class XMLCrossedOutTypePropHdl
77 
~XMLCrossedOutTypePropHdl()78 XMLCrossedOutTypePropHdl::~XMLCrossedOutTypePropHdl()
79 {
80     // nothing to do
81 }
82 
importXML(const OUString & rStrImpValue,uno::Any & rValue,const SvXMLUnitConverter &) const83 bool XMLCrossedOutTypePropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
84 {
85     sal_uInt16 eNewStrikeout = 0;
86     bool bRet = SvXMLUnitConverter::convertEnum(
87         eNewStrikeout, rStrImpValue, pXML_CrossedoutType_Enum );
88     if( bRet )
89     {
90         // multi property: style and width might be set already.
91         // If the old value is NONE, the new is used unchanged.
92         sal_Int16 eStrikeout = sal_Int16();
93         if( (rValue >>= eStrikeout) && awt::FontStrikeout::NONE!=eStrikeout )
94         {
95             switch( eNewStrikeout )
96             {
97             case awt::FontStrikeout::NONE:
98             case awt::FontStrikeout::SINGLE:
99                 // keep existing line style
100                 eNewStrikeout = eStrikeout;
101                 break;
102             case awt::FontStrikeout::DOUBLE:
103                 // A double line style has priority over a solid or a bold
104                 // line style,
105                 // but not about any other line style
106                 switch( eStrikeout )
107                 {
108                 case awt::FontStrikeout::SINGLE:
109                 case awt::FontStrikeout::BOLD:
110                     break;
111                 default:
112                     // If a double line style is not supported for the existing
113                     // value, keep the new one
114                     eNewStrikeout = eStrikeout;
115                     break;
116                 }
117                 break;
118             default:
119                 OSL_ENSURE( bRet, "unexpected line type value" );
120                 break;
121             }
122             if( eNewStrikeout != eStrikeout )
123                 rValue <<= static_cast<sal_Int16>(eNewStrikeout);
124         }
125         else
126         {
127             rValue <<= static_cast<sal_Int16>(eNewStrikeout);
128         }
129     }
130 
131     return bRet;
132 }
133 
exportXML(OUString & rStrExpValue,const uno::Any & rValue,const SvXMLUnitConverter &) const134 bool XMLCrossedOutTypePropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
135 {
136     bool bRet = false;
137     sal_uInt16 nValue = sal_uInt16();
138     OUStringBuffer aOut;
139 
140     if (rValue >>= nValue)
141     {
142         bRet = SvXMLUnitConverter::convertEnum(
143             aOut, nValue, pXML_CrossedoutType_Enum );
144         if( bRet )
145             rStrExpValue = aOut.makeStringAndClear();
146     }
147 
148     return bRet;
149 }
150 
151 // class XMLCrossedOutStylePropHdl
152 
~XMLCrossedOutStylePropHdl()153 XMLCrossedOutStylePropHdl::~XMLCrossedOutStylePropHdl()
154 {
155     // nothing to do
156 }
157 
importXML(const OUString & rStrImpValue,uno::Any & rValue,const SvXMLUnitConverter &) const158 bool XMLCrossedOutStylePropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
159 {
160     sal_uInt16 eNewStrikeout;
161     bool bRet = SvXMLUnitConverter::convertEnum(
162         eNewStrikeout, rStrImpValue, pXML_CrossedoutStyle_Enum );
163     if( bRet )
164     {
165         // multi property: style and width might be set already.
166         // If the old value is NONE, the new is used unchanged.
167         sal_Int16 eStrikeout = sal_Int16();
168         if( (rValue >>= eStrikeout) && awt::FontStrikeout::NONE!=eStrikeout )
169         {
170             // one NONE a SINGLE are possible new values. For both, the
171             // existing value is kept.
172         }
173         else
174         {
175             rValue <<= static_cast<sal_Int16>(eNewStrikeout);
176         }
177     }
178 
179     return bRet;
180 }
181 
exportXML(OUString & rStrExpValue,const uno::Any & rValue,const SvXMLUnitConverter &) const182 bool XMLCrossedOutStylePropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
183 {
184     bool bRet = false;
185     sal_uInt16 nValue = sal_uInt16();
186     OUStringBuffer aOut;
187 
188     if( rValue >>= nValue )
189     {
190         bRet = SvXMLUnitConverter::convertEnum(
191             aOut, nValue, pXML_CrossedoutStyle_Enum );
192         if( bRet )
193             rStrExpValue = aOut.makeStringAndClear();
194     }
195 
196     return bRet;
197 }
198 
199 // class XMLCrossedOutWidthPropHdl
200 
~XMLCrossedOutWidthPropHdl()201 XMLCrossedOutWidthPropHdl::~XMLCrossedOutWidthPropHdl()
202 {
203     // nothing to do
204 }
205 
importXML(const OUString & rStrImpValue,uno::Any & rValue,const SvXMLUnitConverter &) const206 bool XMLCrossedOutWidthPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
207 {
208     sal_uInt16 eNewStrikeout = 0;
209     bool bRet = SvXMLUnitConverter::convertEnum(
210         eNewStrikeout, rStrImpValue, pXML_CrossedoutWidth_Enum );
211     if( bRet )
212     {
213         // multi property: style and width might be set already.
214         // If the old value is NONE, the new is used unchanged.
215         sal_Int16 eStrikeout = sal_Int16();
216         if( (rValue >>= eStrikeout) && awt::FontStrikeout::NONE!=eStrikeout )
217         {
218             switch( eNewStrikeout )
219             {
220             case awt::FontStrikeout::NONE:
221                 // keep existing line style
222                 eNewStrikeout = eStrikeout;
223                 break;
224             case awt::FontStrikeout::BOLD:
225                 switch( eStrikeout )
226                 {
227                 case awt::FontStrikeout::SINGLE:
228                     break;
229                 default:
230                     // If a double line style is not supported for the existing
231                     // value, keep the new one
232                     eNewStrikeout = eStrikeout;
233                     break;
234                 }
235                 break;
236             default:
237                 OSL_ENSURE( bRet, "unexpected line type value" );
238                 break;
239             }
240             if( eNewStrikeout != eStrikeout )
241                 rValue <<= static_cast<sal_Int16>(eNewStrikeout);
242         }
243         else
244         {
245             rValue <<= static_cast<sal_Int16>(eNewStrikeout);
246         }
247     }
248 
249     return bRet;
250 }
251 
exportXML(OUString & rStrExpValue,const uno::Any & rValue,const SvXMLUnitConverter &) const252 bool XMLCrossedOutWidthPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
253 {
254     bool bRet = false;
255     sal_uInt16 nValue = sal_uInt16();
256     OUStringBuffer aOut;
257 
258     if( (rValue >>= nValue) && (awt::FontStrikeout::BOLD == nValue) )
259     {
260         bRet = SvXMLUnitConverter::convertEnum(
261             aOut, nValue, pXML_CrossedoutWidth_Enum );
262         if( bRet )
263             rStrExpValue = aOut.makeStringAndClear();
264     }
265 
266     return bRet;
267 }
268 
269 // class XMLCrossedOutTextPropHdl
270 
~XMLCrossedOutTextPropHdl()271 XMLCrossedOutTextPropHdl::~XMLCrossedOutTextPropHdl()
272 {
273     // nothing to do
274 }
275 
importXML(const OUString & rStrImpValue,uno::Any & rValue,const SvXMLUnitConverter &) const276 bool XMLCrossedOutTextPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
277 {
278     bool bRet = false;
279 
280     if( !rStrImpValue.isEmpty() )
281     {
282         sal_Int16 eStrikeout = ('/' == rStrImpValue[0]
283                                         ? awt::FontStrikeout::SLASH
284                                         : awt::FontStrikeout::X);
285         rValue <<= eStrikeout;
286         bRet = true;
287     }
288 
289     return bRet;
290 }
291 
exportXML(OUString & rStrExpValue,const uno::Any & rValue,const SvXMLUnitConverter &) const292 bool XMLCrossedOutTextPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
293 {
294     bool bRet = false;
295     sal_Int16 nValue = sal_Int16();
296 
297     if( (rValue >>= nValue) &&
298         (awt::FontStrikeout::SLASH == nValue || awt::FontStrikeout::X == nValue) )
299     {
300         rStrExpValue = OUString(
301             static_cast< sal_Unicode>( awt::FontStrikeout::SLASH == nValue ? '/'
302                                                                       : 'X' ) );
303         bRet = true;
304     }
305 
306     return bRet;
307 }
308 
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
310