bump product version to 4.1.6.2
[LibreOffice.git] / xmloff / source / style / bordrhdl.cxx
blob736551b9cb683631c64fc122cd58553d0c0019d1
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 <bordrhdl.hxx>
22 #include <sax/tools/converter.hxx>
23 #include <xmloff/xmltoken.hxx>
24 #include <xmloff/xmluconv.hxx>
25 #include <rtl/ustrbuf.hxx>
26 #include <com/sun/star/uno/Any.hxx>
27 #include <com/sun/star/table/BorderLine2.hpp>
30 using namespace ::com::sun::star;
31 using namespace ::xmloff::token;
33 const sal_uInt16 API_LINE_SOLID = 0;
34 const sal_uInt16 API_LINE_DOTTED = 1;
35 const sal_uInt16 API_LINE_DASHED = 2;
36 const sal_uInt16 API_LINE_DOUBLE = 3;
37 const sal_uInt16 API_LINE_THINTHICK_SMALLGAP = 4;
38 const sal_uInt16 API_LINE_THINTHICK_MEDIUMGAP = 5;
39 const sal_uInt16 API_LINE_THINTHICK_LARGEGAP = 6;
40 const sal_uInt16 API_LINE_THICKTHIN_SMALLGAP = 7;
41 const sal_uInt16 API_LINE_THICKTHIN_MEDIUMGAP = 8;
42 const sal_uInt16 API_LINE_THICKTHIN_LARGEGAP = 9;
43 const sal_uInt16 API_LINE_EMBOSSED = 10;
44 const sal_uInt16 API_LINE_ENGRAVED = 11;
45 const sal_uInt16 API_LINE_OUTSET = 12;
46 const sal_uInt16 API_LINE_INSET = 13;
47 const sal_uInt16 API_LINE_NONE = USHRT_MAX;
49 #define DEF_LINE_WIDTH_0 1
50 #define DEF_LINE_WIDTH_1 35
51 #define DEF_LINE_WIDTH_2 88
53 #define SVX_XML_BORDER_WIDTH_THIN 0
54 #define SVX_XML_BORDER_WIDTH_MIDDLE 1
55 #define SVX_XML_BORDER_WIDTH_THICK 2
57 SvXMLEnumMapEntry pXML_BorderStyles[] =
59 { XML_NONE, API_LINE_NONE },
60 { XML_HIDDEN, API_LINE_NONE },
61 { XML_SOLID, API_LINE_SOLID },
62 { XML_DOUBLE, API_LINE_DOUBLE },
63 { XML_DOTTED, API_LINE_DOTTED },
64 { XML_DASHED, API_LINE_DASHED },
65 { XML_GROOVE, API_LINE_ENGRAVED },
66 { XML_RIDGE, API_LINE_EMBOSSED },
67 { XML_INSET, API_LINE_INSET },
68 { XML_OUTSET, API_LINE_OUTSET },
69 { XML_TOKEN_INVALID, 0 }
72 SvXMLEnumMapEntry pXML_NamedBorderWidths[] =
74 { XML_THIN, SVX_XML_BORDER_WIDTH_THIN },
75 { XML_MIDDLE, SVX_XML_BORDER_WIDTH_MIDDLE },
76 { XML_THICK, SVX_XML_BORDER_WIDTH_THICK },
77 { XML_TOKEN_INVALID, 0 }
79 // mapping tables to map external xml input to intarnal box line widths
82 static sal_uInt16 const aBorderWidths[] =
84 DEF_LINE_WIDTH_0,
85 DEF_LINE_WIDTH_1,
86 DEF_LINE_WIDTH_2
89 static void lcl_frmitems_setXMLBorderStyle( table::BorderLine2 & rBorderLine, sal_uInt16 nStyle )
91 sal_Int16 eStyle = -1; // None
92 if ( nStyle != API_LINE_NONE )
93 eStyle = sal_Int16( nStyle );
95 rBorderLine.LineStyle = eStyle;
99 ///////////////////////////////////////////////////////////////////////////////
101 // class XMLEscapementPropHdl
104 XMLBorderWidthHdl::~XMLBorderWidthHdl()
106 // nothing to do
109 sal_Bool XMLBorderWidthHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
111 SvXMLTokenEnumerator aTokenEnum( rStrImpValue );
113 sal_Int32 nInWidth, nDistance, nOutWidth;
115 OUString aToken;
116 if( !aTokenEnum.getNextToken( aToken ) )
117 return sal_False;
119 if (!rUnitConverter.convertMeasureToCore( nInWidth, aToken, 0, 500 ))
120 return sal_False;
122 if( !aTokenEnum.getNextToken( aToken ) )
123 return sal_False;
125 if (!rUnitConverter.convertMeasureToCore( nDistance, aToken, 0, 500 ))
126 return sal_False;
128 if( !aTokenEnum.getNextToken( aToken ) )
129 return sal_False;
131 if (!rUnitConverter.convertMeasureToCore( nOutWidth, aToken, 0, 500 ))
132 return sal_False;
134 table::BorderLine2 aBorderLine;
135 if(!(rValue >>= aBorderLine))
136 aBorderLine.Color = 0;
138 aBorderLine.InnerLineWidth = sal::static_int_cast< sal_Int16 >(nInWidth);
139 aBorderLine.OuterLineWidth = sal::static_int_cast< sal_Int16 >(nOutWidth);
140 aBorderLine.LineDistance = sal::static_int_cast< sal_Int16 >(nDistance);
142 rValue <<= aBorderLine;
143 return sal_True;
146 sal_Bool XMLBorderWidthHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
148 OUStringBuffer aOut;
150 table::BorderLine2 aBorderLine;
151 if(!(rValue >>= aBorderLine))
152 return sal_False;
154 bool bDouble = false;
155 switch ( aBorderLine.LineStyle )
157 case API_LINE_DOUBLE:
158 case API_LINE_THINTHICK_SMALLGAP:
159 case API_LINE_THINTHICK_MEDIUMGAP:
160 case API_LINE_THINTHICK_LARGEGAP:
161 case API_LINE_THICKTHIN_SMALLGAP:
162 case API_LINE_THICKTHIN_MEDIUMGAP:
163 case API_LINE_THICKTHIN_LARGEGAP:
164 bDouble = true;
165 break;
166 default:
167 break;
170 if( ( aBorderLine.LineDistance == 0 && aBorderLine.InnerLineWidth == 0 ) || !bDouble )
171 return sal_False;
173 rUnitConverter.convertMeasureToXML( aOut, aBorderLine.InnerLineWidth );
174 aOut.append( sal_Unicode( ' ' ) );
175 rUnitConverter.convertMeasureToXML( aOut, aBorderLine.LineDistance );
176 aOut.append( sal_Unicode( ' ' ) );
177 rUnitConverter.convertMeasureToXML( aOut, aBorderLine.OuterLineWidth );
179 rStrExpValue = aOut.makeStringAndClear();
180 return sal_True;
183 ///////////////////////////////////////////////////////////////////////////////
185 // class XMLEscapementHeightPropHdl
188 XMLBorderHdl::~XMLBorderHdl()
190 // nothing to do
193 sal_Bool XMLBorderHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
195 OUString aToken;
196 SvXMLTokenEnumerator aTokens( rStrImpValue );
198 sal_Bool bHasStyle = sal_False;
199 sal_Bool bHasWidth = sal_False;
200 sal_Bool bHasColor = sal_False;
202 sal_uInt16 nStyle = USHRT_MAX;
203 sal_uInt16 nWidth = 0;
204 sal_uInt16 nNamedWidth = USHRT_MAX;
205 sal_Int32 nColor = 0;
207 sal_Int32 nTemp;
208 while( aTokens.getNextToken( aToken ) && !aToken.isEmpty() )
210 if( !bHasWidth &&
211 rUnitConverter.convertEnum( nNamedWidth, aToken,
212 pXML_NamedBorderWidths ) )
214 bHasWidth = sal_True;
216 else if( !bHasStyle &&
217 rUnitConverter.convertEnum( nStyle, aToken,
218 pXML_BorderStyles ) )
220 bHasStyle = sal_True;
222 else if (!bHasColor && ::sax::Converter::convertColor(nColor, aToken))
224 bHasColor = sal_True;
226 else if( !bHasWidth &&
227 rUnitConverter.convertMeasureToCore( nTemp, aToken, 0,
228 USHRT_MAX ) )
230 nWidth = (sal_uInt16)nTemp;
231 bHasWidth = sal_True;
233 else
235 // missformed
236 return sal_False;
240 // if there is no style or a different style than none but no width,
241 // then the declaration is not valid.
242 if( !bHasStyle || (API_LINE_NONE != nStyle && !bHasWidth) )
243 return sal_False;
245 table::BorderLine2 aBorderLine;
246 if(!(rValue >>= aBorderLine))
248 aBorderLine.Color = 0;
249 aBorderLine.InnerLineWidth = 0;
250 aBorderLine.OuterLineWidth = 0;
251 aBorderLine.LineDistance = 0;
252 aBorderLine.LineWidth = 0;
255 // first of all, delete an empty line
256 if( (bHasStyle && API_LINE_NONE == nStyle) ||
257 (bHasWidth && USHRT_MAX == nNamedWidth && 0 == nWidth) )
259 aBorderLine.InnerLineWidth = 0;
260 aBorderLine.OuterLineWidth = 0;
261 aBorderLine.LineDistance = 0;
262 aBorderLine.LineWidth = 0;
264 else if( bHasWidth )
266 if( USHRT_MAX != nNamedWidth )
268 aBorderLine.LineWidth = aBorderWidths[nNamedWidth];
270 else
272 aBorderLine.LineWidth = nWidth;
273 lcl_frmitems_setXMLBorderStyle( aBorderLine, nStyle );
276 else
278 aBorderLine.LineWidth = 0;
279 lcl_frmitems_setXMLBorderStyle( aBorderLine, nStyle );
282 // set color
283 if( bHasColor )
285 aBorderLine.Color = nColor;
288 rValue <<= aBorderLine;
289 return sal_True;
292 sal_Bool XMLBorderHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& /* rUnitConverter */ ) const
294 OUStringBuffer aOut;
296 table::BorderLine2 aBorderLine;
297 if(!(rValue >>= aBorderLine))
298 return sal_False;
300 sal_Int32 nWidth = aBorderLine.LineWidth;
302 if( nWidth == 0 )
304 aOut.append( GetXMLToken( XML_NONE ) );
306 else
308 ::sax::Converter::convertMeasure( aOut, nWidth,
309 util::MeasureUnit::MM_100TH, util::MeasureUnit::POINT);
311 aOut.append( sal_Unicode( ' ' ) );
313 XMLTokenEnum eStyleToken = XML_SOLID;
314 switch ( aBorderLine.LineStyle )
316 case API_LINE_DASHED:
317 eStyleToken = XML_DASHED;
318 break;
319 case API_LINE_DOTTED:
320 eStyleToken = XML_DOTTED;
321 break;
322 case API_LINE_DOUBLE:
323 case API_LINE_THINTHICK_SMALLGAP:
324 case API_LINE_THINTHICK_MEDIUMGAP:
325 case API_LINE_THINTHICK_LARGEGAP:
326 case API_LINE_THICKTHIN_SMALLGAP:
327 case API_LINE_THICKTHIN_MEDIUMGAP:
328 case API_LINE_THICKTHIN_LARGEGAP:
329 eStyleToken = XML_DOUBLE;
330 break;
331 case API_LINE_EMBOSSED:
332 eStyleToken = XML_RIDGE;
333 break;
334 case API_LINE_ENGRAVED:
335 eStyleToken = XML_GROOVE;
336 break;
337 case API_LINE_OUTSET:
338 eStyleToken = XML_OUTSET;
339 break;
340 case API_LINE_INSET:
341 eStyleToken = XML_INSET;
342 break;
343 case API_LINE_SOLID:
344 default:
345 break;
347 aOut.append( GetXMLToken( eStyleToken ) );
349 aOut.append( sal_Unicode( ' ' ) );
351 ::sax::Converter::convertColor( aOut, aBorderLine.Color );
354 rStrExpValue = aOut.makeStringAndClear();
356 return sal_True;
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */