]>
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
37 #include <hildon/hildon.h>
38 #endif // wxUSE_LIBHILDON2
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 gulong
* hook_id
= (gulong
*)data
;
64 // record that hook is not installed
71 // Add signal emission hooks, to re-install idle handler when needed.
72 static void wx_add_idle_hooks()
76 static gulong hook_id
= 0;
79 static guint sig_id
= 0;
81 sig_id
= g_signal_lookup("event", GTK_TYPE_WIDGET
);
82 hook_id
= g_signal_add_emission_hook(
83 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
86 // "size_allocate" hook
87 // Needed to match the behavior of the old idle system,
88 // but probably not necessary.
90 static gulong hook_id
= 0;
93 static guint sig_id
= 0;
95 sig_id
= g_signal_lookup("size_allocate", GTK_TYPE_WIDGET
);
96 hook_id
= g_signal_add_emission_hook(
97 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
103 static gboolean
wxapp_idle_callback(gpointer
)
105 return wxTheApp
->DoIdle();
113 // Allow another idle source to be added while this one is busy.
114 // Needed if an idle event handler runs a new event loop,
115 // for example by showing a dialog.
117 wxMutexLocker
lock(*m_idleMutex
);
119 id_save
= m_idleSourceId
;
124 // don't generate the idle events while the assert modal dialog is shown,
125 // this matches the behavior of wxMSW
134 ProcessPendingEvents();
136 needMore
= ProcessIdle();
137 } while (needMore
&& gtk_events_pending() == 0);
141 wxMutexLocker
lock(*m_idleMutex
);
143 // if a new idle source was added during ProcessIdle
144 if (m_idleSourceId
!= 0)
147 g_source_remove(m_idleSourceId
);
151 // Pending events can be added asynchronously,
152 // need to keep idle source if any have appeared
153 needMore
= needMore
|| HasPendingEvents();
155 // if more idle processing requested
158 // keep this source installed
159 m_idleSourceId
= id_save
;
162 // add hooks and remove this source
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;
203 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
);
227 bool wxApp::OnInitGui()
229 if ( !wxAppBase::OnInitGui() )
232 // if this is a wxGLApp (derived from wxApp), and we've already
233 // chosen a specific visual, then derive the GdkVisual from that
234 if ( GetXVisualInfo() )
236 GdkVisual
* vis
= gtk_widget_get_default_visual();
238 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
239 gtk_widget_set_default_colormap( colormap
);
243 // On some machines, the default visual is just 256 colours, so
244 // we make sure we get the best. This can sometimes be wasteful.
247 if (m_forceTrueColour
)
249 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
252 wxLogError(wxT("Unable to initialize TrueColor visual."));
255 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
256 gtk_widget_set_default_colormap( colormap
);
260 if (gdk_visual_get_best() != gdk_visual_get_system())
262 GdkVisual
* visual
= gdk_visual_get_best();
263 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
264 gtk_widget_set_default_colormap( colormap
);
270 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
271 if ( !GetHildonProgram() )
273 wxLogError(_("Unable to initialize Hildon program"));
276 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2
281 GdkVisual
*wxApp::GetGdkVisual()
283 GdkVisual
*visual
= NULL
;
285 XVisualInfo
*xvi
= (XVisualInfo
*)GetXVisualInfo();
287 visual
= gdkx_visual_get( xvi
->visualid
);
289 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
296 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
297 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
299 if ( !wxAppBase::Initialize(argc_
, argv_
) )
303 if (!g_thread_supported())
308 #endif // wxUSE_THREADS
310 // gtk+ 2.0 supports Unicode through UTF-8 strings
311 wxConvCurrent
= &wxConvUTF8
;
313 // decide which conversion to use for the file names
315 // (1) this variable exists for the sole purpose of specifying the encoding
316 // of the filenames for GTK+ programs, so use it if it is set
317 wxString
encName(wxGetenv(wxT("G_FILENAME_ENCODING")));
318 encName
= encName
.BeforeFirst(wxT(','));
319 if (encName
.CmpNoCase(wxT("@locale")) == 0)
325 // (2) if a non default locale is set, assume that the user wants his
326 // filenames in this locale too
327 encName
= wxLocale::GetSystemEncodingName().Upper();
328 // (3) finally use UTF-8 by default
329 if (encName
.empty() || encName
== wxT("US-ASCII"))
330 encName
= wxT("UTF-8");
331 wxSetEnv(wxT("G_FILENAME_ENCODING"), encName
);
335 encName
= wxT("UTF-8");
337 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
338 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
339 // from gtk_init_check() as it does by default
340 gtk_disable_setlocale();
343 static wxConvBrokenFileNames
fileconv(encName
);
344 wxConvFileName
= &fileconv
;
351 // gtk_init() wants UTF-8, not wchar_t, so convert
352 char **argvGTK
= new char *[argc_
+ 1];
353 for ( i
= 0; i
< argc_
; i
++ )
355 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv_
[i
]));
358 argvGTK
[argc_
] = NULL
;
363 init_result
= true; // is there a _check() version of this?
364 gpe_application_init( &argcGTK
, &argvGTK
);
366 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
368 wxUpdateLocaleIsUtf8();
370 if ( argcGTK
!= argc_
)
372 // we have to drop the parameters which were consumed by GTK+
373 for ( i
= 0; i
< argcGTK
; i
++ )
375 while ( strcmp(wxConvUTF8
.cWX2MB(argv_
[i
]), argvGTK
[i
]) != 0 )
377 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc_
- i
)*sizeof(*argv_
));
384 //else: gtk_init() didn't modify our parameters
387 for ( i
= 0; i
< argcGTK
; i
++ )
393 #else // !wxUSE_UNICODE
394 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
395 // it's ok to pass pointer to it
396 init_result
= gtk_init_check( &argc_
, &argv_
);
397 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
399 // update internal arg[cv] as GTK+ may have removed processed options:
405 // if there are still GTK+ standard options unparsed in the command
406 // line, it means that they were not syntactically correct and GTK+
407 // already printed a warning on the command line and we should now
409 wxArrayString opt
, desc
;
410 m_traits
->GetStandardCmdLineOptions(opt
, desc
);
412 for ( i
= 0; i
< argc_
; i
++ )
414 // leave just the names of the options with values
415 const wxString str
= wxString(argv_
[i
]).BeforeFirst('=');
417 for ( size_t j
= 0; j
< opt
.size(); j
++ )
419 // remove the leading spaces from the option string as it does
421 if ( opt
[j
].Trim(false).BeforeFirst('=') == str
)
423 // a GTK+ option can be left on the command line only if
424 // there was an error in (or before, in another standard
425 // options) it, so abort, just as we do if incorrect
426 // program option is given
427 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
437 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
441 // we can not enter threads before gtk_init is done
445 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
449 m_idleMutex
= new wxMutex
;
451 // make sure GtkWidget type is loaded, idle hooks need it
452 g_type_class_ref(GTK_TYPE_WIDGET
);
458 void wxApp::CleanUp()
460 if (m_idleSourceId
!= 0)
461 g_source_remove(m_idleSourceId
);
463 // release reference acquired by Initialize()
464 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET
));
468 wxAppBase::CleanUp();
470 // delete this mutex as late as possible as it's used from WakeUpIdle(), in
471 // particular do it after calling the base class CleanUp() which can result
472 // in it being called
479 void wxApp::WakeUpIdle()
482 wxMutexLocker
lock(*m_idleMutex
);
484 if (m_idleSourceId
== 0)
485 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
488 // Checking for pending events requires first removing our idle source,
489 // otherwise it will cause the check to always return true.
490 bool wxApp::EventsPending()
493 wxMutexLocker
lock(*m_idleMutex
);
495 if (m_idleSourceId
!= 0)
497 g_source_remove(m_idleSourceId
);
501 return gtk_events_pending() != 0;
504 void wxApp::OnAssertFailure(const wxChar
*file
,
510 // there is no need to do anything if asserts are disabled in this build
513 // block wx idle events while assert dialog is showing
516 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
518 m_isInAssert
= false;
519 #else // !wxDEBUG_LEVEL
525 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
529 void wxGUIAppTraits::MutexGuiEnter()
534 void wxGUIAppTraits::MutexGuiLeave()
538 #endif // wxUSE_THREADS
540 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
541 // Maemo-specific method: get the main program object
542 HildonProgram
*wxApp::GetHildonProgram()
544 return hildon_program_get_instance();
547 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2