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 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // the type of the function used to create a wxApp object on program start up
40 typedef wxAppConsole
* (*wxAppInitializerFunction
)();
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
49 wxPRINT_POSTSCRIPT
= 2
52 // ----------------------------------------------------------------------------
53 // support for framebuffer ports
54 // ----------------------------------------------------------------------------
57 // VS: Fullscreen/framebuffer application needs to choose display mode prior
58 // to wxWindows initialization. This class holds information about display
59 // mode. It is used by wxApp::Set/GetDisplayMode.
60 class WXDLLIMPEXP_CORE wxDisplayModeInfo
63 wxDisplayModeInfo() : m_ok(FALSE
) {}
64 wxDisplayModeInfo(unsigned width
, unsigned height
, unsigned depth
)
65 : m_width(width
), m_height(height
), m_depth(depth
), m_ok(TRUE
) {}
67 unsigned GetWidth() const { return m_width
; }
68 unsigned GetHeight() const { return m_height
; }
69 unsigned GetDepth() const { return m_depth
; }
70 bool IsOk() const { return m_ok
; }
73 unsigned m_width
, m_height
, m_depth
;
79 // ----------------------------------------------------------------------------
80 // wxAppConsole: wxApp for non-GUI applications
81 // ----------------------------------------------------------------------------
83 class WXDLLIMPEXP_BASE wxAppConsole
: public wxEvtHandler
88 virtual ~wxAppConsole();
91 // the virtual functions which may/must be overridden in the derived class
92 // -----------------------------------------------------------------------
94 // This is the very first function called for a newly created wxApp object,
95 // it is used by the library to do the global initialization. If, for some
96 // reason, you must override it (instead of just overriding OnInit(), as
97 // usual, for app-specific initializations), do not forget to call the base
99 virtual bool Initialize(int& argc
, wxChar
**argv
);
101 // This gives wxCocoa a chance to call OnInit() with a memory pool in place
102 virtual bool CallOnInit() { return OnInit(); }
104 // Called before OnRun(), this is a good place to do initialization -- if
105 // anything fails, return false from here to prevent the program from
106 // continuing. The command line is normally parsed here, call the base
107 // class OnInit() to do it.
108 virtual bool OnInit();
110 // this is here only temproary hopefully (FIXME)
111 virtual bool OnInitGui() { return true; }
113 // This is the replacement for the normal main(): all program work should
114 // be done here. When OnRun() returns, the programs starts shutting down.
115 virtual int OnRun() = 0;
117 // This is only called if OnInit() returned true so it's a good place to do
118 // any cleanup matching the initializations done there.
119 virtual int OnExit();
121 // This is the very last function called on wxApp object before it is
122 // destroyed. If you override it (instead of overriding OnExit() as usual)
123 // do not forget to call the base class version!
124 virtual void CleanUp();
126 // Called when a fatal exception occurs, this function should take care not
127 // to do anything which might provoke a nested exception! It may be
128 // overridden if you wish to react somehow in non-default way (core dump
129 // under Unix, application crash under Windows) to fatal program errors,
130 // however extreme care should be taken if you don't want this function to
132 virtual void OnFatalException() { }
134 // Called from wxExit() function, should terminate the application a.s.a.p.
138 // application info: name, description, vendor
139 // -------------------------------------------
141 // NB: all these should be set by the application itself, there are no
142 // reasonable default except for the application name which is taken to
145 // set/get the application name
146 wxString
GetAppName() const
148 return m_appName
.empty() ? m_className
: m_appName
;
150 void SetAppName(const wxString
& name
) { m_appName
= name
; }
152 // set/get the app class name
153 wxString
GetClassName() const { return m_className
; }
154 void SetClassName(const wxString
& name
) { m_className
= name
; }
156 // set/get the vendor name
157 const wxString
& GetVendorName() const { return m_vendorName
; }
158 void SetVendorName(const wxString
& name
) { m_vendorName
= name
; }
161 // cmd line parsing stuff
162 // ----------------------
164 // all of these methods may be overridden in the derived class to
165 // customize the command line parsing (by default only a few standard
166 // options are handled)
168 // you also need to call wxApp::OnInit() from YourApp::OnInit() for all
171 #if wxUSE_CMDLINE_PARSER
172 // this one is called from OnInit() to add all supported options
173 // to the given parser (don't forget to call the base class version if you
175 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
177 // called after successfully parsing the command line, return TRUE
178 // to continue and FALSE to exit (don't forget to call the base class
179 // version if you override it!)
180 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
182 // called if "--help" option was specified, return TRUE to continue
184 virtual bool OnCmdLineHelp(wxCmdLineParser
& parser
);
186 // called if incorrect command line options were given, return
187 // FALSE to abort and TRUE to continue
188 virtual bool OnCmdLineError(wxCmdLineParser
& parser
);
189 #endif // wxUSE_CMDLINE_PARSER
192 // miscellaneous customization functions
193 // -------------------------------------
195 // create the app traits object to which we delegate for everything which
196 // either should be configurable by the user (then he can change the
197 // default behaviour simply by overriding CreateTraits() and returning his
198 // own traits object) or which is GUI/console dependent as then wxAppTraits
199 // allows us to abstract the differences behind the common façade
200 wxAppTraits
*GetTraits();
202 // the functions below shouldn't be used now that we have wxAppTraits
203 #if WXWIN_COMPATIBILITY_2_4
206 // override this function to create default log target of arbitrary
207 // user-defined class (default implementation creates a wxLogGui
208 // object) -- this log object is used by default by all wxLogXXX()
210 virtual wxLog
*CreateLogTarget();
213 // similar to CreateLogTarget() but for the global wxMessageOutput
215 virtual wxMessageOutput
*CreateMessageOutput();
217 #endif // WXWIN_COMPATIBILITY_2_4
220 // event processing functions
221 // --------------------------
223 // this method allows to filter all the events processed by the program, so
224 // you should try to return quickly from it to avoid slowing down the
225 // program to the crawl
227 // return value should be -1 to continue with the normal event processing,
228 // or TRUE or FALSE to stop further processing and pretend that the event
229 // had been already processed or won't be processed at all, respectively
230 virtual int FilterEvent(wxEvent
& event
);
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() { }
248 // this function is called when an assert failure occurs, the base class
249 // version does the normal processing (i.e. shows the usual assert failure
252 // the arguments are the place where the assert occured, the text of the
253 // assert itself and the user-specified message
255 virtual void OnAssert(const wxChar
*file
,
259 #endif // __WXDEBUG__
261 // check that the wxBuildOptions object (constructed in the application
262 // itself, usually the one from IMPLEMENT_APP() macro) matches the build
263 // options of the library and abort if it doesn't
264 static bool CheckBuildOptions(const wxBuildOptions
& buildOptions
);
267 // implementation only from now on
268 // -------------------------------
270 // helpers for dynamic wxApp construction
271 static void SetInitializerFunction(wxAppInitializerFunction fn
)
272 { ms_appInitFn
= fn
; }
273 static wxAppInitializerFunction
GetInitializerFunction()
274 { return ms_appInitFn
; }
276 // accessors for ms_appInstance field (external code might wish to modify
277 // it, this is why we provide a setter here as well, but you should really
278 // know what you're doing if you call it), wxTheApp is usually used instead
280 static wxAppConsole
*GetInstance() { return ms_appInstance
; }
281 static void SetInstance(wxAppConsole
*app
) { ms_appInstance
= app
; }
284 // command line arguments (public for backwards compatibility)
289 // the function which creates the traits object when GetTraits() needs it
290 // for the first time
291 virtual wxAppTraits
*CreateTraits();
294 // function used for dynamic wxApp creation
295 static wxAppInitializerFunction ms_appInitFn
;
297 // the one and only global application object
298 static wxAppConsole
*ms_appInstance
;
301 // application info (must be set from the user code)
302 wxString m_vendorName
, // vendor name (ACME Inc)
303 m_appName
, // app name
304 m_className
; // class name
306 // the class defining the application behaviour, NULL initially and created
307 // by GetTraits() when first needed
308 wxAppTraits
*m_traits
;
311 // the application object is a singleton anyhow, there is no sense in
313 DECLARE_NO_COPY_CLASS(wxAppConsole
)
316 // ----------------------------------------------------------------------------
317 // wxAppBase: the common part of wxApp implementations for all platforms
318 // ----------------------------------------------------------------------------
322 class WXDLLIMPEXP_CORE wxAppBase
: public wxAppConsole
326 virtual ~wxAppBase();
328 // the virtual functions which may/must be overridden in the derived class
329 // -----------------------------------------------------------------------
331 // very first initialization function
333 // Override: very rarely
334 virtual bool Initialize(int& argc
, wxChar
**argv
);
336 // a platform-dependent version of OnInit(): the code here is likely to
337 // depend on the toolkit. default version does nothing.
340 virtual bool OnInitGui();
342 // called to start program execution - the default version just enters
343 // the main GUI loop in which events are received and processed until
344 // the last window is not deleted (if GetExitOnFrameDelete) or
345 // ExitMainLoop() is called. In console mode programs, the execution
346 // of the program really starts here
348 // Override: rarely in GUI applications, always in console ones.
351 // a matching function for OnInit()
352 virtual int OnExit();
354 // very last clean up function
356 // Override: very rarely
357 virtual void CleanUp();
360 // the worker functions - usually not used directly by the user code
361 // -----------------------------------------------------------------
363 // execute the main GUI loop, the function returns when the loop ends
364 virtual int MainLoop() = 0;
366 // exit the main loop thus terminating the application
369 // exit the main GUI loop during the next iteration (i.e. it does not
370 // stop the program immediately!)
371 virtual void ExitMainLoop() = 0;
373 // returns TRUE if the program is initialized
374 virtual bool Initialized() = 0;
376 // returns TRUE if there are unprocessed events in the event queue
377 virtual bool Pending() = 0;
379 // process the first event in the event queue (blocks until an event
380 // apperas if there are none currently)
381 virtual void Dispatch() = 0;
383 // process all currently pending events right now
385 // it is an error to call Yield() recursively unless the value of
386 // onlyIfNeeded is TRUE
388 // WARNING: this function is dangerous as it can lead to unexpected
389 // reentrancies (i.e. when called from an event handler it
390 // may result in calling the same event handler again), use
391 // with _extreme_ care or, better, don't use at all!
392 virtual bool Yield(bool onlyIfNeeded
= FALSE
) = 0;
394 // this virtual function is called in the GUI mode when the application
395 // becomes idle and normally just sends wxIdleEvent to all interested
398 // it should return TRUE if more idle events are needed, FALSE if not
399 virtual bool ProcessIdle() ;
401 // Send idle event to window and all subwindows
402 // Returns TRUE if more idle time is requested.
403 virtual bool SendIdleEvents(wxWindow
* win
, wxIdleEvent
& event
);
405 // Perform standard OnIdle behaviour: call from port's OnIdle
406 void OnIdle(wxIdleEvent
& event
);
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
425 else if (wxTopLevelWindows
.GetCount() > 0)
426 return wxTopLevelWindows
.GetFirst()->GetData();
428 return (wxWindow
*)NULL
;
431 // control the exit behaviour: by default, the program will exit the
432 // main loop (and so, usually, terminate) when the last top-level
433 // program window is deleted. Beware that if you disable this behaviour
434 // (with SetExitOnFrameDelete(FALSE)), you'll have to call
435 // ExitMainLoop() explicitly from somewhere.
436 void SetExitOnFrameDelete(bool flag
)
437 { m_exitOnFrameDelete
= flag
? Yes
: No
; }
438 bool GetExitOnFrameDelete() const
439 { return m_exitOnFrameDelete
== Yes
; }
442 // display mode, visual, printing mode, ...
443 // ------------------------------------------------------------------------
445 // Get display mode that is used use. This is only used in framebuffer
446 // wxWin ports (such as wxMGL).
447 virtual wxDisplayModeInfo
GetDisplayMode() const { return wxDisplayModeInfo(); }
448 // Set display mode to use. This is only used in framebuffer wxWin
449 // ports (such as wxMGL). This method should be called from
451 virtual bool SetDisplayMode(const wxDisplayModeInfo
& WXUNUSED(info
)) { return TRUE
; }
453 // set use of best visual flag (see below)
454 void SetUseBestVisual( bool flag
) { m_useBestVisual
= flag
; }
455 bool GetUseBestVisual() const { return m_useBestVisual
; }
457 // set/get printing mode: see wxPRINT_XXX constants.
459 // default behaviour is the normal one for Unix: always use PostScript
461 virtual void SetPrintMode(int WXUNUSED(mode
)) { }
462 int GetPrintMode() const { return wxPRINT_POSTSCRIPT
; }
465 // command line parsing (GUI-specific)
466 // ------------------------------------------------------------------------
468 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
469 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
472 // miscellaneous other stuff
473 // ------------------------------------------------------------------------
475 // called by toolkit-specific code to set the app status: active (we have
476 // focus) or not and also the last window which had focus before we were
478 virtual void SetActive(bool isActive
, wxWindow
*lastFocus
);
482 // delete all objects in wxPendingDelete list
483 void DeletePendingObjects();
485 // override base class method to use GUI traits
486 virtual wxAppTraits
*CreateTraits();
489 // the main top level window (may be NULL)
490 wxWindow
*m_topWindow
;
492 // if Yes, exit the main loop when the last top level window is deleted, if
493 // No don't do it and if Later -- only do it once we reach our OnRun()
495 // the explanation for using this strange scheme is given in appcmn.cpp
501 } m_exitOnFrameDelete
;
503 // TRUE if the apps whats to use the best visual on systems where
504 // more than one are available (Sun, SGI, XFree86 4.0 ?)
505 bool m_useBestVisual
;
507 // does any of our windows has focus?
511 DECLARE_NO_COPY_CLASS(wxAppBase
)
516 // ----------------------------------------------------------------------------
517 // now include the declaration of the real class
518 // ----------------------------------------------------------------------------
521 #if defined(__WXMSW__)
522 #include "wx/msw/app.h"
523 #elif defined(__WXMOTIF__)
524 #include "wx/motif/app.h"
525 #elif defined(__WXMGL__)
526 #include "wx/mgl/app.h"
527 #elif defined(__WXGTK__)
528 #include "wx/gtk/app.h"
529 #elif defined(__WXX11__)
530 #include "wx/x11/app.h"
531 #elif defined(__WXMAC__)
532 #include "wx/mac/app.h"
533 #elif defined(__WXCOCOA__)
534 #include "wx/cocoa/app.h"
535 #elif defined(__WXPM__)
536 #include "wx/os2/app.h"
539 // allow using just wxApp (instead of wxAppConsole) in console programs
540 typedef wxAppConsole wxApp
;
543 // ----------------------------------------------------------------------------
545 // ----------------------------------------------------------------------------
547 // for compatibility, we define this macro to access the global application
548 // object of type wxApp
550 // note that instead of using of wxTheApp in application code you should
551 // consider using DECLARE_APP() after which you may call wxGetApp() which will
552 // return the object of the correct type (i.e. MyApp and not wxApp)
554 // the cast is safe as in GUI build we only use wxApp, not wxAppConsole, and in
555 // console mode it does nothing at all
556 #define wxTheApp ((wxApp *)wxApp::GetInstance())
558 // ----------------------------------------------------------------------------
560 // ----------------------------------------------------------------------------
562 // event loop related functions only work in GUI programs
563 // ------------------------------------------------------
565 // Force an exit from main loop
566 extern void WXDLLIMPEXP_BASE
wxExit();
568 // Yield to other apps/messages
569 extern bool WXDLLIMPEXP_BASE
wxYield();
571 // Yield to other apps/messages
572 extern void WXDLLIMPEXP_BASE
wxWakeUpIdle();
574 // ----------------------------------------------------------------------------
575 // macros for dynamic creation of the application object
576 // ----------------------------------------------------------------------------
578 // Having a global instance of this class allows wxApp to be aware of the app
579 // creator function. wxApp can then call this function to create a new app
580 // object. Convoluted, but necessary.
582 class WXDLLIMPEXP_BASE wxAppInitializer
585 wxAppInitializer(wxAppInitializerFunction fn
)
586 { wxApp::SetInitializerFunction(fn
); }
589 // Here's a macro you can use if your compiler really, really wants main() to
590 // be in your main program (e.g. hello.cpp). Now IMPLEMENT_APP should add this
593 #if !wxUSE_GUI || !defined(__WXMSW__)
594 #define IMPLEMENT_WXWIN_MAIN \
595 int main(int argc, char **argv) { return wxEntry(argc, argv); }
596 #elif defined(__WXMSW__) && defined(WXUSINGDLL)
597 // we need HINSTANCE declaration to define WinMain()
598 #include "wx/msw/wrapwin.h"
600 #define IMPLEMENT_WXWIN_MAIN \
601 extern int wxEntry(HINSTANCE hInstance, \
602 HINSTANCE hPrevInstance = NULL, \
603 char *pCmdLine = NULL, \
604 int nCmdShow = SW_NORMAL); \
605 extern "C" int WINAPI WinMain(HINSTANCE hInstance, \
606 HINSTANCE hPrevInstance, \
610 return wxEntry(hInstance, hPrevInstance, lpCmdLine, nCmdShow); \
613 #define IMPLEMENT_WXWIN_MAIN
616 #ifdef __WXUNIVERSAL__
617 #include "wx/univ/theme.h"
619 #define IMPLEMENT_WX_THEME_SUPPORT \
620 WX_USE_THEME(win32); \
623 #define IMPLEMENT_WX_THEME_SUPPORT
626 // Use this macro if you want to define your own main() or WinMain() function
627 // and call wxEntry() from there.
628 #define IMPLEMENT_APP_NO_MAIN(appname) \
629 wxAppConsole *wxCreateApp() \
631 wxAppConsole::CheckBuildOptions(wxBuildOptions()); \
632 return new appname; \
635 wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
636 appname& wxGetApp() { return *(appname *)wxTheApp; }
638 // Same as IMPLEMENT_APP() normally but doesn't include themes support in
639 // wxUniversal builds
640 #define IMPLEMENT_APP_NO_THEMES(appname) \
641 IMPLEMENT_APP_NO_MAIN(appname) \
644 // Use this macro exactly once, the argument is the name of the wxApp-derived
645 // class which is the class of your application.
646 #define IMPLEMENT_APP(appname) \
647 IMPLEMENT_APP_NO_THEMES(appname) \
648 IMPLEMENT_WX_THEME_SUPPORT
650 // this macro can be used multiple times and just allows you to use wxGetApp()
652 #define DECLARE_APP(appname) extern appname& wxGetApp();
654 #endif // _WX_APP_H_BASE_