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 :
20 : #include <com/sun/star/text/XText.hpp>
21 : #include <com/sun/star/text/XRelativeTextContentRemove.hpp>
22 : #include <xmloff/nmspmap.hxx>
23 : #include "xmloff/xmlnmspe.hxx"
24 : #include "XMLTextHeaderFooterContext.hxx"
25 : #include <xmloff/XMLTextTableContext.hxx>
26 : #include <xmloff/xmlimp.hxx>
27 :
28 :
29 : using ::rtl::OUString;
30 : using ::rtl::OUStringBuffer;
31 :
32 : using namespace ::com::sun::star;
33 : using namespace ::com::sun::star::uno;
34 : using namespace ::com::sun::star::xml::sax;
35 : using namespace ::com::sun::star::text;
36 : using namespace ::com::sun::star::beans;
37 :
38 :
39 0 : TYPEINIT1( XMLTextHeaderFooterContext, SvXMLImportContext );
40 :
41 30 : XMLTextHeaderFooterContext::XMLTextHeaderFooterContext( SvXMLImport& rImport, sal_uInt16 nPrfx,
42 : const OUString& rLName,
43 : const uno::Reference<
44 : xml::sax::XAttributeList > &,
45 : const Reference < XPropertySet > & rPageStylePropSet,
46 : sal_Bool bFooter, sal_Bool bLft, sal_Bool bFrst ) :
47 : SvXMLImportContext( rImport, nPrfx, rLName ),
48 : xPropSet( rPageStylePropSet ),
49 : sOn( bFooter ? OUString(RTL_CONSTASCII_USTRINGPARAM( "FooterIsOn" )) : OUString(RTL_CONSTASCII_USTRINGPARAM( "HeaderIsOn" )) ),
50 : sShareContent( bFooter ? OUString(RTL_CONSTASCII_USTRINGPARAM( "FooterIsShared" ))
51 : : OUString(RTL_CONSTASCII_USTRINGPARAM( "HeaderIsShared" )) ),
52 : sShareContentFirst( "FirstIsShared" ),
53 : sText( bFooter ? OUString(RTL_CONSTASCII_USTRINGPARAM( "FooterText" )) : OUString(RTL_CONSTASCII_USTRINGPARAM( "HeaderText" )) ),
54 : sTextFirst(bFooter ? OUString("FooterTextFirst") : OUString("HeaderTextFirst")),
55 : sTextLeft( bFooter ? OUString(RTL_CONSTASCII_USTRINGPARAM( "FooterTextLeft" ))
56 : : OUString(RTL_CONSTASCII_USTRINGPARAM( "HeaderTextLeft" )) ),
57 : bInsertContent( sal_True ),
58 : bLeft( bLft ),
59 30 : bFirst( bFrst )
60 : {
61 30 : if( bLeft || bFirst )
62 : {
63 16 : Any aAny;
64 :
65 16 : aAny = xPropSet->getPropertyValue( sOn );
66 16 : sal_Bool bOn = *(sal_Bool *)aAny.getValue();
67 :
68 16 : if( bOn )
69 : {
70 16 : if (bLeft)
71 : {
72 8 : aAny = xPropSet->getPropertyValue( sShareContent );
73 8 : sal_Bool bShared = *(sal_Bool *)aAny.getValue();
74 8 : if( bShared )
75 : {
76 : // Don't share headers any longer
77 8 : bShared = sal_False;
78 8 : aAny.setValue( &bShared, ::getBooleanCppuType() );
79 8 : xPropSet->setPropertyValue( sShareContent, aAny );
80 : }
81 : }
82 16 : if (bFirst)
83 : {
84 8 : aAny = xPropSet->getPropertyValue( sShareContentFirst );
85 8 : sal_Bool bSharedFirst = *(sal_Bool *)aAny.getValue();
86 8 : if( bSharedFirst )
87 : {
88 : // Don't share first/right headers any longer
89 4 : bSharedFirst = sal_False;
90 4 : aAny.setValue( &bSharedFirst, ::getBooleanCppuType() );
91 4 : xPropSet->setPropertyValue( sShareContentFirst, aAny );
92 : }
93 : }
94 : }
95 : else
96 : {
97 : // If headers or footers are switched off, no content must be
98 : // inserted.
99 0 : bInsertContent = sal_False;
100 16 : }
101 : }
102 30 : }
103 :
104 60 : XMLTextHeaderFooterContext::~XMLTextHeaderFooterContext()
105 : {
106 60 : }
107 :
108 30 : SvXMLImportContext *XMLTextHeaderFooterContext::CreateChildContext(
109 : sal_uInt16 nPrefix,
110 : const OUString& rLocalName,
111 : const uno::Reference< xml::sax::XAttributeList > & xAttrList )
112 : {
113 30 : SvXMLImportContext *pContext = 0;
114 30 : if( bInsertContent )
115 : {
116 30 : if( !xOldTextCursor.is() )
117 : {
118 30 : sal_Bool bRemoveContent = sal_True;
119 30 : Any aAny;
120 30 : if( bLeft || bFirst )
121 : {
122 : // Headers and footers are switched on already,
123 : // and they aren't shared.
124 32 : if (bLeft)
125 8 : aAny = xPropSet->getPropertyValue( sTextLeft );
126 : else
127 8 : aAny = xPropSet->getPropertyValue( sTextFirst );
128 : }
129 : else
130 : {
131 14 : aAny = xPropSet->getPropertyValue( sOn );
132 14 : sal_Bool bOn = *(sal_Bool *)aAny.getValue();
133 :
134 14 : if( !bOn )
135 : {
136 : // Switch header on
137 14 : bOn = sal_True;
138 14 : aAny.setValue( &bOn, ::getBooleanCppuType() );
139 14 : xPropSet->setPropertyValue( sOn, aAny );
140 :
141 : // The content has not to be removed, because the header
142 : // or footer is empty already.
143 14 : bRemoveContent = sal_False;
144 : }
145 :
146 : // If a header or footer is not shared, share it now.
147 14 : aAny = xPropSet->getPropertyValue( sShareContent );
148 14 : sal_Bool bShared = *(sal_Bool *)aAny.getValue();
149 14 : if( !bShared )
150 : {
151 0 : bShared = sal_True;
152 0 : aAny.setValue( &bShared, ::getBooleanCppuType() );
153 0 : xPropSet->setPropertyValue( sShareContent, aAny );
154 : }
155 :
156 14 : aAny = xPropSet->getPropertyValue( sText );
157 : }
158 :
159 30 : Reference < XText > xText;
160 30 : aAny >>= xText;
161 :
162 30 : if( bRemoveContent )
163 : {
164 16 : OUString aText;
165 16 : xText->setString( aText );
166 : }
167 :
168 : UniReference < XMLTextImportHelper > xTxtImport =
169 30 : GetImport().GetTextImport();
170 :
171 30 : xOldTextCursor = xTxtImport->GetCursor();
172 30 : xTxtImport->SetCursor( xText->createTextCursor() );
173 : }
174 :
175 : pContext =
176 30 : GetImport().GetTextImport()->CreateTextChildContext(
177 30 : GetImport(), nPrefix, rLocalName, xAttrList,
178 60 : XML_TEXT_TYPE_HEADER_FOOTER );
179 : }
180 30 : if( !pContext )
181 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
182 :
183 30 : return pContext;
184 : }
185 :
186 30 : void XMLTextHeaderFooterContext::EndElement()
187 : {
188 30 : if( xOldTextCursor.is() )
189 : {
190 30 : GetImport().GetTextImport()->DeleteParagraph();
191 30 : GetImport().GetTextImport()->SetCursor( xOldTextCursor );
192 : }
193 0 : else if( !bLeft )
194 : {
195 : // If no content has been inserted inro the header or footer,
196 : // switch it off.
197 0 : sal_Bool bOn = sal_False;
198 0 : Any aAny;
199 0 : aAny.setValue( &bOn, ::getBooleanCppuType() );
200 0 : xPropSet->setPropertyValue( sOn, aAny );
201 : }
202 30 : }
203 :
204 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|