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 60 : 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 60 : bFirst( bFrst )
60 : {
61 60 : if( bLeft || bFirst )
62 : {
63 32 : Any aAny;
64 :
65 32 : aAny = xPropSet->getPropertyValue( sOn );
66 32 : sal_Bool bOn = *(sal_Bool *)aAny.getValue();
67 :
68 32 : if( bOn )
69 : {
70 32 : if (bLeft)
71 : {
72 16 : aAny = xPropSet->getPropertyValue( sShareContent );
73 16 : sal_Bool bShared = *(sal_Bool *)aAny.getValue();
74 16 : if( bShared )
75 : {
76 : // Don't share headers any longer
77 16 : bShared = sal_False;
78 16 : aAny.setValue( &bShared, ::getBooleanCppuType() );
79 16 : xPropSet->setPropertyValue( sShareContent, aAny );
80 : }
81 : }
82 32 : if (bFirst)
83 : {
84 16 : aAny = xPropSet->getPropertyValue( sShareContentFirst );
85 16 : sal_Bool bSharedFirst = *(sal_Bool *)aAny.getValue();
86 16 : if( bSharedFirst )
87 : {
88 : // Don't share first/right headers any longer
89 8 : bSharedFirst = sal_False;
90 8 : aAny.setValue( &bSharedFirst, ::getBooleanCppuType() );
91 8 : 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 32 : }
101 : }
102 60 : }
103 :
104 120 : XMLTextHeaderFooterContext::~XMLTextHeaderFooterContext()
105 : {
106 120 : }
107 :
108 60 : SvXMLImportContext *XMLTextHeaderFooterContext::CreateChildContext(
109 : sal_uInt16 nPrefix,
110 : const OUString& rLocalName,
111 : const uno::Reference< xml::sax::XAttributeList > & xAttrList )
112 : {
113 60 : SvXMLImportContext *pContext = 0;
114 60 : if( bInsertContent )
115 : {
116 60 : if( !xOldTextCursor.is() )
117 : {
118 60 : sal_Bool bRemoveContent = sal_True;
119 60 : Any aAny;
120 60 : if( bLeft || bFirst )
121 : {
122 : // Headers and footers are switched on already,
123 : // and they aren't shared.
124 64 : if (bLeft)
125 16 : aAny = xPropSet->getPropertyValue( sTextLeft );
126 : else
127 16 : aAny = xPropSet->getPropertyValue( sTextFirst );
128 : }
129 : else
130 : {
131 28 : aAny = xPropSet->getPropertyValue( sOn );
132 28 : sal_Bool bOn = *(sal_Bool *)aAny.getValue();
133 :
134 28 : if( !bOn )
135 : {
136 : // Switch header on
137 28 : bOn = sal_True;
138 28 : aAny.setValue( &bOn, ::getBooleanCppuType() );
139 28 : xPropSet->setPropertyValue( sOn, aAny );
140 :
141 : // The content has not to be removed, because the header
142 : // or footer is empty already.
143 28 : bRemoveContent = sal_False;
144 : }
145 :
146 : // If a header or footer is not shared, share it now.
147 28 : aAny = xPropSet->getPropertyValue( sShareContent );
148 28 : sal_Bool bShared = *(sal_Bool *)aAny.getValue();
149 28 : if( !bShared )
150 : {
151 0 : bShared = sal_True;
152 0 : aAny.setValue( &bShared, ::getBooleanCppuType() );
153 0 : xPropSet->setPropertyValue( sShareContent, aAny );
154 : }
155 :
156 28 : aAny = xPropSet->getPropertyValue( sText );
157 : }
158 :
159 60 : Reference < XText > xText;
160 60 : aAny >>= xText;
161 :
162 60 : if( bRemoveContent )
163 : {
164 32 : OUString aText;
165 32 : xText->setString( aText );
166 : }
167 :
168 : UniReference < XMLTextImportHelper > xTxtImport =
169 60 : GetImport().GetTextImport();
170 :
171 60 : xOldTextCursor = xTxtImport->GetCursor();
172 60 : xTxtImport->SetCursor( xText->createTextCursor() );
173 : }
174 :
175 : pContext =
176 60 : GetImport().GetTextImport()->CreateTextChildContext(
177 60 : GetImport(), nPrefix, rLocalName, xAttrList,
178 120 : XML_TEXT_TYPE_HEADER_FOOTER );
179 : }
180 60 : if( !pContext )
181 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
182 :
183 60 : return pContext;
184 : }
185 :
186 60 : void XMLTextHeaderFooterContext::EndElement()
187 : {
188 60 : if( xOldTextCursor.is() )
189 : {
190 60 : GetImport().GetTextImport()->DeleteParagraph();
191 60 : 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 60 : }
203 :
204 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|