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