1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/xmlimppr.hxx>
22 #include <xmloff/xmlnamespace.hxx>
23 #include <xmloff/xmlprmap.hxx>
24 #include <xmloff/xmltoken.hxx>
25 #include "PageMasterPropHdl.hxx"
26 #include "PagePropertySetContext.hxx"
27 #include "PageHeaderFooterContext.hxx"
28 #include <PageMasterStyleMap.hxx>
29 #include <com/sun/star/frame/XModel.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <osl/diagnose.h>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/beans/XPropertySetInfo.hpp>
36 #include <xmloff/xmlerror.hxx>
37 #include <xmloff/XMLTextMasterPageContext.hxx>
39 using namespace ::com::sun::star
;
40 using namespace ::xmloff::token
;
41 using namespace ::com::sun::star::uno
;
42 using namespace ::com::sun::star::lang
;
45 using namespace ::com::sun::star::beans
;
47 void PageStyleContext::SetAttribute( sal_uInt16 nPrefixKey
,
48 const OUString
& rLocalName
,
49 const OUString
& rValue
)
51 // TODO: use a map here
52 if( XML_NAMESPACE_STYLE
== nPrefixKey
&& IsXMLToken( rLocalName
, XML_PAGE_USAGE
) )
58 XMLPropStyleContext::SetAttribute( nPrefixKey
, rLocalName
, rValue
);
63 PageStyleContext::PageStyleContext( SvXMLImport
& rImport
,
64 SvXMLStylesContext
& rStyles
,
66 XMLPropStyleContext( rImport
, rStyles
, XmlStyleFamily::PAGE_MASTER
, bDefaultStyle
),
68 m_bIsFillStyleAlreadyConverted(false) //
72 PageStyleContext::~PageStyleContext()
76 css::uno::Reference
< css::xml::sax::XFastContextHandler
> PageStyleContext::createFastChildContext(
78 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
80 if( nElement
== XML_ELEMENT(STYLE
, XML_HEADER_STYLE
) ||
81 nElement
== XML_ELEMENT(STYLE
, XML_FOOTER_STYLE
) )
83 bool bHeader
= nElement
== XML_ELEMENT(STYLE
, XML_HEADER_STYLE
);
84 rtl::Reference
< SvXMLImportPropertyMapper
> xImpPrMap
=
85 GetStyles()->GetImportPropertyMapper( GetFamily() );
88 const rtl::Reference
< XMLPropertySetMapper
>& rMapper
= xImpPrMap
->getPropertySetMapper();
91 nFlag
= CTF_PM_HEADERFLAG
;
93 nFlag
= CTF_PM_FOOTERFLAG
;
94 sal_Int32
nStartIndex (-1);
95 sal_Int32
nEndIndex (-1);
99 while ( nIndex
< rMapper
->GetEntryCount() && !bEnd
)
101 if ((rMapper
->GetEntryContextId( nIndex
) & CTF_PM_FLAGMASK
) == nFlag
)
106 nStartIndex
= nIndex
;
118 return new PageHeaderFooterContext(GetImport(),
119 GetProperties(), xImpPrMap
, nStartIndex
, nEndIndex
, bHeader
);
123 if( nElement
== XML_ELEMENT(STYLE
, XML_PAGE_LAYOUT_PROPERTIES
) )
125 rtl::Reference
< SvXMLImportPropertyMapper
> xImpPrMap
=
126 GetStyles()->GetImportPropertyMapper( GetFamily() );
129 const rtl::Reference
< XMLPropertySetMapper
>& rMapper
= xImpPrMap
->getPropertySetMapper();
130 sal_Int32
nEndIndex (-1);
132 sal_Int32 nIndex
= 0;
133 sal_Int16 nContextID
;
134 while ( nIndex
< rMapper
->GetEntryCount() && !bEnd
)
136 nContextID
= rMapper
->GetEntryContextId( nIndex
);
137 if (nContextID
&& ((nContextID
& CTF_PM_FLAGMASK
) != XML_PM_CTF_START
))
146 return new PagePropertySetContext( GetImport(), nElement
,
148 XML_TYPE_PROP_PAGE_LAYOUT
,
150 xImpPrMap
, 0, nEndIndex
, Page
);
154 return XMLPropStyleContext::createFastChildContext(nElement
, xAttrList
);
157 void PageStyleContext::FillPropertySet(const uno::Reference
<beans::XPropertySet
> &)
159 assert(false); // don't call this virtual, call function below
162 void PageStyleContext::FillPropertySet_PageStyle(
163 const uno::Reference
<beans::XPropertySet
> & rPropSet
,
164 XMLPropStyleContext
*const pDrawingPageStyle
)
166 // need to filter out old fill definitions when the new ones are used. The new
167 // ones are used when a FillStyle is defined
168 if(!m_bIsFillStyleAlreadyConverted
&& !GetProperties().empty())
170 static OUString
s_FillStyle("FillStyle");
171 static OUString
s_HeaderFillStyle("HeaderFillStyle");
172 static OUString
s_FooterFillStyle("FooterFillStyle");
174 // note: the function must only check by property name, not any id/flag!
175 if (doNewDrawingLayerFillStyleDefinitionsExist(s_FillStyle
)
176 || (pDrawingPageStyle
&& pDrawingPageStyle
->doNewDrawingLayerFillStyleDefinitionsExist(s_FillStyle
)))
178 deactivateOldFillStyleDefinitions(getStandardSet());
181 if(doNewDrawingLayerFillStyleDefinitionsExist(s_HeaderFillStyle
))
183 deactivateOldFillStyleDefinitions(getHeaderSet());
186 if(doNewDrawingLayerFillStyleDefinitionsExist(s_FooterFillStyle
))
188 deactivateOldFillStyleDefinitions(getFooterSet());
191 m_bIsFillStyleAlreadyConverted
= true;
194 // do not use XMLPropStyleContext::FillPropertySet, we need to handle this ourselves since
195 // we have properties which use the MID_FLAG_NO_PROPERTY_IMPORT flag since they need some special
197 rtl::Reference
< SvXMLImportPropertyMapper
> xImpPrMap
= GetStyles()->GetImportPropertyMapper(GetFamily());
201 // properties that need special handling because they need the used name to be translated first
202 struct ContextID_Index_Pair aContextIDs
[] =
204 { CTF_PM_FILLGRADIENTNAME
, -1 },
205 { CTF_PM_FILLTRANSNAME
, -1 },
206 { CTF_PM_FILLHATCHNAME
, -1 },
207 { CTF_PM_FILLBITMAPNAME
, -1 },
209 // also need to special handling for header entries
210 { CTF_PM_HEADERFILLGRADIENTNAME
, -1 },
211 { CTF_PM_HEADERFILLTRANSNAME
, -1 },
212 { CTF_PM_HEADERFILLHATCHNAME
, -1 },
213 { CTF_PM_HEADERFILLBITMAPNAME
, -1 },
215 // also need to special handling for footer entries
216 { CTF_PM_FOOTERFILLGRADIENTNAME
, -1 },
217 { CTF_PM_FOOTERFILLTRANSNAME
, -1 },
218 { CTF_PM_FOOTERFILLHATCHNAME
, -1 },
219 { CTF_PM_FOOTERFILLBITMAPNAME
, -1 },
224 // the style families associated with the same index modulo 4
225 static const XmlStyleFamily aFamilies
[] =
227 XmlStyleFamily::SD_GRADIENT_ID
,
228 XmlStyleFamily::SD_GRADIENT_ID
,
229 XmlStyleFamily::SD_HATCH_ID
,
230 XmlStyleFamily::SD_FILL_IMAGE_ID
233 // Fill PropertySet, but let it handle special properties not itself
234 xImpPrMap
->FillPropertySet(GetProperties(), rPropSet
, aContextIDs
);
236 // get property set mapper
237 const rtl::Reference
< XMLPropertySetMapper
>& rMapper
= xImpPrMap
->getPropertySetMapper();
238 Reference
< XPropertySetInfo
> xInfo
;
240 // handle special attributes which have MID_FLAG_NO_PROPERTY_IMPORT set
241 for(sal_uInt16 i
= 0; aContextIDs
[i
].nContextID
!= -1; i
++)
243 sal_Int32 nIndex
= aContextIDs
[i
].nIndex
;
247 switch(aContextIDs
[i
].nContextID
)
249 case CTF_PM_FILLGRADIENTNAME
:
250 case CTF_PM_FILLTRANSNAME
:
251 case CTF_PM_FILLHATCHNAME
:
252 case CTF_PM_FILLBITMAPNAME
:
254 case CTF_PM_HEADERFILLGRADIENTNAME
:
255 case CTF_PM_HEADERFILLTRANSNAME
:
256 case CTF_PM_HEADERFILLHATCHNAME
:
257 case CTF_PM_HEADERFILLBITMAPNAME
:
259 case CTF_PM_FOOTERFILLGRADIENTNAME
:
260 case CTF_PM_FOOTERFILLTRANSNAME
:
261 case CTF_PM_FOOTERFILLHATCHNAME
:
262 case CTF_PM_FOOTERFILLBITMAPNAME
:
264 struct XMLPropertyState
& rState
= GetProperties()[nIndex
];
266 rState
.maValue
>>= sStyleName
;
268 // translate the used name from ODF intern to the name used in the Model
269 sStyleName
= GetImport().GetStyleDisplayName(aFamilies
[i
%4], sStyleName
);
274 const OUString
& rPropertyName
= rMapper
->GetEntryAPIName(rState
.mnIndex
);
278 xInfo
= rPropSet
->getPropertySetInfo();
281 if(xInfo
->hasPropertyByName(rPropertyName
))
283 rPropSet
->setPropertyValue(rPropertyName
,Any(sStyleName
));
286 catch(css::lang::IllegalArgumentException
& e
)
288 Sequence
<OUString
> aSeq
{ sStyleName
};
289 GetImport().SetError(
290 XMLERROR_STYLE_PROP_VALUE
| XMLERROR_FLAG_WARNING
,
291 aSeq
,e
.Message
,nullptr);
301 OSL_ENSURE(xImpPrMap
.is(), "Got no SvXMLImportPropertyMapper (!)");
304 // pDrawingPageStyle overrides this
305 if (pDrawingPageStyle
)
307 pDrawingPageStyle
->FillPropertySet(rPropSet
);
310 // old code, replaced by above stuff
311 // XMLPropStyleContext::FillPropertySet(rPropSet);
313 if (!sPageUsage
.isEmpty())
316 XMLPMPropHdl_PageStyleLayout aPageUsageHdl
;
317 if (aPageUsageHdl
.importXML(sPageUsage
, aPageUsage
, GetImport().GetMM100UnitConverter()))
318 rPropSet
->setPropertyValue("PageStyleLayout", aPageUsage
);
322 extern ContextID_Index_Pair
const g_MasterPageContextIDs
[] =
324 { CTF_PM_FILLGRADIENTNAME
, -1 },
325 { CTF_PM_FILLTRANSNAME
, -1 },
326 { CTF_PM_FILLHATCHNAME
, -1 },
327 { CTF_PM_FILLBITMAPNAME
, -1 },
332 extern XmlStyleFamily
const g_MasterPageFamilies
[] =
334 XmlStyleFamily::SD_GRADIENT_ID
,
335 XmlStyleFamily::SD_GRADIENT_ID
,
336 XmlStyleFamily::SD_HATCH_ID
,
337 XmlStyleFamily::SD_FILL_IMAGE_ID
340 // text grid enhancement for better CJK support
341 //set default page layout style
342 void PageStyleContext::SetDefaults( )
344 Reference
< XMultiServiceFactory
> xFactory ( GetImport().GetModel(), UNO_QUERY
);
347 Reference
< XInterface
> xInt
= xFactory
->createInstance( "com.sun.star.text.Defaults" );
348 Reference
< beans::XPropertySet
> xProperties ( xInt
, UNO_QUERY
);
349 if ( xProperties
.is() )
350 FillPropertySet_PageStyle(xProperties
, nullptr);
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */