]>
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/apptrait.h"
30 #include "wx/fontmap.h"
33 #include <hildon-widgets/hildon-program.h>
34 #endif // wxUSE_LIBHILDON
37 #include <hildon/hildon.h>
38 #endif // wxUSE_LIBHILDON2
41 #include "wx/gtk/private.h"
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 behaviour 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 behaviour of wxMSW
137 ProcessPendingEvents();
139 needMore
= ProcessIdle();
140 } while (needMore
&& gtk_events_pending() == 0);
144 wxMutexLocker
lock(m_idleMutex
);
147 bool keepSource
= false;
148 // if a new idle source has not been added, either as a result of idle
149 // processing above or by another thread calling WakeUpIdle()
150 if (m_idleSourceId
== 0)
152 // if more idle processing was requested or pending events have appeared
153 if (needMore
|| HasPendingEvents())
155 // keep this source installed
156 m_idleSourceId
= id_save
;
159 else // add hooks and remove this source
162 // else remove this source, leave new one installed
163 // we must keep an idle source, otherwise a wakeup could be lost
168 //-----------------------------------------------------------------------------
169 // Access to the root window global
170 //-----------------------------------------------------------------------------
172 GtkWidget
* wxGetRootWindow()
174 static GtkWidget
*s_RootWindow
= NULL
;
176 if (s_RootWindow
== NULL
)
178 s_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
179 gtk_widget_realize( s_RootWindow
);
184 //-----------------------------------------------------------------------------
186 //-----------------------------------------------------------------------------
188 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
192 m_isInAssert
= false;
200 bool wxApp::SetNativeTheme(const wxString
& theme
)
207 path
= gtk_rc_get_theme_dir();
209 path
+= theme
.utf8_str();
210 path
+= "/gtk-2.0/gtkrc";
212 if ( wxFileExists(path
.utf8_str()) )
213 gtk_rc_add_default_file(path
.utf8_str());
214 else if ( wxFileExists(theme
.utf8_str()) )
215 gtk_rc_add_default_file(theme
.utf8_str());
218 wxLogWarning("Theme \"%s\" not available.", theme
);
223 gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE
);
229 bool wxApp::OnInitGui()
231 if ( !wxAppBase::OnInitGui() )
235 // if this is a wxGLApp (derived from wxApp), and we've already
236 // chosen a specific visual, then derive the GdkVisual from that
237 if ( GetXVisualInfo() )
239 GdkVisual
* vis
= gtk_widget_get_default_visual();
241 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
242 gtk_widget_set_default_colormap( colormap
);
246 // On some machines, the default visual is just 256 colours, so
247 // we make sure we get the best. This can sometimes be wasteful.
250 if (m_forceTrueColour
)
252 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
255 wxLogError(wxT("Unable to initialize TrueColor visual."));
258 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
259 gtk_widget_set_default_colormap( colormap
);
263 if (gdk_visual_get_best() != gdk_visual_get_system())
265 GdkVisual
* visual
= gdk_visual_get_best();
266 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
267 gtk_widget_set_default_colormap( colormap
);
274 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
275 if ( !GetHildonProgram() )
277 wxLogError(_("Unable to initialize Hildon program"));
280 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2
285 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
286 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
288 if ( !wxAppBase::Initialize(argc_
, argv_
) )
292 if (!g_thread_supported())
297 #endif // wxUSE_THREADS
299 // gtk+ 2.0 supports Unicode through UTF-8 strings
300 wxConvCurrent
= &wxConvUTF8
;
303 // decide which conversion to use for the file names
305 // (1) this variable exists for the sole purpose of specifying the encoding
306 // of the filenames for GTK+ programs, so use it if it is set
307 wxString
encName(wxGetenv(wxT("G_FILENAME_ENCODING")));
308 encName
= encName
.BeforeFirst(wxT(','));
309 if (encName
.CmpNoCase(wxT("@locale")) == 0)
315 // (2) if a non default locale is set, assume that the user wants his
316 // filenames in this locale too
317 encName
= wxLocale::GetSystemEncodingName().Upper();
319 // But don't consider ASCII in this case.
320 if ( !encName
.empty() )
323 wxFontEncoding enc
= wxFontMapperBase::GetEncodingFromName(encName
);
324 if ( enc
== wxFONTENCODING_DEFAULT
)
325 #else // !wxUSE_FONTMAP
326 if ( encName
== wxT("US-ASCII") )
327 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
329 // 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
);
341 encName
= wxT("UTF-8");
343 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
344 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
345 // from gtk_init_check() as it does by default
346 gtk_disable_setlocale();
349 static wxConvBrokenFileNames
fileconv(encName
);
350 wxConvFileName
= &fileconv
;
358 // gtk_init() wants UTF-8, not wchar_t, so convert
359 char **argvGTK
= new char *[argc_
+ 1];
360 for ( i
= 0; i
< argc_
; i
++ )
362 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv_
[i
]));
365 argvGTK
[argc_
] = NULL
;
370 init_result
= true; // is there a _check() version of this?
371 gpe_application_init( &argcGTK
, &argvGTK
);
373 init_result
= gtk_init_check( &argcGTK
, &argvGTK
) != 0;
375 wxUpdateLocaleIsUtf8();
377 if ( argcGTK
!= argc_
)
379 // we have to drop the parameters which were consumed by GTK+
380 for ( i
= 0; i
< argcGTK
; i
++ )
382 while ( strcmp(wxConvUTF8
.cWX2MB(argv_
[i
]), argvGTK
[i
]) != 0 )
384 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc_
- i
)*sizeof(*argv_
));
391 //else: gtk_init() didn't modify our parameters
394 for ( i
= 0; i
< argcGTK
; i
++ )
400 #else // !wxUSE_UNICODE
401 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
402 // it's ok to pass pointer to it
403 init_result
= gtk_init_check( &argc_
, &argv_
);
404 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
406 // update internal arg[cv] as GTK+ may have removed processed options:
412 // if there are still GTK+ standard options unparsed in the command
413 // line, it means that they were not syntactically correct and GTK+
414 // already printed a warning on the command line and we should now
416 wxArrayString opt
, desc
;
417 m_traits
->GetStandardCmdLineOptions(opt
, desc
);
419 for ( i
= 0; i
< argc_
; i
++ )
421 // leave just the names of the options with values
422 const wxString str
= wxString(argv_
[i
]).BeforeFirst('=');
424 for ( size_t j
= 0; j
< opt
.size(); j
++ )
426 // remove the leading spaces from the option string as it does
428 if ( opt
[j
].Trim(false).BeforeFirst('=') == str
)
430 // a GTK+ option can be left on the command line only if
431 // there was an error in (or before, in another standard
432 // options) it, so abort, just as we do if incorrect
433 // program option is given
434 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
444 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
448 // we cannot enter threads before gtk_init is done
452 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
455 // make sure GtkWidget type is loaded, idle hooks need it
456 g_type_class_ref(GTK_TYPE_WIDGET
);
462 void wxApp::CleanUp()
464 if (m_idleSourceId
!= 0)
465 g_source_remove(m_idleSourceId
);
467 // release reference acquired by Initialize()
468 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET
));
472 wxAppBase::CleanUp();
475 void wxApp::WakeUpIdle()
478 wxMutexLocker
lock(m_idleMutex
);
480 if (m_idleSourceId
== 0)
481 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
484 // Checking for pending events requires first removing our idle source,
485 // otherwise it will cause the check to always return true.
486 bool wxApp::EventsPending()
489 wxMutexLocker
lock(m_idleMutex
);
491 if (m_idleSourceId
!= 0)
493 g_source_remove(m_idleSourceId
);
497 return gtk_events_pending() != 0;
500 void wxApp::OnAssertFailure(const wxChar
*file
,
506 // there is no need to do anything if asserts are disabled in this build
509 // block wx idle events while assert dialog is showing
512 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
514 m_isInAssert
= false;
515 #else // !wxDEBUG_LEVEL
521 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
525 void wxGUIAppTraits::MutexGuiEnter()
530 void wxGUIAppTraits::MutexGuiLeave()
534 #endif // wxUSE_THREADS
536 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
537 // Maemo-specific method: get the main program object
538 HildonProgram
*wxApp::GetHildonProgram()
540 return hildon_program_get_instance();
543 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2