]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/app.cpp
Trace module initialization and cleanup.
[wxWidgets.git] / src / gtk1 / app.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
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
df744f4d 10#ifdef __VMS
f3858bf5
JJ
11// vms_jackets.h should for proper working be included before anything else
12# include <vms_jackets.h>
ff7d1dcb 13#undef ConnectionNumber
df744f4d
JJ
14#endif
15
f3858bf5
JJ
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
c801d85f
KB
19#include "wx/app.h"
20#include "wx/gdicmn.h"
21#include "wx/utils.h"
c801d85f
KB
22#include "wx/intl.h"
23#include "wx/log.h"
46dc76ba 24#include "wx/memory.h"
a3622daa
VZ
25#include "wx/font.h"
26#include "wx/settings.h"
0d2a2b60 27#include "wx/dialog.h"
94a6f0f8 28#include "wx/msgdlg.h"
b1ac3b56 29#include "wx/file.h"
2b5f62a0 30#include "wx/filename.h"
031b2a7b 31#include "wx/module.h"
4bc67cc5 32#include "wx/image.h"
fe593cc5 33#include "wx/thread.h"
afb74891 34
62be94e1
RR
35#ifdef __WXGPE__
36#include <gpe/init.h>
37#endif
38
df028524
VS
39#ifdef __WXUNIVERSAL__
40 #include "wx/univ/theme.h"
41 #include "wx/univ/renderer.h"
42#endif
43
91b8de8d 44#if wxUSE_THREADS
a37a5a73 45 #include "wx/thread.h"
91b8de8d 46#endif
afb74891 47
20e05ffb 48#include <unistd.h>
2b5f62a0
VZ
49
50#ifdef HAVE_POLL
51 #if defined(__VMS)
52 #include <poll.h>
53 #else
54 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
55 // in neither poll.h nor sys/poll.h which results in link errors later
56 #ifdef __OPENBSD__
57 extern "C"
58 {
59 #endif
60
61 #include <sys/poll.h>
62
63 #ifdef __OPENBSD__
64 };
65 #endif
66 #endif // platform
67#else // !HAVE_POLL
68 // we implement poll() ourselves using select() which is supposed exist in
69 // all modern Unices
70 #include <sys/types.h>
71 #include <sys/time.h>
72 #include <unistd.h>
73#endif // HAVE_POLL/!HAVE_POLL
74
e8106239 75#include "wx/gtk/win_gtk.h"
c801d85f 76
20e05ffb 77#include <gtk/gtk.h>
24178e4a 78
c801d85f
KB
79//-----------------------------------------------------------------------------
80// global data
81//-----------------------------------------------------------------------------
82
96997d65
RR
83bool g_mainThreadLocked = FALSE;
84gint g_pendingTag = 0;
3ac8d3bc 85
c2fa61e8 86static GtkWidget *gs_RootWindow = (GtkWidget*) NULL;
d76fe38b 87
c801d85f 88//-----------------------------------------------------------------------------
3133cb9f 89// idle system
c801d85f
KB
90//-----------------------------------------------------------------------------
91
3133cb9f 92extern bool g_isIdle;
c4fda16b 93
90350682 94void wxapp_install_idle_handler();
bf9e3e73 95
fe593cc5
VS
96#if wxUSE_THREADS
97static wxMutex gs_idleTagsMutex;
98#endif
99
bf9e3e73
RR
100//-----------------------------------------------------------------------------
101// wxYield
102//-----------------------------------------------------------------------------
53a8af59 103
1ee339ee
VZ
104// not static because used by textctrl.cpp
105//
106// MT-FIXME
107bool wxIsInsideYield = FALSE;
108
8461e4c2 109bool wxApp::Yield(bool onlyIfNeeded)
c801d85f 110{
1ee339ee 111 if ( wxIsInsideYield )
8461e4c2
VZ
112 {
113 if ( !onlyIfNeeded )
114 {
115 wxFAIL_MSG( wxT("wxYield called recursively" ) );
116 }
117
118 return FALSE;
119 }
120
6dc5fd71
VZ
121#if wxUSE_THREADS
122 if ( !wxThread::IsMain() )
123 {
124 // can't call gtk_main_iteration() from other threads like this
125 return TRUE;
126 }
127#endif // wxUSE_THREADS
128
1ee339ee 129 wxIsInsideYield = TRUE;
e90c1d2a 130
c263eb03
VS
131 // We need to remove idle callbacks or the loop will
132 // never finish.
133 wxTheApp->RemoveIdleTag();
acfd422a 134
dd7641ef 135#if wxUSE_LOG
2ed3265e
VZ
136 // disable log flushing from here because a call to wxYield() shouldn't
137 // normally result in message boxes popping up &c
138 wxLog::Suspend();
dd7641ef 139#endif
2ed3265e 140
406a6f6b
VZ
141 while (gtk_events_pending())
142 gtk_main_iteration();
143
5375a1f5
RR
144 // It's necessary to call ProcessIdle() to update the frames sizes which
145 // might have been changed (it also will update other things set from
be88a6ad 146 // OnUpdateUI() which is a nice (and desired) side effect). But we
5375a1f5
RR
147 // call ProcessIdle() only once since this is not meant for longish
148 // background jobs (controlled by wxIdleEvent::RequestMore() and the
149 // return value of Processidle().
150 ProcessIdle();
2ed3265e 151
dd7641ef 152#if wxUSE_LOG
2ed3265e
VZ
153 // let the logs be flashed again
154 wxLog::Resume();
dd7641ef 155#endif
7741c4e1 156
1ee339ee 157 wxIsInsideYield = FALSE;
99ba739f 158
acfd422a
RR
159 return TRUE;
160}
161
bf9e3e73
RR
162//-----------------------------------------------------------------------------
163// wxWakeUpIdle
164//-----------------------------------------------------------------------------
165
19722331
KH
166// RR/KH: The wxMutexGui calls are not needed on GTK2 according to
167// the GTK faq, http://www.gtk.org/faq/#AEN500
168// The calls to gdk_threads_enter() and leave() are specifically noted
169// as not being necessary. The MutexGui calls are still left in for GTK1.
170// Eliminating the MutexGui calls fixes the long-standing "random" lockup
171// when using wxPostEvent (which calls WakeUpIdle) from a thread.
172
e2478fde 173void wxApp::WakeUpIdle()
bf9e3e73 174{
19722331 175#ifndef __WXGTK20__
924ef850
RR
176#if wxUSE_THREADS
177 if (!wxThread::IsMain())
ce6d2511 178 wxMutexGuiEnter();
19722331
KH
179#endif // wxUSE_THREADS_
180#endif // __WXGTK2__
924ef850 181
c263eb03 182 wxapp_install_idle_handler();
2286341c 183
19722331 184#ifndef __WXGTK20__
924ef850
RR
185#if wxUSE_THREADS
186 if (!wxThread::IsMain())
ce6d2511 187 wxMutexGuiLeave();
19722331
KH
188#endif // wxUSE_THREADS_
189#endif // __WXGTK2__
bf9e3e73
RR
190}
191
192//-----------------------------------------------------------------------------
193// local functions
194//-----------------------------------------------------------------------------
195
90350682
VZ
196// the callback functions must be extern "C" to comply with GTK+ declarations
197extern "C"
198{
199
3133cb9f 200static gint wxapp_pending_callback( gpointer WXUNUSED(data) )
acfd422a
RR
201{
202 if (!wxTheApp) return TRUE;
c2fa61e8 203
5375a1f5 204 // When getting called from GDK's time-out handler
96997d65 205 // we are no longer within GDK's grab on the GUI
5375a1f5 206 // thread so we must lock it here ourselves.
96997d65
RR
207 gdk_threads_enter();
208
5375a1f5 209 // Sent idle event to all who request them.
96997d65 210 wxTheApp->ProcessPendingEvents();
e90c1d2a 211
fe593cc5
VS
212 {
213#if wxUSE_THREADS
214 wxMutexLocker lock(gs_idleTagsMutex);
215#endif
216 g_pendingTag = 0;
217 }
96997d65 218
5375a1f5 219 // Flush the logged messages if any.
1b2dab34
RR
220#if wxUSE_LOG
221 wxLog::FlushActive();
222#endif // wxUSE_LOG
223
96997d65
RR
224 // Release lock again
225 gdk_threads_leave();
226
227 // Return FALSE to indicate that no more idle events are
228 // to be sent (single shot instead of continuous stream)
229 return FALSE;
230}
231
3133cb9f 232static gint wxapp_idle_callback( gpointer WXUNUSED(data) )
96997d65 233{
a5f1fd3e
VZ
234 if (!wxTheApp)
235 return TRUE;
236
237#ifdef __WXDEBUG__
6d477bb4
VZ
238 // don't generate the idle events while the assert modal dialog is shown,
239 // this completely confuses the apps which don't expect to be reentered
240 // from some safely-looking functions
a5f1fd3e
VZ
241 if ( wxTheApp->IsInAssert() )
242 {
94a6f0f8
JS
243 // But repaint the assertion message if necessary
244 if (wxTopLevelWindows.GetCount() > 0)
245 {
b1d4dd7a 246 wxWindow* win = (wxWindow*) wxTopLevelWindows.GetLast()->GetData();
13a7abf9
VS
247#ifdef __WXGTK20__
248 if (win->IsKindOf(CLASSINFO(wxMessageDialog)))
249#else
94a6f0f8 250 if (win->IsKindOf(CLASSINFO(wxGenericMessageDialog)))
13a7abf9 251#endif
94a6f0f8
JS
252 win->OnInternalIdle();
253 }
6d477bb4 254 return TRUE;
a5f1fd3e
VZ
255 }
256#endif // __WXDEBUG__
c2fa61e8 257
5375a1f5 258 // When getting called from GDK's time-out handler
924ef850 259 // we are no longer within GDK's grab on the GUI
5375a1f5 260 // thread so we must lock it here ourselves.
924ef850 261 gdk_threads_enter();
094637f6 262
5375a1f5
RR
263 // Indicate that we are now in idle mode and event handlers
264 // will have to reinstall the idle handler again.
fe593cc5
VS
265 {
266#if wxUSE_THREADS
267 wxMutexLocker lock(gs_idleTagsMutex);
268#endif
269 g_isIdle = TRUE;
270 wxTheApp->m_idleTag = 0;
271 }
094637f6 272
3133cb9f 273 // Send idle event to all who request them as long as
5375a1f5
RR
274 // no events have popped up in the event queue.
275 while (wxTheApp->ProcessIdle() && (gtk_events_pending() == 0))
6d477bb4 276 ;
c9e35272 277
96997d65 278 // Release lock again
924ef850 279 gdk_threads_leave();
acfd422a 280
96997d65 281 // Return FALSE to indicate that no more idle events are
5375a1f5 282 // to be sent (single shot instead of continuous stream).
96997d65 283 return FALSE;
acfd422a 284}
8801832d 285
90350682
VZ
286#if wxUSE_THREADS
287
2b5f62a0
VZ
288#ifdef HAVE_POLL
289 #define wxPoll poll
290 #define wxPollFd pollfd
291#else // !HAVE_POLL
292
293typedef GPollFD wxPollFd;
294
295int wxPoll(wxPollFd *ufds, unsigned int nfds, int timeout)
296{
297 // convert timeout from ms to struct timeval (s/us)
298 timeval tv_timeout;
299 tv_timeout.tv_sec = timeout/1000;
300 tv_timeout.tv_usec = (timeout%1000)*1000;
301
302 // remember the highest fd used here
303 int fdMax = -1;
304
305 // and fill the sets for select()
306 fd_set readfds;
307 fd_set writefds;
308 fd_set exceptfds;
309 FD_ZERO(&readfds);
310 FD_ZERO(&writefds);
311 FD_ZERO(&exceptfds);
312
313 unsigned int i;
314 for ( i = 0; i < nfds; i++ )
315 {
316 wxASSERT_MSG( ufds[i].fd < FD_SETSIZE, _T("fd out of range") );
317
318 if ( ufds[i].events & G_IO_IN )
319 FD_SET(ufds[i].fd, &readfds);
320
321 if ( ufds[i].events & G_IO_PRI )
322 FD_SET(ufds[i].fd, &exceptfds);
323
324 if ( ufds[i].events & G_IO_OUT )
325 FD_SET(ufds[i].fd, &writefds);
326
327 if ( ufds[i].fd > fdMax )
328 fdMax = ufds[i].fd;
329 }
330
331 fdMax++;
332 int res = select(fdMax, &readfds, &writefds, &exceptfds, &tv_timeout);
333
334 // translate the results back
335 for ( i = 0; i < nfds; i++ )
336 {
337 ufds[i].revents = 0;
338
339 if ( FD_ISSET(ufds[i].fd, &readfds ) )
340 ufds[i].revents |= G_IO_IN;
341
342 if ( FD_ISSET(ufds[i].fd, &exceptfds ) )
343 ufds[i].revents |= G_IO_PRI;
344
345 if ( FD_ISSET(ufds[i].fd, &writefds ) )
346 ufds[i].revents |= G_IO_OUT;
347 }
348
349 return res;
350}
351
352#endif // HAVE_POLL/!HAVE_POLL
353
3133cb9f 354static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout )
acfd422a 355{
90350682 356 gdk_threads_enter();
7b90a8f2 357
90350682 358 wxMutexGuiLeave();
90350682 359 g_mainThreadLocked = TRUE;
7941ba11 360
2b5f62a0
VZ
361 // we rely on the fact that glib GPollFD struct is really just pollfd but
362 // I wonder how wise is this in the long term (VZ)
363 gint res = wxPoll( (wxPollFd *) ufds, nfds, timeout );
90350682 364
90350682 365 wxMutexGuiEnter();
90350682
VZ
366 g_mainThreadLocked = FALSE;
367
90350682
VZ
368 gdk_threads_leave();
369
3133cb9f 370 return res;
ff7b1510 371}
c801d85f 372
90350682
VZ
373#endif // wxUSE_THREADS
374
375} // extern "C"
376
3133cb9f 377void wxapp_install_idle_handler()
b453e1b2 378{
fe593cc5
VS
379#if wxUSE_THREADS
380 wxMutexLocker lock(gs_idleTagsMutex);
381#endif
382
c263eb03
VS
383 // Don't install the handler if it's already installed. This test *MUST*
384 // be done when gs_idleTagsMutex is locked!
385 if (!g_isIdle)
386 return;
387
e8617760
GD
388 // GD: this assert is raised when using the thread sample (which works)
389 // so the test is probably not so easy. Can widget callbacks be
390 // triggered from child threads and, if so, for which widgets?
391 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
d5f3e1eb 392
3133cb9f 393 wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") );
c2fa61e8 394
3133cb9f 395 g_isIdle = FALSE;
96997d65 396
3133cb9f
RR
397 if (g_pendingTag == 0)
398 g_pendingTag = gtk_idle_add_priority( 900, wxapp_pending_callback, (gpointer) NULL );
e90c1d2a 399
3133cb9f
RR
400 // This routine gets called by all event handlers
401 // indicating that the idle is over. It may also
402 // get called from other thread for sending events
403 // to the main thread (and processing these in
404 // idle time). Very low priority.
405 wxTheApp->m_idleTag = gtk_idle_add_priority( 1000, wxapp_idle_callback, (gpointer) NULL );
b453e1b2
RR
406}
407
005f5d18
RR
408//-----------------------------------------------------------------------------
409// Access to the root window global
410//-----------------------------------------------------------------------------
411
412GtkWidget* wxGetRootWindow()
413{
414 if (gs_RootWindow == NULL)
415 {
416 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
417 gtk_widget_realize( gs_RootWindow );
418 }
419 return gs_RootWindow;
420}
421
c801d85f
KB
422//-----------------------------------------------------------------------------
423// wxApp
424//-----------------------------------------------------------------------------
425
426IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
427
53010e52 428BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
955a9197 429 EVT_IDLE(wxAppBase::OnIdle)
53010e52
RR
430END_EVENT_TABLE()
431
c801d85f
KB
432wxApp::wxApp()
433{
a5f1fd3e
VZ
434#ifdef __WXDEBUG__
435 m_isInAssert = FALSE;
436#endif // __WXDEBUG__
437
df5ddbca 438 m_idleTag = 0;
c263eb03 439 g_isIdle = TRUE;
df5ddbca 440 wxapp_install_idle_handler();
094637f6 441
6b3eb77a 442#if wxUSE_THREADS
3133cb9f 443 g_main_set_poll_func( wxapp_poll_func );
6b3eb77a 444#endif
60acb947 445
f6fcbb63 446 m_colorCube = (unsigned char*) NULL;
be88a6ad 447
a6f5aa49
VZ
448 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
449 m_glVisualInfo = (void *) NULL;
34a34b02 450 m_glFBCInfo = (void *) NULL;
ff7b1510 451}
c801d85f 452
60acb947 453wxApp::~wxApp()
c801d85f 454{
acfd422a 455 if (m_idleTag) gtk_idle_remove( m_idleTag );
60acb947 456
f6fcbb63 457 if (m_colorCube) free(m_colorCube);
ff7b1510 458}
c801d85f 459
0d2a2b60 460bool wxApp::OnInitGui()
c801d85f 461{
1e6feb95
VZ
462 if ( !wxAppBase::OnInitGui() )
463 return FALSE;
464
b134516c
RR
465 GdkVisual *visual = gdk_visual_get_system();
466
a6f5aa49
VZ
467 // if this is a wxGLApp (derived from wxApp), and we've already
468 // chosen a specific visual, then derive the GdkVisual from that
005f5d18
RR
469 if (m_glVisualInfo != NULL)
470 {
a6f5aa49 471#ifdef __WXGTK20__
005f5d18 472 // seems gtk_widget_set_default_visual no longer exists?
a6f5aa49
VZ
473 GdkVisual* vis = gtk_widget_get_default_visual();
474#else
be88a6ad 475 GdkVisual* vis = gdkx_visual_get(
a6f5aa49
VZ
476 ((XVisualInfo *) m_glVisualInfo) ->visualid );
477 gtk_widget_set_default_visual( vis );
478#endif
479
480 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
481 gtk_widget_set_default_colormap( colormap );
482
483 visual = vis;
484 }
be88a6ad 485
005f5d18
RR
486 // On some machines, the default visual is just 256 colours, so
487 // we make sure we get the best. This can sometimes be wasteful.
2286341c 488
005f5d18
RR
489 else
490 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual))
b134516c 491 {
2d4dc3a4
OK
492#ifdef __WXGTK20__
493 /* seems gtk_widget_set_default_visual no longer exists? */
494 GdkVisual* vis = gtk_widget_get_default_visual();
495#else
b134516c
RR
496 GdkVisual* vis = gdk_visual_get_best();
497 gtk_widget_set_default_visual( vis );
2d4dc3a4 498#endif
f6fcbb63 499
b134516c
RR
500 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
501 gtk_widget_set_default_colormap( colormap );
094637f6
VZ
502
503 visual = vis;
b134516c 504 }
d76fe38b 505
005f5d18 506 // Nothing to do for 15, 16, 24, 32 bit displays
f6fcbb63 507 if (visual->depth > 8) return TRUE;
60acb947 508
005f5d18 509 // initialize color cube for 8-bit color reduction dithering
60acb947 510
f6fcbb63 511 GdkColormap *cmap = gtk_widget_get_default_colormap();
60acb947 512
f6fcbb63
RR
513 m_colorCube = (unsigned char*)malloc(32 * 32 * 32);
514
f03fc89f
VZ
515 for (int r = 0; r < 32; r++)
516 {
8801832d
VZ
517 for (int g = 0; g < 32; g++)
518 {
519 for (int b = 0; b < 32; b++)
520 {
521 int rr = (r << 3) | (r >> 2);
522 int gg = (g << 3) | (g >> 2);
523 int bb = (b << 3) | (b >> 2);
60acb947 524
f03fc89f
VZ
525 int index = -1;
526
f6fcbb63 527 GdkColor *colors = cmap->colors;
ca26177c 528 if (colors)
f03fc89f
VZ
529 {
530 int max = 3 * 65536;
531
532 for (int i = 0; i < cmap->size; i++)
533 {
534 int rdiff = ((rr << 8) - colors[i].red);
535 int gdiff = ((gg << 8) - colors[i].green);
536 int bdiff = ((bb << 8) - colors[i].blue);
537 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
538 if (sum < max)
094637f6 539 {
f03fc89f
VZ
540 index = i; max = sum;
541 }
542 }
f6fcbb63 543 }
094637f6
VZ
544 else
545 {
005f5d18 546 // assume 8-bit true or static colors. this really exists
094637f6
VZ
547 GdkVisual* vis = gdk_colormap_get_visual( cmap );
548 index = (r >> (5 - vis->red_prec)) << vis->red_shift;
549 index |= (g >> (5 - vis->green_prec)) << vis->green_shift;
550 index |= (b >> (5 - vis->blue_prec)) << vis->blue_shift;
094637f6 551 }
8801832d
VZ
552 m_colorCube[ (r*1024) + (g*32) + b ] = index;
553 }
554 }
f6fcbb63 555 }
c801d85f 556
bbe0af5b
RR
557 return TRUE;
558}
559
005f5d18
RR
560GdkVisual *wxApp::GetGdkVisual()
561{
562 GdkVisual *visual = NULL;
be88a6ad 563
005f5d18 564 if (m_glVisualInfo)
7b775074 565 visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid );
005f5d18
RR
566 else
567 visual = gdk_window_get_visual( wxGetRootWindow()->window );
be88a6ad 568
005f5d18 569 wxASSERT( visual );
be88a6ad 570
005f5d18
RR
571 return visual;
572}
573
abca8ebf 574bool wxApp::Initialize(int& argc, wxChar **argv)
c801d85f 575{
c156411a
RD
576 bool init_result;
577
924ef850 578#if wxUSE_THREADS
005f5d18
RR
579 // GTK 1.2 up to version 1.2.3 has broken threads
580 if ((gtk_major_version == 1) &&
8f75cb6c 581 (gtk_minor_version == 2) &&
2286341c 582 (gtk_micro_version < 4))
96997d65 583 {
77ffb593 584 printf( "wxWidgets warning: GUI threading disabled due to outdated GTK version\n" );
8f75cb6c
RR
585 }
586 else
587 {
863136de
RR
588 if (!g_thread_supported())
589 g_thread_init(NULL);
8f75cb6c 590 }
05e2b077 591#endif // wxUSE_THREADS
2286341c 592
0d2a2b60 593 gtk_set_locale();
c801d85f 594
f9862abd
JS
595 // We should have the wxUSE_WCHAR_T test on the _outside_
596#if wxUSE_WCHAR_T
05e2b077
VZ
597 #if defined(__WXGTK20__)
598 // gtk+ 2.0 supports Unicode through UTF-8 strings
599 wxConvCurrent = &wxConvUTF8;
600 #else // GTK 1.x
601 if (!wxOKlibc())
602 wxConvCurrent = &wxConvLocal;
603 #endif
604#else // !wxUSE_WCHAR_T
605 if (!wxOKlibc())
606 wxConvCurrent = (wxMBConv*) NULL;
607#endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
002f4218 608
66bf0099 609#ifdef __WXGTK20__
845905d5
MW
610 // decide which conversion to use for the file names
611
612 // (1) this variable exists for the sole purpose of specifying the encoding
613 // of the filenames for GTK+ programs, so use it if it is set
614 wxString encName(wxGetenv(_T("G_FILENAME_ENCODING")));
29c326b7 615 encName = encName.BeforeFirst(_T(','));
d24b23b7
MW
616 if (encName == _T("@locale"))
617 encName.clear();
845905d5
MW
618 encName.MakeUpper();
619#if wxUSE_INTL
620 if (encName.empty())
621 {
622 // (2) if a non default locale is set, assume that the user wants his
623 // filenames in this locale too
624 encName = wxLocale::GetSystemEncodingName().Upper();
625 // (3) finally use UTF-8 by default
626 if (encName.empty() || encName == _T("US-ASCII"))
627 encName = _T("UTF-8");
628 wxSetEnv(_T("G_FILENAME_ENCODING"), encName);
629 }
630#else
631 if (encName.empty())
632 encName = _T("UTF-8");
633#endif // wxUSE_INTL
634 static wxConvBrokenFileNames fileconv(encName);
635 wxConvFileName = &fileconv;
636#endif // __WXGTK20__
66bf0099 637
05e2b077
VZ
638#if wxUSE_UNICODE
639 // gtk_init() wants UTF-8, not wchar_t, so convert
640 int i;
0d8dda32 641 char **argvGTK = new char *[argc + 1];
05e2b077 642 for ( i = 0; i < argc; i++ )
924ef850 643 {
05e2b077 644 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv[i]));
924ef850 645 }
0cf2cb36 646
05e2b077 647 argvGTK[argc] = NULL;
954de0f1 648
05e2b077 649 int argcGTK = argc;
62be94e1
RR
650
651#ifdef __WXGPE__
c156411a 652 init_result = true; // is there a _check() version of this?
62be94e1
RR
653 gpe_application_init( &argcGTK, &argvGTK );
654#else
c156411a 655 init_result = gtk_init_check( &argcGTK, &argvGTK );
62be94e1 656#endif
954de0f1 657
05e2b077 658 if ( argcGTK != argc )
924ef850 659 {
05e2b077
VZ
660 // we have to drop the parameters which were consumed by GTK+
661 for ( i = 0; i < argcGTK; i++ )
662 {
0d8dda32 663 while ( strcmp(wxConvUTF8.cWX2MB(argv[i]), argvGTK[i]) != 0 )
05e2b077
VZ
664 {
665 memmove(argv + i, argv + i + 1, argc - i);
666 }
667 }
0cf2cb36 668
05e2b077 669 argc = argcGTK;
2b5f62a0 670 }
05e2b077 671 //else: gtk_init() didn't modify our parameters
e0253070 672
05e2b077
VZ
673 // free our copy
674 for ( i = 0; i < argcGTK; i++ )
0151c3eb 675 {
05e2b077 676 free(argvGTK[i]);
0151c3eb 677 }
0cf2cb36 678
05e2b077
VZ
679 delete [] argvGTK;
680#else // !wxUSE_UNICODE
681 // gtk_init() shouldn't actually change argv itself (just its contents) so
682 // it's ok to pass pointer to it
c156411a 683 init_result = gtk_init_check( &argc, &argv );
05e2b077 684#endif // wxUSE_UNICODE/!wxUSE_UNICODE
0cf2cb36 685
c156411a
RD
686 if (!init_result) {
687 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
688 return false;
689 }
690
05e2b077
VZ
691 // we can not enter threads before gtk_init is done
692 gdk_threads_enter();
0cf2cb36 693
abca8ebf
VZ
694 if ( !wxAppBase::Initialize(argc, argv) )
695 {
696 gdk_threads_leave();
697
698 return false;
699 }
700
05e2b077 701 wxSetDetectableAutoRepeat( TRUE );
be88a6ad 702
05e2b077
VZ
703#if wxUSE_INTL
704 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
705#endif
be88a6ad 706
05e2b077
VZ
707 return true;
708}
0cf2cb36 709
05e2b077
VZ
710void wxApp::CleanUp()
711{
05e2b077 712 gdk_threads_leave();
abca8ebf
VZ
713
714 wxAppBase::CleanUp();
ff7b1510 715}
1a56f55c 716
a5f1fd3e
VZ
717#ifdef __WXDEBUG__
718
be88a6ad 719void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
a5f1fd3e
VZ
720{
721 m_isInAssert = TRUE;
722
be88a6ad 723 wxAppBase::OnAssert(file, line, cond, msg);
a5f1fd3e
VZ
724
725 m_isInAssert = FALSE;
726}
727
728#endif // __WXDEBUG__
729
fe593cc5
VS
730void wxApp::RemoveIdleTag()
731{
732#if wxUSE_THREADS
733 wxMutexLocker lock(gs_idleTagsMutex);
734#endif
c263eb03
VS
735 if (!g_isIdle)
736 {
737 gtk_idle_remove( wxTheApp->m_idleTag );
738 wxTheApp->m_idleTag = 0;
739 g_isIdle = TRUE;
740 }
fe593cc5 741}