]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/app.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling, Julian Smart | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #include "wx/app.h" | |
14 | ||
15 | #ifndef WX_PRECOMP | |
16 | #include "wx/intl.h" | |
17 | #include "wx/log.h" | |
18 | #include "wx/utils.h" | |
19 | #include "wx/memory.h" | |
20 | #include "wx/font.h" | |
21 | #endif | |
22 | ||
23 | #include "wx/thread.h" | |
24 | ||
25 | #ifdef __WXGPE__ | |
26 | #include <gpe/init.h> | |
27 | #endif | |
28 | ||
29 | #include "wx/gtk/private.h" | |
30 | #include "wx/apptrait.h" | |
31 | ||
32 | #if wxUSE_LIBHILDON | |
33 | #include <hildon-widgets/hildon-program.h> | |
34 | #endif // wxUSE_LIBHILDON | |
35 | ||
36 | #include <gdk/gdkx.h> | |
37 | ||
38 | //----------------------------------------------------------------------------- | |
39 | // link GnomeVFS | |
40 | //----------------------------------------------------------------------------- | |
41 | ||
42 | #if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS | |
43 | #include "wx/link.h" | |
44 | wxFORCE_LINK_MODULE(gnome_vfs) | |
45 | #endif | |
46 | ||
47 | //----------------------------------------------------------------------------- | |
48 | // local functions | |
49 | //----------------------------------------------------------------------------- | |
50 | ||
51 | // One-shot signal emission hook, to install idle handler. | |
52 | extern "C" { | |
53 | static gboolean | |
54 | wx_emission_hook(GSignalInvocationHint*, guint, const GValue*, gpointer data) | |
55 | { | |
56 | wxApp* app = wxTheApp; | |
57 | if (app != NULL) | |
58 | app->WakeUpIdle(); | |
59 | gulong* hook_id = (gulong*)data; | |
60 | // record that hook is not installed | |
61 | *hook_id = 0; | |
62 | // remove hook | |
63 | return false; | |
64 | } | |
65 | } | |
66 | ||
67 | // Add signal emission hooks, to re-install idle handler when needed. | |
68 | static void wx_add_idle_hooks() | |
69 | { | |
70 | // "event" hook | |
71 | { | |
72 | static gulong hook_id = 0; | |
73 | if (hook_id == 0) | |
74 | { | |
75 | static guint sig_id = 0; | |
76 | if (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); | |
80 | } | |
81 | } | |
82 | // "size_allocate" hook | |
83 | // Needed to match the behavior of the old idle system, | |
84 | // but probably not necessary. | |
85 | { | |
86 | static gulong hook_id = 0; | |
87 | if (hook_id == 0) | |
88 | { | |
89 | static guint sig_id = 0; | |
90 | if (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); | |
94 | } | |
95 | } | |
96 | } | |
97 | ||
98 | extern "C" { | |
99 | static gboolean wxapp_idle_callback(gpointer) | |
100 | { | |
101 | return wxTheApp->DoIdle(); | |
102 | } | |
103 | } | |
104 | ||
105 | bool wxApp::DoIdle() | |
106 | { | |
107 | guint id_save; | |
108 | { | |
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. | |
112 | #if wxUSE_THREADS | |
113 | wxMutexLocker lock(*m_idleMutex); | |
114 | #endif | |
115 | id_save = m_idleSourceId; | |
116 | m_idleSourceId = 0; | |
117 | wx_add_idle_hooks(); | |
118 | ||
119 | #if wxDEBUG_LEVEL | |
120 | // don't generate the idle events while the assert modal dialog is shown, | |
121 | // this matches the behavior of wxMSW | |
122 | if (m_isInAssert) | |
123 | return false; | |
124 | #endif | |
125 | } | |
126 | ||
127 | gdk_threads_enter(); | |
128 | bool needMore; | |
129 | do { | |
130 | needMore = ProcessIdle(); | |
131 | } while (needMore && gtk_events_pending() == 0); | |
132 | gdk_threads_leave(); | |
133 | ||
134 | #if wxUSE_THREADS | |
135 | wxMutexLocker lock(*m_idleMutex); | |
136 | #endif | |
137 | // if a new idle source was added during ProcessIdle | |
138 | if (m_idleSourceId != 0) | |
139 | { | |
140 | // remove it | |
141 | g_source_remove(m_idleSourceId); | |
142 | m_idleSourceId = 0; | |
143 | } | |
144 | ||
145 | // Pending events can be added asynchronously, | |
146 | // need to keep idle source if any have appeared | |
147 | needMore = needMore || HasPendingEvents(); | |
148 | ||
149 | // if more idle processing requested | |
150 | if (needMore) | |
151 | { | |
152 | // keep this source installed | |
153 | m_idleSourceId = id_save; | |
154 | return true; | |
155 | } | |
156 | // add hooks and remove this source | |
157 | wx_add_idle_hooks(); | |
158 | return false; | |
159 | } | |
160 | ||
161 | //----------------------------------------------------------------------------- | |
162 | // Access to the root window global | |
163 | //----------------------------------------------------------------------------- | |
164 | ||
165 | GtkWidget* wxGetRootWindow() | |
166 | { | |
167 | static GtkWidget *s_RootWindow = NULL; | |
168 | ||
169 | if (s_RootWindow == NULL) | |
170 | { | |
171 | s_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL ); | |
172 | gtk_widget_realize( s_RootWindow ); | |
173 | } | |
174 | return s_RootWindow; | |
175 | } | |
176 | ||
177 | //----------------------------------------------------------------------------- | |
178 | // wxApp | |
179 | //----------------------------------------------------------------------------- | |
180 | ||
181 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) | |
182 | ||
183 | wxApp::wxApp() | |
184 | { | |
185 | m_isInAssert = false; | |
186 | ||
187 | #if wxUSE_THREADS | |
188 | m_idleMutex = NULL; | |
189 | #endif | |
190 | m_idleSourceId = 0; | |
191 | } | |
192 | ||
193 | wxApp::~wxApp() | |
194 | { | |
195 | } | |
196 | ||
197 | bool wxApp::SetNativeTheme(const wxString& theme) | |
198 | { | |
199 | wxString path; | |
200 | path = gtk_rc_get_theme_dir(); | |
201 | path += "/"; | |
202 | path += theme.utf8_str(); | |
203 | path += "/gtk-2.0/gtkrc"; | |
204 | ||
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()); | |
209 | else | |
210 | { | |
211 | wxLogWarning("Theme \"%s\" not available.", theme); | |
212 | ||
213 | return false; | |
214 | } | |
215 | ||
216 | gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE); | |
217 | ||
218 | return true; | |
219 | } | |
220 | ||
221 | bool wxApp::OnInitGui() | |
222 | { | |
223 | if ( !wxAppBase::OnInitGui() ) | |
224 | return false; | |
225 | ||
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() ) | |
229 | { | |
230 | GdkVisual* vis = gtk_widget_get_default_visual(); | |
231 | ||
232 | GdkColormap *colormap = gdk_colormap_new( vis, FALSE ); | |
233 | gtk_widget_set_default_colormap( colormap ); | |
234 | } | |
235 | else | |
236 | { | |
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. | |
239 | if (m_useBestVisual) | |
240 | { | |
241 | if (m_forceTrueColour) | |
242 | { | |
243 | GdkVisual* visual = gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR ); | |
244 | if (!visual) | |
245 | { | |
246 | wxLogError(wxT("Unable to initialize TrueColor visual.")); | |
247 | return false; | |
248 | } | |
249 | GdkColormap *colormap = gdk_colormap_new( visual, FALSE ); | |
250 | gtk_widget_set_default_colormap( colormap ); | |
251 | } | |
252 | else | |
253 | { | |
254 | if (gdk_visual_get_best() != gdk_visual_get_system()) | |
255 | { | |
256 | GdkVisual* visual = gdk_visual_get_best(); | |
257 | GdkColormap *colormap = gdk_colormap_new( visual, FALSE ); | |
258 | gtk_widget_set_default_colormap( colormap ); | |
259 | } | |
260 | } | |
261 | } | |
262 | } | |
263 | ||
264 | #if wxUSE_LIBHILDON | |
265 | m_hildonProgram = hildon_program_get_instance(); | |
266 | if ( !m_hildonProgram ) | |
267 | { | |
268 | wxLogError(_("Unable to initialize Hildon program")); | |
269 | return false; | |
270 | } | |
271 | #endif // wxUSE_LIBHILDON | |
272 | ||
273 | return true; | |
274 | } | |
275 | ||
276 | GdkVisual *wxApp::GetGdkVisual() | |
277 | { | |
278 | GdkVisual *visual = NULL; | |
279 | ||
280 | XVisualInfo *xvi = (XVisualInfo *)GetXVisualInfo(); | |
281 | if ( xvi ) | |
282 | visual = gdkx_visual_get( xvi->visualid ); | |
283 | else | |
284 | visual = gdk_drawable_get_visual( wxGetRootWindow()->window ); | |
285 | ||
286 | wxASSERT( visual ); | |
287 | ||
288 | return visual; | |
289 | } | |
290 | ||
291 | // use unusual names for the parameters to avoid conflict with wxApp::arg[cv] | |
292 | bool wxApp::Initialize(int& argc_, wxChar **argv_) | |
293 | { | |
294 | if ( !wxAppBase::Initialize(argc_, argv_) ) | |
295 | return false; | |
296 | ||
297 | #if wxUSE_THREADS | |
298 | if (!g_thread_supported()) | |
299 | { | |
300 | g_thread_init(NULL); | |
301 | gdk_threads_init(); | |
302 | } | |
303 | #endif // wxUSE_THREADS | |
304 | ||
305 | // gtk+ 2.0 supports Unicode through UTF-8 strings | |
306 | wxConvCurrent = &wxConvUTF8; | |
307 | ||
308 | // decide which conversion to use for the file names | |
309 | ||
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) | |
315 | encName.clear(); | |
316 | encName.MakeUpper(); | |
317 | #if wxUSE_INTL | |
318 | if (encName.empty()) | |
319 | { | |
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); | |
327 | } | |
328 | #else | |
329 | if (encName.empty()) | |
330 | encName = _T("UTF-8"); | |
331 | ||
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(); | |
336 | ||
337 | #endif // wxUSE_INTL | |
338 | static wxConvBrokenFileNames fileconv(encName); | |
339 | wxConvFileName = &fileconv; | |
340 | ||
341 | ||
342 | bool init_result; | |
343 | int i; | |
344 | ||
345 | #if wxUSE_UNICODE | |
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++ ) | |
349 | { | |
350 | argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv_[i])); | |
351 | } | |
352 | ||
353 | argvGTK[argc_] = NULL; | |
354 | ||
355 | int argcGTK = argc_; | |
356 | ||
357 | #ifdef __WXGPE__ | |
358 | init_result = true; // is there a _check() version of this? | |
359 | gpe_application_init( &argcGTK, &argvGTK ); | |
360 | #else | |
361 | init_result = gtk_init_check( &argcGTK, &argvGTK ); | |
362 | #endif | |
363 | wxUpdateLocaleIsUtf8(); | |
364 | ||
365 | if ( argcGTK != argc_ ) | |
366 | { | |
367 | // we have to drop the parameters which were consumed by GTK+ | |
368 | for ( i = 0; i < argcGTK; i++ ) | |
369 | { | |
370 | while ( strcmp(wxConvUTF8.cWX2MB(argv_[i]), argvGTK[i]) != 0 ) | |
371 | { | |
372 | memmove(argv_ + i, argv_ + i + 1, (argc_ - i)*sizeof(*argv_)); | |
373 | } | |
374 | } | |
375 | ||
376 | argc_ = argcGTK; | |
377 | argv_[argc_] = NULL; | |
378 | } | |
379 | //else: gtk_init() didn't modify our parameters | |
380 | ||
381 | // free our copy | |
382 | for ( i = 0; i < argcGTK; i++ ) | |
383 | { | |
384 | free(argvGTK[i]); | |
385 | } | |
386 | ||
387 | delete [] argvGTK; | |
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 | |
393 | ||
394 | // update internal arg[cv] as GTK+ may have removed processed options: | |
395 | this->argc = argc_; | |
396 | this->argv = argv_; | |
397 | ||
398 | if ( m_traits ) | |
399 | { | |
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 | |
403 | // exit: | |
404 | wxArrayString opt, desc; | |
405 | m_traits->GetStandardCmdLineOptions(opt, desc); | |
406 | ||
407 | for ( i = 0; i < argc_; i++ ) | |
408 | { | |
409 | // leave just the names of the options with values | |
410 | const wxString str = wxString(argv_[i]).BeforeFirst('='); | |
411 | ||
412 | for ( size_t j = 0; j < opt.size(); j++ ) | |
413 | { | |
414 | // remove the leading spaces from the option string as it does | |
415 | // have them | |
416 | if ( opt[j].Trim(false).BeforeFirst('=') == str ) | |
417 | { | |
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\""), | |
423 | argv_[0]); | |
424 | return false; | |
425 | } | |
426 | } | |
427 | } | |
428 | } | |
429 | ||
430 | if ( !init_result ) | |
431 | { | |
432 | wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?")); | |
433 | return false; | |
434 | } | |
435 | ||
436 | // we can not enter threads before gtk_init is done | |
437 | gdk_threads_enter(); | |
438 | ||
439 | wxSetDetectableAutoRepeat( true ); | |
440 | ||
441 | #if wxUSE_INTL | |
442 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
443 | #endif | |
444 | ||
445 | #if wxUSE_THREADS | |
446 | m_idleMutex = new wxMutex; | |
447 | #endif | |
448 | // make sure GtkWidget type is loaded, idle hooks need it | |
449 | g_type_class_ref(GTK_TYPE_WIDGET); | |
450 | WakeUpIdle(); | |
451 | ||
452 | return true; | |
453 | } | |
454 | ||
455 | void wxApp::CleanUp() | |
456 | { | |
457 | if (m_idleSourceId != 0) | |
458 | g_source_remove(m_idleSourceId); | |
459 | ||
460 | // release reference acquired by Initialize() | |
461 | g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET)); | |
462 | ||
463 | gdk_threads_leave(); | |
464 | ||
465 | wxAppBase::CleanUp(); | |
466 | ||
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 | |
470 | #if wxUSE_THREADS | |
471 | delete m_idleMutex; | |
472 | m_idleMutex = NULL; | |
473 | #endif | |
474 | } | |
475 | ||
476 | void wxApp::WakeUpIdle() | |
477 | { | |
478 | #if wxUSE_THREADS | |
479 | wxMutexLocker lock(*m_idleMutex); | |
480 | #endif | |
481 | if (m_idleSourceId == 0) | |
482 | m_idleSourceId = g_idle_add_full(G_PRIORITY_LOW, wxapp_idle_callback, NULL, NULL); | |
483 | } | |
484 | ||
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() | |
488 | { | |
489 | #if wxUSE_THREADS | |
490 | wxMutexLocker lock(*m_idleMutex); | |
491 | #endif | |
492 | if (m_idleSourceId != 0) | |
493 | { | |
494 | g_source_remove(m_idleSourceId); | |
495 | m_idleSourceId = 0; | |
496 | wx_add_idle_hooks(); | |
497 | } | |
498 | return gtk_events_pending() != 0; | |
499 | } | |
500 | ||
501 | void wxApp::OnAssertFailure(const wxChar *file, | |
502 | int line, | |
503 | const wxChar* func, | |
504 | const wxChar* cond, | |
505 | const wxChar *msg) | |
506 | { | |
507 | // there is no need to do anything if asserts are disabled in this build | |
508 | // anyhow | |
509 | #if wxDEBUG_LEVEL | |
510 | // block wx idle events while assert dialog is showing | |
511 | m_isInAssert = true; | |
512 | ||
513 | wxAppBase::OnAssertFailure(file, line, func, cond, msg); | |
514 | ||
515 | m_isInAssert = false; | |
516 | #else // !wxDEBUG_LEVEL | |
517 | wxUnusedVar(file); | |
518 | wxUnusedVar(line); | |
519 | wxUnusedVar(func); | |
520 | wxUnusedVar(cond); | |
521 | wxUnusedVar(msg); | |
522 | #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL | |
523 | } | |
524 | ||
525 | #if wxUSE_THREADS | |
526 | void wxGUIAppTraits::MutexGuiEnter() | |
527 | { | |
528 | gdk_threads_enter(); | |
529 | } | |
530 | ||
531 | void wxGUIAppTraits::MutexGuiLeave() | |
532 | { | |
533 | gdk_threads_leave(); | |
534 | } | |
535 | #endif // wxUSE_THREADS |