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
27 #include "wx/init.h" // we must declare wxEntry()
29 class WXDLLIMPEXP_BASE wxAppConsole
;
30 class WXDLLIMPEXP_BASE wxAppTraits
;
31 class WXDLLIMPEXP_BASE wxCmdLineParser
;
32 class WXDLLIMPEXP_BASE wxLog
;
33 class WXDLLIMPEXP_BASE wxMessageOutput
;
35 class WXDLLEXPORT wxEventLoop
;
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // the type of the function used to create a wxApp object on program start up
42 typedef wxAppConsole
* (*wxAppInitializerFunction
)();
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
51 wxPRINT_POSTSCRIPT
= 2
54 // ----------------------------------------------------------------------------
55 // support for framebuffer ports
56 // ----------------------------------------------------------------------------
59 // VS: Fullscreen/framebuffer application needs to choose display mode prior
60 // to wxWindows initialization. This class holds information about display
61 // mode. It is used by wxApp::Set/GetDisplayMode.
62 class WXDLLIMPEXP_CORE wxDisplayModeInfo
65 wxDisplayModeInfo() : m_ok(FALSE
) {}
66 wxDisplayModeInfo(unsigned width
, unsigned height
, unsigned depth
)
67 : m_width(width
), m_height(height
), m_depth(depth
), m_ok(TRUE
) {}
69 unsigned GetWidth() const { return m_width
; }
70 unsigned GetHeight() const { return m_height
; }
71 unsigned GetDepth() const { return m_depth
; }
72 bool IsOk() const { return m_ok
; }
75 unsigned m_width
, m_height
, m_depth
;
81 // ----------------------------------------------------------------------------
82 // wxAppConsole: wxApp for non-GUI applications
83 // ----------------------------------------------------------------------------
85 class WXDLLIMPEXP_BASE wxAppConsole
: public wxEvtHandler
90 virtual ~wxAppConsole();
93 // the virtual functions which may/must be overridden in the derived class
94 // -----------------------------------------------------------------------
96 // This is the very first function called for a newly created wxApp object,
97 // it is used by the library to do the global initialization. If, for some
98 // reason, you must override it (instead of just overriding OnInit(), as
99 // usual, for app-specific initializations), do not forget to call the base
101 virtual bool Initialize(int& argc
, wxChar
**argv
);
103 // This gives wxCocoa a chance to call OnInit() with a memory pool in place
104 virtual bool CallOnInit() { return OnInit(); }
106 // Called before OnRun(), this is a good place to do initialization -- if
107 // anything fails, return false from here to prevent the program from
108 // continuing. The command line is normally parsed here, call the base
109 // class OnInit() to do it.
110 virtual bool OnInit();
112 // this is here only temproary hopefully (FIXME)
113 virtual bool OnInitGui() { return true; }
115 // This is the replacement for the normal main(): all program work should
116 // be done here. When OnRun() returns, the programs starts shutting down.
117 virtual int OnRun() = 0;
119 // This is only called if OnInit() returned true so it's a good place to do
120 // any cleanup matching the initializations done there.
121 virtual int OnExit();
123 // This is the very last function called on wxApp object before it is
124 // destroyed. If you override it (instead of overriding OnExit() as usual)
125 // do not forget to call the base class version!
126 virtual void CleanUp();
128 // Called when a fatal exception occurs, this function should take care not
129 // to do anything which might provoke a nested exception! It may be
130 // overridden if you wish to react somehow in non-default way (core dump
131 // under Unix, application crash under Windows) to fatal program errors,
132 // however extreme care should be taken if you don't want this function to
134 virtual void OnFatalException() { }
136 // Called from wxExit() function, should terminate the application a.s.a.p.
140 // application info: name, description, vendor
141 // -------------------------------------------
143 // NB: all these should be set by the application itself, there are no
144 // reasonable default except for the application name which is taken to
147 // set/get the application name
148 wxString
GetAppName() const
150 return m_appName
.empty() ? m_className
: m_appName
;
152 void SetAppName(const wxString
& name
) { m_appName
= name
; }
154 // set/get the app class name
155 wxString
GetClassName() const { return m_className
; }
156 void SetClassName(const wxString
& name
) { m_className
= name
; }
158 // set/get the vendor name
159 const wxString
& GetVendorName() const { return m_vendorName
; }
160 void SetVendorName(const wxString
& name
) { m_vendorName
= name
; }
163 // cmd line parsing stuff
164 // ----------------------
166 // all of these methods may be overridden in the derived class to
167 // customize the command line parsing (by default only a few standard
168 // options are handled)
170 // you also need to call wxApp::OnInit() from YourApp::OnInit() for all
173 #if wxUSE_CMDLINE_PARSER
174 // this one is called from OnInit() to add all supported options
175 // to the given parser (don't forget to call the base class version if you
177 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
179 // called after successfully parsing the command line, return TRUE
180 // to continue and FALSE to exit (don't forget to call the base class
181 // version if you override it!)
182 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
184 // called if "--help" option was specified, return TRUE to continue
186 virtual bool OnCmdLineHelp(wxCmdLineParser
& parser
);
188 // called if incorrect command line options were given, return
189 // FALSE to abort and TRUE to continue
190 virtual bool OnCmdLineError(wxCmdLineParser
& parser
);
191 #endif // wxUSE_CMDLINE_PARSER
194 // miscellaneous customization functions
195 // -------------------------------------
197 // create the app traits object to which we delegate for everything which
198 // either should be configurable by the user (then he can change the
199 // default behaviour simply by overriding CreateTraits() and returning his
200 // own traits object) or which is GUI/console dependent as then wxAppTraits
201 // allows us to abstract the differences behind the common façade
202 wxAppTraits
*GetTraits();
204 // the functions below shouldn't be used now that we have wxAppTraits
205 #if WXWIN_COMPATIBILITY_2_4
208 // override this function to create default log target of arbitrary
209 // user-defined class (default implementation creates a wxLogGui
210 // object) -- this log object is used by default by all wxLogXXX()
212 virtual wxLog
*CreateLogTarget();
215 // similar to CreateLogTarget() but for the global wxMessageOutput
217 virtual wxMessageOutput
*CreateMessageOutput();
219 #endif // WXWIN_COMPATIBILITY_2_4
222 // event processing functions
223 // --------------------------
225 // this method allows to filter all the events processed by the program, so
226 // you should try to return quickly from it to avoid slowing down the
227 // program to the crawl
229 // return value should be -1 to continue with the normal event processing,
230 // or TRUE or FALSE to stop further processing and pretend that the event
231 // had been already processed or won't be processed at all, respectively
232 virtual int FilterEvent(wxEvent
& event
);
234 // process all events in the wxPendingEvents list -- it is necessary to
235 // call this function to process posted events. This happens during each
236 // event loop iteration in GUI mode but if there is no main loop, it may be
237 // also called directly.
238 virtual void ProcessPendingEvents();
240 // doesn't do anything in this class, just a hook for GUI wxApp
241 virtual bool Yield(bool WXUNUSED(onlyIfNeeded
) = false) { return true; }
243 // make sure that idle events are sent again
244 virtual void WakeUpIdle() { }
250 // this function is called when an assert failure occurs, the base class
251 // version does the normal processing (i.e. shows the usual assert failure
254 // the arguments are the place where the assert occured, the text of the
255 // assert itself and the user-specified message
257 virtual void OnAssert(const wxChar
*file
,
261 #endif // __WXDEBUG__
263 // check that the wxBuildOptions object (constructed in the application
264 // itself, usually the one from IMPLEMENT_APP() macro) matches the build
265 // options of the library and abort if it doesn't
266 static bool CheckBuildOptions(const char *optionsSignature
,
267 const char *componentName
);
268 #if WXWIN_COMPATIBILITY_2_4
269 static bool CheckBuildOptions(const wxBuildOptions
& buildOptions
)
271 return CheckBuildOptions(buildOptions
.m_signature
, "your program");
275 // implementation only from now on
276 // -------------------------------
278 // helpers for dynamic wxApp construction
279 static void SetInitializerFunction(wxAppInitializerFunction fn
)
280 { ms_appInitFn
= fn
; }
281 static wxAppInitializerFunction
GetInitializerFunction()
282 { return ms_appInitFn
; }
284 // accessors for ms_appInstance field (external code might wish to modify
285 // it, this is why we provide a setter here as well, but you should really
286 // know what you're doing if you call it), wxTheApp is usually used instead
288 static wxAppConsole
*GetInstance() { return ms_appInstance
; }
289 static void SetInstance(wxAppConsole
*app
) { ms_appInstance
= app
; }
292 // command line arguments (public for backwards compatibility)
297 // the function which creates the traits object when GetTraits() needs it
298 // for the first time
299 virtual wxAppTraits
*CreateTraits();
302 // function used for dynamic wxApp creation
303 static wxAppInitializerFunction ms_appInitFn
;
305 // the one and only global application object
306 static wxAppConsole
*ms_appInstance
;
309 // application info (must be set from the user code)
310 wxString m_vendorName
, // vendor name (ACME Inc)
311 m_appName
, // app name
312 m_className
; // class name
314 // the class defining the application behaviour, NULL initially and created
315 // by GetTraits() when first needed
316 wxAppTraits
*m_traits
;
319 // the application object is a singleton anyhow, there is no sense in
321 DECLARE_NO_COPY_CLASS(wxAppConsole
)
324 // ----------------------------------------------------------------------------
325 // wxAppBase: the common part of wxApp implementations for all platforms
326 // ----------------------------------------------------------------------------
330 class WXDLLIMPEXP_CORE wxAppBase
: public wxAppConsole
334 virtual ~wxAppBase();
336 // the virtual functions which may/must be overridden in the derived class
337 // -----------------------------------------------------------------------
339 // very first initialization function
341 // Override: very rarely
342 virtual bool Initialize(int& argc
, wxChar
**argv
);
344 // a platform-dependent version of OnInit(): the code here is likely to
345 // depend on the toolkit. default version does nothing.
348 virtual bool OnInitGui();
350 // called to start program execution - the default version just enters
351 // the main GUI loop in which events are received and processed until
352 // the last window is not deleted (if GetExitOnFrameDelete) or
353 // ExitMainLoop() is called. In console mode programs, the execution
354 // of the program really starts here
356 // Override: rarely in GUI applications, always in console ones.
359 // a matching function for OnInit()
360 virtual int OnExit();
362 // very last clean up function
364 // Override: very rarely
365 virtual void CleanUp();
368 // the worker functions - usually not used directly by the user code
369 // -----------------------------------------------------------------
371 // execute the main GUI loop, the function returns when the loop ends
372 virtual int MainLoop();
374 // exit the main loop thus terminating the application
377 // exit the main GUI loop during the next iteration (i.e. it does not
378 // stop the program immediately!)
379 virtual void ExitMainLoop();
381 // returns true if the program is initialized, i.e. OnInit() has been
382 // completed successfully
383 virtual bool Initialized() = 0;
385 // returns TRUE if there are unprocessed events in the event queue
386 virtual bool Pending();
388 // process the first event in the event queue (blocks until an event
389 // appears if there are none currently, use Pending() if this is not
390 // wanted), returns false if the event loop should stop and true
392 virtual bool Dispatch();
394 // process all currently pending events right now
396 // it is an error to call Yield() recursively unless the value of
397 // onlyIfNeeded is TRUE
399 // WARNING: this function is dangerous as it can lead to unexpected
400 // reentrancies (i.e. when called from an event handler it
401 // may result in calling the same event handler again), use
402 // with _extreme_ care or, better, don't use at all!
403 virtual bool Yield(bool onlyIfNeeded
= FALSE
) = 0;
405 // this virtual function is called in the GUI mode when the application
406 // becomes idle and normally just sends wxIdleEvent to all interested
409 // it should return TRUE if more idle events are needed, FALSE if not
410 virtual bool ProcessIdle();
412 // Send idle event to window and all subwindows
413 // Returns TRUE if more idle time is requested.
414 virtual bool SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
);
416 // Perform standard OnIdle behaviour: call from port's OnIdle
417 void OnIdle(wxIdleEvent
& event
);
420 // top level window functions
421 // --------------------------
423 // return TRUE if our app has focus
424 virtual bool IsActive() const { return m_isActive
; }
426 // set the "main" top level window
427 void SetTopWindow(wxWindow
*win
) { m_topWindow
= win
; }
429 // return the "main" top level window (if it hadn't been set previously
430 // with SetTopWindow(), will return just some top level window and, if
431 // there are none, will return NULL)
432 virtual wxWindow
*GetTopWindow() const
436 else if (wxTopLevelWindows
.GetCount() > 0)
437 return wxTopLevelWindows
.GetFirst()->GetData();
439 return (wxWindow
*)NULL
;
442 // control the exit behaviour: by default, the program will exit the
443 // main loop (and so, usually, terminate) when the last top-level
444 // program window is deleted. Beware that if you disable this behaviour
445 // (with SetExitOnFrameDelete(FALSE)), you'll have to call
446 // ExitMainLoop() explicitly from somewhere.
447 void SetExitOnFrameDelete(bool flag
)
448 { m_exitOnFrameDelete
= flag
? Yes
: No
; }
449 bool GetExitOnFrameDelete() const
450 { return m_exitOnFrameDelete
== Yes
; }
453 // display mode, visual, printing mode, ...
454 // ------------------------------------------------------------------------
456 // Get display mode that is used use. This is only used in framebuffer
457 // wxWin ports (such as wxMGL).
458 virtual wxDisplayModeInfo
GetDisplayMode() const { return wxDisplayModeInfo(); }
459 // Set display mode to use. This is only used in framebuffer wxWin
460 // ports (such as wxMGL). This method should be called from
462 virtual bool SetDisplayMode(const wxDisplayModeInfo
& WXUNUSED(info
)) { return TRUE
; }
464 // set use of best visual flag (see below)
465 void SetUseBestVisual( bool flag
) { m_useBestVisual
= flag
; }
466 bool GetUseBestVisual() const { return m_useBestVisual
; }
468 // set/get printing mode: see wxPRINT_XXX constants.
470 // default behaviour is the normal one for Unix: always use PostScript
472 virtual void SetPrintMode(int WXUNUSED(mode
)) { }
473 int GetPrintMode() const { return wxPRINT_POSTSCRIPT
; }
476 // command line parsing (GUI-specific)
477 // ------------------------------------------------------------------------
479 #if wxUSE_CMDLINE_PARSER
480 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
481 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
484 // miscellaneous other stuff
485 // ------------------------------------------------------------------------
487 // called by toolkit-specific code to set the app status: active (we have
488 // focus) or not and also the last window which had focus before we were
490 virtual void SetActive(bool isActive
, wxWindow
*lastFocus
);
494 // delete all objects in wxPendingDelete list
495 void DeletePendingObjects();
497 // override base class method to use GUI traits
498 virtual wxAppTraits
*CreateTraits();
501 // the main event loop of the application (may be NULL if the loop hasn't
502 // been started yet or has already terminated)
503 wxEventLoop
*m_mainLoop
;
505 // the main top level window (may be NULL)
506 wxWindow
*m_topWindow
;
508 // if Yes, exit the main loop when the last top level window is deleted, if
509 // No don't do it and if Later -- only do it once we reach our OnRun()
511 // the explanation for using this strange scheme is given in appcmn.cpp
517 } m_exitOnFrameDelete
;
519 // TRUE if the apps whats to use the best visual on systems where
520 // more than one are available (Sun, SGI, XFree86 4.0 ?)
521 bool m_useBestVisual
;
523 // does any of our windows has focus?
527 DECLARE_NO_COPY_CLASS(wxAppBase
)
532 // ----------------------------------------------------------------------------
533 // now include the declaration of the real class
534 // ----------------------------------------------------------------------------
537 #if defined(__WXMSW__)
538 #include "wx/msw/app.h"
539 #elif defined(__WXMOTIF__)
540 #include "wx/motif/app.h"
541 #elif defined(__WXMGL__)
542 #include "wx/mgl/app.h"
543 #elif defined(__WXGTK__)
544 #include "wx/gtk/app.h"
545 #elif defined(__WXX11__)
546 #include "wx/x11/app.h"
547 #elif defined(__WXMAC__)
548 #include "wx/mac/app.h"
549 #elif defined(__WXCOCOA__)
550 #include "wx/cocoa/app.h"
551 #elif defined(__WXPM__)
552 #include "wx/os2/app.h"
555 // allow using just wxApp (instead of wxAppConsole) in console programs
556 typedef wxAppConsole wxApp
;
559 // ----------------------------------------------------------------------------
561 // ----------------------------------------------------------------------------
563 // for compatibility, we define this macro to access the global application
564 // object of type wxApp
566 // note that instead of using of wxTheApp in application code you should
567 // consider using DECLARE_APP() after which you may call wxGetApp() which will
568 // return the object of the correct type (i.e. MyApp and not wxApp)
570 // the cast is safe as in GUI build we only use wxApp, not wxAppConsole, and in
571 // console mode it does nothing at all
572 #define wxTheApp ((wxApp *)wxApp::GetInstance())
574 // ----------------------------------------------------------------------------
576 // ----------------------------------------------------------------------------
578 // event loop related functions only work in GUI programs
579 // ------------------------------------------------------
581 // Force an exit from main loop
582 extern void WXDLLIMPEXP_BASE
wxExit();
584 // Yield to other apps/messages
585 extern bool WXDLLIMPEXP_BASE
wxYield();
587 // Yield to other apps/messages
588 extern void WXDLLIMPEXP_BASE
wxWakeUpIdle();
590 // ----------------------------------------------------------------------------
591 // macros for dynamic creation of the application object
592 // ----------------------------------------------------------------------------
594 // Having a global instance of this class allows wxApp to be aware of the app
595 // creator function. wxApp can then call this function to create a new app
596 // object. Convoluted, but necessary.
598 class WXDLLIMPEXP_BASE wxAppInitializer
601 wxAppInitializer(wxAppInitializerFunction fn
)
602 { wxApp::SetInitializerFunction(fn
); }
605 // Here's a macro you can use if your compiler really, really wants main() to
606 // be in your main program (e.g. hello.cpp). Now IMPLEMENT_APP should add this
609 #define IMPLEMENT_WXWIN_MAIN_CONSOLE \
610 int main(int argc, char **argv) { return wxEntry(argc, argv); }
612 #if !wxUSE_GUI || !defined(__WXMSW__)
613 #define IMPLEMENT_WXWIN_MAIN \
614 IMPLEMENT_WXWIN_MAIN_CONSOLE
615 #elif defined(__WXMSW__)
616 // we need HINSTANCE declaration to define WinMain()
617 #include "wx/msw/wrapwin.h"
620 #define wxSW_SHOWNORMAL SW_SHOWNORMAL
622 #define wxSW_SHOWNORMAL 0
625 #define IMPLEMENT_WXWIN_MAIN \
626 extern int wxEntry(HINSTANCE hInstance, \
627 HINSTANCE hPrevInstance = NULL, \
628 char *pCmdLine = NULL, \
629 int nCmdShow = wxSW_SHOWNORMAL); \
630 extern "C" int WINAPI WinMain(HINSTANCE hInstance, \
631 HINSTANCE hPrevInstance, \
635 return wxEntry(hInstance, hPrevInstance, lpCmdLine, nCmdShow); \
638 #define IMPLEMENT_WXWIN_MAIN
641 #ifdef __WXUNIVERSAL__
642 #include "wx/univ/theme.h"
644 #define IMPLEMENT_WX_THEME_SUPPORT \
645 WX_USE_THEME(win32); \
648 #define IMPLEMENT_WX_THEME_SUPPORT
651 // Use this macro if you want to define your own main() or WinMain() function
652 // and call wxEntry() from there.
653 #define IMPLEMENT_APP_NO_MAIN(appname) \
654 wxAppConsole *wxCreateApp() \
656 wxAppConsole::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, \
658 return new appname; \
661 wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
662 appname& wxGetApp() { return *(appname *)wxTheApp; }
664 // Same as IMPLEMENT_APP() normally but doesn't include themes support in
665 // wxUniversal builds
666 #define IMPLEMENT_APP_NO_THEMES(appname) \
667 IMPLEMENT_APP_NO_MAIN(appname) \
670 // Use this macro exactly once, the argument is the name of the wxApp-derived
671 // class which is the class of your application.
672 #define IMPLEMENT_APP(appname) \
673 IMPLEMENT_APP_NO_THEMES(appname) \
674 IMPLEMENT_WX_THEME_SUPPORT
676 // Same as IMPLEMENT_APP(), but for console applications.
677 #define IMPLEMENT_APP_CONSOLE(appname) \
678 IMPLEMENT_APP_NO_MAIN(appname) \
679 IMPLEMENT_WXWIN_MAIN_CONSOLE
681 // this macro can be used multiple times and just allows you to use wxGetApp()
683 #define DECLARE_APP(appname) extern appname& wxGetApp();
685 #endif // _WX_APP_H_BASE_