Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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"
33 :
34 :
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;
42 :
43 : enum SvXMLTokenMapAttrs
44 : {
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
49 : };
50 :
51 : enum SvXMLSepTokenMapAttrs
52 : {
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
59 : };
60 :
61 : static SvXMLTokenMapEntry aColAttrTokenMap[] =
62 : {
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
67 : };
68 :
69 : static SvXMLTokenMapEntry aColSepAttrTokenMap[] =
70 : {
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
77 : };
78 :
79 : SvXMLEnumMapEntry const pXML_Sep_Style_Enum[] =
80 : {
81 : { XML_NONE, 0 },
82 : { XML_SOLID, 1 },
83 : { XML_DOTTED, 2 },
84 : { XML_DASHED, 3 },
85 : { XML_TOKEN_INVALID, 0 }
86 : };
87 :
88 : SvXMLEnumMapEntry const pXML_Sep_Align_Enum[] =
89 : {
90 : { XML_TOP, VerticalAlignment_TOP },
91 : { XML_MIDDLE, VerticalAlignment_MIDDLE },
92 : { XML_BOTTOM, VerticalAlignment_BOTTOM },
93 : { XML_TOKEN_INVALID, 0 }
94 : };
95 :
96 : class XMLTextColumnContext_Impl: public SvXMLImportContext
97 : {
98 : text::TextColumn aColumn;
99 :
100 : public:
101 : TYPEINFO();
102 :
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 );
108 :
109 : virtual ~XMLTextColumnContext_Impl();
110 :
111 0 : text::TextColumn& getTextColumn() { return aColumn; }
112 : };
113 :
114 0 : TYPEINIT1( XMLTextColumnContext_Impl, SvXMLImportContext );
115 :
116 12 : 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 12 : SvXMLImportContext( rImport, nPrfx, rLName )
123 : {
124 12 : aColumn.Width = 0;
125 12 : aColumn.LeftMargin = 0;
126 12 : aColumn.RightMargin = 0;
127 :
128 12 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
129 48 : for( sal_Int16 i=0; i < nAttrCount; i++ )
130 : {
131 36 : const OUString& rAttrName = xAttrList->getNameByIndex( i );
132 72 : OUString aLocalName;
133 : sal_uInt16 nPrefix =
134 36 : GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
135 36 : &aLocalName );
136 72 : const OUString& rValue = xAttrList->getValueByIndex( i );
137 :
138 : sal_Int32 nVal;
139 36 : switch( rTokenMap.Get( nPrefix, aLocalName ) )
140 : {
141 : case XML_TOK_COLUMN_WIDTH:
142 : {
143 12 : sal_Int32 nPos = rValue.indexOf( (sal_Unicode)'*' );
144 12 : if( nPos != -1 && nPos+1 == rValue.getLength() )
145 : {
146 12 : OUString sTmp( rValue.copy( 0, nPos ) );
147 12 : if (::sax::Converter::convertNumber(
148 : nVal, sTmp, 0, USHRT_MAX))
149 12 : aColumn.Width = nVal;
150 : }
151 : }
152 12 : break;
153 : case XML_TOK_COLUMN_MARGIN_LEFT:
154 12 : if( GetImport().GetMM100UnitConverter().
155 : convertMeasureToCore( nVal, rValue ) )
156 12 : aColumn.LeftMargin = nVal;
157 12 : break;
158 : case XML_TOK_COLUMN_MARGIN_RIGHT:
159 :
160 12 : if( GetImport().GetMM100UnitConverter().
161 : convertMeasureToCore( nVal, rValue ) )
162 12 : aColumn.RightMargin = nVal;
163 12 : break;
164 : default:
165 0 : break;
166 : }
167 36 : }
168 12 : }
169 :
170 24 : XMLTextColumnContext_Impl::~XMLTextColumnContext_Impl()
171 : {
172 24 : }
173 :
174 : // --------------------------------------------------------------------------
175 :
176 : class XMLTextColumnSepContext_Impl: public SvXMLImportContext
177 : {
178 : sal_Int32 nWidth;
179 : sal_Int32 nColor;
180 : sal_Int8 nHeight;
181 : sal_Int8 nStyle;
182 : VerticalAlignment eVertAlign;
183 :
184 :
185 : public:
186 : TYPEINFO();
187 :
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 );
193 :
194 : virtual ~XMLTextColumnSepContext_Impl();
195 :
196 0 : sal_Int32 GetWidth() const { return nWidth; }
197 0 : sal_Int32 GetColor() const { return nColor; }
198 0 : sal_Int8 GetHeight() const { return nHeight; }
199 0 : sal_Int8 GetStyle() const { return nStyle; }
200 0 : VerticalAlignment GetVertAlign() const { return eVertAlign; }
201 : };
202 :
203 :
204 0 : TYPEINIT1( XMLTextColumnSepContext_Impl, SvXMLImportContext );
205 :
206 0 : 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 0 : eVertAlign( VerticalAlignment_TOP )
218 : {
219 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
220 0 : for( sal_Int16 i=0; i < nAttrCount; i++ )
221 : {
222 0 : const OUString& rAttrName = xAttrList->getNameByIndex( i );
223 0 : OUString aLocalName;
224 : sal_uInt16 nPrefix =
225 0 : GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
226 0 : &aLocalName );
227 0 : const OUString& rValue = xAttrList->getValueByIndex( i );
228 :
229 : sal_Int32 nVal;
230 0 : switch( rTokenMap.Get( nPrefix, aLocalName ) )
231 : {
232 : case XML_TOK_COLUMN_SEP_WIDTH:
233 0 : if( GetImport().GetMM100UnitConverter().
234 : convertMeasureToCore( nVal, rValue ) )
235 0 : nWidth = nVal;
236 0 : break;
237 : case XML_TOK_COLUMN_SEP_HEIGHT:
238 0 : if (::sax::Converter::convertPercent( nVal, rValue ) &&
239 0 : nVal >=1 && nVal <= 100 )
240 0 : nHeight = (sal_Int8)nVal;
241 0 : break;
242 : case XML_TOK_COLUMN_SEP_COLOR:
243 : {
244 0 : ::sax::Converter::convertColor( nColor, rValue );
245 : }
246 0 : break;
247 : case XML_TOK_COLUMN_SEP_ALIGN:
248 : {
249 : sal_uInt16 nAlign;
250 0 : if( GetImport().GetMM100UnitConverter().
251 : convertEnum( nAlign, rValue,
252 0 : pXML_Sep_Align_Enum ) )
253 0 : eVertAlign = (VerticalAlignment)nAlign;
254 : }
255 0 : break;
256 : case XML_TOK_COLUMN_SEP_STYLE:
257 : {
258 : sal_uInt16 nStyleVal;
259 0 : if( GetImport().GetMM100UnitConverter().
260 : convertEnum( nStyleVal, rValue,
261 0 : pXML_Sep_Style_Enum ) )
262 0 : nStyle = (sal_Int8)nStyleVal;
263 : }
264 0 : break;
265 : }
266 0 : }
267 0 : }
268 :
269 0 : XMLTextColumnSepContext_Impl::~XMLTextColumnSepContext_Impl()
270 : {
271 0 : }
272 :
273 : // --------------------------------------------------------------------------
274 :
275 8 : class XMLTextColumnsArray_Impl : public std::vector<XMLTextColumnContext_Impl *> {};
276 :
277 0 : TYPEINIT1( XMLTextColumnsContext, XMLElementPropertyContext );
278 :
279 12 : 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 12 : , pColumnAttrTokenMap( new SvXMLTokenMap(aColAttrTokenMap) )
298 12 : , pColumnSepAttrTokenMap( new SvXMLTokenMap(aColSepAttrTokenMap) )
299 : , nCount( 0 )
300 : , bAutomatic( sal_False )
301 36 : , nAutomaticDistance( 0 )
302 : {
303 12 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
304 : sal_Int32 nVal;
305 36 : for( sal_Int16 i=0; i < nAttrCount; i++ )
306 : {
307 24 : const OUString& rAttrName = xAttrList->getNameByIndex( i );
308 48 : OUString aLocalName;
309 : sal_uInt16 nPrefix =
310 24 : GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
311 24 : &aLocalName );
312 48 : const OUString& rValue = xAttrList->getValueByIndex( i );
313 24 : if( XML_NAMESPACE_FO == nPrefix )
314 : {
315 36 : if( IsXMLToken( aLocalName, XML_COLUMN_COUNT ) &&
316 12 : ::sax::Converter::convertNumber( nVal, rValue, 0, SHRT_MAX ))
317 : {
318 12 : nCount = (sal_Int16)nVal;
319 : }
320 12 : else if( IsXMLToken( aLocalName, XML_COLUMN_GAP ) )
321 : {
322 12 : bAutomatic = GetImport().GetMM100UnitConverter().
323 24 : convertMeasureToCore( nAutomaticDistance, rValue );
324 : }
325 : }
326 24 : }
327 12 : }
328 :
329 36 : XMLTextColumnsContext::~XMLTextColumnsContext()
330 : {
331 12 : if( pColumns )
332 : {
333 48 : for (XMLTextColumnsArray_Impl::iterator it = pColumns->begin();
334 32 : it != pColumns->end(); ++it)
335 : {
336 12 : (*it)->ReleaseRef();
337 : }
338 : }
339 12 : if( pColumnSep )
340 0 : pColumnSep->ReleaseRef();
341 :
342 12 : delete pColumns;
343 12 : delete pColumnAttrTokenMap;
344 12 : delete pColumnSepAttrTokenMap;
345 24 : }
346 :
347 12 : SvXMLImportContext *XMLTextColumnsContext::CreateChildContext(
348 : sal_uInt16 nPrefix,
349 : const OUString& rLocalName,
350 : const uno::Reference< xml::sax::XAttributeList > & xAttrList )
351 : {
352 12 : SvXMLImportContext *pContext = 0;
353 :
354 24 : if( XML_NAMESPACE_STYLE == nPrefix &&
355 12 : IsXMLToken( rLocalName, XML_COLUMN ) )
356 : {
357 : XMLTextColumnContext_Impl *pColumn =
358 12 : new XMLTextColumnContext_Impl( GetImport(), nPrefix, rLocalName,
359 12 : xAttrList, *pColumnAttrTokenMap );
360 :
361 : // add new tabstop to array of tabstops
362 12 : if( !pColumns )
363 4 : pColumns = new XMLTextColumnsArray_Impl;
364 :
365 12 : pColumns->push_back( pColumn );
366 12 : pColumn->AddRef();
367 :
368 12 : pContext = pColumn;
369 : }
370 0 : else if( XML_NAMESPACE_STYLE == nPrefix &&
371 0 : IsXMLToken( rLocalName, XML_COLUMN_SEP ) )
372 : {
373 : pColumnSep =
374 0 : new XMLTextColumnSepContext_Impl( GetImport(), nPrefix, rLocalName,
375 0 : xAttrList, *pColumnSepAttrTokenMap );
376 0 : pColumnSep->AddRef();
377 :
378 0 : pContext = pColumnSep;
379 : }
380 : else
381 : {
382 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
383 : }
384 :
385 12 : return pContext;
386 : }
387 :
388 12 : void XMLTextColumnsContext::EndElement( )
389 : {
390 12 : Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
391 12 : if( !xFactory.is() )
392 0 : return;
393 :
394 24 : Reference<XInterface> xIfc = xFactory->createInstance("com.sun.star.text.TextColumns");
395 12 : if( !xIfc.is() )
396 0 : return;
397 :
398 24 : Reference< XTextColumns > xColumns( xIfc, UNO_QUERY );
399 12 : if ( 0 == nCount )
400 : {
401 : // zero columns = no columns -> 1 column
402 0 : xColumns->setColumnCount( 1 );
403 : }
404 12 : else if( !bAutomatic && pColumns &&
405 0 : pColumns->size() == (sal_uInt16)nCount )
406 : {
407 : // if we have column descriptions, one per column, and we don't use
408 : // automatic width, then set the column widths
409 :
410 0 : sal_Int32 nRelWidth = 0;
411 0 : sal_uInt16 nColumnsWithWidth = 0;
412 : sal_Int16 i;
413 :
414 0 : for( i = 0; i < nCount; i++ )
415 : {
416 : const TextColumn& rColumn =
417 0 : (*pColumns)[(sal_uInt16)i]->getTextColumn();
418 0 : if( rColumn.Width > 0 )
419 : {
420 0 : nRelWidth += rColumn.Width;
421 0 : nColumnsWithWidth++;
422 : }
423 : }
424 0 : if( nColumnsWithWidth < nCount )
425 : {
426 : sal_Int32 nColWidth = 0==nRelWidth
427 : ? USHRT_MAX / nCount
428 0 : : nRelWidth / nColumnsWithWidth;
429 :
430 0 : for( i=0; i < nCount; i++ )
431 : {
432 : TextColumn& rColumn =
433 0 : (*pColumns)[(sal_uInt16)i]->getTextColumn();
434 0 : if( rColumn.Width == 0 )
435 : {
436 0 : rColumn.Width = nColWidth;
437 0 : nRelWidth += rColumn.Width;
438 0 : if( 0 == --nColumnsWithWidth )
439 0 : break;
440 : }
441 : }
442 : }
443 :
444 0 : Sequence< TextColumn > aColumns( (sal_Int32)nCount );
445 0 : TextColumn *pTextColumns = aColumns.getArray();
446 0 : for( i=0; i < nCount; i++ )
447 0 : *pTextColumns++ = (*pColumns)[(sal_uInt16)i]->getTextColumn();
448 :
449 0 : xColumns->setColumns( aColumns );
450 : }
451 : else
452 : {
453 : // only set column count (and let the columns be distributed
454 : // automatically)
455 :
456 12 : xColumns->setColumnCount( nCount );
457 : }
458 :
459 24 : Reference < XPropertySet > xPropSet( xColumns, UNO_QUERY );
460 12 : if( xPropSet.is() )
461 : {
462 12 : Any aAny;
463 12 : sal_Bool bOn = pColumnSep != 0;
464 :
465 12 : aAny.setValue( &bOn, ::getBooleanCppuType() );
466 12 : xPropSet->setPropertyValue( sSeparatorLineIsOn, aAny );
467 :
468 12 : if( pColumnSep )
469 : {
470 0 : if( pColumnSep->GetWidth() )
471 : {
472 0 : aAny <<= pColumnSep->GetWidth();
473 0 : xPropSet->setPropertyValue( sSeparatorLineWidth, aAny );
474 : }
475 0 : if( pColumnSep->GetHeight() )
476 : {
477 0 : aAny <<= pColumnSep->GetHeight();
478 0 : xPropSet->setPropertyValue( sSeparatorLineRelativeHeight,
479 0 : aAny );
480 : }
481 0 : if ( pColumnSep->GetStyle() )
482 : {
483 0 : aAny <<= pColumnSep->GetStyle();
484 0 : xPropSet->setPropertyValue( sSeparatorLineStyle, aAny );
485 : }
486 :
487 :
488 0 : aAny <<= pColumnSep->GetColor();
489 0 : xPropSet->setPropertyValue( sSeparatorLineColor, aAny );
490 :
491 :
492 0 : aAny <<= pColumnSep->GetVertAlign();
493 0 : xPropSet->setPropertyValue( sSeparatorLineVerticalAlignment, aAny );
494 : }
495 :
496 : // handle 'automatic columns': column distance
497 12 : if( bAutomatic )
498 : {
499 12 : aAny <<= nAutomaticDistance;
500 12 : xPropSet->setPropertyValue( sAutomaticDistance, aAny );
501 12 : }
502 : }
503 :
504 12 : aProp.maValue <<= xColumns;
505 :
506 12 : SetInsert( sal_True );
507 24 : XMLElementPropertyContext::EndElement();
508 :
509 : }
510 :
511 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|