]>
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 /////////////////////////////////////////////////////////////////////////////
14 This class is essential for writing console-only or hybrid apps without
15 having to define wxUSE_GUI=0.
20 @category{appmanagement}
22 @see @ref overview_app
24 class wxAppConsole
: public wxEvtHandler
28 Creates the wxAppTraits object when GetTraits() needs it for the first time.
32 virtual wxAppTraits
* CreateTraits();
44 virtual ~wxAppConsole();
47 Dispatches the next event in the windowing system event queue.
48 Blocks until an event appears if there are none currently
49 (use Pending() if this is not wanted).
51 This can be used for programming event loops, e.g.
58 @return @false if the event loop should stop and @true otherwise.
62 virtual bool Dispatch();
65 Call this to explicitly exit the main message (event) loop.
66 You should normally exit the main loop (and the application) by deleting
69 virtual void ExitMainLoop();
72 This function is called before processing any event and allows the application
73 to preempt the processing of some events.
75 If this method returns -1 the event is processed normally, otherwise either
76 @true or @false should be returned and the event processing stops immediately
77 considering that the event had been already processed (for the former return
78 value) or that it is not going to be processed at all (for the latter one).
80 virtual int FilterEvent(wxEvent
& event
);
83 Returns the user-readable application name.
85 The difference between this string and the one returned by GetAppName() is that
86 this one is meant to be shown to the user and so should be used for the window
87 titles, page headers and so on while the other one should be only used internally,
88 e.g. for the file names or configuration file keys.
89 By default, returns the same string as GetAppName().
93 wxString
GetAppDisplayName() const;
96 Returns the application name.
98 @remarks wxWidgets sets this to a reasonable default before calling
99 OnInit(), but the application can reset it at will.
101 @see GetAppDisplayName()
103 wxString
GetAppName() const;
106 Gets the class name of the application. The class name may be used in a
107 platform specific manner to refer to the application.
111 wxString
GetClassName() const;
114 Returns the one and only global application object.
115 Usually ::wxTheApp is usead instead.
119 static wxAppConsole
* GetInstance();
122 Returns a pointer to the wxAppTraits object for the application.
123 If you want to customize the wxAppTraits object, you must override the
124 CreateTraits() function.
126 wxAppTraits
* GetTraits();
129 Returns the user-readable vendor name. The difference between this string
130 and the one returned by GetVendorName() is that this one is meant to be shown
131 to the user and so should be used for the window titles, page headers and so on
132 while the other one should be only used internally, e.g. for the file names or
133 configuration file keys.
135 By default, returns the same string as GetVendorName().
139 const wxString
& GetVendorDisplayName() const;
142 Returns the application's vendor name.
144 const wxString
& GetVendorName() const;
147 This function simply invokes the given method @a func of the specified
148 event handler @a handler with the @a event as parameter. It exists solely
149 to allow to catch the C++ exceptions which could be thrown by all event
150 handlers in the application in one place: if you want to do this, override
151 this function in your wxApp-derived class and add try/catch clause(s) to it.
153 virtual void HandleEvent(wxEvtHandler
* handler
,
154 wxEventFunction func
,
155 wxEvent
& event
) const;
158 Returns @true if the main event loop is currently running, i.e. if the
159 application is inside OnRun().
161 This can be useful to test whether events can be dispatched. For example,
162 if this function returns @false, non-blocking sockets cannot be used because
163 the events from them would never be processed.
165 static bool IsMainLoopRunning();
168 Mac specific. Called in response of an "open-application" Apple event.
169 Override this to create a new document in your app.
171 virtual void MacNewFile();
174 Mac specific. Called in response of an "open-document" Apple event.
176 You need to override this method in order to open a document file after the
177 user double clicked on it or if the document file was dropped on either the
178 running application or the application icon in Finder.
180 virtual void MacOpenFile(const wxString
& fileName
);
183 Mac specific. Called in response of a "get-url" Apple event.
185 virtual void MacOpenURL(const wxString
& url
);
188 Mac specific. Called in response of a "print-document" Apple event.
190 virtual void MacPrintFile(const wxString
& fileName
);
193 Mac specific. Called in response of a "reopen-application" Apple event.
195 virtual void MacReopenApp();
198 Called by wxWidgets on creation of the application. Override this if you wish
199 to provide your own (environment-dependent) main loop.
201 @returns Returns 0 under X, and the wParam of the WM_QUIT message under
204 virtual int MainLoop();
207 This function is called when an assert failure occurs, i.e. the condition
208 specified in wxASSERT() macro evaluated to @false.
210 It is only called in debug mode (when @c __WXDEBUG__ is defined) as
211 asserts are not left in the release code at all.
212 The base class version shows the default assert failure dialog box proposing to
213 the user to stop the program, continue or ignore all subsequent asserts.
216 the name of the source file where the assert occurred
218 the line number in this file where the assert occurred
220 the name of the function where the assert occurred, may be
221 empty if the compiler doesn't support C99 __FUNCTION__
223 the condition of the failed assert in text form
225 the message specified as argument to wxASSERT_MSG or wxFAIL_MSG, will
226 be @NULL if just wxASSERT or wxFAIL was used
228 virtual void OnAssertFailure(const wxChar
*file
,
235 Called when command line parsing fails (i.e. an incorrect command line option
236 was specified by the user). The default behaviour is to show the program usage
237 text and abort the program.
239 Return @true to continue normal execution or @false to return
240 @false from OnInit() thus terminating the program.
244 virtual bool OnCmdLineError(wxCmdLineParser
& parser
);
247 Called when the help option (@c --help) was specified on the command line.
248 The default behaviour is to show the program usage text and abort the program.
250 Return @true to continue normal execution or @false to return
251 @false from OnInit() thus terminating the program.
255 virtual bool OnCmdLineHelp(wxCmdLineParser
& parser
);
258 Called after the command line had been successfully parsed. You may override
259 this method to test for the values of the various parameters which could be
260 set from the command line.
262 Don't forget to call the base class version unless you want to suppress
263 processing of the standard command line options.
264 Return @true to continue normal execution or @false to return @false from
265 OnInit() thus terminating the program.
269 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
272 This function is called if an unhandled exception occurs inside the main
273 application event loop. It can return @true to ignore the exception and to
274 continue running the loop or @false to exit the loop and terminate the
275 program. In the latter case it can also use C++ @c throw keyword to
276 rethrow the current exception.
278 The default behaviour of this function is the latter in all ports except under
279 Windows where a dialog is shown to the user which allows him to choose between
280 the different options. You may override this function in your class to do
281 something more appropriate.
283 Finally note that if the exception is rethrown from here, it can be caught in
284 OnUnhandledException().
286 virtual bool OnExceptionInMainLoop();
289 Override this member function for any processing which needs to be
290 done as the application is about to exit. OnExit is called after
291 destroying all application windows and controls, but before
292 wxWidgets cleanup. Note that it is not called at all if
295 The return value of this function is currently ignored, return the same
296 value as returned by the base class method if you override it.
298 virtual int OnExit();
301 This function may be called if something fatal happens: an unhandled
302 exception under Win32 or a a fatal signal under Unix, for example. However,
303 this will not happen by default: you have to explicitly call
304 wxHandleFatalExceptions() to enable this.
306 Generally speaking, this function should only show a message to the user and
307 return. You may attempt to save unsaved data but this is not guaranteed to
308 work and, in fact, probably won't.
310 @see wxHandleFatalExceptions()
312 virtual void OnFatalException();
315 This must be provided by the application, and will usually create the
316 application's main window, optionally calling SetTopWindow().
318 You may use OnExit() to clean up anything initialized here, provided
319 that the function returns @true.
321 Notice that if you want to to use the command line processing provided by
322 wxWidgets you have to call the base class version in the derived class
325 Return @true to continue processing, @false to exit the application
328 virtual bool OnInit();
331 Called from OnInit() and may be used to initialize the parser with the
332 command line options for this application. The base class versions adds
333 support for a few standard options only.
335 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
338 This virtual function is where the execution of a program written in wxWidgets
339 starts. The default implementation just enters the main loop and starts
340 handling the events until it terminates, either because ExitMainLoop() has
341 been explicitly called or because the last frame has been deleted and
342 GetExitOnFrameDelete() flag is @true (this is the default).
344 The return value of this function becomes the exit code of the program, so it
345 should return 0 in case of successful termination.
350 This function is called when an unhandled C++ exception occurs inside
351 OnRun() (the exceptions which occur during the program startup and shutdown
352 might not be caught at all). Notice that by now the main event loop has been
353 terminated and the program will exit, if you want to prevent this from happening
354 (i.e. continue running after catching an exception) you need to override
355 OnExceptionInMainLoop().
357 The default implementation shows information about the exception in debug build
358 but does nothing in the release build.
360 virtual void OnUnhandledException();
363 Returns @true if unprocessed events are in the window system event queue.
367 virtual bool Pending();
370 Set the application name to be used in the user-visible places such as window
371 titles. See GetAppDisplayName() for more about the differences between the
372 display name and name.
374 void SetAppDisplayName(const wxString
& name
);
377 Sets the name of the application. This name should be used for file names,
378 configuration file entries and other internal strings. For the user-visible
379 strings, such as the window titles, the application display name set by
380 SetAppDisplayName() is used instead.
382 By default the application name is set to the name of its executable file.
386 void SetAppName(const wxString
& name
);
389 Sets the class name of the application. This may be used in a platform specific
390 manner to refer to the application.
394 void SetClassName(const wxString
& name
);
397 Allows external code to modify global ::wxTheApp, but you should really
398 know what you're doing if you call it.
401 Replacement for the global application object.
405 static void SetInstance(wxAppConsole
* app
);
408 Set the vendor name to be used in the user-visible places.
409 See GetVendorDisplayName() for more about the differences between the
410 display name and name.
412 void SetVendorDisplayName(const wxString
& name
);
415 Sets the name of application's vendor. The name will be used
416 in registry access. A default name is set by wxWidgets.
420 void SetVendorName(const wxString
& name
);
423 Yields control to pending messages in the windowing system.
425 This can be useful, for example, when a time-consuming process writes to a
426 text window. Without an occasional yield, the text window will not be updated
427 properly, and on systems with cooperative multitasking, such as Windows 3.1
428 other processes will not respond.
430 Caution should be exercised, however, since yielding may allow the
431 user to perform actions which are not compatible with the current task.
432 Disabling menu items or whole menus during processing can avoid unwanted
433 reentrance of code: see ::wxSafeYield for a better function.
435 Note that Yield() will not flush the message logs. This is intentional as
436 calling Yield() is usually done to quickly update the screen and popping up
437 a message box dialog may be undesirable. If you do wish to flush the log
438 messages immediately (otherwise it will be done during the next idle loop
439 iteration), call wxLog::FlushActive.
441 Calling Yield() recursively is normally an error and an assert failure is
442 raised in debug build if such situation is detected. However if the
443 @a onlyIfNeeded parameter is @true, the method will just silently
444 return @false instead.
446 virtual bool Yield(bool onlyIfNeeded
= false);
449 Number of command line arguments (after environment-specific processing).
454 Command line arguments (after environment-specific processing).
456 Under Windows and Linux/Unix, you should parse the command line
457 arguments and check for files to be opened when starting your
458 application. Under OS X, you need to override MacOpenFile()
459 since command line arguments are used differently there.
461 You may use the wxCmdLineParser to parse command line arguments.
473 The wxApp class represents the application itself. It is used to:
475 @li set and get application-wide properties;
476 @li implement the windowing system message or event loop;
477 @li initiate application processing via wxApp::OnInit;
478 @li allow default processing of events not handled by other
479 objects in the application.
481 You should use the macro IMPLEMENT_APP(appClass) in your application
482 implementation file to tell wxWidgets how to create an instance of your
485 Use DECLARE_APP(appClass) in a header file if you want the wxGetApp function
486 (which returns a reference to your application object) to be visible to other
490 @category{appmanagement}
492 @see @ref overview_app
494 class wxApp
: public wxAppConsole
498 Constructor. Called implicitly with a definition of a wxApp object.
503 Destructor. Will be called implicitly on program exit if the wxApp
504 object is created on the stack.
509 Returns @true if the application will exit when the top-level frame is deleted.
511 @see SetExitOnFrameDelete()
513 bool GetExitOnFrameDelete() const;
516 Returns @true if the application will use the best visual on systems that support
517 different visuals, @false otherwise.
519 @see SetUseBestVisual()
521 bool GetUseBestVisual() const;
524 Returns a pointer to the top window.
526 @remarks If the top window hasn't been set using SetTopWindow(),
527 this function will find the first top-level window
528 (frame or dialog) and return that.
532 virtual wxWindow
* GetTopWindow() const;
535 Returns @true if the application is active, i.e. if one of its windows is
536 currently in the foreground.
538 If this function returns @false and you need to attract users attention to
539 the application, you may use wxTopLevelWindow::RequestUserAttention to do it.
541 virtual bool IsActive() const;
544 Windows-only function for processing a message. This function is called
545 from the main message loop, checking for windows that may wish to process it.
547 The function returns @true if the message was processed, @false otherwise.
548 If you use wxWidgets with another class library with its own message loop,
549 you should make sure that this function is called to allow wxWidgets to
550 receive messages. For example, to allow co-existence with the Microsoft
551 Foundation Classes, override the PreTranslateMessage function:
554 // Provide wxWidgets message loop compatibility
555 BOOL CTheApp::PreTranslateMessage(MSG *msg)
557 if (wxTheApp && wxTheApp->ProcessMessage((WXMSW *)msg))
560 return CWinApp::PreTranslateMessage(msg);
564 bool ProcessMessage(WXMSG
* msg
);
567 Sends idle events to a window and its children.
568 Please note that this function is internal to wxWidgets and shouldn't be used
571 @remarks These functions poll the top-level windows, and their children,
572 for idle event processing. If @true is returned, more OnIdle
573 processing is requested by one or more window.
577 virtual bool SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
);
580 Allows the programmer to specify whether the application will exit when the
581 top-level frame is deleted.
584 If @true (the default), the application will exit when the top-level frame
585 is deleted. If @false, the application will continue to run.
587 @see GetExitOnFrameDelete(), @ref overview_app_shutdown
589 void SetExitOnFrameDelete(bool flag
);
592 Allows external code to modify global ::wxTheApp, but you should really
593 know what you're doing if you call it.
596 Replacement for the global application object.
600 static void SetInstance(wxAppConsole
* app
);
603 Allows runtime switching of the UI environment theme.
605 Currently implemented for wxGTK2-only.
606 Return @true if theme was successfully changed.
609 The name of the new theme or an absolute path to a gtkrc-theme-file
611 virtual bool SetNativeTheme(const wxString
& theme
);
614 Sets the 'top' window. You can call this from within OnInit() to let wxWidgets
615 know which is the main window. You don't have to set the top window;
616 it is only a convenience so that (for example) certain dialogs without parents
617 can use a specific window as the top window. If no top window is specified by the
618 application, wxWidgets just uses the first frame or dialog in its top-level window
619 list, when it needs to use the top window.
624 @see GetTopWindow(), OnInit()
626 void SetTopWindow(wxWindow
* window
);
629 Allows the programmer to specify whether the application will use the best
630 visual on systems that support several visual on the same display. This is typically
631 the case under Solaris and IRIX, where the default visual is only 8-bit whereas
632 certain applications are supposed to run in TrueColour mode.
634 Note that this function has to be called in the constructor of the wxApp
635 instance and won't have any effect when called later on.
636 This function currently only has effect under GTK.
639 If @true, the app will use the best visual.
640 @param forceTrueColour
641 If @true then the application will try to force using a TrueColour
642 visual and abort the app if none is found.
644 void SetUseBestVisual(bool flag
, bool forceTrueColour
= false);
649 // ============================================================================
650 // Global functions/macros
651 // ============================================================================
654 /** @ingroup group_funcmacro_rtti */
658 This is used in headers to create a forward declaration of the wxGetApp()
659 function implemented by IMPLEMENT_APP().
661 It creates the declaration @a className wxGetApp(void).
671 #define DECLARE_APP( className )
674 This is used in the application class implementation file to make the
675 application class known to wxWidgets for dynamic construction.
687 #define IMPLEMENT_APP( className )
694 The global pointer to the singleton wxApp object.
696 @see wxApp::GetInstance()
702 /** @ingroup group_funcmacro_appinitterm */
706 This function doesn't exist in wxWidgets but it is created by using the
707 IMPLEMENT_APP() macro.
709 Thus, before using it anywhere but in the same module where this macro is
710 used, you must make it available using DECLARE_APP().
712 The advantage of using this function compared to directly using the global
713 ::wxTheApp pointer is that the latter is of type wxApp* and so wouldn't
714 allow you to access the functions specific to your application class but
715 not present in wxApp while wxGetApp() returns the object of the right type.
719 wxAppDerivedClass
& wxGetApp();
722 If @a doIt is @true, the fatal exceptions (also known as general protection
723 faults under Windows or segmentation violations in the Unix world) will be
724 caught and passed to wxApp::OnFatalException.
726 By default, i.e. before this function is called, they will be handled in
727 the normal way which usually just means that the application will be
728 terminated. Calling wxHandleFatalExceptions() with @a doIt equal to @false
729 will restore this default behaviour.
731 Notice that this function is only available if @c wxUSE_ON_FATAL_EXCEPTION
732 is 1 and under Windows platform this requires a compiler with support for
733 SEH (structured exception handling) which currently means only Microsoft
734 Visual C++ or a recent Borland C++ version.
738 bool wxHandleFatalExceptions(bool doIt
= true);
741 This function is used in wxBase only and only if you don't create
742 wxApp object at all. In this case you must call it from your
743 @c main() function before calling any other wxWidgets functions.
745 If the function returns @false the initialization could not be performed,
746 in this case the library cannot be used and wxUninitialize() shouldn't be
749 This function may be called several times but wxUninitialize() must be
750 called for each successful call to this function.
757 This function is for use in console (wxBase) programs only. It must be called
758 once for each previous successful call to wxInitialize().
762 void wxUninitialize();
765 This function wakes up the (internal and platform dependent) idle system,
766 i.e. it will force the system to send an idle event even if the system
767 currently @e is idle and thus would not send any idle event until after
768 some other event would get sent. This is also useful for sending events
769 between two threads and is used by the corresponding functions
770 wxPostEvent() and wxEvtHandler::AddPendingEvent().
780 This function is kept only for backwards compatibility. Please use
781 the wxApp::Yield method instead in any new code.
788 This function is similar to wxYield, except that it disables the user input to
789 all program windows before calling wxYield and re-enables it again
790 afterwards. If @a win is not @NULL, this window will remain enabled,
791 allowing the implementation of some limited user interaction.
792 Returns the result of the call to ::wxYield.
796 bool wxSafeYield(wxWindow
* win
= NULL
, bool onlyIfNeeded
= false);
799 This function initializes wxWidgets in a platform-dependent way. Use this if you
800 are not using the default wxWidgets entry code (e.g. main or WinMain).
802 For example, you can initialize wxWidgets from an Microsoft Foundation Classes
803 (MFC) application using this function.
805 @note This overload of wxEntry is available under all platforms.
811 int wxEntry(int& argc
, wxChar
** argv
);
814 See wxEntry(int&,wxChar**) for more info about this function.
816 Notice that under Windows CE platform, and only there, the type of @a pCmdLine
817 is @c wchar_t *, otherwise it is @c char *, even in Unicode build.
819 @remarks To clean up wxWidgets, call wxApp::OnExit followed by the static
820 function wxApp::CleanUp. For example, if exiting from an MFC application
821 that also uses wxWidgets:
823 int CTheApp::ExitInstance()
825 // OnExit isn't called by CleanUp so must be called explicitly.
829 return CWinApp::ExitInstance();
835 int wxEntry(HINSTANCE hInstance
,
836 HINSTANCE hPrevInstance
= NULL
,
837 char* pCmdLine
= NULL
,
838 int nCmdShow
= SW_SHOWNORMAL
);
844 /** @ingroup group_funcmacro_procctrl */
848 Exits application after calling wxApp::OnExit.
850 Should only be used in an emergency: normally the top-level frame
851 should be deleted (after deleting all other frames) to terminate the
852 application. See wxCloseEvent and wxApp.