bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / style / PageMasterImportContext.cxx
blobeea9debfefcea14c6fd91bfb389bddd4218822dc
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 .
20 #include "PageMasterImportContext.hxx"
21 #include <xmloff/xmlnmspe.hxx>
22 #include <xmloff/xmltoken.hxx>
23 #include "PageMasterPropHdl.hxx"
24 #include "PagePropertySetContext.hxx"
25 #include "PageHeaderFooterContext.hxx"
26 #include "PageMasterPropMapper.hxx"
27 #include "PageMasterImportPropMapper.hxx"
28 #include <xmloff/PageMasterStyleMap.hxx>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <osl/diagnose.h>
32 //UUUU
33 #include <xmlsdtypes.hxx>
34 #include <com/sun/star/beans/XPropertySetInfo.hpp>
35 #include <xmloff/xmlerror.hxx>
37 using namespace ::com::sun::star;
38 using namespace ::xmloff::token;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::lang;
42 //UUUU
43 using namespace ::com::sun::star::beans;
45 void PageStyleContext::SetAttribute( sal_uInt16 nPrefixKey,
46 const OUString& rLocalName,
47 const OUString& rValue )
49 // TODO: use a map here
50 if( XML_NAMESPACE_STYLE == nPrefixKey && IsXMLToken( rLocalName, XML_PAGE_USAGE ) )
52 sPageUsage = rValue;
54 else
56 XMLPropStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
60 TYPEINIT1( PageStyleContext, XMLPropStyleContext );
62 PageStyleContext::PageStyleContext( SvXMLImport& rImport,
63 sal_uInt16 nPrfx, const OUString& rLName,
64 const uno::Reference< xml::sax::XAttributeList > & xAttrList,
65 SvXMLStylesContext& rStyles,
66 bool bDefaultStyle) :
67 XMLPropStyleContext( rImport, nPrfx, rLName, xAttrList, rStyles, XML_STYLE_FAMILY_PAGE_MASTER, bDefaultStyle),
68 sPageUsage(),
69 m_bIsFillStyleAlreadyConverted(false) //UUUU
73 PageStyleContext::~PageStyleContext()
77 SvXMLImportContext *PageStyleContext::CreateChildContext(
78 sal_uInt16 nPrefix,
79 const OUString& rLocalName,
80 const uno::Reference< xml::sax::XAttributeList > & xAttrList )
82 if( XML_NAMESPACE_STYLE == nPrefix &&
83 ((IsXMLToken(rLocalName, XML_HEADER_STYLE )) ||
84 (IsXMLToken(rLocalName, XML_FOOTER_STYLE )) ) )
86 bool bHeader = IsXMLToken(rLocalName, XML_HEADER_STYLE);
87 rtl::Reference < SvXMLImportPropertyMapper > xImpPrMap =
88 GetStyles()->GetImportPropertyMapper( GetFamily() );
89 if( xImpPrMap.is() )
91 const rtl::Reference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
92 sal_Int32 nFlag;
93 if (bHeader)
94 nFlag = CTF_PM_HEADERFLAG;
95 else
96 nFlag = CTF_PM_FOOTERFLAG;
97 sal_Int32 nStartIndex (-1);
98 sal_Int32 nEndIndex (-1);
99 bool bFirst(false);
100 bool bEnd(false);
101 sal_Int32 nIndex = 0;
102 while ( nIndex < rMapper->GetEntryCount() && !bEnd)
104 if ((rMapper->GetEntryContextId( nIndex ) & CTF_PM_FLAGMASK) == nFlag)
106 if (!bFirst)
108 bFirst = true;
109 nStartIndex = nIndex;
112 else if (bFirst)
114 bEnd = true;
115 nEndIndex = nIndex;
117 nIndex++;
119 if (!bEnd)
120 nEndIndex = nIndex;
121 return new PageHeaderFooterContext(GetImport(), nPrefix, rLocalName,
122 xAttrList, GetProperties(), xImpPrMap, nStartIndex, nEndIndex, bHeader);
126 if( XML_NAMESPACE_STYLE == nPrefix &&
127 IsXMLToken(rLocalName, XML_PAGE_LAYOUT_PROPERTIES) )
129 rtl::Reference < SvXMLImportPropertyMapper > xImpPrMap =
130 GetStyles()->GetImportPropertyMapper( GetFamily() );
131 if( xImpPrMap.is() )
133 const rtl::Reference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
134 sal_Int32 nEndIndex (-1);
135 bool bEnd(false);
136 sal_Int32 nIndex = 0;
137 sal_Int16 nContextID;
138 while ( nIndex < rMapper->GetEntryCount() && !bEnd)
140 nContextID = rMapper->GetEntryContextId( nIndex );
141 if (nContextID && ((nContextID & CTF_PM_FLAGMASK) != XML_PM_CTF_START))
143 nEndIndex = nIndex;
144 bEnd = true;
146 nIndex++;
148 if (!bEnd)
149 nEndIndex = nIndex;
150 PageContextType aType = Page;
151 return new PagePropertySetContext( GetImport(), nPrefix,
152 rLocalName, xAttrList,
153 XML_TYPE_PROP_PAGE_LAYOUT,
154 GetProperties(),
155 xImpPrMap, 0, nEndIndex, aType);
159 return XMLPropStyleContext::CreateChildContext(nPrefix, rLocalName, xAttrList);
162 void PageStyleContext::FillPropertySet(const uno::Reference<beans::XPropertySet > & rPropSet)
164 //UUUU need to filter out old fill definitions when the new ones are used. The new
165 // ones are used when a FillStyle is defined
166 if(!m_bIsFillStyleAlreadyConverted && GetProperties().size())
168 static ::rtl::OUString s_FillStyle(RTL_CONSTASCII_USTRINGPARAM("FillStyle"));
169 static ::rtl::OUString s_HeaderFillStyle(RTL_CONSTASCII_USTRINGPARAM("HeaderFillStyle"));
170 static ::rtl::OUString s_FooterFillStyle(RTL_CONSTASCII_USTRINGPARAM("FooterFillStyle"));
172 if(doNewDrawingLayerFillStyleDefinitionsExist(s_FillStyle))
174 deactivateOldFillStyleDefinitions(getStandardSet());
177 if(doNewDrawingLayerFillStyleDefinitionsExist(s_HeaderFillStyle))
179 deactivateOldFillStyleDefinitions(getHeaderSet());
182 if(doNewDrawingLayerFillStyleDefinitionsExist(s_FooterFillStyle))
184 deactivateOldFillStyleDefinitions(getFooterSet());
187 m_bIsFillStyleAlreadyConverted = true;
190 //UUUU do not use XMLPropStyleContext::FillPropertySet, we need to handle this ourselves since
191 // we have properties which use the MID_FLAG_NO_PROPERTY_IMPORT flag since they need some special
192 // handling
193 rtl::Reference < SvXMLImportPropertyMapper > xImpPrMap = GetStyles()->GetImportPropertyMapper(GetFamily());
195 if(xImpPrMap.is())
197 // properties that need special handling because they need the used name to be translated first
198 struct _ContextID_Index_Pair aContextIDs[] =
200 { CTF_PM_FILLGRADIENTNAME, -1 },
201 { CTF_PM_FILLTRANSNAME, -1 },
202 { CTF_PM_FILLHATCHNAME, -1 },
203 { CTF_PM_FILLBITMAPNAME, -1 },
205 // also need to special handling for header entries
206 { CTF_PM_HEADERFILLGRADIENTNAME, -1 },
207 { CTF_PM_HEADERFILLTRANSNAME, -1 },
208 { CTF_PM_HEADERFILLHATCHNAME, -1 },
209 { CTF_PM_HEADERFILLBITMAPNAME, -1 },
211 // also need to special handling for footer entries
212 { CTF_PM_FOOTERFILLGRADIENTNAME, -1 },
213 { CTF_PM_FOOTERFILLTRANSNAME, -1 },
214 { CTF_PM_FOOTERFILLHATCHNAME, -1 },
215 { CTF_PM_FOOTERFILLBITMAPNAME, -1 },
217 {-1, -1}
220 // the style families associated with the same index modulo 4
221 static sal_uInt16 aFamilies[] =
223 XML_STYLE_FAMILY_SD_GRADIENT_ID,
224 XML_STYLE_FAMILY_SD_GRADIENT_ID,
225 XML_STYLE_FAMILY_SD_HATCH_ID,
226 XML_STYLE_FAMILY_SD_FILL_IMAGE_ID
229 //UUUU Fill PropertySet, but let it handle special properties not itself
230 xImpPrMap->FillPropertySet(GetProperties(), rPropSet, aContextIDs);
232 // get property set mapper
233 const rtl::Reference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
234 Reference< XPropertySetInfo > xInfo;
236 //UUUU handle special attributes which have MID_FLAG_NO_PROPERTY_IMPORT set
237 for(sal_uInt16 i = 0; aContextIDs[i].nContextID != -1; i++)
239 sal_Int32 nIndex = aContextIDs[i].nIndex;
241 if(nIndex != -1)
243 switch(aContextIDs[i].nContextID)
245 case CTF_PM_FILLGRADIENTNAME:
246 case CTF_PM_FILLTRANSNAME:
247 case CTF_PM_FILLHATCHNAME:
248 case CTF_PM_FILLBITMAPNAME:
250 case CTF_PM_HEADERFILLGRADIENTNAME:
251 case CTF_PM_HEADERFILLTRANSNAME:
252 case CTF_PM_HEADERFILLHATCHNAME:
253 case CTF_PM_HEADERFILLBITMAPNAME:
255 case CTF_PM_FOOTERFILLGRADIENTNAME:
256 case CTF_PM_FOOTERFILLTRANSNAME:
257 case CTF_PM_FOOTERFILLHATCHNAME:
258 case CTF_PM_FOOTERFILLBITMAPNAME:
260 struct XMLPropertyState& rState = GetProperties()[nIndex];
261 rtl::OUString sStyleName;
262 rState.maValue >>= sStyleName;
264 //UUUU translate the used name from ODF intern to the name used in the Model
265 sStyleName = GetImport().GetStyleDisplayName(aFamilies[i%4], sStyleName);
269 // set property
270 const rtl::OUString& rPropertyName = rMapper->GetEntryAPIName(rState.mnIndex);
272 if(!xInfo.is())
274 xInfo = rPropSet->getPropertySetInfo();
277 if(xInfo->hasPropertyByName(rPropertyName))
279 rPropSet->setPropertyValue(rPropertyName,Any(sStyleName));
282 catch(::com::sun::star::lang::IllegalArgumentException& e)
284 Sequence< rtl::OUString > aSeq(1);
285 aSeq[0] = sStyleName;
286 GetImport().SetError(
287 XMLERROR_STYLE_PROP_VALUE | XMLERROR_FLAG_WARNING,
288 aSeq,e.Message,NULL);
290 break;
296 else
298 OSL_ENSURE(xImpPrMap.is(), "Got no SvXMLImportPropertyMapper (!)");
301 //UUUU old code, replaced by above stuff
302 // XMLPropStyleContext::FillPropertySet(rPropSet);
304 if (!sPageUsage.isEmpty())
306 uno::Any aPageUsage;
307 XMLPMPropHdl_PageStyleLayout aPageUsageHdl;
308 if (aPageUsageHdl.importXML(sPageUsage, aPageUsage, GetImport().GetMM100UnitConverter()))
309 rPropSet->setPropertyValue("PageStyleLayout", aPageUsage);
313 // text grid enhancement for better CJK support
314 //set default page layout style
315 void PageStyleContext::SetDefaults( )
317 Reference < XMultiServiceFactory > xFactory ( GetImport().GetModel(), UNO_QUERY);
318 if (xFactory.is())
320 Reference < XInterface > xInt = xFactory->createInstance (
321 OUString ( "com.sun.star.text.Defaults" ) );
322 Reference < beans::XPropertySet > xProperties ( xInt, UNO_QUERY );
323 if ( xProperties.is() )
324 FillPropertySet ( xProperties );
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */