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()
32 class WXDLLIMPEXP_BASE wxAppConsole
;
33 class WXDLLIMPEXP_BASE wxAppTraits
;
34 class WXDLLIMPEXP_BASE wxCmdLineParser
;
35 class WXDLLIMPEXP_BASE wxLog
;
36 class WXDLLIMPEXP_BASE wxMessageOutput
;
39 class WXDLLEXPORT wxEventLoop
;
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // the type of the function used to create a wxApp object on program start up
47 typedef wxAppConsole
* (*wxAppInitializerFunction
)();
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
56 wxPRINT_POSTSCRIPT
= 2
59 // ----------------------------------------------------------------------------
60 // wxAppConsole: wxApp for non-GUI applications
61 // ----------------------------------------------------------------------------
63 class WXDLLIMPEXP_BASE wxAppConsole
: public wxEvtHandler
68 virtual ~wxAppConsole();
71 // the virtual functions which may/must be overridden in the derived class
72 // -----------------------------------------------------------------------
74 // This is the very first function called for a newly created wxApp object,
75 // it is used by the library to do the global initialization. If, for some
76 // reason, you must override it (instead of just overriding OnInit(), as
77 // usual, for app-specific initializations), do not forget to call the base
79 virtual bool Initialize(int& argc
, wxChar
**argv
);
81 // This gives wxCocoa a chance to call OnInit() with a memory pool in place
82 virtual bool CallOnInit() { return OnInit(); }
84 // Called before OnRun(), this is a good place to do initialization -- if
85 // anything fails, return false from here to prevent the program from
86 // continuing. The command line is normally parsed here, call the base
87 // class OnInit() to do it.
88 virtual bool OnInit();
90 // this is here only temporary hopefully (FIXME)
91 virtual bool OnInitGui() { return true; }
93 // This is the replacement for the normal main(): all program work should
94 // be done here. When OnRun() returns, the programs starts shutting down.
95 virtual int OnRun() = 0;
97 // This is only called if OnInit() returned true so it's a good place to do
98 // any cleanup matching the initializations done there.
101 // This is the very last function called on wxApp object before it is
102 // destroyed. If you override it (instead of overriding OnExit() as usual)
103 // do not forget to call the base class version!
104 virtual void CleanUp();
106 // Called when a fatal exception occurs, this function should take care not
107 // to do anything which might provoke a nested exception! It may be
108 // overridden if you wish to react somehow in non-default way (core dump
109 // under Unix, application crash under Windows) to fatal program errors,
110 // however extreme care should be taken if you don't want this function to
112 virtual void OnFatalException() { }
114 // Called from wxExit() function, should terminate the application a.s.a.p.
117 // Return the layout direction for the current locale
118 virtual wxLayoutDirection
GetLayoutDirection() const;
121 // application info: name, description, vendor
122 // -------------------------------------------
124 // NB: all these should be set by the application itself, there are no
125 // reasonable default except for the application name which is taken to
128 // set/get the application name
129 wxString
GetAppName() const
131 return m_appName
.empty() ? m_className
: m_appName
;
133 void SetAppName(const wxString
& name
) { m_appName
= name
; }
135 // set/get the app class name
136 wxString
GetClassName() const { return m_className
; }
137 void SetClassName(const wxString
& name
) { m_className
= name
; }
139 // set/get the vendor name
140 const wxString
& GetVendorName() const { return m_vendorName
; }
141 void SetVendorName(const wxString
& name
) { m_vendorName
= name
; }
144 // cmd line parsing stuff
145 // ----------------------
147 // all of these methods may be overridden in the derived class to
148 // customize the command line parsing (by default only a few standard
149 // options are handled)
151 // you also need to call wxApp::OnInit() from YourApp::OnInit() for all
154 #if wxUSE_CMDLINE_PARSER
155 // this one is called from OnInit() to add all supported options
156 // to the given parser (don't forget to call the base class version if you
158 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
160 // called after successfully parsing the command line, return true
161 // to continue and false to exit (don't forget to call the base class
162 // version if you override it!)
163 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
165 // called if "--help" option was specified, return true to continue
167 virtual bool OnCmdLineHelp(wxCmdLineParser
& parser
);
169 // called if incorrect command line options were given, return
170 // false to abort and true to continue
171 virtual bool OnCmdLineError(wxCmdLineParser
& parser
);
172 #endif // wxUSE_CMDLINE_PARSER
175 // miscellaneous customization functions
176 // -------------------------------------
178 // create the app traits object to which we delegate for everything which
179 // either should be configurable by the user (then he can change the
180 // default behaviour simply by overriding CreateTraits() and returning his
181 // own traits object) or which is GUI/console dependent as then wxAppTraits
182 // allows us to abstract the differences behind the common façade
183 wxAppTraits
*GetTraits();
185 // the functions below shouldn't be used now that we have wxAppTraits
186 #if WXWIN_COMPATIBILITY_2_4
189 // override this function to create default log target of arbitrary
190 // user-defined class (default implementation creates a wxLogGui
191 // object) -- this log object is used by default by all wxLogXXX()
193 wxDEPRECATED( virtual wxLog
*CreateLogTarget() );
196 // similar to CreateLogTarget() but for the global wxMessageOutput
198 wxDEPRECATED( virtual wxMessageOutput
*CreateMessageOutput() );
200 #endif // WXWIN_COMPATIBILITY_2_4
203 // event processing functions
204 // --------------------------
206 // this method allows to filter all the events processed by the program, so
207 // you should try to return quickly from it to avoid slowing down the
208 // program to the crawl
210 // return value should be -1 to continue with the normal event processing,
211 // or TRUE or FALSE to stop further processing and pretend that the event
212 // had been already processed or won't be processed at all, respectively
213 virtual int FilterEvent(wxEvent
& event
);
216 // call the specified handler on the given object with the given event
218 // this method only exists to allow catching the exceptions thrown by any
219 // event handler, it would lead to an extra (useless) virtual function call
220 // if the exceptions were not used, so it doesn't even exist in that case
221 virtual void HandleEvent(wxEvtHandler
*handler
,
222 wxEventFunction func
,
223 wxEvent
& event
) const;
225 // Called when an unhandled C++ exception occurs inside OnRun(): note that
226 // the exception type is lost by now, so if you really want to handle the
227 // exception you should override OnRun() and put a try/catch around
228 // MainLoop() call there or use OnExceptionInMainLoop()
229 virtual void OnUnhandledException() { }
230 #endif // wxUSE_EXCEPTIONS
232 // process all events in the wxPendingEvents list -- it is necessary to
233 // call this function to process posted events. This happens during each
234 // event loop iteration in GUI mode but if there is no main loop, it may be
235 // also called directly.
236 virtual void ProcessPendingEvents();
238 // doesn't do anything in this class, just a hook for GUI wxApp
239 virtual bool Yield(bool WXUNUSED(onlyIfNeeded
) = false) { return true; }
241 // make sure that idle events are sent again
242 virtual void WakeUpIdle() { }
244 // this is just a convenience: by providing its implementation here we
245 // avoid #ifdefs in the code using it
246 static bool IsMainLoopRunning() { return false; }
253 // this function is called when an assert failure occurs, the base class
254 // version does the normal processing (i.e. shows the usual assert failure
257 // the arguments are the location of the failed assert (func may be empty
258 // if the compiler doesn't support C99 __FUNCTION__), the text of the
259 // assert itself and the user-specified message
260 virtual void OnAssertFailure(const wxChar
*file
,
266 // old version of the function without func parameter, for compatibility
267 // only, override OnAssertFailure() in the new code
268 virtual void OnAssert(const wxChar
*file
,
272 #endif // __WXDEBUG__
274 // check that the wxBuildOptions object (constructed in the application
275 // itself, usually the one from IMPLEMENT_APP() macro) matches the build
276 // options of the library and abort if it doesn't
277 static bool CheckBuildOptions(const char *optionsSignature
,
278 const char *componentName
);
279 #if WXWIN_COMPATIBILITY_2_4
280 wxDEPRECATED( static bool CheckBuildOptions(const wxBuildOptions
& buildOptions
) );
283 // implementation only from now on
284 // -------------------------------
286 // helpers for dynamic wxApp construction
287 static void SetInitializerFunction(wxAppInitializerFunction fn
)
288 { ms_appInitFn
= fn
; }
289 static wxAppInitializerFunction
GetInitializerFunction()
290 { return ms_appInitFn
; }
292 // accessors for ms_appInstance field (external code might wish to modify
293 // it, this is why we provide a setter here as well, but you should really
294 // know what you're doing if you call it), wxTheApp is usually used instead
296 static wxAppConsole
*GetInstance() { return ms_appInstance
; }
297 static void SetInstance(wxAppConsole
*app
) { ms_appInstance
= app
; }
300 // command line arguments (public for backwards compatibility)
305 // the function which creates the traits object when GetTraits() needs it
306 // for the first time
307 virtual wxAppTraits
*CreateTraits();
310 // function used for dynamic wxApp creation
311 static wxAppInitializerFunction ms_appInitFn
;
313 // the one and only global application object
314 static wxAppConsole
*ms_appInstance
;
317 // application info (must be set from the user code)
318 wxString m_vendorName
, // vendor name (ACME Inc)
319 m_appName
, // app name
320 m_className
; // class name
322 // the class defining the application behaviour, NULL initially and created
323 // by GetTraits() when first needed
324 wxAppTraits
*m_traits
;
327 // the application object is a singleton anyhow, there is no sense in
329 DECLARE_NO_COPY_CLASS(wxAppConsole
)
332 // ----------------------------------------------------------------------------
333 // wxAppBase: the common part of wxApp implementations for all platforms
334 // ----------------------------------------------------------------------------
338 class WXDLLIMPEXP_CORE wxAppBase
: public wxAppConsole
342 virtual ~wxAppBase();
344 // the virtual functions which may/must be overridden in the derived class
345 // -----------------------------------------------------------------------
347 // very first initialization function
349 // Override: very rarely
350 virtual bool Initialize(int& argc
, wxChar
**argv
);
352 // a platform-dependent version of OnInit(): the code here is likely to
353 // depend on the toolkit. default version does nothing.
356 virtual bool OnInitGui();
358 // called to start program execution - the default version just enters
359 // the main GUI loop in which events are received and processed until
360 // the last window is not deleted (if GetExitOnFrameDelete) or
361 // ExitMainLoop() is called. In console mode programs, the execution
362 // of the program really starts here
364 // Override: rarely in GUI applications, always in console ones.
367 // a matching function for OnInit()
368 virtual int OnExit();
370 // very last clean up function
372 // Override: very rarely
373 virtual void CleanUp();
376 // the worker functions - usually not used directly by the user code
377 // -----------------------------------------------------------------
379 // return true if we're running main loop, i.e. if the events can
380 // (already) be dispatched
381 static bool IsMainLoopRunning()
383 wxAppBase
*app
= wx_static_cast(wxAppBase
*, GetInstance());
384 return app
&& app
->m_mainLoop
!= NULL
;
387 // execute the main GUI loop, the function returns when the loop ends
388 virtual int MainLoop();
390 // exit the main loop thus terminating the application
393 // exit the main GUI loop during the next iteration (i.e. it does not
394 // stop the program immediately!)
395 virtual void ExitMainLoop();
397 // returns true if there are unprocessed events in the event queue
398 virtual bool Pending();
400 // process the first event in the event queue (blocks until an event
401 // appears if there are none currently, use Pending() if this is not
402 // wanted), returns false if the event loop should stop and true
404 virtual bool Dispatch();
406 // process all currently pending events right now
408 // it is an error to call Yield() recursively unless the value of
409 // onlyIfNeeded is true
411 // WARNING: this function is dangerous as it can lead to unexpected
412 // reentrancies (i.e. when called from an event handler it
413 // may result in calling the same event handler again), use
414 // with _extreme_ care or, better, don't use at all!
415 virtual bool Yield(bool onlyIfNeeded
= false) = 0;
417 // this virtual function is called in the GUI mode when the application
418 // becomes idle and normally just sends wxIdleEvent to all interested
421 // it should return true if more idle events are needed, false if not
422 virtual bool ProcessIdle();
424 // Send idle event to window and all subwindows
425 // Returns true if more idle time is requested.
426 virtual bool SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
);
430 // Function called if an uncaught exception is caught inside the main
431 // event loop: it may return true to continue running the event loop or
432 // false to stop it (in the latter case it may rethrow the exception as
434 virtual bool OnExceptionInMainLoop();
435 #endif // wxUSE_EXCEPTIONS
438 // top level window functions
439 // --------------------------
441 // return true if our app has focus
442 virtual bool IsActive() const { return m_isActive
; }
444 // set the "main" top level window
445 void SetTopWindow(wxWindow
*win
) { m_topWindow
= win
; }
447 // return the "main" top level window (if it hadn't been set previously
448 // with SetTopWindow(), will return just some top level window and, if
449 // there are none, will return NULL)
450 virtual wxWindow
*GetTopWindow() const
454 else if (wxTopLevelWindows
.GetCount() > 0)
455 return wxTopLevelWindows
.GetFirst()->GetData();
457 return (wxWindow
*)NULL
;
460 // control the exit behaviour: by default, the program will exit the
461 // main loop (and so, usually, terminate) when the last top-level
462 // program window is deleted. Beware that if you disable this behaviour
463 // (with SetExitOnFrameDelete(false)), you'll have to call
464 // ExitMainLoop() explicitly from somewhere.
465 void SetExitOnFrameDelete(bool flag
)
466 { m_exitOnFrameDelete
= flag
? Yes
: No
; }
467 bool GetExitOnFrameDelete() const
468 { return m_exitOnFrameDelete
== Yes
; }
471 // display mode, visual, printing mode, ...
472 // ------------------------------------------------------------------------
474 // Get display mode that is used use. This is only used in framebuffer
475 // wxWin ports (such as wxMGL or wxDFB).
476 virtual wxVideoMode
GetDisplayMode() const { return wxVideoMode(); }
477 // Set display mode to use. This is only used in framebuffer wxWin
478 // ports (such as wxMGL or wxDFB). This method should be called from
480 virtual bool SetDisplayMode(const wxVideoMode
& WXUNUSED(info
)) { return true; }
482 // set use of best visual flag (see below)
483 void SetUseBestVisual( bool flag
) { m_useBestVisual
= flag
; }
484 bool GetUseBestVisual() const { return m_useBestVisual
; }
486 // set/get printing mode: see wxPRINT_XXX constants.
488 // default behaviour is the normal one for Unix: always use PostScript
490 virtual void SetPrintMode(int WXUNUSED(mode
)) { }
491 int GetPrintMode() const { return wxPRINT_POSTSCRIPT
; }
494 // command line parsing (GUI-specific)
495 // ------------------------------------------------------------------------
497 #if wxUSE_CMDLINE_PARSER
498 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
499 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
502 // miscellaneous other stuff
503 // ------------------------------------------------------------------------
505 // called by toolkit-specific code to set the app status: active (we have
506 // focus) or not and also the last window which had focus before we were
508 virtual void SetActive(bool isActive
, wxWindow
*lastFocus
);
510 #if WXWIN_COMPATIBILITY_2_6
511 // OBSOLETE: don't use, always returns true
513 // returns true if the program is successfully initialized
514 wxDEPRECATED( bool Initialized() );
515 #endif // WXWIN_COMPATIBILITY_2_6
517 // perform standard OnIdle behaviour, ensure that this is always called
518 void OnIdle(wxIdleEvent
& event
);
522 // delete all objects in wxPendingDelete list
523 void DeletePendingObjects();
525 // override base class method to use GUI traits
526 virtual wxAppTraits
*CreateTraits();
529 // the main event loop of the application (may be NULL if the loop hasn't
530 // been started yet or has already terminated)
531 wxEventLoop
*m_mainLoop
;
533 // the main top level window (may be NULL)
534 wxWindow
*m_topWindow
;
536 // if Yes, exit the main loop when the last top level window is deleted, if
537 // No don't do it and if Later -- only do it once we reach our OnRun()
539 // the explanation for using this strange scheme is given in appcmn.cpp
545 } m_exitOnFrameDelete
;
547 // true if the app wants to use the best visual on systems where
548 // more than one are available (Sun, SGI, XFree86 4.0 ?)
549 bool m_useBestVisual
;
551 // does any of our windows have focus?
555 DECLARE_NO_COPY_CLASS(wxAppBase
)
558 #if WXWIN_COMPATIBILITY_2_6
559 inline bool wxAppBase::Initialized() { return true; }
560 #endif // WXWIN_COMPATIBILITY_2_6
564 // ----------------------------------------------------------------------------
565 // now include the declaration of the real class
566 // ----------------------------------------------------------------------------
569 #if defined(__WXPALMOS__)
570 #include "wx/palmos/app.h"
571 #elif defined(__WXMSW__)
572 #include "wx/msw/app.h"
573 #elif defined(__WXMOTIF__)
574 #include "wx/motif/app.h"
575 #elif defined(__WXMGL__)
576 #include "wx/mgl/app.h"
577 #elif defined(__WXDFB__)
578 #include "wx/dfb/app.h"
579 #elif defined(__WXGTK20__)
580 #include "wx/gtk/app.h"
581 #elif defined(__WXGTK__)
582 #include "wx/gtk1/app.h"
583 #elif defined(__WXX11__)
584 #include "wx/x11/app.h"
585 #elif defined(__WXMAC__)
586 #include "wx/mac/app.h"
587 #elif defined(__WXCOCOA__)
588 #include "wx/cocoa/app.h"
589 #elif defined(__WXPM__)
590 #include "wx/os2/app.h"
593 // allow using just wxApp (instead of wxAppConsole) in console programs
594 typedef wxAppConsole wxApp
;
597 // ----------------------------------------------------------------------------
599 // ----------------------------------------------------------------------------
601 // for compatibility, we define this macro to access the global application
602 // object of type wxApp
604 // note that instead of using of wxTheApp in application code you should
605 // consider using DECLARE_APP() after which you may call wxGetApp() which will
606 // return the object of the correct type (i.e. MyApp and not wxApp)
608 // the cast is safe as in GUI build we only use wxApp, not wxAppConsole, and in
609 // console mode it does nothing at all
610 #define wxTheApp ((wxApp *)wxApp::GetInstance())
612 // ----------------------------------------------------------------------------
614 // ----------------------------------------------------------------------------
616 // event loop related functions only work in GUI programs
617 // ------------------------------------------------------
619 // Force an exit from main loop
620 extern void WXDLLIMPEXP_BASE
wxExit();
622 // Yield to other apps/messages
623 extern bool WXDLLIMPEXP_BASE
wxYield();
625 // Yield to other apps/messages
626 extern void WXDLLIMPEXP_BASE
wxWakeUpIdle();
628 // ----------------------------------------------------------------------------
629 // macros for dynamic creation of the application object
630 // ----------------------------------------------------------------------------
632 // Having a global instance of this class allows wxApp to be aware of the app
633 // creator function. wxApp can then call this function to create a new app
634 // object. Convoluted, but necessary.
636 class WXDLLIMPEXP_BASE wxAppInitializer
639 wxAppInitializer(wxAppInitializerFunction fn
)
640 { wxApp::SetInitializerFunction(fn
); }
643 // the code below defines a IMPLEMENT_WXWIN_MAIN macro which you can use if
644 // your compiler really, really wants main() to be in your main program (e.g.
645 // hello.cpp). Now IMPLEMENT_APP should add this code if required.
647 #define IMPLEMENT_WXWIN_MAIN_CONSOLE \
648 int main(int argc, char **argv) { return wxEntry(argc, argv); }
650 // port-specific header could have defined it already in some special way
651 #ifndef IMPLEMENT_WXWIN_MAIN
652 #define IMPLEMENT_WXWIN_MAIN IMPLEMENT_WXWIN_MAIN_CONSOLE
653 #endif // defined(IMPLEMENT_WXWIN_MAIN)
655 #ifdef __WXUNIVERSAL__
656 #include "wx/univ/theme.h"
658 #define IMPLEMENT_WX_THEME_SUPPORT \
659 WX_USE_THEME(win32); \
662 #define IMPLEMENT_WX_THEME_SUPPORT
665 // Use this macro if you want to define your own main() or WinMain() function
666 // and call wxEntry() from there.
667 #define IMPLEMENT_APP_NO_MAIN(appname) \
668 wxAppConsole *wxCreateApp() \
670 wxAppConsole::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, \
672 return new appname; \
675 wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
676 DECLARE_APP(appname) \
677 appname& wxGetApp() { return *(appname *)wxTheApp; }
679 // Same as IMPLEMENT_APP() normally but doesn't include themes support in
680 // wxUniversal builds
681 #define IMPLEMENT_APP_NO_THEMES(appname) \
682 IMPLEMENT_APP_NO_MAIN(appname) \
685 // Use this macro exactly once, the argument is the name of the wxApp-derived
686 // class which is the class of your application.
687 #define IMPLEMENT_APP(appname) \
688 IMPLEMENT_APP_NO_THEMES(appname) \
689 IMPLEMENT_WX_THEME_SUPPORT
691 // Same as IMPLEMENT_APP(), but for console applications.
692 #define IMPLEMENT_APP_CONSOLE(appname) \
693 IMPLEMENT_APP_NO_MAIN(appname) \
694 IMPLEMENT_WXWIN_MAIN_CONSOLE
696 // this macro can be used multiple times and just allows you to use wxGetApp()
698 #define DECLARE_APP(appname) extern appname& wxGetApp();
701 // declare the stuff defined by IMPLEMENT_APP() macro, it's not really needed
702 // anywhere else but at the very least it suppresses icc warnings about
703 // defining extern symbols without prior declaration, and it shouldn't do any
705 extern wxAppConsole
*wxCreateApp();
706 extern wxAppInitializer wxTheAppInitializer
;
708 #endif // _WX_APP_H_BASE_