Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/sw/source/uibase/docvw/romenu.cxx
Line
Count
Source (jump to first uncovered line)
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 <memory>
21
#include <hintids.hxx>
22
23
#include <svl/eitem.hxx>
24
#include <vcl/settings.hxx>
25
#include <vcl/transfer.hxx>
26
#include <sfx2/dispatch.hxx>
27
#include <sfx2/viewfrm.hxx>
28
#include <svx/gallery.hxx>
29
#include <svx/graphichelper.hxx>
30
#include <editeng/brushitem.hxx>
31
32
#include <fmtinfmt.hxx>
33
#include <docsh.hxx>
34
#include <view.hxx>
35
#include <wrtsh.hxx>
36
#include <viewopt.hxx>
37
#include <swmodule.hxx>
38
#include "romenu.hxx"
39
#include <pagedesc.hxx>
40
#include <modcfg.hxx>
41
42
#include <cmdid.h>
43
44
using namespace ::com::sun::star;
45
46
SwReadOnlyPopup::~SwReadOnlyPopup()
47
0
{
48
0
    m_xMenu.disposeAndClear();
49
0
}
50
51
void SwReadOnlyPopup::Check( sal_uInt16 nMID, sal_uInt16 nSID, SfxDispatcher const &rDis )
52
0
{
53
0
    std::unique_ptr<SfxPoolItem> _pItem;
54
0
    SfxItemState eState = rDis.GetBindings()->QueryState( nSID, _pItem );
55
0
    if (eState >= SfxItemState::DEFAULT)
56
0
    {
57
0
        m_xMenu->EnableItem(nMID);
58
0
        if (_pItem)
59
0
        {
60
0
            m_xMenu->CheckItem(nMID, !IsDisabledItem(_pItem.get()) &&
61
0
                            dynamic_cast< const SfxBoolItem *>( _pItem.get() ) !=  nullptr &&
62
0
                            static_cast<SfxBoolItem*>(_pItem.get())->GetValue());
63
            //remove full screen entry when not in full screen mode
64
0
            if (SID_WIN_FULLSCREEN == nSID && !m_xMenu->IsItemChecked(m_nReadonlyFullscreen))
65
0
                m_xMenu->EnableItem(nMID, false);
66
0
        }
67
0
    }
68
0
    else
69
0
        m_xMenu->EnableItem(nMID, false);
70
0
}
71
72
0
#define MN_READONLY_GRAPHICTOGALLERY 1000
73
0
#define MN_READONLY_BACKGROUNDTOGALLERY 2000
74
75
SwReadOnlyPopup::SwReadOnlyPopup(const Point &rDPos, SwView &rV)
76
0
    : m_aBuilder(nullptr, AllSettings::GetUIRootDir(), u"modules/swriter/ui/readonlymenu.ui"_ustr, u""_ustr)
77
0
    , m_xMenu(m_aBuilder.get_menu(u"menu"))
78
0
    , m_nReadonlyOpenurl(m_xMenu->GetItemId(u"openurl"))
79
0
    , m_nReadonlyOpendoc(m_xMenu->GetItemId(u"opendoc"))
80
0
    , m_nReadonlyEditdoc(m_xMenu->GetItemId(u"edit"))
81
0
    , m_nReadonlySelectionMode(m_xMenu->GetItemId(u"selection"))
82
0
    , m_nReadonlyReload(m_xMenu->GetItemId(u"reload"))
83
0
    , m_nReadonlyReloadFrame(m_xMenu->GetItemId(u"reloadframe"))
84
0
    , m_nReadonlySourceview(m_xMenu->GetItemId(u"html"))
85
0
    , m_nReadonlyBrowseBackward(m_xMenu->GetItemId(u"backward"))
86
0
    , m_nReadonlyBrowseForward(m_xMenu->GetItemId(u"forward"))
87
0
    , m_nReadonlySaveGraphic(m_xMenu->GetItemId(u"savegraphic"))
