window deletion overviewclasses: wxcloseevent, wxwindow window deletion can be a confusing subject, so this overview is provided to help make it clear when and how you delete windows, or respond to user requests to close windows. what is the sequence of events in a window deletion? when the user clicks on the system close button or system close command, in a frame or a dialog, wxwidgets calls wxwindow::close. this in turn generates an evt_close event: see wxcloseevent. it is the duty of the application to define a suitable event handler, and decide whether or not to destroy the window. if the application is for some reason forcing the application to close (wxcloseevent::canveto returns false), the window should always be destroyed, otherwise there is the option to ignore the request, or maybe wait until the user has answered a question before deciding whether it is safe to close. the handler for evt_close should signal to the calling code if it does not destroy the window, by calling wxcloseevent::veto. calling this provides useful information to the calling code. the wxcloseevent handler should only call wxwindow::destroy to delete the window, and not use the delete operator. this is because for some window classes, wxwidgets delays actual deletion of the window until all events have been processed, since otherwise there is the danger that events will be sent to a non-existent window. as reinforced in the next section, calling close does not guarantee that the window will be destroyed. call wxwindow::destroy if you want to be certain that the window is destroyed. how can the application close a window itself? your application can either use wxwindow::close event just as the framework does, or it can call wxwindow::destroy directly. if using close(), you can pass a true argument to this function to tell the event handler that we definitely want to delete the frame and it cannot be vetoed. the advantage of using close instead of destroy is that it will call any clean-up code defined by the evt_close handler; for example it may close a document contained in a window after first asking the user whether the work should be saved. close can be vetoed by this process (return false), whereas destroy definitely destroys the window. what is the default behaviour? the default close event handler for wxdialog simulates a cancel command, generating a wxid_cancel event. since the handler for this cancel event might itself call close, there is a check for infinite looping. the default handler for wxid_cancel hides the dialog (if modeless) or calls endmodal(wxid_cancel) (if modal). in other words, by default, the dialog is not destroyed (it might have been created on the stack, so the assumption of dynamic creation cannot be made). the default close event handler for wxframe destroys the frame using destroy(). what should i do when the user calls up exit from a menu? you can simply call wxwindow::close on the frame. this will invoke your own close event handler which may destroy the frame. you can do checking to see if your application can be safely exited at this point, either from within your close event handler, or from within your exit menu command handler. for example, you may wish to check that all files have been saved. give the user a chance to save and quit, to not save but quit anyway, or to cancel the exit command altogether. what should i do to upgrade my 1.xx onclose to 2.0? in wxwidgets 1.xx, the onclose function did not actually delete 'this', but signaled to the calling function (either close, or the wxwidgets framework) to delete or not delete the window. to update your code, you should provide an event table entry in your frame or dialog, using the evt_close macro. the event handler function might look like this:
a wxwidgets application automatically exits when the last top level window (wxframe or wxdialog), is destroyed. put any application-wide cleanup code in wxapp::onexit (this is a virtual function, not an event handler). do child windows get deleted automatically? yes, child windows are deleted from within the parent destructor. this includes any children that are themselves frames or dialogs, so you may wish to close these child frame or dialog windows explicitly from within the parent close handler. what about other kinds of window? so far we've been talking about 'managed' windows, i.e. frames and dialogs. windows with parents, such as controls, don't have delayed destruction and don't usually have close event handlers, though you can implement them if you wish. for consistency, continue to use the wxwindow::destroy function instead of the delete operator when deleting these kinds of windows explicitly.
|