lokdialog: Convert the Format -> Page... dialog to async exec.

This actually also converts the Style -> Edit Style... at the same time.

Change-Id: I4c09fcdfd7a543cce613eaa3620d0b623540d7e8
Reviewed-on: https://gerrit.libreoffice.org/48043
Tested-by: Jan Holesovsky <kendy@collabora.com>
Reviewed-by: Jan Holesovsky <kendy@collabora.com>
diff --git a/sw/inc/docsh.hxx b/sw/inc/docsh.hxx
index d38b9b7..e2c754f 100644
--- a/sw/inc/docsh.hxx
+++ b/sw/inc/docsh.hxx
@@ -127,7 +127,8 @@ class SW_DLLPUBLIC SwDocShell
        const bool bNew,
        const OString& sPageId,
        SwWrtShell* pActShell,
        SfxRequest* pRequest = nullptr);
        SfxRequest* pRequest = nullptr,
        sal_uInt16 nSlot = 0);

    SAL_DLLPRIVATE void                  Delete(const OUString &rName, SfxStyleFamily nFamily);
    SAL_DLLPRIVATE void                  Hide(const OUString &rName, SfxStyleFamily nFamily, bool bHidden);
diff --git a/sw/source/uibase/app/docst.cxx b/sw/source/uibase/app/docst.cxx
index bfbe643..4361dd1 100644
--- a/sw/source/uibase/app/docst.cxx
+++ b/sw/source/uibase/app/docst.cxx
@@ -322,15 +322,7 @@ void SwDocShell::ExecStyleSheet( SfxRequest& rReq )
            if (sName.isEmpty() && m_xBasePool.get())
                sName = SfxStyleDialog::GenerateUnusedName(*m_xBasePool);

            Edit(sName, sParent, nFamily, nMask, true, OString(), nullptr, &rReq);

            // Update Watermark if new page style was created
            if( nFamily == SfxStyleFamily::Page )
            {
                SwWrtShell* pShell = GetWrtShell();
                const SfxWatermarkItem aWatermark = pShell->GetWatermark();
                pShell->SetWatermark( aWatermark );
            }
            Edit(sName, sParent, nFamily, nMask, true, OString(), nullptr, &rReq, nSlot);
        }
        break;

@@ -657,10 +649,11 @@ void SwDocShell::Edit(
    const bool bNew,
    const OString& sPage,
    SwWrtShell* pActShell,
    SfxRequest* pRequest)
    SfxRequest* pReq,
    sal_uInt16 nSlot)
{
    assert( GetWrtShell() );
    const bool bBasic = pRequest && pRequest->IsAPI();
    const bool bBasic = pReq && pReq->IsAPI();
    SfxStyleSheetBase *pStyle = nullptr;

    bool bModified = m_xDoc->getIDocumentState().IsModified();
@@ -830,36 +823,52 @@ void SwDocShell::Edit(
                                                    *(xTmp.get()), nFamily, sPage,
                                                    pActShell ? pActShell : m_pWrtShell, bNew));
        assert( pDlg );
        ApplyStyle aApplyStyleHelper(*this, bNew, xTmp, nFamily, pDlg.get(), m_xBasePool, bModified);
        pDlg->SetApplyHdl(LINK(&aApplyStyleHelper, ApplyStyle, ApplyHdl));
        std::shared_ptr<ApplyStyle> pApplyStyleHelper(new ApplyStyle(*this, bNew, xTmp, nFamily, pDlg.get(), m_xBasePool, bModified));
        pDlg->SetApplyHdl(LINK(pApplyStyleHelper.get(), ApplyStyle, ApplyHdl));

        short nDlgRet = pDlg->Execute();

        if (RET_OK == nDlgRet)
        std::shared_ptr<SfxRequest> pRequest;
        if (pReq)
        {
            aApplyStyleHelper.apply();
            pRequest.reset(new SfxRequest(*pReq));
            pReq->Ignore(); // the 'old' request is not relevant any more
        }

        if (bNew)
        {
            SwRewriter aRewriter;
            aRewriter.AddRule(UndoArg1, xTmp->GetName());
            //Group the create style and change style operations together under the
            //one "create style" comment
            m_pWrtShell->EndUndo(nNewStyleUndoId, &aRewriter);
        }
        pDlg->StartExecuteAsync([=](sal_Int32 nResult){
            if (RET_OK == nResult)
                pApplyStyleHelper->apply();

        if (RET_OK != nDlgRet)
        {
            if( bNew )
            if (bNew)
            {
                GetWrtShell()->Undo();
                m_xDoc->GetIDocumentUndoRedo().ClearRedo();
                SwRewriter aRewriter;
                aRewriter.AddRule(UndoArg1, xTmp->GetName());
                //Group the create style and change style operations together under the
                //one "create style" comment
                m_pWrtShell->EndUndo(nNewStyleUndoId, &aRewriter);
            }

            if( !bModified )
                m_xDoc->getIDocumentState().ResetModified();
        }
            if (RET_OK != nResult)
            {
                if (bNew)
                {
                    GetWrtShell()->Undo();
                    m_xDoc->GetIDocumentUndoRedo().ClearRedo();
                }

                if (!bModified)
                    m_xDoc->getIDocumentState().ResetModified();
            }

            // Update Watermark if new page style was created
            if (nSlot == SID_STYLE_NEW && nFamily == SfxStyleFamily::Page)
            {
                SwWrtShell* pShell = GetWrtShell();
                const SfxWatermarkItem aWatermark = pShell->GetWatermark();
                pShell->SetWatermark(aWatermark);
            }

            if (pRequest)
                pRequest->Done();
        }, pDlg);
    }
    else
    {
diff --git a/sw/source/uibase/shells/basesh.cxx b/sw/source/uibase/shells/basesh.cxx
index 6906bb9..5948953 100644
--- a/sw/source/uibase/shells/basesh.cxx
+++ b/sw/source/uibase/shells/basesh.cxx
@@ -2451,6 +2451,8 @@ void SwBaseShell::ExecDlg(SfxRequest &rReq)

                rTempView.GetDocShell()->FormatPage(rPageDesc.GetName(), sPageId, rSh, &rReq);
                rTempView.InvalidateRulerPos();

                bDone = true; // FormatPage() takes care of calling Done()
            }
        }
        break;