88
0
    , m_nReadonlyGraphictogallery(m_xMenu->GetItemId(u"graphictogallery"))
89
0
    , m_nReadonlyTogallerylink(m_xMenu->GetItemId(u"graphicaslink"))
90
0
    , m_nReadonlyTogallerycopy(m_xMenu->GetItemId(u"graphicascopy"))
91
0
    , m_nReadonlySaveBackground(m_xMenu->GetItemId(u"savebackground"))
92
0
    , m_nReadonlyBackgroundtogallery(m_xMenu->GetItemId(u"backgroundtogallery"))
93
0
    , m_nReadonlyBackgroundTogallerylink(m_xMenu->GetItemId(u"backaslink"))
94
0
    , m_nReadonlyBackgroundTogallerycopy(m_xMenu->GetItemId(u"backascopy"))
95
0
    , m_nReadonlyCopylink(m_xMenu->GetItemId(u"copylink"))
96
0
    , m_nReadonlyLoadGraphic(m_xMenu->GetItemId(u"loadgraphic"))
97
0
    , m_nReadonlyGraphicoff(m_xMenu->GetItemId(u"imagesoff"))
98
0
    , m_nReadonlyFullscreen(m_xMenu->GetItemId(u"fullscreen"))
99
0
    , m_nReadonlyCopyField(m_xMenu->GetItemId(u"copyfield"))
100
0
    , m_nReadonlyCopy(m_xMenu->GetItemId(u"copy"))
101
0
    , m_rView(rV)
102
0
    , m_xBrushItem(std::make_unique<SvxBrushItem>(RES_BACKGROUND))
