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