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