]>
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" |
afb74891 | 31 | |
06cfab17 | 32 | #if wxUSE_WX_RESOURCES |
afb74891 | 33 | #include "wx/resource.h" |
f5abe911 | 34 | #endif |
afb74891 | 35 | |
031b2a7b | 36 | #include "wx/module.h" |
4bc67cc5 | 37 | #include "wx/image.h" |
afb74891 | 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 | ||
e8106239 | 75 | #include "wx/gtk/win_gtk.h" |
c801d85f | 76 | |
20e05ffb | 77 | #include <gtk/gtk.h> |
24178e4a | 78 | |
83624f79 | 79 | |
c801d85f KB |
80 | //----------------------------------------------------------------------------- |
81 | // global data | |
82 | //----------------------------------------------------------------------------- | |
83 | ||
c67daf87 | 84 | wxApp *wxTheApp = (wxApp *) NULL; |
32d4bfd1 | 85 | wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL; |
c801d85f | 86 | |
96997d65 RR |
87 | bool g_mainThreadLocked = FALSE; |
88 | gint g_pendingTag = 0; | |
3ac8d3bc | 89 | |
c2fa61e8 | 90 | static GtkWidget *gs_RootWindow = (GtkWidget*) NULL; |
d76fe38b | 91 | |
c801d85f | 92 | //----------------------------------------------------------------------------- |
3133cb9f | 93 | // idle system |
c801d85f KB |
94 | //----------------------------------------------------------------------------- |
95 | ||
3133cb9f | 96 | extern bool g_isIdle; |
c4fda16b | 97 | |
90350682 | 98 | void wxapp_install_idle_handler(); |
bf9e3e73 | 99 | |
c801d85f | 100 | //----------------------------------------------------------------------------- |
bf9e3e73 | 101 | // wxExit |
c801d85f KB |
102 | //----------------------------------------------------------------------------- |
103 | ||
60acb947 | 104 | void wxExit() |
c801d85f | 105 | { |
ec758a20 | 106 | gtk_main_quit(); |
ff7b1510 | 107 | } |
c801d85f | 108 | |
bf9e3e73 RR |
109 | //----------------------------------------------------------------------------- |
110 | // wxYield | |
111 | //----------------------------------------------------------------------------- | |
53a8af59 | 112 | |
1ee339ee VZ |
113 | // not static because used by textctrl.cpp |
114 | // | |
115 | // MT-FIXME | |
116 | bool wxIsInsideYield = FALSE; | |
117 | ||
8461e4c2 | 118 | bool wxApp::Yield(bool onlyIfNeeded) |
c801d85f | 119 | { |
1ee339ee | 120 | if ( wxIsInsideYield ) |
8461e4c2 VZ |
121 | { |
122 | if ( !onlyIfNeeded ) | |
123 | { | |
124 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
125 | } | |
126 | ||
127 | return FALSE; | |
128 | } | |
129 | ||
6dc5fd71 VZ |
130 | #if wxUSE_THREADS |
131 | if ( !wxThread::IsMain() ) | |
132 | { | |
133 | // can't call gtk_main_iteration() from other threads like this | |
134 | return TRUE; | |
135 | } | |
136 | #endif // wxUSE_THREADS | |
137 | ||
1ee339ee | 138 | wxIsInsideYield = TRUE; |
e90c1d2a | 139 | |
99ba739f | 140 | if (!g_isIdle) |
acfd422a | 141 | { |
99ba739f RR |
142 | // We need to remove idle callbacks or the loop will |
143 | // never finish. | |
8461e4c2 VZ |
144 | gtk_idle_remove( m_idleTag ); |
145 | m_idleTag = 0; | |
99ba739f | 146 | g_isIdle = TRUE; |
956dbab1 | 147 | } |
acfd422a | 148 | |
2ed3265e VZ |
149 | // disable log flushing from here because a call to wxYield() shouldn't |
150 | // normally result in message boxes popping up &c | |
151 | wxLog::Suspend(); | |
152 | ||
406a6f6b VZ |
153 | while (gtk_events_pending()) |
154 | gtk_main_iteration(); | |
155 | ||
5375a1f5 RR |
156 | // It's necessary to call ProcessIdle() to update the frames sizes which |
157 | // might have been changed (it also will update other things set from | |
be88a6ad | 158 | // OnUpdateUI() which is a nice (and desired) side effect). But we |
5375a1f5 RR |
159 | // call ProcessIdle() only once since this is not meant for longish |
160 | // background jobs (controlled by wxIdleEvent::RequestMore() and the | |
161 | // return value of Processidle(). | |
162 | ProcessIdle(); | |
2ed3265e VZ |
163 | |
164 | // let the logs be flashed again | |
165 | wxLog::Resume(); | |
7741c4e1 | 166 | |
1ee339ee | 167 | wxIsInsideYield = FALSE; |
99ba739f | 168 | |
acfd422a RR |
169 | return TRUE; |
170 | } | |
171 | ||
bf9e3e73 RR |
172 | //----------------------------------------------------------------------------- |
173 | // wxWakeUpIdle | |
174 | //----------------------------------------------------------------------------- | |
175 | ||
176 | void wxWakeUpIdle() | |
177 | { | |
924ef850 RR |
178 | #if wxUSE_THREADS |
179 | if (!wxThread::IsMain()) | |
ce6d2511 | 180 | wxMutexGuiEnter(); |
924ef850 RR |
181 | #endif |
182 | ||
2286341c | 183 | if (g_isIdle) |
bf9e3e73 | 184 | wxapp_install_idle_handler(); |
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 | { |
3133cb9f | 369 | wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") ); |
c2fa61e8 | 370 | |
3133cb9f | 371 | g_isIdle = FALSE; |
96997d65 | 372 | |
3133cb9f RR |
373 | if (g_pendingTag == 0) |
374 | g_pendingTag = gtk_idle_add_priority( 900, wxapp_pending_callback, (gpointer) NULL ); | |
e90c1d2a | 375 | |
3133cb9f RR |
376 | // This routine gets called by all event handlers |
377 | // indicating that the idle is over. It may also | |
378 | // get called from other thread for sending events | |
379 | // to the main thread (and processing these in | |
380 | // idle time). Very low priority. | |
381 | wxTheApp->m_idleTag = gtk_idle_add_priority( 1000, wxapp_idle_callback, (gpointer) NULL ); | |
b453e1b2 RR |
382 | } |
383 | ||
005f5d18 RR |
384 | //----------------------------------------------------------------------------- |
385 | // Access to the root window global | |
386 | //----------------------------------------------------------------------------- | |
387 | ||
388 | GtkWidget* wxGetRootWindow() | |
389 | { | |
390 | if (gs_RootWindow == NULL) | |
391 | { | |
392 | gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL ); | |
393 | gtk_widget_realize( gs_RootWindow ); | |
394 | } | |
395 | return gs_RootWindow; | |
396 | } | |
397 | ||
c801d85f KB |
398 | //----------------------------------------------------------------------------- |
399 | // wxApp | |
400 | //----------------------------------------------------------------------------- | |
401 | ||
402 | IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler) | |
403 | ||
53010e52 RR |
404 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) |
405 | EVT_IDLE(wxApp::OnIdle) | |
406 | END_EVENT_TABLE() | |
407 | ||
c801d85f KB |
408 | wxApp::wxApp() |
409 | { | |
a5f1fd3e VZ |
410 | m_initialized = FALSE; |
411 | #ifdef __WXDEBUG__ | |
412 | m_isInAssert = FALSE; | |
413 | #endif // __WXDEBUG__ | |
414 | ||
df5ddbca RR |
415 | m_idleTag = 0; |
416 | wxapp_install_idle_handler(); | |
094637f6 | 417 | |
6b3eb77a | 418 | #if wxUSE_THREADS |
3133cb9f | 419 | g_main_set_poll_func( wxapp_poll_func ); |
6b3eb77a | 420 | #endif |
60acb947 | 421 | |
f6fcbb63 | 422 | m_colorCube = (unsigned char*) NULL; |
be88a6ad | 423 | |
a6f5aa49 VZ |
424 | // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp |
425 | m_glVisualInfo = (void *) NULL; | |
ff7b1510 | 426 | } |
c801d85f | 427 | |
60acb947 | 428 | wxApp::~wxApp() |
c801d85f | 429 | { |
acfd422a | 430 | if (m_idleTag) gtk_idle_remove( m_idleTag ); |
60acb947 | 431 | |
f6fcbb63 | 432 | if (m_colorCube) free(m_colorCube); |
ff7b1510 | 433 | } |
c801d85f | 434 | |
0d2a2b60 | 435 | bool wxApp::OnInitGui() |
c801d85f | 436 | { |
1e6feb95 VZ |
437 | if ( !wxAppBase::OnInitGui() ) |
438 | return FALSE; | |
439 | ||
b134516c RR |
440 | GdkVisual *visual = gdk_visual_get_system(); |
441 | ||
a6f5aa49 VZ |
442 | // if this is a wxGLApp (derived from wxApp), and we've already |
443 | // chosen a specific visual, then derive the GdkVisual from that | |
005f5d18 RR |
444 | if (m_glVisualInfo != NULL) |
445 | { | |
a6f5aa49 | 446 | #ifdef __WXGTK20__ |
005f5d18 | 447 | // seems gtk_widget_set_default_visual no longer exists? |
a6f5aa49 VZ |
448 | GdkVisual* vis = gtk_widget_get_default_visual(); |
449 | #else | |
be88a6ad | 450 | GdkVisual* vis = gdkx_visual_get( |
a6f5aa49 VZ |
451 | ((XVisualInfo *) m_glVisualInfo) ->visualid ); |
452 | gtk_widget_set_default_visual( vis ); | |
453 | #endif | |
454 | ||
455 | GdkColormap *colormap = gdk_colormap_new( vis, FALSE ); | |
456 | gtk_widget_set_default_colormap( colormap ); | |
457 | ||
458 | visual = vis; | |
459 | } | |
be88a6ad | 460 | |
005f5d18 RR |
461 | // On some machines, the default visual is just 256 colours, so |
462 | // we make sure we get the best. This can sometimes be wasteful. | |
2286341c | 463 | |
005f5d18 RR |
464 | else |
465 | if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual)) | |
b134516c | 466 | { |
2d4dc3a4 OK |
467 | #ifdef __WXGTK20__ |
468 | /* seems gtk_widget_set_default_visual no longer exists? */ | |
469 | GdkVisual* vis = gtk_widget_get_default_visual(); | |
470 | #else | |
b134516c RR |
471 | GdkVisual* vis = gdk_visual_get_best(); |
472 | gtk_widget_set_default_visual( vis ); | |
2d4dc3a4 | 473 | #endif |
f6fcbb63 | 474 | |
b134516c RR |
475 | GdkColormap *colormap = gdk_colormap_new( vis, FALSE ); |
476 | gtk_widget_set_default_colormap( colormap ); | |
094637f6 VZ |
477 | |
478 | visual = vis; | |
b134516c | 479 | } |
d76fe38b | 480 | |
005f5d18 | 481 | // Nothing to do for 15, 16, 24, 32 bit displays |
f6fcbb63 | 482 | if (visual->depth > 8) return TRUE; |
60acb947 | 483 | |
005f5d18 | 484 | // initialize color cube for 8-bit color reduction dithering |
60acb947 | 485 | |
f6fcbb63 | 486 | GdkColormap *cmap = gtk_widget_get_default_colormap(); |
60acb947 | 487 | |
f6fcbb63 RR |
488 | m_colorCube = (unsigned char*)malloc(32 * 32 * 32); |
489 | ||
f03fc89f VZ |
490 | for (int r = 0; r < 32; r++) |
491 | { | |
8801832d VZ |
492 | for (int g = 0; g < 32; g++) |
493 | { | |
494 | for (int b = 0; b < 32; b++) | |
495 | { | |
496 | int rr = (r << 3) | (r >> 2); | |
497 | int gg = (g << 3) | (g >> 2); | |
498 | int bb = (b << 3) | (b >> 2); | |
60acb947 | 499 | |
f03fc89f VZ |
500 | int index = -1; |
501 | ||
f6fcbb63 | 502 | GdkColor *colors = cmap->colors; |
ca26177c | 503 | if (colors) |
f03fc89f VZ |
504 | { |
505 | int max = 3 * 65536; | |
506 | ||
507 | for (int i = 0; i < cmap->size; i++) | |
508 | { | |
509 | int rdiff = ((rr << 8) - colors[i].red); | |
510 | int gdiff = ((gg << 8) - colors[i].green); | |
511 | int bdiff = ((bb << 8) - colors[i].blue); | |
512 | int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff); | |
513 | if (sum < max) | |
094637f6 | 514 | { |
f03fc89f VZ |
515 | index = i; max = sum; |
516 | } | |
517 | } | |
f6fcbb63 | 518 | } |
094637f6 VZ |
519 | else |
520 | { | |
005f5d18 | 521 | // assume 8-bit true or static colors. this really exists |
094637f6 VZ |
522 | GdkVisual* vis = gdk_colormap_get_visual( cmap ); |
523 | index = (r >> (5 - vis->red_prec)) << vis->red_shift; | |
524 | index |= (g >> (5 - vis->green_prec)) << vis->green_shift; | |
525 | index |= (b >> (5 - vis->blue_prec)) << vis->blue_shift; | |
094637f6 | 526 | } |
8801832d VZ |
527 | m_colorCube[ (r*1024) + (g*32) + b ] = index; |
528 | } | |
529 | } | |
f6fcbb63 | 530 | } |
c801d85f | 531 | |
bbe0af5b RR |
532 | return TRUE; |
533 | } | |
534 | ||
005f5d18 RR |
535 | GdkVisual *wxApp::GetGdkVisual() |
536 | { | |
537 | GdkVisual *visual = NULL; | |
be88a6ad | 538 | |
005f5d18 | 539 | if (m_glVisualInfo) |
7b775074 | 540 | visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid ); |
005f5d18 RR |
541 | else |
542 | visual = gdk_window_get_visual( wxGetRootWindow()->window ); | |
be88a6ad | 543 | |
005f5d18 | 544 | wxASSERT( visual ); |
be88a6ad | 545 | |
005f5d18 RR |
546 | return visual; |
547 | } | |
548 | ||
60acb947 | 549 | bool wxApp::ProcessIdle() |
53010e52 | 550 | { |
2b5f62a0 VZ |
551 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); |
552 | node = wxTopLevelWindows.GetFirst(); | |
553 | while (node) | |
554 | { | |
555 | wxWindow* win = node->GetData(); | |
556 | CallInternalIdle( win ); | |
557 | ||
558 | node = node->GetNext(); | |
559 | } | |
560 | ||
ec758a20 RR |
561 | wxIdleEvent event; |
562 | event.SetEventObject( this ); | |
563 | ProcessEvent( event ); | |
0cf2cb36 | 564 | |
ec758a20 | 565 | return event.MoreRequested(); |
ff7b1510 | 566 | } |
53010e52 RR |
567 | |
568 | void wxApp::OnIdle( wxIdleEvent &event ) | |
c801d85f | 569 | { |
956dbab1 | 570 | static bool s_inOnIdle = FALSE; |
2286341c | 571 | |
5375a1f5 | 572 | // Avoid recursion (via ProcessEvent default case) |
956dbab1 | 573 | if (s_inOnIdle) |
ec758a20 | 574 | return; |
2286341c | 575 | |
956dbab1 | 576 | s_inOnIdle = TRUE; |
53010e52 | 577 | |
5375a1f5 RR |
578 | // Resend in the main thread events which have been prepared in other |
579 | // threads | |
7214297d GL |
580 | ProcessPendingEvents(); |
581 | ||
5375a1f5 | 582 | // 'Garbage' collection of windows deleted with Close() |
ec758a20 | 583 | DeletePendingObjects(); |
53010e52 | 584 | |
5375a1f5 | 585 | // Send OnIdle events to all windows |
ec758a20 | 586 | bool needMore = SendIdleEvents(); |
53010e52 | 587 | |
ec758a20 RR |
588 | if (needMore) |
589 | event.RequestMore(TRUE); | |
53010e52 | 590 | |
956dbab1 | 591 | s_inOnIdle = FALSE; |
ff7b1510 | 592 | } |
53010e52 | 593 | |
60acb947 | 594 | bool wxApp::SendIdleEvents() |
53010e52 RR |
595 | { |
596 | bool needMore = FALSE; | |
e0253070 | 597 | |
e146b8c8 | 598 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); |
ec758a20 RR |
599 | while (node) |
600 | { | |
e146b8c8 | 601 | wxWindow* win = node->GetData(); |
f3855ef0 | 602 | if (SendIdleEvents(win)) |
53010e52 | 603 | needMore = TRUE; |
be88a6ad | 604 | |
e146b8c8 | 605 | node = node->GetNext(); |
ec758a20 | 606 | } |
e146b8c8 | 607 | |
53010e52 | 608 | return needMore; |
ff7b1510 | 609 | } |
53010e52 | 610 | |
010afced RR |
611 | bool wxApp::CallInternalIdle( wxWindow* win ) |
612 | { | |
613 | win->OnInternalIdle(); | |
614 | ||
b1d4dd7a | 615 | wxWindowList::Node *node = win->GetChildren().GetFirst(); |
010afced RR |
616 | while (node) |
617 | { | |
b1d4dd7a | 618 | wxWindow *win = node->GetData(); |
010afced | 619 | |
b1d4dd7a RL |
620 | CallInternalIdle( win ); |
621 | node = node->GetNext(); | |
010afced | 622 | } |
be88a6ad | 623 | |
010afced RR |
624 | return TRUE; |
625 | } | |
626 | ||
53010e52 RR |
627 | bool wxApp::SendIdleEvents( wxWindow* win ) |
628 | { | |
629 | bool needMore = FALSE; | |
630 | ||
8bbe427f VZ |
631 | wxIdleEvent event; |
632 | event.SetEventObject(win); | |
60acb947 | 633 | |
f6bcfd97 | 634 | win->GetEventHandler()->ProcessEvent(event); |
2b5f62a0 | 635 | |
53010e52 RR |
636 | if (event.MoreRequested()) |
637 | needMore = TRUE; | |
638 | ||
b1d4dd7a | 639 | wxWindowList::Node *node = win->GetChildren().GetFirst(); |
8bbe427f VZ |
640 | while (node) |
641 | { | |
b1d4dd7a RL |
642 | wxWindow *win = node->GetData(); |
643 | ||
8bbe427f | 644 | if (SendIdleEvents(win)) |
53010e52 | 645 | needMore = TRUE; |
b1d4dd7a | 646 | node = node->GetNext(); |
8bbe427f | 647 | } |
be88a6ad | 648 | |
0d1dff01 | 649 | return needMore; |
ff7b1510 | 650 | } |
c801d85f | 651 | |
60acb947 | 652 | int wxApp::MainLoop() |
c801d85f | 653 | { |
ec758a20 RR |
654 | gtk_main(); |
655 | return 0; | |
ff7b1510 | 656 | } |
c801d85f | 657 | |
60acb947 | 658 | void wxApp::ExitMainLoop() |
c801d85f | 659 | { |
7ec2881a RR |
660 | if (gtk_main_level() > 0) |
661 | gtk_main_quit(); | |
ff7b1510 | 662 | } |
c801d85f | 663 | |
60acb947 | 664 | bool wxApp::Initialized() |
c801d85f | 665 | { |
ec758a20 | 666 | return m_initialized; |
ff7b1510 | 667 | } |
c801d85f | 668 | |
60acb947 | 669 | bool wxApp::Pending() |
c801d85f | 670 | { |
acfd422a | 671 | return (gtk_events_pending() > 0); |
ff7b1510 | 672 | } |
c801d85f | 673 | |
60acb947 | 674 | void wxApp::Dispatch() |
c801d85f | 675 | { |
8801832d | 676 | gtk_main_iteration(); |
ff7b1510 | 677 | } |
c801d85f | 678 | |
60acb947 | 679 | void wxApp::DeletePendingObjects() |
c801d85f | 680 | { |
b1d4dd7a | 681 | wxNode *node = wxPendingDelete.GetFirst(); |
ec758a20 RR |
682 | while (node) |
683 | { | |
b1d4dd7a | 684 | wxObject *obj = (wxObject *)node->GetData(); |
0cf2cb36 | 685 | |
ec758a20 | 686 | delete obj; |
c801d85f | 687 | |
f03fc89f VZ |
688 | if (wxPendingDelete.Find(obj)) |
689 | delete node; | |
c801d85f | 690 | |
b1d4dd7a | 691 | node = wxPendingDelete.GetFirst(); |
ec758a20 | 692 | } |
ff7b1510 | 693 | } |
c801d85f | 694 | |
60acb947 | 695 | bool wxApp::Initialize() |
c801d85f | 696 | { |
0d2a2b60 | 697 | wxClassInfo::InitializeClasses(); |
60acb947 | 698 | |
4d3a259a GL |
699 | // GL: I'm annoyed ... I don't know where to put this and I don't want to |
700 | // create a module for that as it's part of the core. | |
701 | #if wxUSE_THREADS | |
702 | wxPendingEvents = new wxList(); | |
703 | wxPendingEventsLocker = new wxCriticalSection(); | |
704 | #endif | |
705 | ||
0d2a2b60 RR |
706 | wxTheColourDatabase = new wxColourDatabase( wxKEY_STRING ); |
707 | wxTheColourDatabase->Initialize(); | |
a3622daa | 708 | |
0d2a2b60 RR |
709 | wxInitializeStockLists(); |
710 | wxInitializeStockObjects(); | |
c801d85f | 711 | |
4a0c68a7 RR |
712 | #if wxUSE_WX_RESOURCES |
713 | wxInitializeResourceSystem(); | |
714 | #endif | |
715 | ||
0d2a2b60 | 716 | wxModule::RegisterModules(); |
2b5f62a0 VZ |
717 | if (!wxModule::InitializeModules()) |
718 | return FALSE; | |
719 | ||
720 | #if wxUSE_INTL | |
721 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
722 | #endif | |
60acb947 | 723 | |
0d2a2b60 | 724 | return TRUE; |
ff7b1510 | 725 | } |
c801d85f | 726 | |
60acb947 | 727 | void wxApp::CleanUp() |
c801d85f | 728 | { |
0d2a2b60 | 729 | wxModule::CleanUpModules(); |
0cf2cb36 | 730 | |
4a0c68a7 RR |
731 | #if wxUSE_WX_RESOURCES |
732 | wxCleanUpResourceSystem(); | |
733 | #endif | |
734 | ||
a11672a4 | 735 | delete wxTheColourDatabase; |
0d2a2b60 | 736 | wxTheColourDatabase = (wxColourDatabase*) NULL; |
60acb947 | 737 | |
0d2a2b60 RR |
738 | wxDeleteStockObjects(); |
739 | ||
ec758a20 | 740 | wxDeleteStockLists(); |
a3622daa | 741 | |
0d2a2b60 RR |
742 | delete wxTheApp; |
743 | wxTheApp = (wxApp*) NULL; | |
744 | ||
a11672a4 RR |
745 | wxClassInfo::CleanUpClasses(); |
746 | ||
4d3a259a GL |
747 | #if wxUSE_THREADS |
748 | delete wxPendingEvents; | |
749 | delete wxPendingEventsLocker; | |
750 | #endif | |
751 | ||
60acb947 | 752 | // check for memory leaks |
0d2a2b60 | 753 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
6981d3ec | 754 | if (wxDebugContext::CountObjectsLeft(TRUE) > 0) |
0d2a2b60 | 755 | { |
223d09f6 | 756 | wxLogDebug(wxT("There were memory leaks.\n")); |
0d2a2b60 RR |
757 | wxDebugContext::Dump(); |
758 | wxDebugContext::PrintStatistics(); | |
759 | } | |
8801832d | 760 | #endif // Debug |
0d2a2b60 | 761 | |
88ac883a | 762 | #if wxUSE_LOG |
60acb947 | 763 | // do this as the very last thing because everything else can log messages |
0d2a2b60 | 764 | wxLog::DontCreateOnDemand(); |
60acb947 | 765 | |
0d2a2b60 | 766 | wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL ); |
60acb947 VZ |
767 | if (oldLog) |
768 | delete oldLog; | |
88ac883a | 769 | #endif // wxUSE_LOG |
c19c1ca9 | 770 | } |
c801d85f KB |
771 | |
772 | //----------------------------------------------------------------------------- | |
773 | // wxEntry | |
774 | //----------------------------------------------------------------------------- | |
775 | ||
17154fc8 VZ |
776 | // NB: argc and argv may be changed here, pass by reference! |
777 | int wxEntryStart( int& argc, char *argv[] ) | |
c801d85f | 778 | { |
924ef850 | 779 | #if wxUSE_THREADS |
005f5d18 RR |
780 | // GTK 1.2 up to version 1.2.3 has broken threads |
781 | if ((gtk_major_version == 1) && | |
8f75cb6c | 782 | (gtk_minor_version == 2) && |
2286341c | 783 | (gtk_micro_version < 4)) |
96997d65 | 784 | { |
fb65642c | 785 | printf( "wxWindows warning: GUI threading disabled due to outdated GTK version\n" ); |
8f75cb6c RR |
786 | } |
787 | else | |
788 | { | |
789 | g_thread_init(NULL); | |
790 | } | |
924ef850 | 791 | #endif |
2286341c | 792 | |
0d2a2b60 | 793 | gtk_set_locale(); |
c801d85f | 794 | |
f9862abd JS |
795 | // We should have the wxUSE_WCHAR_T test on the _outside_ |
796 | #if wxUSE_WCHAR_T | |
2d4dc3a4 OK |
797 | #if defined(__WXGTK20__) |
798 | // gtk+ 2.0 supports Unicode through UTF-8 strings | |
799 | wxConvCurrent = &wxConvUTF8; | |
f9862abd | 800 | #else |
dcf924a3 | 801 | if (!wxOKlibc()) wxConvCurrent = &wxConvLocal; |
f9862abd | 802 | #endif |
fd9811b1 RR |
803 | #else |
804 | if (!wxOKlibc()) wxConvCurrent = (wxMBConv*) NULL; | |
805 | #endif | |
002f4218 | 806 | |
924ef850 RR |
807 | gdk_threads_enter(); |
808 | ||
0d2a2b60 | 809 | gtk_init( &argc, &argv ); |
0cf2cb36 | 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 |