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 : #include <dbaccess/controllerframe.hxx>
21 : #include <dbaccess/IController.hxx>
22 :
23 : #include <com/sun/star/sdb/XOfficeDatabaseDocument.hpp>
24 : #include <com/sun/star/awt/XTopWindow.hpp>
25 : #include <com/sun/star/awt/XWindow2.hpp>
26 : #include <com/sun/star/lang/DisposedException.hpp>
27 : #include <com/sun/star/document/XDocumentEventBroadcaster.hpp>
28 : #include <com/sun/star/frame/XController2.hpp>
29 :
30 : #include <cppuhelper/implbase1.hxx>
31 : #include <rtl/ref.hxx>
32 : #include <sfx2/objsh.hxx>
33 : #include <tools/diagnose_ex.h>
34 : #include <toolkit/helper/vclunohelper.hxx>
35 : #include <vcl/window.hxx>
36 :
37 : namespace dbaui
38 : {
39 :
40 : using ::com::sun::star::uno::Reference;
41 : using ::com::sun::star::uno::XInterface;
42 : using ::com::sun::star::uno::UNO_QUERY;
43 : using ::com::sun::star::uno::UNO_QUERY_THROW;
44 : using ::com::sun::star::uno::UNO_SET_THROW;
45 : using ::com::sun::star::uno::Exception;
46 : using ::com::sun::star::uno::RuntimeException;
47 : using ::com::sun::star::uno::Any;
48 : using ::com::sun::star::uno::makeAny;
49 : using ::com::sun::star::frame::XFrame;
50 : using ::com::sun::star::frame::FrameAction;
51 : using ::com::sun::star::frame::FrameAction_FRAME_ACTIVATED;
52 : using ::com::sun::star::frame::FrameAction_FRAME_UI_ACTIVATED;
53 : using ::com::sun::star::frame::FrameAction_FRAME_DEACTIVATING;
54 : using ::com::sun::star::frame::FrameAction_FRAME_UI_DEACTIVATING;
55 : using ::com::sun::star::frame::XModel;
56 : using ::com::sun::star::frame::XController;
57 : using ::com::sun::star::frame::XController2;
58 : using ::com::sun::star::frame::XFramesSupplier;
59 : using ::com::sun::star::sdb::XOfficeDatabaseDocument;
60 : using ::com::sun::star::awt::XTopWindow;
61 : using ::com::sun::star::awt::XTopWindowListener;
62 : using ::com::sun::star::awt::XWindow2;
63 : using ::com::sun::star::lang::DisposedException;
64 : using ::com::sun::star::lang::EventObject;
65 : using ::com::sun::star::document::XDocumentEventBroadcaster;
66 : using ::com::sun::star::awt::XWindow;
67 :
68 : // FrameWindowActivationListener
69 : typedef ::cppu::WeakImplHelper1 < XTopWindowListener
70 : > FrameWindowActivationListener_Base;
71 : class FrameWindowActivationListener : public FrameWindowActivationListener_Base
72 : {
73 : public:
74 : FrameWindowActivationListener( ControllerFrame_Data& _rData );
75 :
76 : void dispose();
77 :
78 : protected:
79 : virtual ~FrameWindowActivationListener();
80 :
81 : // XTopWindowListener
82 : virtual void SAL_CALL windowOpened( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
83 : virtual void SAL_CALL windowClosing( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
84 : virtual void SAL_CALL windowClosed( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
85 : virtual void SAL_CALL windowMinimized( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
86 : virtual void SAL_CALL windowNormalized( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
87 : virtual void SAL_CALL windowActivated( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
88 : virtual void SAL_CALL windowDeactivated( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
89 :
90 : // XEventListener
91 : virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
92 :
93 : private:
94 : void impl_checkDisposed_throw() const;
95 : void impl_registerOnFrameContainerWindow_nothrow( bool _bRegister );
96 :
97 : private:
98 : ControllerFrame_Data* m_pData;
99 : };
100 :
101 : // ControllerFrame_Data
102 0 : struct ControllerFrame_Data
103 : {
104 0 : ControllerFrame_Data( IController& _rController )
105 : :m_rController( _rController )
106 : ,m_xFrame()
107 : ,m_xDocEventBroadcaster()
108 : ,m_pListener()
109 : ,m_bActive( false )
110 0 : ,m_bIsTopLevelDocumentWindow( false )
111 : {
112 0 : }
113 :
114 : IController& m_rController;
115 : Reference< XFrame > m_xFrame;
116 : Reference< XDocumentEventBroadcaster > m_xDocEventBroadcaster;
117 : ::rtl::Reference< FrameWindowActivationListener > m_pListener;
118 : bool m_bActive;
119 : bool m_bIsTopLevelDocumentWindow;
120 : };
121 :
122 : // helper
123 0 : static void lcl_setFrame_nothrow( ControllerFrame_Data& _rData, const Reference< XFrame >& _rxFrame )
124 : {
125 : // release old listener
126 0 : if ( _rData.m_pListener.get() )
127 : {
128 0 : _rData.m_pListener->dispose();
129 0 : _rData.m_pListener = NULL;
130 : }
131 :
132 : // remember new frame
133 0 : _rData.m_xFrame = _rxFrame;
134 :
135 : // create new listener
136 0 : if ( _rData.m_xFrame.is() )
137 0 : _rData.m_pListener = new FrameWindowActivationListener( _rData );
138 :
139 : // at this point in time, we can assume the controller also has a model set, if it supports models
140 : try
141 : {
142 0 : Reference< XController > xController( _rData.m_rController.getXController(), UNO_SET_THROW );
143 0 : Reference< XModel > xModel( xController->getModel() );
144 0 : if ( xModel.is() )
145 0 : _rData.m_xDocEventBroadcaster.set( xModel, UNO_QUERY );
146 : }
147 0 : catch( const Exception& )
148 : {
149 : DBG_UNHANDLED_EXCEPTION();
150 : }
151 0 : }
152 :
153 0 : static bool lcl_isActive_nothrow( const Reference< XFrame >& _rxFrame )
154 : {
155 0 : bool bIsActive = false;
156 : try
157 : {
158 0 : if ( _rxFrame.is() )
159 : {
160 0 : Reference< XWindow2 > xWindow( _rxFrame->getContainerWindow(), UNO_QUERY_THROW );
161 0 : bIsActive = xWindow->isActive();
162 : }
163 :
164 : }
165 0 : catch( const Exception& )
166 : {
167 : DBG_UNHANDLED_EXCEPTION();
168 : }
169 0 : return bIsActive;
170 : }
171 :
172 : /** updates various global and local states with a new active component
173 :
174 : In particular, the following are updated
175 : * the global working document (aka Basic's ThisComponent in the application
176 : Basic), with our controller's model, or the controller itself if there is no such
177 : model.
178 : */
179 0 : static void lcl_updateActiveComponents_nothrow( const ControllerFrame_Data& _rData )
180 : {
181 : try
182 : {
183 0 : Reference< XController > xCompController( _rData.m_rController.getXController() );
184 : OSL_ENSURE( xCompController.is(), "lcl_updateActiveComponents_nothrow: can't do anything without a controller!" );
185 0 : if ( !xCompController.is() )
186 0 : return;
187 :
188 0 : if ( _rData.m_bActive && _rData.m_bIsTopLevelDocumentWindow )
189 : {
190 : // set the "current component" at the SfxObjectShell
191 0 : Reference< XModel > xModel( xCompController->getModel() );
192 0 : Reference< XInterface > xCurrentComponent;
193 0 : if ( xModel.is() )
194 0 : xCurrentComponent = xModel;
195 : else
196 0 : xCurrentComponent = xCompController;
197 0 : SfxObjectShell::SetCurrentComponent( xCurrentComponent );
198 0 : }
199 : }
200 0 : catch( const Exception& )
201 : {
202 : DBG_UNHANDLED_EXCEPTION();
203 : }
204 : }
205 :
206 : /** broadcasts the OnFocus resp. OnUnfocus event
207 : */
208 0 : static void lcl_notifyFocusChange_nothrow( ControllerFrame_Data& _rData, bool _bActive )
209 : {
210 : try
211 : {
212 0 : if ( _rData.m_xDocEventBroadcaster.is() )
213 : {
214 0 : OUString sEventName = _bActive ? OUString("OnFocus") : OUString("OnUnfocus");
215 0 : Reference< XController2 > xController( _rData.m_rController.getXController(), UNO_QUERY_THROW );
216 0 : _rData.m_xDocEventBroadcaster->notifyDocumentEvent( sEventName, xController, Any() );
217 : }
218 : }
219 0 : catch( const Exception& )
220 : {
221 : DBG_UNHANDLED_EXCEPTION();
222 : }
223 0 : }
224 :
225 0 : static void lcl_updateActive_nothrow( ControllerFrame_Data& _rData, bool _bActive )
226 : {
227 0 : if ( _rData.m_bActive == _bActive )
228 0 : return;
229 0 : _rData.m_bActive = _bActive;
230 :
231 0 : lcl_updateActiveComponents_nothrow( _rData );
232 0 : lcl_notifyFocusChange_nothrow( _rData, _bActive );
233 : }
234 :
235 0 : FrameWindowActivationListener::FrameWindowActivationListener( ControllerFrame_Data& _rData )
236 0 : :m_pData( &_rData )
237 : {
238 0 : impl_registerOnFrameContainerWindow_nothrow( true );
239 0 : }
240 :
241 0 : FrameWindowActivationListener::~FrameWindowActivationListener()
242 : {
243 0 : }
244 :
245 0 : void FrameWindowActivationListener::dispose()
246 : {
247 0 : impl_registerOnFrameContainerWindow_nothrow( false );
248 0 : m_pData = NULL;
249 0 : }
250 :
251 0 : void FrameWindowActivationListener::impl_registerOnFrameContainerWindow_nothrow( bool _bRegister )
252 : {
253 : OSL_ENSURE( m_pData && m_pData->m_xFrame.is(), "FrameWindowActivationListener::impl_registerOnFrameContainerWindow_nothrow: no frame!" );
254 0 : if ( !m_pData || !m_pData->m_xFrame.is() )
255 0 : return;
256 :
257 : try
258 : {
259 : void ( SAL_CALL XTopWindow::*pListenerAction )( const Reference< XTopWindowListener >& ) =
260 0 : _bRegister ? &XTopWindow::addTopWindowListener : &XTopWindow::removeTopWindowListener;
261 :
262 0 : const Reference< XWindow > xContainerWindow( m_pData->m_xFrame->getContainerWindow(), UNO_SET_THROW );
263 0 : if ( _bRegister )
264 : {
265 0 : const Window* pContainerWindow = VCLUnoHelper::GetWindow( xContainerWindow );
266 0 : ENSURE_OR_THROW( pContainerWindow, "no Window implementation for the frame's container window!" );
267 :
268 0 : m_pData->m_bIsTopLevelDocumentWindow = ( pContainerWindow->GetExtendedStyle() & WB_EXT_DOCUMENT ) != 0;
269 : }
270 :
271 0 : const Reference< XTopWindow > xFrameContainer( xContainerWindow, UNO_QUERY );
272 0 : if ( xFrameContainer.is() )
273 0 : (xFrameContainer.get()->*pListenerAction)( this );
274 : }
275 0 : catch( const Exception& )
276 : {
277 : DBG_UNHANDLED_EXCEPTION();
278 : }
279 : }
280 :
281 0 : void FrameWindowActivationListener::impl_checkDisposed_throw() const
282 : {
283 0 : if ( !m_pData )
284 0 : throw DisposedException( OUString(), *const_cast< FrameWindowActivationListener* >( this ) );
285 0 : }
286 :
287 0 : void SAL_CALL FrameWindowActivationListener::windowOpened( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
288 : {
289 : // not interested in
290 0 : }
291 :
292 0 : void SAL_CALL FrameWindowActivationListener::windowClosing( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
293 : {
294 : // not interested in
295 0 : }
296 :
297 0 : void SAL_CALL FrameWindowActivationListener::windowClosed( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
298 : {
299 : // not interested in
300 0 : }
301 :
302 0 : void SAL_CALL FrameWindowActivationListener::windowMinimized( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
303 : {
304 : // not interested in
305 0 : }
306 :
307 0 : void SAL_CALL FrameWindowActivationListener::windowNormalized( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
308 : {
309 : // not interested in
310 0 : }
311 :
312 0 : void SAL_CALL FrameWindowActivationListener::windowActivated( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
313 : {
314 0 : impl_checkDisposed_throw();
315 0 : lcl_updateActive_nothrow( *m_pData, true );
316 0 : }
317 :
318 0 : void SAL_CALL FrameWindowActivationListener::windowDeactivated( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
319 : {
320 0 : impl_checkDisposed_throw();
321 0 : lcl_updateActive_nothrow( *m_pData, false );
322 0 : }
323 :
324 0 : void SAL_CALL FrameWindowActivationListener::disposing( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
325 : {
326 0 : dispose();
327 0 : }
328 :
329 : // ControllerFrame
330 0 : ControllerFrame::ControllerFrame( IController& _rController )
331 0 : :m_pData( new ControllerFrame_Data( _rController ) )
332 : {
333 0 : }
334 :
335 0 : ControllerFrame::~ControllerFrame()
336 : {
337 0 : }
338 :
339 0 : const Reference< XFrame >& ControllerFrame::attachFrame( const Reference< XFrame >& _rxFrame )
340 : {
341 : // set new frame, including listener handling
342 0 : lcl_setFrame_nothrow( *m_pData, _rxFrame );
343 :
344 : // determine whether we're active
345 0 : m_pData->m_bActive = lcl_isActive_nothrow( m_pData->m_xFrame );
346 :
347 : // update active component
348 0 : if ( m_pData->m_bActive )
349 : {
350 0 : lcl_updateActiveComponents_nothrow( *m_pData );
351 0 : lcl_notifyFocusChange_nothrow( *m_pData, true );
352 : }
353 :
354 0 : return m_pData->m_xFrame;
355 : }
356 :
357 0 : const Reference< XFrame >& ControllerFrame::getFrame() const
358 : {
359 0 : return m_pData->m_xFrame;
360 : }
361 :
362 0 : bool ControllerFrame::isActive() const
363 : {
364 0 : return m_pData->m_bActive;
365 : }
366 :
367 0 : void ControllerFrame::frameAction( FrameAction _eAction )
368 : {
369 0 : bool bActive = m_pData->m_bActive;
370 :
371 0 : switch ( _eAction )
372 : {
373 : case FrameAction_FRAME_ACTIVATED:
374 : case FrameAction_FRAME_UI_ACTIVATED:
375 0 : bActive = true;
376 0 : break;
377 :
378 : case FrameAction_FRAME_DEACTIVATING:
379 : case FrameAction_FRAME_UI_DEACTIVATING:
380 0 : bActive = false;
381 0 : break;
382 :
383 : default:
384 0 : break;
385 : }
386 :
387 0 : lcl_updateActive_nothrow( *m_pData, bActive );
388 0 : }
389 :
390 : } // namespace dbaui
391 :
392 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|