summaryrefslogtreecommitdiff
path: root/sc/source/ui/view/tabvwshb.cxx
blob: 3b1bf47dc238b91ce7b6622b2682e39466ec9373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <com/sun/star/chart2/data/XDataReceiver.hpp>
#include <com/sun/star/awt/XRequestCallback.hpp>
#include <com/sun/star/awt/Rectangle.hpp>

#include <com/sun/star/embed/EmbedMisc.hpp>
#include <com/sun/star/embed/XEmbeddedObject.hpp>
#include <vcl/errinf.hxx>
#include <sfx2/app.hxx>
#include <toolkit/helper/vclunohelper.hxx>
#include <svx/svxdlg.hxx>
#include <svx/dataaccessdescriptor.hxx>
#include <svx/svditer.hxx>
#include <svx/svdmark.hxx>
#include <svx/svdograf.hxx>
#include <svx/svdogrp.hxx>
#include <svx/svdoole2.hxx>
#include <svx/svdouno.hxx>
#include <svx/svdview.hxx>
#include <sfx2/linkmgr.hxx>
#include <svx/fontworkbar.hxx>
#include <sfx2/bindings.hxx>
#include <sfx2/dispatch.hxx>
#include <sfx2/viewfrm.hxx>
#include <svtools/soerr.hxx>
#include <svl/rectitem.hxx>
#include <svl/stritem.hxx>
#include <svl/slstitm.hxx>
#include <svl/whiter.hxx>
#include <svtools/strings.hrc>
#include <unotools/moduleoptions.hxx>
#include <sot/exchange.hxx>
#include <comphelper/diagnose_ex.hxx>

#include <tabvwsh.hxx>
#include <scmod.hxx>
#include <document.hxx>
#include <sc.hrc>
#include <client.hxx>
#include <fuinsert.hxx>
#include <docsh.hxx>
#include <drawview.hxx>
#include <ChartRangeSelectionListener.hxx>
#include <gridwin.hxx>
#include <undomanager.hxx>
#include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
#include <svx/svdpagv.hxx>
#include <o3tl/temporary.hxx>
#include <officecfg/Office/Common.hxx>

#include <comphelper/lok.hxx>

using namespace com::sun::star;

void ScTabViewShell::ConnectObject( const SdrOle2Obj* pObj )
{
    // is called from paint

    const uno::Reference < embed::XEmbeddedObject >& xObj = pObj->GetObjRef();
    vcl::Window* pWin = GetActiveWin();

    // when already connected do not execute SetObjArea/SetSizeScale again

    SfxInPlaceClient* pClient = FindIPClient( xObj, pWin );
    if ( pClient )
        return;

    pClient = new ScClient( this, pWin, &GetScDrawView()->GetModel(), pObj );
    ScViewData& rViewData = GetViewData();
    ScDocShell* pDocSh = rViewData.GetDocShell();
    ScDocument& rDoc = pDocSh->GetDocument();
    bool bNegativeX = comphelper::LibreOfficeKit::isActive() && rDoc.IsNegativePage(rViewData.GetTabNo());
    if (bNegativeX)
        pClient->SetNegativeX(true);

    tools::Rectangle aRect = pObj->GetLogicRect();
    Size aDrawSize = aRect.GetSize();

    Size aOleSize = pObj->GetOrigObjSize();

    Fraction aScaleWidth (aDrawSize.Width(),  aOleSize.Width() );
    Fraction aScaleHeight(aDrawSize.Height(), aOleSize.Height() );
    aScaleWidth.ReduceInaccurate(10);       // compatible with SdrOle2Obj
    aScaleHeight.ReduceInaccurate(10);
    pClient->SetSizeScale(aScaleWidth,aScaleHeight);

    // visible section is only changed inplace!
    // the object area must be set after the scaling since it triggers the resizing
    aRect.SetSize( aOleSize );
    pClient->SetObjArea( aRect );
}

namespace {

class PopupCallback : public cppu::WeakImplHelper<css::awt::XCallback>
{
    ScTabViewShell* m_pViewShell;
    SdrOle2Obj* m_pObject;

public:
    explicit PopupCallback(ScTabViewShell* pViewShell, SdrOle2Obj* pObject)
        : m_pViewShell(pViewShell)
        , m_pObject(pObject)
    {}

