LibreOffice Module sw (master)  1
vbaheaderfooterhelper.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  */
20 #include "wordvbahelper.hxx"
21 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
22 #include <com/sun/star/text/XTextRangeCompare.hpp>
23 #include <com/sun/star/text/XTextRange.hpp>
24 #include <com/sun/star/text/XPageCursor.hpp>
25 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
26 #include <com/sun/star/container/XNameAccess.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 
30 using namespace ::com::sun::star;
31 using namespace ::ooo::vba;
32 
33 #define FIRST_PAGE 1
34 
35 // Class HeaderFooterHelper
36 bool HeaderFooterHelper::isHeaderFooter( const uno::Reference< frame::XModel >& xModel )
37 {
38  return isHeaderFooter( word::getCurrentXText( xModel ) );
39 }
40 
41 bool HeaderFooterHelper::isHeaderFooter( const uno::Reference< text::XText >& xText )
42 {
43  uno::Reference< lang::XServiceInfo > xServiceInfo( xText, uno::UNO_QUERY_THROW );
44  OUString aImplName = xServiceInfo->getImplementationName();
45  return aImplName == "SwXHeadFootText";
46 }
47 
48 bool HeaderFooterHelper::isHeader( const uno::Reference< frame::XModel >& xModel )
49 {
50  const uno::Reference< text::XText > xCurrentText = word::getCurrentXText( xModel );
51  if( !isHeaderFooter( xCurrentText ) )
52  return false;
53 
54  OUString aPropIsShared = "HeaderIsShared";
55  OUString aPropText = "HeaderText";
56  uno::Reference< style::XStyle > xPageStyle = word::getCurrentPageStyle( xModel );
57  uno::Reference< beans::XPropertySet > xPageProps( xPageStyle, uno::UNO_QUERY_THROW );
58  bool isShared = true;
59  xPageProps->getPropertyValue( aPropIsShared ) >>= isShared;
60  if( !isShared )
61  {
62  uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
63  if( 0 == xPageCursor->getPage() % 2 )
64  aPropText = "HeaderTextLeft";
65  else
66  aPropText = "HeaderTextRight";
67  }
68 
69  uno::Reference< text::XText > xHeaderText( xPageProps->getPropertyValue( aPropText ), uno::UNO_QUERY_THROW );
70  uno::Reference< text::XTextRangeCompare > xTRC( xHeaderText, uno::UNO_QUERY_THROW );
71  uno::Reference< text::XTextRange > xTR1( xCurrentText, uno::UNO_QUERY_THROW );
72  uno::Reference< text::XTextRange > xTR2( xHeaderText, uno::UNO_QUERY_THROW );
73  try
74  {
75  if( xTRC->compareRegionStarts( xTR1, xTR2 ) == 0 )
76  return true;
77  }
78  catch (const lang::IllegalArgumentException&)
79  {
80  return false;
81  }
82 
83  return false;
84 }
85 
86 bool HeaderFooterHelper::isFirstPageHeader( const uno::Reference< frame::XModel >& xModel )
87 {
88  if( isHeader( xModel ) )
89  {
90  uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
91  // FIXME: getPage always returns 1
92  sal_Int32 nPage = xPageCursor->getPage();
93  return nPage == FIRST_PAGE;
94  }
95  return false;
96 }
97 
98 bool HeaderFooterHelper::isEvenPagesHeader( const uno::Reference< frame::XModel >& xModel )
99 {
100  if( isHeader( xModel ) )
101  {
102  uno::Reference< beans::XPropertySet > xStyleProps( word::getCurrentPageStyle( xModel ), uno::UNO_QUERY_THROW );
103  bool isShared = false;
104  xStyleProps->getPropertyValue("HeaderIsShared") >>= isShared;
105  if( !isShared )
106  {
107  uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
108  return ( 0 == xPageCursor->getPage() % 2 );
109  }
110  }
111  return false;
112 }
113 
114 bool HeaderFooterHelper::isFooter( const uno::Reference< frame::XModel >& xModel )
115 {
116  const uno::Reference< text::XText > xCurrentText = word::getCurrentXText( xModel );
117  if( !isHeaderFooter( xCurrentText ) )
118  return false;
119 
120  OUString aPropIsShared = "FooterIsShared";
121  OUString aPropText = "FooterText";
122  uno::Reference< style::XStyle > xPageStyle = word::getCurrentPageStyle( xModel );
123  uno::Reference< beans::XPropertySet > xPageProps( xPageStyle, uno::UNO_QUERY_THROW );
124  bool isShared = true;
125  xPageProps->getPropertyValue( aPropIsShared ) >>= isShared;
126  if( !isShared )
127  {
128  uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
129  if( 0 == xPageCursor->getPage() % 2 )
130  aPropText = "FooterTextLeft";
131  else
132  aPropText = "FooterTextRight";
133  }
134 
135  uno::Reference< text::XText > xFooterText( xPageProps->getPropertyValue( aPropText ), uno::UNO_QUERY_THROW );
136  uno::Reference< text::XTextRangeCompare > xTRC( xFooterText, uno::UNO_QUERY_THROW );
137  uno::Reference< text::XTextRange > xTR1( xCurrentText, uno::UNO_QUERY_THROW );
138  uno::Reference< text::XTextRange > xTR2( xFooterText, uno::UNO_QUERY_THROW );
139  try
140  {
141  if( xTRC->compareRegionStarts( xTR1, xTR2 ) == 0 )
142  return true;
143  }
144  catch (const lang::IllegalArgumentException&)
145  {
146  return false;
147  }
148 
149  return false;
150 }
151 
152 bool HeaderFooterHelper::isFirstPageFooter( const uno::Reference< frame::XModel >& xModel )
153 {
154  if( isFooter( xModel ) )
155  {
156  uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
157  sal_Int32 nPage = xPageCursor->getPage();
158  return nPage == FIRST_PAGE;
159  }
160  return false;
161 }
162 
163 bool HeaderFooterHelper::isEvenPagesFooter( const uno::Reference< frame::XModel >& xModel )
164 {
165  if( isFooter( xModel ) )
166  {
167  uno::Reference< beans::XPropertySet > xStyleProps( word::getCurrentPageStyle( xModel ), uno::UNO_QUERY_THROW );
168  bool isShared = false;
169  xStyleProps->getPropertyValue("FooterIsShared") >>= isShared;
170  if( !isShared )
171  {
172  uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
173  return ( 0 == xPageCursor->getPage() % 2 );
174  }
175  }
176  return false;
177 }
178 
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool isFirstPageHeader(const css::uno::Reference< css::frame::XModel > &xModel)
static bool isEvenPagesFooter(const css::uno::Reference< css::frame::XModel > &xModel)
uno::Reference< style::XStyle > getCurrentPageStyle(const uno::Reference< frame::XModel > &xModel)
uno::Reference< text::XTextViewCursor > getXTextViewCursor(const uno::Reference< frame::XModel > &xModel)
static bool isFooter(const css::uno::Reference< css::frame::XModel > &xModel)
#define FIRST_PAGE
static bool isEvenPagesHeader(const css::uno::Reference< css::frame::XModel > &xModel)
static bool isHeaderFooter(const css::uno::Reference< css::frame::XModel > &xModel)
uno::Reference< text::XText > getCurrentXText(const uno::Reference< frame::XModel > &xModel)
static bool isFirstPageFooter(const css::uno::Reference< css::frame::XModel > &xModel)
static bool isHeader(const css::uno::Reference< css::frame::XModel > &xModel)