]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/app.cpp
follow up parent chain to properly support modal dialog parents, see #15383
[wxWidgets.git] / src / gtk1 / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/app.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling, Julian Smart
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11
12 #include "wx/app.h"
13
14 #ifndef WX_PRECOMP
15 #include "wx/intl.h"
16 #include "wx/log.h"
17 #include "wx/utils.h"
18 #include "wx/dialog.h"
19 #include "wx/settings.h"
20 #include "wx/msgdlg.h"
21 #include "wx/memory.h"
22 #include "wx/font.h"
23 #include "wx/gdicmn.h"
24 #include "wx/image.h"
25 #include "wx/module.h"
26 #endif
27
28 #include "wx/file.h"
29 #include "wx/filename.h"
30 #include "wx/thread.h"
31
32 #ifdef __WXGPE__
33 #include <gpe/init.h>
34 #endif
35
36 #ifdef __WXUNIVERSAL__
37 #include "wx/univ/theme.h"
38 #include "wx/univ/renderer.h"
39 #endif
40
41 #if wxUSE_THREADS
42 #include "wx/thread.h"
43 #endif
44
45 #include <unistd.h>
46
47 #ifdef HAVE_POLL
48 #if defined(__VMS)
49 #include <poll.h>
50 #else
51 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
52 // in neither poll.h nor sys/poll.h which results in link errors later
53 #ifdef __OPENBSD__
54 extern "C"
55 {
56 #endif
57
58 #include <sys/poll.h>
59
60 #ifdef __OPENBSD__
61 };
62 #endif
63 #endif // platform
64 #else // !HAVE_POLL
65 // we implement poll() ourselves using select() which is supposed exist in
66 // all modern Unices
67 #include <sys/types.h>
68 #include <sys/time.h>
69 #include <unistd.h>
70 #ifdef HAVE_SYS_SELECT_H
71 #include <sys/select.h>
72 #endif
73 #endif // HAVE_POLL/!HAVE_POLL
74
75 #include "wx/unix/private.h"
76 #include "wx/gtk1/win_gtk.h"
77
78 #include <gtk/gtk.h>
79
80 //-----------------------------------------------------------------------------
81 // global data
82 //-----------------------------------------------------------------------------
83
84 bool g_mainThreadLocked = false;
85 gint g_pendingTag = 0;
86
87 static GtkWidget *gs_RootWindow = NULL;
88
89 //-----------------------------------------------------------------------------
90 // idle system
91 //-----------------------------------------------------------------------------
92
93 extern bool g_isIdle;
94
95 void wxapp_install_idle_handler();
96
97 #if wxUSE_THREADS
98 static wxMutex gs_idleTagsMutex;
99 #endif
100
101
102 //-----------------------------------------------------------------------------
103 // wxWakeUpIdle
104 //-----------------------------------------------------------------------------
105
106 // RR/KH: The wxMutexGui calls are not needed on GTK2 according to
107 // the GTK faq, http://www.gtk.org/faq/#AEN500
108 // The calls to gdk_threads_enter() and leave() are specifically noted
109 // as not being necessary. The MutexGui calls are still left in for GTK1.
110 // Eliminating the MutexGui calls fixes the long-standing "random" lockup
111 // when using wxPostEvent (which calls WakeUpIdle) from a thread.
112
113 void wxApp::WakeUpIdle()
114 {
115 #if wxUSE_THREADS
116 if (!wxThread::IsMain())
117 wxMutexGuiEnter();
118 #endif // wxUSE_THREADS
119
120 wxapp_install_idle_handler();
121
122 #if wxUSE_THREADS
123 if (!wxThread::IsMain())
124 wxMutexGuiLeave();
125 #endif // wxUSE_THREADS
126 }
127
128 //-----------------------------------------------------------------------------
129 // local functions
130 //-----------------------------------------------------------------------------
131
132 // the callback functions must be extern "C" to comply with GTK+ declarations
133 extern "C"
134 {
135
136 static gint wxapp_pending_callback( gpointer WXUNUSED(data) )
137 {
138 if (!wxTheApp) return TRUE;
139
140 // When getting called from GDK's time-out handler
141 // we are no longer within GDK's grab on the GUI
142 // thread so we must lock it here ourselves.
143 gdk_threads_enter();
144
145 // Sent idle event to all who request them.
146 wxTheApp->ProcessPendingEvents();
147
148 {
149 #if wxUSE_THREADS
150 wxMutexLocker lock(gs_idleTagsMutex);
151 #endif
152 g_pendingTag = 0;
153 }
154
155 // Flush the logged messages if any.
156 #if wxUSE_LOG
157 wxLog::FlushActive();
158 #endif // wxUSE_LOG
159
160 // Release lock again
161 gdk_threads_leave();
162
163 // Return FALSE to indicate that no more idle events are
164 // to be sent (single shot instead of continuous stream)
165 return FALSE;
166 }
167
168 static gint wxapp_idle_callback( gpointer WXUNUSED(data) )
169 {
170 if (!wxTheApp)
171 return TRUE;
172
173 #if wxDEBUG_LEVEL
174 // don't generate the idle events while the assert modal dialog is shown,
175 // this completely confuses the apps which don't expect to be reentered
176 // from some safely-looking functions
177 if ( wxTheApp->IsInAssert() )
178 {
179 // But repaint the assertion message if necessary
180 if (wxTopLevelWindows.GetCount() > 0)
181 {
182 wxWindow* win = (wxWindow*) wxTopLevelWindows.GetLast()->GetData();
183 if (win->IsKindOf(CLASSINFO(wxGenericMessageDialog)))
184 win->OnInternalIdle();
185 }
186 return TRUE;
187 }
188 #endif // wxDEBUG_LEVEL
189
190 // When getting called from GDK's time-out handler
191 // we are no longer within GDK's grab on the GUI
192 // thread so we must lock it here ourselves.
193 gdk_threads_enter();
194
195 // Indicate that we are now in idle mode and event handlers
196 // will have to reinstall the idle handler again.
197 {
198 #if wxUSE_THREADS
199 wxMutexLocker lock(gs_idleTagsMutex);
200 #endif
201 g_isIdle = TRUE;
202 wxTheApp->m_idleTag = 0;
203 }
204
205 // Send idle event to all who request them as long as
206 // no events have popped up in the event queue.
207 while (wxTheApp->ProcessIdle() && (gtk_events_pending() == 0))
208 ;
209
210 // Release lock again
211 gdk_threads_leave();
212
213 // Return FALSE to indicate that no more idle events are
214 // to be sent (single shot instead of continuous stream).
215 return FALSE;
216 }
217
218 #if wxUSE_THREADS
219
220 #ifdef HAVE_POLL
221 #define wxPoll poll
222 #define wxPollFd pollfd
223 #else // !HAVE_POLL
224
225 typedef GPollFD wxPollFd;
226
227 int wxPoll(wxPollFd *ufds, unsigned int nfds, int timeout)
228 {
229 // convert timeout from ms to struct timeval (s/us)
230 timeval tv_timeout;
231 tv_timeout.tv_sec = timeout/1000;
232 tv_timeout.tv_usec = (timeout%1000)*1000;
233
234 // remember the highest fd used here
235 int fdMax = -1;
236
237 // and fill the sets for select()
238 fd_set readfds;
239 fd_set writefds;
240 fd_set exceptfds;
241 wxFD_ZERO(&readfds);
242 wxFD_ZERO(&writefds);
243 wxFD_ZERO(&exceptfds);
244
245 unsigned int i;
246 for ( i = 0; i < nfds; i++ )
247 {
248 wxASSERT_MSG( ufds[i].fd < FD_SETSIZE, wxT("fd out of range") );
249
250 if ( ufds[i].events & G_IO_IN )
251 wxFD_SET(ufds[i].fd, &readfds);
252
253 if ( ufds[i].events & G_IO_PRI )
254 wxFD_SET(ufds[i].fd, &exceptfds);
255
256 if ( ufds[i].events & G_IO_OUT )
257 wxFD_SET(ufds[i].fd, &writefds);
258
259 if ( ufds[i].fd > fdMax )
260 fdMax = ufds[i].fd;
261 }
262
263 fdMax++;
264 int res = select(fdMax, &readfds, &writefds, &exceptfds, &tv_timeout);
265
266 // translate the results back
267 for ( i = 0; i < nfds; i++ )
268 {
269 ufds[i].revents = 0;
270
271 if ( wxFD_ISSET(ufds[i].fd, &readfds ) )
272 ufds[i].revents |= G_IO_IN;
273
274 if ( wxFD_ISSET(ufds[i].fd, &exceptfds ) )
275 ufds[i].revents |= G_IO_PRI;
276
277 if ( wxFD_ISSET(ufds[i].fd, &writefds ) )
278 ufds[i].revents |= G_IO_OUT;
279 }
280
281 return res;
282 }
283
284 #endif // HAVE_POLL/!HAVE_POLL
285
286 static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout )
287 {
288 gdk_threads_enter();
289
290 wxMutexGuiLeave();
291 g_mainThreadLocked = true;
292
293 // we rely on the fact that glib GPollFD struct is really just pollfd but
294 // I wonder how wise is this in the long term (VZ)
295 gint res = wxPoll( (wxPollFd *) ufds, nfds, timeout );
296
297 wxMutexGuiEnter();
298 g_mainThreadLocked = false;
299
300 gdk_threads_leave();
301
302 return res;
303 }
304
305 #endif // wxUSE_THREADS
306
307 } // extern "C"
308
309 void wxapp_install_idle_handler()
310 {
311 #if wxUSE_THREADS
312 wxMutexLocker lock(gs_idleTagsMutex);
313 #endif
314
315 // Don't install the handler if it's already installed. This test *MUST*
316 // be done when gs_idleTagsMutex is locked!
317 if (!g_isIdle)
318 return;
319
320 // GD: this assert is raised when using the thread sample (which works)
321 // so the test is probably not so easy. Can widget callbacks be
322 // triggered from child threads and, if so, for which widgets?
323 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
324
325 wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") );
326
327 g_isIdle = false;
328
329 if (g_pendingTag == 0)
330 g_pendingTag = gtk_idle_add_priority( 900, wxapp_pending_callback, (gpointer) NULL );
331
332 // This routine gets called by all event handlers
333 // indicating that the idle is over. It may also
334 // get called from other thread for sending events
335 // to the main thread (and processing these in
336 // idle time). Very low priority.
337 wxTheApp->m_idleTag = gtk_idle_add_priority( 1000, wxapp_idle_callback, (gpointer) NULL );
338 }
339
340 static bool wxOKlibc()
341 {
342 #if defined(__UNIX__) && defined(__GLIBC__)
343 // glibc 2.0 uses UTF-8 even when it shouldn't
344 wchar_t res = 0;
345 if ((MB_CUR_MAX == 2) &&
346 (wxMB2WC(&res, "\xdd\xa5", 1) == 1) &&
347 (res==0x765))
348 {
349 // this is UTF-8 allright, check whether that's what we want
350 char *cur_locale = setlocale(LC_CTYPE, NULL);
351 if ((strlen(cur_locale) < 4) ||
352 (strcasecmp(cur_locale + strlen(cur_locale) - 4, "utf8")) ||
353 (strcasecmp(cur_locale + strlen(cur_locale) - 5, "utf-8"))) {
354 // nope, don't use libc conversion
355 return false;
356 }
357 }
358 #endif
359 return true;
360 }
361
362 //-----------------------------------------------------------------------------
363 // Access to the root window global
364 //-----------------------------------------------------------------------------
365
366 GtkWidget* wxGetRootWindow()
367 {
368 if (gs_RootWindow == NULL)
369 {
370 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
371 gtk_widget_realize( gs_RootWindow );
372 }
373 return gs_RootWindow;
374 }
375
376 //-----------------------------------------------------------------------------
377 // wxApp
378 //-----------------------------------------------------------------------------
379
380 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
381
382 wxApp::wxApp()
383 {
384 m_isInAssert = false;
385
386 m_idleTag = 0;
387 g_isIdle = TRUE;
388 wxapp_install_idle_handler();
389
390 #if wxUSE_THREADS
391 g_main_set_poll_func( wxapp_poll_func );
392 #endif
393
394 m_colorCube = (unsigned char*) NULL;
395
396 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
397 m_glVisualInfo = NULL;
398 m_glFBCInfo = NULL;
399 }
400
401 wxApp::~wxApp()
402 {
403 if (m_idleTag) gtk_idle_remove( m_idleTag );
404
405 if (m_colorCube) free(m_colorCube);
406 }
407
408 bool wxApp::OnInitGui()
409 {
410 if ( !wxAppBase::OnInitGui() )
411 return false;
412
413 GdkVisual *visual = gdk_visual_get_system();
414
415 // if this is a wxGLApp (derived from wxApp), and we've already
416 // chosen a specific visual, then derive the GdkVisual from that
417 if (m_glVisualInfo != NULL)
418 {
419 GdkVisual* vis = gdkx_visual_get(
420 ((XVisualInfo *) m_glVisualInfo) ->visualid );
421 gtk_widget_set_default_visual( vis );
422
423 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
424 gtk_widget_set_default_colormap( colormap );
425
426 visual = vis;
427 }
428
429 // On some machines, the default visual is just 256 colours, so
430 // we make sure we get the best. This can sometimes be wasteful.
431
432 else
433 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual))
434 {
435 GdkVisual* vis = gdk_visual_get_best();
436 gtk_widget_set_default_visual( vis );
437
438 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
439 gtk_widget_set_default_colormap( colormap );
440
441 visual = vis;
442 }
443
444 // Nothing to do for 15, 16, 24, 32 bit displays
445 if (visual->depth > 8) return TRUE;
446
447 // initialize color cube for 8-bit color reduction dithering
448
449 GdkColormap *cmap = gtk_widget_get_default_colormap();
450
451 m_colorCube = (unsigned char*)malloc(32 * 32 * 32);
452
453 for (int r = 0; r < 32; r++)
454 {
455 for (int g = 0; g < 32; g++)
456 {
457 for (int b = 0; b < 32; b++)
458 {
459 int rr = (r << 3) | (r >> 2);
460 int gg = (g << 3) | (g >> 2);
461 int bb = (b << 3) | (b >> 2);
462
463 int index = -1;
464
465 GdkColor *colors = cmap->colors;
466 if (colors)
467 {
468 int max = 3 * 65536;
469
470 for (int i = 0; i < cmap->size; i++)
471 {
472 int rdiff = ((rr << 8) - colors[i].red);
473 int gdiff = ((gg << 8) - colors[i].green);
474 int bdiff = ((bb << 8) - colors[i].blue);
475 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
476 if (sum < max)
477 {
478 index = i; max = sum;
479 }
480 }
481 }
482 else
483 {
484 // assume 8-bit true or static colors. this really exists
485 GdkVisual* vis = gdk_colormap_get_visual( cmap );
486 index = (r >> (5 - vis->red_prec)) << vis->red_shift;
487 index |= (g >> (5 - vis->green_prec)) << vis->green_shift;
488 index |= (b >> (5 - vis->blue_prec)) << vis->blue_shift;
489 }
490 m_colorCube[ (r*1024) + (g*32) + b ] = index;
491 }
492 }
493 }
494
495 return true;
496 }
497
498 GdkVisual *wxApp::GetGdkVisual()
499 {
500 GdkVisual *visual = NULL;
501
502 if (m_glVisualInfo)
503 visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid );
504 else
505 visual = gdk_window_get_visual( wxGetRootWindow()->window );
506
507 wxASSERT( visual );
508
509 return visual;
510 }
511
512 bool wxApp::Initialize(int& argc, wxChar **argv)
513 {
514 bool init_result;
515
516 #if wxUSE_THREADS
517 // GTK 1.2 up to version 1.2.3 has broken threads
518 if ((gtk_major_version == 1) &&
519 (gtk_minor_version == 2) &&
520 (gtk_micro_version < 4))
521 {
522 printf( "wxWidgets warning: GUI threading disabled due to outdated GTK version\n" );
523 }
524 else
525 {
526 if (!g_thread_supported())
527 g_thread_init(NULL);
528 }
529 #endif // wxUSE_THREADS
530
531 gtk_set_locale();
532
533 if (!wxOKlibc())
534 wxConvCurrent = &wxConvLocal;
535
536 #if wxUSE_UNICODE
537 // gtk_init() wants UTF-8, not wchar_t, so convert
538 int i;
539 char **argvGTK = new char *[argc + 1];
540 for ( i = 0; i < argc; i++ )
541 {
542 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv[i]));
543 }
544
545 argvGTK[argc] = NULL;
546
547 int argcGTK = argc;
548
549 #ifdef __WXGPE__
550 init_result = true; // is there a _check() version of this?
551 gpe_application_init( &argcGTK, &argvGTK );
552 #else
553 init_result = gtk_init_check( &argcGTK, &argvGTK );
554 #endif
555
556 if ( argcGTK != argc )
557 {
558 // we have to drop the parameters which were consumed by GTK+
559 for ( i = 0; i < argcGTK; i++ )
560 {
561 while ( strcmp(wxConvUTF8.cWX2MB(argv[i]), argvGTK[i]) != 0 )
562 {
563 memmove(argv + i, argv + i + 1, argc - i);
564 }
565 }
566
567 argc = argcGTK;
568 }
569 //else: gtk_init() didn't modify our parameters
570
571 // free our copy
572 for ( i = 0; i < argcGTK; i++ )
573 {
574 free(argvGTK[i]);
575 }
576
577 delete [] argvGTK;
578 #else // !wxUSE_UNICODE
579 // gtk_init() shouldn't actually change argv itself (just its contents) so
580 // it's ok to pass pointer to it
581 init_result = gtk_init_check( &argc, &argv );
582 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
583
584 if (!init_result) {
585 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
586 return false;
587 }
588
589 // we cannot enter threads before gtk_init is done
590 gdk_threads_enter();
591
592 if ( !wxAppBase::Initialize(argc, argv) )
593 {
594 gdk_threads_leave();
595
596 return false;
597 }
598
599 wxSetDetectableAutoRepeat( TRUE );
600
601 #if wxUSE_INTL
602 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
603 #endif
604
605 return true;
606 }
607
608 void wxApp::CleanUp()
609 {
610 gdk_threads_leave();
611
612 wxAppBase::CleanUp();
613 }
614
615 void wxApp::OnAssertFailure(const wxChar *file,
616 int line,
617 const wxChar* func,
618 const wxChar* cond,
619 const wxChar *msg)
620 {
621 // there is no need to do anything if asserts are disabled in this build
622 // anyhow
623 #if wxDEBUG_LEVEL
624 // block wx idle events while assert dialog is showing
625 m_isInAssert = true;
626
627 wxAppBase::OnAssertFailure(file, line, func, cond, msg);
628
629 m_isInAssert = false;
630 #else // !wxDEBUG_LEVEL
631 wxUnusedVar(file);
632 wxUnusedVar(line);
633 wxUnusedVar(func);
634 wxUnusedVar(cond);
635 wxUnusedVar(msg);
636 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
637 }
638
639 void wxApp::RemoveIdleTag()
640 {
641 #if wxUSE_THREADS
642 wxMutexLocker lock(gs_idleTagsMutex);
643 #endif
644 if (!g_isIdle)
645 {
646 gtk_idle_remove( wxTheApp->m_idleTag );
647 wxTheApp->m_idleTag = 0;
648 g_isIdle = true;
649 }
650 }