app.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: app.h
  3. // Purpose: topic overview
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @page overview_app wxApp Overview
  9. @tableofcontents
  10. A wxWidgets application does not have a @e main procedure; the equivalent is
  11. the wxApp::OnInit member defined for a class derived from wxApp.
  12. @e OnInit will usually create a top window as a bare minimum. Unlike in earlier
  13. versions of wxWidgets, OnInit does not return a frame. Instead it returns a
  14. boolean value which indicates whether processing should continue (@true) or not
  15. (@false).
  16. Note that the program's command line arguments, represented by @e argc and
  17. @e argv, are available from within wxApp member functions.
  18. An application closes by destroying all windows. Because all frames must be
  19. destroyed for the application to exit, it is advisable to use parent frames
  20. wherever possible when creating new frames, so that deleting the top level
  21. frame will automatically delete child frames. The alternative is to explicitly
  22. delete child frames in the top-level frame's wxCloseEvent handler.
  23. In emergencies the wxExit function can be called to kill the application
  24. however normally the application shuts down automatically, see
  25. @ref overview_app_shutdown.
  26. An example of defining an application follows:
  27. @code
  28. class DerivedApp : public wxApp
  29. {
  30. public:
  31. virtual bool OnInit();
  32. };
  33. IMPLEMENT_APP(DerivedApp)
  34. bool DerivedApp::OnInit()
  35. {
  36. wxFrame *the_frame = new wxFrame(NULL, ID_MYFRAME, argv[0]);
  37. ...
  38. the_frame->Show(true);
  39. return true;
  40. }
  41. @endcode
  42. Note the use of IMPLEMENT_APP(appClass), which allows wxWidgets to dynamically
  43. create an instance of the application object at the appropriate point in
  44. wxWidgets initialization. Previous versions of wxWidgets used to rely on the
  45. creation of a global application object, but this is no longer recommended,
  46. because required global initialization may not have been performed at
  47. application object construction time.
  48. You can also use DECLARE_APP(appClass) in a header file to declare the wxGetApp
  49. function which returns a reference to the application object. Otherwise you can
  50. only use the global @c wxTheApp pointer which is of type @c wxApp*.
  51. @section overview_app_shutdown Application Shutdown
  52. The application normally shuts down when the last of its top level windows is
  53. closed. This is normally the expected behaviour and means that it is enough to
  54. call wxWindow::Close() in response to the "Exit" menu command if your program
  55. has a single top level window. If this behaviour is not desirable
  56. wxApp::SetExitOnFrameDelete can be called to change it.
  57. Note that such logic doesn't apply for the windows shown before the program
  58. enters the main loop: in other words, you can safely show a dialog from
  59. wxApp::OnInit and not be afraid that your application terminates when this
  60. dialog -- which is the last top level window for the moment -- is closed.
  61. Another aspect of the application shutdown is wxApp::OnExit which is called
  62. when the application exits but @e before wxWidgets cleans up its internal
  63. structures. You should delete all wxWidgets object that you created by the time
  64. OnExit finishes.
  65. In particular, do @b not destroy them from application class' destructor! For
  66. example, this code may crash:
  67. @code
  68. class MyApp : public wxApp
  69. {
  70. public:
  71. wxCHMHelpController m_helpCtrl;
  72. ...
  73. };
  74. @endcode
  75. The reason for that is that @c m_helpCtrl is a member object and is thus
  76. destroyed from MyApp destructor. But MyApp object is deleted after wxWidgets
  77. structures that wxCHMHelpController depends on were uninitialized! The solution
  78. is to destroy HelpCtrl in @e OnExit:
  79. @code
  80. class MyApp : public wxApp
  81. {
  82. public:
  83. wxCHMHelpController *m_helpCtrl;
  84. ...
  85. };
  86. bool MyApp::OnInit()
  87. {
  88. ...
  89. m_helpCtrl = new wxCHMHelpController;
  90. ...
  91. }
  92. int MyApp::OnExit()
  93. {
  94. delete m_helpCtrl;
  95. return 0;
  96. }
  97. @endcode
  98. */