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