    // XCallback
    virtual void SAL_CALL notify(const css::uno::Any& aData) override
    {
        uno::Sequence<beans::PropertyValue> aProperties;
        if (!(aData >>= aProperties))
            return;

        awt::Rectangle xRectangle;
        sal_Int32 dimensionIndex = 0;
        OUString sPivotTableName(u"DataPilot1"_ustr);

        for (beans::PropertyValue const& rProperty : aProperties)
        {
            if (rProperty.Name == "Rectangle")
                rProperty.Value >>= xRectangle;
            if (rProperty.Name == "DimensionIndex")
                rProperty.Value >>= dimensionIndex;
            if (rProperty.Name == "PivotTableName")
                rProperty.Value >>= sPivotTableName;
        }

        tools::Rectangle aChartRect = m_pObject->GetLogicRect();

        Point aPoint(xRectangle.X  + aChartRect.Left(), xRectangle.Y + aChartRect.Top());
        Size aSize(xRectangle.Width, xRectangle.Height);

        m_pViewShell->DoDPFieldPopup(sPivotTableName, dimensionIndex, aPoint, aSize);
    }
};

}

void ScTabViewShell::ActivateObject(SdrOle2Obj* pObj, sal_Int32 nVerb)
{
    // Do not leave the hint message box on top of the object
    RemoveHintWindow();

    uno::Reference < embed::XEmbeddedObject > xObj = pObj->GetObjRef();
    vcl::Window* pWin = GetActiveWin();
    ErrCodeMsg nErr = ERRCODE_NONE;
    bool bErrorShown = false;

    {
        ScViewData& rViewData = GetViewData();
        ScDocShell* pDocSh = rViewData.GetDocShell();
        ScDocument& rDoc = pDocSh->GetDocument();
        bool bNegativeX = comphelper::LibreOfficeKit::isActive() && rDoc.IsNegativePage(rViewData.GetTabNo());
        SfxInPlaceClient* pClient = FindIPClient( xObj, pWin );
        if ( !pClient )
            pClient = new ScClient( this, pWin, &GetScDrawView()->GetModel(), pObj );

        if (bNegativeX)
            pClient->SetNegativeX(true);

        if ( (sal_uInt32(nErr.GetCode()) & ERRCODE_ERROR_MASK) == 0 && xObj.is() )
        {
            tools::Rectangle aRect = pObj->GetLogicRect();

            {
                // #i118485# center on BoundRect for activation,
                // OLE may be sheared/rotated now
                const tools::Rectangle& rBoundRect = pObj->GetCurrentBoundRect();
                const Point aDelta(rBoundRect.Center() - aRect.Center());
                aRect.Move(aDelta.X(), aDelta.Y());
            }

            Size aDrawSize = aRect.GetSize();

            MapMode aMapMode( MapUnit::Map100thMM );
            Size aOleSize = pObj->GetOrigObjSize( &aMapMode );

            if ( pClient->GetAspect() != embed::Aspects::MSOLE_ICON
              && ( xObj->getStatus( pClient->GetAspect() ) & embed::EmbedMisc::MS_EMBED_RECOMPOSEONRESIZE ) )
            {
                //  scale must always be 1 - change VisArea if different from client size

                if ( aDrawSize != aOleSize )
                {
                    MapUnit aUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( pClient->GetAspect() ) );
                    aOleSize = OutputDevice::LogicToLogic( aDrawSize,
                                MapMode(MapUnit::Map100thMM), MapMode(aUnit));
                    awt::Size aSz( aOleSize.Width(), aOleSize.Height() );
                    xObj->setVisualAreaSize( pClient->GetAspect(), aSz );
                }
                Fraction aOne( 1, 1 );
                pClient->SetSizeScale( aOne, aOne );
            }
            else
            {
                //  calculate scale from client and VisArea size

                Fraction aScaleWidth (aDrawSize.Width(),  aOleSize.Width() );
                Fraction aScaleHeight(aDrawSize.Height(), aOleSize.Height() );
                aScaleWidth.ReduceInaccurate(10);       // compatible with SdrOle2Obj
                aScaleHeight.ReduceInaccurate(10);
                pClient->SetSizeScale(aScaleWidth,aScaleHeight);
            }

            // visible section is only changed inplace!
            // the object area must be set after the scaling since it triggers the resizing
            aRect.SetSize( aOleSize );
            pClient->SetObjArea( aRect );

            nErr = pClient->DoVerb( nVerb );
            bErrorShown = true;
            // SfxViewShell::DoVerb shows its error messages

            // attach listener to selection changes in chart that affect cell
            // ranges, so those can be highlighted
            // note: do that after DoVerb, so that the chart controller exists
            if ( SvtModuleOptions().IsChart() )
            {
                SvGlobalName aObjClsId ( xObj->getClassID() );
                if (SotExchange::IsChart( aObjClsId ))
                {
                    try
                    {
                        uno::Reference < embed::XComponentSupplier > xSup( xObj, uno::UNO_QUERY_THROW );
                        uno::Reference< chart2::data::XDataReceiver > xDataReceiver(
                            xSup->getComponent(), uno::UNO_QUERY_THROW );
                        uno::Reference< chart2::data::XRangeHighlighter > xRangeHighlighter(
                            xDataReceiver->getRangeHighlighter());
                        if (xRangeHighlighter.is())
                        {
                            uno::Reference< view::XSelectionChangeListener > xListener(
                                new ScChartRangeSelectionListener( this ));
                            xRangeHighlighter->addSelectionChangeListener( xListener );
                        }
                        uno::Reference<awt::XRequestCallback> xPopupRequest(xDataReceiver->getPopupRequest());
                        if (xPopupRequest.is())
                        {
                            uno::Reference<awt::XCallback> xCallback(new PopupCallback(this, pObj));
                            uno::Any aAny;
                            xPopupRequest->addCallback(xCallback, aAny);
                        }
                    }
                    catch( const uno::Exception & )
                    {
                        TOOLS_WARN_EXCEPTION( "sc", "Exception caught while querying chart" );
                    }
                }
            }
        }
    }
    if (nErr != ERRCODE_NONE && !bErrorShown)
        ErrorHandler::HandleError(nErr);

    // #i118524# refresh handles to suppress for activated OLE
    if(GetScDrawView())
    {
        GetScDrawView()->AdjustMarkHdl();
    }
    //! SetDocumentName should already happen in Sfx ???
    //TODO/LATER: how "SetDocumentName"?
    //xIPObj->SetDocumentName( GetViewData().GetDocShell()->GetTitle() );
}

