]>
git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/app.h
14e941b15586ac538f070be39be74f504a8b5569
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: topic overview
4 // Author: wxWidgets team
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
11 @page overview_app wxApp Overview
15 A wxWidgets application does not have a @e main procedure; the equivalent is
16 the wxApp::OnInit member defined for a class derived from wxApp.
18 @e OnInit will usually create a top window as a bare minimum. Unlike in earlier
19 versions of wxWidgets, OnInit does not return a frame. Instead it returns a
20 boolean value which indicates whether processing should continue (@true) or not
23 Note that the program's command line arguments, represented by @e argc and
24 @e argv, are available from within wxApp member functions.
26 An application closes by destroying all windows. Because all frames must be
27 destroyed for the application to exit, it is advisable to use parent frames
28 wherever possible when creating new frames, so that deleting the top level
29 frame will automatically delete child frames. The alternative is to explicitly
30 delete child frames in the top-level frame's wxCloseEvent handler.
32 In emergencies the wxExit function can be called to kill the application
33 however normally the application shuts down automatically, see
34 @ref overview_app_shutdown.
36 An example of defining an application follows:
39 class DerivedApp : public wxApp
42 virtual bool OnInit();
45 IMPLEMENT_APP(DerivedApp)
47 bool DerivedApp::OnInit()
49 wxFrame *the_frame = new wxFrame(NULL, ID_MYFRAME, argv[0]);
51 the_frame->Show(true);
57 Note the use of IMPLEMENT_APP(appClass), which allows wxWidgets to dynamically
58 create an instance of the application object at the appropriate point in
59 wxWidgets initialization. Previous versions of wxWidgets used to rely on the
60 creation of a global application object, but this is no longer recommended,
61 because required global initialization may not have been performed at
62 application object construction time.
64 You can also use DECLARE_APP(appClass) in a header file to declare the wxGetApp
65 function which returns a reference to the application object. Otherwise you can
66 only use the global @c wxTheApp pointer which is of type @c wxApp*.
70 @section overview_app_shutdown Application Shutdown
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 wxWindow::Close() in response to the "Exit" menu command if your program
75 has a single top level window. If this behaviour is not desirable
76 wxApp::SetExitOnFrameDelete can be called to change it.
78 Note that such logic doesn't apply for the windows shown before the program
79 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
81 dialog -- which is the last top level window for the moment -- is closed.
83 Another aspect of the application shutdown is wxApp::OnExit which is called
84 when the application exits but @e before wxWidgets cleans up its internal
85 structures. You should delete all wxWidgets object that you created by the time
88 In particular, do @b not destroy them from application class' destructor! For
89 example, this code may crash:
92 class MyApp : public wxApp
95 wxCHMHelpController m_helpCtrl;
100 The reason for that is that @c m_helpCtrl is a member object and is thus
101 destroyed from MyApp destructor. But MyApp object is deleted after wxWidgets
102 structures that wxCHMHelpController depends on were uninitialized! The solution
103 is to destroy HelpCtrl in @e OnExit:
106 class MyApp : public wxApp
109 wxCHMHelpController *m_helpCtrl;
116 m_helpCtrl = new wxCHMHelpController;