]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
88a7a4e1 | 2 | // Name: src/gtk/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 | ||
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 | 19 | #include "wx/app.h" |
88a7a4e1 WS |
20 | |
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/intl.h" | |
e4db172a | 23 | #include "wx/log.h" |
de6185e2 | 24 | #include "wx/utils.h" |
fdf565fe | 25 | #include "wx/dialog.h" |
9eddec69 | 26 | #include "wx/settings.h" |
246c5004 | 27 | #include "wx/msgdlg.h" |
5b56bffb | 28 | #include "wx/memory.h" |
48a1108e | 29 | #include "wx/font.h" |
dd05139a | 30 | #include "wx/gdicmn.h" |
155ecd4c | 31 | #include "wx/image.h" |
88a7a4e1 WS |
32 | #endif |
33 | ||
b1ac3b56 | 34 | #include "wx/file.h" |
2b5f62a0 | 35 | #include "wx/filename.h" |
031b2a7b | 36 | #include "wx/module.h" |
fe593cc5 | 37 | #include "wx/thread.h" |
afb74891 | 38 | |
62be94e1 | 39 | #ifdef __WXGPE__ |
88a7a4e1 | 40 | #include <gpe/init.h> |
62be94e1 RR |
41 | #endif |
42 | ||
df028524 VS |
43 | #ifdef __WXUNIVERSAL__ |
44 | #include "wx/univ/theme.h" | |
45 | #include "wx/univ/renderer.h" | |
46 | #endif | |
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" |
e8106239 | 76 | #include "wx/gtk/win_gtk.h" |
84833214 | 77 | #include "wx/gtk/private.h" |
c801d85f | 78 | |
c259ffdf | 79 | #include <gdk/gdkx.h> |
24178e4a | 80 | |
2b850ae1 RR |
81 | //----------------------------------------------------------------------------- |
82 | // link GnomeVFS | |
83 | //----------------------------------------------------------------------------- | |
84 | ||
85 | #if wxUSE_LIBGNOMEVFS | |
86 | #include "wx/html/forcelnk.h" | |
87 | FORCE_LINK(gnome_vfs) | |
88 | #endif | |
89 | ||
c801d85f KB |
90 | //----------------------------------------------------------------------------- |
91 | // global data | |
92 | //----------------------------------------------------------------------------- | |
93 | ||
de6185e2 | 94 | bool g_mainThreadLocked = false; |
96997d65 | 95 | gint g_pendingTag = 0; |
3ac8d3bc | 96 | |
c2fa61e8 | 97 | static GtkWidget *gs_RootWindow = (GtkWidget*) NULL; |
d76fe38b | 98 | |
c801d85f | 99 | //----------------------------------------------------------------------------- |
3133cb9f | 100 | // idle system |
c801d85f KB |
101 | //----------------------------------------------------------------------------- |
102 | ||
90350682 | 103 | void wxapp_install_idle_handler(); |
bf9e3e73 | 104 | |
fe593cc5 VS |
105 | #if wxUSE_THREADS |
106 | static wxMutex gs_idleTagsMutex; | |
107 | #endif | |
108 | ||
bf9e3e73 RR |
109 | //----------------------------------------------------------------------------- |
110 | // wxYield | |
111 | //----------------------------------------------------------------------------- | |
53a8af59 | 112 | |
1ee339ee VZ |
113 | // not static because used by textctrl.cpp |
114 | // | |
115 | // MT-FIXME | |
88a7a4e1 | 116 | bool wxIsInsideYield = false; |
1ee339ee | 117 | |
8461e4c2 | 118 | bool wxApp::Yield(bool onlyIfNeeded) |
c801d85f | 119 | { |
1ee339ee | 120 | if ( wxIsInsideYield ) |
8461e4c2 VZ |
121 | { |
122 | if ( !onlyIfNeeded ) | |
123 | { | |
124 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
125 | } | |
126 | ||
88a7a4e1 | 127 | return false; |
8461e4c2 VZ |
128 | } |
129 | ||
6dc5fd71 VZ |
130 | #if wxUSE_THREADS |
131 | if ( !wxThread::IsMain() ) | |
132 | { | |
133 | // can't call gtk_main_iteration() from other threads like this | |
88a7a4e1 | 134 | return true; |
6dc5fd71 VZ |
135 | } |
136 | #endif // wxUSE_THREADS | |
137 | ||
88a7a4e1 | 138 | wxIsInsideYield = true; |
e90c1d2a | 139 | |
c263eb03 VS |
140 | // We need to remove idle callbacks or the loop will |
141 | // never finish. | |
142 | wxTheApp->RemoveIdleTag(); | |
acfd422a | 143 | |
dd7641ef | 144 | #if wxUSE_LOG |
2ed3265e VZ |
145 | // disable log flushing from here because a call to wxYield() shouldn't |
146 | // normally result in message boxes popping up &c | |
147 | wxLog::Suspend(); | |
dd7641ef | 148 | #endif |
2ed3265e | 149 | |
406a6f6b VZ |
150 | while (gtk_events_pending()) |
151 | gtk_main_iteration(); | |
152 | ||
5375a1f5 RR |
153 | // It's necessary to call ProcessIdle() to update the frames sizes which |
154 | // might have been changed (it also will update other things set from | |
be88a6ad | 155 | // OnUpdateUI() which is a nice (and desired) side effect). But we |
5375a1f5 RR |
156 | // call ProcessIdle() only once since this is not meant for longish |
157 | // background jobs (controlled by wxIdleEvent::RequestMore() and the | |
158 | // return value of Processidle(). | |
159 | ProcessIdle(); | |
2ed3265e | 160 | |
dd7641ef | 161 | #if wxUSE_LOG |
2ed3265e VZ |
162 | // let the logs be flashed again |
163 | wxLog::Resume(); | |
dd7641ef | 164 | #endif |
7741c4e1 | 165 | |
88a7a4e1 | 166 | wxIsInsideYield = false; |
99ba739f | 167 | |
88a7a4e1 | 168 | return true; |
acfd422a RR |
169 | } |
170 | ||
bf9e3e73 RR |
171 | //----------------------------------------------------------------------------- |
172 | // wxWakeUpIdle | |
173 | //----------------------------------------------------------------------------- | |
174 | ||
68567a96 MR |
175 | // RR/KH: No wxMutexGui calls are needed here according to the GTK faq, |
176 | // http://www.gtk.org/faq/#AEN500 - this caused problems for wxPostEvent. | |
19722331 | 177 | |
e2478fde | 178 | void wxApp::WakeUpIdle() |
bf9e3e73 | 179 | { |
c263eb03 | 180 | wxapp_install_idle_handler(); |
bf9e3e73 RR |
181 | } |
182 | ||
183 | //----------------------------------------------------------------------------- | |
184 | // local functions | |
185 | //----------------------------------------------------------------------------- | |
186 | ||
90350682 VZ |
187 | // the callback functions must be extern "C" to comply with GTK+ declarations |
188 | extern "C" | |
189 | { | |
190 | ||
3133cb9f | 191 | static gint wxapp_pending_callback( gpointer WXUNUSED(data) ) |
acfd422a RR |
192 | { |
193 | if (!wxTheApp) return TRUE; | |
c2fa61e8 | 194 | |
5375a1f5 | 195 | // When getting called from GDK's time-out handler |
96997d65 | 196 | // we are no longer within GDK's grab on the GUI |
5375a1f5 | 197 | // thread so we must lock it here ourselves. |
96997d65 RR |
198 | gdk_threads_enter(); |
199 | ||
5375a1f5 | 200 | // Sent idle event to all who request them. |
96997d65 | 201 | wxTheApp->ProcessPendingEvents(); |
e90c1d2a | 202 | |
fe593cc5 VS |
203 | { |
204 | #if wxUSE_THREADS | |
205 | wxMutexLocker lock(gs_idleTagsMutex); | |
206 | #endif | |
207 | g_pendingTag = 0; | |
208 | } | |
96997d65 | 209 | |
5375a1f5 | 210 | // Flush the logged messages if any. |
1b2dab34 RR |
211 | #if wxUSE_LOG |
212 | wxLog::FlushActive(); | |
213 | #endif // wxUSE_LOG | |
214 | ||
96997d65 RR |
215 | // Release lock again |
216 | gdk_threads_leave(); | |
217 | ||
218 | // Return FALSE to indicate that no more idle events are | |
219 | // to be sent (single shot instead of continuous stream) | |
220 | return FALSE; | |
221 | } | |
222 | ||
3133cb9f | 223 | static gint wxapp_idle_callback( gpointer WXUNUSED(data) ) |
96997d65 | 224 | { |
a5f1fd3e VZ |
225 | if (!wxTheApp) |
226 | return TRUE; | |
227 | ||
228 | #ifdef __WXDEBUG__ | |
6d477bb4 VZ |
229 | // don't generate the idle events while the assert modal dialog is shown, |
230 | // this completely confuses the apps which don't expect to be reentered | |
231 | // from some safely-looking functions | |
a5f1fd3e VZ |
232 | if ( wxTheApp->IsInAssert() ) |
233 | { | |
94a6f0f8 JS |
234 | // But repaint the assertion message if necessary |
235 | if (wxTopLevelWindows.GetCount() > 0) | |
236 | { | |
b1d4dd7a | 237 | wxWindow* win = (wxWindow*) wxTopLevelWindows.GetLast()->GetData(); |
13a7abf9 | 238 | if (win->IsKindOf(CLASSINFO(wxMessageDialog))) |
94a6f0f8 JS |
239 | win->OnInternalIdle(); |
240 | } | |
6d477bb4 | 241 | return TRUE; |
a5f1fd3e VZ |
242 | } |
243 | #endif // __WXDEBUG__ | |
c2fa61e8 | 244 | |
5375a1f5 | 245 | // When getting called from GDK's time-out handler |
924ef850 | 246 | // we are no longer within GDK's grab on the GUI |
5375a1f5 | 247 | // thread so we must lock it here ourselves. |
924ef850 | 248 | gdk_threads_enter(); |
094637f6 | 249 | |
5375a1f5 RR |
250 | // Indicate that we are now in idle mode and event handlers |
251 | // will have to reinstall the idle handler again. | |
fe593cc5 VS |
252 | { |
253 | #if wxUSE_THREADS | |
254 | wxMutexLocker lock(gs_idleTagsMutex); | |
255 | #endif | |
e4db172a | 256 | g_isIdle = true; |
fe593cc5 VS |
257 | wxTheApp->m_idleTag = 0; |
258 | } | |
094637f6 | 259 | |
0faa7620 | 260 | bool moreIdles; |
8ab9a536 | 261 | |
3133cb9f | 262 | // Send idle event to all who request them as long as |
5375a1f5 | 263 | // no events have popped up in the event queue. |
0faa7620 | 264 | while ( (moreIdles = wxTheApp->ProcessIdle()) && gtk_events_pending() == 0) |
6d477bb4 | 265 | ; |
c9e35272 | 266 | |
96997d65 | 267 | // Release lock again |
924ef850 | 268 | gdk_threads_leave(); |
acfd422a | 269 | |
8ab9a536 RR |
270 | // Return FALSE if no more idle events are to be sent |
271 | return moreIdles; | |
acfd422a | 272 | } |
8801832d | 273 | |
90350682 VZ |
274 | #if wxUSE_THREADS |
275 | ||
2b5f62a0 VZ |
276 | #ifdef HAVE_POLL |
277 | #define wxPoll poll | |
278 | #define wxPollFd pollfd | |
279 | #else // !HAVE_POLL | |
280 | ||
281 | typedef GPollFD wxPollFd; | |
282 | ||
283 | int wxPoll(wxPollFd *ufds, unsigned int nfds, int timeout) | |
284 | { | |
285 | // convert timeout from ms to struct timeval (s/us) | |
286 | timeval tv_timeout; | |
287 | tv_timeout.tv_sec = timeout/1000; | |
288 | tv_timeout.tv_usec = (timeout%1000)*1000; | |
289 | ||
290 | // remember the highest fd used here | |
291 | int fdMax = -1; | |
292 | ||
293 | // and fill the sets for select() | |
294 | fd_set readfds; | |
295 | fd_set writefds; | |
296 | fd_set exceptfds; | |
17a1ebd1 VZ |
297 | wxFD_ZERO(&readfds); |
298 | wxFD_ZERO(&writefds); | |
299 | wxFD_ZERO(&exceptfds); | |
2b5f62a0 VZ |
300 | |
301 | unsigned int i; | |
302 | for ( i = 0; i < nfds; i++ ) | |
303 | { | |
17a1ebd1 | 304 | wxASSERT_MSG( ufds[i].fd < wxFD_SETSIZE, _T("fd out of range") ); |
2b5f62a0 VZ |
305 | |
306 | if ( ufds[i].events & G_IO_IN ) | |
17a1ebd1 | 307 | wxFD_SET(ufds[i].fd, &readfds); |
2b5f62a0 VZ |
308 | |
309 | if ( ufds[i].events & G_IO_PRI ) | |
17a1ebd1 | 310 | wxFD_SET(ufds[i].fd, &exceptfds); |
2b5f62a0 VZ |
311 | |
312 | if ( ufds[i].events & G_IO_OUT ) | |
17a1ebd1 | 313 | wxFD_SET(ufds[i].fd, &writefds); |
2b5f62a0 VZ |
314 | |
315 | if ( ufds[i].fd > fdMax ) | |
316 | fdMax = ufds[i].fd; | |
317 | } | |
318 | ||
319 | fdMax++; | |
320 | int res = select(fdMax, &readfds, &writefds, &exceptfds, &tv_timeout); | |
321 | ||
322 | // translate the results back | |
323 | for ( i = 0; i < nfds; i++ ) | |
324 | { | |
325 | ufds[i].revents = 0; | |
326 | ||
17a1ebd1 | 327 | if ( wxFD_ISSET(ufds[i].fd, &readfds ) ) |
2b5f62a0 VZ |
328 | ufds[i].revents |= G_IO_IN; |
329 | ||
17a1ebd1 | 330 | if ( wxFD_ISSET(ufds[i].fd, &exceptfds ) ) |
2b5f62a0 VZ |
331 | ufds[i].revents |= G_IO_PRI; |
332 | ||
17a1ebd1 | 333 | if ( wxFD_ISSET(ufds[i].fd, &writefds ) ) |
2b5f62a0 VZ |
334 | ufds[i].revents |= G_IO_OUT; |
335 | } | |
336 | ||
337 | return res; | |
338 | } | |
339 | ||
340 | #endif // HAVE_POLL/!HAVE_POLL | |
341 | ||
3133cb9f | 342 | static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout ) |
acfd422a | 343 | { |
90350682 | 344 | gdk_threads_enter(); |
7b90a8f2 | 345 | |
90350682 | 346 | wxMutexGuiLeave(); |
e4db172a | 347 | g_mainThreadLocked = true; |
7941ba11 | 348 | |
2b5f62a0 VZ |
349 | // we rely on the fact that glib GPollFD struct is really just pollfd but |
350 | // I wonder how wise is this in the long term (VZ) | |
351 | gint res = wxPoll( (wxPollFd *) ufds, nfds, timeout ); | |
90350682 | 352 | |
90350682 | 353 | wxMutexGuiEnter(); |
de6185e2 | 354 | g_mainThreadLocked = false; |
90350682 | 355 | |
90350682 VZ |
356 | gdk_threads_leave(); |
357 | ||
3133cb9f | 358 | return res; |
ff7b1510 | 359 | } |
c801d85f | 360 | |
90350682 VZ |
361 | #endif // wxUSE_THREADS |
362 | ||
363 | } // extern "C" | |
364 | ||
3133cb9f | 365 | void wxapp_install_idle_handler() |
b453e1b2 | 366 | { |
fe593cc5 VS |
367 | #if wxUSE_THREADS |
368 | wxMutexLocker lock(gs_idleTagsMutex); | |
369 | #endif | |
370 | ||
c263eb03 VS |
371 | // Don't install the handler if it's already installed. This test *MUST* |
372 | // be done when gs_idleTagsMutex is locked! | |
373 | if (!g_isIdle) | |
374 | return; | |
375 | ||
e8617760 GD |
376 | // GD: this assert is raised when using the thread sample (which works) |
377 | // so the test is probably not so easy. Can widget callbacks be | |
378 | // triggered from child threads and, if so, for which widgets? | |
379 | // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") ); | |
d5f3e1eb | 380 | |
3133cb9f | 381 | wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") ); |
c2fa61e8 | 382 | |
de6185e2 | 383 | g_isIdle = false; |
96997d65 | 384 | |
3133cb9f | 385 | if (g_pendingTag == 0) |
5d038ec5 | 386 | g_pendingTag = g_idle_add_full( 900, wxapp_pending_callback, NULL, NULL ); |
e90c1d2a | 387 | |
3133cb9f RR |
388 | // This routine gets called by all event handlers |
389 | // indicating that the idle is over. It may also | |
390 | // get called from other thread for sending events | |
391 | // to the main thread (and processing these in | |
392 | // idle time). Very low priority. | |
5d038ec5 | 393 | wxTheApp->m_idleTag = g_idle_add_full( 1000, wxapp_idle_callback, NULL, NULL ); |
b453e1b2 RR |
394 | } |
395 | ||
005f5d18 RR |
396 | //----------------------------------------------------------------------------- |
397 | // Access to the root window global | |
398 | //----------------------------------------------------------------------------- | |
399 | ||
400 | GtkWidget* wxGetRootWindow() | |
401 | { | |
402 | if (gs_RootWindow == NULL) | |
403 | { | |
404 | gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL ); | |
405 | gtk_widget_realize( gs_RootWindow ); | |
406 | } | |
407 | return gs_RootWindow; | |
408 | } | |
409 | ||
c801d85f KB |
410 | //----------------------------------------------------------------------------- |
411 | // wxApp | |
412 | //----------------------------------------------------------------------------- | |
413 | ||
414 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) | |
415 | ||
53010e52 | 416 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) |
955a9197 | 417 | EVT_IDLE(wxAppBase::OnIdle) |
53010e52 RR |
418 | END_EVENT_TABLE() |
419 | ||
c801d85f KB |
420 | wxApp::wxApp() |
421 | { | |
a5f1fd3e | 422 | #ifdef __WXDEBUG__ |
de6185e2 | 423 | m_isInAssert = false; |
a5f1fd3e VZ |
424 | #endif // __WXDEBUG__ |
425 | ||
df5ddbca | 426 | m_idleTag = 0; |
e4db172a | 427 | g_isIdle = true; |
df5ddbca | 428 | wxapp_install_idle_handler(); |
094637f6 | 429 | |
6b3eb77a | 430 | #if wxUSE_THREADS |
15894949 | 431 | g_main_context_set_poll_func( NULL, wxapp_poll_func ); |
6b3eb77a | 432 | #endif |
60acb947 | 433 | |
a6f5aa49 VZ |
434 | // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp |
435 | m_glVisualInfo = (void *) NULL; | |
34a34b02 | 436 | m_glFBCInfo = (void *) NULL; |
ff7b1510 | 437 | } |
c801d85f | 438 | |
60acb947 | 439 | wxApp::~wxApp() |
c801d85f | 440 | { |
5d038ec5 MR |
441 | if (m_idleTag) |
442 | g_source_remove( m_idleTag ); | |
ff7b1510 | 443 | } |
c801d85f | 444 | |
0d2a2b60 | 445 | bool wxApp::OnInitGui() |
c801d85f | 446 | { |
1e6feb95 | 447 | if ( !wxAppBase::OnInitGui() ) |
de6185e2 | 448 | return false; |
1e6feb95 | 449 | |
a6f5aa49 VZ |
450 | // if this is a wxGLApp (derived from wxApp), and we've already |
451 | // chosen a specific visual, then derive the GdkVisual from that | |
005f5d18 RR |
452 | if (m_glVisualInfo != NULL) |
453 | { | |
005f5d18 | 454 | // seems gtk_widget_set_default_visual no longer exists? |
a6f5aa49 | 455 | GdkVisual* vis = gtk_widget_get_default_visual(); |
a6f5aa49 VZ |
456 | |
457 | GdkColormap *colormap = gdk_colormap_new( vis, FALSE ); | |
458 | gtk_widget_set_default_colormap( colormap ); | |
a6f5aa49 | 459 | } |
be88a6ad | 460 | |
005f5d18 RR |
461 | // On some machines, the default visual is just 256 colours, so |
462 | // we make sure we get the best. This can sometimes be wasteful. | |
2286341c | 463 | |
005f5d18 RR |
464 | else |
465 | if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual)) | |
b134516c | 466 | { |
2d4dc3a4 OK |
467 | /* seems gtk_widget_set_default_visual no longer exists? */ |
468 | GdkVisual* vis = gtk_widget_get_default_visual(); | |
f6fcbb63 | 469 | |
b134516c RR |
470 | GdkColormap *colormap = gdk_colormap_new( vis, FALSE ); |
471 | gtk_widget_set_default_colormap( colormap ); | |
f6fcbb63 | 472 | } |
c801d85f | 473 | |
88a7a4e1 | 474 | return true; |
bbe0af5b RR |
475 | } |
476 | ||
005f5d18 RR |
477 | GdkVisual *wxApp::GetGdkVisual() |
478 | { | |
479 | GdkVisual *visual = NULL; | |
be88a6ad | 480 | |
005f5d18 | 481 | if (m_glVisualInfo) |
7b775074 | 482 | visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid ); |
005f5d18 | 483 | else |
22a3bce4 | 484 | visual = gdk_drawable_get_visual( wxGetRootWindow()->window ); |
be88a6ad | 485 | |
005f5d18 | 486 | wxASSERT( visual ); |
be88a6ad | 487 | |
005f5d18 RR |
488 | return visual; |
489 | } | |
490 | ||
abca8ebf | 491 | bool wxApp::Initialize(int& argc, wxChar **argv) |
c801d85f | 492 | { |
c156411a | 493 | bool init_result; |
68567a96 | 494 | |
924ef850 | 495 | #if wxUSE_THREADS |
ac131bab MR |
496 | if (!g_thread_supported()) |
497 | g_thread_init(NULL); | |
05e2b077 | 498 | #endif // wxUSE_THREADS |
2286341c | 499 | |
0d2a2b60 | 500 | gtk_set_locale(); |
c801d85f | 501 | |
f9862abd JS |
502 | // We should have the wxUSE_WCHAR_T test on the _outside_ |
503 | #if wxUSE_WCHAR_T | |
68567a96 MR |
504 | // gtk+ 2.0 supports Unicode through UTF-8 strings |
505 | wxConvCurrent = &wxConvUTF8; | |
05e2b077 VZ |
506 | #else // !wxUSE_WCHAR_T |
507 | if (!wxOKlibc()) | |
508 | wxConvCurrent = (wxMBConv*) NULL; | |
509 | #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T | |
002f4218 | 510 | |
845905d5 MW |
511 | // decide which conversion to use for the file names |
512 | ||
513 | // (1) this variable exists for the sole purpose of specifying the encoding | |
514 | // of the filenames for GTK+ programs, so use it if it is set | |
515 | wxString encName(wxGetenv(_T("G_FILENAME_ENCODING"))); | |
29c326b7 | 516 | encName = encName.BeforeFirst(_T(',')); |
d24b23b7 MW |
517 | if (encName == _T("@locale")) |
518 | encName.clear(); | |
845905d5 | 519 | encName.MakeUpper(); |
68567a96 | 520 | #if wxUSE_INTL |
845905d5 MW |
521 | if (encName.empty()) |
522 | { | |
523 | // (2) if a non default locale is set, assume that the user wants his | |
524 | // filenames in this locale too | |
525 | encName = wxLocale::GetSystemEncodingName().Upper(); | |
526 | // (3) finally use UTF-8 by default | |
527 | if (encName.empty() || encName == _T("US-ASCII")) | |
528 | encName = _T("UTF-8"); | |
529 | wxSetEnv(_T("G_FILENAME_ENCODING"), encName); | |
530 | } | |
531 | #else | |
532 | if (encName.empty()) | |
533 | encName = _T("UTF-8"); | |
534 | #endif // wxUSE_INTL | |
535 | static wxConvBrokenFileNames fileconv(encName); | |
536 | wxConvFileName = &fileconv; | |
66bf0099 | 537 | |
05e2b077 VZ |
538 | #if wxUSE_UNICODE |
539 | // gtk_init() wants UTF-8, not wchar_t, so convert | |
540 | int i; | |
0d8dda32 | 541 | char **argvGTK = new char *[argc + 1]; |
05e2b077 | 542 | for ( i = 0; i < argc; i++ ) |
924ef850 | 543 | { |
05e2b077 | 544 | argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv[i])); |
924ef850 | 545 | } |
0cf2cb36 | 546 | |
05e2b077 | 547 | argvGTK[argc] = NULL; |
954de0f1 | 548 | |
05e2b077 | 549 | int argcGTK = argc; |
68567a96 | 550 | |
62be94e1 | 551 | #ifdef __WXGPE__ |
c156411a | 552 | init_result = true; // is there a _check() version of this? |
62be94e1 RR |
553 | gpe_application_init( &argcGTK, &argvGTK ); |
554 | #else | |
c156411a | 555 | init_result = gtk_init_check( &argcGTK, &argvGTK ); |
62be94e1 | 556 | #endif |
954de0f1 | 557 | |
05e2b077 | 558 | if ( argcGTK != argc ) |
924ef850 | 559 | { |
05e2b077 VZ |
560 | // we have to drop the parameters which were consumed by GTK+ |
561 | for ( i = 0; i < argcGTK; i++ ) | |
562 | { | |
0d8dda32 | 563 | while ( strcmp(wxConvUTF8.cWX2MB(argv[i]), argvGTK[i]) != 0 ) |
05e2b077 VZ |
564 | { |
565 | memmove(argv + i, argv + i + 1, argc - i); | |
566 | } | |
567 | } | |
0cf2cb36 | 568 | |
05e2b077 | 569 | argc = argcGTK; |
2b5f62a0 | 570 | } |
05e2b077 | 571 | //else: gtk_init() didn't modify our parameters |
e0253070 | 572 | |
05e2b077 VZ |
573 | // free our copy |
574 | for ( i = 0; i < argcGTK; i++ ) | |
0151c3eb | 575 | { |
05e2b077 | 576 | free(argvGTK[i]); |
0151c3eb | 577 | } |
0cf2cb36 | 578 | |
05e2b077 VZ |
579 | delete [] argvGTK; |
580 | #else // !wxUSE_UNICODE | |
581 | // gtk_init() shouldn't actually change argv itself (just its contents) so | |
582 | // it's ok to pass pointer to it | |
c156411a | 583 | init_result = gtk_init_check( &argc, &argv ); |
05e2b077 | 584 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
0cf2cb36 | 585 | |
c156411a RD |
586 | if (!init_result) { |
587 | wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?")); | |
588 | return false; | |
589 | } | |
68567a96 | 590 | |
05e2b077 VZ |
591 | // we can not enter threads before gtk_init is done |
592 | gdk_threads_enter(); | |
0cf2cb36 | 593 | |
abca8ebf VZ |
594 | if ( !wxAppBase::Initialize(argc, argv) ) |
595 | { | |
596 | gdk_threads_leave(); | |
597 | ||
598 | return false; | |
599 | } | |
600 | ||
e4db172a | 601 | wxSetDetectableAutoRepeat( true ); |
be88a6ad | 602 | |
05e2b077 VZ |
603 | #if wxUSE_INTL |
604 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
605 | #endif | |
be88a6ad | 606 | |
05e2b077 VZ |
607 | return true; |
608 | } | |
0cf2cb36 | 609 | |
05e2b077 VZ |
610 | void wxApp::CleanUp() |
611 | { | |
05e2b077 | 612 | gdk_threads_leave(); |
abca8ebf VZ |
613 | |
614 | wxAppBase::CleanUp(); | |
ff7b1510 | 615 | } |
1a56f55c | 616 | |
a5f1fd3e VZ |
617 | #ifdef __WXDEBUG__ |
618 | ||
be88a6ad | 619 | void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg) |
a5f1fd3e | 620 | { |
88a7a4e1 | 621 | m_isInAssert = true; |
a5f1fd3e | 622 | |
be88a6ad | 623 | wxAppBase::OnAssert(file, line, cond, msg); |
a5f1fd3e | 624 | |
de6185e2 | 625 | m_isInAssert = false; |
a5f1fd3e VZ |
626 | } |
627 | ||
628 | #endif // __WXDEBUG__ | |
629 | ||
fe593cc5 VS |
630 | void wxApp::RemoveIdleTag() |
631 | { | |
632 | #if wxUSE_THREADS | |
633 | wxMutexLocker lock(gs_idleTagsMutex); | |
634 | #endif | |
c263eb03 VS |
635 | if (!g_isIdle) |
636 | { | |
5d038ec5 | 637 | g_source_remove( wxTheApp->m_idleTag ); |
c263eb03 | 638 | wxTheApp->m_idleTag = 0; |
e4db172a | 639 | g_isIdle = true; |
c263eb03 | 640 | } |
fe593cc5 | 641 | } |