Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / xml / XMLTableHeaderFooterContext.cxx
blobc61ee8f3986da2333504154b28ddfe2c3320b72f
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/beans/XPropertySet.hpp>
21 #include <com/sun/star/text/XText.hpp>
22 #include <com/sun/star/sheet/XHeaderFooterContent.hpp>
23 #include <xmloff/nmspmap.hxx>
24 #include <xmloff/xmlnmspe.hxx>
25 #include <xmloff/xmlimp.hxx>
26 #include "XMLTableHeaderFooterContext.hxx"
27 #include <xmloff/xmltoken.hxx>
28 #include <comphelper/extract.hxx>
30 #include <unonames.hxx>
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::xml::sax;
35 using namespace ::com::sun::star::text;
36 using namespace ::com::sun::star::beans;
37 using namespace xmloff::token;
40 XMLTableHeaderFooterContext::XMLTableHeaderFooterContext( SvXMLImport& rImport, sal_uInt16 nPrfx,
41 const OUString& rLName,
42 const uno::Reference<
43 xml::sax::XAttributeList > & xAttrList,
44 const Reference < XPropertySet > & rPageStylePropSet,
45 bool bFooter, bool bLeft ) :
46 SvXMLImportContext( rImport, nPrfx, rLName ),
47 xPropSet( rPageStylePropSet ),
48 sOn( bFooter ? OUString(SC_UNO_PAGE_FTRON) : OUString(SC_UNO_PAGE_HDRON) ),
49 bContainsLeft(false),
50 bContainsRight(false),
51 bContainsCenter(false)
53 OUString sContent( bFooter ? OUString(SC_UNO_PAGE_RIGHTFTRCON) : OUString(SC_UNO_PAGE_RIGHTHDRCON) );
54 OUString sContentLeft( bFooter ? OUString(SC_UNO_PAGE_LEFTFTRCONT) : OUString(SC_UNO_PAGE_LEFTHDRCONT) );
55 OUString sShareContent( bFooter ? OUString(SC_UNO_PAGE_FTRSHARED) : OUString(SC_UNO_PAGE_HDRSHARED) );
56 bool bDisplay( true );
57 sal_Int16 nAttrCount(xAttrList.is() ? xAttrList->getLength() : 0);
58 for( sal_Int16 i=0; i < nAttrCount; ++i )
60 const OUString& rAttrName(xAttrList->getNameByIndex( i ));
61 OUString aLName;
62 sal_uInt16 nPrefix(GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLName ));
63 const OUString& rValue(xAttrList->getValueByIndex( i ));
65 // TODO: use a map here
66 if( XML_NAMESPACE_STYLE == nPrefix )
68 if( IsXMLToken(aLName, XML_DISPLAY ) )
69 bDisplay = IsXMLToken(rValue, XML_TRUE);
72 if( bLeft )
74 bool bOn(::cppu::any2bool(xPropSet->getPropertyValue( sOn )));
76 if( bOn && bDisplay )
78 if( ::cppu::any2bool(xPropSet->getPropertyValue( sShareContent )) )
79 // Don't share headers any longer
80 xPropSet->setPropertyValue( sShareContent, uno::makeAny(false) );
82 else
84 if( !::cppu::any2bool(xPropSet->getPropertyValue( sShareContent )) )
85 // share headers
86 xPropSet->setPropertyValue( sShareContent, uno::makeAny(true) );
89 else
91 bool bOn(::cppu::any2bool(xPropSet->getPropertyValue( sOn )));
92 if ( bOn != bDisplay )
93 xPropSet->setPropertyValue( sOn, uno::makeAny(bDisplay) );
95 if (bLeft)
96 sCont = sContentLeft;
97 else
98 sCont = sContent;
99 xPropSet->getPropertyValue( sCont ) >>= xHeaderFooterContent;
102 XMLTableHeaderFooterContext::~XMLTableHeaderFooterContext()
106 SvXMLImportContextRef XMLTableHeaderFooterContext::CreateChildContext(
107 sal_uInt16 nPrefix,
108 const OUString& rLocalName,
109 const uno::Reference< xml::sax::XAttributeList > & xAttrList )
111 SvXMLImportContext *pContext(nullptr);
113 if ((nPrefix == XML_NAMESPACE_TEXT) &&
114 IsXMLToken(rLocalName, XML_P))
116 if (!xTextCursor.is())
118 if( xHeaderFooterContent.is() )
120 uno::Reference < text::XText > xText(xHeaderFooterContent->getCenterText());
121 xText->setString("");
122 xTextCursor.set(xText->createTextCursor());
123 xOldTextCursor.set(GetImport().GetTextImport()->GetCursor());
124 GetImport().GetTextImport()->SetCursor( xTextCursor );
125 bContainsCenter = true;
128 pContext =
129 GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
130 nPrefix,
131 rLocalName,
132 xAttrList);
134 else
136 if (nPrefix == XML_NAMESPACE_STYLE)
138 if (xHeaderFooterContent.is())
140 uno::Reference < text::XText > xText;
141 if (IsXMLToken(rLocalName, XML_REGION_LEFT ))
143 xText.set(xHeaderFooterContent->getLeftText());
144 bContainsLeft = true;
146 else if (IsXMLToken(rLocalName, XML_REGION_CENTER ))
148 xText.set(xHeaderFooterContent->getCenterText());
149 bContainsCenter = true;
151 else if (IsXMLToken(rLocalName, XML_REGION_RIGHT ))
153 xText.set(xHeaderFooterContent->getRightText());
154 bContainsRight = true;
156 if (xText.is())
158 xText->setString("");
159 //SvXMLImport aSvXMLImport( GetImport() );
160 uno::Reference < text::XTextCursor > xTempTextCursor(xText->createTextCursor());
161 pContext = new XMLHeaderFooterRegionContext( GetImport(), nPrefix, rLocalName, xTempTextCursor);
166 if( !pContext )
167 pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
169 return pContext;
172 void XMLTableHeaderFooterContext::EndElement()
174 if( GetImport().GetTextImport()->GetCursor().is() )
176 //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
177 if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
179 GetImport().GetTextImport()->GetText()->insertString(
180 GetImport().GetTextImport()->GetCursorAsRange(), "",
181 true );
183 GetImport().GetTextImport()->ResetCursor();
185 if (xOldTextCursor.is())
186 GetImport().GetTextImport()->SetCursor(xOldTextCursor);
187 if (xHeaderFooterContent.is())
189 if (!bContainsLeft)
190 xHeaderFooterContent->getLeftText()->setString("");
191 if (!bContainsCenter)
192 xHeaderFooterContent->getCenterText()->setString("");
193 if (!bContainsRight)
194 xHeaderFooterContent->getRightText()->setString("");
196 xPropSet->setPropertyValue( sCont, uno::makeAny(xHeaderFooterContent) );
201 XMLHeaderFooterRegionContext::XMLHeaderFooterRegionContext( SvXMLImport& rImport, sal_uInt16 nPrfx,
202 const OUString& rLName,
203 uno::Reference< text::XTextCursor >& xCursor ) :
204 SvXMLImportContext( rImport, nPrfx, rLName ),
205 xTextCursor ( xCursor )
207 xOldTextCursor.set(GetImport().GetTextImport()->GetCursor());
208 GetImport().GetTextImport()->SetCursor( xTextCursor );
211 XMLHeaderFooterRegionContext::~XMLHeaderFooterRegionContext()
215 SvXMLImportContextRef XMLHeaderFooterRegionContext::CreateChildContext(
216 sal_uInt16 nPrefix,
217 const OUString& rLocalName,
218 const uno::Reference< xml::sax::XAttributeList > & xAttrList )
220 SvXMLImportContext *pContext(nullptr);
222 if ((nPrefix == XML_NAMESPACE_TEXT) &&
223 IsXMLToken(rLocalName, XML_P))
225 pContext =
226 GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
227 nPrefix,
228 rLocalName,
229 xAttrList);
231 if( !pContext )
232 pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
234 return pContext;
237 void XMLHeaderFooterRegionContext::EndElement()
239 if( GetImport().GetTextImport()->GetCursor().is() )
241 //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
242 if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
244 GetImport().GetTextImport()->GetText()->insertString(
245 GetImport().GetTextImport()->GetCursorAsRange(), "",
246 true );
248 GetImport().GetTextImport()->ResetCursor();
250 if (xOldTextCursor.is())
251 GetImport().GetTextImport()->SetCursor(xOldTextCursor);
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */