]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/app.cpp
wx/wxprec.h already includes wx/defs.h (with other minor cleaning).
[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
17a1ebd1 75#include "wx/unix/private.h"
3cbab641 76#include "wx/gtk1/win_gtk.h"
c801d85f 77
20e05ffb 78#include <gtk/gtk.h>
24178e4a 79
c801d85f
KB
80//-----------------------------------------------------------------------------
81// global data
82//-----------------------------------------------------------------------------
83
96997d65
RR
84bool g_mainThreadLocked = FALSE;
85gint g_pendingTag = 0;
3ac8d3bc 86
c2fa61e8 87static GtkWidget *gs_RootWindow = (GtkWidget*) NULL;
d76fe38b 88
c801d85f 89//-----------------------------------------------------------------------------
3133cb9f 90// idle system
c801d85f
KB
91//-----------------------------------------------------------------------------
92
3133cb9f 93extern bool g_isIdle;
c4fda16b 94
90350682 95void wxapp_install_idle_handler();
bf9e3e73 96
fe593cc5
VS
97#if wxUSE_THREADS
98static wxMutex gs_idleTagsMutex;
99#endif
100
bf9e3e73
RR
101//-----------------------------------------------------------------------------
102// wxYield
103//-----------------------------------------------------------------------------
53a8af59 104
1ee339ee
VZ
105// not static because used by textctrl.cpp
106//
107// MT-FIXME
108bool wxIsInsideYield = FALSE;
109
8461e4c2 110bool wxApp::Yield(bool onlyIfNeeded)
c801d85f 111{
1ee339ee 112 if ( wxIsInsideYield )
8461e4c2
VZ
113 {
114 if ( !onlyIfNeeded )
115 {
116 wxFAIL_MSG( wxT("wxYield called recursively" ) );
117 }
118
119 return FALSE;
120 }
121
6dc5fd71
VZ
122#if wxUSE_THREADS
123 if ( !wxThread::IsMain() )
124 {
125 // can't call gtk_main_iteration() from other threads like this
126 return TRUE;
127 }
128#endif // wxUSE_THREADS
129
1ee339ee 130 wxIsInsideYield = TRUE;
e90c1d2a 131
c263eb03
VS
132 // We need to remove idle callbacks or the loop will
133 // never finish.
134 wxTheApp->RemoveIdleTag();
acfd422a 135
dd7641ef 136#if wxUSE_LOG
2ed3265e
VZ
137 // disable log flushing from here because a call to wxYield() shouldn't
138 // normally result in message boxes popping up &c
139 wxLog::Suspend();
dd7641ef 140#endif
2ed3265e 141
406a6f6b
VZ
142 while (gtk_events_pending())
143 gtk_main_iteration();
144
5375a1f5
RR
145 // It's necessary to call ProcessIdle() to update the frames sizes which
146 // might have been changed (it also will update other things set from
be88a6ad 147 // OnUpdateUI() which is a nice (and desired) side effect). But we
5375a1f5
RR
148 // call ProcessIdle() only once since this is not meant for longish
149 // background jobs (controlled by wxIdleEvent::RequestMore() and the
150 // return value of Processidle().
151 ProcessIdle();
2ed3265e 152
dd7641ef 153#if wxUSE_LOG
2ed3265e
VZ
154 // let the logs be flashed again
155 wxLog::Resume();
dd7641ef 156#endif
7741c4e1 157
1ee339ee 158 wxIsInsideYield = FALSE;
99ba739f 159
acfd422a
RR
160 return TRUE;
161}
162
bf9e3e73
RR
163//-----------------------------------------------------------------------------
164// wxWakeUpIdle
165//-----------------------------------------------------------------------------
166
19722331
KH
167// RR/KH: The wxMutexGui calls are not needed on GTK2 according to
168// the GTK faq, http://www.gtk.org/faq/#AEN500
169// The calls to gdk_threads_enter() and leave() are specifically noted
170// as not being necessary. The MutexGui calls are still left in for GTK1.
171// Eliminating the MutexGui calls fixes the long-standing "random" lockup
172// when using wxPostEvent (which calls WakeUpIdle) from a thread.
173
e2478fde 174void wxApp::WakeUpIdle()
bf9e3e73 175{
924ef850
RR
176#if wxUSE_THREADS
177 if (!wxThread::IsMain())
ce6d2511 178 wxMutexGuiEnter();
19722331 179#endif // wxUSE_THREADS_
924ef850 180
c263eb03 181 wxapp_install_idle_handler();
2286341c 182
924ef850
RR
183#if wxUSE_THREADS
184 if (!wxThread::IsMain())
ce6d2511 185 wxMutexGuiLeave();
19722331 186#endif // wxUSE_THREADS_
bf9e3e73
RR
187}
188
189//-----------------------------------------------------------------------------
190// local functions
191//-----------------------------------------------------------------------------
192
90350682
VZ
193// the callback functions must be extern "C" to comply with GTK+ declarations
194extern "C"
195{
196
3133cb9f 197static gint wxapp_pending_callback( gpointer WXUNUSED(data) )
acfd422a
RR
198{
199 if (!wxTheApp) return TRUE;
c2fa61e8 200
5375a1f5 201 // When getting called from GDK's time-out handler
96997d65 202 // we are no longer within GDK's grab on the GUI
5375a1f5 203 // thread so we must lock it here ourselves.
96997d65
RR
204 gdk_threads_enter();
205
5375a1f5 206 // Sent idle event to all who request them.
96997d65 207 wxTheApp->ProcessPendingEvents();
e90c1d2a 208
fe593cc5
VS
209 {
210#if wxUSE_THREADS
211 wxMutexLocker lock(gs_idleTagsMutex);
212#endif
213 g_pendingTag = 0;
214 }
96997d65 215
5375a1f5 216 // Flush the logged messages if any.
1b2dab34
RR
217#if wxUSE_LOG
218 wxLog::FlushActive();
219#endif // wxUSE_LOG
220
96997d65
RR
221 // Release lock again
222 gdk_threads_leave();
223
224 // Return FALSE to indicate that no more idle events are
225 // to be sent (single shot instead of continuous stream)
226 return FALSE;
227}
228
3133cb9f 229static gint wxapp_idle_callback( gpointer WXUNUSED(data) )
96997d65 230{
a5f1fd3e
VZ
231 if (!wxTheApp)
232 return TRUE;
233
234#ifdef __WXDEBUG__
6d477bb4
VZ
235 // don't generate the idle events while the assert modal dialog is shown,
236 // this completely confuses the apps which don't expect to be reentered
237 // from some safely-looking functions
a5f1fd3e
VZ
238 if ( wxTheApp->IsInAssert() )
239 {
94a6f0f8
JS
240 // But repaint the assertion message if necessary
241 if (wxTopLevelWindows.GetCount() > 0)
242 {
b1d4dd7a 243 wxWindow* win = (wxWindow*) wxTopLevelWindows.GetLast()->GetData();
94a6f0f8
JS
244 if (win->IsKindOf(CLASSINFO(wxGenericMessageDialog)))
245 win->OnInternalIdle();
246 }
6d477bb4 247 return TRUE;
a5f1fd3e
VZ
248 }
249#endif // __WXDEBUG__
c2fa61e8 250
5375a1f5 251 // When getting called from GDK's time-out handler
924ef850 252 // we are no longer within GDK's grab on the GUI
5375a1f5 253 // thread so we must lock it here ourselves.
924ef850 254 gdk_threads_enter();
094637f6 255
5375a1f5
RR
256 // Indicate that we are now in idle mode and event handlers
257 // will have to reinstall the idle handler again.
fe593cc5
VS
258 {
259#if wxUSE_THREADS
260 wxMutexLocker lock(gs_idleTagsMutex);
261#endif
262 g_isIdle = TRUE;
263 wxTheApp->m_idleTag = 0;
264 }
094637f6 265
3133cb9f 266 // Send idle event to all who request them as long as
5375a1f5
RR
267 // no events have popped up in the event queue.
268 while (wxTheApp->ProcessIdle() && (gtk_events_pending() == 0))
6d477bb4 269 ;
c9e35272 270
96997d65 271 // Release lock again
924ef850 272 gdk_threads_leave();
acfd422a 273
96997d65 274 // Return FALSE to indicate that no more idle events are
5375a1f5 275 // to be sent (single shot instead of continuous stream).
96997d65 276 return FALSE;
acfd422a 277}
8801832d 278
90350682
VZ
279#if wxUSE_THREADS
280
2b5f62a0
VZ
281#ifdef HAVE_POLL
282 #define wxPoll poll
283 #define wxPollFd pollfd
284#else // !HAVE_POLL
285
286typedef GPollFD wxPollFd;
287
288int wxPoll(wxPollFd *ufds, unsigned int nfds, int timeout)
289{
290 // convert timeout from ms to struct timeval (s/us)
291 timeval tv_timeout;
292 tv_timeout.tv_sec = timeout/1000;
293 tv_timeout.tv_usec = (timeout%1000)*1000;
294
295 // remember the highest fd used here
296 int fdMax = -1;
297
298 // and fill the sets for select()
299 fd_set readfds;
300 fd_set writefds;
301 fd_set exceptfds;
17a1ebd1
VZ
302 wxFD_ZERO(&readfds);
303 wxFD_ZERO(&writefds);
304 wxFD_ZERO(&exceptfds);
2b5f62a0
VZ
305
306 unsigned int i;
307 for ( i = 0; i < nfds; i++ )
308 {
17a1ebd1 309 wxASSERT_MSG( ufds[i].fd < wxFD_SETSIZE, _T("fd out of range") );
2b5f62a0
VZ
310
311 if ( ufds[i].events & G_IO_IN )
17a1ebd1 312 wxFD_SET(ufds[i].fd, &readfds);
2b5f62a0
VZ
313
314 if ( ufds[i].events & G_IO_PRI )
17a1ebd1 315 wxFD_SET(ufds[i].fd, &exceptfds);
2b5f62a0
VZ
316
317 if ( ufds[i].events & G_IO_OUT )
17a1ebd1 318 wxFD_SET(ufds[i].fd, &writefds);
2b5f62a0
VZ
319
320 if ( ufds[i].fd > fdMax )
321 fdMax = ufds[i].fd;
322 }
323
324 fdMax++;
325 int res = select(fdMax, &readfds, &writefds, &exceptfds, &tv_timeout);
326
327 // translate the results back
328 for ( i = 0; i < nfds; i++ )
329 {
330 ufds[i].revents = 0;
331
17a1ebd1 332 if ( wxFD_ISSET(ufds[i].fd, &readfds ) )
2b5f62a0
VZ
333 ufds[i].revents |= G_IO_IN;
334
17a1ebd1 335 if ( wxFD_ISSET(ufds[i].fd, &exceptfds ) )
2b5f62a0
VZ
336 ufds[i].revents |= G_IO_PRI;
337
17a1ebd1 338 if ( wxFD_ISSET(ufds[i].fd, &writefds ) )
2b5f62a0
VZ
339 ufds[i].revents |= G_IO_OUT;
340 }
341
342 return res;
343}
344
345#endif // HAVE_POLL/!HAVE_POLL
346
3133cb9f 347static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout )
acfd422a 348{
90350682 349 gdk_threads_enter();
7b90a8f2 350
90350682 351 wxMutexGuiLeave();
90350682 352 g_mainThreadLocked = TRUE;
7941ba11 353
2b5f62a0
VZ
354 // we rely on the fact that glib GPollFD struct is really just pollfd but
355 // I wonder how wise is this in the long term (VZ)
356 gint res = wxPoll( (wxPollFd *) ufds, nfds, timeout );
90350682 357
90350682 358 wxMutexGuiEnter();
90350682
VZ
359 g_mainThreadLocked = FALSE;
360
90350682
VZ
361 gdk_threads_leave();
362
3133cb9f 363 return res;
ff7b1510 364}
c801d85f 365
90350682
VZ
366#endif // wxUSE_THREADS
367
368} // extern "C"
369
3133cb9f 370void wxapp_install_idle_handler()
b453e1b2 371{
fe593cc5
VS
372#if wxUSE_THREADS
373 wxMutexLocker lock(gs_idleTagsMutex);
374#endif
375
c263eb03
VS
376 // Don't install the handler if it's already installed. This test *MUST*
377 // be done when gs_idleTagsMutex is locked!
378 if (!g_isIdle)
379 return;
380
e8617760
GD
381 // GD: this assert is raised when using the thread sample (which works)
382 // so the test is probably not so easy. Can widget callbacks be
383 // triggered from child threads and, if so, for which widgets?
384 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
d5f3e1eb 385
3133cb9f 386 wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") );
c2fa61e8 387
3133cb9f 388 g_isIdle = FALSE;
96997d65 389
3133cb9f
RR
390 if (g_pendingTag == 0)
391 g_pendingTag = gtk_idle_add_priority( 900, wxapp_pending_callback, (gpointer) NULL );
e90c1d2a 392
3133cb9f
RR
393 // This routine gets called by all event handlers
394 // indicating that the idle is over. It may also
395 // get called from other thread for sending events
396 // to the main thread (and processing these in
397 // idle time). Very low priority.
398 wxTheApp->m_idleTag = gtk_idle_add_priority( 1000, wxapp_idle_callback, (gpointer) NULL );
b453e1b2
RR
399}
400
005f5d18
RR
401//-----------------------------------------------------------------------------
402// Access to the root window global
403//-----------------------------------------------------------------------------
404
405GtkWidget* wxGetRootWindow()
406{
407 if (gs_RootWindow == NULL)
408 {
409 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
410 gtk_widget_realize( gs_RootWindow );
411 }
412 return gs_RootWindow;
413}
414
c801d85f
KB
415//-----------------------------------------------------------------------------
416// wxApp
417//-----------------------------------------------------------------------------
418
419IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
420
53010e52 421BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
955a9197 422 EVT_IDLE(wxAppBase::OnIdle)
53010e52
RR
423END_EVENT_TABLE()
424
c801d85f
KB
425wxApp::wxApp()
426{
a5f1fd3e
VZ
427#ifdef __WXDEBUG__
428 m_isInAssert = FALSE;
429#endif // __WXDEBUG__
430
df5ddbca 431 m_idleTag = 0;
c263eb03 432 g_isIdle = TRUE;
df5ddbca 433 wxapp_install_idle_handler();
094637f6 434
6b3eb77a 435#if wxUSE_THREADS
3133cb9f 436 g_main_set_poll_func( wxapp_poll_func );
6b3eb77a 437#endif
60acb947 438
f6fcbb63 439 m_colorCube = (unsigned char*) NULL;
be88a6ad 440
a6f5aa49
VZ
441 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
442 m_glVisualInfo = (void *) NULL;
34a34b02 443 m_glFBCInfo = (void *) NULL;
ff7b1510 444}
c801d85f 445
60acb947 446wxApp::~wxApp()
c801d85f 447{
acfd422a 448 if (m_idleTag) gtk_idle_remove( m_idleTag );
60acb947 449
f6fcbb63 450 if (m_colorCube) free(m_colorCube);
ff7b1510 451}
c801d85f 452
0d2a2b60 453bool wxApp::OnInitGui()
c801d85f 454{
1e6feb95
VZ
455 if ( !wxAppBase::OnInitGui() )
456 return FALSE;
457
b134516c
RR
458 GdkVisual *visual = gdk_visual_get_system();
459
a6f5aa49
VZ
460 // if this is a wxGLApp (derived from wxApp), and we've already
461 // chosen a specific visual, then derive the GdkVisual from that
005f5d18
RR
462 if (m_glVisualInfo != NULL)
463 {
be88a6ad 464 GdkVisual* vis = gdkx_visual_get(
a6f5aa49
VZ
465 ((XVisualInfo *) m_glVisualInfo) ->visualid );
466 gtk_widget_set_default_visual( vis );
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
RR
479 {
480 GdkVisual* vis = gdk_visual_get_best();
481 gtk_widget_set_default_visual( vis );
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
f6fcbb63 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
bbe0af5b
RR
540 return TRUE;
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
RR
549 else
550 visual = gdk_window_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;
3cbab641 560
924ef850 561#if wxUSE_THREADS
005f5d18
RR
562 // GTK 1.2 up to version 1.2.3 has broken threads
563 if ((gtk_major_version == 1) &&
8f75cb6c 564 (gtk_minor_version == 2) &&
2286341c 565 (gtk_micro_version < 4))
96997d65 566 {
77ffb593 567 printf( "wxWidgets warning: GUI threading disabled due to outdated GTK version\n" );
8f75cb6c
RR
568 }
569 else
570 {
863136de
RR
571 if (!g_thread_supported())
572 g_thread_init(NULL);
8f75cb6c 573 }
05e2b077 574#endif // wxUSE_THREADS
2286341c 575
0d2a2b60 576 gtk_set_locale();
c801d85f 577
f9862abd
JS
578 // We should have the wxUSE_WCHAR_T test on the _outside_
579#if wxUSE_WCHAR_T
05e2b077
VZ
580 if (!wxOKlibc())
581 wxConvCurrent = &wxConvLocal;
05e2b077
VZ
582#else // !wxUSE_WCHAR_T
583 if (!wxOKlibc())
584 wxConvCurrent = (wxMBConv*) NULL;
585#endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
002f4218 586
05e2b077
VZ
587#if wxUSE_UNICODE
588 // gtk_init() wants UTF-8, not wchar_t, so convert
589 int i;
0d8dda32 590 char **argvGTK = new char *[argc + 1];
05e2b077 591 for ( i = 0; i < argc; i++ )
924ef850 592 {
05e2b077 593 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv[i]));
924ef850 594 }
0cf2cb36 595
05e2b077 596 argvGTK[argc] = NULL;
954de0f1 597
05e2b077 598 int argcGTK = argc;
3cbab641 599
62be94e1 600#ifdef __WXGPE__
c156411a 601 init_result = true; // is there a _check() version of this?
62be94e1
RR
602 gpe_application_init( &argcGTK, &argvGTK );
603#else
c156411a 604 init_result = gtk_init_check( &argcGTK, &argvGTK );
62be94e1 605#endif
954de0f1 606
05e2b077 607 if ( argcGTK != argc )
924ef850 608 {
05e2b077
VZ
609 // we have to drop the parameters which were consumed by GTK+
610 for ( i = 0; i < argcGTK; i++ )
611 {
0d8dda32 612 while ( strcmp(wxConvUTF8.cWX2MB(argv[i]), argvGTK[i]) != 0 )
05e2b077
VZ
613 {
614 memmove(argv + i, argv + i + 1, argc - i);
615 }
616 }
0cf2cb36 617
05e2b077 618 argc = argcGTK;
2b5f62a0 619 }
05e2b077 620 //else: gtk_init() didn't modify our parameters
e0253070 621
05e2b077
VZ
622 // free our copy
623 for ( i = 0; i < argcGTK; i++ )
0151c3eb 624 {
05e2b077 625 free(argvGTK[i]);
0151c3eb 626 }
0cf2cb36 627
05e2b077
VZ
628 delete [] argvGTK;
629#else // !wxUSE_UNICODE
630 // gtk_init() shouldn't actually change argv itself (just its contents) so
631 // it's ok to pass pointer to it
c156411a 632 init_result = gtk_init_check( &argc, &argv );
05e2b077 633#endif // wxUSE_UNICODE/!wxUSE_UNICODE
0cf2cb36 634
c156411a
RD
635 if (!init_result) {
636 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
637 return false;
638 }
3cbab641 639
05e2b077
VZ
640 // we can not enter threads before gtk_init is done
641 gdk_threads_enter();
0cf2cb36 642
abca8ebf
VZ
643 if ( !wxAppBase::Initialize(argc, argv) )
644 {
645 gdk_threads_leave();
646
647 return false;
648 }
649
05e2b077 650 wxSetDetectableAutoRepeat( TRUE );
be88a6ad 651
05e2b077
VZ
652#if wxUSE_INTL
653 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
654#endif
be88a6ad 655
05e2b077
VZ
656 return true;
657}
0cf2cb36 658
05e2b077
VZ
659void wxApp::CleanUp()
660{
05e2b077 661 gdk_threads_leave();
abca8ebf
VZ
662
663 wxAppBase::CleanUp();
ff7b1510 664}
1a56f55c 665
a5f1fd3e
VZ
666#ifdef __WXDEBUG__
667
be88a6ad 668void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
a5f1fd3e
VZ
669{
670 m_isInAssert = TRUE;
671
be88a6ad 672 wxAppBase::OnAssert(file, line, cond, msg);
a5f1fd3e
VZ
673
674 m_isInAssert = FALSE;
675}
676
677#endif // __WXDEBUG__
678
fe593cc5
VS
679void wxApp::RemoveIdleTag()
680{
681#if wxUSE_THREADS
682 wxMutexLocker lock(gs_idleTagsMutex);
683#endif
c263eb03
VS
684 if (!g_isIdle)
685 {
686 gtk_idle_remove( wxTheApp->m_idleTag );
687 wxTheApp->m_idleTag = 0;
688 g_isIdle = TRUE;
689 }
fe593cc5 690}