]>
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
= NULL
;
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 bool wxApp::Yield(bool onlyIfNeeded
)
59 if ( m_isInsideYield
)
63 wxFAIL_MSG( wxT("wxYield called recursively" ) );
70 if ( !wxThread::IsMain() )
72 // can't call gtk_main_iteration() from other threads like this
75 #endif // wxUSE_THREADS
77 m_isInsideYield
= true;
80 // disable log flushing from here because a call to wxYield() shouldn't
81 // normally result in message boxes popping up &c
85 while (EventsPending())
88 // It's necessary to call ProcessIdle() to update the frames sizes which
89 // might have been changed (it also will update other things set from
90 // OnUpdateUI() which is a nice (and desired) side effect). But we
91 // call ProcessIdle() only once since this is not meant for longish
92 // background jobs (controlled by wxIdleEvent::RequestMore() and the
93 // return value of Processidle().
97 // let the logs be flashed again
101 m_isInsideYield
= false;
106 //-----------------------------------------------------------------------------
108 //-----------------------------------------------------------------------------
110 // One-shot signal emission hook, to install idle handler.
113 wx_emission_hook(GSignalInvocationHint
*, guint
, const GValue
*, gpointer data
)
115 wxApp
* app
= wxTheApp
;
118 gulong
* hook_id
= (gulong
*)data
;
119 // record that hook is not installed
126 // Add signal emission hooks, to re-install idle handler when needed.
127 static void wx_add_idle_hooks()
131 static gulong hook_id
= 0;
134 static guint sig_id
= 0;
136 sig_id
= g_signal_lookup("event", GTK_TYPE_WIDGET
);
137 hook_id
= g_signal_add_emission_hook(
138 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
141 // "size_allocate" hook
142 // Needed to match the behavior of the old idle system,
143 // but probably not necessary.
145 static gulong hook_id
= 0;
148 static guint sig_id
= 0;
150 sig_id
= g_signal_lookup("size_allocate", GTK_TYPE_WIDGET
);
151 hook_id
= g_signal_add_emission_hook(
152 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
158 static gboolean
wxapp_idle_callback(gpointer
)
160 return wxTheApp
->DoIdle();
168 // Allow another idle source to be added while this one is busy.
169 // Needed if an idle event handler runs a new event loop,
170 // for example by showing a dialog.
172 wxMutexLocker
lock(*m_idleMutex
);
174 id_save
= m_idleSourceId
;
178 // don't generate the idle events while the assert modal dialog is shown,
179 // this matches the behavior of wxMSW
188 needMore
= ProcessIdle();
189 } while (needMore
&& gtk_events_pending() == 0);
193 wxMutexLocker
lock(*m_idleMutex
);
195 // if a new idle source was added during ProcessIdle
196 if (m_idleSourceId
!= 0)
199 g_source_remove(m_idleSourceId
);
203 // Pending events can be added asynchronously,
204 // need to keep idle source if any have appeared
205 needMore
= needMore
|| HasPendingEvents();
207 // if more idle processing requested
210 // keep this source installed
211 m_idleSourceId
= id_save
;
214 // add hooks and remove this source
219 //-----------------------------------------------------------------------------
220 // Access to the root window global
221 //-----------------------------------------------------------------------------
223 GtkWidget
* wxGetRootWindow()
225 if (gs_RootWindow
== NULL
)
227 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
228 gtk_widget_realize( gs_RootWindow
);
230 return gs_RootWindow
;
233 //-----------------------------------------------------------------------------
235 //-----------------------------------------------------------------------------
237 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
242 m_isInAssert
= false;
243 #endif // __WXDEBUG__
254 bool wxApp::SetNativeTheme(const wxString
& theme
)
257 path
= gtk_rc_get_theme_dir();
259 path
+= theme
.utf8_str();
260 path
+= "/gtk-2.0/gtkrc";
262 if ( wxFileExists(path
.utf8_str()) )
263 gtk_rc_add_default_file(path
.utf8_str());
264 else if ( wxFileExists(theme
.utf8_str()) )
265 gtk_rc_add_default_file(theme
.utf8_str());
268 wxLogWarning("Theme \"%s\" not available.", theme
);
273 gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE
);
278 bool wxApp::OnInitGui()
280 if ( !wxAppBase::OnInitGui() )
283 // if this is a wxGLApp (derived from wxApp), and we've already
284 // chosen a specific visual, then derive the GdkVisual from that
285 if ( GetXVisualInfo() )
287 GdkVisual
* vis
= gtk_widget_get_default_visual();
289 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
290 gtk_widget_set_default_colormap( colormap
);
294 // On some machines, the default visual is just 256 colours, so
295 // we make sure we get the best. This can sometimes be wasteful.
298 if (m_forceTrueColour
)
300 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
303 wxLogError(wxT("Unable to initialize TrueColor visual."));
306 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
307 gtk_widget_set_default_colormap( colormap
);
311 if (gdk_visual_get_best() != gdk_visual_get_system())
313 GdkVisual
* visual
= gdk_visual_get_best();
314 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
315 gtk_widget_set_default_colormap( colormap
);
322 m_hildonProgram
= hildon_program_get_instance();
323 if ( !m_hildonProgram
)
325 wxLogError(_("Unable to initialize Hildon program"));
328 #endif // wxUSE_LIBHILDON
333 GdkVisual
*wxApp::GetGdkVisual()
335 GdkVisual
*visual
= NULL
;
337 XVisualInfo
*xvi
= (XVisualInfo
*)GetXVisualInfo();
339 visual
= gdkx_visual_get( xvi
->visualid
);
341 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
348 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
349 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
351 if ( !wxAppBase::Initialize(argc_
, argv_
) )
355 if (!g_thread_supported())
360 #endif // wxUSE_THREADS
362 // gtk+ 2.0 supports Unicode through UTF-8 strings
363 wxConvCurrent
= &wxConvUTF8
;
365 // decide which conversion to use for the file names
367 // (1) this variable exists for the sole purpose of specifying the encoding
368 // of the filenames for GTK+ programs, so use it if it is set
369 wxString
encName(wxGetenv(_T("G_FILENAME_ENCODING")));
370 encName
= encName
.BeforeFirst(_T(','));
371 if (encName
.CmpNoCase(_T("@locale")) == 0)
377 // (2) if a non default locale is set, assume that the user wants his
378 // filenames in this locale too
379 encName
= wxLocale::GetSystemEncodingName().Upper();
380 // (3) finally use UTF-8 by default
381 if (encName
.empty() || encName
== _T("US-ASCII"))
382 encName
= _T("UTF-8");
383 wxSetEnv(_T("G_FILENAME_ENCODING"), encName
);
387 encName
= _T("UTF-8");
389 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
390 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
391 // from gtk_init_check() as it does by default
392 gtk_disable_setlocale();
395 static wxConvBrokenFileNames
fileconv(encName
);
396 wxConvFileName
= &fileconv
;
403 // gtk_init() wants UTF-8, not wchar_t, so convert
404 char **argvGTK
= new char *[argc_
+ 1];
405 for ( i
= 0; i
< argc_
; i
++ )
407 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv_
[i
]));
410 argvGTK
[argc_
] = NULL
;
415 init_result
= true; // is there a _check() version of this?
416 gpe_application_init( &argcGTK
, &argvGTK
);
418 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
420 wxUpdateLocaleIsUtf8();
422 if ( argcGTK
!= argc_
)
424 // we have to drop the parameters which were consumed by GTK+
425 for ( i
= 0; i
< argcGTK
; i
++ )
427 while ( strcmp(wxConvUTF8
.cWX2MB(argv_
[i
]), argvGTK
[i
]) != 0 )
429 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc_
- i
)*sizeof(*argv_
));
436 //else: gtk_init() didn't modify our parameters
439 for ( i
= 0; i
< argcGTK
; i
++ )
445 #else // !wxUSE_UNICODE
446 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
447 // it's ok to pass pointer to it
448 init_result
= gtk_init_check( &argc_
, &argv_
);
449 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
451 // update internal arg[cv] as GTK+ may have removed processed options:
457 // if there are still GTK+ standard options unparsed in the command
458 // line, it means that they were not syntactically correct and GTK+
459 // already printed a warning on the command line and we should now
461 wxArrayString opt
, desc
;
462 m_traits
->GetStandardCmdLineOptions(opt
, desc
);
464 for ( i
= 0; i
< argc_
; i
++ )
466 // leave just the names of the options with values
467 const wxString str
= wxString(argv_
[i
]).BeforeFirst('=');
469 for ( size_t j
= 0; j
< opt
.size(); j
++ )
471 // remove the leading spaces from the option string as it does
473 if ( opt
[j
].Trim(false).BeforeFirst('=') == str
)
475 // a GTK+ option can be left on the command line only if
476 // there was an error in (or before, in another standard
477 // options) it, so abort, just as we do if incorrect
478 // program option is given
479 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
489 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
493 // we can not enter threads before gtk_init is done
496 wxSetDetectableAutoRepeat( true );
499 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
503 m_idleMutex
= new wxMutex
;
505 // make sure GtkWidget type is loaded, idle hooks need it
506 g_type_class_ref(GTK_TYPE_WIDGET
);
512 void wxApp::CleanUp()
514 if (m_idleSourceId
!= 0)
515 g_source_remove(m_idleSourceId
);
517 // release reference acquired by Initialize()
518 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET
));
522 wxAppBase::CleanUp();
524 // delete this mutex as late as possible as it's used from WakeUpIdle(), in
525 // particular do it after calling the base class CleanUp() which can result
526 // in it being called
533 void wxApp::WakeUpIdle()
536 wxMutexLocker
lock(*m_idleMutex
);
538 if (m_idleSourceId
== 0)
539 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
542 // Checking for pending events requires first removing our idle source,
543 // otherwise it will cause the check to always return true.
544 bool wxApp::EventsPending()
547 wxMutexLocker
lock(*m_idleMutex
);
549 if (m_idleSourceId
!= 0)
551 g_source_remove(m_idleSourceId
);
555 return gtk_events_pending() != 0;
560 void wxApp::OnAssertFailure(const wxChar
*file
,
567 // block wx idle events while assert dialog is showing
570 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
572 m_isInAssert
= false;
575 #endif // __WXDEBUG__
578 void wxGUIAppTraits::MutexGuiEnter()
583 void wxGUIAppTraits::MutexGuiLeave()
587 #endif // wxUSE_THREADS