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