replaced wxAppTraits::GetOSVersion with GetToolkitInfo with richer information that...
[wxWidgets.git] / include / wx / app.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/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
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_APP_H_BASE_
14 #define _WX_APP_H_BASE_
15
16 // ----------------------------------------------------------------------------
17 // headers we have to include here
18 // ----------------------------------------------------------------------------
19
20 #include "wx/event.h" // for the base class
21
22 #if wxUSE_GUI
23 #include "wx/window.h" // for wxTopLevelWindows
24 #endif // wxUSE_GUI
25
26 #include "wx/build.h"
27 #include "wx/init.h" // we must declare wxEntry()
28
29 class WXDLLIMPEXP_BASE wxAppConsole;
30 class WXDLLIMPEXP_BASE wxAppTraits;
31 class WXDLLIMPEXP_BASE wxCmdLineParser;
32 class WXDLLIMPEXP_BASE wxLog;
33 class WXDLLIMPEXP_BASE wxMessageOutput;
34
35 // ----------------------------------------------------------------------------
36 // typedefs
37 // ----------------------------------------------------------------------------
38
39 // the type of the function used to create a wxApp object on program start up
40 typedef wxAppConsole* (*wxAppInitializerFunction)();
41
42 // ----------------------------------------------------------------------------
43 // constants
44 // ----------------------------------------------------------------------------
45
46 enum
47 {
48 wxPRINT_WINDOWS = 1,
49 wxPRINT_POSTSCRIPT = 2
50 };
51
52 // ----------------------------------------------------------------------------
53 // support for framebuffer ports
54 // ----------------------------------------------------------------------------
55
56 #if wxUSE_GUI
57 // VS: Fullscreen/framebuffer application needs to choose display mode prior
58 // to wxWindows initialization. This class holds information about display
59 // mode. It is used by wxApp::Set/GetDisplayMode.
60 class WXDLLIMPEXP_CORE wxDisplayModeInfo
61 {
62 public:
63 wxDisplayModeInfo() : m_ok(FALSE) {}
64 wxDisplayModeInfo(unsigned width, unsigned height, unsigned depth)
65 : m_width(width), m_height(height), m_depth(depth), m_ok(TRUE) {}
66
67 unsigned GetWidth() const { return m_width; }
68 unsigned GetHeight() const { return m_height; }
69 unsigned GetDepth() const { return m_depth; }
70 bool IsOk() const { return m_ok; }
71
72 private:
73 unsigned m_width, m_height, m_depth;
74 bool m_ok;
75 };
76 #endif // wxUSE_GUI
77
78
79 // ----------------------------------------------------------------------------
80 // wxAppConsole: wxApp for non-GUI applications
81 // ----------------------------------------------------------------------------
82
83 class WXDLLIMPEXP_BASE wxAppConsole : public wxEvtHandler
84 {
85 public:
86 // ctor and dtor
87 wxAppConsole();
88 virtual ~wxAppConsole();
89
90
91 // the virtual functions which may/must be overridden in the derived class
92 // -----------------------------------------------------------------------
93
94 // This is the very first function called for a newly created wxApp object,
95 // it is used by the library to do the global initialization. If, for some
96 // reason, you must override it (instead of just overriding OnInit(), as
97 // usual, for app-specific initializations), do not forget to call the base
98 // class version!
99 virtual bool Initialize(int& argc, wxChar **argv);
100
101 // This gives wxCocoa a chance to call OnInit() with a memory pool in place
102 virtual bool CallOnInit() { return OnInit(); }
103
104 // Called before OnRun(), this is a good place to do initialization -- if
105 // anything fails, return false from here to prevent the program from
106 // continuing. The command line is normally parsed here, call the base
107 // class OnInit() to do it.
108 virtual bool OnInit();
109
110 // this is here only temproary hopefully (FIXME)
111 virtual bool OnInitGui() { return true; }
112
113 // This is the replacement for the normal main(): all program work should
114 // be done here. When OnRun() returns, the programs starts shutting down.
115 virtual int OnRun() = 0;
116
117 // This is only called if OnInit() returned true so it's a good place to do
118 // any cleanup matching the initializations done there.
119 virtual int OnExit();
120
121 // This is the very last function called on wxApp object before it is
122 // destroyed. If you override it (instead of overriding OnExit() as usual)
123 // do not forget to call the base class version!
124 virtual void CleanUp();
125
126 // Called when a fatal exception occurs, this function should take care not
127 // to do anything which might provoke a nested exception! It may be
128 // overridden if you wish to react somehow in non-default way (core dump
129 // under Unix, application crash under Windows) to fatal program errors,
130 // however extreme care should be taken if you don't want this function to
131 // crash.
132 virtual void OnFatalException() { }
133
134 // Called from wxExit() function, should terminate the application a.s.a.p.
135 virtual void Exit();
136
137
138 // application info: name, description, vendor
139 // -------------------------------------------
140
141 // NB: all these should be set by the application itself, there are no
142 // reasonable default except for the application name which is taken to
143 // be argv[0]
144
145 // set/get the application name
146 wxString GetAppName() const
147 {
148 return m_appName.empty() ? m_className : m_appName;
149 }
150 void SetAppName(const wxString& name) { m_appName = name; }
151
152 // set/get the app class name
153 wxString GetClassName() const { return m_className; }
154 void SetClassName(const wxString& name) { m_className = name; }
155
156 // set/get the vendor name
157 const wxString& GetVendorName() const { return m_vendorName; }
158 void SetVendorName(const wxString& name) { m_vendorName = name; }
159
160
161 // cmd line parsing stuff
162 // ----------------------
163
164 // all of these methods may be overridden in the derived class to
165 // customize the command line parsing (by default only a few standard
166 // options are handled)
167 //
168 // you also need to call wxApp::OnInit() from YourApp::OnInit() for all
169 // this to work
170
171 #if wxUSE_CMDLINE_PARSER
172 // this one is called from OnInit() to add all supported options
173 // to the given parser
174 virtual void OnInitCmdLine(wxCmdLineParser& parser);
175
176 // called after successfully parsing the command line, return TRUE
177 // to continue and FALSE to exit
178 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
179
180 // called if "--help" option was specified, return TRUE to continue
181 // and FALSE to exit
182 virtual bool OnCmdLineHelp(wxCmdLineParser& parser);
183
184 // called if incorrect command line options were given, return
185 // FALSE to abort and TRUE to continue
186 virtual bool OnCmdLineError(wxCmdLineParser& parser);
187 #endif // wxUSE_CMDLINE_PARSER
188
189
190 // miscellaneous customization functions
191 // -------------------------------------
192
193 // create the app traits object to which we delegate for everything which
194 // either should be configurable by the user (then he can change the
195 // default behaviour simply by overriding CreateTraits() and returning his
196 // own traits object) or which is GUI/console dependent as then wxAppTraits
197 // allows us to abstract the differences behind the common façade
198 wxAppTraits *GetTraits();
199
200 // the functions below shouldn't be used now that we have wxAppTraits
201 #if WXWIN_COMPATIBILITY_2_4
202
203 #if wxUSE_LOG
204 // override this function to create default log target of arbitrary
205 // user-defined class (default implementation creates a wxLogGui
206 // object) -- this log object is used by default by all wxLogXXX()
207 // functions.
208 virtual wxLog *CreateLogTarget();
209 #endif // wxUSE_LOG
210
211 // similar to CreateLogTarget() but for the global wxMessageOutput
212 // object
213 virtual wxMessageOutput *CreateMessageOutput();
214
215 #endif // WXWIN_COMPATIBILITY_2_4
216
217
218 // event processing functions
219 // --------------------------
220
221 // this method allows to filter all the events processed by the program, so
222 // you should try to return quickly from it to avoid slowing down the
223 // program to the crawl
224 //
225 // return value should be -1 to continue with the normal event processing,
226 // or TRUE or FALSE to stop further processing and pretend that the event
227 // had been already processed or won't be processed at all, respectively
228 virtual int FilterEvent(wxEvent& event);
229
230 // process all events in the wxPendingEvents list -- it is necessary to
231 // call this function to process posted events. This happens during each
232 // event loop iteration in GUI mode but if there is no main loop, it may be
233 // also called directly.
234 virtual void ProcessPendingEvents();
235
236 // doesn't do anything in this class, just a hook for GUI wxApp
237 virtual bool Yield(bool WXUNUSED(onlyIfNeeded) = false) { return true; }
238
239 // make sure that idle events are sent again
240 virtual void WakeUpIdle() { }
241
242
243 // debugging support
244 // -----------------
245
246 // this function is called when an assert failure occurs, the base class
247 // version does the normal processing (i.e. shows the usual assert failure
248 // dialog box)
249 //
250 // the arguments are the place where the assert occured, the text of the
251 // assert itself and the user-specified message
252 #ifdef __WXDEBUG__
253 virtual void OnAssert(const wxChar *file,
254 int line,
255 const wxChar *cond,
256 const wxChar *msg);
257 #endif // __WXDEBUG__
258
259 // check that the wxBuildOptions object (constructed in the application
260 // itself, usually the one from IMPLEMENT_APP() macro) matches the build
261 // options of the library and abort if it doesn't
262 static bool CheckBuildOptions(const wxBuildOptions& buildOptions);
263
264
265 // implementation only from now on
266 // -------------------------------
267
268 // helpers for dynamic wxApp construction
269 static void SetInitializerFunction(wxAppInitializerFunction fn)
270 { ms_appInitFn = fn; }
271 static wxAppInitializerFunction GetInitializerFunction()
272 { return ms_appInitFn; }
273
274 // accessors for ms_appInstance field (external code might wish to modify
275 // it, this is why we provide a setter here as well, but you should really
276 // know what you're doing if you call it), wxTheApp is usually used instead
277 // of GetInstance()
278 static wxAppConsole *GetInstance() { return ms_appInstance; }
279 static void SetInstance(wxAppConsole *app) { ms_appInstance = app; }
280
281
282 // command line arguments (public for backwards compatibility)
283 int argc;
284 wxChar **argv;
285
286 protected:
287 // the function which creates the traits object when GetTraits() needs it
288 // for the first time
289 virtual wxAppTraits *CreateTraits();
290
291
292 // function used for dynamic wxApp creation
293 static wxAppInitializerFunction ms_appInitFn;
294
295 // the one and only global application object
296 static wxAppConsole *ms_appInstance;
297
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 // the class defining the application behaviour, NULL initially and created
305 // by GetTraits() when first needed
306 wxAppTraits *m_traits;
307
308
309 // the application object is a singleton anyhow, there is no sense in
310 // copying it
311 DECLARE_NO_COPY_CLASS(wxAppConsole)
312 };
313
314 // ----------------------------------------------------------------------------
315 // wxAppBase: the common part of wxApp implementations for all platforms
316 // ----------------------------------------------------------------------------
317
318 #if wxUSE_GUI
319
320 class WXDLLIMPEXP_CORE wxAppBase : public wxAppConsole
321 {
322 public:
323 wxAppBase();
324 virtual ~wxAppBase();
325
326 // the virtual functions which may/must be overridden in the derived class
327 // -----------------------------------------------------------------------
328
329 // very first initialization function
330 //
331 // Override: very rarely
332 virtual bool Initialize(int& argc, wxChar **argv);
333
334 // a platform-dependent version of OnInit(): the code here is likely to
335 // depend on the toolkit. default version does nothing.
336 //
337 // Override: rarely.
338 virtual bool OnInitGui();
339
340 // called to start program execution - the default version just enters
341 // the main GUI loop in which events are received and processed until
342 // the last window is not deleted (if GetExitOnFrameDelete) or
343 // ExitMainLoop() is called. In console mode programs, the execution
344 // of the program really starts here
345 //
346 // Override: rarely in GUI applications, always in console ones.
347 virtual int OnRun();
348
349 // very last clean up function
350 //
351 // Override: very rarely
352 virtual void CleanUp();
353
354
355 // the worker functions - usually not used directly by the user code
356 // -----------------------------------------------------------------
357
358 // execute the main GUI loop, the function returns when the loop ends
359 virtual int MainLoop() = 0;
360
361 // exit the main loop thus terminating the application
362 virtual void Exit();
363
364 // exit the main GUI loop during the next iteration (i.e. it does not
365 // stop the program immediately!)
366 virtual void ExitMainLoop() = 0;
367
368 // returns TRUE if the program is initialized
369 virtual bool Initialized() = 0;
370
371 // returns TRUE if there are unprocessed events in the event queue
372 virtual bool Pending() = 0;
373
374 // process the first event in the event queue (blocks until an event
375 // apperas if there are none currently)
376 virtual void Dispatch() = 0;
377
378 // process all currently pending events right now
379 //
380 // it is an error to call Yield() recursively unless the value of
381 // onlyIfNeeded is TRUE
382 //
383 // WARNING: this function is dangerous as it can lead to unexpected
384 // reentrancies (i.e. when called from an event handler it
385 // may result in calling the same event handler again), use
386 // with _extreme_ care or, better, don't use at all!
387 virtual bool Yield(bool onlyIfNeeded = FALSE) = 0;
388
389 // this virtual function is called in the GUI mode when the application
390 // becomes idle and normally just sends wxIdleEvent to all interested
391 // parties
392 //
393 // it should return TRUE if more idle events are needed, FALSE if not
394 virtual bool ProcessIdle() ;
395
396 // Send idle event to window and all subwindows
397 // Returns TRUE if more idle time is requested.
398 virtual bool SendIdleEvents(wxWindow* win, wxIdleEvent& event);
399
400 // Perform standard OnIdle behaviour: call from port's OnIdle
401 void OnIdle(wxIdleEvent& event);
402
403
404 // top level window functions
405 // --------------------------
406
407 // return TRUE if our app has focus
408 virtual bool IsActive() const { return m_isActive; }
409
410 // set the "main" top level window
411 void SetTopWindow(wxWindow *win) { m_topWindow = win; }
412
413 // return the "main" top level window (if it hadn't been set previously
414 // with SetTopWindow(), will return just some top level window and, if
415 // there are none, will return NULL)
416 virtual wxWindow *GetTopWindow() const
417 {
418 if (m_topWindow)
419 return m_topWindow;
420 else if (wxTopLevelWindows.GetCount() > 0)
421 return wxTopLevelWindows.GetFirst()->GetData();
422 else
423 return (wxWindow *)NULL;
424 }
425
426 // control the exit behaviour: by default, the program will exit the
427 // main loop (and so, usually, terminate) when the last top-level
428 // program window is deleted. Beware that if you disable this behaviour
429 // (with SetExitOnFrameDelete(FALSE)), you'll have to call
430 // ExitMainLoop() explicitly from somewhere.
431 void SetExitOnFrameDelete(bool flag)
432 { m_exitOnFrameDelete = flag ? Yes : No; }
433 bool GetExitOnFrameDelete() const
434 { return m_exitOnFrameDelete == Yes; }
435
436
437 // display mode, visual, printing mode, ...
438 // ------------------------------------------------------------------------
439
440 // Get display mode that is used use. This is only used in framebuffer
441 // wxWin ports (such as wxMGL).
442 virtual wxDisplayModeInfo GetDisplayMode() const { return wxDisplayModeInfo(); }
443 // Set display mode to use. This is only used in framebuffer wxWin
444 // ports (such as wxMGL). This method should be called from
445 // wxApp::OnInitGui
446 virtual bool SetDisplayMode(const wxDisplayModeInfo& WXUNUSED(info)) { return TRUE; }
447
448 // set use of best visual flag (see below)
449 void SetUseBestVisual( bool flag ) { m_useBestVisual = flag; }
450 bool GetUseBestVisual() const { return m_useBestVisual; }
451
452 // set/get printing mode: see wxPRINT_XXX constants.
453 //
454 // default behaviour is the normal one for Unix: always use PostScript
455 // printing.
456 virtual void SetPrintMode(int WXUNUSED(mode)) { }
457 int GetPrintMode() const { return wxPRINT_POSTSCRIPT; }
458
459
460 // miscellaneous other stuff
461 // ------------------------------------------------------------------------
462
463 // called by toolkit-specific code to set the app status: active (we have
464 // focus) or not and also the last window which had focus before we were
465 // deactivated
466 virtual void SetActive(bool isActive, wxWindow *lastFocus);
467
468
469 protected:
470 // delete all objects in wxPendingDelete list
471 void DeletePendingObjects();
472
473 // override base class method to use GUI traits
474 virtual wxAppTraits *CreateTraits();
475
476
477 // the main top level window (may be NULL)
478 wxWindow *m_topWindow;
479
480 // if Yes, exit the main loop when the last top level window is deleted, if
481 // No don't do it and if Later -- only do it once we reach our OnRun()
482 //
483 // the explanation for using this strange scheme is given in appcmn.cpp
484 enum
485 {
486 Later = -1,
487 No,
488 Yes
489 } m_exitOnFrameDelete;
490
491 // TRUE if the apps whats to use the best visual on systems where
492 // more than one are available (Sun, SGI, XFree86 4.0 ?)
493 bool m_useBestVisual;
494
495 // does any of our windows has focus?
496 bool m_isActive;
497
498
499 DECLARE_NO_COPY_CLASS(wxAppBase)
500 };
501
502 #endif // wxUSE_GUI
503
504 // ----------------------------------------------------------------------------
505 // now include the declaration of the real class
506 // ----------------------------------------------------------------------------
507
508 #if wxUSE_GUI
509 #if defined(__WXMSW__)
510 #include "wx/msw/app.h"
511 #elif defined(__WXMOTIF__)
512 #include "wx/motif/app.h"
513 #elif defined(__WXMGL__)
514 #include "wx/mgl/app.h"
515 #elif defined(__WXGTK__)
516 #include "wx/gtk/app.h"
517 #elif defined(__WXX11__)
518 #include "wx/x11/app.h"
519 #elif defined(__WXMAC__)
520 #include "wx/mac/app.h"
521 #elif defined(__WXCOCOA__)
522 #include "wx/cocoa/app.h"
523 #elif defined(__WXPM__)
524 #include "wx/os2/app.h"
525 #endif
526 #else // !GUI
527 // allow using just wxApp (instead of wxAppConsole) in console programs
528 typedef wxAppConsole wxApp;
529 #endif // GUI/!GUI
530
531 // ----------------------------------------------------------------------------
532 // the global data
533 // ----------------------------------------------------------------------------
534
535 // for compatibility, we define this macro to access the global application
536 // object of type wxApp
537 //
538 // note that instead of using of wxTheApp in application code you should
539 // consider using DECLARE_APP() after which you may call wxGetApp() which will
540 // return the object of the correct type (i.e. MyApp and not wxApp)
541 //
542 // the cast is safe as in GUI build we only use wxApp, not wxAppConsole, and in
543 // console mode it does nothing at all
544 #define wxTheApp ((wxApp *)wxApp::GetInstance())
545
546 // ----------------------------------------------------------------------------
547 // global functions
548 // ----------------------------------------------------------------------------
549
550 // event loop related functions only work in GUI programs
551 // ------------------------------------------------------
552
553 // Force an exit from main loop
554 extern void WXDLLIMPEXP_BASE wxExit();
555
556 // Yield to other apps/messages
557 extern bool WXDLLIMPEXP_BASE wxYield();
558
559 // Yield to other apps/messages
560 extern void WXDLLIMPEXP_BASE wxWakeUpIdle();
561
562 // ----------------------------------------------------------------------------
563 // macros for dynamic creation of the application object
564 // ----------------------------------------------------------------------------
565
566 // Having a global instance of this class allows wxApp to be aware of the app
567 // creator function. wxApp can then call this function to create a new app
568 // object. Convoluted, but necessary.
569
570 class WXDLLIMPEXP_BASE wxAppInitializer
571 {
572 public:
573 wxAppInitializer(wxAppInitializerFunction fn)
574 { wxApp::SetInitializerFunction(fn); }
575 };
576
577 // Here's a macro you can use if your compiler really, really wants main() to
578 // be in your main program (e.g. hello.cpp). Now IMPLEMENT_APP should add this
579 // code if required.
580
581 #if !wxUSE_GUI || !defined(__WXMSW__)
582 #define IMPLEMENT_WXWIN_MAIN \
583 int main(int argc, char **argv) { return wxEntry(argc, argv); }
584 #elif defined(__WXMSW__) && defined(WXUSINGDLL)
585 // we need HINSTANCE declaration to define WinMain()
586 #include <windows.h>
587 #include "wx/msw/winundef.h"
588
589 #define IMPLEMENT_WXWIN_MAIN \
590 extern int wxEntry(HINSTANCE hInstance, \
591 HINSTANCE hPrevInstance = NULL, \
592 char *pCmdLine = NULL, \
593 int nCmdShow = SW_NORMAL); \
594 extern "C" int WINAPI WinMain(HINSTANCE hInstance, \
595 HINSTANCE hPrevInstance, \
596 char *lpCmdLine, \
597 int nCmdShow) \
598 { \
599 return wxEntry(hInstance, hPrevInstance, lpCmdLine, nCmdShow); \
600 }
601 #else
602 #define IMPLEMENT_WXWIN_MAIN
603 #endif
604
605 #ifdef __WXUNIVERSAL__
606 #include "wx/univ/theme.h"
607
608 #define IMPLEMENT_WX_THEME_SUPPORT \
609 WX_USE_THEME(win32); \
610 WX_USE_THEME(gtk);
611 #else
612 #define IMPLEMENT_WX_THEME_SUPPORT
613 #endif
614
615 // Use this macro if you want to define your own main() or WinMain() function
616 // and call wxEntry() from there.
617 #define IMPLEMENT_APP_NO_MAIN(appname) \
618 wxAppConsole *wxCreateApp() \
619 { \
620 wxAppConsole::CheckBuildOptions(wxBuildOptions()); \
621 return new appname; \
622 } \
623 wxAppInitializer \
624 wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
625 appname& wxGetApp() { return *(appname *)wxTheApp; }
626
627 // Same as IMPLEMENT_APP() normally but doesn't include themes support in
628 // wxUniversal builds
629 #define IMPLEMENT_APP_NO_THEMES(appname) \
630 IMPLEMENT_APP_NO_MAIN(appname) \
631 IMPLEMENT_WXWIN_MAIN
632
633 // Use this macro exactly once, the argument is the name of the wxApp-derived
634 // class which is the class of your application.
635 #define IMPLEMENT_APP(appname) \
636 IMPLEMENT_APP_NO_THEMES(appname) \
637 IMPLEMENT_WX_THEME_SUPPORT
638
639 // this macro can be used multiple times and just allows you to use wxGetApp()
640 // function
641 #define DECLARE_APP(appname) extern appname& wxGetApp();
642
643 #endif // _WX_APP_H_BASE_
644