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/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*/,
42 xml::sax::XFastAttributeList
> & xAttrList
,
43 const Reference
< XPropertySet
> & rPageStylePropSet
,
44 bool bFooter
, bool bLeft
, bool bFirst
) :
45 SvXMLImportContext( rImport
),
46 xPropSet( rPageStylePropSet
),
48 bContainsRight(false),
49 bContainsCenter(false)
51 OUString
sOn( bFooter
? SC_UNO_PAGE_FTRON
: SC_UNO_PAGE_HDRON
);
52 OUString
sShareFirstContent( bFooter
? SC_UNO_PAGE_FIRSTFTRSHARED
: SC_UNO_PAGE_FIRSTHDRSHARED
);
53 bool bDisplay( true );
54 for( auto &aIter
: sax_fastparser::castToFastAttributeList( xAttrList
) )
56 if( aIter
.getToken() == XML_ELEMENT(STYLE
, XML_DISPLAY
) )
57 bDisplay
= IsXMLToken(aIter
, XML_TRUE
);
59 XMLOFF_WARN_UNKNOWN("sc", aIter
);
61 bool bOn(::cppu::any2bool(xPropSet
->getPropertyValue( sOn
)));
64 OUString
sShareContent( bFooter
? SC_UNO_PAGE_FTRSHARED
: SC_UNO_PAGE_HDRSHARED
);
65 const OUString sShare
= bLeft
? sShareContent
: sShareFirstContent
;
68 if( ::cppu::any2bool(xPropSet
->getPropertyValue( sShare
)) )
69 // Don't share headers any longer
70 xPropSet
->setPropertyValue( sShare
, uno::Any(false) );
74 if( !::cppu::any2bool(xPropSet
->getPropertyValue( sShare
)) )
76 xPropSet
->setPropertyValue( sShare
, uno::Any(true) );
81 if ( bOn
!= bDisplay
)
82 xPropSet
->setPropertyValue( sOn
, uno::Any(bDisplay
) );
86 sCont
= bFooter
? SC_UNO_PAGE_LEFTFTRCONT
: SC_UNO_PAGE_LEFTHDRCONT
;
90 sCont
= bFooter
? SC_UNO_PAGE_FIRSTFTRCONT
: SC_UNO_PAGE_FIRSTHDRCONT
;
91 xPropSet
->setPropertyValue( sShareFirstContent
, uno::Any(!bDisplay
) );
95 sCont
= bFooter
? SC_UNO_PAGE_RIGHTFTRCON
: SC_UNO_PAGE_RIGHTHDRCON
;
97 xPropSet
->getPropertyValue( sCont
) >>= xHeaderFooterContent
;
100 XMLTableHeaderFooterContext::~XMLTableHeaderFooterContext()
104 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLTableHeaderFooterContext::createFastChildContext(
106 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
108 if (xHeaderFooterContent
.is())
110 uno::Reference
< text::XText
> xText
;
113 case XML_ELEMENT(STYLE
, XML_REGION_LEFT
):
114 xText
.set(xHeaderFooterContent
->getLeftText());
115 bContainsLeft
= true;
117 case XML_ELEMENT(STYLE
, XML_REGION_CENTER
):
118 xText
.set(xHeaderFooterContent
->getCenterText());
119 bContainsCenter
= true;
121 case XML_ELEMENT(STYLE
, XML_REGION_RIGHT
):
122 xText
.set(xHeaderFooterContent
->getRightText());
123 bContainsRight
= true;
129 xText
->setString(u
""_ustr
);
130 uno::Reference
< text::XTextCursor
> xTempTextCursor(xText
->createTextCursor());
131 return new XMLHeaderFooterRegionContext( GetImport(), xTempTextCursor
);
135 if ( nElement
== XML_ELEMENT(TEXT
, XML_P
) )
137 if (!xTextCursor
.is())
139 if( xHeaderFooterContent
.is() )
141 uno::Reference
< text::XText
> xText(xHeaderFooterContent
->getCenterText());
142 xText
->setString(u
""_ustr
);
143 xTextCursor
.set(xText
->createTextCursor());
144 xOldTextCursor
.set(GetImport().GetTextImport()->GetCursor());
145 GetImport().GetTextImport()->SetCursor( xTextCursor
);
146 bContainsCenter
= true;
150 GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
155 XMLOFF_WARN_UNKNOWN_ELEMENT("sc", nElement
);
159 void XMLTableHeaderFooterContext::endFastElement(sal_Int32
)
161 if( GetImport().GetTextImport()->GetCursor().is() )
163 //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
164 if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
166 GetImport().GetTextImport()->GetText()->insertString(
167 GetImport().GetTextImport()->GetCursorAsRange(), u
""_ustr
,
170 GetImport().GetTextImport()->ResetCursor();
172 if (xOldTextCursor
.is())
173 GetImport().GetTextImport()->SetCursor(xOldTextCursor
);
174 if (xHeaderFooterContent
.is())
177 xHeaderFooterContent
->getLeftText()->setString(u
""_ustr
);
178 if (!bContainsCenter
)
179 xHeaderFooterContent
->getCenterText()->setString(u
""_ustr
);
181 xHeaderFooterContent
->getRightText()->setString(u
""_ustr
);
183 xPropSet
->setPropertyValue( sCont
, uno::Any(xHeaderFooterContent
) );
188 XMLHeaderFooterRegionContext::XMLHeaderFooterRegionContext( SvXMLImport
& rImport
,
189 uno::Reference
< text::XTextCursor
>& xCursor
) :
190 SvXMLImportContext( rImport
),
191 xTextCursor ( xCursor
)
193 xOldTextCursor
.set(GetImport().GetTextImport()->GetCursor());
194 GetImport().GetTextImport()->SetCursor( xTextCursor
);
197 XMLHeaderFooterRegionContext::~XMLHeaderFooterRegionContext()
201 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLHeaderFooterRegionContext::createFastChildContext(
203 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
205 SvXMLImportContext
*pContext(nullptr);
207 if (nElement
== XML_ELEMENT(TEXT
, XML_P
))
209 return GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
213 XMLOFF_WARN_UNKNOWN_ELEMENT("sc", nElement
);
217 void XMLHeaderFooterRegionContext::endFastElement(sal_Int32
)
219 if( GetImport().GetTextImport()->GetCursor().is() )
221 //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
222 if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
224 GetImport().GetTextImport()->GetText()->insertString(
225 GetImport().GetTextImport()->GetCursorAsRange(), u
""_ustr
,
228 GetImport().GetTextImport()->ResetCursor();
230 if (xOldTextCursor
.is())
231 GetImport().GetTextImport()->SetCursor(xOldTextCursor
);
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */