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 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 static const int wxPRINT_WINDOWS
= 1;
39 static const int wxPRINT_POSTSCRIPT
= 2;
41 // ----------------------------------------------------------------------------
42 // the common part of wxApp implementations for all platforms
43 // ----------------------------------------------------------------------------
45 class WXDLLEXPORT wxAppBase
: public wxEvtHandler
48 // the virtual functions which may/must be overridden in the derived class
49 // -----------------------------------------------------------------------
51 // called during the program initialization, returning FALSE from here
52 // prevents the program from continuing - it's a good place to create
53 // the top level program window and return TRUE.
56 virtual bool OnInit() { return FALSE
; };
58 // a platform-dependent version of OnInit(): the code here is likely to
59 // depend on the toolkit. default version does nothing.
62 virtual bool OnInitGui() { return TRUE
; }
64 // called to start program execution - the default version just enters
65 // the main GUI loop in which events are received and processed until
66 // the last window is not deleted (if GetExitOnFrameDelete) or
67 // ExitMainLoop() is called.
70 virtual int OnRun() { return MainLoop(); };
72 // called after the main loop termination. This is a good place for
73 // cleaning up (it may be too late in dtor) and is also useful if you
74 // want to return some non-default exit code - this is just the return
75 // value of this method.
78 virtual int OnExit() { return 0; }
80 // called when a fatal exception occurs, this function should take care
81 // not to do anything which might provoke a nested exception! It may be
82 // overridden if you wish to react somehow in non-default way (core
83 // dump under Unix, application crash under Windows) to fatal program
84 // errors, however extreme care should be taken if you don't want this
88 virtual void OnFatalException() { }
90 // the worker functions - usually not used directly by the user code
91 // -----------------------------------------------------------------
93 // execute the main GUI loop, the function returns when the loop ends
94 virtual int MainLoop() = 0;
96 // exit the main GUI loop during the next iteration (i.e. it does not
97 // stop the program immediately!)
98 virtual void ExitMainLoop() = 0;
100 // returns TRUE if the program is initialized
101 virtual bool Initialized() = 0;
103 // returns TRUE if there are unprocessed events in the event queue
104 virtual bool Pending() = 0;
106 // process the first event in the event queue (blocks until an event
107 // apperas if there are none currently)
108 virtual void Dispatch() = 0;
110 // application info: name, description, vendor
111 // -------------------------------------------
113 // NB: all these should be set by the application itself, there are no
114 // reasonable default except for the application name which is taken to
117 // set/get the application name
118 wxString
GetAppName() const
125 void SetAppName(const wxString
& name
) { m_appName
= name
; }
127 // set/get the app class name
128 wxString
GetClassName() const { return m_className
; }
129 void SetClassName(const wxString
& name
) { m_className
= name
; }
131 // set/get the vendor name
132 const wxString
& GetVendorName() const { return m_vendorName
; }
133 void SetVendorName(const wxString
& name
) { m_vendorName
= name
; }
135 // top level window functions
136 // --------------------------
138 // set the "main" top level window
139 void SetTopWindow(wxWindow
*win
) { m_topWindow
= win
; }
141 // return the "main" top level window (if it hadn't been set previously
142 // with SetTopWindow(), will return just some top level window and, if
143 // there are none, will return NULL)
144 wxWindow
*GetTopWindow() const
148 else if (wxTopLevelWindows
.GetCount() > 0)
149 return wxTopLevelWindows
.GetFirst()->GetData();
151 return (wxWindow
*)NULL
;
154 // control the exit behaviour: by default, the program will exit the
155 // main loop (and so, usually, terminate) when the last top-level
156 // program window is deleted. Beware that if you disabel this (with
157 // SetExitOnFrameDelete(FALSE)), you'll have to call ExitMainLoop()
158 // explicitly from somewhere.
159 void SetExitOnFrameDelete(bool flag
) { m_exitOnFrameDelete
= flag
; }
160 bool GetExitOnFrameDelete() const { return m_exitOnFrameDelete
; }
162 // miscellaneous customization functions
163 // -------------------------------------
166 // override this function to create default log target of arbitrary
167 // user-defined class (default implementation creates a wxLogGui
168 // object) - this log object is used by default by all wxLogXXX()
170 virtual wxLog
*CreateLogTarget() { return new wxLogGui
; }
174 // get the standard icon used by wxWin dialogs - this allows the user
175 // to customize the standard dialogs. The 'which' parameter is one of
177 virtual wxIcon
GetStdIcon(int which
) const = 0;
179 // VZ: what does this do exactly?
180 void SetWantDebugOutput( bool flag
) { m_wantDebugOutput
= flag
; }
181 bool GetWantDebugOutput() const { return m_wantDebugOutput
; }
183 // set/get printing mode: see wxPRINT_XXX constants.
185 // default behaviour is the normal one for Unix: always use PostScript
187 virtual void SetPrintMode(int WXUNUSED(mode
)) { }
188 int GetPrintMode() const { return wxPRINT_POSTSCRIPT
; }
190 // implementation only from now on
191 // -------------------------------
193 // helpers for dynamic wxApp construction
194 static void SetInitializerFunction(wxAppInitializerFunction fn
)
195 { m_appInitFn
= fn
; }
196 static wxAppInitializerFunction
GetInitializerFunction()
197 { return m_appInitFn
; }
199 // access to the command line arguments
205 // function used for dynamic wxApp creation
206 static wxAppInitializerFunction m_appInitFn
;
208 // application info (must be set from the user code)
209 wxString m_vendorName
, // vendor name (ACME Inc)
210 m_appName
, // app name
211 m_className
; // class name
213 // if TRUE, exit the main loop when the last top level window is deleted
214 bool m_exitOnFrameDelete
;
216 // TRUE if the application wants to get debug output
217 bool m_wantDebugOutput
;
219 // the main top level window - may be NULL
220 wxWindow
*m_topWindow
;
223 // ----------------------------------------------------------------------------
224 // now include the declaration of the real class
225 // ----------------------------------------------------------------------------
227 #if defined(__WXMSW__)
228 #include "wx/msw/app.h"
229 #elif defined(__WXMOTIF__)
230 #include "wx/motif/app.h"
231 #elif defined(__WXQT__)
232 #include "wx/qt/app.h"
233 #elif defined(__WXGTK__)
234 #include "wx/gtk/app.h"
235 #elif defined(__WXMAC__)
236 #include "wx/mac/app.h"
237 #elif defined(__WXSTUBS__)
238 #include "wx/stubs/app.h"
241 // ----------------------------------------------------------------------------
242 // macros for dynamic creation of the application object
243 // ----------------------------------------------------------------------------
245 // Having a global instance of this class allows wxApp to be aware of the app
246 // creator function. wxApp can then call this function to create a new app
247 // object. Convoluted, but necessary.
249 class WXDLLEXPORT wxAppInitializer
252 wxAppInitializer(wxAppInitializerFunction fn
)
253 { wxApp::SetInitializerFunction(fn
); }
256 // Here's a macro you can use if your compiler really, really wants main() to
257 // be in your main program (e.g. hello.cpp). Now IMPLEMENT_APP should add this
260 #if defined(__AIX__) || defined(__HPUX__)
261 #define IMPLEMENT_WXWIN_MAIN \
262 extern int wxEntry( int argc, char *argv[] ); \
263 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
264 #elif defined(__WXMSW__) && defined(WXUSINGDLL)
265 // NT defines APIENTRY, 3.x not
266 #if !defined(WXAPIENTRY)
268 #define WXAPIENTRY PASCAL
270 #define WXAPIENTRY FAR PASCAL
274 #define IMPLEMENT_WXWIN_MAIN \
275 int WXAPIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,\
276 LPSTR m_lpCmdLine, int nCmdShow )\
278 return wxEntry((WXHINSTANCE) hInstance, \
279 (WXHINSTANCE) hPrevInstance,\
280 m_lpCmdLine, nCmdShow);\
284 #define IMPLEMENT_WXWIN_MAIN
287 // use this macro exactly once, the argument is the name of the wxApp-derived
288 // class which is the class of your application
289 #define IMPLEMENT_APP(appname) \
290 wxApp *wxCreateApp() { return new appname; } \
291 wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
292 appname& wxGetApp() { return *(appname *)wxTheApp; } \
295 #define DECLARE_APP(appname) extern appname& wxGetApp();