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/namespacemap.hxx>
24 #include <xmloff/xmlnamespace.hxx>
25 #include <xmloff/xmlimp.hxx>
26 #include "XMLTableHeaderFooterContext.hxx"
27 #include <xmloff/xmltoken.hxx>
28 #include <comphelper/extract.hxx>
29 #include <sal/log.hxx>
31 #include <unonames.hxx>
33 using namespace ::com::sun::star
;
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::xml::sax
;
36 using namespace ::com::sun::star::text
;
37 using namespace ::com::sun::star::beans
;
38 using namespace xmloff::token
;
41 XMLTableHeaderFooterContext::XMLTableHeaderFooterContext( SvXMLImport
& rImport
, sal_Int32
/*nElement*/,
43 xml::sax::XFastAttributeList
> & xAttrList
,
44 const Reference
< XPropertySet
> & rPageStylePropSet
,
45 bool bFooter
, bool bLeft
) :
46 SvXMLImportContext( rImport
),
47 xPropSet( rPageStylePropSet
),
49 bContainsRight(false),
50 bContainsCenter(false)
52 OUString
sOn( bFooter
? OUString(SC_UNO_PAGE_FTRON
) : OUString(SC_UNO_PAGE_HDRON
) );
53 OUString
sContent( bFooter
? OUString(SC_UNO_PAGE_RIGHTFTRCON
) : OUString(SC_UNO_PAGE_RIGHTHDRCON
) );
54 OUString
sContentLeft( bFooter
? OUString(SC_UNO_PAGE_LEFTFTRCONT
) : OUString(SC_UNO_PAGE_LEFTHDRCONT
) );
55 OUString
sShareContent( bFooter
? OUString(SC_UNO_PAGE_FTRSHARED
) : OUString(SC_UNO_PAGE_HDRSHARED
) );
56 bool bDisplay( true );
57 for( auto &aIter
: sax_fastparser::castToFastAttributeList( xAttrList
) )
59 if( aIter
.getToken() == XML_ELEMENT(STYLE
, XML_DISPLAY
) )
60 bDisplay
= IsXMLToken(aIter
.toString(), XML_TRUE
);
62 XMLOFF_WARN_UNKNOWN("sc", aIter
);
66 bool bOn(::cppu::any2bool(xPropSet
->getPropertyValue( sOn
)));
70 if( ::cppu::any2bool(xPropSet
->getPropertyValue( sShareContent
)) )
71 // Don't share headers any longer
72 xPropSet
->setPropertyValue( sShareContent
, uno::makeAny(false) );
76 if( !::cppu::any2bool(xPropSet
->getPropertyValue( sShareContent
)) )
78 xPropSet
->setPropertyValue( sShareContent
, uno::makeAny(true) );
83 bool bOn(::cppu::any2bool(xPropSet
->getPropertyValue( sOn
)));
84 if ( bOn
!= bDisplay
)
85 xPropSet
->setPropertyValue( sOn
, uno::makeAny(bDisplay
) );
91 xPropSet
->getPropertyValue( sCont
) >>= xHeaderFooterContent
;
94 XMLTableHeaderFooterContext::~XMLTableHeaderFooterContext()
98 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLTableHeaderFooterContext::createFastChildContext(
100 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& /*xAttrList*/ )
102 if (xHeaderFooterContent
.is())
104 uno::Reference
< text::XText
> xText
;
107 case XML_ELEMENT(STYLE
, XML_REGION_LEFT
):
108 xText
.set(xHeaderFooterContent
->getLeftText());
109 bContainsLeft
= true;
111 case XML_ELEMENT(STYLE
, XML_REGION_CENTER
):
112 xText
.set(xHeaderFooterContent
->getCenterText());
113 bContainsCenter
= true;
115 case XML_ELEMENT(STYLE
, XML_REGION_RIGHT
):
116 xText
.set(xHeaderFooterContent
->getRightText());
117 bContainsRight
= true;
123 xText
->setString("");
124 uno::Reference
< text::XTextCursor
> xTempTextCursor(xText
->createTextCursor());
125 return new XMLHeaderFooterRegionContext( GetImport(), xTempTextCursor
);
131 SvXMLImportContextRef
XMLTableHeaderFooterContext::CreateChildContext(
133 const OUString
& rLocalName
,
134 const uno::Reference
< xml::sax::XAttributeList
> & xAttrList
)
136 SvXMLImportContext
*pContext(nullptr);
138 if ((nPrefix
== XML_NAMESPACE_TEXT
) &&
139 IsXMLToken(rLocalName
, XML_P
))
141 if (!xTextCursor
.is())
143 if( xHeaderFooterContent
.is() )
145 uno::Reference
< text::XText
> xText(xHeaderFooterContent
->getCenterText());
146 xText
->setString("");
147 xTextCursor
.set(xText
->createTextCursor());
148 xOldTextCursor
.set(GetImport().GetTextImport()->GetCursor());
149 GetImport().GetTextImport()->SetCursor( xTextCursor
);
150 bContainsCenter
= true;
154 GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
163 void XMLTableHeaderFooterContext::endFastElement(sal_Int32
)
165 if( GetImport().GetTextImport()->GetCursor().is() )
167 //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
168 if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
170 GetImport().GetTextImport()->GetText()->insertString(
171 GetImport().GetTextImport()->GetCursorAsRange(), "",
174 GetImport().GetTextImport()->ResetCursor();
176 if (xOldTextCursor
.is())
177 GetImport().GetTextImport()->SetCursor(xOldTextCursor
);
178 if (xHeaderFooterContent
.is())
181 xHeaderFooterContent
->getLeftText()->setString("");
182 if (!bContainsCenter
)
183 xHeaderFooterContent
->getCenterText()->setString("");
185 xHeaderFooterContent
->getRightText()->setString("");
187 xPropSet
->setPropertyValue( sCont
, uno::makeAny(xHeaderFooterContent
) );
192 XMLHeaderFooterRegionContext::XMLHeaderFooterRegionContext( SvXMLImport
& rImport
,
193 uno::Reference
< text::XTextCursor
>& xCursor
) :
194 SvXMLImportContext( rImport
),
195 xTextCursor ( xCursor
)
197 xOldTextCursor
.set(GetImport().GetTextImport()->GetCursor());
198 GetImport().GetTextImport()->SetCursor( xTextCursor
);
201 XMLHeaderFooterRegionContext::~XMLHeaderFooterRegionContext()
205 SvXMLImportContextRef
XMLHeaderFooterRegionContext::CreateChildContext(
207 const OUString
& rLocalName
,
208 const uno::Reference
< xml::sax::XAttributeList
> & xAttrList
)
210 SvXMLImportContext
*pContext(nullptr);
212 if ((nPrefix
== XML_NAMESPACE_TEXT
) &&
213 IsXMLToken(rLocalName
, XML_P
))
216 GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
225 void XMLHeaderFooterRegionContext::endFastElement(sal_Int32
)
227 if( GetImport().GetTextImport()->GetCursor().is() )
229 //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
230 if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
232 GetImport().GetTextImport()->GetText()->insertString(
233 GetImport().GetTextImport()->GetCursorAsRange(), "",
236 GetImport().GetTextImport()->ResetCursor();
238 if (xOldTextCursor
.is())
239 GetImport().GetTextImport()->SetCursor(xOldTextCursor
);
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */