]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/app.cpp
56b33401b890e2fe38f6df43c1780030d063d45a
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 // not static because used by textctrl.cpp
64 bool wxIsInsideYield
= false;
66 bool wxApp::Yield(bool onlyIfNeeded
)
68 if ( wxIsInsideYield
)
72 wxFAIL_MSG( wxT("wxYield called recursively" ) );
79 if ( !wxThread::IsMain() )
81 // can't call gtk_main_iteration() from other threads like this
84 #endif // wxUSE_THREADS
86 wxIsInsideYield
= true;
89 // disable log flushing from here because a call to wxYield() shouldn't
90 // normally result in message boxes popping up &c
94 while (EventsPending())
97 // It's necessary to call ProcessIdle() to update the frames sizes which
98 // might have been changed (it also will update other things set from
99 // OnUpdateUI() which is a nice (and desired) side effect). But we
100 // call ProcessIdle() only once since this is not meant for longish
101 // background jobs (controlled by wxIdleEvent::RequestMore() and the
102 // return value of Processidle().
106 // let the logs be flashed again
110 wxIsInsideYield
= false;
115 //-----------------------------------------------------------------------------
117 //-----------------------------------------------------------------------------
119 // One-shot signal emission hook, to install idle handler.
122 wx_emission_hook(GSignalInvocationHint
*, guint
, const GValue
*, gpointer data
)
124 wxApp
* app
= wxTheApp
;
127 gulong
* hook_id
= (gulong
*)data
;
128 // record that hook is not installed
135 // Add signal emission hooks, to re-install idle handler when needed.
136 static void wx_add_idle_hooks()
140 static gulong hook_id
= 0;
143 static guint sig_id
= 0;
145 sig_id
= g_signal_lookup("event", GTK_TYPE_WIDGET
);
146 hook_id
= g_signal_add_emission_hook(
147 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
150 // "size_allocate" hook
151 // Needed to match the behavior of the old idle system,
152 // but probably not necessary.
154 static gulong hook_id
= 0;
157 static guint sig_id
= 0;
159 sig_id
= g_signal_lookup("size_allocate", GTK_TYPE_WIDGET
);
160 hook_id
= g_signal_add_emission_hook(
161 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
167 static gboolean
wxapp_idle_callback(gpointer
)
169 return wxTheApp
->DoIdle();
177 // Allow another idle source to be added while this one is busy.
178 // Needed if an idle event handler runs a new event loop,
179 // for example by showing a dialog.
181 wxMutexLocker
lock(*m_idleMutex
);
183 id_save
= m_idleSourceId
;
187 // don't generate the idle events while the assert modal dialog is shown,
188 // this matches the behavior of wxMSW
197 needMore
= ProcessIdle();
198 } while (needMore
&& gtk_events_pending() == 0);
202 wxMutexLocker
lock(*m_idleMutex
);
204 // if a new idle source was added during ProcessIdle
205 if (m_idleSourceId
!= 0)
208 g_source_remove(m_idleSourceId
);
211 // if more idle processing requested
214 // keep this source installed
215 m_idleSourceId
= id_save
;
218 // add hooks and remove this source
225 static GPollFunc wxgs_poll_func
;
228 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
233 g_mainThreadLocked
= true;
235 gint res
= (*wxgs_poll_func
)(ufds
, nfds
, timeout
);
238 g_mainThreadLocked
= false;
246 #endif // wxUSE_THREADS
248 //-----------------------------------------------------------------------------
249 // Access to the root window global
250 //-----------------------------------------------------------------------------
252 GtkWidget
* wxGetRootWindow()
254 if (gs_RootWindow
== NULL
)
256 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
257 gtk_widget_realize( gs_RootWindow
);
259 return gs_RootWindow
;
262 //-----------------------------------------------------------------------------
264 //-----------------------------------------------------------------------------
266 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
268 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
269 EVT_IDLE(wxAppBase::OnIdle
)
275 m_isInAssert
= false;
276 #endif // __WXDEBUG__
287 bool wxApp::OnInitGui()
289 if ( !wxAppBase::OnInitGui() )
292 // if this is a wxGLApp (derived from wxApp), and we've already
293 // chosen a specific visual, then derive the GdkVisual from that
294 if ( GetXVisualInfo() )
296 GdkVisual
* vis
= gtk_widget_get_default_visual();
298 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
299 gtk_widget_set_default_colormap( colormap
);
303 // On some machines, the default visual is just 256 colours, so
304 // we make sure we get the best. This can sometimes be wasteful.
307 if (m_forceTrueColour
)
309 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
312 wxLogError(wxT("Unable to initialize TrueColor visual."));
315 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
316 gtk_widget_set_default_colormap( colormap
);
320 if (gdk_visual_get_best() != gdk_visual_get_system())
322 GdkVisual
* visual
= gdk_visual_get_best();
323 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
324 gtk_widget_set_default_colormap( colormap
);
333 GdkVisual
*wxApp::GetGdkVisual()
335 GdkVisual
*visual
= NULL
;
337 XVisualInfo
*xvi
= (XVisualInfo
*)GetXVisualInfo();
339 visual
= gdkx_visual_get( xvi
->visualid
);
341 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
348 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
350 if ( !wxAppBase::Initialize(argc
, argv
) )
354 if (!g_thread_supported())
357 wxgs_poll_func
= g_main_context_get_poll_func(NULL
);
358 g_main_context_set_poll_func(NULL
, wxapp_poll_func
);
359 #endif // wxUSE_THREADS
361 // We should have the wxUSE_WCHAR_T test on the _outside_
363 // gtk+ 2.0 supports Unicode through UTF-8 strings
364 wxConvCurrent
= &wxConvUTF8
;
365 #else // !wxUSE_WCHAR_T
367 wxConvCurrent
= (wxMBConv
*) NULL
;
368 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
370 // decide which conversion to use for the file names
372 // (1) this variable exists for the sole purpose of specifying the encoding
373 // of the filenames for GTK+ programs, so use it if it is set
374 wxString
encName(wxGetenv(_T("G_FILENAME_ENCODING")));
375 encName
= encName
.BeforeFirst(_T(','));
376 if (encName
.CmpNoCase(_T("@locale")) == 0)
382 // (2) if a non default locale is set, assume that the user wants his
383 // filenames in this locale too
384 encName
= wxLocale::GetSystemEncodingName().Upper();
385 // (3) finally use UTF-8 by default
386 if (encName
.empty() || encName
== _T("US-ASCII"))
387 encName
= _T("UTF-8");
388 wxSetEnv(_T("G_FILENAME_ENCODING"), encName
);
392 encName
= _T("UTF-8");
394 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
395 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
396 // from gtk_init_check() as it does by default
397 gtk_disable_setlocale();
400 static wxConvBrokenFileNames
fileconv(encName
);
401 wxConvFileName
= &fileconv
;
407 // gtk_init() wants UTF-8, not wchar_t, so convert
409 char **argvGTK
= new char *[argc
+ 1];
410 for ( i
= 0; i
< argc
; i
++ )
412 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv
[i
]));
415 argvGTK
[argc
] = NULL
;
420 init_result
= true; // is there a _check() version of this?
421 gpe_application_init( &argcGTK
, &argvGTK
);
423 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
426 if ( argcGTK
!= argc
)
428 // we have to drop the parameters which were consumed by GTK+
429 for ( i
= 0; i
< argcGTK
; i
++ )
431 while ( strcmp(wxConvUTF8
.cWX2MB(argv
[i
]), argvGTK
[i
]) != 0 )
433 memmove(argv
+ i
, argv
+ i
+ 1, (argc
- i
)*sizeof(*argv
));
439 //else: gtk_init() didn't modify our parameters
442 for ( i
= 0; i
< argcGTK
; i
++ )
448 #else // !wxUSE_UNICODE
449 // gtk_init() shouldn't actually change argv itself (just its contents) so
450 // it's ok to pass pointer to it
451 init_result
= gtk_init_check( &argc
, &argv
);
452 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
455 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
459 // we can not enter threads before gtk_init is done
462 wxSetDetectableAutoRepeat( true );
465 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
469 m_idleMutex
= new wxMutex
;
471 // make sure GtkWidget type is loaded, idle hooks need it
472 g_type_class_ref(GTK_TYPE_WIDGET
);
478 void wxApp::CleanUp()
480 if (m_idleSourceId
!= 0)
481 g_source_remove(m_idleSourceId
);
486 // release reference acquired by Initialize()
487 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET
));
491 wxAppBase::CleanUp();
494 void wxApp::WakeUpIdle()
497 wxMutexLocker
lock(*m_idleMutex
);
499 if (m_idleSourceId
== 0)
500 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
503 // Checking for pending events requires first removing our idle source,
504 // otherwise it will cause the check to always return true.
505 bool wxApp::EventsPending()
508 wxMutexLocker
lock(*m_idleMutex
);
510 if (m_idleSourceId
!= 0)
512 g_source_remove(m_idleSourceId
);
516 return gtk_events_pending() != 0;
521 void wxApp::OnAssertFailure(const wxChar
*file
,
528 // block wx idle events while assert dialog is showing
531 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
533 m_isInAssert
= false;
536 #endif // __WXDEBUG__