]>
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 |
8bbe427f | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | #ifdef __GNUG__ | |
afb74891 | 11 | #pragma implementation "app.h" |
c801d85f KB |
12 | #endif |
13 | ||
df744f4d JJ |
14 | #ifdef __VMS |
15 | #include <vms_jackets.h> | |
ff7d1dcb | 16 | #undef ConnectionNumber |
df744f4d JJ |
17 | #endif |
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" |
afb74891 | 33 | |
df028524 VS |
34 | #ifdef __WXUNIVERSAL__ |
35 | #include "wx/univ/theme.h" | |
36 | #include "wx/univ/renderer.h" | |
37 | #endif | |
38 | ||
91b8de8d | 39 | #if wxUSE_THREADS |
a37a5a73 | 40 | #include "wx/thread.h" |
91b8de8d | 41 | #endif |
afb74891 | 42 | |
20e05ffb | 43 | #include <unistd.h> |
2b5f62a0 VZ |
44 | |
45 | #ifdef HAVE_POLL | |
46 | #if defined(__VMS) | |
47 | #include <poll.h> | |
48 | #else | |
49 | // bug in the OpenBSD headers: at least in 3.1 there is no extern "C" | |
50 | // in neither poll.h nor sys/poll.h which results in link errors later | |
51 | #ifdef __OPENBSD__ | |
52 | extern "C" | |
53 | { | |
54 | #endif | |
55 | ||
56 | #include <sys/poll.h> | |
57 | ||
58 | #ifdef __OPENBSD__ | |
59 | }; | |
60 | #endif | |
61 | #endif // platform | |
62 | #else // !HAVE_POLL | |
63 | // we implement poll() ourselves using select() which is supposed exist in | |
64 | // all modern Unices | |
65 | #include <sys/types.h> | |
66 | #include <sys/time.h> | |
67 | #include <unistd.h> | |
68 | #endif // HAVE_POLL/!HAVE_POLL | |
69 | ||
e8106239 | 70 | #include "wx/gtk/win_gtk.h" |
c801d85f | 71 | |
20e05ffb | 72 | #include <gtk/gtk.h> |
24178e4a | 73 | |
83624f79 | 74 | |
c801d85f KB |
75 | //----------------------------------------------------------------------------- |
76 | // global data | |
77 | //----------------------------------------------------------------------------- | |
78 | ||
c67daf87 | 79 | wxApp *wxTheApp = (wxApp *) NULL; |
32d4bfd1 | 80 | wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL; |
c801d85f | 81 | |
96997d65 RR |
82 | bool g_mainThreadLocked = FALSE; |
83 | gint g_pendingTag = 0; | |
3ac8d3bc | 84 | |
c2fa61e8 | 85 | static GtkWidget *gs_RootWindow = (GtkWidget*) NULL; |
d76fe38b | 86 | |
c801d85f | 87 | //----------------------------------------------------------------------------- |
3133cb9f | 88 | // idle system |
c801d85f KB |
89 | //----------------------------------------------------------------------------- |
90 | ||
3133cb9f | 91 | extern bool g_isIdle; |
c4fda16b | 92 | |
90350682 | 93 | void wxapp_install_idle_handler(); |
bf9e3e73 | 94 | |
c801d85f | 95 | //----------------------------------------------------------------------------- |
bf9e3e73 | 96 | // wxExit |
c801d85f KB |
97 | //----------------------------------------------------------------------------- |
98 | ||
60acb947 | 99 | void wxExit() |
c801d85f | 100 | { |
ec758a20 | 101 | gtk_main_quit(); |
ff7b1510 | 102 | } |
c801d85f | 103 | |
bf9e3e73 RR |
104 | //----------------------------------------------------------------------------- |
105 | // wxYield | |
106 | //----------------------------------------------------------------------------- | |
53a8af59 | 107 | |
1ee339ee VZ |
108 | // not static because used by textctrl.cpp |
109 | // | |
110 | // MT-FIXME | |
111 | bool wxIsInsideYield = FALSE; | |
112 | ||
8461e4c2 | 113 | bool wxApp::Yield(bool onlyIfNeeded) |
c801d85f | 114 | { |
1ee339ee | 115 | if ( wxIsInsideYield ) |
8461e4c2 VZ |
116 | { |
117 | if ( !onlyIfNeeded ) | |
118 | { | |
119 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
120 | } | |
121 | ||
122 | return FALSE; | |
123 | } | |
124 | ||
6dc5fd71 VZ |
125 | #if wxUSE_THREADS |
126 | if ( !wxThread::IsMain() ) | |
127 | { | |
128 | // can't call gtk_main_iteration() from other threads like this | |
129 | return TRUE; | |
130 | } | |
131 | #endif // wxUSE_THREADS | |
132 | ||
1ee339ee | 133 | wxIsInsideYield = TRUE; |
e90c1d2a | 134 | |
99ba739f | 135 | if (!g_isIdle) |
acfd422a | 136 | { |
99ba739f RR |
137 | // We need to remove idle callbacks or the loop will |
138 | // never finish. | |
8461e4c2 VZ |
139 | gtk_idle_remove( m_idleTag ); |
140 | m_idleTag = 0; | |
99ba739f | 141 | g_isIdle = TRUE; |
956dbab1 | 142 | } |
acfd422a | 143 | |
2ed3265e VZ |
144 | // disable log flushing from here because a call to wxYield() shouldn't |
145 | // normally result in message boxes popping up &c | |
146 | wxLog::Suspend(); | |
147 | ||
406a6f6b VZ |
148 | while (gtk_events_pending()) |
149 | gtk_main_iteration(); | |
150 | ||
5375a1f5 RR |
151 | // It's necessary to call ProcessIdle() to update the frames sizes which |
152 | // might have been changed (it also will update other things set from | |
be88a6ad | 153 | // OnUpdateUI() which is a nice (and desired) side effect). But we |
5375a1f5 RR |
154 | // call ProcessIdle() only once since this is not meant for longish |
155 | // background jobs (controlled by wxIdleEvent::RequestMore() and the | |
156 | // return value of Processidle(). | |
157 | ProcessIdle(); | |
2ed3265e VZ |
158 | |
159 | // let the logs be flashed again | |
160 | wxLog::Resume(); | |
7741c4e1 | 161 | |
1ee339ee | 162 | wxIsInsideYield = FALSE; |
99ba739f | 163 | |
acfd422a RR |
164 | return TRUE; |
165 | } | |
166 | ||
bf9e3e73 RR |
167 | //----------------------------------------------------------------------------- |
168 | // wxWakeUpIdle | |
169 | //----------------------------------------------------------------------------- | |
170 | ||
d5f3e1eb GD |
171 | static bool gs_WakeUpIdle = false; |
172 | ||
bf9e3e73 RR |
173 | void wxWakeUpIdle() |
174 | { | |
924ef850 RR |
175 | #if wxUSE_THREADS |
176 | if (!wxThread::IsMain()) | |
ce6d2511 | 177 | wxMutexGuiEnter(); |
924ef850 RR |
178 | #endif |
179 | ||
d5f3e1eb GD |
180 | if (g_isIdle) { |
181 | gs_WakeUpIdle = true; | |
bf9e3e73 | 182 | wxapp_install_idle_handler(); |
d5f3e1eb GD |
183 | gs_WakeUpIdle = false; |
184 | } | |
2286341c | 185 | |
924ef850 RR |
186 | #if wxUSE_THREADS |
187 | if (!wxThread::IsMain()) | |
ce6d2511 | 188 | wxMutexGuiLeave(); |
924ef850 | 189 | #endif |
bf9e3e73 RR |
190 | } |
191 | ||
192 | //----------------------------------------------------------------------------- | |
193 | // local functions | |
194 | //----------------------------------------------------------------------------- | |
195 | ||
90350682 VZ |
196 | // the callback functions must be extern "C" to comply with GTK+ declarations |
197 | extern "C" | |
198 | { | |
199 | ||
3133cb9f | 200 | static gint wxapp_pending_callback( gpointer WXUNUSED(data) ) |
acfd422a RR |
201 | { |
202 | if (!wxTheApp) return TRUE; | |
c2fa61e8 | 203 | |
5375a1f5 | 204 | // When getting called from GDK's time-out handler |
96997d65 | 205 | // we are no longer within GDK's grab on the GUI |
5375a1f5 | 206 | // thread so we must lock it here ourselves. |
96997d65 RR |
207 | gdk_threads_enter(); |
208 | ||
5375a1f5 | 209 | // Sent idle event to all who request them. |
96997d65 | 210 | wxTheApp->ProcessPendingEvents(); |
e90c1d2a | 211 | |
96997d65 RR |
212 | g_pendingTag = 0; |
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(); |
13a7abf9 VS |
242 | #ifdef __WXGTK20__ |
243 | if (win->IsKindOf(CLASSINFO(wxMessageDialog))) | |
244 | #else | |
94a6f0f8 | 245 | if (win->IsKindOf(CLASSINFO(wxGenericMessageDialog))) |
13a7abf9 | 246 | #endif |
94a6f0f8 JS |
247 | win->OnInternalIdle(); |
248 | } | |
6d477bb4 | 249 | return TRUE; |
a5f1fd3e VZ |
250 | } |
251 | #endif // __WXDEBUG__ | |
c2fa61e8 | 252 | |
5375a1f5 | 253 | // When getting called from GDK's time-out handler |
924ef850 | 254 | // we are no longer within GDK's grab on the GUI |
5375a1f5 | 255 | // thread so we must lock it here ourselves. |
924ef850 | 256 | gdk_threads_enter(); |
094637f6 | 257 | |
5375a1f5 RR |
258 | // Indicate that we are now in idle mode and event handlers |
259 | // will have to reinstall the idle handler again. | |
acfd422a | 260 | g_isIdle = TRUE; |
96997d65 | 261 | wxTheApp->m_idleTag = 0; |
094637f6 | 262 | |
3133cb9f | 263 | // Send idle event to all who request them as long as |
5375a1f5 RR |
264 | // no events have popped up in the event queue. |
265 | while (wxTheApp->ProcessIdle() && (gtk_events_pending() == 0)) | |
6d477bb4 | 266 | ; |
c9e35272 | 267 | |
96997d65 | 268 | // Release lock again |
924ef850 | 269 | gdk_threads_leave(); |
acfd422a | 270 | |
96997d65 | 271 | // Return FALSE to indicate that no more idle events are |
5375a1f5 | 272 | // to be sent (single shot instead of continuous stream). |
96997d65 | 273 | return FALSE; |
acfd422a | 274 | } |
8801832d | 275 | |
90350682 VZ |
276 | #if wxUSE_THREADS |
277 | ||
2b5f62a0 VZ |
278 | #ifdef HAVE_POLL |
279 | #define wxPoll poll | |
280 | #define wxPollFd pollfd | |
281 | #else // !HAVE_POLL | |
282 | ||
283 | typedef GPollFD wxPollFd; | |
284 | ||
285 | int wxPoll(wxPollFd *ufds, unsigned int nfds, int timeout) | |
286 | { | |
287 | // convert timeout from ms to struct timeval (s/us) | |
288 | timeval tv_timeout; | |
289 | tv_timeout.tv_sec = timeout/1000; | |
290 | tv_timeout.tv_usec = (timeout%1000)*1000; | |
291 | ||
292 | // remember the highest fd used here | |
293 | int fdMax = -1; | |
294 | ||
295 | // and fill the sets for select() | |
296 | fd_set readfds; | |
297 | fd_set writefds; | |
298 | fd_set exceptfds; | |
299 | FD_ZERO(&readfds); | |
300 | FD_ZERO(&writefds); | |
301 | FD_ZERO(&exceptfds); | |
302 | ||
303 | unsigned int i; | |
304 | for ( i = 0; i < nfds; i++ ) | |
305 | { | |
306 | wxASSERT_MSG( ufds[i].fd < FD_SETSIZE, _T("fd out of range") ); | |
307 | ||
308 | if ( ufds[i].events & G_IO_IN ) | |
309 | FD_SET(ufds[i].fd, &readfds); | |
310 | ||
311 | if ( ufds[i].events & G_IO_PRI ) | |
312 | FD_SET(ufds[i].fd, &exceptfds); | |
313 | ||
314 | if ( ufds[i].events & G_IO_OUT ) | |
315 | FD_SET(ufds[i].fd, &writefds); | |
316 | ||
317 | if ( ufds[i].fd > fdMax ) | |
318 | fdMax = ufds[i].fd; | |
319 | } | |
320 | ||
321 | fdMax++; | |
322 | int res = select(fdMax, &readfds, &writefds, &exceptfds, &tv_timeout); | |
323 | ||
324 | // translate the results back | |
325 | for ( i = 0; i < nfds; i++ ) | |
326 | { | |
327 | ufds[i].revents = 0; | |
328 | ||
329 | if ( FD_ISSET(ufds[i].fd, &readfds ) ) | |
330 | ufds[i].revents |= G_IO_IN; | |
331 | ||
332 | if ( FD_ISSET(ufds[i].fd, &exceptfds ) ) | |
333 | ufds[i].revents |= G_IO_PRI; | |
334 | ||
335 | if ( FD_ISSET(ufds[i].fd, &writefds ) ) | |
336 | ufds[i].revents |= G_IO_OUT; | |
337 | } | |
338 | ||
339 | return res; | |
340 | } | |
341 | ||
342 | #endif // HAVE_POLL/!HAVE_POLL | |
343 | ||
3133cb9f | 344 | static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout ) |
acfd422a | 345 | { |
90350682 | 346 | gdk_threads_enter(); |
7b90a8f2 | 347 | |
90350682 | 348 | wxMutexGuiLeave(); |
90350682 | 349 | g_mainThreadLocked = TRUE; |
7941ba11 | 350 | |
2b5f62a0 VZ |
351 | // we rely on the fact that glib GPollFD struct is really just pollfd but |
352 | // I wonder how wise is this in the long term (VZ) | |
353 | gint res = wxPoll( (wxPollFd *) ufds, nfds, timeout ); | |
90350682 | 354 | |
90350682 | 355 | wxMutexGuiEnter(); |
90350682 VZ |
356 | g_mainThreadLocked = FALSE; |
357 | ||
90350682 VZ |
358 | gdk_threads_leave(); |
359 | ||
3133cb9f | 360 | return res; |
ff7b1510 | 361 | } |
c801d85f | 362 | |
90350682 VZ |
363 | #endif // wxUSE_THREADS |
364 | ||
365 | } // extern "C" | |
366 | ||
3133cb9f | 367 | void wxapp_install_idle_handler() |
b453e1b2 | 368 | { |
e8617760 GD |
369 | // GD: this assert is raised when using the thread sample (which works) |
370 | // so the test is probably not so easy. Can widget callbacks be | |
371 | // triggered from child threads and, if so, for which widgets? | |
372 | // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") ); | |
d5f3e1eb | 373 | |
3133cb9f | 374 | wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") ); |
c2fa61e8 | 375 | |
3133cb9f | 376 | g_isIdle = FALSE; |
96997d65 | 377 | |
3133cb9f RR |
378 | if (g_pendingTag == 0) |
379 | g_pendingTag = gtk_idle_add_priority( 900, wxapp_pending_callback, (gpointer) NULL ); | |
e90c1d2a | 380 | |
3133cb9f RR |
381 | // This routine gets called by all event handlers |
382 | // indicating that the idle is over. It may also | |
383 | // get called from other thread for sending events | |
384 | // to the main thread (and processing these in | |
385 | // idle time). Very low priority. | |
386 | wxTheApp->m_idleTag = gtk_idle_add_priority( 1000, wxapp_idle_callback, (gpointer) NULL ); | |
b453e1b2 RR |
387 | } |
388 | ||
005f5d18 RR |
389 | //----------------------------------------------------------------------------- |
390 | // Access to the root window global | |
391 | //----------------------------------------------------------------------------- | |
392 | ||
393 | GtkWidget* wxGetRootWindow() | |
394 | { | |
395 | if (gs_RootWindow == NULL) | |
396 | { | |
397 | gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL ); | |
398 | gtk_widget_realize( gs_RootWindow ); | |
399 | } | |
400 | return gs_RootWindow; | |
401 | } | |
402 | ||
c801d85f KB |
403 | //----------------------------------------------------------------------------- |
404 | // wxApp | |
405 | //----------------------------------------------------------------------------- | |
406 | ||
407 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) | |
408 | ||
53010e52 RR |
409 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) |
410 | EVT_IDLE(wxApp::OnIdle) | |
411 | END_EVENT_TABLE() | |
412 | ||
c801d85f KB |
413 | wxApp::wxApp() |
414 | { | |
a5f1fd3e VZ |
415 | m_initialized = FALSE; |
416 | #ifdef __WXDEBUG__ | |
417 | m_isInAssert = FALSE; | |
418 | #endif // __WXDEBUG__ | |
419 | ||
df5ddbca RR |
420 | m_idleTag = 0; |
421 | wxapp_install_idle_handler(); | |
094637f6 | 422 | |
6b3eb77a | 423 | #if wxUSE_THREADS |
3133cb9f | 424 | g_main_set_poll_func( wxapp_poll_func ); |
6b3eb77a | 425 | #endif |
60acb947 | 426 | |
f6fcbb63 | 427 | m_colorCube = (unsigned char*) NULL; |
be88a6ad | 428 | |
a6f5aa49 VZ |
429 | // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp |
430 | m_glVisualInfo = (void *) NULL; | |
ff7b1510 | 431 | } |
c801d85f | 432 | |
60acb947 | 433 | wxApp::~wxApp() |
c801d85f | 434 | { |
acfd422a | 435 | if (m_idleTag) gtk_idle_remove( m_idleTag ); |
60acb947 | 436 | |
f6fcbb63 | 437 | if (m_colorCube) free(m_colorCube); |
ff7b1510 | 438 | } |
c801d85f | 439 | |
0d2a2b60 | 440 | bool wxApp::OnInitGui() |
c801d85f | 441 | { |
1e6feb95 VZ |
442 | if ( !wxAppBase::OnInitGui() ) |
443 | return FALSE; | |
444 | ||
b134516c RR |
445 | GdkVisual *visual = gdk_visual_get_system(); |
446 | ||
a6f5aa49 VZ |
447 | // if this is a wxGLApp (derived from wxApp), and we've already |
448 | // chosen a specific visual, then derive the GdkVisual from that | |
005f5d18 RR |
449 | if (m_glVisualInfo != NULL) |
450 | { | |
a6f5aa49 | 451 | #ifdef __WXGTK20__ |
005f5d18 | 452 | // seems gtk_widget_set_default_visual no longer exists? |
a6f5aa49 VZ |
453 | GdkVisual* vis = gtk_widget_get_default_visual(); |
454 | #else | |
be88a6ad | 455 | GdkVisual* vis = gdkx_visual_get( |
a6f5aa49 VZ |
456 | ((XVisualInfo *) m_glVisualInfo) ->visualid ); |
457 | gtk_widget_set_default_visual( vis ); | |
458 | #endif | |
459 | ||
460 | GdkColormap *colormap = gdk_colormap_new( vis, FALSE ); | |
461 | gtk_widget_set_default_colormap( colormap ); | |
462 | ||
463 | visual = vis; | |
464 | } | |
be88a6ad | 465 | |
005f5d18 RR |
466 | // On some machines, the default visual is just 256 colours, so |
467 | // we make sure we get the best. This can sometimes be wasteful. | |
2286341c | 468 | |
005f5d18 RR |
469 | else |
470 | if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual)) | |
b134516c | 471 | { |
2d4dc3a4 OK |
472 | #ifdef __WXGTK20__ |
473 | /* seems gtk_widget_set_default_visual no longer exists? */ | |
474 | GdkVisual* vis = gtk_widget_get_default_visual(); | |
475 | #else | |
b134516c RR |
476 | GdkVisual* vis = gdk_visual_get_best(); |
477 | gtk_widget_set_default_visual( vis ); | |
2d4dc3a4 | 478 | #endif |
f6fcbb63 | 479 | |
b134516c RR |
480 | GdkColormap *colormap = gdk_colormap_new( vis, FALSE ); |
481 | gtk_widget_set_default_colormap( colormap ); | |
094637f6 VZ |
482 | |
483 | visual = vis; | |
b134516c | 484 | } |
d76fe38b | 485 | |
005f5d18 | 486 | // Nothing to do for 15, 16, 24, 32 bit displays |
f6fcbb63 | 487 | if (visual->depth > 8) return TRUE; |
60acb947 | 488 | |
005f5d18 | 489 | // initialize color cube for 8-bit color reduction dithering |
60acb947 | 490 | |
f6fcbb63 | 491 | GdkColormap *cmap = gtk_widget_get_default_colormap(); |
60acb947 | 492 | |
f6fcbb63 RR |
493 | m_colorCube = (unsigned char*)malloc(32 * 32 * 32); |
494 | ||
f03fc89f VZ |
495 | for (int r = 0; r < 32; r++) |
496 | { | |
8801832d VZ |
497 | for (int g = 0; g < 32; g++) |
498 | { | |
499 | for (int b = 0; b < 32; b++) | |
500 | { | |
501 | int rr = (r << 3) | (r >> 2); | |
502 | int gg = (g << 3) | (g >> 2); | |
503 | int bb = (b << 3) | (b >> 2); | |
60acb947 | 504 | |
f03fc89f VZ |
505 | int index = -1; |
506 | ||
f6fcbb63 | 507 | GdkColor *colors = cmap->colors; |
ca26177c | 508 | if (colors) |
f03fc89f VZ |
509 | { |
510 | int max = 3 * 65536; | |
511 | ||
512 | for (int i = 0; i < cmap->size; i++) | |
513 | { | |
514 | int rdiff = ((rr << 8) - colors[i].red); | |
515 | int gdiff = ((gg << 8) - colors[i].green); | |
516 | int bdiff = ((bb << 8) - colors[i].blue); | |
517 | int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff); | |
518 | if (sum < max) | |
094637f6 | 519 | { |
f03fc89f VZ |
520 | index = i; max = sum; |
521 | } | |
522 | } | |
f6fcbb63 | 523 | } |
094637f6 VZ |
524 | else |
525 | { | |
005f5d18 | 526 | // assume 8-bit true or static colors. this really exists |
094637f6 VZ |
527 | GdkVisual* vis = gdk_colormap_get_visual( cmap ); |
528 | index = (r >> (5 - vis->red_prec)) << vis->red_shift; | |
529 | index |= (g >> (5 - vis->green_prec)) << vis->green_shift; | |
530 | index |= (b >> (5 - vis->blue_prec)) << vis->blue_shift; | |
094637f6 | 531 | } |
8801832d VZ |
532 | m_colorCube[ (r*1024) + (g*32) + b ] = index; |
533 | } | |
534 | } | |
f6fcbb63 | 535 | } |
c801d85f | 536 | |
bbe0af5b RR |
537 | return TRUE; |
538 | } | |
539 | ||
005f5d18 RR |
540 | GdkVisual *wxApp::GetGdkVisual() |
541 | { | |
542 | GdkVisual *visual = NULL; | |
be88a6ad | 543 | |
005f5d18 | 544 | if (m_glVisualInfo) |
7b775074 | 545 | visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid ); |
005f5d18 RR |
546 | else |
547 | visual = gdk_window_get_visual( wxGetRootWindow()->window ); | |
be88a6ad | 548 | |
005f5d18 | 549 | wxASSERT( visual ); |
be88a6ad | 550 | |
005f5d18 RR |
551 | return visual; |
552 | } | |
553 | ||
60acb947 | 554 | bool wxApp::ProcessIdle() |
53010e52 | 555 | { |
2b5f62a0 VZ |
556 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); |
557 | node = wxTopLevelWindows.GetFirst(); | |
558 | while (node) | |
559 | { | |
560 | wxWindow* win = node->GetData(); | |
561 | CallInternalIdle( win ); | |
562 | ||
563 | node = node->GetNext(); | |
564 | } | |
565 | ||
ec758a20 RR |
566 | wxIdleEvent event; |
567 | event.SetEventObject( this ); | |
568 | ProcessEvent( event ); | |
0cf2cb36 | 569 | |
ec758a20 | 570 | return event.MoreRequested(); |
ff7b1510 | 571 | } |
53010e52 RR |
572 | |
573 | void wxApp::OnIdle( wxIdleEvent &event ) | |
c801d85f | 574 | { |
956dbab1 | 575 | static bool s_inOnIdle = FALSE; |
2286341c | 576 | |
5375a1f5 | 577 | // Avoid recursion (via ProcessEvent default case) |
956dbab1 | 578 | if (s_inOnIdle) |
ec758a20 | 579 | return; |
2286341c | 580 | |
956dbab1 | 581 | s_inOnIdle = TRUE; |
53010e52 | 582 | |
5375a1f5 RR |
583 | // Resend in the main thread events which have been prepared in other |
584 | // threads | |
7214297d GL |
585 | ProcessPendingEvents(); |
586 | ||
5375a1f5 | 587 | // 'Garbage' collection of windows deleted with Close() |
ec758a20 | 588 | DeletePendingObjects(); |
53010e52 | 589 | |
5375a1f5 | 590 | // Send OnIdle events to all windows |
ec758a20 | 591 | bool needMore = SendIdleEvents(); |
53010e52 | 592 | |
ec758a20 RR |
593 | if (needMore) |
594 | event.RequestMore(TRUE); | |
53010e52 | 595 | |
956dbab1 | 596 | s_inOnIdle = FALSE; |
ff7b1510 | 597 | } |
53010e52 | 598 | |
60acb947 | 599 | bool wxApp::SendIdleEvents() |
53010e52 RR |
600 | { |
601 | bool needMore = FALSE; | |
e0253070 | 602 | |
e146b8c8 | 603 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); |
ec758a20 RR |
604 | while (node) |
605 | { | |
e146b8c8 | 606 | wxWindow* win = node->GetData(); |
f3855ef0 | 607 | if (SendIdleEvents(win)) |
53010e52 | 608 | needMore = TRUE; |
be88a6ad | 609 | |
e146b8c8 | 610 | node = node->GetNext(); |
ec758a20 | 611 | } |
e146b8c8 | 612 | |
53010e52 | 613 | return needMore; |
ff7b1510 | 614 | } |
53010e52 | 615 | |
010afced RR |
616 | bool wxApp::CallInternalIdle( wxWindow* win ) |
617 | { | |
618 | win->OnInternalIdle(); | |
619 | ||
b1d4dd7a | 620 | wxWindowList::Node *node = win->GetChildren().GetFirst(); |
010afced RR |
621 | while (node) |
622 | { | |
b1d4dd7a | 623 | wxWindow *win = node->GetData(); |
010afced | 624 | |
b1d4dd7a RL |
625 | CallInternalIdle( win ); |
626 | node = node->GetNext(); | |
010afced | 627 | } |
be88a6ad | 628 | |
010afced RR |
629 | return TRUE; |
630 | } | |
631 | ||
53010e52 RR |
632 | bool wxApp::SendIdleEvents( wxWindow* win ) |
633 | { | |
634 | bool needMore = FALSE; | |
635 | ||
8bbe427f VZ |
636 | wxIdleEvent event; |
637 | event.SetEventObject(win); | |
60acb947 | 638 | |
f6bcfd97 | 639 | win->GetEventHandler()->ProcessEvent(event); |
2b5f62a0 | 640 | |
53010e52 RR |
641 | if (event.MoreRequested()) |
642 | needMore = TRUE; | |
643 | ||
b1d4dd7a | 644 | wxWindowList::Node *node = win->GetChildren().GetFirst(); |
8bbe427f VZ |
645 | while (node) |
646 | { | |
b1d4dd7a RL |
647 | wxWindow *win = node->GetData(); |
648 | ||
8bbe427f | 649 | if (SendIdleEvents(win)) |
53010e52 | 650 | needMore = TRUE; |
b1d4dd7a | 651 | node = node->GetNext(); |
8bbe427f | 652 | } |
be88a6ad | 653 | |
0d1dff01 | 654 | return needMore; |
ff7b1510 | 655 | } |
c801d85f | 656 | |
60acb947 | 657 | int wxApp::MainLoop() |
c801d85f | 658 | { |
ec758a20 RR |
659 | gtk_main(); |
660 | return 0; | |
ff7b1510 | 661 | } |
c801d85f | 662 | |
60acb947 | 663 | void wxApp::ExitMainLoop() |
c801d85f | 664 | { |
7ec2881a RR |
665 | if (gtk_main_level() > 0) |
666 | gtk_main_quit(); | |
ff7b1510 | 667 | } |
c801d85f | 668 | |
60acb947 | 669 | bool wxApp::Initialized() |
c801d85f | 670 | { |
ec758a20 | 671 | return m_initialized; |
ff7b1510 | 672 | } |
c801d85f | 673 | |
60acb947 | 674 | bool wxApp::Pending() |
c801d85f | 675 | { |
acfd422a | 676 | return (gtk_events_pending() > 0); |
ff7b1510 | 677 | } |
c801d85f | 678 | |
60acb947 | 679 | void wxApp::Dispatch() |
c801d85f | 680 | { |
8801832d | 681 | gtk_main_iteration(); |
ff7b1510 | 682 | } |
c801d85f | 683 | |
60acb947 | 684 | void wxApp::DeletePendingObjects() |
c801d85f | 685 | { |
b1d4dd7a | 686 | wxNode *node = wxPendingDelete.GetFirst(); |
ec758a20 RR |
687 | while (node) |
688 | { | |
b1d4dd7a | 689 | wxObject *obj = (wxObject *)node->GetData(); |
0cf2cb36 | 690 | |
ec758a20 | 691 | delete obj; |
c801d85f | 692 | |
f03fc89f VZ |
693 | if (wxPendingDelete.Find(obj)) |
694 | delete node; | |
c801d85f | 695 | |
b1d4dd7a | 696 | node = wxPendingDelete.GetFirst(); |
ec758a20 | 697 | } |
ff7b1510 | 698 | } |
c801d85f | 699 | |
60acb947 | 700 | bool wxApp::Initialize() |
c801d85f | 701 | { |
0d2a2b60 | 702 | wxClassInfo::InitializeClasses(); |
60acb947 | 703 | |
4d3a259a GL |
704 | // GL: I'm annoyed ... I don't know where to put this and I don't want to |
705 | // create a module for that as it's part of the core. | |
706 | #if wxUSE_THREADS | |
707 | wxPendingEvents = new wxList(); | |
708 | wxPendingEventsLocker = new wxCriticalSection(); | |
709 | #endif | |
710 | ||
0d2a2b60 RR |
711 | wxTheColourDatabase = new wxColourDatabase( wxKEY_STRING ); |
712 | wxTheColourDatabase->Initialize(); | |
a3622daa | 713 | |
0d2a2b60 RR |
714 | wxInitializeStockLists(); |
715 | wxInitializeStockObjects(); | |
c801d85f | 716 | |
0d2a2b60 | 717 | wxModule::RegisterModules(); |
2b5f62a0 VZ |
718 | if (!wxModule::InitializeModules()) |
719 | return FALSE; | |
720 | ||
721 | #if wxUSE_INTL | |
722 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
723 | #endif | |
60acb947 | 724 | |
0d2a2b60 | 725 | return TRUE; |
ff7b1510 | 726 | } |
c801d85f | 727 | |
60acb947 | 728 | void wxApp::CleanUp() |
c801d85f | 729 | { |
0d2a2b60 | 730 | wxModule::CleanUpModules(); |
0cf2cb36 | 731 | |
a11672a4 | 732 | delete wxTheColourDatabase; |
0d2a2b60 | 733 | wxTheColourDatabase = (wxColourDatabase*) NULL; |
60acb947 | 734 | |
0d2a2b60 RR |
735 | wxDeleteStockObjects(); |
736 | ||
ec758a20 | 737 | wxDeleteStockLists(); |
a3622daa | 738 | |
0d2a2b60 RR |
739 | delete wxTheApp; |
740 | wxTheApp = (wxApp*) NULL; | |
741 | ||
a11672a4 RR |
742 | wxClassInfo::CleanUpClasses(); |
743 | ||
4d3a259a GL |
744 | #if wxUSE_THREADS |
745 | delete wxPendingEvents; | |
1ae2e0af | 746 | wxPendingEvents = NULL; |
4d3a259a | 747 | delete wxPendingEventsLocker; |
1ae2e0af | 748 | wxPendingEventsLocker = NULL; |
4d3a259a GL |
749 | #endif |
750 | ||
60acb947 | 751 | // check for memory leaks |
0d2a2b60 | 752 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
6981d3ec | 753 | if (wxDebugContext::CountObjectsLeft(TRUE) > 0) |
0d2a2b60 | 754 | { |
223d09f6 | 755 | wxLogDebug(wxT("There were memory leaks.\n")); |
0d2a2b60 RR |
756 | wxDebugContext::Dump(); |
757 | wxDebugContext::PrintStatistics(); | |
758 | } | |
8801832d | 759 | #endif // Debug |
0d2a2b60 | 760 | |
88ac883a | 761 | #if wxUSE_LOG |
60acb947 | 762 | // do this as the very last thing because everything else can log messages |
0d2a2b60 | 763 | wxLog::DontCreateOnDemand(); |
60acb947 | 764 | |
0d2a2b60 | 765 | wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL ); |
60acb947 VZ |
766 | if (oldLog) |
767 | delete oldLog; | |
88ac883a | 768 | #endif // wxUSE_LOG |
c19c1ca9 | 769 | } |
c801d85f KB |
770 | |
771 | //----------------------------------------------------------------------------- | |
772 | // wxEntry | |
773 | //----------------------------------------------------------------------------- | |
774 | ||
17154fc8 VZ |
775 | // NB: argc and argv may be changed here, pass by reference! |
776 | int wxEntryStart( int& argc, char *argv[] ) | |
c801d85f | 777 | { |
924ef850 | 778 | #if wxUSE_THREADS |
005f5d18 RR |
779 | // GTK 1.2 up to version 1.2.3 has broken threads |
780 | if ((gtk_major_version == 1) && | |
8f75cb6c | 781 | (gtk_minor_version == 2) && |
2286341c | 782 | (gtk_micro_version < 4)) |
96997d65 | 783 | { |
fb65642c | 784 | printf( "wxWindows warning: GUI threading disabled due to outdated GTK version\n" ); |
8f75cb6c RR |
785 | } |
786 | else | |
787 | { | |
788 | g_thread_init(NULL); | |
789 | } | |
924ef850 | 790 | #endif |
2286341c | 791 | |
0d2a2b60 | 792 | gtk_set_locale(); |
c801d85f | 793 | |
f9862abd JS |
794 | // We should have the wxUSE_WCHAR_T test on the _outside_ |
795 | #if wxUSE_WCHAR_T | |
2d4dc3a4 OK |
796 | #if defined(__WXGTK20__) |
797 | // gtk+ 2.0 supports Unicode through UTF-8 strings | |
798 | wxConvCurrent = &wxConvUTF8; | |
f9862abd | 799 | #else |
dcf924a3 | 800 | if (!wxOKlibc()) wxConvCurrent = &wxConvLocal; |
f9862abd | 801 | #endif |
fd9811b1 RR |
802 | #else |
803 | if (!wxOKlibc()) wxConvCurrent = (wxMBConv*) NULL; | |
804 | #endif | |
002f4218 | 805 | |
0d2a2b60 | 806 | gtk_init( &argc, &argv ); |
0cf2cb36 | 807 | |
f3466c55 JS |
808 | /* we can not enter threads before gtk_init is done */ |
809 | gdk_threads_enter(); | |
810 | ||
f0492f7d | 811 | wxSetDetectableAutoRepeat( TRUE ); |
094637f6 | 812 | |
60acb947 | 813 | if (!wxApp::Initialize()) |
924ef850 RR |
814 | { |
815 | gdk_threads_leave(); | |
60acb947 | 816 | return -1; |
924ef850 | 817 | } |
0cf2cb36 | 818 | |
954de0f1 RD |
819 | return 0; |
820 | } | |
821 | ||
c2fa61e8 | 822 | |
954de0f1 RD |
823 | int wxEntryInitGui() |
824 | { | |
825 | int retValue = 0; | |
826 | ||
827 | if ( !wxTheApp->OnInitGui() ) | |
828 | retValue = -1; | |
829 | ||
c2fa61e8 | 830 | wxGetRootWindow(); |
954de0f1 RD |
831 | |
832 | return retValue; | |
833 | } | |
834 | ||
835 | ||
836 | void wxEntryCleanup() | |
837 | { | |
838 | #if wxUSE_LOG | |
839 | // flush the logged messages if any | |
840 | wxLog *log = wxLog::GetActiveTarget(); | |
841 | if (log != NULL && log->HasPendingMessages()) | |
842 | log->Flush(); | |
843 | ||
844 | // continuing to use user defined log target is unsafe from now on because | |
845 | // some resources may be already unavailable, so replace it by something | |
846 | // more safe | |
847 | wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr); | |
848 | if ( oldlog ) | |
849 | delete oldlog; | |
850 | #endif // wxUSE_LOG | |
851 | ||
852 | wxApp::CleanUp(); | |
853 | ||
854 | gdk_threads_leave(); | |
855 | } | |
856 | ||
857 | ||
954de0f1 RD |
858 | int wxEntry( int argc, char *argv[] ) |
859 | { | |
2db0bbde JS |
860 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
861 | // This seems to be necessary since there are 'rogue' | |
862 | // objects present at this point (perhaps global objects?) | |
863 | // Setting a checkpoint will ignore them as far as the | |
864 | // memory checking facility is concerned. | |
865 | // Of course you may argue that memory allocated in globals should be | |
866 | // checked, but this is a reasonable compromise. | |
867 | wxDebugContext::SetCheckpoint(); | |
868 | #endif | |
a37a5a73 | 869 | int err = wxEntryStart(argc, argv); |
954de0f1 RD |
870 | if (err) |
871 | return err; | |
872 | ||
ec758a20 | 873 | if (!wxTheApp) |
c801d85f | 874 | { |
60acb947 | 875 | wxCHECK_MSG( wxApp::GetInitializerFunction(), -1, |
223d09f6 | 876 | wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") ); |
0cf2cb36 | 877 | |
ec758a20 | 878 | wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction(); |
0cf2cb36 | 879 | |
ec758a20 | 880 | wxObject *test_app = app_ini(); |
0cf2cb36 | 881 | |
ec758a20 RR |
882 | wxTheApp = (wxApp*) test_app; |
883 | } | |
0cf2cb36 | 884 | |
223d09f6 | 885 | wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") ); |
c801d85f | 886 | |
ec758a20 | 887 | wxTheApp->argc = argc; |
6de92826 OK |
888 | #if wxUSE_UNICODE |
889 | wxTheApp->argv = new wxChar*[argc+1]; | |
890 | int mb_argc = 0; | |
2286341c | 891 | while (mb_argc < argc) |
924ef850 RR |
892 | { |
893 | wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc])); | |
894 | mb_argc++; | |
6de92826 OK |
895 | } |
896 | wxTheApp->argv[mb_argc] = (wxChar *)NULL; | |
897 | #else | |
ec758a20 | 898 | wxTheApp->argv = argv; |
6de92826 | 899 | #endif |
0cf2cb36 | 900 | |
2b5f62a0 VZ |
901 | if (wxTheApp->argc > 0) |
902 | { | |
903 | wxFileName fname( wxTheApp->argv[0] ); | |
904 | wxTheApp->SetAppName( fname.GetName() ); | |
905 | } | |
e0253070 | 906 | |
954de0f1 RD |
907 | int retValue; |
908 | retValue = wxEntryInitGui(); | |
68df5777 | 909 | |
8801832d VZ |
910 | // Here frames insert themselves automatically into wxTopLevelWindows by |
911 | // getting created in OnInit(). | |
0151c3eb VZ |
912 | if ( retValue == 0 ) |
913 | { | |
914 | if ( !wxTheApp->OnInit() ) | |
915 | retValue = -1; | |
916 | } | |
0cf2cb36 | 917 | |
0151c3eb VZ |
918 | if ( retValue == 0 ) |
919 | { | |
047ac72b | 920 | // Delete pending toplevel windows |
cfb50f14 | 921 | wxTheApp->DeletePendingObjects(); |
094637f6 | 922 | |
047ac72b RR |
923 | // When is the app not initialized ? |
924 | wxTheApp->m_initialized = TRUE; | |
0cf2cb36 | 925 | |
0151c3eb | 926 | if (wxTheApp->Initialized()) |
094637f6 | 927 | { |
2286341c | 928 | wxTheApp->OnRun(); |
0cf2cb36 | 929 | |
cfb50f14 | 930 | wxWindow *topWindow = wxTheApp->GetTopWindow(); |
be88a6ad | 931 | |
047ac72b RR |
932 | // Delete all pending windows if any |
933 | wxTheApp->DeletePendingObjects(); | |
be88a6ad RD |
934 | |
935 | // Reset top window | |
cfb50f14 | 936 | if (topWindow) |
047ac72b | 937 | wxTheApp->SetTopWindow( (wxWindow*) NULL ); |
2286341c VZ |
938 | |
939 | retValue = wxTheApp->OnExit(); | |
0d2a2b60 | 940 | } |
0151c3eb | 941 | } |
0cf2cb36 | 942 | |
954de0f1 | 943 | wxEntryCleanup(); |
924ef850 | 944 | |
ec758a20 | 945 | return retValue; |
ff7b1510 | 946 | } |
1a56f55c | 947 | |
a5f1fd3e VZ |
948 | #ifdef __WXDEBUG__ |
949 | ||
be88a6ad | 950 | void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg) |
a5f1fd3e VZ |
951 | { |
952 | m_isInAssert = TRUE; | |
953 | ||
be88a6ad | 954 | wxAppBase::OnAssert(file, line, cond, msg); |
a5f1fd3e VZ |
955 | |
956 | m_isInAssert = FALSE; | |
957 | } | |
958 | ||
959 | #endif // __WXDEBUG__ | |
960 |