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 virtual ~wxAppBase() { }
72 // called during the program initialization, returning FALSE from here
73 // prevents the program from continuing - it's a good place to create
74 // the top level program window and return TRUE.
76 // Override: always in GUI application, rarely in console ones.
77 virtual bool OnInit();
80 // a platform-dependent version of OnInit(): the code here is likely to
81 // depend on the toolkit. default version does nothing.
84 virtual bool OnInitGui();
87 // called to start program execution - the default version just enters
88 // the main GUI loop in which events are received and processed until
89 // the last window is not deleted (if GetExitOnFrameDelete) or
90 // ExitMainLoop() is called. In console mode programs, the execution
91 // of the program really starts here
93 // Override: rarely in GUI applications, always in console ones.
95 virtual int OnRun() { return MainLoop(); };
97 virtual int OnRun() = 0;
100 // called after the main loop termination. This is a good place for
101 // cleaning up (it may be too late in dtor) and is also useful if you
102 // want to return some non-default exit code - this is just the return
103 // value of this method.
106 virtual int OnExit();
108 // called when a fatal exception occurs, this function should take care
109 // not to do anything which might provoke a nested exception! It may be
110 // overridden if you wish to react somehow in non-default way (core
111 // dump under Unix, application crash under Windows) to fatal program
112 // errors, however extreme care should be taken if you don't want this
113 // function to crash.
116 virtual void OnFatalException() { }
118 // the worker functions - usually not used directly by the user code
119 // -----------------------------------------------------------------
122 // execute the main GUI loop, the function returns when the loop ends
123 virtual int MainLoop() = 0;
125 // exit the main GUI loop during the next iteration (i.e. it does not
126 // stop the program immediately!)
127 virtual void ExitMainLoop() = 0;
129 // returns TRUE if the program is initialized
130 virtual bool Initialized() = 0;
132 // returns TRUE if there are unprocessed events in the event queue
133 virtual bool Pending() = 0;
135 // process the first event in the event queue (blocks until an event
136 // apperas if there are none currently)
137 virtual void Dispatch() = 0;
140 // application info: name, description, vendor
141 // -------------------------------------------
143 // NB: all these should be set by the application itself, there are no
144 // reasonable default except for the application name which is taken to
147 // set/get the application name
148 wxString
GetAppName() const
155 void SetAppName(const wxString
& name
) { m_appName
= name
; }
157 // set/get the app class name
158 wxString
GetClassName() const { return m_className
; }
159 void SetClassName(const wxString
& name
) { m_className
= name
; }
161 // set/get the vendor name
162 const wxString
& GetVendorName() const { return m_vendorName
; }
163 void SetVendorName(const wxString
& name
) { m_vendorName
= name
; }
166 // top level window functions
167 // --------------------------
169 // return TRUE if our app has focus
170 virtual bool IsActive() const { return m_isActive
; }
172 // set the "main" top level window
173 void SetTopWindow(wxWindow
*win
) { m_topWindow
= win
; }
175 // return the "main" top level window (if it hadn't been set previously
176 // with SetTopWindow(), will return just some top level window and, if
177 // there are none, will return NULL)
178 virtual wxWindow
*GetTopWindow() const
182 else if (wxTopLevelWindows
.GetCount() > 0)
183 return wxTopLevelWindows
.GetFirst()->GetData();
185 return (wxWindow
*)NULL
;
188 // control the exit behaviour: by default, the program will exit the
189 // main loop (and so, usually, terminate) when the last top-level
190 // program window is deleted. Beware that if you disabel this (with
191 // SetExitOnFrameDelete(FALSE)), you'll have to call ExitMainLoop()
192 // explicitly from somewhere.
193 void SetExitOnFrameDelete(bool flag
) { m_exitOnFrameDelete
= flag
; }
194 bool GetExitOnFrameDelete() const { return m_exitOnFrameDelete
; }
198 // cmd line parsing stuff
199 // ----------------------
201 // all of these methods may be overridden in the derived class to
202 // customize the command line parsing (by default only a few standard
203 // options are handled)
205 // you also need to call wxApp::OnInit() from YourApp::OnInit() for all
208 #if wxUSE_CMDLINE_PARSER
209 // this one is called from OnInit() to add all supported options
210 // to the given parser
211 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
213 // called after successfully parsing the command line, return TRUE
214 // to continue and FALSE to exit
215 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
217 // called if "--help" option was specified, return TRUE to continue
219 virtual bool OnCmdLineHelp(wxCmdLineParser
& parser
);
221 // called if incorrect command line options were given, return
222 // FALSE to abort and TRUE to continue
223 virtual bool OnCmdLineError(wxCmdLineParser
& parser
);
224 #endif // wxUSE_CMDLINE_PARSER
226 // miscellaneous customization functions
227 // -------------------------------------
230 // override this function to create default log target of arbitrary
231 // user-defined class (default implementation creates a wxLogGui
232 // object) - this log object is used by default by all wxLogXXX()
234 virtual wxLog
*CreateLogTarget()
235 #if wxUSE_GUI && wxUSE_LOGGUI && !defined(__WXMICROWIN__)
236 { return new wxLogGui
; }
238 { return new wxLogStderr
; }
243 // get the standard icon used by wxWin dialogs - this allows the user
244 // to customize the standard dialogs. The 'which' parameter is one of
246 virtual wxIcon
GetStdIcon(int which
) const = 0;
248 // VZ: what does this do exactly?
249 void SetWantDebugOutput( bool flag
) { m_wantDebugOutput
= flag
; }
250 bool GetWantDebugOutput() const { return m_wantDebugOutput
; }
252 // set use of best visual flag (see below)
253 void SetUseBestVisual( bool flag
) { m_useBestVisual
= flag
; }
254 bool GetUseBestVisual() const { return m_useBestVisual
; }
256 // set/get printing mode: see wxPRINT_XXX constants.
258 // default behaviour is the normal one for Unix: always use PostScript
260 virtual void SetPrintMode(int WXUNUSED(mode
)) { }
261 int GetPrintMode() const { return wxPRINT_POSTSCRIPT
; }
263 // called by toolkit-specific code to set the app status: active (we have
264 // focus) or not and also the last window which had focus before we were
266 virtual void SetActive(bool isActive
, wxWindow
*lastFocus
);
272 // this function is called when an assert failure occurs, the base class
273 // version does the normal processing (i.e. shows the usual assert failure
276 virtual void OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
);
277 #endif // __WXDEBUG__
279 // implementation only from now on
280 // -------------------------------
282 // helpers for dynamic wxApp construction
283 static void SetInitializerFunction(wxAppInitializerFunction fn
)
284 { m_appInitFn
= fn
; }
285 static wxAppInitializerFunction
GetInitializerFunction()
286 { return m_appInitFn
; }
288 // process all events in the wxPendingEvents list
289 virtual void ProcessPendingEvents();
291 // access to the command line arguments
296 // function used for dynamic wxApp creation
297 static wxAppInitializerFunction m_appInitFn
;
299 // application info (must be set from the user code)
300 wxString m_vendorName
, // vendor name (ACME Inc)
301 m_appName
, // app name
302 m_className
; // class name
304 // TRUE if the application wants to get debug output
305 bool m_wantDebugOutput
;
308 // the main top level window - may be NULL
309 wxWindow
*m_topWindow
;
311 // if TRUE, exit the main loop when the last top level window is deleted
312 bool m_exitOnFrameDelete
;
314 // TRUE if the apps whats to use the best visual on systems where
315 // more than one are available (Sun, SGI, XFree86 4.0 ?)
316 bool m_useBestVisual
;
318 // does any of our windows has focus?
323 // ----------------------------------------------------------------------------
324 // now include the declaration of the real class
325 // ----------------------------------------------------------------------------
328 #if defined(__WXMSW__)
329 #include "wx/msw/app.h"
330 #elif defined(__WXMOTIF__)
331 #include "wx/motif/app.h"
332 #elif defined(__WXMGL__)
333 #include "wx/mgl/app.h"
334 #elif defined(__WXQT__)
335 #include "wx/qt/app.h"
336 #elif defined(__WXGTK__)
337 #include "wx/gtk/app.h"
338 #elif defined(__WXMAC__)
339 #include "wx/mac/app.h"
340 #elif defined(__WXPM__)
341 #include "wx/os2/app.h"
342 #elif defined(__WXSTUBS__)
343 #include "wx/stubs/app.h"
346 // can't use typedef because wxApp forward declared as a class
347 class WXDLLEXPORT wxApp
: public wxAppBase
352 // ----------------------------------------------------------------------------
354 // ----------------------------------------------------------------------------
356 // the one and only application object - use of wxTheApp in application code
357 // is discouraged, consider using DECLARE_APP() after which you may call
358 // wxGetApp() which will return the object of the correct type (i.e. MyApp and
360 WXDLLEXPORT_DATA(extern wxApp
*) wxTheApp
;
362 // ----------------------------------------------------------------------------
364 // ----------------------------------------------------------------------------
366 // event loop related functions only work in GUI programs
367 // ------------------------------------------------------
369 // Force an exit from main loop
370 extern void WXDLLEXPORT
wxExit();
372 // Yield to other apps/messages
373 extern bool WXDLLEXPORT
wxYield();
375 // Yield to other apps/messages
376 extern void WXDLLEXPORT
wxWakeUpIdle();
378 // Post a message to the given eventhandler which will be processed during the
379 // next event loop iteration
380 inline void wxPostEvent(wxEvtHandler
*dest
, wxEvent
& event
)
382 wxCHECK_RET( dest
, wxT("need an object to post event to in wxPostEvent") );
385 dest
->AddPendingEvent(event
);
387 dest
->ProcessEvent(event
);
391 // console applications may avoid using DECLARE_APP and IMPLEMENT_APP macros
392 // and call these functions instead at the program startup and termination
393 // -------------------------------------------------------------------------
397 // initialize the library (may be called as many times as needed, but each
398 // call to wxInitialize() must be matched by wxUninitialize())
399 extern bool WXDLLEXPORT
wxInitialize();
401 // clean up - the library can't be used any more after the last call to
403 extern void WXDLLEXPORT
wxUninitialize();
405 // create an object of this class on stack to initialize/cleanup thel ibrary
407 class WXDLLEXPORT wxInitializer
410 // initialize the library
411 wxInitializer() { m_ok
= wxInitialize(); }
413 // has the initialization been successful? (explicit test)
414 bool IsOk() const { return m_ok
; }
416 // has the initialization been successful? (implicit test)
417 operator bool() const { return m_ok
; }
419 // dtor only does clean up if we initialized the library properly
420 ~wxInitializer() { if ( m_ok
) wxUninitialize(); }
428 // ----------------------------------------------------------------------------
429 // macros for dynamic creation of the application object
430 // ----------------------------------------------------------------------------
432 // Having a global instance of this class allows wxApp to be aware of the app
433 // creator function. wxApp can then call this function to create a new app
434 // object. Convoluted, but necessary.
436 class WXDLLEXPORT wxAppInitializer
439 wxAppInitializer(wxAppInitializerFunction fn
)
440 { wxApp::SetInitializerFunction(fn
); }
443 // Here's a macro you can use if your compiler really, really wants main() to
444 // be in your main program (e.g. hello.cpp). Now IMPLEMENT_APP should add this
447 #if !wxUSE_GUI || defined(__WXMOTIF__) || defined(__WXGTK__) || defined(__WXPM__) || defined(__WXMGL__)
448 #define IMPLEMENT_WXWIN_MAIN \
449 extern int wxEntry( int argc, char *argv[] ); \
450 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
451 #elif defined(__WXMAC__) && defined(__UNIX__)
452 // wxMac seems to have a specific wxEntry prototype
453 #define IMPLEMENT_WXWIN_MAIN \
454 extern int wxEntry( int argc, char *argv[], bool enterLoop = 1 ); \
455 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
456 #elif defined(__WXMSW__) && defined(WXUSINGDLL)
457 // NT defines APIENTRY, 3.x not
458 #if !defined(WXAPIENTRY)
459 #define WXAPIENTRY WXFAR wxSTDCALL
463 #include "wx/msw/winundef.h"
465 #define IMPLEMENT_WXWIN_MAIN \
466 extern "C" int WXAPIENTRY WinMain(HINSTANCE hInstance,\
467 HINSTANCE hPrevInstance,\
468 LPSTR m_lpCmdLine, int nCmdShow)\
470 return wxEntry((WXHINSTANCE) hInstance,\
471 (WXHINSTANCE) hPrevInstance,\
472 m_lpCmdLine, nCmdShow);\
475 #define IMPLEMENT_WXWIN_MAIN
478 #ifdef __WXUNIVERSAL__
479 #include "wx/univ/theme.h"
481 #define IMPLEMENT_WX_THEME_SUPPORT \
482 WX_USE_THEME(win32); \
485 #define IMPLEMENT_WX_THEME_SUPPORT
488 // Use this macro if you want to define your own main() or WinMain() function
489 // and call wxEntry() from there.
490 #define IMPLEMENT_APP_NO_MAIN(appname) \
491 wxApp *wxCreateApp() { return new appname; } \
492 wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
493 appname& wxGetApp() { return *(appname *)wxTheApp; }
495 // Same as IMPLEMENT_APP() normally but doesn't include themes support in
496 // wxUniversal builds
497 #define IMPLEMENT_APP_NO_THEMES(appname) \
498 IMPLEMENT_APP_NO_MAIN(appname) \
501 // Use this macro exactly once, the argument is the name of the wxApp-derived
502 // class which is the class of your application.
503 #define IMPLEMENT_APP(appname) \
504 IMPLEMENT_APP_NO_THEMES(appname) \
505 IMPLEMENT_WX_THEME_SUPPORT
507 // this macro can be used multiple times and just allows you to use wxGetApp()
509 #define DECLARE_APP(appname) extern appname& wxGetApp();