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