1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <bordrhdl.hxx>
21 #include <sax/tools/converter.hxx>
22 #include <xmloff/xmltoken.hxx>
23 #include <xmloff/xmluconv.hxx>
24 #include <rtl/ustrbuf.hxx>
25 #include <com/sun/star/uno/Any.hxx>
26 #include <com/sun/star/table/BorderLine2.hpp>
27 #include <com/sun/star/table/BorderLineStyle.hpp>
29 using namespace ::com::sun::star
;
30 using namespace ::xmloff::token
;
32 #define DEF_LINE_WIDTH_0 1
33 #define DEF_LINE_WIDTH_1 35
34 #define DEF_LINE_WIDTH_2 88
36 #define SVX_XML_BORDER_WIDTH_THIN 0
37 #define SVX_XML_BORDER_WIDTH_MIDDLE 1
38 #define SVX_XML_BORDER_WIDTH_THICK 2
40 SvXMLEnumMapEntry pXML_BorderStyles
[] =
42 { XML_NONE
, table::BorderLineStyle::NONE
},
43 { XML_HIDDEN
, table::BorderLineStyle::NONE
},
44 { XML_SOLID
, table::BorderLineStyle::SOLID
},
45 { XML_DOUBLE
, table::BorderLineStyle::DOUBLE
},
46 { XML_DOUBLE_THIN
, table::BorderLineStyle::DOUBLE_THIN
},
47 { XML_DOTTED
, table::BorderLineStyle::DOTTED
},
48 { XML_DASHED
, table::BorderLineStyle::DASHED
},
49 { XML_GROOVE
, table::BorderLineStyle::ENGRAVED
},
50 { XML_RIDGE
, table::BorderLineStyle::EMBOSSED
},
51 { XML_INSET
, table::BorderLineStyle::INSET
},
52 { XML_OUTSET
, table::BorderLineStyle::OUTSET
},
53 { XML_FINE_DASHED
, table::BorderLineStyle::FINE_DASHED
},
54 { XML_DASH_DOT
, table::BorderLineStyle::DASH_DOT
},
55 { XML_DASH_DOT_DOT
, table::BorderLineStyle::DASH_DOT_DOT
},
56 { XML_TOKEN_INVALID
, 0 }
59 SvXMLEnumMapEntry pXML_NamedBorderWidths
[] =
61 { XML_THIN
, SVX_XML_BORDER_WIDTH_THIN
},
62 { XML_MIDDLE
, SVX_XML_BORDER_WIDTH_MIDDLE
},
63 { XML_THICK
, SVX_XML_BORDER_WIDTH_THICK
},
64 { XML_TOKEN_INVALID
, 0 }
66 // mapping tables to map external xml input to internal box line widths
68 static sal_uInt16
const aBorderWidths
[] =
75 static void lcl_frmitems_setXMLBorderStyle( table::BorderLine2
& rBorderLine
, sal_uInt16 nStyle
)
77 sal_Int16 eStyle
= -1; // None
78 if (nStyle
!= table::BorderLineStyle::NONE
)
79 eStyle
= sal_Int16( nStyle
);
81 rBorderLine
.LineStyle
= eStyle
;
85 // class XMLEscapementPropHdl
88 XMLBorderWidthHdl::~XMLBorderWidthHdl()
93 bool XMLBorderWidthHdl::importXML( const OUString
& rStrImpValue
, uno::Any
& rValue
, const SvXMLUnitConverter
& rUnitConverter
) const
95 SvXMLTokenEnumerator
aTokenEnum( rStrImpValue
);
97 sal_Int32 nInWidth
, nDistance
, nOutWidth
;
100 if( !aTokenEnum
.getNextToken( aToken
) )
103 if (!rUnitConverter
.convertMeasureToCore( nInWidth
, aToken
, 0, 500 ))
106 if( !aTokenEnum
.getNextToken( aToken
) )
109 if (!rUnitConverter
.convertMeasureToCore( nDistance
, aToken
, 0, 500 ))
112 if( !aTokenEnum
.getNextToken( aToken
) )
115 if (!rUnitConverter
.convertMeasureToCore( nOutWidth
, aToken
, 0, 500 ))
118 table::BorderLine2 aBorderLine
;
119 if(!(rValue
>>= aBorderLine
))
120 aBorderLine
.Color
= 0;
122 aBorderLine
.InnerLineWidth
= sal::static_int_cast
< sal_Int16
>(nInWidth
);
123 aBorderLine
.OuterLineWidth
= sal::static_int_cast
< sal_Int16
>(nOutWidth
);
124 aBorderLine
.LineDistance
= sal::static_int_cast
< sal_Int16
>(nDistance
);
126 rValue
<<= aBorderLine
;
130 bool XMLBorderWidthHdl::exportXML( OUString
& rStrExpValue
, const uno::Any
& rValue
, const SvXMLUnitConverter
& rUnitConverter
) const
134 table::BorderLine2 aBorderLine
;
135 if(!(rValue
>>= aBorderLine
))
138 bool bDouble
= false;
139 switch ( aBorderLine
.LineStyle
)
141 case table::BorderLineStyle::DOUBLE
:
142 case table::BorderLineStyle::DOUBLE_THIN
:
143 case table::BorderLineStyle::THINTHICK_SMALLGAP
:
144 case table::BorderLineStyle::THINTHICK_MEDIUMGAP
:
145 case table::BorderLineStyle::THINTHICK_LARGEGAP
:
146 case table::BorderLineStyle::THICKTHIN_SMALLGAP
:
147 case table::BorderLineStyle::THICKTHIN_MEDIUMGAP
:
148 case table::BorderLineStyle::THICKTHIN_LARGEGAP
:
155 if( ( aBorderLine
.LineDistance
== 0 && aBorderLine
.InnerLineWidth
== 0 ) || !bDouble
)
158 rUnitConverter
.convertMeasureToXML( aOut
, aBorderLine
.InnerLineWidth
);
160 rUnitConverter
.convertMeasureToXML( aOut
, aBorderLine
.LineDistance
);
162 rUnitConverter
.convertMeasureToXML( aOut
, aBorderLine
.OuterLineWidth
);
164 rStrExpValue
= aOut
.makeStringAndClear();
169 // class XMLEscapementHeightPropHdl
172 XMLBorderHdl::~XMLBorderHdl()
177 bool XMLBorderHdl::importXML( const OUString
& rStrImpValue
, uno::Any
& rValue
, const SvXMLUnitConverter
& rUnitConverter
) const
180 SvXMLTokenEnumerator
aTokens( rStrImpValue
);
182 bool bHasStyle
= false;
183 bool bHasWidth
= false;
184 bool bHasColor
= false;
186 sal_uInt16 nStyle
= USHRT_MAX
;
187 sal_uInt16 nWidth
= 0;
188 sal_uInt16 nNamedWidth
= USHRT_MAX
;
189 sal_Int32 nColor
= 0;
192 while( aTokens
.getNextToken( aToken
) && !aToken
.isEmpty() )
195 SvXMLUnitConverter::convertEnum( nNamedWidth
, aToken
,
196 pXML_NamedBorderWidths
) )
200 else if( !bHasStyle
&&
201 SvXMLUnitConverter::convertEnum( nStyle
, aToken
,
202 pXML_BorderStyles
) )
206 else if (!bHasColor
&& ::sax::Converter::convertColor(nColor
, aToken
))
210 else if( !bHasWidth
&&
211 rUnitConverter
.convertMeasureToCore( nTemp
, aToken
, 0,
214 nWidth
= (sal_uInt16
)nTemp
;
224 // if there is no style or a different style than none but no width,
225 // then the declaration is not valid.
226 if (!bHasStyle
|| (table::BorderLineStyle::NONE
!= nStyle
&& !bHasWidth
))
229 table::BorderLine2 aBorderLine
;
230 if(!(rValue
>>= aBorderLine
))
232 aBorderLine
.Color
= 0;
233 aBorderLine
.InnerLineWidth
= 0;
234 aBorderLine
.OuterLineWidth
= 0;
235 aBorderLine
.LineDistance
= 0;
236 aBorderLine
.LineWidth
= 0;
239 // first of all, delete an empty line
240 if (table::BorderLineStyle::NONE
== nStyle
||
241 (bHasWidth
&& USHRT_MAX
== nNamedWidth
&& 0 == nWidth
) )
243 aBorderLine
.InnerLineWidth
= 0;
244 aBorderLine
.OuterLineWidth
= 0;
245 aBorderLine
.LineDistance
= 0;
246 aBorderLine
.LineWidth
= 0;
250 if( USHRT_MAX
!= nNamedWidth
)
252 aBorderLine
.LineWidth
= aBorderWidths
[nNamedWidth
];
256 aBorderLine
.LineWidth
= nWidth
;
257 lcl_frmitems_setXMLBorderStyle( aBorderLine
, nStyle
);
264 aBorderLine
.Color
= nColor
;
267 rValue
<<= aBorderLine
;
271 bool XMLBorderHdl::exportXML( OUString
& rStrExpValue
, const uno::Any
& rValue
, const SvXMLUnitConverter
& /* rUnitConverter */ ) const
275 table::BorderLine2 aBorderLine
;
276 if(!(rValue
>>= aBorderLine
))
279 sal_Int32 nWidth
= aBorderLine
.LineWidth
;
283 aOut
.append( GetXMLToken( XML_NONE
) );
287 ::sax::Converter::convertMeasure( aOut
, nWidth
,
288 util::MeasureUnit::MM_100TH
, util::MeasureUnit::POINT
);
292 XMLTokenEnum eStyleToken
= XML_SOLID
;
293 switch ( aBorderLine
.LineStyle
)
295 case table::BorderLineStyle::DASHED
:
296 eStyleToken
= XML_DASHED
;
298 case table::BorderLineStyle::DOTTED
:
299 eStyleToken
= XML_DOTTED
;
301 case table::BorderLineStyle::DOUBLE
:
302 case table::BorderLineStyle::THINTHICK_SMALLGAP
:
303 case table::BorderLineStyle::THINTHICK_MEDIUMGAP
:
304 case table::BorderLineStyle::THINTHICK_LARGEGAP
:
305 case table::BorderLineStyle::THICKTHIN_SMALLGAP
:
306 case table::BorderLineStyle::THICKTHIN_MEDIUMGAP
:
307 case table::BorderLineStyle::THICKTHIN_LARGEGAP
:
308 eStyleToken
= XML_DOUBLE
;
310 case table::BorderLineStyle::EMBOSSED
:
311 eStyleToken
= XML_RIDGE
;
313 case table::BorderLineStyle::ENGRAVED
:
314 eStyleToken
= XML_GROOVE
;
316 case table::BorderLineStyle::OUTSET
:
317 eStyleToken
= XML_OUTSET
;
319 case table::BorderLineStyle::INSET
:
320 eStyleToken
= XML_INSET
;
322 case table::BorderLineStyle::FINE_DASHED
:
323 eStyleToken
= XML_FINE_DASHED
;
325 case table::BorderLineStyle::DASH_DOT
:
326 eStyleToken
= XML_DASH_DOT
;
328 case table::BorderLineStyle::DASH_DOT_DOT
:
329 eStyleToken
= XML_DASH_DOT_DOT
;
331 case table::BorderLineStyle::DOUBLE_THIN
:
332 eStyleToken
= XML_DOUBLE_THIN
;
334 case table::BorderLineStyle::SOLID
:
338 aOut
.append( GetXMLToken( eStyleToken
) );
342 ::sax::Converter::convertColor( aOut
, aBorderLine
.Color
);
345 rStrExpValue
= aOut
.makeStringAndClear();
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */