1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: commoncontrol.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _EXTENSIONS_PROPCTRLR_COMMONCONTROL_HXX_
32 #define _EXTENSIONS_PROPCTRLR_COMMONCONTROL_HXX_
34 /** === begin UNO includes === **/
35 #include <com/sun/star/inspection/XPropertyControl.hpp>
36 #include <com/sun/star/lang/DisposedException.hpp>
37 /** === end UNO includes === **/
38 #include <cppuhelper/compbase1.hxx>
39 #include <comphelper/broadcasthelper.hxx>
40 #include <tools/link.hxx>
41 #include <vcl/window.hxx>
44 //............................................................................
47 //............................................................................
50 //========================================================================
52 //========================================================================
53 template< class WINDOW
>
54 class ControlWindow
: public WINDOW
57 typedef WINDOW WindowType
;
60 ControlHelper
* m_pHelper
;
63 ControlWindow( Window
* _pParent
, WinBits _nStyle
)
64 :WindowType( _pParent
, _nStyle
)
69 /// sets a ControlHelper instance which some functionality is delegated to
70 inline virtual void setControlHelper( ControlHelper
& _rControlHelper
);
73 // Window overridables
74 inline virtual long PreNotify( NotifyEvent
& rNEvt
);
77 //========================================================================
79 //========================================================================
80 class SAL_NO_VTABLE IModifyListener
83 virtual void modified() = 0;
86 //========================================================================
88 //========================================================================
89 /** A helper class for implementing the <type scope="com::sun::star::inspection">XPropertyControl</type>
90 or one of its derived interfaces.
92 This class is intended to be held as member of another class which implements the
93 <type scope="com::sun::star::inspection">XPropertyControl</type> interface. The pointer
94 to this interface is to be passed to the ctor.
99 Window
* m_pControlWindow
;
100 sal_Int16 m_nControlType
;
101 ::com::sun::star::uno::Reference
< ::com::sun::star::inspection::XPropertyControlContext
>
103 ::com::sun::star::inspection::XPropertyControl
&
105 IModifyListener
* m_pModifyListener
;
106 sal_Bool m_bModified
;
109 /** creates the instance
110 @param _rControlWindow
111 the window which is associated with the <type scope="com::sun::star::inspection">XPropertyControl</type>.
112 Must not be <NULL/>.<br/>
113 Ownership for this window is taken by the ControlHelper - it will be deleted in <member>disposing</member>.
115 the type of the control - one of the <type scope="com::sun::star::inspection">PropertyControlType</type>
118 Reference to the instance as whose "impl-class" we act. This reference is held during lifetime
119 of the <type>ControlHelper</type> class, within acquiring it. Thus, the owner of the
120 <type>ControlHelper</type> is responsible for assuring the lifetime of the instance
121 pointed to by <arg>_pAntiImpl</arg>.
122 @param _pModifyListener
123 a listener to be modfied when the user modified the control's value. the
124 <member>IModifyListener::modified</member> of this listener is called from within our
125 ModifiedHdl. A default implementation of <member>IModifyListener::modified</member>
126 would just call our <member>setModified</member>.
129 Window
* _pControlWindow
,
130 sal_Int16 _nControlType
,
131 ::com::sun::star::inspection::XPropertyControl
& _rAntiImpl
,
132 IModifyListener
* _pModifyListener
);
134 virtual ~ControlHelper();
136 /** sets our "modified" flag to <TRUE/>
138 inline void setModified() { m_bModified
= sal_True
; }
139 inline Window
* getVclControlWindow() { return m_pControlWindow
; }
140 inline const Window
* getVclControlWindow() const { return m_pControlWindow
; }
144 ::sal_Int16 SAL_CALL
getControlType() throw (::com::sun::star::uno::RuntimeException
);
145 ::com::sun::star::uno::Reference
< ::com::sun::star::inspection::XPropertyControlContext
> SAL_CALL
getControlContext() throw (::com::sun::star::uno::RuntimeException
);
146 void SAL_CALL
setControlContext( const ::com::sun::star::uno::Reference
< ::com::sun::star::inspection::XPropertyControlContext
>& _controlcontext
) throw (::com::sun::star::uno::RuntimeException
);
147 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XWindow
> SAL_CALL
getControlWindow() throw (::com::sun::star::uno::RuntimeException
);
148 ::sal_Bool SAL_CALL
isModified( ) throw (::com::sun::star::uno::RuntimeException
);
149 void SAL_CALL
notifyModifiedValue( ) throw (::com::sun::star::uno::RuntimeException
);
152 virtual void SAL_CALL
dispose();
154 /** (fail-safe) wrapper around calling our context's activateNextControl
156 inline void activateNextControl() const { impl_activateNextControl_nothrow(); }
159 /// may be used to implement the default handling in PreNotify; returns sal_True if handled
160 bool handlePreNotify(NotifyEvent
& _rNEvt
);
162 /// automatically size the window given in the ctor
163 void autoSizeWindow();
165 /// may be used by derived classes, they forward the event to the PropCtrListener
166 DECL_LINK( ModifiedHdl
, Window
* );
167 DECL_LINK( GetFocusHdl
, Window
* );
168 DECL_LINK( LoseFocusHdl
, Window
* );
171 /** fail-safe wrapper around calling our context's activateNextControl
173 void impl_activateNextControl_nothrow() const;
176 //========================================================================
177 //= CommonBehaviourControl
178 //========================================================================
179 /** implements a base class for <type scope="com::sun::star::inspection">XPropertyControl</type>
180 implementations, which delegates the generic functionality of this interface to a
181 <type>ControlHelper</type> member.
183 @param CONTROL_INTERFACE
184 an interface class which is derived from (or identical to) <type scope="com::sun::star::inspection">XPropertyControl</type>
185 @param CONTROL_WINDOW
186 a class which is derived from ControlWindow
188 template < class CONTROL_INTERFACE
, class CONTROL_WINDOW
>
189 class CommonBehaviourControl
:public ::comphelper::OBaseMutex
190 ,public ::cppu::WeakComponentImplHelper1
< CONTROL_INTERFACE
>
191 ,public IModifyListener
194 typedef CONTROL_INTERFACE InterfaceType
;
195 typedef CONTROL_WINDOW WindowType
;
197 typedef ::comphelper::OBaseMutex MutexBaseClass
;
198 typedef ::cppu::WeakComponentImplHelper1
< CONTROL_INTERFACE
> ComponentBaseClass
;
201 ControlHelper m_aImplControl
;
204 inline CommonBehaviourControl( sal_Int16 _nControlType
, Window
* _pParentWindow
, WinBits _nWindowStyle
, bool _bDoSetHandlers
= true );
206 // XPropertyControl - delegated to ->m_aImplControl
207 inline ::sal_Int16 SAL_CALL
getControlType() throw (::com::sun::star::uno::RuntimeException
);
208 inline ::com::sun::star::uno::Reference
< ::com::sun::star::inspection::XPropertyControlContext
> SAL_CALL
getControlContext() throw (::com::sun::star::uno::RuntimeException
);
209 inline void SAL_CALL
setControlContext( const ::com::sun::star::uno::Reference
< ::com::sun::star::inspection::XPropertyControlContext
>& _controlcontext
) throw (::com::sun::star::uno::RuntimeException
);
210 inline ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XWindow
> SAL_CALL
getControlWindow() throw (::com::sun::star::uno::RuntimeException
);
211 inline ::sal_Bool SAL_CALL
isModified( ) throw (::com::sun::star::uno::RuntimeException
);
212 inline void SAL_CALL
notifyModifiedValue( ) throw (::com::sun::star::uno::RuntimeException
);
215 inline virtual void SAL_CALL
disposing();
218 inline virtual void modified();
220 /// returns a typed pointer to our control window
221 WindowType
* getTypedControlWindow() { return static_cast< WindowType
* > ( m_aImplControl
.getVclControlWindow() ); }
222 const WindowType
* getTypedControlWindow() const { return static_cast< const WindowType
* >( m_aImplControl
.getVclControlWindow() ); }
225 /** checks whether the instance is already disposed
226 @throws DisposedException
227 if the instance is already disposed
229 inline void impl_checkDisposed_throw();
232 //========================================================================
233 //= ControlWindow - implementation
234 //========================================================================
235 //------------------------------------------------------------------------
236 template< class WINDOW
>
237 inline void ControlWindow
< WINDOW
>::setControlHelper( ControlHelper
& _rControlHelper
)
239 m_pHelper
= &_rControlHelper
;
242 //------------------------------------------------------------------------
243 template< class WINDOW
>
244 inline long ControlWindow
< WINDOW
>::PreNotify( NotifyEvent
& rNEvt
)
246 if ( m_pHelper
&& m_pHelper
->handlePreNotify( rNEvt
) )
248 return WindowType::PreNotify( rNEvt
);
251 //========================================================================
252 //= CommonBehaviourControl - implementation
253 //========================================================================
254 //------------------------------------------------------------------------
255 template< class CONTROL_INTERFACE
, class CONTROL_WINDOW
>
256 inline CommonBehaviourControl
< CONTROL_INTERFACE
, CONTROL_WINDOW
>::CommonBehaviourControl ( sal_Int16 _nControlType
, Window
* _pParentWindow
, WinBits _nWindowStyle
, bool _bDoSetHandlers
)
257 :ComponentBaseClass( m_aMutex
)
258 ,m_aImplControl( new WindowType( _pParentWindow
, _nWindowStyle
), _nControlType
, *this, this )
260 WindowType
* pControlWindow( getTypedControlWindow() );
261 pControlWindow
->setControlHelper( m_aImplControl
);
262 if ( _bDoSetHandlers
)
264 pControlWindow
->SetModifyHdl( LINK( &m_aImplControl
, ControlHelper
, ModifiedHdl
) );
265 pControlWindow
->SetGetFocusHdl( LINK( &m_aImplControl
, ControlHelper
, GetFocusHdl
) );
266 pControlWindow
->SetLoseFocusHdl( LINK( &m_aImplControl
, ControlHelper
, LoseFocusHdl
) );
268 m_aImplControl
.autoSizeWindow();
271 //--------------------------------------------------------------------
272 template< class CONTROL_INTERFACE
, class CONTROL_WINDOW
>
273 inline ::sal_Int16 SAL_CALL CommonBehaviourControl
< CONTROL_INTERFACE
, CONTROL_WINDOW
>::getControlType() throw (::com::sun::star::uno::RuntimeException
)
275 return m_aImplControl
.getControlType();
278 //--------------------------------------------------------------------
279 template< class CONTROL_INTERFACE
, class CONTROL_WINDOW
>
280 inline ::com::sun::star::uno::Reference
< ::com::sun::star::inspection::XPropertyControlContext
> SAL_CALL CommonBehaviourControl
< CONTROL_INTERFACE
, CONTROL_WINDOW
>::getControlContext() throw (::com::sun::star::uno::RuntimeException
)
282 return m_aImplControl
.getControlContext();
285 //--------------------------------------------------------------------
286 template< class CONTROL_INTERFACE
, class CONTROL_WINDOW
>
287 inline void SAL_CALL CommonBehaviourControl
< CONTROL_INTERFACE
, CONTROL_WINDOW
>::setControlContext( const ::com::sun::star::uno::Reference
< ::com::sun::star::inspection::XPropertyControlContext
>& _controlcontext
) throw (::com::sun::star::uno::RuntimeException
)
289 m_aImplControl
.setControlContext( _controlcontext
);
292 //--------------------------------------------------------------------
293 template< class CONTROL_INTERFACE
, class CONTROL_WINDOW
>
294 inline ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XWindow
> SAL_CALL CommonBehaviourControl
< CONTROL_INTERFACE
, CONTROL_WINDOW
>::getControlWindow() throw (::com::sun::star::uno::RuntimeException
)
296 return m_aImplControl
.getControlWindow();
299 //--------------------------------------------------------------------
300 template< class CONTROL_INTERFACE
, class CONTROL_WINDOW
>
301 inline ::sal_Bool SAL_CALL CommonBehaviourControl
< CONTROL_INTERFACE
, CONTROL_WINDOW
>::isModified( ) throw (::com::sun::star::uno::RuntimeException
)
303 return m_aImplControl
.isModified();
306 //--------------------------------------------------------------------
307 template< class CONTROL_INTERFACE
, class CONTROL_WINDOW
>
308 inline void SAL_CALL CommonBehaviourControl
< CONTROL_INTERFACE
, CONTROL_WINDOW
>::notifyModifiedValue( ) throw (::com::sun::star::uno::RuntimeException
)
310 m_aImplControl
.notifyModifiedValue();
313 //--------------------------------------------------------------------
314 template< class CONTROL_INTERFACE
, class CONTROL_WINDOW
>
315 inline void SAL_CALL CommonBehaviourControl
< CONTROL_INTERFACE
, CONTROL_WINDOW
>::disposing()
317 m_aImplControl
.dispose();
320 //--------------------------------------------------------------------
321 template< class CONTROL_INTERFACE
, class CONTROL_WINDOW
>
322 inline void CommonBehaviourControl
< CONTROL_INTERFACE
, CONTROL_WINDOW
>::modified()
324 m_aImplControl
.setModified();
327 //--------------------------------------------------------------------
328 template< class CONTROL_INTERFACE
, class CONTROL_WINDOW
>
329 inline void CommonBehaviourControl
< CONTROL_INTERFACE
, CONTROL_WINDOW
>::impl_checkDisposed_throw()
331 if ( ComponentBaseClass::rBHelper
.bDisposed
)
332 throw ::com::sun::star::lang::DisposedException( ::rtl::OUString(), *this );
335 //............................................................................
337 //............................................................................
339 #endif // _EXTENSIONS_PROPCTRLR_COMMONCONTROL_HXX_