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