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