]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: app.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6 | // Id: | |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "app.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/app.h" | |
16 | #include "wx/gdicmn.h" | |
17 | #include "wx/utils.h" | |
c801d85f KB |
18 | #include "wx/intl.h" |
19 | #include "wx/log.h" | |
46dc76ba | 20 | #include "wx/memory.h" |
a3622daa VZ |
21 | #include "wx/font.h" |
22 | #include "wx/settings.h" | |
8d71b555 | 23 | #include "wx/resource.h" |
c801d85f KB |
24 | |
25 | #include "unistd.h" | |
26 | ||
47d67540 | 27 | #ifdef wxUSE_GDK_IMLIB |
1f0299c1 | 28 | #include "../gdk_imlib/gdk_imlib.h" |
c801d85f KB |
29 | #endif |
30 | ||
31 | //----------------------------------------------------------------------------- | |
32 | // global data | |
33 | //----------------------------------------------------------------------------- | |
34 | ||
c67daf87 | 35 | wxApp *wxTheApp = (wxApp *) NULL; |
c801d85f KB |
36 | wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL; |
37 | ||
38 | extern wxList wxPendingDelete; | |
a3622daa | 39 | extern wxResourceCache *wxTheResourceCache; |
c801d85f KB |
40 | |
41 | //----------------------------------------------------------------------------- | |
42 | // local functions | |
43 | //----------------------------------------------------------------------------- | |
44 | ||
45 | extern void wxFlushResources(void); | |
46 | ||
47 | //----------------------------------------------------------------------------- | |
48 | // global functions | |
49 | //----------------------------------------------------------------------------- | |
50 | ||
51 | void wxExit(void) | |
52 | { | |
53 | gtk_main_quit(); | |
ff7b1510 | 54 | } |
c801d85f KB |
55 | |
56 | bool wxYield(void) | |
57 | { | |
58 | while (gtk_events_pending() > 0) gtk_main_iteration(); | |
59 | return TRUE; | |
ff7b1510 | 60 | } |
c801d85f KB |
61 | |
62 | //----------------------------------------------------------------------------- | |
63 | // wxApp | |
64 | //----------------------------------------------------------------------------- | |
65 | ||
66 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) | |
67 | ||
53010e52 RR |
68 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) |
69 | EVT_IDLE(wxApp::OnIdle) | |
70 | END_EVENT_TABLE() | |
71 | ||
c801d85f KB |
72 | gint wxapp_idle_callback( gpointer WXUNUSED(data) ) |
73 | { | |
ff7b1510 | 74 | if (wxTheApp) while (wxTheApp->ProcessIdle()) {} |
c801d85f KB |
75 | usleep( 10000 ); |
76 | return TRUE; | |
ff7b1510 | 77 | } |
c801d85f KB |
78 | |
79 | wxApp::wxApp() | |
80 | { | |
81 | m_idleTag = 0; | |
c67daf87 | 82 | m_topWindow = (wxWindow *) NULL; |
c801d85f | 83 | m_exitOnFrameDelete = TRUE; |
0cf2cb36 | 84 | wxTheApp = this; |
ff7b1510 | 85 | } |
c801d85f KB |
86 | |
87 | wxApp::~wxApp(void) | |
88 | { | |
89 | gtk_idle_remove( m_idleTag ); | |
ff7b1510 | 90 | } |
c801d85f KB |
91 | |
92 | bool wxApp::OnInit(void) | |
93 | { | |
94 | return TRUE; | |
ff7b1510 | 95 | } |
c801d85f KB |
96 | |
97 | bool wxApp::OnInitGui(void) | |
0cf2cb36 | 98 | { |
c801d85f | 99 | m_idleTag = gtk_idle_add( wxapp_idle_callback, NULL ); |
0cf2cb36 | 100 | return TRUE; |
ff7b1510 | 101 | } |
c801d85f | 102 | |
0cf2cb36 RD |
103 | int wxApp::OnRun(void) |
104 | { | |
105 | return MainLoop(); | |
ff7b1510 | 106 | } |
c801d85f | 107 | |
53010e52 RR |
108 | bool wxApp::ProcessIdle(void) |
109 | { | |
110 | wxIdleEvent event; | |
111 | event.SetEventObject( this ); | |
112 | ProcessEvent( event ); | |
0cf2cb36 | 113 | |
53010e52 | 114 | return event.MoreRequested(); |
ff7b1510 | 115 | } |
53010e52 RR |
116 | |
117 | void wxApp::OnIdle( wxIdleEvent &event ) | |
c801d85f | 118 | { |
53010e52 RR |
119 | static bool inOnIdle = FALSE; |
120 | ||
121 | // Avoid recursion (via ProcessEvent default case) | |
122 | if (inOnIdle) | |
123 | return; | |
124 | ||
125 | inOnIdle = TRUE; | |
126 | ||
127 | // 'Garbage' collection of windows deleted with Close(). | |
c801d85f | 128 | DeletePendingObjects(); |
53010e52 RR |
129 | |
130 | // flush the logged messages if any | |
131 | wxLog *pLog = wxLog::GetActiveTarget(); | |
132 | if ( pLog != NULL && pLog->HasPendingMessages() ) | |
133 | pLog->Flush(); | |
134 | ||
135 | // Send OnIdle events to all windows | |
136 | bool needMore = SendIdleEvents(); | |
137 | ||
138 | if (needMore) | |
139 | event.RequestMore(TRUE); | |
140 | ||
141 | inOnIdle = FALSE; | |
ff7b1510 | 142 | } |
53010e52 RR |
143 | |
144 | bool wxApp::SendIdleEvents(void) | |
145 | { | |
146 | bool needMore = FALSE; | |
147 | wxNode* node = wxTopLevelWindows.First(); | |
148 | while (node) | |
149 | { | |
150 | wxWindow* win = (wxWindow*) node->Data(); | |
151 | if (SendIdleEvents(win)) | |
152 | needMore = TRUE; | |
153 | ||
154 | node = node->Next(); | |
155 | } | |
156 | return needMore; | |
ff7b1510 | 157 | } |
53010e52 RR |
158 | |
159 | bool wxApp::SendIdleEvents( wxWindow* win ) | |
160 | { | |
161 | bool needMore = FALSE; | |
162 | ||
163 | wxIdleEvent event; | |
164 | event.SetEventObject(win); | |
165 | win->ProcessEvent(event); | |
166 | ||
167 | if (event.MoreRequested()) | |
168 | needMore = TRUE; | |
169 | ||
170 | wxNode* node = win->GetChildren()->First(); | |
171 | while (node) | |
172 | { | |
173 | wxWindow* win = (wxWindow*) node->Data(); | |
174 | if (SendIdleEvents(win)) | |
175 | needMore = TRUE; | |
176 | ||
177 | node = node->Next(); | |
178 | } | |
179 | return needMore ; | |
ff7b1510 | 180 | } |
c801d85f KB |
181 | |
182 | int wxApp::OnExit(void) | |
183 | { | |
184 | return 0; | |
ff7b1510 | 185 | } |
c801d85f KB |
186 | |
187 | int wxApp::MainLoop(void) | |
188 | { | |
189 | gtk_main(); | |
190 | return 0; | |
ff7b1510 | 191 | } |
c801d85f KB |
192 | |
193 | void wxApp::ExitMainLoop(void) | |
194 | { | |
195 | gtk_main_quit(); | |
ff7b1510 | 196 | } |
c801d85f KB |
197 | |
198 | bool wxApp::Initialized(void) | |
199 | { | |
200 | return m_initialized; | |
ff7b1510 | 201 | } |
c801d85f | 202 | |
0cf2cb36 | 203 | bool wxApp::Pending(void) |
c801d85f KB |
204 | { |
205 | return FALSE; | |
ff7b1510 | 206 | } |
c801d85f | 207 | |
0cf2cb36 | 208 | void wxApp::Dispatch(void) |
c801d85f | 209 | { |
ff7b1510 | 210 | } |
c801d85f KB |
211 | |
212 | void wxApp::DeletePendingObjects(void) | |
213 | { | |
214 | wxNode *node = wxPendingDelete.First(); | |
215 | while (node) | |
216 | { | |
217 | wxObject *obj = (wxObject *)node->Data(); | |
0cf2cb36 | 218 | |
c801d85f KB |
219 | delete obj; |
220 | ||
221 | if (wxPendingDelete.Member(obj)) | |
222 | delete node; | |
223 | ||
224 | node = wxPendingDelete.First(); | |
ff7b1510 RR |
225 | } |
226 | } | |
c801d85f KB |
227 | |
228 | wxWindow *wxApp::GetTopWindow(void) | |
229 | { | |
230 | if (m_topWindow) return m_topWindow; | |
231 | wxNode *node = wxTopLevelWindows.First(); | |
c67daf87 | 232 | if (!node) return (wxWindow *) NULL; |
c801d85f | 233 | return (wxWindow*)node->Data(); |
ff7b1510 | 234 | } |
c801d85f KB |
235 | |
236 | void wxApp::SetTopWindow( wxWindow *win ) | |
237 | { | |
238 | m_topWindow = win; | |
ff7b1510 | 239 | } |
c801d85f KB |
240 | |
241 | void wxApp::CommonInit(void) | |
242 | { | |
243 | ||
244 | /* | |
47d67540 | 245 | #if wxUSE_RESOURCES |
c801d85f KB |
246 | (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion); |
247 | #endif | |
248 | */ | |
a3622daa VZ |
249 | wxSystemSettings::Init(); |
250 | wxTheResourceCache = new wxResourceCache(wxKEY_STRING); | |
251 | ||
252 | wxTheFontNameDirectory = new wxFontNameDirectory; | |
253 | wxTheFontNameDirectory->Initialize(); | |
c801d85f KB |
254 | |
255 | wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING); | |
256 | wxTheColourDatabase->Initialize(); | |
a3622daa VZ |
257 | |
258 | wxInitializeStockLists(); | |
c801d85f KB |
259 | wxInitializeStockObjects(); |
260 | ||
8d71b555 | 261 | wxInitializeResourceSystem(); |
0cf2cb36 | 262 | |
c801d85f | 263 | // For PostScript printing |
47d67540 | 264 | #if wxUSE_POSTSCRIPT |
dfad0599 | 265 | /* Now done in wxPostScriptModule |
c801d85f KB |
266 | wxInitializePrintSetupData(); |
267 | wxThePrintPaperDatabase = new wxPrintPaperDatabase; | |
268 | wxThePrintPaperDatabase->CreateDatabase(); | |
dfad0599 | 269 | */ |
c801d85f KB |
270 | #endif |
271 | ||
272 | ||
273 | /* | |
274 | wxBitmap::InitStandardHandlers(); | |
275 | ||
276 | g_globalCursor = new wxCursor; | |
277 | */ | |
ff7b1510 | 278 | } |
c801d85f KB |
279 | |
280 | void wxApp::CommonCleanUp(void) | |
281 | { | |
a3622daa | 282 | wxDELETE(wxTheColourDatabase); |
dfad0599 | 283 | /* Now done in wxPostScriptModule |
a3622daa VZ |
284 | wxDELETE(wxThePrintPaperDatabase); |
285 | wxDELETE(wxThePrintSetupData); | |
dfad0599 | 286 | */ |
a3622daa | 287 | wxDELETE(wxTheFontNameDirectory); |
c801d85f | 288 | wxDeleteStockObjects(); |
0cf2cb36 | 289 | |
c801d85f | 290 | wxFlushResources(); |
a3622daa VZ |
291 | |
292 | wxDELETE(wxTheResourceCache); | |
293 | ||
294 | wxDeleteStockLists(); | |
295 | ||
8d71b555 | 296 | wxCleanUpResourceSystem(); |
0cf2cb36 | 297 | |
a3622daa | 298 | wxSystemSettings::Done(); |
ff7b1510 | 299 | } |
0cf2cb36 | 300 | |
c801d85f KB |
301 | wxLog *wxApp::CreateLogTarget() |
302 | { | |
303 | return new wxLogGui; | |
304 | } | |
305 | ||
306 | //----------------------------------------------------------------------------- | |
307 | // wxEntry | |
308 | //----------------------------------------------------------------------------- | |
309 | ||
310 | int wxEntry( int argc, char *argv[] ) | |
311 | { | |
312 | wxBuffer = new char[BUFSIZ + 512]; | |
313 | ||
314 | wxClassInfo::InitializeClasses(); | |
0cf2cb36 | 315 | |
47d67540 | 316 | #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
46dc76ba | 317 | |
46dc76ba | 318 | streambuf* sBuf = new wxDebugStreamBuf; |
46dc76ba RR |
319 | ostream* oStr = new ostream(sBuf) ; |
320 | wxDebugContext::SetStream(oStr, sBuf); | |
321 | ||
322 | #endif | |
0cf2cb36 | 323 | |
c801d85f KB |
324 | if (!wxTheApp) |
325 | { | |
326 | if (!wxApp::GetInitializerFunction()) | |
327 | { | |
8429bec1 | 328 | printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" ); |
c801d85f | 329 | return 0; |
ff7b1510 | 330 | } |
0cf2cb36 | 331 | |
c801d85f | 332 | wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction(); |
0cf2cb36 | 333 | |
c801d85f | 334 | wxObject *test_app = app_ini(); |
0cf2cb36 | 335 | |
c801d85f | 336 | wxTheApp = (wxApp*) test_app; |
ff7b1510 | 337 | } |
0cf2cb36 RD |
338 | |
339 | if (!wxTheApp) | |
c801d85f | 340 | { |
2f2aa628 | 341 | printf( "wxWindows error: wxTheApp == NULL\n" ); |
c801d85f | 342 | return 0; |
ff7b1510 | 343 | } |
c801d85f | 344 | |
c801d85f KB |
345 | wxTheApp->argc = argc; |
346 | wxTheApp->argv = argv; | |
0cf2cb36 | 347 | |
e55ad60e RR |
348 | char name[200]; |
349 | strcpy( name, argv[0] ); | |
350 | strcpy( name, wxFileNameFromPath(name) ); | |
351 | wxStripExtension( name ); | |
352 | wxTheApp->SetAppName( name ); | |
353 | ||
edaa81ae RR |
354 | gtk_set_locale(); |
355 | ||
c801d85f KB |
356 | gtk_init( &argc, &argv ); |
357 | ||
47d67540 | 358 | #ifdef wxUSE_GDK_IMLIB |
c801d85f KB |
359 | |
360 | gdk_imlib_init(); | |
0cf2cb36 | 361 | |
c801d85f | 362 | gtk_widget_push_visual(gdk_imlib_get_visual()); |
0cf2cb36 | 363 | |
c801d85f | 364 | gtk_widget_push_colormap(gdk_imlib_get_colormap()); |
0cf2cb36 RD |
365 | |
366 | #endif | |
367 | ||
c801d85f KB |
368 | wxApp::CommonInit(); |
369 | ||
370 | wxTheApp->OnInitGui(); | |
371 | ||
372 | // Here frames insert themselves automatically | |
373 | // into wxTopLevelWindows by getting created | |
374 | // in OnInit(). | |
0cf2cb36 | 375 | |
c801d85f KB |
376 | if (!wxTheApp->OnInit()) return 0; |
377 | ||
378 | wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0); | |
0cf2cb36 | 379 | |
c801d85f | 380 | int retValue = 0; |
0cf2cb36 | 381 | |
c801d85f | 382 | if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun(); |
0cf2cb36 RD |
383 | |
384 | wxTheApp->DeletePendingObjects(); | |
385 | ||
c801d85f | 386 | wxTheApp->OnExit(); |
0cf2cb36 | 387 | |
c801d85f | 388 | wxApp::CommonCleanUp(); |
a3622daa VZ |
389 | |
390 | wxDELETE(wxTheApp); | |
0cf2cb36 | 391 | |
e55ad60e RR |
392 | wxLog *oldLog = wxLog::SetActiveTarget( NULL ); |
393 | if (oldLog) delete oldLog; | |
394 | ||
395 | wxClassInfo::CleanUpClasses(); | |
396 | ||
397 | delete[] wxBuffer; | |
398 | ||
47d67540 | 399 | #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
e55ad60e | 400 | |
46dc76ba RR |
401 | if (wxDebugContext::CountObjectsLeft() > 0) |
402 | { | |
403 | wxTrace("There were memory leaks.\n"); | |
404 | wxDebugContext::Dump(); | |
405 | wxDebugContext::PrintStatistics(); | |
406 | } | |
407 | wxDebugContext::SetStream(NULL, NULL); | |
e55ad60e | 408 | |
46dc76ba | 409 | #endif |
0cf2cb36 | 410 | |
c801d85f | 411 | return retValue; |
ff7b1510 | 412 | } |
1a56f55c RR |
413 | |
414 | //----------------------------------------------------------------------------- | |
415 | // main() | |
416 | //----------------------------------------------------------------------------- | |
417 | ||
0cf2cb36 | 418 | #if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN) |
1a56f55c RR |
419 | |
420 | // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h | |
421 | ||
422 | #else | |
423 | ||
424 | int main(int argc, char *argv[]) { return wxEntry(argc, argv); } | |
425 | ||
426 | #endif | |
427 | ||
428 |