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