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
? u
"FooterIsOn"_ustr
: u
"HeaderIsOn"_ustr
),
41 sShareContent( bFooter
? u
"FooterIsShared"_ustr
: u
"HeaderIsShared"_ustr
),
42 sText( bFooter
? u
"FooterText"_ustr
: u
"HeaderText"_ustr
),
43 sTextFirst(bFooter
? u
"FooterTextFirst"_ustr
: u
"HeaderTextFirst"_ustr
),
44 sTextLeft( bFooter
? u
"FooterTextLeft"_ustr
: u
"HeaderTextLeft"_ustr
),
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 static constexpr OUString
sShareContentFirst( u
"FirstIsShared"_ustr
);
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 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLTextHeaderFooterContext::createFastChildContext(
98 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
100 SvXMLImportContext
*pContext
= nullptr;
103 if( !xOldTextCursor
.is() )
105 bool bRemoveContent
= true;
107 if( bLeft
|| bFirst
)
109 // Headers and footers are switched on already,
110 // and they aren't shared.
112 aAny
= xPropSet
->getPropertyValue( sTextLeft
);
114 aAny
= xPropSet
->getPropertyValue( sTextFirst
);
118 aAny
= xPropSet
->getPropertyValue( sOn
);
119 bool bOn
= *o3tl::doAccess
<bool>(aAny
);
124 xPropSet
->setPropertyValue( sOn
, Any(true) );
126 // The content has not to be removed, because the header
127 // or footer is empty already.
128 bRemoveContent
= false;
131 // If a header or footer is not shared, share it now.
132 aAny
= xPropSet
->getPropertyValue( sShareContent
);
133 bool bShared
= *o3tl::doAccess
<bool>(aAny
);
136 xPropSet
->setPropertyValue( sShareContent
, Any(true) );
139 aAny
= xPropSet
->getPropertyValue( sText
);
142 Reference
< XText
> xText
;
147 xText
->setString(OUString());
148 // fdo#82165 shapes anchored at the beginning or end survive
149 // setString("") - kill them the hard way: SwDoc::DelFullPara()
150 uno::Reference
<text::XParagraphAppend
> const xAppend(
151 xText
, uno::UNO_QUERY_THROW
);
152 uno::Reference
<lang::XComponent
> const xPara(
153 xAppend
->finishParagraph(
154 uno::Sequence
<beans::PropertyValue
>()),
155 uno::UNO_QUERY_THROW
);
159 rtl::Reference
< XMLTextImportHelper
> xTxtImport
=
160 GetImport().GetTextImport();
162 xOldTextCursor
= xTxtImport
->GetCursor();
163 xTxtImport
->SetCursor( xText
->createTextCursor() );
167 GetImport().GetTextImport()->CreateTextChildContext(
168 GetImport(), nElement
, xAttrList
,
169 XMLTextType::HeaderFooter
);
175 void XMLTextHeaderFooterContext::endFastElement(sal_Int32
)
177 if( xOldTextCursor
.is() )
179 GetImport().GetTextImport()->DeleteParagraph();
180 GetImport().GetTextImport()->SetCursor( xOldTextCursor
);
184 // If no content has been inserted into the header or footer,
186 xPropSet
->setPropertyValue( sOn
, Any(false) );
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */