Define wxSetDetectableAutoRepeat() for X11-based ports only.
[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 // 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 ProcessPendingEvents();
131
132 needMore = ProcessIdle();
133 } while (needMore && gtk_events_pending() == 0);
134 gdk_threads_leave();
135
136 #if wxUSE_THREADS
137 wxMutexLocker lock(*m_idleMutex);
138 #endif
139 // if a new idle source was added during ProcessIdle
140 if (m_idleSourceId != 0)
141 {
142 // remove it
143 g_source_remove(m_idleSourceId);
144 m_idleSourceId = 0;
145 }
146
147 // Pending events can be added asynchronously,
148 // need to keep idle source if any have appeared
149 needMore = needMore || HasPendingEvents();
150
151 // if more idle processing requested
152 if (needMore)
153 {
154 // keep this source installed
155 m_idleSourceId = id_save;
156 return true;
157 }
158 // add hooks and remove this source
159 wx_add_idle_hooks();
160 return false;
161 }
162
163 //-----------------------------------------------------------------------------
164 // Access to the root window global
165 //-----------------------------------------------------------------------------
166
167 GtkWidget* wxGetRootWindow()
168 {
169 static GtkWidget *s_RootWindow = NULL;
170
171 if (s_RootWindow == NULL)
172 {
173 s_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
174 gtk_widget_realize( s_RootWindow );
175 }
176 return s_RootWindow;
177 }
178
179 //-----------------------------------------------------------------------------
180 // wxApp
181 //-----------------------------------------------------------------------------
182
183 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
184
185 wxApp::wxApp()
186 {
187 m_isInAssert = false;
188
189 #if wxUSE_THREADS
190 m_idleMutex = NULL;
191 #endif
192 m_idleSourceId = 0;
193 }
194
195 wxApp::~wxApp()
196 {
197 }
198
199 bool wxApp::SetNativeTheme(const wxString& theme)
200 {
201 wxString path;
202 path = gtk_rc_get_theme_dir();
203 path += "/";
204 path += theme.utf8_str();
205 path += "/gtk-2.0/gtkrc";
206
207 if ( wxFileExists(path.utf8_str()) )
208 gtk_rc_add_default_file(path.utf8_str());
209 else if ( wxFileExists(theme.utf8_str()) )
210 gtk_rc_add_default_file(theme.utf8_str());
211 else
212 {
213 wxLogWarning("Theme \"%s\" not available.", theme);
214
215 return false;
216 }
217
218 gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE);
219
220 return true;
221 }
222
223 bool wxApp::OnInitGui()
224 {
225 if ( !wxAppBase::OnInitGui() )
226 return false;
227
228 // if this is a wxGLApp (derived from wxApp), and we've already
229 // chosen a specific visual, then derive the GdkVisual from that
230 if ( GetXVisualInfo() )
231 {
232 GdkVisual* vis = gtk_widget_get_default_visual();
233
234 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
235 gtk_widget_set_default_colormap( colormap );
236 }
237 else
238 {
239 // On some machines, the default visual is just 256 colours, so
240 // we make sure we get the best. This can sometimes be wasteful.
241 if (m_useBestVisual)
242 {
243 if (m_forceTrueColour)
244 {
245 GdkVisual* visual = gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR );
246 if (!visual)
247 {
248 wxLogError(wxT("Unable to initialize TrueColor visual."));
249 return false;
250 }
251 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
252 gtk_widget_set_default_colormap( colormap );
253 }
254 else
255 {
256 if (gdk_visual_get_best() != gdk_visual_get_system())
257 {
258 GdkVisual* visual = gdk_visual_get_best();
259 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
260 gtk_widget_set_default_colormap( colormap );
261 }
262 }
263 }
264 }
265
266 #if wxUSE_LIBHILDON
267 m_hildonProgram = hildon_program_get_instance();
268 if ( !m_hildonProgram )
269 {
270 wxLogError(_("Unable to initialize Hildon program"));
271 return false;
272 }
273 #endif // wxUSE_LIBHILDON
274
275 return true;
276 }
277
278 GdkVisual *wxApp::GetGdkVisual()
279 {
280 GdkVisual *visual = NULL;
281
282 XVisualInfo *xvi = (XVisualInfo *)GetXVisualInfo();
283 if ( xvi )
284 visual = gdkx_visual_get( xvi->visualid );
285 else
286 visual = gdk_drawable_get_visual( wxGetRootWindow()->window );
287
288 wxASSERT( visual );
289
290 return visual;
291 }
292
293 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
294 bool wxApp::Initialize(int& argc_, wxChar **argv_)
295 {
296 if ( !wxAppBase::Initialize(argc_, argv_) )
297 return false;
298
299 #if wxUSE_THREADS
300 if (!g_thread_supported())
301 {
302 g_thread_init(NULL);
303 gdk_threads_init();
304 }
305 #endif // wxUSE_THREADS
306
307 // gtk+ 2.0 supports Unicode through UTF-8 strings
308 wxConvCurrent = &wxConvUTF8;
309
310 // decide which conversion to use for the file names
311
312 // (1) this variable exists for the sole purpose of specifying the encoding
313 // of the filenames for GTK+ programs, so use it if it is set
314 wxString encName(wxGetenv(wxT("G_FILENAME_ENCODING")));
315 encName = encName.BeforeFirst(wxT(','));
316 if (encName.CmpNoCase(wxT("@locale")) == 0)
317 encName.clear();
318 encName.MakeUpper();
319 #if wxUSE_INTL
320 if (encName.empty())
321 {
322 // (2) if a non default locale is set, assume that the user wants his
323 // filenames in this locale too
324 encName = wxLocale::GetSystemEncodingName().Upper();
325 // (3) finally use UTF-8 by default
326 if (encName.empty() || encName == wxT("US-ASCII"))
327 encName = wxT("UTF-8");
328 wxSetEnv(wxT("G_FILENAME_ENCODING"), encName);
329 }
330 #else
331 if (encName.empty())
332 encName = wxT("UTF-8");
333
334 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
335 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
336 // from gtk_init_check() as it does by default
337 gtk_disable_setlocale();
338
339 #endif // wxUSE_INTL
340 static wxConvBrokenFileNames fileconv(encName);
341 wxConvFileName = &fileconv;
342
343
344 bool init_result;
345 int i;
346
347 #if wxUSE_UNICODE
348 // gtk_init() wants UTF-8, not wchar_t, so convert
349 char **argvGTK = new char *[argc_ + 1];
350 for ( i = 0; i < argc_; i++ )
351 {
352 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv_[i]));
353 }
354
355 argvGTK[argc_] = NULL;
356
357 int argcGTK = argc_;
358
359 #ifdef __WXGPE__
360 init_result = true; // is there a _check() version of this?
361 gpe_application_init( &argcGTK, &argvGTK );
362 #else
363 init_result = gtk_init_check( &argcGTK, &argvGTK );
364 #endif
365 wxUpdateLocaleIsUtf8();
366
367 if ( argcGTK != argc_ )
368 {
369 // we have to drop the parameters which were consumed by GTK+
370 for ( i = 0; i < argcGTK; i++ )
371 {
372 while ( strcmp(wxConvUTF8.cWX2MB(argv_[i]), argvGTK[i]) != 0 )
373 {
374 memmove(argv_ + i, argv_ + i + 1, (argc_ - i)*sizeof(*argv_));
375 }
376 }
377
378 argc_ = argcGTK;
379 argv_[argc_] = NULL;
380 }
381 //else: gtk_init() didn't modify our parameters
382
383 // free our copy
384 for ( i = 0; i < argcGTK; i++ )
385 {
386 free(argvGTK[i]);
387 }
388
389 delete [] argvGTK;
390 #else // !wxUSE_UNICODE
391 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
392 // it's ok to pass pointer to it
393 init_result = gtk_init_check( &argc_, &argv_ );
394 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
395
396 // update internal arg[cv] as GTK+ may have removed processed options:
397 this->argc = argc_;
398 this->argv = argv_;
399
400 if ( m_traits )
401 {
402 // if there are still GTK+ standard options unparsed in the command
403 // line, it means that they were not syntactically correct and GTK+
404 // already printed a warning on the command line and we should now
405 // exit:
406 wxArrayString opt, desc;
407 m_traits->GetStandardCmdLineOptions(opt, desc);
408
409 for ( i = 0; i < argc_; i++ )
410 {
411 // leave just the names of the options with values
412 const wxString str = wxString(argv_[i]).BeforeFirst('=');
413
414 for ( size_t j = 0; j < opt.size(); j++ )
415 {
416 // remove the leading spaces from the option string as it does
417 // have them
418 if ( opt[j].Trim(false).BeforeFirst('=') == str )
419 {
420 // a GTK+ option can be left on the command line only if
421 // there was an error in (or before, in another standard
422 // options) it, so abort, just as we do if incorrect
423 // program option is given
424 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
425 argv_[0]);
426 return false;
427 }
428 }
429 }
430 }
431
432 if ( !init_result )
433 {
434 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
435 return false;
436 }
437
438 // we can not enter threads before gtk_init is done
439 gdk_threads_enter();
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