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