Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sc/source/ui/drawfunc/fuins1.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
#include <config_features.h>
21
22
#include <officecfg/Office/Common.hxx>
23
#include <editeng/sizeitem.hxx>
24
#include <sal/log.hxx>
25
#include <sfx2/lokhelper.hxx>
26
#include <sfx2/opengrf.hxx>
27
#include <sfx2/viewfrm.hxx>
28
#include <svx/svdograf.hxx>
29
#include <svx/svdomedia.hxx>
30
#include <svx/svdpage.hxx>
31
#include <svx/svdpagv.hxx>
32
#include <svx/svdview.hxx>
33
#include <svx/linkwarn.hxx>
34
#include <svx/svxids.hrc>
35
#include <vcl/graphicfilter.hxx>
36
#include <svl/stritem.hxx>
37
#include <avmedia/mediawindow.hxx>
38
#include <vcl/svapp.hxx>
39
#include <vcl/weld/weld.hxx>
40
#include <vcl/GraphicNativeTransform.hxx>
41
#include <vcl/GraphicNativeMetadata.hxx>
42
#include <fuinsert.hxx>
43
#include <tabvwsh.hxx>
44
#include <drwlayer.hxx>
45
#include <drawview.hxx>
46
#include <document.hxx>
47
#include <scresid.hxx>
48
#include <strings.hrc>
49
#include <globstr.hrc>
50
#include <comphelper/lok.hxx>
51
52
#include <tools/hostfilter.hxx>
53
#include <tools/urlobj.hxx>
54
55
#include <com/sun/star/frame/XDispatchProvider.hpp>
56
#include <com/sun/star/media/XPlayer.hpp>
57
#include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
58
#include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
59
#include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
60
#include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
61
#include <com/sun/star/uno/Sequence.hxx>
62
63
using namespace css;
64
using namespace css::uno;
65
66
void ScLimitSizeOnDrawPage( Size& rSize, Point& rPos, const Size& rPage )
67
0
{
68
0
    if ( !rPage.Width() || !rPage.Height() )
69
0
        return;
70
71
0
    Size aPageSize = rPage;
72
0
    bool bNegative = aPageSize.Width() < 0;
73
0
    if ( bNegative )
74
0
    {
75
        //  make everything positive temporarily
76
0
        aPageSize.setWidth( -aPageSize.Width() );
77
0
        rPos.setX( -rPos.X() - rSize.Width() );
78
0
    }
79
80
0
    if ( rSize.Width() > aPageSize.Width() || rSize.Height() > aPageSize.Height() )
81
0
    {
82
0
        double fX = aPageSize.Width()  / static_cast<double>(rSize.Width());
83
0
        double fY = aPageSize.Height() / static_cast<double>(rSize.Height());
84
85
0
        if ( fX < fY )
86
0
        {
87
0
            rSize.setWidth( aPageSize.Width() );
88
0
            rSize.setHeight( static_cast<tools::Long>( rSize.Height() * fX ) );
89
0
        }
90
0
        else
91
0
        {
92
0
            rSize.setHeight( aPageSize.Height() );
93
0
            rSize.setWidth( static_cast<tools::Long>( rSize.Width() * fY ) );
94
0
        }
95
96
0
        if (!rSize.Width())
97
0
            rSize.setWidth( 1 );
98
0
        if (!rSize.Height())
99
0
            rSize.setHeight( 1 );
100
0
    }
101
102
0
    if ( rPos.X() + rSize.Width() > aPageSize.Width() )
103
0
        rPos.setX( aPageSize.Width() - rSize.Width() );
104
0
    if ( rPos.Y() + rSize.Height() > aPageSize.Height() )
105
0
        rPos.setY( aPageSize.Height() - rSize.Height() );
106
107
0
    if ( bNegative )
108
0
        rPos.setX( -rPos.X() - rSize.Width() );       // back to real position
109
0
}
110
111
static void lcl_InsertGraphic( const Graphic& rGraphic,
112
                        const OUString& rFileName, bool bAsLink, bool bApi,
113
                        ScTabViewShell& rViewSh, const vcl::Window* pWindow, SdrView* pView,
114
                        ScAnchorType aAnchorType = SCA_CELL )
