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