]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/overviews/windowdeletion.h
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / docs / doxygen / overviews / windowdeletion.h
CommitLineData
15b6757b 1/////////////////////////////////////////////////////////////////////////////
3863c5eb 2// Name: windowdeletion.h
15b6757b
FM
3// Purpose: topic overview
4// Author: wxWidgets team
526954c5 5// Licence: wxWindows licence
15b6757b
FM
6/////////////////////////////////////////////////////////////////////////////
7
880efa2a 8/**
36c9828f 9
880efa2a 10@page overview_windowdeletion Window Deletion
36c9828f 11
831e1028 12@tableofcontents
3863c5eb
BP
13
14Window deletion can be a confusing subject, so this overview is provided to
15help make it clear when and how you delete windows, or respond to user requests
16to close windows.
17
831e1028 18@see wxCloseEvent, wxWindow
30738aae
FM
19
20
3863c5eb
BP
21
22@section overview_windowdeletion_sequence Sequence of Events During Window Deletion
23
24When the user clicks on the system close button or system close command, in a
25frame or a dialog, wxWidgets calls wxWindow::Close. This in turn generates an
26EVT_CLOSE event: see wxCloseEvent.
27
28It is the duty of the application to define a suitable event handler, and
29decide whether or not to destroy the window. If the application is for some
30reason forcing the application to close (wxCloseEvent::CanVeto returns @false),
31the window should always be destroyed, otherwise there is the option to ignore
32the request, or maybe wait until the user has answered a question before
33deciding whether it is safe to close. The handler for EVT_CLOSE should signal
34to the calling code if it does not destroy the window, by calling
35wxCloseEvent::Veto. Calling this provides useful information to the calling
36code.
37
38The wxCloseEvent handler should only call wxWindow::Destroy to delete the
39window, and not use the @c delete operator. This is because for some window
40classes, wxWidgets delays actual deletion of the window until all events have
41been processed, since otherwise there is the danger that events will be sent to
42a non-existent window.
43
44As reinforced in the next section, calling Close does not guarantee that the window
45will be destroyed. Call wxWindow::Destroy if you want to be
46certain that the window is destroyed.
47
48
49@section overview_windowdeletion_close Closing Windows
50
51Your application can either use wxWindow::Close event just as the framework
52does, or it can call wxWindow::Destroy directly. If using Close(), you can pass
53a @true argument to this function to tell the event handler that we definitely
54want to delete the frame and it cannot be vetoed.
55
56The advantage of using Close instead of Destroy is that it will call any
57clean-up code defined by the EVT_CLOSE handler; for example it may close a
58document contained in a window after first asking the user whether the work
59should be saved. Close can be vetoed by this process (return @false), whereas
60Destroy definitely destroys the window.
61
62
63@section overview_windowdeletion_default Default Window Close Behaviour
64
65The default close event handler for wxDialog simulates a Cancel command,
66generating a wxID_CANCEL event. Since the handler for this cancel event might
67itself call Close, there is a check for infinite looping. The default handler
68for wxID_CANCEL hides the dialog (if modeless) or calls EndModal(wxID_CANCEL)
69(if modal). In other words, by default, the dialog @e is not destroyed (it
70might have been created on the stack, so the assumption of dynamic creation
71cannot be made).
72
73The default close event handler for wxFrame destroys the frame using Destroy().
74
75
76@section overview_windowdeletion_menuexit User Calls to Exit From a Menu
77
78What should I do when the user calls up Exit from a menu? You can simply call
79wxWindow::Close on the frame. This will invoke your own close event handler
80which may destroy the frame.
81
82You can do checking to see if your application can be safely exited at this
83point, either from within your close event handler, or from within your exit
84menu command handler. For example, you may wish to check that all files have
85been saved. Give the user a chance to save and quit, to not save but quit
86anyway, or to cancel the exit command altogether.
87
88
89@section overview_windowdeletion_exitapp Exiting the Application Gracefully
90
91A wxWidgets application automatically exits when the last top level window
92(wxFrame or wxDialog), is destroyed. Put any application-wide cleanup code in
93wxApp::OnExit (this is a virtual function, not an event handler).
94
95
96@section overview_windowdeletion_deletion Automatic Deletion of Child Windows
97
98Child windows are deleted from within the parent destructor. This includes any
99children that are themselves frames or dialogs, so you may wish to close these
100child frame or dialog windows explicitly from within the parent close handler.
101
102
103@section overview_windowdeletion_windowkinds Other Kinds of Windows
104
105So far we've been talking about 'managed' windows, i.e. frames and dialogs.
106Windows with parents, such as controls, don't have delayed destruction and
107don't usually have close event handlers, though you can implement them if you
108wish. For consistency, continue to use the wxWindow::Destroy function instead
109of the @c delete operator when deleting these kinds of windows explicitly.
110
111*/