sc/sdi/scalc.sdi                   |    2 
 sc/source/ui/attrdlg/scdlgfact.cxx |    7 +
 sc/source/ui/attrdlg/scdlgfact.hxx |    5 -
 sc/source/ui/inc/viewfunc.hxx      |    3 
 sc/source/ui/view/tabvwshf.cxx     |  160 +++++++++++++++++++++++++++++++------
 sc/source/ui/view/viewfun2.cxx     |   61 +++++++++-----
 6 files changed, 188 insertions(+), 50 deletions(-)

New commits:
commit d24b3ee8195d96846649a407606ae9068ef2114c
Author:     Bayram Çiçek <bayram.ci...@collabora.com>
AuthorDate: Tue Oct 17 11:48:18 2023 +0300
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Tue Jan 23 15:43:42 2024 +0100

    sc: make inactive sheets movable and copyable
    
    - move and copy use the same ScViewFunc::MoveTable
    function
    - added 3 new parameters to "SfxVoidItem Move FID_TAB_MOVE"
      - SfxBoolItem FromContextMenu FN_PARAM_4,
      - SfxUInt16Item ContextMenuIndex FN_PARAM_5,
      - SfxBoolItem MoveOrCopySheetDialog FN_PARAM_6
    - moveLeft and moveRight of inactive tabs are implemented
    - move and copy of inactive tabs from the Move/Copy Sheet
    dialog are implemented
    - made Move/Copy Sheet dialog async
    
    Change-Id: I97461780a6c46f601bce9490315cd5cbea9505d4
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/158078
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Gökay ŞATIR <gokaysa...@collabora.com>
    (cherry picked from commit 4d5f8b43b04734805f48b921b98a74fd9ab67d0f)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/162387
    Tested-by: Caolán McNamara <caolan.mcnam...@collabora.com>
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/sc/sdi/scalc.sdi b/sc/sdi/scalc.sdi
index 38151604a3d9..4240bb5ffc86 100644
--- a/sc/sdi/scalc.sdi
+++ b/sc/sdi/scalc.sdi
@@ -3598,7 +3598,7 @@ SfxVoidItem Mirror SID_OBJECT_MIRROR
 
 
 SfxVoidItem Move FID_TAB_MOVE
-(SfxStringItem DocName FID_TAB_MOVE,SfxUInt16Item Index FN_PARAM_1,SfxBoolItem 
Copy FN_PARAM_2,SfxBoolItem UseCurrentDocument FN_PARAM_3)
+(SfxStringItem DocName FID_TAB_MOVE,SfxUInt16Item Index FN_PARAM_1,SfxBoolItem 
Copy FN_PARAM_2,SfxBoolItem UseCurrentDocument FN_PARAM_3,SfxBoolItem 
FromContextMenu FN_PARAM_4,SfxUInt16Item ContextMenuIndex 
FN_PARAM_5,SfxBoolItem MoveOrCopySheetDialog FN_PARAM_6)
 [
     AutoUpdate = FALSE,
     FastCall = FALSE,
diff --git a/sc/source/ui/attrdlg/scdlgfact.cxx 
b/sc/source/ui/attrdlg/scdlgfact.cxx
index 050b6d957737..7fe3435d1bce 100644
--- a/sc/source/ui/attrdlg/scdlgfact.cxx
+++ b/sc/source/ui/attrdlg/scdlgfact.cxx
@@ -214,6 +214,11 @@ short AbstractScMoveTableDlg_Impl::Execute()
     return m_xDlg->run();
 }
 
