]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/app.cpp
#11259: Correction for bad help button on OSX
[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
84833214 29#include "wx/gtk/private.h"
f7a3c9be 30#include "wx/apptrait.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
c259ffdf 40#include <gdk/gdkx.h>
24178e4a 41
2b850ae1
RR
42//-----------------------------------------------------------------------------
43// link GnomeVFS
44//-----------------------------------------------------------------------------
45
09a09455
PC
46#if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
47 #include "wx/link.h"
48 wxFORCE_LINK_MODULE(gnome_vfs)
2b850ae1
RR
49#endif
50
bf9e3e73
RR
51//-----------------------------------------------------------------------------
52// local functions
53//-----------------------------------------------------------------------------
54
a1abca32
PC
55// One-shot signal emission hook, to install idle handler.
56extern "C" {
14819684 57static gboolean
a1abca32 58wx_emission_hook(GSignalInvocationHint*, guint, const GValue*, gpointer data)
14819684 59{
a1abca32
PC
60 wxApp* app = wxTheApp;
61 if (app != NULL)
62 app->WakeUpIdle();
63 gulong* hook_id = (gulong*)data;
64 // record that hook is not installed
65 *hook_id = 0;
14819684
PC
66 // remove hook
67 return false;
68}
a1abca32 69}
14819684 70
a1abca32
PC
71// Add signal emission hooks, to re-install idle handler when needed.
72static void wx_add_idle_hooks()
8a378a9e 73{
a1abca32 74 // "event" hook
176d9824 75 {
a1abca32
PC
76 static gulong hook_id = 0;
77 if (hook_id == 0)
78 {
79 static guint sig_id = 0;
80 if (sig_id == 0)
81 sig_id = g_signal_lookup("event", GTK_TYPE_WIDGET);
82 hook_id = g_signal_add_emission_hook(
83 sig_id, 0, wx_emission_hook, &hook_id, NULL);
84 }
85 }
86 // "size_allocate" hook
87 // Needed to match the behavior of the old idle system,
88 // but probably not necessary.
89 {
90 static gulong hook_id = 0;
91 if (hook_id == 0)
92 {
93 static guint sig_id = 0;
94 if (sig_id == 0)
95 sig_id = g_signal_lookup("size_allocate", GTK_TYPE_WIDGET);
96 hook_id = g_signal_add_emission_hook(
97 sig_id, 0, wx_emission_hook, &hook_id, NULL);
98 }
176d9824 99 }
8a378a9e
PC
100}
101
a1abca32
PC
102extern "C" {
103static gboolean wxapp_idle_callback(gpointer)
96997d65 104{
a1abca32
PC
105 return wxTheApp->DoIdle();
106}
107}
ec439571 108
a1abca32
PC
109bool wxApp::DoIdle()
110{
111 guint id_save;
ec439571 112 {
a1abca32
PC
113 // Allow another idle source to be added while this one is busy.
114 // Needed if an idle event handler runs a new event loop,
115 // for example by showing a dialog.
046c2f14 116#if wxUSE_THREADS
a1abca32 117 wxMutexLocker lock(*m_idleMutex);
046c2f14 118#endif
a1abca32
PC
119 id_save = m_idleSourceId;
120 m_idleSourceId = 0;
121 wx_add_idle_hooks();
657a8a35
VZ
122
123#if wxDEBUG_LEVEL
a1abca32
PC
124 // don't generate the idle events while the assert modal dialog is shown,
125 // this matches the behavior of wxMSW
126 if (m_isInAssert)
127 return false;
128#endif
129 }
ec439571 130
a1abca32
PC
131 gdk_threads_enter();
132 bool needMore;
133 do {
26bacb82
VZ
134 ProcessPendingEvents();
135
a1abca32
PC
136 needMore = ProcessIdle();
137 } while (needMore && gtk_events_pending() == 0);
138 gdk_threads_leave();
f4322df6 139
046c2f14 140#if wxUSE_THREADS
a1abca32 141 wxMutexLocker lock(*m_idleMutex);
046c2f14 142#endif
a1abca32
PC
143 // if a new idle source was added during ProcessIdle
144 if (m_idleSourceId != 0)
145 {
146 // remove it
147 g_source_remove(m_idleSourceId);
148 m_idleSourceId = 0;
ec439571 149 }
81a2edf9
PC
150
151 // Pending events can be added asynchronously,
152 // need to keep idle source if any have appeared
153 needMore = needMore || HasPendingEvents();
154
a1abca32
PC
155 // if more idle processing requested
156 if (needMore)
9a5c9a0c 157 {
a1abca32
PC
158 // keep this source installed
159 m_idleSourceId = id_save;
160 return true;
9a5c9a0c 161 }
a1abca32
PC
162 // add hooks and remove this source
163 wx_add_idle_hooks();
164 return false;
acfd422a 165}
8801832d 166
005f5d18
RR
167//-----------------------------------------------------------------------------
168// Access to the root window global
169//-----------------------------------------------------------------------------
170
171GtkWidget* wxGetRootWindow()
172{
dde19c21
FM
173 static GtkWidget *s_RootWindow = NULL;
174
175 if (s_RootWindow == NULL)
005f5d18 176 {
dde19c21
FM
177 s_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
178 gtk_widget_realize( s_RootWindow );
005f5d18 179 }
dde19c21 180 return s_RootWindow;
005f5d18
RR
181}
182
c801d85f
KB
183//-----------------------------------------------------------------------------
184// wxApp
185//-----------------------------------------------------------------------------
186
187IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
188
c801d85f
KB
189wxApp::wxApp()
190{
de6185e2 191 m_isInAssert = false;
657a8a35 192
a1abca32
PC
193#if wxUSE_THREADS
194 m_idleMutex = NULL;
195#endif
196 m_idleSourceId = 0;
ff7b1510 197}
c801d85f 198
60acb947 199wxApp::~wxApp()
c801d85f 200{
ff7b1510 201}
c801d85f 202
c50c6fb2
VZ
203bool 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
0d2a2b60 227bool wxApp::OnInitGui()
c801d85f 228{
1e6feb95 229 if ( !wxAppBase::OnInitGui() )
de6185e2 230 return false;
1e6feb95 231
a6f5aa49
VZ
232 // if this is a wxGLApp (derived from wxApp), and we've already
233 // chosen a specific visual, then derive the GdkVisual from that
498ace9e 234 if ( GetXVisualInfo() )
005f5d18 235 {
a6f5aa49 236 GdkVisual* vis = gtk_widget_get_default_visual();
a6f5aa49
VZ
237
238 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
239 gtk_widget_set_default_colormap( colormap );
a6f5aa49 240 }
005f5d18 241 else
b134516c 242 {
c77eea28
RR
243 // On some machines, the default visual is just 256 colours, so
244 // we make sure we get the best. This can sometimes be wasteful.
245 if (m_useBestVisual)
246 {
247 if (m_forceTrueColour)
248 {
249 GdkVisual* visual = gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR );
250 if (!visual)
251 {
252 wxLogError(wxT("Unable to initialize TrueColor visual."));
253 return false;
254 }
255 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
256 gtk_widget_set_default_colormap( colormap );
257 }
258 else
259 {
260 if (gdk_visual_get_best() != gdk_visual_get_system())
261 {
262 GdkVisual* visual = gdk_visual_get_best();
263 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
264 gtk_widget_set_default_colormap( colormap );
265 }
266 }
267 }
f6fcbb63 268 }
c801d85f 269
426d19f1
JS
270#if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
271 if ( !GetHildonProgram() )
e2f3bc41
VZ
272 {
273 wxLogError(_("Unable to initialize Hildon program"));
274 return false;
275 }
426d19f1 276#endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2
e2f3bc41 277
88a7a4e1 278 return true;
bbe0af5b
RR
279}
280
005f5d18
RR
281GdkVisual *wxApp::GetGdkVisual()
282{
283 GdkVisual *visual = NULL;
be88a6ad 284
498ace9e
VZ
285 XVisualInfo *xvi = (XVisualInfo *)GetXVisualInfo();
286 if ( xvi )
287 visual = gdkx_visual_get( xvi->visualid );
005f5d18 288 else
22a3bce4 289 visual = gdk_drawable_get_visual( wxGetRootWindow()->window );
be88a6ad 290
005f5d18 291 wxASSERT( visual );
be88a6ad 292
005f5d18
RR
293 return visual;
294}
295
291b0f5b
VZ
296// use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
297bool wxApp::Initialize(int& argc_, wxChar **argv_)
c801d85f 298{
291b0f5b 299 if ( !wxAppBase::Initialize(argc_, argv_) )
d774f916 300 return false;
68567a96 301
924ef850 302#if wxUSE_THREADS
ac131bab 303 if (!g_thread_supported())
d254213e 304 {
ac131bab 305 g_thread_init(NULL);
d254213e
PC
306 gdk_threads_init();
307 }
05e2b077 308#endif // wxUSE_THREADS
2286341c 309
68567a96
MR
310 // gtk+ 2.0 supports Unicode through UTF-8 strings
311 wxConvCurrent = &wxConvUTF8;
002f4218 312
845905d5
MW
313 // decide which conversion to use for the file names
314
315 // (1) this variable exists for the sole purpose of specifying the encoding
316 // of the filenames for GTK+ programs, so use it if it is set
9a83f860
VZ
317 wxString encName(wxGetenv(wxT("G_FILENAME_ENCODING")));
318 encName = encName.BeforeFirst(wxT(','));
319 if (encName.CmpNoCase(wxT("@locale")) == 0)
d24b23b7 320 encName.clear();
845905d5 321 encName.MakeUpper();
68567a96 322#if wxUSE_INTL
845905d5
MW
323 if (encName.empty())
324 {
325 // (2) if a non default locale is set, assume that the user wants his
326 // filenames in this locale too
327 encName = wxLocale::GetSystemEncodingName().Upper();
328 // (3) finally use UTF-8 by default
9a83f860
VZ
329 if (encName.empty() || encName == wxT("US-ASCII"))
330 encName = wxT("UTF-8");
331 wxSetEnv(wxT("G_FILENAME_ENCODING"), encName);
845905d5
MW
332 }
333#else
334 if (encName.empty())
9a83f860 335 encName = wxT("UTF-8");
7ecb75b7
VZ
336
337 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
338 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
339 // from gtk_init_check() as it does by default
340 gtk_disable_setlocale();
341
845905d5
MW
342#endif // wxUSE_INTL
343 static wxConvBrokenFileNames fileconv(encName);
344 wxConvFileName = &fileconv;
66bf0099 345
d774f916
VZ
346
347 bool init_result;
4b4e81ee 348 int i;
d774f916 349
05e2b077
VZ
350#if wxUSE_UNICODE
351 // gtk_init() wants UTF-8, not wchar_t, so convert
291b0f5b
VZ
352 char **argvGTK = new char *[argc_ + 1];
353 for ( i = 0; i < argc_; i++ )
924ef850 354 {
291b0f5b 355 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv_[i]));
924ef850 356 }
0cf2cb36 357
291b0f5b 358 argvGTK[argc_] = NULL;
954de0f1 359
291b0f5b 360 int argcGTK = argc_;
68567a96 361
62be94e1 362#ifdef __WXGPE__
c156411a 363 init_result = true; // is there a _check() version of this?
62be94e1
RR
364 gpe_application_init( &argcGTK, &argvGTK );
365#else
c156411a 366 init_result = gtk_init_check( &argcGTK, &argvGTK );
62be94e1 367#endif
cb352236 368 wxUpdateLocaleIsUtf8();
954de0f1 369
291b0f5b 370 if ( argcGTK != argc_ )
924ef850 371 {
05e2b077
VZ
372 // we have to drop the parameters which were consumed by GTK+
373 for ( i = 0; i < argcGTK; i++ )
374 {
291b0f5b 375 while ( strcmp(wxConvUTF8.cWX2MB(argv_[i]), argvGTK[i]) != 0 )
05e2b077 376 {
291b0f5b 377 memmove(argv_ + i, argv_ + i + 1, (argc_ - i)*sizeof(*argv_));
05e2b077
VZ
378 }
379 }
0cf2cb36 380
291b0f5b 381 argc_ = argcGTK;
4f6b94a3 382 argv_[argc_] = NULL;
2b5f62a0 383 }
05e2b077 384 //else: gtk_init() didn't modify our parameters
e0253070 385
05e2b077
VZ
386 // free our copy
387 for ( i = 0; i < argcGTK; i++ )
0151c3eb 388 {
05e2b077 389 free(argvGTK[i]);
0151c3eb 390 }
0cf2cb36 391
05e2b077
VZ
392 delete [] argvGTK;
393#else // !wxUSE_UNICODE
291b0f5b 394 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
05e2b077 395 // it's ok to pass pointer to it
291b0f5b 396 init_result = gtk_init_check( &argc_, &argv_ );
05e2b077 397#endif // wxUSE_UNICODE/!wxUSE_UNICODE
0cf2cb36 398
f7a3c9be 399 // update internal arg[cv] as GTK+ may have removed processed options:
291b0f5b
VZ
400 this->argc = argc_;
401 this->argv = argv_;
f7a3c9be 402
b045eff2
VZ
403 if ( m_traits )
404 {
405 // if there are still GTK+ standard options unparsed in the command
406 // line, it means that they were not syntactically correct and GTK+
407 // already printed a warning on the command line and we should now
408 // exit:
409 wxArrayString opt, desc;
410 m_traits->GetStandardCmdLineOptions(opt, desc);
411
4b4e81ee 412 for ( i = 0; i < argc_; i++ )
b045eff2
VZ
413 {
414 // leave just the names of the options with values
291b0f5b 415 const wxString str = wxString(argv_[i]).BeforeFirst('=');
b045eff2
VZ
416
417 for ( size_t j = 0; j < opt.size(); j++ )
418 {
419 // remove the leading spaces from the option string as it does
420 // have them
421 if ( opt[j].Trim(false).BeforeFirst('=') == str )
422 {
423 // a GTK+ option can be left on the command line only if
424 // there was an error in (or before, in another standard
425 // options) it, so abort, just as we do if incorrect
426 // program option is given
427 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
291b0f5b 428 argv_[0]);
b045eff2
VZ
429 return false;
430 }
431 }
432 }
433 }
434
f7a3c9be
VZ
435 if ( !init_result )
436 {
437 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
c156411a
RD
438 return false;
439 }
68567a96 440
05e2b077
VZ
441 // we can not enter threads before gtk_init is done
442 gdk_threads_enter();
0cf2cb36 443
05e2b077
VZ
444#if wxUSE_INTL
445 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
446#endif
be88a6ad 447
a1abca32
PC
448#if wxUSE_THREADS
449 m_idleMutex = new wxMutex;
450#endif
451 // make sure GtkWidget type is loaded, idle hooks need it
452 g_type_class_ref(GTK_TYPE_WIDGET);
453 WakeUpIdle();
454
05e2b077
VZ
455 return true;
456}
0cf2cb36 457
05e2b077
VZ
458void wxApp::CleanUp()
459{
a1abca32
PC
460 if (m_idleSourceId != 0)
461 g_source_remove(m_idleSourceId);
c114eb7a 462
a1abca32
PC
463 // release reference acquired by Initialize()
464 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET));
465
05e2b077 466 gdk_threads_leave();
abca8ebf
VZ
467
468 wxAppBase::CleanUp();
c114eb7a
VZ
469
470 // delete this mutex as late as possible as it's used from WakeUpIdle(), in
471 // particular do it after calling the base class CleanUp() which can result
472 // in it being called
473#if wxUSE_THREADS
474 delete m_idleMutex;
475 m_idleMutex = NULL;
476#endif
ff7b1510 477}
1a56f55c 478
a1abca32
PC
479void wxApp::WakeUpIdle()
480{
481#if wxUSE_THREADS
482 wxMutexLocker lock(*m_idleMutex);
483#endif
484 if (m_idleSourceId == 0)
485 m_idleSourceId = g_idle_add_full(G_PRIORITY_LOW, wxapp_idle_callback, NULL, NULL);
486}
487
488// Checking for pending events requires first removing our idle source,
489// otherwise it will cause the check to always return true.
490bool wxApp::EventsPending()
491{
492#if wxUSE_THREADS
493 wxMutexLocker lock(*m_idleMutex);
494#endif
495 if (m_idleSourceId != 0)
496 {
497 g_source_remove(m_idleSourceId);
498 m_idleSourceId = 0;
499 wx_add_idle_hooks();
500 }
501 return gtk_events_pending() != 0;
502}
503
2d97237d
VZ
504void wxApp::OnAssertFailure(const wxChar *file,
505 int line,
506 const wxChar* func,
507 const wxChar* cond,
508 const wxChar *msg)
a5f1fd3e 509{
657a8a35
VZ
510 // there is no need to do anything if asserts are disabled in this build
511 // anyhow
512#if wxDEBUG_LEVEL
ec439571
PC
513 // block wx idle events while assert dialog is showing
514 m_isInAssert = true;
a5f1fd3e 515
2d97237d 516 wxAppBase::OnAssertFailure(file, line, func, cond, msg);
a5f1fd3e 517
ec439571 518 m_isInAssert = false;
657a8a35
VZ
519#else // !wxDEBUG_LEVEL
520 wxUnusedVar(file);
521 wxUnusedVar(line);
522 wxUnusedVar(func);
523 wxUnusedVar(cond);
524 wxUnusedVar(msg);
525#endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
a5f1fd3e
VZ
526}
527
d254213e
PC
528#if wxUSE_THREADS
529void wxGUIAppTraits::MutexGuiEnter()
530{
531 gdk_threads_enter();
532}
533
534void wxGUIAppTraits::MutexGuiLeave()
535{
536 gdk_threads_leave();
537}
538#endif // wxUSE_THREADS
426d19f1
JS
539
540#if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
541// Maemo-specific method: get the main program object
542HildonProgram *wxApp::GetHildonProgram()
543{
544 return hildon_program_get_instance();
545}
546
547#endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2