ed899e5ceedde80877a3a9a574fe836592a20cc5
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"
30 #include "wx/filename.h"
31 #include "wx/thread.h"
37 #ifdef __WXUNIVERSAL__
38 #include "wx/univ/theme.h"
39 #include "wx/univ/renderer.h"
42 #include "wx/gtk/win_gtk.h"
43 #include "wx/gtk/private.h"
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
52 #include "wx/html/forcelnk.h"
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
60 bool g_mainThreadLocked
= false;
62 static GtkWidget
*gs_RootWindow
= (GtkWidget
*) NULL
;
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
68 void wxapp_install_idle_handler();
71 static wxMutex gs_idleTagsMutex
;
74 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
78 // not static because used by textctrl.cpp
81 bool wxIsInsideYield
= false;
83 bool wxApp::Yield(bool onlyIfNeeded
)
85 if ( wxIsInsideYield
)
89 wxFAIL_MSG( wxT("wxYield called recursively" ) );
96 if ( !wxThread::IsMain() )
98 // can't call gtk_main_iteration() from other threads like this
101 #endif // wxUSE_THREADS
103 wxIsInsideYield
= true;
105 // We need to remove idle callbacks or the loop will
107 SuspendIdleCallback();
110 // disable log flushing from here because a call to wxYield() shouldn't
111 // normally result in message boxes popping up &c
115 while (gtk_events_pending())
116 gtk_main_iteration();
118 // It's necessary to call ProcessIdle() to update the frames sizes which
119 // might have been changed (it also will update other things set from
120 // OnUpdateUI() which is a nice (and desired) side effect). But we
121 // call ProcessIdle() only once since this is not meant for longish
122 // background jobs (controlled by wxIdleEvent::RequestMore() and the
123 // return value of Processidle().
127 // let the logs be flashed again
131 wxIsInsideYield
= false;
136 //-----------------------------------------------------------------------------
138 //-----------------------------------------------------------------------------
140 // RR/KH: No wxMutexGui calls are needed here according to the GTK faq,
141 // http://www.gtk.org/faq/#AEN500 - this caused problems for wxPostEvent.
143 void wxApp::WakeUpIdle()
145 wxapp_install_idle_handler();
148 //-----------------------------------------------------------------------------
150 //-----------------------------------------------------------------------------
152 // the callback functions must be extern "C" to comply with GTK+ declarations
156 // One-shot emission hook for "event" signal, to install idle handler.
157 // This will be called when the "event" signal is issued on any GtkWidget object.
159 event_emission_hook(GSignalInvocationHint
*, guint
, const GValue
*, gpointer
)
161 wxapp_install_idle_handler();
166 // add emission hook for "event" signal, to re-install idle handler when needed
167 static inline void wxAddEmissionHook()
169 GType widgetType
= GTK_TYPE_WIDGET
;
170 // if GtkWidget type is loaded
171 if (g_type_class_peek(widgetType
) != NULL
)
173 guint sig_id
= g_signal_lookup("event", widgetType
);
174 g_signal_add_emission_hook(sig_id
, 0, event_emission_hook
, NULL
, NULL
);
178 static gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
180 // this does not look possible, but just in case...
184 bool moreIdles
= false;
187 // don't generate the idle events while the assert modal dialog is shown,
188 // this matches the behavior of wxMSW
189 if (!wxTheApp
->IsInAssert())
190 #endif // __WXDEBUG__
194 // Allow another idle source to be added while this one is busy.
195 // Needed if an idle event handler runs a new event loop,
196 // for example by showing a dialog.
198 wxMutexLocker
lock(gs_idleTagsMutex
);
200 idleID_save
= wxTheApp
->m_idleTag
;
201 wxTheApp
->m_idleTag
= 0;
206 // When getting called from GDK's time-out handler
207 // we are no longer within GDK's grab on the GUI
208 // thread so we must lock it here ourselves.
211 // Send idle event to all who request them as long as
212 // no events have popped up in the event queue.
214 moreIdles
= wxTheApp
->ProcessIdle();
215 } while (moreIdles
&& gtk_events_pending() == 0);
217 // Release lock again
221 // If another idle source was added, remove it
223 wxMutexLocker
lock(gs_idleTagsMutex
);
225 if (wxTheApp
->m_idleTag
!= 0)
226 g_source_remove(wxTheApp
->m_idleTag
);
227 wxTheApp
->m_idleTag
= idleID_save
;
235 wxMutexLocker
lock(gs_idleTagsMutex
);
237 // Indicate that we are now in idle mode and event handlers
238 // will have to reinstall the idle handler again.
240 wxTheApp
->m_idleTag
= 0;
245 // Return FALSE if no more idle events are to be sent
252 static GPollFunc wxgs_poll_func
;
255 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
260 g_mainThreadLocked
= true;
262 gint res
= (*wxgs_poll_func
)(ufds
, nfds
, timeout
);
265 g_mainThreadLocked
= false;
273 #endif // wxUSE_THREADS
275 void wxapp_install_idle_handler()
277 if (wxTheApp
== NULL
)
281 wxMutexLocker
lock(gs_idleTagsMutex
);
284 // Don't install the handler if it's already installed. This test *MUST*
285 // be done when gs_idleTagsMutex is locked!
289 // GD: this assert is raised when using the thread sample (which works)
290 // so the test is probably not so easy. Can widget callbacks be
291 // triggered from child threads and, if so, for which widgets?
292 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
294 wxASSERT_MSG( wxTheApp
->m_idleTag
== 0, wxT("attempt to install idle handler twice") );
298 // This routine gets called by all event handlers
299 // indicating that the idle is over. It may also
300 // get called from other thread for sending events
301 // to the main thread (and processing these in
302 // idle time). Very low priority.
303 wxTheApp
->m_idleTag
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
306 //-----------------------------------------------------------------------------
307 // Access to the root window global
308 //-----------------------------------------------------------------------------
310 GtkWidget
* wxGetRootWindow()
312 if (gs_RootWindow
== NULL
)
314 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
315 gtk_widget_realize( gs_RootWindow
);
317 return gs_RootWindow
;
320 //-----------------------------------------------------------------------------
322 //-----------------------------------------------------------------------------
324 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
326 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
327 EVT_IDLE(wxAppBase::OnIdle
)
333 m_isInAssert
= false;
334 #endif // __WXDEBUG__
338 wxapp_install_idle_handler();
340 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
341 m_glVisualInfo
= (void *) NULL
;
342 m_glFBCInfo
= (void *) NULL
;
348 g_source_remove( m_idleTag
);
351 bool wxApp::OnInitGui()
353 if ( !wxAppBase::OnInitGui() )
356 // if this is a wxGLApp (derived from wxApp), and we've already
357 // chosen a specific visual, then derive the GdkVisual from that
358 if (m_glVisualInfo
!= NULL
)
360 GdkVisual
* vis
= gtk_widget_get_default_visual();
362 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
363 gtk_widget_set_default_colormap( colormap
);
367 // On some machines, the default visual is just 256 colours, so
368 // we make sure we get the best. This can sometimes be wasteful.
371 if (m_forceTrueColour
)
373 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
376 wxLogError(wxT("Unable to initialize TrueColor visual."));
379 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
380 gtk_widget_set_default_colormap( colormap
);
384 if (gdk_visual_get_best() != gdk_visual_get_system())
386 GdkVisual
* visual
= gdk_visual_get_best();
387 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
388 gtk_widget_set_default_colormap( colormap
);
397 GdkVisual
*wxApp::GetGdkVisual()
399 GdkVisual
*visual
= NULL
;
402 visual
= gdkx_visual_get( ((XVisualInfo
*) m_glVisualInfo
)->visualid
);
404 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
411 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
416 if (!g_thread_supported())
419 wxgs_poll_func
= g_main_context_get_poll_func(NULL
);
420 g_main_context_set_poll_func(NULL
, wxapp_poll_func
);
421 #endif // wxUSE_THREADS
425 // We should have the wxUSE_WCHAR_T test on the _outside_
427 // gtk+ 2.0 supports Unicode through UTF-8 strings
428 wxConvCurrent
= &wxConvUTF8
;
429 #else // !wxUSE_WCHAR_T
431 wxConvCurrent
= (wxMBConv
*) NULL
;
432 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
434 // decide which conversion to use for the file names
436 // (1) this variable exists for the sole purpose of specifying the encoding
437 // of the filenames for GTK+ programs, so use it if it is set
438 wxString
encName(wxGetenv(_T("G_FILENAME_ENCODING")));
439 encName
= encName
.BeforeFirst(_T(','));
440 if (encName
.CmpNoCase(_T("@locale")) == 0)
446 // (2) if a non default locale is set, assume that the user wants his
447 // filenames in this locale too
448 encName
= wxLocale::GetSystemEncodingName().Upper();
449 // (3) finally use UTF-8 by default
450 if (encName
.empty() || encName
== _T("US-ASCII"))
451 encName
= _T("UTF-8");
452 wxSetEnv(_T("G_FILENAME_ENCODING"), encName
);
456 encName
= _T("UTF-8");
458 static wxConvBrokenFileNames
fileconv(encName
);
459 wxConvFileName
= &fileconv
;
462 // gtk_init() wants UTF-8, not wchar_t, so convert
464 char **argvGTK
= new char *[argc
+ 1];
465 for ( i
= 0; i
< argc
; i
++ )
467 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv
[i
]));
470 argvGTK
[argc
] = NULL
;
475 init_result
= true; // is there a _check() version of this?
476 gpe_application_init( &argcGTK
, &argvGTK
);
478 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
481 if ( argcGTK
!= argc
)
483 // we have to drop the parameters which were consumed by GTK+
484 for ( i
= 0; i
< argcGTK
; i
++ )
486 while ( strcmp(wxConvUTF8
.cWX2MB(argv
[i
]), argvGTK
[i
]) != 0 )
488 memmove(argv
+ i
, argv
+ i
+ 1, argc
- i
);
494 //else: gtk_init() didn't modify our parameters
497 for ( i
= 0; i
< argcGTK
; i
++ )
503 #else // !wxUSE_UNICODE
504 // gtk_init() shouldn't actually change argv itself (just its contents) so
505 // it's ok to pass pointer to it
506 init_result
= gtk_init_check( &argc
, &argv
);
507 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
510 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
514 // we can not enter threads before gtk_init is done
517 if ( !wxAppBase::Initialize(argc
, argv
) )
524 wxSetDetectableAutoRepeat( true );
527 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
533 void wxApp::CleanUp()
537 wxAppBase::CleanUp();
542 void wxApp::OnAssertFailure(const wxChar
*file
,
549 // block wx idle events while assert dialog is showing
552 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
554 m_isInAssert
= false;
557 #endif // __WXDEBUG__
559 void wxApp::SuspendIdleCallback()
562 wxMutexLocker
lock(gs_idleTagsMutex
);
566 g_source_remove(m_idleTag
);