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