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