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
29 class WXDLLEXPORT wxApp
;
30 class WXDLLEXPORT wxAppTraits
;
31 class WXDLLEXPORT wxCmdLineParser
;
32 class WXDLLEXPORT wxLog
;
33 class WXDLLEXPORT wxMessageOutput
;
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // the type of the function used to create a wxApp object on program start up
40 typedef wxApp
* (*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 WXDLLEXPORT 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
;
78 // ----------------------------------------------------------------------------
79 // wxAppConsole: wxApp for non-GUI applications
80 // ----------------------------------------------------------------------------
82 class WXDLLEXPORT wxAppConsole
: public wxEvtHandler
87 virtual ~wxAppConsole();
90 // the virtual functions which may/must be overridden in the derived class
91 // -----------------------------------------------------------------------
93 // This is the very first function called for a newly created wxApp object,
94 // it is used by the library to do the global initialization. If, for some
95 // reason, you must override it (instead of just overriding OnInit(), as
96 // usual, for app-specific initializations), do not forget to call the base
98 virtual bool Initialize(int& argc
, wxChar
**argv
);
100 // Called before OnRun(), this is a good place to do initialization -- if
101 // anything fails, return false from here to prevent the program from
102 // continuing. The command line is normally parsed here, call the base
103 // class OnInit() to do it.
104 virtual bool OnInit();
106 // this is here only temproary hopefully (FIXME)
107 virtual bool OnInitGui() { return true; }
109 // This is the replacement for the normal main(): all program work should
110 // be done here. When OnRun() returns, the programs starts shutting down.
111 virtual int OnRun() = 0;
113 // This is only called if OnInit() returned true so it's a good place to do
114 // any cleanup matching the initializations done there.
115 virtual int OnExit();
117 // This is the very last function called on wxApp object before it is
118 // destroyed. If you override it (instead of overriding OnExit() as usual)
119 // do not forget to call the base class version!
120 virtual void CleanUp();
122 // Called when a fatal exception occurs, this function should take care not
123 // to do anything which might provoke a nested exception! It may be
124 // overridden if you wish to react somehow in non-default way (core dump
125 // under Unix, application crash under Windows) to fatal program errors,
126 // however extreme care should be taken if you don't want this function to
128 virtual void OnFatalException() { }
130 // Called from wxExit() function, should terminate the application a.s.a.p.
134 // application info: name, description, vendor
135 // -------------------------------------------
137 // NB: all these should be set by the application itself, there are no
138 // reasonable default except for the application name which is taken to
141 // set/get the application name
142 wxString
GetAppName() const
144 return m_appName
.empty() ? m_className
: m_appName
;
146 void SetAppName(const wxString
& name
) { m_appName
= name
; }
148 // set/get the app class name
149 wxString
GetClassName() const { return m_className
; }
150 void SetClassName(const wxString
& name
) { m_className
= name
; }
152 // set/get the vendor name
153 const wxString
& GetVendorName() const { return m_vendorName
; }
154 void SetVendorName(const wxString
& name
) { m_vendorName
= name
; }
157 // cmd line parsing stuff
158 // ----------------------
160 // all of these methods may be overridden in the derived class to
161 // customize the command line parsing (by default only a few standard
162 // options are handled)
164 // you also need to call wxApp::OnInit() from YourApp::OnInit() for all
167 #if wxUSE_CMDLINE_PARSER
168 // this one is called from OnInit() to add all supported options
169 // to the given parser
170 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
172 // called after successfully parsing the command line, return TRUE
173 // to continue and FALSE to exit
174 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
176 // called if "--help" option was specified, return TRUE to continue
178 virtual bool OnCmdLineHelp(wxCmdLineParser
& parser
);
180 // called if incorrect command line options were given, return
181 // FALSE to abort and TRUE to continue
182 virtual bool OnCmdLineError(wxCmdLineParser
& parser
);
183 #endif // wxUSE_CMDLINE_PARSER
186 // miscellaneous customization functions
187 // -------------------------------------
189 // create the app traits object to which we delegate for everything which
190 // either should be configurable by the user (then he can change the
191 // default behaviour simply by overriding CreateTraits() and returning his
192 // own traits object) or which is GUI/console dependent as then wxAppTraits
193 // allows us to abstract the differences behind the common façade
194 wxAppTraits
*GetTraits();
196 // the functions below shouldn't be used now that we have wxAppTraits
197 #if WXWIN_COMPATIBILITY_2_4
200 // override this function to create default log target of arbitrary
201 // user-defined class (default implementation creates a wxLogGui
202 // object) -- this log object is used by default by all wxLogXXX()
204 virtual wxLog
*CreateLogTarget();
207 // similar to CreateLogTarget() but for the global wxMessageOutput
209 virtual wxMessageOutput
*CreateMessageOutput();
211 #endif // WXWIN_COMPATIBILITY_2_4
214 // event processing functions
215 // --------------------------
217 // this method allows to filter all the events processed by the program, so
218 // you should try to return quickly from it to avoid slowing down the
219 // program to the crawl
221 // return value should be -1 to continue with the normal event processing,
222 // or TRUE or FALSE to stop further processing and pretend that the event
223 // had been already processed or won't be processed at all, respectively
224 virtual int FilterEvent(wxEvent
& event
);
226 // process all events in the wxPendingEvents list -- it is necessary to
227 // call this function to process posted events. This happens during each
228 // event loop iteration in GUI mode but if there is no main loop, it may be
229 // also called directly.
230 virtual void ProcessPendingEvents();
232 // doesn't do anything in this class, just a hook for GUI wxApp
233 virtual bool Yield(bool WXUNUSED(onlyIfNeeded
) = false) { return true; }
235 // make sure that idle events are sent again
236 virtual void WakeUpIdle() { }
242 // this function is called when an assert failure occurs, the base class
243 // version does the normal processing (i.e. shows the usual assert failure
246 // the arguments are the place where the assert occured, the text of the
247 // assert itself and the user-specified message
249 virtual void OnAssert(const wxChar
*file
,
253 #endif // __WXDEBUG__
255 // check that the wxBuildOptions object (constructed in the application
256 // itself, usually the one from IMPLEMENT_APP() macro) matches the build
257 // options of the library and abort if it doesn't
258 static bool CheckBuildOptions(const wxBuildOptions
& buildOptions
);
261 // implementation only from now on
262 // -------------------------------
264 // helpers for dynamic wxApp construction
265 static void SetInitializerFunction(wxAppInitializerFunction fn
)
266 { ms_appInitFn
= fn
; }
267 static wxAppInitializerFunction
GetInitializerFunction()
268 { return ms_appInitFn
; }
271 // command line arguments (public for backwards compatibility)
276 // the function which creates the traits object when GetTraits() needs it
277 // for the first time
278 virtual wxAppTraits
*CreateTraits();
281 // function used for dynamic wxApp creation
282 static wxAppInitializerFunction ms_appInitFn
;
284 // application info (must be set from the user code)
285 wxString m_vendorName
, // vendor name (ACME Inc)
286 m_appName
, // app name
287 m_className
; // class name
289 // the class defining the application behaviour, NULL initially and created
290 // by GetTraits() when first needed
291 wxAppTraits
*m_traits
;
294 // the application object is a singleton anyhow, there is no sense in
296 DECLARE_NO_COPY_CLASS(wxAppConsole
)
299 // ----------------------------------------------------------------------------
300 // wxAppBase: the common part of wxApp implementations for all platforms
301 // ----------------------------------------------------------------------------
305 class WXDLLEXPORT wxAppBase
: public wxAppConsole
309 virtual ~wxAppBase();
311 // the virtual functions which may/must be overridden in the derived class
312 // -----------------------------------------------------------------------
314 // very first initialization function
316 // Override: very rarely
317 virtual bool Initialize(int& argc
, wxChar
**argv
);
319 // a platform-dependent version of OnInit(): the code here is likely to
320 // depend on the toolkit. default version does nothing.
323 virtual bool OnInitGui();
325 // called to start program execution - the default version just enters
326 // the main GUI loop in which events are received and processed until
327 // the last window is not deleted (if GetExitOnFrameDelete) or
328 // ExitMainLoop() is called. In console mode programs, the execution
329 // of the program really starts here
331 // Override: rarely in GUI applications, always in console ones.
334 // very last clean up function
336 // Override: very rarely
337 virtual void CleanUp();
340 // the worker functions - usually not used directly by the user code
341 // -----------------------------------------------------------------
343 // execute the main GUI loop, the function returns when the loop ends
344 virtual int MainLoop() = 0;
346 // exit the main loop thus terminating the application
349 // exit the main GUI loop during the next iteration (i.e. it does not
350 // stop the program immediately!)
351 virtual void ExitMainLoop() = 0;
353 // returns TRUE if the program is initialized
354 virtual bool Initialized() = 0;
356 // returns TRUE if there are unprocessed events in the event queue
357 virtual bool Pending() = 0;
359 // process the first event in the event queue (blocks until an event
360 // apperas if there are none currently)
361 virtual void Dispatch() = 0;
363 // process all currently pending events right now
365 // it is an error to call Yield() recursively unless the value of
366 // onlyIfNeeded is TRUE
368 // WARNING: this function is dangerous as it can lead to unexpected
369 // reentrancies (i.e. when called from an event handler it
370 // may result in calling the same event handler again), use
371 // with _extreme_ care or, better, don't use at all!
372 virtual bool Yield(bool onlyIfNeeded
= FALSE
) = 0;
374 // this virtual function is called in the GUI mode when the application
375 // becomes idle and normally just sends wxIdleEvent to all interested
378 // it should return TRUE if more idle events are needed, FALSE if not
379 virtual bool ProcessIdle() = 0;
382 // top level window functions
383 // --------------------------
385 // return TRUE if our app has focus
386 virtual bool IsActive() const { return m_isActive
; }
388 // set the "main" top level window
389 void SetTopWindow(wxWindow
*win
) { m_topWindow
= win
; }
391 // return the "main" top level window (if it hadn't been set previously
392 // with SetTopWindow(), will return just some top level window and, if
393 // there are none, will return NULL)
394 virtual wxWindow
*GetTopWindow() const
398 else if (wxTopLevelWindows
.GetCount() > 0)
399 return wxTopLevelWindows
.GetFirst()->GetData();
401 return (wxWindow
*)NULL
;
404 // control the exit behaviour: by default, the program will exit the
405 // main loop (and so, usually, terminate) when the last top-level
406 // program window is deleted. Beware that if you disable this behaviour
407 // (with SetExitOnFrameDelete(FALSE)), you'll have to call
408 // ExitMainLoop() explicitly from somewhere.
409 void SetExitOnFrameDelete(bool flag
)
410 { m_exitOnFrameDelete
= flag
? Yes
: No
; }
411 bool GetExitOnFrameDelete() const
412 { return m_exitOnFrameDelete
== Yes
; }
415 // display mode, visual, printing mode, ...
416 // ------------------------------------------------------------------------
418 // Get display mode that is used use. This is only used in framebuffer
419 // wxWin ports (such as wxMGL).
420 virtual wxDisplayModeInfo
GetDisplayMode() const { return wxDisplayModeInfo(); }
421 // Set display mode to use. This is only used in framebuffer wxWin
422 // ports (such as wxMGL). This method should be called from
424 virtual bool SetDisplayMode(const wxDisplayModeInfo
& WXUNUSED(info
)) { return TRUE
; }
426 // set use of best visual flag (see below)
427 void SetUseBestVisual( bool flag
) { m_useBestVisual
= flag
; }
428 bool GetUseBestVisual() const { return m_useBestVisual
; }
430 // set/get printing mode: see wxPRINT_XXX constants.
432 // default behaviour is the normal one for Unix: always use PostScript
434 virtual void SetPrintMode(int WXUNUSED(mode
)) { }
435 int GetPrintMode() const { return wxPRINT_POSTSCRIPT
; }
438 // miscellaneous other stuff
439 // ------------------------------------------------------------------------
441 // called by toolkit-specific code to set the app status: active (we have
442 // focus) or not and also the last window which had focus before we were
444 virtual void SetActive(bool isActive
, wxWindow
*lastFocus
);
448 // delete all objects in wxPendingDelete list
449 void DeletePendingObjects();
451 // override base class method to use GUI traits
452 virtual wxAppTraits
*CreateTraits();
455 // the main top level window (may be NULL)
456 wxWindow
*m_topWindow
;
458 // if Yes, exit the main loop when the last top level window is deleted, if
459 // No don't do it and if Later -- only do it once we reach our OnRun()
461 // the explanation for using this strange scheme is given in appcmn.cpp
467 } m_exitOnFrameDelete
;
469 // TRUE if the apps whats to use the best visual on systems where
470 // more than one are available (Sun, SGI, XFree86 4.0 ?)
471 bool m_useBestVisual
;
473 // does any of our windows has focus?
477 DECLARE_NO_COPY_CLASS(wxAppBase
)
482 // ----------------------------------------------------------------------------
483 // now include the declaration of the real class
484 // ----------------------------------------------------------------------------
487 #if defined(__WXMSW__)
488 #include "wx/msw/app.h"
489 #elif defined(__WXMOTIF__)
490 #include "wx/motif/app.h"
491 #elif defined(__WXMGL__)
492 #include "wx/mgl/app.h"
493 #elif defined(__WXGTK__)
494 #include "wx/gtk/app.h"
495 #elif defined(__WXX11__)
496 #include "wx/x11/app.h"
497 #elif defined(__WXMAC__)
498 #include "wx/mac/app.h"
499 #elif defined(__WXCOCOA__)
500 #include "wx/cocoa/app.h"
501 #elif defined(__WXPM__)
502 #include "wx/os2/app.h"
505 // can't use typedef because wxApp forward declared as a class
506 class WXDLLEXPORT wxApp
: public wxAppConsole
511 // ----------------------------------------------------------------------------
513 // ----------------------------------------------------------------------------
515 // the one and only application object - use of wxTheApp in application code
516 // is discouraged, consider using DECLARE_APP() after which you may call
517 // wxGetApp() which will return the object of the correct type (i.e. MyApp and
519 WXDLLEXPORT_DATA(extern wxApp
*) wxTheApp
;
521 // ----------------------------------------------------------------------------
523 // ----------------------------------------------------------------------------
525 // event loop related functions only work in GUI programs
526 // ------------------------------------------------------
528 // Force an exit from main loop
529 extern void WXDLLEXPORT
wxExit();
531 // Yield to other apps/messages
532 extern bool WXDLLEXPORT
wxYield();
534 // Yield to other apps/messages
535 extern void WXDLLEXPORT
wxWakeUpIdle();
537 // ----------------------------------------------------------------------------
538 // macros for dynamic creation of the application object
539 // ----------------------------------------------------------------------------
541 // Having a global instance of this class allows wxApp to be aware of the app
542 // creator function. wxApp can then call this function to create a new app
543 // object. Convoluted, but necessary.
545 class WXDLLEXPORT wxAppInitializer
548 wxAppInitializer(wxAppInitializerFunction fn
)
549 { wxApp::SetInitializerFunction(fn
); }
552 // Here's a macro you can use if your compiler really, really wants main() to
553 // be in your main program (e.g. hello.cpp). Now IMPLEMENT_APP should add this
556 #if !wxUSE_GUI || defined(__WXMOTIF__) || defined(__WXGTK__) || defined(__WXPM__) || defined(__WXMGL__) || defined(__WXCOCOA__)
557 #define IMPLEMENT_WXWIN_MAIN \
558 extern int wxEntry( int argc, char **argv ); \
559 int main(int argc, char **argv) { return wxEntry(argc, argv); }
560 #elif defined(__WXMAC__)
561 // wxMac seems to have a specific wxEntry prototype
562 #define IMPLEMENT_WXWIN_MAIN \
563 extern int wxEntry( int argc, char **argv, bool enterLoop = TRUE ); \
564 int main(int argc, char **argv) { return wxEntry(argc, argv); }
565 #elif defined(__WXMSW__) && defined(WXUSINGDLL)
566 #define IMPLEMENT_WXWIN_MAIN \
567 extern int wxEntry(WXHINSTANCE hInstance, \
568 WXHINSTANCE hPrevInstance, \
571 extern "C" int wxSTDCALL WinMain(WXHINSTANCE hInstance, \
572 WXHINSTANCE hPrevInstance, \
576 return wxEntry(hInstance, hPrevInstance, lpCmdLine, nCmdShow); \
579 #define IMPLEMENT_WXWIN_MAIN
582 #ifdef __WXUNIVERSAL__
583 #include "wx/univ/theme.h"
585 #define IMPLEMENT_WX_THEME_SUPPORT \
586 WX_USE_THEME(win32); \
589 #define IMPLEMENT_WX_THEME_SUPPORT
592 // Use this macro if you want to define your own main() or WinMain() function
593 // and call wxEntry() from there.
594 #define IMPLEMENT_APP_NO_MAIN(appname) \
595 wxApp *wxCreateApp() \
597 wxApp::CheckBuildOptions(wxBuildOptions()); \
598 return new appname; \
601 wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
602 appname& wxGetApp() { return *(appname *)wxTheApp; }
604 // Same as IMPLEMENT_APP() normally but doesn't include themes support in
605 // wxUniversal builds
606 #define IMPLEMENT_APP_NO_THEMES(appname) \
607 IMPLEMENT_APP_NO_MAIN(appname) \
610 // Use this macro exactly once, the argument is the name of the wxApp-derived
611 // class which is the class of your application.
612 #define IMPLEMENT_APP(appname) \
613 IMPLEMENT_APP_NO_THEMES(appname) \
614 IMPLEMENT_WX_THEME_SUPPORT
616 // this macro can be used multiple times and just allows you to use wxGetApp()
618 #define DECLARE_APP(appname) extern appname& wxGetApp();
620 #endif // _WX_APP_H_BASE_