lokdialog: Convert the Format -> ... -> Position and Size... to async exec.

Change-Id: Idcdbfb1366db61e247c31eab5cb27a39978b0fd9
Reviewed-on: https://gerrit.libreoffice.org/48055
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Jan Holesovsky <kendy@collabora.com>
diff --git a/cui/source/factory/dlgfact.hxx b/cui/source/factory/dlgfact.hxx
index c3240f9..eef4029ff 100644
--- a/cui/source/factory/dlgfact.hxx
+++ b/cui/source/factory/dlgfact.hxx
@@ -67,12 +67,17 @@ public:                                             \
    explicit        Class( DialogClass* p)          \
                     : pDlg(p)                      \
                     {}                             \
    virtual short   Execute() override ;
    virtual short   Execute() override;             \
    virtual bool    StartExecuteAsync(VclAbstractDialog::AsyncContext &rCtx) override;

#define IMPL_ABSTDLG_BASE(Class)                    \
short Class::Execute()                              \
{                                                   \
    return pDlg->Execute();                         \
}                                                   \
bool Class::StartExecuteAsync(VclAbstractDialog::AsyncContext &rCtx) \
{                                                   \
    return pDlg->StartExecuteAsync(rCtx);           \
}

class VclAbstractDialog2_Impl : public VclAbstractDialog2
diff --git a/sd/source/ui/func/futransf.cxx b/sd/source/ui/func/futransf.cxx
index 4a811aa..0d0a778 100644
--- a/sd/source/ui/func/futransf.cxx
+++ b/sd/source/ui/func/futransf.cxx
@@ -32,8 +32,7 @@

#include <memory>

namespace sd {

using namespace sd;

FuTransform::FuTransform(ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView,
                         SdDrawDocument* pDoc, SfxRequest& rReq)
@@ -48,74 +47,112 @@ rtl::Reference<FuPoor> FuTransform::Create( ViewShell* pViewSh, ::sd::Window* pW
    return xFunc;
}

void FuTransform::DoExecute( SfxRequest& rReq )
namespace {

void setUndo(::sd::View* pView, const SfxItemSet* pArgs)
{
    if( mpView->AreObjectsMarked() )
    {
        const SfxItemSet* pArgs = rReq.GetArgs();
    // Undo
    OUString aString(pView->GetDescriptionOfMarkedObjects());
    aString += " " + SdResId(STR_TRANSFORM);
    pView->BegUndo(aString);

        if( !pArgs )
        {
            // --------- itemset for size and position --------
            SfxItemSet aSet( mpView->GetGeoAttrFromMarked() );

            const SdrMarkList& rMarkList = mpView->GetMarkedObjectList();
            SdrObject* pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
            if( rMarkList.GetMarkCount() == 1 &&
                pObj->GetObjInventor() == SdrInventor::Default &&
                pObj->GetObjIdentifier() == OBJ_CAPTION )
            {
                // --------- itemset for caption --------
                SfxItemSet aNewAttr( mpDoc->GetPool() );
                mpView->GetAttributes( aNewAttr );

                SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
                if ( pFact )
                {
                    ScopedVclPtr< SfxAbstractTabDialog > pDlg( pFact->CreateCaptionDialog( nullptr, mpView ) );

                    const sal_uInt16* pRange = pDlg->GetInputRanges( *aNewAttr.GetPool() );
                    SfxItemSet aCombSet( *aNewAttr.GetPool(), pRange );
                    aCombSet.Put( aNewAttr );
                    aCombSet.Put( aSet );
                    pDlg->SetInputSet( &aCombSet );

                    if( pDlg.get() && (pDlg->Execute() == RET_OK) )
                    {
                        rReq.Done( *( pDlg->GetOutputItemSet() ) );
                        pArgs = rReq.GetArgs();
                    }
                }
            }
            else
            {
                SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
                if(pFact)
                {
                    ScopedVclPtr< SfxAbstractTabDialog > pDlg( pFact->CreateSvxTransformTabDialog( nullptr, &aSet, mpView ) );
                    if( pDlg.get() && (pDlg->Execute() == RET_OK) )
                    {
                        rReq.Done( *( pDlg->GetOutputItemSet() ) );
                        pArgs = rReq.GetArgs();
                    }
                }
            }
        }

        if( pArgs )
        {
            // Undo
            OUString aString( mpView->GetDescriptionOfMarkedObjects() );
            aString += " " + SdResId( STR_TRANSFORM );
            mpView->BegUndo( aString );

            mpView->SetGeoAttrToMarked( *pArgs );
            mpView->SetAttributes( *pArgs );
            mpView->EndUndo();
        }
    }
    pView->SetGeoAttrToMarked(*pArgs);
    pView->SetAttributes(*pArgs);
    pView->EndUndo();
}

} // end of namespace sd
class ScopeCleanup
{
    ViewShell* mpViewShell;
public:
    ScopeCleanup(ViewShell* pViewShell) : mpViewShell(pViewShell)
    {
    }

