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/FormController.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 1 : extern "C" void SAL_CALL createRegistryInfo_EventHandler()
73 : {
74 1 : ::pcr::OAutoRegistration< ::pcr::EventHandler > aAutoRegistration;
75 1 : }
76 :
77 : //........................................................................
78 : namespace pcr
79 : {
80 : //........................................................................
81 :
82 : using ::com::sun::star::uno::Reference;
83 : using ::com::sun::star::uno::XComponentContext;
84 : using ::com::sun::star::beans::XPropertySet;
85 : using ::com::sun::star::uno::Any;
86 : using ::com::sun::star::uno::TypeClass_STRING;
87 : using ::com::sun::star::uno::Type;
88 : using ::com::sun::star::beans::Introspection;
89 : using ::com::sun::star::beans::XPropertyChangeListener;
90 : using ::com::sun::star::beans::Property;
91 : using ::com::sun::star::beans::PropertyState;
92 : using ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
93 : using ::com::sun::star::uno::Sequence;
94 : using ::com::sun::star::script::ScriptEventDescriptor;
95 : using ::com::sun::star::script::XScriptEventsSupplier;
96 : using ::com::sun::star::lang::NullPointerException;
97 : using ::com::sun::star::uno::Exception;
98 : using ::com::sun::star::container::XChild;
99 : using ::com::sun::star::container::XIndexAccess;
100 : using ::com::sun::star::script::XEventAttacherManager;
101 : using ::com::sun::star::uno::UNO_QUERY;
102 : using ::com::sun::star::uno::UNO_QUERY_THROW;
103 : using ::com::sun::star::uno::XInterface;
104 : using ::com::sun::star::beans::XIntrospection;
105 : using ::com::sun::star::beans::XIntrospectionAccess;
106 : using ::com::sun::star::container::XNameContainer;
107 : using ::com::sun::star::awt::XTabControllerModel;
108 : using ::com::sun::star::form::XForm;
109 : using ::com::sun::star::form::runtime::FormController;
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 :
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 OString& _sHelpId, const OString& _sUniqueBrowseId )
146 : :sDisplayName( String( PcrRes( _nDisplayNameResId ) ) )
147 : ,sListenerMethodName( OUString::createFromAscii( _pListenerMethodAsciiName ) )
148 : ,sHelpId( _sHelpId )
149 : ,sUniqueBrowseId( _sUniqueBrowseId )
150 0 : ,nId( _nId )
151 : {
152 0 : 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 : 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 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 : OUString lcl_getEventPropertyName( const OUString& _rListenerClassName, const OUString& _rMethodName )
228 : {
229 0 : 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 : OUString sLocation = aScriptEvent.ScriptCode.copy( 0, nPrefixLen );
275 0 : OUString sMacroPath = aScriptEvent.ScriptCode.copy( nPrefixLen + 1 );
276 :
277 0 : 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 = OUString( "Script" );
287 0 : }
288 0 : return aScriptEvent;
289 : }
290 :
291 : //................................................................
292 0 : 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< OUString, ScriptEventDescriptor, 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 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 OUString& _rEventName ) const;
362 :
363 : // XNameReplace
364 : virtual void SAL_CALL replaceByName( const OUString& _rName, const Any& aElement ) throw (IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException);
365 : virtual Any SAL_CALL getByName( const OUString& _rName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException);
366 : virtual Sequence< OUString > SAL_CALL getElementNames( ) throw (RuntimeException);
367 : virtual ::sal_Bool SAL_CALL hasByName( const 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 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 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 OUString& _rEventName ) const
404 : {
405 0 : return impl_getDescriptor_throw( _rEventName );
406 : }
407 :
408 : //------------------------------------------------------------------------
409 0 : ScriptEventDescriptor EventHolder::impl_getDescriptor_throw( const 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( OUString(), *const_cast< EventHolder* >( this ) );
414 0 : return pos->second;
415 : }
416 :
417 : //------------------------------------------------------------------------
418 0 : void SAL_CALL EventHolder::replaceByName( const 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( 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", OUString() );
430 0 : pos->second.ScriptCode = aExtractor.getOrDefault( "Script", OUString() );
431 0 : }
432 :
433 : //------------------------------------------------------------------------
434 0 : Any SAL_CALL EventHolder::getByName( const 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 = OUString("EventType");
441 0 : aScriptDescriptor[0].Value <<= aDescriptor.ScriptType;
442 0 : aScriptDescriptor[1].Name = OUString("Script");
443 0 : aScriptDescriptor[1].Value <<= aDescriptor.ScriptCode;
444 :
445 0 : return makeAny( aScriptDescriptor );
446 : }
447 :
448 : //------------------------------------------------------------------------
449 0 : Sequence< OUString > SAL_CALL EventHolder::getElementNames( ) throw (RuntimeException)
450 : {
451 0 : Sequence< OUString > aReturn( m_aEventIndexAccess.size() );
452 0 : 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 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_xContext( _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 : 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 OUString& ServiceName ) throw (RuntimeException)
521 : {
522 0 : StlSyntaxSequence< OUString > aAllServices( getSupportedServiceNames() );
523 0 : return ::std::find( aAllServices.begin(), aAllServices.end(), ServiceName ) != aAllServices.end();
524 : }
525 :
526 : //--------------------------------------------------------------------
527 0 : Sequence< OUString > SAL_CALL EventHandler::getSupportedServiceNames( ) throw (RuntimeException)
528 : {
529 0 : return getSupportedServiceNames_static();
530 : }
531 :
532 : //--------------------------------------------------------------------
533 1 : OUString SAL_CALL EventHandler::getImplementationName_static( ) throw (RuntimeException)
534 : {
535 1 : return OUString( "com.sun.star.comp.extensions.EventHandler" );
536 : }
537 :
538 : //--------------------------------------------------------------------
539 1 : Sequence< OUString > SAL_CALL EventHandler::getSupportedServiceNames_static( ) throw (RuntimeException)
540 : {
541 1 : Sequence< OUString > aSupported( 1 );
542 1 : aSupported[0] = OUString( "com.sun.star.form.inspection.EventHandler" );
543 1 : 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 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 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 : PropertyHandlerHelper::setContextDocumentModified( m_xContext );
641 :
642 0 : PropertyChangeEvent aEvent;
643 0 : aEvent.Source = m_xComponent;
644 0 : aEvent.PropertyHandle = rEvent.nId;
645 0 : aEvent.PropertyName = _rPropertyName;
646 0 : aEvent.OldValue <<= aOldScriptEvent;
647 0 : aEvent.NewValue <<= aNewScriptEvent;
648 0 : m_aPropertyListeners.notify( aEvent, &XPropertyChangeListener::propertyChange );
649 : }
650 :
651 : //--------------------------------------------------------------------
652 0 : Any SAL_CALL EventHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue ) throw (UnknownPropertyException, RuntimeException)
653 : {
654 0 : ::osl::MutexGuard aGuard( m_aMutex );
655 :
656 0 : OUString sNewScriptCode;
657 0 : OSL_VERIFY( _rControlValue >>= sNewScriptCode );
658 :
659 0 : Sequence< ScriptEventDescriptor > aAllAssignedEvents;
660 0 : impl_getComponentScriptEvents_nothrow( aAllAssignedEvents );
661 :
662 0 : const EventDescription& rEvent = impl_getEventForName_throw( _rPropertyName );
663 0 : ScriptEventDescriptor aAssignedScript = lcl_getAssignedScriptEvent( rEvent, aAllAssignedEvents );
664 :
665 : OSL_ENSURE( sNewScriptCode.isEmpty(), "EventHandler::convertToPropertyValue: cannot convert a non-empty display name!" );
666 : // Usually, there is no possibility for the user to change the content of an event binding directly in the
667 : // input field, this instead is done with the macro assignment dialog.
668 : // The only exception is the user pressing "DEL" while the control has the focus, in this case, we reset the
669 : // control content to an empty string. So this is the only scenario where this method is allowed to be called.
670 :
671 : // Striclty, we would be able to convert the display value to a property value,
672 : // using the "name (location, language)" format we used in convertToControlValue. However,
673 : // there is no need for this code ...
674 :
675 0 : aAssignedScript.ScriptCode = sNewScriptCode;
676 0 : return makeAny( aAssignedScript );
677 : }
678 :
679 : //--------------------------------------------------------------------
680 0 : Any SAL_CALL EventHandler::convertToControlValue( const OUString& /*_rPropertyName*/, const Any& _rPropertyValue, const Type& _rControlValueType ) throw (UnknownPropertyException, RuntimeException)
681 : {
682 0 : ::osl::MutexGuard aGuard( m_aMutex );
683 :
684 0 : ScriptEventDescriptor aScriptEvent;
685 0 : OSL_VERIFY( _rPropertyValue >>= aScriptEvent );
686 :
687 : OSL_ENSURE( _rControlValueType.getTypeClass() == TypeClass_STRING,
688 : "EventHandler::convertToControlValue: unexpected ControlValue type class!" );
689 : (void)_rControlValueType;
690 :
691 0 : OUString sScript( aScriptEvent.ScriptCode );
692 0 : if ( !sScript.isEmpty() )
693 : {
694 : // format is: "name (location, language)"
695 : try
696 : {
697 : // parse
698 0 : Reference< XUriReferenceFactory > xUriRefFac = UriReferenceFactory::create( m_xContext );
699 0 : Reference< XVndSunStarScriptUrlReference > xScriptUri( xUriRefFac->parse( sScript ), UNO_QUERY_THROW );
700 :
701 0 : OUStringBuffer aComposeBuffer;
702 :
703 : // name
704 0 : aComposeBuffer.append( xScriptUri->getName() );
705 :
706 : // location
707 0 : const OUString sLocationParamName( "location" );
708 0 : const OUString sLocation = xScriptUri->getParameter( sLocationParamName );
709 0 : const OUString sLangParamName( "language" );
710 0 : const OUString sLanguage = xScriptUri->getParameter( sLangParamName );
711 :
712 0 : if ( !(sLocation.isEmpty() && sLanguage.isEmpty()) )
713 : {
714 0 : aComposeBuffer.appendAscii( " (" );
715 :
716 : // location
717 : OSL_ENSURE( !sLocation.isEmpty(), "EventHandler::convertToControlValue: unexpected: no location!" );
718 0 : if ( !sLocation.isEmpty() )
719 : {
720 0 : aComposeBuffer.append( sLocation );
721 0 : aComposeBuffer.appendAscii( ", " );
722 : }
723 :
724 : // language
725 0 : if ( !sLanguage.isEmpty() )
726 : {
727 0 : aComposeBuffer.append( sLanguage );
728 : }
729 :
730 0 : aComposeBuffer.append( sal_Unicode( ')' ) );
731 : }
732 :
733 0 : sScript = aComposeBuffer.makeStringAndClear();
734 : }
735 0 : catch( const Exception& )
736 : {
737 : DBG_UNHANDLED_EXCEPTION();
738 : }
739 : }
740 :
741 0 : return makeAny( sScript );
742 : }
743 :
744 : //--------------------------------------------------------------------
745 0 : PropertyState SAL_CALL EventHandler::getPropertyState( const OUString& /*_rPropertyName*/ ) throw (UnknownPropertyException, RuntimeException)
746 : {
747 0 : return PropertyState_DIRECT_VALUE;
748 : }
749 :
750 : //--------------------------------------------------------------------
751 0 : void SAL_CALL EventHandler::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException)
752 : {
753 0 : ::osl::MutexGuard aGuard( m_aMutex );
754 0 : if ( !_rxListener.is() )
755 0 : throw NullPointerException();
756 0 : m_aPropertyListeners.addListener( _rxListener );
757 0 : }
758 :
759 : //--------------------------------------------------------------------
760 0 : void SAL_CALL EventHandler::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException)
761 : {
762 0 : ::osl::MutexGuard aGuard( m_aMutex );
763 0 : m_aPropertyListeners.removeListener( _rxListener );
764 0 : }
765 :
766 : //--------------------------------------------------------------------
767 0 : Sequence< Property > SAL_CALL EventHandler::getSupportedProperties() throw (RuntimeException)
768 : {
769 0 : ::osl::MutexGuard aGuard( m_aMutex );
770 0 : if ( !m_bEventsMapInitialized )
771 : {
772 0 : const_cast< EventHandler* >( this )->m_bEventsMapInitialized = true;
773 : try
774 : {
775 0 : Sequence< Type > aListeners;
776 0 : impl_getCopmonentListenerTypes_nothrow( aListeners );
777 0 : sal_Int32 listenerCount = aListeners.getLength();
778 :
779 0 : Property aCurrentProperty;
780 0 : OUString sListenerClassName;
781 :
782 : // loop through all listeners and all methods, and see which we can present at the UI
783 0 : const Type* pListeners = aListeners.getConstArray();
784 0 : for ( sal_Int32 listener = 0; listener < listenerCount; ++listener, ++pListeners )
785 : {
786 0 : aCurrentProperty = Property();
787 :
788 : // the programmatic name of the listener, to be used as "property" name
789 0 : sListenerClassName = pListeners->getTypeName();
790 : OSL_ENSURE( !sListenerClassName.isEmpty(), "EventHandler::getSupportedProperties: strange - no listener name ..." );
791 0 : if ( sListenerClassName.isEmpty() )
792 0 : continue;
793 :
794 : // loop through all methods
795 0 : Sequence< OUString > aMethods( comphelper::getEventMethodsForType( *pListeners ) );
796 :
797 0 : const OUString* pMethods = aMethods.getConstArray();
798 0 : sal_uInt32 methodCount = aMethods.getLength();
799 :
800 0 : for (sal_uInt32 method = 0 ; method < methodCount ; ++method, ++pMethods )
801 : {
802 0 : EventDescription aEvent;
803 0 : if ( !lcl_getEventDescriptionForMethod( *pMethods, aEvent ) )
804 0 : continue;
805 :
806 0 : if ( !impl_filterMethod_nothrow( aEvent ) )
807 0 : continue;
808 :
809 : const_cast< EventHandler* >( this )->m_aEvents.insert( EventMap::value_type(
810 0 : lcl_getEventPropertyName( sListenerClassName, *pMethods ), aEvent ) );
811 0 : }
812 0 : }
813 :
814 : }
815 0 : catch( const Exception& )
816 : {
817 : DBG_UNHANDLED_EXCEPTION();
818 : }
819 : }
820 :
821 : // sort them by ID - this is the relative ordering in the UI
822 0 : ::std::map< EventId, Property > aOrderedProperties;
823 0 : for ( EventMap::const_iterator loop = m_aEvents.begin();
824 0 : loop != m_aEvents.end();
825 : ++loop
826 : )
827 : {
828 0 : aOrderedProperties[ loop->second.nId ] = Property(
829 0 : loop->first, loop->second.nId,
830 0 : ::getCppuType( static_cast< const OUString* >( NULL ) ),
831 0 : PropertyAttribute::BOUND );
832 : }
833 :
834 0 : StlSyntaxSequence< Property > aReturn( aOrderedProperties.size() );
835 : ::std::transform( aOrderedProperties.begin(), aOrderedProperties.end(), aReturn.begin(),
836 0 : ::o3tl::select2nd< ::std::map< EventId, Property >::value_type >() );
837 0 : return aReturn;
838 : }
839 :
840 : //--------------------------------------------------------------------
841 0 : Sequence< OUString > SAL_CALL EventHandler::getSupersededProperties( ) throw (RuntimeException)
842 : {
843 : // none
844 0 : return Sequence< OUString >( );
845 : }
846 :
847 : //--------------------------------------------------------------------
848 0 : Sequence< OUString > SAL_CALL EventHandler::getActuatingProperties( ) throw (RuntimeException)
849 : {
850 : // none
851 0 : return Sequence< OUString >( );
852 : }
853 :
854 : //--------------------------------------------------------------------
855 0 : LineDescriptor SAL_CALL EventHandler::describePropertyLine( const OUString& _rPropertyName,
856 : const Reference< XPropertyControlFactory >& _rxControlFactory )
857 : throw (UnknownPropertyException, NullPointerException, RuntimeException)
858 : {
859 0 : if ( !_rxControlFactory.is() )
860 0 : throw NullPointerException();
861 :
862 0 : ::osl::MutexGuard aGuard( m_aMutex );
863 :
864 0 : LineDescriptor aDescriptor;
865 :
866 0 : aDescriptor.Control = _rxControlFactory->createPropertyControl( PropertyControlType::TextField, sal_True );
867 0 : Reference< XEventListener > xControlExtender = new PropertyControlExtender( aDescriptor.Control );
868 :
869 0 : const EventDescription& rEvent = impl_getEventForName_throw( _rPropertyName );
870 0 : aDescriptor.DisplayName = rEvent.sDisplayName;
871 0 : aDescriptor.HelpURL = HelpIdUrl::getHelpURL( rEvent.sHelpId );
872 0 : aDescriptor.PrimaryButtonId = OStringToOUString(rEvent.sUniqueBrowseId, RTL_TEXTENCODING_UTF8);
873 0 : aDescriptor.HasPrimaryButton = sal_True;
874 0 : aDescriptor.Category = OUString( "Events" );
875 0 : return aDescriptor;
876 : }
877 :
878 : //--------------------------------------------------------------------
879 0 : ::sal_Bool SAL_CALL EventHandler::isComposable( const OUString& /*_rPropertyName*/ ) throw (UnknownPropertyException, RuntimeException)
880 : {
881 0 : return sal_False;
882 : }
883 :
884 : //--------------------------------------------------------------------
885 0 : InteractiveSelectionResult SAL_CALL EventHandler::onInteractivePropertySelection( const OUString& _rPropertyName, sal_Bool /*_bPrimary*/, Any& /*_rData*/, const Reference< XObjectInspectorUI >& _rxInspectorUI ) throw (UnknownPropertyException, NullPointerException, RuntimeException)
886 : {
887 0 : if ( !_rxInspectorUI.is() )
888 0 : throw NullPointerException();
889 :
890 0 : ::osl::MutexGuard aGuard( m_aMutex );
891 0 : const EventDescription& rForEvent = impl_getEventForName_throw( _rPropertyName );
892 :
893 0 : Sequence< ScriptEventDescriptor > aAllAssignedEvents;
894 0 : impl_getComponentScriptEvents_nothrow( aAllAssignedEvents );
895 :
896 : // SvxMacroAssignDlg-compatible structure holding all event/assignments
897 0 : ::rtl::Reference< EventHolder > pEventHolder( new EventHolder );
898 :
899 0 : for ( EventMap::const_iterator event = m_aEvents.begin();
900 0 : event != m_aEvents.end();
901 : ++event
902 : )
903 : {
904 : // the script which is assigned to the current event (if any)
905 0 : ScriptEventDescriptor aAssignedScript = lcl_getAssignedScriptEvent( event->second, aAllAssignedEvents );
906 0 : pEventHolder->addEvent( event->second.nId, event->second.sListenerMethodName, aAssignedScript );
907 0 : }
908 :
909 : // the initial selection in the dialog
910 0 : Sequence< OUString > aNames( pEventHolder->getElementNames() );
911 0 : const OUString* pChosenEvent = ::std::find( aNames.getConstArray(), aNames.getConstArray() + aNames.getLength(), rForEvent.sListenerMethodName );
912 0 : sal_uInt16 nInitialSelection = (sal_uInt16)( pChosenEvent - aNames.getConstArray() );
913 :
914 : // the dialog
915 0 : SvxAbstractDialogFactory* pFactory = SvxAbstractDialogFactory::Create();
916 0 : if ( !pFactory )
917 0 : return InteractiveSelectionResult_Cancelled;
918 :
919 : ::std::auto_ptr< VclAbstractDialog > pDialog( pFactory->CreateSvxMacroAssignDlg(
920 : PropertyHandlerHelper::getDialogParentWindow( m_xContext ),
921 : impl_getContextFrame_nothrow(),
922 : m_bIsDialogElement,
923 0 : pEventHolder.get(),
924 : nInitialSelection
925 0 : ) );
926 :
927 0 : if ( !pDialog.get() )
928 0 : return InteractiveSelectionResult_Cancelled;
929 :
930 : // DF definite problem here
931 : // OK & Cancel seem to be both returning 0
932 0 : if ( pDialog->Execute() == RET_CANCEL )
933 0 : return InteractiveSelectionResult_Cancelled;
934 :
935 : try
936 : {
937 0 : for ( EventMap::const_iterator event = m_aEvents.begin();
938 0 : event != m_aEvents.end();
939 : ++event
940 : )
941 : {
942 0 : ScriptEventDescriptor aScriptDescriptor( pEventHolder->getNormalizedDescriptorByName( event->second.sListenerMethodName ) );
943 :
944 : // set the new "property value"
945 : setPropertyValue(
946 0 : lcl_getEventPropertyName( event->second.sListenerClassName, event->second.sListenerMethodName ),
947 : makeAny( aScriptDescriptor )
948 0 : );
949 0 : }
950 : }
951 0 : catch( const Exception& )
952 : {
953 : DBG_UNHANDLED_EXCEPTION();
954 : }
955 :
956 0 : return InteractiveSelectionResult_Success;
957 : }
958 :
959 : //--------------------------------------------------------------------
960 0 : void SAL_CALL EventHandler::actuatingPropertyChanged( const OUString& /*_rActuatingPropertyName*/, const Any& /*_rNewValue*/, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/, sal_Bool /*_bFirstTimeInit*/ ) throw (NullPointerException, RuntimeException)
961 : {
962 : OSL_FAIL( "EventHandler::actuatingPropertyChanged: no actuating properties -> no callback (well, this is how it *should* be!)" );
963 0 : }
964 :
965 : //--------------------------------------------------------------------
966 0 : IMPLEMENT_FORWARD_XCOMPONENT( EventHandler, EventHandler_Base )
967 :
968 : //--------------------------------------------------------------------
969 0 : void SAL_CALL EventHandler::disposing()
970 : {
971 0 : EventMap aEmpty;
972 0 : m_aEvents.swap( aEmpty );
973 0 : m_xComponent.clear();
974 0 : }
975 :
976 : //--------------------------------------------------------------------
977 0 : sal_Bool SAL_CALL EventHandler::suspend( sal_Bool /*_bSuspend*/ ) throw (RuntimeException)
978 : {
979 0 : return sal_True;
980 : }
981 :
982 : //------------------------------------------------------------------------
983 0 : Reference< XFrame > EventHandler::impl_getContextFrame_nothrow() const
984 : {
985 0 : Reference< XFrame > xContextFrame;
986 :
987 : try
988 : {
989 0 : Reference< XModel > xContextDocument( PropertyHandlerHelper::getContextDocument(m_xContext), UNO_QUERY_THROW );
990 0 : Reference< XController > xController( xContextDocument->getCurrentController(), UNO_SET_THROW );
991 0 : xContextFrame.set( xController->getFrame(), UNO_SET_THROW );
992 : }
993 0 : catch( const Exception& )
994 : {
995 : DBG_UNHANDLED_EXCEPTION();
996 : }
997 :
998 0 : return xContextFrame;
999 : }
1000 :
1001 : //--------------------------------------------------------------------
1002 0 : sal_Int32 EventHandler::impl_getComponentIndexInParent_throw() const
1003 : {
1004 0 : Reference< XChild > xChild( m_xComponent, UNO_QUERY_THROW );
1005 0 : Reference< XIndexAccess > xParentAsIndexAccess( xChild->getParent(), UNO_QUERY_THROW );
1006 :
1007 : // get the index of the inspected object within it's parent container
1008 0 : sal_Int32 nElements = xParentAsIndexAccess->getCount();
1009 0 : for ( sal_Int32 i=0; i<nElements; ++i )
1010 : {
1011 0 : Reference< XInterface > xElement( xParentAsIndexAccess->getByIndex( i ), UNO_QUERY_THROW );
1012 0 : if ( xElement == m_xComponent )
1013 0 : return i;
1014 0 : }
1015 0 : throw NoSuchElementException();
1016 : }
1017 :
1018 : //--------------------------------------------------------------------
1019 0 : void EventHandler::impl_getFormComponentScriptEvents_nothrow( Sequence < ScriptEventDescriptor >& _out_rEvents ) const
1020 : {
1021 0 : _out_rEvents = Sequence < ScriptEventDescriptor >();
1022 : try
1023 : {
1024 0 : Reference< XChild > xChild( m_xComponent, UNO_QUERY_THROW );
1025 0 : Reference< XEventAttacherManager > xEventManager( xChild->getParent(), UNO_QUERY_THROW );
1026 0 : _out_rEvents = xEventManager->getScriptEvents( impl_getComponentIndexInParent_throw() );
1027 :
1028 : // the form component script API has unqualified listener names, but for normalization
1029 : // purpose, we want fully qualified ones
1030 0 : ScriptEventDescriptor* pEvents = _out_rEvents.getArray();
1031 0 : ScriptEventDescriptor* pEventsEnd = _out_rEvents.getArray() + _out_rEvents.getLength();
1032 0 : while ( pEvents != pEventsEnd )
1033 : {
1034 0 : pEvents->ListenerType = lcl_getQualifiedKnownListenerName( *pEvents );
1035 0 : ++pEvents;
1036 0 : }
1037 : }
1038 0 : catch( const Exception& )
1039 : {
1040 : DBG_UNHANDLED_EXCEPTION();
1041 : }
1042 0 : }
1043 :
1044 : //--------------------------------------------------------------------
1045 0 : void EventHandler::impl_getCopmonentListenerTypes_nothrow( Sequence< Type >& _out_rTypes ) const
1046 : {
1047 0 : _out_rTypes.realloc( 0 );
1048 : try
1049 : {
1050 : // we use a set to avoid duplicates
1051 0 : TypeBag aListeners;
1052 :
1053 0 : Reference< XIntrospection > xIntrospection = Introspection::create( m_xContext );
1054 :
1055 : // --- model listeners
1056 : lcl_addListenerTypesFor_throw(
1057 0 : m_xComponent, xIntrospection, aListeners );
1058 :
1059 : // --- "secondary component" (usually: "control" listeners)
1060 : {
1061 0 : Reference< XInterface > xSecondaryComponent( impl_getSecondaryComponentForEventInspection_throw() );
1062 0 : lcl_addListenerTypesFor_throw( xSecondaryComponent, xIntrospection, aListeners );
1063 0 : ::comphelper::disposeComponent( xSecondaryComponent );
1064 : }
1065 :
1066 : // now that they're disambiguated, copy these types into our member
1067 0 : _out_rTypes.realloc( aListeners.size() );
1068 0 : ::std::copy( aListeners.begin(), aListeners.end(), _out_rTypes.getArray() );
1069 : }
1070 0 : catch( const Exception& )
1071 : {
1072 : DBG_UNHANDLED_EXCEPTION();
1073 : }
1074 0 : }
1075 :
1076 : //--------------------------------------------------------------------
1077 0 : void EventHandler::impl_getDialogElementScriptEvents_nothrow( Sequence < ScriptEventDescriptor >& _out_rEvents ) const
1078 : {
1079 0 : _out_rEvents = Sequence < ScriptEventDescriptor >();
1080 : try
1081 : {
1082 0 : Reference< XScriptEventsSupplier > xEventsSupplier( m_xComponent, UNO_QUERY_THROW );
1083 0 : Reference< XNameContainer > xEvents( xEventsSupplier->getEvents(), UNO_QUERY_THROW );
1084 0 : Sequence< OUString > aEventNames( xEvents->getElementNames() );
1085 :
1086 0 : sal_Int32 nEventCount = aEventNames.getLength();
1087 0 : _out_rEvents.realloc( nEventCount );
1088 :
1089 0 : const OUString* pNames = aEventNames.getConstArray();
1090 0 : ScriptEventDescriptor* pDescs = _out_rEvents.getArray();
1091 :
1092 0 : for( sal_Int32 i = 0 ; i < nEventCount ; ++i, ++pNames, ++pDescs )
1093 0 : OSL_VERIFY( xEvents->getByName( *pNames ) >>= *pDescs );
1094 : }
1095 0 : catch( const Exception& )
1096 : {
1097 : DBG_UNHANDLED_EXCEPTION();
1098 : }
1099 0 : }
1100 :
1101 : //--------------------------------------------------------------------
1102 0 : Reference< XInterface > EventHandler::impl_getSecondaryComponentForEventInspection_throw( ) const
1103 : {
1104 0 : Reference< XInterface > xReturn;
1105 :
1106 : // if it's a form, create a form controller for the additional events
1107 0 : Reference< XForm > xComponentAsForm( m_xComponent, UNO_QUERY );
1108 0 : if ( xComponentAsForm.is() )
1109 : {
1110 0 : Reference< XTabControllerModel > xComponentAsTCModel( m_xComponent, UNO_QUERY_THROW );
1111 0 : Reference< XFormController > xController = FormController::create( m_xContext );
1112 0 : xController->setModel( xComponentAsTCModel );
1113 :
1114 0 : xReturn = xController;
1115 : }
1116 : else
1117 : {
1118 0 : OUString sControlService;
1119 0 : OSL_VERIFY( m_xComponent->getPropertyValue( PROPERTY_DEFAULTCONTROL ) >>= sControlService );
1120 :
1121 0 : xReturn = m_xContext->getServiceManager()->createInstanceWithContext( sControlService, m_xContext );
1122 : }
1123 0 : return xReturn;
1124 : }
1125 :
1126 : //--------------------------------------------------------------------
1127 0 : const EventDescription& EventHandler::impl_getEventForName_throw( const OUString& _rPropertyName ) const
1128 : {
1129 0 : EventMap::const_iterator pos = m_aEvents.find( _rPropertyName );
1130 0 : if ( pos == m_aEvents.end() )
1131 0 : throw UnknownPropertyException();
1132 0 : return pos->second;
1133 : }
1134 :
1135 : //--------------------------------------------------------------------
1136 : namespace
1137 : {
1138 0 : static bool lcl_endsWith( const OUString& _rText, const OUString& _rCheck )
1139 : {
1140 0 : sal_Int32 nTextLen = _rText.getLength();
1141 0 : sal_Int32 nCheckLen = _rCheck.getLength();
1142 0 : if ( nCheckLen > nTextLen )
1143 0 : return false;
1144 :
1145 0 : return _rText.indexOf( _rCheck ) == ( nTextLen - nCheckLen );
1146 : }
1147 : }
1148 : //--------------------------------------------------------------------
1149 0 : void EventHandler::impl_setFormComponentScriptEvent_nothrow( const ScriptEventDescriptor& _rScriptEvent )
1150 : {
1151 : try
1152 : {
1153 0 : OUString sScriptCode( _rScriptEvent.ScriptCode );
1154 0 : OUString sScriptType( _rScriptEvent.ScriptType );
1155 0 : bool bResetScript = sScriptCode.isEmpty();
1156 :
1157 0 : sal_Int32 nObjectIndex = impl_getComponentIndexInParent_throw();
1158 0 : Reference< XChild > xChild( m_xComponent, UNO_QUERY_THROW );
1159 0 : Reference< XEventAttacherManager > xEventManager( xChild->getParent(), UNO_QUERY_THROW );
1160 0 : Sequence< ScriptEventDescriptor > aEvents( xEventManager->getScriptEvents( nObjectIndex ) );
1161 :
1162 : // is there already a registered script for this event?
1163 0 : ScriptEventDescriptor* pEvent = aEvents.getArray();
1164 0 : sal_Int32 eventCount = aEvents.getLength(), event = 0;
1165 0 : for ( event = 0; event < eventCount; ++event, ++pEvent )
1166 : {
1167 0 : if ( ( pEvent->EventMethod == _rScriptEvent.EventMethod )
1168 0 : && ( lcl_endsWith( _rScriptEvent.ListenerType, pEvent->ListenerType ) )
1169 : // (strange enough, the events we get from getScriptEvents are not fully qualified)
1170 : )
1171 : {
1172 : // yes
1173 0 : if ( !bResetScript )
1174 : {
1175 : // set to something non-empty -> overwrite
1176 0 : pEvent->ScriptCode = sScriptCode;
1177 0 : pEvent->ScriptType = sScriptType;
1178 : }
1179 : else
1180 : {
1181 : // set to empty -> remove from sequence
1182 0 : ::std::copy( pEvent + 1, aEvents.getArray() + eventCount, pEvent );
1183 0 : aEvents.realloc( eventCount - 1 );
1184 0 : --eventCount;
1185 : }
1186 0 : break;
1187 : }
1188 : }
1189 0 : if ( ( event >= eventCount ) && !bResetScript )
1190 : {
1191 : // no, did not find it -> append
1192 0 : aEvents.realloc( eventCount + 1 );
1193 0 : aEvents[ eventCount ] = _rScriptEvent;
1194 : }
1195 :
1196 0 : xEventManager->revokeScriptEvents( nObjectIndex );
1197 0 : xEventManager->registerScriptEvents( nObjectIndex, aEvents );
1198 :
1199 0 : PropertyHandlerHelper::setContextDocumentModified( m_xContext );
1200 : }
1201 0 : catch( const Exception& )
1202 : {
1203 : DBG_UNHANDLED_EXCEPTION();
1204 : }
1205 0 : }
1206 :
1207 : //--------------------------------------------------------------------
1208 0 : void EventHandler::impl_setDialogElementScriptEvent_nothrow( const ScriptEventDescriptor& _rScriptEvent )
1209 : {
1210 : try
1211 : {
1212 0 : OUString sScriptCode( _rScriptEvent.ScriptCode );
1213 0 : bool bResetScript = sScriptCode.isEmpty();
1214 :
1215 0 : Reference< XScriptEventsSupplier > xEventsSupplier( m_xComponent, UNO_QUERY_THROW );
1216 0 : Reference< XNameContainer > xEvents( xEventsSupplier->getEvents(), UNO_QUERY_THROW );
1217 :
1218 0 : OUStringBuffer aCompleteName;
1219 0 : aCompleteName.append( _rScriptEvent.ListenerType );
1220 0 : aCompleteName.appendAscii( "::" );
1221 0 : aCompleteName.append( _rScriptEvent.EventMethod );
1222 0 : OUString sCompleteName( aCompleteName.makeStringAndClear() );
1223 :
1224 0 : bool bExists = xEvents->hasByName( sCompleteName );
1225 :
1226 0 : if ( bResetScript )
1227 : {
1228 0 : if ( bExists )
1229 0 : xEvents->removeByName( sCompleteName );
1230 : }
1231 : else
1232 : {
1233 0 : Any aNewValue; aNewValue <<= _rScriptEvent;
1234 :
1235 0 : if ( bExists )
1236 0 : xEvents->replaceByName( sCompleteName, aNewValue );
1237 : else
1238 0 : xEvents->insertByName( sCompleteName, aNewValue );
1239 0 : }
1240 : }
1241 0 : catch( const Exception& )
1242 : {
1243 : DBG_UNHANDLED_EXCEPTION();
1244 : }
1245 0 : }
1246 :
1247 : //--------------------------------------------------------------------
1248 0 : bool EventHandler::impl_filterMethod_nothrow( const EventDescription& _rEvent ) const
1249 : {
1250 : // some (control-triggered) events do not make sense for certain grid control columns. However,
1251 : // our mechnism to retrieve control-triggered events does not know about this, so we do some
1252 : // late filtering here.
1253 0 : switch ( m_nGridColumnType )
1254 : {
1255 : case FormComponentType::COMBOBOX:
1256 0 : if ( UID_BRWEVT_ACTIONPERFORMED == _rEvent.sUniqueBrowseId )
1257 0 : return false;
1258 0 : break;
1259 : case FormComponentType::LISTBOX:
1260 0 : if ( ( UID_BRWEVT_CHANGED == _rEvent.sUniqueBrowseId )
1261 0 : || ( UID_BRWEVT_ACTIONPERFORMED == _rEvent.sUniqueBrowseId )
1262 : )
1263 0 : return false;
1264 0 : break;
1265 : }
1266 :
1267 0 : return true;
1268 : }
1269 :
1270 : //........................................................................
1271 3 : } // namespace pcr
1272 : //........................................................................
1273 :
1274 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|