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