fdo#79011: Properly implement the search results dialog as modeless.

It's unfortunate that adding a modeless dialog is such a pain.  But we
still need to implemenet this properly else we'll leak at best, or end
up with tons of weird bugs at worst.

Change-Id: Ie03260f288fad76f994d0ca6a8b1feeade299ffd
diff --git a/sc/inc/sc.hrc b/sc/inc/sc.hrc
index 2253aac..5204efb 100644
--- a/sc/inc/sc.hrc
+++ b/sc/inc/sc.hrc
@@ -270,6 +270,7 @@
#define SID_MOVING_AVERAGE_DIALOG           (SC_MESSAGE_START + 77)
#define SID_TTEST_DIALOG                    (SC_MESSAGE_START + 78)
#define SID_FTEST_DIALOG                    (SC_MESSAGE_START + 79)
#define SID_SEARCH_RESULTS_DIALOG           (SC_MESSAGE_START + 80)

// functions

diff --git a/sc/sdi/cellsh.sdi b/sc/sdi/cellsh.sdi
index 62ce485..8447768 100644
--- a/sc/sdi/cellsh.sdi
+++ b/sc/sdi/cellsh.sdi
@@ -161,6 +161,7 @@ interface CellSelection
    SID_MOVING_AVERAGE_DIALOG           [ ExecMethod = ExecuteEdit; StateMethod = GetBlockState; ]
    SID_TTEST_DIALOG                    [ ExecMethod = ExecuteEdit; StateMethod = GetBlockState; ]
    SID_FTEST_DIALOG                    [ ExecMethod = ExecuteEdit; StateMethod = GetBlockState; ]
    SID_SEARCH_RESULTS_DIALOG [ ExecMethod = ExecuteEdit; StateMethod = GetBlockState; ]
    SID_MARKDATAAREA    [ ExecMethod = ExecuteMove; StateMethod = GetStateCursor; ]
    SID_MARKARRAYFORMULA [ ExecMethod = ExecuteMove; StateMethod = GetStateCursor; ]
    SID_SETINPUTMODE    [ ExecMethod = ExecuteMove; StateMethod = GetStateCursor; ]
diff --git a/sc/sdi/scalc.sdi b/sc/sdi/scalc.sdi
index fe3d780..1835459 100644
--- a/sc/sdi/scalc.sdi
+++ b/sc/sdi/scalc.sdi
@@ -3231,6 +3231,29 @@ SfxVoidItem SolverDialog SID_OPENDLG_OPTSOLVER
    GroupId = GID_OPTIONS;
]

SfxVoidItem SearchResultsDialog SID_SEARCH_RESULTS_DIALOG
()
[
    /* flags: */
    AutoUpdate = FALSE,
    Cachable = Cachable,
    FastCall = FALSE,
    HasCoreId = FALSE,
    HasDialog = TRUE,
    ReadOnlyDoc = TRUE,
    Toggle = FALSE,
    Container = FALSE,
    RecordAbsolute = FALSE,
    RecordPerSet;
    Synchron;

    /* config: */
    AccelConfig = TRUE,
    MenuConfig = TRUE,
    StatusBarConfig = FALSE,
    ToolBoxConfig = TRUE,
    GroupId = GID_OPTIONS;
]

SfxVoidItem ValidityReference SID_VALIDITY_REFERENCE
()
diff --git a/sc/source/ui/app/scdll.cxx b/sc/source/ui/app/scdll.cxx
index d4cf759..9f9f839 100644
--- a/sc/source/ui/app/scdll.cxx
+++ b/sc/source/ui/app/scdll.cxx
@@ -72,6 +72,7 @@

#include "docpool.hxx"
#include "appoptio.hxx"
#include <searchresults.hxx>

// Controls

@@ -285,6 +286,7 @@ void ScDLL::Init()
    ScSpellDialogChildWindow    ::RegisterChildWindow(false, pMod);

    ScValidityRefChildWin::RegisterChildWindow(false, pMod);
    sc::SearchResultsDlgWrapper::RegisterChildWindow(false, pMod);

    // EditEngine Field; insofar not already defined in OfficeApplication::Init
    SvClassManager& rClassManager = SvxFieldItem::GetClassManager();
