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 <com/sun/star/text/XTextColumns.hpp>
21 #include <com/sun/star/text/TextColumn.hpp>
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/style/VerticalAlignment.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <sax/tools/converter.hxx>
26 #include <xmloff/xmltkmap.hxx>
27 #include <xmloff/xmluconv.hxx>
28 #include <xmloff/nmspmap.hxx>
29 #include <xmloff/xmlnmspe.hxx>
30 #include <xmloff/xmlimp.hxx>
31 #include <xmloff/xmltoken.hxx>
32 #include "XMLTextColumnsContext.hxx"
34 using namespace ::com::sun::star
;
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::text
;
38 using namespace ::com::sun::star::style
;
39 using namespace ::com::sun::star::beans
;
40 using namespace ::xmloff::token
;
42 enum SvXMLTokenMapAttrs
45 XML_TOK_COLUMN_MARGIN_LEFT
,
46 XML_TOK_COLUMN_MARGIN_RIGHT
,
47 XML_TOK_COLUMN_END
=XML_TOK_UNKNOWN
50 enum SvXMLSepTokenMapAttrs
52 XML_TOK_COLUMN_SEP_WIDTH
,
53 XML_TOK_COLUMN_SEP_HEIGHT
,
54 XML_TOK_COLUMN_SEP_COLOR
,
55 XML_TOK_COLUMN_SEP_ALIGN
,
56 XML_TOK_COLUMN_SEP_STYLE
,
57 XML_TOK_COLUMN_SEP_END
=XML_TOK_UNKNOWN
60 static const SvXMLTokenMapEntry aColAttrTokenMap
[] =
62 { XML_NAMESPACE_STYLE
, XML_REL_WIDTH
, XML_TOK_COLUMN_WIDTH
},
63 { XML_NAMESPACE_FO
, XML_START_INDENT
, XML_TOK_COLUMN_MARGIN_LEFT
},
64 { XML_NAMESPACE_FO
, XML_END_INDENT
, XML_TOK_COLUMN_MARGIN_RIGHT
},
68 static const SvXMLTokenMapEntry aColSepAttrTokenMap
[] =
70 { XML_NAMESPACE_STYLE
, XML_WIDTH
, XML_TOK_COLUMN_SEP_WIDTH
},
71 { XML_NAMESPACE_STYLE
, XML_COLOR
, XML_TOK_COLUMN_SEP_COLOR
},
72 { XML_NAMESPACE_STYLE
, XML_HEIGHT
, XML_TOK_COLUMN_SEP_HEIGHT
},
73 { XML_NAMESPACE_STYLE
, XML_VERTICAL_ALIGN
, XML_TOK_COLUMN_SEP_ALIGN
},
74 { XML_NAMESPACE_STYLE
, XML_STYLE
, XML_TOK_COLUMN_SEP_STYLE
},
78 static SvXMLEnumMapEntry
const pXML_Sep_Style_Enum
[] =
84 { XML_TOKEN_INVALID
, 0 }
87 static SvXMLEnumMapEntry
const pXML_Sep_Align_Enum
[] =
89 { XML_TOP
, VerticalAlignment_TOP
},
90 { XML_MIDDLE
, VerticalAlignment_MIDDLE
},
91 { XML_BOTTOM
, VerticalAlignment_BOTTOM
},
92 { XML_TOKEN_INVALID
, 0 }
95 class XMLTextColumnContext_Impl
: public SvXMLImportContext
97 text::TextColumn aColumn
;
102 XMLTextColumnContext_Impl( SvXMLImport
& rImport
, sal_uInt16 nPrfx
,
103 const OUString
& rLName
,
104 const uno::Reference
<
105 xml::sax::XAttributeList
> & xAttrList
,
106 const SvXMLTokenMap
& rTokenMap
);
108 virtual ~XMLTextColumnContext_Impl();
110 text::TextColumn
& getTextColumn() { return aColumn
; }
113 TYPEINIT1( XMLTextColumnContext_Impl
, SvXMLImportContext
);
115 XMLTextColumnContext_Impl::XMLTextColumnContext_Impl(
116 SvXMLImport
& rImport
, sal_uInt16 nPrfx
,
117 const OUString
& rLName
,
118 const uno::Reference
<
119 xml::sax::XAttributeList
> & xAttrList
,
120 const SvXMLTokenMap
& rTokenMap
) :
121 SvXMLImportContext( rImport
, nPrfx
, rLName
)
124 aColumn
.LeftMargin
= 0;
125 aColumn
.RightMargin
= 0;
127 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
128 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
130 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
133 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName
,
135 const OUString
& rValue
= xAttrList
->getValueByIndex( i
);
138 switch( rTokenMap
.Get( nPrefix
, aLocalName
) )
140 case XML_TOK_COLUMN_WIDTH
:
142 sal_Int32 nPos
= rValue
.indexOf( (sal_Unicode
)'*' );
143 if( nPos
!= -1 && nPos
+1 == rValue
.getLength() )
145 OUString
sTmp( rValue
.copy( 0, nPos
) );
146 if (::sax::Converter::convertNumber(
147 nVal
, sTmp
, 0, USHRT_MAX
))
148 aColumn
.Width
= nVal
;
152 case XML_TOK_COLUMN_MARGIN_LEFT
:
153 if( GetImport().GetMM100UnitConverter().
154 convertMeasureToCore( nVal
, rValue
) )
155 aColumn
.LeftMargin
= nVal
;
157 case XML_TOK_COLUMN_MARGIN_RIGHT
:
159 if( GetImport().GetMM100UnitConverter().
160 convertMeasureToCore( nVal
, rValue
) )
161 aColumn
.RightMargin
= nVal
;
169 XMLTextColumnContext_Impl::~XMLTextColumnContext_Impl()
173 class XMLTextColumnSepContext_Impl
: public SvXMLImportContext
179 VerticalAlignment eVertAlign
;
184 XMLTextColumnSepContext_Impl( SvXMLImport
& rImport
, sal_uInt16 nPrfx
,
185 const OUString
& rLName
,
186 const uno::Reference
<
187 xml::sax::XAttributeList
> & xAttrList
,
188 const SvXMLTokenMap
& rTokenMap
);
190 virtual ~XMLTextColumnSepContext_Impl();
192 sal_Int32
GetWidth() const { return nWidth
; }
193 sal_Int32
GetColor() const { return nColor
; }
194 sal_Int8
GetHeight() const { return nHeight
; }
195 sal_Int8
GetStyle() const { return nStyle
; }
196 VerticalAlignment
GetVertAlign() const { return eVertAlign
; }
199 TYPEINIT1( XMLTextColumnSepContext_Impl
, SvXMLImportContext
);
201 XMLTextColumnSepContext_Impl::XMLTextColumnSepContext_Impl(
202 SvXMLImport
& rImport
, sal_uInt16 nPrfx
,
203 const OUString
& rLName
,
204 const uno::Reference
<
205 xml::sax::XAttributeList
> & xAttrList
,
206 const SvXMLTokenMap
& rTokenMap
) :
207 SvXMLImportContext( rImport
, nPrfx
, rLName
),
212 eVertAlign( VerticalAlignment_TOP
)
214 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
215 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
217 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
220 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName
,
222 const OUString
& rValue
= xAttrList
->getValueByIndex( i
);
225 switch( rTokenMap
.Get( nPrefix
, aLocalName
) )
227 case XML_TOK_COLUMN_SEP_WIDTH
:
228 if( GetImport().GetMM100UnitConverter().
229 convertMeasureToCore( nVal
, rValue
) )
232 case XML_TOK_COLUMN_SEP_HEIGHT
:
233 if (::sax::Converter::convertPercent( nVal
, rValue
) &&
234 nVal
>=1 && nVal
<= 100 )
235 nHeight
= (sal_Int8
)nVal
;
237 case XML_TOK_COLUMN_SEP_COLOR
:
239 ::sax::Converter::convertColor( nColor
, rValue
);
242 case XML_TOK_COLUMN_SEP_ALIGN
:
245 if( SvXMLUnitConverter::convertEnum( nAlign
, rValue
,
246 pXML_Sep_Align_Enum
) )
247 eVertAlign
= (VerticalAlignment
)nAlign
;
250 case XML_TOK_COLUMN_SEP_STYLE
:
252 sal_uInt16 nStyleVal
;
253 if( SvXMLUnitConverter::convertEnum( nStyleVal
, rValue
,
254 pXML_Sep_Style_Enum
) )
255 nStyle
= (sal_Int8
)nStyleVal
;
262 XMLTextColumnSepContext_Impl::~XMLTextColumnSepContext_Impl()
266 TYPEINIT1( XMLTextColumnsContext
, XMLElementPropertyContext
);
268 XMLTextColumnsContext::XMLTextColumnsContext(
269 SvXMLImport
& rImport
, sal_uInt16 nPrfx
,
270 const OUString
& rLName
,
271 const Reference
< xml::sax::XAttributeList
>&
273 const XMLPropertyState
& rProp
,
274 ::std::vector
< XMLPropertyState
> &rProps
)
275 : XMLElementPropertyContext( rImport
, nPrfx
, rLName
, rProp
, rProps
)
276 , sSeparatorLineIsOn("SeparatorLineIsOn")
277 , sSeparatorLineWidth("SeparatorLineWidth")
278 , sSeparatorLineColor("SeparatorLineColor")
279 , sSeparatorLineRelativeHeight("SeparatorLineRelativeHeight")
280 , sSeparatorLineVerticalAlignment("SeparatorLineVerticalAlignment")
281 , sAutomaticDistance("AutomaticDistance")
282 , sSeparatorLineStyle("SeparatorLineStyle")
285 , pColumnAttrTokenMap( new SvXMLTokenMap(aColAttrTokenMap
) )
286 , pColumnSepAttrTokenMap( new SvXMLTokenMap(aColSepAttrTokenMap
) )
288 , bAutomatic( false )
289 , nAutomaticDistance( 0 )
291 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
293 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
295 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
298 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName
,
300 const OUString
& rValue
= xAttrList
->getValueByIndex( i
);
301 if( XML_NAMESPACE_FO
== nPrefix
)
303 if( IsXMLToken( aLocalName
, XML_COLUMN_COUNT
) &&
304 ::sax::Converter::convertNumber( nVal
, rValue
, 0, SHRT_MAX
))
306 nCount
= (sal_Int16
)nVal
;
308 else if( IsXMLToken( aLocalName
, XML_COLUMN_GAP
) )
310 bAutomatic
= GetImport().GetMM100UnitConverter().
311 convertMeasureToCore( nAutomaticDistance
, rValue
);
317 XMLTextColumnsContext::~XMLTextColumnsContext()
321 for (XMLTextColumnsArray_Impl::iterator it
= pColumns
->begin();
322 it
!= pColumns
->end(); ++it
)
328 pColumnSep
->ReleaseRef();
331 delete pColumnAttrTokenMap
;
332 delete pColumnSepAttrTokenMap
;
335 SvXMLImportContext
*XMLTextColumnsContext::CreateChildContext(
337 const OUString
& rLocalName
,
338 const uno::Reference
< xml::sax::XAttributeList
> & xAttrList
)
340 SvXMLImportContext
*pContext
= 0;
342 if( XML_NAMESPACE_STYLE
== nPrefix
&&
343 IsXMLToken( rLocalName
, XML_COLUMN
) )
345 XMLTextColumnContext_Impl
*pColumn
=
346 new XMLTextColumnContext_Impl( GetImport(), nPrefix
, rLocalName
,
347 xAttrList
, *pColumnAttrTokenMap
);
349 // add new tabstop to array of tabstops
351 pColumns
= new XMLTextColumnsArray_Impl
;
353 pColumns
->push_back( pColumn
);
354 pColumn
->AddFirstRef();
358 else if( XML_NAMESPACE_STYLE
== nPrefix
&&
359 IsXMLToken( rLocalName
, XML_COLUMN_SEP
) )
362 new XMLTextColumnSepContext_Impl( GetImport(), nPrefix
, rLocalName
,
363 xAttrList
, *pColumnSepAttrTokenMap
);
364 pColumnSep
->AddFirstRef();
366 pContext
= pColumnSep
;
370 pContext
= new SvXMLImportContext( GetImport(), nPrefix
, rLocalName
);
376 void XMLTextColumnsContext::EndElement( )
378 Reference
<XMultiServiceFactory
> xFactory(GetImport().GetModel(),UNO_QUERY
);
382 Reference
<XInterface
> xIfc
= xFactory
->createInstance("com.sun.star.text.TextColumns");
386 Reference
< XTextColumns
> xColumns( xIfc
, UNO_QUERY
);
389 // zero columns = no columns -> 1 column
390 xColumns
->setColumnCount( 1 );
392 else if( !bAutomatic
&& pColumns
&&
393 pColumns
->size() == (sal_uInt16
)nCount
)
395 // if we have column descriptions, one per column, and we don't use
396 // automatic width, then set the column widths
398 sal_Int32 nRelWidth
= 0;
399 sal_uInt16 nColumnsWithWidth
= 0;
402 for( i
= 0; i
< nCount
; i
++ )
404 const TextColumn
& rColumn
=
405 (*pColumns
)[(sal_uInt16
)i
]->getTextColumn();
406 if( rColumn
.Width
> 0 )
408 nRelWidth
+= rColumn
.Width
;
412 if( nColumnsWithWidth
< nCount
)
414 sal_Int32 nColWidth
= 0==nRelWidth
416 : nRelWidth
/ nColumnsWithWidth
;
418 for( i
=0; i
< nCount
; i
++ )
420 TextColumn
& rColumn
=
421 (*pColumns
)[(sal_uInt16
)i
]->getTextColumn();
422 if( rColumn
.Width
== 0 )
424 rColumn
.Width
= nColWidth
;
425 nRelWidth
+= rColumn
.Width
;
426 if( 0 == --nColumnsWithWidth
)
432 Sequence
< TextColumn
> aColumns( (sal_Int32
)nCount
);
433 TextColumn
*pTextColumns
= aColumns
.getArray();
434 for( i
=0; i
< nCount
; i
++ )
435 *pTextColumns
++ = (*pColumns
)[(sal_uInt16
)i
]->getTextColumn();
437 xColumns
->setColumns( aColumns
);
441 // only set column count (and let the columns be distributed
444 xColumns
->setColumnCount( nCount
);
447 Reference
< XPropertySet
> xPropSet( xColumns
, UNO_QUERY
);
451 sal_Bool bOn
= pColumnSep
!= 0;
453 aAny
.setValue( &bOn
, cppu::UnoType
<bool>::get() );
454 xPropSet
->setPropertyValue( sSeparatorLineIsOn
, aAny
);
458 if( pColumnSep
->GetWidth() )
460 aAny
<<= pColumnSep
->GetWidth();
461 xPropSet
->setPropertyValue( sSeparatorLineWidth
, aAny
);
463 if( pColumnSep
->GetHeight() )
465 aAny
<<= pColumnSep
->GetHeight();
466 xPropSet
->setPropertyValue( sSeparatorLineRelativeHeight
,
469 if ( pColumnSep
->GetStyle() )
471 aAny
<<= pColumnSep
->GetStyle();
472 xPropSet
->setPropertyValue( sSeparatorLineStyle
, aAny
);
475 aAny
<<= pColumnSep
->GetColor();
476 xPropSet
->setPropertyValue( sSeparatorLineColor
, aAny
);
478 aAny
<<= pColumnSep
->GetVertAlign();
479 xPropSet
->setPropertyValue( sSeparatorLineVerticalAlignment
, aAny
);
482 // handle 'automatic columns': column distance
485 aAny
<<= nAutomaticDistance
;
486 xPropSet
->setPropertyValue( sAutomaticDistance
, aAny
);
490 aProp
.maValue
<<= xColumns
;
493 XMLElementPropertyContext::EndElement();
497 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */