1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>wxWidgets: Window Deletion</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="extra_stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="page_container">
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0" style="width: 100%;">
<tbody>
<tr>
<td id="projectlogo">
<a href="http://www.wxwidgets.org/" target="_new">
<img alt="wxWidgets" src="logo.png"/>
</a>
</td>
<td style="padding-left: 0.5em; text-align: right;">
<span id="projectnumber">Version: 3.0.2</span>
</td>
</tr>
</tbody>
</table>
</div>
<!-- Generated by Doxygen 1.8.2 -->
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main Page</span></a></li>
<li class="current"><a href="pages.html"><span>Related Pages</span></a></li>
<li><a href="modules.html"><span>Categories</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
</ul>
</div>
<div id="nav-path" class="navpath">
<ul>
<li class="navelem"><a class="el" href="index.html">Documentation</a></li><li class="navelem"><a class="el" href="page_topics.html">Programming Guides</a></li> </ul>
</div>
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">Window Deletion </div> </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#overview_windowdeletion_sequence">Sequence of Events During Window Deletion</a></li>
<li class="level1"><a href="#overview_windowdeletion_close">Closing Windows</a></li>
<li class="level1"><a href="#overview_windowdeletion_default">Default Window Close Behaviour</a></li>
<li class="level1"><a href="#overview_windowdeletion_menuexit">User Calls to Exit From a Menu</a></li>
<li class="level1"><a href="#overview_windowdeletion_exitapp">Exiting the Application Gracefully</a></li>
<li class="level1"><a href="#overview_windowdeletion_deletion">Automatic Deletion of Child Windows</a></li>
<li class="level1"><a href="#overview_windowdeletion_windowkinds">Other Kinds of Windows</a></li>
</ul>
</div>
<div class="textblock"><p>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.</p>
<dl class="section see"><dt>See Also</dt><dd><a class="el" href="classwx_close_event.html" title="This event class contains information about window and session close events.">wxCloseEvent</a>, <a class="el" href="classwx_window.html" title="wxWindow is the base class for all windows and represents any visible object on screen.">wxWindow</a></dd></dl>
<h1><a class="anchor" id="overview_windowdeletion_sequence"></a>
Sequence of Events During Window Deletion</h1>
<p>When the user clicks on the system close button or system close command, in a frame or a dialog, wxWidgets calls <a class="el" href="classwx_window.html#a3e44f4a494fc9ef4346c4fba70c8de0c" title="This function simply generates a wxCloseEvent whose handler usually tries to close the window...">wxWindow::Close</a>. This in turn generates an EVT_CLOSE event: see <a class="el" href="classwx_close_event.html" title="This event class contains information about window and session close events.">wxCloseEvent</a>.</p>
<p>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 (<a class="el" href="classwx_close_event.html#adaa0548df45e05e8487cc5664ac352cf" title="Returns true if you can veto a system shutdown or a window close event.">wxCloseEvent::CanVeto</a> returns <span class="literal">false</span>), 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 <a class="el" href="classwx_close_event.html#a0ec337c77613d5d8407831cd64e16417" title="Call this from your event handler to veto a system shutdown or to signal to the calling application t...">wxCloseEvent::Veto</a>. Calling this provides useful information to the calling code.</p>
<p>The <a class="el" href="classwx_close_event.html" title="This event class contains information about window and session close events.">wxCloseEvent</a> handler should only call <a class="el" href="classwx_window.html#a6bf0c5be864544d9ce0560087667b7fc" title="Destroys the window safely.">wxWindow::Destroy</a> to delete the window, and not use the <code>delete</code> 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.</p>
<p>As reinforced in the next section, calling Close does not guarantee that the window will be destroyed. Call <a class="el" href="classwx_window.html#a6bf0c5be864544d9ce0560087667b7fc" title="Destroys the window safely.">wxWindow::Destroy</a> if you want to be certain that the window is destroyed.</p>
<h1><a class="anchor" id="overview_windowdeletion_close"></a>
Closing Windows</h1>
<p>Your application can either use <a class="el" href="classwx_window.html#a3e44f4a494fc9ef4346c4fba70c8de0c" title="This function simply generates a wxCloseEvent whose handler usually tries to close the window...">wxWindow::Close</a> event just as the framework does, or it can call <a class="el" href="classwx_window.html#a6bf0c5be864544d9ce0560087667b7fc" title="Destroys the window safely.">wxWindow::Destroy</a> directly. If using Close(), you can pass a <span class="literal">true</span> argument to this function to tell the event handler that we definitely want to delete the frame and it cannot be vetoed.</p>
<p>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 <span class="literal">false</span>), whereas Destroy definitely destroys the window.</p>
<h1><a class="anchor" id="overview_windowdeletion_default"></a>
Default Window Close Behaviour</h1>
<p>The default close event handler for <a class="el" href="classwx_dialog.html" title="A dialog box is a window with a title bar and sometimes a system menu, which can be moved around the ...">wxDialog</a> 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 <em>is</em> not destroyed (it might have been created on the stack, so the assumption of dynamic creation cannot be made).</p>
<p>The default close event handler for <a class="el" href="classwx_frame.html" title="A frame is a window whose size and position can (usually) be changed by the user.">wxFrame</a> destroys the frame using Destroy().</p>
<h1><a class="anchor" id="overview_windowdeletion_menuexit"></a>
User Calls to Exit From a Menu</h1>
<p>What should I do when the user calls up Exit from a menu? You can simply call <a class="el" href="classwx_window.html#a3e44f4a494fc9ef4346c4fba70c8de0c" title="This function simply generates a wxCloseEvent whose handler usually tries to close the window...">wxWindow::Close</a> on the frame. This will invoke your own close event handler which may destroy the frame.</p>
<p>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.</p>
<h1><a class="anchor" id="overview_windowdeletion_exitapp"></a>
Exiting the Application Gracefully</h1>
<p>A wxWidgets application automatically exits when the last top level window (<a class="el" href="classwx_frame.html" title="A frame is a window whose size and position can (usually) be changed by the user.">wxFrame</a> or <a class="el" href="classwx_dialog.html" title="A dialog box is a window with a title bar and sometimes a system menu, which can be moved around the ...">wxDialog</a>), is destroyed. Put any application-wide cleanup code in <a class="el" href="classwx_app_console.html#a5ee60051c92b0b2933258799626a0485" title="Override this member function for any processing which needs to be done as the application is about t...">wxApp::OnExit</a> (this is a virtual function, not an event handler).</p>
<h1><a class="anchor" id="overview_windowdeletion_deletion"></a>
Automatic Deletion of Child Windows</h1>
<p>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.</p>
<h1><a class="anchor" id="overview_windowdeletion_windowkinds"></a>
Other Kinds of Windows</h1>
<p>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 <a class="el" href="classwx_window.html#a6bf0c5be864544d9ce0560087667b7fc" title="Destroys the window safely.">wxWindow::Destroy</a> function instead of the <code>delete</code> operator when deleting these kinds of windows explicitly. </p>
</div></div><!-- contents -->
<address class="footer">
<small>
Generated on Thu Nov 27 2014 13:46:42 for wxWidgets by <a href="http://www.doxygen.org/index.html" target="_new">Doxygen</a> 1.8.2
</small>
</address>
<script src="wxwidgets.js" type="text/javascript"></script>
</div><!-- #page_container -->
</body>
</html>
|