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