tdf#146913 don't destroy the dialog if we just want to hide it

Change-Id: Ie534aba915120d12e65aa965e5435bc7575ecb29
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/128879
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
diff --git a/include/sfx2/basedlgs.hxx b/include/sfx2/basedlgs.hxx
index c198f12..e7cf250 100644
--- a/include/sfx2/basedlgs.hxx
+++ b/include/sfx2/basedlgs.hxx
@@ -48,7 +48,7 @@ public:
    // when the dialog has an associated SfxChildWin, typically for Modeless interaction
    virtual void ChildWinDispose() {} // called from the associated SfxChildWin dtor
    virtual void Close() {} // called by the SfxChildWin when the dialog is closed
    virtual void EndDialog(); // called by the SfxChildWin to close the dialog
    virtual void EndDialog(int nResponse); // called by the SfxChildWin to close the dialog
};

class SfxModelessDialog_Impl;
@@ -73,7 +73,7 @@ public:
    void                    Initialize (SfxChildWinInfo const * pInfo);
    bool                    IsClosing() const;
    virtual void            Close() override;
    virtual void            EndDialog() override;
    virtual void            EndDialog(int nRespose) override;
    virtual void            Activate() override;
    virtual void            Deactivate() override;
    virtual void            ChildWinDispose() override;
diff --git a/include/sfx2/childwin.hxx b/include/sfx2/childwin.hxx
index 66d125a..6a93b46 100644
--- a/include/sfx2/childwin.hxx
+++ b/include/sfx2/childwin.hxx
@@ -155,6 +155,7 @@ public:
    SAL_DLLPRIVATE void SetFactory_Impl( const SfxChildWinFactory* );
};

const int nCloseResponseToJustHide = -42;

#define SFX_DECL_CHILDWINDOW(Class) \
    public  :   \
diff --git a/sc/source/ui/dbgui/validate.cxx b/sc/source/ui/dbgui/validate.cxx
index 237ec6c..ac75d90 100644
--- a/sc/source/ui/dbgui/validate.cxx
+++ b/sc/source/ui/dbgui/validate.cxx
@@ -101,12 +101,12 @@ ScValidationDlg::ScValidationDlg(weld::Window* pParent, const SfxItemSet* pArgSe
    }
}

void ScValidationDlg::EndDialog()
void ScValidationDlg::EndDialog(int nResponse)
{
    // tdf#137215 ensure original modality of true is restored before dialog loop ends
    if (m_bOwnRefHdlr)
        RemoveRefDlg(true);
    ScValidationDlgBase::EndDialog();
    ScValidationDlgBase::EndDialog(nResponse);
}

ScValidationDlg::~ScValidationDlg()
diff --git a/sc/source/ui/inc/validate.hxx b/sc/source/ui/inc/validate.hxx
index 6117e87..618cfb2 100644
--- a/sc/source/ui/inc/validate.hxx
+++ b/sc/source/ui/inc/validate.hxx
@@ -176,7 +176,7 @@ public:

    void            SetModal(bool bModal) { m_xDialog->set_modal(bModal); }

    virtual void EndDialog() override;
    virtual void EndDialog(int nResponse) override;

    virtual void            SetReference( const ScRange& rRef, ScDocument& rDoc ) override
    {
diff --git a/sfx2/source/appl/childwin.cxx b/sfx2/source/appl/childwin.cxx
index 263f750..944fe47 100644
--- a/sfx2/source/appl/childwin.cxx
+++ b/sfx2/source/appl/childwin.cxx
@@ -511,7 +511,7 @@ void SfxChildWindow::SetVisible_Impl( bool bVis )
void SfxChildWindow::Hide()
{
    if (xController)
        xController->EndDialog();
        xController->EndDialog(nCloseResponseToJustHide);
    else
        pWindow->Hide();
}
@@ -523,7 +523,11 @@ void SfxChildWindow::Show( ShowFlags nFlags )
        if (!xController->getDialog()->get_visible())
        {
            weld::DialogController::runAsync(xController,
                [this](sal_Int32 /*nResult*/){ xController->Close(); });
                [this](sal_Int32 nResult) {
                    if (nResult == nCloseResponseToJustHide)
                        return;
                    xController->Close();
                });
        }
    }
    else
