update wxApp::argc/argv to remove the options parsed by GTK+ itself from them
[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 #ifdef __VMS
11 // vms_jackets.h should for proper working be included before anything else
12 # include <vms_jackets.h>
13 #undef ConnectionNumber
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #include "wx/app.h"
20
21 #ifndef WX_PRECOMP
22 #include "wx/intl.h"
23 #include "wx/log.h"
24 #include "wx/utils.h"
25 #include "wx/memory.h"
26 #include "wx/font.h"
27 #endif
28
29 #include "wx/thread.h"
30
31 #ifdef __WXGPE__
32 #include <gpe/init.h>
33 #endif
34
35 #include "wx/gtk/win_gtk.h"
36 #include "wx/gtk/private.h"
37 #include "wx/apptrait.h"
38
39 #include <gdk/gdkx.h>
40
41 //-----------------------------------------------------------------------------
42 // link GnomeVFS
43 //-----------------------------------------------------------------------------
44
45 #if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
46 #include "wx/link.h"
47 wxFORCE_LINK_MODULE(gnome_vfs)
48 #endif
49
50 //-----------------------------------------------------------------------------
51 // global data
52 //-----------------------------------------------------------------------------
53
54 bool g_mainThreadLocked = false;
55
56 static GtkWidget *gs_RootWindow = (GtkWidget*) NULL;
57
58 //-----------------------------------------------------------------------------
59 // wxYield
60 //-----------------------------------------------------------------------------
61
62 // not static because used by textctrl.cpp
63 //
64 // MT-FIXME
65 bool wxIsInsideYield = false;
66
67 bool wxApp::Yield(bool onlyIfNeeded)
68 {
69 if ( wxIsInsideYield )
70 {
71 if ( !onlyIfNeeded )
72 {
73 wxFAIL_MSG( wxT("wxYield called recursively" ) );
74 }
75
76 return false;
77 }
78
79 #if wxUSE_THREADS
80 if ( !wxThread::IsMain() )
81 {
82 // can't call gtk_main_iteration() from other threads like this
83 return true;
84 }
85 #endif // wxUSE_THREADS
86
87 wxIsInsideYield = true;
88
89 #if wxUSE_LOG
90 // disable log flushing from here because a call to wxYield() shouldn't
91 // normally result in message boxes popping up &c
92 wxLog::Suspend();
93 #endif
94
95 while (EventsPending())
96 gtk_main_iteration();
97
98 // It's necessary to call ProcessIdle() to update the frames sizes which
99 // might have been changed (it also will update other things set from
100 // OnUpdateUI() which is a nice (and desired) side effect). But we
101 // call ProcessIdle() only once since this is not meant for longish
102 // background jobs (controlled by wxIdleEvent::RequestMore() and the
103 // return value of Processidle().
104 ProcessIdle();
105
106 #if wxUSE_LOG
107 // let the logs be flashed again
108 wxLog::Resume();
109 #endif
110
111 wxIsInsideYield = false;
112
113 return true;
114 }
115
116 //-----------------------------------------------------------------------------
117 // local functions
118 //-----------------------------------------------------------------------------
119
120 // One-shot signal emission hook, to install idle handler.
121 extern "C" {
122 static gboolean
123 wx_emission_hook(GSignalInvocationHint*, guint, const GValue*, gpointer data)
124 {
125 wxApp* app = wxTheApp;
126 if (app != NULL)
127 app->WakeUpIdle();
128 gulong* hook_id = (gulong*)data;
129 // record that hook is not installed
130 *hook_id = 0;
131 // remove hook
132 return false;
133 }
134 }
135
136 // Add signal emission hooks, to re-install idle handler when needed.
137 static void wx_add_idle_hooks()
138 {
139 // "event" hook
140 {
141 static gulong hook_id = 0;
142 if (hook_id == 0)
143 {
144 static guint sig_id = 0;
145 if (sig_id == 0)
146 sig_id = g_signal_lookup("event", GTK_TYPE_WIDGET);
147 hook_id = g_signal_add_emission_hook(
148 sig_id, 0, wx_emission_hook, &hook_id, NULL);
149 }
150 }
151 // "size_allocate" hook
152 // Needed to match the behavior of the old idle system,
153 // but probably not necessary.
154 {
155 static gulong hook_id = 0;
156 if (hook_id == 0)
157 {
158 static guint sig_id = 0;
159 if (sig_id == 0)
160 sig_id = g_signal_lookup("size_allocate", GTK_TYPE_WIDGET);
161 hook_id = g_signal_add_emission_hook(
162 sig_id, 0, wx_emission_hook, &hook_id, NULL);
163 }
164 }
165 }
166
167 extern "C" {
168 static gboolean wxapp_idle_callback(gpointer)
169 {
170 return wxTheApp->DoIdle();
171 }
172 }
173
174 bool wxApp::DoIdle()
175 {
176 guint id_save;
177 {
178 // Allow another idle source to be added while this one is busy.
179 // Needed if an idle event handler runs a new event loop,
180 // for example by showing a dialog.
181 #if wxUSE_THREADS
182 wxMutexLocker lock(*m_idleMutex);
183 #endif
184 id_save = m_idleSourceId;
185 m_idleSourceId = 0;
186 wx_add_idle_hooks();
187 #ifdef __WXDEBUG__
188 // don't generate the idle events while the assert modal dialog is shown,
189 // this matches the behavior of wxMSW
190 if (m_isInAssert)
191 return false;
192 #endif
193 }
194
195 gdk_threads_enter();
196 bool needMore;
197 do {
198 needMore = ProcessIdle();
199 } while (needMore && gtk_events_pending() == 0);
200 gdk_threads_leave();
201
202 #if wxUSE_THREADS
203 wxMutexLocker lock(*m_idleMutex);
204 #endif
205 // if a new idle source was added during ProcessIdle
206 if (m_idleSourceId != 0)
207 {
208 // remove it
209 g_source_remove(m_idleSourceId);
210 m_idleSourceId = 0;
211 }
212 // if more idle processing requested
213 if (needMore)
214 {
215 // keep this source installed
216 m_idleSourceId = id_save;
217 return true;
218 }
219 // add hooks and remove this source
220 wx_add_idle_hooks();
221 return false;
222 }
223
224 #if wxUSE_THREADS
225
226 static GPollFunc wxgs_poll_func;
227
228 extern "C" {
229 static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout )
230 {
231 gdk_threads_enter();
232
233 wxMutexGuiLeave();
234 g_mainThreadLocked = true;
235
236 gint res = (*wxgs_poll_func)(ufds, nfds, timeout);
237
238 wxMutexGuiEnter();
239 g_mainThreadLocked = false;
240
241 gdk_threads_leave();
242
243 return res;
244 }
245 }
246
247 #endif // wxUSE_THREADS
248
249 //-----------------------------------------------------------------------------
250 // Access to the root window global
251 //-----------------------------------------------------------------------------
252
253 GtkWidget* wxGetRootWindow()
254 {
255 if (gs_RootWindow == NULL)
256 {
257 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
258 gtk_widget_realize( gs_RootWindow );
259 }
260 return gs_RootWindow;
261 }
262
263 //-----------------------------------------------------------------------------
264 // wxApp
265 //-----------------------------------------------------------------------------
266
267 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
268
269 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
270 EVT_IDLE(wxAppBase::OnIdle)
271 END_EVENT_TABLE()
272
273 wxApp::wxApp()
274 {
275 #ifdef __WXDEBUG__
276 m_isInAssert = false;
277 #endif // __WXDEBUG__
278 #if wxUSE_THREADS
279 m_idleMutex = NULL;
280 #endif
281 m_idleSourceId = 0;
282 }
283
284 wxApp::~wxApp()
285 {
286 }
287
288 bool wxApp::OnInitGui()
289 {
290 if ( !wxAppBase::OnInitGui() )
291 return false;
292
293 // if this is a wxGLApp (derived from wxApp), and we've already
294 // chosen a specific visual, then derive the GdkVisual from that
295 if ( GetXVisualInfo() )
296 {
297 GdkVisual* vis = gtk_widget_get_default_visual();
298
299 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
300 gtk_widget_set_default_colormap( colormap );
301 }
302 else
303 {
304 // On some machines, the default visual is just 256 colours, so
305 // we make sure we get the best. This can sometimes be wasteful.
306 if (m_useBestVisual)
307 {
308 if (m_forceTrueColour)
309 {
310 GdkVisual* visual = gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR );
311 if (!visual)
312 {
313 wxLogError(wxT("Unable to initialize TrueColor visual."));
314 return false;
315 }
316 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
317 gtk_widget_set_default_colormap( colormap );
318 }
319 else
320 {
321 if (gdk_visual_get_best() != gdk_visual_get_system())
322 {
323 GdkVisual* visual = gdk_visual_get_best();
324 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
325 gtk_widget_set_default_colormap( colormap );
326 }
327 }
328 }
329 }
330
331 return true;
332 }
333
334 GdkVisual *wxApp::GetGdkVisual()
335 {
336 GdkVisual *visual = NULL;
337
338 XVisualInfo *xvi = (XVisualInfo *)GetXVisualInfo();
339 if ( xvi )
340 visual = gdkx_visual_get( xvi->visualid );
341 else
342 visual = gdk_drawable_get_visual( wxGetRootWindow()->window );
343
344 wxASSERT( visual );
345
346 return visual;
347 }
348
349 bool wxApp::Initialize(int& argc, wxChar **argv)
350 {
351 if ( !wxAppBase::Initialize(argc, argv) )
352 return false;
353
354 #if wxUSE_THREADS
355 if (!g_thread_supported())
356 g_thread_init(NULL);
357
358 wxgs_poll_func = g_main_context_get_poll_func(NULL);
359 g_main_context_set_poll_func(NULL, wxapp_poll_func);
360 #endif // wxUSE_THREADS
361
362 // We should have the wxUSE_WCHAR_T test on the _outside_
363 #if wxUSE_WCHAR_T
364 // gtk+ 2.0 supports Unicode through UTF-8 strings
365 wxConvCurrent = &wxConvUTF8;
366 #else // !wxUSE_WCHAR_T
367 if (!wxOKlibc())
368 wxConvCurrent = (wxMBConv*) NULL;
369 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
370
371 // decide which conversion to use for the file names
372
373 // (1) this variable exists for the sole purpose of specifying the encoding
374 // of the filenames for GTK+ programs, so use it if it is set
375 wxString encName(wxGetenv(_T("G_FILENAME_ENCODING")));
376 encName = encName.BeforeFirst(_T(','));
377 if (encName.CmpNoCase(_T("@locale")) == 0)
378 encName.clear();
379 encName.MakeUpper();
380 #if wxUSE_INTL
381 if (encName.empty())
382 {
383 // (2) if a non default locale is set, assume that the user wants his
384 // filenames in this locale too
385 encName = wxLocale::GetSystemEncodingName().Upper();
386 // (3) finally use UTF-8 by default
387 if (encName.empty() || encName == _T("US-ASCII"))
388 encName = _T("UTF-8");
389 wxSetEnv(_T("G_FILENAME_ENCODING"), encName);
390 }
391 #else
392 if (encName.empty())
393 encName = _T("UTF-8");
394
395 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
396 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
397 // from gtk_init_check() as it does by default
398 gtk_disable_setlocale();
399
400 #endif // wxUSE_INTL
401 static wxConvBrokenFileNames fileconv(encName);
402 wxConvFileName = &fileconv;
403
404
405 bool init_result;
406
407 #if wxUSE_UNICODE
408 // gtk_init() wants UTF-8, not wchar_t, so convert
409 int i;
410 char **argvGTK = new char *[argc + 1];
411 for ( i = 0; i < argc; i++ )
412 {
413 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv[i]));
414 }
415
416 argvGTK[argc] = NULL;
417
418 int argcGTK = argc;
419
420 #ifdef __WXGPE__
421 init_result = true; // is there a _check() version of this?
422 gpe_application_init( &argcGTK, &argvGTK );
423 #else
424 init_result = gtk_init_check( &argcGTK, &argvGTK );
425 #endif
426
427 if ( argcGTK != argc )
428 {
429 // we have to drop the parameters which were consumed by GTK+
430 for ( i = 0; i < argcGTK; i++ )
431 {
432 while ( strcmp(wxConvUTF8.cWX2MB(argv[i]), argvGTK[i]) != 0 )
433 {
434 memmove(argv + i, argv + i + 1, (argc - i)*sizeof(*argv));
435 }
436 }
437
438 argc = argcGTK;
439 }
440 //else: gtk_init() didn't modify our parameters
441
442 // free our copy
443 for ( i = 0; i < argcGTK; i++ )
444 {
445 free(argvGTK[i]);
446 }
447
448 delete [] argvGTK;
449 #else // !wxUSE_UNICODE
450 // gtk_init() shouldn't actually change argv itself (just its contents) so
451 // it's ok to pass pointer to it
452 init_result = gtk_init_check( &argc, &argv );
453 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
454
455 // update internal arg[cv] as GTK+ may have removed processed options:
456 this->argc = argc;
457 this->argv = argv;
458
459 if ( !init_result )
460 {
461 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
462 return false;
463 }
464
465 // we can not enter threads before gtk_init is done
466 gdk_threads_enter();
467
468 wxSetDetectableAutoRepeat( true );
469
470 #if wxUSE_INTL
471 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
472 #endif
473
474 #if wxUSE_THREADS
475 m_idleMutex = new wxMutex;
476 #endif
477 // make sure GtkWidget type is loaded, idle hooks need it
478 g_type_class_ref(GTK_TYPE_WIDGET);
479 WakeUpIdle();
480
481 return true;
482 }
483
484 void wxApp::CleanUp()
485 {
486 if (m_idleSourceId != 0)
487 g_source_remove(m_idleSourceId);
488 #if wxUSE_THREADS
489 delete m_idleMutex;
490 m_idleMutex = NULL;
491 #endif
492 // release reference acquired by Initialize()
493 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET));
494
495 gdk_threads_leave();
496
497 wxAppBase::CleanUp();
498 }
499
500 void wxApp::WakeUpIdle()
501 {
502 #if wxUSE_THREADS
503 wxMutexLocker lock(*m_idleMutex);
504 #endif
505 if (m_idleSourceId == 0)
506 m_idleSourceId = g_idle_add_full(G_PRIORITY_LOW, wxapp_idle_callback, NULL, NULL);
507 }
508
509 // Checking for pending events requires first removing our idle source,
510 // otherwise it will cause the check to always return true.
511 bool wxApp::EventsPending()
512 {
513 #if wxUSE_THREADS
514 wxMutexLocker lock(*m_idleMutex);
515 #endif
516 if (m_idleSourceId != 0)
517 {
518 g_source_remove(m_idleSourceId);
519 m_idleSourceId = 0;
520 wx_add_idle_hooks();
521 }
522 return gtk_events_pending() != 0;
523 }
524
525 #ifdef __WXDEBUG__
526
527 void wxApp::OnAssertFailure(const wxChar *file,
528 int line,
529 const wxChar* func,
530 const wxChar* cond,
531 const wxChar *msg)
532 {
533
534 // block wx idle events while assert dialog is showing
535 m_isInAssert = true;
536
537 wxAppBase::OnAssertFailure(file, line, func, cond, msg);
538
539 m_isInAssert = false;
540 }
541
542 #endif // __WXDEBUG__