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