]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/app.cpp
[ 1579793 ] numerous bug fixes when mask is used
[wxWidgets.git] / src / gtk / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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/memory.h"
26 #include "wx/font.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 #include <unistd.h>
43
44 #ifdef HAVE_POLL
45 #if defined(__VMS)
46 #include <poll.h>
47 #else
48 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
49 // in neither poll.h nor sys/poll.h which results in link errors later
50 #ifdef __OPENBSD__
51 extern "C"
52 {
53 #endif
54
55 #include <sys/poll.h>
56
57 #ifdef __OPENBSD__
58 };
59 #endif
60 #endif // platform
61 #else // !HAVE_POLL
62 // we implement poll() ourselves using select() which is supposed exist in
63 // all modern Unices
64 #include <sys/types.h>
65 #include <sys/time.h>
66 #include <unistd.h>
67 #ifdef HAVE_SYS_SELECT_H
68 #include <sys/select.h>
69 #endif
70 #endif // HAVE_POLL/!HAVE_POLL
71
72 #include "wx/unix/private.h"
73 #include "wx/gtk/win_gtk.h"
74 #include "wx/gtk/private.h"
75
76 #include <gdk/gdkx.h>
77
78 //-----------------------------------------------------------------------------
79 // link GnomeVFS
80 //-----------------------------------------------------------------------------
81
82 #if wxUSE_LIBGNOMEVFS
83 #include "wx/html/forcelnk.h"
84 FORCE_LINK(gnome_vfs)
85 #endif
86
87 //-----------------------------------------------------------------------------
88 // global data
89 //-----------------------------------------------------------------------------
90
91 bool g_mainThreadLocked = false;
92
93 static GtkWidget *gs_RootWindow = (GtkWidget*) NULL;
94
95 //-----------------------------------------------------------------------------
96 // idle system
97 //-----------------------------------------------------------------------------
98
99 void wxapp_install_idle_handler();
100
101 #if wxUSE_THREADS
102 static wxMutex gs_idleTagsMutex;
103 #endif
104
105 //-----------------------------------------------------------------------------
106 // wxYield
107 //-----------------------------------------------------------------------------
108
109 // not static because used by textctrl.cpp
110 //
111 // MT-FIXME
112 bool wxIsInsideYield = false;
113
114 bool wxApp::Yield(bool onlyIfNeeded)
115 {
116 if ( wxIsInsideYield )
117 {
118 if ( !onlyIfNeeded )
119 {
120 wxFAIL_MSG( wxT("wxYield called recursively" ) );
121 }
122
123 return false;
124 }
125
126 #if wxUSE_THREADS
127 if ( !wxThread::IsMain() )
128 {
129 // can't call gtk_main_iteration() from other threads like this
130 return true;
131 }
132 #endif // wxUSE_THREADS
133
134 wxIsInsideYield = true;
135
136 // We need to remove idle callbacks or the loop will
137 // never finish.
138 RemoveIdleSource();
139
140 #if wxUSE_LOG
141 // disable log flushing from here because a call to wxYield() shouldn't
142 // normally result in message boxes popping up &c
143 wxLog::Suspend();
144 #endif
145
146 while (gtk_events_pending())
147 gtk_main_iteration();
148
149 // It's necessary to call ProcessIdle() to update the frames sizes which
150 // might have been changed (it also will update other things set from
151 // OnUpdateUI() which is a nice (and desired) side effect). But we
152 // call ProcessIdle() only once since this is not meant for longish
153 // background jobs (controlled by wxIdleEvent::RequestMore() and the
154 // return value of Processidle().
155 ProcessIdle();
156
157 #if wxUSE_LOG
158 // let the logs be flashed again
159 wxLog::Resume();
160 #endif
161
162 wxIsInsideYield = false;
163
164 return true;
165 }
166
167 //-----------------------------------------------------------------------------
168 // wxWakeUpIdle
169 //-----------------------------------------------------------------------------
170
171 // RR/KH: No wxMutexGui calls are needed here according to the GTK faq,
172 // http://www.gtk.org/faq/#AEN500 - this caused problems for wxPostEvent.
173
174 void wxApp::WakeUpIdle()
175 {
176 wxapp_install_idle_handler();
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 // One-shot emission hook for "event" signal, to install idle handler.
188 // This will be called when the "event" signal is issued on any GtkWidget object.
189 static gboolean
190 event_emission_hook(GSignalInvocationHint*, guint, const GValue*, gpointer)
191 {
192 wxapp_install_idle_handler();
193 // remove hook
194 return false;
195 }
196
197 static inline void wxAddEmissionHook()
198 {
199 // add emission hook for "event" signal, to re-install idle handler when needed
200 guint sig_id = g_signal_lookup("event", GTK_TYPE_WIDGET);
201 g_signal_add_emission_hook(sig_id, 0, event_emission_hook, NULL, NULL);
202 }
203
204 static gint wxapp_idle_callback( gpointer WXUNUSED(data) )
205 {
206 if (!wxTheApp)
207 return false;
208
209 #ifdef __WXDEBUG__
210 // don't generate the idle events while the assert modal dialog is shown,
211 // this completely confuses the apps which don't expect to be reentered
212 // from some safely-looking functions
213 if ( wxTheApp->IsInAssert() )
214 return false;
215 #endif // __WXDEBUG__
216
217 // When getting called from GDK's time-out handler
218 // we are no longer within GDK's grab on the GUI
219 // thread so we must lock it here ourselves.
220 gdk_threads_enter();
221
222 // Indicate that we are now in idle mode and event handlers
223 // will have to reinstall the idle handler again.
224 {
225 #if wxUSE_THREADS
226 wxMutexLocker lock(gs_idleTagsMutex);
227 #endif
228 g_isIdle = true;
229 wxTheApp->m_idleTag = 0;
230 }
231
232 bool moreIdles;
233
234 // Send idle event to all who request them as long as
235 // no events have popped up in the event queue.
236 while ( (moreIdles = wxTheApp->ProcessIdle()) && gtk_events_pending() == 0)
237 ;
238
239 // Release lock again
240 gdk_threads_leave();
241
242 if (!moreIdles)
243 wxAddEmissionHook();
244
245 // Return FALSE if no more idle events are to be sent
246 return moreIdles;
247 }
248
249 #if wxUSE_THREADS
250
251 #ifdef HAVE_POLL
252 #define wxPoll poll
253 #define wxPollFd pollfd
254 #else // !HAVE_POLL
255
256 typedef GPollFD wxPollFd;
257
258 int wxPoll(wxPollFd *ufds, unsigned int nfds, int timeout)
259 {
260 // convert timeout from ms to struct timeval (s/us)
261 timeval tv_timeout;
262 tv_timeout.tv_sec = timeout/1000;
263 tv_timeout.tv_usec = (timeout%1000)*1000;
264
265 // remember the highest fd used here
266 int fdMax = -1;
267
268 // and fill the sets for select()
269 fd_set readfds;
270 fd_set writefds;
271 fd_set exceptfds;
272 wxFD_ZERO(&readfds);
273 wxFD_ZERO(&writefds);
274 wxFD_ZERO(&exceptfds);
275
276 unsigned int i;
277 for ( i = 0; i < nfds; i++ )
278 {
279 wxASSERT_MSG( ufds[i].fd < wxFD_SETSIZE, _T("fd out of range") );
280
281 if ( ufds[i].events & G_IO_IN )
282 wxFD_SET(ufds[i].fd, &readfds);
283
284 if ( ufds[i].events & G_IO_PRI )
285 wxFD_SET(ufds[i].fd, &exceptfds);
286
287 if ( ufds[i].events & G_IO_OUT )
288 wxFD_SET(ufds[i].fd, &writefds);
289
290 if ( ufds[i].fd > fdMax )
291 fdMax = ufds[i].fd;
292 }
293
294 fdMax++;
295 int res = select(fdMax, &readfds, &writefds, &exceptfds, &tv_timeout);
296
297 // translate the results back
298 for ( i = 0; i < nfds; i++ )
299 {
300 ufds[i].revents = 0;
301
302 if ( wxFD_ISSET(ufds[i].fd, &readfds ) )
303 ufds[i].revents |= G_IO_IN;
304
305 if ( wxFD_ISSET(ufds[i].fd, &exceptfds ) )
306 ufds[i].revents |= G_IO_PRI;
307
308 if ( wxFD_ISSET(ufds[i].fd, &writefds ) )
309 ufds[i].revents |= G_IO_OUT;
310 }
311
312 return res;
313 }
314
315 #endif // HAVE_POLL/!HAVE_POLL
316
317 static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout )
318 {
319 gdk_threads_enter();
320
321 wxMutexGuiLeave();
322 g_mainThreadLocked = true;
323
324 // we rely on the fact that glib GPollFD struct is really just pollfd but
325 // I wonder how wise is this in the long term (VZ)
326 gint res = wxPoll( (wxPollFd *) ufds, nfds, timeout );
327
328 wxMutexGuiEnter();
329 g_mainThreadLocked = false;
330
331 gdk_threads_leave();
332
333 return res;
334 }
335
336 #endif // wxUSE_THREADS
337
338 } // extern "C"
339
340 void wxapp_install_idle_handler()
341 {
342 #if wxUSE_THREADS
343 wxMutexLocker lock(gs_idleTagsMutex);
344 #endif
345
346 // Don't install the handler if it's already installed. This test *MUST*
347 // be done when gs_idleTagsMutex is locked!
348 if (!g_isIdle)
349 return;
350
351 // GD: this assert is raised when using the thread sample (which works)
352 // so the test is probably not so easy. Can widget callbacks be
353 // triggered from child threads and, if so, for which widgets?
354 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
355
356 wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") );
357
358 g_isIdle = false;
359
360 // This routine gets called by all event handlers
361 // indicating that the idle is over. It may also
362 // get called from other thread for sending events
363 // to the main thread (and processing these in
364 // idle time). Very low priority.
365 wxTheApp->m_idleTag = g_idle_add_full(G_PRIORITY_LOW, wxapp_idle_callback, NULL, NULL);
366 }
367
368 //-----------------------------------------------------------------------------
369 // Access to the root window global
370 //-----------------------------------------------------------------------------
371
372 GtkWidget* wxGetRootWindow()
373 {
374 if (gs_RootWindow == NULL)
375 {
376 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
377 gtk_widget_realize( gs_RootWindow );
378 }
379 return gs_RootWindow;
380 }
381
382 //-----------------------------------------------------------------------------
383 // wxApp
384 //-----------------------------------------------------------------------------
385
386 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
387
388 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
389 EVT_IDLE(wxAppBase::OnIdle)
390 END_EVENT_TABLE()
391
392 wxApp::wxApp()
393 {
394 #ifdef __WXDEBUG__
395 m_isInAssert = false;
396 #endif // __WXDEBUG__
397
398 m_idleTag = 0;
399 g_isIdle = true;
400 wxapp_install_idle_handler();
401
402 #if wxUSE_THREADS
403 g_main_context_set_poll_func( NULL, wxapp_poll_func );
404 #endif
405
406 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
407 m_glVisualInfo = (void *) NULL;
408 m_glFBCInfo = (void *) NULL;
409 }
410
411 wxApp::~wxApp()
412 {
413 if (m_idleTag)
414 g_source_remove( m_idleTag );
415 }
416
417 bool wxApp::OnInitGui()
418 {
419 if ( !wxAppBase::OnInitGui() )
420 return false;
421
422 // if this is a wxGLApp (derived from wxApp), and we've already
423 // chosen a specific visual, then derive the GdkVisual from that
424 if (m_glVisualInfo != NULL)
425 {
426 // seems gtk_widget_set_default_visual no longer exists?
427 GdkVisual* vis = gtk_widget_get_default_visual();
428
429 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
430 gtk_widget_set_default_colormap( colormap );
431 }
432
433 // On some machines, the default visual is just 256 colours, so
434 // we make sure we get the best. This can sometimes be wasteful.
435
436 else
437 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual))
438 {
439 /* seems gtk_widget_set_default_visual no longer exists? */
440 GdkVisual* vis = gtk_widget_get_default_visual();
441
442 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
443 gtk_widget_set_default_colormap( colormap );
444 }
445
446 return true;
447 }
448
449 GdkVisual *wxApp::GetGdkVisual()
450 {
451 GdkVisual *visual = NULL;
452
453 if (m_glVisualInfo)
454 visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid );
455 else
456 visual = gdk_drawable_get_visual( wxGetRootWindow()->window );
457
458 wxASSERT( visual );
459
460 return visual;
461 }
462
463 bool wxApp::Initialize(int& argc, wxChar **argv)
464 {
465 bool init_result;
466
467 #if wxUSE_THREADS
468 if (!g_thread_supported())
469 g_thread_init(NULL);
470 #endif // wxUSE_THREADS
471
472 gtk_set_locale();
473
474 // We should have the wxUSE_WCHAR_T test on the _outside_
475 #if wxUSE_WCHAR_T
476 // gtk+ 2.0 supports Unicode through UTF-8 strings
477 wxConvCurrent = &wxConvUTF8;
478 #else // !wxUSE_WCHAR_T
479 if (!wxOKlibc())
480 wxConvCurrent = (wxMBConv*) NULL;
481 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
482
483 // decide which conversion to use for the file names
484
485 // (1) this variable exists for the sole purpose of specifying the encoding
486 // of the filenames for GTK+ programs, so use it if it is set
487 wxString encName(wxGetenv(_T("G_FILENAME_ENCODING")));
488 encName = encName.BeforeFirst(_T(','));
489 if (encName == _T("@locale"))
490 encName.clear();
491 encName.MakeUpper();
492 #if wxUSE_INTL
493 if (encName.empty())
494 {
495 // (2) if a non default locale is set, assume that the user wants his
496 // filenames in this locale too
497 encName = wxLocale::GetSystemEncodingName().Upper();
498 // (3) finally use UTF-8 by default
499 if (encName.empty() || encName == _T("US-ASCII"))
500 encName = _T("UTF-8");
501 wxSetEnv(_T("G_FILENAME_ENCODING"), encName);
502 }
503 #else
504 if (encName.empty())
505 encName = _T("UTF-8");
506 #endif // wxUSE_INTL
507 static wxConvBrokenFileNames fileconv(encName);
508 wxConvFileName = &fileconv;
509
510 #if wxUSE_UNICODE
511 // gtk_init() wants UTF-8, not wchar_t, so convert
512 int i;
513 char **argvGTK = new char *[argc + 1];
514 for ( i = 0; i < argc; i++ )
515 {
516 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv[i]));
517 }
518
519 argvGTK[argc] = NULL;
520
521 int argcGTK = argc;
522
523 #ifdef __WXGPE__
524 init_result = true; // is there a _check() version of this?
525 gpe_application_init( &argcGTK, &argvGTK );
526 #else
527 init_result = gtk_init_check( &argcGTK, &argvGTK );
528 #endif
529
530 if ( argcGTK != argc )
531 {
532 // we have to drop the parameters which were consumed by GTK+
533 for ( i = 0; i < argcGTK; i++ )
534 {
535 while ( strcmp(wxConvUTF8.cWX2MB(argv[i]), argvGTK[i]) != 0 )
536 {
537 memmove(argv + i, argv + i + 1, argc - i);
538 }
539 }
540
541 argc = argcGTK;
542 }
543 //else: gtk_init() didn't modify our parameters
544
545 // free our copy
546 for ( i = 0; i < argcGTK; i++ )
547 {
548 free(argvGTK[i]);
549 }
550
551 delete [] argvGTK;
552 #else // !wxUSE_UNICODE
553 // gtk_init() shouldn't actually change argv itself (just its contents) so
554 // it's ok to pass pointer to it
555 init_result = gtk_init_check( &argc, &argv );
556 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
557
558 if (!init_result) {
559 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
560 return false;
561 }
562
563 // we can not enter threads before gtk_init is done
564 gdk_threads_enter();
565
566 if ( !wxAppBase::Initialize(argc, argv) )
567 {
568 gdk_threads_leave();
569
570 return false;
571 }
572
573 wxSetDetectableAutoRepeat( true );
574
575 #if wxUSE_INTL
576 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
577 #endif
578
579 return true;
580 }
581
582 void wxApp::CleanUp()
583 {
584 gdk_threads_leave();
585
586 wxAppBase::CleanUp();
587 }
588
589 #ifdef __WXDEBUG__
590
591 void wxApp::OnAssertFailure(const wxChar *file,
592 int line,
593 const wxChar* func,
594 const wxChar* cond,
595 const wxChar *msg)
596 {
597 m_isInAssert = true;
598
599 wxAppBase::OnAssertFailure(file, line, func, cond, msg);
600
601 m_isInAssert = false;
602 }
603
604 #endif // __WXDEBUG__
605
606 void wxApp::RemoveIdleSource()
607 {
608 #if wxUSE_THREADS
609 wxMutexLocker lock(gs_idleTagsMutex);
610 #endif
611 if (m_idleTag != 0)
612 {
613 g_source_remove(m_idleTag);
614 m_idleTag = 0;
615 g_isIdle = true;
616 wxAddEmissionHook();
617 }
618 }