1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "vbaheaderfooterhelper.hxx"
31 #include "wordvbahelper.hxx"
32 #include <comphelper/processfactory.hxx>
33 #include <com/sun/star/frame/XController.hpp>
34 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
35 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
36 #include <com/sun/star/container/XNameAccess.hpp>
38 using namespace ::com::sun::star
;
39 using namespace ::ooo::vba
;
43 // Class HeaderFooterHelper
45 sal_Bool
HeaderFooterHelper::isHeader( const uno::Reference
< frame::XModel
>& xModel
, const uno::Reference
< text::XText
>& xCurrentText
) throw (uno::RuntimeException
)
47 uno::Reference
< text::XPageCursor
> xPageCursor( word::getXTextViewCursor( xModel
), uno::UNO_QUERY_THROW
);
48 uno::Reference
< beans::XPropertySet
> xStyleProps( word::getCurrentPageStyle( xModel
), uno::UNO_QUERY_THROW
);
50 sal_Bool isOn
= sal_False
;
51 xStyleProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsOn"))) >>= isOn
;
55 sal_Bool isShared
= sal_False
;
56 xStyleProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsShared"))) >>= isShared
;
58 rtl::OUString aPropText
= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderText") );
61 if( 0 == xPageCursor
->getPage() % 2 )
63 aPropText
= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderTextLeft") );
67 aPropText
= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderTextRight") );
71 uno::Reference
< text::XText
> xText( xStyleProps
->getPropertyValue( aPropText
), uno::UNO_QUERY_THROW
);
72 //FIXME: can not compare in this way?
73 return ( xText
== xCurrentText
);
76 sal_Bool
HeaderFooterHelper::isFirstPageHeader( const uno::Reference
< frame::XModel
>& xModel
, const uno::Reference
< text::XText
>& xCurrentText
) throw (uno::RuntimeException
)
78 if( isHeader( xModel
, xCurrentText
) )
80 uno::Reference
< text::XPageCursor
> xPageCursor( word::getXTextViewCursor( xModel
), uno::UNO_QUERY_THROW
);
81 // FIXME: getPage allways returns 1
82 sal_Int32 nPage
= xPageCursor
->getPage();
83 return nPage
== FIRST_PAGE
;
88 sal_Bool
HeaderFooterHelper::isEvenPagesHeader( const uno::Reference
< frame::XModel
>& xModel
, const uno::Reference
< text::XText
>& xCurrentText
) throw (uno::RuntimeException
)
90 if( isHeader( xModel
, xCurrentText
) )
92 uno::Reference
< beans::XPropertySet
> xStyleProps( word::getCurrentPageStyle( xModel
), uno::UNO_QUERY_THROW
);
93 sal_Bool isShared
= sal_False
;
94 xStyleProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("HeaderIsShared"))) >>= isShared
;
97 uno::Reference
< text::XPageCursor
> xPageCursor( word::getXTextViewCursor( xModel
), uno::UNO_QUERY_THROW
);
98 return ( 0 == xPageCursor
->getPage() % 2 );
104 sal_Bool
HeaderFooterHelper::isFooter( const uno::Reference
< frame::XModel
>& xModel
, const uno::Reference
< text::XText
>& xCurrentText
) throw (uno::RuntimeException
)
106 uno::Reference
< text::XPageCursor
> xPageCursor( word::getXTextViewCursor( xModel
), uno::UNO_QUERY_THROW
);
107 uno::Reference
< beans::XPropertySet
> xStyleProps( word::getCurrentPageStyle( xModel
), uno::UNO_QUERY_THROW
);
109 sal_Bool isOn
= sal_False
;
110 xStyleProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsOn"))) >>= isOn
;
114 sal_Bool isShared
= sal_False
;
115 xStyleProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsShared"))) >>= isShared
;
117 rtl::OUString aPropText
= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterText") );
120 if( 0 == xPageCursor
->getPage() % 2 )
122 aPropText
= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterTextLeft") );
126 aPropText
= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterTextRight") );
130 uno::Reference
< text::XText
> xText( xStyleProps
->getPropertyValue( aPropText
), uno::UNO_QUERY_THROW
);
132 return ( xText
== xCurrentText
);
135 sal_Bool
HeaderFooterHelper::isFirstPageFooter( const uno::Reference
< frame::XModel
>& xModel
, const uno::Reference
< text::XText
>& xCurrentText
) throw (uno::RuntimeException
)
137 if( isFooter( xModel
, xCurrentText
) )
139 uno::Reference
< text::XPageCursor
> xPageCursor( word::getXTextViewCursor( xModel
), uno::UNO_QUERY_THROW
);
140 sal_Int32 nPage
= xPageCursor
->getPage();
141 return nPage
== FIRST_PAGE
;
146 sal_Bool
HeaderFooterHelper::isEvenPagesFooter( const uno::Reference
< frame::XModel
>& xModel
, const uno::Reference
< text::XText
>& xCurrentText
) throw (uno::RuntimeException
)
148 if( isFooter( xModel
, xCurrentText
) )
150 uno::Reference
< beans::XPropertySet
> xStyleProps( word::getCurrentPageStyle( xModel
), uno::UNO_QUERY_THROW
);
151 sal_Bool isShared
= sal_False
;
152 xStyleProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FooterIsShared"))) >>= isShared
;
155 uno::Reference
< text::XPageCursor
> xPageCursor( word::getXTextViewCursor( xModel
), uno::UNO_QUERY_THROW
);
156 return ( 0 == xPageCursor
->getPage() % 2 );
162 sal_Bool
HeaderFooterHelper::isPrimaryHeader( const uno::Reference
< frame::XModel
>& xModel
, const uno::Reference
< text::XText
>& xCurrentText
) throw (uno::RuntimeException
)
164 if( isHeader( xModel
, xCurrentText
) )
166 return( !( isFirstPageHeader( xModel
, xCurrentText
) && isEvenPagesHeader( xModel
, xCurrentText
) ) );
171 sal_Bool
HeaderFooterHelper::isPrimaryFooter( const uno::Reference
< frame::XModel
>& xModel
, const uno::Reference
< text::XText
>& xCurrentText
) throw (uno::RuntimeException
)
173 if( isHeader( xModel
, xCurrentText
) )
175 return( !( isFirstPageFooter( xModel
, xCurrentText
) && isEvenPagesFooter( xModel
, xCurrentText
) ) );