]> git.saurik.com Git - wxWidgets.git/blob - include/wx/app.h
Some OS/2 updates
[wxWidgets.git] / include / wx / app.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.h
3 // Purpose: wxAppBase class and macros used for declaration of wxApp
4 // derived class in the user code
5 // Author: Julian Smart
6 // Modified by:
7 // Created: 01/02/97
8 // RCS-ID: $Id$
9 // Copyright: (c) Julian Smart and Markus Holzem
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_APP_H_BASE_
14 #define _WX_APP_H_BASE_
15
16 #ifdef __GNUG__
17 #pragma interface "appbase.h"
18 #endif
19
20 // ----------------------------------------------------------------------------
21 // typedefs
22 // ----------------------------------------------------------------------------
23
24 #if (defined(__WXMSW__) && !defined(__WXMICROWIN__)) || defined (__WXPM__)
25 class WXDLLEXPORT wxApp;
26 typedef wxApp* (*wxAppInitializerFunction)();
27 #else
28 // returning wxApp* won't work with gcc
29 #include "wx/object.h"
30
31 typedef wxObject* (*wxAppInitializerFunction)();
32 #endif
33
34 class WXDLLEXPORT wxCmdLineParser;
35
36 // ----------------------------------------------------------------------------
37 // headers we have to include here
38 // ----------------------------------------------------------------------------
39
40 #include "wx/event.h" // for the base class
41
42 #if wxUSE_GUI
43 #include "wx/window.h" // for wxTopLevelWindows
44 #endif // wxUSE_GUI
45
46 #if wxUSE_LOG
47 #include "wx/log.h"
48 #endif
49
50 // ----------------------------------------------------------------------------
51 // constants
52 // ----------------------------------------------------------------------------
53
54 static const int wxPRINT_WINDOWS = 1;
55 static const int wxPRINT_POSTSCRIPT = 2;
56
57 // ----------------------------------------------------------------------------
58 // the common part of wxApp implementations for all platforms
59 // ----------------------------------------------------------------------------
60
61 class WXDLLEXPORT wxAppBase : public wxEvtHandler
62 {
63 public:
64 wxAppBase();
65
66 // the virtual functions which may/must be overridden in the derived class
67 // -----------------------------------------------------------------------
68 #ifdef __DARWIN__
69 virtual ~wxAppBase() { }
70 #endif
71
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 //
76 // Override: always in GUI application, rarely in console ones.
77 virtual bool OnInit();
78
79 #if wxUSE_GUI
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.
84 virtual bool OnInitGui();
85 #endif // wxUSE_GUI
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
90 // ExitMainLoop() is called. In console mode programs, the execution
91 // of the program really starts here
92 //
93 // Override: rarely in GUI applications, always in console ones.
94 #if wxUSE_GUI
95 virtual int OnRun() { return MainLoop(); };
96 #else // !GUI
97 virtual int OnRun() = 0;
98 #endif // wxUSE_GUI
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.
106 virtual int OnExit();
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
121 #if wxUSE_GUI
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;
138 #endif // wxUSE_GUI
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
165 #if wxUSE_GUI
166 // top level window functions
167 // --------------------------
168
169 // return TRUE if our app has focus
170 virtual bool IsActive() const { return m_isActive; }
171
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)
178 virtual wxWindow *GetTopWindow() const
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
196 #endif // wxUSE_GUI
197
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
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.
234 virtual wxLog *CreateLogTarget()
235 #if wxUSE_GUI && wxUSE_LOGGUI && !defined(__WXMICROWIN__)
236 { return new wxLogGui; }
237 #else // !GUI
238 { return new wxLogStderr; }
239 #endif // wxUSE_GUI
240 #endif // wxUSE_LOG
241
242 #if wxUSE_GUI
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
252 // set use of best visual flag (see below)
253 void SetUseBestVisual( bool flag ) { m_useBestVisual = flag; }
254 bool GetUseBestVisual() const { return m_useBestVisual; }
255
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; }
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);
267 #endif // wxUSE_GUI
268
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
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
288 // process all events in the wxPendingEvents list
289 virtual void ProcessPendingEvents();
290
291 // access to the command line arguments
292 int argc;
293 wxChar **argv;
294
295 protected:
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
304 // TRUE if the application wants to get debug output
305 bool m_wantDebugOutput;
306
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
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;
317
318 // does any of our windows has focus?
319 bool m_isActive;
320 #endif // wxUSE_GUI
321 };
322
323 // ----------------------------------------------------------------------------
324 // now include the declaration of the real class
325 // ----------------------------------------------------------------------------
326
327 #if wxUSE_GUI
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(__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
344 // can't use typedef because wxApp forward declared as a class
345 class WXDLLEXPORT wxApp : public wxAppBase
346 {
347 };
348 #endif // GUI/!GUI
349
350 // ----------------------------------------------------------------------------
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)
358 WXDLLEXPORT_DATA(extern wxApp*) wxTheApp;
359
360 // ----------------------------------------------------------------------------
361 // global functions
362 // ----------------------------------------------------------------------------
363
364 // event loop related functions only work in GUI programs
365 // ------------------------------------------------------
366
367 // Force an exit from main loop
368 extern void WXDLLEXPORT wxExit();
369
370 // Yield to other apps/messages
371 extern bool WXDLLEXPORT wxYield();
372
373 // Yield to other apps/messages
374 extern void WXDLLEXPORT wxWakeUpIdle();
375
376 // Post a message to the given eventhandler which will be processed during the
377 // next event loop iteration
378 inline void wxPostEvent(wxEvtHandler *dest, wxEvent& event)
379 {
380 wxCHECK_RET( dest, wxT("need an object to post event to in wxPostEvent") );
381
382 #if wxUSE_GUI
383 dest->AddPendingEvent(event);
384 #else
385 dest->ProcessEvent(event);
386 #endif // wxUSE_GUI
387 }
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
393 #if !wxUSE_GUI
394
395 // initialize the library (may be called as many times as needed, but each
396 // call to wxInitialize() must be matched by wxUninitialize())
397 extern bool WXDLLEXPORT wxInitialize();
398
399 // clean up - the library can't be used any more after the last call to
400 // wxUninitialize()
401 extern void WXDLLEXPORT wxUninitialize();
402
403 // create an object of this class on stack to initialize/cleanup thel ibrary
404 // automatically
405 class WXDLLEXPORT wxInitializer
406 {
407 public:
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
420 private:
421 bool m_ok;
422 };
423
424 #endif // !wxUSE_GUI
425
426 // ----------------------------------------------------------------------------
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.
433
434 class WXDLLEXPORT wxAppInitializer
435 {
436 public:
437 wxAppInitializer(wxAppInitializerFunction fn)
438 { wxApp::SetInitializerFunction(fn); }
439 };
440
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.
444
445 #if !wxUSE_GUI || defined(__WXMOTIF__) || defined(__WXGTK__) || defined(__WXPM__) || defined(__WXMGL__)
446 #define IMPLEMENT_WXWIN_MAIN \
447 extern int wxEntry( int argc, char *argv[] ); \
448 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
449 #elif defined(__WXMAC__) && defined(__UNIX__)
450 // wxMac seems to have a specific wxEntry prototype
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); }
454 #elif defined(__WXMSW__) && defined(WXUSINGDLL)
455 // NT defines APIENTRY, 3.x not
456 #if !defined(WXAPIENTRY)
457 #define WXAPIENTRY WXFAR wxSTDCALL
458 #endif
459
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 }
472 #else
473 #define IMPLEMENT_WXWIN_MAIN
474 #endif
475
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
485
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
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
507 #define DECLARE_APP(appname) extern appname& wxGetApp();
508
509 #endif
510 // _WX_APP_H_BASE_