]>
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/dialog.h" | |
23 | ||
24 | #if wxUSE_WX_RESOURCES | |
25 | #include "wx/resource.h" | |
26 | #endif | |
27 | ||
28 | #include "wx/module.h" | |
29 | #include "wx/image.h" | |
30 | ||
31 | #ifdef __WXUNIVERSAL__ | |
32 | #include "wx/univ/theme.h" | |
33 | #include "wx/univ/renderer.h" | |
34 | #endif | |
35 | ||
36 | #if wxUSE_THREADS | |
37 | #include "wx/thread.h" | |
38 | #endif | |
39 | ||
40 | #include <unistd.h> | |
41 | #include <sys/poll.h> | |
42 | #include "wx/gtk/win_gtk.h" | |
43 | ||
44 | #include <gtk/gtk.h> | |
45 | ||
46 | ||
47 | //----------------------------------------------------------------------------- | |
48 | // global data | |
49 | //----------------------------------------------------------------------------- | |
50 | ||
51 | wxApp *wxTheApp = (wxApp *) NULL; | |
52 | wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL; | |
53 | ||
54 | bool g_mainThreadLocked = FALSE; | |
55 | gint g_pendingTag = 0; | |
56 | ||
57 | static GtkWidget *gs_RootWindow = (GtkWidget*) NULL; | |
58 | ||
59 | //----------------------------------------------------------------------------- | |
60 | // idle system | |
61 | //----------------------------------------------------------------------------- | |
62 | ||
63 | extern bool g_isIdle; | |
64 | ||
65 | void wxapp_install_idle_handler(); | |
66 | ||
67 | //----------------------------------------------------------------------------- | |
68 | // wxExit | |
69 | //----------------------------------------------------------------------------- | |
70 | ||
71 | void wxExit() | |
72 | { | |
73 | gtk_main_quit(); | |
74 | } | |
75 | ||
76 | //----------------------------------------------------------------------------- | |
77 | // wxYield | |
78 | //----------------------------------------------------------------------------- | |
79 | ||
80 | bool wxApp::Yield(bool onlyIfNeeded) | |
81 | { | |
82 | // MT-FIXME | |
83 | static bool s_inYield = FALSE; | |
84 | ||
85 | if ( s_inYield ) | |
86 | { | |
87 | if ( !onlyIfNeeded ) | |
88 | { | |
89 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
90 | } | |
91 | ||
92 | return FALSE; | |
93 | } | |
94 | ||
95 | #if wxUSE_THREADS | |
96 | if ( !wxThread::IsMain() ) | |
97 | { | |
98 | // can't call gtk_main_iteration() from other threads like this | |
99 | return TRUE; | |
100 | } | |
101 | #endif // wxUSE_THREADS | |
102 | ||
103 | s_inYield = TRUE; | |
104 | ||
105 | if (!g_isIdle) | |
106 | { | |
107 | // We need to remove idle callbacks or the loop will | |
108 | // never finish. | |
109 | gtk_idle_remove( m_idleTag ); | |
110 | m_idleTag = 0; | |
111 | g_isIdle = TRUE; | |
112 | } | |
113 | ||
114 | // disable log flushing from here because a call to wxYield() shouldn't | |
115 | // normally result in message boxes popping up &c | |
116 | wxLog::Suspend(); | |
117 | ||
118 | while (gtk_events_pending()) | |
119 | gtk_main_iteration(); | |
120 | ||
121 | // It's necessary to call ProcessIdle() to update the frames sizes which | |
122 | // might have been changed (it also will update other things set from | |
123 | // OnUpdateUI() which is a nice (and desired) side effect). But we | |
124 | // call ProcessIdle() only once since this is not meant for longish | |
125 | // background jobs (controlled by wxIdleEvent::RequestMore() and the | |
126 | // return value of Processidle(). | |
127 | ProcessIdle(); | |
128 | ||
129 | // let the logs be flashed again | |
130 | wxLog::Resume(); | |
131 | ||
132 | s_inYield = FALSE; | |
133 | ||
134 | return TRUE; | |
135 | } | |
136 | ||
137 | //----------------------------------------------------------------------------- | |
138 | // wxWakeUpIdle | |
139 | //----------------------------------------------------------------------------- | |
140 | ||
141 | void wxWakeUpIdle() | |
142 | { | |
143 | #if wxUSE_THREADS | |
144 | if (!wxThread::IsMain()) | |
145 | wxMutexGuiEnter(); | |
146 | #endif | |
147 | ||
148 | if (g_isIdle) | |
149 | wxapp_install_idle_handler(); | |
150 | ||
151 | #if wxUSE_THREADS | |
152 | if (!wxThread::IsMain()) | |
153 | wxMutexGuiLeave(); | |
154 | #endif | |
155 | } | |
156 | ||
157 | //----------------------------------------------------------------------------- | |
158 | // local functions | |
159 | //----------------------------------------------------------------------------- | |
160 | ||
161 | // the callback functions must be extern "C" to comply with GTK+ declarations | |
162 | extern "C" | |
163 | { | |
164 | ||
165 | static gint wxapp_pending_callback( gpointer WXUNUSED(data) ) | |
166 | { | |
167 | if (!wxTheApp) return TRUE; | |
168 | ||
169 | // When getting called from GDK's time-out handler | |
170 | // we are no longer within GDK's grab on the GUI | |
171 | // thread so we must lock it here ourselves. | |
172 | gdk_threads_enter(); | |
173 | ||
174 | // Sent idle event to all who request them. | |
175 | wxTheApp->ProcessPendingEvents(); | |
176 | ||
177 | g_pendingTag = 0; | |
178 | ||
179 | // Flush the logged messages if any. | |
180 | #if wxUSE_LOG | |
181 | wxLog::FlushActive(); | |
182 | #endif // wxUSE_LOG | |
183 | ||
184 | // Release lock again | |
185 | gdk_threads_leave(); | |
186 | ||
187 | // Return FALSE to indicate that no more idle events are | |
188 | // to be sent (single shot instead of continuous stream) | |
189 | return FALSE; | |
190 | } | |
191 | ||
192 | static gint wxapp_idle_callback( gpointer WXUNUSED(data) ) | |
193 | { | |
194 | if (!wxTheApp) | |
195 | return TRUE; | |
196 | ||
197 | #ifdef __WXDEBUG__ | |
198 | // don't generate the idle events while the assert modal dialog is shown, | |
199 | // this completely confuses the apps which don't expect to be reentered | |
200 | // from some safely-looking functions | |
201 | if ( wxTheApp->IsInAssert() ) | |
202 | { | |
203 | return TRUE; | |
204 | } | |
205 | #endif // __WXDEBUG__ | |
206 | ||
207 | // When getting called from GDK's time-out handler | |
208 | // we are no longer within GDK's grab on the GUI | |
209 | // thread so we must lock it here ourselves. | |
210 | gdk_threads_enter(); | |
211 | ||
212 | // Indicate that we are now in idle mode and event handlers | |
213 | // will have to reinstall the idle handler again. | |
214 | g_isIdle = TRUE; | |
215 | wxTheApp->m_idleTag = 0; | |
216 | ||
217 | // Send idle event to all who request them as long as | |
218 | // no events have popped up in the event queue. | |
219 | while (wxTheApp->ProcessIdle() && (gtk_events_pending() == 0)) | |
220 | ; | |
221 | ||
222 | // Release lock again | |
223 | gdk_threads_leave(); | |
224 | ||
225 | // Return FALSE to indicate that no more idle events are | |
226 | // to be sent (single shot instead of continuous stream). | |
227 | return FALSE; | |
228 | } | |
229 | ||
230 | #if wxUSE_THREADS | |
231 | ||
232 | static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout ) | |
233 | { | |
234 | gint res; | |
235 | gdk_threads_enter(); | |
236 | ||
237 | wxMutexGuiLeave(); | |
238 | g_mainThreadLocked = TRUE; | |
239 | ||
240 | res = poll( (struct pollfd*) ufds, nfds, timeout ); | |
241 | ||
242 | wxMutexGuiEnter(); | |
243 | g_mainThreadLocked = FALSE; | |
244 | ||
245 | gdk_threads_leave(); | |
246 | ||
247 | return res; | |
248 | } | |
249 | ||
250 | #endif // wxUSE_THREADS | |
251 | ||
252 | } // extern "C" | |
253 | ||
254 | void wxapp_install_idle_handler() | |
255 | { | |
256 | wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") ); | |
257 | ||
258 | g_isIdle = FALSE; | |
259 | ||
260 | if (g_pendingTag == 0) | |
261 | g_pendingTag = gtk_idle_add_priority( 900, wxapp_pending_callback, (gpointer) NULL ); | |
262 | ||
263 | // This routine gets called by all event handlers | |
264 | // indicating that the idle is over. It may also | |
265 | // get called from other thread for sending events | |
266 | // to the main thread (and processing these in | |
267 | // idle time). Very low priority. | |
268 | wxTheApp->m_idleTag = gtk_idle_add_priority( 1000, wxapp_idle_callback, (gpointer) NULL ); | |
269 | } | |
270 | ||
271 | //----------------------------------------------------------------------------- | |
272 | // Access to the root window global | |
273 | //----------------------------------------------------------------------------- | |
274 | ||
275 | GtkWidget* wxGetRootWindow() | |
276 | { | |
277 | if (gs_RootWindow == NULL) | |
278 | { | |
279 | gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL ); | |
280 | gtk_widget_realize( gs_RootWindow ); | |
281 | } | |
282 | return gs_RootWindow; | |
283 | } | |
284 | ||
285 | //----------------------------------------------------------------------------- | |
286 | // wxApp | |
287 | //----------------------------------------------------------------------------- | |
288 | ||
289 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) | |
290 | ||
291 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
292 | EVT_IDLE(wxApp::OnIdle) | |
293 | END_EVENT_TABLE() | |
294 | ||
295 | wxApp::wxApp() | |
296 | { | |
297 | m_initialized = FALSE; | |
298 | #ifdef __WXDEBUG__ | |
299 | m_isInAssert = FALSE; | |
300 | #endif // __WXDEBUG__ | |
301 | ||
302 | m_idleTag = 0; | |
303 | wxapp_install_idle_handler(); | |
304 | ||
305 | #if wxUSE_THREADS | |
306 | g_main_set_poll_func( wxapp_poll_func ); | |
307 | #endif | |
308 | ||
309 | m_colorCube = (unsigned char*) NULL; | |
310 | ||
311 | // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp | |
312 | m_glVisualInfo = (void *) NULL; | |
313 | } | |
314 | ||
315 | wxApp::~wxApp() | |
316 | { | |
317 | if (m_idleTag) gtk_idle_remove( m_idleTag ); | |
318 | ||
319 | if (m_colorCube) free(m_colorCube); | |
320 | } | |
321 | ||
322 | bool wxApp::OnInitGui() | |
323 | { | |
324 | if ( !wxAppBase::OnInitGui() ) | |
325 | return FALSE; | |
326 | ||
327 | GdkVisual *visual = gdk_visual_get_system(); | |
328 | ||
329 | // if this is a wxGLApp (derived from wxApp), and we've already | |
330 | // chosen a specific visual, then derive the GdkVisual from that | |
331 | if (m_glVisualInfo != NULL) | |
332 | { | |
333 | #ifdef __WXGTK20__ | |
334 | // seems gtk_widget_set_default_visual no longer exists? | |
335 | GdkVisual* vis = gtk_widget_get_default_visual(); | |
336 | #else | |
337 | GdkVisual* vis = gdkx_visual_get( | |
338 | ((XVisualInfo *) m_glVisualInfo) ->visualid ); | |
339 | gtk_widget_set_default_visual( vis ); | |
340 | #endif | |
341 | ||
342 | GdkColormap *colormap = gdk_colormap_new( vis, FALSE ); | |
343 | gtk_widget_set_default_colormap( colormap ); | |
344 | ||
345 | visual = vis; | |
346 | } | |
347 | ||
348 | // On some machines, the default visual is just 256 colours, so | |
349 | // we make sure we get the best. This can sometimes be wasteful. | |
350 | ||
351 | else | |
352 | if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual)) | |
353 | { | |
354 | #ifdef __WXGTK20__ | |
355 | /* seems gtk_widget_set_default_visual no longer exists? */ | |
356 | GdkVisual* vis = gtk_widget_get_default_visual(); | |
357 | #else | |
358 | GdkVisual* vis = gdk_visual_get_best(); | |
359 | gtk_widget_set_default_visual( vis ); | |
360 | #endif | |
361 | ||
362 | GdkColormap *colormap = gdk_colormap_new( vis, FALSE ); | |
363 | gtk_widget_set_default_colormap( colormap ); | |
364 | ||
365 | visual = vis; | |
366 | } | |
367 | ||
368 | // Nothing to do for 15, 16, 24, 32 bit displays | |
369 | if (visual->depth > 8) return TRUE; | |
370 | ||
371 | // initialize color cube for 8-bit color reduction dithering | |
372 | ||
373 | GdkColormap *cmap = gtk_widget_get_default_colormap(); | |
374 | ||
375 | m_colorCube = (unsigned char*)malloc(32 * 32 * 32); | |
376 | ||
377 | for (int r = 0; r < 32; r++) | |
378 | { | |
379 | for (int g = 0; g < 32; g++) | |
380 | { | |
381 | for (int b = 0; b < 32; b++) | |
382 | { | |
383 | int rr = (r << 3) | (r >> 2); | |
384 | int gg = (g << 3) | (g >> 2); | |
385 | int bb = (b << 3) | (b >> 2); | |
386 | ||
387 | int index = -1; | |
388 | ||
389 | GdkColor *colors = cmap->colors; | |
390 | if (colors) | |
391 | { | |
392 | int max = 3 * 65536; | |
393 | ||
394 | for (int i = 0; i < cmap->size; i++) | |
395 | { | |
396 | int rdiff = ((rr << 8) - colors[i].red); | |
397 | int gdiff = ((gg << 8) - colors[i].green); | |
398 | int bdiff = ((bb << 8) - colors[i].blue); | |
399 | int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff); | |
400 | if (sum < max) | |
401 | { | |
402 | index = i; max = sum; | |
403 | } | |
404 | } | |
405 | } | |
406 | else | |
407 | { | |
408 | // assume 8-bit true or static colors. this really exists | |
409 | GdkVisual* vis = gdk_colormap_get_visual( cmap ); | |
410 | index = (r >> (5 - vis->red_prec)) << vis->red_shift; | |
411 | index |= (g >> (5 - vis->green_prec)) << vis->green_shift; | |
412 | index |= (b >> (5 - vis->blue_prec)) << vis->blue_shift; | |
413 | } | |
414 | m_colorCube[ (r*1024) + (g*32) + b ] = index; | |
415 | } | |
416 | } | |
417 | } | |
418 | ||
419 | return TRUE; | |
420 | } | |
421 | ||
422 | GdkVisual *wxApp::GetGdkVisual() | |
423 | { | |
424 | GdkVisual *visual = NULL; | |
425 | ||
426 | if (m_glVisualInfo) | |
427 | visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid ); | |
428 | else | |
429 | visual = gdk_window_get_visual( wxGetRootWindow()->window ); | |
430 | ||
431 | wxASSERT( visual ); | |
432 | ||
433 | return visual; | |
434 | } | |
435 | ||
436 | bool wxApp::ProcessIdle() | |
437 | { | |
438 | wxIdleEvent event; | |
439 | event.SetEventObject( this ); | |
440 | ProcessEvent( event ); | |
441 | ||
442 | return event.MoreRequested(); | |
443 | } | |
444 | ||
445 | void wxApp::OnIdle( wxIdleEvent &event ) | |
446 | { | |
447 | static bool s_inOnIdle = FALSE; | |
448 | ||
449 | // Avoid recursion (via ProcessEvent default case) | |
450 | if (s_inOnIdle) | |
451 | return; | |
452 | ||
453 | s_inOnIdle = TRUE; | |
454 | ||
455 | // Resend in the main thread events which have been prepared in other | |
456 | // threads | |
457 | ProcessPendingEvents(); | |
458 | ||
459 | // 'Garbage' collection of windows deleted with Close() | |
460 | DeletePendingObjects(); | |
461 | ||
462 | // Send OnIdle events to all windows | |
463 | bool needMore = SendIdleEvents(); | |
464 | ||
465 | if (needMore) | |
466 | event.RequestMore(TRUE); | |
467 | ||
468 | s_inOnIdle = FALSE; | |
469 | } | |
470 | ||
471 | bool wxApp::SendIdleEvents() | |
472 | { | |
473 | bool needMore = FALSE; | |
474 | ||
475 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); | |
476 | while (node) | |
477 | { | |
478 | wxWindow* win = node->GetData(); | |
479 | if (SendIdleEvents(win)) | |
480 | needMore = TRUE; | |
481 | node = node->GetNext(); | |
482 | } | |
483 | ||
484 | return needMore; | |
485 | } | |
486 | ||
487 | bool wxApp::SendIdleEvents( wxWindow* win ) | |
488 | { | |
489 | bool needMore = FALSE; | |
490 | ||
491 | wxIdleEvent event; | |
492 | event.SetEventObject(win); | |
493 | ||
494 | win->GetEventHandler()->ProcessEvent(event); | |
495 | ||
496 | win->OnInternalIdle(); | |
497 | ||
498 | if (event.MoreRequested()) | |
499 | needMore = TRUE; | |
500 | ||
501 | wxNode* node = win->GetChildren().First(); | |
502 | while (node) | |
503 | { | |
504 | wxWindow* win = (wxWindow*) node->Data(); | |
505 | if (SendIdleEvents(win)) | |
506 | needMore = TRUE; | |
507 | ||
508 | node = node->Next(); | |
509 | } | |
510 | return needMore ; | |
511 | } | |
512 | ||
513 | int wxApp::MainLoop() | |
514 | { | |
515 | gtk_main(); | |
516 | return 0; | |
517 | } | |
518 | ||
519 | void wxApp::ExitMainLoop() | |
520 | { | |
521 | if (gtk_main_level() > 0) | |
522 | gtk_main_quit(); | |
523 | } | |
524 | ||
525 | bool wxApp::Initialized() | |
526 | { | |
527 | return m_initialized; | |
528 | } | |
529 | ||
530 | bool wxApp::Pending() | |
531 | { | |
532 | return (gtk_events_pending() > 0); | |
533 | } | |
534 | ||
535 | void wxApp::Dispatch() | |
536 | { | |
537 | gtk_main_iteration(); | |
538 | } | |
539 | ||
540 | void wxApp::DeletePendingObjects() | |
541 | { | |
542 | wxNode *node = wxPendingDelete.First(); | |
543 | while (node) | |
544 | { | |
545 | wxObject *obj = (wxObject *)node->Data(); | |
546 | ||
547 | delete obj; | |
548 | ||
549 | if (wxPendingDelete.Find(obj)) | |
550 | delete node; | |
551 | ||
552 | node = wxPendingDelete.First(); | |
553 | } | |
554 | } | |
555 | ||
556 | bool wxApp::Initialize() | |
557 | { | |
558 | wxBuffer = new wxChar[BUFSIZ + 512]; | |
559 | ||
560 | wxClassInfo::InitializeClasses(); | |
561 | ||
562 | #if wxUSE_INTL | |
563 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
564 | #endif | |
565 | ||
566 | // GL: I'm annoyed ... I don't know where to put this and I don't want to | |
567 | // create a module for that as it's part of the core. | |
568 | #if wxUSE_THREADS | |
569 | wxPendingEvents = new wxList(); | |
570 | wxPendingEventsLocker = new wxCriticalSection(); | |
571 | #endif | |
572 | ||
573 | wxTheColourDatabase = new wxColourDatabase( wxKEY_STRING ); | |
574 | wxTheColourDatabase->Initialize(); | |
575 | ||
576 | wxInitializeStockLists(); | |
577 | wxInitializeStockObjects(); | |
578 | ||
579 | #if wxUSE_WX_RESOURCES | |
580 | wxInitializeResourceSystem(); | |
581 | #endif | |
582 | ||
583 | wxModule::RegisterModules(); | |
584 | if (!wxModule::InitializeModules()) return FALSE; | |
585 | ||
586 | return TRUE; | |
587 | } | |
588 | ||
589 | void wxApp::CleanUp() | |
590 | { | |
591 | wxModule::CleanUpModules(); | |
592 | ||
593 | #if wxUSE_WX_RESOURCES | |
594 | wxCleanUpResourceSystem(); | |
595 | #endif | |
596 | ||
597 | if (wxTheColourDatabase) | |
598 | delete wxTheColourDatabase; | |
599 | ||
600 | wxTheColourDatabase = (wxColourDatabase*) NULL; | |
601 | ||
602 | wxDeleteStockObjects(); | |
603 | ||
604 | wxDeleteStockLists(); | |
605 | ||
606 | delete wxTheApp; | |
607 | wxTheApp = (wxApp*) NULL; | |
608 | ||
609 | // GL: I'm annoyed ... I don't know where to put this and I don't want to | |
610 | // create a module for that as it's part of the core. | |
611 | #if wxUSE_THREADS | |
612 | delete wxPendingEvents; | |
613 | delete wxPendingEventsLocker; | |
614 | #endif | |
615 | ||
616 | delete[] wxBuffer; | |
617 | ||
618 | wxClassInfo::CleanUpClasses(); | |
619 | ||
620 | // check for memory leaks | |
621 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT | |
622 | if (wxDebugContext::CountObjectsLeft(TRUE) > 0) | |
623 | { | |
624 | wxLogDebug(wxT("There were memory leaks.\n")); | |
625 | wxDebugContext::Dump(); | |
626 | wxDebugContext::PrintStatistics(); | |
627 | } | |
628 | #endif // Debug | |
629 | ||
630 | #if wxUSE_LOG | |
631 | // do this as the very last thing because everything else can log messages | |
632 | wxLog::DontCreateOnDemand(); | |
633 | ||
634 | wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL ); | |
635 | if (oldLog) | |
636 | delete oldLog; | |
637 | #endif // wxUSE_LOG | |
638 | } | |
639 | ||
640 | //----------------------------------------------------------------------------- | |
641 | // wxEntry | |
642 | //----------------------------------------------------------------------------- | |
643 | ||
644 | // NB: argc and argv may be changed here, pass by reference! | |
645 | int wxEntryStart( int& argc, char *argv[] ) | |
646 | { | |
647 | #if wxUSE_THREADS | |
648 | // GTK 1.2 up to version 1.2.3 has broken threads | |
649 | if ((gtk_major_version == 1) && | |
650 | (gtk_minor_version == 2) && | |
651 | (gtk_micro_version < 4)) | |
652 | { | |
653 | printf( "wxWindows warning: GUI threading disabled due to outdated GTK version\n" ); | |
654 | } | |
655 | else | |
656 | { | |
657 | g_thread_init(NULL); | |
658 | } | |
659 | #endif | |
660 | ||
661 | gtk_set_locale(); | |
662 | ||
663 | // We should have the wxUSE_WCHAR_T test on the _outside_ | |
664 | #if wxUSE_WCHAR_T | |
665 | #if defined(__WXGTK20__) | |
666 | // gtk+ 2.0 supports Unicode through UTF-8 strings | |
667 | wxConvCurrent = &wxConvUTF8; | |
668 | #else | |
669 | if (!wxOKlibc()) wxConvCurrent = &wxConvLocal; | |
670 | #endif | |
671 | #else | |
672 | if (!wxOKlibc()) wxConvCurrent = (wxMBConv*) NULL; | |
673 | #endif | |
674 | ||
675 | gdk_threads_enter(); | |
676 | ||
677 | gtk_init( &argc, &argv ); | |
678 | ||
679 | wxSetDetectableAutoRepeat( TRUE ); | |
680 | ||
681 | if (!wxApp::Initialize()) | |
682 | { | |
683 | gdk_threads_leave(); | |
684 | return -1; | |
685 | } | |
686 | ||
687 | return 0; | |
688 | } | |
689 | ||
690 | ||
691 | int wxEntryInitGui() | |
692 | { | |
693 | int retValue = 0; | |
694 | ||
695 | if ( !wxTheApp->OnInitGui() ) | |
696 | retValue = -1; | |
697 | ||
698 | wxGetRootWindow(); | |
699 | ||
700 | return retValue; | |
701 | } | |
702 | ||
703 | ||
704 | void wxEntryCleanup() | |
705 | { | |
706 | #if wxUSE_LOG | |
707 | // flush the logged messages if any | |
708 | wxLog *log = wxLog::GetActiveTarget(); | |
709 | if (log != NULL && log->HasPendingMessages()) | |
710 | log->Flush(); | |
711 | ||
712 | // continuing to use user defined log target is unsafe from now on because | |
713 | // some resources may be already unavailable, so replace it by something | |
714 | // more safe | |
715 | wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr); | |
716 | if ( oldlog ) | |
717 | delete oldlog; | |
718 | #endif // wxUSE_LOG | |
719 | ||
720 | wxApp::CleanUp(); | |
721 | ||
722 | gdk_threads_leave(); | |
723 | } | |
724 | ||
725 | ||
726 | int wxEntry( int argc, char *argv[] ) | |
727 | { | |
728 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT | |
729 | // This seems to be necessary since there are 'rogue' | |
730 | // objects present at this point (perhaps global objects?) | |
731 | // Setting a checkpoint will ignore them as far as the | |
732 | // memory checking facility is concerned. | |
733 | // Of course you may argue that memory allocated in globals should be | |
734 | // checked, but this is a reasonable compromise. | |
735 | wxDebugContext::SetCheckpoint(); | |
736 | #endif | |
737 | int err = wxEntryStart(argc, argv); | |
738 | if (err) | |
739 | return err; | |
740 | ||
741 | if (!wxTheApp) | |
742 | { | |
743 | wxCHECK_MSG( wxApp::GetInitializerFunction(), -1, | |
744 | wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") ); | |
745 | ||
746 | wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction(); | |
747 | ||
748 | wxObject *test_app = app_ini(); | |
749 | ||
750 | wxTheApp = (wxApp*) test_app; | |
751 | } | |
752 | ||
753 | wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") ); | |
754 | ||
755 | wxTheApp->argc = argc; | |
756 | #if wxUSE_UNICODE | |
757 | wxTheApp->argv = new wxChar*[argc+1]; | |
758 | int mb_argc = 0; | |
759 | while (mb_argc < argc) | |
760 | { | |
761 | wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc])); | |
762 | mb_argc++; | |
763 | } | |
764 | wxTheApp->argv[mb_argc] = (wxChar *)NULL; | |
765 | #else | |
766 | wxTheApp->argv = argv; | |
767 | #endif | |
768 | ||
769 | wxString name(wxFileNameFromPath(argv[0])); | |
770 | wxStripExtension( name ); | |
771 | wxTheApp->SetAppName( name ); | |
772 | ||
773 | int retValue; | |
774 | retValue = wxEntryInitGui(); | |
775 | ||
776 | // Here frames insert themselves automatically into wxTopLevelWindows by | |
777 | // getting created in OnInit(). | |
778 | if ( retValue == 0 ) | |
779 | { | |
780 | if ( !wxTheApp->OnInit() ) | |
781 | retValue = -1; | |
782 | } | |
783 | ||
784 | if ( retValue == 0 ) | |
785 | { | |
786 | /* delete pending toplevel windows (typically a single | |
787 | dialog) so that, if there isn't any left, we don't | |
788 | call OnRun() */ | |
789 | wxTheApp->DeletePendingObjects(); | |
790 | ||
791 | wxTheApp->m_initialized = wxTopLevelWindows.GetCount() != 0; | |
792 | ||
793 | if (wxTheApp->Initialized()) | |
794 | { | |
795 | wxTheApp->OnRun(); | |
796 | ||
797 | wxWindow *topWindow = wxTheApp->GetTopWindow(); | |
798 | if (topWindow) | |
799 | { | |
800 | /* Forcibly delete the window. */ | |
801 | if (topWindow->IsKindOf(CLASSINFO(wxFrame)) || | |
802 | topWindow->IsKindOf(CLASSINFO(wxDialog)) ) | |
803 | { | |
804 | topWindow->Close( TRUE ); | |
805 | wxTheApp->DeletePendingObjects(); | |
806 | } | |
807 | else | |
808 | { | |
809 | delete topWindow; | |
810 | wxTheApp->SetTopWindow( (wxWindow*) NULL ); | |
811 | } | |
812 | } | |
813 | ||
814 | retValue = wxTheApp->OnExit(); | |
815 | } | |
816 | } | |
817 | ||
818 | wxEntryCleanup(); | |
819 | ||
820 | return retValue; | |
821 | } | |
822 | ||
823 | #ifndef __WXUNIVERSAL__ | |
824 | ||
825 | // XPM hack: make the arrays const | |
826 | #define static static const | |
827 | ||
828 | #include "wx/gtk/info.xpm" | |
829 | #include "wx/gtk/error.xpm" | |
830 | #include "wx/gtk/question.xpm" | |
831 | #include "wx/gtk/warning.xpm" | |
832 | ||
833 | #undef static | |
834 | ||
835 | wxIcon wxApp::GetStdIcon(int which) const | |
836 | { | |
837 | switch(which) | |
838 | { | |
839 | case wxICON_INFORMATION: | |
840 | return wxIcon(info_xpm); | |
841 | ||
842 | case wxICON_QUESTION: | |
843 | return wxIcon(question_xpm); | |
844 | ||
845 | case wxICON_EXCLAMATION: | |
846 | return wxIcon(warning_xpm); | |
847 | ||
848 | default: | |
849 | wxFAIL_MSG(wxT("requested non existent standard icon")); | |
850 | // still fall through | |
851 | ||
852 | case wxICON_HAND: | |
853 | return wxIcon(error_xpm); | |
854 | } | |
855 | } | |
856 | #else | |
857 | wxIcon wxApp::GetStdIcon(int which) const | |
858 | { | |
859 | return wxTheme::Get()->GetRenderer()->GetStdIcon(which); | |
860 | } | |
861 | #endif // !__WXUNIVERSAL__ | |
862 | ||
863 | ||
864 | #ifdef __WXDEBUG__ | |
865 | ||
866 | void wxApp::OnAssert(const wxChar *file, int line, const wxChar *msg) | |
867 | { | |
868 | m_isInAssert = TRUE; | |
869 | ||
870 | wxAppBase::OnAssert(file, line, msg); | |
871 | ||
872 | m_isInAssert = FALSE; | |
873 | } | |
874 | ||
875 | #endif // __WXDEBUG__ | |
876 |