Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / view / frame2.cxx
blob1e5b6b2c3d39ece641405e2642220ffa27621624
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 .
21 #include "impframe.hxx"
22 #include <workwin.hxx>
24 #include <sfx2/bindings.hxx>
25 #include <sfx2/dispatch.hxx>
26 #include <sfx2/docfile.hxx>
27 #include <sfx2/sfxsids.hrc>
28 #include <sfx2/sfxuno.hxx>
29 #include <sfx2/viewsh.hxx>
31 #include <com/sun/star/awt/XWindow2.hpp>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/frame/Desktop.hpp>
34 #include <com/sun/star/frame/XComponentLoader.hpp>
35 #include <com/sun/star/frame/Frame.hpp>
36 #include <com/sun/star/frame/XLayoutManager.hpp>
38 #include <comphelper/namedvaluecollection.hxx>
39 #include <comphelper/processfactory.hxx>
40 #include <toolkit/helper/vclunohelper.hxx>
41 #include <tools/diagnose_ex.h>
42 #include <vcl/event.hxx>
43 #include <vcl/syswin.hxx>
44 #include <sal/log.hxx>
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::frame;
49 using namespace ::com::sun::star::util;
50 using namespace ::com::sun::star::container;
51 using namespace ::com::sun::star::beans;
52 using ::com::sun::star::frame::XComponentLoader;
54 class SfxFrameWindow_Impl : public vcl::Window
56 DECL_LINK(ModalHierarchyHdl, bool, void);
57 public:
58 SfxFrame* pFrame;
60 SfxFrameWindow_Impl( SfxFrame* pF, vcl::Window& i_rContainerWindow );
62 virtual void DataChanged( const DataChangedEvent& rDCEvt ) override;
63 virtual void StateChanged( StateChangedType nStateChange ) override;
64 virtual bool PreNotify( NotifyEvent& rNEvt ) override;
65 virtual bool EventNotify( NotifyEvent& rEvt ) override;
66 virtual void Resize() override;
67 virtual void GetFocus() override;
68 virtual void dispose() override;
69 void DoResize();
72 SfxFrameWindow_Impl::SfxFrameWindow_Impl(SfxFrame* pF, vcl::Window& i_rContainerWindow)
73 : Window(&i_rContainerWindow, WB_BORDER | WB_CLIPCHILDREN | WB_NODIALOGCONTROL | WB_3DLOOK)
74 , pFrame(pF)
76 i_rContainerWindow.SetModalHierarchyHdl(LINK(this, SfxFrameWindow_Impl, ModalHierarchyHdl));
79 void SfxFrameWindow_Impl::dispose()
81 GetParent()->SetModalHierarchyHdl(Link<bool, void>());
82 vcl::Window::dispose();
85 void SfxFrameWindow_Impl::DataChanged( const DataChangedEvent& rDCEvt )
87 Window::DataChanged( rDCEvt );
88 // tdf#131613 the printers changing has no effect on window layout
89 if (rDCEvt.GetType() == DataChangedEventType::PRINTER)
90 return;
91 SfxWorkWindow *pWorkWin = pFrame->GetWorkWindow_Impl();
92 if ( pWorkWin )
93 pWorkWin->DataChanged_Impl();
96 bool SfxFrameWindow_Impl::EventNotify( NotifyEvent& rNEvt )
98 if ( pFrame->IsClosing_Impl() || !pFrame->GetFrameInterface().is() )
99 return false;
101 SfxViewFrame* pView = pFrame->GetCurrentViewFrame();
102 if ( !pView || !pView->GetObjectShell() )
103 return Window::EventNotify( rNEvt );
105 if ( rNEvt.GetType() == MouseNotifyEvent::GETFOCUS )
107 if ( pView->GetViewShell() && !pView->GetViewShell()->GetUIActiveIPClient_Impl() && !pFrame->IsInPlace() )
109 SAL_INFO("sfx", "SfxFrame: GotFocus");
110 pView->MakeActive_Impl( false );
113 // if focus was on an external window, the clipboard content might have been changed
114 pView->GetBindings().Invalidate( SID_PASTE );
115 pView->GetBindings().Invalidate( SID_PASTE_SPECIAL );
116 return true;
118 else if( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
120 if ( pView->GetViewShell()->KeyInput( *rNEvt.GetKeyEvent() ) )
121 return true;
124 return Window::EventNotify( rNEvt );
127 IMPL_LINK(SfxFrameWindow_Impl, ModalHierarchyHdl, bool, bSetModal, void)
129 SfxViewFrame* pView = pFrame->GetCurrentViewFrame();
130 if (!pView || !pView->GetObjectShell())
131 return;
132 pView->SetModalMode(bSetModal);
135 bool SfxFrameWindow_Impl::PreNotify( NotifyEvent& rNEvt )
137 MouseNotifyEvent nType = rNEvt.GetType();
138 if ( nType == MouseNotifyEvent::KEYINPUT || nType == MouseNotifyEvent::KEYUP )
140 SfxViewFrame* pView = pFrame->GetCurrentViewFrame();
141 SfxViewShell* pShell = pView ? pView->GetViewShell() : nullptr;
142 if ( pShell && pShell->HasKeyListeners_Impl() && pShell->HandleNotifyEvent_Impl( rNEvt ) )
143 return true;
145 else if ( nType == MouseNotifyEvent::MOUSEBUTTONUP || nType == MouseNotifyEvent::MOUSEBUTTONDOWN )
147 vcl::Window* pWindow = rNEvt.GetWindow();
148 SfxViewFrame* pView = pFrame->GetCurrentViewFrame();
149 SfxViewShell* pShell = pView ? pView->GetViewShell() : nullptr;
150 if ( pShell )
151 if ( pWindow == pShell->GetWindow() || pShell->GetWindow()->IsChild( pWindow ) )
152 if ( pShell->HasMouseClickListeners_Impl() && pShell->HandleNotifyEvent_Impl( rNEvt ) )
153 return true;
156 if ( nType == MouseNotifyEvent::MOUSEBUTTONDOWN )
158 vcl::Window* pWindow = rNEvt.GetWindow();
159 const MouseEvent* pMEvent = rNEvt.GetMouseEvent();
160 Point aPos = pWindow->OutputToScreenPixel( pMEvent->GetPosPixel() );
161 SfxWorkWindow *pWorkWin = pFrame->GetWorkWindow_Impl();
162 if ( pWorkWin )
163 pWorkWin->EndAutoShow_Impl( aPos );
166 return Window::PreNotify( rNEvt );
169 void SfxFrameWindow_Impl::GetFocus()
171 if ( pFrame && !pFrame->IsClosing_Impl() && pFrame->GetCurrentViewFrame() && pFrame->GetFrameInterface().is() )
172 pFrame->GetCurrentViewFrame()->MakeActive_Impl( true );
175 void SfxFrameWindow_Impl::Resize()
177 if ( IsReallyVisible() || IsReallyShown() || GetOutputSizePixel().Width() )
178 DoResize();
181 void SfxFrameWindow_Impl::StateChanged( StateChangedType nStateChange )
183 if ( nStateChange == StateChangedType::InitShow )
185 pFrame->pImpl->bHidden = false;
186 if ( pFrame->IsInPlace() )
187 // TODO/MBA: workaround for bug in LayoutManager: the final resize does not get through because the
188 // LayoutManager works asynchronously and between resize and time execution the DockingAcceptor was exchanged so that
189 // the resize event never is sent to the component
190 SetSizePixel( GetParent()->GetOutputSizePixel() );
192 DoResize();
193 SfxViewFrame* pView = pFrame->GetCurrentViewFrame();
194 if ( pView )
195 pView->GetBindings().GetWorkWindow_Impl()->ShowChildren_Impl();
198 Window::StateChanged( nStateChange );
201 void SfxFrameWindow_Impl::DoResize()
203 if ( !pFrame->pImpl->bLockResize )
204 pFrame->Resize();
207 Reference < XFrame > SfxFrame::CreateBlankFrame()
209 Reference < XFrame > xFrame;
212 Reference < XDesktop2 > xDesktop = Desktop::create( ::comphelper::getProcessComponentContext() );
213 xFrame.set( xDesktop->findFrame( "_blank", 0 ), UNO_SET_THROW );
215 catch( const Exception& )
217 DBG_UNHANDLED_EXCEPTION("sfx.view");
219 return xFrame;
222 SfxFrame* SfxFrame::CreateHidden( SfxObjectShell const & rDoc, vcl::Window& rWindow, SfxInterfaceId nViewId )
224 SfxFrame* pFrame = nullptr;
227 // create and initialize new top level frame for this window
228 Reference < XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
229 Reference < XDesktop2 > xDesktop = Desktop::create( xContext );
230 Reference < XFrame2 > xFrame = Frame::create( xContext );
232 Reference< awt::XWindow2 > xWin( VCLUnoHelper::GetInterface ( &rWindow ), uno::UNO_QUERY_THROW );
233 xFrame->initialize( xWin.get() );
234 xDesktop->getFrames()->append( xFrame );
236 if ( xWin->isActive() )
237 xFrame->activate();
239 // create load arguments
240 Sequence< PropertyValue > aLoadArgs;
241 TransformItems( SID_OPENDOC, *rDoc.GetMedium()->GetItemSet(), aLoadArgs );
243 ::comphelper::NamedValueCollection aArgs( aLoadArgs );
244 aArgs.put( "Model", rDoc.GetModel() );
245 aArgs.put( "Hidden", true );
246 if ( nViewId != SFX_INTERFACE_NONE )
247 aArgs.put( "ViewId", static_cast<sal_uInt16>(nViewId) );
249 aLoadArgs = aArgs.getPropertyValues();
251 // load the doc into that frame
252 Reference< XComponentLoader > xLoader( xFrame, UNO_QUERY_THROW );
253 xLoader->loadComponentFromURL(
254 "private:object",
255 "_self",
257 aLoadArgs
260 for ( pFrame = SfxFrame::GetFirst();
261 pFrame;
262 pFrame = SfxFrame::GetNext( *pFrame )
265 if ( pFrame->GetFrameInterface() == xFrame )
266 break;
269 OSL_ENSURE( pFrame, "SfxFrame::Create: load succeeded, but no SfxFrame was created during this!" );
271 catch( const Exception& )
273 DBG_UNHANDLED_EXCEPTION("sfx.view");
276 return pFrame;
279 SfxFrame* SfxFrame::Create( const Reference < XFrame >& i_rFrame )
281 // create a new TopFrame to an external XFrame object ( wrap controller )
282 ENSURE_OR_THROW( i_rFrame.is(), "NULL frame not allowed" );
283 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( i_rFrame->getContainerWindow() );
284 ENSURE_OR_THROW( pWindow, "frame without container window not allowed" );
286 SfxFrame* pFrame = new SfxFrame( *pWindow );
287 pFrame->SetFrameInterface_Impl( i_rFrame );
288 return pFrame;
291 SfxFrame::SfxFrame( vcl::Window& i_rContainerWindow )
292 :SvCompatWeakBase<SfxFrame>( this )
293 ,pWindow( nullptr )
295 Construct_Impl();
297 pImpl->bHidden = false;
298 InsertTopFrame_Impl( this );
299 pImpl->pExternalContainerWindow = &i_rContainerWindow;
301 pWindow = VclPtr<SfxFrameWindow_Impl>::Create( this, i_rContainerWindow );
303 // always show pWindow, which is the ComponentWindow of the XFrame we live in
304 // nowadays, since SfxFrames can be created with an XFrame only, hiding or showing the complete XFrame
305 // is not done at level of the container window, not at SFX level. Thus, the component window can
306 // always be visible.
307 pWindow->Show();
310 void SfxFrame::SetPresentationMode( bool bSet )
312 if ( GetCurrentViewFrame() )
313 GetCurrentViewFrame()->GetWindow().SetBorderStyle( bSet ? WindowBorderStyle::NOBORDER : WindowBorderStyle::NORMAL );
315 Reference< css::beans::XPropertySet > xPropSet( GetFrameInterface(), UNO_QUERY );
316 Reference< css::frame::XLayoutManager > xLayoutManager;
318 if ( xPropSet.is() )
320 Any aValue = xPropSet->getPropertyValue("LayoutManager");
321 aValue >>= xLayoutManager;
324 if ( xLayoutManager.is() )
325 xLayoutManager->setVisible( !bSet ); // we don't want to have ui in presentation mode
327 SetMenuBarOn_Impl( !bSet );
328 if ( GetWorkWindow_Impl() )
329 GetWorkWindow_Impl()->SetDockingAllowed( !bSet );
330 if ( GetCurrentViewFrame() )
331 GetCurrentViewFrame()->GetDispatcher()->Update_Impl( true );
334 SystemWindow* SfxFrame::GetSystemWindow() const
336 return GetTopWindow_Impl();
339 SystemWindow* SfxFrame::GetTopWindow_Impl() const
341 if ( pImpl->pExternalContainerWindow->IsSystemWindow() )
342 return static_cast<SystemWindow*>( pImpl->pExternalContainerWindow.get() );
343 else
344 return nullptr;
348 void SfxFrame::LockResize_Impl( bool bLock )
350 pImpl->bLockResize = bLock;
353 void SfxFrame::SetMenuBarOn_Impl( bool bOn )
355 pImpl->bMenuBarOn = bOn;
357 Reference< css::beans::XPropertySet > xPropSet( GetFrameInterface(), UNO_QUERY );
358 Reference< css::frame::XLayoutManager > xLayoutManager;
360 if ( xPropSet.is() )
362 Any aValue = xPropSet->getPropertyValue("LayoutManager");
363 aValue >>= xLayoutManager;
366 if ( xLayoutManager.is() )
368 OUString aMenuBarURL( "private:resource/menubar/menubar" );
370 if ( bOn )
371 xLayoutManager->showElement( aMenuBarURL );
372 else
373 xLayoutManager->hideElement( aMenuBarURL );
377 bool SfxFrame::IsMenuBarOn_Impl() const
379 return pImpl->bMenuBarOn;
382 void SfxFrame::PrepareForDoc_Impl( SfxObjectShell& i_rDoc )
384 const ::comphelper::NamedValueCollection aDocumentArgs( i_rDoc.GetModel()->getArgs() );
386 // hidden?
387 OSL_ENSURE( !pImpl->bHidden, "when does this happen?" );
388 pImpl->bHidden = aDocumentArgs.getOrDefault( "Hidden", pImpl->bHidden );
390 // update our descriptor
391 UpdateDescriptor( &i_rDoc );
393 // plugin mode
394 sal_Int16 nPluginMode = aDocumentArgs.getOrDefault( "PluginMode", sal_Int16( 0 ) );
395 if ( nPluginMode && ( nPluginMode != 2 ) )
396 pImpl->bInPlace = true;
399 bool SfxFrame::IsMarkedHidden_Impl() const
401 return pImpl->bHidden;
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */