1 \section{Window deletion overview
}\label{windowdeletionoverview
}
3 Classes:
\helpref{wxCloseEvent
}{wxcloseevent
},
\helpref{wxWindow
}{wxwindow
}
5 Window deletion can be a confusing subject, so this overview is provided
6 to make it clear when and how you delete windows, or respond to user requests
9 \wxheading{What is the sequence of events in a window deletion?
}
11 When the user clicks on the system close button or system close command,
12 in a frame or a dialog, wxWindows calls
\helpref{wxWindow::Close
}{wxwindowclose
}.
14 This function then generates a
\helpref{wxCloseEvent
}{wxcloseevent
} event which
15 can be handled by the application (by using an EVT
\_CLOSE event table entry). It is the duty of the application to
16 define a suitable event handler, and decide whether or not to destroy the window.
17 If the application is for some reason forcing the application to close,
18 the window should always be destroyed, otherwise there is the option to
19 ignore the request, or maybe wait until the user has answered a question
20 before deciding whether it's safe to close.
22 The wxCloseEvent handler should only call
\helpref{wxWindow::Destroy
}{wxwindowdestroy
} to
23 delete the window, and not use the
{\bf delete
} operator. This is because
24 for some window classes, wxWindows delays actual deletion of the window until all events have been processed,
25 since otherwise there is the danger that events will be sent to a non-existent window.
27 \wxheading{How can the application close a window itself?
}
29 Your application can use the
\helpref{wxWindow::Close
}{wxwindowclose
} event just as
30 the framework does. Pass a TRUE argument to this function to tell the event handler
31 that we definitely want to delete the frame.
33 If for some reason you don't wish to use the
{\bf Close
} function to delete a window, at least use
34 the
{\bf Destroy
} function so that wxWindows can decide when it's safe to delete the window.
36 \wxheading{What is the default behaviour?
}
38 By default, the close event handlers for wxFrame and wxDialog
39 both call the old
\helpref{wxWindow::OnClose
}{wxwindowonclose
} handler
40 for backward compatibility. So you can still use the old form if you wish.
42 In addition, the default close event handler for wxDialog simulates a Cancel command,
43 generating a wxID
\_CANCEL event. Since the handler for this cancel event might
44 itself call
{\bf Close
}, there is a check for infinite looping.
46 Under Windows, wxDialog also defines a handler for
\helpref{wxWindow::OnCharHook
}{wxwindowoncharhook
} that
47 generates a Cancel event if the Escape key has been pressed.
49 \wxheading{What should I do when the user calls up Exit from a menu?
}
51 You can simply call
\helpref{wxWindow::Close
}{wxwindowclose
} on the frame. This
52 will invoke your own close event handler which may destroy the frame.
54 You can do checking to see if your application can be safely exited at this point,
55 either from within your close event handler, or from within your exit menu command
56 handler. For example, you may wish to check that all files have been saved.
57 Give the user a chance to save and quit, to not save but quit anyway, or to cancel
58 the exit command altogether.
60 \wxheading{What should I do to upgrade my
1.xx OnClose to
2.0?
}
62 In wxWindows
1.xx, the
{\bf OnClose
} function did not actually delete 'this', but signalled
63 to the calling function (either
{\bf Close
}, or the wxWindows framework) to delete
64 or not delete the window.
66 You can still use this function unchanged in
2.0, but it's worth upgrading to
67 the new method in case future versions of wxWindows does not support the old one.
69 To update your code, you should provide an event table entry in your frame or
70 dialog, using the EVT
\_CLOSE macro. The event handler function might look like this:
74 void MyFrame::OnCloseWindow(wxCloseEvent& event)
76 // If the application forces the deletion,
77 // obey without question.
85 if (MyDataHasBeenModified())
87 wxMessageDialog* dialog = new wxMessageDialog(this,
88 "Save changed data?", "My app", wxYES_NO|wxCANCEL);
90 int ans = dialog->ShowModal();
95 case wxID_YES: // Save, then destroy, quitting app
99 case wxID_NO: // Don't save; just destroy, quitting app
102 case wxID_CANCEL: // Do nothing - so don't quit app.
111 \wxheading{How do I exit the application gracefully?
}
113 A wxWindows application automatically exits when the top frame (returned
114 from
\helpref{wxApp::OnInit
}{wxapponinit
}) is destroyed. This may be modified
115 in later versions to exit only when the
{\it last
} top-level frame is destroyed.
117 \wxheading{Do child windows get deleted automatically?
}
119 Yes, child windows are deleted from within the parent destructor. This includes any children
120 that are themselves frames or dialogs, so you may wish to close these child frame or dialog windows
121 explicitly from within the parent close handler.
123 \wxheading{What about other kinds of window?
}
125 So far we've been talking about `managed' windows, i.e. frames and dialogs. Windows
126 with parents, such as controls, don't have delayed destruction and don't usually have
127 close event handlers, though you can implement them if you wish. For consistency,
128 continue to use the
\helpref{wxWindow::Destroy
}{wxwindowdestroy
} function instead
129 of the
{\bf delete
} operator when deleting these kinds of windows explicitly.