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