fix setting background color in wxGTK3 with themes which use background images or...
[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 wxString path;
203 path = gtk_rc_get_theme_dir();
204 path += "/";
205 path += theme.utf8_str();
206 path += "/gtk-2.0/gtkrc";
207
208 if ( wxFileExists(path.utf8_str()) )
209 gtk_rc_add_default_file(path.utf8_str());
210 else if ( wxFileExists(theme.utf8_str()) )
211 gtk_rc_add_default_file(theme.utf8_str());
212 else
213 {
214 wxLogWarning("Theme \"%s\" not available.", theme);
215
216 return false;
217 }
218
219 gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE);
220
221 return true;
222 }
223
224 bool wxApp::OnInitGui()
225 {
226 if ( !wxAppBase::OnInitGui() )
227 return false;
228
229 #ifndef __WXGTK3__
230 // if this is a wxGLApp (derived from wxApp), and we've already
231 // chosen a specific visual, then derive the GdkVisual from that
232 if ( GetXVisualInfo() )
233 {
234 GdkVisual* vis = gtk_widget_get_default_visual();
235
236 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
237 gtk_widget_set_default_colormap( colormap );
238 }
239 else
240 {
241 // On some machines, the default visual is just 256 colours, so
242 // we make sure we get the best. This can sometimes be wasteful.
243 if (m_useBestVisual)
244 {
245 if (m_forceTrueColour)
246 {
247 GdkVisual* visual = gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR );
248 if (!visual)
249 {
250 wxLogError(wxT("Unable to initialize TrueColor visual."));
251 return false;
252 }
253 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
254 gtk_widget_set_default_colormap( colormap );
255 }
256 else
257 {
258 if (gdk_visual_get_best() != gdk_visual_get_system())
259 {
260 GdkVisual* visual = gdk_visual_get_best();
261 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
262 gtk_widget_set_default_colormap( colormap );
263 }
264 }
265 }
266 }
267 #endif
268
269 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
270 if ( !GetHildonProgram() )
271 {
272 wxLogError(_("Unable to initialize Hildon program"));
273 return false;
274 }
275 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2
276
277 return true;
278 }
279
280 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
281 bool wxApp::Initialize(int& argc_, wxChar **argv_)
282 {
283 if ( !wxAppBase::Initialize(argc_, argv_) )
284 return false;
285
286 #if wxUSE_THREADS
287 if (!g_thread_supported())
288 {
289 g_thread_init(NULL);
290 gdk_threads_init();
291 }
292 #endif // wxUSE_THREADS
293
294 // gtk+ 2.0 supports Unicode through UTF-8 strings
295 wxConvCurrent = &wxConvUTF8;
296
297 #ifdef __UNIX__
298 // decide which conversion to use for the file names
299
300 // (1) this variable exists for the sole purpose of specifying the encoding
301 // of the filenames for GTK+ programs, so use it if it is set
302 wxString encName(wxGetenv(wxT("G_FILENAME_ENCODING")));
303 encName = encName.BeforeFirst(wxT(','));
304 if (encName.CmpNoCase(wxT("@locale")) == 0)
305 encName.clear();
306 encName.MakeUpper();
307 #if wxUSE_INTL
308 if (encName.empty())
309 {
310 // (2) if a non default locale is set, assume that the user wants his
311 // filenames in this locale too
312 encName = wxLocale::GetSystemEncodingName().Upper();
313
314 // But don't consider ASCII in this case.
315 if ( !encName.empty() )
316 {
317 #if wxUSE_FONTMAP
318 wxFontEncoding enc = wxFontMapperBase::GetEncodingFromName(encName);
319 if ( enc == wxFONTENCODING_DEFAULT )
320 #else // !wxUSE_FONTMAP
321 if ( encName == wxT("US-ASCII") )
322 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
323 {
324 // This means US-ASCII when returned from GetEncodingFromName().
325 encName.clear();
326 }
327 }
328
329 // (3) finally use UTF-8 by default
330 if ( encName.empty() )
331 encName = wxT("UTF-8");
332 wxSetEnv(wxT("G_FILENAME_ENCODING"), encName);
333 }
334 #else
335 if (encName.empty())
336 encName = wxT("UTF-8");
337
338 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
339 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
340 // from gtk_init_check() as it does by default
341 gtk_disable_setlocale();
342
343 #endif // wxUSE_INTL
344 static wxConvBrokenFileNames fileconv(encName);
345 wxConvFileName = &fileconv;
346 #endif // __UNIX__
347
348
349 bool init_result;
350 int i;
351
352 #if wxUSE_UNICODE
353 // gtk_init() wants UTF-8, not wchar_t, so convert
354 char **argvGTK = new char *[argc_ + 1];
355 for ( i = 0; i < argc_; i++ )
356 {
357 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv_[i]));
358 }
359
360 argvGTK[argc_] = NULL;
361
362 int argcGTK = argc_;
363
364 #ifdef __WXGPE__
365 init_result = true; // is there a _check() version of this?
366 gpe_application_init( &argcGTK, &argvGTK );
367 #else
368 init_result = gtk_init_check( &argcGTK, &argvGTK ) != 0;
369 #endif
370 wxUpdateLocaleIsUtf8();
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 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
532 // Maemo-specific method: get the main program object
533 HildonProgram *wxApp::GetHildonProgram()
534 {
535 return hildon_program_get_instance();
536 }
537
538 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2