LibreOffice Module sc (master)  1
XMLTableHeaderFooterContext.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
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>
26 #include <xmloff/xmltoken.hxx>
27 #include <comphelper/extract.hxx>
28 #include <sal/log.hxx>
29 
30 #include <unonames.hxx>
31 
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;
38 
39 
41  const uno::Reference<
42  xml::sax::XFastAttributeList > & xAttrList,
43  const Reference < XPropertySet > & rPageStylePropSet,
44  bool bFooter, bool bLeft, bool bFirst ) :
45  SvXMLImportContext( rImport ),
46  xPropSet( rPageStylePropSet ),
47  bContainsLeft(false),
48  bContainsRight(false),
49  bContainsCenter(false)
50 {
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 ) )
59  {
60  if( aIter.getToken() == XML_ELEMENT(STYLE, XML_DISPLAY) )
61  bDisplay = IsXMLToken(aIter, XML_TRUE);
62  else
63  XMLOFF_WARN_UNKNOWN("sc", aIter);
64  }
65  bool bOn(::cppu::any2bool(xPropSet->getPropertyValue( sOn )));
66  if( bLeft || bFirst )
67  {
68  const OUString sShare = bLeft ? sShareContent : sShareFirstContent;
69  if( bOn && bDisplay )
70  {
71  if( ::cppu::any2bool(xPropSet->getPropertyValue( sShare )) )
72  // Don't share headers any longer
73  xPropSet->setPropertyValue( sShare, uno::makeAny(false) );
74  }
75  else
76  {
77  if( !::cppu::any2bool(xPropSet->getPropertyValue( sShare )) )
78  // share headers
79  xPropSet->setPropertyValue( sShare, uno::makeAny(true) );
80  }
81  }
82  else
83  {
84  if ( bOn != bDisplay )
85  xPropSet->setPropertyValue( sOn, uno::makeAny(bDisplay) );
86  }
87  if (bLeft)
88  {
89  sCont = sContentLeft;
90  }
91  else if (bFirst)
92  {
93  sCont = sContentFirst;
94  xPropSet->setPropertyValue( sShareFirstContent, uno::makeAny(!bDisplay) );
95  }
96  else
97  {
98  sCont = sContent;
99  }
100  xPropSet->getPropertyValue( sCont ) >>= xHeaderFooterContent;
101 }
102 
104 {
105 }
106 
107 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTableHeaderFooterContext::createFastChildContext(
108  sal_Int32 nElement,
109  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
110 {
111  if (xHeaderFooterContent.is())
112  {
113  uno::Reference < text::XText > xText;
114  switch (nElement)
115  {
117  xText.set(xHeaderFooterContent->getLeftText());
118  bContainsLeft = true;
119  break;
121  xText.set(xHeaderFooterContent->getCenterText());
122  bContainsCenter = true;
123  break;
125  xText.set(xHeaderFooterContent->getRightText());
126  bContainsRight = true;
127  break;
128  default: break;
129  }
130  if (xText.is())
131  {
132  xText->setString("");
133  uno::Reference < text::XTextCursor > xTempTextCursor(xText->createTextCursor());
134  return new XMLHeaderFooterRegionContext( GetImport(), xTempTextCursor);
135  }
136  }
137 
138  if ( nElement == XML_ELEMENT(TEXT, XML_P) )
139  {
140  if (!xTextCursor.is())
141  {
142  if( xHeaderFooterContent.is() )
143  {
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;
150  }
151  }
152  return
153  GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
154  nElement,
155  xAttrList);
156  }
157 
158  XMLOFF_WARN_UNKNOWN_ELEMENT("sc", nElement);
159  return nullptr;
160 }
161 
163 {
164  if( GetImport().GetTextImport()->GetCursor().is() )
165  {
166  //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
167  if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
168  {
169  GetImport().GetTextImport()->GetText()->insertString(
170  GetImport().GetTextImport()->GetCursorAsRange(), "",
171  true );
172  }
173  GetImport().GetTextImport()->ResetCursor();
174  }
175  if (xOldTextCursor.is())
176  GetImport().GetTextImport()->SetCursor(xOldTextCursor);
177  if (xHeaderFooterContent.is())
178  {
179  if (!bContainsLeft)
180  xHeaderFooterContent->getLeftText()->setString("");
181  if (!bContainsCenter)
182  xHeaderFooterContent->getCenterText()->setString("");
183  if (!bContainsRight)
184  xHeaderFooterContent->getRightText()->setString("");
185 
186  xPropSet->setPropertyValue( sCont, uno::makeAny(xHeaderFooterContent) );
187  }
188 }
189 
190 
192  uno::Reference< text::XTextCursor >& xCursor ) :
193  SvXMLImportContext( rImport ),
194  xTextCursor ( xCursor )
195 {
196  xOldTextCursor.set(GetImport().GetTextImport()->GetCursor());
197  GetImport().GetTextImport()->SetCursor( xTextCursor );
198 }
199 
201 {
202 }
203 
204 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLHeaderFooterRegionContext::createFastChildContext(
205  sal_Int32 nElement,
206  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
207 {
208  SvXMLImportContext *pContext(nullptr);
209 
210  if (nElement == XML_ELEMENT(TEXT, XML_P))
211  {
212  return GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
213  nElement,
214  xAttrList);
215  }
216  XMLOFF_WARN_UNKNOWN_ELEMENT("sc", nElement);
217  return pContext;
218 }
219 
221 {
222  if( GetImport().GetTextImport()->GetCursor().is() )
223  {
224  //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
225  if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
226  {
227  GetImport().GetTextImport()->GetText()->insertString(
228  GetImport().GetTextImport()->GetCursorAsRange(), "",
229  true );
230  }
231  GetImport().GetTextImport()->ResetCursor();
232  }
233  if (xOldTextCursor.is())
234  GetImport().GetTextImport()->SetCursor(xOldTextCursor);
235 }
236 
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SC_UNO_PAGE_FIRSTFTRCONT
Definition: unonames.hxx:451
css::uno::Reference< css::text::XTextCursor > xTextCursor
XML_REGION_LEFT
#define SC_UNO_PAGE_RIGHTFTRCON
Definition: unonames.hxx:449
SvXMLImport & GetImport()
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &) override
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
#define SC_UNO_PAGE_FIRSTHDRSHARED
Definition: unonames.hxx:444
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
#define SC_UNO_PAGE_FIRSTHDRCONT
Definition: unonames.hxx:450
#define XMLOFF_WARN_UNKNOWN(area, rIter)
#define SC_UNO_PAGE_LEFTHDRCONT
Definition: unonames.hxx:446
XMLTableHeaderFooterContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, const css::uno::Reference< css::beans::XPropertySet > &rPageStylePropSet, bool bFooter, bool bLft, bool bFirst)
#define SC_UNO_PAGE_HDRON
Definition: unonames.hxx:474
XML_P
css::uno::Reference< css::text::XTextCursor > & xTextCursor
XML_REGION_RIGHT
XML_REGION_CENTER
XML_DISPLAY
#define SC_UNO_PAGE_LEFTFTRCONT
Definition: unonames.hxx:447
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
#define SC_UNO_PAGE_FIRSTFTRSHARED
Definition: unonames.hxx:445
css::uno::Reference< css::beans::XPropertySet > xPropSet
XML_TRUE
#define SC_UNO_PAGE_FTRON
Definition: unonames.hxx:497
css::uno::Reference< css::text::XTextCursor > xOldTextCursor
css::uno::Reference< css::text::XTextCursor > xOldTextCursor
#define SC_UNO_PAGE_HDRSHARED
Definition: unonames.hxx:476
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
#define XML_ELEMENT(prefix, name)
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
XMLHeaderFooterRegionContext(SvXMLImport &rImport, css::uno::Reference< css::text::XTextCursor > &xCursor)
#define SC_UNO_PAGE_FTRSHARED
Definition: unonames.hxx:499
#define XMLOFF_WARN_UNKNOWN_ELEMENT(area, token)
TEXT
#define SC_UNO_PAGE_RIGHTHDRCON
Definition: unonames.hxx:448
bool any2bool(const css::uno::Any &rAny)
css::uno::Reference< css::sheet::XHeaderFooterContent > xHeaderFooterContent