diff --git a/sfx2/source/appl/workwin.cxx b/sfx2/source/appl/workwin.cxx
index ef9f546..ca0e55d 100644
--- a/sfx2/source/appl/workwin.cxx
+++ b/sfx2/source/appl/workwin.cxx
@@ -999,7 +999,11 @@ void SfxWorkWindow::ShowChildren_Impl()
                    {
                        auto xController = pCli->xController;
                        weld::DialogController::runAsync(xController,
                            [=](sal_Int32 /*nResult*/){ xController->Close(); });
                            [=](sal_Int32 nResult){
                                if (nResult == nCloseResponseToJustHide)
                                    return;
                                xController->Close();
                            });
                    }
                }
                else
diff --git a/sfx2/source/dialog/basedlgs.cxx b/sfx2/source/dialog/basedlgs.cxx
index f28d31d..7319b99 100644
--- a/sfx2/source/dialog/basedlgs.cxx
+++ b/sfx2/source/dialog/basedlgs.cxx
@@ -142,11 +142,11 @@ SfxModelessDialogController::~SfxModelessDialogController()
        m_pBindings->SetActiveFrame(nullptr);
}

void SfxDialogController::EndDialog()
void SfxDialogController::EndDialog(int nResponse)
{
    if (!m_xDialog->get_visible())
        return;
    response(RET_CLOSE);
    response(nResponse);
}

bool SfxModelessDialogController::IsClosing() const
@@ -154,7 +154,7 @@ bool SfxModelessDialogController::IsClosing() const
    return m_xImpl->bClosing;
}

void SfxModelessDialogController::EndDialog()
void SfxModelessDialogController::EndDialog(int nResponse)
{
    if (m_xImpl->bClosing)
        return;
@@ -163,7 +163,7 @@ void SfxModelessDialogController::EndDialog()
    // stack frame.
    auto aHoldSelf = shared_from_this();
    m_xImpl->bClosing = true;
    SfxDialogController::EndDialog();
    SfxDialogController::EndDialog(nResponse);
    if (!m_xImpl)
        return;
    m_xImpl->bClosing = false;
diff --git a/sw/source/ui/fldui/fldtdlg.cxx b/sw/source/ui/fldui/fldtdlg.cxx
index 2075175..002f14c 100644
--- a/sw/source/ui/fldui/fldtdlg.cxx
+++ b/sw/source/ui/fldui/fldtdlg.cxx
@@ -102,10 +102,10 @@ SwFieldDlg::~SwFieldDlg()
{
}

void SwFieldDlg::EndDialog()
void SwFieldDlg::EndDialog(int nResponse)
{
    m_bClosing = true;
    SfxTabDialogController::EndDialog();
    SfxTabDialogController::EndDialog(nResponse);
    m_bClosing = false;
}

@@ -121,7 +121,7 @@ void SwFieldDlg::Close()
        // If Execute action did fail for whatever reason, this means that request
        // to close did fail or wasn't delivered to SwTextShell::ExecField().
        // Just explicitly close dialog in this case.
        SfxTabDialogController::EndDialog();
        SfxTabDialogController::EndDialog(RET_CLOSE);
    }
}

diff --git a/sw/source/uibase/inc/fldtdlg.hxx b/sw/source/uibase/inc/fldtdlg.hxx
index b2cd767..b7bda89 100644
--- a/sw/source/uibase/inc/fldtdlg.hxx
+++ b/sw/source/uibase/inc/fldtdlg.hxx
@@ -59,7 +59,7 @@ public:
    void                ActivateDatabasePage();
    void                ShowReferencePage();
    virtual void        Close() override;
    virtual void        EndDialog() override;
    virtual void        EndDialog(int nResponse) override;
    virtual void        Activate() override;
};