android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / ui / vba / vbaheaderfooterhelper.cxx
blob987088b6a5adf9d0047fe50cabf32b35e3df5226
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include "vbaheaderfooterhelper.hxx"
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>
27 using namespace ::com::sun::star;
28 using namespace ::ooo::vba;
30 #define FIRST_PAGE 1
32 // Class HeaderFooterHelper
33 bool HeaderFooterHelper::isHeaderFooter( const uno::Reference< frame::XModel >& xModel )
35 return isHeaderFooter( word::getCurrentXText( xModel ) );
38 bool HeaderFooterHelper::isHeaderFooter( const uno::Reference< text::XText >& xText )
40 uno::Reference< lang::XServiceInfo > xServiceInfo( xText, uno::UNO_QUERY_THROW );
41 OUString aImplName = xServiceInfo->getImplementationName();
42 return aImplName == "SwXHeadFootText";
45 bool HeaderFooterHelper::isHeader( const uno::Reference< frame::XModel >& xModel )
47 const uno::Reference< text::XText > xCurrentText = word::getCurrentXText( xModel );
48 if( !isHeaderFooter( xCurrentText ) )
49 return false;
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 )
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";
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
71 if( xTRC->compareRegionStarts( xTR1, xTR2 ) == 0 )
72 return true;
74 catch (const lang::IllegalArgumentException&)
76 return false;
79 return false;
82 bool HeaderFooterHelper::isFirstPageHeader( const uno::Reference< frame::XModel >& xModel )
84 if( isHeader( xModel ) )
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;
91 return false;
94 bool HeaderFooterHelper::isEvenPagesHeader( const uno::Reference< frame::XModel >& xModel )
96 if( isHeader( xModel ) )
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 )
103 uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
104 return ( 0 == xPageCursor->getPage() % 2 );
107 return false;
110 bool HeaderFooterHelper::isFooter( const uno::Reference< frame::XModel >& xModel )
112 const uno::Reference< text::XText > xCurrentText = word::getCurrentXText( xModel );
113 if( !isHeaderFooter( xCurrentText ) )
114 return false;
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 )
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";
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 );
136 if( xTRC->compareRegionStarts( xTR1, xTR2 ) == 0 )
137 return true;
139 catch (const lang::IllegalArgumentException&)
141 return false;
144 return false;
147 bool HeaderFooterHelper::isFirstPageFooter( const uno::Reference< frame::XModel >& xModel )
149 if( isFooter( xModel ) )
151 uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
152 sal_Int32 nPage = xPageCursor->getPage();
153 return nPage == FIRST_PAGE;
155 return false;
158 bool HeaderFooterHelper::isEvenPagesFooter( const uno::Reference< frame::XModel >& xModel )
160 if( isFooter( xModel ) )
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 )
167 uno::Reference< text::XPageCursor > xPageCursor( word::getXTextViewCursor( xModel ), uno::UNO_QUERY_THROW );
168 return ( 0 == xPageCursor->getPage() % 2 );
171 return false;
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */