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 
21 #include "impframe.hxx"
22 #include <workwin.hxx>
23 
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>
30 
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>
37 
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>
45 
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;
53 
54 class SfxFrameWindow_Impl : public vcl::Window
55 {
56     DECL_LINK(ModalHierarchyHdl, bool, void);
57 public:
58     SfxFrame*           pFrame;
59 
60     SfxFrameWindow_Impl( SfxFrame* pF, vcl::Window& i_rContainerWindow );
61 
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();
70 };
71 
SfxFrameWindow_Impl(SfxFrame * pF,vcl::Window & i_rContainerWindow)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)
75 {
76     i_rContainerWindow.SetModalHierarchyHdl(LINK(this, SfxFrameWindow_Impl, ModalHierarchyHdl));
77 }
78 
dispose()79 void SfxFrameWindow_Impl::dispose()
80 {
81     GetParent()->SetModalHierarchyHdl(Link<bool, void>());
82     vcl::Window::dispose();
83 }
84 
DataChanged(const DataChangedEvent & rDCEvt)85 void SfxFrameWindow_Impl::DataChanged( const DataChangedEvent& rDCEvt )
86 {
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();
94 }
95 
EventNotify(NotifyEvent & rNEvt)96 bool SfxFrameWindow_Impl::EventNotify( NotifyEvent& rNEvt )
97 {
98     if ( pFrame->IsClosing_Impl() || !pFrame->GetFrameInterface().is() )
99         return false;
100 
101     SfxViewFrame* pView = pFrame->GetCurrentViewFrame();
102     if ( !pView || !pView->GetObjectShell() )
103         return Window::EventNotify( rNEvt );
104 
105     if ( rNEvt.GetType() == MouseNotifyEvent::GETFOCUS )
106     {
107         if ( pView->GetViewShell() && !pView->GetViewShell()->GetUIActiveIPClient_Impl() && !pFrame->IsInPlace() )
108         {
109             SAL_INFO("sfx", "SfxFrame: GotFocus");
110             pView->MakeActive_Impl( false );
111         }
112 
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;
117     }
118     else if( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
119     {
120         if ( pView->GetViewShell()->KeyInput( *rNEvt.GetKeyEvent() ) )
121             return true;
122     }
123 
124     return Window::EventNotify( rNEvt );
125 }
126 
IMPL_LINK(SfxFrameWindow_Impl,ModalHierarchyHdl,bool,bSetModal,void)127 IMPL_LINK(SfxFrameWindow_Impl, ModalHierarchyHdl, bool, bSetModal, void)
128 {
129     SfxViewFrame* pView = pFrame->GetCurrentViewFrame();
130     if (!pView || !pView->GetObjectShell())
131         return;
132     pView->SetModalMode(bSetModal);
133 }
134 
PreNotify(NotifyEvent & rNEvt)135 bool SfxFrameWindow_Impl::PreNotify( NotifyEvent& rNEvt )
136 {
137     MouseNotifyEvent nType = rNEvt.GetType();
138     if ( nType == MouseNotifyEvent::KEYINPUT || nType == MouseNotifyEvent::KEYUP )
139     {
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;
144     }
145     else if ( nType == MouseNotifyEvent::MOUSEBUTTONUP || nType == MouseNotifyEvent::MOUSEBUTTONDOWN )
146     {
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;
154     }
155 
156     if ( nType == MouseNotifyEvent::MOUSEBUTTONDOWN )
157     {
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 );
164     }
165 
166     return Window::PreNotify( rNEvt );
167 }
168 
GetFocus()169 void SfxFrameWindow_Impl::GetFocus()
170 {
171     if ( pFrame && !pFrame->IsClosing_Impl() && pFrame->GetCurrentViewFrame() && pFrame->GetFrameInterface().is() )
172         pFrame->GetCurrentViewFrame()->MakeActive_Impl( true );
173 }
174 
Resize()175 void SfxFrameWindow_Impl::Resize()
176 {
177     if ( IsReallyVisible() || IsReallyShown() || GetOutputSizePixel().Width() )
178         DoResize();
179 }
180 
StateChanged(StateChangedType nStateChange)181 void SfxFrameWindow_Impl::StateChanged( StateChangedType nStateChange )
182 {
183     if ( nStateChange == StateChangedType::InitShow )
184     {
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() );
191 
192         DoResize();
193         SfxViewFrame* pView = pFrame->GetCurrentViewFrame();
194         if ( pView )
195             pView->GetBindings().GetWorkWindow_Impl()->ShowChildren_Impl();
196     }
197 
198     Window::StateChanged( nStateChange );
199 }
200 
DoResize()201 void SfxFrameWindow_Impl::DoResize()
202 {
203     if ( !pFrame->pImpl->bLockResize )
204         pFrame->Resize();
205 }
206 
CreateBlankFrame()207 Reference < XFrame > SfxFrame::CreateBlankFrame()
208 {
209     Reference < XFrame > xFrame;
210     try
211     {
212         Reference < XDesktop2 > xDesktop = Desktop::create( ::comphelper::getProcessComponentContext() );
213         xFrame.set( xDesktop->findFrame( "_blank", 0 ), UNO_SET_THROW );
214     }
215     catch( const Exception& )
216     {
217         DBG_UNHANDLED_EXCEPTION("sfx.view");
218     }
219     return xFrame;
220 }
221 
CreateHidden(SfxObjectShell const & rDoc,vcl::Window & rWindow,SfxInterfaceId nViewId)222 SfxFrame* SfxFrame::CreateHidden( SfxObjectShell const & rDoc, vcl::Window& rWindow, SfxInterfaceId nViewId )
223 {
224     SfxFrame* pFrame = nullptr;
225     try
226     {
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 );
231 
232         Reference< awt::XWindow2 > xWin( VCLUnoHelper::GetInterface ( &rWindow ), uno::UNO_QUERY_THROW );
233         xFrame->initialize( xWin );
234         xDesktop->getFrames()->append( xFrame );
235 
236         if ( xWin->isActive() )
237             xFrame->activate();
238 
239         // create load arguments
240         Sequence< PropertyValue > aLoadArgs;
241         TransformItems( SID_OPENDOC, *rDoc.GetMedium()->GetItemSet(), aLoadArgs );
242 
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) );
248 
249         aLoadArgs = aArgs.getPropertyValues();
250 
251         // load the doc into that frame
252         Reference< XComponentLoader > xLoader( xFrame, UNO_QUERY_THROW );
253         xLoader->loadComponentFromURL(
254             "private:object",
255             "_self",
256             0,
257             aLoadArgs
258         );
259 
260         for (   pFrame = SfxFrame::GetFirst();
261                 pFrame;
262                 pFrame = SfxFrame::GetNext( *pFrame )
263             )
264         {
265             if ( pFrame->GetFrameInterface() == xFrame )
266                 break;
267         }
268 
269         OSL_ENSURE( pFrame, "SfxFrame::Create: load succeeded, but no SfxFrame was created during this!" );
270     }
271     catch( const Exception& )
272     {
273         DBG_UNHANDLED_EXCEPTION("sfx.view");
274     }
275 
276     return pFrame;
277 }
278 
Create(const Reference<XFrame> & i_rFrame)279 SfxFrame* SfxFrame::Create( const Reference < XFrame >& i_rFrame )
280 {
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" );
285 
286     SfxFrame* pFrame = new SfxFrame( *pWindow );
287     pFrame->SetFrameInterface_Impl( i_rFrame );
288     return pFrame;
289 }
290 
SfxFrame(vcl::Window & i_rContainerWindow)291 SfxFrame::SfxFrame( vcl::Window& i_rContainerWindow )
292     :SvCompatWeakBase<SfxFrame>( this )
293     ,pWindow( nullptr )
294 {
295     Construct_Impl();
296 
297     pImpl->bHidden = false;
298     InsertTopFrame_Impl( this );
299     pImpl->pExternalContainerWindow = &i_rContainerWindow;
300 
301     pWindow = VclPtr<SfxFrameWindow_Impl>::Create( this, i_rContainerWindow );
302 
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();
308 }
309 
SetPresentationMode(bool bSet)310 void SfxFrame::SetPresentationMode( bool bSet )
311 {
312     if ( GetCurrentViewFrame() )
313         GetCurrentViewFrame()->GetWindow().SetBorderStyle( bSet ? WindowBorderStyle::NOBORDER : WindowBorderStyle::NORMAL );
314 
315     Reference< css::beans::XPropertySet > xPropSet( GetFrameInterface(), UNO_QUERY );
316     Reference< css::frame::XLayoutManager > xLayoutManager;
317 
318     if ( xPropSet.is() )
319     {
320         Any aValue = xPropSet->getPropertyValue("LayoutManager");
321         aValue >>= xLayoutManager;
322     }
323 
324     if ( xLayoutManager.is() )
325         xLayoutManager->setVisible( !bSet ); // we don't want to have ui in presentation mode
326 
327     SetMenuBarOn_Impl( !bSet );
328     if ( GetWorkWindow_Impl() )
329         GetWorkWindow_Impl()->SetDockingAllowed( !bSet );
330     if ( GetCurrentViewFrame() )
331         GetCurrentViewFrame()->GetDispatcher()->Update_Impl( true );
332 }
333 
GetSystemWindow() const334 SystemWindow* SfxFrame::GetSystemWindow() const
335 {
336     return GetTopWindow_Impl();
337 }
338 
GetTopWindow_Impl() const339 SystemWindow* SfxFrame::GetTopWindow_Impl() const
340 {
341     if ( pImpl->pExternalContainerWindow->IsSystemWindow() )
342         return static_cast<SystemWindow*>( pImpl->pExternalContainerWindow.get() );
343     else
344         return nullptr;
345 }
346 
347 
LockResize_Impl(bool bLock)348 void SfxFrame::LockResize_Impl( bool bLock )
349 {
350     pImpl->bLockResize = bLock;
351 }
352 
SetMenuBarOn_Impl(bool bOn)353 void SfxFrame::SetMenuBarOn_Impl( bool bOn )
354 {
355     pImpl->bMenuBarOn = bOn;
356 
357     Reference< css::beans::XPropertySet > xPropSet( GetFrameInterface(), UNO_QUERY );
358     Reference< css::frame::XLayoutManager > xLayoutManager;
359 
360     if ( xPropSet.is() )
361     {
362         Any aValue = xPropSet->getPropertyValue("LayoutManager");
363         aValue >>= xLayoutManager;
364     }
365 
366     if ( xLayoutManager.is() )
367     {
368         OUString aMenuBarURL( "private:resource/menubar/menubar" );
369 
370         if ( bOn )
371             xLayoutManager->showElement( aMenuBarURL );
372         else
373             xLayoutManager->hideElement( aMenuBarURL );
374     }
375 }
376 
IsMenuBarOn_Impl() const377 bool SfxFrame::IsMenuBarOn_Impl() const
378 {
379     return pImpl->bMenuBarOn;
380 }
381 
PrepareForDoc_Impl(SfxObjectShell & i_rDoc)382 void SfxFrame::PrepareForDoc_Impl( SfxObjectShell& i_rDoc )
383 {
384     const ::comphelper::NamedValueCollection aDocumentArgs( i_rDoc.GetModel()->getArgs2( { "Hidden", "PluginMode" } ) );
385 
386     // hidden?
387     OSL_ENSURE( !pImpl->bHidden, "when does this happen?" );
388     pImpl->bHidden = aDocumentArgs.getOrDefault( "Hidden", pImpl->bHidden );
389 
390     // update our descriptor
391     UpdateDescriptor( &i_rDoc );
392 
393     // plugin mode
394     sal_Int16 nPluginMode = aDocumentArgs.getOrDefault( "PluginMode", sal_Int16( 0 ) );
395     if ( nPluginMode && ( nPluginMode != 2 ) )
396         pImpl->bInPlace = true;
397 }
398 
IsMarkedHidden_Impl() const399 bool SfxFrame::IsMarkedHidden_Impl() const
400 {
401     return pImpl->bHidden;
402 }
403 
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
405