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