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
? OUString(SC_UNO_PAGE_FTRON
) : OUString(SC_UNO_PAGE_HDRON
) );
52 OUString
sContent( bFooter
? OUString(SC_UNO_PAGE_RIGHTFTRCON
) : OUString(SC_UNO_PAGE_RIGHTHDRCON
) );
53 OUString
sContentLeft( bFooter
? OUString(SC_UNO_PAGE_LEFTFTRCONT
) : OUString(SC_UNO_PAGE_LEFTHDRCONT
) );
54 OUString
sContentFirst( bFooter
? OUString(SC_UNO_PAGE_FIRSTFTRCONT
) : OUString(SC_UNO_PAGE_FIRSTHDRCONT
) );
55 OUString
sShareContent( bFooter
? OUString(SC_UNO_PAGE_FTRSHARED
) : OUString(SC_UNO_PAGE_HDRSHARED
) );
56 OUString
sShareFirstContent( bFooter
? OUString(SC_UNO_PAGE_FIRSTFTRSHARED
) : OUString(SC_UNO_PAGE_FIRSTHDRSHARED
) );
57 bool bDisplay( true );
58 for( auto &aIter
: sax_fastparser::castToFastAttributeList( xAttrList
) )
60 if( aIter
.getToken() == XML_ELEMENT(STYLE
, XML_DISPLAY
) )
61 bDisplay
= IsXMLToken(aIter
, XML_TRUE
);
63 XMLOFF_WARN_UNKNOWN("sc", aIter
);
65 bool bOn(::cppu::any2bool(xPropSet
->getPropertyValue( sOn
)));
68 const OUString sShare
= bLeft
? sShareContent
: sShareFirstContent
;
71 if( ::cppu::any2bool(xPropSet
->getPropertyValue( sShare
)) )
72 // Don't share headers any longer
73 xPropSet
->setPropertyValue( sShare
, uno::Any(false) );
77 if( !::cppu::any2bool(xPropSet
->getPropertyValue( sShare
)) )
79 xPropSet
->setPropertyValue( sShare
, uno::Any(true) );
84 if ( bOn
!= bDisplay
)
85 xPropSet
->setPropertyValue( sOn
, uno::Any(bDisplay
) );
93 sCont
= sContentFirst
;
94 xPropSet
->setPropertyValue( sShareFirstContent
, uno::Any(!bDisplay
) );
100 xPropSet
->getPropertyValue( sCont
) >>= xHeaderFooterContent
;
103 XMLTableHeaderFooterContext::~XMLTableHeaderFooterContext()
107 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLTableHeaderFooterContext::createFastChildContext(
109 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
111 if (xHeaderFooterContent
.is())
113 uno::Reference
< text::XText
> xText
;
116 case XML_ELEMENT(STYLE
, XML_REGION_LEFT
):
117 xText
.set(xHeaderFooterContent
->getLeftText());
118 bContainsLeft
= true;
120 case XML_ELEMENT(STYLE
, XML_REGION_CENTER
):
121 xText
.set(xHeaderFooterContent
->getCenterText());
122 bContainsCenter
= true;
124 case XML_ELEMENT(STYLE
, XML_REGION_RIGHT
):
125 xText
.set(xHeaderFooterContent
->getRightText());
126 bContainsRight
= true;
132 xText
->setString("");
133 uno::Reference
< text::XTextCursor
> xTempTextCursor(xText
->createTextCursor());
134 return new XMLHeaderFooterRegionContext( GetImport(), xTempTextCursor
);
138 if ( nElement
== XML_ELEMENT(TEXT
, XML_P
) )
140 if (!xTextCursor
.is())
142 if( xHeaderFooterContent
.is() )
144 uno::Reference
< text::XText
> xText(xHeaderFooterContent
->getCenterText());
145 xText
->setString("");
146 xTextCursor
.set(xText
->createTextCursor());
147 xOldTextCursor
.set(GetImport().GetTextImport()->GetCursor());
148 GetImport().GetTextImport()->SetCursor( xTextCursor
);
149 bContainsCenter
= true;
153 GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
158 XMLOFF_WARN_UNKNOWN_ELEMENT("sc", nElement
);
162 void XMLTableHeaderFooterContext::endFastElement(sal_Int32
)
164 if( GetImport().GetTextImport()->GetCursor().is() )
166 //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
167 if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
169 GetImport().GetTextImport()->GetText()->insertString(
170 GetImport().GetTextImport()->GetCursorAsRange(), "",
173 GetImport().GetTextImport()->ResetCursor();
175 if (xOldTextCursor
.is())
176 GetImport().GetTextImport()->SetCursor(xOldTextCursor
);
177 if (xHeaderFooterContent
.is())
180 xHeaderFooterContent
->getLeftText()->setString("");
181 if (!bContainsCenter
)
182 xHeaderFooterContent
->getCenterText()->setString("");
184 xHeaderFooterContent
->getRightText()->setString("");
186 xPropSet
->setPropertyValue( sCont
, uno::Any(xHeaderFooterContent
) );
191 XMLHeaderFooterRegionContext::XMLHeaderFooterRegionContext( SvXMLImport
& rImport
,
192 uno::Reference
< text::XTextCursor
>& xCursor
) :
193 SvXMLImportContext( rImport
),
194 xTextCursor ( xCursor
)
196 xOldTextCursor
.set(GetImport().GetTextImport()->GetCursor());
197 GetImport().GetTextImport()->SetCursor( xTextCursor
);
200 XMLHeaderFooterRegionContext::~XMLHeaderFooterRegionContext()
204 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLHeaderFooterRegionContext::createFastChildContext(
206 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
208 SvXMLImportContext
*pContext(nullptr);
210 if (nElement
== XML_ELEMENT(TEXT
, XML_P
))
212 return GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
216 XMLOFF_WARN_UNKNOWN_ELEMENT("sc", nElement
);
220 void XMLHeaderFooterRegionContext::endFastElement(sal_Int32
)
222 if( GetImport().GetTextImport()->GetCursor().is() )
224 //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
225 if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
227 GetImport().GetTextImport()->GetText()->insertString(
228 GetImport().GetTextImport()->GetCursorAsRange(), "",
231 GetImport().GetTextImport()->ResetCursor();
233 if (xOldTextCursor
.is())
234 GetImport().GetTextImport()->SetCursor(xOldTextCursor
);
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */