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