115
0
{
116
0
    Graphic& rGraphic1 = const_cast<Graphic &>(rGraphic);
117
0
    GraphicNativeMetadata aMetadata;
118
0
    if ( aMetadata.read(rGraphic1) )
119
0
    {
120
0
        const Degree10 aRotation = aMetadata.getRotation();
121
0
        if (aRotation)
122
0
        {
123
0
            GraphicNativeTransform aTransform( rGraphic1 );
124
0
            aTransform.rotate( aRotation );
125
0
        }
126
0
    }
127
0
    ScDrawView* pDrawView = rViewSh.GetScDrawView();
128
129
    //  set the size so the graphic has its original pixel size
130
    //  at 100% view scale (as in SetMarkedOriginalSize),
131
    //  instead of respecting the current view scale
132
0
    MapMode aSourceMap = rGraphic.GetPrefMapMode();
133
0
    MapMode aDestMap( MapUnit::Map100thMM );
134
0
    if ( aSourceMap.GetMapUnit() == MapUnit::MapPixel && pDrawView )
135
0
    {
136
0
        Fraction aScaleX, aScaleY;
137
0
        pDrawView->CalcNormScale( aScaleX, aScaleY );
138
0
        aDestMap.SetScaleX(aScaleX);
139
0
        aDestMap.SetScaleY(aScaleY);
140
0
    }
141
0
    Size aLogicSize = pWindow->LogicToLogic(
142
0
                            rGraphic.GetPrefSize(), &aSourceMap, &aDestMap );
143
144
    //  Limit size
145
146
0
    SdrPageView* pPV  = pView->GetSdrPageView();
147
0
    SdrPage* pPage = pPV->GetPage();
148
0
    Point aInsertPos = rViewSh.GetInsertPos();
149
150
0
    ScViewData& rData = rViewSh.GetViewData();
151
0
    if ( rData.GetDocument().IsNegativePage( rData.CurrentTabForData() ) )
152
0
        aInsertPos.AdjustX( -(aLogicSize.Width()) );       // move position to left edge
153
154
0
    ScLimitSizeOnDrawPage( aLogicSize, aInsertPos, pPage->GetSize() );
155
156
0
    tools::Rectangle aRect ( aInsertPos, aLogicSize );
157
158
0
    rtl::Reference<SdrGrafObj> pObj = new SdrGrafObj(
159
0
        pView->getSdrModelFromSdrView(), // TTTT pView should be reference
160
0
        rGraphic1,
161
0
        aRect);
162
163
    // calling SetGraphicLink here doesn't work
164
    // Yes, due to the SdrObject had no SdrModel
165
    //  Path is no longer used as name for the graphics object
166
167
0
    ScDrawLayer* pLayer = static_cast<ScDrawLayer*>(&pView->GetModel());
168
0
    OUString aName = pLayer->GetNewGraphicName();                 // "Graphics"
169
0
    pObj->SetName(aName);
170
171
0
    if (aAnchorType == SCA_CELL || aAnchorType == SCA_CELL_RESIZE)
172
0
        ScDrawLayer::SetCellAnchoredFromPosition(*pObj, rData.GetDocument(), rData.CurrentTabForData(),
173
0
                                                 aAnchorType == SCA_CELL_RESIZE);
174
175
    //  don't select if from (dispatch) API, to allow subsequent cell operations
176
0
    SdrInsertFlags nInsOptions = (bApi && !comphelper::LibreOfficeKit::isActive()) ? SdrInsertFlags::DONTMARK : SdrInsertFlags::NONE;
177
0
    bool bSuccess = pView->InsertObjectAtView( pObj.get(), *pPV, nInsOptions );
178
179
    // SetGraphicLink has to be used after inserting the object,
180
    // otherwise an empty graphic is swapped in and the contact stuff crashes.
181
    // See #i37444#.
182
0
    if (bSuccess && bAsLink)
183
0
        pObj->SetGraphicLink( rFileName );
184
0
}
185
186
#if HAVE_FEATURE_AVMEDIA
187
188
static void lcl_InsertMedia( const OUString& rMediaURL, bool bApi,
189
                      ScTabViewShell* pViewSh, const vcl::Window* pWindow, SdrView* pView,
190
                      const Size& rPrefSize, bool const bLink )
