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