diff --git a/sc/source/ui/dialogs/searchresults.cxx b/sc/source/ui/dialogs/searchresults.cxx
index 3b27a2a..51c5ece 100644
--- a/sc/source/ui/dialogs/searchresults.cxx
+++ b/sc/source/ui/dialogs/searchresults.cxx
@@ -11,14 +11,19 @@

#include <svtools/simptabl.hxx>
#include <svtools/treelistentry.hxx>
#include <sfx2/bindings.hxx>
#include <sfx2/dispatch.hxx>
#include "dociter.hxx"
#include "document.hxx"
#include "rangeutl.hxx"
#include "tabvwsh.hxx"
#include <sc.hrc>

SearchResults::SearchResults(ScDocument *pDoc) :
    ModelessDialog(NULL, "SearchResultsDialog", "modules/scalc/ui/searchresults.ui")
    , mpDoc(pDoc)
namespace sc {

SearchResultsDlg::SearchResultsDlg( SfxBindings* _pBindings, Window* pParent, sal_uInt16 nId ) :
    ModelessDialog(pParent, "SearchResultsDialog", "modules/scalc/ui/searchresults.ui"),
    mpBindings(_pBindings), mnId(nId), mpDoc(NULL)
{
    SvSimpleTableContainer *pContainer = get<SvSimpleTableContainer>("results");
    Size aControlSize(150, 120);
@@ -30,36 +35,56 @@ SearchResults::SearchResults(ScDocument *pDoc) :
    long nTabs[] = {3, 0, 40, 60};
    mpList->SetTabs(&nTabs[0]);
    mpList->InsertHeaderEntry("Sheet\tCell\tContent");
    mpList->SetSelectHdl( LINK(this, SearchResults, ListSelectHdl) );
    mpList->SetSelectHdl( LINK(this, SearchResultsDlg, ListSelectHdl) );
}

SearchResults::~SearchResults()
SearchResultsDlg::~SearchResultsDlg()
{
    delete mpList;
}

void SearchResults::Show(const ScRangeList &rMatchedRanges)
void SearchResultsDlg::FillResults( ScDocument* pDoc, const ScRangeList &rMatchedRanges )
{
    mpList->Clear();
    mpList->SetUpdateMode(false);
    for (size_t i = 0, n = rMatchedRanges.size(); i < n; ++i)
    {
        ScCellIterator aIter(mpDoc, *rMatchedRanges[i]);
        ScCellIterator aIter(pDoc, *rMatchedRanges[i]);
        for (bool bHas = aIter.first(); bHas; bHas = aIter.next())
        {
            ScAddress aAddress = aIter.GetPos();
            OUString sAddress;
            ScRangeStringConverter::GetStringFromAddress(sAddress, aAddress,
                    mpDoc, formula::FormulaGrammar::CONV_OOO);
            mpList->InsertEntry(sAddress.replace('.', '\t') + "\t" + mpDoc->GetString(aAddress));
                    pDoc, formula::FormulaGrammar::CONV_OOO);
            mpList->InsertEntry(sAddress.replace('.', '\t') + "\t" + pDoc->GetString(aAddress));
        }
    }
    mpList->SetUpdateMode(true);
    ModelessDialog::Show();

    mpDoc = pDoc;
}

IMPL_LINK_NOARG( SearchResults, ListSelectHdl )
bool SearchResultsDlg::Close()
{
    if (mpBindings)
    {
        // Remove this dialog from the view frame after the dialog gets
        // dismissed, else it would keep popping up endlessly!
        SfxDispatcher* pDispacher = mpBindings ->GetDispatcher();
        SfxBoolItem aItem(SID_SEARCH_RESULTS_DIALOG, false);
        if (pDispacher)
            pDispacher->Execute(
                SID_SEARCH_RESULTS_DIALOG, SFX_CALLMODE_ASYNCHRON | SFX_CALLMODE_RECORD, &aItem, 0L);
    }

    return ModelessDialog::Close();
}

