Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "eventhandler.hxx"
22 : #include "propctrlr.hrc"
23 : #include "formbrowsertools.hxx"
24 : #include "formresid.hrc"
25 : #include "formstrings.hxx"
26 : #include "handlerhelper.hxx"
27 : #include "modulepcr.hxx"
28 : #include "pcrcommon.hxx"
29 : #include "pcrstrings.hxx"
30 : #include "propertycontrolextender.hxx"
31 :
32 : #include <com/sun/star/awt/XTabControllerModel.hpp>
33 : #include <com/sun/star/beans/PropertyAttribute.hpp>
34 : #include <com/sun/star/beans/UnknownPropertyException.hpp>
35 : #include <com/sun/star/beans/Introspection.hpp>
36 : #include <com/sun/star/beans/XIntrospectionAccess.hpp>
37 : #include <com/sun/star/container/NoSuchElementException.hpp>
38 : #include <com/sun/star/container/XChild.hpp>
39 : #include <com/sun/star/container/XIndexAccess.hpp>
40 : #include <com/sun/star/container/XNameContainer.hpp>
41 : #include <com/sun/star/container/XNameReplace.hpp>
42 : #include <com/sun/star/form/FormComponentType.hpp>
43 : #include <com/sun/star/form/XForm.hpp>
44 : #include <com/sun/star/form/runtime/XFormController.hpp>
45 : #include <com/sun/star/inspection/PropertyControlType.hpp>
46 : #include <com/sun/star/lang/NullPointerException.hpp>
47 : #include <com/sun/star/script/XEventAttacherManager.hpp>
48 : #include <com/sun/star/script/XScriptEventsSupplier.hpp>
49 : #include <com/sun/star/util/XModifiable.hpp>
50 : #include <com/sun/star/uri/UriReferenceFactory.hpp>
51 : #include <com/sun/star/uri/XVndSunStarScriptUrlReference.hpp>
52 :
53 : #include <comphelper/namedvaluecollection.hxx>
54 : #include <comphelper/evtmethodhelper.hxx>
55 : #include <comphelper/types.hxx>
56 : #include <cppuhelper/implbase1.hxx>
57 : #include <rtl/ref.hxx>
58 : #include <rtl/ustrbuf.hxx>
59 : #include <sfx2/app.hxx>
60 : #include <svl/eitem.hxx>
61 : #include <svl/itemset.hxx>
62 : #include <svx/svxdlg.hxx>
63 : #include <svx/svxids.hrc>
64 : #include <tools/diagnose_ex.h>
65 : #include <vcl/msgbox.hxx>
66 :
67 : #include <map>
68 : #include <algorithm>
69 : #include <o3tl/compat_functional.hxx>
70 :
71 : //------------------------------------------------------------------------
72 0 : extern "C" void SAL_CALL createRegistryInfo_EventHandler()
73 : {
74 0 : ::pcr::OAutoRegistration< ::pcr::EventHandler > aAutoRegistration;
75 0 : }
76 :
77 : //........................................................................
78 : namespace pcr
79 : {
80 : //........................................................................
81 :
82 : /** === begin UNO using === **/
83 : using ::com::sun::star::uno::Reference;
84 : using ::com::sun::star::uno::XComponentContext;
85 : using ::com::sun::star::beans::XPropertySet;
86 : using ::com::sun::star::uno::Any;
87 : using ::com::sun::star::uno::TypeClass_STRING;
88 : using ::com::sun::star::uno::Type;
89 : using ::com::sun::star::beans::Introspection;
90 : using ::com::sun::star::beans::XPropertyChangeListener;
91 : using ::com::sun::star::beans::Property;
92 : using ::com::sun::star::beans::PropertyState;
93 : using ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
94 : using ::com::sun::star::uno::Sequence;
95 : using ::com::sun::star::script::ScriptEventDescriptor;
96 : using ::com::sun::star::script::XScriptEventsSupplier;
97 : using ::com::sun::star::lang::NullPointerException;
98 : using ::com::sun::star::uno::Exception;
99 : using ::com::sun::star::container::XChild;
100 : using ::com::sun::star::container::XIndexAccess;
101 : using ::com::sun::star::script::XEventAttacherManager;
102 : using ::com::sun::star::uno::UNO_QUERY;
103 : using ::com::sun::star::uno::UNO_QUERY_THROW;
104 : using ::com::sun::star::uno::XInterface;
105 : using ::com::sun::star::beans::XIntrospection;
106 : using ::com::sun::star::beans::XIntrospectionAccess;
107 : using ::com::sun::star::container::XNameContainer;
108 : using ::com::sun::star::awt::XTabControllerModel;
109 : using ::com::sun::star::form::XForm;
110 : using ::com::sun::star::form::runtime::XFormController;
111 : using ::com::sun::star::beans::UnknownPropertyException;
112 : using ::com::sun::star::uno::makeAny;
113 : using ::com::sun::star::container::NoSuchElementException;
114 : using ::com::sun::star::beans::XPropertySetInfo;
115 : using ::com::sun::star::container::XNameReplace;
116 : using ::com::sun::star::lang::IllegalArgumentException;
117 : using ::com::sun::star::lang::WrappedTargetException;
118 : using ::com::sun::star::uno::RuntimeException;
119 : using ::com::sun::star::beans::PropertyValue;
120 : using ::com::sun::star::inspection::LineDescriptor;
121 : using ::com::sun::star::inspection::XPropertyControlFactory;
122 : using ::com::sun::star::inspection::InteractiveSelectionResult;
123 : using ::com::sun::star::inspection::InteractiveSelectionResult_Cancelled;
124 : using ::com::sun::star::inspection::InteractiveSelectionResult_Success;
125 : using ::com::sun::star::inspection::XObjectInspectorUI;
126 : using ::com::sun::star::util::XModifiable;
127 : using ::com::sun::star::beans::PropertyChangeEvent;
128 : using ::com::sun::star::frame::XFrame;
129 : using ::com::sun::star::frame::XModel;
130 : using ::com::sun::star::frame::XController;
131 : using ::com::sun::star::uno::UNO_SET_THROW;
132 : using com::sun::star::uri::UriReferenceFactory;
133 : using com::sun::star::uri::XUriReferenceFactory;
134 : using com::sun::star::uri::XVndSunStarScriptUrlReference;
135 : using ::com::sun::star::lang::XEventListener;
136 : /** === end UNO using === **/
137 : namespace PropertyControlType = ::com::sun::star::inspection::PropertyControlType;
138 : namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
139 : namespace FormComponentType = ::com::sun::star::form::FormComponentType;
140 :
141 : //====================================================================
142 : //= EventDescription
143 : //====================================================================
144 0 : EventDescription::EventDescription( EventId _nId, const sal_Char* _pListenerNamespaceAscii, const sal_Char* _pListenerClassAsciiName,
145 : const sal_Char* _pListenerMethodAsciiName, sal_uInt16 _nDisplayNameResId, const rtl::OString& _sHelpId, const rtl::OString& _sUniqueBrowseId )
146 : :sDisplayName( String( PcrRes( _nDisplayNameResId ) ) )
147 : ,sListenerMethodName( ::rtl::OUString::createFromAscii( _pListenerMethodAsciiName ) )
148 : ,sHelpId( _sHelpId )
149 : ,sUniqueBrowseId( _sUniqueBrowseId )
150 0 : ,nId( _nId )
151 : {
152 0 : ::rtl::OUStringBuffer aQualifiedListenerClass;
153 0 : aQualifiedListenerClass.appendAscii( "com.sun.star." );
154 0 : aQualifiedListenerClass.appendAscii( _pListenerNamespaceAscii );
155 0 : aQualifiedListenerClass.appendAscii( "." );
156 0 : aQualifiedListenerClass.appendAscii( _pListenerClassAsciiName );
157 0 : sListenerClassName = aQualifiedListenerClass.makeStringAndClear();
158 0 : }
159 :
160 : //========================================================================
161 : //= helper
162 : //========================================================================
163 : namespace
164 : {
165 : //....................................................................
166 : #define DESCRIBE_EVENT( asciinamespace, asciilistener, asciimethod, id_postfix ) \
167 : s_aKnownEvents.insert( EventMap::value_type( \
168 : ::rtl::OUString::createFromAscii( asciimethod ), \
169 : EventDescription( ++nEventId, asciinamespace, asciilistener, asciimethod, RID_STR_EVT_##id_postfix, HID_EVT_##id_postfix, UID_BRWEVT_##id_postfix ) ) )
170 :
171 : //....................................................................
172 0 : bool lcl_getEventDescriptionForMethod( const ::rtl::OUString& _rMethodName, EventDescription& _out_rDescription )
173 : {
174 0 : static EventMap s_aKnownEvents;
175 0 : if ( s_aKnownEvents.empty() )
176 : {
177 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
178 0 : if ( s_aKnownEvents.empty() )
179 : {
180 : static sal_Int32 nEventId = 0;
181 :
182 0 : DESCRIBE_EVENT( "form", "XApproveActionListener", "approveAction", APPROVEACTIONPERFORMED );
183 0 : DESCRIBE_EVENT( "awt", "XActionListener", "actionPerformed", ACTIONPERFORMED );
184 0 : DESCRIBE_EVENT( "form", "XChangeListener", "changed", CHANGED );
185 0 : DESCRIBE_EVENT( "awt", "XTextListener", "textChanged", TEXTCHANGED );
186 0 : DESCRIBE_EVENT( "awt", "XItemListener", "itemStateChanged", ITEMSTATECHANGED );
187 0 : DESCRIBE_EVENT( "awt", "XFocusListener", "focusGained", FOCUSGAINED );
188 0 : DESCRIBE_EVENT( "awt", "XFocusListener", "focusLost", FOCUSLOST );
189 0 : DESCRIBE_EVENT( "awt", "XKeyListener", "keyPressed", KEYTYPED );
190 0 : DESCRIBE_EVENT( "awt", "XKeyListener", "keyReleased", KEYUP );
191 0 : DESCRIBE_EVENT( "awt", "XMouseListener", "mouseEntered", MOUSEENTERED );
192 0 : DESCRIBE_EVENT( "awt", "XMouseMotionListener", "mouseDragged", MOUSEDRAGGED );
193 0 : DESCRIBE_EVENT( "awt", "XMouseMotionListener", "mouseMoved", MOUSEMOVED );
194 0 : DESCRIBE_EVENT( "awt", "XMouseListener", "mousePressed", MOUSEPRESSED );
195 0 : DESCRIBE_EVENT( "awt", "XMouseListener", "mouseReleased", MOUSERELEASED );
196 0 : DESCRIBE_EVENT( "awt", "XMouseListener", "mouseExited", MOUSEEXITED );
197 0 : DESCRIBE_EVENT( "form", "XResetListener", "approveReset", APPROVERESETTED );
198 0 : DESCRIBE_EVENT( "form", "XResetListener", "resetted", RESETTED );
199 0 : DESCRIBE_EVENT( "form", "XSubmitListener", "approveSubmit", SUBMITTED );
200 0 : DESCRIBE_EVENT( "form", "XUpdateListener", "approveUpdate", BEFOREUPDATE );
201 0 : DESCRIBE_EVENT( "form", "XUpdateListener", "updated", AFTERUPDATE );
202 0 : DESCRIBE_EVENT( "form", "XLoadListener", "loaded", LOADED );
203 0 : DESCRIBE_EVENT( "form", "XLoadListener", "reloading", RELOADING );
204 0 : DESCRIBE_EVENT( "form", "XLoadListener", "reloaded", RELOADED );
205 0 : DESCRIBE_EVENT( "form", "XLoadListener", "unloading", UNLOADING );
206 0 : DESCRIBE_EVENT( "form", "XLoadListener", "unloaded", UNLOADED );
207 0 : DESCRIBE_EVENT( "form", "XConfirmDeleteListener", "confirmDelete", CONFIRMDELETE );
208 0 : DESCRIBE_EVENT( "sdb", "XRowSetApproveListener", "approveRowChange", APPROVEROWCHANGE );
209 0 : DESCRIBE_EVENT( "sdbc", "XRowSetListener", "rowChanged", ROWCHANGE );
210 0 : DESCRIBE_EVENT( "sdb", "XRowSetApproveListener", "approveCursorMove", POSITIONING );
211 0 : DESCRIBE_EVENT( "sdbc", "XRowSetListener", "cursorMoved", POSITIONED );
212 0 : DESCRIBE_EVENT( "form", "XDatabaseParameterListener", "approveParameter", APPROVEPARAMETER );
213 0 : DESCRIBE_EVENT( "sdb", "XSQLErrorListener", "errorOccured", ERROROCCURRED );
214 0 : DESCRIBE_EVENT( "awt", "XAdjustmentListener", "adjustmentValueChanged", ADJUSTMENTVALUECHANGED );
215 0 : }
216 : }
217 :
218 0 : EventMap::const_iterator pos = s_aKnownEvents.find( _rMethodName );
219 0 : if ( pos == s_aKnownEvents.end() )
220 0 : return false;
221 :
222 0 : _out_rDescription = pos->second;
223 0 : return true;
224 : }
225 :
226 : //....................................................................
227 0 : ::rtl::OUString lcl_getEventPropertyName( const ::rtl::OUString& _rListenerClassName, const ::rtl::OUString& _rMethodName )
228 : {
229 0 : ::rtl::OUStringBuffer aPropertyName;
230 0 : aPropertyName.append( _rListenerClassName );
231 0 : aPropertyName.append( (sal_Unicode)';' );
232 0 : aPropertyName.append( _rMethodName.getStr() );
233 0 : return aPropertyName.makeStringAndClear();
234 : }
235 :
236 : //................................................................
237 0 : ScriptEventDescriptor lcl_getAssignedScriptEvent( const EventDescription& _rEvent, const Sequence< ScriptEventDescriptor >& _rAllAssignedMacros )
238 : {
239 0 : ScriptEventDescriptor aScriptEvent;
240 : // for the case there is actually no event assigned, initialize at least ListenerType and MethodName,
241 : // so this ScriptEventDescriptor properly describes the given event
242 0 : aScriptEvent.ListenerType = _rEvent.sListenerClassName;
243 0 : aScriptEvent.EventMethod = _rEvent.sListenerMethodName;
244 :
245 0 : const ScriptEventDescriptor* pAssignedEvent = _rAllAssignedMacros.getConstArray();
246 0 : sal_Int32 assignedEventCount( _rAllAssignedMacros.getLength() );
247 0 : for ( sal_Int32 assignedEvent = 0; assignedEvent < assignedEventCount; ++assignedEvent, ++pAssignedEvent )
248 : {
249 0 : if ( ( pAssignedEvent->ListenerType != _rEvent.sListenerClassName )
250 0 : || ( pAssignedEvent->EventMethod != _rEvent.sListenerMethodName )
251 : )
252 0 : continue;
253 :
254 0 : if ( ( pAssignedEvent->ScriptCode.isEmpty() )
255 0 : || ( pAssignedEvent->ScriptType.isEmpty() )
256 : )
257 : {
258 : OSL_FAIL( "lcl_getAssignedScriptEvent: me thinks this should not happen!" );
259 0 : continue;
260 : }
261 :
262 0 : aScriptEvent = *pAssignedEvent;
263 :
264 0 : if ( aScriptEvent.ScriptType != "StarBasic" )
265 0 : continue;
266 :
267 : // this is an old-style macro specification:
268 : // [document|application]:Library.Module.Function
269 : // we need to translate this to the new-style macro specification
270 : // vnd.sun.star.script:Library.Module.Function?language=Basic&location=[document|application]
271 :
272 0 : sal_Int32 nPrefixLen = aScriptEvent.ScriptCode.indexOf( ':' );
273 : OSL_ENSURE( nPrefixLen > 0, "lcl_getAssignedScriptEvent: illegal location!" );
274 0 : ::rtl::OUString sLocation = aScriptEvent.ScriptCode.copy( 0, nPrefixLen );
275 0 : ::rtl::OUString sMacroPath = aScriptEvent.ScriptCode.copy( nPrefixLen + 1 );
276 :
277 0 : ::rtl::OUStringBuffer aNewStyleSpec;
278 0 : aNewStyleSpec.appendAscii( "vnd.sun.star.script:" );
279 0 : aNewStyleSpec.append ( sMacroPath );
280 0 : aNewStyleSpec.appendAscii( "?language=Basic&location=" );
281 0 : aNewStyleSpec.append ( sLocation );
282 :
283 0 : aScriptEvent.ScriptCode = aNewStyleSpec.makeStringAndClear();
284 :
285 : // also, this new-style spec requires the script code to be "Script" instead of "StarBasic"
286 0 : aScriptEvent.ScriptType = ::rtl::OUString( "Script" );
287 0 : }
288 0 : return aScriptEvent;
289 : }
290 :
291 : //................................................................
292 0 : ::rtl::OUString lcl_getQualifiedKnownListenerName( const ScriptEventDescriptor& _rFormComponentEventDescriptor )
293 : {
294 0 : EventDescription aKnownEvent;
295 0 : if ( lcl_getEventDescriptionForMethod( _rFormComponentEventDescriptor.EventMethod, aKnownEvent ) )
296 0 : return aKnownEvent.sListenerClassName;
297 : OSL_FAIL( "lcl_getQualifiedKnownListenerName: unknown method name!" );
298 : // somebody assigned an script to a form component event which we don't know
299 : // Speaking strictly, this is not really an error - it is possible to do
300 : // this programmatically -, but it should rarely happen, since it's not possible
301 : // via UI
302 0 : return _rFormComponentEventDescriptor.ListenerType;
303 : }
304 :
305 : //................................................................
306 : typedef ::std::set< Type, TypeLessByName > TypeBag;
307 :
308 : //................................................................
309 0 : void lcl_addListenerTypesFor_throw( const Reference< XInterface >& _rxComponent,
310 : const Reference< XIntrospection >& _rxIntrospection, TypeBag& _out_rTypes )
311 : {
312 0 : if ( !_rxComponent.is() )
313 0 : return;
314 : OSL_PRECOND( _rxIntrospection.is(), "lcl_addListenerTypesFor_throw: this will crash!" );
315 :
316 : Reference< XIntrospectionAccess > xIntrospectionAccess(
317 0 : _rxIntrospection->inspect( makeAny( _rxComponent ) ), UNO_QUERY_THROW );
318 :
319 0 : Sequence< Type > aListeners( xIntrospectionAccess->getSupportedListeners() );
320 :
321 0 : ::std::copy( aListeners.getConstArray(), aListeners.getConstArray() + aListeners.getLength(),
322 0 : ::std::insert_iterator< TypeBag >( _out_rTypes, _out_rTypes.begin() ) );
323 : }
324 :
325 : //................................................................
326 0 : bool operator ==( const ScriptEventDescriptor _lhs, const ScriptEventDescriptor _rhs )
327 : {
328 0 : return ( ( _lhs.ListenerType == _rhs.ListenerType )
329 0 : && ( _lhs.EventMethod == _rhs.EventMethod )
330 0 : && ( _lhs.AddListenerParam == _rhs.AddListenerParam )
331 0 : && ( _lhs.ScriptType == _rhs.ScriptType )
332 0 : && ( _lhs.ScriptCode == _rhs.ScriptCode )
333 0 : );
334 : }
335 : }
336 :
337 : //====================================================================
338 : //= EventHandler
339 : //====================================================================
340 : typedef ::cppu::WeakImplHelper1 < ::com::sun::star::container::XNameReplace
341 : > EventHolder_Base;
342 : /** a UNO component holding assigned event descriptions, for use with a SvxMacroAssignDlg
343 : */
344 : class EventHolder : public EventHolder_Base
345 : {
346 : private:
347 : typedef ::boost::unordered_map< ::rtl::OUString, ScriptEventDescriptor, ::rtl::OUStringHash > EventMap;
348 : typedef ::std::map< EventId, EventMap::iterator > EventMapIndexAccess;
349 :
350 : EventMap m_aEventNameAccess;
351 : EventMapIndexAccess m_aEventIndexAccess;
352 :
353 : public:
354 : EventHolder( );
355 :
356 : void addEvent( EventId _nId, const ::rtl::OUString& _rEventName, const ScriptEventDescriptor& _rScriptEvent );
357 :
358 : /** effectively the same as getByName, but instead of converting the ScriptEventDescriptor to the weird
359 : format used by the macro assignment dialog, it is returned directly
360 : */
361 : ScriptEventDescriptor getNormalizedDescriptorByName( const ::rtl::OUString& _rEventName ) const;
362 :
363 : // XNameReplace
364 : virtual void SAL_CALL replaceByName( const ::rtl::OUString& _rName, const Any& aElement ) throw (IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException);
365 : virtual Any SAL_CALL getByName( const ::rtl::OUString& _rName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException);
366 : virtual Sequence< ::rtl::OUString > SAL_CALL getElementNames( ) throw (RuntimeException);
367 : virtual ::sal_Bool SAL_CALL hasByName( const ::rtl::OUString& _rName ) throw (RuntimeException);
368 : virtual Type SAL_CALL getElementType( ) throw (RuntimeException);
369 : virtual ::sal_Bool SAL_CALL hasElements( ) throw (RuntimeException);
370 :
371 : protected:
372 : ~EventHolder( );
373 :
374 : private:
375 : ScriptEventDescriptor impl_getDescriptor_throw( const ::rtl::OUString& _rEventName ) const;
376 : };
377 :
378 : DBG_NAME( EventHolder )
379 : //------------------------------------------------------------------------
380 0 : EventHolder::EventHolder()
381 : {
382 : DBG_CTOR( EventHolder, NULL );
383 0 : }
384 :
385 : //------------------------------------------------------------------------
386 0 : EventHolder::~EventHolder()
387 : {
388 0 : m_aEventNameAccess.clear();
389 0 : m_aEventIndexAccess.clear();
390 : DBG_DTOR( EventHolder, NULL );
391 0 : }
392 :
393 : //------------------------------------------------------------------------
394 0 : void EventHolder::addEvent( EventId _nId, const ::rtl::OUString& _rEventName, const ScriptEventDescriptor& _rScriptEvent )
395 : {
396 : ::std::pair< EventMap::iterator, bool > insertionResult =
397 0 : m_aEventNameAccess.insert( EventMap::value_type( _rEventName, _rScriptEvent ) );
398 : OSL_ENSURE( insertionResult.second, "EventHolder::addEvent: there already was a MacroURL for this event!" );
399 0 : m_aEventIndexAccess[ _nId ] = insertionResult.first;
400 0 : }
401 :
402 : //------------------------------------------------------------------------
403 0 : ScriptEventDescriptor EventHolder::getNormalizedDescriptorByName( const ::rtl::OUString& _rEventName ) const
404 : {
405 0 : return impl_getDescriptor_throw( _rEventName );
406 : }
407 :
408 : //------------------------------------------------------------------------
409 0 : ScriptEventDescriptor EventHolder::impl_getDescriptor_throw( const ::rtl::OUString& _rEventName ) const
410 : {
411 0 : EventMap::const_iterator pos = m_aEventNameAccess.find( _rEventName );
412 0 : if ( pos == m_aEventNameAccess.end() )
413 0 : throw NoSuchElementException( ::rtl::OUString(), *const_cast< EventHolder* >( this ) );
414 0 : return pos->second;
415 : }
416 :
417 : //------------------------------------------------------------------------
418 0 : void SAL_CALL EventHolder::replaceByName( const ::rtl::OUString& _rName, const Any& _rElement ) throw (IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
419 : {
420 0 : EventMap::iterator pos = m_aEventNameAccess.find( _rName );
421 0 : if ( pos == m_aEventNameAccess.end() )
422 0 : throw NoSuchElementException( ::rtl::OUString(), *this );
423 :
424 0 : Sequence< PropertyValue > aScriptDescriptor;
425 0 : OSL_VERIFY( _rElement >>= aScriptDescriptor );
426 :
427 0 : ::comphelper::NamedValueCollection aExtractor( aScriptDescriptor );
428 :
429 0 : pos->second.ScriptType = aExtractor.getOrDefault( "EventType", ::rtl::OUString() );
430 0 : pos->second.ScriptCode = aExtractor.getOrDefault( "Script", ::rtl::OUString() );
431 0 : }
432 :
433 : //------------------------------------------------------------------------
434 0 : Any SAL_CALL EventHolder::getByName( const ::rtl::OUString& _rName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException)
435 : {
436 0 : ScriptEventDescriptor aDescriptor( impl_getDescriptor_throw( _rName ) );
437 :
438 0 : Any aRet;
439 0 : Sequence< PropertyValue > aScriptDescriptor( 2 );
440 0 : aScriptDescriptor[0].Name = ::rtl::OUString("EventType");
441 0 : aScriptDescriptor[0].Value <<= aDescriptor.ScriptType;
442 0 : aScriptDescriptor[1].Name = ::rtl::OUString("Script");
443 0 : aScriptDescriptor[1].Value <<= aDescriptor.ScriptCode;
444 :
445 0 : return makeAny( aScriptDescriptor );
446 : }
447 :
448 : //------------------------------------------------------------------------
449 0 : Sequence< ::rtl::OUString > SAL_CALL EventHolder::getElementNames( ) throw (RuntimeException)
450 : {
451 0 : Sequence< ::rtl::OUString > aReturn( m_aEventIndexAccess.size() );
452 0 : ::rtl::OUString* pReturn = aReturn.getArray();
453 :
454 : // SvxMacroAssignDlg has a weird API: It expects a XNameReplace, means a container whose
455 : // main access method is by name. In it's UI, it shows the possible events in exactly the
456 : // order in which XNameAccess::getElementNames returns them.
457 : // However, SvxMacroAssignDlg *also* takes an index for the initial selection, which is
458 : // relative to the sequence returned by XNameAccess::getElementNames.
459 : // This is IMO weird, since it mixes index access with name access, which decreases efficiency
460 : // of the implementation.
461 : // Well, it means we're forced to return the events in getElementNames in exactly the same as they
462 : // appear in the property browser UI.
463 0 : for ( EventMapIndexAccess::const_iterator loop = m_aEventIndexAccess.begin();
464 0 : loop != m_aEventIndexAccess.end();
465 : ++loop, ++pReturn
466 : )
467 0 : *pReturn = loop->second->first;
468 0 : return aReturn;
469 : }
470 :
471 : //------------------------------------------------------------------------
472 0 : sal_Bool SAL_CALL EventHolder::hasByName( const ::rtl::OUString& _rName ) throw (RuntimeException)
473 : {
474 0 : EventMap::const_iterator pos = m_aEventNameAccess.find( _rName );
475 0 : return pos != m_aEventNameAccess.end();
476 : }
477 :
478 : //------------------------------------------------------------------------
479 0 : Type SAL_CALL EventHolder::getElementType( ) throw (RuntimeException)
480 : {
481 0 : return ::getCppuType( static_cast< Sequence< PropertyValue >* >( NULL ) );
482 : }
483 :
484 : //------------------------------------------------------------------------
485 0 : sal_Bool SAL_CALL EventHolder::hasElements( ) throw (RuntimeException)
486 : {
487 0 : return !m_aEventNameAccess.empty();
488 : }
489 :
490 :
491 : //====================================================================
492 : //= EventHandler
493 : //====================================================================
494 : DBG_NAME( EventHandler )
495 : //--------------------------------------------------------------------
496 0 : EventHandler::EventHandler( const Reference< XComponentContext >& _rxContext )
497 : :EventHandler_Base( m_aMutex )
498 : ,m_aContext( _rxContext )
499 : ,m_aPropertyListeners( m_aMutex )
500 : ,m_bEventsMapInitialized( false )
501 : ,m_bIsDialogElement( false )
502 0 : ,m_nGridColumnType( -1 )
503 : {
504 : DBG_CTOR( EventHandler, NULL );
505 0 : }
506 :
507 : //--------------------------------------------------------------------
508 0 : EventHandler::~EventHandler()
509 : {
510 : DBG_DTOR( EventHandler, NULL );
511 0 : }
512 :
513 : //--------------------------------------------------------------------
514 0 : ::rtl::OUString SAL_CALL EventHandler::getImplementationName( ) throw (RuntimeException)
515 : {
516 0 : return getImplementationName_static();
517 : }
518 :
519 : //--------------------------------------------------------------------
520 0 : ::sal_Bool SAL_CALL EventHandler::supportsService( const ::rtl::OUString& ServiceName ) throw (RuntimeException)
521 : {
522 0 : StlSyntaxSequence< ::rtl::OUString > aAllServices( getSupportedServiceNames() );
523 0 : return ::std::find( aAllServices.begin(), aAllServices.end(), ServiceName ) != aAllServices.end();
524 : }
525 :
526 : //--------------------------------------------------------------------
527 0 : Sequence< ::rtl::OUString > SAL_CALL EventHandler::getSupportedServiceNames( ) throw (RuntimeException)
528 : {
529 0 : return getSupportedServiceNames_static();
530 : }
531 :
532 : //--------------------------------------------------------------------
533 0 : ::rtl::OUString SAL_CALL EventHandler::getImplementationName_static( ) throw (RuntimeException)
534 : {
535 0 : return ::rtl::OUString( "com.sun.star.comp.extensions.EventHandler" );
536 : }
537 :
538 : //--------------------------------------------------------------------
539 0 : Sequence< ::rtl::OUString > SAL_CALL EventHandler::getSupportedServiceNames_static( ) throw (RuntimeException)
540 : {
541 0 : Sequence< ::rtl::OUString > aSupported( 1 );
542 0 : aSupported[0] = ::rtl::OUString( "com.sun.star.form.inspection.EventHandler" );
543 0 : return aSupported;
544 : }
545 :
546 : //--------------------------------------------------------------------
547 0 : Reference< XInterface > SAL_CALL EventHandler::Create( const Reference< XComponentContext >& _rxContext )
548 : {
549 0 : return *( new EventHandler( _rxContext ) );
550 : }
551 :
552 : //--------------------------------------------------------------------
553 0 : void SAL_CALL EventHandler::inspect( const Reference< XInterface >& _rxIntrospectee ) throw (RuntimeException, NullPointerException)
554 : {
555 0 : ::osl::MutexGuard aGuard( m_aMutex );
556 :
557 0 : if ( !_rxIntrospectee.is() )
558 0 : throw NullPointerException();
559 :
560 0 : m_xComponent = Reference< XPropertySet >( _rxIntrospectee, UNO_QUERY_THROW );
561 :
562 0 : m_bEventsMapInitialized = false;
563 0 : EventMap aEmpty;
564 0 : m_aEvents.swap( aEmpty );
565 :
566 0 : m_bIsDialogElement = false;
567 0 : m_nGridColumnType = -1;
568 : try
569 : {
570 0 : Reference< XPropertySetInfo > xPSI( m_xComponent->getPropertySetInfo() );
571 0 : m_bIsDialogElement = xPSI.is()
572 0 : && xPSI->hasPropertyByName( PROPERTY_WIDTH )
573 0 : && xPSI->hasPropertyByName( PROPERTY_HEIGHT )
574 0 : && xPSI->hasPropertyByName( PROPERTY_POSITIONX )
575 0 : && xPSI->hasPropertyByName( PROPERTY_POSITIONY );
576 :
577 0 : Reference< XChild > xAsChild( _rxIntrospectee, UNO_QUERY );
578 0 : if ( xAsChild.is() && !Reference< XForm >( _rxIntrospectee, UNO_QUERY ).is() )
579 : {
580 0 : if ( FormComponentType::GRIDCONTROL == classifyComponent( xAsChild->getParent() ) )
581 : {
582 0 : m_nGridColumnType = classifyComponent( _rxIntrospectee );
583 : }
584 0 : }
585 : }
586 0 : catch( const Exception& )
587 : {
588 : DBG_UNHANDLED_EXCEPTION();
589 0 : }
590 0 : }
591 :
592 : //--------------------------------------------------------------------
593 0 : Any SAL_CALL EventHandler::getPropertyValue( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException)
594 : {
595 0 : ::osl::MutexGuard aGuard( m_aMutex );
596 :
597 0 : const EventDescription& rEvent = impl_getEventForName_throw( _rPropertyName );
598 :
599 0 : Sequence< ScriptEventDescriptor > aEvents;
600 0 : impl_getComponentScriptEvents_nothrow( aEvents );
601 :
602 0 : sal_Int32 nEventCount = aEvents.getLength();
603 0 : const ScriptEventDescriptor* pEvents = aEvents.getConstArray();
604 :
605 0 : ScriptEventDescriptor aPropertyValue;
606 0 : for ( sal_Int32 event = 0; event < nEventCount; ++event, ++pEvents )
607 : {
608 0 : if ( rEvent.sListenerClassName == pEvents->ListenerType
609 0 : && rEvent.sListenerMethodName == pEvents->EventMethod
610 : )
611 : {
612 0 : aPropertyValue = *pEvents;
613 0 : break;
614 : }
615 : }
616 :
617 0 : return makeAny( aPropertyValue );
618 : }
619 :
620 : //--------------------------------------------------------------------
621 0 : void SAL_CALL EventHandler::setPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& _rValue ) throw (UnknownPropertyException, RuntimeException)
622 : {
623 0 : ::osl::MutexGuard aGuard( m_aMutex );
624 :
625 0 : const EventDescription& rEvent = impl_getEventForName_throw( _rPropertyName );
626 :
627 0 : ScriptEventDescriptor aNewScriptEvent;
628 0 : OSL_VERIFY( _rValue >>= aNewScriptEvent );
629 :
630 0 : ScriptEventDescriptor aOldScriptEvent;
631 0 : OSL_VERIFY( getPropertyValue( _rPropertyName ) >>= aOldScriptEvent );
632 0 : if ( aOldScriptEvent == aNewScriptEvent )
633 0 : return;
634 :
635 0 : if ( m_bIsDialogElement )
636 0 : impl_setDialogElementScriptEvent_nothrow( aNewScriptEvent );
637 : else
638 0 : impl_setFormComponentScriptEvent_nothrow( aNewScriptEvent );
639 :
640 0 : Reference< XModifiable > xDoc( m_aContext.getContextValueByAsciiName( "ContextDocument" ), UNO_QUERY );
641 0 : if ( xDoc.is() )
642 0 : xDoc->setModified( sal_True );
643 :
644 0 : PropertyChangeEvent aEvent;
645 0 : aEvent.Source = m_xComponent;
646 0 : aEvent.PropertyHandle = rEvent.nId;
647 0 : aEvent.PropertyName = _rPropertyName;
648 0 : aEvent.OldValue <<= aOldScriptEvent;
649 0 : aEvent.NewValue <<= aNewScriptEvent;
650 0 : m_aPropertyListeners.notify( aEvent, &XPropertyChangeListener::propertyChange );
651 : }
652 :
653 : //--------------------------------------------------------------------
654 0 : Any SAL_CALL EventHandler::convertToPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& _rControlValue ) throw (UnknownPropertyException, RuntimeException)
655 : {
656 0 : ::osl::MutexGuard aGuard( m_aMutex );
657 :
658 0 : ::rtl::OUString sNewScriptCode;
659 0 : OSL_VERIFY( _rControlValue >>= sNewScriptCode );
660 :
661 0 : Sequence< ScriptEventDescriptor > aAllAssignedEvents;
662 0 : impl_getComponentScriptEvents_nothrow( aAllAssignedEvents );
663 :
664 0 : const EventDescription& rEvent = impl_getEventForName_throw( _rPropertyName );
665 0 : ScriptEventDescriptor aAssignedScript = lcl_getAssignedScriptEvent( rEvent, aAllAssignedEvents );
666 :
667 : OSL_ENSURE( sNewScriptCode.isEmpty(), "EventHandler::convertToPropertyValue: cannot convert a non-empty display name!" );
668 : // Usually, there is no possibility for the user to change the content of an event binding directly in the
669 : // input field, this instead is done with the macro assignment dialog.
670 : // The only exception is the user pressing "DEL" while the control has the focus, in this case, we reset the
671 : // control content to an empty string. So this is the only scenario where this method is allowed to be called.
672 :
673 : // Striclty, we would be able to convert the display value to a property value,
674 : // using the "name (location, language)" format we used in convertToControlValue. However,
675 : // there is no need for this code ...
676 :
677 0 : aAssignedScript.ScriptCode = sNewScriptCode;
678 0 : return makeAny( aAssignedScript );
679 : }
680 :
681 : //--------------------------------------------------------------------
682 0 : Any SAL_CALL EventHandler::convertToControlValue( const ::rtl::OUString& /*_rPropertyName*/, const Any& _rPropertyValue, const Type& _rControlValueType ) throw (UnknownPropertyException, RuntimeException)
683 : {
684 0 : ::osl::MutexGuard aGuard( m_aMutex );
685 :
686 0 : ScriptEventDescriptor aScriptEvent;
687 0 : OSL_VERIFY( _rPropertyValue >>= aScriptEvent );
688 :
689 : OSL_ENSURE( _rControlValueType.getTypeClass() == TypeClass_STRING,
690 : "EventHandler::convertToControlValue: unexpected ControlValue type class!" );
691 : (void)_rControlValueType;
692 :
693 0 : ::rtl::OUString sScript( aScriptEvent.ScriptCode );
694 0 : if ( !sScript.isEmpty() )
695 : {
696 : // format is: "name (location, language)"
697 : try
698 : {
699 : // parse
700 0 : Reference< XUriReferenceFactory > xUriRefFac = UriReferenceFactory::create( m_aContext.getUNOContext() );
701 0 : Reference< XVndSunStarScriptUrlReference > xScriptUri( xUriRefFac->parse( sScript ), UNO_QUERY_THROW );
702 :
703 0 : ::rtl::OUStringBuffer aComposeBuffer;
704 :
705 : // name
706 0 : aComposeBuffer.append( xScriptUri->getName() );
707 :
708 : // location
709 0 : const ::rtl::OUString sLocationParamName( "location" );
710 0 : const ::rtl::OUString sLocation = xScriptUri->getParameter( sLocationParamName );
711 0 : const ::rtl::OUString sLangParamName( "language" );
712 0 : const ::rtl::OUString sLanguage = xScriptUri->getParameter( sLangParamName );
713 :
714 0 : if ( !(sLocation.isEmpty() && sLanguage.isEmpty()) )
715 : {
716 0 : aComposeBuffer.appendAscii( " (" );
717 :
718 : // location
719 : OSL_ENSURE( !sLocation.isEmpty(), "EventHandler::convertToControlValue: unexpected: no location!" );
720 0 : if ( !sLocation.isEmpty() )
721 : {
722 0 : aComposeBuffer.append( sLocation );
723 0 : aComposeBuffer.appendAscii( ", " );
724 : }
725 :
726 : // language
727 0 : if ( !sLanguage.isEmpty() )
728 : {
729 0 : aComposeBuffer.append( sLanguage );
730 : }
731 :
732 0 : aComposeBuffer.append( sal_Unicode( ')' ) );
733 : }
734 :
735 0 : sScript = aComposeBuffer.makeStringAndClear();
736 : }
737 0 : catch( const Exception& )
738 : {
739 : DBG_UNHANDLED_EXCEPTION();
740 : }
741 : }
742 :
743 0 : return makeAny( sScript );
744 : }
745 :
746 : //--------------------------------------------------------------------
747 0 : PropertyState SAL_CALL EventHandler::getPropertyState( const ::rtl::OUString& /*_rPropertyName*/ ) throw (UnknownPropertyException, RuntimeException)
748 : {
749 0 : return PropertyState_DIRECT_VALUE;
750 : }
751 :
752 : //--------------------------------------------------------------------
753 0 : void SAL_CALL EventHandler::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException)
754 : {
755 0 : ::osl::MutexGuard aGuard( m_aMutex );
756 0 : if ( !_rxListener.is() )
757 0 : throw NullPointerException();
758 0 : m_aPropertyListeners.addListener( _rxListener );
759 0 : }
760 :
761 : //--------------------------------------------------------------------
762 0 : void SAL_CALL EventHandler::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException)
763 : {
764 0 : ::osl::MutexGuard aGuard( m_aMutex );
765 0 : m_aPropertyListeners.removeListener( _rxListener );
766 0 : }
767 :
768 : //--------------------------------------------------------------------
769 0 : Sequence< Property > SAL_CALL EventHandler::getSupportedProperties() throw (RuntimeException)
770 : {
771 0 : ::osl::MutexGuard aGuard( m_aMutex );
772 0 : if ( !m_bEventsMapInitialized )
773 : {
774 0 : const_cast< EventHandler* >( this )->m_bEventsMapInitialized = true;
775 : try
776 : {
777 0 : Sequence< Type > aListeners;
778 0 : impl_getCopmonentListenerTypes_nothrow( aListeners );
779 0 : sal_Int32 listenerCount = aListeners.getLength();
780 :
781 0 : Property aCurrentProperty;
782 0 : ::rtl::OUString sListenerClassName;
783 :
784 : // loop through all listeners and all methods, and see which we can present at the UI
785 0 : const Type* pListeners = aListeners.getConstArray();
786 0 : for ( sal_Int32 listener = 0; listener < listenerCount; ++listener, ++pListeners )
787 : {
788 0 : aCurrentProperty = Property();
789 :
790 : // the programmatic name of the listener, to be used as "property" name
791 0 : sListenerClassName = pListeners->getTypeName();
792 : OSL_ENSURE( !sListenerClassName.isEmpty(), "EventHandler::getSupportedProperties: strange - no listener name ..." );
793 0 : if ( sListenerClassName.isEmpty() )
794 0 : continue;
795 :
796 : // loop through all methods
797 0 : Sequence< ::rtl::OUString > aMethods( comphelper::getEventMethodsForType( *pListeners ) );
798 :
799 0 : const ::rtl::OUString* pMethods = aMethods.getConstArray();
800 0 : sal_uInt32 methodCount = aMethods.getLength();
801 :
802 0 : for (sal_uInt32 method = 0 ; method < methodCount ; ++method, ++pMethods )
803 : {
804 0 : EventDescription aEvent;
805 0 : if ( !lcl_getEventDescriptionForMethod( *pMethods, aEvent ) )
806 0 : continue;
807 :
808 0 : if ( !impl_filterMethod_nothrow( aEvent ) )
809 0 : continue;
810 :
811 : const_cast< EventHandler* >( this )->m_aEvents.insert( EventMap::value_type(
812 0 : lcl_getEventPropertyName( sListenerClassName, *pMethods ), aEvent ) );
813 0 : }
814 0 : }
815 :
816 : }
817 0 : catch( const Exception& )
818 : {
819 : DBG_UNHANDLED_EXCEPTION();
820 : }
821 : }
822 :
823 : // sort them by ID - this is the relative ordering in the UI
824 0 : ::std::map< EventId, Property > aOrderedProperties;
825 0 : for ( EventMap::const_iterator loop = m_aEvents.begin();
826 0 : loop != m_aEvents.end();
827 : ++loop
828 : )
829 : {
830 0 : aOrderedProperties[ loop->second.nId ] = Property(
831 0 : loop->first, loop->second.nId,
832 0 : ::getCppuType( static_cast< const ::rtl::OUString* >( NULL ) ),
833 0 : PropertyAttribute::BOUND );
834 : }
835 :
836 0 : StlSyntaxSequence< Property > aReturn( aOrderedProperties.size() );
837 : ::std::transform( aOrderedProperties.begin(), aOrderedProperties.end(), aReturn.begin(),
838 0 : ::o3tl::select2nd< ::std::map< EventId, Property >::value_type >() );
839 0 : return aReturn;
840 : }
841 :
842 : //--------------------------------------------------------------------
843 0 : Sequence< ::rtl::OUString > SAL_CALL EventHandler::getSupersededProperties( ) throw (RuntimeException)
844 : {
845 : // none
846 0 : return Sequence< ::rtl::OUString >( );
847 : }
848 :
849 : //--------------------------------------------------------------------
850 0 : Sequence< ::rtl::OUString > SAL_CALL EventHandler::getActuatingProperties( ) throw (RuntimeException)
851 : {
852 : // none
853 0 : return Sequence< ::rtl::OUString >( );
854 : }
855 :
856 : //--------------------------------------------------------------------
857 0 : LineDescriptor SAL_CALL EventHandler::describePropertyLine( const ::rtl::OUString& _rPropertyName,
858 : const Reference< XPropertyControlFactory >& _rxControlFactory )
859 : throw (UnknownPropertyException, NullPointerException, RuntimeException)
860 : {
861 0 : if ( !_rxControlFactory.is() )
862 0 : throw NullPointerException();
863 :
864 0 : ::osl::MutexGuard aGuard( m_aMutex );
865 :
866 0 : LineDescriptor aDescriptor;
867 :
868 0 : aDescriptor.Control = _rxControlFactory->createPropertyControl( PropertyControlType::TextField, sal_True );
869 0 : Reference< XEventListener > xControlExtender = new PropertyControlExtender( aDescriptor.Control );
870 :
871 0 : const EventDescription& rEvent = impl_getEventForName_throw( _rPropertyName );
872 0 : aDescriptor.DisplayName = rEvent.sDisplayName;
873 0 : aDescriptor.HelpURL = HelpIdUrl::getHelpURL( rEvent.sHelpId );
874 0 : aDescriptor.PrimaryButtonId = rtl::OStringToOUString(rEvent.sUniqueBrowseId, RTL_TEXTENCODING_UTF8);
875 0 : aDescriptor.HasPrimaryButton = sal_True;
876 0 : aDescriptor.Category = ::rtl::OUString( "Events" );
877 0 : return aDescriptor;
878 : }
879 :
880 : //--------------------------------------------------------------------
881 0 : ::sal_Bool SAL_CALL EventHandler::isComposable( const ::rtl::OUString& /*_rPropertyName*/ ) throw (UnknownPropertyException, RuntimeException)
882 : {
883 0 : return sal_False;
884 : }
885 :
886 : //--------------------------------------------------------------------
887 0 : InteractiveSelectionResult SAL_CALL EventHandler::onInteractivePropertySelection( const ::rtl::OUString& _rPropertyName, sal_Bool /*_bPrimary*/, Any& /*_rData*/, const Reference< XObjectInspectorUI >& _rxInspectorUI ) throw (UnknownPropertyException, NullPointerException, RuntimeException)
888 : {
889 0 : if ( !_rxInspectorUI.is() )
890 0 : throw NullPointerException();
891 :
892 0 : ::osl::MutexGuard aGuard( m_aMutex );
893 0 : const EventDescription& rForEvent = impl_getEventForName_throw( _rPropertyName );
894 :
895 0 : Sequence< ScriptEventDescriptor > aAllAssignedEvents;
896 0 : impl_getComponentScriptEvents_nothrow( aAllAssignedEvents );
897 :
898 : // SvxMacroAssignDlg-compatible structure holding all event/assignments
899 0 : ::rtl::Reference< EventHolder > pEventHolder( new EventHolder );
900 :
901 0 : for ( EventMap::const_iterator event = m_aEvents.begin();
902 0 : event != m_aEvents.end();
903 : ++event
904 : )
905 : {
906 : // the script which is assigned to the current event (if any)
907 0 : ScriptEventDescriptor aAssignedScript = lcl_getAssignedScriptEvent( event->second, aAllAssignedEvents );
908 0 : pEventHolder->addEvent( event->second.nId, event->second.sListenerMethodName, aAssignedScript );
909 0 : }
910 :
911 : // the inital selection in the dialog
912 0 : Sequence< ::rtl::OUString > aNames( pEventHolder->getElementNames() );
913 0 : const ::rtl::OUString* pChosenEvent = ::std::find( aNames.getConstArray(), aNames.getConstArray() + aNames.getLength(), rForEvent.sListenerMethodName );
914 0 : sal_uInt16 nInitialSelection = (sal_uInt16)( pChosenEvent - aNames.getConstArray() );
915 :
916 : // the dialog
917 0 : SvxAbstractDialogFactory* pFactory = SvxAbstractDialogFactory::Create();
918 0 : if ( !pFactory )
919 0 : return InteractiveSelectionResult_Cancelled;
920 :
921 : ::std::auto_ptr< VclAbstractDialog > pDialog( pFactory->CreateSvxMacroAssignDlg(
922 : PropertyHandlerHelper::getDialogParentWindow( m_aContext ),
923 : impl_getContextFrame_nothrow(),
924 : m_bIsDialogElement,
925 0 : pEventHolder.get(),
926 : nInitialSelection
927 0 : ) );
928 :
929 0 : if ( !pDialog.get() )
930 0 : return InteractiveSelectionResult_Cancelled;
931 :
932 : // DF definite problem here
933 : // OK & Cancel seem to be both returning 0
934 0 : if ( pDialog->Execute() == RET_CANCEL )
935 0 : return InteractiveSelectionResult_Cancelled;
936 :
937 : try
938 : {
939 0 : for ( EventMap::const_iterator event = m_aEvents.begin();
940 0 : event != m_aEvents.end();
941 : ++event
942 : )
943 : {
944 0 : ScriptEventDescriptor aScriptDescriptor( pEventHolder->getNormalizedDescriptorByName( event->second.sListenerMethodName ) );
945 :
946 : // set the new "property value"
947 : setPropertyValue(
948 0 : lcl_getEventPropertyName( event->second.sListenerClassName, event->second.sListenerMethodName ),
949 : makeAny( aScriptDescriptor )
950 0 : );
951 0 : }
952 : }
953 0 : catch( const Exception& )
954 : {
955 : DBG_UNHANDLED_EXCEPTION();
956 : }
957 :
958 0 : return InteractiveSelectionResult_Success;
959 : }
960 :
961 : //--------------------------------------------------------------------
962 0 : void SAL_CALL EventHandler::actuatingPropertyChanged( const ::rtl::OUString& /*_rActuatingPropertyName*/, const Any& /*_rNewValue*/, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/, sal_Bool /*_bFirstTimeInit*/ ) throw (NullPointerException, RuntimeException)
963 : {
964 : OSL_FAIL( "EventHandler::actuatingPropertyChanged: no actuating properties -> no callback (well, this is how it *should* be!)" );
965 0 : }
966 :
967 : //--------------------------------------------------------------------
968 0 : IMPLEMENT_FORWARD_XCOMPONENT( EventHandler, EventHandler_Base )
969 :
970 : //--------------------------------------------------------------------
971 0 : void SAL_CALL EventHandler::disposing()
972 : {
973 0 : EventMap aEmpty;
974 0 : m_aEvents.swap( aEmpty );
975 0 : m_xComponent.clear();
976 0 : }
977 :
978 : //--------------------------------------------------------------------
979 0 : sal_Bool SAL_CALL EventHandler::suspend( sal_Bool /*_bSuspend*/ ) throw (RuntimeException)
980 : {
981 0 : return sal_True;
982 : }
983 :
984 : //------------------------------------------------------------------------
985 0 : Reference< XFrame > EventHandler::impl_getContextFrame_nothrow() const
986 : {
987 0 : Reference< XFrame > xContextFrame;
988 :
989 : try
990 : {
991 0 : Reference< XModel > xContextDocument( m_aContext.getContextValueByAsciiName( "ContextDocument" ), UNO_QUERY_THROW );
992 0 : Reference< XController > xController( xContextDocument->getCurrentController(), UNO_SET_THROW );
993 0 : xContextFrame.set( xController->getFrame(), UNO_SET_THROW );
994 : }
995 0 : catch( const Exception& )
996 : {
997 : DBG_UNHANDLED_EXCEPTION();
998 : }
999 :
1000 0 : return xContextFrame;
1001 : }
1002 :
1003 : //--------------------------------------------------------------------
1004 0 : sal_Int32 EventHandler::impl_getComponentIndexInParent_throw() const
1005 : {
1006 0 : Reference< XChild > xChild( m_xComponent, UNO_QUERY_THROW );
1007 0 : Reference< XIndexAccess > xParentAsIndexAccess( xChild->getParent(), UNO_QUERY_THROW );
1008 :
1009 : // get the index of the inspected object within it's parent container
1010 0 : sal_Int32 nElements = xParentAsIndexAccess->getCount();
1011 0 : for ( sal_Int32 i=0; i<nElements; ++i )
1012 : {
1013 0 : Reference< XInterface > xElement( xParentAsIndexAccess->getByIndex( i ), UNO_QUERY_THROW );
1014 0 : if ( xElement == m_xComponent )
1015 0 : return i;
1016 0 : }
1017 0 : throw NoSuchElementException();
1018 : }
1019 :
1020 : //--------------------------------------------------------------------
1021 0 : void EventHandler::impl_getFormComponentScriptEvents_nothrow( Sequence < ScriptEventDescriptor >& _out_rEvents ) const
1022 : {
1023 0 : _out_rEvents = Sequence < ScriptEventDescriptor >();
1024 : try
1025 : {
1026 0 : Reference< XChild > xChild( m_xComponent, UNO_QUERY_THROW );
1027 0 : Reference< XEventAttacherManager > xEventManager( xChild->getParent(), UNO_QUERY_THROW );
1028 0 : _out_rEvents = xEventManager->getScriptEvents( impl_getComponentIndexInParent_throw() );
1029 :
1030 : // the form component script API has unqualified listener names, but for normalization
1031 : // purpose, we want fully qualified ones
1032 0 : ScriptEventDescriptor* pEvents = _out_rEvents.getArray();
1033 0 : ScriptEventDescriptor* pEventsEnd = _out_rEvents.getArray() + _out_rEvents.getLength();
1034 0 : while ( pEvents != pEventsEnd )
1035 : {
1036 0 : pEvents->ListenerType = lcl_getQualifiedKnownListenerName( *pEvents );
1037 0 : ++pEvents;
1038 0 : }
1039 : }
1040 0 : catch( const Exception& )
1041 : {
1042 : DBG_UNHANDLED_EXCEPTION();
1043 : }
1044 0 : }
1045 :
1046 : //--------------------------------------------------------------------
1047 0 : void EventHandler::impl_getCopmonentListenerTypes_nothrow( Sequence< Type >& _out_rTypes ) const
1048 : {
1049 0 : _out_rTypes.realloc( 0 );
1050 : try
1051 : {
1052 : // we use a set to avoid duplicates
1053 0 : TypeBag aListeners;
1054 :
1055 0 : Reference< XIntrospection > xIntrospection = Introspection::create( m_aContext.getUNOContext() );
1056 :
1057 : // --- model listeners
1058 : lcl_addListenerTypesFor_throw(
1059 0 : m_xComponent, xIntrospection, aListeners );
1060 :
1061 : // --- "secondary component" (usually: "control" listeners)
1062 : {
1063 0 : Reference< XInterface > xSecondaryComponent( impl_getSecondaryComponentForEventInspection_throw() );
1064 0 : lcl_addListenerTypesFor_throw( xSecondaryComponent, xIntrospection, aListeners );
1065 0 : ::comphelper::disposeComponent( xSecondaryComponent );
1066 : }
1067 :
1068 : // now that they're disambiguated, copy these types into our member
1069 0 : _out_rTypes.realloc( aListeners.size() );
1070 0 : ::std::copy( aListeners.begin(), aListeners.end(), _out_rTypes.getArray() );
1071 : }
1072 0 : catch( const Exception& )
1073 : {
1074 : DBG_UNHANDLED_EXCEPTION();
1075 : }
1076 0 : }
1077 :
1078 : //--------------------------------------------------------------------
1079 0 : void EventHandler::impl_getDialogElementScriptEvents_nothrow( Sequence < ScriptEventDescriptor >& _out_rEvents ) const
1080 : {
1081 0 : _out_rEvents = Sequence < ScriptEventDescriptor >();
1082 : try
1083 : {
1084 0 : Reference< XScriptEventsSupplier > xEventsSupplier( m_xComponent, UNO_QUERY_THROW );
1085 0 : Reference< XNameContainer > xEvents( xEventsSupplier->getEvents(), UNO_QUERY_THROW );
1086 0 : Sequence< ::rtl::OUString > aEventNames( xEvents->getElementNames() );
1087 :
1088 0 : sal_Int32 nEventCount = aEventNames.getLength();
1089 0 : _out_rEvents.realloc( nEventCount );
1090 :
1091 0 : const ::rtl::OUString* pNames = aEventNames.getConstArray();
1092 0 : ScriptEventDescriptor* pDescs = _out_rEvents.getArray();
1093 :
1094 0 : for( sal_Int32 i = 0 ; i < nEventCount ; ++i, ++pNames, ++pDescs )
1095 0 : OSL_VERIFY( xEvents->getByName( *pNames ) >>= *pDescs );
1096 : }
1097 0 : catch( const Exception& )
1098 : {
1099 : DBG_UNHANDLED_EXCEPTION();
1100 : }
1101 0 : }
1102 :
1103 : //--------------------------------------------------------------------
1104 0 : Reference< XInterface > EventHandler::impl_getSecondaryComponentForEventInspection_throw( ) const
1105 : {
1106 0 : Reference< XInterface > xReturn;
1107 :
1108 : // if it's a form, create a form controller for the additional events
1109 0 : Reference< XForm > xComponentAsForm( m_xComponent, UNO_QUERY );
1110 0 : if ( xComponentAsForm.is() )
1111 : {
1112 0 : Reference< XTabControllerModel > xComponentAsTCModel( m_xComponent, UNO_QUERY_THROW );
1113 : Reference< XFormController > xController(
1114 0 : m_aContext.createComponent( "com.sun.star.form.runtime.FormController" ), UNO_QUERY_THROW );
1115 0 : xController->setModel( xComponentAsTCModel );
1116 :
1117 0 : xReturn = xController;
1118 : }
1119 : else
1120 : {
1121 0 : ::rtl::OUString sControlService;
1122 0 : OSL_VERIFY( m_xComponent->getPropertyValue( PROPERTY_DEFAULTCONTROL ) >>= sControlService );
1123 :
1124 0 : xReturn = m_aContext.createComponent( sControlService );
1125 : }
1126 0 : return xReturn;
1127 : }
1128 :
1129 : //--------------------------------------------------------------------
1130 0 : const EventDescription& EventHandler::impl_getEventForName_throw( const ::rtl::OUString& _rPropertyName ) const
1131 : {
1132 0 : EventMap::const_iterator pos = m_aEvents.find( _rPropertyName );
1133 0 : if ( pos == m_aEvents.end() )
1134 0 : throw UnknownPropertyException();
1135 0 : return pos->second;
1136 : }
1137 :
1138 : //--------------------------------------------------------------------
1139 : namespace
1140 : {
1141 0 : static bool lcl_endsWith( const ::rtl::OUString& _rText, const ::rtl::OUString& _rCheck )
1142 : {
1143 0 : sal_Int32 nTextLen = _rText.getLength();
1144 0 : sal_Int32 nCheckLen = _rCheck.getLength();
1145 0 : if ( nCheckLen > nTextLen )
1146 0 : return false;
1147 :
1148 0 : return _rText.indexOf( _rCheck ) == ( nTextLen - nCheckLen );
1149 : }
1150 : }
1151 : //--------------------------------------------------------------------
1152 0 : void EventHandler::impl_setFormComponentScriptEvent_nothrow( const ScriptEventDescriptor& _rScriptEvent )
1153 : {
1154 : try
1155 : {
1156 0 : ::rtl::OUString sScriptCode( _rScriptEvent.ScriptCode );
1157 0 : ::rtl::OUString sScriptType( _rScriptEvent.ScriptType );
1158 0 : bool bResetScript = sScriptCode.isEmpty();
1159 :
1160 0 : sal_Int32 nObjectIndex = impl_getComponentIndexInParent_throw();
1161 0 : Reference< XChild > xChild( m_xComponent, UNO_QUERY_THROW );
1162 0 : Reference< XEventAttacherManager > xEventManager( xChild->getParent(), UNO_QUERY_THROW );
1163 0 : Sequence< ScriptEventDescriptor > aEvents( xEventManager->getScriptEvents( nObjectIndex ) );
1164 :
1165 : // is there already a registered script for this event?
1166 0 : ScriptEventDescriptor* pEvent = aEvents.getArray();
1167 0 : sal_Int32 eventCount = aEvents.getLength(), event = 0;
1168 0 : for ( event = 0; event < eventCount; ++event, ++pEvent )
1169 : {
1170 0 : if ( ( pEvent->EventMethod == _rScriptEvent.EventMethod )
1171 0 : && ( lcl_endsWith( _rScriptEvent.ListenerType, pEvent->ListenerType ) )
1172 : // (strange enough, the events we get from getScriptEvents are not fully qualified)
1173 : )
1174 : {
1175 : // yes
1176 0 : if ( !bResetScript )
1177 : {
1178 : // set to something non-empty -> overwrite
1179 0 : pEvent->ScriptCode = sScriptCode;
1180 0 : pEvent->ScriptType = sScriptType;
1181 : }
1182 : else
1183 : {
1184 : // set to empty -> remove from sequence
1185 0 : ::std::copy( pEvent + 1, aEvents.getArray() + eventCount, pEvent );
1186 0 : aEvents.realloc( eventCount - 1 );
1187 0 : --eventCount;
1188 : }
1189 0 : break;
1190 : }
1191 : }
1192 0 : if ( ( event >= eventCount ) && !bResetScript )
1193 : {
1194 : // no, did not find it -> append
1195 0 : aEvents.realloc( eventCount + 1 );
1196 0 : aEvents[ eventCount ] = _rScriptEvent;
1197 : }
1198 :
1199 0 : xEventManager->revokeScriptEvents( nObjectIndex );
1200 0 : xEventManager->registerScriptEvents( nObjectIndex, aEvents );
1201 :
1202 0 : PropertyHandlerHelper::setContextDocumentModified( m_aContext );
1203 : }
1204 0 : catch( const Exception& )
1205 : {
1206 : DBG_UNHANDLED_EXCEPTION();
1207 : }
1208 0 : }
1209 :
1210 : //--------------------------------------------------------------------
1211 0 : void EventHandler::impl_setDialogElementScriptEvent_nothrow( const ScriptEventDescriptor& _rScriptEvent )
1212 : {
1213 : try
1214 : {
1215 0 : ::rtl::OUString sScriptCode( _rScriptEvent.ScriptCode );
1216 0 : bool bResetScript = sScriptCode.isEmpty();
1217 :
1218 0 : Reference< XScriptEventsSupplier > xEventsSupplier( m_xComponent, UNO_QUERY_THROW );
1219 0 : Reference< XNameContainer > xEvents( xEventsSupplier->getEvents(), UNO_QUERY_THROW );
1220 :
1221 0 : ::rtl::OUStringBuffer aCompleteName;
1222 0 : aCompleteName.append( _rScriptEvent.ListenerType );
1223 0 : aCompleteName.appendAscii( "::" );
1224 0 : aCompleteName.append( _rScriptEvent.EventMethod );
1225 0 : ::rtl::OUString sCompleteName( aCompleteName.makeStringAndClear() );
1226 :
1227 0 : bool bExists = xEvents->hasByName( sCompleteName );
1228 :
1229 0 : if ( bResetScript )
1230 : {
1231 0 : if ( bExists )
1232 0 : xEvents->removeByName( sCompleteName );
1233 : }
1234 : else
1235 : {
1236 0 : Any aNewValue; aNewValue <<= _rScriptEvent;
1237 :
1238 0 : if ( bExists )
1239 0 : xEvents->replaceByName( sCompleteName, aNewValue );
1240 : else
1241 0 : xEvents->insertByName( sCompleteName, aNewValue );
1242 0 : }
1243 : }
1244 0 : catch( const Exception& )
1245 : {
1246 : DBG_UNHANDLED_EXCEPTION();
1247 : }
1248 0 : }
1249 :
1250 : //--------------------------------------------------------------------
1251 0 : bool EventHandler::impl_filterMethod_nothrow( const EventDescription& _rEvent ) const
1252 : {
1253 : // some (control-triggered) events do not make sense for certain grid control columns. However,
1254 : // our mechnism to retrieve control-triggered events does not know about this, so we do some
1255 : // late filtering here.
1256 0 : switch ( m_nGridColumnType )
1257 : {
1258 : case FormComponentType::COMBOBOX:
1259 0 : if ( UID_BRWEVT_ACTIONPERFORMED == _rEvent.sUniqueBrowseId )
1260 0 : return false;
1261 0 : break;
1262 : case FormComponentType::LISTBOX:
1263 0 : if ( ( UID_BRWEVT_CHANGED == _rEvent.sUniqueBrowseId )
1264 0 : || ( UID_BRWEVT_ACTIONPERFORMED == _rEvent.sUniqueBrowseId )
1265 : )
1266 0 : return false;
1267 0 : break;
1268 : }
1269 :
1270 0 : return true;
1271 : }
1272 :
1273 : //........................................................................
1274 0 : } // namespace pcr
1275 : //........................................................................
1276 :
1277 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|