+bool 
AbstractScMoveTableDlg_Impl::StartExecuteAsync(VclAbstractDialog::AsyncContext& 
rCtx)
+{
+    return weld::DialogController::runAsync(m_xDlg, rCtx.maEndDialogFn);
+}
+
 BitmapEx AbstractScMoveTableDlg_Impl::createScreenshot() const
 {
     VclPtr<VirtualDevice> xDialogSurface(m_xDlg->getDialog()->screenshot());
@@ -1166,7 +1171,7 @@ VclPtr<AbstractScMetricInputDlg> 
ScAbstractDialogFactory_Impl::CreateScMetricInp
 VclPtr<AbstractScMoveTableDlg> 
ScAbstractDialogFactory_Impl::CreateScMoveTableDlg(weld::Window* pParent,
     const OUString& rDefault)
 {
-    return 
VclPtr<AbstractScMoveTableDlg_Impl>::Create(std::make_unique<ScMoveTableDlg>(pParent,
 rDefault));
+    return 
VclPtr<AbstractScMoveTableDlg_Impl>::Create(std::make_shared<ScMoveTableDlg>(pParent,
 rDefault));
 }
 
 VclPtr<AbstractScNameCreateDlg> 
ScAbstractDialogFactory_Impl::CreateScNameCreateDlg(weld::Window * pParent, 
CreateNameFlags nFlags)
diff --git a/sc/source/ui/attrdlg/scdlgfact.hxx 
b/sc/source/ui/attrdlg/scdlgfact.hxx
index 5de1ca37834e..880c1f153652 100644
--- a/sc/source/ui/attrdlg/scdlgfact.hxx
+++ b/sc/source/ui/attrdlg/scdlgfact.hxx
@@ -361,13 +361,14 @@ public:
 
 class AbstractScMoveTableDlg_Impl : public AbstractScMoveTableDlg
 {
-    std::unique_ptr<ScMoveTableDlg> m_xDlg;
+    std::shared_ptr<ScMoveTableDlg> m_xDlg;
 public:
-    explicit AbstractScMoveTableDlg_Impl(std::unique_ptr<ScMoveTableDlg> p)
+    explicit AbstractScMoveTableDlg_Impl(std::shared_ptr<ScMoveTableDlg> p)
         : m_xDlg(std::move(p))
     {
     }
     virtual short Execute() override;
+    virtual bool StartExecuteAsync(VclAbstractDialog::AsyncContext& rCtx) 
override;
     virtual sal_uInt16  GetSelectedDocument     () const override;
     virtual sal_uInt16  GetSelectedTable        () const override;
     virtual bool    GetCopyTable            () const override;
diff --git a/sc/source/ui/inc/viewfunc.hxx b/sc/source/ui/inc/viewfunc.hxx
index 52e2aedd9d22..bf6a9a77269a 100644
--- a/sc/source/ui/inc/viewfunc.hxx
+++ b/sc/source/ui/inc/viewfunc.hxx
@@ -271,7 +271,8 @@ public:
     void            DeleteTables(SCTAB nTab, SCTAB nSheets);
 
     bool            RenameTable( const OUString& rName, SCTAB nTabNr );
-    void            MoveTable( sal_uInt16 nDestDocNo, SCTAB nDestTab, bool 
bCopy, const OUString* pNewTabName = nullptr );
+    void            MoveTable( sal_uInt16 nDestDocNo, SCTAB nDestTab, bool 
bCopy, const OUString* pNewTabName = nullptr,
+                               bool bContextMenu = false, SCTAB 
nContextMenuSourceTab = -1 );
     void            ImportTables( ScDocShell* pSrcShell,
                                     SCTAB nCount, const SCTAB* pSrcTabs,
                                     bool bLink,SCTAB nTab);
diff --git a/sc/source/ui/view/tabvwshf.cxx b/sc/source/ui/view/tabvwshf.cxx
index 2ac3b93760a5..b5355e2d1c28 100644
--- a/sc/source/ui/view/tabvwshf.cxx
+++ b/sc/source/ui/view/tabvwshf.cxx
@@ -463,10 +463,14 @@ void ScTabViewShell::ExecuteTable( SfxRequest& rReq )
                 bool   bDoIt = false;
                 sal_uInt16 nDoc = 0;
                 SCTAB nTab = rViewData.GetTabNo();
+                SCTAB nContextMenuTab = -1;
+                bool bFromContextMenu = false;
+                bool bFromMoveOrCopySheetDialog = false; // FN_PARAM_6
                 bool bCpy = false, bUseCurrentDocument = false;
                 OUString aDocName;
                 OUString aTabName;
 
+                // if FID_TAB_MOVE has parameters
                 if( pReqArgs != nullptr )
                 {
                     SCTAB nTableCount = rDoc.GetTableCount();
@@ -483,6 +487,8 @@ void ScTabViewShell::ExecuteTable( SfxRequest& rReq )
 
                     if( pReqArgs->HasItem( FN_PARAM_1, &pItem ) )
                     {
+                        // nTab is the target tab.
+                        // source tab is either the active tab or the tab that 
context menu opened on.
                         //  table is 1-based
                         nTab = static_cast<const 
SfxUInt16Item*>(pItem)->GetValue() - 1;
                         if ( nTab >= nTableCount )
@@ -491,42 +497,142 @@ void ScTabViewShell::ExecuteTable( SfxRequest& rReq )
                     if( pReqArgs->HasItem( FN_PARAM_2, &pItem ) )
                         bCpy = static_cast<const 
SfxBoolItem*>(pItem)->GetValue();
 
-                    if (!aDocName.isEmpty())
+                    if (pReqArgs->HasItem(FN_PARAM_4, &pItem))
                     {
-                        SfxObjectShell* pSh     = SfxObjectShell::GetFirst();
-                        ScDocShell*     pScSh   = nullptr;
-                        sal_uInt16          i=0;
+                        bFromContextMenu = static_cast<const 
SfxBoolItem*>(pItem)->GetValue();
 
-                        while ( pSh )
+                        if (bFromContextMenu)
                         {
-                            pScSh = dynamic_cast<ScDocShell*>( pSh  );
+                            // source tab: the tab that context menu opened on
+                            if (pReqArgs->HasItem(FN_PARAM_5, &pItem))
+                                nContextMenuTab
+                                    = static_cast<const 
SfxUInt16Item*>(pItem)->GetValue();
+
+                            if (pReqArgs->HasItem(FN_PARAM_6, &pItem))
+                                bFromMoveOrCopySheetDialog
+                                    = static_cast<const 
SfxBoolItem*>(pItem)->GetValue();
+                        }
+                    }
 
-                            if( pScSh )
-                            {
-                                pScSh->GetTitle();
+                    if (bFromMoveOrCopySheetDialog)
+                    {
+                        OUString aDefaultName;
+                        rDoc.GetName(nContextMenuTab, aDefaultName);
+
+                        ScAbstractDialogFactory* pFact = 
ScAbstractDialogFactory::Create();
+
+                        VclPtr<AbstractScMoveTableDlg> pDlg(
+                            pFact->CreateScMoveTableDlg(GetFrameWeld(), 
aDefaultName));
+
+                        ScMarkData& rMark = GetViewData().GetMarkData();
+                        SCTAB nTabSelCount = rMark.GetSelectCount();
+
+                        if (nTableCount == nTabSelCount)
+                        {
+                            pDlg->SetForceCopyTable();
+                        }
+
+                        // We support direct renaming of sheet only when one 
sheet
+                        // is selected.
+                        pDlg->EnableRenameTable(nTabSelCount == 1);
 
-                                if (aDocName == pScSh->GetTitle())
+                        std::shared_ptr<SfxRequest> pReq = 
std::make_shared<SfxRequest>(rReq);
+                        pDlg->StartExecuteAsync([this, pDlg, pReq,
+                                                 nContextMenuTab](sal_Int32 
nResult) {
+
+                            OUString aTableName;
+                            sal_uInt16 nDocument = 0;
+                            SCTAB nTargetIndex = -1;
+                            bool bCopy = false;
+                            bool bDoItAsync = false;
+
+                            if (RET_OK == nResult)
+                            {
+                                nDocument = pDlg->GetSelectedDocument();
+                                nTargetIndex = pDlg->GetSelectedTable();
+                                bCopy = pDlg->GetCopyTable();
+                                bool bRna = pDlg->GetRenameTable();
+                                // Leave aTabName string empty, when Rename is 
FALSE.
+                                if (bRna)
+                                    pDlg->GetTabNameString(aTableName);
+
+                                bDoItAsync = true;
+
+                                OUString aFoundDocName;
+                                if (nDocument != SC_DOC_NEW)
                                 {
-                                    nDoc = i;
-                                    ScDocument& rDestDoc = 
pScSh->GetDocument();
-                                    nTableCount = rDestDoc.GetTableCount();
-                                    bDoIt = rDestDoc.IsDocEditable();
-                                    break;
+                                    ScDocShell* pSh = 
ScDocShell::GetShellByNum(nDocument);
+                                    if (pSh)
+                                    {
+                                        aFoundDocName = pSh->GetTitle();
+                                        if 
(!pSh->GetDocument().IsDocEditable())
+                                        {
+                                            ErrorMessage(STR_READONLYERR);
+                                            bDoItAsync = false;
+                                        }
+                                    }
                                 }
+                                pReq->AppendItem(SfxStringItem(FID_TAB_MOVE, 
aFoundDocName));
+                                // 1-based table, if not APPEND
+                                SCTAB nBasicTab = (nContextMenuTab <= MAXTAB)
+                                                      ? (nContextMenuTab + 1)
+                                                      : nContextMenuTab;
+                                pReq->AppendItem(
+                                    SfxUInt16Item(FN_PARAM_1, 
static_cast<sal_uInt16>(nBasicTab)));
+                                pReq->AppendItem(SfxBoolItem(FN_PARAM_2, 
bCopy));
+
+                                if (bDoItAsync)
+                                {
+                                    pReq->Done();
 
-                                i++;        // only count ScDocShell
+                                    // send move or copy request
+                                    MoveTable(nDocument, nTargetIndex, bCopy, 
&aTableName, true,
+                                              nContextMenuTab);
+                                }
                             }
-                            pSh = SfxObjectShell::GetNext( *pSh );
-                        }
+                            pDlg->disposeOnce();
+                        });
+                        rReq.Ignore();
                     }
-                    else // no doc-name -> new doc
+                    else
                     {
-                        nDoc = SC_DOC_NEW;
-                        bDoIt = true;
-                    }
+                        if (!aDocName.isEmpty())
+                        {
+                            SfxObjectShell* pSh     = 
SfxObjectShell::GetFirst();
+                            ScDocShell*     pScSh   = nullptr;
+                            sal_uInt16          i=0;
+
+                            while ( pSh )
+                            {
+                                pScSh = dynamic_cast<ScDocShell*>( pSh  );
+
+                                if( pScSh )
+                                {
+                                    pScSh->GetTitle();
+
+                                    if (aDocName == pScSh->GetTitle())
+                                    {
+                                        nDoc = i;
+                                        ScDocument& rDestDoc = 
pScSh->GetDocument();
+                                        nTableCount = rDestDoc.GetTableCount();
+                                        bDoIt = rDestDoc.IsDocEditable();
+                                        break;
+                                    }
 
-                    if ( bDoIt && nTab >= nTableCount )     // if necessary 
append
-                        nTab = SC_TAB_APPEND;
+                                    i++;        // only count ScDocShell
+                                }
+                                pSh = SfxObjectShell::GetNext( *pSh );
+                            }
+                        }
+                        else // no doc-name -> new doc
+                        {
+                            nDoc = SC_DOC_NEW;
+                            bDoIt = true;
+                        }
+
+                        if ( bDoIt && nTab >= nTableCount )     // if 
necessary append
+                            nTab = SC_TAB_APPEND;
+                    }
                 }
                 else
                 {
@@ -590,7 +696,11 @@ void ScTabViewShell::ExecuteTable( SfxRequest& rReq )
                 {
                     rReq.Done();        // record, while doc is active
 
-                    MoveTable( nDoc, nTab, bCpy, &aTabName );
+                    if (bFromContextMenu)
+                        MoveTable(nDoc, nTab, bCpy, &aTabName, true,
+                                  nContextMenuTab);
+                    else
+                        MoveTable( nDoc, nTab, bCpy, &aTabName );
                 }
             }
             break;
diff --git a/sc/source/ui/view/viewfun2.cxx b/sc/source/ui/view/viewfun2.cxx
index 224bb722e0dd..50db721190c9 100644
--- a/sc/source/ui/view/viewfun2.cxx
+++ b/sc/source/ui/view/viewfun2.cxx
@@ -2862,8 +2862,9 @@ void ScViewFunc::ImportTables( ScDocShell* pSrcShell,
 
 //  Move/Copy table to another document
 
-void ScViewFunc::MoveTable(
-    sal_uInt16 nDestDocNo, SCTAB nDestTab, bool bCopy, const OUString* 
pNewTabName )
+void ScViewFunc::MoveTable(sal_uInt16 nDestDocNo, SCTAB nDestTab, bool bCopy,
+                           const OUString* pNewTabName, bool bContextMenu,
+                           SCTAB nContextMenuSourceTab)
 {
     ScDocument& rDoc       = GetViewData().GetDocument();
     ScDocShell* pDocShell  = GetViewData().GetDocShell();
@@ -3092,23 +3093,32 @@ void ScViewFunc::MoveTable(
         pTabNames->reserve(nTabCount);
         OUString aDestName;
 
-        for(SCTAB i=0;i<nTabCount;i++)
+        if (bContextMenu)
         {
-            if(rMark.GetTableSelect(i))
+            OUString aTabName;
+            rDoc.GetName(nContextMenuSourceTab, aTabName);
+            pTabNames->push_back(aTabName);
+        }
+        else
+        {
+            for(SCTAB i=0;i<nTabCount;i++)
             {
-                OUString aTabName;
-                rDoc.GetName( i, aTabName);
-                pTabNames->push_back(aTabName);
-
-                for(SCTAB j=i+1;j<nTabCount;j++)
+                if(rMark.GetTableSelect(i))
                 {
-                    if((!rDoc.IsVisible(j)) && rDoc.IsScenario(j))
+                    OUString aTabName;
+                    rDoc.GetName( i, aTabName);
+                    pTabNames->push_back(aTabName);
+
+                    for(SCTAB j=i+1;j<nTabCount;j++)
                     {
-                        rDoc.GetName( j, aTabName);
-                        pTabNames->push_back(aTabName);
-                        i=j;
+                        if((!rDoc.IsVisible(j)) && rDoc.IsScenario(j))
+                        {
+                            rDoc.GetName( j, aTabName);
+                            pTabNames->push_back(aTabName);
+                            i=j;
+                        }
+                        else break;
                     }
-                    else break;
                 }
             }
         }
@@ -3208,13 +3218,24 @@ void ScViewFunc::MoveTable(
             }
         }
 
-        SCTAB nNewTab = nDestTab;
-        if (nNewTab == SC_TAB_APPEND)
-            nNewTab = rDoc.GetTableCount()-1;
-        else if (!bCopy && nTab<nDestTab)
-            nNewTab--;
+        if (bContextMenu)
+        {
+            for (SCTAB i = 0; i < nTabCount; i++)
+            {
+                if (rMark.GetTableSelect(i))
+                    SetTabNo(i, true);
+            }
+        }
+        else
+        {
+            SCTAB nNewTab = nDestTab;
+            if (nNewTab == SC_TAB_APPEND)
+                nNewTab = rDoc.GetTableCount() - 1;
+            else if (!bCopy && nTab < nDestTab)
+                nNewTab--;
 
-        SetTabNo( nNewTab, true );
+            SetTabNo(nNewTab, true);
+        }
     }
 }
 

Reply via email to