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 <com/sun/star/document/XEventsSupplier.hpp>
21 #include <com/sun/star/container/XNameReplace.hpp>
22 #include <com/sun/star/presentation/AnimationSpeed.hpp>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <tools/urlobj.hxx>
25 #include <osl/diagnose.h>
26 #include <sal/log.hxx>
27 #include <o3tl/string_view.hxx>
29 #include <sax/tools/converter.hxx>
31 #include <xmloff/xmltoken.hxx>
32 #include <xmloff/xmlimp.hxx>
33 #include <xmloff/xmlnamespace.hxx>
34 #include <xmloff/xmluconv.hxx>
35 #include <xmloff/namespacemap.hxx>
36 #include "eventimp.hxx"
38 using namespace ::cppu
;
39 using namespace ::com::sun::star
;
40 using namespace ::com::sun::star::xml
;
41 using namespace ::com::sun::star::xml::sax
;
42 using namespace ::com::sun::star::uno
;
43 using namespace ::com::sun::star::drawing
;
44 using namespace ::com::sun::star::beans
;
45 using namespace ::com::sun::star::lang
;
46 using namespace ::com::sun::star::document
;
47 using namespace ::com::sun::star::container
;
48 using namespace ::com::sun::star::presentation
;
49 using namespace ::xmloff::token
;
51 SvXMLEnumMapEntry
<ClickAction
> const aXML_EventActions_EnumMap
[] =
53 { XML_NONE
, ClickAction_NONE
},
54 { XML_PREVIOUS_PAGE
, ClickAction_PREVPAGE
},
55 { XML_NEXT_PAGE
, ClickAction_NEXTPAGE
},
56 { XML_FIRST_PAGE
, ClickAction_FIRSTPAGE
},
57 { XML_LAST_PAGE
, ClickAction_LASTPAGE
},
58 { XML_HIDE
, ClickAction_INVISIBLE
},
59 { XML_STOP
, ClickAction_STOPPRESENTATION
},
60 { XML_EXECUTE
, ClickAction_PROGRAM
},
61 { XML_SHOW
, ClickAction_BOOKMARK
},
62 { XML_SHOW
, ClickAction_DOCUMENT
},
63 { XML_EXECUTE_MACRO
, ClickAction_MACRO
},
64 { XML_VERB
, ClickAction_VERB
},
65 { XML_FADE_OUT
, ClickAction_VANISH
},
66 { XML_SOUND
, ClickAction_SOUND
},
67 { XML_TOKEN_INVALID
, ClickAction(0) }
70 SdXMLEventContextData::SdXMLEventContextData(const Reference
< XShape
>& rxShape
)
71 : mxShape(rxShape
), mbValid(false), mbScript(false)
72 , meClickAction(ClickAction_NONE
), meEffect(EK_none
)
73 , meDirection(ED_none
), mnStartScale(100), meSpeed(AnimationSpeed_MEDIUM
)
74 , mnVerb(0), mbPlayFull(false)
80 class SdXMLEventContext
: public SvXMLImportContext
83 SdXMLEventContextData maData
;
87 SdXMLEventContext( SvXMLImport
& rImport
, sal_Int32 nElement
, const Reference
< XFastAttributeList
>& xAttrList
, const Reference
< XShape
>& rxShape
);
89 virtual css::uno::Reference
< css::xml::sax::XFastContextHandler
> SAL_CALL
createFastChildContext(
91 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& AttrList
) override
;
92 virtual void SAL_CALL
endFastElement(sal_Int32 nElement
) override
;
95 class XMLEventSoundContext
: public SvXMLImportContext
99 XMLEventSoundContext( SvXMLImport
& rImport
, const Reference
< XFastAttributeList
>& xAttrList
, SdXMLEventContext
* pParent
);
104 XMLEventSoundContext::XMLEventSoundContext( SvXMLImport
& rImp
, const Reference
< XFastAttributeList
>& xAttrList
, SdXMLEventContext
* pParent
)
105 : SvXMLImportContext( rImp
)
107 for( auto& aIter
: sax_fastparser::castToFastAttributeList(xAttrList
) )
109 switch( aIter
.getToken() )
111 case XML_ELEMENT(XLINK
, XML_HREF
):
112 pParent
->maData
.msSoundURL
= rImp
.GetAbsoluteReference(aIter
.toString());
114 case XML_ELEMENT(PRESENTATION
, XML_PLAY_FULL
):
115 pParent
->maData
.mbPlayFull
= IsXMLToken( aIter
, XML_TRUE
);
118 XMLOFF_WARN_UNKNOWN("xmloff", aIter
);
123 SdXMLEventContext::SdXMLEventContext( SvXMLImport
& rImp
,
125 const Reference
< XFastAttributeList
>& xAttrList
, const Reference
< XShape
>& rxShape
)
126 : SvXMLImportContext(rImp
)
129 if( nElement
== XML_ELEMENT(PRESENTATION
, XML_EVENT_LISTENER
) )
131 maData
.mbValid
= true;
133 else if( nElement
== XML_ELEMENT(SCRIPT
, XML_EVENT_LISTENER
) )
135 maData
.mbScript
= true;
136 maData
.mbValid
= true;
145 for( auto& aIter
: sax_fastparser::castToFastAttributeList(xAttrList
) )
147 switch( aIter
.getToken() )
149 case XML_ELEMENT(PRESENTATION
, XML_ACTION
):
150 SvXMLUnitConverter::convertEnum( maData
.meClickAction
, aIter
.toView(), aXML_EventActions_EnumMap
);
152 case XML_ELEMENT(PRESENTATION
, XML_EFFECT
):
153 SvXMLUnitConverter::convertEnum( maData
.meEffect
, aIter
.toView(), aXML_AnimationEffect_EnumMap
);
155 case XML_ELEMENT(PRESENTATION
, XML_DIRECTION
):
156 SvXMLUnitConverter::convertEnum( maData
.meDirection
, aIter
.toView(), aXML_AnimationDirection_EnumMap
);
158 case XML_ELEMENT(PRESENTATION
, XML_START_SCALE
):
161 if (::sax::Converter::convertPercent( nScale
, aIter
.toView() ))
162 maData
.mnStartScale
= static_cast<sal_Int16
>(nScale
);
165 case XML_ELEMENT(PRESENTATION
, XML_SPEED
):
166 SvXMLUnitConverter::convertEnum( maData
.meSpeed
, aIter
.toView(), aXML_AnimationSpeed_EnumMap
);
168 case XML_ELEMENT(PRESENTATION
, XML_VERB
):
169 ::sax::Converter::convertNumber( maData
.mnVerb
, aIter
.toView() );
171 case XML_ELEMENT(SCRIPT
, XML_EVENT_NAME
):
173 sEventName
= aIter
.toString();
174 sal_uInt16 nScriptPrefix
=
175 GetImport().GetNamespaceMap().GetKeyByAttrValueQName(sEventName
, &sEventName
);
176 maData
.mbValid
= XML_NAMESPACE_DOM
== nScriptPrefix
&& sEventName
== "click";
179 case XML_ELEMENT(SCRIPT
, XML_LANGUAGE
):
181 // language is not evaluated!
182 OUString aScriptLanguage
;
183 maData
.msLanguage
= aIter
.toString();
184 sal_uInt16 nScriptPrefix
= rImp
.GetNamespaceMap().
185 GetKeyByAttrValueQName(maData
.msLanguage
, &aScriptLanguage
);
186 if( XML_NAMESPACE_OOO
== nScriptPrefix
)
187 maData
.msLanguage
= aScriptLanguage
;
190 case XML_ELEMENT(SCRIPT
, XML_MACRO_NAME
):
191 maData
.msMacroName
= aIter
.toString();
193 case XML_ELEMENT(XLINK
, XML_HREF
):
195 if ( maData
.mbScript
)
197 maData
.msMacroName
= aIter
.toString();
201 const OUString
&rTmp
=
202 rImp
.GetAbsoluteReference(aIter
.toString());
203 INetURLObject::translateToInternal( rTmp
, maData
.msBookmark
,
204 INetURLObject::DecodeMechanism::Unambiguous
);
212 maData
.mbValid
= !sEventName
.isEmpty();
214 if (!maData
.msMacroName
.isEmpty())
215 rImp
.NotifyMacroEventRead();
218 css::uno::Reference
< css::xml::sax::XFastContextHandler
> SdXMLEventContext::createFastChildContext(
220 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
222 if( nElement
== XML_ELEMENT(PRESENTATION
, XML_SOUND
) )
223 return new XMLEventSoundContext( GetImport(), xAttrList
, this );
225 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement
);
229 void SdXMLEventContext::endFastElement(sal_Int32
)
231 GetImport().GetShapeImport()->addShapeEvents(maData
);
234 void SdXMLEventContextData::ApplyProperties()
241 Reference
< XEventsSupplier
> xEventsSupplier( mxShape
, UNO_QUERY
);
242 if( !xEventsSupplier
.is() )
245 Reference
< XNameReplace
> xEvents( xEventsSupplier
->getEvents() );
246 SAL_WARN_IF( !xEvents
.is(), "xmloff", "XEventsSupplier::getEvents() returned NULL" );
250 OUString sAPIEventName
;
251 uno::Sequence
< beans::PropertyValue
> aProperties
;
253 sAPIEventName
= "OnClick";
256 meClickAction
= ClickAction_MACRO
;
258 sal_Int32 nPropertyCount
= 2;
259 switch( meClickAction
)
261 case ClickAction_NONE
:
262 case ClickAction_PREVPAGE
:
263 case ClickAction_NEXTPAGE
:
264 case ClickAction_FIRSTPAGE
:
265 case ClickAction_LASTPAGE
:
266 case ClickAction_INVISIBLE
:
267 case ClickAction_STOPPRESENTATION
:
269 case ClickAction_PROGRAM
:
270 case ClickAction_VERB
:
271 case ClickAction_BOOKMARK
:
272 case ClickAction_DOCUMENT
:
275 case ClickAction_MACRO
:
276 if ( msLanguage
.equalsIgnoreAsciiCase("starbasic") )
280 case ClickAction_SOUND
:
284 case ClickAction_VANISH
:
291 aProperties
.realloc( nPropertyCount
);
292 beans::PropertyValue
* pProperties
= aProperties
.getArray();
294 if( ClickAction_MACRO
== meClickAction
)
296 if ( msLanguage
.equalsIgnoreAsciiCase("starbasic") )
299 const OUString
& rApp
= GetXMLToken( XML_APPLICATION
);
300 const OUString
& rDoc
= GetXMLToken( XML_DOCUMENT
);
301 if( msMacroName
.getLength() > rApp
.getLength()+1 &&
302 o3tl::equalsIgnoreAsciiCase(msMacroName
.subView(0,rApp
.getLength()), rApp
) &&
303 ':' == msMacroName
[rApp
.getLength()] )
305 sLibrary
= "StarOffice";
306 msMacroName
= msMacroName
.copy( rApp
.getLength()+1 );
308 else if( msMacroName
.getLength() > rDoc
.getLength()+1 &&
309 o3tl::equalsIgnoreAsciiCase(msMacroName
.subView(0,rDoc
.getLength()), rDoc
) &&
310 ':' == msMacroName
[rDoc
.getLength()] )
313 msMacroName
= msMacroName
.copy( rDoc
.getLength()+1 );
316 pProperties
->Name
= "EventType";
317 pProperties
->Handle
= -1;
318 pProperties
->Value
<<= OUString( "StarBasic" );
319 pProperties
->State
= beans::PropertyState_DIRECT_VALUE
;
322 pProperties
->Name
= "MacroName";
323 pProperties
->Handle
= -1;
324 pProperties
->Value
<<= msMacroName
;
325 pProperties
->State
= beans::PropertyState_DIRECT_VALUE
;
328 pProperties
->Name
= "Library";
329 pProperties
->Handle
= -1;
330 pProperties
->Value
<<= sLibrary
;
331 pProperties
->State
= beans::PropertyState_DIRECT_VALUE
;
335 pProperties
->Name
= "EventType";
336 pProperties
->Handle
= -1;
337 pProperties
->Value
<<= OUString( "Script" );
338 pProperties
->State
= beans::PropertyState_DIRECT_VALUE
;
341 pProperties
->Name
= "Script";
342 pProperties
->Handle
= -1;
343 pProperties
->Value
<<= msMacroName
;
344 pProperties
->State
= beans::PropertyState_DIRECT_VALUE
;
349 pProperties
->Name
= "EventType";
350 pProperties
->Handle
= -1;
351 pProperties
->Value
<<= OUString( "Presentation" );
352 pProperties
->State
= beans::PropertyState_DIRECT_VALUE
;
355 // ClickAction_BOOKMARK and ClickAction_DOCUMENT share the same xml event
356 // so check here if it's really a bookmark or maybe a document
357 if( meClickAction
== ClickAction_BOOKMARK
)
359 if( !msBookmark
.startsWith( "#" ) )
360 meClickAction
= ClickAction_DOCUMENT
;
363 pProperties
->Name
= "ClickAction";
364 pProperties
->Handle
= -1;
365 pProperties
->Value
<<= meClickAction
;
366 pProperties
->State
= beans::PropertyState_DIRECT_VALUE
;
369 switch( meClickAction
)
371 case ClickAction_NONE
:
372 case ClickAction_PREVPAGE
:
373 case ClickAction_NEXTPAGE
:
374 case ClickAction_FIRSTPAGE
:
375 case ClickAction_LASTPAGE
:
376 case ClickAction_INVISIBLE
:
377 case ClickAction_STOPPRESENTATION
:
380 case ClickAction_BOOKMARK
:
381 msBookmark
= msBookmark
.copy(1);
385 case ClickAction_DOCUMENT
:
386 case ClickAction_PROGRAM
:
387 pProperties
->Name
= "Bookmark";
388 pProperties
->Handle
= -1;
389 pProperties
->Value
<<= msBookmark
;
390 pProperties
->State
= beans::PropertyState_DIRECT_VALUE
;
393 case ClickAction_VANISH
:
394 pProperties
->Name
= "Effect";
395 pProperties
->Handle
= -1;
396 pProperties
->Value
<<= ImplSdXMLgetEffect( meEffect
, meDirection
, mnStartScale
, true );
397 pProperties
->State
= beans::PropertyState_DIRECT_VALUE
;
400 pProperties
->Name
= "Speed";
401 pProperties
->Handle
= -1;
402 pProperties
->Value
<<= meSpeed
;
403 pProperties
->State
= beans::PropertyState_DIRECT_VALUE
;
408 case ClickAction_SOUND
:
409 pProperties
->Name
= "SoundURL";
410 pProperties
->Handle
= -1;
411 pProperties
->Value
<<= msSoundURL
;
412 pProperties
->State
= beans::PropertyState_DIRECT_VALUE
;
415 pProperties
->Name
= "PlayFull";
416 pProperties
->Handle
= -1;
417 pProperties
->Value
<<= mbPlayFull
;
418 pProperties
->State
= beans::PropertyState_DIRECT_VALUE
;
421 case ClickAction_VERB
:
422 pProperties
->Name
= "Verb";
423 pProperties
->Handle
= -1;
424 pProperties
->Value
<<= mnVerb
;
425 pProperties
->State
= beans::PropertyState_DIRECT_VALUE
;
427 case ClickAction_MACRO
:
428 OSL_FAIL("xmloff::SdXMLEventContext::EndElement(), ClickAction_MACRO must be handled in different if case");
434 xEvents
->replaceByName( sAPIEventName
, uno::Any( aProperties
) );
440 SdXMLEventsContext::SdXMLEventsContext( SvXMLImport
& rImport
, const Reference
< XShape
>& rxShape
)
441 : SvXMLImportContext(rImport
), mxShape( rxShape
)
445 SdXMLEventsContext::~SdXMLEventsContext()
449 css::uno::Reference
< css::xml::sax::XFastContextHandler
> SdXMLEventsContext::createFastChildContext(
451 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
453 return new SdXMLEventContext( GetImport(), nElement
, xAttrList
, mxShape
);
456 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */