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 <com/sun/star/chart2/data/XDataReceiver.hpp>
21 #include <com/sun/star/awt/XRequestCallback.hpp>
22 #include <com/sun/star/awt/Rectangle.hpp>
23 
24 #include <com/sun/star/embed/EmbedMisc.hpp>
25 #include <com/sun/star/embed/XEmbeddedObject.hpp>
26 #include <vcl/errinf.hxx>
27 #include <sfx2/app.hxx>
28 #include <toolkit/helper/vclunohelper.hxx>
29 #include <svx/svxdlg.hxx>
30 #include <svx/dataaccessdescriptor.hxx>
31 #include <svx/svditer.hxx>
32 #include <svx/svdmark.hxx>
33 #include <svx/svdograf.hxx>
34 #include <svx/svdogrp.hxx>
35 #include <svx/svdoole2.hxx>
36 #include <svx/svdouno.hxx>
37 #include <svx/svdview.hxx>
38 #include <sfx2/linkmgr.hxx>
39 #include <svx/fontworkbar.hxx>
40 #include <sfx2/bindings.hxx>
41 #include <sfx2/dispatch.hxx>
42 #include <sfx2/viewfrm.hxx>
43 #include <svtools/soerr.hxx>
44 #include <svl/rectitem.hxx>
45 #include <svl/slstitm.hxx>
46 #include <svl/whiter.hxx>
47 #include <unotools/moduleoptions.hxx>
48 #include <sot/exchange.hxx>
49 
50 #include <tabvwsh.hxx>
51 #include <scmod.hxx>
52 #include <document.hxx>
53 #include <sc.hrc>
54 #include <client.hxx>
55 #include <fuinsert.hxx>
56 #include <docsh.hxx>
57 #include <drawview.hxx>
58 #include <ChartRangeSelectionListener.hxx>
59 #include <gridwin.hxx>
60 
61 #include <comphelper/lok.hxx>
62 
63 using namespace com::sun::star;
64 
ConnectObject(const SdrOle2Obj * pObj)65 void ScTabViewShell::ConnectObject( const SdrOle2Obj* pObj )
66 {
67     // is called from paint
68 
69     uno::Reference < embed::XEmbeddedObject > xObj = pObj->GetObjRef();
70     vcl::Window* pWin = GetActiveWin();
71 
72     // when already connected do not execute SetObjArea/SetSizeScale again
73 
74     SfxInPlaceClient* pClient = FindIPClient( xObj, pWin );
75     if ( !pClient )
76     {
77         pClient = new ScClient( this, pWin, GetSdrView()->GetModel(), pObj );
78         tools::Rectangle aRect = pObj->GetLogicRect();
79         Size aDrawSize = aRect.GetSize();
80 
81         Size aOleSize = pObj->GetOrigObjSize();
82 
83         Fraction aScaleWidth (aDrawSize.Width(),  aOleSize.Width() );
84         Fraction aScaleHeight(aDrawSize.Height(), aOleSize.Height() );
85         aScaleWidth.ReduceInaccurate(10);       // compatible with SdrOle2Obj
86         aScaleHeight.ReduceInaccurate(10);
87         pClient->SetSizeScale(aScaleWidth,aScaleHeight);
88 
89         // visible section is only changed inplace!
90         // the object area must be set after the scaling since it triggers the resizing
91         aRect.SetSize( aOleSize );
92         pClient->SetObjArea( aRect );
93     }
94 }
95 
96 class PopupCallback : public cppu::WeakImplHelper<css::awt::XCallback>
97 {
98     ScTabViewShell* m_pViewShell;
99     SdrOle2Obj* const m_pObject;
100 
101 public:
PopupCallback(ScTabViewShell * pViewShell,SdrOle2Obj * pObject)102     explicit PopupCallback(ScTabViewShell* pViewShell, SdrOle2Obj* pObject)
103         : m_pViewShell(pViewShell)
104         , m_pObject(pObject)
105     {}
106 
107     // XCallback
notify(const css::uno::Any & aData)108     virtual void SAL_CALL notify(const css::uno::Any& aData) override
109     {
110         uno::Sequence<beans::PropertyValue> aProperties;
111         if (aData >>= aProperties)
112         {
113             awt::Rectangle xRectangle;
114             sal_Int32 dimensionIndex = 0;
115             OUString sPivotTableName("DataPilot1");
116 
117             for (beans::PropertyValue const & rProperty : std::as_const(aProperties))
118             {
119                 if (rProperty.Name == "Rectangle")
120                     rProperty.Value >>= xRectangle;
121                 if (rProperty.Name == "DimensionIndex")
122                     rProperty.Value >>= dimensionIndex;
123                 if (rProperty.Name == "PivotTableName")
124                     rProperty.Value >>= sPivotTableName;
125             }
126 
127             tools::Rectangle aChartRect = m_pObject->GetLogicRect();
128 
129             Point aPoint(xRectangle.X  + aChartRect.Left(), xRectangle.Y + aChartRect.Top());
130             Size aSize(xRectangle.Width, xRectangle.Height);
131 
132             m_pViewShell->DoDPFieldPopup(sPivotTableName, dimensionIndex, aPoint, aSize);
133         }
134     }
135 };
136 
ActivateObject(SdrOle2Obj * pObj,long nVerb)137 void ScTabViewShell::ActivateObject( SdrOle2Obj* pObj, long nVerb )
138 {
139     // Do not leave the hint message box on top of the object
140     RemoveHintWindow();
141 
142     uno::Reference < embed::XEmbeddedObject > xObj = pObj->GetObjRef();
143     vcl::Window* pWin = GetActiveWin();
144     ErrCode nErr = ERRCODE_NONE;
145     bool bErrorShown = false;
146 
147     {
148         SfxInPlaceClient* pClient = FindIPClient( xObj, pWin );
149         if ( !pClient )
150             pClient = new ScClient( this, pWin, GetSdrView()->GetModel(), pObj );
151 
152         if ( (sal_uInt32(nErr) & ERRCODE_ERROR_MASK) == 0 && xObj.is() )
153         {
154             tools::Rectangle aRect = pObj->GetLogicRect();
155 
156             {
157                 // #i118485# center on BoundRect for activation,
158                 // OLE may be sheared/rotated now
159                 const tools::Rectangle& rBoundRect = pObj->GetCurrentBoundRect();
160                 const Point aDelta(rBoundRect.Center() - aRect.Center());
161                 aRect.Move(aDelta.X(), aDelta.Y());
162             }
163 
164             Size aDrawSize = aRect.GetSize();
165 
166             MapMode aMapMode( MapUnit::Map100thMM );
167             Size aOleSize = pObj->GetOrigObjSize( &aMapMode );
168 
169             if ( pClient->GetAspect() != embed::Aspects::MSOLE_ICON
170               && ( xObj->getStatus( pClient->GetAspect() ) & embed::EmbedMisc::MS_EMBED_RECOMPOSEONRESIZE ) )
171             {
172                 //  scale must always be 1 - change VisArea if different from client size
173 
174                 if ( aDrawSize != aOleSize )
175                 {
176                     MapUnit aUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( pClient->GetAspect() ) );
177                     aOleSize = OutputDevice::LogicToLogic( aDrawSize,
178                                 MapMode(MapUnit::Map100thMM), MapMode(aUnit));
179                     awt::Size aSz( aOleSize.Width(), aOleSize.Height() );
180                     xObj->setVisualAreaSize( pClient->GetAspect(), aSz );
181                 }
182                 Fraction aOne( 1, 1 );
183                 pClient->SetSizeScale( aOne, aOne );
184             }
185             else
186             {
187                 //  calculate scale from client and VisArea size
188 
189                 Fraction aScaleWidth (aDrawSize.Width(),  aOleSize.Width() );
190                 Fraction aScaleHeight(aDrawSize.Height(), aOleSize.Height() );
191                 aScaleWidth.ReduceInaccurate(10);       // compatible with SdrOle2Obj
192                 aScaleHeight.ReduceInaccurate(10);
193                 pClient->SetSizeScale(aScaleWidth,aScaleHeight);
194             }
195 
196             // visible section is only changed inplace!
197             // the object area must be set after the scaling since it triggers the resizing
198             aRect.SetSize( aOleSize );
199             pClient->SetObjArea( aRect );
200 
201             nErr = pClient->DoVerb( nVerb );
202             bErrorShown = true;
203             // SfxViewShell::DoVerb shows its error messages
204 
205             // attach listener to selection changes in chart that affect cell
206             // ranges, so those can be highlighted
207             // note: do that after DoVerb, so that the chart controller exists
208             if ( SvtModuleOptions().IsChart() )
209             {
210                 SvGlobalName aObjClsId ( xObj->getClassID() );
211                 if (SotExchange::IsChart( aObjClsId ))
212                 {
213                     try
214                     {
215                         uno::Reference < embed::XComponentSupplier > xSup( xObj, uno::UNO_QUERY_THROW );
216                         uno::Reference< chart2::data::XDataReceiver > xDataReceiver(
217                             xSup->getComponent(), uno::UNO_QUERY_THROW );
218                         uno::Reference< chart2::data::XRangeHighlighter > xRangeHightlighter(
219                             xDataReceiver->getRangeHighlighter());
220                         if (xRangeHightlighter.is())
221                         {
222                             uno::Reference< view::XSelectionChangeListener > xListener(
223                                 new ScChartRangeSelectionListener( this ));
224                             xRangeHightlighter->addSelectionChangeListener( xListener );
225                         }
226                         uno::Reference<awt::XRequestCallback> xPopupRequest(xDataReceiver->getPopupRequest());
227                         if (xPopupRequest.is())
228                         {
229                             uno::Reference<awt::XCallback> xCallback(new PopupCallback(this, pObj));
230                             uno::Any aAny;
231                             xPopupRequest->addCallback(xCallback, aAny);
232                         }
233                     }
234                     catch( const uno::Exception & )
235                     {
236                         OSL_FAIL( "Exception caught while querying chart" );
237                     }
238                 }
239             }
240         }
241     }
242     if (nErr != ERRCODE_NONE && !bErrorShown)
243         ErrorHandler::HandleError(nErr);
244 
245     // #i118524# refresh handles to suppress for activated OLE
246     if(GetSdrView())
247     {
248         GetSdrView()->AdjustMarkHdl();
249     }
250     //! SetDocumentName should already happen in Sfx ???
251     //TODO/LATER: how "SetDocumentName"?
252     //xIPObj->SetDocumentName( GetViewData().GetDocShell()->GetTitle() );
253 }
254 
DoVerb(long nVerb)255 ErrCode ScTabViewShell::DoVerb(long nVerb)
256 {
257     SdrView* pView = GetSdrView();
258     if (!pView)
259         return ERRCODE_SO_NOTIMPL;          // should not be
260 
261     SdrOle2Obj* pOle2Obj = nullptr;
262 
263     const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
264     if (rMarkList.GetMarkCount() == 1)
265     {
266         SdrObject* pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
267         if (pObj->GetObjIdentifier() == OBJ_OLE2)
268             pOle2Obj = static_cast<SdrOle2Obj*>(pObj);
269     }
270 
271     if (pOle2Obj)
272     {
273         ActivateObject( pOle2Obj, nVerb );
274     }
275     else
276     {
277         OSL_FAIL("no object for Verb found");
278     }
279 
280     return ERRCODE_NONE;
281 }
282 
DeactivateOle()283 void ScTabViewShell::DeactivateOle()
284 {
285     // deactivate inplace editing if currently active
286 
287     ScModule* pScMod = SC_MOD();
288     bool bUnoRefDialog = pScMod->IsRefDialogOpen() && pScMod->GetCurRefDlgId() == WID_SIMPLE_REF;
289 
290     ScClient* pClient = static_cast<ScClient*>(GetIPClient());
291     if ( pClient && pClient->IsObjectInPlaceActive() && !bUnoRefDialog )
292         pClient->DeactivateObject();
293 }
294 
ExecDrawIns(SfxRequest & rReq)295 void ScTabViewShell::ExecDrawIns(SfxRequest& rReq)
296 {
297     sal_uInt16 nSlot = rReq.GetSlot();
298     if (nSlot != SID_OBJECTRESIZE )
299     {
300         SC_MOD()->InputEnterHandler();
301         UpdateInputHandler();
302     }
303 
304     // insertion of border for Chart is cancelled:
305     FuPoor* pPoor = GetDrawFuncPtr();
306     if ( pPoor && pPoor->GetSlotID() == SID_DRAW_CHART )
307         GetViewData().GetDispatcher().Execute(SID_DRAW_CHART, SfxCallMode::SLOT | SfxCallMode::RECORD);
308 
309     MakeDrawLayer();
310 
311     SfxBindings& rBindings = GetViewFrame()->GetBindings();
312     ScTabView*   pTabView  = GetViewData().GetView();
313     vcl::Window*      pWin      = pTabView->GetActiveWin();
314     ScDrawView*  pView     = pTabView->GetScDrawView();
315     ScDocShell*  pDocSh    = GetViewData().GetDocShell();
316     ScDocument&  rDoc      = pDocSh->GetDocument();
317     SdrModel*    pDrModel  = pView->GetModel();
318 
319     switch ( nSlot )
320     {
321         case SID_INSERT_GRAPHIC:
322             FuInsertGraphic(*this, pWin, pView, pDrModel, rReq);
323             // shell is set in MarkListHasChanged
324             break;
325 
326         case SID_INSERT_AVMEDIA:
327             FuInsertMedia(*this, pWin, pView, pDrModel, rReq);
328             // shell is set in MarkListHasChanged
329             break;
330 
331         case SID_INSERT_DIAGRAM:
332             FuInsertChart(*this, pWin, pView, pDrModel, rReq);
333             break;
334 
335         case SID_INSERT_OBJECT:
336         case SID_INSERT_SMATH:
337         case SID_INSERT_FLOATINGFRAME:
338             FuInsertOLE(*this, pWin, pView, pDrModel, rReq);
339             break;
340 
341         case SID_INSERT_SIGNATURELINE:
342         case SID_EDIT_SIGNATURELINE:
343             {
344                 const uno::Reference<frame::XModel> xModel( GetViewData().GetDocShell()->GetBaseModel() );
345 
346                 VclAbstractDialogFactory* pFact = VclAbstractDialogFactory::Create();
347                 ScopedVclPtr<AbstractSignatureLineDialog> pDialog(pFact->CreateSignatureLineDialog(
348                     pWin->GetFrameWeld(), xModel, rReq.GetSlot() == SID_EDIT_SIGNATURELINE));
349                 pDialog->Execute();
350                 break;
351             }
352 
353         case SID_SIGN_SIGNATURELINE:
354             {
355                 const uno::Reference<frame::XModel> xModel(
356                     GetViewData().GetDocShell()->GetBaseModel());
357 
358                 VclAbstractDialogFactory* pFact = VclAbstractDialogFactory::Create();
359                 ScopedVclPtr<AbstractSignSignatureLineDialog> pDialog(
360                     pFact->CreateSignSignatureLineDialog(GetFrameWeld(), xModel));
361                 pDialog->Execute();
362                 break;
363             }
364 
365         case SID_INSERT_QRCODE:
366         case SID_EDIT_QRCODE:
367             {
368                 const uno::Reference<frame::XModel> xModel( GetViewData().GetDocShell()->GetBaseModel() );
369 
370                 VclAbstractDialogFactory* pFact = VclAbstractDialogFactory::Create();
371                 ScopedVclPtr<AbstractQrCodeGenDialog> pDialog(pFact->CreateQrCodeGenDialog(
372                     pWin->GetFrameWeld(), xModel, rReq.GetSlot() == SID_EDIT_QRCODE));
373                 pDialog->Execute();
374                 break;
375             }
376 
377         case SID_OBJECTRESIZE:
378             {
379                 //         the server would like to change the client size
380 
381                 SfxInPlaceClient* pClient = GetIPClient();
382 
383                 if ( pClient && pClient->IsObjectInPlaceActive() )
384                 {
385                     const SfxRectangleItem& rRect = rReq.GetArgs()->Get(SID_OBJECTRESIZE);
386                     tools::Rectangle aRect( pWin->PixelToLogic( rRect.GetValue() ) );
387 
388                     if ( pView->AreObjectsMarked() )
389                     {
390                         const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
391 
392                         if (rMarkList.GetMarkCount() == 1)
393                         {
394                             SdrMark* pMark = rMarkList.GetMark(0);
395                             SdrObject* pObj = pMark->GetMarkedSdrObj();
396 
397                             sal_uInt16 nSdrObjKind = pObj->GetObjIdentifier();
398 
399                             if (nSdrObjKind == OBJ_OLE2)
400                             {
401                                 if ( static_cast<SdrOle2Obj*>(pObj)->GetObjRef().is() )
402                                 {
403                                     pObj->SetLogicRect(aRect);
404                                 }
405                             }
406                         }
407                     }
408                 }
409             }
410             break;
411 
412         case SID_LINKS:
413             {
414                 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
415                 ScopedVclPtr<SfxAbstractLinksDialog> pDlg(pFact->CreateLinksDialog(pWin->GetFrameWeld(), rDoc.GetLinkManager()));
416                 pDlg->Execute();
417                 rBindings.Invalidate( nSlot );
418                 SfxGetpApp()->Broadcast( SfxHint( SfxHintId::ScAreaLinksChanged ) );     // Navigator
419                 rReq.Done();
420             }
421             break;
422 
423         case SID_FM_CREATE_FIELDCONTROL:
424             {
425                 const SfxUnoAnyItem* pDescriptorItem = rReq.GetArg<SfxUnoAnyItem>(SID_FM_DATACCESS_DESCRIPTOR);
426                 OSL_ENSURE( pDescriptorItem, "SID_FM_CREATE_FIELDCONTROL: invalid request args!" );
427 
428                 if(pDescriptorItem)
429                 {
430                     //! merge with ScViewFunc::PasteDataFormat (SotClipboardFormatId::SBA_FIELDDATAEXCHANGE)?
431 
432                     ScDrawView* pDrView = GetScDrawView();
433                     SdrPageView* pPageView = pDrView ? pDrView->GetSdrPageView() : nullptr;
434                     if(pPageView)
435                     {
436                         svx::ODataAccessDescriptor aDescriptor(pDescriptorItem->GetValue());
437                         SdrObjectUniquePtr pNewDBField = pDrView->CreateFieldControl(aDescriptor);
438 
439                         if(pNewDBField)
440                         {
441                             tools::Rectangle aVisArea = pWin->PixelToLogic(tools::Rectangle(Point(0,0), pWin->GetOutputSizePixel()));
442                             Point aObjPos(aVisArea.Center());
443                             Size aObjSize(pNewDBField->GetLogicRect().GetSize());
444                             aObjPos.AdjustX( -(aObjSize.Width() / 2) );
445                             aObjPos.AdjustY( -(aObjSize.Height() / 2) );
446                             tools::Rectangle aNewObjectRectangle(aObjPos, aObjSize);
447 
448                             pNewDBField->SetLogicRect(aNewObjectRectangle);
449 
450                             // controls must be on control layer, groups on front layer
451                             if ( dynamic_cast<const SdrUnoObj*>( pNewDBField.get() ) !=  nullptr )
452                                 pNewDBField->NbcSetLayer(SC_LAYER_CONTROLS);
453                             else
454                                 pNewDBField->NbcSetLayer(SC_LAYER_FRONT);
455                             if (dynamic_cast<const SdrObjGroup*>( pNewDBField.get() ) !=  nullptr)
456                             {
457                                 SdrObjListIter aIter( *pNewDBField, SdrIterMode::DeepWithGroups );
458                                 SdrObject* pSubObj = aIter.Next();
459                                 while (pSubObj)
460                                 {
461                                     if ( dynamic_cast<const SdrUnoObj*>( pSubObj) !=  nullptr )
462                                         pSubObj->NbcSetLayer(SC_LAYER_CONTROLS);
463                                     else
464                                         pSubObj->NbcSetLayer(SC_LAYER_FRONT);
465                                     pSubObj = aIter.Next();
466                                 }
467                             }
468 
469                             pView->InsertObjectAtView(pNewDBField.release(), *pPageView);
470                         }
471                     }
472                 }
473                 rReq.Done();
474             }
475             break;
476 
477         case SID_FONTWORK_GALLERY_FLOATER:
478             svx::FontworkBar::execute(*pView, rReq, GetViewFrame()->GetBindings());
479             rReq.Ignore();
480             break;
481     }
482 }
483 
GetDrawInsState(SfxItemSet & rSet)484 void ScTabViewShell::GetDrawInsState(SfxItemSet &rSet)
485 {
486     bool bOle = GetViewFrame()->GetFrame().IsInPlace();
487     bool bTabProt = GetViewData().GetDocument()->IsTabProtected(GetViewData().GetTabNo());
488     ScDocShell* pDocShell = GetViewData().GetDocShell();
489     bool bShared = pDocShell && pDocShell->IsDocShared();
490     SdrView* pSdrView = GetSdrView();
491 
492     SfxWhichIter aIter(rSet);
493     sal_uInt16 nWhich = aIter.FirstWhich();
494     while ( nWhich )
495     {
496         switch ( nWhich )
497         {
498             case SID_INSERT_DIAGRAM:
499                 if ( bOle || bTabProt || !SvtModuleOptions().IsChart() || bShared )
500                     rSet.DisableItem( nWhich );
501                 break;
502 
503             case SID_INSERT_SMATH:
504                 if ( bOle || bTabProt || !SvtModuleOptions().IsMath() || bShared )
505                     rSet.DisableItem( nWhich );
506                 break;
507 
508             case SID_INSERT_OBJECT:
509             case SID_INSERT_FLOATINGFRAME:
510                 if ( bOle || bTabProt || bShared )
511                     rSet.DisableItem( nWhich );
512                 break;
513 
514             case SID_INSERT_AVMEDIA:
515             case SID_FONTWORK_GALLERY_FLOATER:
516                 if ( bTabProt || bShared )
517                     rSet.DisableItem( nWhich );
518                 break;
519 
520             case SID_INSERT_SIGNATURELINE:
521                 if ( bTabProt || bShared || (pSdrView && pSdrView->GetMarkedObjectCount() != 0))
522                     rSet.DisableItem( nWhich );
523                 break;
524             case SID_EDIT_SIGNATURELINE:
525             case SID_SIGN_SIGNATURELINE:
526                 if (!IsSignatureLineSelected() || IsSignatureLineSigned())
527                     rSet.DisableItem(nWhich);
528                 break;
529 
530             case SID_INSERT_QRCODE:
531                 if ( bTabProt || bShared || (pSdrView && pSdrView->GetMarkedObjectCount() != 0))
532                     rSet.DisableItem( nWhich );
533                 break;
534             case SID_EDIT_QRCODE:
535                 if (!IsQRCodeSelected())
536                     rSet.DisableItem(nWhich);
537                 break;
538 
539             case SID_INSERT_GRAPHIC:
540                 if (bTabProt || bShared)
541                 {
542                     // do not disable 'insert graphic' item if the currently marked area is editable (not protected)
543                     // if there is no marked area, check the current cell
544                     bool bDisableInsertImage = true;
545                     ScMarkData& rMark = GetViewData().GetMarkData();
546                     if (!rMark.GetMarkedRanges().empty() && GetViewData().GetDocument()->IsSelectionEditable(rMark))
547                         bDisableInsertImage = false;
548                     else
549                     {
550                         if (GetViewData().GetDocument()->IsBlockEditable
551                             (GetViewData().GetTabNo(), GetViewData().GetCurX(), GetViewData().GetCurY(), GetViewData().GetCurX(), GetViewData().GetCurY()))
552                         {
553                             bDisableInsertImage = false;
554                         }
555                     }
556 
557                     if (bDisableInsertImage)
558                         rSet.DisableItem(nWhich);
559                 }
560                 break;
561 
562             case SID_LINKS:
563                 {
564                     if (GetViewData().GetDocument()->GetLinkManager()->GetLinks().empty())
565                         rSet.DisableItem( SID_LINKS );
566                 }
567                 break;
568         }
569         nWhich = aIter.NextWhich();
570     }
571 }
572 
IsSignatureLineSelected()573 bool ScTabViewShell::IsSignatureLineSelected()
574 {
575     SdrView* pSdrView = GetSdrView();
576     if (!pSdrView)
577         return false;
578 
579     if (pSdrView->GetMarkedObjectCount() != 1)
580         return false;
581 
582     SdrObject* pPickObj = pSdrView->GetMarkedObjectByIndex(0);
583     if (!pPickObj)
584         return false;
585 
586     SdrGrafObj* pGraphic = dynamic_cast<SdrGrafObj*>(pPickObj);
587     if (!pGraphic)
588         return false;
589 
590     return pGraphic->isSignatureLine();
591 }
592 
IsQRCodeSelected()593 bool ScTabViewShell::IsQRCodeSelected()
594 {
595     SdrView* pSdrView = GetSdrView();
596     if (!pSdrView)
597         return false;
598 
599     if (pSdrView->GetMarkedObjectCount() != 1)
600         return false;
601 
602     SdrObject* pPickObj = pSdrView->GetMarkedObjectByIndex(0);
603     if (!pPickObj)
604         return false;
605 
606     SdrGrafObj* pGraphic = dynamic_cast<SdrGrafObj*>(pPickObj);
607     if (!pGraphic)
608         return false;
609 
610     if(pGraphic->getQrCode())
611     {
612         return true;
613     }
614     else{
615         return false;
616     }
617 }
618 
IsSignatureLineSigned()619 bool ScTabViewShell::IsSignatureLineSigned()
620 {
621     SdrView* pSdrView = GetSdrView();
622     if (!pSdrView)
623         return false;
624 
625     if (pSdrView->GetMarkedObjectCount() != 1)
626         return false;
627 
628     SdrObject* pPickObj = pSdrView->GetMarkedObjectByIndex(0);
629     if (!pPickObj)
630         return false;
631 
632     SdrGrafObj* pGraphic = dynamic_cast<SdrGrafObj*>(pPickObj);
633     if (!pGraphic)
634         return false;
635 
636     return pGraphic->isSignatureLineSigned();
637 }
638 
ExecuteUndo(SfxRequest & rReq)639 void ScTabViewShell::ExecuteUndo(SfxRequest& rReq)
640 {
641     SfxShell* pSh = GetViewData().GetDispatcher().GetShell(0);
642     SfxUndoManager* pUndoManager = pSh->GetUndoManager();
643 
644     const SfxItemSet* pReqArgs = rReq.GetArgs();
645     ScDocShell* pDocSh = GetViewData().GetDocShell();
646 
647     sal_uInt16 nSlot = rReq.GetSlot();
648     switch ( nSlot )
649     {
650         case SID_UNDO:
651         case SID_REDO:
652             if ( pUndoManager )
653             {
654                 bool bIsUndo = ( nSlot == SID_UNDO );
655 
656                 sal_uInt16 nCount = 1;
657                 const SfxPoolItem* pItem;
658                 if ( pReqArgs && pReqArgs->GetItemState( nSlot, true, &pItem ) == SfxItemState::SET )
659                     nCount = static_cast<const SfxUInt16Item*>(pItem)->GetValue();
660 
661                 // Repair mode: allow undo/redo of all undo actions, even if access would
662                 // be limited based on the view shell ID.
663                 bool bRepair = false;
664                 if (pReqArgs && pReqArgs->GetItemState(SID_REPAIRPACKAGE, false, &pItem) == SfxItemState::SET)
665                     bRepair = static_cast<const SfxBoolItem*>(pItem)->GetValue();
666 
667                 if (comphelper::LibreOfficeKit::isActive() && !bRepair)
668                 {
669                     SfxUndoAction* pAction = nullptr;
670                     if (bIsUndo)
671                     {
672                         if (pUndoManager->GetUndoActionCount() != 0)
673                             pAction = pUndoManager->GetUndoAction();
674                     }
675                     else
676                     {
677                         if (pUndoManager->GetRedoActionCount() != 0)
678                             pAction = pUndoManager->GetRedoAction();
679                     }
680                     if (pAction)
681                     {
682                         ViewShellId nViewShellId = GetViewShellId();
683                         if (pAction->GetViewShellId() != nViewShellId)
684                         {
685                             rReq.SetReturnValue(SfxUInt32Item(SID_UNDO, static_cast<sal_uInt32>(SID_REPAIRPACKAGE)));
686                             return;
687                         }
688                     }
689                 }
690 
691                 // lock paint for more than one cell undo action (not for editing within a cell)
692                 bool bLockPaint = ( nCount > 1 && pUndoManager == GetUndoManager() );
693                 if ( bLockPaint )
694                     pDocSh->LockPaint();
695 
696                 try
697                 {
698                     for (sal_uInt16 i=0; i<nCount; i++)
699                     {
700                         if ( bIsUndo )
701                             pUndoManager->Undo();
702                         else
703                             pUndoManager->Redo();
704                     }
705                 }
706                 catch ( const uno::Exception& )
707                 {
708                     // no need to handle. By definition, the UndoManager handled this by clearing the
709                     // Undo/Redo stacks
710                 }
711 
712                 if ( bLockPaint )
713                     pDocSh->UnlockPaint();
714 
715                 GetViewFrame()->GetBindings().InvalidateAll(false);
716             }
717             break;
718 //      default:
719 //          GetViewFrame()->ExecuteSlot( rReq );
720     }
721 }
722 
GetUndoState(SfxItemSet & rSet)723 void ScTabViewShell::GetUndoState(SfxItemSet &rSet)
724 {
725     SfxShell* pSh = GetViewData().GetDispatcher().GetShell(0);
726     SfxUndoManager* pUndoManager = pSh->GetUndoManager();
727 
728     SfxWhichIter aIter(rSet);
729     sal_uInt16 nWhich = aIter.FirstWhich();
730     while ( nWhich )
731     {
732         switch (nWhich)
733         {
734             case SID_GETUNDOSTRINGS:
735             case SID_GETREDOSTRINGS:
736                 {
737                     SfxStringListItem aStrLst( nWhich );
738                     if ( pUndoManager )
739                     {
740                         std::vector<OUString> &aList = aStrLst.GetList();
741                         bool bIsUndo = ( nWhich == SID_GETUNDOSTRINGS );
742                         size_t nCount = bIsUndo ? pUndoManager->GetUndoActionCount() : pUndoManager->GetRedoActionCount();
743                         for (size_t i=0; i<nCount; ++i)
744                         {
745                             aList.push_back( bIsUndo ? pUndoManager->GetUndoActionComment(i) :
746                                                        pUndoManager->GetRedoActionComment(i) );
747                         }
748                     }
749                     rSet.Put( aStrLst );
750                 }
751                 break;
752             default:
753                 // get state from sfx view frame
754                 GetViewFrame()->GetSlotState( nWhich, nullptr, &rSet );
755         }
756 
757         nWhich = aIter.NextWhich();
758     }
759 }
760 
761 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
762