1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxAppBase class and macros used for declaration of wxApp
4 // derived class in the user code
5 // Author: Julian Smart
9 // Copyright: (c) Julian Smart
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_APP_H_BASE_
14 #define _WX_APP_H_BASE_
16 // ----------------------------------------------------------------------------
17 // headers we have to include here
18 // ----------------------------------------------------------------------------
20 #include "wx/event.h" // for the base class
22 #include "wx/init.h" // we must declare wxEntry()
23 #include "wx/intl.h" // for wxLayoutDirection
25 class WXDLLIMPEXP_BASE wxAppConsole
;
26 class WXDLLIMPEXP_BASE wxAppTraits
;
27 class WXDLLIMPEXP_BASE wxCmdLineParser
;
28 class WXDLLIMPEXP_BASE wxLog
;
29 class WXDLLIMPEXP_BASE wxMessageOutput
;
32 class WXDLLEXPORT wxEventLoop
;
33 struct WXDLLIMPEXP_CORE wxVideoMode
;
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // the type of the function used to create a wxApp object on program start up
41 typedef wxAppConsole
* (*wxAppInitializerFunction
)();
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
50 wxPRINT_POSTSCRIPT
= 2
53 // ----------------------------------------------------------------------------
54 // wxAppConsole: wxApp for non-GUI applications
55 // ----------------------------------------------------------------------------
57 class WXDLLIMPEXP_BASE wxAppConsole
: public wxEvtHandler
62 virtual ~wxAppConsole();
65 // the virtual functions which may/must be overridden in the derived class
66 // -----------------------------------------------------------------------
68 // This is the very first function called for a newly created wxApp object,
69 // it is used by the library to do the global initialization. If, for some
70 // reason, you must override it (instead of just overriding OnInit(), as
71 // usual, for app-specific initializations), do not forget to call the base
73 virtual bool Initialize(int& argc
, wxChar
**argv
);
75 // This gives wxCocoa a chance to call OnInit() with a memory pool in place
76 virtual bool CallOnInit() { return OnInit(); }
78 // Called before OnRun(), this is a good place to do initialization -- if
79 // anything fails, return false from here to prevent the program from
80 // continuing. The command line is normally parsed here, call the base
81 // class OnInit() to do it.
82 virtual bool OnInit();
84 // this is here only temporary hopefully (FIXME)
85 virtual bool OnInitGui() { return true; }
87 // This is the replacement for the normal main(): all program work should
88 // be done here. When OnRun() returns, the programs starts shutting down.
89 virtual int OnRun() = 0;
91 // This is only called if OnInit() returned true so it's a good place to do
92 // any cleanup matching the initializations done there.
95 // This is the very last function called on wxApp object before it is
96 // destroyed. If you override it (instead of overriding OnExit() as usual)
97 // do not forget to call the base class version!
98 virtual void CleanUp();
100 // Called when a fatal exception occurs, this function should take care not
101 // to do anything which might provoke a nested exception! It may be
102 // overridden if you wish to react somehow in non-default way (core dump
103 // under Unix, application crash under Windows) to fatal program errors,
104 // however extreme care should be taken if you don't want this function to
106 virtual void OnFatalException() { }
108 // Called from wxExit() function, should terminate the application a.s.a.p.
112 // application info: name, description, vendor
113 // -------------------------------------------
115 // NB: all these should be set by the application itself, there are no
116 // reasonable default except for the application name which is taken to
119 // set/get the application name
120 wxString
GetAppName() const
122 return m_appName
.empty() ? m_className
: m_appName
;
124 void SetAppName(const wxString
& name
) { m_appName
= name
; }
126 // set/get the app class name
127 wxString
GetClassName() const { return m_className
; }
128 void SetClassName(const wxString
& name
) { m_className
= name
; }
130 // set/get the vendor name
131 const wxString
& GetVendorName() const { return m_vendorName
; }
132 void SetVendorName(const wxString
& name
) { m_vendorName
= name
; }
135 // cmd line parsing stuff
136 // ----------------------
138 // all of these methods may be overridden in the derived class to
139 // customize the command line parsing (by default only a few standard
140 // options are handled)
142 // you also need to call wxApp::OnInit() from YourApp::OnInit() for all
145 #if wxUSE_CMDLINE_PARSER
146 // this one is called from OnInit() to add all supported options
147 // to the given parser (don't forget to call the base class version if you
149 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
151 // called after successfully parsing the command line, return true
152 // to continue and false to exit (don't forget to call the base class
153 // version if you override it!)
154 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
156 // called if "--help" option was specified, return true to continue
158 virtual bool OnCmdLineHelp(wxCmdLineParser
& parser
);
160 // called if incorrect command line options were given, return
161 // false to abort and true to continue
162 virtual bool OnCmdLineError(wxCmdLineParser
& parser
);
163 #endif // wxUSE_CMDLINE_PARSER
166 // miscellaneous customization functions
167 // -------------------------------------
169 // create the app traits object to which we delegate for everything which
170 // either should be configurable by the user (then he can change the
171 // default behaviour simply by overriding CreateTraits() and returning his
172 // own traits object) or which is GUI/console dependent as then wxAppTraits
173 // allows us to abstract the differences behind the common façade
174 wxAppTraits
*GetTraits();
177 // event processing functions
178 // --------------------------
180 // this method allows to filter all the events processed by the program, so
181 // you should try to return quickly from it to avoid slowing down the
182 // program to the crawl
184 // return value should be -1 to continue with the normal event processing,
185 // or TRUE or FALSE to stop further processing and pretend that the event
186 // had been already processed or won't be processed at all, respectively
187 virtual int FilterEvent(wxEvent
& event
);
190 // call the specified handler on the given object with the given event
192 // this method only exists to allow catching the exceptions thrown by any
193 // event handler, it would lead to an extra (useless) virtual function call
194 // if the exceptions were not used, so it doesn't even exist in that case
195 virtual void HandleEvent(wxEvtHandler
*handler
,
196 wxEventFunction func
,
197 wxEvent
& event
) const;
199 // Called when an unhandled C++ exception occurs inside OnRun(): note that
200 // the exception type is lost by now, so if you really want to handle the
201 // exception you should override OnRun() and put a try/catch around
202 // MainLoop() call there or use OnExceptionInMainLoop()
203 virtual void OnUnhandledException() { }
204 #endif // wxUSE_EXCEPTIONS
206 // process all events in the wxPendingEvents list -- it is necessary to
207 // call this function to process posted events. This happens during each
208 // event loop iteration in GUI mode but if there is no main loop, it may be
209 // also called directly.
210 virtual void ProcessPendingEvents();
212 // doesn't do anything in this class, just a hook for GUI wxApp
213 virtual bool Yield(bool WXUNUSED(onlyIfNeeded
) = false) { return true; }
215 // make sure that idle events are sent again
216 virtual void WakeUpIdle() { }
218 // this is just a convenience: by providing its implementation here we
219 // avoid #ifdefs in the code using it
220 static bool IsMainLoopRunning() { return false; }
227 // this function is called when an assert failure occurs, the base class
228 // version does the normal processing (i.e. shows the usual assert failure
231 // the arguments are the location of the failed assert (func may be empty
232 // if the compiler doesn't support C99 __FUNCTION__), the text of the
233 // assert itself and the user-specified message
234 virtual void OnAssertFailure(const wxChar
*file
,
240 // old version of the function without func parameter, for compatibility
241 // only, override OnAssertFailure() in the new code
242 virtual void OnAssert(const wxChar
*file
,
246 #endif // __WXDEBUG__
248 // check that the wxBuildOptions object (constructed in the application
249 // itself, usually the one from IMPLEMENT_APP() macro) matches the build
250 // options of the library and abort if it doesn't
251 static bool CheckBuildOptions(const char *optionsSignature
,
252 const char *componentName
);
254 // implementation only from now on
255 // -------------------------------
257 // helpers for dynamic wxApp construction
258 static void SetInitializerFunction(wxAppInitializerFunction fn
)
259 { ms_appInitFn
= fn
; }
260 static wxAppInitializerFunction
GetInitializerFunction()
261 { return ms_appInitFn
; }
263 // accessors for ms_appInstance field (external code might wish to modify
264 // it, this is why we provide a setter here as well, but you should really
265 // know what you're doing if you call it), wxTheApp is usually used instead
267 static wxAppConsole
*GetInstance() { return ms_appInstance
; }
268 static void SetInstance(wxAppConsole
*app
) { ms_appInstance
= app
; }
271 // command line arguments (public for backwards compatibility)
276 // the function which creates the traits object when GetTraits() needs it
277 // for the first time
278 virtual wxAppTraits
*CreateTraits();
281 // function used for dynamic wxApp creation
282 static wxAppInitializerFunction ms_appInitFn
;
284 // the one and only global application object
285 static wxAppConsole
*ms_appInstance
;
288 // application info (must be set from the user code)
289 wxString m_vendorName
, // vendor name (ACME Inc)
290 m_appName
, // app name
291 m_className
; // class name
293 // the class defining the application behaviour, NULL initially and created
294 // by GetTraits() when first needed
295 wxAppTraits
*m_traits
;
298 // the application object is a singleton anyhow, there is no sense in
300 DECLARE_NO_COPY_CLASS(wxAppConsole
)
303 // ----------------------------------------------------------------------------
304 // wxAppBase: the common part of wxApp implementations for all platforms
305 // ----------------------------------------------------------------------------
309 class WXDLLIMPEXP_CORE wxAppBase
: public wxAppConsole
313 virtual ~wxAppBase();
315 // the virtual functions which may/must be overridden in the derived class
316 // -----------------------------------------------------------------------
318 // very first initialization function
320 // Override: very rarely
321 virtual bool Initialize(int& argc
, wxChar
**argv
);
323 // a platform-dependent version of OnInit(): the code here is likely to
324 // depend on the toolkit. default version does nothing.
327 virtual bool OnInitGui();
329 // called to start program execution - the default version just enters
330 // the main GUI loop in which events are received and processed until
331 // the last window is not deleted (if GetExitOnFrameDelete) or
332 // ExitMainLoop() is called. In console mode programs, the execution
333 // of the program really starts here
335 // Override: rarely in GUI applications, always in console ones.
338 // a matching function for OnInit()
339 virtual int OnExit();
341 // very last clean up function
343 // Override: very rarely
344 virtual void CleanUp();
347 // the worker functions - usually not used directly by the user code
348 // -----------------------------------------------------------------
350 // return true if we're running main loop, i.e. if the events can
351 // (already) be dispatched
352 static bool IsMainLoopRunning()
354 wxAppBase
*app
= wx_static_cast(wxAppBase
*, GetInstance());
355 return app
&& app
->m_mainLoop
!= NULL
;
358 // execute the main GUI loop, the function returns when the loop ends
359 virtual int MainLoop();
361 // exit the main loop thus terminating the application
364 // exit the main GUI loop during the next iteration (i.e. it does not
365 // stop the program immediately!)
366 virtual void ExitMainLoop();
368 // returns true if there are unprocessed events in the event queue
369 virtual bool Pending();
371 // process the first event in the event queue (blocks until an event
372 // appears if there are none currently, use Pending() if this is not
373 // wanted), returns false if the event loop should stop and true
375 virtual bool Dispatch();
377 // process all currently pending events right now
379 // it is an error to call Yield() recursively unless the value of
380 // onlyIfNeeded is true
382 // WARNING: this function is dangerous as it can lead to unexpected
383 // reentrancies (i.e. when called from an event handler it
384 // may result in calling the same event handler again), use
385 // with _extreme_ care or, better, don't use at all!
386 virtual bool Yield(bool onlyIfNeeded
= false) = 0;
388 // this virtual function is called in the GUI mode when the application
389 // becomes idle and normally just sends wxIdleEvent to all interested
392 // it should return true if more idle events are needed, false if not
393 virtual bool ProcessIdle();
395 // Send idle event to window and all subwindows
396 // Returns true if more idle time is requested.
397 virtual bool SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
);
401 // Function called if an uncaught exception is caught inside the main
402 // event loop: it may return true to continue running the event loop or
403 // false to stop it (in the latter case it may rethrow the exception as
405 virtual bool OnExceptionInMainLoop();
406 #endif // wxUSE_EXCEPTIONS
409 // top level window functions
410 // --------------------------
412 // return true if our app has focus
413 virtual bool IsActive() const { return m_isActive
; }
415 // set the "main" top level window
416 void SetTopWindow(wxWindow
*win
) { m_topWindow
= win
; }
418 // return the "main" top level window (if it hadn't been set previously
419 // with SetTopWindow(), will return just some top level window and, if
420 // there are none, will return NULL)
421 virtual wxWindow
*GetTopWindow() const;
423 // control the exit behaviour: by default, the program will exit the
424 // main loop (and so, usually, terminate) when the last top-level
425 // program window is deleted. Beware that if you disable this behaviour
426 // (with SetExitOnFrameDelete(false)), you'll have to call
427 // ExitMainLoop() explicitly from somewhere.
428 void SetExitOnFrameDelete(bool flag
)
429 { m_exitOnFrameDelete
= flag
? Yes
: No
; }
430 bool GetExitOnFrameDelete() const
431 { return m_exitOnFrameDelete
== Yes
; }
434 // display mode, visual, printing mode, ...
435 // ------------------------------------------------------------------------
437 // Get display mode that is used use. This is only used in framebuffer
438 // wxWin ports (such as wxMGL or wxDFB).
439 virtual wxVideoMode
GetDisplayMode() const;
440 // Set display mode to use. This is only used in framebuffer wxWin
441 // ports (such as wxMGL or wxDFB). This method should be called from
443 virtual bool SetDisplayMode(const wxVideoMode
& WXUNUSED(info
)) { return true; }
445 // set use of best visual flag (see below)
446 void SetUseBestVisual( bool flag
, bool forceTrueColour
= false )
447 { m_useBestVisual
= flag
; m_forceTrueColour
= forceTrueColour
; }
448 bool GetUseBestVisual() const { return m_useBestVisual
; }
450 // set/get printing mode: see wxPRINT_XXX constants.
452 // default behaviour is the normal one for Unix: always use PostScript
454 virtual void SetPrintMode(int WXUNUSED(mode
)) { }
455 int GetPrintMode() const { return wxPRINT_POSTSCRIPT
; }
457 // Return the layout direction for the current locale or wxLayout_Default
459 virtual wxLayoutDirection
GetLayoutDirection() const;
462 // command line parsing (GUI-specific)
463 // ------------------------------------------------------------------------
465 #if wxUSE_CMDLINE_PARSER
466 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
467 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
470 // miscellaneous other stuff
471 // ------------------------------------------------------------------------
473 // called by toolkit-specific code to set the app status: active (we have
474 // focus) or not and also the last window which had focus before we were
476 virtual void SetActive(bool isActive
, wxWindow
*lastFocus
);
478 #if WXWIN_COMPATIBILITY_2_6
479 // OBSOLETE: don't use, always returns true
481 // returns true if the program is successfully initialized
482 wxDEPRECATED( bool Initialized() );
483 #endif // WXWIN_COMPATIBILITY_2_6
485 // perform standard OnIdle behaviour, ensure that this is always called
486 void OnIdle(wxIdleEvent
& event
);
490 // delete all objects in wxPendingDelete list
491 void DeletePendingObjects();
493 // override base class method to use GUI traits
494 virtual wxAppTraits
*CreateTraits();
497 // the main event loop of the application (may be NULL if the loop hasn't
498 // been started yet or has already terminated)
499 wxEventLoop
*m_mainLoop
;
501 // the main top level window (may be NULL)
502 wxWindow
*m_topWindow
;
504 // if Yes, exit the main loop when the last top level window is deleted, if
505 // No don't do it and if Later -- only do it once we reach our OnRun()
507 // the explanation for using this strange scheme is given in appcmn.cpp
513 } m_exitOnFrameDelete
;
515 // true if the app wants to use the best visual on systems where
516 // more than one are available (Sun, SGI, XFree86 4.0 ?)
517 bool m_useBestVisual
;
518 // force TrueColour just in case "best" isn't TrueColour
519 bool m_forceTrueColour
;
521 // does any of our windows have focus?
525 DECLARE_NO_COPY_CLASS(wxAppBase
)
528 #if WXWIN_COMPATIBILITY_2_6
529 inline bool wxAppBase::Initialized() { return true; }
530 #endif // WXWIN_COMPATIBILITY_2_6
534 // ----------------------------------------------------------------------------
535 // now include the declaration of the real class
536 // ----------------------------------------------------------------------------
539 #if defined(__WXPALMOS__)
540 #include "wx/palmos/app.h"
541 #elif defined(__WXMSW__)
542 #include "wx/msw/app.h"
543 #elif defined(__WXMOTIF__)
544 #include "wx/motif/app.h"
545 #elif defined(__WXMGL__)
546 #include "wx/mgl/app.h"
547 #elif defined(__WXDFB__)
548 #include "wx/dfb/app.h"
549 #elif defined(__WXGTK20__)
550 #include "wx/gtk/app.h"
551 #elif defined(__WXGTK__)
552 #include "wx/gtk1/app.h"
553 #elif defined(__WXX11__)
554 #include "wx/x11/app.h"
555 #elif defined(__WXMAC__)
556 #include "wx/mac/app.h"
557 #elif defined(__WXCOCOA__)
558 #include "wx/cocoa/app.h"
559 #elif defined(__WXPM__)
560 #include "wx/os2/app.h"
563 // allow using just wxApp (instead of wxAppConsole) in console programs
564 typedef wxAppConsole wxApp
;
567 // ----------------------------------------------------------------------------
569 // ----------------------------------------------------------------------------
571 // for compatibility, we define this macro to access the global application
572 // object of type wxApp
574 // note that instead of using of wxTheApp in application code you should
575 // consider using DECLARE_APP() after which you may call wxGetApp() which will
576 // return the object of the correct type (i.e. MyApp and not wxApp)
578 // the cast is safe as in GUI build we only use wxApp, not wxAppConsole, and in
579 // console mode it does nothing at all
580 #define wxTheApp wx_static_cast(wxApp*, wxApp::GetInstance())
582 // ----------------------------------------------------------------------------
584 // ----------------------------------------------------------------------------
586 // event loop related functions only work in GUI programs
587 // ------------------------------------------------------
589 // Force an exit from main loop
590 extern void WXDLLIMPEXP_BASE
wxExit();
592 // Yield to other apps/messages
593 extern bool WXDLLIMPEXP_BASE
wxYield();
595 // Yield to other apps/messages
596 extern void WXDLLIMPEXP_BASE
wxWakeUpIdle();
598 // ----------------------------------------------------------------------------
599 // macros for dynamic creation of the application object
600 // ----------------------------------------------------------------------------
602 // Having a global instance of this class allows wxApp to be aware of the app
603 // creator function. wxApp can then call this function to create a new app
604 // object. Convoluted, but necessary.
606 class WXDLLIMPEXP_BASE wxAppInitializer
609 wxAppInitializer(wxAppInitializerFunction fn
)
610 { wxApp::SetInitializerFunction(fn
); }
613 // the code below defines a IMPLEMENT_WXWIN_MAIN macro which you can use if
614 // your compiler really, really wants main() to be in your main program (e.g.
615 // hello.cpp). Now IMPLEMENT_APP should add this code if required.
617 #define IMPLEMENT_WXWIN_MAIN_CONSOLE \
618 int main(int argc, char **argv) { return wxEntry(argc, argv); }
620 // port-specific header could have defined it already in some special way
621 #ifndef IMPLEMENT_WXWIN_MAIN
622 #define IMPLEMENT_WXWIN_MAIN IMPLEMENT_WXWIN_MAIN_CONSOLE
623 #endif // defined(IMPLEMENT_WXWIN_MAIN)
625 #ifdef __WXUNIVERSAL__
626 #include "wx/univ/theme.h"
628 #ifdef wxUNIV_DEFAULT_THEME
629 #define IMPLEMENT_WX_THEME_SUPPORT \
630 WX_USE_THEME(wxUNIV_DEFAULT_THEME);
632 #define IMPLEMENT_WX_THEME_SUPPORT
635 #define IMPLEMENT_WX_THEME_SUPPORT
638 // Use this macro if you want to define your own main() or WinMain() function
639 // and call wxEntry() from there.
640 #define IMPLEMENT_APP_NO_MAIN(appname) \
641 wxAppConsole *wxCreateApp() \
643 wxAppConsole::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, \
645 return new appname; \
648 wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
649 DECLARE_APP(appname) \
650 appname& wxGetApp() { return *wx_static_cast(appname*, wxApp::GetInstance()); }
652 // Same as IMPLEMENT_APP() normally but doesn't include themes support in
653 // wxUniversal builds
654 #define IMPLEMENT_APP_NO_THEMES(appname) \
655 IMPLEMENT_APP_NO_MAIN(appname) \
658 // Use this macro exactly once, the argument is the name of the wxApp-derived
659 // class which is the class of your application.
660 #define IMPLEMENT_APP(appname) \
661 IMPLEMENT_APP_NO_THEMES(appname) \
662 IMPLEMENT_WX_THEME_SUPPORT
664 // Same as IMPLEMENT_APP(), but for console applications.
665 #define IMPLEMENT_APP_CONSOLE(appname) \
666 IMPLEMENT_APP_NO_MAIN(appname) \
667 IMPLEMENT_WXWIN_MAIN_CONSOLE
669 // this macro can be used multiple times and just allows you to use wxGetApp()
671 #define DECLARE_APP(appname) extern appname& wxGetApp();
674 // declare the stuff defined by IMPLEMENT_APP() macro, it's not really needed
675 // anywhere else but at the very least it suppresses icc warnings about
676 // defining extern symbols without prior declaration, and it shouldn't do any
678 extern wxAppConsole
*wxCreateApp();
679 extern wxAppInitializer wxTheAppInitializer
;
681 #endif // _WX_APP_H_BASE_