]>
git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/app.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: topic overview
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
11 @page app_overview wxApp overview
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
21 Note that the program's command line arguments, represented by @e argc
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
29 In emergencies the #wxExit function can be called to kill the
30 application however normally the application shuts down automatically,
31 @ref appshutdown_overview.
32 An example of defining an application follows:
35 class DerivedApp : public wxApp
38 virtual bool OnInit();
41 IMPLEMENT_APP(DerivedApp)
43 bool DerivedApp::OnInit()
45 wxFrame *the_frame = new wxFrame(@NULL, ID_MYFRAME, argv[0]);
47 the_frame-Show(@true);
48 SetTopWindow(the_frame);
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
59 You can also use DECLARE_APP(appClass) in a header file to declare the wxGetApp function which returns
60 a reference to the application object. Otherwise you can only use the global
61 @c wxTheApp pointer which is of type @c wxApp *.
63 @ref appshutdown_overview
66 @section wxappshutdownoverview Application shutdown
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
75 other words, you can safely show a dialog from
76 wxApp::OnInit and not be afraid that your application
77 terminates when this dialog -- which is the last top level window for the
80 Another aspect of the application shutdown is #OnExit
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:
88 class MyApp : public wxApp
91 wxCHMHelpController m_helpCtrl;
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
99 uninitialized! The solution is to destroy HelpCtrl in @e OnExit:
102 class MyApp : public wxApp
105 wxCHMHelpController *m_helpCtrl;
112 m_helpCtrl = new wxCHMHelpController;