ErrCode ScTabViewShell::DoVerb(sal_Int32 nVerb)
{
    SdrView* pView = GetScDrawView();
    if (!pView)
        return ERRCODE_SO_NOTIMPL;          // should not be

    SdrOle2Obj* pOle2Obj = nullptr;

    const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
    if (rMarkList.GetMarkCount() == 1)
    {
        SdrObject* pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
        if (pObj->GetObjIdentifier() == SdrObjKind::OLE2)
            pOle2Obj = static_cast<SdrOle2Obj*>(pObj);
    }

    if (pOle2Obj)
    {
        ActivateObject( pOle2Obj, nVerb );
    }
    else
    {
        OSL_FAIL("no object for Verb found");
    }

    return ERRCODE_NONE;
}

void ScTabViewShell::DeactivateOle()
{
    // deactivate inplace editing if currently active

    ScModule* pScMod = SC_MOD();
    bool bUnoRefDialog = pScMod->IsRefDialogOpen() && pScMod->GetCurRefDlgId() == WID_SIMPLE_REF;

    ScClient* pClient = static_cast<ScClient*>(GetIPClient());
    if ( pClient && pClient->IsObjectInPlaceActive() && !bUnoRefDialog )
        pClient->DeactivateObject();
}

void ScTabViewShell::SetInsertWizardUndoMark()
{
    assert(m_InsertWizardUndoMark == MARK_INVALID);
    m_InsertWizardUndoMark = GetUndoManager()->MarkTopUndoAction();
}

IMPL_LINK( ScTabViewShell, DialogClosedHdl, css::ui::dialogs::DialogClosedEvent*, pEvent, void )
{
    assert(m_InsertWizardUndoMark != MARK_INVALID);
    UndoStackMark nInsertWizardUndoMark = m_InsertWizardUndoMark;
    m_InsertWizardUndoMark = MARK_INVALID;
    if( pEvent->DialogResult == ui::dialogs::ExecutableDialogResults::CANCEL )
    {
        ScTabView* pTabView = GetViewData().GetView();
        ScDrawView* pView = pTabView->GetScDrawView();
        ScViewData& rData = GetViewData();
        ScDocShell* pScDocSh = rData.GetDocShell();
        ScDocument& rScDoc = pScDocSh->GetDocument();
        // leave OLE inplace mode and unmark
        OSL_ASSERT( pView );
        DeactivateOle();
        pView->UnMarkAll();

        auto pUndoManager = rScDoc.GetUndoManager();
        if (pUndoManager->GetRedoActionCount())
        {
            pUndoManager->RemoveMark(nInsertWizardUndoMark);
        }
        else
        {
            pUndoManager->UndoMark(nInsertWizardUndoMark);
            pUndoManager->ClearRedo();
        }

        // leave the draw shell
        SetDrawShell( false );

        // reset marked cell area
        ScMarkData aMark = GetViewData().GetMarkData();
        GetViewData().GetViewShell()->SetMarkData(aMark);
    }
    else
    {
        OSL_ASSERT( pEvent->DialogResult == ui::dialogs::ExecutableDialogResults::OK );
        //@todo maybe move chart to different table
    }
}

void ScTabViewShell::ExecDrawIns(SfxRequest& rReq)
{
    sal_uInt16 nSlot = rReq.GetSlot();
    if (nSlot != SID_OBJECTRESIZE )
    {
        SC_MOD()->InputEnterHandler();
        UpdateInputHandler();
    }

    // insertion of border for Chart is cancelled:
    FuPoor* pPoor = GetDrawFuncPtr();
    if ( pPoor && pPoor->GetSlotID() == SID_DRAW_CHART )
        GetViewData().GetDispatcher().Execute(SID_DRAW_CHART, SfxCallMode::SLOT | SfxCallMode::RECORD);

    MakeDrawLayer();

    ScTabView*   pTabView  = GetViewData().GetView();
    vcl::Window*      pWin      = pTabView->GetActiveWin();
    ScDrawView*  pView     = pTabView->GetScDrawView();
    ScDocShell*  pDocSh    = GetViewData().GetDocShell();
    ScDocument&  rDoc      = pDocSh->GetDocument();
    SdrModel& rModel = pView->GetModel();

    switch ( nSlot )
    {
        case SID_INSERT_GRAPHIC:
            FuInsertGraphic(*this, pWin, pView, &rModel, rReq);
            // shell is set in MarkListHasChanged
            break;

        case SID_INSERT_AVMEDIA:
            FuInsertMedia(*this, pWin, pView, &rModel, rReq);
            // shell is set in MarkListHasChanged
            break;

        case SID_INSERT_DIAGRAM:
            FuInsertChart(*this, pWin, pView, &rModel, rReq, LINK( this, ScTabViewShell, DialogClosedHdl ));
            if (comphelper::LibreOfficeKit::isActive())
                pDocSh->SetModified();
            break;

        case SID_INSERT_OBJECT:
        case SID_INSERT_SMATH:
        case SID_INSERT_FLOATINGFRAME:
            FuInsertOLE(*this, pWin, pView, &rModel, rReq);
            break;

        case SID_INSERT_SIGNATURELINE:
        case SID_EDIT_SIGNATURELINE:
            {
                const uno::Reference<frame::XModel> xModel( GetViewData().GetDocShell()->GetBaseModel() );

                VclAbstractDialogFactory* pFact = VclAbstractDialogFactory::Create();
                VclPtr<AbstractSignatureLineDialog> pDialog(pFact->CreateSignatureLineDialog(
                    pWin->GetFrameWeld(), xModel, rReq.GetSlot() == SID_EDIT_SIGNATURELINE));
                auto xRequest = std::make_shared<SfxRequest>(rReq);
                rReq.Ignore(); // the 'old' request is not relevant any more
                pDialog->StartExecuteAsync(
                    [pDialog, xRequest=std::move(xRequest)] (sal_Int32 nResult)->void
                    {
                        if (nResult == RET_OK)
                            pDialog->Apply();
                        pDialog->disposeOnce();
                        xRequest->Done();
                    }
                );
                break;
            }

        case SID_SIGN_SIGNATURELINE:
            {
                const uno::Reference<frame::XModel> xModel(
                    GetViewData().GetDocShell()->GetBaseModel());

                VclAbstractDialogFactory* pFact = VclAbstractDialogFactory::Create();
                VclPtr<AbstractSignSignatureLineDialog> pDialog(
                    pFact->CreateSignSignatureLineDialog(GetFrameWeld(), xModel));
                pDialog->StartExecuteAsync(
                    [pDialog] (sal_Int32 nResult)->void
                    {
                        if (nResult == RET_OK)
                            pDialog->Apply();
                        pDialog->disposeOnce();
                    }
                );
                break;
            }

        case SID_INSERT_QRCODE:
        case SID_EDIT_QRCODE:
            {
                const uno::Reference<frame::XModel> xModel( GetViewData().GetDocShell()->GetBaseModel() );

                VclAbstractDialogFactory* pFact = VclAbstractDialogFactory::Create();
                ScopedVclPtr<AbstractQrCodeGenDialog> pDialog(pFact->CreateQrCodeGenDialog(
                    pWin->GetFrameWeld(), xModel, rReq.GetSlot() == SID_EDIT_QRCODE));
                pDialog->Execute();
                break;
            }

            case SID_OBJECTRESIZE:
            {
                //         the server would like to change the client size

                SfxInPlaceClient* pClient = GetIPClient();

                if ( pClient && pClient->IsObjectInPlaceActive() )
                {
                    const SfxRectangleItem& rRect = rReq.GetArgs()->Get(SID_OBJECTRESIZE);
                    tools::Rectangle aRect( pWin->PixelToLogic( rRect.GetValue() ) );

                    const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
                    if ( rMarkList.GetMarkCount() != 0 )
                    {
                        if (rMarkList.GetMarkCount() == 1)
                        {
                            SdrMark* pMark = rMarkList.GetMark(0);
                            SdrObject* pObj = pMark->GetMarkedSdrObj();

                            SdrObjKind nSdrObjKind = pObj->GetObjIdentifier();

                            if (nSdrObjKind == SdrObjKind::OLE2)
                            {
                                if ( static_cast<SdrOle2Obj*>(pObj)->GetObjRef().is() )
                                {
                                    pObj->SetLogicRect(aRect);
                                }
                            }
                        }
                    }
                }
            }
            break;

        case SID_LINKS:
            {
                SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
                if (officecfg::Office::Common::Security::Scripting::DisableActiveContent::get())
                {
                    std::unique_ptr<weld::MessageDialog> xError(Application::CreateMessageDialog(
                        nullptr, VclMessageType::Warning, VclButtonsType::Ok,
                        SvtResId(STR_WARNING_EXTERNAL_LINK_EDIT_DISABLED)));
                    xError->run();
                    break;
                }

                VclPtr<SfxAbstractLinksDialog> pDlg(pFact->CreateLinksDialog(pWin->GetFrameWeld(), rDoc.GetLinkManager()));
                auto xRequest = std::make_shared<SfxRequest>(rReq);
                rReq.Ignore(); // the 'old' request is not relevant any more
                pDlg->StartExecuteAsync(
                    [this, pDlg, xRequest=std::move(xRequest)] (sal_Int32 /*nResult*/)->void
                    {
                        GetViewFrame().GetBindings().Invalidate( SID_LINKS );
                        SfxGetpApp()->Broadcast( SfxHint( SfxHintId::ScAreaLinksChanged ) );     // Navigator
                        pDlg->disposeOnce();
                        xRequest->Done();
                    }
                );
            }
            break;

        case SID_FM_CREATE_FIELDCONTROL:
            {
                const SfxUnoAnyItem* pDescriptorItem = rReq.GetArg<SfxUnoAnyItem>(SID_FM_DATACCESS_DESCRIPTOR);
                OSL_ENSURE( pDescriptorItem, "SID_FM_CREATE_FIELDCONTROL: invalid request args!" );

                if(pDescriptorItem)
                {
                    //! merge with ScViewFunc::PasteDataFormat (SotClipboardFormatId::SBA_FIELDDATAEXCHANGE)?

                    ScDrawView* pDrView = GetScDrawView();
                    SdrPageView* pPageView = pDrView ? pDrView->GetSdrPageView() : nullptr;
                    if(pPageView)
                    {
                        svx::ODataAccessDescriptor aDescriptor(pDescriptorItem->GetValue());
                        rtl::Reference<SdrObject> pNewDBField = pDrView->CreateFieldControl(aDescriptor);

                        if(pNewDBField)
                        {
                            tools::Rectangle aVisArea = pWin->PixelToLogic(tools::Rectangle(Point(0,0), pWin->GetOutputSizePixel()));
                            Point aObjPos(aVisArea.Center());
                            Size aObjSize(pNewDBField->GetLogicRect().GetSize());
                            aObjPos.AdjustX( -(aObjSize.Width() / 2) );
                            aObjPos.AdjustY( -(aObjSize.Height() / 2) );
                            tools::Rectangle aNewObjectRectangle(aObjPos, aObjSize);

                            pNewDBField->SetLogicRect(aNewObjectRectangle);

                            // controls must be on control layer, groups on front layer
                            if ( dynamic_cast<const SdrUnoObj*>( pNewDBField.get() ) !=  nullptr )
                                pNewDBField->NbcSetLayer(SC_LAYER_CONTROLS);
                            else
                                pNewDBField->NbcSetLayer(SC_LAYER_FRONT);
                            if (dynamic_cast<const SdrObjGroup*>( pNewDBField.get() ) !=  nullptr)
                            {
                                SdrObjListIter aIter( *pNewDBField, SdrIterMode::DeepWithGroups );
                                SdrObject* pSubObj = aIter.Next();
                                while (pSubObj)
                                {
                                    if ( dynamic_cast<const SdrUnoObj*>( pSubObj) !=  nullptr )
                                        pSubObj->NbcSetLayer(SC_LAYER_CONTROLS);
                                    else
                                        pSubObj->NbcSetLayer(SC_LAYER_FRONT);
                                    pSubObj = aIter.Next();
                                }
                            }

                            pView->InsertObjectAtView(pNewDBField.get(), *pPageView);
                        }
                    }
                }
                rReq.Done();
            }
            break;

        case SID_FONTWORK_GALLERY_FLOATER:
            svx::FontworkBar::execute(*pView, rReq, GetViewFrame().GetBindings());
            rReq.Ignore();
            break;
    }
}

