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