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 // ----------------------------------------------------------------------------
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 // called during the program initialization, returning FALSE from here
66 // prevents the program from continuing - it's a good place to create
67 // the top level program window and return TRUE.
69 // Override: always in GUI application, rarely in console ones.
71 virtual bool OnInit() { return FALSE
; };
73 virtual bool OnInit() { return TRUE
; };
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() { return TRUE
; }
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() { return 0; }
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;
137 // application info: name, description, vendor
138 // -------------------------------------------
140 // NB: all these should be set by the application itself, there are no
141 // reasonable default except for the application name which is taken to
144 // set/get the application name
145 wxString
GetAppName() const
152 void SetAppName(const wxString
& name
) { m_appName
= name
; }
154 // set/get the app class name
155 wxString
GetClassName() const { return m_className
; }
156 void SetClassName(const wxString
& name
) { m_className
= name
; }
158 // set/get the vendor name
159 const wxString
& GetVendorName() const { return m_vendorName
; }
160 void SetVendorName(const wxString
& name
) { m_vendorName
= name
; }
163 // top level window functions
164 // --------------------------
166 // set the "main" top level window
167 void SetTopWindow(wxWindow
*win
) { m_topWindow
= win
; }
169 // return the "main" top level window (if it hadn't been set previously
170 // with SetTopWindow(), will return just some top level window and, if
171 // there are none, will return NULL)
172 wxWindow
*GetTopWindow() const
176 else if (wxTopLevelWindows
.GetCount() > 0)
177 return wxTopLevelWindows
.GetFirst()->GetData();
179 return (wxWindow
*)NULL
;
182 // control the exit behaviour: by default, the program will exit the
183 // main loop (and so, usually, terminate) when the last top-level
184 // program window is deleted. Beware that if you disabel this (with
185 // SetExitOnFrameDelete(FALSE)), you'll have to call ExitMainLoop()
186 // explicitly from somewhere.
187 void SetExitOnFrameDelete(bool flag
) { m_exitOnFrameDelete
= flag
; }
188 bool GetExitOnFrameDelete() const { return m_exitOnFrameDelete
; }
192 // miscellaneous customization functions
193 // -------------------------------------
196 // override this function to create default log target of arbitrary
197 // user-defined class (default implementation creates a wxLogGui
198 // object) - this log object is used by default by all wxLogXXX()
200 virtual wxLog
*CreateLogTarget()
202 { return new wxLogGui
; }
204 { return new wxLogStderr
; }
209 // get the standard icon used by wxWin dialogs - this allows the user
210 // to customize the standard dialogs. The 'which' parameter is one of
212 virtual wxIcon
GetStdIcon(int which
) const = 0;
214 // VZ: what does this do exactly?
215 void SetWantDebugOutput( bool flag
) { m_wantDebugOutput
= flag
; }
216 bool GetWantDebugOutput() const { return m_wantDebugOutput
; }
218 // set/get printing mode: see wxPRINT_XXX constants.
220 // default behaviour is the normal one for Unix: always use PostScript
222 virtual void SetPrintMode(int WXUNUSED(mode
)) { }
223 int GetPrintMode() const { return wxPRINT_POSTSCRIPT
; }
226 // implementation only from now on
227 // -------------------------------
229 // helpers for dynamic wxApp construction
230 static void SetInitializerFunction(wxAppInitializerFunction fn
)
231 { m_appInitFn
= fn
; }
232 static wxAppInitializerFunction
GetInitializerFunction()
233 { return m_appInitFn
; }
235 // access to the command line arguments
241 // function used for dynamic wxApp creation
242 static wxAppInitializerFunction m_appInitFn
;
244 // application info (must be set from the user code)
245 wxString m_vendorName
, // vendor name (ACME Inc)
246 m_appName
, // app name
247 m_className
; // class name
249 // if TRUE, exit the main loop when the last top level window is deleted
250 bool m_exitOnFrameDelete
;
252 // TRUE if the application wants to get debug output
253 bool m_wantDebugOutput
;
256 // the main top level window - may be NULL
257 wxWindow
*m_topWindow
;
261 // ----------------------------------------------------------------------------
262 // now include the declaration of the real class
263 // ----------------------------------------------------------------------------
266 #if defined(__WXMSW__)
267 #include "wx/msw/app.h"
268 #elif defined(__WXMOTIF__)
269 #include "wx/motif/app.h"
270 #elif defined(__WXQT__)
271 #include "wx/qt/app.h"
272 #elif defined(__WXGTK__)
273 #include "wx/gtk/app.h"
274 #elif defined(__WXMAC__)
275 #include "wx/mac/app.h"
276 #elif defined(__WXPM__)
277 #include "wx/os2/app.h"
278 #elif defined(__WXSTUBS__)
279 #include "wx/stubs/app.h"
282 typedef wxAppBase wxApp
;
285 // ----------------------------------------------------------------------------
287 // ----------------------------------------------------------------------------
289 // the one and only application object - use of wxTheApp in application code
290 // is discouraged, consider using DECLARE_APP() after which you may call
291 // wxGetApp() which will return the object of the correct type (i.e. MyApp and
293 WXDLLEXPORT_DATA(extern wxApp
*) wxTheApp
;
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
299 // event loop related functions only work in GUI programs
300 // ------------------------------------------------------
304 // Force an exit from main loop
305 extern void WXDLLEXPORT
wxExit();
307 // Yield to other apps/messages
308 extern bool WXDLLEXPORT
wxYield();
312 // console applications may avoid using DECLARE_APP and IMPLEMENT_APP macros
313 // and call these functions instead at the program startup and termination
314 // -------------------------------------------------------------------------
318 // initialize the library (may be called as many times as needed, but each
319 // call to wxInitialize() must be matched by wxUninitialize())
320 extern bool WXDLLEXPORT
wxInitialize();
322 // clean up - the library can't be used any more after the last call to
324 extern void WXDLLEXPORT
wxUninitialize();
326 #endif // wxUSE_NOGUI
328 // ----------------------------------------------------------------------------
329 // macros for dynamic creation of the application object
330 // ----------------------------------------------------------------------------
332 // Having a global instance of this class allows wxApp to be aware of the app
333 // creator function. wxApp can then call this function to create a new app
334 // object. Convoluted, but necessary.
336 class WXDLLEXPORT wxAppInitializer
339 wxAppInitializer(wxAppInitializerFunction fn
)
340 { wxApp::SetInitializerFunction(fn
); }
343 // Here's a macro you can use if your compiler really, really wants main() to
344 // be in your main program (e.g. hello.cpp). Now IMPLEMENT_APP should add this
347 #if defined(__AIX__) || defined(__HPUX__)
348 #define IMPLEMENT_WXWIN_MAIN \
349 extern int wxEntry( int argc, char *argv[] ); \
350 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
351 #elif defined(__WXMSW__) && defined(WXUSINGDLL)
352 // NT defines APIENTRY, 3.x not
353 #if !defined(WXAPIENTRY)
355 #define WXAPIENTRY PASCAL
357 #define WXAPIENTRY FAR PASCAL
361 #define IMPLEMENT_WXWIN_MAIN \
362 int WXAPIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,\
363 LPSTR m_lpCmdLine, int nCmdShow )\
365 return wxEntry((WXHINSTANCE) hInstance, \
366 (WXHINSTANCE) hPrevInstance,\
367 m_lpCmdLine, nCmdShow);\
371 #define IMPLEMENT_WXWIN_MAIN
374 // use this macro exactly once, the argument is the name of the wxApp-derived
375 // class which is the class of your application
376 #define IMPLEMENT_APP(appname) \
377 wxApp *wxCreateApp() { return new appname; } \
378 wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
379 appname& wxGetApp() { return *(appname *)wxTheApp; } \
382 #define DECLARE_APP(appname) extern appname& wxGetApp();