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