void ScTabViewShell::GetDrawInsState(SfxItemSet &rSet)
{
    bool bOle = GetViewFrame().GetFrame().IsInPlace();
    bool bTabProt = GetViewData().GetDocument().IsTabProtected(GetViewData().GetTabNo());
    ScDocShell* pDocShell = GetViewData().GetDocShell();
    bool bShared = pDocShell && pDocShell->IsDocShared();
    SdrView* pSdrView = GetScDrawView();

    SfxWhichIter aIter(rSet);
    sal_uInt16 nWhich = aIter.FirstWhich();
    while ( nWhich )
    {
        switch ( nWhich )
        {
            case SID_INSERT_DIAGRAM:
                if ( bOle || bTabProt || !SvtModuleOptions().IsChart() || bShared )
                    rSet.DisableItem( nWhich );
                break;

            case SID_INSERT_SMATH:
                if ( bOle || bTabProt || !SvtModuleOptions().IsMath() || bShared )
                    rSet.DisableItem( nWhich );
                break;

            case SID_INSERT_OBJECT:
            case SID_INSERT_FLOATINGFRAME:
                if ( bOle || bTabProt || bShared )
                    rSet.DisableItem( nWhich );
                break;

            case SID_INSERT_AVMEDIA:
            case SID_FONTWORK_GALLERY_FLOATER:
                if ( bTabProt || bShared )
                    rSet.DisableItem( nWhich );
                break;

            case SID_INSERT_SIGNATURELINE:
            case SID_INSERT_QRCODE:
                if ( bTabProt || bShared || (pSdrView && pSdrView->GetMarkedObjectList().GetMarkCount() != 0))
                    rSet.DisableItem( nWhich );
                break;
            case SID_EDIT_SIGNATURELINE:
            case SID_SIGN_SIGNATURELINE:
                if (!IsSignatureLineSelected() || IsSignatureLineSigned())
                    rSet.DisableItem(nWhich);
                break;

            case SID_EDIT_QRCODE:
                if (!IsQRCodeSelected())
                    rSet.DisableItem(nWhich);
                break;

            case SID_INSERT_GRAPHIC:
                if (bTabProt || bShared)
                {
                    // do not disable 'insert graphic' item if the currently marked area is editable (not protected)
                    // if there is no marked area, check the current cell
                    bool bDisableInsertImage = true;
                    ScMarkData& rMark = GetViewData().GetMarkData();
                    if (!rMark.GetMarkedRanges().empty() && GetViewData().GetDocument().IsSelectionEditable(rMark))
                        bDisableInsertImage = false;
                    else
                    {
                        if (GetViewData().GetDocument().IsBlockEditable
                            (GetViewData().GetTabNo(), GetViewData().GetCurX(), GetViewData().GetCurY(), GetViewData().GetCurX(), GetViewData().GetCurY()))
                        {
                            bDisableInsertImage = false;
                        }
                    }

                    if (bDisableInsertImage)
                        rSet.DisableItem(nWhich);
                }
                break;

            case SID_LINKS:
                {
                    if (GetViewData().GetDocument().GetLinkManager()->GetLinks().empty())
                        rSet.DisableItem( SID_LINKS );
                }
                break;
        }
        nWhich = aIter.NextWhich();
    }
}

