contents up previous next

wxapp

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 wxwidgets 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

wxevthandler
wxobject

include files

<wx/app.h>

see also

wxapp overview

members

wxapp::wxapp
wxapp::~wxapp
wxapp::argc
wxapp::argv
wxapp::createlogtarget
wxapp::createtraits
wxapp::dispatch
wxapp::exitmainloop
wxapp::filterevent
wxapp::getappname
wxapp::getclassname
wxapp::getexitonframedelete
wxapp::getinstance
wxapp::gettopwindow
wxapp::gettraits
wxapp::getusebestvisual
wxapp::getvendorname
wxapp::isactive
wxapp::ismainlooprunning
wxapp::mainloop
wxapp::onassertfailure
wxapp::oncmdlineerror
wxapp::oncmdlinehelp
wxapp::oncmdlineparsed
wxapp::onexceptioninmainloop
wxapp::onexit
wxapp::onfatalexception
wxapp::oninit
wxapp::oninitcmdline
wxapp::onrun
wxapp::onunhandledexception
wxapp::processmessage
wxapp::pending
wxapp::sendidleevents
wxapp::setappname
wxapp::setclassname
wxapp::setexitonframedelete
wxapp::setinstance
wxapp::settopwindow
wxapp::setvendorname
wxapp::setusebestvisual
wxapp::handleevent
wxapp::yield


wxapp::wxapp

wxapp()

constructor. called implicitly with a definition of a wxapp object.


wxapp::~wxapp

virtual ~wxapp()

destructor. will be called implicitly on program exit if the wxapp object is created on the stack.


wxapp::argc

int argc

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


wxapp::argv

wxchar ** argv

command line arguments (after environment-specific processing).


wxapp::createlogtarget

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

wxlog


wxapp::createtraits

virtual wxapptraits * createtraits()

creates the wxapptraits object when gettraits needs it for the first time.

see also

wxapptraits


wxapp::dispatch

virtual 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

wxapp::pending


wxapp::exitmainloop

virtual 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.


wxapp::filterevent

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).


wxapp::getappname

wxstring getappname() const

returns the application name.

remarks

wxwidgets sets this to a reasonable default before calling wxapp::oninit, but the application can reset it at will.


wxapp::getclassname

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

wxapp::setclassname


wxapp::getexitonframedelete

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


wxapp::getinstance

static wxappconsole * getinstance()

returns the one and only global application object. usually wxtheapp is usead instead.

see also

wxapp::setinstance


wxapp::gettopwindow

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

settopwindow


wxapp::gettraits

wxapptraits * 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.


wxapp::getusebestvisual

bool getusebestvisual() const

returns true if the application will use the best visual on systems that support different visuals, false otherwise.

see also

setusebestvisual


wxapp::getvendorname

wxstring getvendorname() const

returns the application's vendor name.


wxapp::isactive

bool isactive() const

returns true if the application is active, i.e. if one of its windows is currently in the foreground. if this function returns false and you need to attract users attention to the application, you may use wxtoplevelwindow::requestuserattention to do it.


wxapp::ismainlooprunning

static bool ismainlooprunning()

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 the 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.


wxapp::mainloop

virtual int mainloop()

called by wxwidgets 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.


wxapp::onassertfailure

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

line

func

cond

msg


wxapp::oncmdlineerror

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

oninitcmdline


wxapp::oncmdlinehelp

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

oninitcmdline


wxapp::oncmdlineparsed

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

oninitcmdline


wxapp::onexceptioninmainloop

virtual bool onexceptioninmainloop()

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.


wxapp::onexit

virtual int onexit()

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.


wxapp::onfatalexception

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

wxhandlefatalexceptions


wxapp::oninit

bool oninit()

this must be provided by the application, and will usually create the application's main window, optionally calling wxapp::settopwindow. you may use onexit to clean up anything initialized here, provided that the function returns true.

notice that if you want to 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.


wxapp::oninitcmdline

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.


wxapp::onrun

virtual int onrun()

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.


wxapp::onunhandledexception

virtual void onunhandledexception()

this function is called when an unhandled c++ exception occurs inside onrun() (the exceptions which occur during the program startup and shutdown might not be caught at all). note that the exception type is lost by now, so if you want to really handle the exception you should override onrun() and put a try/catch clause around the call to the base class version there.


wxapp::processmessage

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 wxwidgets with another class library with its own message loop, you should make sure that this function is called to allow wxwidgets to receive messages. for example, to allow co-existence with the microsoft foundation classes, override the pretranslatemessage function:

// provide wxwidgets message loop compatibility
bool ctheapp::pretranslatemessage(msg *msg)
{
  if (wxtheapp && wxtheapp->processmessage((wxmsw *)msg))
    return true;
  else
    return cwinapp::pretranslatemessage(msg);
}

wxapp::pending

virtual bool pending()

returns true if unprocessed events are in the window system event queue.

see also

wxapp::dispatch


wxapp::sendidleevents

bool sendidleevents(wxwindow* win, wxidleevent& event)

sends idle events to a window and its children.

please note that this function is internal to wxwidgets and shouldn't be used by user code.

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

wxidleevent


wxapp::setappname

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 wxwidgets.

see also

wxapp::getappname


wxapp::setclassname

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

wxapp::getclassname


wxapp::setexitonframedelete

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


wxapp::setinstance

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.

parameters

app

see also

wxapp::getinstance


wxapp::settopwindow

void settopwindow(wxwindow* window)

sets the 'top' window. you can call this from within wxapp::oninit to let wxwidgets 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, wxwidgets 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


wxapp::setvendorname

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 wxwidgets.

see also

wxapp::getvendorname


wxapp::setusebestvisual

void setusebestvisual(bool flag, bool forcetruecolour = false)

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.

if forcetruecolour is true then the application will try to force using a truecolour visual and abort the app if none is found.

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


wxapp::handleevent

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


wxapp::yield

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 onlyifneeded parameter is true, the method will just silently return false instead.