Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / src / gtk / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/app.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling, Julian Smart
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11
12 #include "wx/app.h"
13
14 #ifndef WX_PRECOMP
15 #include "wx/intl.h"
16 #include "wx/log.h"
17 #include "wx/utils.h"
18 #include "wx/memory.h"
19 #include "wx/font.h"
20 #endif
21
22 #include "wx/thread.h"
23
24 #ifdef __WXGPE__
25 #include <gpe/init.h>
26 #endif
27
28 #include "wx/apptrait.h"
29 #include "wx/fontmap.h"
30
31 #if wxUSE_LIBHILDON
32 #include <hildon-widgets/hildon-program.h>
33 #endif // wxUSE_LIBHILDON
34
35 #if wxUSE_LIBHILDON2
36 #include <hildon/hildon.h>
37 #endif // wxUSE_LIBHILDON2
38
39 #include <gtk/gtk.h>
40 #include "wx/gtk/private.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 behaviour 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 behaviour 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
146 bool keepSource = false;
147 // if a new idle source has not been added, either as a result of idle
148 // processing above or by another thread calling WakeUpIdle()
149 if (m_idleSourceId == 0)
150 {
151 // if more idle processing was requested or pending events have appeared
152 if (needMore || HasPendingEvents())
153 {
154 // keep this source installed
155 m_idleSourceId = id_save;
156 keepSource = true;
157 }
158 else // add hooks and remove this source
159 wx_add_idle_hooks();
160 }
161 // else remove this source, leave new one installed
162 // we must keep an idle source, otherwise a wakeup could be lost
163
164 return keepSource;
165 }
166
167 //-----------------------------------------------------------------------------
168 // Access to the root window global
169 //-----------------------------------------------------------------------------
170
171 GtkWidget* wxGetRootWindow()
172 {
173 static GtkWidget *s_RootWindow = NULL;
174
175 if (s_RootWindow == NULL)
176 {
177 s_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
178 gtk_widget_realize( s_RootWindow );
179 }
180 return s_RootWindow;
181 }
182
183 //-----------------------------------------------------------------------------
184 // wxApp
185 //-----------------------------------------------------------------------------
186
187 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
188
189 wxApp::wxApp()
190 {
191 m_isInAssert = false;
192 m_idleSourceId = 0;
193 }
194
195 wxApp::~wxApp()
196 {
197 }
198
199 bool wxApp::SetNativeTheme(const wxString& theme)
200 {
201 #ifdef __WXGTK3__
202 wxUnusedVar(theme);
203 return false;
204 #else
205 wxString path;
206 path = gtk_rc_get_theme_dir();
207 path += "/";
208 path += theme.utf8_str();
209 path += "/gtk-2.0/gtkrc";
210
211 if ( wxFileExists(path.utf8_str()) )
212 gtk_rc_add_default_file(path.utf8_str());
213 else if ( wxFileExists(theme.utf8_str()) )
214 gtk_rc_add_default_file(theme.utf8_str());
215 else
216 {
217 wxLogWarning("Theme \"%s\" not available.", theme);
218
219 return false;
220 }
221
222 gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE);
223
224 return true;
225 #endif
226 }
227
228 bool wxApp::OnInitGui()
229 {
230 if ( !wxAppBase::OnInitGui() )
231 return false;
232
233 #ifndef __WXGTK3__
234 // if this is a wxGLApp (derived from wxApp), and we've already
235 // chosen a specific visual, then derive the GdkVisual from that
236 if ( GetXVisualInfo() )
237 {
238 GdkVisual* vis = gtk_widget_get_default_visual();
239
240 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
241 gtk_widget_set_default_colormap( colormap );
242 }
243 else
244 {
245 // On some machines, the default visual is just 256 colours, so
246 // we make sure we get the best. This can sometimes be wasteful.
247 if (m_useBestVisual)
248 {
249 if (m_forceTrueColour)
250 {
251 GdkVisual* visual = gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR );
252 if (!visual)
253 {
254 wxLogError(wxT("Unable to initialize TrueColor visual."));
255 return false;
256 }
257 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
258 gtk_widget_set_default_colormap( colormap );
259 }
260 else
261 {
262 if (gdk_visual_get_best() != gdk_visual_get_system())
263 {
264 GdkVisual* visual = gdk_visual_get_best();
265 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
266 gtk_widget_set_default_colormap( colormap );
267 }
268 }
269 }
270 }
271 #endif
272
273 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
274 if ( !GetHildonProgram() )
275 {
276 wxLogError(_("Unable to initialize Hildon program"));
277 return false;
278 }
279 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2
280
281 return true;
282 }
283
284 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
285 bool wxApp::Initialize(int& argc_, wxChar **argv_)
286 {
287 if ( !wxAppBase::Initialize(argc_, argv_) )
288 return false;
289
290 #if wxUSE_THREADS
291 if (!g_thread_supported())
292 {
293 g_thread_init(NULL);
294 gdk_threads_init();
295 }
296 #endif // wxUSE_THREADS
297
298 // gtk+ 2.0 supports Unicode through UTF-8 strings
299 wxConvCurrent = &wxConvUTF8;
300
301 #ifdef __UNIX__
302 // decide which conversion to use for the file names
303
304 // (1) this variable exists for the sole purpose of specifying the encoding
305 // of the filenames for GTK+ programs, so use it if it is set
306 wxString encName(wxGetenv(wxT("G_FILENAME_ENCODING")));
307 encName = encName.BeforeFirst(wxT(','));
308 if (encName.CmpNoCase(wxT("@locale")) == 0)
309 encName.clear();
310 encName.MakeUpper();
311 if (encName.empty())
312 {
313 #if wxUSE_INTL
314 // (2) if a non default locale is set, assume that the user wants his
315 // filenames in this locale too
316 encName = wxLocale::GetSystemEncodingName().Upper();
317
318 // But don't consider ASCII in this case.
319 if ( !encName.empty() )
320 {
321 #if wxUSE_FONTMAP
322 wxFontEncoding enc = wxFontMapperBase::GetEncodingFromName(encName);
323 if ( enc == wxFONTENCODING_DEFAULT )
324 #else // !wxUSE_FONTMAP
325 if ( encName == wxT("US-ASCII") )
326 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
327 {
328 // This means US-ASCII when returned from GetEncodingFromName().
329 encName.clear();
330 }
331 }
332 #endif // wxUSE_INTL
333
334 // (3) finally use UTF-8 by default
335 if ( encName.empty() )
336 encName = wxT("UTF-8");
337 wxSetEnv(wxT("G_FILENAME_ENCODING"), encName);
338 }
339
340 static wxConvBrokenFileNames fileconv(encName);
341 wxConvFileName = &fileconv;
342 #endif // __UNIX__
343
344
345 bool init_result;
346 int i;
347
348 #if wxUSE_UNICODE
349 // gtk_init() wants UTF-8, not wchar_t, so convert
350 char **argvGTK = new char *[argc_ + 1];
351 for ( i = 0; i < argc_; i++ )
352 {
353 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv_[i]));
354 }
355
356 argvGTK[argc_] = NULL;
357
358 int argcGTK = argc_;
359
360 // Prevent gtk_init_check() from changing the locale automatically for
361 // consistency with the other ports that don't do it. If necessary,
362 // wxApp::SetCLocale() may be explicitly called.
363 gtk_disable_setlocale();
364
365 #ifdef __WXGPE__
366 init_result = true; // is there a _check() version of this?
367 gpe_application_init( &argcGTK, &argvGTK );
368 #else
369 init_result = gtk_init_check( &argcGTK, &argvGTK ) != 0;
370 #endif
371
372 if ( argcGTK != argc_ )
373 {
374 // we have to drop the parameters which were consumed by GTK+
375 for ( i = 0; i < argcGTK; i++ )
376 {
377 while ( strcmp(wxConvUTF8.cWX2MB(argv_[i]), argvGTK[i]) != 0 )
378 {
379 memmove(argv_ + i, argv_ + i + 1, (argc_ - i)*sizeof(*argv_));
380 }
381 }
382
383 argc_ = argcGTK;
384 argv_[argc_] = NULL;
385 }
386 //else: gtk_init() didn't modify our parameters
387
388 // free our copy
389 for ( i = 0; i < argcGTK; i++ )
390 {
391 free(argvGTK[i]);
392 }
393
394 delete [] argvGTK;
395 #else // !wxUSE_UNICODE
396 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
397 // it's ok to pass pointer to it
398 init_result = gtk_init_check( &argc_, &argv_ );
399 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
400
401 // update internal arg[cv] as GTK+ may have removed processed options:
402 this->argc = argc_;
403 this->argv = argv_;
404
405 if ( m_traits )
406 {
407 // if there are still GTK+ standard options unparsed in the command
408 // line, it means that they were not syntactically correct and GTK+
409 // already printed a warning on the command line and we should now
410 // exit:
411 wxArrayString opt, desc;
412 m_traits->GetStandardCmdLineOptions(opt, desc);
413
414 for ( i = 0; i < argc_; i++ )
415 {
416 // leave just the names of the options with values
417 const wxString str = wxString(argv_[i]).BeforeFirst('=');
418
419 for ( size_t j = 0; j < opt.size(); j++ )
420 {
421 // remove the leading spaces from the option string as it does
422 // have them
423 if ( opt[j].Trim(false).BeforeFirst('=') == str )
424 {
425 // a GTK+ option can be left on the command line only if
426 // there was an error in (or before, in another standard
427 // options) it, so abort, just as we do if incorrect
428 // program option is given
429 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
430 argv_[0]);
431 return false;
432 }
433 }
434 }
435 }
436
437 if ( !init_result )
438 {
439 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
440 return false;
441 }
442
443 // we cannot enter threads before gtk_init is done
444 gdk_threads_enter();
445
446 #if wxUSE_INTL
447 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
448 #endif
449
450 // make sure GtkWidget type is loaded, idle hooks need it
451 g_type_class_ref(GTK_TYPE_WIDGET);
452 WakeUpIdle();
453
454 return true;
455 }
456
457 void wxApp::CleanUp()
458 {
459 if (m_idleSourceId != 0)
460 g_source_remove(m_idleSourceId);
461
462 // release reference acquired by Initialize()
463 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET));
464
465 gdk_threads_leave();
466
467 wxAppBase::CleanUp();
468 }
469
470 void wxApp::WakeUpIdle()
471 {
472 #if wxUSE_THREADS
473 wxMutexLocker lock(m_idleMutex);
474 #endif
475 if (m_idleSourceId == 0)
476 m_idleSourceId = g_idle_add_full(G_PRIORITY_LOW, wxapp_idle_callback, NULL, NULL);
477 }
478
479 // Checking for pending events requires first removing our idle source,
480 // otherwise it will cause the check to always return true.
481 bool wxApp::EventsPending()
482 {
483 #if wxUSE_THREADS
484 wxMutexLocker lock(m_idleMutex);
485 #endif
486 if (m_idleSourceId != 0)
487 {
488 g_source_remove(m_idleSourceId);
489 m_idleSourceId = 0;
490 wx_add_idle_hooks();
491 }
492 return gtk_events_pending() != 0;
493 }
494
495 void wxApp::OnAssertFailure(const wxChar *file,
496 int line,
497 const wxChar* func,
498 const wxChar* cond,
499 const wxChar *msg)
500 {
501 // there is no need to do anything if asserts are disabled in this build
502 // anyhow
503 #if wxDEBUG_LEVEL
504 // block wx idle events while assert dialog is showing
505 m_isInAssert = true;
506
507 wxAppBase::OnAssertFailure(file, line, func, cond, msg);
508
509 m_isInAssert = false;
510 #else // !wxDEBUG_LEVEL
511 wxUnusedVar(file);
512 wxUnusedVar(line);
513 wxUnusedVar(func);
514 wxUnusedVar(cond);
515 wxUnusedVar(msg);
516 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
517 }
518
519 #if wxUSE_THREADS
520 void wxGUIAppTraits::MutexGuiEnter()
521 {
522 gdk_threads_enter();
523 }
524
525 void wxGUIAppTraits::MutexGuiLeave()
526 {
527 gdk_threads_leave();
528 }
529 #endif // wxUSE_THREADS
530
531 /* static */
532 bool wxApp::GTKIsUsingGlobalMenu()
533 {
534 static int s_isUsingGlobalMenu = -1;
535 if ( s_isUsingGlobalMenu == -1 )
536 {
537 // Currently we just check for this environment variable because this
538 // is how support for the global menu is implemented under Ubuntu.
539 //
540 // If we ever get false positives, we could also check for
541 // XDG_CURRENT_DESKTOP env var being set to "Unity".
542 wxString proxy;
543 s_isUsingGlobalMenu = wxGetEnv("UBUNTU_MENUPROXY", &proxy) &&
544 !proxy.empty() && proxy != "0";
545 }
546
547 return s_isUsingGlobalMenu == 1;
548 }
549
550 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
551 // Maemo-specific method: get the main program object
552 HildonProgram *wxApp::GetHildonProgram()
553 {
554 return hildon_program_get_instance();
555 }
556
557 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2