]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/app.cpp
restore previous encoding, changed for error to UTF8
[wxWidgets.git] / src / gtk / app.cpp
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
208 // Pending events can be added asynchronously,
209 // need to keep idle source if any have appeared
210 needMore = needMore || HasPendingEvents();
211
212 // if more idle processing requested
213 if (needMore)
214 {
215 // keep this source installed
216 m_idleSourceId = id_save;
217 return true;
218 }
219 // add hooks and remove this source
220 wx_add_idle_hooks();
221 return false;
222 }
223
224 //-----------------------------------------------------------------------------
225 // Access to the root window global
226 //-----------------------------------------------------------------------------
227
228 GtkWidget* wxGetRootWindow()
229 {
230 if (gs_RootWindow == NULL)
231 {
232 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
233 gtk_widget_realize( gs_RootWindow );
234 }
235 return gs_RootWindow;
236 }
237
238 //-----------------------------------------------------------------------------
239 // wxApp
240 //-----------------------------------------------------------------------------
241
242 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
243
244 wxApp::wxApp()
245 {
246 #ifdef __WXDEBUG__
247 m_isInAssert = false;
248 #endif // __WXDEBUG__
249 #if wxUSE_THREADS
250 m_idleMutex = NULL;
251 #endif
252 m_idleSourceId = 0;
253 }
254
255 wxApp::~wxApp()
256 {
257 }
258
259 bool wxApp::SetNativeTheme(const wxString& theme)
260 {
261 wxString path;
262 path = gtk_rc_get_theme_dir();
263 path += "/";
264 path += theme.utf8_str();
265 path += "/gtk-2.0/gtkrc";
266
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());
271 else
272 {
273 wxLogWarning("Theme \"%s\" not available.", theme);
274
275 return false;
276 }
277
278 gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE);
279
280 return true;
281 }
282
283 bool wxApp::OnInitGui()
284 {
285 if ( !wxAppBase::OnInitGui() )
286 return false;
287
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() )
291 {
292 GdkVisual* vis = gtk_widget_get_default_visual();
293
294 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
295 gtk_widget_set_default_colormap( colormap );
296 }
297 else
298 {
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.
301 if (m_useBestVisual)
302 {
303 if (m_forceTrueColour)
304 {
305 GdkVisual* visual = gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR );
306 if (!visual)
307 {
308 wxLogError(wxT("Unable to initialize TrueColor visual."));
309 return false;
310 }
311 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
312 gtk_widget_set_default_colormap( colormap );
313 }
314 else
315 {
316 if (gdk_visual_get_best() != gdk_visual_get_system())
317 {
318 GdkVisual* visual = gdk_visual_get_best();
319 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
320 gtk_widget_set_default_colormap( colormap );
321 }
322 }
323 }
324 }
325
326 #if wxUSE_LIBHILDON
327 m_hildonProgram = hildon_program_get_instance();
328 if ( !m_hildonProgram )
329 {
330 wxLogError(_("Unable to initialize Hildon program"));
331 return false;
332 }
333 #endif // wxUSE_LIBHILDON
334
335 return true;
336 }
337
338 GdkVisual *wxApp::GetGdkVisual()
339 {
340 GdkVisual *visual = NULL;
341
342 XVisualInfo *xvi = (XVisualInfo *)GetXVisualInfo();
343 if ( xvi )
344 visual = gdkx_visual_get( xvi->visualid );
345 else
346 visual = gdk_drawable_get_visual( wxGetRootWindow()->window );
347
348 wxASSERT( visual );
349
350 return visual;
351 }
352
353 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
354 bool wxApp::Initialize(int& argc_, wxChar **argv_)
355 {
356 if ( !wxAppBase::Initialize(argc_, argv_) )
357 return false;
358
359 #if wxUSE_THREADS
360 if (!g_thread_supported())
361 {
362 g_thread_init(NULL);
363 gdk_threads_init();
364 }
365 #endif // wxUSE_THREADS
366
367 // We should have the wxUSE_WCHAR_T test on the _outside_
368 #if wxUSE_WCHAR_T
369 // gtk+ 2.0 supports Unicode through UTF-8 strings
370 wxConvCurrent = &wxConvUTF8;
371 #else // !wxUSE_WCHAR_T
372 if (!wxOKlibc())
373 wxConvCurrent = (wxMBConv*) NULL;
374 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
375
376 // decide which conversion to use for the file names
377
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)
383 encName.clear();
384 encName.MakeUpper();
385 #if wxUSE_INTL
386 if (encName.empty())
387 {
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);
395 }
396 #else
397 if (encName.empty())
398 encName = _T("UTF-8");
399
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();
404
405 #endif // wxUSE_INTL
406 static wxConvBrokenFileNames fileconv(encName);
407 wxConvFileName = &fileconv;
408
409
410 bool init_result;
411 int i;
412
413 #if wxUSE_UNICODE
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++ )
417 {
418 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv_[i]));
419 }
420
421 argvGTK[argc_] = NULL;
422
423 int argcGTK = argc_;
424
425 #ifdef __WXGPE__
426 init_result = true; // is there a _check() version of this?
427 gpe_application_init( &argcGTK, &argvGTK );
428 #else
429 init_result = gtk_init_check( &argcGTK, &argvGTK );
430 #endif
431 wxUpdateLocaleIsUtf8();
432
433 if ( argcGTK != argc_ )
434 {
435 // we have to drop the parameters which were consumed by GTK+
436 for ( i = 0; i < argcGTK; i++ )
437 {
438 while ( strcmp(wxConvUTF8.cWX2MB(argv_[i]), argvGTK[i]) != 0 )
439 {
440 memmove(argv_ + i, argv_ + i + 1, (argc_ - i)*sizeof(*argv_));
441 }
442 }
443
444 argc_ = argcGTK;
445 argv_[argc_] = NULL;
446 }
447 //else: gtk_init() didn't modify our parameters
448
449 // free our copy
450 for ( i = 0; i < argcGTK; i++ )
451 {
452 free(argvGTK[i]);
453 }
454
455 delete [] argvGTK;
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
461
462 // update internal arg[cv] as GTK+ may have removed processed options:
463 this->argc = argc_;
464 this->argv = argv_;
465
466 if ( m_traits )
467 {
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
471 // exit:
472 wxArrayString opt, desc;
473 m_traits->GetStandardCmdLineOptions(opt, desc);
474
475 for ( i = 0; i < argc_; i++ )
476 {
477 // leave just the names of the options with values
478 const wxString str = wxString(argv_[i]).BeforeFirst('=');
479
480 for ( size_t j = 0; j < opt.size(); j++ )
481 {
482 // remove the leading spaces from the option string as it does
483 // have them
484 if ( opt[j].Trim(false).BeforeFirst('=') == str )
485 {
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\""),
491 argv_[0]);
492 return false;
493 }
494 }
495 }
496 }
497
498 if ( !init_result )
499 {
500 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
501 return false;
502 }
503
504 // we can not enter threads before gtk_init is done
505 gdk_threads_enter();
506
507 wxSetDetectableAutoRepeat( true );
508
509 #if wxUSE_INTL
510 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
511 #endif
512
513 #if wxUSE_THREADS
514 m_idleMutex = new wxMutex;
515 #endif
516 // make sure GtkWidget type is loaded, idle hooks need it
517 g_type_class_ref(GTK_TYPE_WIDGET);
518 WakeUpIdle();
519
520 return true;
521 }
522
523 void wxApp::CleanUp()
524 {
525 if (m_idleSourceId != 0)
526 g_source_remove(m_idleSourceId);
527
528 // release reference acquired by Initialize()
529 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET));
530
531 gdk_threads_leave();
532
533 wxAppBase::CleanUp();
534
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
538 #if wxUSE_THREADS
539 delete m_idleMutex;
540 m_idleMutex = NULL;
541 #endif
542 }
543
544 void wxApp::WakeUpIdle()
545 {
546 #if wxUSE_THREADS
547 wxMutexLocker lock(*m_idleMutex);
548 #endif
549 if (m_idleSourceId == 0)
550 m_idleSourceId = g_idle_add_full(G_PRIORITY_LOW, wxapp_idle_callback, NULL, NULL);
551 }
552
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()
556 {
557 #if wxUSE_THREADS
558 wxMutexLocker lock(*m_idleMutex);
559 #endif
560 if (m_idleSourceId != 0)
561 {
562 g_source_remove(m_idleSourceId);
563 m_idleSourceId = 0;
564 wx_add_idle_hooks();
565 }
566 return gtk_events_pending() != 0;
567 }
568
569 #ifdef __WXDEBUG__
570
571 void wxApp::OnAssertFailure(const wxChar *file,
572 int line,
573 const wxChar* func,
574 const wxChar* cond,
575 const wxChar *msg)
576 {
577
578 // block wx idle events while assert dialog is showing
579 m_isInAssert = true;
580
581 wxAppBase::OnAssertFailure(file, line, func, cond, msg);
582
583 m_isInAssert = false;
584 }
585
586 #endif // __WXDEBUG__
587
588 #if wxUSE_THREADS
589 void wxGUIAppTraits::MutexGuiEnter()
590 {
591 gdk_threads_enter();
592 }
593
594 void wxGUIAppTraits::MutexGuiLeave()
595 {
596 gdk_threads_leave();
597 }
598 #endif // wxUSE_THREADS