]> git.saurik.com Git - wxWidgets.git/blame - include/wx/app.h
moved MGL initialization to earlier stage (crashes otherwise...) and implemented...
[wxWidgets.git] / include / wx / app.h
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.h
094637f6
VZ
3// Purpose: wxAppBase class and macros used for declaration of wxApp
4// derived class in the user code
c801d85f
KB
5// Author: Julian Smart
6// Modified by:
7// Created: 01/02/97
8// RCS-ID: $Id$
9// Copyright: (c) Julian Smart and Markus Holzem
094637f6 10// Licence: wxWindows licence
c801d85f
KB
11/////////////////////////////////////////////////////////////////////////////
12
34138703
JS
13#ifndef _WX_APP_H_BASE_
14#define _WX_APP_H_BASE_
c801d85f 15
094637f6
VZ
16#ifdef __GNUG__
17 #pragma interface "appbase.h"
10b959e3 18#endif
c801d85f 19
094637f6
VZ
20// ----------------------------------------------------------------------------
21// typedefs
22// ----------------------------------------------------------------------------
23
04ef50df 24#if (defined(__WXMSW__) && !defined(__WXMICROWIN__)) || defined (__WXPM__)
094637f6
VZ
25 class WXDLLEXPORT wxApp;
26 typedef wxApp* (*wxAppInitializerFunction)();
27#else
28 // returning wxApp* won't work with gcc
29 #include "wx/object.h"
c801d85f 30
094637f6 31 typedef wxObject* (*wxAppInitializerFunction)();
10b959e3 32#endif
c801d85f 33
bf188f1a
VZ
34class WXDLLEXPORT wxCmdLineParser;
35
23280650
VZ
36// ----------------------------------------------------------------------------
37// headers we have to include here
38// ----------------------------------------------------------------------------
39
40#include "wx/event.h" // for the base class
41
e90c1d2a 42#if wxUSE_GUI
bf188f1a 43 #include "wx/window.h" // for wxTopLevelWindows
e90c1d2a 44#endif // wxUSE_GUI
23280650
VZ
45
46#if wxUSE_LOG
47 #include "wx/log.h"
48#endif
49
094637f6
VZ
50// ----------------------------------------------------------------------------
51// constants
52// ----------------------------------------------------------------------------
53
54static const int wxPRINT_WINDOWS = 1;
55static const int wxPRINT_POSTSCRIPT = 2;
56
57// ----------------------------------------------------------------------------
58// the common part of wxApp implementations for all platforms
59// ----------------------------------------------------------------------------
60
61class WXDLLEXPORT wxAppBase : public wxEvtHandler
62{
63public:
1e6feb95
VZ
64 wxAppBase();
65
094637f6
VZ
66 // the virtual functions which may/must be overridden in the derived class
67 // -----------------------------------------------------------------------
f11bdd03
GD
68#ifdef __DARWIN__
69 virtual ~wxAppBase() { }
70#endif
bf188f1a 71
094637f6
VZ
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.
75 //
e90c1d2a 76 // Override: always in GUI application, rarely in console ones.
bf188f1a 77 virtual bool OnInit();
094637f6 78
e90c1d2a 79#if wxUSE_GUI
094637f6
VZ
80 // a platform-dependent version of OnInit(): the code here is likely to
81 // depend on the toolkit. default version does nothing.
82 //
83 // Override: rarely.
1e6feb95 84 virtual bool OnInitGui();
e90c1d2a 85#endif // wxUSE_GUI
094637f6
VZ
86
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
e90c1d2a
VZ
90 // ExitMainLoop() is called. In console mode programs, the execution
91 // of the program really starts here
094637f6 92 //
e90c1d2a
VZ
93 // Override: rarely in GUI applications, always in console ones.
94#if wxUSE_GUI
094637f6 95 virtual int OnRun() { return MainLoop(); };
e90c1d2a
VZ
96#else // !GUI
97 virtual int OnRun() = 0;
98#endif // wxUSE_GUI
094637f6
VZ
99
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.
104 //
105 // Override: often.
7beba2fc 106 virtual int OnExit();
094637f6
VZ
107
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.
114 //
115 // Override: rarely.
116 virtual void OnFatalException() { }
117
118 // the worker functions - usually not used directly by the user code
119 // -----------------------------------------------------------------
120
e90c1d2a 121#if wxUSE_GUI
094637f6
VZ
122 // execute the main GUI loop, the function returns when the loop ends
123 virtual int MainLoop() = 0;
124
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;
128
129 // returns TRUE if the program is initialized
130 virtual bool Initialized() = 0;
131
132 // returns TRUE if there are unprocessed events in the event queue
133 virtual bool Pending() = 0;
134
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;
e90c1d2a 138#endif // wxUSE_GUI
094637f6
VZ
139
140 // application info: name, description, vendor
141 // -------------------------------------------
142
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
145 // be argv[0]
146
147 // set/get the application name
148 wxString GetAppName() const
149 {
150 if ( !m_appName )
151 return m_className;
152 else
153 return m_appName;
154 }
155 void SetAppName(const wxString& name) { m_appName = name; }
156
157 // set/get the app class name
158 wxString GetClassName() const { return m_className; }
159 void SetClassName(const wxString& name) { m_className = name; }
160
161 // set/get the vendor name
162 const wxString& GetVendorName() const { return m_vendorName; }
163 void SetVendorName(const wxString& name) { m_vendorName = name; }
164
e90c1d2a 165#if wxUSE_GUI
094637f6
VZ
166 // top level window functions
167 // --------------------------
168
1e6feb95
VZ
169 // return TRUE if our app has focus
170 virtual bool IsActive() const { return m_isActive; }
171
094637f6
VZ
172 // set the "main" top level window
173 void SetTopWindow(wxWindow *win) { m_topWindow = win; }
174
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)
83a5b533 178 virtual wxWindow *GetTopWindow() const
094637f6
VZ
179 {
180 if (m_topWindow)
181 return m_topWindow;
182 else if (wxTopLevelWindows.GetCount() > 0)
183 return wxTopLevelWindows.GetFirst()->GetData();
184 else
185 return (wxWindow *)NULL;
186 }
187
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; }
195
e90c1d2a
VZ
196#endif // wxUSE_GUI
197
bf188f1a
VZ
198 // cmd line parsing stuff
199 // ----------------------
200
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)
204 //
205 // you also need to call wxApp::OnInit() from YourApp::OnInit() for all
206 // this to work
207
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);
212
213 // called after successfully parsing the command line, return TRUE
214 // to continue and FALSE to exit
215 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
216
217 // called if "--help" option was specified, return TRUE to continue
218 // and FALSE to exit
219 virtual bool OnCmdLineHelp(wxCmdLineParser& parser);
220
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
225
094637f6
VZ
226 // miscellaneous customization functions
227 // -------------------------------------
228
229#if wxUSE_LOG
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()
233 // functions.
e90c1d2a 234 virtual wxLog *CreateLogTarget()
54800df8 235 #if wxUSE_GUI && wxUSE_LOGGUI && !defined(__WXMICROWIN__)
1e6feb95
VZ
236 { return new wxLogGui; }
237 #else // !GUI
238 { return new wxLogStderr; }
239 #endif // wxUSE_GUI
094637f6
VZ
240#endif // wxUSE_LOG
241
e90c1d2a 242#if wxUSE_GUI
094637f6
VZ
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
245 // wxICON_XXX values
246 virtual wxIcon GetStdIcon(int which) const = 0;
247
248 // VZ: what does this do exactly?
249 void SetWantDebugOutput( bool flag ) { m_wantDebugOutput = flag; }
250 bool GetWantDebugOutput() const { return m_wantDebugOutput; }
251
8480b297
RR
252 // set use of best visual flag (see below)
253 void SetUseBestVisual( bool flag ) { m_useBestVisual = flag; }
254 bool GetUseBestVisual() const { return m_useBestVisual; }
75acb532 255
094637f6
VZ
256 // set/get printing mode: see wxPRINT_XXX constants.
257 //
258 // default behaviour is the normal one for Unix: always use PostScript
259 // printing.
260 virtual void SetPrintMode(int WXUNUSED(mode)) { }
261 int GetPrintMode() const { return wxPRINT_POSTSCRIPT; }
1e6feb95
VZ
262
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
265 // deactivated
266 virtual void SetActive(bool isActive, wxWindow *lastFocus);
e90c1d2a 267#endif // wxUSE_GUI
094637f6 268
a5f1fd3e
VZ
269 // debugging support
270 // -----------------
271
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
274 // dialog box)
275#ifdef __WXDEBUG__
276 virtual void OnAssert(const wxChar *file, int line, const wxChar *msg);
277#endif // __WXDEBUG__
278
094637f6
VZ
279 // implementation only from now on
280 // -------------------------------
281
282 // helpers for dynamic wxApp construction
283 static void SetInitializerFunction(wxAppInitializerFunction fn)
284 { m_appInitFn = fn; }
285 static wxAppInitializerFunction GetInitializerFunction()
286 { return m_appInitFn; }
287
72cdf4c9
VZ
288 // process all events in the wxPendingEvents list
289 virtual void ProcessPendingEvents();
290
094637f6 291 // access to the command line arguments
c1b03ce8
OK
292 int argc;
293 wxChar **argv;
094637f6 294
094637f6
VZ
295protected:
296 // function used for dynamic wxApp creation
297 static wxAppInitializerFunction m_appInitFn;
298
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
303
094637f6
VZ
304 // TRUE if the application wants to get debug output
305 bool m_wantDebugOutput;
75acb532 306
1e6feb95
VZ
307#if wxUSE_GUI
308 // the main top level window - may be NULL
309 wxWindow *m_topWindow;
310
311 // if TRUE, exit the main loop when the last top level window is deleted
312 bool m_exitOnFrameDelete;
313
8480b297
RR
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;
094637f6 317
1e6feb95
VZ
318 // does any of our windows has focus?
319 bool m_isActive;
e90c1d2a 320#endif // wxUSE_GUI
094637f6
VZ
321};
322
323// ----------------------------------------------------------------------------
324// now include the declaration of the real class
325// ----------------------------------------------------------------------------
326
e90c1d2a
VZ
327#if wxUSE_GUI
328 #if defined(__WXMSW__)
329 #include "wx/msw/app.h"
330 #elif defined(__WXMOTIF__)
331 #include "wx/motif/app.h"
1e6feb95
VZ
332 #elif defined(__WXMGL__)
333 #include "wx/mgl/app.h"
e90c1d2a
VZ
334 #elif defined(__WXGTK__)
335 #include "wx/gtk/app.h"
336 #elif defined(__WXMAC__)
337 #include "wx/mac/app.h"
338 #elif defined(__WXPM__)
339 #include "wx/os2/app.h"
340 #elif defined(__WXSTUBS__)
341 #include "wx/stubs/app.h"
342 #endif
343#else // !GUI
b568d04f
VZ
344 // can't use typedef because wxApp forward declared as a class
345 class WXDLLEXPORT wxApp : public wxAppBase
346 {
347 };
e90c1d2a 348#endif // GUI/!GUI
c801d85f 349
094637f6 350// ----------------------------------------------------------------------------
ee31c392
VZ
351// the global data
352// ----------------------------------------------------------------------------
353
354// the one and only application object - use of wxTheApp in application code
355// is discouraged, consider using DECLARE_APP() after which you may call
356// wxGetApp() which will return the object of the correct type (i.e. MyApp and
357// not wxApp)
358WXDLLEXPORT_DATA(extern wxApp*) wxTheApp;
359
360// ----------------------------------------------------------------------------
361// global functions
362// ----------------------------------------------------------------------------
363
e90c1d2a
VZ
364// event loop related functions only work in GUI programs
365// ------------------------------------------------------
366
ee31c392 367// Force an exit from main loop
e90c1d2a 368extern void WXDLLEXPORT wxExit();
ee31c392
VZ
369
370// Yield to other apps/messages
e90c1d2a
VZ
371extern bool WXDLLEXPORT wxYield();
372
bf9e3e73
RR
373// Yield to other apps/messages
374extern void WXDLLEXPORT wxWakeUpIdle();
bf9e3e73 375
8e193f38
VZ
376// Post a message to the given eventhandler which will be processed during the
377// next event loop iteration
6d56eb5c 378inline void wxPostEvent(wxEvtHandler *dest, wxEvent& event)
8e193f38
VZ
379{
380 wxCHECK_RET( dest, wxT("need an object to post event to in wxPostEvent") );
381
52a07708 382#if wxUSE_GUI
8e193f38 383 dest->AddPendingEvent(event);
52a07708
VZ
384#else
385 dest->ProcessEvent(event);
b568d04f 386#endif // wxUSE_GUI
52a07708 387}
e90c1d2a
VZ
388
389// console applications may avoid using DECLARE_APP and IMPLEMENT_APP macros
390// and call these functions instead at the program startup and termination
391// -------------------------------------------------------------------------
392
b568d04f 393#if !wxUSE_GUI
e90c1d2a
VZ
394
395// initialize the library (may be called as many times as needed, but each
396// call to wxInitialize() must be matched by wxUninitialize())
397extern bool WXDLLEXPORT wxInitialize();
398
399// clean up - the library can't be used any more after the last call to
400// wxUninitialize()
401extern void WXDLLEXPORT wxUninitialize();
402
fae9f590
VZ
403// create an object of this class on stack to initialize/cleanup thel ibrary
404// automatically
405class WXDLLEXPORT wxInitializer
406{
407public:
408 // initialize the library
409 wxInitializer() { m_ok = wxInitialize(); }
410
411 // has the initialization been successful? (explicit test)
412 bool IsOk() const { return m_ok; }
413
414 // has the initialization been successful? (implicit test)
415 operator bool() const { return m_ok; }
416
417 // dtor only does clean up if we initialized the library properly
418 ~wxInitializer() { if ( m_ok ) wxUninitialize(); }
419
420private:
421 bool m_ok;
422};
423
b568d04f 424#endif // !wxUSE_GUI
ee31c392
VZ
425
426// ----------------------------------------------------------------------------
094637f6
VZ
427// macros for dynamic creation of the application object
428// ----------------------------------------------------------------------------
429
430// Having a global instance of this class allows wxApp to be aware of the app
431// creator function. wxApp can then call this function to create a new app
432// object. Convoluted, but necessary.
c801d85f
KB
433
434class WXDLLEXPORT wxAppInitializer
435{
436public:
094637f6
VZ
437 wxAppInitializer(wxAppInitializerFunction fn)
438 { wxApp::SetInitializerFunction(fn); }
c801d85f
KB
439};
440
094637f6
VZ
441// Here's a macro you can use if your compiler really, really wants main() to
442// be in your main program (e.g. hello.cpp). Now IMPLEMENT_APP should add this
443// code if required.
26a87b69 444
f9a3f576 445#if !wxUSE_GUI || defined(__WXMOTIF__) || defined(__WXGTK__) || defined(__WXPM__) || defined(__WXMGL__)
094637f6
VZ
446 #define IMPLEMENT_WXWIN_MAIN \
447 extern int wxEntry( int argc, char *argv[] ); \
448 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
d5408e98 449#elif defined(__WXMAC__) && defined(__UNIX__)
bf188f1a 450 // wxMac seems to have a specific wxEntry prototype
d5408e98
GD
451 #define IMPLEMENT_WXWIN_MAIN \
452 extern int wxEntry( int argc, char *argv[], bool enterLoop = 1 ); \
453 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
750b78ba 454#elif defined(__WXMSW__) && defined(WXUSINGDLL)
094637f6
VZ
455 // NT defines APIENTRY, 3.x not
456 #if !defined(WXAPIENTRY)
790ad94f 457 #define WXAPIENTRY WXFAR wxSTDCALL
094637f6 458 #endif
750b78ba 459
f6bcfd97
BP
460 #include <windows.h>
461 #include "wx/msw/winundef.h"
462
463 #define IMPLEMENT_WXWIN_MAIN \
464 extern "C" int WXAPIENTRY WinMain(HINSTANCE hInstance,\
465 HINSTANCE hPrevInstance,\
466 LPSTR m_lpCmdLine, int nCmdShow)\
467 {\
468 return wxEntry((WXHINSTANCE) hInstance,\
469 (WXHINSTANCE) hPrevInstance,\
470 m_lpCmdLine, nCmdShow);\
471 }
26a87b69 472#else
094637f6 473 #define IMPLEMENT_WXWIN_MAIN
26a87b69
JS
474#endif
475
24e78d27
VZ
476#ifdef __WXUNIVERSAL__
477 #include "wx/univ/theme.h"
478
479 #define IMPLEMENT_WX_THEME_SUPPORT \
480 WX_USE_THEME(win32); \
481 WX_USE_THEME(gtk);
482#else
483 #define IMPLEMENT_WX_THEME_SUPPORT
484#endif
c801d85f 485
c661ecca
RR
486// Use this macro if you want to define your own main() or WinMain() function
487// and call wxEntry() from there.
488#define IMPLEMENT_APP_NO_MAIN(appname) \
489 wxApp *wxCreateApp() { return new appname; } \
490 wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
491 appname& wxGetApp() { return *(appname *)wxTheApp; }
492
24e78d27
VZ
493// Same as IMPLEMENT_APP() normally but doesn't include themes support in
494// wxUniversal builds
495#define IMPLEMENT_APP_NO_THEMES(appname) \
496 IMPLEMENT_APP_NO_MAIN(appname) \
497 IMPLEMENT_WXWIN_MAIN
498
499// Use this macro exactly once, the argument is the name of the wxApp-derived
500// class which is the class of your application.
501#define IMPLEMENT_APP(appname) \
502 IMPLEMENT_APP_NO_THEMES(appname) \
503 IMPLEMENT_WX_THEME_SUPPORT
504
505// this macro can be used multiple times and just allows you to use wxGetApp()
506// function
094637f6 507#define DECLARE_APP(appname) extern appname& wxGetApp();
c801d85f
KB
508
509#endif
34138703 510 // _WX_APP_H_BASE_