]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/tapp.tex
Applied patch [ 589484 ] wxGenericListCtrl, fix for direct use.
[wxWidgets.git] / docs / latex / wx / tapp.tex
CommitLineData
a660d684
KB
1\section{wxApp overview}\label{wxappoverview}
2
3Classes: \helpref{wxApp}{wxapp}
4
5A wxWindows application does not have a {\it main} procedure; the equivalent is the
6\rtfsp\helpref{OnInit}{wxapponinit} member defined for a class derived from wxApp.\rtfsp
7\rtfsp{\it OnInit} will usually create a top window as a bare minimum.
8
9Unlike in earlier versions of wxWindows, OnInit does not return a frame. Instead it
10returns a boolean value which indicates whether processing should continue (TRUE) or not (FALSE).
11You call \helpref{wxApp::SetTopWindow}{wxappsettopwindow} to let wxWindows know
12about the top window.
13
14Note that the program's command line arguments, represented by {\it
15argc} and {\it argv}, are available from within wxApp member functions.
16
17An application closes by destroying all windows. Because all frames must
18be destroyed for the application to exit, it is advisable to use parent
19frames wherever possible when creating new frames, so that deleting the
20top level frame will automatically delete child frames. The alternative
f4fcc291 21is to explicitly delete child frames in the top-level frame's \helpref{wxCloseEvent}{wxcloseevent}\rtfsp
a660d684
KB
22handler.
23
24In emergencies the \helpref{wxExit}{wxexit} function can be called to kill the
25application.
26
27An example of defining an application follows:
28
29\begin{verbatim}
6e6110ee 30class DerivedApp : public wxApp
a660d684 31{
6e6110ee
VZ
32public:
33 virtual bool OnInit();
a660d684
KB
34};
35
36IMPLEMENT_APP(DerivedApp)
37
6e6110ee 38bool DerivedApp::OnInit()
a660d684
KB
39{
40 wxFrame *the_frame = new wxFrame(NULL, argv[0]);
41 ...
42 SetTopWindow(the_frame);
43
44 return TRUE;
45}
46\end{verbatim}
47
48Note the use of IMPLEMENT\_APP(appClass), which allows wxWindows to dynamically create an instance of the application object
49at the appropriate point in wxWindows initialization. Previous versions of wxWindows used
50to rely on the creation of a global application object, but this is no longer recommended,
51because required global initialization may not have been performed at application object
52construction time.
53
54You can also use DECLARE\_APP(appClass) in a header file to declare the wxGetApp function which returns
55a reference to the application object.
56
1e8724e6
VS
57\subsection{Application shutdown}
58
59\helpref{OnExit}{wxapponexit} is called when the application exits but {\it before}
60wxWindows cleans its internal structures. Your should delete all wxWindows object that
61your created by the time OnExit finishes. In particular, do {\bf not} destroy them
62from application class' destructor!
63
64For example, this code may crash:
65
66\begin{verbatim}
67class MyApp : public wxApp
68{
69 public:
70 wxCHMHelpController m_helpCtrl;
71 ...
72};
73\end{verbatim}
74
75The reason for that is that {\tt m\_helpCtrl} is a member object and is
76thus destroyed from MyApp destructor. But MyApp object is deleted after
77wxWindows structures that wxCHMHelpController depends on were
78uninitialized! The solution is to destroy HelpCtrl in {\it OnExit}:
79
80\begin{verbatim}
81class MyApp : public wxApp
82{
83 public:
84 wxCHMHelpController *m_helpCtrl;
85 ...
86};
87
88bool MyApp::OnInit()
89{
90 ...
91 m_helpCtrl = new wxCHMHelpController;
92 ...
93}
94
95int MyApp::OnExit()
96{
97 delete m_helpCtrl;
98 return 0;
99}
100\end{verbatim}