103
0
{
104
0
    m_bGrfToGalleryAsLnk = SwModule::get()->GetModuleConfig()->IsGrfToGalleryAsLnk();
105
0
    SwWrtShell &rSh = m_rView.GetWrtShell();
106
0
    OUString sDescription;
107
0
    rSh.IsURLGrfAtPos( rDPos, &m_sURL, &m_sTargetFrameName, &sDescription );
108
0
    if ( m_sURL.isEmpty() )
109
0
    {
110
0
        SwContentAtPos aContentAtPos( IsAttrAtPos::InetAttr );
111
0
        if( rSh.GetContentAtPos( rDPos, aContentAtPos))
112
0
        {
113
0
            const SwFormatINetFormat &rIItem = *static_cast<const SwFormatINetFormat*>(aContentAtPos.aFnd.pAttr);
114
0
            m_sURL = rIItem.GetValue();
115
0
            m_sTargetFrameName = rIItem.GetTargetFrame();
116
0
        }
117
0
    }
118
119
0
    bool bLink = false;
120
0
    const Graphic *pGrf = rSh.GetGrfAtPos( rDPos, m_sGrfName, bLink );
121
0
    if ( nullptr == pGrf )
122
0
    {
123
0
        m_xMenu->EnableItem(m_nReadonlySaveGraphic, false);
124
0
    }
125
0
    else
126
0
    {
127
0
        m_aGraphic = *pGrf;
128
0
    }
129
130
0
    bool bEnableGraphicToGallery = bLink;
131
0
    if ( bEnableGraphicToGallery )
132
0
    {
133
0
        if (GalleryExplorer::FillThemeList( m_aThemeList ))
134
0
        {
135
0
            PopupMenu *pMenu = m_xMenu->GetPopupMenu(m_nReadonlyGraphictogallery);
136
0
            pMenu->CheckItem(m_nReadonlyTogallerylink,  m_bGrfToGalleryAsLnk);
137
0
            pMenu->CheckItem(m_nReadonlyTogallerycopy, !m_bGrfToGalleryAsLnk);
138
139
0
            for ( size_t i=0; i < m_aThemeList.size(); ++i )
140
0
                pMenu->InsertItem(MN_READONLY_GRAPHICTOGALLERY + i, m_aThemeList[i]);
141
0
        }
142
0
        else
143
0
            bEnableGraphicToGallery = false;
144
0
    }
145
146
0
    m_xMenu->EnableItem(m_nReadonlyGraphictogallery, bEnableGraphicToGallery);
147
148
0
    SfxViewFrame& rVFrame = rV.GetViewFrame();
149
0
    SfxDispatcher &rDis = *rVFrame.GetDispatcher();
150
0
    const SwPageDesc &rDesc = rSh.GetPageDesc( rSh.GetCurPageDesc() );
151
0
    m_xBrushItem = rDesc.GetMaster().makeBackgroundBrushItem();
152
0
    bool bEnableBackGallery = false,
153
0
         bEnableBack = false;
154
155
0
    if ( m_xBrushItem && GPOS_NONE != m_xBrushItem->GetGraphicPos() )
156
0
    {
157
0
        bEnableBack = true;
158
0
        if ( !m_xBrushItem->GetGraphicLink().isEmpty() )
159
0
        {
160
0
            if ( m_aThemeList.empty() )
161
0
                GalleryExplorer::FillThemeList( m_aThemeList );
162
163
0
            if ( !m_aThemeList.empty() )
164
0
            {
165
0
                PopupMenu *pMenu = m_xMenu->GetPopupMenu(m_nReadonlyBackgroundtogallery);
166
0
                pMenu->CheckItem(m_nReadonlyBackgroundTogallerylink,  m_bGrfToGalleryAsLnk);
167
0
                pMenu->CheckItem(m_nReadonlyBackgroundTogallerycopy, !m_bGrfToGalleryAsLnk);
168
0
                bEnableBackGallery = true;
169
170
0
                for ( size_t i=0; i < m_aThemeList.size(); ++i )
171
0
                    pMenu->InsertItem(MN_READONLY_BACKGROUNDTOGALLERY + i, m_aThemeList[i]);
172
0
            }
173
0
        }
174
0
    }
175
0
    m_xMenu->EnableItem(m_nReadonlySaveBackground, bEnableBack);
176
0
    m_xMenu->EnableItem(m_nReadonlyBackgroundtogallery, bEnableBackGallery);
177
178
0
    if ( !rSh.GetViewOptions()->IsGraphic() )
179
0
        m_xMenu->CheckItem(m_nReadonlyGraphicoff);
180
0
    else
181
0
        m_xMenu->EnableItem(m_nReadonlyLoadGraphic, false);
182
183
0
    m_xMenu->EnableItem(m_nReadonlyReloadFrame, false);
184
0
    m_xMenu->EnableItem(m_nReadonlyReload);
185
186
0
    Check(m_nReadonlyEditdoc, SID_EDITDOC, rDis);
187
0
    Check(m_nReadonlySelectionMode, FN_READONLY_SELECTION_MODE, rDis);
188
0
    Check(m_nReadonlySourceview, SID_SOURCEVIEW, rDis);
189
0
    Check(m_nReadonlyBrowseBackward, SID_BROWSE_BACKWARD, rDis);
190
0
    Check(m_nReadonlyBrowseForward,SID_BROWSE_FORWARD, rDis);
191
0
    Check(m_nReadonlyOpenurl, SID_OPENDOC, rDis);
192
0
    Check(m_nReadonlyOpendoc, SID_OPENDOC, rDis);
193
194
0
    std::unique_ptr<SfxPoolItem> pState;
195
196
0
    SfxItemState eState = rVFrame.GetBindings().QueryState( SID_COPY, pState );
197
0
    Check(m_nReadonlyCopy, SID_COPY, rDis);
198
0
    if (eState < SfxItemState::DEFAULT)
199
0
        m_xMenu->EnableItem(m_nReadonlyCopy, false);
200
201
0
    eState = rVFrame.GetBindings().QueryState( SID_EDITDOC, pState );
202
0
    if (
203
0
        eState < SfxItemState::DEFAULT ||
204
0
        (rSh.IsGlobalDoc() && m_rView.GetDocShell()->IsReadOnlyUI())
205
0
       )
206
0
    {
207
0
        m_xMenu->EnableItem(m_nReadonlyEditdoc, false);
208
0
    }
209
210
0
    if ( m_sURL.isEmpty() )
211
0
    {
212
0
        m_xMenu->EnableItem(m_nReadonlyOpenurl, false);
213
0
        m_xMenu->EnableItem(m_nReadonlyOpendoc, false);
214
0
        m_xMenu->EnableItem(m_nReadonlyCopylink, false);
215
0
    }
216
0
    Check(m_nReadonlyFullscreen, SID_WIN_FULLSCREEN, rDis);
217
0
    eState = rVFrame.GetBindings().QueryState(FN_COPY_FIELD, pState);
218
0
    m_xMenu->EnableItem(m_nReadonlyCopyField, eState > SfxItemState::DISABLED);
219
0
    m_xMenu->RemoveDisabledEntries( true );
220
0
}
221
222
void SwReadOnlyPopup::Execute( vcl::Window* pWin, const Point &rPixPos )
223
0
{
224
0
    sal_uInt16 nId = m_xMenu->Execute(pWin, rPixPos);
225
0
    Execute(pWin, nId);
226
0
}
227
228
// execute the resulting ID only - necessary to support XContextMenuInterception
229
void SwReadOnlyPopup::Execute( vcl::Window* pWin, sal_uInt16 nId )
230
0
{
231
0
    SwWrtShell &rSh = m_rView.GetWrtShell();
232
0
    SfxDispatcher &rDis = *m_rView.GetViewFrame().GetDispatcher();
233
0
    if (nId >= MN_READONLY_GRAPHICTOGALLERY)
234
0
    {
235
0
        OUString sTmp;
236
0
        sal_uInt16 nSaveId;
237
0
        if (m_xBrushItem && nId >= MN_READONLY_BACKGROUNDTOGALLERY)
238
0
        {
239
0
            nId -= MN_READONLY_BACKGROUNDTOGALLERY;
240
0
            nSaveId = m_nReadonlySaveBackground;
241
0
            sTmp = m_xBrushItem->GetGraphicLink();
242
0
        }
243
0
        else
244
0
        {
245
0
            nId -= MN_READONLY_GRAPHICTOGALLERY;
246
0
            nSaveId = m_nReadonlySaveGraphic;
247
0
            sTmp = m_sGrfName;
248
0
        }
249
0
        if ( !m_bGrfToGalleryAsLnk )
250
0
            sTmp = SaveGraphic(nSaveId);
251
252
0
        if ( !sTmp.isEmpty() )
253
0
            GalleryExplorer::InsertURL( m_aThemeList[nId], sTmp );
254
255
0
        return;
256
0
    }
257
258
0
    rtl::Reference<TransferDataContainer> pClipCntnr;
259
260
0
    sal_uInt16 nExecId = USHRT_MAX;
261
0
    bool bFilterSet = false;
262
0
    LoadUrlFlags nFilter = LoadUrlFlags::NONE;
263
0
    if (nId == m_nReadonlyFullscreen)
264
0
        nExecId = SID_WIN_FULLSCREEN;
265
0
    else if (nId == m_nReadonlyOpenurl)
266
0
    {
267
0
        nFilter = LoadUrlFlags::NONE;
268
0
        bFilterSet = true;
269
0
    }
270
0
    else if (nId == m_nReadonlyOpendoc)
271
0
    {
272
0
        nFilter = LoadUrlFlags::NewView;
273
0
        bFilterSet = true;
274
0
    }
275
0
    else if (nId == m_nReadonlyCopy)
276
0
        nExecId = SID_COPY;
277
0
    else if (nId == m_nReadonlyEditdoc)
278
0
        nExecId = SID_EDITDOC;
279
0
    else if (nId == m_nReadonlySelectionMode)
280
0
        nExecId = FN_READONLY_SELECTION_MODE;
281
0
    else if (nId == m_nReadonlyReload || nId == m_nReadonlyReloadFrame)
282
0
        rSh.GetView().GetViewFrame().GetDispatcher()->Execute(SID_RELOAD);
283
0
    else if (nId == m_nReadonlyBrowseBackward)
284
0
        nExecId = SID_BROWSE_BACKWARD;
285
0
    else if (nId == m_nReadonlyBrowseForward)
286
0
        nExecId = SID_BROWSE_FORWARD;
287
0
    else if (nId == m_nReadonlySourceview)
288
0
        nExecId = SID_SOURCEVIEW;
289
0
    else if (nId == m_nReadonlyCopyField)
290
0
        nExecId = FN_COPY_FIELD;
291
0
    else if (nId == m_nReadonlySaveGraphic || nId == m_nReadonlySaveBackground)
292
0
        SaveGraphic(nId);
293
0
    else if (nId == m_nReadonlyCopylink)
294
0
    {
295
0
        pClipCntnr = new TransferDataContainer;
296
0
        pClipCntnr->CopyString( m_sURL );
297
0
    }
298
0
    else if (nId == m_nReadonlyLoadGraphic)
299
0
    {
300
0
        bool bModified = rSh.IsModified();
301
0
        SwViewOption aOpt( *rSh.GetViewOptions() );
302
0
        aOpt.SetGraphic( true );
303
0
        rSh.ApplyViewOptions( aOpt );
304
0
        if(!bModified)
305
0
            rSh.ResetModified();
306
0
    }
307
0
    else if (nId == m_nReadonlyGraphicoff)
308
0
        nExecId = FN_VIEW_GRAPHIC;
309
0
    else if (nId == m_nReadonlyTogallerylink || nId == m_nReadonlyBackgroundTogallerylink)
310
0
        SwModule::get()->GetModuleConfig()->SetGrfToGalleryAsLnk(true);
311
0
    else if (nId == m_nReadonlyTogallerycopy || nId == m_nReadonlyBackgroundTogallerycopy)
312
0
        SwModule::get()->GetModuleConfig()->SetGrfToGalleryAsLnk(false);
313
314
0
    if( USHRT_MAX != nExecId )
315
0
        rDis.GetBindings()->Execute( nExecId );
316
0
    if( bFilterSet )
317
0
        ::LoadURL(rSh, m_sURL, nFilter, m_sTargetFrameName);
318
319
0
    if( pClipCntnr && pClipCntnr->HasAnyData() )
320
0
    {
321
0
        pClipCntnr->CopyToClipboard( pWin );
322
0
    }
323
0
}
324
325
OUString SwReadOnlyPopup::SaveGraphic(sal_uInt16 nId)
326
0
{
327
    // fish out the graphic's name
328
0
    if (nId == m_nReadonlySaveBackground)
329
0
    {
330
0
        if ( m_xBrushItem && !m_xBrushItem->GetGraphicLink().isEmpty() )
331
0
            m_sGrfName = m_xBrushItem->GetGraphicLink();
332
0
        const Graphic *pGrf = m_xBrushItem ? m_xBrushItem->GetGraphic() : nullptr;
333
0
        if ( pGrf )
334
0
        {
335
0
            m_aGraphic = *pGrf;
336
0
            if ( !m_xBrushItem->GetGraphicLink().isEmpty() )
337
0
                m_sGrfName = m_xBrushItem->GetGraphicLink();
338
0
        }
339
0
        else
340
0
            return OUString();
341
0
    }
342
0
    return GraphicHelper::ExportGraphic(m_rView.GetFrameWeld(), m_aGraphic, m_sGrfName);
343
0
}
344
345
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */