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