bool ScTabViewShell::IsSignatureLineSelected()
{
    SdrView* pSdrView = GetScDrawView();
    if (!pSdrView)
        return false;

    const SdrMarkList& rMarkList = pSdrView->GetMarkedObjectList();
    if (rMarkList.GetMarkCount() != 1)
        return false;

    SdrObject* pPickObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
    if (!pPickObj)
        return false;

    SdrGrafObj* pGraphic = dynamic_cast<SdrGrafObj*>(pPickObj);
    if (!pGraphic)
        return false;

    return pGraphic->isSignatureLine();
}

bool ScTabViewShell::IsQRCodeSelected()
{
    SdrView* pSdrView = GetScDrawView();
    if (!pSdrView)
        return false;

    const SdrMarkList& rMarkList = pSdrView->GetMarkedObjectList();
    if (rMarkList.GetMarkCount() != 1)
        return false;

    SdrObject* pPickObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
    if (!pPickObj)
        return false;

    SdrGrafObj* pGraphic = dynamic_cast<SdrGrafObj*>(pPickObj);
    if (!pGraphic)
        return false;

    if(pGraphic->getQrCode())
    {
        return true;
    }
    else{
        return false;
    }
}

bool ScTabViewShell::IsSignatureLineSigned()
{
    SdrView* pSdrView = GetScDrawView();
    if (!pSdrView)
        return false;

    const SdrMarkList& rMarkList = pSdrView->GetMarkedObjectList();
    if (rMarkList.GetMarkCount() != 1)
        return false;

    SdrObject* pPickObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
    if (!pPickObj)
        return false;

    SdrGrafObj* pGraphic = dynamic_cast<SdrGrafObj*>(pPickObj);
    if (!pGraphic)
        return false;

    return pGraphic->isSignatureLineSigned();
}

