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