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