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