1998-05-20 10:25:30 -04:00
|
|
|
\section{wxApp overview}\label{wxappoverview}
|
|
|
|
|
|
|
|
Classes: \helpref{wxApp}{wxapp}
|
|
|
|
|
2004-05-04 04:27:20 -04:00
|
|
|
A wxWidgets application does not have a {\it main} procedure; the equivalent is the
|
1998-05-20 10:25:30 -04:00
|
|
|
\rtfsp\helpref{OnInit}{wxapponinit} member defined for a class derived from wxApp.\rtfsp
|
|
|
|
\rtfsp{\it OnInit} will usually create a top window as a bare minimum.
|
|
|
|
|
2004-05-04 04:27:20 -04:00
|
|
|
Unlike in earlier versions of wxWidgets, OnInit does not return a frame. Instead it
|
2003-01-17 19:16:34 -05:00
|
|
|
returns a boolean value which indicates whether processing should continue (true) or not (false).
|
2004-05-04 04:27:20 -04:00
|
|
|
You call \helpref{wxApp::SetTopWindow}{wxappsettopwindow} to let wxWidgets know
|
1998-05-20 10:25:30 -04:00
|
|
|
about the top window.
|
|
|
|
|
2002-08-01 15:54:55 -04:00
|
|
|
Note that the program's command line arguments, represented by {\it argc}
|
|
|
|
and {\it argv}, are available from within wxApp member functions.
|
1998-05-20 10:25:30 -04:00
|
|
|
|
|
|
|
An application closes by destroying all windows. Because all frames must
|
|
|
|
be destroyed for the application to exit, it is advisable to use parent
|
|
|
|
frames wherever possible when creating new frames, so that deleting the
|
|
|
|
top level frame will automatically delete child frames. The alternative
|
2002-04-08 12:46:27 -04:00
|
|
|
is to explicitly delete child frames in the top-level frame's \helpref{wxCloseEvent}{wxcloseevent}\rtfsp
|
1998-05-20 10:25:30 -04:00
|
|
|
handler.
|
|
|
|
|
|
|
|
In emergencies the \helpref{wxExit}{wxexit} function can be called to kill the
|
2004-09-03 22:44:43 -04:00
|
|
|
application however normally the application shuts down automatically,
|
2002-08-15 16:42:07 -04:00
|
|
|
\helpref{see below}{wxappshutdownoverview}.
|
1998-05-20 10:25:30 -04:00
|
|
|
|
|
|
|
An example of defining an application follows:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
1999-01-02 18:02:30 -05:00
|
|
|
class DerivedApp : public wxApp
|
1998-05-20 10:25:30 -04:00
|
|
|
{
|
1999-01-02 18:02:30 -05:00
|
|
|
public:
|
|
|
|
virtual bool OnInit();
|
1998-05-20 10:25:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
IMPLEMENT_APP(DerivedApp)
|
|
|
|
|
1999-01-02 18:02:30 -05:00
|
|
|
bool DerivedApp::OnInit()
|
1998-05-20 10:25:30 -04:00
|
|
|
{
|
2002-08-01 15:54:55 -04:00
|
|
|
wxFrame *the_frame = new wxFrame(NULL, ID_MYFRAME, argv[0]);
|
1998-05-20 10:25:30 -04:00
|
|
|
...
|
2003-01-17 19:16:34 -05:00
|
|
|
the_frame->Show(true);
|
1998-05-20 10:25:30 -04:00
|
|
|
SetTopWindow(the_frame);
|
|
|
|
|
2003-01-17 19:16:34 -05:00
|
|
|
return true;
|
1998-05-20 10:25:30 -04:00
|
|
|
}
|
|
|
|
\end{verbatim}
|
|
|
|
|
2004-05-04 04:27:20 -04:00
|
|
|
Note the use of IMPLEMENT\_APP(appClass), which allows wxWidgets to dynamically create an instance of the application object
|
|
|
|
at the appropriate point in wxWidgets initialization. Previous versions of wxWidgets used
|
1998-05-20 10:25:30 -04:00
|
|
|
to rely on the creation of a global application object, but this is no longer recommended,
|
|
|
|
because required global initialization may not have been performed at application object
|
|
|
|
construction time.
|
|
|
|
|
|
|
|
You can also use DECLARE\_APP(appClass) in a header file to declare the wxGetApp function which returns
|
2003-11-24 13:37:32 -05:00
|
|
|
a reference to the application object. Otherwise you can only use the global
|
|
|
|
\texttt{wxTheApp} pointer which is of type \texttt{wxApp *}.
|
|
|
|
|
1998-05-20 10:25:30 -04:00
|
|
|
|
2002-08-15 16:42:07 -04:00
|
|
|
\subsection{Application shutdown}\label{wxappshutdownoverview}
|
|
|
|
|
|
|
|
The application normally shuts down when the last of its top level windows is
|
|
|
|
closed. This is normally the expected behaviour and means that it is enough to
|
|
|
|
call \helpref{Close()}{wxwindowclose} in response to the {\tt "Exit"} menu
|
|
|
|
command if your program has a single top level window. If this behaviour is not
|
|
|
|
desirable \helpref{wxApp::SetExitOnFrameDelete}{wxappsetexitonframedelete} can
|
2004-05-04 04:27:20 -04:00
|
|
|
be called to change it. Note that starting from wxWidgets 2.3.3 such logic
|
2002-08-15 16:42:07 -04:00
|
|
|
doesn't apply for the windows shown before the program enters the main loop: in
|
|
|
|
other words, you can safely show a dialog from
|
|
|
|
\helpref{wxApp::OnInit}{wxapponinit} and not be afraid that your application
|
|
|
|
terminates when this dialog -- which is the last top level window for the
|
|
|
|
moment -- is closed.
|
|
|
|
|
|
|
|
|
2004-09-03 22:44:43 -04:00
|
|
|
Another aspect of the application shutdown is \helpref{OnExit}{wxapponexit}
|
2004-05-04 04:27:20 -04:00
|
|
|
which is called when the application exits but {\it before} wxWidgets cleans up
|
2004-09-03 22:44:43 -04:00
|
|
|
its internal structures. You should delete all wxWidgets object that you
|
2002-08-15 16:42:07 -04:00
|
|
|
created by the time OnExit finishes. In particular, do {\bf not} destroy them
|
2002-07-27 14:14:47 -04:00
|
|
|
from application class' destructor!
|
|
|
|
|
|
|
|
For example, this code may crash:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
class MyApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxCHMHelpController m_helpCtrl;
|
|
|
|
...
|
|
|
|
};
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
The reason for that is that {\tt m\_helpCtrl} is a member object and is
|
|
|
|
thus destroyed from MyApp destructor. But MyApp object is deleted after
|
2004-05-04 04:27:20 -04:00
|
|
|
wxWidgets structures that wxCHMHelpController depends on were
|
2002-07-27 14:14:47 -04:00
|
|
|
uninitialized! The solution is to destroy HelpCtrl in {\it OnExit}:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
class MyApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxCHMHelpController *m_helpCtrl;
|
|
|
|
...
|
|
|
|
};
|
|
|
|
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
|
|
|
...
|
|
|
|
m_helpCtrl = new wxCHMHelpController;
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
int MyApp::OnExit()
|
|
|
|
{
|
|
|
|
delete m_helpCtrl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
\end{verbatim}
|