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