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