]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/app.cpp
added reference to the bug in case somebody wanted to remove this
[wxWidgets.git] / src / gtk1 / app.cpp
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "app.h"
12 #endif
13
14 #ifdef __VMS
15 // vms_jackets.h should for proper working be included before anything else
16 # include <vms_jackets.h>
17 #undef ConnectionNumber
18 #endif
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #include "wx/app.h"
24 #include "wx/gdicmn.h"
25 #include "wx/utils.h"
26 #include "wx/intl.h"
27 #include "wx/log.h"
28 #include "wx/memory.h"
29 #include "wx/font.h"
30 #include "wx/settings.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.h"
33 #include "wx/file.h"
34 #include "wx/filename.h"
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 bool g_mainThreadLocked = FALSE;
84 gint g_pendingTag = 0;
85
86 static GtkWidget *gs_RootWindow = (GtkWidget*) NULL;
87
88 //-----------------------------------------------------------------------------
89 // idle system
90 //-----------------------------------------------------------------------------
91
92 extern bool g_isIdle;
93
94 void wxapp_install_idle_handler();
95
96 //-----------------------------------------------------------------------------
97 // wxYield
98 //-----------------------------------------------------------------------------
99
100 // not static because used by textctrl.cpp
101 //
102 // MT-FIXME
103 bool wxIsInsideYield = FALSE;
104
105 bool wxApp::Yield(bool onlyIfNeeded)
106 {
107 if ( wxIsInsideYield )
108 {
109 if ( !onlyIfNeeded )
110 {
111 wxFAIL_MSG( wxT("wxYield called recursively" ) );
112 }
113
114 return FALSE;
115 }
116
117 #if wxUSE_THREADS
118 if ( !wxThread::IsMain() )
119 {
120 // can't call gtk_main_iteration() from other threads like this
121 return TRUE;
122 }
123 #endif // wxUSE_THREADS
124
125 wxIsInsideYield = TRUE;
126
127 if (!g_isIdle)
128 {
129 // We need to remove idle callbacks or the loop will
130 // never finish.
131 gtk_idle_remove( m_idleTag );
132 m_idleTag = 0;
133 g_isIdle = TRUE;
134 }
135
136 // disable log flushing from here because a call to wxYield() shouldn't
137 // normally result in message boxes popping up &c
138 wxLog::Suspend();
139
140 while (gtk_events_pending())
141 gtk_main_iteration();
142
143 // It's necessary to call ProcessIdle() to update the frames sizes which
144 // might have been changed (it also will update other things set from
145 // OnUpdateUI() which is a nice (and desired) side effect). But we
146 // call ProcessIdle() only once since this is not meant for longish
147 // background jobs (controlled by wxIdleEvent::RequestMore() and the
148 // return value of Processidle().
149 ProcessIdle();
150
151 // let the logs be flashed again
152 wxLog::Resume();
153
154 wxIsInsideYield = FALSE;
155
156 return TRUE;
157 }
158
159 //-----------------------------------------------------------------------------
160 // wxWakeUpIdle
161 //-----------------------------------------------------------------------------
162
163 void wxApp::WakeUpIdle()
164 {
165 #if wxUSE_THREADS
166 if (!wxThread::IsMain())
167 wxMutexGuiEnter();
168 #endif
169
170 if (g_isIdle)
171 wxapp_install_idle_handler();
172
173 #if wxUSE_THREADS
174 if (!wxThread::IsMain())
175 wxMutexGuiLeave();
176 #endif
177 }
178
179 //-----------------------------------------------------------------------------
180 // local functions
181 //-----------------------------------------------------------------------------
182
183 // the callback functions must be extern "C" to comply with GTK+ declarations
184 extern "C"
185 {
186
187 static gint wxapp_pending_callback( gpointer WXUNUSED(data) )
188 {
189 if (!wxTheApp) return TRUE;
190
191 // When getting called from GDK's time-out handler
192 // we are no longer within GDK's grab on the GUI
193 // thread so we must lock it here ourselves.
194 gdk_threads_enter();
195
196 // Sent idle event to all who request them.
197 wxTheApp->ProcessPendingEvents();
198
199 g_pendingTag = 0;
200
201 // Flush the logged messages if any.
202 #if wxUSE_LOG
203 wxLog::FlushActive();
204 #endif // wxUSE_LOG
205
206 // Release lock again
207 gdk_threads_leave();
208
209 // Return FALSE to indicate that no more idle events are
210 // to be sent (single shot instead of continuous stream)
211 return FALSE;
212 }
213
214 static gint wxapp_idle_callback( gpointer WXUNUSED(data) )
215 {
216 if (!wxTheApp)
217 return TRUE;
218
219 #ifdef __WXDEBUG__
220 // don't generate the idle events while the assert modal dialog is shown,
221 // this completely confuses the apps which don't expect to be reentered
222 // from some safely-looking functions
223 if ( wxTheApp->IsInAssert() )
224 {
225 // But repaint the assertion message if necessary
226 if (wxTopLevelWindows.GetCount() > 0)
227 {
228 wxWindow* win = (wxWindow*) wxTopLevelWindows.GetLast()->GetData();
229 #ifdef __WXGTK20__
230 if (win->IsKindOf(CLASSINFO(wxMessageDialog)))
231 #else
232 if (win->IsKindOf(CLASSINFO(wxGenericMessageDialog)))
233 #endif
234 win->OnInternalIdle();
235 }
236 return TRUE;
237 }
238 #endif // __WXDEBUG__
239
240 // When getting called from GDK's time-out handler
241 // we are no longer within GDK's grab on the GUI
242 // thread so we must lock it here ourselves.
243 gdk_threads_enter();
244
245 // Indicate that we are now in idle mode and event handlers
246 // will have to reinstall the idle handler again.
247 g_isIdle = TRUE;
248 wxTheApp->m_idleTag = 0;
249
250 // Send idle event to all who request them as long as
251 // no events have popped up in the event queue.
252 while (wxTheApp->ProcessIdle() && (gtk_events_pending() == 0))
253 ;
254
255 // Release lock again
256 gdk_threads_leave();
257
258 // Return FALSE to indicate that no more idle events are
259 // to be sent (single shot instead of continuous stream).
260 return FALSE;
261 }
262
263 #if wxUSE_THREADS
264
265 #ifdef HAVE_POLL
266 #define wxPoll poll
267 #define wxPollFd pollfd
268 #else // !HAVE_POLL
269
270 typedef GPollFD wxPollFd;
271
272 int wxPoll(wxPollFd *ufds, unsigned int nfds, int timeout)
273 {
274 // convert timeout from ms to struct timeval (s/us)
275 timeval tv_timeout;
276 tv_timeout.tv_sec = timeout/1000;
277 tv_timeout.tv_usec = (timeout%1000)*1000;
278
279 // remember the highest fd used here
280 int fdMax = -1;
281
282 // and fill the sets for select()
283 fd_set readfds;
284 fd_set writefds;
285 fd_set exceptfds;
286 FD_ZERO(&readfds);
287 FD_ZERO(&writefds);
288 FD_ZERO(&exceptfds);
289
290 unsigned int i;
291 for ( i = 0; i < nfds; i++ )
292 {
293 wxASSERT_MSG( ufds[i].fd < FD_SETSIZE, _T("fd out of range") );
294
295 if ( ufds[i].events & G_IO_IN )
296 FD_SET(ufds[i].fd, &readfds);
297
298 if ( ufds[i].events & G_IO_PRI )
299 FD_SET(ufds[i].fd, &exceptfds);
300
301 if ( ufds[i].events & G_IO_OUT )
302 FD_SET(ufds[i].fd, &writefds);
303
304 if ( ufds[i].fd > fdMax )
305 fdMax = ufds[i].fd;
306 }
307
308 fdMax++;
309 int res = select(fdMax, &readfds, &writefds, &exceptfds, &tv_timeout);
310
311 // translate the results back
312 for ( i = 0; i < nfds; i++ )
313 {
314 ufds[i].revents = 0;
315
316 if ( FD_ISSET(ufds[i].fd, &readfds ) )
317 ufds[i].revents |= G_IO_IN;
318
319 if ( FD_ISSET(ufds[i].fd, &exceptfds ) )
320 ufds[i].revents |= G_IO_PRI;
321
322 if ( FD_ISSET(ufds[i].fd, &writefds ) )
323 ufds[i].revents |= G_IO_OUT;
324 }
325
326 return res;
327 }
328
329 #endif // HAVE_POLL/!HAVE_POLL
330
331 static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout )
332 {
333 gdk_threads_enter();
334
335 wxMutexGuiLeave();
336 g_mainThreadLocked = TRUE;
337
338 // we rely on the fact that glib GPollFD struct is really just pollfd but
339 // I wonder how wise is this in the long term (VZ)
340 gint res = wxPoll( (wxPollFd *) ufds, nfds, timeout );
341
342 wxMutexGuiEnter();
343 g_mainThreadLocked = FALSE;
344
345 gdk_threads_leave();
346
347 return res;
348 }
349
350 #endif // wxUSE_THREADS
351
352 } // extern "C"
353
354 void wxapp_install_idle_handler()
355 {
356 // GD: this assert is raised when using the thread sample (which works)
357 // so the test is probably not so easy. Can widget callbacks be
358 // triggered from child threads and, if so, for which widgets?
359 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
360
361 wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") );
362
363 g_isIdle = FALSE;
364
365 if (g_pendingTag == 0)
366 g_pendingTag = gtk_idle_add_priority( 900, wxapp_pending_callback, (gpointer) NULL );
367
368 // This routine gets called by all event handlers
369 // indicating that the idle is over. It may also
370 // get called from other thread for sending events
371 // to the main thread (and processing these in
372 // idle time). Very low priority.
373 wxTheApp->m_idleTag = gtk_idle_add_priority( 1000, wxapp_idle_callback, (gpointer) NULL );
374 }
375
376 //-----------------------------------------------------------------------------
377 // Access to the root window global
378 //-----------------------------------------------------------------------------
379
380 GtkWidget* wxGetRootWindow()
381 {
382 if (gs_RootWindow == NULL)
383 {
384 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
385 gtk_widget_realize( gs_RootWindow );
386 }
387 return gs_RootWindow;
388 }
389
390 //-----------------------------------------------------------------------------
391 // wxApp
392 //-----------------------------------------------------------------------------
393
394 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
395
396 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
397 EVT_IDLE(wxAppBase::OnIdle)
398 END_EVENT_TABLE()
399
400 wxApp::wxApp()
401 {
402 #ifdef __WXDEBUG__
403 m_isInAssert = FALSE;
404 #endif // __WXDEBUG__
405
406 m_idleTag = 0;
407 wxapp_install_idle_handler();
408
409 #if wxUSE_THREADS
410 g_main_set_poll_func( wxapp_poll_func );
411 #endif
412
413 m_colorCube = (unsigned char*) NULL;
414
415 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
416 m_glVisualInfo = (void *) NULL;
417 }
418
419 wxApp::~wxApp()
420 {
421 if (m_idleTag) gtk_idle_remove( m_idleTag );
422
423 if (m_colorCube) free(m_colorCube);
424 }
425
426 bool wxApp::OnInitGui()
427 {
428 if ( !wxAppBase::OnInitGui() )
429 return FALSE;
430
431 GdkVisual *visual = gdk_visual_get_system();
432
433 // if this is a wxGLApp (derived from wxApp), and we've already
434 // chosen a specific visual, then derive the GdkVisual from that
435 if (m_glVisualInfo != NULL)
436 {
437 #ifdef __WXGTK20__
438 // seems gtk_widget_set_default_visual no longer exists?
439 GdkVisual* vis = gtk_widget_get_default_visual();
440 #else
441 GdkVisual* vis = gdkx_visual_get(
442 ((XVisualInfo *) m_glVisualInfo) ->visualid );
443 gtk_widget_set_default_visual( vis );
444 #endif
445
446 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
447 gtk_widget_set_default_colormap( colormap );
448
449 visual = vis;
450 }
451
452 // On some machines, the default visual is just 256 colours, so
453 // we make sure we get the best. This can sometimes be wasteful.
454
455 else
456 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual))
457 {
458 #ifdef __WXGTK20__
459 /* seems gtk_widget_set_default_visual no longer exists? */
460 GdkVisual* vis = gtk_widget_get_default_visual();
461 #else
462 GdkVisual* vis = gdk_visual_get_best();
463 gtk_widget_set_default_visual( vis );
464 #endif
465
466 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
467 gtk_widget_set_default_colormap( colormap );
468
469 visual = vis;
470 }
471
472 // Nothing to do for 15, 16, 24, 32 bit displays
473 if (visual->depth > 8) return TRUE;
474
475 // initialize color cube for 8-bit color reduction dithering
476
477 GdkColormap *cmap = gtk_widget_get_default_colormap();
478
479 m_colorCube = (unsigned char*)malloc(32 * 32 * 32);
480
481 for (int r = 0; r < 32; r++)
482 {
483 for (int g = 0; g < 32; g++)
484 {
485 for (int b = 0; b < 32; b++)
486 {
487 int rr = (r << 3) | (r >> 2);
488 int gg = (g << 3) | (g >> 2);
489 int bb = (b << 3) | (b >> 2);
490
491 int index = -1;
492
493 GdkColor *colors = cmap->colors;
494 if (colors)
495 {
496 int max = 3 * 65536;
497
498 for (int i = 0; i < cmap->size; i++)
499 {
500 int rdiff = ((rr << 8) - colors[i].red);
501 int gdiff = ((gg << 8) - colors[i].green);
502 int bdiff = ((bb << 8) - colors[i].blue);
503 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
504 if (sum < max)
505 {
506 index = i; max = sum;
507 }
508 }
509 }
510 else
511 {
512 // assume 8-bit true or static colors. this really exists
513 GdkVisual* vis = gdk_colormap_get_visual( cmap );
514 index = (r >> (5 - vis->red_prec)) << vis->red_shift;
515 index |= (g >> (5 - vis->green_prec)) << vis->green_shift;
516 index |= (b >> (5 - vis->blue_prec)) << vis->blue_shift;
517 }
518 m_colorCube[ (r*1024) + (g*32) + b ] = index;
519 }
520 }
521 }
522
523 return TRUE;
524 }
525
526 GdkVisual *wxApp::GetGdkVisual()
527 {
528 GdkVisual *visual = NULL;
529
530 if (m_glVisualInfo)
531 visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid );
532 else
533 visual = gdk_window_get_visual( wxGetRootWindow()->window );
534
535 wxASSERT( visual );
536
537 return visual;
538 }
539
540 bool wxApp::Initialize(int& argc, wxChar **argv)
541 {
542 #if wxUSE_THREADS
543 // GTK 1.2 up to version 1.2.3 has broken threads
544 if ((gtk_major_version == 1) &&
545 (gtk_minor_version == 2) &&
546 (gtk_micro_version < 4))
547 {
548 printf( "wxWindows warning: GUI threading disabled due to outdated GTK version\n" );
549 }
550 else
551 {
552 if (!g_thread_supported())
553 g_thread_init(NULL);
554 }
555 #endif // wxUSE_THREADS
556
557 gtk_set_locale();
558
559 // We should have the wxUSE_WCHAR_T test on the _outside_
560 #if wxUSE_WCHAR_T
561 #if defined(__WXGTK20__)
562 // gtk+ 2.0 supports Unicode through UTF-8 strings
563 wxConvCurrent = &wxConvUTF8;
564 #else // GTK 1.x
565 if (!wxOKlibc())
566 wxConvCurrent = &wxConvLocal;
567 #endif
568 #else // !wxUSE_WCHAR_T
569 if (!wxOKlibc())
570 wxConvCurrent = (wxMBConv*) NULL;
571 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
572
573 #if wxUSE_UNICODE
574 // gtk_init() wants UTF-8, not wchar_t, so convert
575 int i;
576 char **argvGTK = new char *[argc + 1];
577 for ( i = 0; i < argc; i++ )
578 {
579 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv[i]));
580 }
581
582 argvGTK[argc] = NULL;
583
584 int argcGTK = argc;
585 gtk_init( &argcGTK, &argvGTK );
586
587 if ( argcGTK != argc )
588 {
589 // we have to drop the parameters which were consumed by GTK+
590 for ( i = 0; i < argcGTK; i++ )
591 {
592 while ( strcmp(wxConvUTF8.cWX2MB(argv[i]), argvGTK[i]) != 0 )
593 {
594 memmove(argv + i, argv + i + 1, argc - i);
595 }
596 }
597
598 argc = argcGTK;
599 }
600 //else: gtk_init() didn't modify our parameters
601
602 // free our copy
603 for ( i = 0; i < argcGTK; i++ )
604 {
605 free(argvGTK[i]);
606 }
607
608 delete [] argvGTK;
609 #else // !wxUSE_UNICODE
610 // gtk_init() shouldn't actually change argv itself (just its contents) so
611 // it's ok to pass pointer to it
612 gtk_init( &argc, &argv );
613 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
614
615 // we can not enter threads before gtk_init is done
616 gdk_threads_enter();
617
618 if ( !wxAppBase::Initialize(argc, argv) )
619 {
620 gdk_threads_leave();
621
622 return false;
623 }
624
625 wxSetDetectableAutoRepeat( TRUE );
626
627 #if wxUSE_INTL
628 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
629 #endif
630
631 wxGetRootWindow();
632
633 return true;
634 }
635
636 void wxApp::CleanUp()
637 {
638 gdk_threads_leave();
639
640 wxAppBase::CleanUp();
641 }
642
643 #ifdef __WXDEBUG__
644
645 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
646 {
647 m_isInAssert = TRUE;
648
649 wxAppBase::OnAssert(file, line, cond, msg);
650
651 m_isInAssert = FALSE;
652 }
653
654 #endif // __WXDEBUG__
655