]> git.saurik.com Git - wxWidgets.git/blob - include/wx/app.h
made wxApp::GetTopWindow() virtual
[wxWidgets.git] / include / wx / app.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 and Markus Holzem
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_APP_H_BASE_
14 #define _WX_APP_H_BASE_
15
16 #ifdef __GNUG__
17 #pragma interface "appbase.h"
18 #endif
19
20 // ----------------------------------------------------------------------------
21 // typedefs
22 // ----------------------------------------------------------------------------
23
24 #if defined(__WXMSW__) || defined (__WXPM__)
25 class WXDLLEXPORT wxApp;
26 typedef wxApp* (*wxAppInitializerFunction)();
27 #else
28 // returning wxApp* won't work with gcc
29 #include "wx/object.h"
30
31 typedef wxObject* (*wxAppInitializerFunction)();
32 #endif
33
34 // ----------------------------------------------------------------------------
35 // headers we have to include here
36 // ----------------------------------------------------------------------------
37
38 #include "wx/event.h" // for the base class
39
40 #if wxUSE_GUI
41 #include "wx/window.h" // for wxTopLevelWindows
42 #endif // wxUSE_GUI
43
44 #if wxUSE_LOG
45 #include "wx/log.h"
46 #endif
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 static const int wxPRINT_WINDOWS = 1;
53 static const int wxPRINT_POSTSCRIPT = 2;
54
55 // ----------------------------------------------------------------------------
56 // the common part of wxApp implementations for all platforms
57 // ----------------------------------------------------------------------------
58
59 class WXDLLEXPORT wxAppBase : public wxEvtHandler
60 {
61 public:
62 // the virtual functions which may/must be overridden in the derived class
63 // -----------------------------------------------------------------------
64 #ifdef __WXMAC_X__
65 virtual ~wxAppBase() {} // Added min for Mac X
66 #endif
67 // called during the program initialization, returning FALSE from here
68 // prevents the program from continuing - it's a good place to create
69 // the top level program window and return TRUE.
70 //
71 // Override: always in GUI application, rarely in console ones.
72 #if wxUSE_GUI
73 virtual bool OnInit() { return FALSE; };
74 #else // !GUI
75 virtual bool OnInit() { return TRUE; };
76 #endif // wxUSE_GUI
77
78 #if wxUSE_GUI
79 // a platform-dependent version of OnInit(): the code here is likely to
80 // depend on the toolkit. default version does nothing.
81 //
82 // Override: rarely.
83 virtual bool OnInitGui() { return TRUE; }
84 #endif // wxUSE_GUI
85
86 // called to start program execution - the default version just enters
87 // the main GUI loop in which events are received and processed until
88 // the last window is not deleted (if GetExitOnFrameDelete) or
89 // ExitMainLoop() is called. In console mode programs, the execution
90 // of the program really starts here
91 //
92 // Override: rarely in GUI applications, always in console ones.
93 #if wxUSE_GUI
94 virtual int OnRun() { return MainLoop(); };
95 #else // !GUI
96 virtual int OnRun() = 0;
97 #endif // wxUSE_GUI
98
99 // called after the main loop termination. This is a good place for
100 // cleaning up (it may be too late in dtor) and is also useful if you
101 // want to return some non-default exit code - this is just the return
102 // value of this method.
103 //
104 // Override: often.
105 virtual int OnExit();
106
107 // called when a fatal exception occurs, this function should take care
108 // not to do anything which might provoke a nested exception! It may be
109 // overridden if you wish to react somehow in non-default way (core
110 // dump under Unix, application crash under Windows) to fatal program
111 // errors, however extreme care should be taken if you don't want this
112 // function to crash.
113 //
114 // Override: rarely.
115 virtual void OnFatalException() { }
116
117 // the worker functions - usually not used directly by the user code
118 // -----------------------------------------------------------------
119
120 #if wxUSE_GUI
121 // execute the main GUI loop, the function returns when the loop ends
122 virtual int MainLoop() = 0;
123
124 // exit the main GUI loop during the next iteration (i.e. it does not
125 // stop the program immediately!)
126 virtual void ExitMainLoop() = 0;
127
128 // returns TRUE if the program is initialized
129 virtual bool Initialized() = 0;
130
131 // returns TRUE if there are unprocessed events in the event queue
132 virtual bool Pending() = 0;
133
134 // process the first event in the event queue (blocks until an event
135 // apperas if there are none currently)
136 virtual void Dispatch() = 0;
137 #endif // wxUSE_GUI
138
139 // application info: name, description, vendor
140 // -------------------------------------------
141
142 // NB: all these should be set by the application itself, there are no
143 // reasonable default except for the application name which is taken to
144 // be argv[0]
145
146 // set/get the application name
147 wxString GetAppName() const
148 {
149 if ( !m_appName )
150 return m_className;
151 else
152 return m_appName;
153 }
154 void SetAppName(const wxString& name) { m_appName = name; }
155
156 // set/get the app class name
157 wxString GetClassName() const { return m_className; }
158 void SetClassName(const wxString& name) { m_className = name; }
159
160 // set/get the vendor name
161 const wxString& GetVendorName() const { return m_vendorName; }
162 void SetVendorName(const wxString& name) { m_vendorName = name; }
163
164 #if wxUSE_GUI
165 // top level window functions
166 // --------------------------
167
168 // set the "main" top level window
169 void SetTopWindow(wxWindow *win) { m_topWindow = win; }
170
171 // return the "main" top level window (if it hadn't been set previously
172 // with SetTopWindow(), will return just some top level window and, if
173 // there are none, will return NULL)
174 virtual wxWindow *GetTopWindow() const
175 {
176 if (m_topWindow)
177 return m_topWindow;
178 else if (wxTopLevelWindows.GetCount() > 0)
179 return wxTopLevelWindows.GetFirst()->GetData();
180 else
181 return (wxWindow *)NULL;
182 }
183
184 // control the exit behaviour: by default, the program will exit the
185 // main loop (and so, usually, terminate) when the last top-level
186 // program window is deleted. Beware that if you disabel this (with
187 // SetExitOnFrameDelete(FALSE)), you'll have to call ExitMainLoop()
188 // explicitly from somewhere.
189 void SetExitOnFrameDelete(bool flag) { m_exitOnFrameDelete = flag; }
190 bool GetExitOnFrameDelete() const { return m_exitOnFrameDelete; }
191
192 #endif // wxUSE_GUI
193
194 // miscellaneous customization functions
195 // -------------------------------------
196
197 #if wxUSE_LOG
198 // override this function to create default log target of arbitrary
199 // user-defined class (default implementation creates a wxLogGui
200 // object) - this log object is used by default by all wxLogXXX()
201 // functions.
202 virtual wxLog *CreateLogTarget()
203 #if wxUSE_GUI
204 { return new wxLogGui; }
205 #else // !GUI
206 { return new wxLogStderr; }
207 #endif // wxUSE_GUI
208 #endif // wxUSE_LOG
209
210 #if wxUSE_GUI
211 // get the standard icon used by wxWin dialogs - this allows the user
212 // to customize the standard dialogs. The 'which' parameter is one of
213 // wxICON_XXX values
214 virtual wxIcon GetStdIcon(int which) const = 0;
215
216 // VZ: what does this do exactly?
217 void SetWantDebugOutput( bool flag ) { m_wantDebugOutput = flag; }
218 bool GetWantDebugOutput() const { return m_wantDebugOutput; }
219
220 // set use of best visual flag (see below)
221 void SetUseBestVisual( bool flag ) { m_useBestVisual = flag; }
222 bool GetUseBestVisual() const { return m_useBestVisual; }
223
224 // set/get printing mode: see wxPRINT_XXX constants.
225 //
226 // default behaviour is the normal one for Unix: always use PostScript
227 // printing.
228 virtual void SetPrintMode(int WXUNUSED(mode)) { }
229 int GetPrintMode() const { return wxPRINT_POSTSCRIPT; }
230 #endif // wxUSE_GUI
231
232 // implementation only from now on
233 // -------------------------------
234
235 // helpers for dynamic wxApp construction
236 static void SetInitializerFunction(wxAppInitializerFunction fn)
237 { m_appInitFn = fn; }
238 static wxAppInitializerFunction GetInitializerFunction()
239 { return m_appInitFn; }
240
241 // process all events in the wxPendingEvents list
242 virtual void ProcessPendingEvents();
243
244 // access to the command line arguments
245 int argc;
246 wxChar **argv;
247
248 //private:
249 protected:
250 // function used for dynamic wxApp creation
251 static wxAppInitializerFunction m_appInitFn;
252
253 // application info (must be set from the user code)
254 wxString m_vendorName, // vendor name (ACME Inc)
255 m_appName, // app name
256 m_className; // class name
257
258 // if TRUE, exit the main loop when the last top level window is deleted
259 bool m_exitOnFrameDelete;
260
261 // TRUE if the application wants to get debug output
262 bool m_wantDebugOutput;
263
264 // TRUE if the apps whats to use the best visual on systems where
265 // more than one are available (Sun, SGI, XFree86 4.0 ?)
266 bool m_useBestVisual;
267
268 #if wxUSE_GUI
269 // the main top level window - may be NULL
270 wxWindow *m_topWindow;
271 #endif // wxUSE_GUI
272 };
273
274 // ----------------------------------------------------------------------------
275 // now include the declaration of the real class
276 // ----------------------------------------------------------------------------
277
278 #if wxUSE_GUI
279 #if defined(__WXMSW__)
280 #include "wx/msw/app.h"
281 #elif defined(__WXMOTIF__)
282 #include "wx/motif/app.h"
283 #elif defined(__WXQT__)
284 #include "wx/qt/app.h"
285 #elif defined(__WXGTK__)
286 #include "wx/gtk/app.h"
287 #elif defined(__WXMAC__)
288 #include "wx/mac/app.h"
289 #elif defined(__WXPM__)
290 #include "wx/os2/app.h"
291 #elif defined(__WXSTUBS__)
292 #include "wx/stubs/app.h"
293 #endif
294 #else // !GUI
295 // can't use typedef because wxApp forward declared as a class
296 class WXDLLEXPORT wxApp : public wxAppBase
297 {
298 };
299 #endif // GUI/!GUI
300
301 // ----------------------------------------------------------------------------
302 // the global data
303 // ----------------------------------------------------------------------------
304
305 // the one and only application object - use of wxTheApp in application code
306 // is discouraged, consider using DECLARE_APP() after which you may call
307 // wxGetApp() which will return the object of the correct type (i.e. MyApp and
308 // not wxApp)
309 WXDLLEXPORT_DATA(extern wxApp*) wxTheApp;
310
311 // ----------------------------------------------------------------------------
312 // global functions
313 // ----------------------------------------------------------------------------
314
315 // event loop related functions only work in GUI programs
316 // ------------------------------------------------------
317
318 // Force an exit from main loop
319 extern void WXDLLEXPORT wxExit();
320
321 // Yield to other apps/messages
322 extern bool WXDLLEXPORT wxYield();
323
324 // Yield to other apps/messages
325 extern void WXDLLEXPORT wxWakeUpIdle();
326
327 // Post a message to the given eventhandler which will be processed during the
328 // next event loop iteration
329 inline void WXDLLEXPORT wxPostEvent(wxEvtHandler *dest, wxEvent& event)
330 {
331 wxCHECK_RET( dest, wxT("need an object to post event to in wxPostEvent") );
332
333 #if wxUSE_GUI
334 dest->AddPendingEvent(event);
335 #else
336 dest->ProcessEvent(event);
337 #endif // wxUSE_GUI
338 }
339
340 // console applications may avoid using DECLARE_APP and IMPLEMENT_APP macros
341 // and call these functions instead at the program startup and termination
342 // -------------------------------------------------------------------------
343
344 #if !wxUSE_GUI
345
346 // initialize the library (may be called as many times as needed, but each
347 // call to wxInitialize() must be matched by wxUninitialize())
348 extern bool WXDLLEXPORT wxInitialize();
349
350 // clean up - the library can't be used any more after the last call to
351 // wxUninitialize()
352 extern void WXDLLEXPORT wxUninitialize();
353
354 #endif // !wxUSE_GUI
355
356 // ----------------------------------------------------------------------------
357 // macros for dynamic creation of the application object
358 // ----------------------------------------------------------------------------
359
360 // Having a global instance of this class allows wxApp to be aware of the app
361 // creator function. wxApp can then call this function to create a new app
362 // object. Convoluted, but necessary.
363
364 class WXDLLEXPORT wxAppInitializer
365 {
366 public:
367 wxAppInitializer(wxAppInitializerFunction fn)
368 { wxApp::SetInitializerFunction(fn); }
369 };
370
371 // Here's a macro you can use if your compiler really, really wants main() to
372 // be in your main program (e.g. hello.cpp). Now IMPLEMENT_APP should add this
373 // code if required.
374
375 #if !wxUSE_GUI || defined(__WXMOTIF__) || defined(__WXGTK__) || defined(__WXPM__) || (defined(__WXMAC__) && defined(__UNIX__))
376 #define IMPLEMENT_WXWIN_MAIN \
377 extern int wxEntry( int argc, char *argv[] ); \
378 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
379 #elif defined(__WXMSW__) && defined(WXUSINGDLL)
380 // NT defines APIENTRY, 3.x not
381 #if !defined(WXAPIENTRY)
382 #define WXAPIENTRY WXFAR wxSTDCALL
383 #endif
384
385 #include <windows.h>
386 #include "wx/msw/winundef.h"
387
388 #define IMPLEMENT_WXWIN_MAIN \
389 extern "C" int WXAPIENTRY WinMain(HINSTANCE hInstance,\
390 HINSTANCE hPrevInstance,\
391 LPSTR m_lpCmdLine, int nCmdShow)\
392 {\
393 return wxEntry((WXHINSTANCE) hInstance,\
394 (WXHINSTANCE) hPrevInstance,\
395 m_lpCmdLine, nCmdShow);\
396 }
397 #else
398 #define IMPLEMENT_WXWIN_MAIN
399 #endif
400
401 // Use this macro exactly once, the argument is the name of the wxApp-derived
402 // class which is the class of your application.
403 #define IMPLEMENT_APP(appname) \
404 wxApp *wxCreateApp() { return new appname; } \
405 wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
406 appname& wxGetApp() { return *(appname *)wxTheApp; } \
407 IMPLEMENT_WXWIN_MAIN
408
409 // Use this macro if you want to define your own main() or WinMain() function
410 // and call wxEntry() from there.
411 #define IMPLEMENT_APP_NO_MAIN(appname) \
412 wxApp *wxCreateApp() { return new appname; } \
413 wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
414 appname& wxGetApp() { return *(appname *)wxTheApp; }
415
416 #define DECLARE_APP(appname) extern appname& wxGetApp();
417
418 #endif
419 // _WX_APP_H_BASE_