]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/tdelwin.tex
added double click handler. various minor fixes.
[wxWidgets.git] / docs / latex / wx / tdelwin.tex
CommitLineData
a660d684
KB
1\section{Window deletion overview}\label{windowdeletionoverview}
2
3Classes: \helpref{wxCloseEvent}{wxcloseevent}, \helpref{wxWindow}{wxwindow}
4
5Window deletion can be a confusing subject, so this overview is provided
6to make it clear when and how you delete windows, or respond to user requests
7to close windows.
8
9\wxheading{What is the sequence of events in a window deletion?}
10
11When the user clicks on the system close button or system close command,
e3065973
JS
12in a frame or a dialog, wxWindows calls \helpref{wxWindow::Close}{wxwindowclose}. This
13in turn generates an EVT\_CLOSE event: see \helpref{wxWindow::OnCloseWindow}{wxwindowonclosewindow}.
a660d684 14
e3065973
JS
15It is the duty of the application to define a suitable event handler, and
16decide whether or not to destroy the window.
17If the application is for some reason forcing the application to close
18(\helpref{wxCloseEvent::CanVeto}{wxcloseeventcanveto} returns FALSE), the window should always be destroyed, otherwise there is the option to
a660d684 19ignore the request, or maybe wait until the user has answered a question
e3065973
JS
20before deciding whether it's safe to close. The handler for EVT\_CLOSE should
21signal to the calling code if it does not destroy the window, by calling
22\helpref{wxCloseEvent::Veto}{wxcloseeventveto}. Calling this provides useful information
23to the calling code.
a660d684
KB
24
25The wxCloseEvent handler should only call \helpref{wxWindow::Destroy}{wxwindowdestroy} to
26delete the window, and not use the {\bf delete} operator. This is because
27for some window classes, wxWindows delays actual deletion of the window until all events have been processed,
28since otherwise there is the danger that events will be sent to a non-existent window.
29
30\wxheading{How can the application close a window itself?}
31
e3065973
JS
32Your application can either use \helpref{wxWindow::Close}{wxwindowclose} event just as
33the framework does, or it can call \helpref{wxWindow::Destroy}{wxwindowdestroy} directly.
34If using Close(), you can pass a TRUE argument to this function to tell the event handler
35that we definitely want to delete the frame and it cannot be vetoed.
a660d684 36
e3065973
JS
37The advantage of using Close instead of Destroy is that it will call any clean-up code
38defined by the EVT\_CLOSE handler; for example it may close a document contained in
39a window after first asking the user whether the work should be saved. Close can be vetoed
40by this process (return FALSE), whereas Destroy definitely destroys the window.
a660d684
KB
41
42\wxheading{What is the default behaviour?}
43
e3065973 44The default close event handler for wxDialog simulates a Cancel command,
a660d684 45generating a wxID\_CANCEL event. Since the handler for this cancel event might
e3065973
JS
46itself call {\bf Close}, there is a check for infinite looping. The default handler
47for wxID\_CANCEL hides the dialog (if modeless) or calls EndModal(wxID\_CANCEL) (if modal).
48In other words, by default, the dialog is not destroyed (it might have been created
49on the stack, so the assumption of dynamic creation cannot be made).
50
51The default close event handler for wxFrame destroys the frame using Destroy().
a660d684 52
e3065973 53Under Windows, wxDialog defines a handler for \helpref{wxWindow::OnCharHook}{wxwindowoncharhook} that
a660d684
KB
54generates a Cancel event if the Escape key has been pressed.
55
56\wxheading{What should I do when the user calls up Exit from a menu?}
57
58You can simply call \helpref{wxWindow::Close}{wxwindowclose} on the frame. This
59will invoke your own close event handler which may destroy the frame.
60
61You can do checking to see if your application can be safely exited at this point,
62either from within your close event handler, or from within your exit menu command
63handler. For example, you may wish to check that all files have been saved.
64Give the user a chance to save and quit, to not save but quit anyway, or to cancel
65the exit command altogether.
66
67\wxheading{What should I do to upgrade my 1.xx OnClose to 2.0?}
68
69In wxWindows 1.xx, the {\bf OnClose} function did not actually delete 'this', but signalled
70to the calling function (either {\bf Close}, or the wxWindows framework) to delete
71or not delete the window.
72
a660d684
KB
73To update your code, you should provide an event table entry in your frame or
74dialog, using the EVT\_CLOSE macro. The event handler function might look like this:
75
76{\small%
77\begin{verbatim}
78 void MyFrame::OnCloseWindow(wxCloseEvent& event)
79 {
a660d684
KB
80 if (MyDataHasBeenModified())
81 {
82 wxMessageDialog* dialog = new wxMessageDialog(this,
83 "Save changed data?", "My app", wxYES_NO|wxCANCEL);
84
85 int ans = dialog->ShowModal();
e3065973 86 dialog->Destroy();
a660d684
KB
87
88 switch (ans)
89 {
90 case wxID_YES: // Save, then destroy, quitting app
91 SaveMyData();
92 this->Destroy();
93 break;
94 case wxID_NO: // Don't save; just destroy, quitting app
95 this->Destroy();
96 break;
97 case wxID_CANCEL: // Do nothing - so don't quit app.
98 default:
e3065973
JS
99 if (!event.CanVeto()) // Test if we can veto this deletion
100 this->Destroy(); // If not, destroy the window anyway.
101 else
102 event.Veto(); // Notify the calling code that we didn't delete the frame.
a660d684
KB
103 break;
104 }
105 }
106 }
107\end{verbatim}
108}%
109
110\wxheading{How do I exit the application gracefully?}
111
2fd284a4 112A wxWindows application automatically exits when the designated top window, or the
e3065973
JS
113last frame or dialog, is destroyed. Put any application-wide cleanup code in \helpref{wxApp::OnExit}{wxapponexit} (this
114is a virtual function, not an event handler).
a660d684
KB
115
116\wxheading{Do child windows get deleted automatically?}
117
118Yes, child windows are deleted from within the parent destructor. This includes any children
119that are themselves frames or dialogs, so you may wish to close these child frame or dialog windows
120explicitly from within the parent close handler.
121
122\wxheading{What about other kinds of window?}
123
124So far we've been talking about `managed' windows, i.e. frames and dialogs. Windows
125with parents, such as controls, don't have delayed destruction and don't usually have
126close event handlers, though you can implement them if you wish. For consistency,
127continue to use the \helpref{wxWindow::Destroy}{wxwindowdestroy} function instead
128of the {\bf delete} operator when deleting these kinds of windows explicitly.
129