]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: app.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling, Julian Smart | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "app.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/app.h" | |
15 | #include "wx/gdicmn.h" | |
16 | #include "wx/utils.h" | |
17 | #include "wx/intl.h" | |
18 | #include "wx/log.h" | |
19 | #include "wx/memory.h" | |
20 | #include "wx/font.h" | |
21 | #include "wx/settings.h" | |
22 | #include "wx/resource.h" | |
23 | ||
24 | #include "unistd.h" | |
25 | ||
26 | //----------------------------------------------------------------------------- | |
27 | // global data | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | wxApp *wxTheApp = (wxApp *) NULL; | |
31 | wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL; | |
32 | ||
33 | extern wxList wxPendingDelete; | |
34 | extern wxResourceCache *wxTheResourceCache; | |
35 | ||
36 | unsigned char g_palette[64*3] = | |
37 | { | |
38 | 0x0, 0x0, 0x0, | |
39 | 0xff, 0xff, 0xff, | |
40 | 0xff, 0x0, 0x0, | |
41 | 0xff, 0xff, 0x0, | |
42 | 0x0, 0xff, 0x0, | |
43 | 0x0, 0x0, 0xff, | |
44 | 0x0, 0xff, 0xff, | |
45 | 0x99, 0x99, 0x99, | |
46 | 0xff, 0x88, 0x0, | |
47 | 0x88, 0x0, 0x0, | |
48 | 0x0, 0x88, 0x88, | |
49 | 0x88, 0x88, 0x0, | |
50 | 0xff, 0xcc, 0x97, | |
51 | 0xbb, 0xbb, 0xbb, | |
52 | 0x9f, 0x6b, 0x42, | |
53 | 0x55, 0x55, 0x55, | |
54 | 0xdd, 0xdd, 0xdd, | |
55 | 0x77, 0x77, 0x77, | |
56 | 0x33, 0x33, 0x33, | |
57 | 0xcc, 0x0, 0x0, | |
58 | 0xff, 0x44, 0x0, | |
59 | 0xff, 0xcc, 0x0, | |
60 | 0xcc, 0xcc, 0x0, | |
61 | 0x60, 0x60, 0x0, | |
62 | 0x0, 0x43, 0x0, | |
63 | 0x0, 0x7f, 0x0, | |
64 | 0x0, 0xcc, 0x0, | |
65 | 0x0, 0x44, 0x44, | |
66 | 0x0, 0x0, 0x44, | |
67 | 0x0, 0x0, 0x88, | |
68 | 0xef, 0xb1, 0x7b, | |
69 | 0xdf, 0x98, 0x5f, | |
70 | 0xbf, 0x87, 0x56, | |
71 | 0x7f, 0x57, 0x26, | |
72 | 0x5f, 0x39, 0xc, | |
73 | 0x3f, 0x1c, 0x0, | |
74 | 0x21, 0x0, 0x0, | |
75 | 0x0, 0x43, 0x87, | |
76 | 0x2d, 0x70, 0xaf, | |
77 | 0x5a, 0x9e, 0xd7, | |
78 | 0x87, 0xcc, 0xff, | |
79 | 0xff, 0xe0, 0xba, | |
80 | 0x21, 0x43, 0xf, | |
81 | 0x3d, 0x5d, 0x25, | |
82 | 0x59, 0x78, 0x3a, | |
83 | 0x75, 0x93, 0x4f, | |
84 | 0x91, 0xae, 0x64, | |
85 | 0xad, 0xc8, 0x7a, | |
86 | 0xf0, 0xa8, 0xef, | |
87 | 0xd0, 0x88, 0xd0, | |
88 | 0xaf, 0x66, 0xaf, | |
89 | 0x8e, 0x44, 0x8e, | |
90 | 0x6d, 0x22, 0x6d, | |
91 | 0x4b, 0x0, 0x4b, | |
92 | 0xff, 0xc0, 0xbc, | |
93 | 0xff, 0x93, 0x91, | |
94 | 0xff, 0x66, 0x67, | |
95 | 0xd8, 0xf2, 0xbf, | |
96 | 0xff, 0xc9, 0x68, | |
97 | 0xff, 0x96, 0x67, | |
98 | 0xa5, 0x60, 0xff, | |
99 | 0x51, 0xff, 0x99, | |
100 | 0x3f, 0xa5, 0x63, | |
101 | 0x98, 0x90, 0x67 | |
102 | }; | |
103 | ||
104 | //----------------------------------------------------------------------------- | |
105 | // local functions | |
106 | //----------------------------------------------------------------------------- | |
107 | ||
108 | extern void wxFlushResources(void); | |
109 | ||
110 | //----------------------------------------------------------------------------- | |
111 | // global functions | |
112 | //----------------------------------------------------------------------------- | |
113 | ||
114 | void wxExit(void) | |
115 | { | |
116 | gtk_main_quit(); | |
117 | } | |
118 | ||
119 | bool wxYield(void) | |
120 | { | |
121 | while (gtk_events_pending() > 0) gtk_main_iteration(); | |
122 | return TRUE; | |
123 | } | |
124 | ||
125 | //----------------------------------------------------------------------------- | |
126 | // wxApp | |
127 | //----------------------------------------------------------------------------- | |
128 | ||
129 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) | |
130 | ||
131 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
132 | EVT_IDLE(wxApp::OnIdle) | |
133 | END_EVENT_TABLE() | |
134 | ||
135 | gint wxapp_idle_callback( gpointer WXUNUSED(data) ) | |
136 | { | |
137 | if (wxTheApp) while (wxTheApp->ProcessIdle()) {} | |
138 | usleep( 10000 ); | |
139 | return TRUE; | |
140 | } | |
141 | ||
142 | wxApp::wxApp() | |
143 | { | |
144 | m_idleTag = 0; | |
145 | m_topWindow = (wxWindow *) NULL; | |
146 | m_exitOnFrameDelete = TRUE; | |
147 | wxTheApp = this; | |
148 | } | |
149 | ||
150 | wxApp::~wxApp(void) | |
151 | { | |
152 | gtk_idle_remove( m_idleTag ); | |
153 | } | |
154 | ||
155 | bool wxApp::OnInit(void) | |
156 | { | |
157 | return TRUE; | |
158 | } | |
159 | ||
160 | bool wxApp::OnInitGui(void) | |
161 | { | |
162 | m_idleTag = gtk_idle_add( wxapp_idle_callback, NULL ); | |
163 | return TRUE; | |
164 | } | |
165 | ||
166 | int wxApp::OnRun(void) | |
167 | { | |
168 | return MainLoop(); | |
169 | } | |
170 | ||
171 | bool wxApp::ProcessIdle(void) | |
172 | { | |
173 | wxIdleEvent event; | |
174 | event.SetEventObject( this ); | |
175 | ProcessEvent( event ); | |
176 | ||
177 | return event.MoreRequested(); | |
178 | } | |
179 | ||
180 | void wxApp::OnIdle( wxIdleEvent &event ) | |
181 | { | |
182 | static bool inOnIdle = FALSE; | |
183 | ||
184 | // Avoid recursion (via ProcessEvent default case) | |
185 | if (inOnIdle) | |
186 | return; | |
187 | ||
188 | inOnIdle = TRUE; | |
189 | ||
190 | // 'Garbage' collection of windows deleted with Close(). | |
191 | DeletePendingObjects(); | |
192 | ||
193 | // flush the logged messages if any | |
194 | wxLog *pLog = wxLog::GetActiveTarget(); | |
195 | if ( pLog != NULL && pLog->HasPendingMessages() ) | |
196 | pLog->Flush(); | |
197 | ||
198 | // Send OnIdle events to all windows | |
199 | bool needMore = SendIdleEvents(); | |
200 | ||
201 | if (needMore) | |
202 | event.RequestMore(TRUE); | |
203 | ||
204 | inOnIdle = FALSE; | |
205 | } | |
206 | ||
207 | bool wxApp::SendIdleEvents(void) | |
208 | { | |
209 | bool needMore = FALSE; | |
210 | wxNode* node = wxTopLevelWindows.First(); | |
211 | while (node) | |
212 | { | |
213 | wxWindow* win = (wxWindow*) node->Data(); | |
214 | if (SendIdleEvents(win)) | |
215 | needMore = TRUE; | |
216 | ||
217 | node = node->Next(); | |
218 | } | |
219 | return needMore; | |
220 | } | |
221 | ||
222 | bool wxApp::SendIdleEvents( wxWindow* win ) | |
223 | { | |
224 | bool needMore = FALSE; | |
225 | ||
226 | wxIdleEvent event; | |
227 | event.SetEventObject(win); | |
228 | win->ProcessEvent(event); | |
229 | ||
230 | if (event.MoreRequested()) | |
231 | needMore = TRUE; | |
232 | ||
233 | wxNode* node = win->GetChildren()->First(); | |
234 | while (node) | |
235 | { | |
236 | wxWindow* win = (wxWindow*) node->Data(); | |
237 | if (SendIdleEvents(win)) | |
238 | needMore = TRUE; | |
239 | ||
240 | node = node->Next(); | |
241 | } | |
242 | return needMore ; | |
243 | } | |
244 | ||
245 | int wxApp::OnExit(void) | |
246 | { | |
247 | return 0; | |
248 | } | |
249 | ||
250 | int wxApp::MainLoop(void) | |
251 | { | |
252 | gtk_main(); | |
253 | return 0; | |
254 | } | |
255 | ||
256 | void wxApp::ExitMainLoop(void) | |
257 | { | |
258 | gtk_main_quit(); | |
259 | } | |
260 | ||
261 | bool wxApp::Initialized(void) | |
262 | { | |
263 | return m_initialized; | |
264 | } | |
265 | ||
266 | bool wxApp::Pending(void) | |
267 | { | |
268 | return FALSE; | |
269 | } | |
270 | ||
271 | void wxApp::Dispatch(void) | |
272 | { | |
273 | } | |
274 | ||
275 | void wxApp::DeletePendingObjects(void) | |
276 | { | |
277 | wxNode *node = wxPendingDelete.First(); | |
278 | while (node) | |
279 | { | |
280 | wxObject *obj = (wxObject *)node->Data(); | |
281 | ||
282 | delete obj; | |
283 | ||
284 | if (wxPendingDelete.Member(obj)) | |
285 | delete node; | |
286 | ||
287 | node = wxPendingDelete.First(); | |
288 | } | |
289 | } | |
290 | ||
291 | wxWindow *wxApp::GetTopWindow(void) | |
292 | { | |
293 | if (m_topWindow) return m_topWindow; | |
294 | wxNode *node = wxTopLevelWindows.First(); | |
295 | if (!node) return (wxWindow *) NULL; | |
296 | return (wxWindow*)node->Data(); | |
297 | } | |
298 | ||
299 | void wxApp::SetTopWindow( wxWindow *win ) | |
300 | { | |
301 | m_topWindow = win; | |
302 | } | |
303 | ||
304 | void wxApp::CommonInit(void) | |
305 | { | |
306 | ||
307 | /* | |
308 | #if wxUSE_RESOURCES | |
309 | (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion); | |
310 | #endif | |
311 | */ | |
312 | wxSystemSettings::Init(); | |
313 | wxTheResourceCache = new wxResourceCache(wxKEY_STRING); | |
314 | ||
315 | wxTheFontNameDirectory = new wxFontNameDirectory; | |
316 | wxTheFontNameDirectory->Initialize(); | |
317 | ||
318 | wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING); | |
319 | wxTheColourDatabase->Initialize(); | |
320 | ||
321 | wxInitializeStockLists(); | |
322 | wxInitializeStockObjects(); | |
323 | ||
324 | wxInitializeResourceSystem(); | |
325 | ||
326 | // For PostScript printing | |
327 | #if wxUSE_POSTSCRIPT | |
328 | /* Now done in wxPostScriptModule | |
329 | wxInitializePrintSetupData(); | |
330 | wxThePrintPaperDatabase = new wxPrintPaperDatabase; | |
331 | wxThePrintPaperDatabase->CreateDatabase(); | |
332 | */ | |
333 | #endif | |
334 | ||
335 | ||
336 | /* | |
337 | wxBitmap::InitStandardHandlers(); | |
338 | ||
339 | g_globalCursor = new wxCursor; | |
340 | */ | |
341 | } | |
342 | ||
343 | void wxApp::CommonCleanUp(void) | |
344 | { | |
345 | wxDELETE(wxTheColourDatabase); | |
346 | /* Now done in wxPostScriptModule | |
347 | wxDELETE(wxThePrintPaperDatabase); | |
348 | wxDELETE(wxThePrintSetupData); | |
349 | */ | |
350 | wxDELETE(wxTheFontNameDirectory); | |
351 | wxDeleteStockObjects(); | |
352 | ||
353 | wxFlushResources(); | |
354 | ||
355 | wxDELETE(wxTheResourceCache); | |
356 | ||
357 | wxDeleteStockLists(); | |
358 | ||
359 | wxCleanUpResourceSystem(); | |
360 | ||
361 | wxSystemSettings::Done(); | |
362 | } | |
363 | ||
364 | wxLog *wxApp::CreateLogTarget() | |
365 | { | |
366 | return new wxLogGui; | |
367 | } | |
368 | ||
369 | //----------------------------------------------------------------------------- | |
370 | // wxEntry | |
371 | //----------------------------------------------------------------------------- | |
372 | ||
373 | int wxEntry( int argc, char *argv[] ) | |
374 | { | |
375 | wxBuffer = new char[BUFSIZ + 512]; | |
376 | ||
377 | wxClassInfo::InitializeClasses(); | |
378 | ||
379 | #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT | |
380 | ||
381 | streambuf* sBuf = new wxDebugStreamBuf; | |
382 | ostream* oStr = new ostream(sBuf) ; | |
383 | wxDebugContext::SetStream(oStr, sBuf); | |
384 | ||
385 | #endif | |
386 | ||
387 | if (!wxTheApp) | |
388 | { | |
389 | if (!wxApp::GetInitializerFunction()) | |
390 | { | |
391 | printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" ); | |
392 | return 0; | |
393 | } | |
394 | ||
395 | wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction(); | |
396 | ||
397 | wxObject *test_app = app_ini(); | |
398 | ||
399 | wxTheApp = (wxApp*) test_app; | |
400 | } | |
401 | ||
402 | if (!wxTheApp) | |
403 | { | |
404 | printf( "wxWindows error: wxTheApp == NULL\n" ); | |
405 | return 0; | |
406 | } | |
407 | ||
408 | wxTheApp->argc = argc; | |
409 | wxTheApp->argv = argv; | |
410 | ||
411 | char name[200]; | |
412 | strcpy( name, argv[0] ); | |
413 | strcpy( name, wxFileNameFromPath(name) ); | |
414 | wxStripExtension( name ); | |
415 | wxTheApp->SetAppName( name ); | |
416 | ||
417 | gtk_set_locale(); | |
418 | ||
419 | gtk_init( &argc, &argv ); | |
420 | ||
421 | GdkColormap *cmap = gdk_colormap_new( gdk_visual_get_system(), TRUE ); | |
422 | ||
423 | for (int i = 0; i < 64; i++) | |
424 | { | |
425 | GdkColor col; | |
426 | col.red = g_palette[i*3 + 0] << 8; | |
427 | col.green = g_palette[i*3 + 1] << 8; | |
428 | col.blue = g_palette[i*3 + 2] << 8; | |
429 | col.pixel = 0; | |
430 | ||
431 | gdk_color_alloc( cmap, &col ); | |
432 | } | |
433 | ||
434 | gtk_widget_push_colormap( cmap ); | |
435 | ||
436 | gtk_widget_set_default_colormap( cmap ); | |
437 | ||
438 | wxApp::CommonInit(); | |
439 | ||
440 | wxTheApp->OnInitGui(); | |
441 | ||
442 | // Here frames insert themselves automatically | |
443 | // into wxTopLevelWindows by getting created | |
444 | // in OnInit(). | |
445 | ||
446 | if (!wxTheApp->OnInit()) return 0; | |
447 | ||
448 | wxTheApp->m_initialized = (wxTopLevelWindows.Number() > 0); | |
449 | ||
450 | int retValue = 0; | |
451 | ||
452 | if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun(); | |
453 | ||
454 | wxTheApp->DeletePendingObjects(); | |
455 | ||
456 | wxTheApp->OnExit(); | |
457 | ||
458 | wxApp::CommonCleanUp(); | |
459 | ||
460 | wxDELETE(wxTheApp); | |
461 | ||
462 | wxLog *oldLog = wxLog::SetActiveTarget( NULL ); | |
463 | if (oldLog) delete oldLog; | |
464 | ||
465 | wxClassInfo::CleanUpClasses(); | |
466 | ||
467 | delete[] wxBuffer; | |
468 | ||
469 | #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT | |
470 | ||
471 | if (wxDebugContext::CountObjectsLeft() > 0) | |
472 | { | |
473 | wxTrace("There were memory leaks.\n"); | |
474 | wxDebugContext::Dump(); | |
475 | wxDebugContext::PrintStatistics(); | |
476 | } | |
477 | wxDebugContext::SetStream(NULL, NULL); | |
478 | ||
479 | #endif | |
480 | ||
481 | return retValue; | |
482 | } | |
483 | ||
484 | //----------------------------------------------------------------------------- | |
485 | // main() | |
486 | //----------------------------------------------------------------------------- | |
487 | ||
488 | #if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN) | |
489 | ||
490 | // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h | |
491 | ||
492 | #else | |
493 | ||
494 | int main(int argc, char *argv[]) { return wxEntry(argc, argv); } | |
495 | ||
496 | #endif | |
497 | ||
498 |