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