]>
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
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 #if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
49 wxFORCE_LINK_MODULE(gnome_vfs
)
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 // One-shot signal emission hook, to install idle handler.
59 wx_emission_hook(GSignalInvocationHint
*, guint
, const GValue
*, gpointer data
)
61 wxApp
* app
= wxTheApp
;
64 bool* hook_installed
= (bool*)data
;
65 // record that hook is not installed
66 *hook_installed
= false;
72 // Add signal emission hooks, to re-install idle handler when needed.
73 static void wx_add_idle_hooks()
77 static bool hook_installed
;
82 sig_id
= g_signal_lookup("event", GTK_TYPE_WIDGET
);
83 hook_installed
= true;
84 g_signal_add_emission_hook(
85 sig_id
, 0, wx_emission_hook
, &hook_installed
, NULL
);
88 // "size_allocate" hook
89 // Needed to match the behavior of the old idle system,
90 // but probably not necessary.
92 static bool hook_installed
;
97 sig_id
= g_signal_lookup("size_allocate", GTK_TYPE_WIDGET
);
98 hook_installed
= true;
99 g_signal_add_emission_hook(
100 sig_id
, 0, wx_emission_hook
, &hook_installed
, NULL
);
106 static gboolean
wxapp_idle_callback(gpointer
)
108 return wxTheApp
->DoIdle();
116 // Allow another idle source to be added while this one is busy.
117 // Needed if an idle event handler runs a new event loop,
118 // for example by showing a dialog.
120 wxMutexLocker
lock(m_idleMutex
);
122 id_save
= m_idleSourceId
;
127 // don't generate the idle events while the assert modal dialog is shown,
128 // this matches the behavior of wxMSW
137 ProcessPendingEvents();
139 needMore
= ProcessIdle();
140 } while (needMore
&& gtk_events_pending() == 0);
144 wxMutexLocker
lock(m_idleMutex
);
146 // if a new idle source was added during ProcessIdle
147 if (m_idleSourceId
!= 0)
150 g_source_remove(m_idleSourceId
);
154 // Pending events can be added asynchronously,
155 // need to keep idle source if any have appeared
156 if (HasPendingEvents())
159 // if more idle processing requested
162 // keep this source installed
163 m_idleSourceId
= id_save
;
166 // add hooks and remove this source
171 //-----------------------------------------------------------------------------
172 // Access to the root window global
173 //-----------------------------------------------------------------------------
175 GtkWidget
* wxGetRootWindow()
177 static GtkWidget
*s_RootWindow
= NULL
;
179 if (s_RootWindow
== NULL
)
181 s_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
182 gtk_widget_realize( s_RootWindow
);
187 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------
191 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
195 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 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
282 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
284 if ( !wxAppBase::Initialize(argc_
, argv_
) )
288 if (!g_thread_supported())
293 #endif // wxUSE_THREADS
295 // gtk+ 2.0 supports Unicode through UTF-8 strings
296 wxConvCurrent
= &wxConvUTF8
;
298 // decide which conversion to use for the file names
300 // (1) this variable exists for the sole purpose of specifying the encoding
301 // of the filenames for GTK+ programs, so use it if it is set
302 wxString
encName(wxGetenv(wxT("G_FILENAME_ENCODING")));
303 encName
= encName
.BeforeFirst(wxT(','));
304 if (encName
.CmpNoCase(wxT("@locale")) == 0)
310 // (2) if a non default locale is set, assume that the user wants his
311 // filenames in this locale too
312 encName
= wxLocale::GetSystemEncodingName().Upper();
314 // But don't consider ASCII in this case.
315 if ( !encName
.empty() )
318 wxFontEncoding enc
= wxFontMapperBase::GetEncodingFromName(encName
);
319 if ( enc
== wxFONTENCODING_DEFAULT
)
320 #else // !wxUSE_FONTMAP
321 if ( encName
== wxT("US-ASCII") )
322 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
324 // This means US-ASCII when returned from GetEncodingFromName().
329 // (3) finally use UTF-8 by default
330 if ( encName
.empty() )
331 encName
= wxT("UTF-8");
332 wxSetEnv(wxT("G_FILENAME_ENCODING"), encName
);
336 encName
= wxT("UTF-8");
338 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
339 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
340 // from gtk_init_check() as it does by default
341 gtk_disable_setlocale();
344 static wxConvBrokenFileNames
fileconv(encName
);
345 wxConvFileName
= &fileconv
;
352 // gtk_init() wants UTF-8, not wchar_t, so convert
353 char **argvGTK
= new char *[argc_
+ 1];
354 for ( i
= 0; i
< argc_
; i
++ )
356 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv_
[i
]));
359 argvGTK
[argc_
] = NULL
;
364 init_result
= true; // is there a _check() version of this?
365 gpe_application_init( &argcGTK
, &argvGTK
);
367 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
369 wxUpdateLocaleIsUtf8();
371 if ( argcGTK
!= argc_
)
373 // we have to drop the parameters which were consumed by GTK+
374 for ( i
= 0; i
< argcGTK
; i
++ )
376 while ( strcmp(wxConvUTF8
.cWX2MB(argv_
[i
]), argvGTK
[i
]) != 0 )
378 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc_
- i
)*sizeof(*argv_
));
385 //else: gtk_init() didn't modify our parameters
388 for ( i
= 0; i
< argcGTK
; i
++ )
394 #else // !wxUSE_UNICODE
395 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
396 // it's ok to pass pointer to it
397 init_result
= gtk_init_check( &argc_
, &argv_
);
398 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
400 // update internal arg[cv] as GTK+ may have removed processed options:
406 // if there are still GTK+ standard options unparsed in the command
407 // line, it means that they were not syntactically correct and GTK+
408 // already printed a warning on the command line and we should now
410 wxArrayString opt
, desc
;
411 m_traits
->GetStandardCmdLineOptions(opt
, desc
);
413 for ( i
= 0; i
< argc_
; i
++ )
415 // leave just the names of the options with values
416 const wxString str
= wxString(argv_
[i
]).BeforeFirst('=');
418 for ( size_t j
= 0; j
< opt
.size(); j
++ )
420 // remove the leading spaces from the option string as it does
422 if ( opt
[j
].Trim(false).BeforeFirst('=') == str
)
424 // a GTK+ option can be left on the command line only if
425 // there was an error in (or before, in another standard
426 // options) it, so abort, just as we do if incorrect
427 // program option is given
428 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
438 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
442 // we can not enter threads before gtk_init is done
446 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
449 // make sure GtkWidget type is loaded, idle hooks need it
450 g_type_class_ref(GTK_TYPE_WIDGET
);
456 void wxApp::CleanUp()
458 if (m_idleSourceId
!= 0)
459 g_source_remove(m_idleSourceId
);
461 // release reference acquired by Initialize()
462 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET
));
466 wxAppBase::CleanUp();
469 void wxApp::WakeUpIdle()
472 wxMutexLocker
lock(m_idleMutex
);
474 if (m_idleSourceId
== 0)
475 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
478 // Checking for pending events requires first removing our idle source,
479 // otherwise it will cause the check to always return true.
480 bool wxApp::EventsPending()
483 wxMutexLocker
lock(m_idleMutex
);
485 if (m_idleSourceId
!= 0)
487 g_source_remove(m_idleSourceId
);
491 return gtk_events_pending() != 0;
494 void wxApp::OnAssertFailure(const wxChar
*file
,
500 // there is no need to do anything if asserts are disabled in this build
503 // block wx idle events while assert dialog is showing
506 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
508 m_isInAssert
= false;
509 #else // !wxDEBUG_LEVEL
515 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
519 void wxGUIAppTraits::MutexGuiEnter()
524 void wxGUIAppTraits::MutexGuiLeave()
528 #endif // wxUSE_THREADS
530 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
531 // Maemo-specific method: get the main program object
532 HildonProgram
*wxApp::GetHildonProgram()
534 return hildon_program_get_instance();
537 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2