]> git.saurik.com Git - wxWidgets.git/blame - include/wx/app.h
another attempt to fix wxPanel/wxFrame::m_winLastFocused handling
[wxWidgets.git] / include / wx / app.h
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.h
094637f6
VZ
3// Purpose: wxAppBase class and macros used for declaration of wxApp
4// derived class in the user code
c801d85f
KB
5// Author: Julian Smart
6// Modified by:
7// Created: 01/02/97
8// RCS-ID: $Id$
9// Copyright: (c) Julian Smart and Markus Holzem
094637f6 10// Licence: wxWindows licence
c801d85f
KB
11/////////////////////////////////////////////////////////////////////////////
12
34138703
JS
13#ifndef _WX_APP_H_BASE_
14#define _WX_APP_H_BASE_
c801d85f 15
094637f6
VZ
16#ifdef __GNUG__
17 #pragma interface "appbase.h"
10b959e3 18#endif
c801d85f 19
094637f6
VZ
20// ----------------------------------------------------------------------------
21// typedefs
22// ----------------------------------------------------------------------------
23
75acb532 24#if defined(__WXMSW__) || defined (__WXPM__)
094637f6
VZ
25 class WXDLLEXPORT wxApp;
26 typedef wxApp* (*wxAppInitializerFunction)();
27#else
28 // returning wxApp* won't work with gcc
29 #include "wx/object.h"
c801d85f 30
094637f6 31 typedef wxObject* (*wxAppInitializerFunction)();
10b959e3 32#endif
c801d85f 33
23280650
VZ
34// ----------------------------------------------------------------------------
35// headers we have to include here
36// ----------------------------------------------------------------------------
37
38#include "wx/event.h" // for the base class
39
e90c1d2a
VZ
40#if wxUSE_GUI
41 #include "wx/window.h" // for wxTopLevelWindows
42#endif // wxUSE_GUI
23280650
VZ
43
44#if wxUSE_LOG
45 #include "wx/log.h"
46#endif
47
094637f6
VZ
48// ----------------------------------------------------------------------------
49// constants
50// ----------------------------------------------------------------------------
51
52static const int wxPRINT_WINDOWS = 1;
53static const int wxPRINT_POSTSCRIPT = 2;
54
55// ----------------------------------------------------------------------------
56// the common part of wxApp implementations for all platforms
57// ----------------------------------------------------------------------------
58
59class WXDLLEXPORT wxAppBase : public wxEvtHandler
60{
61public:
62 // the virtual functions which may/must be overridden in the derived class
63 // -----------------------------------------------------------------------
03e11df5
GD
64#ifdef __WXMAC_X__
65 virtual ~wxAppBase() {} // Added min for Mac X
66#endif
094637f6
VZ
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 //
e90c1d2a
VZ
71 // Override: always in GUI application, rarely in console ones.
72#if wxUSE_GUI
094637f6 73 virtual bool OnInit() { return FALSE; };
e90c1d2a
VZ
74#else // !GUI
75 virtual bool OnInit() { return TRUE; };
76#endif // wxUSE_GUI
094637f6 77
e90c1d2a 78#if wxUSE_GUI
094637f6
VZ
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; }
e90c1d2a 84#endif // wxUSE_GUI
094637f6
VZ
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
e90c1d2a
VZ
89 // ExitMainLoop() is called. In console mode programs, the execution
90 // of the program really starts here
094637f6 91 //
e90c1d2a
VZ
92 // Override: rarely in GUI applications, always in console ones.
93#if wxUSE_GUI
094637f6 94 virtual int OnRun() { return MainLoop(); };
e90c1d2a
VZ
95#else // !GUI
96 virtual int OnRun() = 0;
97#endif // wxUSE_GUI
094637f6
VZ
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.
7beba2fc 105 virtual int OnExit();
094637f6
VZ
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
e90c1d2a 120#if wxUSE_GUI
094637f6
VZ
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;
e90c1d2a 137#endif // wxUSE_GUI
094637f6
VZ
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
e90c1d2a 164#if wxUSE_GUI
094637f6
VZ
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)
83a5b533 174 virtual wxWindow *GetTopWindow() const
094637f6
VZ
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
e90c1d2a
VZ
192#endif // wxUSE_GUI
193
094637f6
VZ
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.
e90c1d2a
VZ
202 virtual wxLog *CreateLogTarget()
203#if wxUSE_GUI
204 { return new wxLogGui; }
205#else // !GUI
206 { return new wxLogStderr; }
207#endif // wxUSE_GUI
094637f6
VZ
208#endif // wxUSE_LOG
209
e90c1d2a 210#if wxUSE_GUI
094637f6
VZ
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
8480b297
RR
220 // set use of best visual flag (see below)
221 void SetUseBestVisual( bool flag ) { m_useBestVisual = flag; }
222 bool GetUseBestVisual() const { return m_useBestVisual; }
75acb532 223
094637f6
VZ
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; }
e90c1d2a 230#endif // wxUSE_GUI
094637f6
VZ
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
72cdf4c9
VZ
241 // process all events in the wxPendingEvents list
242 virtual void ProcessPendingEvents();
243
094637f6 244 // access to the command line arguments
c1b03ce8
OK
245 int argc;
246 wxChar **argv;
094637f6
VZ
247
248//private:
249protected:
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;
75acb532 263
8480b297
RR
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;
094637f6 267
e90c1d2a 268#if wxUSE_GUI
094637f6
VZ
269 // the main top level window - may be NULL
270 wxWindow *m_topWindow;
e90c1d2a 271#endif // wxUSE_GUI
094637f6
VZ
272};
273
274// ----------------------------------------------------------------------------
275// now include the declaration of the real class
276// ----------------------------------------------------------------------------
277
e90c1d2a
VZ
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
b568d04f
VZ
295 // can't use typedef because wxApp forward declared as a class
296 class WXDLLEXPORT wxApp : public wxAppBase
297 {
298 };
e90c1d2a 299#endif // GUI/!GUI
c801d85f 300
094637f6 301// ----------------------------------------------------------------------------
ee31c392
VZ
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)
309WXDLLEXPORT_DATA(extern wxApp*) wxTheApp;
310
311// ----------------------------------------------------------------------------
312// global functions
313// ----------------------------------------------------------------------------
314
e90c1d2a
VZ
315// event loop related functions only work in GUI programs
316// ------------------------------------------------------
317
ee31c392 318// Force an exit from main loop
e90c1d2a 319extern void WXDLLEXPORT wxExit();
ee31c392
VZ
320
321// Yield to other apps/messages
e90c1d2a
VZ
322extern bool WXDLLEXPORT wxYield();
323
bf9e3e73
RR
324// Yield to other apps/messages
325extern void WXDLLEXPORT wxWakeUpIdle();
bf9e3e73 326
8e193f38
VZ
327// Post a message to the given eventhandler which will be processed during the
328// next event loop iteration
6d56eb5c 329inline void wxPostEvent(wxEvtHandler *dest, wxEvent& event)
8e193f38
VZ
330{
331 wxCHECK_RET( dest, wxT("need an object to post event to in wxPostEvent") );
332
52a07708 333#if wxUSE_GUI
8e193f38 334 dest->AddPendingEvent(event);
52a07708
VZ
335#else
336 dest->ProcessEvent(event);
b568d04f 337#endif // wxUSE_GUI
52a07708 338}
e90c1d2a
VZ
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
b568d04f 344#if !wxUSE_GUI
e90c1d2a
VZ
345
346// initialize the library (may be called as many times as needed, but each
347// call to wxInitialize() must be matched by wxUninitialize())
348extern bool WXDLLEXPORT wxInitialize();
349
350// clean up - the library can't be used any more after the last call to
351// wxUninitialize()
352extern void WXDLLEXPORT wxUninitialize();
353
b568d04f 354#endif // !wxUSE_GUI
ee31c392
VZ
355
356// ----------------------------------------------------------------------------
094637f6
VZ
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.
c801d85f
KB
363
364class WXDLLEXPORT wxAppInitializer
365{
366public:
094637f6
VZ
367 wxAppInitializer(wxAppInitializerFunction fn)
368 { wxApp::SetInitializerFunction(fn); }
c801d85f
KB
369};
370
094637f6
VZ
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.
26a87b69 374
d5408e98 375#if !wxUSE_GUI || defined(__WXMOTIF__) || defined(__WXGTK__) || defined(__WXPM__)
094637f6
VZ
376 #define IMPLEMENT_WXWIN_MAIN \
377 extern int wxEntry( int argc, char *argv[] ); \
378 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
d5408e98
GD
379#elif defined(__WXMAC__) && defined(__UNIX__)
380 // wxMac seems to have a specific wxEntry prototype
381 #define IMPLEMENT_WXWIN_MAIN \
382 extern int wxEntry( int argc, char *argv[], bool enterLoop = 1 ); \
383 int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
750b78ba 384#elif defined(__WXMSW__) && defined(WXUSINGDLL)
094637f6
VZ
385 // NT defines APIENTRY, 3.x not
386 #if !defined(WXAPIENTRY)
790ad94f 387 #define WXAPIENTRY WXFAR wxSTDCALL
094637f6 388 #endif
750b78ba 389
f6bcfd97
BP
390 #include <windows.h>
391 #include "wx/msw/winundef.h"
392
393 #define IMPLEMENT_WXWIN_MAIN \
394 extern "C" int WXAPIENTRY WinMain(HINSTANCE hInstance,\
395 HINSTANCE hPrevInstance,\
396 LPSTR m_lpCmdLine, int nCmdShow)\
397 {\
398 return wxEntry((WXHINSTANCE) hInstance,\
399 (WXHINSTANCE) hPrevInstance,\
400 m_lpCmdLine, nCmdShow);\
401 }
26a87b69 402#else
094637f6 403 #define IMPLEMENT_WXWIN_MAIN
26a87b69
JS
404#endif
405
c661ecca
RR
406// Use this macro exactly once, the argument is the name of the wxApp-derived
407// class which is the class of your application.
408#define IMPLEMENT_APP(appname) \
409 wxApp *wxCreateApp() { return new appname; } \
094637f6
VZ
410 wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
411 appname& wxGetApp() { return *(appname *)wxTheApp; } \
412 IMPLEMENT_WXWIN_MAIN
c801d85f 413
c661ecca
RR
414// Use this macro if you want to define your own main() or WinMain() function
415// and call wxEntry() from there.
416#define IMPLEMENT_APP_NO_MAIN(appname) \
417 wxApp *wxCreateApp() { return new appname; } \
418 wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
419 appname& wxGetApp() { return *(appname *)wxTheApp; }
420
094637f6 421#define DECLARE_APP(appname) extern appname& wxGetApp();
c801d85f
KB
422
423#endif
34138703 424 // _WX_APP_H_BASE_