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_PROPCTRLR_PROPCONTROLLER_HXX_
29 #define _EXTENSIONS_PROPCTRLR_PROPCONTROLLER_HXX_
30 
31 #include "composeduiupdate.hxx"
32 #include "formbrowsertools.hxx"
33 #include "formmetadata.hxx"
34 #include "proplinelistener.hxx"
35 #include "propcontrolobserver.hxx"
36 #include "browserview.hxx"
37 #ifndef _EXTENSIONS_PROPCTRLR_MODULEPCR_HXX_
38 #include "modulepcr.hxx"
39 #endif
40 #include "propertyinfo.hxx"
41 #include "pcrcomponentcontext.hxx"
42 
43 /** === begin UNO includes === **/
44 #include <com/sun/star/awt/XFocusListener.hpp>
45 #include <com/sun/star/beans/XPropertyState.hpp>
46 #include <com/sun/star/beans/XPropertySet.hpp>
47 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
48 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
49 #include <com/sun/star/uno/XComponentContext.hpp>
50 #include <com/sun/star/form/XForm.hpp>
51 #include <com/sun/star/script/XEventAttacherManager.hpp>
52 #include <com/sun/star/sdbc/XRowSet.hpp>
53 #include <com/sun/star/uno/Sequence.hxx>
54 #include <com/sun/star/frame/XController.hpp>
55 #include <com/sun/star/lang/XServiceInfo.hpp>
56 #include <com/sun/star/lang/XEventListener.hpp>
57 #include <com/sun/star/sdbc/XConnection.hpp>
58 #include <com/sun/star/awt/XLayoutConstrains.hpp>
59 #include <com/sun/star/awt/XLayoutConstrains.hpp>
60 #include <com/sun/star/awt/XControlContainer.hpp>
61 #include <com/sun/star/inspection/XPropertyControlFactory.hpp>
62 #include <com/sun/star/inspection/XObjectInspector.hpp>
63 #include <com/sun/star/inspection/XObjectInspectorUI.hpp>
64 #include <com/sun/star/inspection/XPropertyHandler.hpp>
65 #include <com/sun/star/lang/XInitialization.hpp>
66 /** === end UNO includes === **/
67 #include <connectivity/dbtools.hxx>
68 #include <cppuhelper/interfacecontainer.hxx>
69 #include <cppuhelper/implbase7.hxx>
70 #include <comphelper/broadcasthelper.hxx>
71 
72 #include <map>
73 #include <hash_map>
74 #include <vector>
75 #include <memory>
76 
77 class SvNumberFormatsSupplierObj;
78 class Font;
79 class Window;
80 class SfxItemSet;
81 
82 //............................................................................
83 namespace pcr
84 {
85 //............................................................................
86 
87 	class OPropertyEditor;
88 	struct OLineDescriptor;
89 
90 #if OSL_DEBUG_LEVEL > 0
91     const char* CheckPropertyBrowserInvariants( const void* pVoid );
92         // for dignostics with DBG_CHKTHIS
93 #endif
94     DBG_NAMEEX( OPropertyBrowserController )
95 
96     //========================================================================
97 	//= OPropertyBrowserController
98 	//========================================================================
99 	// #95343#------------------------------------------------------------------------------------
100 	typedef ::cppu::WeakImplHelper7 <	::com::sun::star::lang::XServiceInfo
101 									,	::com::sun::star::awt::XFocusListener
102 									,	::com::sun::star::awt::XLayoutConstrains
103 									,	::com::sun::star::beans::XPropertyChangeListener
104                                     ,   ::com::sun::star::inspection::XPropertyControlFactory
105                                     ,   ::com::sun::star::inspection::XObjectInspector
106                                     ,   ::com::sun::star::lang::XInitialization
107 									>	OPropertyBrowserController_Base;
108 
109 	class OPropertyBrowserController
110                 :public ::comphelper::OMutexAndBroadcastHelper
111 				,public OPropertyBrowserController_Base
112                 ,public ::com::sun::star::inspection::XObjectInspectorUI
113                     // that's intentionally *not* part of the OPropertyBrowserController_Base
114                     // We do not want this to be available in queryInterface, getTypes, and the like.
115                 ,public IPropertyLineListener
116                 ,public IPropertyControlObserver
117                 ,public IPropertyExistenceCheck
118 	{
119     private:
120         typedef ::std::map< sal_Int32, ::com::sun::star::beans::Property >  OrderedPropertyMap;
121         typedef ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > >
122                                                                             InterfaceArray;
123 
124     protected:
125 		ComponentContext                                                    m_aContext;
126 
127     private:
128 		::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > m_xFrame;
129 		::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow >  m_xView;
130 
131 		::cppu::OInterfaceContainerHelper   m_aDisposeListeners;
132         ::cppu::OInterfaceContainerHelper   m_aControlObservers;
133 		// meta data about the properties
134         OPropertyBrowserView*               m_pView;
135 
136 		::rtl::OUString		                m_sPageSelection;
137 		::rtl::OUString		                m_sLastValidPageSelection;
138 
139         typedef ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyHandler >
140                                                         PropertyHandlerRef;
141         typedef ::std::vector< PropertyHandlerRef >     PropertyHandlerArray;
142         typedef ::std::hash_map< ::rtl::OUString, PropertyHandlerRef, ::rtl::OUStringHash >
143                                                         PropertyHandlerRepository;
144         typedef ::std::hash_multimap< ::rtl::OUString, PropertyHandlerRef, ::rtl::OUStringHash >
145                                                         PropertyHandlerMultiRepository;
146         PropertyHandlerRepository                       m_aPropertyHandlers;
147         PropertyHandlerMultiRepository                  m_aDependencyHandlers;
148         PropertyHandlerRef                              m_xInteractiveHandler;
149 
150         ::std::auto_ptr< ComposedPropertyUIUpdate >     m_pUIRequestComposer;
151 
152         /// our InspectorModel
153         ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XObjectInspectorModel >
154                                                         m_xModel;
155         /// the object(s) we're currently inspecting
156         InterfaceArray                                  m_aInspectedObjects;
157 		/// the properties of the currently inspected object(s)
158         OrderedPropertyMap                              m_aProperties;
159         /// the property we're just committing
160         ::rtl::OUString                                 m_sCommittingProperty;
161 
162         typedef ::std::hash_map< ::rtl::OUString, sal_uInt16, ::rtl::OUStringHash >     HashString2Int16;
163         HashString2Int16                                m_aPageIds;
164 
165 		bool        m_bContainerFocusListening;
166         bool        m_bSuspendingPropertyHandlers;
167         bool        m_bConstructed;
168         bool        m_bBindingIntrospectee;
169 
170 	protected:
171         DECLARE_XINTERFACE()
172 
173 		// XServiceInfo
174 		virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException);
175 		virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);
176 		virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException);
177 
178 		// XController
179 		virtual void SAL_CALL attachFrame( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame ) throw(::com::sun::star::uno::RuntimeException);
180 		virtual sal_Bool SAL_CALL attachModel( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >& xModel ) throw(::com::sun::star::uno::RuntimeException);
181 		virtual sal_Bool SAL_CALL suspend( sal_Bool bSuspend ) throw(::com::sun::star::uno::RuntimeException);
182 		virtual ::com::sun::star::uno::Any SAL_CALL getViewData(  ) throw(::com::sun::star::uno::RuntimeException);
183 		virtual void SAL_CALL restoreViewData( const ::com::sun::star::uno::Any& Data ) throw(::com::sun::star::uno::RuntimeException);
184 		virtual ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel > SAL_CALL getModel(  ) throw(::com::sun::star::uno::RuntimeException);
185 		virtual ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > SAL_CALL getFrame(  ) throw(::com::sun::star::uno::RuntimeException);
186 
187 		// XComponent
188 		virtual void SAL_CALL dispose(  ) throw(::com::sun::star::uno::RuntimeException);
189 		virtual void SAL_CALL addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& xListener ) throw(::com::sun::star::uno::RuntimeException);
190 		virtual void SAL_CALL removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw(::com::sun::star::uno::RuntimeException);
191 
192 		// XFocusListener
193 		virtual void SAL_CALL focusGained( const ::com::sun::star::awt::FocusEvent& _rSource ) throw (::com::sun::star::uno::RuntimeException);
194 		virtual void SAL_CALL focusLost( const ::com::sun::star::awt::FocusEvent& _rSource ) throw (::com::sun::star::uno::RuntimeException);
195 
196 		// XEventListener
197 	    virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw(::com::sun::star::uno::RuntimeException);
198 
199 		// XLayoutConstrains #95343# ----------------
200 		virtual ::com::sun::star::awt::Size SAL_CALL getMinimumSize(  ) throw (::com::sun::star::uno::RuntimeException);
201 		virtual ::com::sun::star::awt::Size SAL_CALL getPreferredSize(  ) throw (::com::sun::star::uno::RuntimeException);
202 		virtual ::com::sun::star::awt::Size SAL_CALL calcAdjustedSize( const ::com::sun::star::awt::Size& aNewSize ) throw (::com::sun::star::uno::RuntimeException);
203 
204         // XPropertyChangeListener
205         virtual void SAL_CALL propertyChange( const ::com::sun::star::beans::PropertyChangeEvent& _rEvent ) throw (::com::sun::star::uno::RuntimeException);
206 
207         /** XPropertyControlFactory
208         */
209         virtual ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControl > SAL_CALL createPropertyControl( ::sal_Int16 ControlType, ::sal_Bool CreateReadOnly ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
210 
211     public:
212 		OPropertyBrowserController(
213 			const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext);
214 
215     protected:
216 		virtual ~OPropertyBrowserController();
217 
218     public:
219 		// XServiceInfo - static versions
220 		static ::rtl::OUString getImplementationName_static(  ) throw(::com::sun::star::uno::RuntimeException);
221 		static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_static(  ) throw(::com::sun::star::uno::RuntimeException);
222 		static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL
223 						Create(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >&);
224 
225     protected:
226 		// IPropertyLineListener
227         virtual void	Clicked(	const ::rtl::OUString& _rName, sal_Bool _bPrimary );
228         virtual void	Commit(		const ::rtl::OUString& _rName, const ::com::sun::star::uno::Any& _rVal );
229 
230         // IPropertyControlObserver
231         virtual void    focusGained( const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControl >& _Control );
232         virtual void    valueChanged( const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControl >& _Control );
233 
234         // IPropertyExistenceCheck
235         virtual ::sal_Bool SAL_CALL hasPropertyByName( const ::rtl::OUString& _rName ) throw (::com::sun::star::uno::RuntimeException);
236 
237         // XObjectInspectorUI
238         virtual void SAL_CALL enablePropertyUI( const ::rtl::OUString& _rPropertyName, ::sal_Bool _bEnable ) throw (::com::sun::star::uno::RuntimeException);
239         virtual void SAL_CALL enablePropertyUIElements( const ::rtl::OUString& _rPropertyName, ::sal_Int16 _nElements, ::sal_Bool _bEnable ) throw (::com::sun::star::uno::RuntimeException);
240         virtual void SAL_CALL rebuildPropertyUI( const ::rtl::OUString& _rPropertyName ) throw (::com::sun::star::uno::RuntimeException);
241         virtual void SAL_CALL showPropertyUI( const ::rtl::OUString& _rPropertyName ) throw (::com::sun::star::uno::RuntimeException);
242         virtual void SAL_CALL hidePropertyUI( const ::rtl::OUString& _rPropertyName ) throw (::com::sun::star::uno::RuntimeException);
243         virtual void SAL_CALL showCategory( const ::rtl::OUString& _rCategory, ::sal_Bool _bShow ) throw (::com::sun::star::uno::RuntimeException);
244         virtual ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControl > SAL_CALL getPropertyControl( const ::rtl::OUString& _rPropertyName ) throw (::com::sun::star::uno::RuntimeException);
245         virtual void SAL_CALL registerControlObserver( const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControlObserver >& _Observer ) throw (::com::sun::star::uno::RuntimeException);
246         virtual void SAL_CALL revokeControlObserver( const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControlObserver >& _Observer ) throw (::com::sun::star::uno::RuntimeException);
247         virtual void SAL_CALL setHelpSectionText( const ::rtl::OUString& HelpText ) throw (::com::sun::star::lang::NoSupportException, ::com::sun::star::uno::RuntimeException);
248 
249         // XObjectInspector
250         virtual ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XObjectInspectorModel > SAL_CALL getInspectorModel() throw (::com::sun::star::uno::RuntimeException);
251         virtual void SAL_CALL setInspectorModel( const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XObjectInspectorModel >& _inspectormodel ) throw (::com::sun::star::uno::RuntimeException);
252         virtual ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XObjectInspectorUI > SAL_CALL getInspectorUI() throw (::com::sun::star::uno::RuntimeException);
253         virtual void SAL_CALL inspect( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > >& Objects ) throw (::com::sun::star::util::VetoException, ::com::sun::star::uno::RuntimeException);
254 
255         // XDispatchProvider
256         virtual ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > SAL_CALL queryDispatch( const ::com::sun::star::util::URL& URL, const ::rtl::OUString& TargetFrameName, ::sal_Int32 SearchFlags ) throw (::com::sun::star::uno::RuntimeException);
257         virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > > SAL_CALL queryDispatches( const ::com::sun::star::uno::Sequence< ::com::sun::star::frame::DispatchDescriptor >& Requests ) throw (::com::sun::star::uno::RuntimeException);
258 
259         // XInitialization
260         virtual void SAL_CALL initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
261 
262     private:
263         void UpdateUI();
264 
265 		void startContainerWindowListening();
266 		void stopContainerWindowListening();
267 
268 		// stop the inspection
269 		void stopInspection( bool _bCommitModified );
270 
271 		sal_Bool haveView() const { return NULL != m_pView; }
272         OPropertyEditor&    getPropertyBox() { return m_pView->getPropertyBox(); }
273 
274 		// does the inspection of the objects as indicated by our model
275 		void doInspection();
276 
277 		// bind the browser to m_xIntrospecteeAsProperty
278         void    impl_rebindToInspectee_nothrow( const InterfaceArray& _rObjects );
279 
280         /** retrieves special property handlers for our introspectee
281         */
282         void    getPropertyHandlers( const InterfaceArray& _rObjects, PropertyHandlerArray& _rHandlers );
283 
284         /** called when a property changed, to broadcast any handlers which might have
285             registered for this property
286 
287             @param _bFirstTimeInit
288                 if set to <FALSE/>, this is a real change in the property value, not just a call
289                 for purposes of initialization.
290         */
291         void    impl_broadcastPropertyChange_nothrow( const ::rtl::OUString& _rPropertyName, const ::com::sun::star::uno::Any& _rNewValue, const ::com::sun::star::uno::Any& _rOldValue, bool _bFirstTimeInit ) const;
292 
293         /** determines whether the given property is an actuating property, that is, at least one
294             handler expressed interest in changes to this property's value.
295         */
296         inline bool impl_isActuatingProperty_nothrow( const ::rtl::OUString& _rPropertyName ) const
297         {
298             return ( m_aDependencyHandlers.find( _rPropertyName ) != m_aDependencyHandlers.end() );
299         }
300 
301 		sal_uInt32      GetPropertyPos(const ::rtl::OUString& _rPropName);
302 
303         /** retrieves the value of the given property, by asking the appropriate XPropertyHandler
304             @param  _rPropertyName
305                 the name whose handler is to be obtained. Must be the name of a property
306                 for which a handler is registered.
307             @throws
308                 RuntimeException if there is no handler for the given property
309             @return
310                 the value of this property
311         */
312         ::com::sun::star::uno::Any
313                         impl_getPropertyValue_throw( const ::rtl::OUString& _rPropertyName );
314 
315         /// calls XPropertyHandler::suspend for all our property handlers
316         sal_Bool    suspendPropertyHandlers_nothrow( sal_Bool _bSuspend );
317 
318         /// suspends the complete inspector
319         sal_Bool    suspendAll_nothrow();
320 
321         /** selects a page according to our current view data
322         */
323         void selectPageFromViewData();
324 
325         /** updates our view data from the currently active page
326         */
327 		void updateViewDataFromActivePage();
328 
329         /// describes the UI for the given property
330         void describePropertyLine( const ::com::sun::star::beans::Property& _rPropertyName, OLineDescriptor& _rDescriptor )
331             SAL_THROW((::com::sun::star::uno::Exception));
332 
333         /** retrieves the position of the property given by name in m_aProperties
334             @return
335                 <TRUE/> if and only if the property could be found. In this case, <arg>_pProperty</arg> (if
336                 not <NULL/> contains the iterator pointing to this property.
337         */
338         bool impl_findObjectProperty_nothrow( const ::rtl::OUString& _rName, OrderedPropertyMap::const_iterator* _pProperty = NULL );
339 
340 		sal_Bool Construct(Window* _pParentWin);
341 
342         /** retrieves the property handler for a given property name
343             @param  _rPropertyName
344                 the name whose handler is to be obtained. Must be the name of a property
345                 for which a handler is registered.
346             @throws
347                 RuntimeException if there is no handler for the given property
348             @return
349                 the handler which is responsible for the given property
350         */
351         PropertyHandlerRef
352             impl_getHandlerForProperty_throw( const ::rtl::OUString& _rPropertyName ) const;
353 
354         /** determines whether we have a handler for the given property
355             @param _rPropertyName
356                 the name of the property for which the existence of a handler should be checked
357         */
358         bool
359             impl_hasPropertyHandlerFor_nothrow( const ::rtl::OUString& _rPropertyName ) const;
360 
361         /** builds up m_aPageIds from InspectorModel::describeCategories, and insert all the
362             respective tab pages into our view
363             @precond
364                 m_aPageIds is empty
365             @throws ::com::sun::star::uno::RuntimeException
366                 if one of the callees of this method throws this exception
367         */
368         void
369             impl_buildCategories_throw();
370 
371         /** retrieves the id of the tab page which represents a given category.
372             @param  _rCategoryName
373                 the programmatic name of a category.
374             @return
375                 the id of the tab page, or <code>(sal_uInt16)-1</code> if there
376                 is no tab page for the given category
377         */
378         sal_uInt16
379             impl_getPageIdForCategory_nothrow( const ::rtl::OUString& _rCategoryName ) const;
380 
381         /** adds or removes ourself as XEventListener to/from all our inspectees
382         */
383         void    impl_toggleInspecteeListening_nothrow( bool _bOn );
384 
385         /** binds the instance to a new model
386         */
387         void    impl_bindToNewModel_nothrow( const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XObjectInspectorModel >& _rxInspectorModel );
388 
389         /** initializes our view, as indicated by the model's view-relevant properties
390 
391             It's allowed to call this method when no model exists, yet. In this case, nothing
392             happens.
393         */
394         void    impl_initializeView_nothrow();
395 
396         /** determines whether the view should be readonly.
397 
398             Effectively, this means that the method simply checks the IsReadOnly attribute of the model.
399             If there is no model, <FALSE/> is returned.
400 
401             @throws ::com::sun::star::uno::RuntimeException
402                 in case asking the model for its IsReadOnly attribute throws a ::com::sun::star::uno::RuntimeException
403                 itself.
404         */
405         bool    impl_isReadOnlyModel_throw() const;
406 
407         /** updates our view so that it is read-only, as indicated by the model property
408             @see impl_isReadOnlyModel_throw
409         */
410         void    impl_updateReadOnlyView_nothrow();
411 
412         /** starts or stops listening at the model
413         */
414         void    impl_startOrStopModelListening_nothrow( bool _bDoListen ) const;
415 
416 	private:
417 		DECL_LINK(OnPageActivation, void*);
418 
419     private:
420         // constructors
421         void    createDefault();
422         void    createWithModel( const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XObjectInspectorModel >& _rxModel );
423 	};
424 
425 //............................................................................
426 } // namespace pcr
427 //............................................................................
428 
429 #endif // _EXTENSIONS_PROPCTRLR_PROPCONTROLLER_HXX_
430 
431