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 "eventimport.hxx"
21 #include <com/sun/star/script/XEventAttacherManager.hpp>
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <osl/diagnose.h>
24 #include "strings.hxx"
29 using namespace ::com::sun::star::uno
;
30 using namespace ::com::sun::star::beans
;
31 using namespace ::com::sun::star::script
;
32 using namespace ::com::sun::star::container
;
34 //= OFormEventsImportContext
35 OFormEventsImportContext::OFormEventsImportContext(SvXMLImport
& _rImport
, sal_uInt16 _nPrefix
, const OUString
& _rLocalName
, IEventAttacher
& _rEventAttacher
)
36 :XMLEventsImportContext(_rImport
, _nPrefix
, _rLocalName
)
37 ,m_rEventAttacher(_rEventAttacher
)
41 void OFormEventsImportContext::EndElement()
43 Sequence
< ScriptEventDescriptor
> aTranslated(aCollectEvents
.size());
44 ScriptEventDescriptor
* pTranslated
= aTranslated
.getArray();
46 // loop through the collected events and translate them
47 sal_Int32 nSeparatorPos
= -1;
48 for ( EventsVector::const_iterator aEvent
= aCollectEvents
.begin();
49 aEvent
!= aCollectEvents
.end();
50 ++aEvent
, ++pTranslated
53 // the name of the event is built from ListenerType::EventMethod
54 nSeparatorPos
= aEvent
->first
.indexOf(EVENT_NAME_SEPARATOR
);
55 OSL_ENSURE(-1 != nSeparatorPos
, "OFormEventsImportContext::EndElement: invalid (unrecognized) event name!");
56 pTranslated
->ListenerType
= aEvent
->first
.copy(0, nSeparatorPos
);
57 pTranslated
->EventMethod
= aEvent
->first
.copy(nSeparatorPos
+ sizeof(EVENT_NAME_SEPARATOR
) - 1);
61 // the local macro name and the event type are specified as properties
62 const PropertyValue
* pEventDescription
= aEvent
->second
.getConstArray();
63 const PropertyValue
* pEventDescriptionEnd
= pEventDescription
+ aEvent
->second
.getLength();
64 for (;pEventDescription
!= pEventDescriptionEnd
; ++pEventDescription
)
66 if (pEventDescription
->Name
== EVENT_LOCALMACRONAME
||
67 pEventDescription
->Name
== EVENT_SCRIPTURL
)
68 pEventDescription
->Value
>>= pTranslated
->ScriptCode
;
69 else if (pEventDescription
->Name
== EVENT_TYPE
)
70 pEventDescription
->Value
>>= pTranslated
->ScriptType
;
71 else if (pEventDescription
->Name
== EVENT_LIBRARY
)
72 pEventDescription
->Value
>>= sLibrary
;
75 if (pTranslated
->ScriptType
== EVENT_STARBASIC
)
77 if (sLibrary
== EVENT_STAROFFICE
)
78 sLibrary
= EVENT_APPLICATION
;
80 if ( !sLibrary
.isEmpty() )
82 // for StarBasic, the library is prepended
83 sal_Unicode cLibSeparator
= ':';
84 sLibrary
+= OUString( &cLibSeparator
, 1 );
86 sLibrary
+= pTranslated
->ScriptCode
;
87 pTranslated
->ScriptCode
= sLibrary
;
91 // register the events
92 m_rEventAttacher
.registerEvents(aTranslated
);
94 XMLEventsImportContext::EndElement();
97 //= ODefaultEventAttacherManager
99 ODefaultEventAttacherManager::~ODefaultEventAttacherManager()
103 void ODefaultEventAttacherManager::registerEvents(const Reference
< XPropertySet
>& _rxElement
,
104 const Sequence
< ScriptEventDescriptor
>& _rEvents
)
106 OSL_ENSURE(m_aEvents
.end() == m_aEvents
.find(_rxElement
),
107 "ODefaultEventAttacherManager::registerEvents: already have events for this object!");
108 // for the moment, only remember the script events
109 m_aEvents
[_rxElement
] = _rEvents
;
112 void ODefaultEventAttacherManager::setEvents(const Reference
< XIndexAccess
>& _rxContainer
)
114 Reference
< XEventAttacherManager
> xEventManager(_rxContainer
, UNO_QUERY
);
115 if (!xEventManager
.is())
117 OSL_FAIL("ODefaultEventAttacherManager::setEvents: invalid argument!");
121 // loop through all elements
122 sal_Int32 nCount
= _rxContainer
->getCount();
123 Reference
< XPropertySet
> xCurrent
;
124 MapPropertySet2ScriptSequence::const_iterator aRegisteredEventsPos
;
125 for (sal_Int32 i
=0; i
<nCount
; ++i
)
127 xCurrent
.set(_rxContainer
->getByIndex(i
), css::uno::UNO_QUERY
);
130 aRegisteredEventsPos
= m_aEvents
.find(xCurrent
);
131 if (m_aEvents
.end() != aRegisteredEventsPos
)
132 xEventManager
->registerScriptEvents(i
, aRegisteredEventsPos
->second
);
137 } // namespace xmloff
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */