]>
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 | // global data | |
49 | //----------------------------------------------------------------------------- | |
50 | ||
51 | static GtkWidget *gs_RootWindow = (GtkWidget*) NULL; | |
52 | ||
53 | //----------------------------------------------------------------------------- | |
54 | // wxYield | |
55 | //----------------------------------------------------------------------------- | |
56 | ||
57 | // not static because used by textctrl.cpp | |
58 | // | |
59 | // MT-FIXME | |
60 | bool wxIsInsideYield = false; | |
61 | ||
62 | bool wxApp::Yield(bool onlyIfNeeded) | |
63 | { | |
64 | if ( wxIsInsideYield ) | |
65 | { | |
66 | if ( !onlyIfNeeded ) | |
67 | { | |
68 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
69 | } | |
70 | ||
71 | return false; | |
72 | } | |
73 | ||
74 | #if wxUSE_THREADS | |
75 | if ( !wxThread::IsMain() ) | |
76 | { | |
77 | // can't call gtk_main_iteration() from other threads like this | |
78 | return true; | |
79 | } | |
80 | #endif // wxUSE_THREADS | |
81 | ||
82 | wxIsInsideYield = true; | |
83 | ||
84 | #if wxUSE_LOG | |
85 | // disable log flushing from here because a call to wxYield() shouldn't | |
86 | // normally result in message boxes popping up &c | |
87 | wxLog::Suspend(); | |
88 | #endif | |
89 | ||
90 | while (EventsPending()) | |
91 | gtk_main_iteration(); | |
92 | ||
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(). | |
99 | ProcessIdle(); | |
100 | ||
101 | #if wxUSE_LOG | |
102 | // let the logs be flashed again | |
103 | wxLog::Resume(); | |
104 | #endif | |
105 | ||
106 | wxIsInsideYield = false; | |
107 | ||
108 | return true; | |
109 | } | |
110 | ||
111 | //----------------------------------------------------------------------------- | |
112 | // local functions | |
113 | //----------------------------------------------------------------------------- | |
114 | ||
115 | // One-shot signal emission hook, to install idle handler. | |
116 | extern "C" { | |
117 | static gboolean | |
118 | wx_emission_hook(GSignalInvocationHint*, guint, const GValue*, gpointer data) | |
119 | { | |
120 | wxApp* app = wxTheApp; | |
121 | if (app != NULL) | |
122 | app->WakeUpIdle(); | |
123 | gulong* hook_id = (gulong*)data; | |
124 | // record that hook is not installed | |
125 | *hook_id = 0; | |
126 | // remove hook | |
127 | return false; | |
128 | } | |
129 | } | |
130 | ||
131 | // Add signal emission hooks, to re-install idle handler when needed. | |
132 | static void wx_add_idle_hooks() | |
133 | { | |
134 | // "event" hook | |
135 | { | |
136 | static gulong hook_id = 0; | |
137 | if (hook_id == 0) | |
138 | { | |
139 | static guint sig_id = 0; | |
140 | if (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); | |
144 | } | |
145 | } | |
146 | // "size_allocate" hook | |
147 | // Needed to match the behavior of the old idle system, | |
148 | // but probably not necessary. | |
149 | { | |
150 | static gulong hook_id = 0; | |
151 | if (hook_id == 0) | |
152 | { | |
153 | static guint sig_id = 0; | |
154 | if (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); | |
158 | } | |
159 | } | |
160 | } | |
161 | ||
162 | extern "C" { | |
163 | static gboolean wxapp_idle_callback(gpointer) | |
164 | { | |
165 | return wxTheApp->DoIdle(); | |
166 | } | |
167 | } | |
168 | ||
169 | bool wxApp::DoIdle() | |
170 | { | |
171 | guint id_save; | |
172 | { | |
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. | |
176 | #if wxUSE_THREADS | |
177 | wxMutexLocker lock(*m_idleMutex); | |
178 | #endif | |
179 | id_save = m_idleSourceId; | |
180 | m_idleSourceId = 0; | |
181 | wx_add_idle_hooks(); | |
182 | #ifdef __WXDEBUG__ | |
183 | // don't generate the idle events while the assert modal dialog is shown, | |
184 | // this matches the behavior of wxMSW | |
185 | if (m_isInAssert) | |
186 | return false; | |
187 | #endif | |
188 | } | |
189 | ||
190 | gdk_threads_enter(); | |
191 | bool needMore; | |
192 | do { | |
193 | needMore = ProcessIdle(); | |
194 | } while (needMore && gtk_events_pending() == 0); | |
195 | gdk_threads_leave(); | |
196 | ||
197 | #if wxUSE_THREADS | |
198 | wxMutexLocker lock(*m_idleMutex); | |
199 | #endif | |
200 | // if a new idle source was added during ProcessIdle | |
201 | if (m_idleSourceId != 0) | |
202 | { | |
203 | // remove it | |
204 | g_source_remove(m_idleSourceId); | |
205 | m_idleSourceId = 0; | |
206 | } | |
207 | // if more idle processing requested | |
208 | if (needMore) | |
209 | { | |
210 | // keep this source installed | |
211 | m_idleSourceId = id_save; | |
212 | return true; | |
213 | } | |
214 | // add hooks and remove this source | |
215 | wx_add_idle_hooks(); | |
216 | return false; | |
217 | } | |
218 | ||
219 | //----------------------------------------------------------------------------- | |
220 | // Access to the root window global | |
221 | //----------------------------------------------------------------------------- | |
222 | ||
223 | GtkWidget* wxGetRootWindow() | |
224 | { | |
225 | if (gs_RootWindow == NULL) | |
226 | { | |
227 | gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL ); | |
228 | gtk_widget_realize( gs_RootWindow ); | |
229 | } | |
230 | return gs_RootWindow; | |
231 | } | |
232 | ||
233 | //----------------------------------------------------------------------------- | |
234 | // wxApp | |
235 | //----------------------------------------------------------------------------- | |
236 | ||
237 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) | |
238 | ||
239 | wxApp::wxApp() | |
240 | { | |
241 | #ifdef __WXDEBUG__ | |
242 | m_isInAssert = false; | |
243 | #endif // __WXDEBUG__ | |
244 | #if wxUSE_THREADS | |
245 | m_idleMutex = NULL; | |
246 | #endif | |
247 | m_idleSourceId = 0; | |
248 | } | |
249 | ||
250 | wxApp::~wxApp() | |
251 | { | |
252 | } | |
253 | ||
254 | bool wxApp::SetNativeTheme(const wxString& theme) | |
255 | { | |
256 | wxString path; | |
257 | path = gtk_rc_get_theme_dir(); | |
258 | path += "/"; | |
259 | path += theme.utf8_str(); | |
260 | path += "/gtk-2.0/gtkrc"; | |
261 | ||
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()); | |
266 | else | |
267 | { | |
268 | wxLogWarning("Theme \"%s\" not available.", theme); | |
269 | ||
270 | return false; | |
271 | } | |
272 | ||
273 | gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE); | |
274 | ||
275 | return true; | |
276 | } | |
277 | ||
278 | bool wxApp::OnInitGui() | |
279 | { | |
280 | if ( !wxAppBase::OnInitGui() ) | |
281 | return false; | |
282 | ||
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() ) | |
286 | { | |
287 | GdkVisual* vis = gtk_widget_get_default_visual(); | |
288 | ||
289 | GdkColormap *colormap = gdk_colormap_new( vis, FALSE ); | |
290 | gtk_widget_set_default_colormap( colormap ); | |
291 | } | |
292 | else | |
293 | { | |
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. | |
296 | if (m_useBestVisual) | |
297 | { | |
298 | if (m_forceTrueColour) | |
299 | { | |
300 | GdkVisual* visual = gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR ); | |
301 | if (!visual) | |
302 | { | |
303 | wxLogError(wxT("Unable to initialize TrueColor visual.")); | |
304 | return false; | |
305 | } | |
306 | GdkColormap *colormap = gdk_colormap_new( visual, FALSE ); | |
307 | gtk_widget_set_default_colormap( colormap ); | |
308 | } | |
309 | else | |
310 | { | |
311 | if (gdk_visual_get_best() != gdk_visual_get_system()) | |
312 | { | |
313 | GdkVisual* visual = gdk_visual_get_best(); | |
314 | GdkColormap *colormap = gdk_colormap_new( visual, FALSE ); | |
315 | gtk_widget_set_default_colormap( colormap ); | |
316 | } | |
317 | } | |
318 | } | |
319 | } | |
320 | ||
321 | #if wxUSE_LIBHILDON | |
322 | m_hildonProgram = hildon_program_get_instance(); | |
323 | if ( !m_hildonProgram ) | |
324 | { | |
325 | wxLogError(_("Unable to initialize Hildon program")); | |
326 | return false; | |
327 | } | |
328 | #endif // wxUSE_LIBHILDON | |
329 | ||
330 | return true; | |
331 | } | |
332 | ||
333 | GdkVisual *wxApp::GetGdkVisual() | |
334 | { | |
335 | GdkVisual *visual = NULL; | |
336 | ||
337 | XVisualInfo *xvi = (XVisualInfo *)GetXVisualInfo(); | |
338 | if ( xvi ) | |
339 | visual = gdkx_visual_get( xvi->visualid ); | |
340 | else | |
341 | visual = gdk_drawable_get_visual( wxGetRootWindow()->window ); | |
342 | ||
343 | wxASSERT( visual ); | |
344 | ||
345 | return visual; | |
346 | } | |
347 | ||
348 | // use unusual names for the parameters to avoid conflict with wxApp::arg[cv] | |
349 | bool wxApp::Initialize(int& argc_, wxChar **argv_) | |
350 | { | |
351 | if ( !wxAppBase::Initialize(argc_, argv_) ) | |
352 | return false; | |
353 | ||
354 | #if wxUSE_THREADS | |
355 | if (!g_thread_supported()) | |
356 | { | |
357 | g_thread_init(NULL); | |
358 | gdk_threads_init(); | |
359 | } | |
360 | #endif // wxUSE_THREADS | |
361 | ||
362 | // We should have the wxUSE_WCHAR_T test on the _outside_ | |
363 | #if wxUSE_WCHAR_T | |
364 | // gtk+ 2.0 supports Unicode through UTF-8 strings | |
365 | wxConvCurrent = &wxConvUTF8; | |
366 | #else // !wxUSE_WCHAR_T | |
367 | if (!wxOKlibc()) | |
368 | wxConvCurrent = (wxMBConv*) NULL; | |
369 | #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T | |
370 | ||
371 | // decide which conversion to use for the file names | |
372 | ||
373 | // (1) this variable exists for the sole purpose of specifying the encoding | |
374 | // of the filenames for GTK+ programs, so use it if it is set | |
375 | wxString encName(wxGetenv(_T("G_FILENAME_ENCODING"))); | |
376 | encName = encName.BeforeFirst(_T(',')); | |
377 | if (encName.CmpNoCase(_T("@locale")) == 0) | |
378 | encName.clear(); | |
379 | encName.MakeUpper(); | |
380 | #if wxUSE_INTL | |
381 | if (encName.empty()) | |
382 | { | |
383 | // (2) if a non default locale is set, assume that the user wants his | |
384 | // filenames in this locale too | |
385 | encName = wxLocale::GetSystemEncodingName().Upper(); | |
386 | // (3) finally use UTF-8 by default | |
387 | if (encName.empty() || encName == _T("US-ASCII")) | |
388 | encName = _T("UTF-8"); | |
389 | wxSetEnv(_T("G_FILENAME_ENCODING"), encName); | |
390 | } | |
391 | #else | |
392 | if (encName.empty()) | |
393 | encName = _T("UTF-8"); | |
394 | ||
395 | // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported | |
396 | // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "") | |
397 | // from gtk_init_check() as it does by default | |
398 | gtk_disable_setlocale(); | |
399 | ||
400 | #endif // wxUSE_INTL | |
401 | static wxConvBrokenFileNames fileconv(encName); | |
402 | wxConvFileName = &fileconv; | |
403 | ||
404 | ||
405 | bool init_result; | |
406 | int i; | |
407 | ||
408 | #if wxUSE_UNICODE | |
409 | // gtk_init() wants UTF-8, not wchar_t, so convert | |
410 | char **argvGTK = new char *[argc_ + 1]; | |
411 | for ( i = 0; i < argc_; i++ ) | |
412 | { | |
413 | argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv_[i])); | |
414 | } | |
415 | ||
416 | argvGTK[argc_] = NULL; | |
417 | ||
418 | int argcGTK = argc_; | |
419 | ||
420 | #ifdef __WXGPE__ | |
421 | init_result = true; // is there a _check() version of this? | |
422 | gpe_application_init( &argcGTK, &argvGTK ); | |
423 | #else | |
424 | init_result = gtk_init_check( &argcGTK, &argvGTK ); | |
425 | #endif | |
426 | wxUpdateLocaleIsUtf8(); | |
427 | ||
428 | if ( argcGTK != argc_ ) | |
429 | { | |
430 | // we have to drop the parameters which were consumed by GTK+ | |
431 | for ( i = 0; i < argcGTK; i++ ) | |
432 | { | |
433 | while ( strcmp(wxConvUTF8.cWX2MB(argv_[i]), argvGTK[i]) != 0 ) | |
434 | { | |
435 | memmove(argv_ + i, argv_ + i + 1, (argc_ - i)*sizeof(*argv_)); | |
436 | } | |
437 | } | |
438 | ||
439 | argc_ = argcGTK; | |
440 | } | |
441 | //else: gtk_init() didn't modify our parameters | |
442 | ||
443 | // free our copy | |
444 | for ( i = 0; i < argcGTK; i++ ) | |
445 | { | |
446 | free(argvGTK[i]); | |
447 | } | |
448 | ||
449 | delete [] argvGTK; | |
450 | #else // !wxUSE_UNICODE | |
451 | // gtk_init() shouldn't actually change argv_ itself (just its contents) so | |
452 | // it's ok to pass pointer to it | |
453 | init_result = gtk_init_check( &argc_, &argv_ ); | |
454 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE | |
455 | ||
456 | // update internal arg[cv] as GTK+ may have removed processed options: | |
457 | this->argc = argc_; | |
458 | this->argv = argv_; | |
459 | ||
460 | if ( m_traits ) | |
461 | { | |
462 | // if there are still GTK+ standard options unparsed in the command | |
463 | // line, it means that they were not syntactically correct and GTK+ | |
464 | // already printed a warning on the command line and we should now | |
465 | // exit: | |
466 | wxArrayString opt, desc; | |
467 | m_traits->GetStandardCmdLineOptions(opt, desc); | |
468 | ||
469 | for ( i = 0; i < argc_; i++ ) | |
470 | { | |
471 | // leave just the names of the options with values | |
472 | const wxString str = wxString(argv_[i]).BeforeFirst('='); | |
473 | ||
474 | for ( size_t j = 0; j < opt.size(); j++ ) | |
475 | { | |
476 | // remove the leading spaces from the option string as it does | |
477 | // have them | |
478 | if ( opt[j].Trim(false).BeforeFirst('=') == str ) | |
479 | { | |
480 | // a GTK+ option can be left on the command line only if | |
481 | // there was an error in (or before, in another standard | |
482 | // options) it, so abort, just as we do if incorrect | |
483 | // program option is given | |
484 | wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""), | |
485 | argv_[0]); | |
486 | return false; | |
487 | } | |
488 | } | |
489 | } | |
490 | } | |
491 | ||
492 | if ( !init_result ) | |
493 | { | |
494 | wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?")); | |
495 | return false; | |
496 | } | |
497 | ||
498 | // we can not enter threads before gtk_init is done | |
499 | gdk_threads_enter(); | |
500 | ||
501 | wxSetDetectableAutoRepeat( true ); | |
502 | ||
503 | #if wxUSE_INTL | |
504 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
505 | #endif | |
506 | ||
507 | #if wxUSE_THREADS | |
508 | m_idleMutex = new wxMutex; | |
509 | #endif | |
510 | // make sure GtkWidget type is loaded, idle hooks need it | |
511 | g_type_class_ref(GTK_TYPE_WIDGET); | |
512 | WakeUpIdle(); | |
513 | ||
514 | return true; | |
515 | } | |
516 | ||
517 | void wxApp::CleanUp() | |
518 | { | |
519 | if (m_idleSourceId != 0) | |
520 | g_source_remove(m_idleSourceId); | |
521 | ||
522 | // release reference acquired by Initialize() | |
523 | g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET)); | |
524 | ||
525 | gdk_threads_leave(); | |
526 | ||
527 | wxAppBase::CleanUp(); | |
528 | ||
529 | // delete this mutex as late as possible as it's used from WakeUpIdle(), in | |
530 | // particular do it after calling the base class CleanUp() which can result | |
531 | // in it being called | |
532 | #if wxUSE_THREADS | |
533 | delete m_idleMutex; | |
534 | m_idleMutex = NULL; | |
535 | #endif | |
536 | } | |
537 | ||
538 | void wxApp::WakeUpIdle() | |
539 | { | |
540 | #if wxUSE_THREADS | |
541 | wxMutexLocker lock(*m_idleMutex); | |
542 | #endif | |
543 | if (m_idleSourceId == 0) | |
544 | m_idleSourceId = g_idle_add_full(G_PRIORITY_LOW, wxapp_idle_callback, NULL, NULL); | |
545 | } | |
546 | ||
547 | // Checking for pending events requires first removing our idle source, | |
548 | // otherwise it will cause the check to always return true. | |
549 | bool wxApp::EventsPending() | |
550 | { | |
551 | #if wxUSE_THREADS | |
552 | wxMutexLocker lock(*m_idleMutex); | |
553 | #endif | |
554 | if (m_idleSourceId != 0) | |
555 | { | |
556 | g_source_remove(m_idleSourceId); | |
557 | m_idleSourceId = 0; | |
558 | wx_add_idle_hooks(); | |
559 | } | |
560 | return gtk_events_pending() != 0; | |
561 | } | |
562 | ||
563 | #ifdef __WXDEBUG__ | |
564 | ||
565 | void wxApp::OnAssertFailure(const wxChar *file, | |
566 | int line, | |
567 | const wxChar* func, | |
568 | const wxChar* cond, | |
569 | const wxChar *msg) | |
570 | { | |
571 | ||
572 | // block wx idle events while assert dialog is showing | |
573 | m_isInAssert = true; | |
574 | ||
575 | wxAppBase::OnAssertFailure(file, line, func, cond, msg); | |
576 | ||
577 | m_isInAssert = false; | |
578 | } | |
579 | ||
580 | #endif // __WXDEBUG__ | |
581 | ||
582 | #if wxUSE_THREADS | |
583 | void wxGUIAppTraits::MutexGuiEnter() | |
584 | { | |
585 | gdk_threads_enter(); | |
586 | } | |
587 | ||
588 | void wxGUIAppTraits::MutexGuiLeave() | |
589 | { | |
590 | gdk_threads_leave(); | |
591 | } | |
592 | #endif // wxUSE_THREADS |