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