]>
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"
31 #include "wx/fontmap.h"
34 #include <hildon-widgets/hildon-program.h>
35 #endif // wxUSE_LIBHILDON
38 #include <hildon/hildon.h>
39 #endif // wxUSE_LIBHILDON2
41 #ifdef GDK_WINDOWING_X11
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 #if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
51 wxFORCE_LINK_MODULE(gnome_vfs
)
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 // One-shot signal emission hook, to install idle handler.
61 wx_emission_hook(GSignalInvocationHint
*, guint
, const GValue
*, gpointer data
)
63 wxApp
* app
= wxTheApp
;
66 bool* hook_installed
= (bool*)data
;
67 // record that hook is not installed
68 *hook_installed
= false;
74 // Add signal emission hooks, to re-install idle handler when needed.
75 static void wx_add_idle_hooks()
79 static bool hook_installed
;
84 sig_id
= g_signal_lookup("event", GTK_TYPE_WIDGET
);
85 hook_installed
= true;
86 g_signal_add_emission_hook(
87 sig_id
, 0, wx_emission_hook
, &hook_installed
, NULL
);
90 // "size_allocate" hook
91 // Needed to match the behaviour of the old idle system,
92 // but probably not necessary.
94 static bool hook_installed
;
99 sig_id
= g_signal_lookup("size_allocate", GTK_TYPE_WIDGET
);
100 hook_installed
= true;
101 g_signal_add_emission_hook(
102 sig_id
, 0, wx_emission_hook
, &hook_installed
, NULL
);
108 static gboolean
wxapp_idle_callback(gpointer
)
110 return wxTheApp
->DoIdle();
118 // Allow another idle source to be added while this one is busy.
119 // Needed if an idle event handler runs a new event loop,
120 // for example by showing a dialog.
122 wxMutexLocker
lock(m_idleMutex
);
124 id_save
= m_idleSourceId
;
129 // don't generate the idle events while the assert modal dialog is shown,
130 // this matches the behaviour of wxMSW
139 ProcessPendingEvents();
141 needMore
= ProcessIdle();
142 } while (needMore
&& gtk_events_pending() == 0);
146 wxMutexLocker
lock(m_idleMutex
);
148 // if a new idle source was added during ProcessIdle
149 if (m_idleSourceId
!= 0)
152 g_source_remove(m_idleSourceId
);
156 // Pending events can be added asynchronously,
157 // need to keep idle source if any have appeared
158 if (HasPendingEvents())
161 // if more idle processing requested
164 // keep this source installed
165 m_idleSourceId
= id_save
;
168 // add hooks and remove this source
173 //-----------------------------------------------------------------------------
174 // Access to the root window global
175 //-----------------------------------------------------------------------------
177 GtkWidget
* wxGetRootWindow()
179 static GtkWidget
*s_RootWindow
= NULL
;
181 if (s_RootWindow
== NULL
)
183 s_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
184 gtk_widget_realize( s_RootWindow
);
189 //-----------------------------------------------------------------------------
191 //-----------------------------------------------------------------------------
193 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
197 m_isInAssert
= false;
205 bool wxApp::SetNativeTheme(const wxString
& theme
)
208 path
= gtk_rc_get_theme_dir();
210 path
+= theme
.utf8_str();
211 path
+= "/gtk-2.0/gtkrc";
213 if ( wxFileExists(path
.utf8_str()) )
214 gtk_rc_add_default_file(path
.utf8_str());
215 else if ( wxFileExists(theme
.utf8_str()) )
216 gtk_rc_add_default_file(theme
.utf8_str());
219 wxLogWarning("Theme \"%s\" not available.", theme
);
224 gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE
);
229 bool wxApp::OnInitGui()
231 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
);
272 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
273 if ( !GetHildonProgram() )
275 wxLogError(_("Unable to initialize Hildon program"));
278 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2
283 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
284 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
286 if ( !wxAppBase::Initialize(argc_
, argv_
) )
290 if (!g_thread_supported())
295 #endif // wxUSE_THREADS
297 // gtk+ 2.0 supports Unicode through UTF-8 strings
298 wxConvCurrent
= &wxConvUTF8
;
301 // decide which conversion to use for the file names
303 // (1) this variable exists for the sole purpose of specifying the encoding
304 // of the filenames for GTK+ programs, so use it if it is set
305 wxString
encName(wxGetenv(wxT("G_FILENAME_ENCODING")));
306 encName
= encName
.BeforeFirst(wxT(','));
307 if (encName
.CmpNoCase(wxT("@locale")) == 0)
313 // (2) if a non default locale is set, assume that the user wants his
314 // filenames in this locale too
315 encName
= wxLocale::GetSystemEncodingName().Upper();
317 // But don't consider ASCII in this case.
318 if ( !encName
.empty() )
321 wxFontEncoding enc
= wxFontMapperBase::GetEncodingFromName(encName
);
322 if ( enc
== wxFONTENCODING_DEFAULT
)
323 #else // !wxUSE_FONTMAP
324 if ( encName
== wxT("US-ASCII") )
325 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
327 // This means US-ASCII when returned from GetEncodingFromName().
332 // (3) finally use UTF-8 by default
333 if ( encName
.empty() )
334 encName
= wxT("UTF-8");
335 wxSetEnv(wxT("G_FILENAME_ENCODING"), encName
);
339 encName
= wxT("UTF-8");
341 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
342 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
343 // from gtk_init_check() as it does by default
344 gtk_disable_setlocale();
347 static wxConvBrokenFileNames
fileconv(encName
);
348 wxConvFileName
= &fileconv
;
356 // gtk_init() wants UTF-8, not wchar_t, so convert
357 char **argvGTK
= new char *[argc_
+ 1];
358 for ( i
= 0; i
< argc_
; i
++ )
360 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv_
[i
]));
363 argvGTK
[argc_
] = NULL
;
368 init_result
= true; // is there a _check() version of this?
369 gpe_application_init( &argcGTK
, &argvGTK
);
371 init_result
= gtk_init_check( &argcGTK
, &argvGTK
) != 0;
373 wxUpdateLocaleIsUtf8();
375 if ( argcGTK
!= argc_
)
377 // we have to drop the parameters which were consumed by GTK+
378 for ( i
= 0; i
< argcGTK
; i
++ )
380 while ( strcmp(wxConvUTF8
.cWX2MB(argv_
[i
]), argvGTK
[i
]) != 0 )
382 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc_
- i
)*sizeof(*argv_
));
389 //else: gtk_init() didn't modify our parameters
392 for ( i
= 0; i
< argcGTK
; i
++ )
398 #else // !wxUSE_UNICODE
399 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
400 // it's ok to pass pointer to it
401 init_result
= gtk_init_check( &argc_
, &argv_
);
402 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
404 // update internal arg[cv] as GTK+ may have removed processed options:
410 // if there are still GTK+ standard options unparsed in the command
411 // line, it means that they were not syntactically correct and GTK+
412 // already printed a warning on the command line and we should now
414 wxArrayString opt
, desc
;
415 m_traits
->GetStandardCmdLineOptions(opt
, desc
);
417 for ( i
= 0; i
< argc_
; i
++ )
419 // leave just the names of the options with values
420 const wxString str
= wxString(argv_
[i
]).BeforeFirst('=');
422 for ( size_t j
= 0; j
< opt
.size(); j
++ )
424 // remove the leading spaces from the option string as it does
426 if ( opt
[j
].Trim(false).BeforeFirst('=') == str
)
428 // a GTK+ option can be left on the command line only if
429 // there was an error in (or before, in another standard
430 // options) it, so abort, just as we do if incorrect
431 // program option is given
432 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
442 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
446 // we cannot enter threads before gtk_init is done
450 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
453 // make sure GtkWidget type is loaded, idle hooks need it
454 g_type_class_ref(GTK_TYPE_WIDGET
);
460 void wxApp::CleanUp()
462 if (m_idleSourceId
!= 0)
463 g_source_remove(m_idleSourceId
);
465 // release reference acquired by Initialize()
466 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET
));
470 wxAppBase::CleanUp();
473 void wxApp::WakeUpIdle()
476 wxMutexLocker
lock(m_idleMutex
);
478 if (m_idleSourceId
== 0)
479 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
482 // Checking for pending events requires first removing our idle source,
483 // otherwise it will cause the check to always return true.
484 bool wxApp::EventsPending()
487 wxMutexLocker
lock(m_idleMutex
);
489 if (m_idleSourceId
!= 0)
491 g_source_remove(m_idleSourceId
);
495 return gtk_events_pending() != 0;
498 void wxApp::OnAssertFailure(const wxChar
*file
,
504 // there is no need to do anything if asserts are disabled in this build
507 // block wx idle events while assert dialog is showing
510 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
512 m_isInAssert
= false;
513 #else // !wxDEBUG_LEVEL
519 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
523 void wxGUIAppTraits::MutexGuiEnter()
528 void wxGUIAppTraits::MutexGuiLeave()
532 #endif // wxUSE_THREADS
534 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
535 // Maemo-specific method: get the main program object
536 HildonProgram
*wxApp::GetHildonProgram()
538 return hildon_program_get_instance();
541 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2