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"
35 using namespace ::com::sun::star
;
36 using namespace ::com::sun::star::uno
;
37 using namespace ::com::sun::star::lang
;
38 using namespace ::com::sun::star::text
;
39 using namespace ::com::sun::star::style
;
40 using namespace ::com::sun::star::beans
;
41 using namespace ::xmloff::token
;
43 enum SvXMLTokenMapAttrs
46 XML_TOK_COLUMN_MARGIN_LEFT
,
47 XML_TOK_COLUMN_MARGIN_RIGHT
,
48 XML_TOK_COLUMN_END
=XML_TOK_UNKNOWN
51 enum SvXMLSepTokenMapAttrs
53 XML_TOK_COLUMN_SEP_WIDTH
,
54 XML_TOK_COLUMN_SEP_HEIGHT
,
55 XML_TOK_COLUMN_SEP_COLOR
,
56 XML_TOK_COLUMN_SEP_ALIGN
,
57 XML_TOK_COLUMN_SEP_STYLE
,
58 XML_TOK_COLUMN_SEP_END
=XML_TOK_UNKNOWN
61 static SvXMLTokenMapEntry aColAttrTokenMap
[] =
63 { XML_NAMESPACE_STYLE
, XML_REL_WIDTH
, XML_TOK_COLUMN_WIDTH
},
64 { XML_NAMESPACE_FO
, XML_START_INDENT
, XML_TOK_COLUMN_MARGIN_LEFT
},
65 { XML_NAMESPACE_FO
, XML_END_INDENT
, XML_TOK_COLUMN_MARGIN_RIGHT
},
69 static SvXMLTokenMapEntry aColSepAttrTokenMap
[] =
71 { XML_NAMESPACE_STYLE
, XML_WIDTH
, XML_TOK_COLUMN_SEP_WIDTH
},
72 { XML_NAMESPACE_STYLE
, XML_COLOR
, XML_TOK_COLUMN_SEP_COLOR
},
73 { XML_NAMESPACE_STYLE
, XML_HEIGHT
, XML_TOK_COLUMN_SEP_HEIGHT
},
74 { XML_NAMESPACE_STYLE
, XML_VERTICAL_ALIGN
, XML_TOK_COLUMN_SEP_ALIGN
},
75 { XML_NAMESPACE_STYLE
, XML_STYLE
, XML_TOK_COLUMN_SEP_STYLE
},
79 SvXMLEnumMapEntry
const pXML_Sep_Style_Enum
[] =
85 { XML_TOKEN_INVALID
, 0 }
88 SvXMLEnumMapEntry
const pXML_Sep_Align_Enum
[] =
90 { XML_TOP
, VerticalAlignment_TOP
},
91 { XML_MIDDLE
, VerticalAlignment_MIDDLE
},
92 { XML_BOTTOM
, VerticalAlignment_BOTTOM
},
93 { XML_TOKEN_INVALID
, 0 }
96 class XMLTextColumnContext_Impl
: public SvXMLImportContext
98 text::TextColumn aColumn
;
103 XMLTextColumnContext_Impl( SvXMLImport
& rImport
, sal_uInt16 nPrfx
,
104 const OUString
& rLName
,
105 const uno::Reference
<
106 xml::sax::XAttributeList
> & xAttrList
,
107 const SvXMLTokenMap
& rTokenMap
);
109 virtual ~XMLTextColumnContext_Impl();
111 text::TextColumn
& getTextColumn() { return aColumn
; }
114 TYPEINIT1( XMLTextColumnContext_Impl
, SvXMLImportContext
);
116 XMLTextColumnContext_Impl::XMLTextColumnContext_Impl(
117 SvXMLImport
& rImport
, sal_uInt16 nPrfx
,
118 const OUString
& rLName
,
119 const uno::Reference
<
120 xml::sax::XAttributeList
> & xAttrList
,
121 const SvXMLTokenMap
& rTokenMap
) :
122 SvXMLImportContext( rImport
, nPrfx
, rLName
)
125 aColumn
.LeftMargin
= 0;
126 aColumn
.RightMargin
= 0;
128 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
129 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
131 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
134 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName
,
136 const OUString
& rValue
= xAttrList
->getValueByIndex( i
);
139 switch( rTokenMap
.Get( nPrefix
, aLocalName
) )
141 case XML_TOK_COLUMN_WIDTH
:
143 sal_Int32 nPos
= rValue
.indexOf( (sal_Unicode
)'*' );
144 if( nPos
!= -1 && nPos
+1 == rValue
.getLength() )
146 OUString
sTmp( rValue
.copy( 0, nPos
) );
147 if (::sax::Converter::convertNumber(
148 nVal
, sTmp
, 0, USHRT_MAX
))
149 aColumn
.Width
= nVal
;
153 case XML_TOK_COLUMN_MARGIN_LEFT
:
154 if( GetImport().GetMM100UnitConverter().
155 convertMeasureToCore( nVal
, rValue
) )
156 aColumn
.LeftMargin
= nVal
;
158 case XML_TOK_COLUMN_MARGIN_RIGHT
:
160 if( GetImport().GetMM100UnitConverter().
161 convertMeasureToCore( nVal
, rValue
) )
162 aColumn
.RightMargin
= nVal
;
170 XMLTextColumnContext_Impl::~XMLTextColumnContext_Impl()
174 // --------------------------------------------------------------------------
176 class XMLTextColumnSepContext_Impl
: public SvXMLImportContext
182 VerticalAlignment eVertAlign
;
188 XMLTextColumnSepContext_Impl( SvXMLImport
& rImport
, sal_uInt16 nPrfx
,
189 const OUString
& rLName
,
190 const uno::Reference
<
191 xml::sax::XAttributeList
> & xAttrList
,
192 const SvXMLTokenMap
& rTokenMap
);
194 virtual ~XMLTextColumnSepContext_Impl();
196 sal_Int32
GetWidth() const { return nWidth
; }
197 sal_Int32
GetColor() const { return nColor
; }
198 sal_Int8
GetHeight() const { return nHeight
; }
199 sal_Int8
GetStyle() const { return nStyle
; }
200 VerticalAlignment
GetVertAlign() const { return eVertAlign
; }
204 TYPEINIT1( XMLTextColumnSepContext_Impl
, SvXMLImportContext
);
206 XMLTextColumnSepContext_Impl::XMLTextColumnSepContext_Impl(
207 SvXMLImport
& rImport
, sal_uInt16 nPrfx
,
208 const OUString
& rLName
,
209 const uno::Reference
<
210 xml::sax::XAttributeList
> & xAttrList
,
211 const SvXMLTokenMap
& rTokenMap
) :
212 SvXMLImportContext( rImport
, nPrfx
, rLName
),
217 eVertAlign( VerticalAlignment_TOP
)
219 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
220 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
222 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
225 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName
,
227 const OUString
& rValue
= xAttrList
->getValueByIndex( i
);
230 switch( rTokenMap
.Get( nPrefix
, aLocalName
) )
232 case XML_TOK_COLUMN_SEP_WIDTH
:
233 if( GetImport().GetMM100UnitConverter().
234 convertMeasureToCore( nVal
, rValue
) )
237 case XML_TOK_COLUMN_SEP_HEIGHT
:
238 if (::sax::Converter::convertPercent( nVal
, rValue
) &&
239 nVal
>=1 && nVal
<= 100 )
240 nHeight
= (sal_Int8
)nVal
;
242 case XML_TOK_COLUMN_SEP_COLOR
:
244 ::sax::Converter::convertColor( nColor
, rValue
);
247 case XML_TOK_COLUMN_SEP_ALIGN
:
250 if( GetImport().GetMM100UnitConverter().
251 convertEnum( nAlign
, rValue
,
252 pXML_Sep_Align_Enum
) )
253 eVertAlign
= (VerticalAlignment
)nAlign
;
256 case XML_TOK_COLUMN_SEP_STYLE
:
258 sal_uInt16 nStyleVal
;
259 if( GetImport().GetMM100UnitConverter().
260 convertEnum( nStyleVal
, rValue
,
261 pXML_Sep_Style_Enum
) )
262 nStyle
= (sal_Int8
)nStyleVal
;
269 XMLTextColumnSepContext_Impl::~XMLTextColumnSepContext_Impl()
273 // --------------------------------------------------------------------------
275 class XMLTextColumnsArray_Impl
: public std::vector
<XMLTextColumnContext_Impl
*> {};
277 TYPEINIT1( XMLTextColumnsContext
, XMLElementPropertyContext
);
279 XMLTextColumnsContext::XMLTextColumnsContext(
280 SvXMLImport
& rImport
, sal_uInt16 nPrfx
,
281 const OUString
& rLName
,
282 const Reference
< xml::sax::XAttributeList
>&
284 const XMLPropertyState
& rProp
,
285 ::std::vector
< XMLPropertyState
> &rProps
)
286 : XMLElementPropertyContext( rImport
, nPrfx
, rLName
, rProp
, rProps
)
287 , sSeparatorLineIsOn("SeparatorLineIsOn")
288 , sSeparatorLineWidth("SeparatorLineWidth")
289 , sSeparatorLineColor("SeparatorLineColor")
290 , sSeparatorLineRelativeHeight("SeparatorLineRelativeHeight")
291 , sSeparatorLineVerticalAlignment("SeparatorLineVerticalAlignment")
292 , sIsAutomatic("IsAutomatic")
293 , sAutomaticDistance("AutomaticDistance")
294 , sSeparatorLineStyle("SeparatorLineStyle")
297 , pColumnAttrTokenMap( new SvXMLTokenMap(aColAttrTokenMap
) )
298 , pColumnSepAttrTokenMap( new SvXMLTokenMap(aColSepAttrTokenMap
) )
300 , bAutomatic( sal_False
)
301 , nAutomaticDistance( 0 )
303 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
305 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
307 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
310 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName
,
312 const OUString
& rValue
= xAttrList
->getValueByIndex( i
);
313 if( XML_NAMESPACE_FO
== nPrefix
)
315 if( IsXMLToken( aLocalName
, XML_COLUMN_COUNT
) &&
316 ::sax::Converter::convertNumber( nVal
, rValue
, 0, SHRT_MAX
))
318 nCount
= (sal_Int16
)nVal
;
320 else if( IsXMLToken( aLocalName
, XML_COLUMN_GAP
) )
322 bAutomatic
= GetImport().GetMM100UnitConverter().
323 convertMeasureToCore( nAutomaticDistance
, rValue
);
329 XMLTextColumnsContext::~XMLTextColumnsContext()
333 for (XMLTextColumnsArray_Impl::iterator it
= pColumns
->begin();
334 it
!= pColumns
->end(); ++it
)
340 pColumnSep
->ReleaseRef();
343 delete pColumnAttrTokenMap
;
344 delete pColumnSepAttrTokenMap
;
347 SvXMLImportContext
*XMLTextColumnsContext::CreateChildContext(
349 const OUString
& rLocalName
,
350 const uno::Reference
< xml::sax::XAttributeList
> & xAttrList
)
352 SvXMLImportContext
*pContext
= 0;
354 if( XML_NAMESPACE_STYLE
== nPrefix
&&
355 IsXMLToken( rLocalName
, XML_COLUMN
) )
357 XMLTextColumnContext_Impl
*pColumn
=
358 new XMLTextColumnContext_Impl( GetImport(), nPrefix
, rLocalName
,
359 xAttrList
, *pColumnAttrTokenMap
);
361 // add new tabstop to array of tabstops
363 pColumns
= new XMLTextColumnsArray_Impl
;
365 pColumns
->push_back( pColumn
);
370 else if( XML_NAMESPACE_STYLE
== nPrefix
&&
371 IsXMLToken( rLocalName
, XML_COLUMN_SEP
) )
374 new XMLTextColumnSepContext_Impl( GetImport(), nPrefix
, rLocalName
,
375 xAttrList
, *pColumnSepAttrTokenMap
);
376 pColumnSep
->AddRef();
378 pContext
= pColumnSep
;
382 pContext
= new SvXMLImportContext( GetImport(), nPrefix
, rLocalName
);
388 void XMLTextColumnsContext::EndElement( )
390 Reference
<XMultiServiceFactory
> xFactory(GetImport().GetModel(),UNO_QUERY
);
394 Reference
<XInterface
> xIfc
= xFactory
->createInstance(OUString("com.sun.star.text.TextColumns"));
398 Reference
< XTextColumns
> xColumns( xIfc
, UNO_QUERY
);
401 // zero columns = no columns -> 1 column
402 xColumns
->setColumnCount( 1 );
404 else if( !bAutomatic
&& pColumns
&&
405 pColumns
->size() == (sal_uInt16
)nCount
)
407 // if we have column descriptions, one per column, and we don't use
408 // automatic width, then set the column widths
410 sal_Int32 nRelWidth
= 0;
411 sal_uInt16 nColumnsWithWidth
= 0;
414 for( i
= 0; i
< nCount
; i
++ )
416 const TextColumn
& rColumn
=
417 (*pColumns
)[(sal_uInt16
)i
]->getTextColumn();
418 if( rColumn
.Width
> 0 )
420 nRelWidth
+= rColumn
.Width
;
424 if( nColumnsWithWidth
< nCount
)
426 sal_Int32 nColWidth
= 0==nRelWidth
428 : nRelWidth
/ nColumnsWithWidth
;
430 for( i
=0; i
< nCount
; i
++ )
432 TextColumn
& rColumn
=
433 (*pColumns
)[(sal_uInt16
)i
]->getTextColumn();
434 if( rColumn
.Width
== 0 )
436 rColumn
.Width
= nColWidth
;
437 nRelWidth
+= rColumn
.Width
;
438 if( 0 == --nColumnsWithWidth
)
444 Sequence
< TextColumn
> aColumns( (sal_Int32
)nCount
);
445 TextColumn
*pTextColumns
= aColumns
.getArray();
446 for( i
=0; i
< nCount
; i
++ )
447 *pTextColumns
++ = (*pColumns
)[(sal_uInt16
)i
]->getTextColumn();
449 xColumns
->setColumns( aColumns
);
453 // only set column count (and let the columns be distributed
456 xColumns
->setColumnCount( nCount
);
459 Reference
< XPropertySet
> xPropSet( xColumns
, UNO_QUERY
);
463 sal_Bool bOn
= pColumnSep
!= 0;
465 aAny
.setValue( &bOn
, ::getBooleanCppuType() );
466 xPropSet
->setPropertyValue( sSeparatorLineIsOn
, aAny
);
470 if( pColumnSep
->GetWidth() )
472 aAny
<<= pColumnSep
->GetWidth();
473 xPropSet
->setPropertyValue( sSeparatorLineWidth
, aAny
);
475 if( pColumnSep
->GetHeight() )
477 aAny
<<= pColumnSep
->GetHeight();
478 xPropSet
->setPropertyValue( sSeparatorLineRelativeHeight
,
481 if ( pColumnSep
->GetStyle() )
483 aAny
<<= pColumnSep
->GetStyle();
484 xPropSet
->setPropertyValue( sSeparatorLineStyle
, aAny
);
488 aAny
<<= pColumnSep
->GetColor();
489 xPropSet
->setPropertyValue( sSeparatorLineColor
, aAny
);
492 aAny
<<= pColumnSep
->GetVertAlign();
493 xPropSet
->setPropertyValue( sSeparatorLineVerticalAlignment
, aAny
);
496 // handle 'automatic columns': column distance
499 aAny
<<= nAutomaticDistance
;
500 xPropSet
->setPropertyValue( sAutomaticDistance
, aAny
);
504 aProp
.maValue
<<= xColumns
;
506 SetInsert( sal_True
);
507 XMLElementPropertyContext::EndElement();
511 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */