]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/app.cpp
Optionally allow showing tooltips for disabled ribbon buttons.
[wxWidgets.git] / src / gtk / app.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
88a7a4e1 2// Name: src/gtk/app.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
32e9da8b 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling, Julian Smart
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
f3858bf5
JJ
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
c801d85f 13#include "wx/app.h"
88a7a4e1
WS
14
15#ifndef WX_PRECOMP
16 #include "wx/intl.h"
e4db172a 17 #include "wx/log.h"
de6185e2 18 #include "wx/utils.h"
5b56bffb 19 #include "wx/memory.h"
48a1108e 20 #include "wx/font.h"
88a7a4e1
WS
21#endif
22
fe593cc5 23#include "wx/thread.h"
afb74891 24
62be94e1 25#ifdef __WXGPE__
88a7a4e1 26 #include <gpe/init.h>
62be94e1
RR
27#endif
28
f7a3c9be 29#include "wx/apptrait.h"
f32848c5 30#include "wx/fontmap.h"
c801d85f 31
e2f3bc41
VZ
32#if wxUSE_LIBHILDON
33 #include <hildon-widgets/hildon-program.h>
34#endif // wxUSE_LIBHILDON
35
426d19f1
JS
36#if wxUSE_LIBHILDON2
37 #include <hildon/hildon.h>
38#endif // wxUSE_LIBHILDON2
39
9dc44eff
PC
40#include <gtk/gtk.h>
41#include "wx/gtk/private.h"
24178e4a 42
2b850ae1
RR
43//-----------------------------------------------------------------------------
44// link GnomeVFS
45//-----------------------------------------------------------------------------
46
09a09455
PC
47#if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
48 #include "wx/link.h"
49 wxFORCE_LINK_MODULE(gnome_vfs)
2b850ae1
RR
50#endif
51
bf9e3e73
RR
52//-----------------------------------------------------------------------------
53// local functions
54//-----------------------------------------------------------------------------
55
a1abca32
PC
56// One-shot signal emission hook, to install idle handler.
57extern "C" {
14819684 58static gboolean
a1abca32 59wx_emission_hook(GSignalInvocationHint*, guint, const GValue*, gpointer data)
14819684 60{
a1abca32
PC
61 wxApp* app = wxTheApp;
62 if (app != NULL)
63 app->WakeUpIdle();
4d4ec2a5 64 bool* hook_installed = (bool*)data;
a1abca32 65 // record that hook is not installed
4d4ec2a5 66 *hook_installed = false;
14819684
PC
67 // remove hook
68 return false;
69}
a1abca32 70}
14819684 71
a1abca32
PC
72// Add signal emission hooks, to re-install idle handler when needed.
73static void wx_add_idle_hooks()
8a378a9e 74{
a1abca32 75 // "event" hook
176d9824 76 {
4d4ec2a5
PC
77 static bool hook_installed;
78 if (!hook_installed)
a1abca32 79 {
4d4ec2a5 80 static guint sig_id;
a1abca32
PC
81 if (sig_id == 0)
82 sig_id = g_signal_lookup("event", GTK_TYPE_WIDGET);
4d4ec2a5
PC
83 hook_installed = true;
84 g_signal_add_emission_hook(
85 sig_id, 0, wx_emission_hook, &hook_installed, NULL);
a1abca32
PC
86 }
87 }
88 // "size_allocate" hook
4c51a665 89 // Needed to match the behaviour of the old idle system,
a1abca32
PC
90 // but probably not necessary.
91 {
4d4ec2a5
PC
92 static bool hook_installed;
93 if (!hook_installed)
a1abca32 94 {
4d4ec2a5 95 static guint sig_id;
a1abca32
PC
96 if (sig_id == 0)
97 sig_id = g_signal_lookup("size_allocate", GTK_TYPE_WIDGET);
4d4ec2a5
PC
98 hook_installed = true;
99 g_signal_add_emission_hook(
100 sig_id, 0, wx_emission_hook, &hook_installed, NULL);
a1abca32 101 }
176d9824 102 }
8a378a9e
PC
103}
104
a1abca32
PC
105extern "C" {
106static gboolean wxapp_idle_callback(gpointer)
96997d65 107{
a1abca32
PC
108 return wxTheApp->DoIdle();
109}
110}
ec439571 111
a1abca32
PC
112bool wxApp::DoIdle()
113{
114 guint id_save;
ec439571 115 {
a1abca32
PC
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.
046c2f14 119#if wxUSE_THREADS
4d4ec2a5 120 wxMutexLocker lock(m_idleMutex);
046c2f14 121#endif
a1abca32
PC
122 id_save = m_idleSourceId;
123 m_idleSourceId = 0;
124 wx_add_idle_hooks();
657a8a35
VZ
125
126#if wxDEBUG_LEVEL
a1abca32 127 // don't generate the idle events while the assert modal dialog is shown,
4c51a665 128 // this matches the behaviour of wxMSW
a1abca32
PC
129 if (m_isInAssert)
130 return false;
131#endif
132 }
ec439571 133
a1abca32
PC
134 gdk_threads_enter();
135 bool needMore;
136 do {
26bacb82
VZ
137 ProcessPendingEvents();
138
a1abca32
PC
139 needMore = ProcessIdle();
140 } while (needMore && gtk_events_pending() == 0);
141 gdk_threads_leave();
f4322df6 142
046c2f14 143#if wxUSE_THREADS
4d4ec2a5 144 wxMutexLocker lock(m_idleMutex);
046c2f14 145#endif
81a2edf9 146
8a48b920
PC
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)
9a5c9a0c 151 {
8a48b920
PC
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();
9a5c9a0c 161 }
8a48b920
PC
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;
acfd422a 166}
8801832d 167
005f5d18
RR
168//-----------------------------------------------------------------------------
169// Access to the root window global
170//-----------------------------------------------------------------------------
171
172GtkWidget* wxGetRootWindow()
173{
dde19c21
FM
174 static GtkWidget *s_RootWindow = NULL;
175
176 if (s_RootWindow == NULL)
005f5d18 177 {
dde19c21
FM
178 s_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
179 gtk_widget_realize( s_RootWindow );
005f5d18 180 }
dde19c21 181 return s_RootWindow;
005f5d18
RR
182}
183
c801d85f
KB
184//-----------------------------------------------------------------------------
185// wxApp
186//-----------------------------------------------------------------------------
187
188IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
189
c801d85f
KB
190wxApp::wxApp()
191{
de6185e2 192 m_isInAssert = false;
a1abca32 193 m_idleSourceId = 0;
ff7b1510 194}
c801d85f 195
60acb947 196wxApp::~wxApp()
c801d85f 197{
ff7b1510 198}
c801d85f 199
c50c6fb2
VZ
200bool wxApp::SetNativeTheme(const wxString& theme)
201{
9f7405d0
PC
202#ifdef __WXGTK3__
203 wxUnusedVar(theme);
204 return false;
205#else
c50c6fb2
VZ
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;
9f7405d0 226#endif
c50c6fb2
VZ
227}
228
0d2a2b60 229bool wxApp::OnInitGui()
c801d85f 230{
1e6feb95 231 if ( !wxAppBase::OnInitGui() )
de6185e2 232 return false;
1e6feb95 233
9dc44eff 234#ifndef __WXGTK3__
a6f5aa49
VZ
235 // if this is a wxGLApp (derived from wxApp), and we've already
236 // chosen a specific visual, then derive the GdkVisual from that
498ace9e 237 if ( GetXVisualInfo() )
005f5d18 238 {
a6f5aa49 239 GdkVisual* vis = gtk_widget_get_default_visual();
a6f5aa49
VZ
240
241 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
242 gtk_widget_set_default_colormap( colormap );
a6f5aa49 243 }
005f5d18 244 else
b134516c 245 {
c77eea28
RR
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 }
f6fcbb63 271 }
9dc44eff 272#endif
c801d85f 273
426d19f1
JS
274#if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
275 if ( !GetHildonProgram() )
e2f3bc41
VZ
276 {
277 wxLogError(_("Unable to initialize Hildon program"));
278 return false;
279 }
426d19f1 280#endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2
e2f3bc41 281
88a7a4e1 282 return true;
bbe0af5b
RR
283}
284
291b0f5b
VZ
285// use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
286bool wxApp::Initialize(int& argc_, wxChar **argv_)
c801d85f 287{
291b0f5b 288 if ( !wxAppBase::Initialize(argc_, argv_) )
d774f916 289 return false;
68567a96 290
924ef850 291#if wxUSE_THREADS
ac131bab 292 if (!g_thread_supported())
d254213e 293 {
ac131bab 294 g_thread_init(NULL);
d254213e
PC
295 gdk_threads_init();
296 }
05e2b077 297#endif // wxUSE_THREADS
2286341c 298
68567a96
MR
299 // gtk+ 2.0 supports Unicode through UTF-8 strings
300 wxConvCurrent = &wxConvUTF8;
002f4218 301
c282e47d 302#ifdef __UNIX__
845905d5
MW
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
9a83f860
VZ
307 wxString encName(wxGetenv(wxT("G_FILENAME_ENCODING")));
308 encName = encName.BeforeFirst(wxT(','));
309 if (encName.CmpNoCase(wxT("@locale")) == 0)
d24b23b7 310 encName.clear();
845905d5 311 encName.MakeUpper();
68567a96 312#if wxUSE_INTL
845905d5
MW
313 if (encName.empty())
314 {
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();
f32848c5
VZ
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
845905d5 334 // (3) finally use UTF-8 by default
f32848c5 335 if ( encName.empty() )
9a83f860
VZ
336 encName = wxT("UTF-8");
337 wxSetEnv(wxT("G_FILENAME_ENCODING"), encName);
845905d5
MW
338 }
339#else
340 if (encName.empty())
9a83f860 341 encName = wxT("UTF-8");
7ecb75b7
VZ
342
343 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
344 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
345 // from gtk_init_check() as it does by default
346 gtk_disable_setlocale();
347
845905d5
MW
348#endif // wxUSE_INTL
349 static wxConvBrokenFileNames fileconv(encName);
350 wxConvFileName = &fileconv;
c282e47d 351#endif // __UNIX__
66bf0099 352
d774f916
VZ
353
354 bool init_result;
4b4e81ee 355 int i;
d774f916 356
05e2b077
VZ
357#if wxUSE_UNICODE
358 // gtk_init() wants UTF-8, not wchar_t, so convert
291b0f5b
VZ
359 char **argvGTK = new char *[argc_ + 1];
360 for ( i = 0; i < argc_; i++ )
924ef850 361 {
291b0f5b 362 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv_[i]));
924ef850 363 }
0cf2cb36 364
291b0f5b 365 argvGTK[argc_] = NULL;
954de0f1 366
291b0f5b 367 int argcGTK = argc_;
68567a96 368
62be94e1 369#ifdef __WXGPE__
c156411a 370 init_result = true; // is there a _check() version of this?
62be94e1
RR
371 gpe_application_init( &argcGTK, &argvGTK );
372#else
d5027818 373 init_result = gtk_init_check( &argcGTK, &argvGTK ) != 0;
62be94e1 374#endif
cb352236 375 wxUpdateLocaleIsUtf8();
954de0f1 376
291b0f5b 377 if ( argcGTK != argc_ )
924ef850 378 {
05e2b077
VZ
379 // we have to drop the parameters which were consumed by GTK+
380 for ( i = 0; i < argcGTK; i++ )
381 {
291b0f5b 382 while ( strcmp(wxConvUTF8.cWX2MB(argv_[i]), argvGTK[i]) != 0 )
05e2b077 383 {
291b0f5b 384 memmove(argv_ + i, argv_ + i + 1, (argc_ - i)*sizeof(*argv_));
05e2b077
VZ
385 }
386 }
0cf2cb36 387
291b0f5b 388 argc_ = argcGTK;
4f6b94a3 389 argv_[argc_] = NULL;
2b5f62a0 390 }
05e2b077 391 //else: gtk_init() didn't modify our parameters
e0253070 392
05e2b077
VZ
393 // free our copy
394 for ( i = 0; i < argcGTK; i++ )
0151c3eb 395 {
05e2b077 396 free(argvGTK[i]);
0151c3eb 397 }
0cf2cb36 398
05e2b077
VZ
399 delete [] argvGTK;
400#else // !wxUSE_UNICODE
291b0f5b 401 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
05e2b077 402 // it's ok to pass pointer to it
291b0f5b 403 init_result = gtk_init_check( &argc_, &argv_ );
05e2b077 404#endif // wxUSE_UNICODE/!wxUSE_UNICODE
0cf2cb36 405
f7a3c9be 406 // update internal arg[cv] as GTK+ may have removed processed options:
291b0f5b
VZ
407 this->argc = argc_;
408 this->argv = argv_;
f7a3c9be 409
b045eff2
VZ
410 if ( m_traits )
411 {
412 // if there are still GTK+ standard options unparsed in the command
413 // line, it means that they were not syntactically correct and GTK+
414 // already printed a warning on the command line and we should now
415 // exit:
416 wxArrayString opt, desc;
417 m_traits->GetStandardCmdLineOptions(opt, desc);
418
4b4e81ee 419 for ( i = 0; i < argc_; i++ )
b045eff2
VZ
420 {
421 // leave just the names of the options with values
291b0f5b 422 const wxString str = wxString(argv_[i]).BeforeFirst('=');
b045eff2
VZ
423
424 for ( size_t j = 0; j < opt.size(); j++ )
425 {
426 // remove the leading spaces from the option string as it does
427 // have them
428 if ( opt[j].Trim(false).BeforeFirst('=') == str )
429 {
430 // a GTK+ option can be left on the command line only if
431 // there was an error in (or before, in another standard
432 // options) it, so abort, just as we do if incorrect
433 // program option is given
434 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
291b0f5b 435 argv_[0]);
b045eff2
VZ
436 return false;
437 }
438 }
439 }
440 }
441
f7a3c9be
VZ
442 if ( !init_result )
443 {
444 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
c156411a
RD
445 return false;
446 }
68567a96 447
4c51a665 448 // we cannot enter threads before gtk_init is done
05e2b077 449 gdk_threads_enter();
0cf2cb36 450
05e2b077
VZ
451#if wxUSE_INTL
452 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
453#endif
be88a6ad 454
a1abca32
PC
455 // make sure GtkWidget type is loaded, idle hooks need it
456 g_type_class_ref(GTK_TYPE_WIDGET);
457 WakeUpIdle();
458
05e2b077
VZ
459 return true;
460}
0cf2cb36 461
05e2b077
VZ
462void wxApp::CleanUp()
463{
a1abca32
PC
464 if (m_idleSourceId != 0)
465 g_source_remove(m_idleSourceId);
c114eb7a 466
a1abca32
PC
467 // release reference acquired by Initialize()
468 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET));
469
05e2b077 470 gdk_threads_leave();
abca8ebf
VZ
471
472 wxAppBase::CleanUp();
ff7b1510 473}
1a56f55c 474
a1abca32
PC
475void wxApp::WakeUpIdle()
476{
477#if wxUSE_THREADS
4d4ec2a5 478 wxMutexLocker lock(m_idleMutex);
a1abca32
PC
479#endif
480 if (m_idleSourceId == 0)
481 m_idleSourceId = g_idle_add_full(G_PRIORITY_LOW, wxapp_idle_callback, NULL, NULL);
482}
483
484// Checking for pending events requires first removing our idle source,
485// otherwise it will cause the check to always return true.
486bool wxApp::EventsPending()
487{
488#if wxUSE_THREADS
4d4ec2a5 489 wxMutexLocker lock(m_idleMutex);
a1abca32
PC
490#endif
491 if (m_idleSourceId != 0)
492 {
493 g_source_remove(m_idleSourceId);
494 m_idleSourceId = 0;
495 wx_add_idle_hooks();
496 }
497 return gtk_events_pending() != 0;
498}
499
2d97237d
VZ
500void wxApp::OnAssertFailure(const wxChar *file,
501 int line,
502 const wxChar* func,
503 const wxChar* cond,
504 const wxChar *msg)
a5f1fd3e 505{
657a8a35
VZ
506 // there is no need to do anything if asserts are disabled in this build
507 // anyhow
508#if wxDEBUG_LEVEL
ec439571
PC
509 // block wx idle events while assert dialog is showing
510 m_isInAssert = true;
a5f1fd3e 511
2d97237d 512 wxAppBase::OnAssertFailure(file, line, func, cond, msg);
a5f1fd3e 513
ec439571 514 m_isInAssert = false;
657a8a35
VZ
515#else // !wxDEBUG_LEVEL
516 wxUnusedVar(file);
517 wxUnusedVar(line);
518 wxUnusedVar(func);
519 wxUnusedVar(cond);
520 wxUnusedVar(msg);
521#endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
a5f1fd3e
VZ
522}
523
d254213e
PC
524#if wxUSE_THREADS
525void wxGUIAppTraits::MutexGuiEnter()
526{
527 gdk_threads_enter();
528}
529
530void wxGUIAppTraits::MutexGuiLeave()
531{
532 gdk_threads_leave();
533}
534#endif // wxUSE_THREADS
426d19f1
JS
535
536#if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
537// Maemo-specific method: get the main program object
538HildonProgram *wxApp::GetHildonProgram()
ce00f59b
VZ
539{
540 return hildon_program_get_instance();
426d19f1 541}
ce00f59b 542
426d19f1 543#endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2