]>
git.saurik.com Git - wxWidgets.git/blob - interface/app.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxApp
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 The @b wxApp class represents the application itself. It is used
16 set and get application-wide properties;
17 implement the windowing system message or event loop;
18 initiate application processing via wxApp::OnInit;
19 allow default processing of events not handled by other
20 objects in the application.
22 You should use the macro IMPLEMENT_APP(appClass) in your application
24 file to tell wxWidgets how to create an instance of your application class.
26 Use DECLARE_APP(appClass) in a header file if you want the wxGetApp function
28 a reference to your application object) to be visible to other files.
31 @category{appmanagement}
33 @see @ref overview_wxappoverview "wxApp overview"
35 class wxApp
: public wxEvtHandler
39 Constructor. Called implicitly with a definition of a wxApp object.
44 Destructor. Will be called implicitly on program exit if the wxApp
45 object is created on the stack.
50 Creates a wxLog class for the application to use for logging errors. The default
51 implementation returns a new wxLogGui class.
55 virtual wxLog
* CreateLogTarget ();
58 Creates the wxAppTraits object when GetTraits()
59 needs it for the first time.
63 virtual wxAppTraits
* CreateTraits ();
66 Dispatches the next event in the windowing system event queue.
67 This can be used for programming event loops, e.g.
71 virtual void Dispatch ();
74 Call this to explicitly exit the main message (event) loop.
75 You should normally exit the main loop (and the application) by deleting
78 virtual void ExitMainLoop ();
81 This function is called before processing any event and allows the application
82 to preempt the processing of some events. If this method returns -1 the event
83 is processed normally, otherwise either @true or @false should be
84 returned and the event processing stops immediately considering that the event
85 had been already processed (for the former return value) or that it is not
86 going to be processed at all (for the latter one).
88 int FilterEvent ( wxEvent
& event
);
91 Returns the user-readable application name. The difference between this string
92 and the one returned by GetAppName() is that this one
93 is meant to be shown to the user and so should be used for the window titles,
94 page headers and so on while the other one should be only used internally, e.g.
95 for the file names or configuration file keys.
96 By default, returns the same string as GetAppName().
100 wxString
GetAppDisplayName () const ;
103 Returns the application name.
105 @remarks wxWidgets sets this to a reasonable default before calling
106 OnInit(), but the application can reset it at will.
108 @see GetAppDisplayName()
110 wxString
GetAppName () const ;
113 Gets the class name of the application. The class name may be used in a
115 manner to refer to the application.
119 wxString
GetClassName () const ;
122 Returns @true if the application will exit when the top-level window is deleted,
126 @see SetExitOnFrameDelete(), @ref overview_wxappshutdownoverview "wxApp
129 bool GetExitOnFrameDelete () const ;
132 Returns the one and only global application object.
133 Usually @c wxTheApp is usead instead.
137 static wxAppConsole
* GetInstance ();
140 Returns a pointer to the top window.
142 @remarks If the top window hasn't been set using SetTopWindow(),
143 this function will find the first top-level window
144 (frame or dialog) and return that.
148 virtual wxWindow
* GetTopWindow () const ;
151 Returns a pointer to the wxAppTraits object for the application.
152 If you want to customize the wxAppTraits object, you must override the
153 CreateTraits() function.
155 wxAppTraits
* GetTraits ();
158 Returns @true if the application will use the best visual on systems that support
159 different visuals, @false otherwise.
161 @see SetUseBestVisual()
163 bool GetUseBestVisual () const ;
166 Returns the user-readable vendor name. The difference between this string
167 and the one returned by GetVendorName() is that this one
168 is meant to be shown to the user and so should be used for the window titles,
169 page headers and so on while the other one should be only used internally, e.g.
170 for the file names or configuration file keys.
171 By default, returns the same string as GetVendorName().
175 wxString
GetVendorDisplayName () const ;
178 Returns the application's vendor name.
180 wxString
GetVendorName () const ;
183 This function simply invokes the given method @a func of the specified
184 event handler @a handler with the @a event as parameter. It exists solely
185 to allow to catch the C++ exceptions which could be thrown by all event
186 handlers in the application in one place: if you want to do this, override this
187 function in your wxApp-derived class and add try/catch clause(s) to it.
189 virtual void HandleEvent ( wxEvtHandler handler
,
190 wxEventFunction func
,
191 wxEvent
& event
) const ;
194 Returns @true if the application is active, i.e. if one of its windows is
195 currently in the foreground. If this function returns @false and you need to
196 attract users attention to the application, you may use
197 wxTopLevelWindow::RequestUserAttention
200 bool IsActive () const ;
203 Returns @true if the main event loop is currently running, i.e. if the
204 application is inside OnRun().
205 This can be useful to test whether events can be dispatched. For example,
206 if this function returns @false, non-blocking sockets cannot be used because
207 the events from them would never be processed.
209 static bool IsMainLoopRunning ();
212 Mac specific. Called in response of an "open-application" Apple event.
213 Override this to create a new document in your app.
218 Mac specific. Called in response of an "open-document" Apple event. You need to
219 override this method in order to open a document file after the
220 user double clicked on it or if the document file was dropped
221 on either the running application or the application icon in
224 void MacOpenFile ( const wxString
& fileName
);
227 Mac specific. Called in response of a "get-url" Apple event.
229 void MacOpenURL ( const wxString
& url
);
232 Mac specific. Called in response of a "print-document" Apple event.
234 void MacPrintFile ( const wxString
& fileName
);
237 Mac specific. Called in response of a "reopen-application" Apple event.
242 Called by wxWidgets on creation of the application. Override this if you wish
243 to provide your own (environment-dependent) main loop.
245 @returns Returns 0 under X, and the wParam of the WM_QUIT message under
248 virtual int MainLoop ();
251 This function is called when an assert failure occurs, i.e. the condition
252 specified in wxASSERT() macro evaluated to @false.
253 It is only called in debug mode (when @c __WXDEBUG__ is defined) as
254 asserts are not left in the release code at all.
255 The base class version shows the default assert failure dialog box proposing to
256 the user to stop the program, continue or ignore all subsequent asserts.
259 the name of the source file where the assert occurred
261 the line number in this file where the assert occurred
263 the name of the function where the assert occurred, may be
264 empty if the compiler doesn't support C99 __FUNCTION__
266 the condition of the failed assert in text form
268 the message specified as argument to
269 wxASSERT_MSG or wxFAIL_MSG, will
270 be @NULL if just wxASSERT or wxFAIL
273 void OnAssertFailure ( const wxChar file
, int line
,
279 Called when command line parsing fails (i.e. an incorrect command line option
280 was specified by the user). The default behaviour is to show the program usage
281 text and abort the program.
282 Return @true to continue normal execution or @false to return
283 @false from OnInit() thus terminating the program.
287 bool OnCmdLineError ( wxCmdLineParser
& parser
);
290 Called when the help option (@c --help) was specified on the command line.
291 The default behaviour is to show the program usage text and abort the program.
292 Return @true to continue normal execution or @false to return
293 @false from OnInit() thus terminating the program.
297 bool OnCmdLineHelp ( wxCmdLineParser
& parser
);
300 Called after the command line had been successfully parsed. You may override
301 this method to test for the values of the various parameters which could be
302 set from the command line.
303 Don't forget to call the base class version unless you want to suppress
304 processing of the standard command line options.
305 Return @true to continue normal execution or @false to return
306 @false from OnInit() thus terminating the program.
310 bool OnCmdLineParsed ( wxCmdLineParser
& parser
);
313 This function is called if an unhandled exception occurs inside the main
314 application event loop. It can return @true to ignore the exception and to
315 continue running the loop or @false to exit the loop and terminate the
316 program. In the latter case it can also use C++ @c throw keyword to
317 rethrow the current exception.
318 The default behaviour of this function is the latter in all ports except under
319 Windows where a dialog is shown to the user which allows him to choose between
320 the different options. You may override this function in your class to do
321 something more appropriate.
322 Finally note that if the exception is rethrown from here, it can be caught in
323 OnUnhandledException().
325 virtual bool OnExceptionInMainLoop ();
328 Override this member function for any processing which needs to be
329 done as the application is about to exit. OnExit is called after
330 destroying all application windows and controls, but before
331 wxWidgets cleanup. Note that it is not called at all if
333 The return value of this function is currently ignored, return the same value
334 as returned by the base class method if you override it.
336 virtual int OnExit ();
339 This function may be called if something fatal happens: an unhandled
340 exception under Win32 or a a fatal signal under Unix, for example. However,
341 this will not happen by default: you have to explicitly call
342 wxHandleFatalExceptions() to enable this.
343 Generally speaking, this function should only show a message to the user and
344 return. You may attempt to save unsaved data but this is not guaranteed to
345 work and, in fact, probably won't.
347 @see wxHandleFatalExceptions()
349 void OnFatalException ();
352 This must be provided by the application, and will usually create the
353 application's main window, optionally calling
354 SetTopWindow(). You may use
355 OnExit() to clean up anything initialized here, provided
356 that the function returns @true.
357 Notice that if you want to to use the command line processing provided by
358 wxWidgets you have to call the base class version in the derived class
360 Return @true to continue processing, @false to exit the application
366 Called from OnInit() and may be used to initialize the
367 parser with the command line options for this application. The base class
368 versions adds support for a few standard options only.
370 void OnInitCmdLine ( wxCmdLineParser
& parser
);
373 This virtual function is where the execution of a program written in wxWidgets
374 starts. The default implementation just enters the main loop and starts
375 handling the events until it terminates, either because
376 ExitMainLoop() has been explicitly called or because
377 the last frame has been deleted and
378 GetExitOnFrameDelete() flag is @true (this
380 The return value of this function becomes the exit code of the program, so it
381 should return 0 in case of successful termination.
386 This function is called when an unhandled C++ exception occurs inside
387 OnRun() (the exceptions which occur during the program
388 startup and shutdown might not be caught at all). Notice that by now the main
389 event loop has been terminated and the program will exit, if you want to
390 prevent this from happening (i.e. continue running after catching an exception)
391 you need to override OnExceptionInMainLoop().
392 The default implementation shows information about the exception in debug build
393 but does nothing in the release build.
395 virtual void OnUnhandledException ();
398 Returns @true if unprocessed events are in the window system event queue.
402 virtual bool Pending ();
405 Windows-only function for processing a message. This function
406 is called from the main message loop, checking for windows that
407 may wish to process it. The function returns @true if the message
408 was processed, @false otherwise. If you use wxWidgets with another class
409 library with its own message loop, you should make sure that this
410 function is called to allow wxWidgets to receive messages. For example,
411 to allow co-existence with the Microsoft Foundation Classes, override
412 the PreTranslateMessage function:
414 bool ProcessMessage ( WXMSG
* msg
);
417 Sends idle events to a window and its children.
418 Please note that this function is internal to wxWidgets and shouldn't be used
421 @remarks These functions poll the top-level windows, and their children,
422 for idle event processing. If @true is returned, more
423 OnIdle processing is requested by one or more window.
427 bool SendIdleEvents ( wxWindow
* win
, wxIdleEvent
& event
);
430 Set the application name to be used in the user-visible places such as window
431 titles. See GetAppDisplayName() for more about
432 the differences between the display name and name.
434 void SetAppDisplayName ( const wxString
& name
);
437 Sets the name of the application. This name should be used for file names,
438 configuration file entries and other internal strings. For the user-visible
439 strings, such as the window titles, the application display name set by
440 SetAppDisplayName() is used instead.
441 By default the application name is set to the name of its executable file.
445 void SetAppName ( const wxString
& name
);
448 Sets the class name of the application. This may be used in a platform specific
449 manner to refer to the application.
453 void SetClassName ( const wxString
& name
);
456 Allows the programmer to specify whether the application will exit when the
457 top-level frame is deleted.
460 If @true (the default), the application will exit when the top-level frame is
461 deleted. If @false, the application will continue to run.
463 @see GetExitOnFrameDelete(), @ref overview_wxappshutdownoverview "wxApp
466 void SetExitOnFrameDelete ( bool flag
);
469 Allows external code to modify global @c wxTheApp, but you should really
470 know what you're doing if you call it.
473 Replacement for the global application object.
477 static void SetInstance ( wxAppConsole
* app
);
480 Allows runtime switching of the UI environment theme. Currently implemented for
482 Return @true if theme was successfully changed.
485 The name of the new theme or an absolute path to a gtkrc-theme-file
487 bool SetNativeTheme ( const wxStringamp
;);
490 Sets the 'top' window. You can call this from within OnInit() to
491 let wxWidgets know which is the main window. You don't have to set the top
493 it is only a convenience so that (for example) certain dialogs without parents
495 specific window as the top window. If no top window is specified by the
497 wxWidgets just uses the first frame or dialog in its top-level window list,
499 needs to use the top window.
504 @see GetTopWindow(), OnInit()
506 void SetTopWindow ( wxWindow
* window
);
509 Allows the programmer to specify whether the application will use the best
511 on systems that support several visual on the same display. This is typically
513 case under Solaris and IRIX, where the default visual is only 8-bit whereas
515 applications are supposed to run in TrueColour mode.
516 If @a forceTrueColour is @true then the application will try to force
517 using a TrueColour visual and abort the app if none is found.
518 Note that this function has to be called in the constructor of the @c wxApp
519 instance and won't have any effect when called later on.
520 This function currently only has effect under GTK.
523 If @true, the app will use the best visual.
525 void SetUseBestVisual ( bool flag
, bool forceTrueColour
= false );
528 Set the vendor name to be used in the user-visible places. See
529 GetVendorDisplayName() for more about
530 the differences between the display name and name.
532 void SetVendorDisplayName ( const wxString
& name
);
535 Sets the name of application's vendor. The name will be used
536 in registry access. A default name is set by
541 void SetVendorName ( const wxString
& name
);
544 Yields control to pending messages in the windowing system. This can be useful,
546 time-consuming process writes to a text window. Without an occasional
547 yield, the text window will not be updated properly, and on systems with
548 cooperative multitasking, such as Windows 3.1 other processes will not respond.
549 Caution should be exercised, however, since yielding may allow the
550 user to perform actions which are not compatible with the current task.
551 Disabling menu items or whole menus during processing can avoid unwanted
552 reentrance of code: see ::wxSafeYield for a better
554 Note that Yield() will not flush the message logs. This is intentional as
555 calling Yield() is usually done to quickly update the screen and popping up a
556 message box dialog may be undesirable. If you do wish to flush the log
557 messages immediately (otherwise it will be done during the next idle loop
558 iteration), call wxLog::FlushActive.
559 Calling Yield() recursively is normally an error and an assert failure is
560 raised in debug build if such situation is detected. However if the
561 @a onlyIfNeeded parameter is @true, the method will just silently
562 return @false instead.
564 bool Yield ( bool onlyIfNeeded
= false );
568 Number of command line arguments (after environment-specific processing).
574 Command line arguments (after environment-specific processing).
575 Under Windows and Linux/Unix, you should parse the command line
576 arguments and check for files to be opened when starting your
577 application. Under OS X, you need to override MacOpenFile()
578 since command line arguments are used differently there.
579 You may use the wxCmdLineParser to
580 parse command line arguments.
586 // ============================================================================
587 // Global functions/macros
588 // ============================================================================
592 For all normal, informational messages. They also appear in a message box by
593 default (but it can be changed).
595 void wxLogMessage ( const char * formatString
, ... );
596 void wxVLogMessage ( const char * formatString
, va_list argPtr
);
601 For verbose output. Normally, it is suppressed, but
602 might be activated if the user wishes to know more details about the program
603 progress (another, but possibly confusing name for the same function is @b
606 void wxLogVerbose ( const char * formatString
, ... );
607 void wxVLogVerbose ( const char * formatString
, va_list argPtr
);
611 This is used in headers to create a forward declaration of the
612 wxGetApp() function implemented by
613 wxIMPLEMENT_APP(). It creates the declaration
614 @c className wxGetApp(void).
621 #define wxDECLARE_APP() /* implementation is private */
624 Exits application after calling wxApp::OnExit.
625 Should only be used in an emergency: normally the top-level frame
626 should be deleted (after deleting all other frames) to terminate the
627 application. See wxCloseEvent and wxApp.
633 For warnings - they are also normally shown to the user, but don't interrupt
636 void wxLogWarning ( const char * formatString
, ... );
637 void wxVLogWarning ( const char * formatString
, va_list argPtr
);
642 Like wxLogError(), but also
643 terminates the program with the exit code 3. Using @e abort() standard
644 function also terminates the program with this exit code.
646 void wxLogFatalError ( const char * formatString
, ... );
647 void wxVLogFatalError ( const char * formatString
,
652 If @a doIt is @true, the fatal exceptions (also known as general protection
653 faults under Windows or segmentation violations in the Unix world) will be
654 caught and passed to wxApp::OnFatalException.
655 By default, i.e. before this function is called, they will be handled in the
656 normal way which usually just means that the application will be terminated.
657 Calling wxHandleFatalExceptions() with @a doIt equal to @false will restore
658 this default behaviour.
659 Notice that this function is only available if
660 @c wxUSE_ON_FATAL_EXCEPTION is 1 and under Windows platform this
661 requires a compiler with support for SEH (structured exception handling) which
662 currently means only Microsoft Visual C++ or a recent Borland C++ version.
664 bool wxHandleFatalExceptions ( bool doIt
= true );
667 This is used in the application class implementation file to make the
668 application class known to
669 wxWidgets for dynamic construction. You use this instead of
682 See also DECLARE_APP().
684 #define IMPLEMENT_APP() /* implementation is private */
687 Returns the error code from the last system call. This function uses
688 @c errno on Unix platforms and @c GetLastError under Win32.
690 @see wxSysErrorMsg(), wxLogSysError()
692 unsigned long wxSysErrorCode ();
695 In a GUI application, this function posts @a event to the specified @e dest
696 object using wxEvtHandler::AddPendingEvent.
697 Otherwise, it dispatches @a event immediately using
698 wxEvtHandler::ProcessEvent.
699 See the respective documentation for details (and caveats).
701 void wxPostEvent ( wxEvtHandler
* dest
, wxEvent
& event
);
705 The functions to use for error messages, i.e. the messages that must be shown
706 to the user. The default processing is to pop up a message box to inform the
709 void wxLogError ( const char * formatString
, ... );
710 void wxVLogError ( const char * formatString
, va_list argPtr
);
715 As @b wxLogDebug, trace functions only do something in debug build and
716 expand to nothing in the release one. The reason for making
717 it a separate function from it is that usually there are a lot of trace
718 messages, so it might make sense to separate them from other debug messages.
719 The trace messages also usually can be separated into different categories and
720 the second and third versions of this function only log the message if the
721 @a mask which it has is currently enabled in wxLog. This
722 allows to selectively trace only some operations and not others by changing
723 the value of the trace mask (possible during the run-time).
724 For the second function (taking a string mask), the message is logged only if
725 the mask has been previously enabled by the call to
726 wxLog::AddTraceMask or by setting
727 @ref overview_envvars "@c WXTRACE environment variable".
728 The predefined string trace masks
729 used by wxWidgets are:
730 wxTRACE_MemAlloc: trace memory allocation (new/delete)
731 wxTRACE_Messages: trace window messages/X callbacks
732 wxTRACE_ResAlloc: trace GDI resource allocation
733 wxTRACE_RefCount: trace various ref counting operations
734 wxTRACE_OleCalls: trace OLE method calls (Win32 only)
735 @b Caveats: since both the mask and the format string are strings,
736 this might lead to function signature confusion in some cases:
737 if you intend to call the format string only version of wxLogTrace,
738 then add a %s format string parameter and then supply a second string parameter
739 for that %s, the string mask version of wxLogTrace will erroneously get called instead, since you are supplying two string parameters to the function.
740 In this case you'll unfortunately have to avoid having two leading
741 string parameters, e.g. by adding a bogus integer (with its %d format string).
742 The third version of the function only logs the message if all the bits
743 corresponding to the @a mask are set in the wxLog trace mask which can be
744 set by wxLog::SetTraceMask. This version is less
745 flexible than the previous one because it doesn't allow defining the user
746 trace masks easily - this is why it is deprecated in favour of using string
748 wxTraceMemAlloc: trace memory allocation (new/delete)
749 wxTraceMessages: trace window messages/X callbacks
750 wxTraceResAlloc: trace GDI resource allocation
751 wxTraceRefCount: trace various ref counting operations
752 wxTraceOleCalls: trace OLE method calls (Win32 only)
754 void wxLogTrace ( const char * formatString
, ... );
755 void wxVLogTrace ( const char * formatString
, va_list argPtr
);
756 void wxLogTrace ( const char * mask
, const char * formatString
,
758 void wxVLogTrace ( const char * mask
,
759 const char * formatString
,
761 void wxLogTrace ( wxTraceMask mask
, const char * formatString
,
763 void wxVLogTrace ( wxTraceMask mask
, const char * formatString
,
768 Returns the error message corresponding to the given system error code. If
769 @a errCode is 0 (default), the last error code (as returned by
770 wxSysErrorCode()) is used.
772 @see wxSysErrorCode(), wxLogSysError()
774 const wxChar
* wxSysErrorMsg ( unsigned long errCode
= 0 );
777 This function is for use in console (wxBase) programs only. It must be called
778 once for each previous successful call to wxInitialize().
780 void wxUninitialize ();
784 The right functions for debug output. They only do something in debug
785 mode (when the preprocessor symbol __WXDEBUG__ is defined) and expand to
786 nothing in release mode (otherwise).
788 void wxLogDebug ( const char * formatString
, ... );
789 void wxVLogDebug ( const char * formatString
, va_list argPtr
);
793 This function doesn't exist in wxWidgets but it is created by using
794 the IMPLEMENT_APP() macro. Thus, before using it
795 anywhere but in the same module where this macro is used, you must make it
796 available using DECLARE_APP().
797 The advantage of using this function compared to directly using the global
798 wxTheApp pointer is that the latter is of type @c wxApp * and so wouldn't
799 allow you to access the functions specific to your application class but not
800 present in wxApp while wxGetApp() returns the object of the right type.
802 wxAppDerivedClass
wxGetApp ();
806 Messages logged by these functions will appear in the statusbar of the @a frame
807 or of the top level application window by default (i.e. when using
808 the second version of the functions).
809 If the target frame doesn't have a statusbar, the message will be lost.
811 void wxLogStatus ( wxFrame
* frame
, const char * formatString
,
813 void wxVLogStatus ( wxFrame
* frame
, const char * formatString
,
815 void wxLogStatus ( const char * formatString
, ... );
816 void wxVLogStatus ( const char * formatString
, va_list argPtr
);
820 This function is used in wxBase only and only if you don't create
821 wxApp object at all. In this case you must call it from your
822 @c main() function before calling any other wxWidgets functions.
823 If the function returns @false the initialization could not be performed,
824 in this case the library cannot be used and
825 wxUninitialize() shouldn't be called neither.
826 This function may be called several times but
827 wxUninitialize() must be called for each successful
828 call to this function.
833 This is used in headers to create a forward declaration of the
834 wxGetApp() function implemented by
835 IMPLEMENT_APP(). It creates the declaration
836 @c className wxGetApp(void).
843 #define DECLARE_APP() /* implementation is private */
847 This function is kept only for backwards compatibility. Please use
848 the wxApp::Yield method instead in any new code.
854 Mostly used by wxWidgets itself, but might be handy for logging errors after
855 system call (API function) failure. It logs the specified message text as well
856 as the last system error code (@e errno or @e ::GetLastError() depending
857 on the platform) and the corresponding error message. The second form
858 of this function takes the error code explicitly as the first argument.
860 @see wxSysErrorCode(), wxSysErrorMsg()
862 void wxLogSysError ( const char * formatString
, ... );
863 void wxVLogSysError ( const char * formatString
,
869 This initializes wxWidgets in a platform-dependent way. Use this if you are not
870 using the default wxWidgets entry code (e.g. main or WinMain). For example, you
871 can initialize wxWidgets from an Microsoft Foundation Classes application using
873 The following overload of wxEntry is available under all platforms:
875 (notice that under Windows CE platform, and only there, the type of
876 @a pCmdLine is @c wchar_t *, otherwise it is @c char *, even in
879 @remarks To clean up wxWidgets, call wxApp::OnExit followed by the static
880 function wxApp::CleanUp. For example, if exiting from
881 an MFC application that also uses wxWidgets:
885 int wxEntry ( int & argc
, wxChar
** argv
);
886 int wxEntry ( HINSTANCE hInstance
,
887 HINSTANCE hPrevInstance
= NULL
,
888 char * pCmdLine
= NULL
,
889 int nCmdShow
= SW_SHOWNORMAL
);