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
23 #include "wx/window.h" // for wxTopLevelWindows
25 #include "wx/vidmode.h"
29 #include "wx/init.h" // we must declare wxEntry()
31 class WXDLLIMPEXP_BASE wxAppConsole
;
32 class WXDLLIMPEXP_BASE wxAppTraits
;
33 class WXDLLIMPEXP_BASE wxCmdLineParser
;
34 class WXDLLIMPEXP_BASE wxLog
;
35 class WXDLLIMPEXP_BASE wxMessageOutput
;
37 // wxUSE_EVTLOOP_IN_APP is a temporary hack needed until all ports are updated
38 // to use wxEventLoop, otherwise we get linking errors on wxMac, it's going to
41 #define wxUSE_EVTLOOP_IN_APP 0
43 #define wxUSE_EVTLOOP_IN_APP 1
44 class WXDLLEXPORT wxEventLoop
;
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // the type of the function used to create a wxApp object on program start up
52 typedef wxAppConsole
* (*wxAppInitializerFunction
)();
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
61 wxPRINT_POSTSCRIPT
= 2
64 // ----------------------------------------------------------------------------
65 // wxAppConsole: wxApp for non-GUI applications
66 // ----------------------------------------------------------------------------
68 class WXDLLIMPEXP_BASE wxAppConsole
: public wxEvtHandler
73 virtual ~wxAppConsole();
76 // the virtual functions which may/must be overridden in the derived class
77 // -----------------------------------------------------------------------
79 // This is the very first function called for a newly created wxApp object,
80 // it is used by the library to do the global initialization. If, for some
81 // reason, you must override it (instead of just overriding OnInit(), as
82 // usual, for app-specific initializations), do not forget to call the base
84 virtual bool Initialize(int& argc
, wxChar
**argv
);
86 // This gives wxCocoa a chance to call OnInit() with a memory pool in place
87 virtual bool CallOnInit() { return OnInit(); }
89 // Called before OnRun(), this is a good place to do initialization -- if
90 // anything fails, return false from here to prevent the program from
91 // continuing. The command line is normally parsed here, call the base
92 // class OnInit() to do it.
93 virtual bool OnInit();
95 // this is here only temporary hopefully (FIXME)
96 virtual bool OnInitGui() { return true; }
98 // This is the replacement for the normal main(): all program work should
99 // be done here. When OnRun() returns, the programs starts shutting down.
100 virtual int OnRun() = 0;
102 // This is only called if OnInit() returned true so it's a good place to do
103 // any cleanup matching the initializations done there.
104 virtual int OnExit();
106 // This is the very last function called on wxApp object before it is
107 // destroyed. If you override it (instead of overriding OnExit() as usual)
108 // do not forget to call the base class version!
109 virtual void CleanUp();
111 // Called when a fatal exception occurs, this function should take care not
112 // to do anything which might provoke a nested exception! It may be
113 // overridden if you wish to react somehow in non-default way (core dump
114 // under Unix, application crash under Windows) to fatal program errors,
115 // however extreme care should be taken if you don't want this function to
117 virtual void OnFatalException() { }
120 // function called if an uncaught exception is caught inside the main
121 // event loop: it may return true to continue running the event loop or
122 // false to stop it (in the latter case it may rethrow the exception as
124 virtual bool OnExceptionInMainLoop();
126 // Called when an unhandled C++ exception occurs inside OnRun(): note that
127 // the exception type is lost by now, so if you really want to handle the
128 // exception you should override OnRun() and put a try/catch around
129 // MainLoop() call there
130 virtual void OnUnhandledException() { }
131 #endif // wxUSE_EXCEPTIONS
133 // Called from wxExit() function, should terminate the application a.s.a.p.
137 // application info: name, description, vendor
138 // -------------------------------------------
140 // NB: all these should be set by the application itself, there are no
141 // reasonable default except for the application name which is taken to
144 // set/get the application name
145 wxString
GetAppName() const
147 return m_appName
.empty() ? m_className
: m_appName
;
149 void SetAppName(const wxString
& name
) { m_appName
= name
; }
151 // set/get the app class name
152 wxString
GetClassName() const { return m_className
; }
153 void SetClassName(const wxString
& name
) { m_className
= name
; }
155 // set/get the vendor name
156 const wxString
& GetVendorName() const { return m_vendorName
; }
157 void SetVendorName(const wxString
& name
) { m_vendorName
= name
; }
160 // cmd line parsing stuff
161 // ----------------------
163 // all of these methods may be overridden in the derived class to
164 // customize the command line parsing (by default only a few standard
165 // options are handled)
167 // you also need to call wxApp::OnInit() from YourApp::OnInit() for all
170 #if wxUSE_CMDLINE_PARSER
171 // this one is called from OnInit() to add all supported options
172 // to the given parser (don't forget to call the base class version if you
174 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
176 // called after successfully parsing the command line, return true
177 // to continue and false to exit (don't forget to call the base class
178 // version if you override it!)
179 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
181 // called if "--help" option was specified, return true to continue
183 virtual bool OnCmdLineHelp(wxCmdLineParser
& parser
);
185 // called if incorrect command line options were given, return
186 // false to abort and true to continue
187 virtual bool OnCmdLineError(wxCmdLineParser
& parser
);
188 #endif // wxUSE_CMDLINE_PARSER
191 // miscellaneous customization functions
192 // -------------------------------------
194 // create the app traits object to which we delegate for everything which
195 // either should be configurable by the user (then he can change the
196 // default behaviour simply by overriding CreateTraits() and returning his
197 // own traits object) or which is GUI/console dependent as then wxAppTraits
198 // allows us to abstract the differences behind the common façade
199 wxAppTraits
*GetTraits();
201 // the functions below shouldn't be used now that we have wxAppTraits
202 #if WXWIN_COMPATIBILITY_2_4
205 // override this function to create default log target of arbitrary
206 // user-defined class (default implementation creates a wxLogGui
207 // object) -- this log object is used by default by all wxLogXXX()
209 wxDEPRECATED( virtual wxLog
*CreateLogTarget() );
212 // similar to CreateLogTarget() but for the global wxMessageOutput
214 wxDEPRECATED( virtual wxMessageOutput
*CreateMessageOutput() );
216 #endif // WXWIN_COMPATIBILITY_2_4
219 // event processing functions
220 // --------------------------
222 // this method allows to filter all the events processed by the program, so
223 // you should try to return quickly from it to avoid slowing down the
224 // program to the crawl
226 // return value should be -1 to continue with the normal event processing,
227 // or TRUE or FALSE to stop further processing and pretend that the event
228 // had been already processed or won't be processed at all, respectively
229 virtual int FilterEvent(wxEvent
& event
);
232 // call the specified handler on the given object with the given event
234 // this method only exists to allow catching the exceptions thrown by any
235 // event handler, it would lead to an extra (useless) virtual function call
236 // if the exceptions were not used, so it doesn't even exist in that case
237 virtual void HandleEvent(wxEvtHandler
*handler
,
238 wxEventFunction func
,
239 wxEvent
& event
) const;
240 #endif // wxUSE_EXCEPTIONS
242 // process all events in the wxPendingEvents list -- it is necessary to
243 // call this function to process posted events. This happens during each
244 // event loop iteration in GUI mode but if there is no main loop, it may be
245 // also called directly.
246 virtual void ProcessPendingEvents();
248 // doesn't do anything in this class, just a hook for GUI wxApp
249 virtual bool Yield(bool WXUNUSED(onlyIfNeeded
) = false) { return true; }
251 // make sure that idle events are sent again
252 virtual void WakeUpIdle() { }
254 // this is just a convenience: by providing its implementation here we
255 // avoid #ifdefs in the code using it
256 static bool IsMainLoopRunning() { return false; }
262 // this function is called when an assert failure occurs, the base class
263 // version does the normal processing (i.e. shows the usual assert failure
266 // the arguments are the place where the assert occurred, the text of the
267 // assert itself and the user-specified message
269 virtual void OnAssert(const wxChar
*file
,
273 #endif // __WXDEBUG__
275 // check that the wxBuildOptions object (constructed in the application
276 // itself, usually the one from IMPLEMENT_APP() macro) matches the build
277 // options of the library and abort if it doesn't
278 static bool CheckBuildOptions(const char *optionsSignature
,
279 const char *componentName
);
280 #if WXWIN_COMPATIBILITY_2_4
281 wxDEPRECATED( static bool CheckBuildOptions(const wxBuildOptions
& buildOptions
) );
284 // implementation only from now on
285 // -------------------------------
287 // helpers for dynamic wxApp construction
288 static void SetInitializerFunction(wxAppInitializerFunction fn
)
289 { ms_appInitFn
= fn
; }
290 static wxAppInitializerFunction
GetInitializerFunction()
291 { return ms_appInitFn
; }
293 // accessors for ms_appInstance field (external code might wish to modify
294 // it, this is why we provide a setter here as well, but you should really
295 // know what you're doing if you call it), wxTheApp is usually used instead
297 static wxAppConsole
*GetInstance() { return ms_appInstance
; }
298 static void SetInstance(wxAppConsole
*app
) { ms_appInstance
= app
; }
301 // command line arguments (public for backwards compatibility)
306 // the function which creates the traits object when GetTraits() needs it
307 // for the first time
308 virtual wxAppTraits
*CreateTraits();
311 // function used for dynamic wxApp creation
312 static wxAppInitializerFunction ms_appInitFn
;
314 // the one and only global application object
315 static wxAppConsole
*ms_appInstance
;
318 // application info (must be set from the user code)
319 wxString m_vendorName
, // vendor name (ACME Inc)
320 m_appName
, // app name
321 m_className
; // class name
323 // the class defining the application behaviour, NULL initially and created
324 // by GetTraits() when first needed
325 wxAppTraits
*m_traits
;
328 // the application object is a singleton anyhow, there is no sense in
330 DECLARE_NO_COPY_CLASS(wxAppConsole
)
333 // ----------------------------------------------------------------------------
334 // wxAppBase: the common part of wxApp implementations for all platforms
335 // ----------------------------------------------------------------------------
339 class WXDLLIMPEXP_CORE wxAppBase
: public wxAppConsole
343 virtual ~wxAppBase();
345 // the virtual functions which may/must be overridden in the derived class
346 // -----------------------------------------------------------------------
348 // very first initialization function
350 // Override: very rarely
351 virtual bool Initialize(int& argc
, wxChar
**argv
);
353 // a platform-dependent version of OnInit(): the code here is likely to
354 // depend on the toolkit. default version does nothing.
357 virtual bool OnInitGui();
359 // called to start program execution - the default version just enters
360 // the main GUI loop in which events are received and processed until
361 // the last window is not deleted (if GetExitOnFrameDelete) or
362 // ExitMainLoop() is called. In console mode programs, the execution
363 // of the program really starts here
365 // Override: rarely in GUI applications, always in console ones.
368 // a matching function for OnInit()
369 virtual int OnExit();
371 // very last clean up function
373 // Override: very rarely
374 virtual void CleanUp();
377 // the worker functions - usually not used directly by the user code
378 // -----------------------------------------------------------------
380 // return true if we're running main loop, i.e. if the events can
381 // (already) be dispatched
382 static bool IsMainLoopRunning()
384 #if wxUSE_EVTLOOP_IN_APP
385 wxAppBase
*app
= wx_static_cast(wxAppBase
*, GetInstance());
386 return app
&& app
->m_mainLoop
!= NULL
;
392 // execute the main GUI loop, the function returns when the loop ends
393 virtual int MainLoop();
395 // exit the main loop thus terminating the application
398 // exit the main GUI loop during the next iteration (i.e. it does not
399 // stop the program immediately!)
400 virtual void ExitMainLoop();
402 // returns true if there are unprocessed events in the event queue
403 virtual bool Pending();
405 // process the first event in the event queue (blocks until an event
406 // appears if there are none currently, use Pending() if this is not
407 // wanted), returns false if the event loop should stop and true
409 virtual bool Dispatch();
411 // process all currently pending events right now
413 // it is an error to call Yield() recursively unless the value of
414 // onlyIfNeeded is true
416 // WARNING: this function is dangerous as it can lead to unexpected
417 // reentrancies (i.e. when called from an event handler it
418 // may result in calling the same event handler again), use
419 // with _extreme_ care or, better, don't use at all!
420 virtual bool Yield(bool onlyIfNeeded
= false) = 0;
422 // this virtual function is called in the GUI mode when the application
423 // becomes idle and normally just sends wxIdleEvent to all interested
426 // it should return true if more idle events are needed, false if not
427 virtual bool ProcessIdle();
429 // Send idle event to window and all subwindows
430 // Returns true if more idle time is requested.
431 virtual bool SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
);
433 // Perform standard OnIdle behaviour: call from port's OnIdle
434 void OnIdle(wxIdleEvent
& event
);
437 // top level window functions
438 // --------------------------
440 // return true if our app has focus
441 virtual bool IsActive() const { return m_isActive
; }
443 // set the "main" top level window
444 void SetTopWindow(wxWindow
*win
) { m_topWindow
= win
; }
446 // return the "main" top level window (if it hadn't been set previously
447 // with SetTopWindow(), will return just some top level window and, if
448 // there are none, will return NULL)
449 virtual wxWindow
*GetTopWindow() const
453 else if (wxTopLevelWindows
.GetCount() > 0)
454 return wxTopLevelWindows
.GetFirst()->GetData();
456 return (wxWindow
*)NULL
;
459 // control the exit behaviour: by default, the program will exit the
460 // main loop (and so, usually, terminate) when the last top-level
461 // program window is deleted. Beware that if you disable this behaviour
462 // (with SetExitOnFrameDelete(false)), you'll have to call
463 // ExitMainLoop() explicitly from somewhere.
464 void SetExitOnFrameDelete(bool flag
)
465 { m_exitOnFrameDelete
= flag
? Yes
: No
; }
466 bool GetExitOnFrameDelete() const
467 { return m_exitOnFrameDelete
== Yes
; }
470 // display mode, visual, printing mode, ...
471 // ------------------------------------------------------------------------
473 // Get display mode that is used use. This is only used in framebuffer
474 // wxWin ports (such as wxMGL).
475 virtual wxVideoMode
GetDisplayMode() const { return wxVideoMode(); }
476 // Set display mode to use. This is only used in framebuffer wxWin
477 // ports (such as wxMGL). This method should be called from
479 virtual bool SetDisplayMode(const wxVideoMode
& WXUNUSED(info
)) { return true; }
481 // set use of best visual flag (see below)
482 void SetUseBestVisual( bool flag
) { m_useBestVisual
= flag
; }
483 bool GetUseBestVisual() const { return m_useBestVisual
; }
485 // set/get printing mode: see wxPRINT_XXX constants.
487 // default behaviour is the normal one for Unix: always use PostScript
489 virtual void SetPrintMode(int WXUNUSED(mode
)) { }
490 int GetPrintMode() const { return wxPRINT_POSTSCRIPT
; }
493 // command line parsing (GUI-specific)
494 // ------------------------------------------------------------------------
496 #if wxUSE_CMDLINE_PARSER
497 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
498 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
501 // miscellaneous other stuff
502 // ------------------------------------------------------------------------
504 // called by toolkit-specific code to set the app status: active (we have
505 // focus) or not and also the last window which had focus before we were
507 virtual void SetActive(bool isActive
, wxWindow
*lastFocus
);
509 // OBSOLETE: don't use, always returns true
511 // returns true if the program is successfully initialized
512 bool Initialized() { return true; }
516 // delete all objects in wxPendingDelete list
517 void DeletePendingObjects();
519 // override base class method to use GUI traits
520 virtual wxAppTraits
*CreateTraits();
523 #if wxUSE_EVTLOOP_IN_APP
524 // the main event loop of the application (may be NULL if the loop hasn't
525 // been started yet or has already terminated)
526 wxEventLoop
*m_mainLoop
;
527 #endif // wxUSE_EVTLOOP_IN_APP
529 // the main top level window (may be NULL)
530 wxWindow
*m_topWindow
;
532 // if Yes, exit the main loop when the last top level window is deleted, if
533 // No don't do it and if Later -- only do it once we reach our OnRun()
535 // the explanation for using this strange scheme is given in appcmn.cpp
541 } m_exitOnFrameDelete
;
543 // true if the apps whats to use the best visual on systems where
544 // more than one are available (Sun, SGI, XFree86 4.0 ?)
545 bool m_useBestVisual
;
547 // does any of our windows has focus?
551 DECLARE_NO_COPY_CLASS(wxAppBase
)
556 // ----------------------------------------------------------------------------
557 // now include the declaration of the real class
558 // ----------------------------------------------------------------------------
561 #if defined(__WXPALMOS__)
562 #include "wx/palmos/app.h"
563 #elif defined(__WXMSW__)
564 #include "wx/msw/app.h"
565 #elif defined(__WXMOTIF__)
566 #include "wx/motif/app.h"
567 #elif defined(__WXMGL__)
568 #include "wx/mgl/app.h"
569 #elif defined(__WXGTK__)
570 #include "wx/gtk/app.h"
571 #elif defined(__WXX11__)
572 #include "wx/x11/app.h"
573 #elif defined(__WXMAC__)
574 #include "wx/mac/app.h"
575 #elif defined(__WXCOCOA__)
576 #include "wx/cocoa/app.h"
577 #elif defined(__WXPM__)
578 #include "wx/os2/app.h"
581 // allow using just wxApp (instead of wxAppConsole) in console programs
582 typedef wxAppConsole wxApp
;
585 // ----------------------------------------------------------------------------
587 // ----------------------------------------------------------------------------
589 // for compatibility, we define this macro to access the global application
590 // object of type wxApp
592 // note that instead of using of wxTheApp in application code you should
593 // consider using DECLARE_APP() after which you may call wxGetApp() which will
594 // return the object of the correct type (i.e. MyApp and not wxApp)
596 // the cast is safe as in GUI build we only use wxApp, not wxAppConsole, and in
597 // console mode it does nothing at all
598 #define wxTheApp ((wxApp *)wxApp::GetInstance())
600 // ----------------------------------------------------------------------------
602 // ----------------------------------------------------------------------------
604 // event loop related functions only work in GUI programs
605 // ------------------------------------------------------
607 // Force an exit from main loop
608 extern void WXDLLIMPEXP_BASE
wxExit();
610 // Yield to other apps/messages
611 extern bool WXDLLIMPEXP_BASE
wxYield();
613 // Yield to other apps/messages
614 extern void WXDLLIMPEXP_BASE
wxWakeUpIdle();
616 // ----------------------------------------------------------------------------
617 // macros for dynamic creation of the application object
618 // ----------------------------------------------------------------------------
620 // Having a global instance of this class allows wxApp to be aware of the app
621 // creator function. wxApp can then call this function to create a new app
622 // object. Convoluted, but necessary.
624 class WXDLLIMPEXP_BASE wxAppInitializer
627 wxAppInitializer(wxAppInitializerFunction fn
)
628 { wxApp::SetInitializerFunction(fn
); }
631 // the code below defines a IMPLEMENT_WXWIN_MAIN macro which you can use if
632 // your compiler really, really wants main() to be in your main program (e.g.
633 // hello.cpp). Now IMPLEMENT_APP should add this code if required.
635 #define IMPLEMENT_WXWIN_MAIN_CONSOLE \
636 int main(int argc, char **argv) { return wxEntry(argc, argv); }
638 // port-specific header could have defined it already in some special wau
639 #ifndef IMPLEMENT_WXWIN_MAIN
640 #define IMPLEMENT_WXWIN_MAIN IMPLEMENT_WXWIN_MAIN_CONSOLE
641 #endif // defined(IMPLEMENT_WXWIN_MAIN)
643 #ifdef __WXUNIVERSAL__
644 #include "wx/univ/theme.h"
646 #define IMPLEMENT_WX_THEME_SUPPORT \
647 WX_USE_THEME(win32); \
650 #define IMPLEMENT_WX_THEME_SUPPORT
653 // Use this macro if you want to define your own main() or WinMain() function
654 // and call wxEntry() from there.
655 #define IMPLEMENT_APP_NO_MAIN(appname) \
656 wxAppConsole *wxCreateApp() \
658 wxAppConsole::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, \
660 return new appname; \
663 wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
664 DECLARE_APP(appname) \
665 appname& wxGetApp() { return *(appname *)wxTheApp; }
667 // Same as IMPLEMENT_APP() normally but doesn't include themes support in
668 // wxUniversal builds
669 #define IMPLEMENT_APP_NO_THEMES(appname) \
670 IMPLEMENT_APP_NO_MAIN(appname) \
673 // Use this macro exactly once, the argument is the name of the wxApp-derived
674 // class which is the class of your application.
675 #define IMPLEMENT_APP(appname) \
676 IMPLEMENT_APP_NO_THEMES(appname) \
677 IMPLEMENT_WX_THEME_SUPPORT
679 // Same as IMPLEMENT_APP(), but for console applications.
680 #define IMPLEMENT_APP_CONSOLE(appname) \
681 IMPLEMENT_APP_NO_MAIN(appname) \
682 IMPLEMENT_WXWIN_MAIN_CONSOLE
684 // this macro can be used multiple times and just allows you to use wxGetApp()
686 #define DECLARE_APP(appname) extern appname& wxGetApp();
689 // declare the stuff defined by IMPLEMENT_APP() macro, it's not really needed
690 // anywhere else but at the very least it suppresses icc warnings about
691 // defining extern symbols without prior declaration, and it shouldn't do any
693 extern wxAppConsole
*wxCreateApp();
694 extern wxAppInitializer wxTheAppInitializer
;
696 #endif // _WX_APP_H_BASE_