]>
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 | #if wxUSE_LIBHILDON2 | |
37 | #include <hildon/hildon.h> | |
38 | #endif // wxUSE_LIBHILDON2 | |
39 | ||
40 | #include <gdk/gdkx.h> | |
41 | ||
42 | //----------------------------------------------------------------------------- | |
43 | // link GnomeVFS | |
44 | //----------------------------------------------------------------------------- | |
45 | ||
46 | #if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS | |
47 | #include "wx/link.h" | |
48 | wxFORCE_LINK_MODULE(gnome_vfs) | |
49 | #endif | |
50 | ||
51 | //----------------------------------------------------------------------------- | |
52 | // local functions | |
53 | //----------------------------------------------------------------------------- | |
54 | ||
55 | // One-shot signal emission hook, to install idle handler. | |
56 | extern "C" { | |
57 | static gboolean | |
58 | wx_emission_hook(GSignalInvocationHint*, guint, const GValue*, gpointer data) | |
59 | { | |
60 | wxApp* app = wxTheApp; | |
61 | if (app != NULL) | |
62 | app->WakeUpIdle(); | |
63 | bool* hook_installed = (bool*)data; | |
64 | // record that hook is not installed | |
65 | *hook_installed = false; | |
66 | // remove hook | |
67 | return false; | |
68 | } | |
69 | } | |
70 | ||
71 | // Add signal emission hooks, to re-install idle handler when needed. | |
72 | static void wx_add_idle_hooks() | |
73 | { | |
74 | // "event" hook | |
75 | { | |
76 | static bool hook_installed; | |
77 | if (!hook_installed) | |
78 | { | |
79 | static guint sig_id; | |
80 | if (sig_id == 0) | |
81 | sig_id = g_signal_lookup("event", GTK_TYPE_WIDGET); | |
82 | hook_installed = true; | |
83 | g_signal_add_emission_hook( | |
84 | sig_id, 0, wx_emission_hook, &hook_installed, NULL); | |
85 | } | |
86 | } | |
87 | // "size_allocate" hook | |
88 | // Needed to match the behavior of the old idle system, | |
89 | // but probably not necessary. | |
90 | { | |
91 | static bool hook_installed; | |
92 | if (!hook_installed) | |
93 | { | |
94 | static guint sig_id; | |
95 | if (sig_id == 0) | |
96 | sig_id = g_signal_lookup("size_allocate", GTK_TYPE_WIDGET); | |
97 | hook_installed = true; | |
98 | g_signal_add_emission_hook( | |
99 | sig_id, 0, wx_emission_hook, &hook_installed, NULL); | |
100 | } | |
101 | } | |
102 | } | |
103 | ||
104 | extern "C" { | |
105 | static gboolean wxapp_idle_callback(gpointer) | |
106 | { | |
107 | return wxTheApp->DoIdle(); | |
108 | } | |
109 | } | |
110 | ||
111 | bool wxApp::DoIdle() | |
112 | { | |
113 | guint id_save; | |
114 | { | |
115 | // Allow another idle source to be added while this one is busy. | |
116 | // Needed if an idle event handler runs a new event loop, | |
117 | // for example by showing a dialog. | |
118 | #if wxUSE_THREADS | |
119 | wxMutexLocker lock(m_idleMutex); | |
120 | #endif | |
121 | id_save = m_idleSourceId; | |
122 | m_idleSourceId = 0; | |
123 | wx_add_idle_hooks(); | |
124 | ||
125 | #if wxDEBUG_LEVEL | |
126 | // don't generate the idle events while the assert modal dialog is shown, | |
127 | // this matches the behavior of wxMSW | |
128 | if (m_isInAssert) | |
129 | return false; | |
130 | #endif | |
131 | } | |
132 | ||
133 | gdk_threads_enter(); | |
134 | bool needMore; | |
135 | do { | |
136 | ProcessPendingEvents(); | |
137 | ||
138 | needMore = ProcessIdle(); | |
139 | } while (needMore && gtk_events_pending() == 0); | |
140 | gdk_threads_leave(); | |
141 | ||
142 | #if wxUSE_THREADS | |
143 | wxMutexLocker lock(m_idleMutex); | |
144 | #endif | |
145 | // if a new idle source was added during ProcessIdle | |
146 | if (m_idleSourceId != 0) | |
147 | { | |
148 | // remove it | |
149 | g_source_remove(m_idleSourceId); | |
150 | m_idleSourceId = 0; | |
151 | } | |
152 | ||
153 | // Pending events can be added asynchronously, | |
154 | // need to keep idle source if any have appeared | |
155 | if (HasPendingEvents()) | |
156 | needMore = true; | |
157 | ||
158 | // if more idle processing requested | |
159 | if (needMore) | |
160 | { | |
161 | // keep this source installed | |
162 | m_idleSourceId = id_save; | |
163 | return true; | |
164 | } | |
165 | // add hooks and remove this source | |
166 | wx_add_idle_hooks(); | |
167 | return false; | |
168 | } | |
169 | ||
170 | //----------------------------------------------------------------------------- | |
171 | // Access to the root window global | |
172 | //----------------------------------------------------------------------------- | |
173 | ||
174 | GtkWidget* wxGetRootWindow() | |
175 | { | |
176 | static GtkWidget *s_RootWindow = NULL; | |
177 | ||
178 | if (s_RootWindow == NULL) | |
179 | { | |
180 | s_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL ); | |
181 | gtk_widget_realize( s_RootWindow ); | |
182 | } | |
183 | return s_RootWindow; | |
184 | } | |
185 | ||
186 | //----------------------------------------------------------------------------- | |
187 | // wxApp | |
188 | //----------------------------------------------------------------------------- | |
189 | ||
190 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) | |
191 | ||
192 | wxApp::wxApp() | |
193 | { | |
194 | m_isInAssert = false; | |
195 | m_idleSourceId = 0; | |
196 | } | |
197 | ||
198 | wxApp::~wxApp() | |
199 | { | |
200 | } | |
201 | ||
202 | bool wxApp::SetNativeTheme(const wxString& theme) | |
203 | { | |
204 | wxString path; | |
205 | path = gtk_rc_get_theme_dir(); | |
206 | path += "/"; | |
207 | path += theme.utf8_str(); | |
208 | path += "/gtk-2.0/gtkrc"; | |
209 | ||
210 | if ( wxFileExists(path.utf8_str()) ) | |
211 | gtk_rc_add_default_file(path.utf8_str()); | |
212 | else if ( wxFileExists(theme.utf8_str()) ) | |
213 | gtk_rc_add_default_file(theme.utf8_str()); | |
214 | else | |
215 | { | |
216 | wxLogWarning("Theme \"%s\" not available.", theme); | |
217 | ||
218 | return false; | |
219 | } | |
220 | ||
221 | gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE); | |
222 | ||
223 | return true; | |
224 | } | |
225 | ||
226 | bool wxApp::OnInitGui() | |
227 | { | |
228 | if ( !wxAppBase::OnInitGui() ) | |
229 | return false; | |
230 | ||
231 | // if this is a wxGLApp (derived from wxApp), and we've already | |
232 | // chosen a specific visual, then derive the GdkVisual from that | |
233 | if ( GetXVisualInfo() ) | |
234 | { | |
235 | GdkVisual* vis = gtk_widget_get_default_visual(); | |
236 | ||
237 | GdkColormap *colormap = gdk_colormap_new( vis, FALSE ); | |
238 | gtk_widget_set_default_colormap( colormap ); | |
239 | } | |
240 | else | |
241 | { | |
242 | // On some machines, the default visual is just 256 colours, so | |
243 | // we make sure we get the best. This can sometimes be wasteful. | |
244 | if (m_useBestVisual) | |
245 | { | |
246 | if (m_forceTrueColour) | |
247 | { | |
248 | GdkVisual* visual = gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR ); | |
249 | if (!visual) | |
250 | { | |
251 | wxLogError(wxT("Unable to initialize TrueColor visual.")); | |
252 | return false; | |
253 | } | |
254 | GdkColormap *colormap = gdk_colormap_new( visual, FALSE ); | |
255 | gtk_widget_set_default_colormap( colormap ); | |
256 | } | |
257 | else | |
258 | { | |
259 | if (gdk_visual_get_best() != gdk_visual_get_system()) | |
260 | { | |
261 | GdkVisual* visual = gdk_visual_get_best(); | |
262 | GdkColormap *colormap = gdk_colormap_new( visual, FALSE ); | |
263 | gtk_widget_set_default_colormap( colormap ); | |
264 | } | |
265 | } | |
266 | } | |
267 | } | |
268 | ||
269 | #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2 | |
270 | if ( !GetHildonProgram() ) | |
271 | { | |
272 | wxLogError(_("Unable to initialize Hildon program")); | |
273 | return false; | |
274 | } | |
275 | #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2 | |
276 | ||
277 | return true; | |
278 | } | |
279 | ||
280 | // use unusual names for the parameters to avoid conflict with wxApp::arg[cv] | |
281 | bool wxApp::Initialize(int& argc_, wxChar **argv_) | |
282 | { | |
283 | if ( !wxAppBase::Initialize(argc_, argv_) ) | |
284 | return false; | |
285 | ||
286 | #if wxUSE_THREADS | |
287 | if (!g_thread_supported()) | |
288 | { | |
289 | g_thread_init(NULL); | |
290 | gdk_threads_init(); | |
291 | } | |
292 | #endif // wxUSE_THREADS | |
293 | ||
294 | // gtk+ 2.0 supports Unicode through UTF-8 strings | |
295 | wxConvCurrent = &wxConvUTF8; | |
296 | ||
297 | // decide which conversion to use for the file names | |
298 | ||
299 | // (1) this variable exists for the sole purpose of specifying the encoding | |
300 | // of the filenames for GTK+ programs, so use it if it is set | |
301 | wxString encName(wxGetenv(wxT("G_FILENAME_ENCODING"))); | |
302 | encName = encName.BeforeFirst(wxT(',')); | |
303 | if (encName.CmpNoCase(wxT("@locale")) == 0) | |
304 | encName.clear(); | |
305 | encName.MakeUpper(); | |
306 | #if wxUSE_INTL | |
307 | if (encName.empty()) | |
308 | { | |
309 | // (2) if a non default locale is set, assume that the user wants his | |
310 | // filenames in this locale too | |
311 | encName = wxLocale::GetSystemEncodingName().Upper(); | |
312 | // (3) finally use UTF-8 by default | |
313 | if (encName.empty() || encName == wxT("US-ASCII")) | |
314 | encName = wxT("UTF-8"); | |
315 | wxSetEnv(wxT("G_FILENAME_ENCODING"), encName); | |
316 | } | |
317 | #else | |
318 | if (encName.empty()) | |
319 | encName = wxT("UTF-8"); | |
320 | ||
321 | // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported | |
322 | // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "") | |
323 | // from gtk_init_check() as it does by default | |
324 | gtk_disable_setlocale(); | |
325 | ||
326 | #endif // wxUSE_INTL | |
327 | static wxConvBrokenFileNames fileconv(encName); | |
328 | wxConvFileName = &fileconv; | |
329 | ||
330 | ||
331 | bool init_result; | |
332 | int i; | |
333 | ||
334 | #if wxUSE_UNICODE | |
335 | // gtk_init() wants UTF-8, not wchar_t, so convert | |
336 | char **argvGTK = new char *[argc_ + 1]; | |
337 | for ( i = 0; i < argc_; i++ ) | |
338 | { | |
339 | argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv_[i])); | |
340 | } | |
341 | ||
342 | argvGTK[argc_] = NULL; | |
343 | ||
344 | int argcGTK = argc_; | |
345 | ||
346 | #ifdef __WXGPE__ | |
347 | init_result = true; // is there a _check() version of this? | |
348 | gpe_application_init( &argcGTK, &argvGTK ); | |
349 | #else | |
350 | init_result = gtk_init_check( &argcGTK, &argvGTK ); | |
351 | #endif | |
352 | wxUpdateLocaleIsUtf8(); | |
353 | ||
354 | if ( argcGTK != argc_ ) | |
355 | { | |
356 | // we have to drop the parameters which were consumed by GTK+ | |
357 | for ( i = 0; i < argcGTK; i++ ) | |
358 | { | |
359 | while ( strcmp(wxConvUTF8.cWX2MB(argv_[i]), argvGTK[i]) != 0 ) | |
360 | { | |
361 | memmove(argv_ + i, argv_ + i + 1, (argc_ - i)*sizeof(*argv_)); | |
362 | } | |
363 | } | |
364 | ||
365 | argc_ = argcGTK; | |
366 | argv_[argc_] = NULL; | |
367 | } | |
368 | //else: gtk_init() didn't modify our parameters | |
369 | ||
370 | // free our copy | |
371 | for ( i = 0; i < argcGTK; i++ ) | |
372 | { | |
373 | free(argvGTK[i]); | |
374 | } | |
375 | ||
376 | delete [] argvGTK; | |
377 | #else // !wxUSE_UNICODE | |
378 | // gtk_init() shouldn't actually change argv_ itself (just its contents) so | |
379 | // it's ok to pass pointer to it | |
380 | init_result = gtk_init_check( &argc_, &argv_ ); | |
381 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE | |
382 | ||
383 | // update internal arg[cv] as GTK+ may have removed processed options: | |
384 | this->argc = argc_; | |
385 | this->argv = argv_; | |
386 | ||
387 | if ( m_traits ) | |
388 | { | |
389 | // if there are still GTK+ standard options unparsed in the command | |
390 | // line, it means that they were not syntactically correct and GTK+ | |
391 | // already printed a warning on the command line and we should now | |
392 | // exit: | |
393 | wxArrayString opt, desc; | |
394 | m_traits->GetStandardCmdLineOptions(opt, desc); | |
395 | ||
396 | for ( i = 0; i < argc_; i++ ) | |
397 | { | |
398 | // leave just the names of the options with values | |
399 | const wxString str = wxString(argv_[i]).BeforeFirst('='); | |
400 | ||
401 | for ( size_t j = 0; j < opt.size(); j++ ) | |
402 | { | |
403 | // remove the leading spaces from the option string as it does | |
404 | // have them | |
405 | if ( opt[j].Trim(false).BeforeFirst('=') == str ) | |
406 | { | |
407 | // a GTK+ option can be left on the command line only if | |
408 | // there was an error in (or before, in another standard | |
409 | // options) it, so abort, just as we do if incorrect | |
410 | // program option is given | |
411 | wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""), | |
412 | argv_[0]); | |
413 | return false; | |
414 | } | |
415 | } | |
416 | } | |
417 | } | |
418 | ||
419 | if ( !init_result ) | |
420 | { | |
421 | wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?")); | |
422 | return false; | |
423 | } | |
424 | ||
425 | // we can not enter threads before gtk_init is done | |
426 | gdk_threads_enter(); | |
427 | ||
428 | #if wxUSE_INTL | |
429 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
430 | #endif | |
431 | ||
432 | // make sure GtkWidget type is loaded, idle hooks need it | |
433 | g_type_class_ref(GTK_TYPE_WIDGET); | |
434 | WakeUpIdle(); | |
435 | ||
436 | return true; | |
437 | } | |
438 | ||
439 | void wxApp::CleanUp() | |
440 | { | |
441 | if (m_idleSourceId != 0) | |
442 | g_source_remove(m_idleSourceId); | |
443 | ||
444 | // release reference acquired by Initialize() | |
445 | g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET)); | |
446 | ||
447 | gdk_threads_leave(); | |
448 | ||
449 | wxAppBase::CleanUp(); | |
450 | } | |
451 | ||
452 | void wxApp::WakeUpIdle() | |
453 | { | |
454 | #if wxUSE_THREADS | |
455 | wxMutexLocker lock(m_idleMutex); | |
456 | #endif | |
457 | if (m_idleSourceId == 0) | |
458 | m_idleSourceId = g_idle_add_full(G_PRIORITY_LOW, wxapp_idle_callback, NULL, NULL); | |
459 | } | |
460 | ||
461 | // Checking for pending events requires first removing our idle source, | |
462 | // otherwise it will cause the check to always return true. | |
463 | bool wxApp::EventsPending() | |
464 | { | |
465 | #if wxUSE_THREADS | |
466 | wxMutexLocker lock(m_idleMutex); | |
467 | #endif | |
468 | if (m_idleSourceId != 0) | |
469 | { | |
470 | g_source_remove(m_idleSourceId); | |
471 | m_idleSourceId = 0; | |
472 | wx_add_idle_hooks(); | |
473 | } | |
474 | return gtk_events_pending() != 0; | |
475 | } | |
476 | ||
477 | void wxApp::OnAssertFailure(const wxChar *file, | |
478 | int line, | |
479 | const wxChar* func, | |
480 | const wxChar* cond, | |
481 | const wxChar *msg) | |
482 | { | |
483 | // there is no need to do anything if asserts are disabled in this build | |
484 | // anyhow | |
485 | #if wxDEBUG_LEVEL | |
486 | // block wx idle events while assert dialog is showing | |
487 | m_isInAssert = true; | |
488 | ||
489 | wxAppBase::OnAssertFailure(file, line, func, cond, msg); | |
490 | ||
491 | m_isInAssert = false; | |
492 | #else // !wxDEBUG_LEVEL | |
493 | wxUnusedVar(file); | |
494 | wxUnusedVar(line); | |
495 | wxUnusedVar(func); | |
496 | wxUnusedVar(cond); | |
497 | wxUnusedVar(msg); | |
498 | #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL | |
499 | } | |
500 | ||
501 | #if wxUSE_THREADS | |
502 | void wxGUIAppTraits::MutexGuiEnter() | |
503 | { | |
504 | gdk_threads_enter(); | |
505 | } | |
506 | ||
507 | void wxGUIAppTraits::MutexGuiLeave() | |
508 | { | |
509 | gdk_threads_leave(); | |
510 | } | |
511 | #endif // wxUSE_THREADS | |
512 | ||
513 | #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2 | |
514 | // Maemo-specific method: get the main program object | |
515 | HildonProgram *wxApp::GetHildonProgram() | |
516 | { | |
517 | return hildon_program_get_instance(); | |
518 | } | |
519 | ||
520 | #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2 |