Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / source / filter / xml / XMLTableHeaderFooterContext.cxx
blob99987a449193aab80570532c89de691ee4b67824
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/xmlnamespace.hxx>
24 #include <xmloff/xmlimp.hxx>
25 #include "XMLTableHeaderFooterContext.hxx"
26 #include <xmloff/xmltoken.hxx>
27 #include <comphelper/extract.hxx>
28 #include <sal/log.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_Int32 /*nElement*/,
41 const uno::Reference<
42 xml::sax::XFastAttributeList > & xAttrList,
43 const Reference < XPropertySet > & rPageStylePropSet,
44 bool bFooter, bool bLeft, bool bFirst ) :
45 SvXMLImportContext( rImport ),
46 xPropSet( rPageStylePropSet ),
47 bContainsLeft(false),
48 bContainsRight(false),
49 bContainsCenter(false)
51 OUString sOn( bFooter ? OUString(SC_UNO_PAGE_FTRON) : OUString(SC_UNO_PAGE_HDRON) );
52 OUString sContent( bFooter ? OUString(SC_UNO_PAGE_RIGHTFTRCON) : OUString(SC_UNO_PAGE_RIGHTHDRCON) );
53 OUString sContentLeft( bFooter ? OUString(SC_UNO_PAGE_LEFTFTRCONT) : OUString(SC_UNO_PAGE_LEFTHDRCONT) );
54 OUString sContentFirst( bFooter ? OUString(SC_UNO_PAGE_FIRSTFTRCONT) : OUString(SC_UNO_PAGE_FIRSTHDRCONT) );
55 OUString sShareContent( bFooter ? OUString(SC_UNO_PAGE_FTRSHARED) : OUString(SC_UNO_PAGE_HDRSHARED) );
56 OUString sShareFirstContent( bFooter ? OUString(SC_UNO_PAGE_FIRSTFTRSHARED) : OUString(SC_UNO_PAGE_FIRSTHDRSHARED) );
57 bool bDisplay( true );
58 for( auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ) )
60 if( aIter.getToken() == XML_ELEMENT(STYLE, XML_DISPLAY) )
61 bDisplay = IsXMLToken(aIter, XML_TRUE);
62 else
63 XMLOFF_WARN_UNKNOWN("sc", aIter);
65 bool bOn(::cppu::any2bool(xPropSet->getPropertyValue( sOn )));
66 if( bLeft || bFirst )
68 const OUString sShare = bLeft ? sShareContent : sShareFirstContent;
69 if( bOn && bDisplay )
71 if( ::cppu::any2bool(xPropSet->getPropertyValue( sShare )) )
72 // Don't share headers any longer
73 xPropSet->setPropertyValue( sShare, uno::Any(false) );
75 else
77 if( !::cppu::any2bool(xPropSet->getPropertyValue( sShare )) )
78 // share headers
79 xPropSet->setPropertyValue( sShare, uno::Any(true) );
82 else
84 if ( bOn != bDisplay )
85 xPropSet->setPropertyValue( sOn, uno::Any(bDisplay) );
87 if (bLeft)
89 sCont = sContentLeft;
91 else if (bFirst)
93 sCont = sContentFirst;
94 xPropSet->setPropertyValue( sShareFirstContent, uno::Any(!bDisplay) );
96 else
98 sCont = sContent;
100 xPropSet->getPropertyValue( sCont ) >>= xHeaderFooterContent;
103 XMLTableHeaderFooterContext::~XMLTableHeaderFooterContext()
107 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTableHeaderFooterContext::createFastChildContext(
108 sal_Int32 nElement,
109 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
111 if (xHeaderFooterContent.is())
113 uno::Reference < text::XText > xText;
114 switch (nElement)
116 case XML_ELEMENT(STYLE, XML_REGION_LEFT):
117 xText.set(xHeaderFooterContent->getLeftText());
118 bContainsLeft = true;
119 break;
120 case XML_ELEMENT(STYLE, XML_REGION_CENTER):
121 xText.set(xHeaderFooterContent->getCenterText());
122 bContainsCenter = true;
123 break;
124 case XML_ELEMENT(STYLE, XML_REGION_RIGHT):
125 xText.set(xHeaderFooterContent->getRightText());
126 bContainsRight = true;
127 break;
128 default: break;
130 if (xText.is())
132 xText->setString("");
133 uno::Reference < text::XTextCursor > xTempTextCursor(xText->createTextCursor());
134 return new XMLHeaderFooterRegionContext( GetImport(), xTempTextCursor);
138 if ( nElement == XML_ELEMENT(TEXT, XML_P) )
140 if (!xTextCursor.is())
142 if( xHeaderFooterContent.is() )
144 uno::Reference < text::XText > xText(xHeaderFooterContent->getCenterText());
145 xText->setString("");
146 xTextCursor.set(xText->createTextCursor());
147 xOldTextCursor.set(GetImport().GetTextImport()->GetCursor());
148 GetImport().GetTextImport()->SetCursor( xTextCursor );
149 bContainsCenter = true;
152 return
153 GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
154 nElement,
155 xAttrList);
158 XMLOFF_WARN_UNKNOWN_ELEMENT("sc", nElement);
159 return nullptr;
162 void XMLTableHeaderFooterContext::endFastElement(sal_Int32 )
164 if( GetImport().GetTextImport()->GetCursor().is() )
166 //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
167 if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
169 GetImport().GetTextImport()->GetText()->insertString(
170 GetImport().GetTextImport()->GetCursorAsRange(), "",
171 true );
173 GetImport().GetTextImport()->ResetCursor();
175 if (xOldTextCursor.is())
176 GetImport().GetTextImport()->SetCursor(xOldTextCursor);
177 if (xHeaderFooterContent.is())
179 if (!bContainsLeft)
180 xHeaderFooterContent->getLeftText()->setString("");
181 if (!bContainsCenter)
182 xHeaderFooterContent->getCenterText()->setString("");
183 if (!bContainsRight)
184 xHeaderFooterContent->getRightText()->setString("");
186 xPropSet->setPropertyValue( sCont, uno::Any(xHeaderFooterContent) );
191 XMLHeaderFooterRegionContext::XMLHeaderFooterRegionContext( SvXMLImport& rImport,
192 uno::Reference< text::XTextCursor >& xCursor ) :
193 SvXMLImportContext( rImport ),
194 xTextCursor ( xCursor )
196 xOldTextCursor.set(GetImport().GetTextImport()->GetCursor());
197 GetImport().GetTextImport()->SetCursor( xTextCursor );
200 XMLHeaderFooterRegionContext::~XMLHeaderFooterRegionContext()
204 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLHeaderFooterRegionContext::createFastChildContext(
205 sal_Int32 nElement,
206 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
208 SvXMLImportContext *pContext(nullptr);
210 if (nElement == XML_ELEMENT(TEXT, XML_P))
212 return GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
213 nElement,
214 xAttrList);
216 XMLOFF_WARN_UNKNOWN_ELEMENT("sc", nElement);
217 return pContext;
220 void XMLHeaderFooterRegionContext::endFastElement(sal_Int32 )
222 if( GetImport().GetTextImport()->GetCursor().is() )
224 //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
225 if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
227 GetImport().GetTextImport()->GetText()->insertString(
228 GetImport().GetTextImport()->GetCursorAsRange(), "",
229 true );
231 GetImport().GetTextImport()->ResetCursor();
233 if (xOldTextCursor.is())
234 GetImport().GetTextImport()->SetCursor(xOldTextCursor);
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */