]>
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 static GtkWidget
*gs_RootWindow
= (GtkWidget
*) NULL
;
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 // not static because used by textctrl.cpp
60 bool wxIsInsideYield
= false;
62 bool wxApp::Yield(bool onlyIfNeeded
)
64 if ( wxIsInsideYield
)
68 wxFAIL_MSG( wxT("wxYield called recursively" ) );
75 if ( !wxThread::IsMain() )
77 // can't call gtk_main_iteration() from other threads like this
80 #endif // wxUSE_THREADS
82 wxIsInsideYield
= true;
85 // disable log flushing from here because a call to wxYield() shouldn't
86 // normally result in message boxes popping up &c
90 while (EventsPending())
93 // It's necessary to call ProcessIdle() to update the frames sizes which
94 // might have been changed (it also will update other things set from
95 // OnUpdateUI() which is a nice (and desired) side effect). But we
96 // call ProcessIdle() only once since this is not meant for longish
97 // background jobs (controlled by wxIdleEvent::RequestMore() and the
98 // return value of Processidle().
102 // let the logs be flashed again
106 wxIsInsideYield
= false;
111 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
115 // One-shot signal emission hook, to install idle handler.
118 wx_emission_hook(GSignalInvocationHint
*, guint
, const GValue
*, gpointer data
)
120 wxApp
* app
= wxTheApp
;
123 gulong
* hook_id
= (gulong
*)data
;
124 // record that hook is not installed
131 // Add signal emission hooks, to re-install idle handler when needed.
132 static void wx_add_idle_hooks()
136 static gulong hook_id
= 0;
139 static guint sig_id
= 0;
141 sig_id
= g_signal_lookup("event", GTK_TYPE_WIDGET
);
142 hook_id
= g_signal_add_emission_hook(
143 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
146 // "size_allocate" hook
147 // Needed to match the behavior of the old idle system,
148 // but probably not necessary.
150 static gulong hook_id
= 0;
153 static guint sig_id
= 0;
155 sig_id
= g_signal_lookup("size_allocate", GTK_TYPE_WIDGET
);
156 hook_id
= g_signal_add_emission_hook(
157 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
163 static gboolean
wxapp_idle_callback(gpointer
)
165 return wxTheApp
->DoIdle();
173 // Allow another idle source to be added while this one is busy.
174 // Needed if an idle event handler runs a new event loop,
175 // for example by showing a dialog.
177 wxMutexLocker
lock(*m_idleMutex
);
179 id_save
= m_idleSourceId
;
183 // don't generate the idle events while the assert modal dialog is shown,
184 // this matches the behavior of wxMSW
193 needMore
= ProcessIdle();
194 } while (needMore
&& gtk_events_pending() == 0);
198 wxMutexLocker
lock(*m_idleMutex
);
200 // if a new idle source was added during ProcessIdle
201 if (m_idleSourceId
!= 0)
204 g_source_remove(m_idleSourceId
);
208 // Pending events can be added asynchronously,
209 // need to keep idle source if any have appeared
210 needMore
= needMore
|| HasPendingEvents();
212 // if more idle processing requested
215 // keep this source installed
216 m_idleSourceId
= id_save
;
219 // add hooks and remove this source
224 //-----------------------------------------------------------------------------
225 // Access to the root window global
226 //-----------------------------------------------------------------------------
228 GtkWidget
* wxGetRootWindow()
230 if (gs_RootWindow
== NULL
)
232 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
233 gtk_widget_realize( gs_RootWindow
);
235 return gs_RootWindow
;
238 //-----------------------------------------------------------------------------
240 //-----------------------------------------------------------------------------
242 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
247 m_isInAssert
= false;
248 #endif // __WXDEBUG__
259 bool wxApp::SetNativeTheme(const wxString
& theme
)
262 path
= gtk_rc_get_theme_dir();
264 path
+= theme
.utf8_str();
265 path
+= "/gtk-2.0/gtkrc";
267 if ( wxFileExists(path
.utf8_str()) )
268 gtk_rc_add_default_file(path
.utf8_str());
269 else if ( wxFileExists(theme
.utf8_str()) )
270 gtk_rc_add_default_file(theme
.utf8_str());
273 wxLogWarning("Theme \"%s\" not available.", theme
);
278 gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE
);
283 bool wxApp::OnInitGui()
285 if ( !wxAppBase::OnInitGui() )
288 // if this is a wxGLApp (derived from wxApp), and we've already
289 // chosen a specific visual, then derive the GdkVisual from that
290 if ( GetXVisualInfo() )
292 GdkVisual
* vis
= gtk_widget_get_default_visual();
294 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
295 gtk_widget_set_default_colormap( colormap
);
299 // On some machines, the default visual is just 256 colours, so
300 // we make sure we get the best. This can sometimes be wasteful.
303 if (m_forceTrueColour
)
305 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
308 wxLogError(wxT("Unable to initialize TrueColor visual."));
311 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
312 gtk_widget_set_default_colormap( colormap
);
316 if (gdk_visual_get_best() != gdk_visual_get_system())
318 GdkVisual
* visual
= gdk_visual_get_best();
319 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
320 gtk_widget_set_default_colormap( colormap
);
327 m_hildonProgram
= hildon_program_get_instance();
328 if ( !m_hildonProgram
)
330 wxLogError(_("Unable to initialize Hildon program"));
333 #endif // wxUSE_LIBHILDON
338 GdkVisual
*wxApp::GetGdkVisual()
340 GdkVisual
*visual
= NULL
;
342 XVisualInfo
*xvi
= (XVisualInfo
*)GetXVisualInfo();
344 visual
= gdkx_visual_get( xvi
->visualid
);
346 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
353 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
354 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
356 if ( !wxAppBase::Initialize(argc_
, argv_
) )
360 if (!g_thread_supported())
365 #endif // wxUSE_THREADS
367 // We should have the wxUSE_WCHAR_T test on the _outside_
369 // gtk+ 2.0 supports Unicode through UTF-8 strings
370 wxConvCurrent
= &wxConvUTF8
;
371 #else // !wxUSE_WCHAR_T
373 wxConvCurrent
= (wxMBConv
*) NULL
;
374 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
376 // decide which conversion to use for the file names
378 // (1) this variable exists for the sole purpose of specifying the encoding
379 // of the filenames for GTK+ programs, so use it if it is set
380 wxString
encName(wxGetenv(_T("G_FILENAME_ENCODING")));
381 encName
= encName
.BeforeFirst(_T(','));
382 if (encName
.CmpNoCase(_T("@locale")) == 0)
388 // (2) if a non default locale is set, assume that the user wants his
389 // filenames in this locale too
390 encName
= wxLocale::GetSystemEncodingName().Upper();
391 // (3) finally use UTF-8 by default
392 if (encName
.empty() || encName
== _T("US-ASCII"))
393 encName
= _T("UTF-8");
394 wxSetEnv(_T("G_FILENAME_ENCODING"), encName
);
398 encName
= _T("UTF-8");
400 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
401 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
402 // from gtk_init_check() as it does by default
403 gtk_disable_setlocale();
406 static wxConvBrokenFileNames
fileconv(encName
);
407 wxConvFileName
= &fileconv
;
414 // gtk_init() wants UTF-8, not wchar_t, so convert
415 char **argvGTK
= new char *[argc_
+ 1];
416 for ( i
= 0; i
< argc_
; i
++ )
418 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv_
[i
]));
421 argvGTK
[argc_
] = NULL
;
426 init_result
= true; // is there a _check() version of this?
427 gpe_application_init( &argcGTK
, &argvGTK
);
429 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
431 wxUpdateLocaleIsUtf8();
433 if ( argcGTK
!= argc_
)
435 // we have to drop the parameters which were consumed by GTK+
436 for ( i
= 0; i
< argcGTK
; i
++ )
438 while ( strcmp(wxConvUTF8
.cWX2MB(argv_
[i
]), argvGTK
[i
]) != 0 )
440 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc_
- i
)*sizeof(*argv_
));
447 //else: gtk_init() didn't modify our parameters
450 for ( i
= 0; i
< argcGTK
; i
++ )
456 #else // !wxUSE_UNICODE
457 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
458 // it's ok to pass pointer to it
459 init_result
= gtk_init_check( &argc_
, &argv_
);
460 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
462 // update internal arg[cv] as GTK+ may have removed processed options:
468 // if there are still GTK+ standard options unparsed in the command
469 // line, it means that they were not syntactically correct and GTK+
470 // already printed a warning on the command line and we should now
472 wxArrayString opt
, desc
;
473 m_traits
->GetStandardCmdLineOptions(opt
, desc
);
475 for ( i
= 0; i
< argc_
; i
++ )
477 // leave just the names of the options with values
478 const wxString str
= wxString(argv_
[i
]).BeforeFirst('=');
480 for ( size_t j
= 0; j
< opt
.size(); j
++ )
482 // remove the leading spaces from the option string as it does
484 if ( opt
[j
].Trim(false).BeforeFirst('=') == str
)
486 // a GTK+ option can be left on the command line only if
487 // there was an error in (or before, in another standard
488 // options) it, so abort, just as we do if incorrect
489 // program option is given
490 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
500 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
504 // we can not enter threads before gtk_init is done
507 wxSetDetectableAutoRepeat( true );
510 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
514 m_idleMutex
= new wxMutex
;
516 // make sure GtkWidget type is loaded, idle hooks need it
517 g_type_class_ref(GTK_TYPE_WIDGET
);
523 void wxApp::CleanUp()
525 if (m_idleSourceId
!= 0)
526 g_source_remove(m_idleSourceId
);
528 // release reference acquired by Initialize()
529 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET
));
533 wxAppBase::CleanUp();
535 // delete this mutex as late as possible as it's used from WakeUpIdle(), in
536 // particular do it after calling the base class CleanUp() which can result
537 // in it being called
544 void wxApp::WakeUpIdle()
547 wxMutexLocker
lock(*m_idleMutex
);
549 if (m_idleSourceId
== 0)
550 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
553 // Checking for pending events requires first removing our idle source,
554 // otherwise it will cause the check to always return true.
555 bool wxApp::EventsPending()
558 wxMutexLocker
lock(*m_idleMutex
);
560 if (m_idleSourceId
!= 0)
562 g_source_remove(m_idleSourceId
);
566 return gtk_events_pending() != 0;
571 void wxApp::OnAssertFailure(const wxChar
*file
,
578 // block wx idle events while assert dialog is showing
581 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
583 m_isInAssert
= false;
586 #endif // __WXDEBUG__
589 void wxGUIAppTraits::MutexGuiEnter()
594 void wxGUIAppTraits::MutexGuiLeave()
598 #endif // wxUSE_THREADS