]> git.saurik.com Git - wxWidgets.git/commitdiff
fixes for programmatically deleting the find/replace dialog
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 18 Nov 2001 23:06:43 +0000 (23:06 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 18 Nov 2001 23:06:43 +0000 (23:06 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@12481 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

samples/dialogs/dialogs.cpp
samples/dialogs/dialogs.h
src/msw/fdrepdlg.cpp

index 1e087c45e72a73adf74c555fa3b43d2b3b342f30..023abe5e4d15812287d2d35e337cc5fb814684cd 100644 (file)
@@ -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
     {
index 5c3396b96b2c9c15aa49e0f147b882aa0b051be6..3193e26f2f5320aee9d8041f20d5ecd019b177d7 100644 (file)
@@ -100,6 +100,9 @@ private:
 
 #if wxUSE_FINDREPLDLG
     wxFindReplaceData m_findData;
+
+    wxFindReplaceDialog *m_dlgFind,
+                        *m_dlgReplace;
 #endif // wxUSE_FINDREPLDLG
 
     DECLARE_EVENT_TABLE()
index 0d34834244735f4eee9afda48dc78fcfa7bd881d..13c4062c8c62c72d686db600e38beb28c8f3015f 100644 (file)
@@ -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;
 }