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
28 class WXDLLEXPORT wxApp
;
29 class WXDLLEXPORT wxAppTraits
;
30 class WXDLLEXPORT wxCmdLineParser
;
31 class WXDLLEXPORT wxLog
;
32 class WXDLLEXPORT wxMessageOutput
;
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // the type of the function used to create a wxApp object on program start up
39 typedef wxApp
* (*wxAppInitializerFunction
)();
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
48 wxPRINT_POSTSCRIPT
= 2
51 // ----------------------------------------------------------------------------
52 // support for framebuffer ports
53 // ----------------------------------------------------------------------------
56 // VS: Fullscreen/framebuffer application needs to choose display mode prior
57 // to wxWindows initialization. This class holds information about display
58 // mode. It is used by wxApp::Set/GetDisplayMode.
59 class WXDLLEXPORT wxDisplayModeInfo
62 wxDisplayModeInfo() : m_ok(FALSE
) {}
63 wxDisplayModeInfo(unsigned width
, unsigned height
, unsigned depth
)
64 : m_width(width
), m_height(height
), m_depth(depth
), m_ok(TRUE
) {}
66 unsigned GetWidth() const { return m_width
; }
67 unsigned GetHeight() const { return m_height
; }
68 unsigned GetDepth() const { return m_depth
; }
69 bool IsOk() const { return m_ok
; }
72 unsigned m_width
, m_height
, m_depth
;
77 // ----------------------------------------------------------------------------
78 // wxAppConsole: wxApp for non-GUI applications
79 // ----------------------------------------------------------------------------
81 class WXDLLEXPORT wxAppConsole
: public wxEvtHandler
86 virtual ~wxAppConsole();
89 // the virtual functions which may/must be overridden in the derived class
90 // -----------------------------------------------------------------------
92 // Called before OnRun(), this is a good place to do initialization -- if
93 // anything fails, return false from here to prevent the program from
94 // continuing. The command line is normally parsed here, call the base
95 // class OnInit() to do it.
96 virtual bool OnInit();
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 // 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.
118 // application info: name, description, vendor
119 // -------------------------------------------
121 // NB: all these should be set by the application itself, there are no
122 // reasonable default except for the application name which is taken to
125 // set/get the application name
126 wxString
GetAppName() const
128 return m_appName
.empty() ? m_className
: m_appName
;
130 void SetAppName(const wxString
& name
) { m_appName
= name
; }
132 // set/get the app class name
133 wxString
GetClassName() const { return m_className
; }
134 void SetClassName(const wxString
& name
) { m_className
= name
; }
136 // set/get the vendor name
137 const wxString
& GetVendorName() const { return m_vendorName
; }
138 void SetVendorName(const wxString
& name
) { m_vendorName
= name
; }
141 // cmd line parsing stuff
142 // ----------------------
144 // all of these methods may be overridden in the derived class to
145 // customize the command line parsing (by default only a few standard
146 // options are handled)
148 // you also need to call wxApp::OnInit() from YourApp::OnInit() for all
151 #if wxUSE_CMDLINE_PARSER
152 // this one is called from OnInit() to add all supported options
153 // to the given parser
154 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
156 // called after successfully parsing the command line, return TRUE
157 // to continue and FALSE to exit
158 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
160 // called if "--help" option was specified, return TRUE to continue
162 virtual bool OnCmdLineHelp(wxCmdLineParser
& parser
);
164 // called if incorrect command line options were given, return
165 // FALSE to abort and TRUE to continue
166 virtual bool OnCmdLineError(wxCmdLineParser
& parser
);
167 #endif // wxUSE_CMDLINE_PARSER
170 // miscellaneous customization functions
171 // -------------------------------------
173 // create the app traits object to which we delegate for everything which
174 // either should be configurable by the user (then he can change the
175 // default behaviour simply by overriding CreateTraits() and returning his
176 // own traits object) or which is GUI/console dependent as then wxAppTraits
177 // allows us to abstract the differences behind the common façade
178 wxAppTraits
*GetTraits();
180 // the functions below shouldn't be used now that we have wxAppTraits
181 #if WXWIN_COMPATIBILITY_2_4
184 // override this function to create default log target of arbitrary
185 // user-defined class (default implementation creates a wxLogGui
186 // object) -- this log object is used by default by all wxLogXXX()
188 virtual wxLog
*CreateLogTarget();
191 // similar to CreateLogTarget() but for the global wxMessageOutput
193 virtual wxMessageOutput
*CreateMessageOutput();
195 #endif // WXWIN_COMPATIBILITY_2_4
198 // event processing functions
199 // --------------------------
201 // this method allows to filter all the events processed by the program, so
202 // you should try to return quickly from it to avoid slowing down the
203 // program to the crawl
205 // return value should be -1 to continue with the normal event processing,
206 // or TRUE or FALSE to stop further processing and pretend that the event
207 // had been already processed or won't be processed at all, respectively
208 virtual int FilterEvent(wxEvent
& event
);
210 // process all events in the wxPendingEvents list -- it is necessary to
211 // call this function to process posted events. This happens during each
212 // event loop iteration in GUI mode but if there is no main loop, it may be
213 // also called directly.
214 virtual void ProcessPendingEvents();
216 // doesn't do anything in this class, just a hook for GUI wxApp
217 virtual bool Yield(bool WXUNUSED(onlyIfNeeded
) = false) { return true; }
219 // make sure that idle events are sent again
220 virtual void WakeUpIdle() { }
226 // this function is called when an assert failure occurs, the base class
227 // version does the normal processing (i.e. shows the usual assert failure
230 // the arguments are the place where the assert occured, the text of the
231 // assert itself and the user-specified message
233 virtual void OnAssert(const wxChar
*file
,
237 #endif // __WXDEBUG__
239 // check that the wxBuildOptions object (constructed in the application
240 // itself, usually the one from IMPLEMENT_APP() macro) matches the build
241 // options of the library and abort if it doesn't
242 static bool CheckBuildOptions(const wxBuildOptions
& buildOptions
);
245 // implementation only from now on
246 // -------------------------------
248 // helpers for dynamic wxApp construction
249 static void SetInitializerFunction(wxAppInitializerFunction fn
)
250 { ms_appInitFn
= fn
; }
251 static wxAppInitializerFunction
GetInitializerFunction()
252 { return ms_appInitFn
; }
255 // command line arguments (public for backwards compatibility)
260 // the function which creates the traits object when GetTraits() needs it
261 // for the first time
262 virtual wxAppTraits
*CreateTraits();
265 // function used for dynamic wxApp creation
266 static wxAppInitializerFunction ms_appInitFn
;
268 // application info (must be set from the user code)
269 wxString m_vendorName
, // vendor name (ACME Inc)
270 m_appName
, // app name
271 m_className
; // class name
273 // the class defining the application behaviour, NULL initially and created
274 // by GetTraits() when first needed
275 wxAppTraits
*m_traits
;
278 // the application object is a singleton anyhow, there is no sense in
280 DECLARE_NO_COPY_CLASS(wxAppConsole
)
283 // ----------------------------------------------------------------------------
284 // wxAppBase: the common part of wxApp implementations for all platforms
285 // ----------------------------------------------------------------------------
289 class WXDLLEXPORT wxAppBase
: public wxAppConsole
293 virtual ~wxAppBase();
295 // the virtual functions which may/must be overridden in the derived class
296 // -----------------------------------------------------------------------
298 // a platform-dependent version of OnInit(): the code here is likely to
299 // depend on the toolkit. default version does nothing.
302 virtual bool OnInitGui();
304 // called to start program execution - the default version just enters
305 // the main GUI loop in which events are received and processed until
306 // the last window is not deleted (if GetExitOnFrameDelete) or
307 // ExitMainLoop() is called. In console mode programs, the execution
308 // of the program really starts here
310 // Override: rarely in GUI applications, always in console ones.
313 // exit the main loop thus terminating the application
317 // the worker functions - usually not used directly by the user code
318 // -----------------------------------------------------------------
320 // execute the main GUI loop, the function returns when the loop ends
321 virtual int MainLoop() = 0;
323 // exit the main GUI loop during the next iteration (i.e. it does not
324 // stop the program immediately!)
325 virtual void ExitMainLoop() = 0;
327 // returns TRUE if the program is initialized
328 virtual bool Initialized() = 0;
330 // returns TRUE if there are unprocessed events in the event queue
331 virtual bool Pending() = 0;
333 // process the first event in the event queue (blocks until an event
334 // apperas if there are none currently)
335 virtual void Dispatch() = 0;
337 // process all currently pending events right now
339 // it is an error to call Yield() recursively unless the value of
340 // onlyIfNeeded is TRUE
342 // WARNING: this function is dangerous as it can lead to unexpected
343 // reentrancies (i.e. when called from an event handler it
344 // may result in calling the same event handler again), use
345 // with _extreme_ care or, better, don't use at all!
346 virtual bool Yield(bool onlyIfNeeded
= FALSE
) = 0;
348 // this virtual function is called in the GUI mode when the application
349 // becomes idle and normally just sends wxIdleEvent to all interested
352 // it should return TRUE if more idle events are needed, FALSE if not
353 virtual bool ProcessIdle() = 0;
356 // top level window functions
357 // --------------------------
359 // return TRUE if our app has focus
360 virtual bool IsActive() const { return m_isActive
; }
362 // set the "main" top level window
363 void SetTopWindow(wxWindow
*win
) { m_topWindow
= win
; }
365 // return the "main" top level window (if it hadn't been set previously
366 // with SetTopWindow(), will return just some top level window and, if
367 // there are none, will return NULL)
368 virtual wxWindow
*GetTopWindow() const
372 else if (wxTopLevelWindows
.GetCount() > 0)
373 return wxTopLevelWindows
.GetFirst()->GetData();
375 return (wxWindow
*)NULL
;
378 // control the exit behaviour: by default, the program will exit the
379 // main loop (and so, usually, terminate) when the last top-level
380 // program window is deleted. Beware that if you disable this behaviour
381 // (with SetExitOnFrameDelete(FALSE)), you'll have to call
382 // ExitMainLoop() explicitly from somewhere.
383 void SetExitOnFrameDelete(bool flag
)
384 { m_exitOnFrameDelete
= flag
? Yes
: No
; }
385 bool GetExitOnFrameDelete() const
386 { return m_exitOnFrameDelete
== Yes
; }
389 // display mode, visual, printing mode, ...
390 // ------------------------------------------------------------------------
392 // Get display mode that is used use. This is only used in framebuffer
393 // wxWin ports (such as wxMGL).
394 virtual wxDisplayModeInfo
GetDisplayMode() const { return wxDisplayModeInfo(); }
395 // Set display mode to use. This is only used in framebuffer wxWin
396 // ports (such as wxMGL). This method should be called from
398 virtual bool SetDisplayMode(const wxDisplayModeInfo
& WXUNUSED(info
)) { return TRUE
; }
400 // set use of best visual flag (see below)
401 void SetUseBestVisual( bool flag
) { m_useBestVisual
= flag
; }
402 bool GetUseBestVisual() const { return m_useBestVisual
; }
404 // set/get printing mode: see wxPRINT_XXX constants.
406 // default behaviour is the normal one for Unix: always use PostScript
408 virtual void SetPrintMode(int WXUNUSED(mode
)) { }
409 int GetPrintMode() const { return wxPRINT_POSTSCRIPT
; }
412 // miscellaneous other stuff
413 // ------------------------------------------------------------------------
415 // called by toolkit-specific code to set the app status: active (we have
416 // focus) or not and also the last window which had focus before we were
418 virtual void SetActive(bool isActive
, wxWindow
*lastFocus
);
422 // override base class method to use GUI traits
423 virtual wxAppTraits
*CreateTraits();
426 // the main top level window (may be NULL)
427 wxWindow
*m_topWindow
;
429 // if Yes, exit the main loop when the last top level window is deleted, if
430 // No don't do it and if Later -- only do it once we reach our OnRun()
432 // the explanation for using this strange scheme is given in appcmn.cpp
438 } m_exitOnFrameDelete
;
440 // TRUE if the apps whats to use the best visual on systems where
441 // more than one are available (Sun, SGI, XFree86 4.0 ?)
442 bool m_useBestVisual
;
444 // does any of our windows has focus?
448 DECLARE_NO_COPY_CLASS(wxAppBase
)
453 // ----------------------------------------------------------------------------
454 // now include the declaration of the real class
455 // ----------------------------------------------------------------------------
458 #if defined(__WXMSW__)
459 #include "wx/msw/app.h"
460 #elif defined(__WXMOTIF__)
461 #include "wx/motif/app.h"
462 #elif defined(__WXMGL__)
463 #include "wx/mgl/app.h"
464 #elif defined(__WXGTK__)
465 #include "wx/gtk/app.h"
466 #elif defined(__WXX11__)
467 #include "wx/x11/app.h"
468 #elif defined(__WXMAC__)
469 #include "wx/mac/app.h"
470 #elif defined(__WXCOCOA__)
471 #include "wx/cocoa/app.h"
472 #elif defined(__WXPM__)
473 #include "wx/os2/app.h"
476 // can't use typedef because wxApp forward declared as a class
477 class WXDLLEXPORT wxApp
: public wxAppConsole
482 // ----------------------------------------------------------------------------
484 // ----------------------------------------------------------------------------
486 // the one and only application object - use of wxTheApp in application code
487 // is discouraged, consider using DECLARE_APP() after which you may call
488 // wxGetApp() which will return the object of the correct type (i.e. MyApp and
490 WXDLLEXPORT_DATA(extern wxApp
*) wxTheApp
;
492 // ----------------------------------------------------------------------------
494 // ----------------------------------------------------------------------------
496 // event loop related functions only work in GUI programs
497 // ------------------------------------------------------
499 // Force an exit from main loop
500 extern void WXDLLEXPORT
wxExit();
502 // Yield to other apps/messages
503 extern bool WXDLLEXPORT
wxYield();
505 // Yield to other apps/messages
506 extern void WXDLLEXPORT
wxWakeUpIdle();
509 // console applications may avoid using DECLARE_APP and IMPLEMENT_APP macros
510 // and call these functions instead at the program startup and termination
511 // -------------------------------------------------------------------------
515 // initialize the library (may be called as many times as needed, but each
516 // call to wxInitialize() must be matched by wxUninitialize())
517 extern bool WXDLLEXPORT
wxInitialize();
519 // clean up - the library can't be used any more after the last call to
521 extern void WXDLLEXPORT
wxUninitialize();
523 // create an object of this class on stack to initialize/cleanup thel ibrary
525 class WXDLLEXPORT wxInitializer
528 // initialize the library
529 wxInitializer() { m_ok
= wxInitialize(); }
531 // has the initialization been successful? (explicit test)
532 bool IsOk() const { return m_ok
; }
534 // has the initialization been successful? (implicit test)
535 operator bool() const { return m_ok
; }
537 // dtor only does clean up if we initialized the library properly
538 ~wxInitializer() { if ( m_ok
) wxUninitialize(); }
546 // ----------------------------------------------------------------------------
547 // macros for dynamic creation of the application object
548 // ----------------------------------------------------------------------------
550 // Having a global instance of this class allows wxApp to be aware of the app
551 // creator function. wxApp can then call this function to create a new app
552 // object. Convoluted, but necessary.
554 class WXDLLEXPORT wxAppInitializer
557 wxAppInitializer(wxAppInitializerFunction fn
)
558 { wxApp::SetInitializerFunction(fn
); }
561 // Here's a macro you can use if your compiler really, really wants main() to
562 // be in your main program (e.g. hello.cpp). Now IMPLEMENT_APP should add this
565 #if !wxUSE_GUI || defined(__WXMOTIF__) || defined(__WXGTK__) || defined(__WXPM__) || defined(__WXMGL__) || defined(__WXCOCOA__)
566 #define IMPLEMENT_WXWIN_MAIN \
567 extern int wxEntry( int argc, char **argv ); \
568 int main(int argc, char **argv) { return wxEntry(argc, argv); }
569 #elif defined(__WXMAC__)
570 // wxMac seems to have a specific wxEntry prototype
571 #define IMPLEMENT_WXWIN_MAIN \
572 extern int wxEntry( int argc, char **argv, bool enterLoop = TRUE ); \
573 int main(int argc, char **argv) { return wxEntry(argc, argv); }
574 #elif defined(__WXMSW__) && defined(WXUSINGDLL)
575 // NT defines APIENTRY, 3.x not
576 #if !defined(WXAPIENTRY)
577 #define WXAPIENTRY WXFAR wxSTDCALL
581 #include "wx/msw/winundef.h"
583 #define IMPLEMENT_WXWIN_MAIN \
584 extern "C" int WXAPIENTRY WinMain(HINSTANCE hInstance,\
585 HINSTANCE hPrevInstance,\
586 LPSTR m_lpCmdLine, int nCmdShow)\
588 return wxEntry((WXHINSTANCE) hInstance,\
589 (WXHINSTANCE) hPrevInstance,\
590 m_lpCmdLine, nCmdShow);\
593 #define IMPLEMENT_WXWIN_MAIN
596 #ifdef __WXUNIVERSAL__
597 #include "wx/univ/theme.h"
599 #define IMPLEMENT_WX_THEME_SUPPORT \
600 WX_USE_THEME(win32); \
603 #define IMPLEMENT_WX_THEME_SUPPORT
606 // Use this macro if you want to define your own main() or WinMain() function
607 // and call wxEntry() from there.
608 #define IMPLEMENT_APP_NO_MAIN(appname) \
609 wxApp *wxCreateApp() \
611 wxApp::CheckBuildOptions(wxBuildOptions()); \
612 return new appname; \
614 wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
615 appname& wxGetApp() { return *(appname *)wxTheApp; }
617 // Same as IMPLEMENT_APP() normally but doesn't include themes support in
618 // wxUniversal builds
619 #define IMPLEMENT_APP_NO_THEMES(appname) \
620 IMPLEMENT_APP_NO_MAIN(appname) \
623 // Use this macro exactly once, the argument is the name of the wxApp-derived
624 // class which is the class of your application.
625 #define IMPLEMENT_APP(appname) \
626 IMPLEMENT_APP_NO_THEMES(appname) \
627 IMPLEMENT_WX_THEME_SUPPORT
629 // this macro can be used multiple times and just allows you to use wxGetApp()
631 #define DECLARE_APP(appname) extern appname& wxGetApp();