IMPL_LINK_NOARG( SearchResultsDlg, ListSelectHdl )
{
    if (!mpDoc)
        return 0;

    SvTreeListEntry *pEntry = mpList->FirstSelected();
    ScAddress aAddress;
    sal_Int32 nOffset = 0;
@@ -70,7 +95,28 @@ IMPL_LINK_NOARG( SearchResults, ListSelectHdl )
    pScViewShell->SetTabNo(aAddress.Tab());
    pScViewShell->SetCursor(aAddress.Col(), aAddress.Row());
    pScViewShell->AlignToCursor(aAddress.Col(), aAddress.Row(), SC_FOLLOW_JUMP);

    return 0;
}

SearchResultsDlgWrapper::SearchResultsDlgWrapper(
    Window* _pParent, sal_uInt16 nId, SfxBindings* pBindings, SfxChildWinInfo* /*pInfo*/ ) :
    SfxChildWindow(_pParent, nId)
{
    pWindow = new SearchResultsDlg(pBindings, _pParent, nId);
}

SearchResultsDlgWrapper::~SearchResultsDlgWrapper() {}

SfxChildWinInfo SearchResultsDlgWrapper::GetInfo() const
{
    SfxChildWinInfo aInfo = SfxChildWindow::GetInfo();
    aInfo.bVisible = false;
    return aInfo;
}

SFX_IMPL_CHILDWINDOW_WITHID(SearchResultsDlgWrapper, SID_SEARCH_RESULTS_DIALOG);

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/inc/searchresults.hxx b/sc/source/ui/inc/searchresults.hxx
index aa03069..efb35f8 100644
--- a/sc/source/ui/inc/searchresults.hxx
+++ b/sc/source/ui/inc/searchresults.hxx
@@ -11,21 +11,45 @@
#define INCLUDED_SC_SOURCE_UI_INC_SEARCHRESULTS_HXX

#include <vcl/dialog.hxx>
#include <sfx2/childwin.hxx>

class ScDocument;
class ScRangeList;
class SvSimpleTable;

class SearchResults : public ModelessDialog
namespace sc {

class SearchResultsDlg : public ModelessDialog
{
    ScDocument *mpDoc;
    SvSimpleTable *mpList;
    SfxBindings* mpBindings;
    sal_uInt16 mnId;

    ScDocument* mpDoc;

    DECL_LINK( ListSelectHdl, void * );
public:
    SearchResults(ScDocument *);
    virtual ~SearchResults();
    void Show(const ScRangeList &);
    SearchResultsDlg( SfxBindings* _pBindings, Window* pParent, sal_uInt16 nId );
    virtual ~SearchResultsDlg();

    void FillResults( ScDocument* pDoc, const ScRangeList& rMatchedRanges );

    virtual bool Close() SAL_OVERRIDE;
};

class SearchResultsDlgWrapper : public SfxChildWindow
{
public:
    SearchResultsDlgWrapper(
        Window* _pParent, sal_uInt16 nId, SfxBindings* pBindings, SfxChildWinInfo* pInfo );

    virtual ~SearchResultsDlgWrapper();

    SFX_DECL_CHILDWINDOW_WITHID(SearchResultsDlgWrapper);
};

}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/view/cellsh1.cxx b/sc/source/ui/view/cellsh1.cxx
index 8781fb7..9ca1e1a 100644
--- a/sc/source/ui/view/cellsh1.cxx
+++ b/sc/source/ui/view/cellsh1.cxx
@@ -84,6 +84,7 @@
#include <tokenarray.hxx>
#include <formulacell.hxx>
#include <gridwin.hxx>
#include <searchresults.hxx>

#include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
@@ -998,6 +999,20 @@ void ScCellShell::ExecuteEdit( SfxRequest& rReq )
            }
            break;

        case SID_SEARCH_RESULTS_DIALOG:
        {
            const SfxPoolItem* pItem = NULL;
            if (pReqArgs->HasItem(SID_SEARCH_RESULTS_DIALOG, &pItem))
            {
                bool bVisible = static_cast<const SfxBoolItem*>(pItem)->GetValue();
                SfxViewFrame* pViewFrm = pTabViewShell->GetViewFrame();
                // The window ID should equal the slot ID, but not a biggie if it wasn't.
                sal_uInt16 nId = sc::SearchResultsDlgWrapper::GetChildWindowId();
                pViewFrm->SetChildWindow(nId, bVisible, false);
            }
            rReq.Done();
        }
        break;

        //  disposal (Outlines)
        //  SID_AUTO_OUTLINE, SID_OUTLINE_DELETEALL in Execute (in docsh.idl)
