Bump version to 24.04.3.4
[LibreOffice.git] / xmloff / source / draw / eventimp.cxx
blob2a6ce1fa23811d4c33a5276a19a9d621028cd3a4
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 <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)
78 namespace {
80 class SdXMLEventContext : public SvXMLImportContext
82 public:
83 SdXMLEventContextData maData;
85 public:
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(
90 sal_Int32 nElement,
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
97 public:
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());
113 break;
114 case XML_ELEMENT(PRESENTATION, XML_PLAY_FULL):
115 pParent->maData.mbPlayFull = IsXMLToken( aIter, XML_TRUE );
116 break;
117 default:
118 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
123 SdXMLEventContext::SdXMLEventContext( SvXMLImport& rImp,
124 sal_Int32 nElement,
125 const Reference< XFastAttributeList >& xAttrList, const Reference< XShape >& rxShape )
126 : SvXMLImportContext(rImp)
127 , maData(rxShape)
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;
138 else
140 return;
143 // read attributes
144 OUString sEventName;
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 );
151 break;
152 case XML_ELEMENT(PRESENTATION, XML_EFFECT):
153 SvXMLUnitConverter::convertEnum( maData.meEffect, aIter.toView(), aXML_AnimationEffect_EnumMap );
154 break;
155 case XML_ELEMENT(PRESENTATION, XML_DIRECTION):
156 SvXMLUnitConverter::convertEnum( maData.meDirection, aIter.toView(), aXML_AnimationDirection_EnumMap );
157 break;
158 case XML_ELEMENT(PRESENTATION, XML_START_SCALE):
160 sal_Int32 nScale;
161 if (::sax::Converter::convertPercent( nScale, aIter.toView() ))
162 maData.mnStartScale = static_cast<sal_Int16>(nScale);
164 break;
165 case XML_ELEMENT(PRESENTATION, XML_SPEED):
166 SvXMLUnitConverter::convertEnum( maData.meSpeed, aIter.toView(), aXML_AnimationSpeed_EnumMap );
167 break;
168 case XML_ELEMENT(PRESENTATION, XML_VERB):
169 ::sax::Converter::convertNumber( maData.mnVerb, aIter.toView() );
170 break;
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";
178 break;
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;
189 break;
190 case XML_ELEMENT(SCRIPT, XML_MACRO_NAME):
191 maData.msMacroName = aIter.toString();
192 break;
193 case XML_ELEMENT(XLINK, XML_HREF):
195 if ( maData.mbScript )
197 maData.msMacroName = aIter.toString();
199 else
201 const OUString &rTmp =
202 rImp.GetAbsoluteReference(aIter.toString());
203 INetURLObject::translateToInternal( rTmp, maData.msBookmark,
204 INetURLObject::DecodeMechanism::Unambiguous );
207 break;
211 if( maData.mbValid )
212 maData.mbValid = !sEventName.isEmpty();
214 if (!maData.msMacroName.isEmpty())
215 rImp.NotifyMacroEventRead();
218 css::uno::Reference< css::xml::sax::XFastContextHandler > SdXMLEventContext::createFastChildContext(
219 sal_Int32 nElement,
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 );
224 else
225 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
226 return nullptr;
229 void SdXMLEventContext::endFastElement(sal_Int32 )
231 GetImport().GetShapeImport()->addShapeEvents(maData);
234 void SdXMLEventContextData::ApplyProperties()
236 if( !mbValid )
237 return;
241 Reference< XEventsSupplier > xEventsSupplier( mxShape, UNO_QUERY );
242 if( !xEventsSupplier.is() )
243 break;
245 Reference< XNameReplace > xEvents( xEventsSupplier->getEvents() );
246 SAL_WARN_IF( !xEvents.is(), "xmloff", "XEventsSupplier::getEvents() returned NULL" );
247 if( !xEvents.is() )
248 break;
250 OUString sAPIEventName;
251 uno::Sequence< beans::PropertyValue > aProperties;
253 sAPIEventName = "OnClick";
255 if( mbScript )
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:
268 break;
269 case ClickAction_PROGRAM:
270 case ClickAction_VERB:
271 case ClickAction_BOOKMARK:
272 case ClickAction_DOCUMENT:
273 nPropertyCount += 1;
274 break;
275 case ClickAction_MACRO:
276 if ( msLanguage.equalsIgnoreAsciiCase("starbasic") )
277 nPropertyCount += 1;
278 break;
280 case ClickAction_SOUND:
281 nPropertyCount += 2;
282 break;
284 case ClickAction_VANISH:
285 nPropertyCount += 4;
286 break;
287 default:
288 break;
291 aProperties.realloc( nPropertyCount );
292 beans::PropertyValue* pProperties = aProperties.getArray();
294 if( ClickAction_MACRO == meClickAction )
296 if ( msLanguage.equalsIgnoreAsciiCase("starbasic") )
298 OUString sLibrary;
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()] )
312 sLibrary = rDoc;
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;
320 pProperties++;
322 pProperties->Name = "MacroName";
323 pProperties->Handle = -1;
324 pProperties->Value <<= msMacroName;
325 pProperties->State = beans::PropertyState_DIRECT_VALUE;
326 pProperties++;
328 pProperties->Name = "Library";
329 pProperties->Handle = -1;
330 pProperties->Value <<= sLibrary;
331 pProperties->State = beans::PropertyState_DIRECT_VALUE;
333 else
335 pProperties->Name = "EventType";
336 pProperties->Handle = -1;
337 pProperties->Value <<= OUString( "Script" );
338 pProperties->State = beans::PropertyState_DIRECT_VALUE;
339 pProperties++;
341 pProperties->Name = "Script";
342 pProperties->Handle = -1;
343 pProperties->Value <<= msMacroName;
344 pProperties->State = beans::PropertyState_DIRECT_VALUE;
347 else
349 pProperties->Name = "EventType";
350 pProperties->Handle = -1;
351 pProperties->Value <<= OUString( "Presentation" );
352 pProperties->State = beans::PropertyState_DIRECT_VALUE;
353 pProperties++;
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;
367 pProperties++;
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:
378 break;
380 case ClickAction_BOOKMARK:
381 msBookmark = msBookmark.copy(1);
383 [[fallthrough]];
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;
391 break;
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;
398 pProperties++;
400 pProperties->Name = "Speed";
401 pProperties->Handle = -1;
402 pProperties->Value <<= meSpeed;
403 pProperties->State = beans::PropertyState_DIRECT_VALUE;
404 pProperties++;
406 [[fallthrough]];
408 case ClickAction_SOUND:
409 pProperties->Name = "SoundURL";
410 pProperties->Handle = -1;
411 pProperties->Value <<= msSoundURL;
412 pProperties->State = beans::PropertyState_DIRECT_VALUE;
413 pProperties++;
415 pProperties->Name = "PlayFull";
416 pProperties->Handle = -1;
417 pProperties->Value <<= mbPlayFull;
418 pProperties->State = beans::PropertyState_DIRECT_VALUE;
419 break;
421 case ClickAction_VERB:
422 pProperties->Name = "Verb";
423 pProperties->Handle = -1;
424 pProperties->Value <<= mnVerb;
425 pProperties->State = beans::PropertyState_DIRECT_VALUE;
426 break;
427 case ClickAction_MACRO:
428 OSL_FAIL("xmloff::SdXMLEventContext::EndElement(), ClickAction_MACRO must be handled in different if case");
429 break;
430 default:
431 break;
434 xEvents->replaceByName( sAPIEventName, uno::Any( aProperties ) );
436 } while(false);
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(
450 sal_Int32 nElement,
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: */