1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 ),
49 // NOTE: if this ever handles XML_DISPLAY attr then beware of fdo#72850 !
50 if( !(bLeft
|| bFirst
) )
53 Any aAny
= xPropSet
->getPropertyValue( sOn
);
54 bool bOn
= *o3tl::doAccess
<bool>(aAny
);
60 aAny
= xPropSet
->getPropertyValue( sShareContent
);
61 bool bShared
= bool();
62 if (!(aAny
>>= bShared
))
63 assert(false); // should return a value!
66 // Don't share headers any longer
67 xPropSet
->setPropertyValue( sShareContent
, Any(false) );
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!
79 // Don't share first/right headers any longer
80 xPropSet
->setPropertyValue( sShareContentFirst
, Any(false) );
86 // If headers or footers are switched off, no content must be
88 bInsertContent
= false;
92 XMLTextHeaderFooterContext::~XMLTextHeaderFooterContext()
96 SvXMLImportContextRef
XMLTextHeaderFooterContext::CreateChildContext(
98 const OUString
& rLocalName
,
99 const uno::Reference
< xml::sax::XAttributeList
> & xAttrList
)
101 SvXMLImportContext
*pContext
= nullptr;
104 if( !xOldTextCursor
.is() )
106 bool bRemoveContent
= true;
108 if( bLeft
|| bFirst
)
110 // Headers and footers are switched on already,
111 // and they aren't shared.
113 aAny
= xPropSet
->getPropertyValue( sTextLeft
);
115 aAny
= xPropSet
->getPropertyValue( sTextFirst
);
119 aAny
= xPropSet
->getPropertyValue( sOn
);
120 bool bOn
= *o3tl::doAccess
<bool>(aAny
);
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
);
137 xPropSet
->setPropertyValue( sShareContent
, Any(true) );
140 aAny
= xPropSet
->getPropertyValue( sText
);
143 Reference
< XText
> xText
;
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
);
160 rtl::Reference
< XMLTextImportHelper
> xTxtImport
=
161 GetImport().GetTextImport();
163 xOldTextCursor
= xTxtImport
->GetCursor();
164 xTxtImport
->SetCursor( xText
->createTextCursor() );
168 GetImport().GetTextImport()->CreateTextChildContext(
169 GetImport(), nPrefix
, rLocalName
, xAttrList
,
170 XMLTextType::HeaderFooter
);
176 void XMLTextHeaderFooterContext::endFastElement(sal_Int32
)
178 if( xOldTextCursor
.is() )
180 GetImport().GetTextImport()->DeleteParagraph();
181 GetImport().GetTextImport()->SetCursor( xOldTextCursor
);
185 // If no content has been inserted into the header or footer,
187 xPropSet
->setPropertyValue( sOn
, Any(false) );
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */