added support for gcc precompiled headers
[wxWidgets.git] / src / gtk / 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 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __VMS
18 #include <vms_jackets.h>
19 #undef ConnectionNumber
20 #endif
21
22 #include "wx/app.h"
23 #include "wx/gdicmn.h"
24 #include "wx/utils.h"
25 #include "wx/intl.h"
26 #include "wx/log.h"
27 #include "wx/memory.h"
28 #include "wx/font.h"
29 #include "wx/settings.h"
30 #include "wx/dialog.h"
31 #include "wx/msgdlg.h"
32 #include "wx/file.h"
33 #include "wx/filename.h"
34 #include "wx/module.h"
35 #include "wx/image.h"
36
37 #ifdef __WXUNIVERSAL__
38 #include "wx/univ/theme.h"
39 #include "wx/univ/renderer.h"
40 #endif
41
42 #if wxUSE_THREADS
43 #include "wx/thread.h"
44 #endif
45
46 #include <unistd.h>
47
48 #ifdef HAVE_POLL
49 #if defined(__VMS)
50 #include <poll.h>
51 #else
52 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
53 // in neither poll.h nor sys/poll.h which results in link errors later
54 #ifdef __OPENBSD__
55 extern "C"
56 {
57 #endif
58
59 #include <sys/poll.h>
60
61 #ifdef __OPENBSD__
62 };
63 #endif
64 #endif // platform
65 #else // !HAVE_POLL
66 // we implement poll() ourselves using select() which is supposed exist in
67 // all modern Unices
68 #include <sys/types.h>
69 #include <sys/time.h>
70 #include <unistd.h>
71 #endif // HAVE_POLL/!HAVE_POLL
72
73 #include "wx/gtk/win_gtk.h"
74
75 #include <gtk/gtk.h>
76
77
78 //-----------------------------------------------------------------------------
79 // global data
80 //-----------------------------------------------------------------------------
81
82 bool g_mainThreadLocked = FALSE;
83 gint g_pendingTag = 0;
84
85 static GtkWidget *gs_RootWindow = (GtkWidget*) NULL;
86
87 //-----------------------------------------------------------------------------
88 // idle system
89 //-----------------------------------------------------------------------------
90
91 extern bool g_isIdle;
92
93 void wxapp_install_idle_handler();
94
95 //-----------------------------------------------------------------------------
96 // wxYield
97 //-----------------------------------------------------------------------------
98
99 // not static because used by textctrl.cpp
100 //
101 // MT-FIXME
102 bool wxIsInsideYield = FALSE;
103
104 bool wxApp::Yield(bool onlyIfNeeded)
105 {
106 if ( wxIsInsideYield )
107 {
108 if ( !onlyIfNeeded )
109 {
110 wxFAIL_MSG( wxT("wxYield called recursively" ) );
111 }
112
113 return FALSE;
114 }
115
116 #if wxUSE_THREADS
117 if ( !wxThread::IsMain() )
118 {
119 // can't call gtk_main_iteration() from other threads like this
120 return TRUE;
121 }
122 #endif // wxUSE_THREADS
123
124 wxIsInsideYield = TRUE;
125
126 if (!g_isIdle)
127 {
128 // We need to remove idle callbacks or the loop will
129 // never finish.
130 gtk_idle_remove( m_idleTag );
131 m_idleTag = 0;
132 g_isIdle = TRUE;
133 }
134
135 // disable log flushing from here because a call to wxYield() shouldn't
136 // normally result in message boxes popping up &c
137 wxLog::Suspend();
138
139 while (gtk_events_pending())
140 gtk_main_iteration();
141
142 // It's necessary to call ProcessIdle() to update the frames sizes which
143 // might have been changed (it also will update other things set from
144 // OnUpdateUI() which is a nice (and desired) side effect). But we
145 // call ProcessIdle() only once since this is not meant for longish
146 // background jobs (controlled by wxIdleEvent::RequestMore() and the
147 // return value of Processidle().
148 ProcessIdle();
149
150 // let the logs be flashed again
151 wxLog::Resume();
152
153 wxIsInsideYield = FALSE;
154
155 return TRUE;
156 }
157
158 //-----------------------------------------------------------------------------
159 // wxWakeUpIdle
160 //-----------------------------------------------------------------------------
161
162 void wxApp::WakeUpIdle()
163 {
164 #if wxUSE_THREADS
165 if (!wxThread::IsMain())
166 wxMutexGuiEnter();
167 #endif
168
169 if (g_isIdle)
170 wxapp_install_idle_handler();
171
172 #if wxUSE_THREADS
173 if (!wxThread::IsMain())
174 wxMutexGuiLeave();
175 #endif
176 }
177
178 //-----------------------------------------------------------------------------
179 // local functions
180 //-----------------------------------------------------------------------------
181
182 // the callback functions must be extern "C" to comply with GTK+ declarations
183 extern "C"
184 {
185
186 static gint wxapp_pending_callback( gpointer WXUNUSED(data) )
187 {
188 if (!wxTheApp) return TRUE;
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 // Sent idle event to all who request them.
196 wxTheApp->ProcessPendingEvents();
197
198 g_pendingTag = 0;
199
200 // Flush the logged messages if any.
201 #if wxUSE_LOG
202 wxLog::FlushActive();
203 #endif // wxUSE_LOG
204
205 // Release lock again
206 gdk_threads_leave();
207
208 // Return FALSE to indicate that no more idle events are
209 // to be sent (single shot instead of continuous stream)
210 return FALSE;
211 }
212
213 static gint wxapp_idle_callback( gpointer WXUNUSED(data) )
214 {
215 if (!wxTheApp)
216 return TRUE;
217
218 #ifdef __WXDEBUG__
219 // don't generate the idle events while the assert modal dialog is shown,
220 // this completely confuses the apps which don't expect to be reentered
221 // from some safely-looking functions
222 if ( wxTheApp->IsInAssert() )
223 {
224 // But repaint the assertion message if necessary
225 if (wxTopLevelWindows.GetCount() > 0)
226 {
227 wxWindow* win = (wxWindow*) wxTopLevelWindows.GetLast()->GetData();
228 #ifdef __WXGTK20__
229 if (win->IsKindOf(CLASSINFO(wxMessageDialog)))
230 #else
231 if (win->IsKindOf(CLASSINFO(wxGenericMessageDialog)))
232 #endif
233 win->OnInternalIdle();
234 }
235 return TRUE;
236 }
237 #endif // __WXDEBUG__
238
239 // When getting called from GDK's time-out handler
240 // we are no longer within GDK's grab on the GUI
241 // thread so we must lock it here ourselves.
242 gdk_threads_enter();
243
244 // Indicate that we are now in idle mode and event handlers
245 // will have to reinstall the idle handler again.
246 g_isIdle = TRUE;
247 wxTheApp->m_idleTag = 0;
248
249 // Send idle event to all who request them as long as
250 // no events have popped up in the event queue.
251 while (wxTheApp->ProcessIdle() && (gtk_events_pending() == 0))
252 ;
253
254 // Release lock again
255 gdk_threads_leave();
256
257 // Return FALSE to indicate that no more idle events are
258 // to be sent (single shot instead of continuous stream).
259 return FALSE;
260 }
261
262 #if wxUSE_THREADS
263
264 #ifdef HAVE_POLL
265 #define wxPoll poll
266 #define wxPollFd pollfd
267 #else // !HAVE_POLL
268
269 typedef GPollFD wxPollFd;
270
271 int wxPoll(wxPollFd *ufds, unsigned int nfds, int timeout)
272 {
273 // convert timeout from ms to struct timeval (s/us)
274 timeval tv_timeout;
275 tv_timeout.tv_sec = timeout/1000;
276 tv_timeout.tv_usec = (timeout%1000)*1000;
277
278 // remember the highest fd used here
279 int fdMax = -1;
280
281 // and fill the sets for select()
282 fd_set readfds;
283 fd_set writefds;
284 fd_set exceptfds;
285 FD_ZERO(&readfds);
286 FD_ZERO(&writefds);
287 FD_ZERO(&exceptfds);
288
289 unsigned int i;
290 for ( i = 0; i < nfds; i++ )
291 {
292 wxASSERT_MSG( ufds[i].fd < FD_SETSIZE, _T("fd out of range") );
293
294 if ( ufds[i].events & G_IO_IN )
295 FD_SET(ufds[i].fd, &readfds);
296
297 if ( ufds[i].events & G_IO_PRI )
298 FD_SET(ufds[i].fd, &exceptfds);
299
300 if ( ufds[i].events & G_IO_OUT )
301 FD_SET(ufds[i].fd, &writefds);
302
303 if ( ufds[i].fd > fdMax )
304 fdMax = ufds[i].fd;
305 }
306
307 fdMax++;
308 int res = select(fdMax, &readfds, &writefds, &exceptfds, &tv_timeout);
309
310 // translate the results back
311 for ( i = 0; i < nfds; i++ )
312 {
313 ufds[i].revents = 0;
314
315 if ( FD_ISSET(ufds[i].fd, &readfds ) )
316 ufds[i].revents |= G_IO_IN;
317
318 if ( FD_ISSET(ufds[i].fd, &exceptfds ) )
319 ufds[i].revents |= G_IO_PRI;
320
321 if ( FD_ISSET(ufds[i].fd, &writefds ) )
322 ufds[i].revents |= G_IO_OUT;
323 }
324
325 return res;
326 }
327
328 #endif // HAVE_POLL/!HAVE_POLL
329
330 static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout )
331 {
332 gdk_threads_enter();
333
334 wxMutexGuiLeave();
335 g_mainThreadLocked = TRUE;
336
337 // we rely on the fact that glib GPollFD struct is really just pollfd but
338 // I wonder how wise is this in the long term (VZ)
339 gint res = wxPoll( (wxPollFd *) ufds, nfds, timeout );
340
341 wxMutexGuiEnter();
342 g_mainThreadLocked = FALSE;
343
344 gdk_threads_leave();
345
346 return res;
347 }
348
349 #endif // wxUSE_THREADS
350
351 } // extern "C"
352
353 void wxapp_install_idle_handler()
354 {
355 // GD: this assert is raised when using the thread sample (which works)
356 // so the test is probably not so easy. Can widget callbacks be
357 // triggered from child threads and, if so, for which widgets?
358 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
359
360 wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") );
361
362 g_isIdle = FALSE;
363
364 if (g_pendingTag == 0)
365 g_pendingTag = gtk_idle_add_priority( 900, wxapp_pending_callback, (gpointer) NULL );
366
367 // This routine gets called by all event handlers
368 // indicating that the idle is over. It may also
369 // get called from other thread for sending events
370 // to the main thread (and processing these in
371 // idle time). Very low priority.
372 wxTheApp->m_idleTag = gtk_idle_add_priority( 1000, wxapp_idle_callback, (gpointer) NULL );
373 }
374
375 //-----------------------------------------------------------------------------
376 // Access to the root window global
377 //-----------------------------------------------------------------------------
378
379 GtkWidget* wxGetRootWindow()
380 {
381 if (gs_RootWindow == NULL)
382 {
383 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
384 gtk_widget_realize( gs_RootWindow );
385 }
386 return gs_RootWindow;
387 }
388
389 //-----------------------------------------------------------------------------
390 // wxApp
391 //-----------------------------------------------------------------------------
392
393 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
394
395 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
396 EVT_IDLE(wxAppBase::OnIdle)
397 END_EVENT_TABLE()
398
399 wxApp::wxApp()
400 {
401 m_initialized = FALSE;
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 int wxApp::MainLoop()
541 {
542 gtk_main();
543 return 0;
544 }
545
546 void wxApp::Exit()
547 {
548 // VZ: no idea why is it different from ExitMainLoop() but this is what
549 // wxExit() used to do
550 gtk_main_quit();
551 }
552
553 void wxApp::ExitMainLoop()
554 {
555 if (gtk_main_level() > 0)
556 gtk_main_quit();
557 }
558
559 bool wxApp::Initialized()
560 {
561 return m_initialized;
562 }
563
564 bool wxApp::Pending()
565 {
566 return (gtk_events_pending() > 0);
567 }
568
569 void wxApp::Dispatch()
570 {
571 gtk_main_iteration();
572 }
573
574 bool wxApp::Initialize(int& argc, wxChar **argv)
575 {
576 #if wxUSE_THREADS
577 // GTK 1.2 up to version 1.2.3 has broken threads
578 if ((gtk_major_version == 1) &&
579 (gtk_minor_version == 2) &&
580 (gtk_micro_version < 4))
581 {
582 printf( "wxWindows warning: GUI threading disabled due to outdated GTK version\n" );
583 }
584 else
585 {
586 g_thread_init(NULL);
587 }
588 #endif // wxUSE_THREADS
589
590 gtk_set_locale();
591
592 // We should have the wxUSE_WCHAR_T test on the _outside_
593 #if wxUSE_WCHAR_T
594 #if defined(__WXGTK20__)
595 // gtk+ 2.0 supports Unicode through UTF-8 strings
596 wxConvCurrent = &wxConvUTF8;
597 #else // GTK 1.x
598 if (!wxOKlibc())
599 wxConvCurrent = &wxConvLocal;
600 #endif
601 #else // !wxUSE_WCHAR_T
602 if (!wxOKlibc())
603 wxConvCurrent = (wxMBConv*) NULL;
604 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
605
606 #if wxUSE_UNICODE
607 // gtk_init() wants UTF-8, not wchar_t, so convert
608 int i;
609 char **argvGTK = new char *[argc + 1];
610 for ( i = 0; i < argc; i++ )
611 {
612 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv[i]));
613 }
614
615 argvGTK[argc] = NULL;
616
617 int argcGTK = argc;
618 gtk_init( &argcGTK, &argvGTK );
619
620 if ( argcGTK != argc )
621 {
622 // we have to drop the parameters which were consumed by GTK+
623 for ( i = 0; i < argcGTK; i++ )
624 {
625 while ( strcmp(wxConvUTF8.cWX2MB(argv[i]), argvGTK[i]) != 0 )
626 {
627 memmove(argv + i, argv + i + 1, argc - i);
628 }
629 }
630
631 argc = argcGTK;
632 }
633 //else: gtk_init() didn't modify our parameters
634
635 // free our copy
636 for ( i = 0; i < argcGTK; i++ )
637 {
638 free(argvGTK[i]);
639 }
640
641 delete [] argvGTK;
642 #else // !wxUSE_UNICODE
643 // gtk_init() shouldn't actually change argv itself (just its contents) so
644 // it's ok to pass pointer to it
645 gtk_init( &argc, &argv );
646 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
647
648 // we can not enter threads before gtk_init is done
649 gdk_threads_enter();
650
651 if ( !wxAppBase::Initialize(argc, argv) )
652 {
653 gdk_threads_leave();
654
655 return false;
656 }
657
658 wxSetDetectableAutoRepeat( TRUE );
659
660 #if wxUSE_INTL
661 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
662 #endif
663
664 wxGetRootWindow();
665
666 return true;
667 }
668
669 void wxApp::CleanUp()
670 {
671 gdk_threads_leave();
672
673 wxAppBase::CleanUp();
674 }
675
676 #ifdef __WXDEBUG__
677
678 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
679 {
680 m_isInAssert = TRUE;
681
682 wxAppBase::OnAssert(file, line, cond, msg);
683
684 m_isInAssert = FALSE;
685 }
686
687 #endif // __WXDEBUG__
688