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