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