lok: async calc merge-cells popup dialog.

Change-Id: I6f7d1e8de4ac36f546706f7702157cc7e49c80b2
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/159581
Tested-by: Jenkins
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
diff --git a/sc/source/ui/inc/viewfunc.hxx b/sc/source/ui/inc/viewfunc.hxx
index 45b053f..52e2aed 100644
--- a/sc/source/ui/inc/viewfunc.hxx
+++ b/sc/source/ui/inc/viewfunc.hxx
@@ -236,7 +236,8 @@ public:
    bool            TestMergeCells();
    bool            TestRemoveMerge();

    bool            MergeCells( bool bApi, bool& rDoContents, bool bCenter );
    void            MergeCells( bool bApi, bool bDoContents, bool bCenter,
                                const sal_uInt16 nSlot );
    bool            RemoveMerge();

    SC_DLLPUBLIC void
diff --git a/sc/source/ui/undo/undoblk3.cxx b/sc/source/ui/undo/undoblk3.cxx
index b7b615d..bc967d9 100644
--- a/sc/source/ui/undo/undoblk3.cxx
+++ b/sc/source/ui/undo/undoblk3.cxx
@@ -755,8 +755,7 @@ void ScUndoMerge::Repeat(SfxRepeatTarget& rTarget)
    if (auto pViewTarget = dynamic_cast<ScTabViewTarget*>( &rTarget))
    {
        ScTabViewShell& rViewShell = *pViewTarget->GetViewShell();
        bool bCont = false;
        rViewShell.MergeCells( false, bCont, false );
        rViewShell.MergeCells( false, false, false, 0 );
    }
}

diff --git a/sc/source/ui/view/cellsh3.cxx b/sc/source/ui/view/cellsh3.cxx
index e052952..e6c89b6 100644
--- a/sc/source/ui/view/cellsh3.cxx
+++ b/sc/source/ui/view/cellsh3.cxx
@@ -943,13 +943,7 @@ void ScCellShell::Execute( SfxRequest& rReq )
                        bMoveContents = static_cast<const SfxBoolItem*>(pItem)->GetValue();
                    }

                    if (pTabViewShell->MergeCells( bApi, bMoveContents, bCenter ))
                    {
                        if (!bApi && bMoveContents)             // "yes" clicked in dialog
                            rReq.AppendItem( SfxBoolItem( nSlot, bMoveContents ) );
                        rBindings.Invalidate( nSlot );
                        rReq.Done();
                    }
                    pTabViewShell->MergeCells( bApi, bMoveContents, bCenter, nSlot );
                }
                else
                {
diff --git a/sc/source/ui/view/viewfun2.cxx b/sc/source/ui/view/viewfun2.cxx
index c2a7bfa..42c3ba2 100644
--- a/sc/source/ui/view/viewfun2.cxx
+++ b/sc/source/ui/view/viewfun2.cxx
@@ -20,6 +20,7 @@
#include <scitems.hxx>

#include <sfx2/app.hxx>
#include <sfx2/request.hxx>
#include <editeng/borderline.hxx>
#include <editeng/boxitem.hxx>
#include <editeng/fontitem.hxx>
@@ -1179,7 +1180,8 @@ bool ScViewFunc::TestMergeCells()           // pre-test (for menu)
        return false;
}

