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 and Markus Holzem
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_APP_H_BASE_
14 #define _WX_APP_H_BASE_
17 #pragma interface "appbase.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 #if (defined(__WXMSW__) && !defined(__WXMICROWIN__)) || defined (__WXPM__)
25 class WXDLLEXPORT wxApp
;
26 typedef wxApp
* (*wxAppInitializerFunction
)();
28 // returning wxApp* won't work with gcc
29 #include "wx/object.h"
31 typedef wxObject
* (*wxAppInitializerFunction
)();
34 class WXDLLEXPORT wxCmdLineParser
;
36 // ----------------------------------------------------------------------------
37 // headers we have to include here
38 // ----------------------------------------------------------------------------
40 #include "wx/event.h" // for the base class
43 #include "wx/window.h" // for wxTopLevelWindows
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 static const int wxPRINT_WINDOWS
= 1;
55 static const int wxPRINT_POSTSCRIPT
= 2;
57 // ----------------------------------------------------------------------------
58 // the common part of wxApp implementations for all platforms
59 // ----------------------------------------------------------------------------
61 class WXDLLEXPORT wxAppBase
: public wxEvtHandler
66 // the virtual functions which may/must be overridden in the derived class
67 // -----------------------------------------------------------------------
69 // called during the program initialization, returning FALSE from here
70 // prevents the program from continuing - it's a good place to create
71 // the top level program window and return TRUE.
73 // Override: always in GUI application, rarely in console ones.
74 virtual bool OnInit();
77 // a platform-dependent version of OnInit(): the code here is likely to
78 // depend on the toolkit. default version does nothing.
81 virtual bool OnInitGui();
84 // called to start program execution - the default version just enters
85 // the main GUI loop in which events are received and processed until
86 // the last window is not deleted (if GetExitOnFrameDelete) or
87 // ExitMainLoop() is called. In console mode programs, the execution
88 // of the program really starts here
90 // Override: rarely in GUI applications, always in console ones.
92 virtual int OnRun() { return MainLoop(); };
94 virtual int OnRun() = 0;
97 // called after the main loop termination. This is a good place for
98 // cleaning up (it may be too late in dtor) and is also useful if you
99 // want to return some non-default exit code - this is just the return
100 // value of this method.
103 virtual int OnExit();
105 // called when a fatal exception occurs, this function should take care
106 // not to do anything which might provoke a nested exception! It may be
107 // overridden if you wish to react somehow in non-default way (core
108 // dump under Unix, application crash under Windows) to fatal program
109 // errors, however extreme care should be taken if you don't want this
110 // function to crash.
113 virtual void OnFatalException() { }
115 // the worker functions - usually not used directly by the user code
116 // -----------------------------------------------------------------
119 // execute the main GUI loop, the function returns when the loop ends
120 virtual int MainLoop() = 0;
122 // exit the main GUI loop during the next iteration (i.e. it does not
123 // stop the program immediately!)
124 virtual void ExitMainLoop() = 0;
126 // returns TRUE if the program is initialized
127 virtual bool Initialized() = 0;
129 // returns TRUE if there are unprocessed events in the event queue
130 virtual bool Pending() = 0;
132 // process the first event in the event queue (blocks until an event
133 // apperas if there are none currently)
134 virtual void Dispatch() = 0;
136 // process all currently pending events right now
138 // it is an error to call Yield() recursively unless the value of
139 // onlyIfNeeded is TRUE
141 // WARNING: this function is dangerous as it can lead to unexpected
142 // reentrancies (i.e. when called from an event handler it
143 // may result in calling the same event handler again), use
144 // with _extreme_ care or, better, don't use at all!
145 virtual bool Yield(bool onlyIfNeeded
= FALSE
) = 0;
148 // application info: name, description, vendor
149 // -------------------------------------------
151 // NB: all these should be set by the application itself, there are no
152 // reasonable default except for the application name which is taken to
155 // set/get the application name
156 wxString
GetAppName() const
163 void SetAppName(const wxString
& name
) { m_appName
= name
; }
165 // set/get the app class name
166 wxString
GetClassName() const { return m_className
; }
167 void SetClassName(const wxString
& name
) { m_className
= name
; }
169 // set/get the vendor name
170 const wxString
& GetVendorName() const { return m_vendorName
; }
171 void SetVendorName(const wxString
& name
) { m_vendorName
= name
; }
174 // top level window functions
175 // --------------------------
177 // return TRUE if our app has focus
178 virtual bool IsActive() const { return m_isActive
; }
180 // set the "main" top level window
181 void SetTopWindow(wxWindow
*win
) { m_topWindow
= win
; }
183 // return the "main" top level window (if it hadn't been set previously
184 // with SetTopWindow(), will return just some top level window and, if
185 // there are none, will return NULL)
186 virtual wxWindow
*GetTopWindow() const
190 else if (wxTopLevelWindows
.GetCount() > 0)
191 return wxTopLevelWindows
.GetFirst()->GetData();
193 return (wxWindow
*)NULL
;
196 // control the exit behaviour: by default, the program will exit the
197 // main loop (and so, usually, terminate) when the last top-level
198 // program window is deleted. Beware that if you disabel this (with
199 // SetExitOnFrameDelete(FALSE)), you'll have to call ExitMainLoop()
200 // explicitly from somewhere.
201 void SetExitOnFrameDelete(bool flag
) { m_exitOnFrameDelete
= flag
; }
202 bool GetExitOnFrameDelete() const { return m_exitOnFrameDelete
; }
206 // cmd line parsing stuff
207 // ----------------------
209 // all of these methods may be overridden in the derived class to
210 // customize the command line parsing (by default only a few standard
211 // options are handled)
213 // you also need to call wxApp::OnInit() from YourApp::OnInit() for all
216 #if wxUSE_CMDLINE_PARSER
217 // this one is called from OnInit() to add all supported options
218 // to the given parser
219 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
221 // called after successfully parsing the command line, return TRUE
222 // to continue and FALSE to exit
223 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
225 // called if "--help" option was specified, return TRUE to continue
227 virtual bool OnCmdLineHelp(wxCmdLineParser
& parser
);
229 // called if incorrect command line options were given, return
230 // FALSE to abort and TRUE to continue
231 virtual bool OnCmdLineError(wxCmdLineParser
& parser
);
232 #endif // wxUSE_CMDLINE_PARSER
234 // miscellaneous customization functions
235 // -------------------------------------
238 // override this function to create default log target of arbitrary
239 // user-defined class (default implementation creates a wxLogGui
240 // object) - this log object is used by default by all wxLogXXX()
242 virtual wxLog
*CreateLogTarget()
243 #if wxUSE_GUI && wxUSE_LOGGUI && !defined(__WXMICROWIN__)
244 { return new wxLogGui
; }
246 { return new wxLogStderr
; }
251 // get the standard icon used by wxWin dialogs - this allows the user
252 // to customize the standard dialogs. The 'which' parameter is one of
254 virtual wxIcon
GetStdIcon(int which
) const = 0;
256 // VZ: what does this do exactly?
257 void SetWantDebugOutput( bool flag
) { m_wantDebugOutput
= flag
; }
258 bool GetWantDebugOutput() const { return m_wantDebugOutput
; }
260 // set use of best visual flag (see below)
261 void SetUseBestVisual( bool flag
) { m_useBestVisual
= flag
; }
262 bool GetUseBestVisual() const { return m_useBestVisual
; }
264 // set/get printing mode: see wxPRINT_XXX constants.
266 // default behaviour is the normal one for Unix: always use PostScript
268 virtual void SetPrintMode(int WXUNUSED(mode
)) { }
269 int GetPrintMode() const { return wxPRINT_POSTSCRIPT
; }
271 // called by toolkit-specific code to set the app status: active (we have
272 // focus) or not and also the last window which had focus before we were
274 virtual void SetActive(bool isActive
, wxWindow
*lastFocus
);
280 // this function is called when an assert failure occurs, the base class
281 // version does the normal processing (i.e. shows the usual assert failure
284 virtual void OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
);
285 #endif // __WXDEBUG__
287 // implementation only from now on
288 // -------------------------------
290 // helpers for dynamic wxApp construction
291 static void SetInitializerFunction(wxAppInitializerFunction fn
)
292 { m_appInitFn
= fn
; }
293 static wxAppInitializerFunction
GetInitializerFunction()
294 { return m_appInitFn
; }
296 // needed to avoid link errors
298 virtual ~wxAppBase() { }
301 // process all events in the wxPendingEvents list
302 virtual void ProcessPendingEvents();
304 // access to the command line arguments
309 // function used for dynamic wxApp creation
310 static wxAppInitializerFunction m_appInitFn
;
312 // application info (must be set from the user code)
313 wxString m_vendorName
, // vendor name (ACME Inc)
314 m_appName
, // app name
315 m_className
; // class name
317 // TRUE if the application wants to get debug output
318 bool m_wantDebugOutput
;
321 // the main top level window - may be NULL
322 wxWindow
*m_topWindow
;
324 // if TRUE, exit the main loop when the last top level window is deleted
325 bool m_exitOnFrameDelete
;
327 // TRUE if the apps whats to use the best visual on systems where
328 // more than one are available (Sun, SGI, XFree86 4.0 ?)
329 bool m_useBestVisual
;
331 // does any of our windows has focus?
336 // ----------------------------------------------------------------------------
337 // now include the declaration of the real class
338 // ----------------------------------------------------------------------------
341 #if defined(__WXMSW__)
342 #include "wx/msw/app.h"
343 #elif defined(__WXMOTIF__)
344 #include "wx/motif/app.h"
345 #elif defined(__WXMGL__)
346 #include "wx/mgl/app.h"
347 #elif defined(__WXGTK__)
348 #include "wx/gtk/app.h"
349 #elif defined(__WXMAC__)
350 #include "wx/mac/app.h"
351 #elif defined(__WXPM__)
352 #include "wx/os2/app.h"
353 #elif defined(__WXSTUBS__)
354 #include "wx/stubs/app.h"
357 // can't use typedef because wxApp forward declared as a class
358 class WXDLLEXPORT wxApp
: public wxAppBase
363 // ----------------------------------------------------------------------------
365 // ----------------------------------------------------------------------------
367 // the one and only application object - use of wxTheApp in application code
368 // is discouraged, consider using DECLARE_APP() after which you may call
369 // wxGetApp() which will return the object of the correct type (i.e. MyApp and
371 WXDLLEXPORT_DATA(extern wxApp
*) wxTheApp
;
373 // ----------------------------------------------------------------------------
375 // ----------------------------------------------------------------------------
377 // event loop related functions only work in GUI programs
378 // ------------------------------------------------------
380 // Force an exit from main loop
381 extern void WXDLLEXPORT
wxExit();
383 // Yield to other apps/messages
384 extern bool WXDLLEXPORT
wxYield();
386 // Yield to other apps/messages
387 extern void WXDLLEXPORT
wxWakeUpIdle();
389 // Post a message to the given eventhandler which will be processed during the
390 // next event loop iteration
391 inline void wxPostEvent(wxEvtHandler
*dest
, wxEvent
& event
)
393 wxCHECK_RET( dest
, wxT("need an object to post event to in wxPostEvent") );
396 dest
->AddPendingEvent(event
);
398 dest
->ProcessEvent(event
);
402 // console applications may avoid using DECLARE_APP and IMPLEMENT_APP macros
403 // and call these functions instead at the program startup and termination
404 // -------------------------------------------------------------------------
408 // initialize the library (may be called as many times as needed, but each
409 // call to wxInitialize() must be matched by wxUninitialize())
410 extern bool WXDLLEXPORT
wxInitialize();
412 // clean up - the library can't be used any more after the last call to
414 extern void WXDLLEXPORT
wxUninitialize();
416 // create an object of this class on stack to initialize/cleanup thel ibrary
418 class WXDLLEXPORT wxInitializer
421 // initialize the library
422 wxInitializer() { m_ok
= wxInitialize(); }
424 // has the initialization been successful? (explicit test)
425 bool IsOk() const { return m_ok
; }
427 // has the initialization been successful? (implicit test)
428 operator bool() const { return m_ok
; }
430 // dtor only does clean up if we initialized the library properly
431 ~wxInitializer() { if ( m_ok
) wxUninitialize(); }
439 // ----------------------------------------------------------------------------
440 // macros for dynamic creation of the application object
441 // ----------------------------------------------------------------------------
443 // Having a global instance of this class allows wxApp to be aware of the app
444 // creator function. wxApp can then call this function to create a new app
445 // object. Convoluted, but necessary.
447 class WXDLLEXPORT wxAppInitializer
450 wxAppInitializer(wxAppInitializerFunction fn
)
451 { wxApp::SetInitializerFunction(fn
); }
454 // Here's a macro you can use if your compiler really, really wants main() to
455 // be in your main program (e.g. hello.cpp). Now IMPLEMENT_APP should add this
458 #if !wxUSE_GUI || defined(__WXMOTIF__) || defined(__WXGTK__) || defined(__WXPM__) || defined(__WXMGL__)
459 #define IMPLEMENT_WXWIN_MAIN \
460 extern int wxEntry( int argc, char *argv[] ); \
461 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
462 #elif defined(__WXMAC__) && defined(__UNIX__)
463 // wxMac seems to have a specific wxEntry prototype
464 #define IMPLEMENT_WXWIN_MAIN \
465 extern int wxEntry( int argc, char *argv[], bool enterLoop = 1 ); \
466 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
467 #elif defined(__WXMSW__) && defined(WXUSINGDLL)
468 // NT defines APIENTRY, 3.x not
469 #if !defined(WXAPIENTRY)
470 #define WXAPIENTRY WXFAR wxSTDCALL
474 #include "wx/msw/winundef.h"
476 #define IMPLEMENT_WXWIN_MAIN \
477 extern "C" int WXAPIENTRY WinMain(HINSTANCE hInstance,\
478 HINSTANCE hPrevInstance,\
479 LPSTR m_lpCmdLine, int nCmdShow)\
481 return wxEntry((WXHINSTANCE) hInstance,\
482 (WXHINSTANCE) hPrevInstance,\
483 m_lpCmdLine, nCmdShow);\
486 #define IMPLEMENT_WXWIN_MAIN
489 #ifdef __WXUNIVERSAL__
490 #include "wx/univ/theme.h"
492 #define IMPLEMENT_WX_THEME_SUPPORT \
493 WX_USE_THEME(win32); \
496 #define IMPLEMENT_WX_THEME_SUPPORT
499 // Use this macro if you want to define your own main() or WinMain() function
500 // and call wxEntry() from there.
501 #define IMPLEMENT_APP_NO_MAIN(appname) \
502 wxApp *wxCreateApp() { return new appname; } \
503 wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
504 appname& wxGetApp() { return *(appname *)wxTheApp; }
506 // Same as IMPLEMENT_APP() normally but doesn't include themes support in
507 // wxUniversal builds
508 #define IMPLEMENT_APP_NO_THEMES(appname) \
509 IMPLEMENT_APP_NO_MAIN(appname) \
512 // Use this macro exactly once, the argument is the name of the wxApp-derived
513 // class which is the class of your application.
514 #define IMPLEMENT_APP(appname) \
515 IMPLEMENT_APP_NO_THEMES(appname) \
516 IMPLEMENT_WX_THEME_SUPPORT
518 // this macro can be used multiple times and just allows you to use wxGetApp()
520 #define DECLARE_APP(appname) extern appname& wxGetApp();