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