]>
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 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 behavior 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 behavior of wxMSW
136 ProcessPendingEvents();
138 needMore
= ProcessIdle();
139 } while (needMore
&& gtk_events_pending() == 0);
143 wxMutexLocker
lock(m_idleMutex
);
145 // if a new idle source was added during ProcessIdle
146 if (m_idleSourceId
!= 0)
149 g_source_remove(m_idleSourceId
);
153 // Pending events can be added asynchronously,
154 // need to keep idle source if any have appeared
155 if (HasPendingEvents())
158 // if more idle processing requested
161 // keep this source installed
162 m_idleSourceId
= id_save
;
165 // add hooks and remove this source
170 //-----------------------------------------------------------------------------
171 // Access to the root window global
172 //-----------------------------------------------------------------------------
174 GtkWidget
* wxGetRootWindow()
176 static GtkWidget
*s_RootWindow
= NULL
;
178 if (s_RootWindow
== NULL
)
180 s_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
181 gtk_widget_realize( s_RootWindow
);
186 //-----------------------------------------------------------------------------
188 //-----------------------------------------------------------------------------
190 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
194 m_isInAssert
= false;
202 bool wxApp::SetNativeTheme(const wxString
& theme
)
205 path
= gtk_rc_get_theme_dir();
207 path
+= theme
.utf8_str();
208 path
+= "/gtk-2.0/gtkrc";
210 if ( wxFileExists(path
.utf8_str()) )
211 gtk_rc_add_default_file(path
.utf8_str());
212 else if ( wxFileExists(theme
.utf8_str()) )
213 gtk_rc_add_default_file(theme
.utf8_str());
216 wxLogWarning("Theme \"%s\" not available.", theme
);
221 gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE
);
226 bool wxApp::OnInitGui()
228 if ( !wxAppBase::OnInitGui() )
231 // if this is a wxGLApp (derived from wxApp), and we've already
232 // chosen a specific visual, then derive the GdkVisual from that
233 if ( GetXVisualInfo() )
235 GdkVisual
* vis
= gtk_widget_get_default_visual();
237 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
238 gtk_widget_set_default_colormap( colormap
);
242 // On some machines, the default visual is just 256 colours, so
243 // we make sure we get the best. This can sometimes be wasteful.
246 if (m_forceTrueColour
)
248 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
251 wxLogError(wxT("Unable to initialize TrueColor visual."));
254 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
255 gtk_widget_set_default_colormap( colormap
);
259 if (gdk_visual_get_best() != gdk_visual_get_system())
261 GdkVisual
* visual
= gdk_visual_get_best();
262 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
263 gtk_widget_set_default_colormap( colormap
);
269 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
270 if ( !GetHildonProgram() )
272 wxLogError(_("Unable to initialize Hildon program"));
275 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2
280 GdkVisual
*wxApp::GetGdkVisual()
282 GdkVisual
*visual
= NULL
;
284 XVisualInfo
*xvi
= (XVisualInfo
*)GetXVisualInfo();
286 visual
= gdkx_visual_get( xvi
->visualid
);
288 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
295 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
296 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
298 if ( !wxAppBase::Initialize(argc_
, argv_
) )
302 if (!g_thread_supported())
307 #endif // wxUSE_THREADS
309 // gtk+ 2.0 supports Unicode through UTF-8 strings
310 wxConvCurrent
= &wxConvUTF8
;
312 // decide which conversion to use for the file names
314 // (1) this variable exists for the sole purpose of specifying the encoding
315 // of the filenames for GTK+ programs, so use it if it is set
316 wxString
encName(wxGetenv(wxT("G_FILENAME_ENCODING")));
317 encName
= encName
.BeforeFirst(wxT(','));
318 if (encName
.CmpNoCase(wxT("@locale")) == 0)
324 // (2) if a non default locale is set, assume that the user wants his
325 // filenames in this locale too
326 encName
= wxLocale::GetSystemEncodingName().Upper();
327 // (3) finally use UTF-8 by default
328 if (encName
.empty() || encName
== wxT("US-ASCII"))
329 encName
= wxT("UTF-8");
330 wxSetEnv(wxT("G_FILENAME_ENCODING"), encName
);
334 encName
= wxT("UTF-8");
336 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
337 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
338 // from gtk_init_check() as it does by default
339 gtk_disable_setlocale();
342 static wxConvBrokenFileNames
fileconv(encName
);
343 wxConvFileName
= &fileconv
;
350 // gtk_init() wants UTF-8, not wchar_t, so convert
351 char **argvGTK
= new char *[argc_
+ 1];
352 for ( i
= 0; i
< argc_
; i
++ )
354 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv_
[i
]));
357 argvGTK
[argc_
] = NULL
;
362 init_result
= true; // is there a _check() version of this?
363 gpe_application_init( &argcGTK
, &argvGTK
);
365 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
367 wxUpdateLocaleIsUtf8();
369 if ( argcGTK
!= argc_
)
371 // we have to drop the parameters which were consumed by GTK+
372 for ( i
= 0; i
< argcGTK
; i
++ )
374 while ( strcmp(wxConvUTF8
.cWX2MB(argv_
[i
]), argvGTK
[i
]) != 0 )
376 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc_
- i
)*sizeof(*argv_
));
383 //else: gtk_init() didn't modify our parameters
386 for ( i
= 0; i
< argcGTK
; i
++ )
392 #else // !wxUSE_UNICODE
393 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
394 // it's ok to pass pointer to it
395 init_result
= gtk_init_check( &argc_
, &argv_
);
396 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
398 // update internal arg[cv] as GTK+ may have removed processed options:
404 // if there are still GTK+ standard options unparsed in the command
405 // line, it means that they were not syntactically correct and GTK+
406 // already printed a warning on the command line and we should now
408 wxArrayString opt
, desc
;
409 m_traits
->GetStandardCmdLineOptions(opt
, desc
);
411 for ( i
= 0; i
< argc_
; i
++ )
413 // leave just the names of the options with values
414 const wxString str
= wxString(argv_
[i
]).BeforeFirst('=');
416 for ( size_t j
= 0; j
< opt
.size(); j
++ )
418 // remove the leading spaces from the option string as it does
420 if ( opt
[j
].Trim(false).BeforeFirst('=') == str
)
422 // a GTK+ option can be left on the command line only if
423 // there was an error in (or before, in another standard
424 // options) it, so abort, just as we do if incorrect
425 // program option is given
426 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
436 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
440 // we can not enter threads before gtk_init is done
444 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
447 // make sure GtkWidget type is loaded, idle hooks need it
448 g_type_class_ref(GTK_TYPE_WIDGET
);
454 void wxApp::CleanUp()
456 if (m_idleSourceId
!= 0)
457 g_source_remove(m_idleSourceId
);
459 // release reference acquired by Initialize()
460 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET
));
464 wxAppBase::CleanUp();
467 void wxApp::WakeUpIdle()
470 wxMutexLocker
lock(m_idleMutex
);
472 if (m_idleSourceId
== 0)
473 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
476 // Checking for pending events requires first removing our idle source,
477 // otherwise it will cause the check to always return true.
478 bool wxApp::EventsPending()
481 wxMutexLocker
lock(m_idleMutex
);
483 if (m_idleSourceId
!= 0)
485 g_source_remove(m_idleSourceId
);
489 return gtk_events_pending() != 0;
492 void wxApp::OnAssertFailure(const wxChar
*file
,
498 // there is no need to do anything if asserts are disabled in this build
501 // block wx idle events while assert dialog is showing
504 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
506 m_isInAssert
= false;
507 #else // !wxDEBUG_LEVEL
513 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
517 void wxGUIAppTraits::MutexGuiEnter()
522 void wxGUIAppTraits::MutexGuiLeave()
526 #endif // wxUSE_THREADS
528 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
529 // Maemo-specific method: get the main program object
530 HildonProgram
*wxApp::GetHildonProgram()
532 return hildon_program_get_instance();
535 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2