1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/app.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 // vms_jackets.h should for proper working be included before anything else
12 # include <vms_jackets.h>
13 #undef ConnectionNumber
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/memory.h"
29 #include "wx/thread.h"
35 #include "wx/gtk/win_gtk.h"
36 #include "wx/gtk/private.h"
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 #if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
46 wxFORCE_LINK_MODULE(gnome_vfs
)
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 bool g_mainThreadLocked
= false;
55 static GtkWidget
*gs_RootWindow
= (GtkWidget
*) NULL
;
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 void wxapp_install_idle_handler();
64 static wxMutex gs_idleTagsMutex
;
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 // not static because used by textctrl.cpp
74 bool wxIsInsideYield
= false;
76 bool wxApp
::Yield(bool onlyIfNeeded
)
78 if ( wxIsInsideYield
)
82 wxFAIL_MSG( wxT("wxYield called recursively" ) );
89 if ( !wxThread
::IsMain() )
91 // can't call gtk_main_iteration() from other threads like this
94 #endif // wxUSE_THREADS
96 wxIsInsideYield
= true;
98 // We need to remove idle callbacks or the loop will
100 SuspendIdleCallback();
103 // disable log flushing from here because a call to wxYield() shouldn't
104 // normally result in message boxes popping up &c
108 while (gtk_events_pending())
109 gtk_main_iteration();
111 // It's necessary to call ProcessIdle() to update the frames sizes which
112 // might have been changed (it also will update other things set from
113 // OnUpdateUI() which is a nice (and desired) side effect). But we
114 // call ProcessIdle() only once since this is not meant for longish
115 // background jobs (controlled by wxIdleEvent::RequestMore() and the
116 // return value of Processidle().
120 // let the logs be flashed again
124 wxIsInsideYield
= false;
129 //-----------------------------------------------------------------------------
131 //-----------------------------------------------------------------------------
133 // RR/KH: No wxMutexGui calls are needed here according to the GTK faq,
134 // http://www.gtk.org/faq/#AEN500 - this caused problems for wxPostEvent.
136 void wxApp
::WakeUpIdle()
138 wxapp_install_idle_handler();
141 //-----------------------------------------------------------------------------
143 //-----------------------------------------------------------------------------
145 // the callback functions must be extern "C" to comply with GTK+ declarations
149 // One-shot emission hook for "event" signal, to install idle handler.
150 // This will be called when the "event" signal is issued on any GtkWidget object.
152 event_emission_hook(GSignalInvocationHint
*, guint
, const GValue
*, gpointer
)
154 wxapp_install_idle_handler();
159 // add emission hook for "event" signal, to re-install idle handler when needed
160 static inline void wxAddEmissionHook()
162 GType widgetType
= GTK_TYPE_WIDGET
;
163 // if GtkWidget type is loaded
164 if (g_type_class_peek(widgetType
) != NULL
)
166 guint sig_id
= g_signal_lookup("event", widgetType
);
167 g_signal_add_emission_hook(sig_id
, 0, event_emission_hook
, NULL
, NULL
);
171 static gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
173 // this does not look possible, but just in case...
177 bool moreIdles
= false;
180 // don't generate the idle events while the assert modal dialog is shown,
181 // this matches the behavior of wxMSW
182 if (!wxTheApp
->IsInAssert())
183 #endif // __WXDEBUG__
187 // Allow another idle source to be added while this one is busy.
188 // Needed if an idle event handler runs a new event loop,
189 // for example by showing a dialog.
191 wxMutexLocker
lock(gs_idleTagsMutex
);
193 idleID_save
= wxTheApp
->m_idleTag
;
194 wxTheApp
->m_idleTag
= 0;
199 // When getting called from GDK's time-out handler
200 // we are no longer within GDK's grab on the GUI
201 // thread so we must lock it here ourselves.
204 // Send idle event to all who request them as long as
205 // no events have popped up in the event queue.
207 moreIdles
= wxTheApp
->ProcessIdle();
208 } while (moreIdles
&& gtk_events_pending() == 0);
210 // Release lock again
214 // If another idle source was added, remove it
216 wxMutexLocker
lock(gs_idleTagsMutex
);
218 if (wxTheApp
->m_idleTag
!= 0)
219 g_source_remove(wxTheApp
->m_idleTag
);
220 wxTheApp
->m_idleTag
= idleID_save
;
228 wxMutexLocker
lock(gs_idleTagsMutex
);
230 // Indicate that we are now in idle mode and event handlers
231 // will have to reinstall the idle handler again.
233 wxTheApp
->m_idleTag
= 0;
238 // Return FALSE if no more idle events are to be sent
245 static GPollFunc wxgs_poll_func
;
248 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
253 g_mainThreadLocked
= true;
255 gint res
= (*wxgs_poll_func
)(ufds
, nfds
, timeout
);
258 g_mainThreadLocked
= false;
266 #endif // wxUSE_THREADS
268 void wxapp_install_idle_handler()
270 if (wxTheApp
== NULL
)
274 wxMutexLocker
lock(gs_idleTagsMutex
);
277 // Don't install the handler if it's already installed. This test *MUST*
278 // be done when gs_idleTagsMutex is locked!
282 // GD: this assert is raised when using the thread sample (which works)
283 // so the test is probably not so easy. Can widget callbacks be
284 // triggered from child threads and, if so, for which widgets?
285 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
287 wxASSERT_MSG( wxTheApp
->m_idleTag
== 0, wxT("attempt to install idle handler twice") );
291 // This routine gets called by all event handlers
292 // indicating that the idle is over. It may also
293 // get called from other thread for sending events
294 // to the main thread (and processing these in
295 // idle time). Very low priority.
296 wxTheApp
->m_idleTag
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
299 //-----------------------------------------------------------------------------
300 // Access to the root window global
301 //-----------------------------------------------------------------------------
303 GtkWidget
* wxGetRootWindow()
305 if (gs_RootWindow
== NULL
)
307 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
308 gtk_widget_realize( gs_RootWindow
);
310 return gs_RootWindow
;
313 //-----------------------------------------------------------------------------
315 //-----------------------------------------------------------------------------
317 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
319 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
320 EVT_IDLE(wxAppBase
::OnIdle
)
326 m_isInAssert
= false;
327 #endif // __WXDEBUG__
331 wxapp_install_idle_handler();
333 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
334 m_glVisualInfo
= (void *) NULL
;
335 m_glFBCInfo
= (void *) NULL
;
341 g_source_remove( m_idleTag
);
346 XFree(m_glVisualInfo
);
349 bool wxApp
::OnInitGui()
351 if ( !wxAppBase
::OnInitGui() )
354 // if this is a wxGLApp (derived from wxApp), and we've already
355 // chosen a specific visual, then derive the GdkVisual from that
356 if (m_glVisualInfo
!= NULL
)
358 GdkVisual
* vis
= gtk_widget_get_default_visual();
360 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
361 gtk_widget_set_default_colormap( colormap
);
365 // On some machines, the default visual is just 256 colours, so
366 // we make sure we get the best. This can sometimes be wasteful.
369 if (m_forceTrueColour
)
371 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
374 wxLogError(wxT("Unable to initialize TrueColor visual."));
377 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
378 gtk_widget_set_default_colormap( colormap
);
382 if (gdk_visual_get_best() != gdk_visual_get_system())
384 GdkVisual
* visual
= gdk_visual_get_best();
385 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
386 gtk_widget_set_default_colormap( colormap
);
395 GdkVisual
*wxApp
::GetGdkVisual()
397 GdkVisual
*visual
= NULL
;
400 visual
= gdkx_visual_get( ((XVisualInfo
*) m_glVisualInfo
)->visualid
);
402 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
409 bool wxApp
::Initialize(int& argc
, wxChar
**argv
)
411 if ( !wxAppBase
::Initialize(argc
, argv
) )
415 if (!g_thread_supported())
418 wxgs_poll_func
= g_main_context_get_poll_func(NULL
);
419 g_main_context_set_poll_func(NULL
, wxapp_poll_func
);
420 #endif // wxUSE_THREADS
422 // We should have the wxUSE_WCHAR_T test on the _outside_
424 // gtk+ 2.0 supports Unicode through UTF-8 strings
425 wxConvCurrent
= &wxConvUTF8
;
426 #else // !wxUSE_WCHAR_T
428 wxConvCurrent
= (wxMBConv
*) NULL
;
429 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
431 // decide which conversion to use for the file names
433 // (1) this variable exists for the sole purpose of specifying the encoding
434 // of the filenames for GTK+ programs, so use it if it is set
435 wxString
encName(wxGetenv(_T("G_FILENAME_ENCODING")));
436 encName
= encName
.BeforeFirst(_T(','));
437 if (encName
.CmpNoCase(_T("@locale")) == 0)
443 // (2) if a non default locale is set, assume that the user wants his
444 // filenames in this locale too
445 encName
= wxLocale
::GetSystemEncodingName().Upper();
446 // (3) finally use UTF-8 by default
447 if (encName
.empty() || encName
== _T("US-ASCII"))
448 encName
= _T("UTF-8");
449 wxSetEnv(_T("G_FILENAME_ENCODING"), encName
);
453 encName
= _T("UTF-8");
455 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
456 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
457 // from gtk_init_check() as it does by default
458 gtk_disable_setlocale();
461 static wxConvBrokenFileNames
fileconv(encName
);
462 wxConvFileName
= &fileconv
;
468 // gtk_init() wants UTF-8, not wchar_t, so convert
470 char **argvGTK
= new char *[argc
+ 1];
471 for ( i
= 0; i
< argc
; i
++ )
473 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv
[i
]));
476 argvGTK
[argc
] = NULL
;
481 init_result
= true; // is there a _check() version of this?
482 gpe_application_init( &argcGTK
, &argvGTK
);
484 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
487 if ( argcGTK
!= argc
)
489 // we have to drop the parameters which were consumed by GTK+
490 for ( i
= 0; i
< argcGTK
; i
++ )
492 while ( strcmp(wxConvUTF8
.cWX2MB(argv
[i
]), argvGTK
[i
]) != 0 )
494 memmove(argv
+ i
, argv
+ i
+ 1, (argc
- i
)*sizeof(*argv
));
500 //else: gtk_init() didn't modify our parameters
503 for ( i
= 0; i
< argcGTK
; i
++ )
509 #else // !wxUSE_UNICODE
510 // gtk_init() shouldn't actually change argv itself (just its contents) so
511 // it's ok to pass pointer to it
512 init_result
= gtk_init_check( &argc
, &argv
);
513 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
516 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
520 // we can not enter threads before gtk_init is done
523 wxSetDetectableAutoRepeat( true );
526 wxFont
::SetDefaultEncoding(wxLocale
::GetSystemEncoding());
532 void wxApp
::CleanUp()
536 wxAppBase
::CleanUp();
541 void wxApp
::OnAssertFailure(const wxChar
*file
,
548 // block wx idle events while assert dialog is showing
551 wxAppBase
::OnAssertFailure(file
, line
, func
, cond
, msg
);
553 m_isInAssert
= false;
556 #endif // __WXDEBUG__
558 void wxApp
::SuspendIdleCallback()
561 wxMutexLocker
lock(gs_idleTagsMutex
);
565 g_source_remove(m_idleTag
);