]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/app.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
19 #include "wx/memory.h"
23 #include "wx/thread.h"
29 #include "wx/gtk/private.h"
30 #include "wx/apptrait.h"
33 #include <hildon-widgets/hildon-program.h>
34 #endif // wxUSE_LIBHILDON
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 #if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
44 wxFORCE_LINK_MODULE(gnome_vfs
)
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 // One-shot signal emission hook, to install idle handler.
54 wx_emission_hook(GSignalInvocationHint
*, guint
, const GValue
*, gpointer data
)
56 wxApp
* app
= wxTheApp
;
59 gulong
* hook_id
= (gulong
*)data
;
60 // record that hook is not installed
67 // Add signal emission hooks, to re-install idle handler when needed.
68 static void wx_add_idle_hooks()
72 static gulong hook_id
= 0;
75 static guint sig_id
= 0;
77 sig_id
= g_signal_lookup("event", GTK_TYPE_WIDGET
);
78 hook_id
= g_signal_add_emission_hook(
79 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
82 // "size_allocate" hook
83 // Needed to match the behavior of the old idle system,
84 // but probably not necessary.
86 static gulong hook_id
= 0;
89 static guint sig_id
= 0;
91 sig_id
= g_signal_lookup("size_allocate", GTK_TYPE_WIDGET
);
92 hook_id
= g_signal_add_emission_hook(
93 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
99 static gboolean
wxapp_idle_callback(gpointer
)
101 return wxTheApp
->DoIdle();
109 // Allow another idle source to be added while this one is busy.
110 // Needed if an idle event handler runs a new event loop,
111 // for example by showing a dialog.
113 wxMutexLocker
lock(*m_idleMutex
);
115 id_save
= m_idleSourceId
;
120 // don't generate the idle events while the assert modal dialog is shown,
121 // this matches the behavior of wxMSW
130 ProcessPendingEvents();
132 needMore
= ProcessIdle();
133 } while (needMore
&& gtk_events_pending() == 0);
137 wxMutexLocker
lock(*m_idleMutex
);
139 // if a new idle source was added during ProcessIdle
140 if (m_idleSourceId
!= 0)
143 g_source_remove(m_idleSourceId
);
147 // Pending events can be added asynchronously,
148 // need to keep idle source if any have appeared
149 needMore
= needMore
|| HasPendingEvents();
151 // if more idle processing requested
154 // keep this source installed
155 m_idleSourceId
= id_save
;
158 // add hooks and remove this source
163 //-----------------------------------------------------------------------------
164 // Access to the root window global
165 //-----------------------------------------------------------------------------
167 GtkWidget
* wxGetRootWindow()
169 static GtkWidget
*s_RootWindow
= NULL
;
171 if (s_RootWindow
== NULL
)
173 s_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
174 gtk_widget_realize( s_RootWindow
);
179 //-----------------------------------------------------------------------------
181 //-----------------------------------------------------------------------------
183 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
187 m_isInAssert
= false;
199 bool wxApp::SetNativeTheme(const wxString
& theme
)
202 path
= gtk_rc_get_theme_dir();
204 path
+= theme
.utf8_str();
205 path
+= "/gtk-2.0/gtkrc";
207 if ( wxFileExists(path
.utf8_str()) )
208 gtk_rc_add_default_file(path
.utf8_str());
209 else if ( wxFileExists(theme
.utf8_str()) )
210 gtk_rc_add_default_file(theme
.utf8_str());
213 wxLogWarning("Theme \"%s\" not available.", theme
);
218 gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE
);
223 bool wxApp::OnInitGui()
225 if ( !wxAppBase::OnInitGui() )
228 // if this is a wxGLApp (derived from wxApp), and we've already
229 // chosen a specific visual, then derive the GdkVisual from that
230 if ( GetXVisualInfo() )
232 GdkVisual
* vis
= gtk_widget_get_default_visual();
234 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
235 gtk_widget_set_default_colormap( colormap
);
239 // On some machines, the default visual is just 256 colours, so
240 // we make sure we get the best. This can sometimes be wasteful.
243 if (m_forceTrueColour
)
245 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
248 wxLogError(wxT("Unable to initialize TrueColor visual."));
251 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
252 gtk_widget_set_default_colormap( colormap
);
256 if (gdk_visual_get_best() != gdk_visual_get_system())
258 GdkVisual
* visual
= gdk_visual_get_best();
259 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
260 gtk_widget_set_default_colormap( colormap
);
267 m_hildonProgram
= hildon_program_get_instance();
268 if ( !m_hildonProgram
)
270 wxLogError(_("Unable to initialize Hildon program"));
273 #endif // wxUSE_LIBHILDON
278 GdkVisual
*wxApp::GetGdkVisual()
280 GdkVisual
*visual
= NULL
;
282 XVisualInfo
*xvi
= (XVisualInfo
*)GetXVisualInfo();
284 visual
= gdkx_visual_get( xvi
->visualid
);
286 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
293 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
294 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
296 if ( !wxAppBase::Initialize(argc_
, argv_
) )
300 if (!g_thread_supported())
305 #endif // wxUSE_THREADS
307 // gtk+ 2.0 supports Unicode through UTF-8 strings
308 wxConvCurrent
= &wxConvUTF8
;
310 // decide which conversion to use for the file names
312 // (1) this variable exists for the sole purpose of specifying the encoding
313 // of the filenames for GTK+ programs, so use it if it is set
314 wxString
encName(wxGetenv(wxT("G_FILENAME_ENCODING")));
315 encName
= encName
.BeforeFirst(wxT(','));
316 if (encName
.CmpNoCase(wxT("@locale")) == 0)
322 // (2) if a non default locale is set, assume that the user wants his
323 // filenames in this locale too
324 encName
= wxLocale::GetSystemEncodingName().Upper();
325 // (3) finally use UTF-8 by default
326 if (encName
.empty() || encName
== wxT("US-ASCII"))
327 encName
= wxT("UTF-8");
328 wxSetEnv(wxT("G_FILENAME_ENCODING"), encName
);
332 encName
= wxT("UTF-8");
334 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
335 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
336 // from gtk_init_check() as it does by default
337 gtk_disable_setlocale();
340 static wxConvBrokenFileNames
fileconv(encName
);
341 wxConvFileName
= &fileconv
;
348 // gtk_init() wants UTF-8, not wchar_t, so convert
349 char **argvGTK
= new char *[argc_
+ 1];
350 for ( i
= 0; i
< argc_
; i
++ )
352 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv_
[i
]));
355 argvGTK
[argc_
] = NULL
;
360 init_result
= true; // is there a _check() version of this?
361 gpe_application_init( &argcGTK
, &argvGTK
);
363 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
365 wxUpdateLocaleIsUtf8();
367 if ( argcGTK
!= argc_
)
369 // we have to drop the parameters which were consumed by GTK+
370 for ( i
= 0; i
< argcGTK
; i
++ )
372 while ( strcmp(wxConvUTF8
.cWX2MB(argv_
[i
]), argvGTK
[i
]) != 0 )
374 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc_
- i
)*sizeof(*argv_
));
381 //else: gtk_init() didn't modify our parameters
384 for ( i
= 0; i
< argcGTK
; i
++ )
390 #else // !wxUSE_UNICODE
391 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
392 // it's ok to pass pointer to it
393 init_result
= gtk_init_check( &argc_
, &argv_
);
394 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
396 // update internal arg[cv] as GTK+ may have removed processed options:
402 // if there are still GTK+ standard options unparsed in the command
403 // line, it means that they were not syntactically correct and GTK+
404 // already printed a warning on the command line and we should now
406 wxArrayString opt
, desc
;
407 m_traits
->GetStandardCmdLineOptions(opt
, desc
);
409 for ( i
= 0; i
< argc_
; i
++ )
411 // leave just the names of the options with values
412 const wxString str
= wxString(argv_
[i
]).BeforeFirst('=');
414 for ( size_t j
= 0; j
< opt
.size(); j
++ )
416 // remove the leading spaces from the option string as it does
418 if ( opt
[j
].Trim(false).BeforeFirst('=') == str
)
420 // a GTK+ option can be left on the command line only if
421 // there was an error in (or before, in another standard
422 // options) it, so abort, just as we do if incorrect
423 // program option is given
424 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
434 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
438 // we can not enter threads before gtk_init is done
441 wxSetDetectableAutoRepeat( true );
444 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
448 m_idleMutex
= new wxMutex
;
450 // make sure GtkWidget type is loaded, idle hooks need it
451 g_type_class_ref(GTK_TYPE_WIDGET
);
457 void wxApp::CleanUp()
459 if (m_idleSourceId
!= 0)
460 g_source_remove(m_idleSourceId
);
462 // release reference acquired by Initialize()
463 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET
));
467 wxAppBase::CleanUp();
469 // delete this mutex as late as possible as it's used from WakeUpIdle(), in
470 // particular do it after calling the base class CleanUp() which can result
471 // in it being called
478 void wxApp::WakeUpIdle()
481 wxMutexLocker
lock(*m_idleMutex
);
483 if (m_idleSourceId
== 0)
484 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
487 // Checking for pending events requires first removing our idle source,
488 // otherwise it will cause the check to always return true.
489 bool wxApp::EventsPending()
492 wxMutexLocker
lock(*m_idleMutex
);
494 if (m_idleSourceId
!= 0)
496 g_source_remove(m_idleSourceId
);
500 return gtk_events_pending() != 0;
503 void wxApp::OnAssertFailure(const wxChar
*file
,
509 // there is no need to do anything if asserts are disabled in this build
512 // block wx idle events while assert dialog is showing
515 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
517 m_isInAssert
= false;
518 #else // !wxDEBUG_LEVEL
524 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
528 void wxGUIAppTraits::MutexGuiEnter()
533 void wxGUIAppTraits::MutexGuiLeave()
537 #endif // wxUSE_THREADS