1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxApp
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 This class is essential for writing console-only or hybrid apps without
14 having to define @c wxUSE_GUI=0.
17 @li set and get application-wide properties (see wxAppConsole::CreateTraits
18 and wxAppConsole::SetXXX functions)
19 @li implement the windowing system message or event loop: events in fact are
20 supported even in console-mode applications (see wxAppConsole::HandleEvent
21 and wxAppConsole::ProcessPendingEvents);
22 @li initiate application processing via wxApp::OnInit;
23 @li allow default processing of events not handled by other
24 objects in the application (see wxAppConsole::FilterEvent)
25 @li implement Apple-specific event handlers (see wxAppConsole::MacXXX functions)
27 You should use the macro IMPLEMENT_APP(appClass) in your application
28 implementation file to tell wxWidgets how to create an instance of your
31 Use DECLARE_APP(appClass) in a header file if you want the ::wxGetApp() function
32 (which returns a reference to your application object) to be visible to other
36 @category{appmanagement}
38 @see @ref overview_app, wxApp, wxAppTraits, wxEventLoopBase
40 class wxAppConsole
: public wxEvtHandler
44 Creates the wxAppTraits object when GetTraits() needs it for the first time.
48 virtual wxAppTraits
* CreateTraits();
55 virtual ~wxAppConsole();
61 Note that you should look at wxEvtLoopBase for more event-processing
67 Called by wxWidgets on creation of the application. Override this if you wish
68 to provide your own (environment-dependent) main loop.
70 @return 0 under X, and the wParam of the WM_QUIT message under Windows.
72 virtual int MainLoop();
75 Call this to explicitly exit the main message (event) loop.
76 You should normally exit the main loop (and the application) by deleting
79 This function simply calls wxEvtLoopBase::Exit() on the active loop.
81 virtual void ExitMainLoop();
84 This function is called before processing any event and allows the application
85 to preempt the processing of some events.
87 If this method returns -1 the event is processed normally, otherwise either
88 @true or @false should be returned and the event processing stops immediately
89 considering that the event had been already processed (for the former return
90 value) or that it is not going to be processed at all (for the latter one).
92 virtual int FilterEvent(wxEvent
& event
);
95 Returns the main event loop instance, i.e. the event loop which is started
96 by OnRun() and which dispatches all events sent from the native toolkit
97 to the application (except when new event loops are temporarily set-up).
98 The returned value maybe @NULL. Put initialization code which needs a
99 non-@NULL main event loop into OnEventLoopEnter().
101 wxEventLoopBase
* GetMainLoop() const;
104 This function simply invokes the given method @a func of the specified
105 event handler @a handler with the @a event as parameter. It exists solely
106 to allow to catch the C++ exceptions which could be thrown by all event
107 handlers in the application in one place: if you want to do this, override
108 this function in your wxApp-derived class and add try/catch clause(s) to it.
110 virtual void HandleEvent(wxEvtHandler
* handler
,
111 wxEventFunction func
,
112 wxEvent
& event
) const;
118 Allows external code to modify global ::wxTheApp, but you should really
119 know what you're doing if you call it.
122 Replacement for the global application object.
126 static void SetInstance(wxAppConsole
* app
);
129 Returns the one and only global application object.
130 Usually ::wxTheApp is used instead.
134 static wxAppConsole
* GetInstance();
137 Returns @true if the main event loop is currently running, i.e. if the
138 application is inside OnRun().
140 This can be useful to test whether events can be dispatched. For example,
141 if this function returns @false, non-blocking sockets cannot be used because
142 the events from them would never be processed.
144 static bool IsMainLoopRunning();
148 @name Mac-specific functions
153 Called in response of an "open-application" Apple event.
154 Override this to create a new document in your app.
158 virtual void MacNewFile();
161 Called in response of an "open-document" Apple event.
163 You need to override this method in order to open a document file after the
164 user double clicked on it or if the document file was dropped on either the
165 running application or the application icon in Finder.
169 virtual void MacOpenFile(const wxString
& fileName
);
172 Called in response of a "get-url" Apple event.
176 virtual void MacOpenURL(const wxString
& url
);
179 Called in response of a "print-document" Apple event.
183 virtual void MacPrintFile(const wxString
& fileName
);
186 Called in response of a "reopen-application" Apple event.
190 virtual void MacReopenApp();
196 @name Callbacks for application-wide "events"
201 This function is called when an assert failure occurs, i.e. the condition
202 specified in wxASSERT() macro evaluated to @false.
204 It is only called in debug mode (when @c __WXDEBUG__ is defined) as
205 asserts are not left in the release code at all.
206 The base class version shows the default assert failure dialog box proposing to
207 the user to stop the program, continue or ignore all subsequent asserts.
210 the name of the source file where the assert occurred
212 the line number in this file where the assert occurred
214 the name of the function where the assert occurred, may be
215 empty if the compiler doesn't support C99 __FUNCTION__
217 the condition of the failed assert in text form
219 the message specified as argument to wxASSERT_MSG or wxFAIL_MSG, will
220 be @NULL if just wxASSERT or wxFAIL was used
222 virtual void OnAssertFailure(const wxChar
*file
,
229 Called when command line parsing fails (i.e. an incorrect command line option
230 was specified by the user). The default behaviour is to show the program usage
231 text and abort the program.
233 Return @true to continue normal execution or @false to return
234 @false from OnInit() thus terminating the program.
238 virtual bool OnCmdLineError(wxCmdLineParser
& parser
);
241 Called when the help option (@c --help) was specified on the command line.
242 The default behaviour is to show the program usage text and abort the program.
244 Return @true to continue normal execution or @false to return
245 @false from OnInit() thus terminating the program.
249 virtual bool OnCmdLineHelp(wxCmdLineParser
& parser
);
252 Called after the command line had been successfully parsed. You may override
253 this method to test for the values of the various parameters which could be
254 set from the command line.
256 Don't forget to call the base class version unless you want to suppress
257 processing of the standard command line options.
258 Return @true to continue normal execution or @false to return @false from
259 OnInit() thus terminating the program.
263 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
266 Called by wxEventLoopBase::SetActive(): you can override this function
267 and put here the code which needs an active event loop.
269 Note that this function is called whenever an event loop is activated;
270 you may want to use wxEventLoopBase::IsMain() to perform initialization
271 specific for the app's main event loop.
273 virtual void OnEventLoopEnter(wxEventLoopBase
* loop
);
276 Called by wxEventLoopBase::OnExit() for each event loop which
279 virtual void OnEventLoopExit(wxEventLoopBase
* loop
);
282 This function is called if an unhandled exception occurs inside the main
283 application event loop. It can return @true to ignore the exception and to
284 continue running the loop or @false to exit the loop and terminate the
285 program. In the latter case it can also use C++ @c throw keyword to
286 rethrow the current exception.
288 The default behaviour of this function is the latter in all ports except under
289 Windows where a dialog is shown to the user which allows him to choose between
290 the different options. You may override this function in your class to do
291 something more appropriate.
293 Finally note that if the exception is rethrown from here, it can be caught in
294 OnUnhandledException().
296 virtual bool OnExceptionInMainLoop();
299 Override this member function for any processing which needs to be
300 done as the application is about to exit. OnExit is called after
301 destroying all application windows and controls, but before
302 wxWidgets cleanup. Note that it is not called at all if
305 The return value of this function is currently ignored, return the same
306 value as returned by the base class method if you override it.
308 virtual int OnExit();
311 This function may be called if something fatal happens: an unhandled
312 exception under Win32 or a a fatal signal under Unix, for example. However,
313 this will not happen by default: you have to explicitly call
314 wxHandleFatalExceptions() to enable this.
316 Generally speaking, this function should only show a message to the user and
317 return. You may attempt to save unsaved data but this is not guaranteed to
318 work and, in fact, probably won't.
320 @see wxHandleFatalExceptions()
322 virtual void OnFatalException();
325 This must be provided by the application, and will usually create the
326 application's main window, optionally calling SetTopWindow().
328 You may use OnExit() to clean up anything initialized here, provided
329 that the function returns @true.
331 Notice that if you want to to use the command line processing provided by
332 wxWidgets you have to call the base class version in the derived class
335 Return @true to continue processing, @false to exit the application
338 virtual bool OnInit();
341 Called from OnInit() and may be used to initialize the parser with the
342 command line options for this application. The base class versions adds
343 support for a few standard options only.
345 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
348 This virtual function is where the execution of a program written in wxWidgets
349 starts. The default implementation just enters the main loop and starts
350 handling the events until it terminates, either because ExitMainLoop() has
351 been explicitly called or because the last frame has been deleted and
352 GetExitOnFrameDelete() flag is @true (this is the default).
354 The return value of this function becomes the exit code of the program, so it
355 should return 0 in case of successful termination.
360 This function is called when an unhandled C++ exception occurs inside
361 OnRun() (the exceptions which occur during the program startup and shutdown
362 might not be caught at all). Notice that by now the main event loop has been
363 terminated and the program will exit, if you want to prevent this from happening
364 (i.e. continue running after catching an exception) you need to override
365 OnExceptionInMainLoop().
367 The default implementation shows information about the exception in debug build
368 but does nothing in the release build.
370 virtual void OnUnhandledException();
376 @name Application informations
381 Returns the user-readable application name.
383 The difference between this string and the one returned by GetAppName()
384 is that this one is meant to be shown to the user and so should be used
385 for the window titles, page headers and so on while the other one
386 should be only used internally, e.g. for the file names or
387 configuration file keys. By default, returns the application name as
388 returned by GetAppName() capitalized using wxString::Capitalize().
392 wxString
GetAppDisplayName() const;
395 Returns the application name.
397 @remarks wxWidgets sets this to a reasonable default before calling
398 OnInit(), but the application can reset it at will.
400 @see GetAppDisplayName()
402 wxString
GetAppName() const;
405 Gets the class name of the application. The class name may be used in a
406 platform specific manner to refer to the application.
410 wxString
GetClassName() const;
413 Returns a pointer to the wxAppTraits object for the application.
414 If you want to customize the wxAppTraits object, you must override the
415 CreateTraits() function.
417 wxAppTraits
* GetTraits();
420 Returns the user-readable vendor name. The difference between this string
421 and the one returned by GetVendorName() is that this one is meant to be shown
422 to the user and so should be used for the window titles, page headers and so on
423 while the other one should be only used internally, e.g. for the file names or
424 configuration file keys.
426 By default, returns the same string as GetVendorName().
430 const wxString
& GetVendorDisplayName() const;
433 Returns the application's vendor name.
435 const wxString
& GetVendorName() const;
438 Set the application name to be used in the user-visible places such as
441 See GetAppDisplayName() for more about the differences between the
442 display name and name.
444 Notice that if this function is called, the name is used as is, without
445 any capitalization as done by default by GetAppDisplayName().
447 void SetAppDisplayName(const wxString
& name
);
450 Sets the name of the application. This name should be used for file names,
451 configuration file entries and other internal strings. For the user-visible
452 strings, such as the window titles, the application display name set by
453 SetAppDisplayName() is used instead.
455 By default the application name is set to the name of its executable file.
459 void SetAppName(const wxString
& name
);
462 Sets the class name of the application. This may be used in a platform specific
463 manner to refer to the application.
467 void SetClassName(const wxString
& name
);
470 Set the vendor name to be used in the user-visible places.
471 See GetVendorDisplayName() for more about the differences between the
472 display name and name.
474 void SetVendorDisplayName(const wxString
& name
);
477 Sets the name of application's vendor. The name will be used
478 in registry access. A default name is set by wxWidgets.
482 void SetVendorName(const wxString
& name
);
488 Number of command line arguments (after environment-specific processing).
493 Command line arguments (after environment-specific processing).
495 Under Windows and Linux/Unix, you should parse the command line
496 arguments and check for files to be opened when starting your
497 application. Under OS X, you need to override MacOpenFile()
498 since command line arguments are used differently there.
500 You may use the wxCmdLineParser to parse command line arguments.
511 The wxApp class represents the application itself when @c wxUSE_GUI=1.
513 In addition to the features provided by wxAppConsole it keeps track of
514 the <em>top window</em> (see SetTopWindow()) and adds support for
515 video modes (see SetVideoMode()).
517 In general, application-wide settings for GUI-only apps are accessible
518 from wxApp (or from wxSystemSettings or wxSystemOptions classes).
520 @beginEventEmissionTable
521 @event{EVT_QUERY_END_SESSION(func)}
522 Process a query end session event, supplying the member function.
524 @event{EVT_END_SESSION(func)}
525 Process an end session event, supplying the member function.
527 @event{EVT_ACTIVATE_APP(func)}
528 Process a @c wxEVT_ACTIVATE_APP event. See wxActivateEvent.
529 @event{EVT_HIBERNATE(func)}
530 Process a hibernate event. See wxActivateEvent.
531 @event{EVT_DIALUP_CONNECTED(func)}
532 A connection with the network was established. See wxDialUpEvent.
533 @event{EVT_DIALUP_DISCONNECTED(func)}
534 The connection with the network was lost. See wxDialUpEvent.
535 @event{EVT_IDLE(func)}
536 Process a @c wxEVT_IDLE event. See wxIdleEvent.
540 @category{appmanagement}
542 @see @ref overview_app, wxAppTraits, wxEventLoopBase, wxSystemSettings
544 class wxApp
: public wxAppConsole
548 Constructor. Called implicitly with a definition of a wxApp object.
553 Destructor. Will be called implicitly on program exit if the wxApp
554 object is created on the stack.
559 Get display mode that is used use. This is only used in framebuffer
560 wxWidgets ports (such as wxMGL or wxDFB).
562 virtual wxVideoMode
GetDisplayMode() const;
565 Returns @true if the application will exit when the top-level frame is deleted.
567 @see SetExitOnFrameDelete()
569 bool GetExitOnFrameDelete() const;
572 Return the layout direction for the current locale or @c wxLayout_Default
575 virtual wxLayoutDirection
GetLayoutDirection() const;
578 Returns @true if the application will use the best visual on systems that support
579 different visuals, @false otherwise.
581 @see SetUseBestVisual()
583 bool GetUseBestVisual() const;
586 Returns a pointer to the top window.
589 If the top window hasn't been set using SetTopWindow(), this function
590 will find the first top-level window (frame or dialog or instance of
591 wxTopLevelWindow) from the internal top level window list and return that.
595 virtual wxWindow
* GetTopWindow() const;
598 Returns @true if the application is active, i.e. if one of its windows is
599 currently in the foreground.
601 If this function returns @false and you need to attract users attention to
602 the application, you may use wxTopLevelWindow::RequestUserAttention to do it.
604 virtual bool IsActive() const;
607 This function is similar to wxYield(), except that it disables the user
608 input to all program windows before calling wxAppConsole::Yield and re-enables it
609 again afterwards. If @a win is not @NULL, this window will remain enabled,
610 allowing the implementation of some limited user interaction.
611 Returns the result of the call to wxAppConsole::Yield.
615 virtual bool SafeYield(wxWindow
*win
, bool onlyIfNeeded
);
618 Works like SafeYield() with @e onlyIfNeeded == @true except that
619 it allows the caller to specify a mask of events to be processed.
621 See wxAppConsole::YieldFor for more info.
623 virtual bool SafeYieldFor(wxWindow
*win
, long eventsToProcess
);
626 Windows-only function for processing a message. This function is called
627 from the main message loop, checking for windows that may wish to process it.
629 The function returns @true if the message was processed, @false otherwise.
630 If you use wxWidgets with another class library with its own message loop,
631 you should make sure that this function is called to allow wxWidgets to
632 receive messages. For example, to allow co-existence with the Microsoft
633 Foundation Classes, override the PreTranslateMessage function:
636 // Provide wxWidgets message loop compatibility
637 BOOL CTheApp::PreTranslateMessage(MSG *msg)
639 if (wxTheApp && wxTheApp->ProcessMessage((WXMSW *)msg))
642 return CWinApp::PreTranslateMessage(msg);
648 bool ProcessMessage(WXMSG
* msg
);
651 Sends idle events to a window and its children.
652 Please note that this function is internal to wxWidgets and shouldn't be used
655 @remarks These functions poll the top-level windows, and their children,
656 for idle event processing. If @true is returned, more OnIdle
657 processing is requested by one or more window.
661 virtual bool SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
);
664 Set display mode to use. This is only used in framebuffer wxWidgets
665 ports (such as wxMGL or wxDFB).
667 virtual bool SetDisplayMode(const wxVideoMode
& info
);
670 Allows the programmer to specify whether the application will exit when the
671 top-level frame is deleted.
674 If @true (the default), the application will exit when the top-level frame
675 is deleted. If @false, the application will continue to run.
677 @see GetExitOnFrameDelete(), @ref overview_app_shutdown
679 void SetExitOnFrameDelete(bool flag
);
682 Allows runtime switching of the UI environment theme.
684 Currently implemented for wxGTK2-only.
685 Return @true if theme was successfully changed.
688 The name of the new theme or an absolute path to a gtkrc-theme-file
690 virtual bool SetNativeTheme(const wxString
& theme
);
693 Sets the 'top' window. You can call this from within OnInit() to let wxWidgets
694 know which is the main window. You don't have to set the top window;
695 it is only a convenience so that (for example) certain dialogs without parents
696 can use a specific window as the top window.
698 If no top window is specified by the application, wxWidgets just uses the
699 first frame or dialog (or better, any wxTopLevelWindow) in its top-level
700 window list, when it needs to use the top window.
701 If you previously called SetTopWindow() and now you need to restore this
702 automatic behaviour you can call @code wxApp::SetTopWindow(NULL) @endcode.
707 @see GetTopWindow(), OnInit()
709 void SetTopWindow(wxWindow
* window
);
712 Allows the programmer to specify whether the application will use the best
713 visual on systems that support several visual on the same display. This is typically
714 the case under Solaris and IRIX, where the default visual is only 8-bit whereas
715 certain applications are supposed to run in TrueColour mode.
717 Note that this function has to be called in the constructor of the wxApp
718 instance and won't have any effect when called later on.
719 This function currently only has effect under GTK.
722 If @true, the app will use the best visual.
723 @param forceTrueColour
724 If @true then the application will try to force using a TrueColour
725 visual and abort the app if none is found.
727 void SetUseBestVisual(bool flag
, bool forceTrueColour
= false);
732 // ============================================================================
733 // Global functions/macros
734 // ============================================================================
737 /** @addtogroup group_funcmacro_rtti */
741 This is used in headers to create a forward declaration of the ::wxGetApp()
742 function implemented by IMPLEMENT_APP().
744 It creates the declaration <tt>className& wxGetApp()</tt>.
754 #define DECLARE_APP( className )
757 This is used in the application class implementation file to make the
758 application class known to wxWidgets for dynamic construction.
770 #define IMPLEMENT_APP( className )
777 The global pointer to the singleton wxApp object.
779 @see wxApp::GetInstance()
785 /** @addtogroup group_funcmacro_appinitterm */
789 This function doesn't exist in wxWidgets but it is created by using the
790 IMPLEMENT_APP() macro.
792 Thus, before using it anywhere but in the same module where this macro is
793 used, you must make it available using DECLARE_APP().
795 The advantage of using this function compared to directly using the global
796 ::wxTheApp pointer is that the latter is of type wxApp* and so wouldn't
797 allow you to access the functions specific to your application class but
798 not present in wxApp while wxGetApp() returns the object of the right type.
802 wxAppDerivedClass
& wxGetApp();
805 If @a doIt is @true, the fatal exceptions (also known as general protection
806 faults under Windows or segmentation violations in the Unix world) will be
807 caught and passed to wxApp::OnFatalException.
809 By default, i.e. before this function is called, they will be handled in
810 the normal way which usually just means that the application will be
811 terminated. Calling wxHandleFatalExceptions() with @a doIt equal to @false
812 will restore this default behaviour.
814 Notice that this function is only available if @c wxUSE_ON_FATAL_EXCEPTION
815 is 1 and under Windows platform this requires a compiler with support for
816 SEH (structured exception handling) which currently means only Microsoft
817 Visual C++ or a recent Borland C++ version.
821 bool wxHandleFatalExceptions(bool doIt
= true);
824 This function is used in wxBase only and only if you don't create
825 wxApp object at all. In this case you must call it from your
826 @c main() function before calling any other wxWidgets functions.
828 If the function returns @false the initialization could not be performed,
829 in this case the library cannot be used and wxUninitialize() shouldn't be
832 This function may be called several times but wxUninitialize() must be
833 called for each successful call to this function.
840 This function is for use in console (wxBase) programs only. It must be called
841 once for each previous successful call to wxInitialize().
845 void wxUninitialize();
848 This function wakes up the (internal and platform dependent) idle system,
849 i.e. it will force the system to send an idle event even if the system
850 currently @e is idle and thus would not send any idle event until after
851 some other event would get sent. This is also useful for sending events
852 between two threads and is used by the corresponding functions
853 wxPostEvent() and wxEvtHandler::AddPendingEvent().
860 Calls wxAppConsole::Yield.
863 This function is kept only for backwards compatibility. Please use
864 the wxAppConsole::Yield method instead in any new code.
871 Calls wxApp::SafeYield.
875 bool wxSafeYield(wxWindow
* win
= NULL
, bool onlyIfNeeded
= false);
878 This function initializes wxWidgets in a platform-dependent way. Use this if you
879 are not using the default wxWidgets entry code (e.g. main or WinMain).
881 For example, you can initialize wxWidgets from an Microsoft Foundation Classes
882 (MFC) application using this function.
884 @note This overload of wxEntry is available under all platforms.
890 int wxEntry(int& argc
, wxChar
** argv
);
893 See wxEntry(int&,wxChar**) for more info about this function.
895 Notice that under Windows CE platform, and only there, the type of @a pCmdLine
896 is @c wchar_t *, otherwise it is @c char *, even in Unicode build.
898 @remarks To clean up wxWidgets, call wxApp::OnExit followed by the static
899 function wxApp::CleanUp. For example, if exiting from an MFC application
900 that also uses wxWidgets:
902 int CTheApp::ExitInstance()
904 // OnExit isn't called by CleanUp so must be called explicitly.
908 return CWinApp::ExitInstance();
914 int wxEntry(HINSTANCE hInstance
,
915 HINSTANCE hPrevInstance
= NULL
,
916 char* pCmdLine
= NULL
,
917 int nCmdShow
= SW_SHOWNORMAL
);
923 /** @addtogroup group_funcmacro_procctrl */
927 Exits application after calling wxApp::OnExit.
929 Should only be used in an emergency: normally the top-level frame
930 should be deleted (after deleting all other frames) to terminate the
931 application. See wxCloseEvent and wxApp.