191
0
{
192
0
    SdrPageView*    pPV  = pView->GetSdrPageView();
193
0
    SdrPage*        pPage = pPV->GetPage();
194
0
    ScViewData&     rData = pViewSh->GetViewData();
195
0
    Point           aInsertPos( pViewSh->GetInsertPos() );
196
0
    Size            aSize;
197
198
0
    if( rPrefSize.Width() && rPrefSize.Height() )
199
0
    {
200
0
        if( pWindow )
201
0
            aSize = pWindow->PixelToLogic(rPrefSize, MapMode(MapUnit::Map100thMM));
202
0
        else
203
0
            aSize = Application::GetDefaultDevice()->PixelToLogic(rPrefSize, MapMode(MapUnit::Map100thMM));
204
0
    }
205
0
    else
206
0
        aSize = Size( 5000, 5000 );
207
208
0
    ScLimitSizeOnDrawPage( aSize, aInsertPos, pPage->GetSize() );
209
210
0
    if( rData.GetDocument().IsNegativePage( rData.CurrentTabForData() ) )
211
0
        aInsertPos.AdjustX( -(aSize.Width()) );
212
213
0
    OUString realURL;
214
0
    if (bLink)
215
0
    {
216
0
        realURL = rMediaURL;
217
0
    }
218
0
    else
219
0
    {
220
0
        uno::Reference<frame::XModel> const xModel(
221
0
                rData.GetDocument().GetDocumentShell()->GetModel());
222
0
        bool const bRet = ::avmedia::EmbedMedia(xModel, rMediaURL, realURL);
223
0
        if (!bRet) { return; }
224
0
    }
225
226
0
    rtl::Reference<SdrMediaObj> pObj = new SdrMediaObj(
227
0
        *rData.GetDocument().GetDrawLayer(),
228
0
        tools::Rectangle(aInsertPos, aSize));
229
230
0
    pObj->setURL( realURL, u""_ustr/*TODO?*/ );
231
0
    pView->InsertObjectAtView( pObj.get(), *pPV, bApi ? SdrInsertFlags::DONTMARK : SdrInsertFlags::NONE );
232
0
}
233
#endif
234
235
FuInsertGraphic::FuInsertGraphic( ScTabViewShell&   rViewSh,
236
                                  vcl::Window*      pWin,
237
                                  ScDrawView*       pViewP,
238
                                  SdrModel&         rDoc,
239
                                  SfxRequest&       rReq )
240
0
       : FuPoor(rViewSh, pWin, pViewP, rDoc, rReq)
