bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / text / XMLTextHeaderFooterContext.cxx
blob673c8fff433a3139b34d49c0c8194ae79217a1aa
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/text/XRelativeTextContentRemove.hpp>
23 #include <xmloff/nmspmap.hxx>
24 #include <xmloff/xmlnmspe.hxx>
25 #include "XMLTextHeaderFooterContext.hxx"
26 #include <xmloff/XMLTextTableContext.hxx>
27 #include <xmloff/xmlimp.hxx>
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::xml::sax;
34 using namespace ::com::sun::star::text;
35 using namespace ::com::sun::star::beans;
38 TYPEINIT1( XMLTextHeaderFooterContext, SvXMLImportContext );
40 XMLTextHeaderFooterContext::XMLTextHeaderFooterContext( SvXMLImport& rImport, sal_uInt16 nPrfx,
41 const OUString& rLName,
42 const uno::Reference<
43 xml::sax::XAttributeList > &,
44 const Reference < XPropertySet > & rPageStylePropSet,
45 bool bFooter, bool bLft, bool bFrst ) :
46 SvXMLImportContext( rImport, nPrfx, rLName ),
47 xPropSet( rPageStylePropSet ),
48 sOn( bFooter ? OUString("FooterIsOn") : OUString("HeaderIsOn") ),
49 sShareContent( bFooter ? OUString("FooterIsShared") : OUString("HeaderIsShared") ),
50 sShareContentFirst( "FirstIsShared" ),
51 sText( bFooter ? OUString("FooterText") : OUString("HeaderText") ),
52 sTextFirst(bFooter ? OUString("FooterTextFirst") : OUString("HeaderTextFirst")),
53 sTextLeft( bFooter ? OUString("FooterTextLeft") : OUString("HeaderTextLeft") ),
54 bInsertContent( true ),
55 bLeft( bLft ),
56 bFirst( bFrst )
58 // NOTE: if this ever handles XML_DISPLAY attr then beware of fdo#72850 !
59 if( bLeft || bFirst )
61 Any aAny;
63 aAny = xPropSet->getPropertyValue( sOn );
64 bool bOn = *static_cast<sal_Bool const *>(aAny.getValue());
66 if( bOn )
68 if (bLeft)
70 aAny = xPropSet->getPropertyValue( sShareContent );
71 bool bShared;
72 if (!(aAny >>= bShared))
73 assert(false); // should return a value!
74 if( bShared )
76 // Don't share headers any longer
77 bShared = false;
78 aAny.setValue( &bShared, cppu::UnoType<bool>::get() );
79 xPropSet->setPropertyValue( sShareContent, aAny );
82 if (bFirst)
84 aAny = xPropSet->getPropertyValue( sShareContentFirst );
85 bool bSharedFirst;
86 if (!(aAny >>= bSharedFirst))
87 assert(false); // should return a value!
88 if( bSharedFirst )
90 // Don't share first/right headers any longer
91 bSharedFirst = false;
92 aAny.setValue( &bSharedFirst, cppu::UnoType<bool>::get() );
93 xPropSet->setPropertyValue( sShareContentFirst, aAny );
97 else
99 // If headers or footers are switched off, no content must be
100 // inserted.
101 bInsertContent = false;
106 XMLTextHeaderFooterContext::~XMLTextHeaderFooterContext()
110 SvXMLImportContext *XMLTextHeaderFooterContext::CreateChildContext(
111 sal_uInt16 nPrefix,
112 const OUString& rLocalName,
113 const uno::Reference< xml::sax::XAttributeList > & xAttrList )
115 SvXMLImportContext *pContext = 0;
116 if( bInsertContent )
118 if( !xOldTextCursor.is() )
120 bool bRemoveContent = true;
121 Any aAny;
122 if( bLeft || bFirst )
124 // Headers and footers are switched on already,
125 // and they aren't shared.
126 if (bLeft)
127 aAny = xPropSet->getPropertyValue( sTextLeft );
128 else
129 aAny = xPropSet->getPropertyValue( sTextFirst );
131 else
133 aAny = xPropSet->getPropertyValue( sOn );
134 sal_Bool bOn = *static_cast<sal_Bool const *>(aAny.getValue());
136 if( !bOn )
138 // Switch header on
139 bOn = sal_True;
140 aAny.setValue( &bOn, cppu::UnoType<bool>::get() );
141 xPropSet->setPropertyValue( sOn, aAny );
143 // The content has not to be removed, because the header
144 // or footer is empty already.
145 bRemoveContent = false;
148 // If a header or footer is not shared, share it now.
149 aAny = xPropSet->getPropertyValue( sShareContent );
150 sal_Bool bShared = *static_cast<sal_Bool const *>(aAny.getValue());
151 if( !bShared )
153 bShared = sal_True;
154 aAny.setValue( &bShared, cppu::UnoType<bool>::get() );
155 xPropSet->setPropertyValue( sShareContent, aAny );
158 aAny = xPropSet->getPropertyValue( sText );
161 Reference < XText > xText;
162 aAny >>= xText;
164 if( bRemoveContent )
166 xText->setString(OUString());
167 // fdo#82165 shapes anchored at the beginning or end survive
168 // setString("") - kill them the hard way: SwDoc::DelFullPara()
169 uno::Reference<text::XParagraphAppend> const xAppend(
170 xText, uno::UNO_QUERY_THROW);
171 uno::Reference<lang::XComponent> const xPara(
172 xAppend->finishParagraph(
173 uno::Sequence<beans::PropertyValue>()),
174 uno::UNO_QUERY_THROW);
175 xPara->dispose();
178 rtl::Reference < XMLTextImportHelper > xTxtImport =
179 GetImport().GetTextImport();
181 xOldTextCursor = xTxtImport->GetCursor();
182 xTxtImport->SetCursor( xText->createTextCursor() );
185 pContext =
186 GetImport().GetTextImport()->CreateTextChildContext(
187 GetImport(), nPrefix, rLocalName, xAttrList,
188 XML_TEXT_TYPE_HEADER_FOOTER );
190 if( !pContext )
191 pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
193 return pContext;
196 void XMLTextHeaderFooterContext::EndElement()
198 if( xOldTextCursor.is() )
200 GetImport().GetTextImport()->DeleteParagraph();
201 GetImport().GetTextImport()->SetCursor( xOldTextCursor );
203 else if( !bLeft )
205 // If no content has been inserted inro the header or footer,
206 // switch it off.
207 sal_Bool bOn = sal_False;
208 Any aAny;
209 aAny.setValue( &bOn, cppu::UnoType<bool>::get() );
210 xPropSet->setPropertyValue( sOn, aAny );
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */