Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/view/frame2.cxx
Line
Count
Source
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
#include <com/sun/star/frame/XModel3.hpp>
38
39
#include <comphelper/namedvaluecollection.hxx>
40
#include <comphelper/processfactory.hxx>
41
#include <comphelper/sequenceashashmap.hxx>
42
#include <toolkit/helper/vclunohelper.hxx>
43
#include <comphelper/diagnose_ex.hxx>
44
#include <vcl/event.hxx>
45
#include <vcl/syswin.hxx>
46
#include <sal/log.hxx>
47
48
using namespace ::com::sun::star;
49
using namespace ::com::sun::star::uno;
50
using namespace ::com::sun::star::frame;
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*           m_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
72
SfxFrameWindow_Impl::SfxFrameWindow_Impl(SfxFrame* pF, vcl::Window& i_rContainerWindow)
73
4.01k
    : Window(&i_rContainerWindow, WB_BORDER | WB_CLIPCHILDREN | WB_NODIALOGCONTROL | WB_3DLOOK)
74
4.01k
    , m_pFrame(pF)
75
4.01k
{
76
4.01k
    i_rContainerWindow.SetModalHierarchyHdl(LINK(this, SfxFrameWindow_Impl, ModalHierarchyHdl));
77
4.01k
}
Unexecuted instantiation: SfxFrameWindow_Impl::SfxFrameWindow_Impl(SfxFrame*, vcl::Window&)
SfxFrameWindow_Impl::SfxFrameWindow_Impl(SfxFrame*, vcl::Window&)
Line
Count
Source
73
4.01k
    : Window(&i_rContainerWindow, WB_BORDER | WB_CLIPCHILDREN | WB_NODIALOGCONTROL | WB_3DLOOK)
74
4.01k
    , m_pFrame(pF)
75
4.01k
{
76
4.01k
    i_rContainerWindow.SetModalHierarchyHdl(LINK(this, SfxFrameWindow_Impl, ModalHierarchyHdl));
77
4.01k
}
78
79
void SfxFrameWindow_Impl::dispose()
80
4.01k
{
81
4.01k
    GetParent()->SetModalHierarchyHdl(Link<bool, void>());
82
4.01k
    vcl::Window::dispose();
83
4.01k
}
84
85
void SfxFrameWindow_Impl::DataChanged( const DataChangedEvent& rDCEvt )
86
0
{
87
0
    Window::DataChanged( rDCEvt );
88
    // tdf#131613 the printers changing has no effect on window layout
89
0
    if (rDCEvt.GetType() == DataChangedEventType::PRINTER)
90
0
        return;
91
0
    SfxWorkWindow *pWorkWin = m_pFrame->GetWorkWindow_Impl();
92
0
    if ( pWorkWin )
93
0
        pWorkWin->DataChanged_Impl();
94
0
}
95
96
bool SfxFrameWindow_Impl::EventNotify( NotifyEvent& rNEvt )
97
0
{
98
0
    if ( m_pFrame->IsClosing_Impl() || !m_pFrame->GetFrameInterface().is() )
99
0
        return false;
100
101
0
    SfxViewFrame* pView = m_pFrame->GetCurrentViewFrame();
102
0
    if ( !pView || !pView->GetObjectShell() )
103
0
        return Window::EventNotify( rNEvt );
104
105
0
    if ( rNEvt.GetType() == NotifyEventType::GETFOCUS )
106
0
    {
107
0
        if ( pView->GetViewShell() && !pView->GetViewShell()->GetUIActiveIPClient_Impl() && !m_pFrame->IsInPlace() )
108
0
        {
109
0
            SAL_INFO("sfx", "SfxFrame: GotFocus");
110
0
            pView->MakeActive_Impl( false );
111
0
        }
112
113
        // if focus was on an external window, the clipboard content might have been changed
114
0
        pView->GetBindings().Invalidate( SID_PASTE );
115
0
        pView->GetBindings().Invalidate( SID_PASTE_SPECIAL );
116
0
        return true;
117
0
    }
118
0
    else if( rNEvt.GetType() == NotifyEventType::KEYINPUT )
119
0
    {
120
0
        if ( pView->GetViewShell()->KeyInput( *rNEvt.GetKeyEvent() ) )
121
0
            return true;
122
0
    }
123
124
0
    return Window::EventNotify( rNEvt );
125
0
}
126
127
IMPL_LINK(SfxFrameWindow_Impl, ModalHierarchyHdl, bool, bSetModal, void)
128
0
{
129
0
    SfxViewFrame* pView = m_pFrame->GetCurrentViewFrame();
130
0
    if (!pView || !pView->GetObjectShell())
131
0
        return;
132
0
    pView->SetModalMode(bSetModal);
133
0
}
134
135
bool SfxFrameWindow_Impl::PreNotify( NotifyEvent& rNEvt )
136
0
{
137
0
    NotifyEventType nType = rNEvt.GetType();
138
0
    if ( nType == NotifyEventType::KEYINPUT || nType == NotifyEventType::KEYUP )
139
0
    {
140
0
        SfxViewFrame* pView = m_pFrame->GetCurrentViewFrame();
141
0
        SfxViewShell* pShell = pView ? pView->GetViewShell() : nullptr;
142
0
        if ( pShell && pShell->HasKeyListeners_Impl() && pShell->HandleNotifyEvent_Impl( rNEvt ) )
143
0
            return true;
144
0
    }
145
0
    else if ( nType == NotifyEventType::MOUSEBUTTONUP || nType == NotifyEventType::MOUSEBUTTONDOWN )
146
0
    {
147
0
        vcl::Window* pWindow = rNEvt.GetWindow();
148
0
        SfxViewFrame* pView = m_pFrame->GetCurrentViewFrame();
149
0
        SfxViewShell* pShell = pView ? pView->GetViewShell() : nullptr;
150
0
        if ( pShell )
151
0
            if ( pWindow == pShell->GetWindow() || pShell->GetWindow()->IsChild( pWindow ) )
152
0
                if ( pShell->HasMouseClickListeners_Impl() && pShell->HandleNotifyEvent_Impl( rNEvt ) )
153
0
                    return true;
154
0
    }
155
156
0
    if ( nType == NotifyEventType::MOUSEBUTTONDOWN )
157
0
    {
158
0
        vcl::Window* pWindow = rNEvt.GetWindow();
159
0
        const MouseEvent* pMEvent = rNEvt.GetMouseEvent();
160
0
        Point aPos = pWindow->OutputToScreenPixel( pMEvent->GetPosPixel() );
161
0
        SfxWorkWindow *pWorkWin = m_pFrame->GetWorkWindow_Impl();
162
0
        if ( pWorkWin )
163
0
            pWorkWin->EndAutoShow_Impl( aPos );
164
0
    }
165
166
0
    return Window::PreNotify( rNEvt );
167
0
}
168
169
void SfxFrameWindow_Impl::GetFocus()
170
0
{
171
0
    if ( m_pFrame && !m_pFrame->IsClosing_Impl() &&
172
0
         m_pFrame->GetCurrentViewFrame() &&
173
0
         m_pFrame->GetFrameInterface().is() )
174
0
        m_pFrame->GetCurrentViewFrame()->MakeActive_Impl( true );
175
0
}
176
177
void SfxFrameWindow_Impl::Resize()
178
4.01k
{
179
4.01k
    if ( IsReallyVisible() || IsReallyShown() || GetOutputSizePixel().Width() )
180
0
        DoResize();
181
4.01k
}
182
183
void SfxFrameWindow_Impl::StateChanged( StateChangedType nStateChange )
184
4.01k
{
185
4.01k
    if ( nStateChange == StateChangedType::InitShow )
186
0
    {
187
0
        m_pFrame->m_pImpl->bHidden = false;
188
0
        if ( m_pFrame->IsInPlace() )
189
            // TODO/MBA: workaround for bug in LayoutManager: the final resize does not get through because the
190
            // LayoutManager works asynchronously and between resize and time execution the DockingAcceptor was exchanged so that
191
            // the resize event never is sent to the component
192
0
            SetSizePixel( GetParent()->GetOutputSizePixel() );
193
194
0
        DoResize();
195
0
        SfxViewFrame* pView = m_pFrame->GetCurrentViewFrame();
196
0
        if ( pView )
197
0
            pView->GetBindings().GetWorkWindow_Impl()->ShowChildren_Impl();
198
0
    }
199
200
4.01k
    Window::StateChanged( nStateChange );
201
4.01k
}
202
203
void SfxFrameWindow_Impl::DoResize()
204
0
{
205
0
    if ( !m_pFrame->m_pImpl->bLockResize )
206
0
        m_pFrame->Resize();
207
0
}
208
209
Reference < XFrame > SfxFrame::CreateBlankFrame()
210
0
{
211
0
    Reference < XFrame > xFrame;
212
0
    try
213
0
    {
214
0
        Reference < XDesktop2 > xDesktop = Desktop::create( ::comphelper::getProcessComponentContext() );
215
0
        xFrame.set( xDesktop->findFrame( u"_blank"_ustr, 0 ), UNO_SET_THROW );
216
0
    }
217
0
    catch( const Exception& )
218
0
    {
219
0
        DBG_UNHANDLED_EXCEPTION("sfx.view");
220
0
    }
221
0
    return xFrame;
222
0
}
223
224
SfxFrame* SfxFrame::CreateHidden( SfxObjectShell const & rDoc, vcl::Window& rWindow, SfxInterfaceId nViewId )
225
0
{
226
0
    SfxFrame* pFrame = nullptr;
227
0
    try
228
0
    {
229
        // create and initialize new top level frame for this window
230
0
        const Reference < XComponentContext >& xContext( ::comphelper::getProcessComponentContext() );
231
0
        Reference < XDesktop2 > xDesktop = Desktop::create( xContext );
232
0
        Reference < XFrame2 > xFrame = Frame::create( xContext );
233
234
0
        Reference< awt::XWindow2 > xWin( VCLUnoHelper::GetInterface ( &rWindow ), uno::UNO_QUERY_THROW );
235
0
        xFrame->initialize( xWin );
236
0
        xDesktop->getFrames()->append( xFrame );
237
238
0
        if ( xWin->isActive() )
239
0
            xFrame->activate();
240
241
        // create load arguments
242
0
        comphelper::SequenceAsHashMap aArgs = TransformItems(SID_OPENDOC, rDoc.GetMedium()->GetItemSet());
243
0
        aArgs[u"Model"_ustr] <<= rDoc.GetModel();
244
0
        aArgs[u"Hidden"_ustr] <<= true;
245
0
        if ( nViewId != SFX_INTERFACE_NONE )
246
0
            aArgs[u"ViewId"_ustr] <<= static_cast<sal_uInt16>(nViewId);
247
248
        // load the doc into that frame
249
0
        Reference< XComponentLoader > xLoader( xFrame, UNO_QUERY_THROW );
250
0
        xLoader->loadComponentFromURL(
251
0
            u"private:object"_ustr,
252
0
            u"_self"_ustr,
253
0
            0,
254
0
            aArgs.getAsConstPropertyValueList()
255
0
        );
256
257
0
        for (   pFrame = SfxFrame::GetFirst();
258
0
                pFrame;
259
0
                pFrame = SfxFrame::GetNext( *pFrame )
260
0
            )
261
0
        {
262
0
            if ( pFrame->GetFrameInterface() == xFrame )
263
0
                break;
264
0
        }
265
266
0
        OSL_ENSURE( pFrame, "SfxFrame::Create: load succeeded, but no SfxFrame was created during this!" );
267
0
    }
268
0
    catch( const Exception& )
269
0
    {
270
0
        DBG_UNHANDLED_EXCEPTION("sfx.view");
271
0
    }
272
273
0
    return pFrame;
274
0
}
275
276
SfxFrame* SfxFrame::Create( const Reference < XFrame >& i_rFrame )
277
4.01k
{
278
    // create a new TopFrame to an external XFrame object ( wrap controller )
279
4.01k
    ENSURE_OR_THROW( i_rFrame.is(), "NULL frame not allowed" );
280
4.01k
    VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( i_rFrame->getContainerWindow() );
281
4.01k
    ENSURE_OR_THROW( pWindow, "frame without container window not allowed" );
282
283
4.01k
    SfxFrame* pFrame = new SfxFrame( *pWindow );
284
4.01k
    pFrame->SetFrameInterface_Impl( i_rFrame );
285
4.01k
    return pFrame;
286
4.01k
}
287
288
SfxFrame::SfxFrame( vcl::Window& i_rContainerWindow )
289
4.01k
    :SvCompatWeakBase<SfxFrame>( this )
