update credits
[LibreOffice.git] / extensions / source / propctrlr / commoncontrol.hxx
blobf8aaa357c0cfa3c94a42aa16a75e7ba9775b0d6e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef _EXTENSIONS_PROPCTRLR_COMMONCONTROL_HXX_
21 #define _EXTENSIONS_PROPCTRLR_COMMONCONTROL_HXX_
23 #include <com/sun/star/inspection/XPropertyControl.hpp>
24 #include <com/sun/star/lang/DisposedException.hpp>
25 #include <cppuhelper/compbase1.hxx>
26 #include <comphelper/broadcasthelper.hxx>
27 #include <tools/link.hxx>
28 #include <vcl/window.hxx>
30 class NotifyEvent;
31 //............................................................................
32 namespace pcr
34 //............................................................................
36 class ControlHelper;
37 //========================================================================
38 //= ControlWindow
39 //========================================================================
40 template< class WINDOW >
41 class ControlWindow : public WINDOW
43 protected:
44 typedef WINDOW WindowType;
46 protected:
47 ControlHelper* m_pHelper;
49 public:
50 ControlWindow( Window* _pParent, WinBits _nStyle )
51 :WindowType( _pParent, _nStyle )
52 ,m_pHelper( NULL )
56 /// sets a ControlHelper instance which some functionality is delegated to
57 inline virtual void setControlHelper( ControlHelper& _rControlHelper );
59 protected:
60 // Window overridables
61 inline virtual long PreNotify( NotifyEvent& rNEvt );
64 //========================================================================
65 //= IModifyListener
66 //========================================================================
67 class SAL_NO_VTABLE IModifyListener
69 public:
70 virtual void modified() = 0;
72 protected:
73 ~IModifyListener() {}
76 //========================================================================
77 //= ControlHelper
78 //========================================================================
79 /** A helper class for implementing the <type scope="com::sun::star::inspection">XPropertyControl</type>
80 or one of its derived interfaces.
82 This class is intended to be held as member of another class which implements the
83 <type scope="com::sun::star::inspection">XPropertyControl</type> interface. The pointer
84 to this interface is to be passed to the ctor.
86 class ControlHelper
88 private:
89 Window* m_pControlWindow;
90 sal_Int16 m_nControlType;
91 ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControlContext >
92 m_xContext;
93 ::com::sun::star::inspection::XPropertyControl&
94 m_rAntiImpl;
95 IModifyListener* m_pModifyListener;
96 sal_Bool m_bModified;
98 public:
99 /** creates the instance
100 @param _rControlWindow
101 the window which is associated with the <type scope="com::sun::star::inspection">XPropertyControl</type>.
102 Must not be <NULL/>.<br/>
103 Ownership for this window is taken by the ControlHelper - it will be deleted in <member>disposing</member>.
104 @param _nControlType
105 the type of the control - one of the <type scope="com::sun::star::inspection">PropertyControlType</type>
106 constants
107 @param _pAntiImpl
108 Reference to the instance as whose "impl-class" we act. This reference is held during lifetime
109 of the <type>ControlHelper</type> class, within acquiring it. Thus, the owner of the
110 <type>ControlHelper</type> is responsible for assuring the lifetime of the instance
111 pointed to by <arg>_pAntiImpl</arg>.
112 @param _pModifyListener
113 a listener to be modfied when the user modified the control's value. the
114 <member>IModifyListener::modified</member> of this listener is called from within our
115 ModifiedHdl. A default implementation of <member>IModifyListener::modified</member>
116 would just call our <member>setModified</member>.
118 ControlHelper(
119 Window* _pControlWindow,
120 sal_Int16 _nControlType,
121 ::com::sun::star::inspection::XPropertyControl& _rAntiImpl,
122 IModifyListener* _pModifyListener );
124 virtual ~ControlHelper();
126 /** sets our "modified" flag to <TRUE/>
128 inline void setModified() { m_bModified = sal_True; }
129 inline Window* getVclControlWindow() { return m_pControlWindow; }
130 inline const Window* getVclControlWindow() const { return m_pControlWindow; }
132 public:
133 // XPropertyControl
134 ::sal_Int16 SAL_CALL getControlType() throw (::com::sun::star::uno::RuntimeException);
135 ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControlContext > SAL_CALL getControlContext() throw (::com::sun::star::uno::RuntimeException);
136 void SAL_CALL setControlContext( const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControlContext >& _controlcontext ) throw (::com::sun::star::uno::RuntimeException);
137 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow > SAL_CALL getControlWindow() throw (::com::sun::star::uno::RuntimeException);
138 ::sal_Bool SAL_CALL isModified( ) throw (::com::sun::star::uno::RuntimeException);
139 void SAL_CALL notifyModifiedValue( ) throw (::com::sun::star::uno::RuntimeException);
141 // XComponent
142 virtual void SAL_CALL dispose();
144 /** (fail-safe) wrapper around calling our context's activateNextControl
146 inline void activateNextControl() const { impl_activateNextControl_nothrow(); }
148 public:
149 /// may be used to implement the default handling in PreNotify; returns sal_True if handled
150 bool handlePreNotify(NotifyEvent& _rNEvt);
152 /// automatically size the window given in the ctor
153 void autoSizeWindow();
155 /// may be used by derived classes, they forward the event to the PropCtrListener
156 DECL_LINK( ModifiedHdl, Window* );
157 DECL_LINK( GetFocusHdl, Window* );
158 DECL_LINK( LoseFocusHdl, Window* );
160 private:
161 /** fail-safe wrapper around calling our context's activateNextControl
163 void impl_activateNextControl_nothrow() const;
166 //========================================================================
167 //= CommonBehaviourControl
168 //========================================================================
169 /** implements a base class for <type scope="com::sun::star::inspection">XPropertyControl</type>
170 implementations, which delegates the generic functionality of this interface to a
171 <type>ControlHelper</type> member.
173 @param CONTROL_INTERFACE
174 an interface class which is derived from (or identical to) <type scope="com::sun::star::inspection">XPropertyControl</type>
175 @param CONTROL_WINDOW
176 a class which is derived from ControlWindow
178 template < class CONTROL_INTERFACE, class CONTROL_WINDOW >
179 class CommonBehaviourControl :public ::comphelper::OBaseMutex
180 ,public ::cppu::WeakComponentImplHelper1< CONTROL_INTERFACE >
181 ,public IModifyListener
183 protected:
184 typedef CONTROL_INTERFACE InterfaceType;
185 typedef CONTROL_WINDOW WindowType;
187 typedef ::comphelper::OBaseMutex MutexBaseClass;
188 typedef ::cppu::WeakComponentImplHelper1< CONTROL_INTERFACE > ComponentBaseClass;
190 protected:
191 ControlHelper m_aImplControl;
193 protected:
194 inline CommonBehaviourControl( sal_Int16 _nControlType, Window* _pParentWindow, WinBits _nWindowStyle, bool _bDoSetHandlers = true );
196 // XPropertyControl - delegated to ->m_aImplControl
197 inline ::sal_Int16 SAL_CALL getControlType() throw (::com::sun::star::uno::RuntimeException);
198 inline ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControlContext > SAL_CALL getControlContext() throw (::com::sun::star::uno::RuntimeException);
199 inline void SAL_CALL setControlContext( const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControlContext >& _controlcontext ) throw (::com::sun::star::uno::RuntimeException);
200 inline ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow > SAL_CALL getControlWindow() throw (::com::sun::star::uno::RuntimeException);
201 inline ::sal_Bool SAL_CALL isModified( ) throw (::com::sun::star::uno::RuntimeException);
202 inline void SAL_CALL notifyModifiedValue( ) throw (::com::sun::star::uno::RuntimeException);
204 // XComponent
205 inline virtual void SAL_CALL disposing();
207 // IModifyListener
208 inline virtual void modified();
210 /// returns a typed pointer to our control window
211 WindowType* getTypedControlWindow() { return static_cast< WindowType* > ( m_aImplControl.getVclControlWindow() ); }
212 const WindowType* getTypedControlWindow() const { return static_cast< const WindowType* >( m_aImplControl.getVclControlWindow() ); }
214 protected:
215 /** checks whether the instance is already disposed
216 @throws DisposedException
217 if the instance is already disposed
219 inline void impl_checkDisposed_throw();
222 //========================================================================
223 //= ControlWindow - implementation
224 //========================================================================
225 //------------------------------------------------------------------------
226 template< class WINDOW >
227 inline void ControlWindow< WINDOW >::setControlHelper( ControlHelper& _rControlHelper )
229 m_pHelper = &_rControlHelper;
232 //------------------------------------------------------------------------
233 template< class WINDOW >
234 inline long ControlWindow< WINDOW >::PreNotify( NotifyEvent& rNEvt )
236 if ( m_pHelper && m_pHelper->handlePreNotify( rNEvt ) )
237 return 1;
238 return WindowType::PreNotify( rNEvt );
241 //========================================================================
242 //= CommonBehaviourControl - implementation
243 //========================================================================
244 //------------------------------------------------------------------------
245 template< class CONTROL_INTERFACE, class CONTROL_WINDOW >
246 inline CommonBehaviourControl< CONTROL_INTERFACE, CONTROL_WINDOW >::CommonBehaviourControl ( sal_Int16 _nControlType, Window* _pParentWindow, WinBits _nWindowStyle, bool _bDoSetHandlers )
247 :ComponentBaseClass( m_aMutex )
248 ,m_aImplControl( new WindowType( _pParentWindow, _nWindowStyle ), _nControlType, *this, this )
250 WindowType* pControlWindow( getTypedControlWindow() );
251 pControlWindow->setControlHelper( m_aImplControl );
252 if ( _bDoSetHandlers )
254 pControlWindow->SetModifyHdl( LINK( &m_aImplControl, ControlHelper, ModifiedHdl ) );
255 pControlWindow->SetGetFocusHdl( LINK( &m_aImplControl, ControlHelper, GetFocusHdl ) );
256 pControlWindow->SetLoseFocusHdl( LINK( &m_aImplControl, ControlHelper, LoseFocusHdl ) );
258 m_aImplControl.autoSizeWindow();
261 //--------------------------------------------------------------------
262 template< class CONTROL_INTERFACE, class CONTROL_WINDOW >
263 inline ::sal_Int16 SAL_CALL CommonBehaviourControl< CONTROL_INTERFACE, CONTROL_WINDOW >::getControlType() throw (::com::sun::star::uno::RuntimeException)
265 return m_aImplControl.getControlType();
268 //--------------------------------------------------------------------
269 template< class CONTROL_INTERFACE, class CONTROL_WINDOW >
270 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)
272 return m_aImplControl.getControlContext();
275 //--------------------------------------------------------------------
276 template< class CONTROL_INTERFACE, class CONTROL_WINDOW >
277 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)
279 m_aImplControl.setControlContext( _controlcontext );
282 //--------------------------------------------------------------------
283 template< class CONTROL_INTERFACE, class CONTROL_WINDOW >
284 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)
286 return m_aImplControl.getControlWindow();
289 //--------------------------------------------------------------------
290 template< class CONTROL_INTERFACE, class CONTROL_WINDOW >
291 inline ::sal_Bool SAL_CALL CommonBehaviourControl< CONTROL_INTERFACE, CONTROL_WINDOW >::isModified( ) throw (::com::sun::star::uno::RuntimeException)
293 return m_aImplControl.isModified();
296 //--------------------------------------------------------------------
297 template< class CONTROL_INTERFACE, class CONTROL_WINDOW >
298 inline void SAL_CALL CommonBehaviourControl< CONTROL_INTERFACE, CONTROL_WINDOW >::notifyModifiedValue( ) throw (::com::sun::star::uno::RuntimeException)
300 m_aImplControl.notifyModifiedValue();
303 //--------------------------------------------------------------------
304 template< class CONTROL_INTERFACE, class CONTROL_WINDOW >
305 inline void SAL_CALL CommonBehaviourControl< CONTROL_INTERFACE, CONTROL_WINDOW >::disposing()
307 m_aImplControl.dispose();
310 //--------------------------------------------------------------------
311 template< class CONTROL_INTERFACE, class CONTROL_WINDOW >
312 inline void CommonBehaviourControl< CONTROL_INTERFACE, CONTROL_WINDOW >::modified()
314 m_aImplControl.setModified();
317 //--------------------------------------------------------------------
318 template< class CONTROL_INTERFACE, class CONTROL_WINDOW >
319 inline void CommonBehaviourControl< CONTROL_INTERFACE, CONTROL_WINDOW >::impl_checkDisposed_throw()
321 if ( ComponentBaseClass::rBHelper.bDisposed )
322 throw ::com::sun::star::lang::DisposedException( OUString(), *this );
325 //............................................................................
326 } // namespace pcr
327 //............................................................................
329 #endif // _EXTENSIONS_PROPCTRLR_COMMONCONTROL_HXX_
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */