fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / xmloff / source / text / XMLTextColumnsContext.cxx
blobf046245f557a976a77c0a5c9a00f19dacb83b0b0
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 .
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
45 XML_TOK_COLUMN_WIDTH,
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 },
66 XML_TOKEN_MAP_END
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 },
76 XML_TOKEN_MAP_END
79 SvXMLEnumMapEntry const pXML_Sep_Style_Enum[] =
81 { XML_NONE, 0 },
82 { XML_SOLID, 1 },
83 { XML_DOTTED, 2 },
84 { XML_DASHED, 3 },
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;
100 public:
101 TYPEINFO();
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 )
124 aColumn.Width = 0;
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 );
132 OUString aLocalName;
133 sal_uInt16 nPrefix =
134 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
135 &aLocalName );
136 const OUString& rValue = xAttrList->getValueByIndex( i );
138 sal_Int32 nVal;
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;
152 break;
153 case XML_TOK_COLUMN_MARGIN_LEFT:
154 if( GetImport().GetMM100UnitConverter().
155 convertMeasureToCore( nVal, rValue ) )
156 aColumn.LeftMargin = nVal;
157 break;
158 case XML_TOK_COLUMN_MARGIN_RIGHT:
160 if( GetImport().GetMM100UnitConverter().
161 convertMeasureToCore( nVal, rValue ) )
162 aColumn.RightMargin = nVal;
163 break;
164 default:
165 break;
170 XMLTextColumnContext_Impl::~XMLTextColumnContext_Impl()
174 // --------------------------------------------------------------------------
176 class XMLTextColumnSepContext_Impl: public SvXMLImportContext
178 sal_Int32 nWidth;
179 sal_Int32 nColor;
180 sal_Int8 nHeight;
181 sal_Int8 nStyle;
182 VerticalAlignment eVertAlign;
185 public:
186 TYPEINFO();
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 ),
213 nWidth( 2 ),
214 nColor( 0 ),
215 nHeight( 100 ),
216 nStyle( 1 ),
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 );
223 OUString aLocalName;
224 sal_uInt16 nPrefix =
225 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
226 &aLocalName );
227 const OUString& rValue = xAttrList->getValueByIndex( i );
229 sal_Int32 nVal;
230 switch( rTokenMap.Get( nPrefix, aLocalName ) )
232 case XML_TOK_COLUMN_SEP_WIDTH:
233 if( GetImport().GetMM100UnitConverter().
234 convertMeasureToCore( nVal, rValue ) )
235 nWidth = nVal;
236 break;
237 case XML_TOK_COLUMN_SEP_HEIGHT:
238 if (::sax::Converter::convertPercent( nVal, rValue ) &&
239 nVal >=1 && nVal <= 100 )
240 nHeight = (sal_Int8)nVal;
241 break;
242 case XML_TOK_COLUMN_SEP_COLOR:
244 ::sax::Converter::convertColor( nColor, rValue );
246 break;
247 case XML_TOK_COLUMN_SEP_ALIGN:
249 sal_uInt16 nAlign;
250 if( GetImport().GetMM100UnitConverter().
251 convertEnum( nAlign, rValue,
252 pXML_Sep_Align_Enum ) )
253 eVertAlign = (VerticalAlignment)nAlign;
255 break;
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;
264 break;
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 >&
283 xAttrList,
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")
295 , pColumns( 0 )
296 , pColumnSep( 0 )
297 , pColumnAttrTokenMap( new SvXMLTokenMap(aColAttrTokenMap) )
298 , pColumnSepAttrTokenMap( new SvXMLTokenMap(aColSepAttrTokenMap) )
299 , nCount( 0 )
300 , bAutomatic( sal_False )
301 , nAutomaticDistance( 0 )
303 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
304 sal_Int32 nVal;
305 for( sal_Int16 i=0; i < nAttrCount; i++ )
307 const OUString& rAttrName = xAttrList->getNameByIndex( i );
308 OUString aLocalName;
309 sal_uInt16 nPrefix =
310 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
311 &aLocalName );
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()
331 if( pColumns )
333 for (XMLTextColumnsArray_Impl::iterator it = pColumns->begin();
334 it != pColumns->end(); ++it)
336 (*it)->ReleaseRef();
339 if( pColumnSep )
340 pColumnSep->ReleaseRef();
342 delete pColumns;
343 delete pColumnAttrTokenMap;
344 delete pColumnSepAttrTokenMap;
347 SvXMLImportContext *XMLTextColumnsContext::CreateChildContext(
348 sal_uInt16 nPrefix,
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
362 if( !pColumns )
363 pColumns = new XMLTextColumnsArray_Impl;
365 pColumns->push_back( pColumn );
366 pColumn->AddRef();
368 pContext = pColumn;
370 else if( XML_NAMESPACE_STYLE == nPrefix &&
371 IsXMLToken( rLocalName, XML_COLUMN_SEP ) )
373 pColumnSep =
374 new XMLTextColumnSepContext_Impl( GetImport(), nPrefix, rLocalName,
375 xAttrList, *pColumnSepAttrTokenMap );
376 pColumnSep->AddRef();
378 pContext = pColumnSep;
380 else
382 pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
385 return pContext;
388 void XMLTextColumnsContext::EndElement( )
390 Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
391 if( !xFactory.is() )
392 return;
394 Reference<XInterface> xIfc = xFactory->createInstance(OUString("com.sun.star.text.TextColumns"));
395 if( !xIfc.is() )
396 return;
398 Reference< XTextColumns > xColumns( xIfc, UNO_QUERY );
399 if ( 0 == nCount )
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;
412 sal_Int16 i;
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;
421 nColumnsWithWidth++;
424 if( nColumnsWithWidth < nCount )
426 sal_Int32 nColWidth = 0==nRelWidth
427 ? USHRT_MAX / nCount
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 )
439 break;
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 );
451 else
453 // only set column count (and let the columns be distributed
454 // automatically)
456 xColumns->setColumnCount( nCount );
459 Reference < XPropertySet > xPropSet( xColumns, UNO_QUERY );
460 if( xPropSet.is() )
462 Any aAny;
463 sal_Bool bOn = pColumnSep != 0;
465 aAny.setValue( &bOn, ::getBooleanCppuType() );
466 xPropSet->setPropertyValue( sSeparatorLineIsOn, aAny );
468 if( pColumnSep )
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,
479 aAny );
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
497 if( bAutomatic )
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: */