From 14fca7389aa93dee91a68cd0a25789c2b4b42879 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 18 Nov 2001 23:06:43 +0000 Subject: [PATCH] fixes for programmatically deleting the find/replace dialog git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@12481 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- samples/dialogs/dialogs.cpp | 83 +++++++++++++++++++++++++++---------- samples/dialogs/dialogs.h | 3 ++ src/msw/fdrepdlg.cpp | 28 ++++++++++++- 3 files changed, 92 insertions(+), 22 deletions(-) diff --git a/samples/dialogs/dialogs.cpp b/samples/dialogs/dialogs.cpp index 1e087c45e7..023abe5e4d 100644 --- a/samples/dialogs/dialogs.cpp +++ b/samples/dialogs/dialogs.cpp @@ -165,8 +165,8 @@ bool MyApp::OnInit() file_menu->Append(DIALOGS_BUSYINFO, "&Busy info dialog\tCtrl-B"); #endif // wxUSE_BUSYINFO #if wxUSE_FINDREPLDLG - file_menu->Append(DIALOGS_FIND, "&Find dialog\tCtrl-F"); - file_menu->Append(DIALOGS_REPLACE, "Find and &replace dialog\tShift-Ctrl-F"); + file_menu->Append(DIALOGS_FIND, "&Find dialog\tCtrl-F", "", TRUE); + file_menu->Append(DIALOGS_REPLACE, "Find and &replace dialog\tShift-Ctrl-F", "", TRUE); #endif // wxUSE_FINDREPLDLG file_menu->AppendSeparator(); file_menu->Append(DIALOGS_MODAL, "Mo&dal dialog\tCtrl-D"); @@ -199,6 +199,9 @@ MyFrame::MyFrame(wxWindow *parent, { m_dialog = (MyModelessDialog *)NULL; + m_dlgFind = + m_dlgReplace = NULL; + CreateStatusBar(); } @@ -658,27 +661,45 @@ void MyFrame::ShowBusyInfo(wxCommandEvent& WXUNUSED(event)) void MyFrame::ShowReplaceDialog( wxCommandEvent& WXUNUSED(event) ) { - wxFindReplaceDialog *dialog = new wxFindReplaceDialog - ( - this, - &m_findData, - "Find and replace dialog", - wxFR_REPLACEDIALOG - ); - dialog->Show(TRUE); + if ( m_dlgReplace ) + { + delete m_dlgReplace; + m_dlgReplace = NULL; + } + else + { + m_dlgReplace = new wxFindReplaceDialog + ( + this, + &m_findData, + "Find and replace dialog", + wxFR_REPLACEDIALOG + ); + + m_dlgReplace->Show(TRUE); + } } void MyFrame::ShowFindDialog( wxCommandEvent& WXUNUSED(event) ) { - wxFindReplaceDialog *dialog = new wxFindReplaceDialog - ( - this, - &m_findData, - "Find dialog", - // just for testing - wxFR_NOWHOLEWORD - ); - dialog->Show(TRUE); + if ( m_dlgFind ) + { + delete m_dlgFind; + m_dlgFind = NULL; + } + else + { + m_dlgFind = new wxFindReplaceDialog + ( + this, + &m_findData, + "Find dialog", + // just for testing + wxFR_NOWHOLEWORD + ); + + m_dlgFind->Show(TRUE); + } } static wxString DecodeFindDialogEventFlags(int flags) @@ -714,9 +735,29 @@ void MyFrame::OnFindDialog(wxFindDialogEvent& event) } else if ( type == wxEVT_COMMAND_FIND_CLOSE ) { - wxLogMessage(wxT("Find dialog is being closed.")); + wxFindReplaceDialog *dlg = event.GetDialog(); + + const wxChar *txt; + if ( dlg == m_dlgFind ) + { + txt = _T("Find"); + m_dlgFind = NULL; + } + else if ( dlg == m_dlgReplace ) + { + txt = _T("Replace"); + m_dlgReplace = NULL; + } + else + { + txt = _T("Unknown"); + + wxFAIL_MSG( _T("unexecpted event") ); + } + + wxLogMessage(wxT("%s dialog is being closed."), txt), - event.GetDialog()->Destroy(); + dlg->Destroy(); } else { diff --git a/samples/dialogs/dialogs.h b/samples/dialogs/dialogs.h index 5c3396b96b..3193e26f2f 100644 --- a/samples/dialogs/dialogs.h +++ b/samples/dialogs/dialogs.h @@ -100,6 +100,9 @@ private: #if wxUSE_FINDREPLDLG wxFindReplaceData m_findData; + + wxFindReplaceDialog *m_dlgFind, + *m_dlgReplace; #endif // wxUSE_FINDREPLDLG DECLARE_EVENT_TABLE() diff --git a/src/msw/fdrepdlg.cpp b/src/msw/fdrepdlg.cpp index 0d34834244..13c4062c8c 100644 --- a/src/msw/fdrepdlg.cpp +++ b/src/msw/fdrepdlg.cpp @@ -81,6 +81,10 @@ public: // only for passing to ::FindText or ::ReplaceText FINDREPLACE *GetPtrFindReplace() { return &m_findReplace; } + // set/query the "closed by user" flag + void SetClosedByUser() { m_wasClosedByUser = TRUE; } + bool WasClosedByUser() const { return m_wasClosedByUser; } + private: void InitString(const wxString& str, LPTSTR *ppStr, WORD *pLen); @@ -93,6 +97,9 @@ private: // the find replace data used by the dialog FINDREPLACE m_findReplace; + // TRUE if the user closed us, FALSE otherwise + bool m_wasClosedByUser; + // registered Message for Dialog static UINT ms_msgFindDialog; }; @@ -124,6 +131,8 @@ wxFindReplaceDialogImpl::wxFindReplaceDialogImpl(wxFindReplaceDialog *dialog, m_hwndOwner = NULL; m_oldParentWndProc = NULL; + m_wasClosedByUser = FALSE; + wxZeroMemory(m_findReplace); // translate the flags: first the dialog creation flags @@ -231,6 +240,11 @@ LRESULT APIENTRY wxFindReplaceWindowProc(HWND hwnd, WXUINT nMsg, bool replace = FALSE; if ( pFR->Flags & FR_DIALOGTERM ) { + // we have to notify the dialog that it's being closed by user and + // not deleted programmatically as it behaves differently in these + // 2 cases + dialog->GetImpl()->SetClosedByUser(); + evtType = wxEVT_COMMAND_FIND_CLOSE; } else if ( pFR->Flags & FR_FINDNEXT ) @@ -338,13 +352,25 @@ wxFindReplaceDialog::wxFindReplaceDialog(wxWindow *parent, wxFindReplaceDialog::~wxFindReplaceDialog() { + // the dialog might have been already deleted if the user closed it + // manually but in this case we should have got a notification about it and + // the flagmust have been set + if ( !m_impl->WasClosedByUser() ) + { + // if it wasn't, delete the dialog ourselves + if ( !::DestroyWindow(GetHwnd()) ) + { + wxLogLastError(_T("DestroyWindow(find dialog)")); + } + } + // unsubclass the parent delete m_impl; // prevent the base class dtor from trying to hide us! m_isShown = FALSE; - // and from destroying our window + // and from destroying our window [again] m_hWnd = (WXHWND)NULL; } -- 2.45.2