]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/app.cpp
implement wxListBox::EnsureVisible() in wxGTK; add a test for it to the widgets sample
[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
005f5d18
RR
402//-----------------------------------------------------------------------------
403// Access to the root window global
404//-----------------------------------------------------------------------------
405
406GtkWidget* wxGetRootWindow()
407{
408 if (gs_RootWindow == NULL)
409 {
410 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
411 gtk_widget_realize( gs_RootWindow );
412 }
413 return gs_RootWindow;
414}
415
c801d85f
KB
416//-----------------------------------------------------------------------------
417// wxApp
418//-----------------------------------------------------------------------------
419
420IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
421
c801d85f
KB
422wxApp::wxApp()
423{
a5f1fd3e 424#ifdef __WXDEBUG__
de6185e2 425 m_isInAssert = false;
a5f1fd3e
VZ
426#endif // __WXDEBUG__
427
df5ddbca 428 m_idleTag = 0;
c263eb03 429 g_isIdle = TRUE;
df5ddbca 430 wxapp_install_idle_handler();
094637f6 431
6b3eb77a 432#if wxUSE_THREADS
3133cb9f 433 g_main_set_poll_func( wxapp_poll_func );
6b3eb77a 434#endif
60acb947 435
f6fcbb63 436 m_colorCube = (unsigned char*) NULL;
be88a6ad 437
a6f5aa49
VZ
438 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
439 m_glVisualInfo = (void *) NULL;
34a34b02 440 m_glFBCInfo = (void *) NULL;
ff7b1510 441}
c801d85f 442
60acb947 443wxApp::~wxApp()
c801d85f 444{
acfd422a 445 if (m_idleTag) gtk_idle_remove( m_idleTag );
60acb947 446
f6fcbb63 447 if (m_colorCube) free(m_colorCube);
ff7b1510 448}
c801d85f 449
0d2a2b60 450bool wxApp::OnInitGui()
c801d85f 451{
1e6feb95 452 if ( !wxAppBase::OnInitGui() )
de6185e2 453 return false;
1e6feb95 454
b134516c
RR
455 GdkVisual *visual = gdk_visual_get_system();
456
a6f5aa49
VZ
457 // if this is a wxGLApp (derived from wxApp), and we've already
458 // chosen a specific visual, then derive the GdkVisual from that
005f5d18
RR
459 if (m_glVisualInfo != NULL)
460 {
be88a6ad 461 GdkVisual* vis = gdkx_visual_get(
a6f5aa49
VZ
462 ((XVisualInfo *) m_glVisualInfo) ->visualid );
463 gtk_widget_set_default_visual( vis );
a6f5aa49
VZ
464
465 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
466 gtk_widget_set_default_colormap( colormap );
467
468 visual = vis;
469 }
be88a6ad 470
005f5d18
RR
471 // On some machines, the default visual is just 256 colours, so
472 // we make sure we get the best. This can sometimes be wasteful.
2286341c 473
005f5d18
RR
474 else
475 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual))
b134516c
RR
476 {
477 GdkVisual* vis = gdk_visual_get_best();
478 gtk_widget_set_default_visual( vis );
f6fcbb63 479
b134516c
RR
480 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
481 gtk_widget_set_default_colormap( colormap );
094637f6
VZ
482
483 visual = vis;
b134516c 484 }
d76fe38b 485
005f5d18 486 // Nothing to do for 15, 16, 24, 32 bit displays
f6fcbb63 487 if (visual->depth > 8) return TRUE;
60acb947 488
005f5d18 489 // initialize color cube for 8-bit color reduction dithering
60acb947 490
f6fcbb63 491 GdkColormap *cmap = gtk_widget_get_default_colormap();
60acb947 492
f6fcbb63
RR
493 m_colorCube = (unsigned char*)malloc(32 * 32 * 32);
494
f03fc89f
VZ
495 for (int r = 0; r < 32; r++)
496 {
8801832d
VZ
497 for (int g = 0; g < 32; g++)
498 {
499 for (int b = 0; b < 32; b++)
500 {
501 int rr = (r << 3) | (r >> 2);
502 int gg = (g << 3) | (g >> 2);
503 int bb = (b << 3) | (b >> 2);
60acb947 504
f03fc89f
VZ
505 int index = -1;
506
f6fcbb63 507 GdkColor *colors = cmap->colors;
ca26177c 508 if (colors)
f03fc89f
VZ
509 {
510 int max = 3 * 65536;
511
512 for (int i = 0; i < cmap->size; i++)
513 {
514 int rdiff = ((rr << 8) - colors[i].red);
515 int gdiff = ((gg << 8) - colors[i].green);
516 int bdiff = ((bb << 8) - colors[i].blue);
517 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
518 if (sum < max)
094637f6 519 {
f03fc89f
VZ
520 index = i; max = sum;
521 }
522 }
f6fcbb63 523 }
094637f6
VZ
524 else
525 {
005f5d18 526 // assume 8-bit true or static colors. this really exists
094637f6
VZ
527 GdkVisual* vis = gdk_colormap_get_visual( cmap );
528 index = (r >> (5 - vis->red_prec)) << vis->red_shift;
529 index |= (g >> (5 - vis->green_prec)) << vis->green_shift;
530 index |= (b >> (5 - vis->blue_prec)) << vis->blue_shift;
094637f6 531 }
8801832d
VZ
532 m_colorCube[ (r*1024) + (g*32) + b ] = index;
533 }
534 }
f6fcbb63 535 }
c801d85f 536
de6185e2 537 return true;
bbe0af5b
RR
538}
539
005f5d18
RR
540GdkVisual *wxApp::GetGdkVisual()
541{
542 GdkVisual *visual = NULL;
be88a6ad 543
005f5d18 544 if (m_glVisualInfo)
7b775074 545 visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid );
005f5d18
RR
546 else
547 visual = gdk_window_get_visual( wxGetRootWindow()->window );
be88a6ad 548
005f5d18 549 wxASSERT( visual );
be88a6ad 550
005f5d18
RR
551 return visual;
552}
553
abca8ebf 554bool wxApp::Initialize(int& argc, wxChar **argv)
c801d85f 555{
c156411a 556 bool init_result;
3cbab641 557
924ef850 558#if wxUSE_THREADS
005f5d18
RR
559 // GTK 1.2 up to version 1.2.3 has broken threads
560 if ((gtk_major_version == 1) &&
8f75cb6c 561 (gtk_minor_version == 2) &&
2286341c 562 (gtk_micro_version < 4))
96997d65 563 {
77ffb593 564 printf( "wxWidgets warning: GUI threading disabled due to outdated GTK version\n" );
8f75cb6c
RR
565 }
566 else
567 {
863136de
RR
568 if (!g_thread_supported())
569 g_thread_init(NULL);
8f75cb6c 570 }
05e2b077 571#endif // wxUSE_THREADS
2286341c 572
0d2a2b60 573 gtk_set_locale();
c801d85f 574
f9862abd
JS
575 // We should have the wxUSE_WCHAR_T test on the _outside_
576#if wxUSE_WCHAR_T
05e2b077
VZ
577 if (!wxOKlibc())
578 wxConvCurrent = &wxConvLocal;
05e2b077
VZ
579#else // !wxUSE_WCHAR_T
580 if (!wxOKlibc())
581 wxConvCurrent = (wxMBConv*) NULL;
582#endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
002f4218 583
05e2b077
VZ
584#if wxUSE_UNICODE
585 // gtk_init() wants UTF-8, not wchar_t, so convert
586 int i;
0d8dda32 587 char **argvGTK = new char *[argc + 1];
05e2b077 588 for ( i = 0; i < argc; i++ )
924ef850 589 {
05e2b077 590 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv[i]));
924ef850 591 }
0cf2cb36 592
05e2b077 593 argvGTK[argc] = NULL;
954de0f1 594
05e2b077 595 int argcGTK = argc;
3cbab641 596
62be94e1 597#ifdef __WXGPE__
c156411a 598 init_result = true; // is there a _check() version of this?
62be94e1
RR
599 gpe_application_init( &argcGTK, &argvGTK );
600#else
c156411a 601 init_result = gtk_init_check( &argcGTK, &argvGTK );
62be94e1 602#endif
954de0f1 603
05e2b077 604 if ( argcGTK != argc )
924ef850 605 {
05e2b077
VZ
606 // we have to drop the parameters which were consumed by GTK+
607 for ( i = 0; i < argcGTK; i++ )
608 {
0d8dda32 609 while ( strcmp(wxConvUTF8.cWX2MB(argv[i]), argvGTK[i]) != 0 )
05e2b077
VZ
610 {
611 memmove(argv + i, argv + i + 1, argc - i);
612 }
613 }
0cf2cb36 614
05e2b077 615 argc = argcGTK;
2b5f62a0 616 }
05e2b077 617 //else: gtk_init() didn't modify our parameters
e0253070 618
05e2b077
VZ
619 // free our copy
620 for ( i = 0; i < argcGTK; i++ )
0151c3eb 621 {
05e2b077 622 free(argvGTK[i]);
0151c3eb 623 }
0cf2cb36 624
05e2b077
VZ
625 delete [] argvGTK;
626#else // !wxUSE_UNICODE
627 // gtk_init() shouldn't actually change argv itself (just its contents) so
628 // it's ok to pass pointer to it
c156411a 629 init_result = gtk_init_check( &argc, &argv );
05e2b077 630#endif // wxUSE_UNICODE/!wxUSE_UNICODE
0cf2cb36 631
c156411a
RD
632 if (!init_result) {
633 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
634 return false;
635 }
3cbab641 636
05e2b077
VZ
637 // we can not enter threads before gtk_init is done
638 gdk_threads_enter();
0cf2cb36 639
abca8ebf
VZ
640 if ( !wxAppBase::Initialize(argc, argv) )
641 {
642 gdk_threads_leave();
643
644 return false;
645 }
646
05e2b077 647 wxSetDetectableAutoRepeat( TRUE );
be88a6ad 648
05e2b077
VZ
649#if wxUSE_INTL
650 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
651#endif
be88a6ad 652
05e2b077
VZ
653 return true;
654}
0cf2cb36 655
05e2b077
VZ
656void wxApp::CleanUp()
657{
05e2b077 658 gdk_threads_leave();
abca8ebf
VZ
659
660 wxAppBase::CleanUp();
ff7b1510 661}
1a56f55c 662
a5f1fd3e
VZ
663#ifdef __WXDEBUG__
664
be88a6ad 665void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
a5f1fd3e 666{
de6185e2 667 m_isInAssert = true;
a5f1fd3e 668
be88a6ad 669 wxAppBase::OnAssert(file, line, cond, msg);
a5f1fd3e 670
de6185e2 671 m_isInAssert = false;
a5f1fd3e
VZ
672}
673
674#endif // __WXDEBUG__
675
fe593cc5
VS
676void wxApp::RemoveIdleTag()
677{
678#if wxUSE_THREADS
679 wxMutexLocker lock(gs_idleTagsMutex);
680#endif
c263eb03
VS
681 if (!g_isIdle)
682 {
683 gtk_idle_remove( wxTheApp->m_idleTag );
684 wxTheApp->m_idleTag = 0;
de6185e2 685 g_isIdle = true;
c263eb03 686 }
fe593cc5 687}