diff --git a/sc/source/ui/view/tabvwsh.cxx b/sc/source/ui/view/tabvwsh.cxx
index 42baf8d..01e61d9 100644
--- a/sc/source/ui/view/tabvwsh.cxx
+++ b/sc/source/ui/view/tabvwsh.cxx
@@ -20,6 +20,7 @@
#include "scitems.hxx"
#include <svx/galbrws.hxx>
#include <svx/imapdlg.hxx>
#include <svx/srchdlg.hxx>
#include <svl/srchitem.hxx>
#include <sfx2/templdlg.hxx>
#include <sfx2/objface.hxx>
@@ -39,6 +40,7 @@
#include "dwfunctr.hxx"
#include "sc.hrc"
#include "spelldialog.hxx"
#include <searchresults.hxx>

#define ScTabViewShell
#include "scslots.hxx"
@@ -79,11 +81,12 @@ void ScTabViewShell::InitInterface_Impl()
    GetStaticInterface()->RegisterChildWindow(ScAcceptChgDlgWrapper::GetChildWindowId());
    GetStaticInterface()->RegisterChildWindow(ScHighlightChgDlgWrapper::GetChildWindowId());
    GetStaticInterface()->RegisterChildWindow(ScSimpleRefDlgWrapper::GetChildWindowId());
    GetStaticInterface()->RegisterChildWindow(SID_SEARCH_DLG);
    GetStaticInterface()->RegisterChildWindow(SvxSearchDialogWrapper::GetChildWindowId());
    GetStaticInterface()->RegisterChildWindow(SID_HYPERLINK_DIALOG);
    GetStaticInterface()->RegisterChildWindow(GalleryChildWindow::GetChildWindowId());
    GetStaticInterface()->RegisterChildWindow(ScSpellDialogChildWindow::GetChildWindowId());
    GetStaticInterface()->RegisterChildWindow(ScValidityRefChildWin::GetChildWindowId());
    GetStaticInterface()->RegisterChildWindow(sc::SearchResultsDlgWrapper::GetChildWindowId());

    GetStaticInterface()->RegisterChildWindow(ScRandomNumberGeneratorDialogWrapper::GetChildWindowId());
    GetStaticInterface()->RegisterChildWindow(ScSamplingDialogWrapper::GetChildWindowId());
diff --git a/sc/source/ui/view/viewfun2.cxx b/sc/source/ui/view/viewfun2.cxx
index 3bbb8e2..85ec403 100644
--- a/sc/source/ui/view/viewfun2.cxx
+++ b/sc/source/ui/view/viewfun2.cxx
@@ -1580,8 +1580,18 @@ bool ScViewFunc::SearchAndReplace( const SvxSearchItem* pSearchItem,

            if (nCommand == SVX_SEARCHCMD_FIND_ALL || nCommand == SVX_SEARCHCMD_REPLACE_ALL)
            {
                SearchResults aSearchResults(pDoc);
                aSearchResults.Show(aMatchedRanges);
                SfxViewFrame* pViewFrm = SfxViewFrame::Current();
                if (pViewFrm)
                {
                    pViewFrm->ShowChildWindow(sc::SearchResultsDlgWrapper::GetChildWindowId(), true);
                    SfxChildWindow* pWnd = pViewFrm->GetChildWindow(sc::SearchResultsDlgWrapper::GetChildWindowId());
                    if (pWnd)
                    {
                        sc::SearchResultsDlg* pDlg = static_cast<sc::SearchResultsDlg*>(pWnd->GetWindow());
                        if (pDlg)
                            pDlg->FillResults(pDoc, aMatchedRanges);
                    }
                }

                rMark.ResetMark();
                for (size_t i = 0, n = aMatchedRanges.size(); i < n; ++i)