The wxApp class represents the application itself. It is used to:
You should use the macro IMPLEMENT_APP(appClass) in your application implementation file to tell wxWindows how to create an instance of your application class.
Use DECLARE_APP(appClass) in a header file if you want the wxGetApp function (which returns a reference to your application object) to be visible to other files.
Derived from
Include files
<wx/app.h>
See also
Members
wxApp::wxApp
wxApp::~wxApp
wxApp::argc
wxApp::argv
wxApp::CreateLogTarget
wxApp::Dispatch
wxApp::FilterEvent
wxApp::GetAppName
wxApp::GetAuto3D
wxApp::GetClassName
wxApp::GetExitOnFrameDelete
wxApp::GetTopWindow
wxApp::GetUseBestVisual
wxApp::GetVendorName
wxApp::ExitMainLoop
wxApp::Initialized
wxApp::MainLoop
wxApp::OnAssert
wxApp::OnExit
wxApp::OnCmdLineError
wxApp::OnCmdLineHelp
wxApp::OnCmdLineParsed
wxApp::OnFatalException
wxApp::OnInit
wxApp::OnInitCmdLine
wxApp::OnQueryEndSession
wxApp::ProcessMessage
wxApp::Pending
wxApp::SendIdleEvents
wxApp::SetAppName
wxApp::SetAuto3D
wxApp::SetClassName
wxApp::SetExitOnFrameDelete
wxApp::SetTopWindow
wxApp::SetVendorName
wxApp::SetUseBestVisual
wxApp::Yield
void wxApp()
Constructor. Called implicitly with a definition of a wxApp object.
void ~wxApp()
Destructor. Will be called implicitly on program exit if the wxApp object is created on the stack.
int argc
Number of command line arguments (after environment-specific processing).
char ** argv
Command line arguments (after environment-specific processing).
virtual wxLog* CreateLogTarget()
Creates a wxLog class for the application to use for logging errors. The default implementation returns a new wxLogGui class.
See also
void Dispatch()
Dispatches the next event in the windowing system event queue.
This can be used for programming event loops, e.g.
while (app.Pending()) Dispatch();See also
int FilterEvent(wxEvent& event)
This function is called before processing any event and allows the application to preempt the processing of some events. If this method returns -1 the event is processed normally, otherwise either TRUE or FALSE should be returned and the event processing stops immediately considering that the event had been already processed (for the former return value) or that it is not going to be processed at all (for the latter one).
wxString GetAppName() const
Returns the application name.
Remarks
wxWindows sets this to a reasonable default before calling wxApp::OnInit, but the application can reset it at will.
bool GetAuto3D() const
Returns TRUE if 3D control mode is on, FALSE otherwise.
See also
wxString GetClassName() const
Gets the class name of the application. The class name may be used in a platform specific manner to refer to the application.
See also
bool GetExitOnFrameDelete() const
Returns TRUE if the application will exit when the top-level window is deleted, FALSE otherwise.
See also
wxApp::SetExitOnFrameDelete,
wxApp shutdown overview
virtual wxWindow * GetTopWindow() const
Returns a pointer to the top window.
Remarks
If the top window hasn't been set using wxApp::SetTopWindow, this function will find the first top-level window (frame or dialog) and return that.
See also
bool GetUseBestVisual() const
Returns TRUE if the application will use the best visual on systems that support different visuals, FALSE otherwise.
See also
wxString GetVendorName() const
Returns the application's vendor name.
void ExitMainLoop()
Call this to explicitly exit the main message (event) loop. You should normally exit the main loop (and the application) by deleting the top window.
bool Initialized()
Returns TRUE if the application has been initialized (i.e. if wxApp::OnInit has returned successfully). This can be useful for error message routines to determine which method of output is best for the current state of the program (some windowing systems may not like dialogs to pop up before the main loop has been entered).
int MainLoop()
Called by wxWindows on creation of the application. Override this if you wish to provide your own (environment-dependent) main loop.
Return value
Returns 0 under X, and the wParam of the WM_QUIT message under Windows.
void OnAssert(const wxChar *file, int line, const wxChar *cond, const wxChar *msg)
This function is called when an assert failure occurs, i.e. the condition specified in wxASSERT macro evaluated to FALSE. It is only called in debug mode (when __WXDEBUG__ is defined) as asserts are not left in the release code at all.
The base class version show the default assert failure dialog box proposing to the user to stop the program, continue or ignore all subsequent asserts.
Parameters
file
line
cond
msg
int OnExit()
Provide this member function for any processing which needs to be done as the application is about to exit. OnExit is called after destroying all application windows and controls, but before wxWindows cleanup.
bool OnCmdLineError(wxCmdLineParser& parser)
Called when command line parsing fails (i.e. an incorrect command line option was specified by the user). The default behaviour is to show the program usage text and abort the program.
Return TRUE to continue normal execution or FALSE to return FALSE from OnInit thus terminating the program.
See also
bool OnCmdLineHelp(wxCmdLineParser& parser)
Called when the help option (--help) was specified on the command line. The default behaviour is to show the program usage text and abort the program.
Return TRUE to continue normal execution or FALSE to return FALSE from OnInit thus terminating the program.
See also
bool OnCmdLineParsed(wxCmdLineParser& parser)
Called after the command line had been successfully parsed. You may override this method to test for the values of the various parameters which could be set from the command line.
Don't forget to call the base class version unless you want to suppress processing of the standard command line options.
Return TRUE to continue normal execution or FALSE to return FALSE from OnInit thus terminating the program.
See also
void OnFatalException()
This function may be called if something fatal happens: an unhandled exception under Win32 or a a fatal signal under Unix, for example. However, this will not happen by default: you have to explicitly call wxHandleFatalExceptions to enable this.
Generally speaking, this function should only show a message to the user and return. You may attempt to save unsaved data but this is not guaranteed to work and, in fact, probably won't.
See also
bool OnInit()
This must be provided by the application, and will usually create the application's main window, optionally calling wxApp::SetTopWindow.
Notice that if you want to to use the command line processing provided by wxWindows you have to call the base class version in the derived class OnInit().
Return TRUE to continue processing, FALSE to exit the application.
void OnInitCmdLine(wxCmdLineParser& parser)
Called from OnInit and may be used to initialize the parser with the command line options for this application. The base class versions adds support for a few standard options only.
void OnQueryEndSession(wxCloseEvent& event)
This is an event handler function called when the operating system or GUI session is about to close down. Typically, an application will try to save unsaved documents at this point.
If wxCloseEvent::CanVeto returns TRUE, the application is allowed to veto the shutdown by calling wxCloseEvent::Veto. The application might veto the shutdown after prompting for documents to be saved, and the user has cancelled the save.
Use the EVT_QUERY_END_SESSION event table macro to handle query end session events.
You should check whether the application is forcing the deletion of the window using wxCloseEvent::GetForce. If this is TRUE, destroy the window using wxWindow::Destroy. If not, it is up to you whether you respond by destroying the window.
The default handler calls wxWindow::Close on the top-level window, and vetoes the shutdown if Close returns FALSE. This will be sufficient for many applications.
Remarks
Under X, OnQueryEndSession is called in response to the 'save session' event.
Under Windows, OnQueryEndSession is called in response to the WM_QUERYENDSESSION message.
See also
bool ProcessMessage(WXMSG *msg)
Windows-only function for processing a message. This function is called from the main message loop, checking for windows that may wish to process it. The function returns TRUE if the message was processed, FALSE otherwise. If you use wxWindows with another class library with its own message loop, you should make sure that this function is called to allow wxWindows to receive messages. For example, to allow co-existence with the Microsoft Foundation Classes, override the PreTranslateMessage function:
// Provide wxWindows message loop compatibility BOOL CTheApp::PreTranslateMessage(MSG *msg) { if (wxTheApp && wxTheApp->ProcessMessage((WXMSW *)msg)) return TRUE; else return CWinApp::PreTranslateMessage(msg); }
bool Pending()
Returns TRUE if unprocessed events are in the window system event queue.
See also
bool SendIdleEvents()
Sends idle events to all top-level windows.
bool SendIdleEvents(wxWindow* win)
Sends idle events to a window and its children.
Remarks
These functions poll the top-level windows, and their children, for idle event processing. If TRUE is returned, more OnIdle processing is requested by one or more window.
See also
void SetAppName(const wxString& name)
Sets the name of the application. The name may be used in dialogs (for example by the document/view framework). A default name is set by wxWindows.
See also
void SetAuto3D(const bool auto3D)
Switches automatic 3D controls on or off.
Parameters
auto3D
Remarks
This has an effect on Windows only.
See also
void SetClassName(const wxString& name)
Sets the class name of the application. This may be used in a platform specific manner to refer to the application.
See also
void SetExitOnFrameDelete(bool flag)
Allows the programmer to specify whether the application will exit when the top-level frame is deleted.
Parameters
flag
See also
wxApp::GetExitOnFrameDelete,
wxApp shutdown overview
void SetTopWindow(wxWindow* window)
Sets the 'top' window. You can call this from within wxApp::OnInit to let wxWindows know which is the main window. You don't have to set the top window; it is only a convenience so that (for example) certain dialogs without parents can use a specific window as the top window. If no top window is specified by the application, wxWindows just uses the first frame or dialog in its top-level window list, when it needs to use the top window.
Parameters
window
See also
wxApp::GetTopWindow, wxApp::OnInit
void SetVendorName(const wxString& name)
Sets the name of application's vendor. The name will be used in registry access. A default name is set by wxWindows.
See also
void SetUseBestVisual(bool flag)
Allows the programmer to specify whether the application will use the best visual on systems that support several visual on the same display. This is typically the case under Solaris and IRIX, where the default visual is only 8-bit whereas certain applications are supposed to run in TrueColour mode.
Note that this function has to be called in the constructor of the wxApp instance and won't have any effect when called later on.
This function currently only has effect under GTK.
Parameters
flag
bool Yield(bool onlyIfNeeded = FALSE)
Yields control to pending messages in the windowing system. This can be useful, for example, when a time-consuming process writes to a text window. Without an occasional yield, the text window will not be updated properly, and on systems with cooperative multitasking, such as Windows 3.1 other processes will not respond.
Caution should be exercised, however, since yielding may allow the user to perform actions which are not compatible with the current task. Disabling menu items or whole menus during processing can avoid unwanted reentrance of code: see ::wxSafeYield for a better function.
Note that Yield() will not flush the message logs. This is intentional as calling Yield() is usually done to quickly update the screen and popping up a message box dialog may be undesirable. If you do wish to flush the log messages immediately (otherwise it will be done during the next idle loop iteration), call wxLog::FlushActive.
Calling Yield() recursively is normally an error and an assert failure is raised in debug build if such situation is detected. However if the the onlyIfNeeded parameter is TRUE, the method will just silently return FALSE instead.