]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/app.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling, Julian Smart
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
18 #include "wx/memory.h"
22 #include "wx/thread.h"
28 #include "wx/apptrait.h"
29 #include "wx/fontmap.h"
32 #include <hildon-widgets/hildon-program.h>
33 #endif // wxUSE_LIBHILDON
36 #include <hildon/hildon.h>
37 #endif // wxUSE_LIBHILDON2
40 #include "wx/gtk/private.h"
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 #if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
48 wxFORCE_LINK_MODULE(gnome_vfs
)
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 // One-shot signal emission hook, to install idle handler.
58 wx_emission_hook(GSignalInvocationHint
*, guint
, const GValue
*, gpointer data
)
60 wxApp
* app
= wxTheApp
;
63 bool* hook_installed
= (bool*)data
;
64 // record that hook is not installed
65 *hook_installed
= false;
71 // Add signal emission hooks, to re-install idle handler when needed.
72 static void wx_add_idle_hooks()
76 static bool hook_installed
;
81 sig_id
= g_signal_lookup("event", GTK_TYPE_WIDGET
);
82 hook_installed
= true;
83 g_signal_add_emission_hook(
84 sig_id
, 0, wx_emission_hook
, &hook_installed
, NULL
);
87 // "size_allocate" hook
88 // Needed to match the behaviour of the old idle system,
89 // but probably not necessary.
91 static bool hook_installed
;
96 sig_id
= g_signal_lookup("size_allocate", GTK_TYPE_WIDGET
);
97 hook_installed
= true;
98 g_signal_add_emission_hook(
99 sig_id
, 0, wx_emission_hook
, &hook_installed
, NULL
);
105 static gboolean
wxapp_idle_callback(gpointer
)
107 return wxTheApp
->DoIdle();
115 // Allow another idle source to be added while this one is busy.
116 // Needed if an idle event handler runs a new event loop,
117 // for example by showing a dialog.
119 wxMutexLocker
lock(m_idleMutex
);
121 id_save
= m_idleSourceId
;
126 // don't generate the idle events while the assert modal dialog is shown,
127 // this matches the behaviour of wxMSW
136 ProcessPendingEvents();
138 needMore
= ProcessIdle();
139 } while (needMore
&& gtk_events_pending() == 0);
143 wxMutexLocker
lock(m_idleMutex
);
146 bool keepSource
= false;
147 // if a new idle source has not been added, either as a result of idle
148 // processing above or by another thread calling WakeUpIdle()
149 if (m_idleSourceId
== 0)
151 // if more idle processing was requested or pending events have appeared
152 if (needMore
|| HasPendingEvents())
154 // keep this source installed
155 m_idleSourceId
= id_save
;
158 else // add hooks and remove this source
161 // else remove this source, leave new one installed
162 // we must keep an idle source, otherwise a wakeup could be lost
167 //-----------------------------------------------------------------------------
168 // Access to the root window global
169 //-----------------------------------------------------------------------------
171 GtkWidget
* wxGetRootWindow()
173 static GtkWidget
*s_RootWindow
= NULL
;
175 if (s_RootWindow
== NULL
)
177 s_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
178 gtk_widget_realize( s_RootWindow
);
183 //-----------------------------------------------------------------------------
185 //-----------------------------------------------------------------------------
187 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
191 m_isInAssert
= false;
199 bool wxApp::SetNativeTheme(const wxString
& theme
)
206 path
= gtk_rc_get_theme_dir();
208 path
+= theme
.utf8_str();
209 path
+= "/gtk-2.0/gtkrc";
211 if ( wxFileExists(path
.utf8_str()) )
212 gtk_rc_add_default_file(path
.utf8_str());
213 else if ( wxFileExists(theme
.utf8_str()) )
214 gtk_rc_add_default_file(theme
.utf8_str());
217 wxLogWarning("Theme \"%s\" not available.", theme
);
222 gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE
);
228 bool wxApp::OnInitGui()
230 if ( !wxAppBase::OnInitGui() )
234 // if this is a wxGLApp (derived from wxApp), and we've already
235 // chosen a specific visual, then derive the GdkVisual from that
236 if ( GetXVisualInfo() )
238 GdkVisual
* vis
= gtk_widget_get_default_visual();
240 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
241 gtk_widget_set_default_colormap( colormap
);
245 // On some machines, the default visual is just 256 colours, so
246 // we make sure we get the best. This can sometimes be wasteful.
249 if (m_forceTrueColour
)
251 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
254 wxLogError(wxT("Unable to initialize TrueColor visual."));
257 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
258 gtk_widget_set_default_colormap( colormap
);
262 if (gdk_visual_get_best() != gdk_visual_get_system())
264 GdkVisual
* visual
= gdk_visual_get_best();
265 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
266 gtk_widget_set_default_colormap( colormap
);
273 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
274 if ( !GetHildonProgram() )
276 wxLogError(_("Unable to initialize Hildon program"));
279 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2
284 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
285 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
287 if ( !wxAppBase::Initialize(argc_
, argv_
) )
291 if (!g_thread_supported())
296 #endif // wxUSE_THREADS
298 // gtk+ 2.0 supports Unicode through UTF-8 strings
299 wxConvCurrent
= &wxConvUTF8
;
302 // decide which conversion to use for the file names
304 // (1) this variable exists for the sole purpose of specifying the encoding
305 // of the filenames for GTK+ programs, so use it if it is set
306 wxString
encName(wxGetenv(wxT("G_FILENAME_ENCODING")));
307 encName
= encName
.BeforeFirst(wxT(','));
308 if (encName
.CmpNoCase(wxT("@locale")) == 0)
314 // (2) if a non default locale is set, assume that the user wants his
315 // filenames in this locale too
316 encName
= wxLocale::GetSystemEncodingName().Upper();
318 // But don't consider ASCII in this case.
319 if ( !encName
.empty() )
322 wxFontEncoding enc
= wxFontMapperBase::GetEncodingFromName(encName
);
323 if ( enc
== wxFONTENCODING_DEFAULT
)
324 #else // !wxUSE_FONTMAP
325 if ( encName
== wxT("US-ASCII") )
326 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
328 // This means US-ASCII when returned from GetEncodingFromName().
334 // (3) finally use UTF-8 by default
335 if ( encName
.empty() )
336 encName
= wxT("UTF-8");
337 wxSetEnv(wxT("G_FILENAME_ENCODING"), encName
);
340 static wxConvBrokenFileNames
fileconv(encName
);
341 wxConvFileName
= &fileconv
;
349 // gtk_init() wants UTF-8, not wchar_t, so convert
350 char **argvGTK
= new char *[argc_
+ 1];
351 for ( i
= 0; i
< argc_
; i
++ )
353 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv_
[i
]));
356 argvGTK
[argc_
] = NULL
;
360 // Prevent gtk_init_check() from changing the locale automatically for
361 // consistency with the other ports that don't do it. If necessary,
362 // wxApp::SetCLocale() may be explicitly called.
363 gtk_disable_setlocale();
366 init_result
= true; // is there a _check() version of this?
367 gpe_application_init( &argcGTK
, &argvGTK
);
369 init_result
= gtk_init_check( &argcGTK
, &argvGTK
) != 0;
372 if ( argcGTK
!= argc_
)
374 // we have to drop the parameters which were consumed by GTK+
375 for ( i
= 0; i
< argcGTK
; i
++ )
377 while ( strcmp(wxConvUTF8
.cWX2MB(argv_
[i
]), argvGTK
[i
]) != 0 )
379 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc_
- i
)*sizeof(*argv_
));
386 //else: gtk_init() didn't modify our parameters
389 for ( i
= 0; i
< argcGTK
; i
++ )
395 #else // !wxUSE_UNICODE
396 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
397 // it's ok to pass pointer to it
398 init_result
= gtk_init_check( &argc_
, &argv_
);
399 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
401 // update internal arg[cv] as GTK+ may have removed processed options:
407 // if there are still GTK+ standard options unparsed in the command
408 // line, it means that they were not syntactically correct and GTK+
409 // already printed a warning on the command line and we should now
411 wxArrayString opt
, desc
;
412 m_traits
->GetStandardCmdLineOptions(opt
, desc
);
414 for ( i
= 0; i
< argc_
; i
++ )
416 // leave just the names of the options with values
417 const wxString str
= wxString(argv_
[i
]).BeforeFirst('=');
419 for ( size_t j
= 0; j
< opt
.size(); j
++ )
421 // remove the leading spaces from the option string as it does
423 if ( opt
[j
].Trim(false).BeforeFirst('=') == str
)
425 // a GTK+ option can be left on the command line only if
426 // there was an error in (or before, in another standard
427 // options) it, so abort, just as we do if incorrect
428 // program option is given
429 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
439 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
443 // we cannot enter threads before gtk_init is done
447 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
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();
470 void wxApp::WakeUpIdle()
473 wxMutexLocker
lock(m_idleMutex
);
475 if (m_idleSourceId
== 0)
476 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
479 // Checking for pending events requires first removing our idle source,
480 // otherwise it will cause the check to always return true.
481 bool wxApp::EventsPending()
484 wxMutexLocker
lock(m_idleMutex
);
486 if (m_idleSourceId
!= 0)
488 g_source_remove(m_idleSourceId
);
492 return gtk_events_pending() != 0;
495 void wxApp::OnAssertFailure(const wxChar
*file
,
501 // there is no need to do anything if asserts are disabled in this build
504 // block wx idle events while assert dialog is showing
507 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
509 m_isInAssert
= false;
510 #else // !wxDEBUG_LEVEL
516 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
520 void wxGUIAppTraits::MutexGuiEnter()
525 void wxGUIAppTraits::MutexGuiLeave()
529 #endif // wxUSE_THREADS
532 bool wxApp::GTKIsUsingGlobalMenu()
534 static int s_isUsingGlobalMenu
= -1;
535 if ( s_isUsingGlobalMenu
== -1 )
537 // Currently we just check for this environment variable because this
538 // is how support for the global menu is implemented under Ubuntu.
540 // If we ever get false positives, we could also check for
541 // XDG_CURRENT_DESKTOP env var being set to "Unity".
543 s_isUsingGlobalMenu
= wxGetEnv("UBUNTU_MENUPROXY", &proxy
) &&
544 !proxy
.empty() && proxy
!= "0";
547 return s_isUsingGlobalMenu
== 1;
550 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
551 // Maemo-specific method: get the main program object
552 HildonProgram
*wxApp::GetHildonProgram()
554 return hildon_program_get_instance();
557 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2