void ScTabViewShell::ExecuteUndo(SfxRequest& rReq)
{
    SfxShell* pSh = GetViewData().GetDispatcher().GetShell(0);
    if (!pSh)
        return;

    ScUndoManager* pUndoManager = static_cast<ScUndoManager*>(pSh->GetUndoManager());

    const SfxItemSet* pReqArgs = rReq.GetArgs();
    ScDocShell* pDocSh = GetViewData().GetDocShell();

    sal_uInt16 nSlot = rReq.GetSlot();
    switch ( nSlot )
    {
        case SID_UNDO:
        case SID_REDO:
            if ( pUndoManager )
            {
                bool bIsUndo = ( nSlot == SID_UNDO );

                sal_uInt16 nCount = 1;
                const SfxPoolItem* pItem;
                if ( pReqArgs && pReqArgs->GetItemState( nSlot, true, &pItem ) == SfxItemState::SET )
                    nCount = static_cast<const SfxUInt16Item*>(pItem)->GetValue();

                // Repair mode: allow undo/redo of all undo actions, even if access would
                // be limited based on the view shell ID.
                bool bRepair = false;
                if (pReqArgs && pReqArgs->GetItemState(SID_REPAIRPACKAGE, false, &pItem) == SfxItemState::SET)
                    bRepair = static_cast<const SfxBoolItem*>(pItem)->GetValue();

                sal_uInt16 nUndoOffset = 0;
                if (comphelper::LibreOfficeKit::isActive() && !bRepair)
                {
                    SfxUndoAction* pAction = nullptr;
                    if (bIsUndo)
                    {
                        if (pUndoManager->GetUndoActionCount() != 0)
                            pAction = pUndoManager->GetUndoAction();
                    }
                    else
                    {
                        if (pUndoManager->GetRedoActionCount() != 0)
                            pAction = pUndoManager->GetRedoAction();
                    }
                    if (pAction)
                    {
                        // If another view created the undo action, prevent undoing it from this view.
                        // Unless we know that the other view's undo action is independent from us.
                        ViewShellId nViewShellId = GetViewShellId();
                        if (pAction->GetViewShellId() != nViewShellId)
                        {
                            sal_uInt16 nOffset = 0;
                            if (pUndoManager->IsViewUndoActionIndependent(this, nOffset))
                            {
                                // Execute the undo with an offset: don't undo the top action, but an
                                // earlier one, since it's independent and that belongs to our view.
                                nUndoOffset += nOffset;
                            }
                            else
                            {
                                rReq.SetReturnValue(SfxUInt32Item(SID_UNDO, static_cast<sal_uInt32>(SID_REPAIRPACKAGE)));
                                return;
                            }
                        }
                    }
                }

                // lock paint for more than one cell undo action (not for editing within a cell)
                bool bLockPaint = ( nCount > 1 && pUndoManager == GetUndoManager() );
                if ( bLockPaint )
                    pDocSh->LockPaint();

                try
                {
                    ScUndoRedoContext aUndoRedoContext;
                    aUndoRedoContext.SetUndoOffset(nUndoOffset);

                    for (sal_uInt16 i=0; i<nCount; i++)
                    {
                        if ( bIsUndo )
                            pUndoManager->UndoWithContext(aUndoRedoContext);
                        else
                            pUndoManager->RedoWithContext(aUndoRedoContext);
                    }
                }
                catch ( const uno::Exception& )
                {
                    // no need to handle. By definition, the UndoManager handled this by clearing the
                    // Undo/Redo stacks
                }

                if ( bLockPaint )
                    pDocSh->UnlockPaint();

                GetViewFrame().GetBindings().InvalidateAll(false);
            }
            break;
//      default:
//          GetViewFrame().ExecuteSlot( rReq );
    }
}

