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