]> git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/app.h
fixing the topic overviews
[wxWidgets.git] / docs / doxygen / overviews / app.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.h
3 // Purpose: topic overview
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /*!
10
11 @page overview_app wxApp overview
12
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.
16
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
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
22 Note that the program's command line arguments, represented by @e argc
23 and @e argv, are available from within wxApp member functions.
24
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.
31
32 In emergencies the #wxExit function can be called to kill the
33 application however normally the application shuts down automatically,
34 see @ref overview_app_shutdown.
35
36 An example of defining an application follows:
37
38 @code
39 class DerivedApp : public wxApp
40 {
41 public:
42 virtual bool OnInit();
43 };
44
45 IMPLEMENT_APP(DerivedApp)
46
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);
53
54 return @true;
55 }
56 @endcode
57
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
66 @c wxTheApp pointer which is of type @c wxApp *.
67
68
69
70 @section overview_app_shutdown Application shutdown
71
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
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.
82
83 Another aspect of the application shutdown is #OnExit
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
86 created by the time OnExit finishes.
87
88 In particular, do @b not destroy them from application class' destructor!
89 For example, this code may crash:
90
91 @code
92 class MyApp : public wxApp
93 {
94 public:
95 wxCHMHelpController m_helpCtrl;
96 ...
97 };
98 @endcode
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
103 uninitialized! The solution is to destroy HelpCtrl in @e OnExit:
104
105 @code
106 class MyApp : public wxApp
107 {
108 public:
109 wxCHMHelpController *m_helpCtrl;
110 ...
111 };
112
113 bool MyApp::OnInit()
114 {
115 ...
116 m_helpCtrl = new wxCHMHelpController;
117 ...
118 }
119
120 int MyApp::OnExit()
121 {
122 delete m_helpCtrl;
123 return 0;
124 }
125 @endcode
126
127 */
128