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