241
0
{
242
0
    const SfxItemSet* pReqArgs = rReq.GetArgs();
243
0
    const SfxStringItem* pGraphicItem;
244
0
    if ( pReqArgs &&
245
0
         (pGraphicItem = pReqArgs->GetItemIfSet( SID_INSERT_GRAPHIC, true )) )
246
0
    {
247
0
        const OUString& aFileName = pGraphicItem->GetValue();
248
249
0
        OUString aFilterName;
250
0
        if ( const SfxStringItem* pFilterItem = pReqArgs->GetItemIfSet( FN_PARAM_FILTER ) )
251
0
            aFilterName = pFilterItem->GetValue();
252
253
0
        bool bAsLink = false;
254
0
        const SfxPoolItem* pItem;
255
0
        if ( pReqArgs->GetItemState( FN_PARAM_1, true, &pItem ) == SfxItemState::SET )
256
0
            bAsLink = static_cast<const SfxBoolItem*>(pItem)->GetValue();
257
258
0
        if (comphelper::LibreOfficeKit::isActive())
259
0
        {
260
0
            INetURLObject aURL(aFileName);
261
0
            if (INetProtocol::File != aURL.GetProtocol() && HostFilter::isForbidden(aURL.GetHost()))
262
0
                SfxLokHelper::sendNetworkAccessError("insert");
263
0
        }
264
265
0
        Graphic aGraphic;
266
0
        ErrCode nError = GraphicFilter::LoadGraphic( aFileName, aFilterName, aGraphic, &GraphicFilter::GetGraphicFilter() );
267
0
        if ( nError == ERRCODE_NONE )
268
0
        {
269
0
            lcl_InsertGraphic( aGraphic, aFileName, bAsLink, true, rViewSh, pWindow, pView );
270
0
        }
271
0
    }
272
0
    else
273
0
    {
274
0
        SvxOpenGraphicDialog aDlg(ScResId(STR_INSERTGRAPHIC), pWin ? pWin->GetFrameWeld() : nullptr,
275
0
                                  ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW_IMAGE_ANCHOR);
276
277
0
        Reference<ui::dialogs::XFilePickerControlAccess> xCtrlAcc = aDlg.GetFilePickerControlAccess();
278
0
        sal_Int16 nSelect = 0;
279
0
        Sequence<OUString> aListBoxEntries {
280
0
            ScResId(STR_ANCHOR_TO_CELL),
281
0
            ScResId(STR_ANCHOR_TO_CELL_RESIZE),
282
0
            ScResId(STR_ANCHOR_TO_PAGE)
283
0
        };
284
0
        try
285
0
        {
286
0
            Any aTemplates(&aListBoxEntries, cppu::UnoType<decltype(aListBoxEntries)>::get());
287
288
0
            xCtrlAcc->setValue(ui::dialogs::ExtendedFilePickerElementIds::LISTBOX_IMAGE_ANCHOR,
289
0
                ui::dialogs::ListboxControlActions::ADD_ITEMS, aTemplates);
290
291
0
            Any aSelectPos(&nSelect, cppu::UnoType<decltype(nSelect)>::get());
292
0
            xCtrlAcc->setValue(ui::dialogs::ExtendedFilePickerElementIds::LISTBOX_IMAGE_ANCHOR,
293
0
                ui::dialogs::ListboxControlActions::SET_SELECT_ITEM, aSelectPos);
294
0
        }
295
0
        catch (const Exception&)
296
0
        {
297
0
            SAL_WARN("sc", "control access failed");
298
0
        }
299
300
0
        if( aDlg.Execute() == ERRCODE_NONE )
301
0
        {
302
0
            Graphic aGraphic;
303
0
            ErrCode nError = aDlg.GetGraphic(aGraphic);
304
0
            if( nError == ERRCODE_NONE )
305
0
            {
306
0
                OUString aFileName = aDlg.GetPath();
307
0
                const OUString& aFilterName = aDlg.GetDetectedFilter();
308
0
                bool bAsLink = aDlg.IsAsLink();
309
310
                // really store as link only?
311
0
                if( bAsLink && officecfg::Office::Common::Misc::ShowLinkWarningDialog::get() )
312
0
                {
313
0
                    SvxLinkWarningDialog aWarnDlg(pWin ? pWin->GetFrameWeld() : nullptr, aFileName);
314
0
                    if (aWarnDlg.run() != RET_OK)
315
0
                        bAsLink = false; // don't store as link
316
0
                }
317
318
                // Anchor to cell or to page?
319
0
                Any aAnchorValue = xCtrlAcc->getValue(
320
0
                    ui::dialogs::ExtendedFilePickerElementIds::LISTBOX_IMAGE_ANCHOR,
321
0
                    ui::dialogs::ListboxControlActions::GET_SELECTED_ITEM );
322
0
                OUString sAnchor;
323
0
                aAnchorValue >>= sAnchor;
324
325
0
                ScAnchorType aAnchorType;
326
0
                if (sAnchor == ScResId(STR_ANCHOR_TO_CELL))
327
0
                    aAnchorType = SCA_CELL;
328
0
                else if (sAnchor == ScResId(STR_ANCHOR_TO_CELL_RESIZE))
329
0
                    aAnchorType = SCA_CELL_RESIZE;
330
0
                else if (sAnchor == ScResId(STR_ANCHOR_TO_PAGE))
331
0
                    aAnchorType = SCA_PAGE;
332
0
                else
333
0
                    aAnchorType = SCA_DONTKNOW;
334
335
0
                lcl_InsertGraphic( aGraphic, aFileName, bAsLink, false, rViewSh, pWindow, pView, aAnchorType );
336
337
                //  append items for recording
338
0
                rReq.AppendItem( SfxStringItem( SID_INSERT_GRAPHIC, aFileName ) );
339
0
                rReq.AppendItem( SfxStringItem( FN_PARAM_FILTER, aFilterName ) );
340
0
                rReq.AppendItem( SfxBoolItem( FN_PARAM_1, bAsLink ) );
341
0
                rReq.Done();
342
0
            }
343
0
            else
344
0
            {
345
                //  error is handled in SvxOpenGraphicDialog::GetGraphic
346
0
            }
347
0
        }
348
0
    }
349
0
}
350
351
FuInsertGraphic::~FuInsertGraphic()
352
0
{
353
0
}
354
355
FuInsertMedia::FuInsertMedia( ScTabViewShell&   rViewSh,
356
                              vcl::Window*      pWin,
357
                              ScDrawView*       pViewP,
358
                              SdrModel&         rDoc,
359
                              const SfxRequest& rReq ) :