void ScTabViewShell::GetUndoState(SfxItemSet &rSet)
{
    SfxShell* pSh = GetViewData().GetDispatcher().GetShell(0);
    if (!pSh)
        return;

    SfxUndoManager* pUndoManager = pSh->GetUndoManager();
    ScUndoManager* pScUndoManager = dynamic_cast<ScUndoManager*>(pUndoManager);

    SfxWhichIter aIter(rSet);
    sal_uInt16 nWhich = aIter.FirstWhich();
    while ( nWhich )
    {
        switch (nWhich)
        {
            case SID_GETUNDOSTRINGS:
            case SID_GETREDOSTRINGS:
                {
                    SfxStringListItem aStrLst( nWhich );
                    if ( pUndoManager )
                    {
                        std::vector<OUString> &aList = aStrLst.GetList();
                        bool bIsUndo = ( nWhich == SID_GETUNDOSTRINGS );
                        size_t nCount = bIsUndo ? pUndoManager->GetUndoActionCount() : pUndoManager->GetRedoActionCount();
                        for (size_t i=0; i<nCount; ++i)
                        {
                            aList.push_back( bIsUndo ? pUndoManager->GetUndoActionComment(i) :
                                                       pUndoManager->GetRedoActionComment(i) );
                        }
                    }
                    rSet.Put( aStrLst );
                }
                break;

            case SID_UNDO:
            {
                if (pScUndoManager)
                {
                    if (pScUndoManager->GetUndoActionCount())
                    {
                        const SfxUndoAction* pAction = pScUndoManager->GetUndoAction();
                        SfxViewShell *pViewSh = GetViewShell();
                        if (pViewSh && pAction->GetViewShellId() != pViewSh->GetViewShellId()
                            && !pScUndoManager->IsViewUndoActionIndependent(this, o3tl::temporary(sal_uInt16())))
                        {
                            rSet.Put(SfxUInt32Item(SID_UNDO, static_cast<sal_uInt32>(SID_REPAIRPACKAGE)));
                        }
                        else
                        {
                            rSet.Put( SfxStringItem( SID_UNDO, SvtResId(STR_UNDO)+pScUndoManager->GetUndoActionComment() ) );
                        }
                    }
                    else
                        rSet.DisableItem( SID_UNDO );
                }
                else
                    // get state from sfx view frame
                    GetViewFrame().GetSlotState( nWhich, nullptr, &rSet );
                break;
            }
            case SID_REDO:
            {
                if (pScUndoManager)
                {
                    if (pScUndoManager->GetRedoActionCount())
                    {
                        const SfxUndoAction* pAction = pScUndoManager->GetRedoAction();
                        SfxViewShell *pViewSh = GetViewShell();
                        if (pViewSh && pAction->GetViewShellId() != pViewSh->GetViewShellId())
                        {
                            rSet.Put(SfxUInt32Item(SID_REDO, static_cast<sal_uInt32>(SID_REPAIRPACKAGE)));
                        }
                        else
                        {
                            rSet.Put(SfxStringItem(SID_REDO, SvtResId(STR_REDO) + pScUndoManager->GetRedoActionComment()));
                        }
                    }
                    else
                        rSet.DisableItem( SID_REDO );
                }
                else
                    // get state from sfx view frame
                    GetViewFrame().GetSlotState( nWhich, nullptr, &rSet );
                break;
            }
            default:
                // get state from sfx view frame
                GetViewFrame().GetSlotState( nWhich, nullptr, &rSet );
        }

        nWhich = aIter.NextWhich();
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */