]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/app.cpp
05cf17cf560bee62fd71a979b0891dc192735338
[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 // add emission hook for "event" signal, to re-install idle handler when needed
198 static inline void wxAddEmissionHook()
199 {
200 GType widgetType = GTK_TYPE_WIDGET;
201 // if GtkWidget type is loaded
202 if (g_type_class_peek(widgetType) != NULL)
203 {
204 guint sig_id = g_signal_lookup("event", widgetType);
205 g_signal_add_emission_hook(sig_id, 0, event_emission_hook, NULL, NULL);
206 }
207 }
208
209 static gint wxapp_idle_callback( gpointer WXUNUSED(data) )
210 {
211 // this does not look possible, but just in case...
212 if (!wxTheApp)
213 return false;
214
215 bool moreIdles = false;
216
217 #ifdef __WXDEBUG__
218 // don't generate the idle events while the assert modal dialog is shown,
219 // this matches the behavior of wxMSW
220 if (!wxTheApp->IsInAssert())
221 #endif // __WXDEBUG__
222 {
223 // When getting called from GDK's time-out handler
224 // we are no longer within GDK's grab on the GUI
225 // thread so we must lock it here ourselves.
226 gdk_threads_enter();
227
228 // Send idle event to all who request them as long as
229 // no events have popped up in the event queue.
230 do {
231 moreIdles = wxTheApp->ProcessIdle();
232 } while (moreIdles && gtk_events_pending() == 0);
233
234 // Release lock again
235 gdk_threads_leave();
236 }
237
238 if (!moreIdles)
239 {
240 #if wxUSE_THREADS
241 wxMutexLocker lock(gs_idleTagsMutex);
242 #endif
243 // Indicate that we are now in idle mode and event handlers
244 // will have to reinstall the idle handler again.
245 g_isIdle = true;
246 wxTheApp->m_idleTag = 0;
247
248 wxAddEmissionHook();
249 }
250
251 // Return FALSE if no more idle events are to be sent
252 return moreIdles;
253 }
254
255 #if wxUSE_THREADS
256
257 #ifdef HAVE_POLL
258 #define wxPoll poll
259 #define wxPollFd pollfd
260 #else // !HAVE_POLL
261
262 typedef GPollFD wxPollFd;
263
264 int wxPoll(wxPollFd *ufds, unsigned int nfds, int timeout)
265 {
266 // convert timeout from ms to struct timeval (s/us)
267 timeval tv_timeout;
268 tv_timeout.tv_sec = timeout/1000;
269 tv_timeout.tv_usec = (timeout%1000)*1000;
270
271 // remember the highest fd used here
272 int fdMax = -1;
273
274 // and fill the sets for select()
275 fd_set readfds;
276 fd_set writefds;
277 fd_set exceptfds;
278 wxFD_ZERO(&readfds);
279 wxFD_ZERO(&writefds);
280 wxFD_ZERO(&exceptfds);
281
282 unsigned int i;
283 for ( i = 0; i < nfds; i++ )
284 {
285 wxASSERT_MSG( ufds[i].fd < FD_SETSIZE, _T("fd out of range") );
286
287 if ( ufds[i].events & G_IO_IN )
288 wxFD_SET(ufds[i].fd, &readfds);
289
290 if ( ufds[i].events & G_IO_PRI )
291 wxFD_SET(ufds[i].fd, &exceptfds);
292
293 if ( ufds[i].events & G_IO_OUT )
294 wxFD_SET(ufds[i].fd, &writefds);
295
296 if ( ufds[i].fd > fdMax )
297 fdMax = ufds[i].fd;
298 }
299
300 fdMax++;
301 int res = select(fdMax, &readfds, &writefds, &exceptfds, &tv_timeout);
302
303 // translate the results back
304 for ( i = 0; i < nfds; i++ )
305 {
306 ufds[i].revents = 0;
307
308 if ( wxFD_ISSET(ufds[i].fd, &readfds ) )
309 ufds[i].revents |= G_IO_IN;
310
311 if ( wxFD_ISSET(ufds[i].fd, &exceptfds ) )
312 ufds[i].revents |= G_IO_PRI;
313
314 if ( wxFD_ISSET(ufds[i].fd, &writefds ) )
315 ufds[i].revents |= G_IO_OUT;
316 }
317
318 return res;
319 }
320
321 #endif // HAVE_POLL/!HAVE_POLL
322
323 static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout )
324 {
325 gdk_threads_enter();
326
327 wxMutexGuiLeave();
328 g_mainThreadLocked = true;
329
330 // we rely on the fact that glib GPollFD struct is really just pollfd but
331 // I wonder how wise is this in the long term (VZ)
332 gint res = wxPoll( (wxPollFd *) ufds, nfds, timeout );
333
334 wxMutexGuiEnter();
335 g_mainThreadLocked = false;
336
337 gdk_threads_leave();
338
339 return res;
340 }
341
342 #endif // wxUSE_THREADS
343
344 } // extern "C"
345
346 void wxapp_install_idle_handler()
347 {
348 if (wxTheApp == NULL)
349 return;
350
351 #if wxUSE_THREADS
352 wxMutexLocker lock(gs_idleTagsMutex);
353 #endif
354
355 // Don't install the handler if it's already installed. This test *MUST*
356 // be done when gs_idleTagsMutex is locked!
357 if (!g_isIdle)
358 return;
359
360 // GD: this assert is raised when using the thread sample (which works)
361 // so the test is probably not so easy. Can widget callbacks be
362 // triggered from child threads and, if so, for which widgets?
363 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
364
365 wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") );
366
367 g_isIdle = false;
368
369 // This routine gets called by all event handlers
370 // indicating that the idle is over. It may also
371 // get called from other thread for sending events
372 // to the main thread (and processing these in
373 // idle time). Very low priority.
374 wxTheApp->m_idleTag = g_idle_add_full(G_PRIORITY_LOW, wxapp_idle_callback, NULL, NULL);
375 }
376
377 //-----------------------------------------------------------------------------
378 // Access to the root window global
379 //-----------------------------------------------------------------------------
380
381 GtkWidget* wxGetRootWindow()
382 {
383 if (gs_RootWindow == NULL)
384 {
385 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
386 gtk_widget_realize( gs_RootWindow );
387 }
388 return gs_RootWindow;
389 }
390
391 //-----------------------------------------------------------------------------
392 // wxApp
393 //-----------------------------------------------------------------------------
394
395 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
396
397 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
398 EVT_IDLE(wxAppBase::OnIdle)
399 END_EVENT_TABLE()
400
401 wxApp::wxApp()
402 {
403 #ifdef __WXDEBUG__
404 m_isInAssert = false;
405 #endif // __WXDEBUG__
406
407 m_idleTag = 0;
408 g_isIdle = true;
409 wxapp_install_idle_handler();
410
411 #if wxUSE_THREADS
412 g_main_context_set_poll_func( NULL, wxapp_poll_func );
413 #endif
414
415 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
416 m_glVisualInfo = (void *) NULL;
417 m_glFBCInfo = (void *) NULL;
418 }
419
420 wxApp::~wxApp()
421 {
422 if (m_idleTag)
423 g_source_remove( m_idleTag );
424 }
425
426 bool wxApp::OnInitGui()
427 {
428 if ( !wxAppBase::OnInitGui() )
429 return false;
430
431 // if this is a wxGLApp (derived from wxApp), and we've already
432 // chosen a specific visual, then derive the GdkVisual from that
433 if (m_glVisualInfo != NULL)
434 {
435 GdkVisual* vis = gtk_widget_get_default_visual();
436
437 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
438 gtk_widget_set_default_colormap( colormap );
439 }
440 else
441 {
442 // On some machines, the default visual is just 256 colours, so
443 // we make sure we get the best. This can sometimes be wasteful.
444 if (m_useBestVisual)
445 {
446 if (m_forceTrueColour)
447 {
448 GdkVisual* visual = gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR );
449 if (!visual)
450 {
451 wxLogError(wxT("Unable to initialize TrueColor visual."));
452 return false;
453 }
454 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
455 gtk_widget_set_default_colormap( colormap );
456 }
457 else
458 {
459 if (gdk_visual_get_best() != gdk_visual_get_system())
460 {
461 GdkVisual* visual = gdk_visual_get_best();
462 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
463 gtk_widget_set_default_colormap( colormap );
464 }
465 }
466 }
467 }
468
469 return true;
470 }
471
472 GdkVisual *wxApp::GetGdkVisual()
473 {
474 GdkVisual *visual = NULL;
475
476 if (m_glVisualInfo)
477 visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid );
478 else
479 visual = gdk_drawable_get_visual( wxGetRootWindow()->window );
480
481 wxASSERT( visual );
482
483 return visual;
484 }
485
486 bool wxApp::Initialize(int& argc, wxChar **argv)
487 {
488 bool init_result;
489
490 #if wxUSE_THREADS
491 if (!g_thread_supported())
492 g_thread_init(NULL);
493 #endif // wxUSE_THREADS
494
495 gtk_set_locale();
496
497 // We should have the wxUSE_WCHAR_T test on the _outside_
498 #if wxUSE_WCHAR_T
499 // gtk+ 2.0 supports Unicode through UTF-8 strings
500 wxConvCurrent = &wxConvUTF8;
501 #else // !wxUSE_WCHAR_T
502 if (!wxOKlibc())
503 wxConvCurrent = (wxMBConv*) NULL;
504 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
505
506 // decide which conversion to use for the file names
507
508 // (1) this variable exists for the sole purpose of specifying the encoding
509 // of the filenames for GTK+ programs, so use it if it is set
510 wxString encName(wxGetenv(_T("G_FILENAME_ENCODING")));
511 encName = encName.BeforeFirst(_T(','));
512 if (encName.CmpNoCase(_T("@locale")) == 0)
513 encName.clear();
514 encName.MakeUpper();
515 #if wxUSE_INTL
516 if (encName.empty())
517 {
518 // (2) if a non default locale is set, assume that the user wants his
519 // filenames in this locale too
520 encName = wxLocale::GetSystemEncodingName().Upper();
521 // (3) finally use UTF-8 by default
522 if (encName.empty() || encName == _T("US-ASCII"))
523 encName = _T("UTF-8");
524 wxSetEnv(_T("G_FILENAME_ENCODING"), encName);
525 }
526 #else
527 if (encName.empty())
528 encName = _T("UTF-8");
529 #endif // wxUSE_INTL
530 static wxConvBrokenFileNames fileconv(encName);
531 wxConvFileName = &fileconv;
532
533 #if wxUSE_UNICODE
534 // gtk_init() wants UTF-8, not wchar_t, so convert
535 int i;
536 char **argvGTK = new char *[argc + 1];
537 for ( i = 0; i < argc; i++ )
538 {
539 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv[i]));
540 }
541
542 argvGTK[argc] = NULL;
543
544 int argcGTK = argc;
545
546 #ifdef __WXGPE__
547 init_result = true; // is there a _check() version of this?
548 gpe_application_init( &argcGTK, &argvGTK );
549 #else
550 init_result = gtk_init_check( &argcGTK, &argvGTK );
551 #endif
552
553 if ( argcGTK != argc )
554 {
555 // we have to drop the parameters which were consumed by GTK+
556 for ( i = 0; i < argcGTK; i++ )
557 {
558 while ( strcmp(wxConvUTF8.cWX2MB(argv[i]), argvGTK[i]) != 0 )
559 {
560 memmove(argv + i, argv + i + 1, argc - i);
561 }
562 }
563
564 argc = argcGTK;
565 }
566 //else: gtk_init() didn't modify our parameters
567
568 // free our copy
569 for ( i = 0; i < argcGTK; i++ )
570 {
571 free(argvGTK[i]);
572 }
573
574 delete [] argvGTK;
575 #else // !wxUSE_UNICODE
576 // gtk_init() shouldn't actually change argv itself (just its contents) so
577 // it's ok to pass pointer to it
578 init_result = gtk_init_check( &argc, &argv );
579 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
580
581 if (!init_result) {
582 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
583 return false;
584 }
585
586 // we can not enter threads before gtk_init is done
587 gdk_threads_enter();
588
589 if ( !wxAppBase::Initialize(argc, argv) )
590 {
591 gdk_threads_leave();
592
593 return false;
594 }
595
596 wxSetDetectableAutoRepeat( true );
597
598 #if wxUSE_INTL
599 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
600 #endif
601
602 return true;
603 }
604
605 void wxApp::CleanUp()
606 {
607 gdk_threads_leave();
608
609 wxAppBase::CleanUp();
610 }
611
612 #ifdef __WXDEBUG__
613
614 void wxApp::OnAssertFailure(const wxChar *file,
615 int line,
616 const wxChar* func,
617 const wxChar* cond,
618 const wxChar *msg)
619 {
620
621 // block wx idle events while assert dialog is showing
622 m_isInAssert = true;
623
624 wxAppBase::OnAssertFailure(file, line, func, cond, msg);
625
626 m_isInAssert = false;
627 }
628
629 #endif // __WXDEBUG__
630
631 void wxApp::RemoveIdleSource()
632 {
633 #if wxUSE_THREADS
634 wxMutexLocker lock(gs_idleTagsMutex);
635 #endif
636 if (m_idleTag != 0)
637 {
638 g_source_remove(m_idleTag);
639 m_idleTag = 0;
640 g_isIdle = true;
641 wxAddEmissionHook();
642 }
643 }