    ~ScopeCleanup()
    {
        if (mpViewShell)
        {
            mpViewShell->Invalidate(SID_RULER_OBJECT);
            mpViewShell->Cancel();
        }
    }

    void ignore()
    {
        mpViewShell = nullptr;
    }
};

}

void FuTransform::DoExecute( SfxRequest& rReq )
{
    ScopeCleanup aCleanup(mpViewShell);

    if (!mpView->AreObjectsMarked())
        return;

    const SfxItemSet* pArgs = rReq.GetArgs();

    if (pArgs)
    {
        setUndo(mpView, pArgs);
        return;
    }

    // --------- itemset for size and position --------
    SfxItemSet aSet( mpView->GetGeoAttrFromMarked() );
    VclPtr<SfxAbstractTabDialog> pDlg;

    const SdrMarkList& rMarkList = mpView->GetMarkedObjectList();
    SdrObject* pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
    if( rMarkList.GetMarkCount() == 1 &&
        pObj->GetObjInventor() == SdrInventor::Default &&
        pObj->GetObjIdentifier() == OBJ_CAPTION )
    {
        // --------- itemset for caption --------
        SfxItemSet aNewAttr( mpDoc->GetPool() );
        mpView->GetAttributes( aNewAttr );

        SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
        if (!pFact)
            return;

        pDlg.reset(pFact->CreateCaptionDialog(nullptr, mpView));

        const sal_uInt16* pRange = pDlg->GetInputRanges( *aNewAttr.GetPool() );
        SfxItemSet aCombSet( *aNewAttr.GetPool(), pRange );
        aCombSet.Put( aNewAttr );
        aCombSet.Put( aSet );
        pDlg->SetInputSet( &aCombSet );
    }
    else
    {
        SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
        if (!pFact)
            return;

        pDlg.reset(pFact->CreateSvxTransformTabDialog(nullptr, &aSet, mpView));
    }

    if (!pDlg)
        return;

    std::shared_ptr<SfxRequest> pRequest(new SfxRequest(rReq));
    rReq.Ignore(); // the 'old' request is not relevant any more
    aCleanup.ignore(); // the lambda does it

    pDlg->StartExecuteAsync([=](sal_Int32 nResult){
        if (nResult == RET_OK)
        {
            pRequest->Done(*(pDlg->GetOutputItemSet()));
            setUndo(mpView, pRequest->GetArgs());
        }

        mpViewShell->Invalidate(SID_RULER_OBJECT);
        mpViewShell->Cancel();
    }, pDlg);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sd/source/ui/view/drviews2.cxx b/sd/source/ui/view/drviews2.cxx
index 74e1621..547348e 100644
--- a/sd/source/ui/view/drviews2.cxx
+++ b/sd/source/ui/view/drviews2.cxx
@@ -1381,8 +1381,7 @@ void DrawViewShell::FuTemporary(SfxRequest& rReq)
        case SID_ATTR_TRANSFORM:
        {
            SetCurrentFunction( FuTransform::Create( this, GetActiveWindow(), mpDrawView, GetDoc(), rReq ) );
            Invalidate(SID_RULER_OBJECT);
            Cancel();
            // Cancel() and Invalidate() called directly in FuTransform::Create()
        }
        break;