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