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