290
4.01k
    ,m_pWindow( nullptr )
291
4.01k
{
292
4.01k
    Construct_Impl();
293
294
4.01k
    m_pImpl->bHidden = false;
295
4.01k
    InsertTopFrame_Impl( this );
296
4.01k
    m_pImpl->pExternalContainerWindow = &i_rContainerWindow;
297
298
4.01k
    m_pWindow = VclPtr<SfxFrameWindow_Impl>::Create( this, i_rContainerWindow );
299
300
    // always show pWindow, which is the ComponentWindow of the XFrame we live in
301
    // nowadays, since SfxFrames can be created with an XFrame only, hiding or showing the complete XFrame
302
    // is not done at level of the container window, not at SFX level. Thus, the component window can
303
    // always be visible.
304
4.01k
    m_pWindow->Show();
305
4.01k
}
306
307
void SfxFrame::SetPresentationMode( bool bSet )
308
0
{
309
0
    if ( GetCurrentViewFrame() )
310
0
        GetCurrentViewFrame()->GetWindow().SetBorderStyle( bSet ? WindowBorderStyle::NOBORDER : WindowBorderStyle::NORMAL );
311
312
0
    Reference< css::beans::XPropertySet > xPropSet( GetFrameInterface(), UNO_QUERY );
313
0
    Reference< css::frame::XLayoutManager > xLayoutManager;
314
315
0
    if ( xPropSet.is() )
316
0
    {
317
0
        Any aValue = xPropSet->getPropertyValue(u"LayoutManager"_ustr);
318
0
        aValue >>= xLayoutManager;
319
0
    }
320
321
0
    if ( xLayoutManager.is() )
322
0
        xLayoutManager->setVisible( !bSet ); // we don't want to have ui in presentation mode
323
324
0
    SetMenuBarOn_Impl( !bSet );
325
0
    if ( GetWorkWindow_Impl() )
326
0
        GetWorkWindow_Impl()->SetDockingAllowed( !bSet );
327
0
    if ( GetCurrentViewFrame() )
328
0
        GetCurrentViewFrame()->GetDispatcher()->Update_Impl( true );
329
0
}
330
331
SystemWindow* SfxFrame::GetSystemWindow() const
332
4.01k
{
333
4.01k
    return GetTopWindow_Impl();
334
4.01k
}
335
336
SystemWindow* SfxFrame::GetTopWindow_Impl() const
337
4.01k
{
338
4.01k
    if ( m_pImpl->pExternalContainerWindow->IsSystemWindow() )
339
4.01k
        return static_cast<SystemWindow*>( m_pImpl->pExternalContainerWindow.get() );
340
0
    else
341
0
        return nullptr;
342
4.01k
}
343
344
345
void SfxFrame::LockResize_Impl( bool bLock )
346
0
{
347
0
    m_pImpl->bLockResize = bLock;
348
0
}
349
350
void SfxFrame::SetMenuBarOn_Impl( bool bOn )
351
0
{
352
0
    m_pImpl->bMenuBarOn = bOn;
353
354
0
    Reference< css::beans::XPropertySet > xPropSet( GetFrameInterface(), UNO_QUERY );
355
0
    Reference< css::frame::XLayoutManager > xLayoutManager;
356
357
0
    if ( xPropSet.is() )
358
0
    {
359
0
        Any aValue = xPropSet->getPropertyValue(u"LayoutManager"_ustr);
360
0
        aValue >>= xLayoutManager;
361
0
    }
362
363
0
    if ( xLayoutManager.is() )
364
0
    {
365
0
        OUString aMenuBarURL( u"private:resource/menubar/menubar"_ustr );
366
367
0
        if ( bOn )
368
0
            xLayoutManager->showElement( aMenuBarURL );
369
0
        else
370
0
            xLayoutManager->hideElement( aMenuBarURL );
371
0
    }
372
0
}
373
374
bool SfxFrame::IsMenuBarOn_Impl() const
375
4.01k
{
376
4.01k
    return m_pImpl->bMenuBarOn;
377
4.01k
}
378
379
void SfxFrame::PrepareForDoc_Impl( const SfxObjectShell& i_rDoc )
380
4.01k
{
381
4.01k
    const ::comphelper::NamedValueCollection aDocumentArgs( i_rDoc.GetModel()->getArgs2( { u"Hidden"_ustr, u"PluginMode"_ustr } ) );
382
383
    // hidden?
384
4.01k
    OSL_ENSURE( !m_pImpl->bHidden, "when does this happen?" );
385
4.01k
    m_pImpl->bHidden = aDocumentArgs.getOrDefault( u"Hidden"_ustr, m_pImpl->bHidden );
386
387
    // update our descriptor
388
4.01k
    UpdateDescriptor( &i_rDoc );
389
390
    // plugin mode
391
4.01k
    sal_Int16 nPluginMode = aDocumentArgs.getOrDefault( u"PluginMode"_ustr, sal_Int16( 0 ) );
392
4.01k
    if ( nPluginMode && ( nPluginMode != 2 ) )
393
0
        m_pImpl->bInPlace = true;
394
4.01k
}
395
396
bool SfxFrame::IsMarkedHidden_Impl() const
397
4.01k
{
398
4.01k
    return m_pImpl->bHidden;
399
4.01k
}
400
401
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */