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