]>
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
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 #if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
44 wxFORCE_LINK_MODULE(gnome_vfs
)
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 // One-shot signal emission hook, to install idle handler.
54 wx_emission_hook(GSignalInvocationHint
*, guint
, const GValue
*, gpointer data
)
56 wxApp
* app
= wxTheApp
;
59 gulong
* hook_id
= (gulong
*)data
;
60 // record that hook is not installed
67 // Add signal emission hooks, to re-install idle handler when needed.
68 static void wx_add_idle_hooks()
72 static gulong hook_id
= 0;
75 static guint sig_id
= 0;
77 sig_id
= g_signal_lookup("event", GTK_TYPE_WIDGET
);
78 hook_id
= g_signal_add_emission_hook(
79 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
82 // "size_allocate" hook
83 // Needed to match the behavior of the old idle system,
84 // but probably not necessary.
86 static gulong hook_id
= 0;
89 static guint sig_id
= 0;
91 sig_id
= g_signal_lookup("size_allocate", GTK_TYPE_WIDGET
);
92 hook_id
= g_signal_add_emission_hook(
93 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
99 static gboolean
wxapp_idle_callback(gpointer
)
101 return wxTheApp
->DoIdle();
109 // Allow another idle source to be added while this one is busy.
110 // Needed if an idle event handler runs a new event loop,
111 // for example by showing a dialog.
113 wxMutexLocker
lock(*m_idleMutex
);
115 id_save
= m_idleSourceId
;
120 // don't generate the idle events while the assert modal dialog is shown,
121 // this matches the behavior of wxMSW
130 needMore
= ProcessIdle();
131 } while (needMore
&& gtk_events_pending() == 0);
135 wxMutexLocker
lock(*m_idleMutex
);
137 // if a new idle source was added during ProcessIdle
138 if (m_idleSourceId
!= 0)
141 g_source_remove(m_idleSourceId
);
145 // Pending events can be added asynchronously,
146 // need to keep idle source if any have appeared
147 needMore
= needMore
|| HasPendingEvents();
149 // if more idle processing requested
152 // keep this source installed
153 m_idleSourceId
= id_save
;
156 // add hooks and remove this source
161 //-----------------------------------------------------------------------------
162 // Access to the root window global
163 //-----------------------------------------------------------------------------
165 GtkWidget
* wxGetRootWindow()
167 static GtkWidget
*s_RootWindow
= NULL
;
169 if (s_RootWindow
== NULL
)
171 s_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
172 gtk_widget_realize( s_RootWindow
);
177 //-----------------------------------------------------------------------------
179 //-----------------------------------------------------------------------------
181 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
185 m_isInAssert
= false;
197 bool wxApp::SetNativeTheme(const wxString
& theme
)
200 path
= gtk_rc_get_theme_dir();
202 path
+= theme
.utf8_str();
203 path
+= "/gtk-2.0/gtkrc";
205 if ( wxFileExists(path
.utf8_str()) )
206 gtk_rc_add_default_file(path
.utf8_str());
207 else if ( wxFileExists(theme
.utf8_str()) )
208 gtk_rc_add_default_file(theme
.utf8_str());
211 wxLogWarning("Theme \"%s\" not available.", theme
);
216 gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE
);
221 bool wxApp::OnInitGui()
223 if ( !wxAppBase::OnInitGui() )
226 // if this is a wxGLApp (derived from wxApp), and we've already
227 // chosen a specific visual, then derive the GdkVisual from that
228 if ( GetXVisualInfo() )
230 GdkVisual
* vis
= gtk_widget_get_default_visual();
232 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
233 gtk_widget_set_default_colormap( colormap
);
237 // On some machines, the default visual is just 256 colours, so
238 // we make sure we get the best. This can sometimes be wasteful.
241 if (m_forceTrueColour
)
243 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
246 wxLogError(wxT("Unable to initialize TrueColor visual."));
249 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
250 gtk_widget_set_default_colormap( colormap
);
254 if (gdk_visual_get_best() != gdk_visual_get_system())
256 GdkVisual
* visual
= gdk_visual_get_best();
257 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
258 gtk_widget_set_default_colormap( colormap
);
265 m_hildonProgram
= hildon_program_get_instance();
266 if ( !m_hildonProgram
)
268 wxLogError(_("Unable to initialize Hildon program"));
271 #endif // wxUSE_LIBHILDON
276 GdkVisual
*wxApp::GetGdkVisual()
278 GdkVisual
*visual
= NULL
;
280 XVisualInfo
*xvi
= (XVisualInfo
*)GetXVisualInfo();
282 visual
= gdkx_visual_get( xvi
->visualid
);
284 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
291 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
292 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
294 if ( !wxAppBase::Initialize(argc_
, argv_
) )
298 if (!g_thread_supported())
303 #endif // wxUSE_THREADS
305 // gtk+ 2.0 supports Unicode through UTF-8 strings
306 wxConvCurrent
= &wxConvUTF8
;
308 // decide which conversion to use for the file names
310 // (1) this variable exists for the sole purpose of specifying the encoding
311 // of the filenames for GTK+ programs, so use it if it is set
312 wxString
encName(wxGetenv(_T("G_FILENAME_ENCODING")));
313 encName
= encName
.BeforeFirst(_T(','));
314 if (encName
.CmpNoCase(_T("@locale")) == 0)
320 // (2) if a non default locale is set, assume that the user wants his
321 // filenames in this locale too
322 encName
= wxLocale::GetSystemEncodingName().Upper();
323 // (3) finally use UTF-8 by default
324 if (encName
.empty() || encName
== _T("US-ASCII"))
325 encName
= _T("UTF-8");
326 wxSetEnv(_T("G_FILENAME_ENCODING"), encName
);
330 encName
= _T("UTF-8");
332 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
333 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
334 // from gtk_init_check() as it does by default
335 gtk_disable_setlocale();
338 static wxConvBrokenFileNames
fileconv(encName
);
339 wxConvFileName
= &fileconv
;
346 // gtk_init() wants UTF-8, not wchar_t, so convert
347 char **argvGTK
= new char *[argc_
+ 1];
348 for ( i
= 0; i
< argc_
; i
++ )
350 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv_
[i
]));
353 argvGTK
[argc_
] = NULL
;
358 init_result
= true; // is there a _check() version of this?
359 gpe_application_init( &argcGTK
, &argvGTK
);
361 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
363 wxUpdateLocaleIsUtf8();
365 if ( argcGTK
!= argc_
)
367 // we have to drop the parameters which were consumed by GTK+
368 for ( i
= 0; i
< argcGTK
; i
++ )
370 while ( strcmp(wxConvUTF8
.cWX2MB(argv_
[i
]), argvGTK
[i
]) != 0 )
372 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc_
- i
)*sizeof(*argv_
));
379 //else: gtk_init() didn't modify our parameters
382 for ( i
= 0; i
< argcGTK
; i
++ )
388 #else // !wxUSE_UNICODE
389 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
390 // it's ok to pass pointer to it
391 init_result
= gtk_init_check( &argc_
, &argv_
);
392 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
394 // update internal arg[cv] as GTK+ may have removed processed options:
400 // if there are still GTK+ standard options unparsed in the command
401 // line, it means that they were not syntactically correct and GTK+
402 // already printed a warning on the command line and we should now
404 wxArrayString opt
, desc
;
405 m_traits
->GetStandardCmdLineOptions(opt
, desc
);
407 for ( i
= 0; i
< argc_
; i
++ )
409 // leave just the names of the options with values
410 const wxString str
= wxString(argv_
[i
]).BeforeFirst('=');
412 for ( size_t j
= 0; j
< opt
.size(); j
++ )
414 // remove the leading spaces from the option string as it does
416 if ( opt
[j
].Trim(false).BeforeFirst('=') == str
)
418 // a GTK+ option can be left on the command line only if
419 // there was an error in (or before, in another standard
420 // options) it, so abort, just as we do if incorrect
421 // program option is given
422 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
432 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
436 // we can not enter threads before gtk_init is done
439 wxSetDetectableAutoRepeat( true );
442 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
446 m_idleMutex
= new wxMutex
;
448 // make sure GtkWidget type is loaded, idle hooks need it
449 g_type_class_ref(GTK_TYPE_WIDGET
);
455 void wxApp::CleanUp()
457 if (m_idleSourceId
!= 0)
458 g_source_remove(m_idleSourceId
);
460 // release reference acquired by Initialize()
461 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET
));
465 wxAppBase::CleanUp();
467 // delete this mutex as late as possible as it's used from WakeUpIdle(), in
468 // particular do it after calling the base class CleanUp() which can result
469 // in it being called
476 void wxApp::WakeUpIdle()
479 wxMutexLocker
lock(*m_idleMutex
);
481 if (m_idleSourceId
== 0)
482 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
485 // Checking for pending events requires first removing our idle source,
486 // otherwise it will cause the check to always return true.
487 bool wxApp::EventsPending()
490 wxMutexLocker
lock(*m_idleMutex
);
492 if (m_idleSourceId
!= 0)
494 g_source_remove(m_idleSourceId
);
498 return gtk_events_pending() != 0;
501 void wxApp::OnAssertFailure(const wxChar
*file
,
507 // there is no need to do anything if asserts are disabled in this build
510 // block wx idle events while assert dialog is showing
513 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
515 m_isInAssert
= false;
516 #else // !wxDEBUG_LEVEL
522 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
526 void wxGUIAppTraits::MutexGuiEnter()
531 void wxGUIAppTraits::MutexGuiLeave()
535 #endif // wxUSE_THREADS