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 #if defined(__GNUG__) && !defined(__APPLE__)
17 #pragma interface "appbase.h"
20 // ----------------------------------------------------------------------------
21 // headers we have to include here
22 // ----------------------------------------------------------------------------
24 #include "wx/event.h" // for the base class
27 #include "wx/window.h" // for wxTopLevelWindows
32 class WXDLLEXPORT wxApp
;
33 class WXDLLEXPORT wxAppTraits
;
34 class WXDLLEXPORT wxCmdLineParser
;
35 class WXDLLEXPORT wxLog
;
36 class WXDLLEXPORT wxMessageOutput
;
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // the type of the function used to create a wxApp object on program start up
43 typedef wxApp
* (*wxAppInitializerFunction
)();
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
52 wxPRINT_POSTSCRIPT
= 2
55 // ----------------------------------------------------------------------------
56 // support for framebuffer ports
57 // ----------------------------------------------------------------------------
60 // VS: Fullscreen/framebuffer application needs to choose display mode prior
61 // to wxWindows initialization. This class holds information about display
62 // mode. It is used by wxApp::Set/GetDisplayMode.
63 class WXDLLEXPORT wxDisplayModeInfo
66 wxDisplayModeInfo() : m_ok(FALSE
) {}
67 wxDisplayModeInfo(unsigned width
, unsigned height
, unsigned depth
)
68 : m_width(width
), m_height(height
), m_depth(depth
), m_ok(TRUE
) {}
70 unsigned GetWidth() const { return m_width
; }
71 unsigned GetHeight() const { return m_height
; }
72 unsigned GetDepth() const { return m_depth
; }
73 bool IsOk() const { return m_ok
; }
76 unsigned m_width
, m_height
, m_depth
;
81 // ----------------------------------------------------------------------------
82 // wxAppConsole: wxApp for non-GUI applications
83 // ----------------------------------------------------------------------------
85 class WXDLLEXPORT wxAppConsole
: public wxEvtHandler
90 virtual ~wxAppConsole();
93 // the virtual functions which may/must be overridden in the derived class
94 // -----------------------------------------------------------------------
96 // Called before OnRun(), this is a good place to do initialization -- if
97 // anything fails, return false from here to prevent the program from
98 // continuing. The command line is normally parsed here, call the base
99 // class OnInit() to do it.
100 virtual bool OnInit();
102 // This is the replacement for the normal main(): all program work should
103 // be done here. When OnRun() returns, the programs starts shutting down.
104 virtual int OnRun() = 0;
106 // This is only called if OnInit() returned true so it's a good place to do
107 // any cleanup matching the initializations done there.
108 virtual int OnExit();
110 // Called when a fatal exception occurs, this function should take care not
111 // to do anything which might provoke a nested exception! It may be
112 // overridden if you wish to react somehow in non-default way (core dump
113 // under Unix, application crash under Windows) to fatal program errors,
114 // however extreme care should be taken if you don't want this function to
116 virtual void OnFatalException() { }
118 // Called from wxExit() function, should terminate the application a.s.a.p.
122 // application info: name, description, vendor
123 // -------------------------------------------
125 // NB: all these should be set by the application itself, there are no
126 // reasonable default except for the application name which is taken to
129 // set/get the application name
130 wxString
GetAppName() const
132 return m_appName
.empty() ? m_className
: m_appName
;
134 void SetAppName(const wxString
& name
) { m_appName
= name
; }
136 // set/get the app class name
137 wxString
GetClassName() const { return m_className
; }
138 void SetClassName(const wxString
& name
) { m_className
= name
; }
140 // set/get the vendor name
141 const wxString
& GetVendorName() const { return m_vendorName
; }
142 void SetVendorName(const wxString
& name
) { m_vendorName
= name
; }
145 // cmd line parsing stuff
146 // ----------------------
148 // all of these methods may be overridden in the derived class to
149 // customize the command line parsing (by default only a few standard
150 // options are handled)
152 // you also need to call wxApp::OnInit() from YourApp::OnInit() for all
155 #if wxUSE_CMDLINE_PARSER
156 // this one is called from OnInit() to add all supported options
157 // to the given parser
158 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
160 // called after successfully parsing the command line, return TRUE
161 // to continue and FALSE to exit
162 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
164 // called if "--help" option was specified, return TRUE to continue
166 virtual bool OnCmdLineHelp(wxCmdLineParser
& parser
);
168 // called if incorrect command line options were given, return
169 // FALSE to abort and TRUE to continue
170 virtual bool OnCmdLineError(wxCmdLineParser
& parser
);
171 #endif // wxUSE_CMDLINE_PARSER
174 // miscellaneous customization functions
175 // -------------------------------------
177 // create the app traits object to which we delegate for everything which
178 // either should be configurable by the user (then he can change the
179 // default behaviour simply by overriding CreateTraits() and returning his
180 // own traits object) or which is GUI/console dependent as then wxAppTraits
181 // allows us to abstract the differences behind the common façade
182 wxAppTraits
*GetTraits();
184 // the functions below shouldn't be used now that we have wxAppTraits
185 #if WXWIN_COMPATIBILITY_2_4
188 // override this function to create default log target of arbitrary
189 // user-defined class (default implementation creates a wxLogGui
190 // object) -- this log object is used by default by all wxLogXXX()
192 virtual wxLog
*CreateLogTarget();
195 // similar to CreateLogTarget() but for the global wxMessageOutput
197 virtual wxMessageOutput
*CreateMessageOutput();
199 #endif // WXWIN_COMPATIBILITY_2_4
202 // event processing functions
203 // --------------------------
205 // this method allows to filter all the events processed by the program, so
206 // you should try to return quickly from it to avoid slowing down the
207 // program to the crawl
209 // return value should be -1 to continue with the normal event processing,
210 // or TRUE or FALSE to stop further processing and pretend that the event
211 // had been already processed or won't be processed at all, respectively
212 virtual int FilterEvent(wxEvent
& event
);
214 // process all events in the wxPendingEvents list -- it is necessary to
215 // call this function to process posted events. This happens during each
216 // event loop iteration in GUI mode but if there is no main loop, it may be
217 // also called directly.
218 virtual void ProcessPendingEvents();
220 // doesn't do anything in this class, just a hook for GUI wxApp
221 virtual bool Yield(bool WXUNUSED(onlyIfNeeded
) = false) { return true; }
223 // make sure that idle events are sent again
224 virtual void WakeUpIdle() { }
230 // this function is called when an assert failure occurs, the base class
231 // version does the normal processing (i.e. shows the usual assert failure
234 // the arguments are the place where the assert occured, the text of the
235 // assert itself and the user-specified message
237 virtual void OnAssert(const wxChar
*file
,
241 #endif // __WXDEBUG__
243 // check that the wxBuildOptions object (constructed in the application
244 // itself, usually the one from IMPLEMENT_APP() macro) matches the build
245 // options of the library and abort if it doesn't
246 static bool CheckBuildOptions(const wxBuildOptions
& buildOptions
);
249 // implementation only from now on
250 // -------------------------------
252 // helpers for dynamic wxApp construction
253 static void SetInitializerFunction(wxAppInitializerFunction fn
)
254 { ms_appInitFn
= fn
; }
255 static wxAppInitializerFunction
GetInitializerFunction()
256 { return ms_appInitFn
; }
259 // command line arguments (public for backwards compatibility)
264 // the function which creates the traits object when GetTraits() needs it
265 // for the first time
266 virtual wxAppTraits
*CreateTraits();
269 // function used for dynamic wxApp creation
270 static wxAppInitializerFunction ms_appInitFn
;
272 // application info (must be set from the user code)
273 wxString m_vendorName
, // vendor name (ACME Inc)
274 m_appName
, // app name
275 m_className
; // class name
277 // the class defining the application behaviour, NULL initially and created
278 // by GetTraits() when first needed
279 wxAppTraits
*m_traits
;
282 // the application object is a singleton anyhow, there is no sense in
284 DECLARE_NO_COPY_CLASS(wxAppConsole
)
287 // ----------------------------------------------------------------------------
288 // wxAppBase: the common part of wxApp implementations for all platforms
289 // ----------------------------------------------------------------------------
293 class WXDLLEXPORT wxAppBase
: public wxAppConsole
297 virtual ~wxAppBase();
299 // the virtual functions which may/must be overridden in the derived class
300 // -----------------------------------------------------------------------
302 // a platform-dependent version of OnInit(): the code here is likely to
303 // depend on the toolkit. default version does nothing.
306 virtual bool OnInitGui();
308 // called to start program execution - the default version just enters
309 // the main GUI loop in which events are received and processed until
310 // the last window is not deleted (if GetExitOnFrameDelete) or
311 // ExitMainLoop() is called. In console mode programs, the execution
312 // of the program really starts here
314 // Override: rarely in GUI applications, always in console ones.
317 // exit the main loop thus terminating the application
321 // the worker functions - usually not used directly by the user code
322 // -----------------------------------------------------------------
324 // execute the main GUI loop, the function returns when the loop ends
325 virtual int MainLoop() = 0;
327 // exit the main GUI loop during the next iteration (i.e. it does not
328 // stop the program immediately!)
329 virtual void ExitMainLoop() = 0;
331 // returns TRUE if the program is initialized
332 virtual bool Initialized() = 0;
334 // returns TRUE if there are unprocessed events in the event queue
335 virtual bool Pending() = 0;
337 // process the first event in the event queue (blocks until an event
338 // apperas if there are none currently)
339 virtual void Dispatch() = 0;
341 // process all currently pending events right now
343 // it is an error to call Yield() recursively unless the value of
344 // onlyIfNeeded is TRUE
346 // WARNING: this function is dangerous as it can lead to unexpected
347 // reentrancies (i.e. when called from an event handler it
348 // may result in calling the same event handler again), use
349 // with _extreme_ care or, better, don't use at all!
350 virtual bool Yield(bool onlyIfNeeded
= FALSE
) = 0;
352 // this virtual function is called in the GUI mode when the application
353 // becomes idle and normally just sends wxIdleEvent to all interested
356 // it should return TRUE if more idle events are needed, FALSE if not
357 virtual bool ProcessIdle() = 0;
360 // top level window functions
361 // --------------------------
363 // return TRUE if our app has focus
364 virtual bool IsActive() const { return m_isActive
; }
366 // set the "main" top level window
367 void SetTopWindow(wxWindow
*win
) { m_topWindow
= win
; }
369 // return the "main" top level window (if it hadn't been set previously
370 // with SetTopWindow(), will return just some top level window and, if
371 // there are none, will return NULL)
372 virtual wxWindow
*GetTopWindow() const
376 else if (wxTopLevelWindows
.GetCount() > 0)
377 return wxTopLevelWindows
.GetFirst()->GetData();
379 return (wxWindow
*)NULL
;
382 // control the exit behaviour: by default, the program will exit the
383 // main loop (and so, usually, terminate) when the last top-level
384 // program window is deleted. Beware that if you disable this behaviour
385 // (with SetExitOnFrameDelete(FALSE)), you'll have to call
386 // ExitMainLoop() explicitly from somewhere.
387 void SetExitOnFrameDelete(bool flag
)
388 { m_exitOnFrameDelete
= flag
? Yes
: No
; }
389 bool GetExitOnFrameDelete() const
390 { return m_exitOnFrameDelete
== Yes
; }
393 // display mode, visual, printing mode, ...
394 // ------------------------------------------------------------------------
396 // Get display mode that is used use. This is only used in framebuffer
397 // wxWin ports (such as wxMGL).
398 virtual wxDisplayModeInfo
GetDisplayMode() const { return wxDisplayModeInfo(); }
399 // Set display mode to use. This is only used in framebuffer wxWin
400 // ports (such as wxMGL). This method should be called from
402 virtual bool SetDisplayMode(const wxDisplayModeInfo
& WXUNUSED(info
)) { return TRUE
; }
404 // set use of best visual flag (see below)
405 void SetUseBestVisual( bool flag
) { m_useBestVisual
= flag
; }
406 bool GetUseBestVisual() const { return m_useBestVisual
; }
408 // set/get printing mode: see wxPRINT_XXX constants.
410 // default behaviour is the normal one for Unix: always use PostScript
412 virtual void SetPrintMode(int WXUNUSED(mode
)) { }
413 int GetPrintMode() const { return wxPRINT_POSTSCRIPT
; }
416 // miscellaneous other stuff
417 // ------------------------------------------------------------------------
419 // called by toolkit-specific code to set the app status: active (we have
420 // focus) or not and also the last window which had focus before we were
422 virtual void SetActive(bool isActive
, wxWindow
*lastFocus
);
426 // override base class method to use GUI traits
427 virtual wxAppTraits
*CreateTraits();
430 // the main top level window (may be NULL)
431 wxWindow
*m_topWindow
;
433 // if Yes, exit the main loop when the last top level window is deleted, if
434 // No don't do it and if Later -- only do it once we reach our OnRun()
436 // the explanation for using this strange scheme is given in appcmn.cpp
442 } m_exitOnFrameDelete
;
444 // TRUE if the apps whats to use the best visual on systems where
445 // more than one are available (Sun, SGI, XFree86 4.0 ?)
446 bool m_useBestVisual
;
448 // does any of our windows has focus?
452 DECLARE_NO_COPY_CLASS(wxAppBase
)
457 // ----------------------------------------------------------------------------
458 // now include the declaration of the real class
459 // ----------------------------------------------------------------------------
462 #if defined(__WXMSW__)
463 #include "wx/msw/app.h"
464 #elif defined(__WXMOTIF__)
465 #include "wx/motif/app.h"
466 #elif defined(__WXMGL__)
467 #include "wx/mgl/app.h"
468 #elif defined(__WXGTK__)
469 #include "wx/gtk/app.h"
470 #elif defined(__WXX11__)
471 #include "wx/x11/app.h"
472 #elif defined(__WXMAC__)
473 #include "wx/mac/app.h"
474 #elif defined(__WXCOCOA__)
475 #include "wx/cocoa/app.h"
476 #elif defined(__WXPM__)
477 #include "wx/os2/app.h"
480 // can't use typedef because wxApp forward declared as a class
481 class WXDLLEXPORT wxApp
: public wxAppConsole
486 // ----------------------------------------------------------------------------
488 // ----------------------------------------------------------------------------
490 // the one and only application object - use of wxTheApp in application code
491 // is discouraged, consider using DECLARE_APP() after which you may call
492 // wxGetApp() which will return the object of the correct type (i.e. MyApp and
494 WXDLLEXPORT_DATA(extern wxApp
*) wxTheApp
;
496 // ----------------------------------------------------------------------------
498 // ----------------------------------------------------------------------------
500 // event loop related functions only work in GUI programs
501 // ------------------------------------------------------
503 // Force an exit from main loop
504 extern void WXDLLEXPORT
wxExit();
506 // Yield to other apps/messages
507 extern bool WXDLLEXPORT
wxYield();
509 // Yield to other apps/messages
510 extern void WXDLLEXPORT
wxWakeUpIdle();
513 // console applications may avoid using DECLARE_APP and IMPLEMENT_APP macros
514 // and call these functions instead at the program startup and termination
515 // -------------------------------------------------------------------------
519 // initialize the library (may be called as many times as needed, but each
520 // call to wxInitialize() must be matched by wxUninitialize())
521 extern bool WXDLLEXPORT
wxInitialize();
523 // clean up - the library can't be used any more after the last call to
525 extern void WXDLLEXPORT
wxUninitialize();
527 // create an object of this class on stack to initialize/cleanup thel ibrary
529 class WXDLLEXPORT wxInitializer
532 // initialize the library
533 wxInitializer() { m_ok
= wxInitialize(); }
535 // has the initialization been successful? (explicit test)
536 bool IsOk() const { return m_ok
; }
538 // has the initialization been successful? (implicit test)
539 operator bool() const { return m_ok
; }
541 // dtor only does clean up if we initialized the library properly
542 ~wxInitializer() { if ( m_ok
) wxUninitialize(); }
550 // ----------------------------------------------------------------------------
551 // macros for dynamic creation of the application object
552 // ----------------------------------------------------------------------------
554 // Having a global instance of this class allows wxApp to be aware of the app
555 // creator function. wxApp can then call this function to create a new app
556 // object. Convoluted, but necessary.
558 class WXDLLEXPORT wxAppInitializer
561 wxAppInitializer(wxAppInitializerFunction fn
)
562 { wxApp::SetInitializerFunction(fn
); }
565 // Here's a macro you can use if your compiler really, really wants main() to
566 // be in your main program (e.g. hello.cpp). Now IMPLEMENT_APP should add this
569 #if !wxUSE_GUI || defined(__WXMOTIF__) || defined(__WXGTK__) || defined(__WXPM__) || defined(__WXMGL__) || defined(__WXCOCOA__)
570 #define IMPLEMENT_WXWIN_MAIN \
571 extern int wxEntry( int argc, char **argv ); \
572 int main(int argc, char **argv) { return wxEntry(argc, argv); }
573 #elif defined(__WXMAC__)
574 // wxMac seems to have a specific wxEntry prototype
575 #define IMPLEMENT_WXWIN_MAIN \
576 extern int wxEntry( int argc, char **argv, bool enterLoop = TRUE ); \
577 int main(int argc, char **argv) { return wxEntry(argc, argv); }
578 #elif defined(__WXMSW__) && defined(WXUSINGDLL)
579 // NT defines APIENTRY, 3.x not
580 #if !defined(WXAPIENTRY)
581 #define WXAPIENTRY WXFAR wxSTDCALL
585 #include "wx/msw/winundef.h"
587 #define IMPLEMENT_WXWIN_MAIN \
588 extern "C" int WXAPIENTRY WinMain(HINSTANCE hInstance,\
589 HINSTANCE hPrevInstance,\
590 LPSTR m_lpCmdLine, int nCmdShow)\
592 return wxEntry((WXHINSTANCE) hInstance,\
593 (WXHINSTANCE) hPrevInstance,\
594 m_lpCmdLine, nCmdShow);\
597 #define IMPLEMENT_WXWIN_MAIN
600 #ifdef __WXUNIVERSAL__
601 #include "wx/univ/theme.h"
603 #define IMPLEMENT_WX_THEME_SUPPORT \
604 WX_USE_THEME(win32); \
607 #define IMPLEMENT_WX_THEME_SUPPORT
610 // Use this macro if you want to define your own main() or WinMain() function
611 // and call wxEntry() from there.
612 #define IMPLEMENT_APP_NO_MAIN(appname) \
613 wxApp *wxCreateApp() \
615 wxApp::CheckBuildOptions(wxBuildOptions()); \
616 return new appname; \
618 wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
619 appname& wxGetApp() { return *(appname *)wxTheApp; }
621 // Same as IMPLEMENT_APP() normally but doesn't include themes support in
622 // wxUniversal builds
623 #define IMPLEMENT_APP_NO_THEMES(appname) \
624 IMPLEMENT_APP_NO_MAIN(appname) \
627 // Use this macro exactly once, the argument is the name of the wxApp-derived
628 // class which is the class of your application.
629 #define IMPLEMENT_APP(appname) \
630 IMPLEMENT_APP_NO_THEMES(appname) \
631 IMPLEMENT_WX_THEME_SUPPORT
633 // this macro can be used multiple times and just allows you to use wxGetApp()
635 #define DECLARE_APP(appname) extern appname& wxGetApp();