1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef EXTENSIONS_SOURCE_PROPCTRLR_EVENTHANDLER_HXX
29 #define EXTENSIONS_SOURCE_PROPCTRLR_EVENTHANDLER_HXX
30 
31 #include "pcrcomponentcontext.hxx"
32 #include "pcrcommontypes.hxx"
33 #include "pcrcommon.hxx"
34 
35 /** === begin UNO includes === **/
36 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
37 #include <com/sun/star/lang/XServiceInfo.hpp>
38 #include <com/sun/star/inspection/XPropertyHandler.hpp>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <com/sun/star/frame/XFrame.hpp>
41 /** === end UNO includes === **/
42 #include <cppuhelper/compbase2.hxx>
43 #include <comphelper/listenernotification.hxx>
44 
45 //........................................................................
46 namespace pcr
47 {
48 //........................................................................
49 
50     //====================================================================
51     //= EventDescription
52     //====================================================================
53     typedef sal_Int32   EventId;
54     struct EventDescription
55     {
56     public:
57         ::rtl::OUString sDisplayName;
58         ::rtl::OUString sListenerClassName;
59         ::rtl::OUString sListenerMethodName;
60         ::rtl::OString  sHelpId;
61         ::rtl::OString  sUniqueBrowseId;
62         EventId         nId;
63 
64         EventDescription()
65             :nId( 0 )
66             {
67             }
68 
69         EventDescription(
70             EventId _nId,
71             const sal_Char* _pListenerNamespaceAscii,
72             const sal_Char* _pListenerClassAsciiName,
73             const sal_Char* _pListenerMethodAsciiName,
74             sal_uInt16 _nDisplayNameResId,
75             const rtl::OString& _sHelpId,
76             const rtl::OString& _sUniqueBrowseId );
77     };
78 
79     typedef ::std::hash_map< ::rtl::OUString, EventDescription, ::rtl::OUStringHash >   EventMap;
80 
81     //====================================================================
82     //= EventHandler
83     //====================================================================
84     typedef ::cppu::WeakComponentImplHelper2    <   ::com::sun::star::inspection::XPropertyHandler
85                                                 ,   ::com::sun::star::lang::XServiceInfo
86                                                 >   EventHandler_Base;
87     class EventHandler : public EventHandler_Base
88     {
89     private:
90         mutable ::osl::Mutex    m_aMutex;
91 
92         /// the context in which the instance was created
93         ComponentContext                                                            m_aContext;
94         /// the properties of the object we're handling
95         ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >   m_xComponent;
96         /// our XPropertyChangeListener(s)
97         PropertyChangeListeners                                                     m_aPropertyListeners;
98         /// cache of the events we found at our introspectee
99         EventMap                                                                    m_aEvents;
100         /// has m_aEvents been initialized?
101         bool                                                                        m_bEventsMapInitialized;
102         /// is our introspectee a dialog element?
103         bool                                                                        m_bIsDialogElement;
104             // TODO: move different handling into different derived classes?
105         /// (FormComponent) type of the grid column being inspected, or -1 if we're not inspecting a grid column
106         sal_Int16                                                                   m_nGridColumnType;
107 
108     public:
109         // XServiceInfo - static versions
110         static ::rtl::OUString SAL_CALL getImplementationName_static(  ) throw (::com::sun::star::uno::RuntimeException);
111         static ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static(  ) throw (::com::sun::star::uno::RuntimeException);
112         static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > Create( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext );
113 
114     protected:
115         EventHandler(
116             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext
117        );
118 
119         ~EventHandler();
120 
121     protected:
122         // XPropertyHandler overridables
123         virtual void                                                SAL_CALL inspect( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxIntrospectee ) throw (::com::sun::star::lang::NullPointerException, ::com::sun::star::uno::RuntimeException);
124         virtual ::com::sun::star::uno::Any                          SAL_CALL getPropertyValue( const ::rtl::OUString& _rPropertyName ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException);
125         virtual void                                                SAL_CALL setPropertyValue( const ::rtl::OUString& _rPropertyName, const ::com::sun::star::uno::Any& _rValue ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException);
126         virtual ::com::sun::star::uno::Any                          SAL_CALL convertToPropertyValue( const ::rtl::OUString& _rPropertyName, const ::com::sun::star::uno::Any& _rControlValue ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException);
127         virtual ::com::sun::star::uno::Any                          SAL_CALL convertToControlValue( const ::rtl::OUString& _rPropertyName, const ::com::sun::star::uno::Any& _rPropertyValue, const ::com::sun::star::uno::Type& _rControlValueType ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException);
128         virtual ::com::sun::star::beans::PropertyState              SAL_CALL getPropertyState( const ::rtl::OUString& _rPropertyName ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException);
129         virtual void                                                SAL_CALL addPropertyChangeListener( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& _rxListener ) throw (::com::sun::star::uno::RuntimeException);
130         virtual void                                                SAL_CALL removePropertyChangeListener( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& _rxListener ) throw (::com::sun::star::uno::RuntimeException);
131         virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property >
132                                                                     SAL_CALL getSupportedProperties() throw (::com::sun::star::uno::RuntimeException);
133         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >  SAL_CALL getSupersededProperties( ) throw (::com::sun::star::uno::RuntimeException);
134         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >  SAL_CALL getActuatingProperties( ) throw (::com::sun::star::uno::RuntimeException);
135         virtual ::com::sun::star::inspection::LineDescriptor        SAL_CALL describePropertyLine( const ::rtl::OUString& _rPropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControlFactory >& _rxControlFactory ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::NullPointerException, ::com::sun::star::uno::RuntimeException);
136         virtual ::sal_Bool                                          SAL_CALL isComposable( const ::rtl::OUString& _rPropertyName ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException);
137         virtual ::com::sun::star::inspection::InteractiveSelectionResult
138                                                                     SAL_CALL onInteractivePropertySelection( const ::rtl::OUString& _rPropertyName, sal_Bool _bPrimary, ::com::sun::star::uno::Any& _rData, const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XObjectInspectorUI >& _rxInspectorUI ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::NullPointerException, ::com::sun::star::uno::RuntimeException);
139         virtual void                                                SAL_CALL actuatingPropertyChanged( const ::rtl::OUString& _rActuatingPropertyName, const ::com::sun::star::uno::Any& _rNewValue, const ::com::sun::star::uno::Any& _rOldValue, const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XObjectInspectorUI >& _rxInspectorUI, sal_Bool _bFirstTimeInit ) throw (::com::sun::star::lang::NullPointerException, ::com::sun::star::uno::RuntimeException);
140         virtual sal_Bool                                            SAL_CALL suspend( sal_Bool _bSuspend ) throw (::com::sun::star::uno::RuntimeException);
141 
142         // XComponent
143         DECLARE_XCOMPONENT()
144         virtual void                                                SAL_CALL disposing();
145 
146         // XServiceInfo
147         virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException);
148         virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (::com::sun::star::uno::RuntimeException);
149         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (::com::sun::star::uno::RuntimeException);
150 
151     private:
152         /** returns the script events associated with our introspectee
153             @param  _out_rEvents
154                 Takes, upon successfull return, the events currently associated with the introspectee
155             @precond
156                 Our introspectee is a form component
157         */
158         void    impl_getFormComponentScriptEvents_nothrow(
159                     ::com::sun::star::uno::Sequence< ::com::sun::star::script::ScriptEventDescriptor >& _out_rEvents
160                 ) const;
161 
162         /** returns the script events associated with our introspectee
163             @param  _out_rEvents
164                 Takes, upon successfull return, the events currently associated with the introspectee
165             @precond
166                 Our introspectee is a dialog element
167         */
168         void    impl_getDialogElementScriptEvents_nothrow(
169                     ::com::sun::star::uno::Sequence< ::com::sun::star::script::ScriptEventDescriptor >& _out_rEvents
170                 ) const;
171 
172         /** returns the script events associated with our introspectee
173             @param  _out_rEvents
174                 Takes, the events currently associated with the introspectee
175         */
176         inline void impl_getComponentScriptEvents_nothrow(
177                     ::com::sun::star::uno::Sequence< ::com::sun::star::script::ScriptEventDescriptor >& _out_rEvents
178                 ) const
179         {
180             if ( m_bIsDialogElement )
181                 impl_getDialogElementScriptEvents_nothrow( _out_rEvents );
182             else
183                 impl_getFormComponentScriptEvents_nothrow( _out_rEvents );
184         }
185 
186         /** returns the types of the listeners which can be registered at our introspectee
187             @param  _out_rTypes
188                 Takes, upon successfull return, the types of possible listeners at the introspectee
189         */
190         void    impl_getCopmonentListenerTypes_nothrow(
191                     ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type >& _out_rTypes
192                 ) const;
193 
194         /** returns a secondary component to be used for event inspection
195 
196             In the UI, we want to mix events for the control model with events for the control.
197             Since our introspectee is a model, this method creates a control for it (if possible).
198 
199             @return
200                 the secondary component whose events should be mixed with the introspectee's events
201                 The caller takes the ownership of the component (if not <NULL/>).
202 
203             @throws
204                 if an unexpected error occurs during creation of the secondary component.
205                 A <NULL/> component to be returned is not unexpected, but allowed
206 
207             @precond
208                 ->m_xComponent is not <NULL/>
209         */
210         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
211                 impl_getSecondaryComponentForEventInspection_throw( ) const;
212 
213         /** returns the event description for the given (programmatic) property name
214             @param  _rPropertyName
215                 the name whose event description should be looked up
216             @return
217                 the event description for the property name
218             @throws ::com::sun::star::beans::UnknownPropertyException
219                 if our introspectee does not have an event with the given logical name (see ->getSupportedProperties)
220         */
221         const EventDescription&
222                 impl_getEventForName_throw( const ::rtl::OUString& _rPropertyName ) const;
223 
224         /** returns the index of our component within its parent, if this parent can be
225             obtained  (XChild::getParent) and supports an ->XIndexAccess interface
226         */
227         sal_Int32   impl_getComponentIndexInParent_throw() const;
228 
229         /** sets a given script event as event handler at a form component
230 
231             @param  _rScriptEvent
232                 the script event to set
233         */
234         void    impl_setFormComponentScriptEvent_nothrow( const ::com::sun::star::script::ScriptEventDescriptor& _rScriptEvent );
235 
236         /** sets a given script event as event handler at a dialog component
237 
238             @param  _rScriptEvent
239                 the script event to set
240         */
241         void    impl_setDialogElementScriptEvent_nothrow( const ::com::sun::star::script::ScriptEventDescriptor& _rScriptEvent );
242 
243         /** returns the frame associated with our context document
244         */
245         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >
246                 impl_getContextFrame_nothrow() const;
247 
248         /** approves or denies a certain method to be included in the UI
249             @return
250                 <TRUE/> if and only if the given method is allowed.
251         */
252         bool    impl_filterMethod_nothrow( const EventDescription& _rEvent ) const;
253 
254     private:
255         EventHandler();                                 // never implemented
256         EventHandler( const EventHandler& );            // never implemented
257         EventHandler& operator=( const EventHandler& ); // never implemented
258     };
259 
260 //........................................................................
261 } // namespace pcr
262 //........................................................................
263 
264 #endif // EXTENSIONS_SOURCE_PROPCTRLR_EVENTHANDLER_HXX
265 
266