bool ScViewFunc::MergeCells( bool bApi, bool& rDoContents, bool bCenter )
void ScViewFunc::MergeCells( bool bApi, bool bDoContents, bool bCenter,
                             const sal_uInt16 nSlot )
{
    //  Editable- and Being-Nested- test must be at the beginning (in DocFunc too),
    //  so that the Contents-QueryBox won't appear
@@ -1187,7 +1189,7 @@ bool ScViewFunc::MergeCells( bool bApi, bool& rDoContents, bool bCenter )
    if (!aTester.IsEditable())
    {
        ErrorMessage(aTester.GetMessageId());
        return false;
        return;
    }

    ScMarkData& rMark = GetViewData().GetMarkData();
@@ -1195,7 +1197,7 @@ bool ScViewFunc::MergeCells( bool bApi, bool& rDoContents, bool bCenter )
    if (!rMark.IsMarked())
    {
        ErrorMessage(STR_NOMULTISELECT);
        return false;
        return;
    }

    ScDocShell* pDocSh = GetViewData().GetDocShell();
@@ -1211,14 +1213,14 @@ bool ScViewFunc::MergeCells( bool bApi, bool& rDoContents, bool bCenter )
    if ( nStartCol == nEndCol && nStartRow == nEndRow )
    {
        // nothing to do
        return true;
        return;
    }

    if ( rDoc.HasAttrib( nStartCol, nStartRow, nStartTab, nEndCol, nEndRow, nEndTab,
                            HasAttrFlags::Merged | HasAttrFlags::Overlapped ) )
    {       // "Don't nest merging  !"
        ErrorMessage(STR_MSSG_MERGECELLS_0);
        return false;
        return;
    }

    // Check for the contents of all selected tables.
@@ -1241,7 +1243,7 @@ bool ScViewFunc::MergeCells( bool bApi, bool& rDoContents, bool bCenter )
            {
                // this range contains only one data cell.
                if (nStartCol != aState.mnCol1 || nStartRow != aState.mnRow1)
                    rDoContents = true; // move the value to the top-left.
                    bDoContents = true; // move the value to the top-left.
                break;
            }
            default:
@@ -1249,48 +1251,16 @@ bool ScViewFunc::MergeCells( bool bApi, bool& rDoContents, bool bCenter )
        }
    }

    bool bOk = true;
    bool bEmptyMergedCells = officecfg::Office::Calc::Compatibility::MergeCells::EmptyMergedCells::get();

    if (bAskDialog)
    auto doMerge = [this, pDocSh, aMergeOption, bApi, nStartCol, nStartRow, aMarkRange]
        (bool bNowDoContents, bool bNowEmptyMergedCells)
    {
        bool bShowDialog = officecfg::Office::Calc::Compatibility::MergeCells::ShowDialog::get();
        if (!bApi && bShowDialog)
        {
            ScMergeCellsDialog aBox(GetViewData().GetDialogParent());
            sal_uInt16 nRetVal = aBox.run();

            if ( nRetVal == RET_OK )
            {
                switch (aBox.GetMergeCellsOption())
                {
                    case MoveContentHiddenCells:
                        rDoContents = true;
                        break;
                    case KeepContentHiddenCells:
                        bEmptyMergedCells = false;
                        break;
                    case EmptyContentHiddenCells:
                        bEmptyMergedCells = true;
                        break;
                    default:
                        assert(!"Unknown option for merge cells.");
                        break;
                }
            }
            else if ( nRetVal == RET_CANCEL )
                bOk = false;
        }
    }

    if (bOk)
    {
        bOk = pDocSh->GetDocFunc().MergeCells( aMergeOption, rDoContents, true/*bRecord*/, bApi, bEmptyMergedCells );

        if (bOk)
        if (pDocSh->GetDocFunc().MergeCells(aMergeOption, bNowDoContents, true/*bRecord*/,
                                             bApi, bNowEmptyMergedCells))
        {
            SetCursor( nStartCol, nStartRow );
            //DoneBlockMode( sal_False);
            // DoneBlockMode( sal_False);
            Unmark();

            pDocSh->UpdateOle(GetViewData());
@@ -1301,9 +1271,55 @@ bool ScViewFunc::MergeCells( bool bApi, bool& rDoContents, bool bCenter )

            collectUIInformation({{"RANGE", aStartAddress + ":" + aEndAddress}}, "MERGE_CELLS");
        }
    }
    };

    return bOk;
    if (bAskDialog)
    {
        bool bShowDialog = officecfg::Office::Calc::Compatibility::MergeCells::ShowDialog::get();
        if (!bApi && bShowDialog)
        {
            auto pBox = std::make_shared<ScMergeCellsDialog>(GetViewData().GetDialogParent());

            SfxViewShell* pViewShell = GetViewData().GetViewShell();

            weld::DialogController::runAsync(pBox, [=](sal_Int32 nRetVal) {
                if (nRetVal == RET_OK)
                {
                    bool bRealDoContents = bDoContents;
                    bool bRealEmptyMergedCells = bEmptyMergedCells;
                    switch (pBox->GetMergeCellsOption())
                    {
                    case MoveContentHiddenCells:
                        bRealDoContents = true;
                        break;
                    case KeepContentHiddenCells:
                        bRealEmptyMergedCells = false;
                        break;
                    case EmptyContentHiddenCells:
                        bRealEmptyMergedCells = true;
                        break;
                    default:
                        assert(!"Unknown option for merge cells.");
                        break;
                    }

                    doMerge(bRealDoContents, bRealEmptyMergedCells);

                    if (nSlot != 0)
                    {
                        SfxRequest aReq(pViewShell->GetViewFrame(), nSlot);
                        if (!bApi && bRealDoContents)
                            aReq.AppendItem(SfxBoolItem(nSlot, bDoContents));
                        SfxBindings& rBindings = pViewShell->GetViewFrame().GetBindings();
                        rBindings.Invalidate(nSlot);
                        aReq.Done();
                    }
                }
                // else cancelled
            });
        }
    } else
        doMerge(bDoContents, bEmptyMergedCells);
}

bool ScViewFunc::TestRemoveMerge()