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