nss: upgrade to release 3.73
[LibreOffice.git] / xmloff / source / text / XMLTextHeaderFooterContext.cxx
blob1bb70bdd081ee54972484a6ec3e61ce7798c7476
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/XText.hpp>
21 #include <com/sun/star/text/XParagraphAppend.hpp>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <o3tl/any.hxx>
24 #include <XMLTextHeaderFooterContext.hxx>
25 #include <xmloff/xmlimp.hxx>
28 using namespace ::com::sun::star;
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::xml::sax;
31 using namespace ::com::sun::star::text;
32 using namespace ::com::sun::star::beans;
35 XMLTextHeaderFooterContext::XMLTextHeaderFooterContext( SvXMLImport& rImport,
36 const Reference < XPropertySet > & rPageStylePropSet,
37 bool bFooter, bool bLft, bool bFrst ) :
38 SvXMLImportContext( rImport ),
39 xPropSet( rPageStylePropSet ),
40 sOn( bFooter ? OUString("FooterIsOn") : OUString("HeaderIsOn") ),
41 sShareContent( bFooter ? OUString("FooterIsShared") : OUString("HeaderIsShared") ),
42 sText( bFooter ? OUString("FooterText") : OUString("HeaderText") ),
43 sTextFirst(bFooter ? OUString("FooterTextFirst") : OUString("HeaderTextFirst")),
44 sTextLeft( bFooter ? OUString("FooterTextLeft") : OUString("HeaderTextLeft") ),
45 bInsertContent( true ),
46 bLeft( bLft ),
47 bFirst( bFrst )
49 // NOTE: if this ever handles XML_DISPLAY attr then beware of fdo#72850 !
50 if( !(bLeft || bFirst) )
51 return;
53 Any aAny = xPropSet->getPropertyValue( sOn );
54 bool bOn = *o3tl::doAccess<bool>(aAny);
56 if( bOn )
58 if (bLeft)
60 aAny = xPropSet->getPropertyValue( sShareContent );
61 bool bShared = bool();
62 if (!(aAny >>= bShared))
63 assert(false); // should return a value!
64 if( bShared )
66 // Don't share headers any longer
67 xPropSet->setPropertyValue( sShareContent, Any(false) );
70 if (bFirst)
72 const OUString sShareContentFirst( "FirstIsShared" );
73 aAny = xPropSet->getPropertyValue( sShareContentFirst );
74 bool bSharedFirst = bool();
75 if (!(aAny >>= bSharedFirst))
76 assert(false); // should return a value!
77 if( bSharedFirst )
79 // Don't share first/right headers any longer
80 xPropSet->setPropertyValue( sShareContentFirst, Any(false) );
84 else
86 // If headers or footers are switched off, no content must be
87 // inserted.
88 bInsertContent = false;
92 XMLTextHeaderFooterContext::~XMLTextHeaderFooterContext()
96 SvXMLImportContextRef XMLTextHeaderFooterContext::CreateChildContext(
97 sal_uInt16 nPrefix,
98 const OUString& rLocalName,
99 const uno::Reference< xml::sax::XAttributeList > & xAttrList )
101 SvXMLImportContext *pContext = nullptr;
102 if( bInsertContent )
104 if( !xOldTextCursor.is() )
106 bool bRemoveContent = true;
107 Any aAny;
108 if( bLeft || bFirst )
110 // Headers and footers are switched on already,
111 // and they aren't shared.
112 if (bLeft)
113 aAny = xPropSet->getPropertyValue( sTextLeft );
114 else
115 aAny = xPropSet->getPropertyValue( sTextFirst );
117 else
119 aAny = xPropSet->getPropertyValue( sOn );
120 bool bOn = *o3tl::doAccess<bool>(aAny);
122 if( !bOn )
124 // Switch header on
125 xPropSet->setPropertyValue( sOn, Any(true) );
127 // The content has not to be removed, because the header
128 // or footer is empty already.
129 bRemoveContent = false;
132 // If a header or footer is not shared, share it now.
133 aAny = xPropSet->getPropertyValue( sShareContent );
134 bool bShared = *o3tl::doAccess<bool>(aAny);
135 if( !bShared )
137 xPropSet->setPropertyValue( sShareContent, Any(true) );
140 aAny = xPropSet->getPropertyValue( sText );
143 Reference < XText > xText;
144 aAny >>= xText;
146 if( bRemoveContent )
148 xText->setString(OUString());
149 // fdo#82165 shapes anchored at the beginning or end survive
150 // setString("") - kill them the hard way: SwDoc::DelFullPara()
151 uno::Reference<text::XParagraphAppend> const xAppend(
152 xText, uno::UNO_QUERY_THROW);
153 uno::Reference<lang::XComponent> const xPara(
154 xAppend->finishParagraph(
155 uno::Sequence<beans::PropertyValue>()),
156 uno::UNO_QUERY_THROW);
157 xPara->dispose();
160 rtl::Reference < XMLTextImportHelper > xTxtImport =
161 GetImport().GetTextImport();
163 xOldTextCursor = xTxtImport->GetCursor();
164 xTxtImport->SetCursor( xText->createTextCursor() );
167 pContext =
168 GetImport().GetTextImport()->CreateTextChildContext(
169 GetImport(), nPrefix, rLocalName, xAttrList,
170 XMLTextType::HeaderFooter );
173 return pContext;
176 void XMLTextHeaderFooterContext::endFastElement(sal_Int32 )
178 if( xOldTextCursor.is() )
180 GetImport().GetTextImport()->DeleteParagraph();
181 GetImport().GetTextImport()->SetCursor( xOldTextCursor );
183 else if( !bLeft )
185 // If no content has been inserted into the header or footer,
186 // switch it off.
187 xPropSet->setPropertyValue( sOn, Any(false) );
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */