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 ) :
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 sShareContent( bFooter ? OUString(SC_UNO_PAGE_FTRSHARED) : OUString(SC_UNO_PAGE_HDRSHARED) );
55  bool bDisplay( true );
56  for( auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ) )
57  {
58  if( aIter.getToken() == XML_ELEMENT(STYLE, XML_DISPLAY) )
59  bDisplay = IsXMLToken(aIter, XML_TRUE);
60  else
61  XMLOFF_WARN_UNKNOWN("sc", aIter);
62  }
63  if( bLeft )
64  {
65  bool bOn(::cppu::any2bool(xPropSet->getPropertyValue( sOn )));
66 
67  if( bOn && bDisplay )
68  {
69  if( ::cppu::any2bool(xPropSet->getPropertyValue( sShareContent )) )
70  // Don't share headers any longer
71  xPropSet->setPropertyValue( sShareContent, uno::makeAny(false) );
72  }
73  else
74  {
75  if( !::cppu::any2bool(xPropSet->getPropertyValue( sShareContent )) )
76  // share headers
77  xPropSet->setPropertyValue( sShareContent, uno::makeAny(true) );
78  }
79  }
80  else
81  {
82  bool bOn(::cppu::any2bool(xPropSet->getPropertyValue( sOn )));
83  if ( bOn != bDisplay )
84  xPropSet->setPropertyValue( sOn, uno::makeAny(bDisplay) );
85  }
86  if (bLeft)
87  sCont = sContentLeft;
88  else
89  sCont = sContent;
90  xPropSet->getPropertyValue( sCont ) >>= xHeaderFooterContent;
91 }
92 
94 {
95 }
96 
97 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTableHeaderFooterContext::createFastChildContext(
98  sal_Int32 nElement,
99  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
100 {
101  if (xHeaderFooterContent.is())
102  {
103  uno::Reference < text::XText > xText;
104  switch (nElement)
105  {
107  xText.set(xHeaderFooterContent->getLeftText());
108  bContainsLeft = true;
109  break;
111  xText.set(xHeaderFooterContent->getCenterText());
112  bContainsCenter = true;
113  break;
115  xText.set(xHeaderFooterContent->getRightText());
116  bContainsRight = true;
117  break;
118  default: break;
119  }
120  if (xText.is())
121  {
122  xText->setString("");
123  uno::Reference < text::XTextCursor > xTempTextCursor(xText->createTextCursor());
124  return new XMLHeaderFooterRegionContext( GetImport(), xTempTextCursor);
125  }
126  }
127 
128  if ( nElement == XML_ELEMENT(TEXT, XML_P) )
129  {
130  if (!xTextCursor.is())
131  {
132  if( xHeaderFooterContent.is() )
133  {
134  uno::Reference < text::XText > xText(xHeaderFooterContent->getCenterText());
135  xText->setString("");
136  xTextCursor.set(xText->createTextCursor());
137  xOldTextCursor.set(GetImport().GetTextImport()->GetCursor());
138  GetImport().GetTextImport()->SetCursor( xTextCursor );
139  bContainsCenter = true;
140  }
141  }
142  return
143  GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
144  nElement,
145  xAttrList);
146  }
147 
148  XMLOFF_WARN_UNKNOWN_ELEMENT("sc", nElement);
149  return nullptr;
150 }
151 
153 {
154  if( GetImport().GetTextImport()->GetCursor().is() )
155  {
156  //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
157  if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
158  {
159  GetImport().GetTextImport()->GetText()->insertString(
160  GetImport().GetTextImport()->GetCursorAsRange(), "",
161  true );
162  }
163  GetImport().GetTextImport()->ResetCursor();
164  }
165  if (xOldTextCursor.is())
166  GetImport().GetTextImport()->SetCursor(xOldTextCursor);
167  if (xHeaderFooterContent.is())
168  {
169  if (!bContainsLeft)
170  xHeaderFooterContent->getLeftText()->setString("");
171  if (!bContainsCenter)
172  xHeaderFooterContent->getCenterText()->setString("");
173  if (!bContainsRight)
174  xHeaderFooterContent->getRightText()->setString("");
175 
176  xPropSet->setPropertyValue( sCont, uno::makeAny(xHeaderFooterContent) );
177  }
178 }
179 
180 
182  uno::Reference< text::XTextCursor >& xCursor ) :
183  SvXMLImportContext( rImport ),
184  xTextCursor ( xCursor )
185 {
186  xOldTextCursor.set(GetImport().GetTextImport()->GetCursor());
187  GetImport().GetTextImport()->SetCursor( xTextCursor );
188 }
189 
191 {
192 }
193 
194 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLHeaderFooterRegionContext::createFastChildContext(
195  sal_Int32 nElement,
196  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
197 {
198  SvXMLImportContext *pContext(nullptr);
199 
200  if (nElement == XML_ELEMENT(TEXT, XML_P))
201  {
202  return GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
203  nElement,
204  xAttrList);
205  }
206  XMLOFF_WARN_UNKNOWN_ELEMENT("sc", nElement);
207  return pContext;
208 }
209 
211 {
212  if( GetImport().GetTextImport()->GetCursor().is() )
213  {
214  //GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
215  if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, true ) )
216  {
217  GetImport().GetTextImport()->GetText()->insertString(
218  GetImport().GetTextImport()->GetCursorAsRange(), "",
219  true );
220  }
221  GetImport().GetTextImport()->ResetCursor();
222  }
223  if (xOldTextCursor.is())
224  GetImport().GetTextImport()->SetCursor(xOldTextCursor);
225 }
226 
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::text::XTextCursor > xTextCursor
XML_DISPLAY
#define SC_UNO_PAGE_RIGHTFTRCON
Definition: unonames.hxx:448
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)
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)
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
XML_REGION_RIGHT
#define XMLOFF_WARN_UNKNOWN(area, rIter)
#define SC_UNO_PAGE_LEFTHDRCONT
Definition: unonames.hxx:445
XML_REGION_LEFT
#define SC_UNO_PAGE_HDRON
Definition: unonames.hxx:471
XML_REGION_CENTER
css::uno::Reference< css::text::XTextCursor > & xTextCursor
#define SC_UNO_PAGE_LEFTFTRCONT
Definition: unonames.hxx:446
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
css::uno::Reference< css::beans::XPropertySet > xPropSet
#define SC_UNO_PAGE_FTRON
Definition: unonames.hxx:494
css::uno::Reference< css::text::XTextCursor > xOldTextCursor
css::uno::Reference< css::text::XTextCursor > xOldTextCursor
#define SC_UNO_PAGE_HDRSHARED
Definition: unonames.hxx:473
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
XML_P
#define XML_ELEMENT(prefix, name)
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
XMLHeaderFooterRegionContext(SvXMLImport &rImport, css::uno::Reference< css::text::XTextCursor > &xCursor)
XML_TRUE
#define SC_UNO_PAGE_FTRSHARED
Definition: unonames.hxx:496
#define XMLOFF_WARN_UNKNOWN_ELEMENT(area, token)
TEXT
#define SC_UNO_PAGE_RIGHTHDRCON
Definition: unonames.hxx:447
bool any2bool(const css::uno::Any &rAny)
css::uno::Reference< css::sheet::XHeaderFooterContent > xHeaderFooterContent