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