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 (__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 // ----------------------------------------------------------------------------
35 // headers we have to include here
36 // ----------------------------------------------------------------------------
38 #include "wx/event.h" // for the base class
41 #include "wx/window.h" // for wxTopLevelWindows
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 static const int wxPRINT_WINDOWS
= 1;
53 static const int wxPRINT_POSTSCRIPT
= 2;
55 // ----------------------------------------------------------------------------
56 // the common part of wxApp implementations for all platforms
57 // ----------------------------------------------------------------------------
59 class WXDLLEXPORT wxAppBase
: public wxEvtHandler
62 // the virtual functions which may/must be overridden in the derived class
63 // -----------------------------------------------------------------------
65 virtual ~wxAppBase() {} // Added min for Mac X
67 // called during the program initialization, returning FALSE from here
68 // prevents the program from continuing - it's a good place to create
69 // the top level program window and return TRUE.
71 // Override: always in GUI application, rarely in console ones.
73 virtual bool OnInit() { return FALSE
; };
75 virtual bool OnInit() { return TRUE
; };
79 // a platform-dependent version of OnInit(): the code here is likely to
80 // depend on the toolkit. default version does nothing.
83 virtual bool OnInitGui() { return TRUE
; }
86 // called to start program execution - the default version just enters
87 // the main GUI loop in which events are received and processed until
88 // the last window is not deleted (if GetExitOnFrameDelete) or
89 // ExitMainLoop() is called. In console mode programs, the execution
90 // of the program really starts here
92 // Override: rarely in GUI applications, always in console ones.
94 virtual int OnRun() { return MainLoop(); };
96 virtual int OnRun() = 0;
99 // called after the main loop termination. This is a good place for
100 // cleaning up (it may be too late in dtor) and is also useful if you
101 // want to return some non-default exit code - this is just the return
102 // value of this method.
105 virtual int OnExit();
107 // called when a fatal exception occurs, this function should take care
108 // not to do anything which might provoke a nested exception! It may be
109 // overridden if you wish to react somehow in non-default way (core
110 // dump under Unix, application crash under Windows) to fatal program
111 // errors, however extreme care should be taken if you don't want this
112 // function to crash.
115 virtual void OnFatalException() { }
117 // the worker functions - usually not used directly by the user code
118 // -----------------------------------------------------------------
121 // execute the main GUI loop, the function returns when the loop ends
122 virtual int MainLoop() = 0;
124 // exit the main GUI loop during the next iteration (i.e. it does not
125 // stop the program immediately!)
126 virtual void ExitMainLoop() = 0;
128 // returns TRUE if the program is initialized
129 virtual bool Initialized() = 0;
131 // returns TRUE if there are unprocessed events in the event queue
132 virtual bool Pending() = 0;
134 // process the first event in the event queue (blocks until an event
135 // apperas if there are none currently)
136 virtual void Dispatch() = 0;
139 // application info: name, description, vendor
140 // -------------------------------------------
142 // NB: all these should be set by the application itself, there are no
143 // reasonable default except for the application name which is taken to
146 // set/get the application name
147 wxString
GetAppName() const
154 void SetAppName(const wxString
& name
) { m_appName
= name
; }
156 // set/get the app class name
157 wxString
GetClassName() const { return m_className
; }
158 void SetClassName(const wxString
& name
) { m_className
= name
; }
160 // set/get the vendor name
161 const wxString
& GetVendorName() const { return m_vendorName
; }
162 void SetVendorName(const wxString
& name
) { m_vendorName
= name
; }
165 // top level window functions
166 // --------------------------
168 // set the "main" top level window
169 void SetTopWindow(wxWindow
*win
) { m_topWindow
= win
; }
171 // return the "main" top level window (if it hadn't been set previously
172 // with SetTopWindow(), will return just some top level window and, if
173 // there are none, will return NULL)
174 virtual wxWindow
*GetTopWindow() const
178 else if (wxTopLevelWindows
.GetCount() > 0)
179 return wxTopLevelWindows
.GetFirst()->GetData();
181 return (wxWindow
*)NULL
;
184 // control the exit behaviour: by default, the program will exit the
185 // main loop (and so, usually, terminate) when the last top-level
186 // program window is deleted. Beware that if you disabel this (with
187 // SetExitOnFrameDelete(FALSE)), you'll have to call ExitMainLoop()
188 // explicitly from somewhere.
189 void SetExitOnFrameDelete(bool flag
) { m_exitOnFrameDelete
= flag
; }
190 bool GetExitOnFrameDelete() const { return m_exitOnFrameDelete
; }
194 // miscellaneous customization functions
195 // -------------------------------------
198 // override this function to create default log target of arbitrary
199 // user-defined class (default implementation creates a wxLogGui
200 // object) - this log object is used by default by all wxLogXXX()
202 virtual wxLog
*CreateLogTarget()
204 { return new wxLogGui
; }
206 { return new wxLogStderr
; }
211 // get the standard icon used by wxWin dialogs - this allows the user
212 // to customize the standard dialogs. The 'which' parameter is one of
214 virtual wxIcon
GetStdIcon(int which
) const = 0;
216 // VZ: what does this do exactly?
217 void SetWantDebugOutput( bool flag
) { m_wantDebugOutput
= flag
; }
218 bool GetWantDebugOutput() const { return m_wantDebugOutput
; }
220 // set use of best visual flag (see below)
221 void SetUseBestVisual( bool flag
) { m_useBestVisual
= flag
; }
222 bool GetUseBestVisual() const { return m_useBestVisual
; }
224 // set/get printing mode: see wxPRINT_XXX constants.
226 // default behaviour is the normal one for Unix: always use PostScript
228 virtual void SetPrintMode(int WXUNUSED(mode
)) { }
229 int GetPrintMode() const { return wxPRINT_POSTSCRIPT
; }
232 // implementation only from now on
233 // -------------------------------
235 // helpers for dynamic wxApp construction
236 static void SetInitializerFunction(wxAppInitializerFunction fn
)
237 { m_appInitFn
= fn
; }
238 static wxAppInitializerFunction
GetInitializerFunction()
239 { return m_appInitFn
; }
241 // process all events in the wxPendingEvents list
242 virtual void ProcessPendingEvents();
244 // access to the command line arguments
250 // function used for dynamic wxApp creation
251 static wxAppInitializerFunction m_appInitFn
;
253 // application info (must be set from the user code)
254 wxString m_vendorName
, // vendor name (ACME Inc)
255 m_appName
, // app name
256 m_className
; // class name
258 // if TRUE, exit the main loop when the last top level window is deleted
259 bool m_exitOnFrameDelete
;
261 // TRUE if the application wants to get debug output
262 bool m_wantDebugOutput
;
264 // TRUE if the apps whats to use the best visual on systems where
265 // more than one are available (Sun, SGI, XFree86 4.0 ?)
266 bool m_useBestVisual
;
269 // the main top level window - may be NULL
270 wxWindow
*m_topWindow
;
274 // ----------------------------------------------------------------------------
275 // now include the declaration of the real class
276 // ----------------------------------------------------------------------------
279 #if defined(__WXMSW__)
280 #include "wx/msw/app.h"
281 #elif defined(__WXMOTIF__)
282 #include "wx/motif/app.h"
283 #elif defined(__WXQT__)
284 #include "wx/qt/app.h"
285 #elif defined(__WXGTK__)
286 #include "wx/gtk/app.h"
287 #elif defined(__WXMAC__)
288 #include "wx/mac/app.h"
289 #elif defined(__WXPM__)
290 #include "wx/os2/app.h"
291 #elif defined(__WXSTUBS__)
292 #include "wx/stubs/app.h"
295 // can't use typedef because wxApp forward declared as a class
296 class WXDLLEXPORT wxApp
: public wxAppBase
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 // the one and only application object - use of wxTheApp in application code
306 // is discouraged, consider using DECLARE_APP() after which you may call
307 // wxGetApp() which will return the object of the correct type (i.e. MyApp and
309 WXDLLEXPORT_DATA(extern wxApp
*) wxTheApp
;
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 // event loop related functions only work in GUI programs
316 // ------------------------------------------------------
318 // Force an exit from main loop
319 extern void WXDLLEXPORT
wxExit();
321 // Yield to other apps/messages
322 extern bool WXDLLEXPORT
wxYield();
324 // Yield to other apps/messages
325 extern void WXDLLEXPORT
wxWakeUpIdle();
327 // Post a message to the given eventhandler which will be processed during the
328 // next event loop iteration
329 inline void wxPostEvent(wxEvtHandler
*dest
, wxEvent
& event
)
331 wxCHECK_RET( dest
, wxT("need an object to post event to in wxPostEvent") );
334 dest
->AddPendingEvent(event
);
336 dest
->ProcessEvent(event
);
340 // console applications may avoid using DECLARE_APP and IMPLEMENT_APP macros
341 // and call these functions instead at the program startup and termination
342 // -------------------------------------------------------------------------
346 // initialize the library (may be called as many times as needed, but each
347 // call to wxInitialize() must be matched by wxUninitialize())
348 extern bool WXDLLEXPORT
wxInitialize();
350 // clean up - the library can't be used any more after the last call to
352 extern void WXDLLEXPORT
wxUninitialize();
354 // create an object of this class on stack to initialize/cleanup thel ibrary
356 class WXDLLEXPORT wxInitializer
359 // initialize the library
360 wxInitializer() { m_ok
= wxInitialize(); }
362 // has the initialization been successful? (explicit test)
363 bool IsOk() const { return m_ok
; }
365 // has the initialization been successful? (implicit test)
366 operator bool() const { return m_ok
; }
368 // dtor only does clean up if we initialized the library properly
369 ~wxInitializer() { if ( m_ok
) wxUninitialize(); }
377 // ----------------------------------------------------------------------------
378 // macros for dynamic creation of the application object
379 // ----------------------------------------------------------------------------
381 // Having a global instance of this class allows wxApp to be aware of the app
382 // creator function. wxApp can then call this function to create a new app
383 // object. Convoluted, but necessary.
385 class WXDLLEXPORT wxAppInitializer
388 wxAppInitializer(wxAppInitializerFunction fn
)
389 { wxApp::SetInitializerFunction(fn
); }
392 // Here's a macro you can use if your compiler really, really wants main() to
393 // be in your main program (e.g. hello.cpp). Now IMPLEMENT_APP should add this
396 #if !wxUSE_GUI || defined(__WXMOTIF__) || defined(__WXGTK__) || defined(__WXPM__)
397 #define IMPLEMENT_WXWIN_MAIN \
398 extern int wxEntry( int argc, char *argv[] ); \
399 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
400 #elif defined(__WXMAC__) && defined(__UNIX__)
401 // wxMac seems to have a specific wxEntry prototype
402 #define IMPLEMENT_WXWIN_MAIN \
403 extern int wxEntry( int argc, char *argv[], bool enterLoop = 1 ); \
404 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
405 #elif defined(__WXMSW__) && defined(WXUSINGDLL)
406 // NT defines APIENTRY, 3.x not
407 #if !defined(WXAPIENTRY)
408 #define WXAPIENTRY WXFAR wxSTDCALL
412 #include "wx/msw/winundef.h"
414 #define IMPLEMENT_WXWIN_MAIN \
415 extern "C" int WXAPIENTRY WinMain(HINSTANCE hInstance,\
416 HINSTANCE hPrevInstance,\
417 LPSTR m_lpCmdLine, int nCmdShow)\
419 return wxEntry((WXHINSTANCE) hInstance,\
420 (WXHINSTANCE) hPrevInstance,\
421 m_lpCmdLine, nCmdShow);\
424 #define IMPLEMENT_WXWIN_MAIN
427 // Use this macro exactly once, the argument is the name of the wxApp-derived
428 // class which is the class of your application.
429 #define IMPLEMENT_APP(appname) \
430 wxApp *wxCreateApp() { return new appname; } \
431 wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
432 appname& wxGetApp() { return *(appname *)wxTheApp; } \
435 // Use this macro if you want to define your own main() or WinMain() function
436 // and call wxEntry() from there.
437 #define IMPLEMENT_APP_NO_MAIN(appname) \
438 wxApp *wxCreateApp() { return new appname; } \
439 wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
440 appname& wxGetApp() { return *(appname *)wxTheApp; }
442 #define DECLARE_APP(appname) extern appname& wxGetApp();