# wxAppConsole Class Reference
[Application and Process Management]

`#include </home/work/projects/wx/2.9/wx/interface/wx/app.h>`

Inheritance diagram for wxAppConsole:
[legend]

## Detailed Description

This class is essential for writing console-only or hybrid apps without having to define `wxUSE_GUI=0`.

It is used to:

- set and get application-wide properties (see wxAppConsole::CreateTraits and wxAppConsole::SetXXX functions)
- implement the windowing system message or event loop: events in fact are supported even in console-mode applications (see wxAppConsole::HandleEvent and wxAppConsole::ProcessPendingEvents);
- initiate application processing via wxApp::OnInit;
- allow default processing of events not handled by other objects in the application (see wxAppConsole::FilterEvent)
- implement Apple-specific event handlers (see wxAppConsole::MacXXX functions)

You should use the macro wxIMPLEMENT_APP(appClass) in your application implementation file to tell wxWidgets how to create an instance of your application class.

Use wxDECLARE_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.

##

Library: wxBase
Category: Application and Process Management

See also:
  wxApp Overview, wxApp, wxAppTraits, wxEventLoopBase

## Public Member Functions

virtual | ~wxAppConsole ()
| Destructor.
Event-handling
Note that you should look at wxEvtLoopBase for more event-processing documentation.
virtual int | MainLoop ()
| Called by wxWidgets on creation of the application.
virtual void | ExitMainLoop ()
| Call this to explicitly exit the main message (event) loop.
virtual int | FilterEvent (wxEvent &event)
| This function is called before processing any event and allows the application to preempt the processing of some events.
wxEventLoopBase * | GetMainLoop () const
| Returns the main event loop instance, i.e.
virtual void | HandleEvent (wxEvtHandler *handler, wxEventFunction func, wxEvent &event) const
| This function simply invokes the given method func of the specified event handler handler with the event as parameter.
virtual bool | UsesEventLoop () const
| Returns true if the application is using an event loop.
Pending events
Pending events are handled by wxAppConsole rather than wxEventLoopBase to allow queuing of events even when there's no event loop (e.g. in wxAppConsole::OnInit).
virtual void | ProcessPendingEvents ()
| Process all pending events; it is necessary to call this function to process events posted with wxEvtHandler::QueueEvent or wxEvtHandler::AddPendingEvent.
void | DeletePendingEvents ()
| Deletes the pending events of all wxEvtHandlers of this application.
bool | HasPendingEvents () const
| Returns true if there are pending events on the internal pending event list.
void | SuspendProcessingOfPendingEvents ()
| Temporary suspends processing of the pending events.
void | ResumeProcessingOfPendingEvents ()
| Resume processing of the pending events previously stopped because of a call to SuspendProcessingOfPendingEvents().
void | ScheduleForDestruction (wxObject *object)
| Delayed objects destruction.
bool | IsScheduledForDestruction (wxObject *object) const
| Check if the object had been scheduled for destruction with ScheduleForDestruction().
Callbacks for application-wide "events"
virtual void | OnAssertFailure (const wxChar *file, int line, const wxChar *func, const wxChar *cond, const wxChar *msg)
| This function is called when an assert failure occurs, i.e.
virtual bool | OnCmdLineError (wxCmdLineParser &parser)
| Called when command line parsing fails (i.e.
virtual bool | OnCmdLineHelp (wxCmdLineParser &parser)
| Called when the help option (`--help`) was specified on the command line.
virtual bool | OnCmdLineParsed (wxCmdLineParser &parser)
| Called after the command line had been successfully parsed.
virtual void | OnEventLoopEnter (wxEventLoopBase *loop)
| Called by wxEventLoopBase::SetActive(): you can override this function and put here the code which needs an active event loop.
virtual void | OnEventLoopExit (wxEventLoopBase *loop)
| Called by wxEventLoopBase::OnExit() for each event loop which is exited.
virtual bool | OnExceptionInMainLoop ()
| This function is called if an unhandled exception occurs inside the main application event loop.
virtual int | OnExit ()
| Override this member function for any processing which needs to be done as the application is about to exit.
virtual void | OnFatalException ()
| This function may be called if something fatal happens: an unhandled exception under Win32 or a fatal signal under Unix, for example.
virtual bool | OnInit ()
| This must be provided by the application, and will usually create the application's main window, optionally calling SetTopWindow().
virtual void | OnInitCmdLine (wxCmdLineParser &parser)
| Called from OnInit() and may be used to initialize the parser with the command line options for this application.
virtual int | OnRun ()
| This virtual function is where the execution of a program written in wxWidgets starts.
virtual void | OnUnhandledException ()
| This function is called when an unhandled C++ exception occurs in user code called by wxWidgets.
Application informations
wxString | GetAppDisplayName () const
| Returns the user-readable application name.
wxString | GetAppName () const
| Returns the application name.
wxString | GetClassName () const
| Gets the class name of the application.
wxAppTraits * | GetTraits ()
| Returns a pointer to the wxAppTraits object for the application.
const wxString & | GetVendorDisplayName () const
| Returns the user-readable vendor name.
const wxString & | GetVendorName () const
| Returns the application's vendor name.
void | SetAppDisplayName (const wxString &name)
| Set the application name to be used in the user-visible places such as window titles.
void | SetAppName (const wxString &name)
| Sets the name of the application.
void | SetClassName (const wxString &name)
| Sets the class name of the application.
void | SetVendorDisplayName (const wxString &name)
| Set the vendor name to be used in the user-visible places.
void | SetVendorName (const wxString &name)
| Sets the name of application's vendor.

## Static Public Member Functions

static void | SetInstance (wxAppConsole *app)
| Allows external code to modify global wxTheApp, but you should really know what you're doing if you call it.
static wxAppConsole * | GetInstance ()
| Returns the one and only global application object.
static bool | IsMainLoopRunning ()
| Returns true if the main event loop is currently running, i.e.

## Public Attributes

int | argc
| Number of command line arguments (after environment-specific processing).
wxChar ** | argv
| Command line arguments (after environment-specific processing).

## Protected Member Functions

virtual wxAppTraits * | CreateTraits ()
| Creates the wxAppTraits object when GetTraits() needs it for the first time.

List of all members.

## Constructor & Destructor Documentation

virtual wxAppConsole::~wxAppConsole | ( |  | ) | `[virtual]`

Destructor.

## Member Function Documentation

virtual wxAppTraits* wxAppConsole::CreateTraits | ( |  | ) | `[protected, virtual]`

Creates the wxAppTraits object when GetTraits() needs it for the first time.

See also:
  wxAppTraits

void wxAppConsole::DeletePendingEvents | ( |  | ) |

Deletes the pending events of all wxEvtHandlers of this application.

See wxEvtHandler::DeletePendingEvents() for warnings about deleting the pending events.

Reimplemented from wxEvtHandler.

virtual void wxAppConsole::ExitMainLoop | ( |  | ) | `[virtual]`

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.

This function simply calls wxEvtLoopBase::Exit() on the active loop.

virtual int wxAppConsole::FilterEvent | ( | wxEvent & | event | ) | `[virtual]`

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 wxAppConsole::GetAppDisplayName | ( |  | ) | const

Returns the user-readable application name.

The difference between this string and the one returned by GetAppName() is that this one is meant to be shown to the user and so should be used for the window titles, page headers and so on while the other one should be only used internally, e.g. for the file names or configuration file keys.

If the application name for display had been previously set by SetAppDisplayName(), it will be returned by this function. Otherwise, if SetAppName() had been called its value will be returned; also as is. Finally if none was called, this function returns the program name capitalized using wxString::Capitalize().

Since:
  2.9.0

wxString wxAppConsole::GetAppName | ( |  | ) | const

Returns the application name.

If SetAppName() had been called, returns the string passed to it. Otherwise returns the program name, i.e. the value of `argv`[0] passed to the `main()` function.

See also:
  GetAppDisplayName()

wxString wxAppConsole::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:
  SetClassName()

static wxAppConsole* wxAppConsole::GetInstance | ( |  | ) | `[static]`

Returns the one and only global application object.

Usually wxTheApp is used instead.

See also:
  SetInstance()

wxEventLoopBase* wxAppConsole::GetMainLoop | ( |  | ) | const

Returns the main event loop instance, i.e.

the event loop which is started by OnRun() and which dispatches all events sent from the native toolkit to the application (except when new event loops are temporarily set-up). The returned value maybe NULL. Put initialization code which needs a non-NULL main event loop into OnEventLoopEnter().

wxAppTraits* wxAppConsole::GetTraits | ( |  | ) |

Returns a pointer to the wxAppTraits object for the application.

If you want to customize the wxAppTraits object, you must override the CreateTraits() function.

const wxString& wxAppConsole::GetVendorDisplayName | ( |  | ) | const

Returns the user-readable vendor name.

The difference between this string and the one returned by GetVendorName() is that this one is meant to be shown to the user and so should be used for the window titles, page headers and so on while the other one should be only used internally, e.g. for the file names or configuration file keys.

By default, returns the same string as GetVendorName().

Since:
  2.9.0

const wxString& wxAppConsole::GetVendorName | ( |  | ) | const

Returns the application's vendor name.

virtual void wxAppConsole::HandleEvent | ( | wxEvtHandler * | handler,
|  | wxEventFunction | func,
|  | wxEvent & | event |
| ) |  |  | const`[virtual]`

This function simply invokes the given method func of the specified event handler handler with the event as parameter.

It exists solely to allow to catch the C++ exceptions which could be thrown by all event handlers in the application in one place: if you want to do this, override this function in your wxApp-derived class and add try/catch clause(s) to it.

bool wxAppConsole::HasPendingEvents | ( |  | ) | const

Returns true if there are pending events on the internal pending event list.

Whenever wxEvtHandler::QueueEvent or wxEvtHandler::AddPendingEvent() are called (not only for wxApp itself, but for any event handler of the application!), the internal wxApp's list of handlers with pending events is updated and this function will return true.

static bool wxAppConsole::IsMainLoopRunning | ( |  | ) | `[static]`

Returns true if the main event loop is currently running, i.e.

if the application is inside OnRun().

This can be useful to test whether events can be dispatched. For example, if this function returns false, non-blocking sockets cannot be used because the events from them would never be processed.

bool wxAppConsole::IsScheduledForDestruction | ( | wxObject * | object | ) | const

Check if the object had been scheduled for destruction with ScheduleForDestruction().

This function may be useful as an optimization to avoid doing something with an object which will be soon destroyed in any case.

virtual int wxAppConsole::MainLoop | ( |  | ) | `[virtual]`

Called by wxWidgets on creation of the application.

Override this if you wish to provide your own (environment-dependent) main loop.

Returns:
  0 under X, and the wParam of the WM_QUIT message under Windows.

virtual void wxAppConsole::OnAssertFailure | ( | const wxChar * | file,
|  | int | line,
|  | const wxChar * | func,
|  | const wxChar * | cond,
|  | const wxChar * | msg |
| ) |  |  | `[virtual]`

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 shows the default assert failure dialog box proposing to the user to stop the program, continue or ignore all subsequent asserts.

Parameters:

  | file | the name of the source file where the assert occurred
  | line | the line number in this file where the assert occurred
  | func | the name of the function where the assert occurred, may be empty if the compiler doesn't support C99 __FUNCTION__
  | cond | the condition of the failed assert in text form
  | msg | the message specified as argument to wxASSERT_MSG or wxFAIL_MSG, will be NULL if just wxASSERT or wxFAIL was used

virtual bool wxAppConsole::OnCmdLineError | ( | wxCmdLineParser & | parser | ) | `[virtual]`

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:
  OnInitCmdLine()

virtual bool wxAppConsole::OnCmdLineHelp | ( | wxCmdLineParser & | parser | ) | `[virtual]`

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:
  OnInitCmdLine()

virtual bool wxAppConsole::OnCmdLineParsed | ( | wxCmdLineParser & | parser | ) | `[virtual]`

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:
  OnInitCmdLine()

virtual void wxAppConsole::OnEventLoopEnter | ( | wxEventLoopBase * | loop | ) | `[virtual]`

Called by wxEventLoopBase::SetActive(): you can override this function and put here the code which needs an active event loop.

Note that this function is called whenever an event loop is activated; you may want to use wxEventLoopBase::IsMain() to perform initialization specific for the app's main event loop.

See also:
  OnEventLoopExit()

virtual void wxAppConsole::OnEventLoopExit | ( | wxEventLoopBase * | loop | ) | `[virtual]`

Called by wxEventLoopBase::OnExit() for each event loop which is exited.

See also:
  OnEventLoopEnter()

virtual bool wxAppConsole::OnExceptionInMainLoop | ( |  | ) | `[virtual]`

This function is called if an unhandled exception occurs inside the main application event loop.

It can return true to ignore the exception and to continue running the loop or false to exit the loop and terminate the program. In the latter case it can also use C++ `throw` keyword to rethrow the current exception.

The default behaviour of this function is the latter in all ports except under Windows where a dialog is shown to the user which allows him to choose between the different options. You may override this function in your class to do something more appropriate.

Finally note that if the exception is rethrown from here, it can be caught in OnUnhandledException().

virtual int wxAppConsole::OnExit | ( |  | ) | `[virtual]`

Override 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 wxWidgets cleanup. Note that it is not called at all if OnInit() failed.

The return value of this function is currently ignored, return the same value as returned by the base class method if you override it.

virtual void wxAppConsole::OnFatalException | ( |  | ) | `[virtual]`

This function may be called if something fatal happens: an unhandled exception under Win32 or 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:
  wxHandleFatalExceptions()

virtual bool wxAppConsole::OnInit | ( |  | ) | `[virtual]`

This must be provided by the application, and will usually create the application's main window, optionally calling SetTopWindow().

You may use OnExit() to clean up anything initialized here, provided that the function returns true.

Notice that if you want to use the command line processing provided by wxWidgets you have to call the base class version in the derived class OnInit().

Return true to continue processing, false to exit the application immediately.

virtual void wxAppConsole::OnInitCmdLine | ( | wxCmdLineParser & | parser | ) | `[virtual]`

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.

virtual int wxAppConsole::OnRun | ( |  | ) | `[virtual]`

This virtual function is where the execution of a program written in wxWidgets starts.

The default implementation just enters the main loop and starts handling the events until it terminates, either because ExitMainLoop() has been explicitly called or because the last frame has been deleted and GetExitOnFrameDelete() flag is true (this is the default).

The return value of this function becomes the exit code of the program, so it should return 0 in case of successful termination.

virtual void wxAppConsole::OnUnhandledException | ( |  | ) | `[virtual]`

This function is called when an unhandled C++ exception occurs in user code called by wxWidgets.

Any unhandled exceptions thrown from (overridden versions of) OnInit() and OnExit() methods as well as any exceptions thrown from inside the main loop and re-thrown by OnUnhandledException() will result in a call to this function.

By the time this function is called, the program is already about to exit and the exception can't be handled nor ignored any more, override OnUnhandledException() or use explicit `try/catch` blocks around OnInit() body to be able to handle the exception earlier.

The default implementation dumps information about the exception using wxMessageOutputBest.

virtual void wxAppConsole::ProcessPendingEvents | ( |  | ) | `[virtual]`

Process all pending events; it is necessary to call this function to process events posted with wxEvtHandler::QueueEvent or wxEvtHandler::AddPendingEvent.

This happens during each event loop iteration (see wxEventLoopBase) in GUI mode but it may be also called directly.

Note that this function does not only process the pending events for the wxApp object itself (which derives from wxEvtHandler) but also the pending events for any event handler of this application.

This function will immediately return and do nothing if SuspendProcessingOfPendingEvents() was called.

Reimplemented from wxEvtHandler.

void wxAppConsole::ResumeProcessingOfPendingEvents | ( |  | ) |

Resume processing of the pending events previously stopped because of a call to SuspendProcessingOfPendingEvents().

void wxAppConsole::ScheduleForDestruction | ( | wxObject * | object | ) |

Delayed objects destruction.

In applications using events it may be unsafe for an event handler to delete the object which generated the event because more events may be still pending for the same object. In this case the handler may call ScheduleForDestruction() instead. Schedule the object for destruction in the near future.

Notice that if the application is not using an event loop, i.e. if UsesEventLoop() returns false, this method will simply delete the object immediately.

Examples of using this function inside wxWidgets itself include deleting the top level windows when they are closed and sockets when they are disconnected.

void wxAppConsole::SetAppDisplayName | ( | const wxString & | name | ) |

Set the application name to be used in the user-visible places such as window titles.

See GetAppDisplayName() for more about the differences between the display name and name.

Notice that if this function is called, the name is used as is, without any capitalization as done by default by GetAppDisplayName().

void wxAppConsole::SetAppName | ( | const wxString & | name | ) |

Sets the name of the application.

This name should be used for file names, configuration file entries and other internal strings. For the user-visible strings, such as the window titles, the application display name set by SetAppDisplayName() is used instead.

By default the application name is set to the name of its executable file.

See also:
  GetAppName()

void wxAppConsole::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:
  GetClassName()

static void wxAppConsole::SetInstance | ( | wxAppConsole * | app | ) | `[static]`

Allows external code to modify global wxTheApp, but you should really know what you're doing if you call it.

Parameters:

  | app | Replacement for the global application object.

See also:
  GetInstance()

void wxAppConsole::SetVendorDisplayName | ( | const wxString & | name | ) |

Set the vendor name to be used in the user-visible places.

See GetVendorDisplayName() for more about the differences between the display name and name.

void wxAppConsole::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 wxWidgets.

See also:
  GetVendorName()

void wxAppConsole::SuspendProcessingOfPendingEvents | ( |  | ) |

Temporary suspends processing of the pending events.

See also:
  ResumeProcessingOfPendingEvents()

virtual bool wxAppConsole::UsesEventLoop | ( |  | ) | const`[virtual]`

Returns true if the application is using an event loop.

This function always returns true for the GUI applications which must use an event loop but by default only returns true for the console programs if an event loop is already running as it can't know whether one will be created in the future.

Thus, it only makes sense to override it in console applications which do use an event loop, to return true instead of checking if there is a currently active event loop.

## Member Data Documentation

int wxAppConsole::argc

Number of command line arguments (after environment-specific processing).

wxChar** wxAppConsole::argv

Command line arguments (after environment-specific processing).

Under Windows and Linux/Unix, you should parse the command line arguments and check for files to be opened when starting your application. Under OS X, you need to override MacOpenFile() since command line arguments are used differently there.

You may use the wxCmdLineParser to parse command line arguments.

|  | [ top ]