]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/tdelwin.tex
Some warnings removed.
[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,
12in a frame or a dialog, wxWindows calls \helpref{wxWindow::Close}{wxwindowclose}.
13
14This function then generates a \helpref{wxCloseEvent}{wxcloseevent} event which
15can be handled by the application (by using an EVT\_CLOSE event table entry). It is the duty of the application to
16define a suitable event handler, and decide whether or not to destroy the window.
17If the application is for some reason forcing the application to close,
18the window should always be destroyed, otherwise there is the option to
19ignore the request, or maybe wait until the user has answered a question
20before deciding whether it's safe to close.
21
22The wxCloseEvent handler should only call \helpref{wxWindow::Destroy}{wxwindowdestroy} to
23delete the window, and not use the {\bf delete} operator. This is because
24for some window classes, wxWindows delays actual deletion of the window until all events have been processed,
25since otherwise there is the danger that events will be sent to a non-existent window.
26
27\wxheading{How can the application close a window itself?}
28
29Your application can use the \helpref{wxWindow::Close}{wxwindowclose} event just as
30the framework does. Pass a TRUE argument to this function to tell the event handler
31that we definitely want to delete the frame.
32
33If for some reason you don't wish to use the {\bf Close} function to delete a window, at least use
34the {\bf Destroy} function so that wxWindows can decide when it's safe to delete the window.
35
36\wxheading{What is the default behaviour?}
37
38By default, the close event handlers for wxFrame and wxDialog
39both call the old \helpref{wxWindow::OnClose}{wxwindowonclose} handler
40for backward compatibility. So you can still use the old form if you wish.
41
42In addition, the default close event handler for wxDialog simulates a Cancel command,
43generating a wxID\_CANCEL event. Since the handler for this cancel event might
44itself call {\bf Close}, there is a check for infinite looping.
45
46Under Windows, wxDialog also defines a handler for \helpref{wxWindow::OnCharHook}{wxwindowoncharhook} that
47generates a Cancel event if the Escape key has been pressed.
48
49\wxheading{What should I do when the user calls up Exit from a menu?}
50
51You can simply call \helpref{wxWindow::Close}{wxwindowclose} on the frame. This
52will invoke your own close event handler which may destroy the frame.
53
54You can do checking to see if your application can be safely exited at this point,
55either from within your close event handler, or from within your exit menu command
56handler. For example, you may wish to check that all files have been saved.
57Give the user a chance to save and quit, to not save but quit anyway, or to cancel
58the exit command altogether.
59
60\wxheading{What should I do to upgrade my 1.xx OnClose to 2.0?}
61
62In wxWindows 1.xx, the {\bf OnClose} function did not actually delete 'this', but signalled
63to the calling function (either {\bf Close}, or the wxWindows framework) to delete
64or not delete the window.
65
66You can still use this function unchanged in 2.0, but it's worth upgrading to
67the new method in case future versions of wxWindows does not support the old one.
68
69To update your code, you should provide an event table entry in your frame or
70dialog, using the EVT\_CLOSE macro. The event handler function might look like this:
71
72{\small%
73\begin{verbatim}
74 void MyFrame::OnCloseWindow(wxCloseEvent& event)
75 {
76 // If the application forces the deletion,
77 // obey without question.
78 if (event.GetForce())
79 {
80 this->Destroy();
81 return;
82 }
83
84 // Otherwise...
85 if (MyDataHasBeenModified())
86 {
87 wxMessageDialog* dialog = new wxMessageDialog(this,
88 "Save changed data?", "My app", wxYES_NO|wxCANCEL);
89
90 int ans = dialog->ShowModal();
91 dialog->Close(TRUE);
92
93 switch (ans)
94 {
95 case wxID_YES: // Save, then destroy, quitting app
96 SaveMyData();
97 this->Destroy();
98 break;
99 case wxID_NO: // Don't save; just destroy, quitting app
100 this->Destroy();
101 break;
102 case wxID_CANCEL: // Do nothing - so don't quit app.
103 default:
104 break;
105 }
106 }
107 }
108\end{verbatim}
109}%
110
111\wxheading{How do I exit the application gracefully?}
112
2fd284a4
JS
113A wxWindows application automatically exits when the designated top window, or the
114last frame or dialog, is destroyed.
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