360
0
    FuPoor(rViewSh, pWin, pViewP, rDoc, rReq)
361
0
{
362
0
#if HAVE_FEATURE_AVMEDIA
363
0
    OUString     aURL;
364
0
    const SfxItemSet*   pReqArgs = rReq.GetArgs();
365
0
    bool                bAPI = false;
366
367
0
    const SvxSizeItem* pSizeItem = rReq.GetArg<SvxSizeItem>(FN_PARAM_1);
368
0
    const SfxBoolItem* pLinkItem = rReq.GetArg<SfxBoolItem>(FN_PARAM_2);
369
0
    const bool bSizeUnknown = !pSizeItem;
370
0
    Size aPrefSize;
371
372
0
    if( pReqArgs )
373
0
    {
374
0
        const SfxStringItem* pStringItem = dynamic_cast<const SfxStringItem*>( &pReqArgs->Get( rReq.GetSlot() )  );
375
376
0
        if( pStringItem )
377
0
        {
378
0
            aURL = pStringItem->GetValue();
379
0
            bAPI = aURL.getLength();
380
0
        }
381
0
    }
382
383
0
    bool bLink(pLinkItem ? pLinkItem->GetValue() : true);
384
0
    bool bInsertMedia = bAPI;
385
0
    if (!bInsertMedia)
386
0
        bInsertMedia = ::avmedia::MediaWindow::executeMediaURLDialog(pWin ? pWin->GetFrameWeld() : nullptr, aURL, &bLink);
387
0
    if (!bInsertMedia)
388
0
        return;
389
390
0
    if (!bSizeUnknown)
391
0
    {
392
0
        aPrefSize = pSizeItem->GetSize();
393
0
    }
394
0
    else
395
0
    {
396
0
        if( pWin )
397
0
            pWin->EnterWait();
398
399
0
        css::uno::Reference<css::frame::XDispatchProvider> xDispatchProvider(rViewShell.GetViewFrame().GetFrame().GetFrameInterface(), css::uno::UNO_QUERY);
400
401
0
        rtl::Reference<avmedia::PlayerListener> xPlayerListener(new avmedia::PlayerListener(
402
0
            [xDispatchProvider, aURL, bLink](const css::uno::Reference<css::media::XPlayer>& rPlayer){
403
0
                css::awt::Size aSize = rPlayer->getPreferredPlayerWindowSize();
404
0
                avmedia::MediaWindow::dispatchInsertAVMedia(xDispatchProvider, aSize, aURL, bLink);
405
0
            }));
406
407
0
        const bool bIsMediaURL = ::avmedia::MediaWindow::isMediaURL(aURL, u""_ustr/*TODO?*/, true, xPlayerListener);
408
409
0
        if( pWin )
410
0
            pWin->LeaveWait();
411
412
0
        if (!bIsMediaURL && !bAPI)
413
0
            ::avmedia::MediaWindow::executeFormatErrorBox(pWindow ? pWindow->GetFrameWeld() : nullptr);
414
415
0
        return;
416
0
    }
417
418
0
    if (pWin)
419
0
        pWin->EnterWait();
420
421
0
    lcl_InsertMedia(aURL, bAPI, &rViewSh, pWindow, pView, aPrefSize, bLink);
422
423
0
    if (pWin)
424
0
        pWin->LeaveWait();
425
0
#endif
426
0
}
427
428
FuInsertMedia::~FuInsertMedia()
429
0
{
430
0
}
431
432
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */