windowdeletion.h 4.8 KB

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