]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/tapp.tex
Applied patch [ 587450 ] Scoped Smart pointers and docs
[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
90a1a975
JS
14Note that the program's command line arguments, represented by {\it argc}
15and {\it argv}, are available from within wxApp member functions.
a660d684
KB
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 39{
90a1a975 40 wxFrame *the_frame = new wxFrame(NULL, ID_MYFRAME, argv[0]);
a660d684 41 ...
90a1a975 42 the_frame->Show(TRUE);
a660d684
KB
43 SetTopWindow(the_frame);
44
45 return TRUE;
46}
47\end{verbatim}
48
49Note the use of IMPLEMENT\_APP(appClass), which allows wxWindows to dynamically create an instance of the application object
50at the appropriate point in wxWindows initialization. Previous versions of wxWindows used
51to rely on the creation of a global application object, but this is no longer recommended,
52because required global initialization may not have been performed at application object
53construction time.
54
55You can also use DECLARE\_APP(appClass) in a header file to declare the wxGetApp function which returns
56a reference to the application object.
57
1e8724e6
VS
58\subsection{Application shutdown}
59
60\helpref{OnExit}{wxapponexit} is called when the application exits but {\it before}
61wxWindows cleans its internal structures. Your should delete all wxWindows object that
62your created by the time OnExit finishes. In particular, do {\bf not} destroy them
63from application class' destructor!
64
65For example, this code may crash:
66
67\begin{verbatim}
68class MyApp : public wxApp
69{
70 public:
71 wxCHMHelpController m_helpCtrl;
72 ...
73};
74\end{verbatim}
75
76The reason for that is that {\tt m\_helpCtrl} is a member object and is
77thus destroyed from MyApp destructor. But MyApp object is deleted after
78wxWindows structures that wxCHMHelpController depends on were
79uninitialized! The solution is to destroy HelpCtrl in {\it OnExit}:
80
81\begin{verbatim}
82class MyApp : public wxApp
83{
84 public:
85 wxCHMHelpController *m_helpCtrl;
86 ...
87};
88
89bool MyApp::OnInit()
90{
91 ...
92 m_helpCtrl = new wxCHMHelpController;
93 ...
94}
95
96int MyApp::OnExit()
97{
98 delete m_helpCtrl;
99 return 0;
100}
101\end{verbatim}