]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/app.cpp
Added implementations for two cases of wxSystemSettingsNative::GetMetric to
[wxWidgets.git] / src / gtk1 / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "app.h"
12 #endif
13
14 #ifdef __VMS
15 #include <vms_jackets.h>
16 #undef ConnectionNumber
17 #endif
18
19 #include "wx/app.h"
20 #include "wx/gdicmn.h"
21 #include "wx/utils.h"
22 #include "wx/intl.h"
23 #include "wx/log.h"
24 #include "wx/memory.h"
25 #include "wx/font.h"
26 #include "wx/settings.h"
27 #include "wx/dialog.h"
28 #include "wx/msgdlg.h"
29 #include "wx/file.h"
30 #include "wx/filename.h"
31 #include "wx/module.h"
32 #include "wx/image.h"
33
34 #ifdef __WXUNIVERSAL__
35 #include "wx/univ/theme.h"
36 #include "wx/univ/renderer.h"
37 #endif
38
39 #if wxUSE_THREADS
40 #include "wx/thread.h"
41 #endif
42
43 #include <unistd.h>
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
70 #include "wx/gtk/win_gtk.h"
71
72 #include <gtk/gtk.h>
73
74
75 //-----------------------------------------------------------------------------
76 // global data
77 //-----------------------------------------------------------------------------
78
79 bool g_mainThreadLocked = FALSE;
80 gint g_pendingTag = 0;
81
82 static GtkWidget *gs_RootWindow = (GtkWidget*) NULL;
83
84 //-----------------------------------------------------------------------------
85 // idle system
86 //-----------------------------------------------------------------------------
87
88 extern bool g_isIdle;
89
90 void wxapp_install_idle_handler();
91
92 //-----------------------------------------------------------------------------
93 // wxYield
94 //-----------------------------------------------------------------------------
95
96 // not static because used by textctrl.cpp
97 //
98 // MT-FIXME
99 bool wxIsInsideYield = FALSE;
100
101 bool wxApp::Yield(bool onlyIfNeeded)
102 {
103 if ( wxIsInsideYield )
104 {
105 if ( !onlyIfNeeded )
106 {
107 wxFAIL_MSG( wxT("wxYield called recursively" ) );
108 }
109
110 return FALSE;
111 }
112
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
121 wxIsInsideYield = TRUE;
122
123 if (!g_isIdle)
124 {
125 // We need to remove idle callbacks or the loop will
126 // never finish.
127 gtk_idle_remove( m_idleTag );
128 m_idleTag = 0;
129 g_isIdle = TRUE;
130 }
131
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
136 while (gtk_events_pending())
137 gtk_main_iteration();
138
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
141 // OnUpdateUI() which is a nice (and desired) side effect). But we
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();
146
147 // let the logs be flashed again
148 wxLog::Resume();
149
150 wxIsInsideYield = FALSE;
151
152 return TRUE;
153 }
154
155 //-----------------------------------------------------------------------------
156 // wxWakeUpIdle
157 //-----------------------------------------------------------------------------
158
159 void wxApp::WakeUpIdle()
160 {
161 #if wxUSE_THREADS
162 if (!wxThread::IsMain())
163 wxMutexGuiEnter();
164 #endif
165
166 if (g_isIdle)
167 wxapp_install_idle_handler();
168
169 #if wxUSE_THREADS
170 if (!wxThread::IsMain())
171 wxMutexGuiLeave();
172 #endif
173 }
174
175 //-----------------------------------------------------------------------------
176 // local functions
177 //-----------------------------------------------------------------------------
178
179 // the callback functions must be extern "C" to comply with GTK+ declarations
180 extern "C"
181 {
182
183 static gint wxapp_pending_callback( gpointer WXUNUSED(data) )
184 {
185 if (!wxTheApp) return TRUE;
186
187 // When getting called from GDK's time-out handler
188 // we are no longer within GDK's grab on the GUI
189 // thread so we must lock it here ourselves.
190 gdk_threads_enter();
191
192 // Sent idle event to all who request them.
193 wxTheApp->ProcessPendingEvents();
194
195 g_pendingTag = 0;
196
197 // Flush the logged messages if any.
198 #if wxUSE_LOG
199 wxLog::FlushActive();
200 #endif // wxUSE_LOG
201
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
210 static gint wxapp_idle_callback( gpointer WXUNUSED(data) )
211 {
212 if (!wxTheApp)
213 return TRUE;
214
215 #ifdef __WXDEBUG__
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
219 if ( wxTheApp->IsInAssert() )
220 {
221 // But repaint the assertion message if necessary
222 if (wxTopLevelWindows.GetCount() > 0)
223 {
224 wxWindow* win = (wxWindow*) wxTopLevelWindows.GetLast()->GetData();
225 #ifdef __WXGTK20__
226 if (win->IsKindOf(CLASSINFO(wxMessageDialog)))
227 #else
228 if (win->IsKindOf(CLASSINFO(wxGenericMessageDialog)))
229 #endif
230 win->OnInternalIdle();
231 }
232 return TRUE;
233 }
234 #endif // __WXDEBUG__
235
236 // When getting called from GDK's time-out handler
237 // we are no longer within GDK's grab on the GUI
238 // thread so we must lock it here ourselves.
239 gdk_threads_enter();
240
241 // Indicate that we are now in idle mode and event handlers
242 // will have to reinstall the idle handler again.
243 g_isIdle = TRUE;
244 wxTheApp->m_idleTag = 0;
245
246 // Send idle event to all who request them as long as
247 // no events have popped up in the event queue.
248 while (wxTheApp->ProcessIdle() && (gtk_events_pending() == 0))
249 ;
250
251 // Release lock again
252 gdk_threads_leave();
253
254 // Return FALSE to indicate that no more idle events are
255 // to be sent (single shot instead of continuous stream).
256 return FALSE;
257 }
258
259 #if wxUSE_THREADS
260
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
327 static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout )
328 {
329 gdk_threads_enter();
330
331 wxMutexGuiLeave();
332 g_mainThreadLocked = TRUE;
333
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 );
337
338 wxMutexGuiEnter();
339 g_mainThreadLocked = FALSE;
340
341 gdk_threads_leave();
342
343 return res;
344 }
345
346 #endif // wxUSE_THREADS
347
348 } // extern "C"
349
350 void wxapp_install_idle_handler()
351 {
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)") );
356
357 wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") );
358
359 g_isIdle = FALSE;
360
361 if (g_pendingTag == 0)
362 g_pendingTag = gtk_idle_add_priority( 900, wxapp_pending_callback, (gpointer) NULL );
363
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 );
370 }
371
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
386 //-----------------------------------------------------------------------------
387 // wxApp
388 //-----------------------------------------------------------------------------
389
390 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
391
392 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
393 EVT_IDLE(wxAppBase::OnIdle)
394 END_EVENT_TABLE()
395
396 wxApp::wxApp()
397 {
398 m_initialized = FALSE;
399 #ifdef __WXDEBUG__
400 m_isInAssert = FALSE;
401 #endif // __WXDEBUG__
402
403 m_idleTag = 0;
404 wxapp_install_idle_handler();
405
406 #if wxUSE_THREADS
407 g_main_set_poll_func( wxapp_poll_func );
408 #endif
409
410 m_colorCube = (unsigned char*) NULL;
411
412 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
413 m_glVisualInfo = (void *) NULL;
414 }
415
416 wxApp::~wxApp()
417 {
418 if (m_idleTag) gtk_idle_remove( m_idleTag );
419
420 if (m_colorCube) free(m_colorCube);
421 }
422
423 bool wxApp::OnInitGui()
424 {
425 if ( !wxAppBase::OnInitGui() )
426 return FALSE;
427
428 GdkVisual *visual = gdk_visual_get_system();
429
430 // if this is a wxGLApp (derived from wxApp), and we've already
431 // chosen a specific visual, then derive the GdkVisual from that
432 if (m_glVisualInfo != NULL)
433 {
434 #ifdef __WXGTK20__
435 // seems gtk_widget_set_default_visual no longer exists?
436 GdkVisual* vis = gtk_widget_get_default_visual();
437 #else
438 GdkVisual* vis = gdkx_visual_get(
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 }
448
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.
451
452 else
453 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual))
454 {
455 #ifdef __WXGTK20__
456 /* seems gtk_widget_set_default_visual no longer exists? */
457 GdkVisual* vis = gtk_widget_get_default_visual();
458 #else
459 GdkVisual* vis = gdk_visual_get_best();
460 gtk_widget_set_default_visual( vis );
461 #endif
462
463 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
464 gtk_widget_set_default_colormap( colormap );
465
466 visual = vis;
467 }
468
469 // Nothing to do for 15, 16, 24, 32 bit displays
470 if (visual->depth > 8) return TRUE;
471
472 // initialize color cube for 8-bit color reduction dithering
473
474 GdkColormap *cmap = gtk_widget_get_default_colormap();
475
476 m_colorCube = (unsigned char*)malloc(32 * 32 * 32);
477
478 for (int r = 0; r < 32; r++)
479 {
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);
487
488 int index = -1;
489
490 GdkColor *colors = cmap->colors;
491 if (colors)
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)
502 {
503 index = i; max = sum;
504 }
505 }
506 }
507 else
508 {
509 // assume 8-bit true or static colors. this really exists
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;
514 }
515 m_colorCube[ (r*1024) + (g*32) + b ] = index;
516 }
517 }
518 }
519
520 return TRUE;
521 }
522
523 GdkVisual *wxApp::GetGdkVisual()
524 {
525 GdkVisual *visual = NULL;
526
527 if (m_glVisualInfo)
528 visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid );
529 else
530 visual = gdk_window_get_visual( wxGetRootWindow()->window );
531
532 wxASSERT( visual );
533
534 return visual;
535 }
536
537 int wxApp::MainLoop()
538 {
539 gtk_main();
540 return 0;
541 }
542
543 void wxApp::Exit()
544 {
545 // VZ: no idea why is it different from ExitMainLoop() but this is what
546 // wxExit() used to do
547 gtk_main_quit();
548 }
549
550 void wxApp::ExitMainLoop()
551 {
552 if (gtk_main_level() > 0)
553 gtk_main_quit();
554 }
555
556 bool wxApp::Initialized()
557 {
558 return m_initialized;
559 }
560
561 bool wxApp::Pending()
562 {
563 return (gtk_events_pending() > 0);
564 }
565
566 void wxApp::Dispatch()
567 {
568 gtk_main_iteration();
569 }
570
571 bool wxApp::Initialize(int& argc, wxChar **argv)
572 {
573 #if wxUSE_THREADS
574 // GTK 1.2 up to version 1.2.3 has broken threads
575 if ((gtk_major_version == 1) &&
576 (gtk_minor_version == 2) &&
577 (gtk_micro_version < 4))
578 {
579 printf( "wxWindows warning: GUI threading disabled due to outdated GTK version\n" );
580 }
581 else
582 {
583 g_thread_init(NULL);
584 }
585 #endif // wxUSE_THREADS
586
587 gtk_set_locale();
588
589 // We should have the wxUSE_WCHAR_T test on the _outside_
590 #if wxUSE_WCHAR_T
591 #if defined(__WXGTK20__)
592 // gtk+ 2.0 supports Unicode through UTF-8 strings
593 wxConvCurrent = &wxConvUTF8;
594 #else // GTK 1.x
595 if (!wxOKlibc())
596 wxConvCurrent = &wxConvLocal;
597 #endif
598 #else // !wxUSE_WCHAR_T
599 if (!wxOKlibc())
600 wxConvCurrent = (wxMBConv*) NULL;
601 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
602
603 #if wxUSE_UNICODE
604 // gtk_init() wants UTF-8, not wchar_t, so convert
605 int i;
606 char **argvGTK = new char *[argc + 1];
607 for ( i = 0; i < argc; i++ )
608 {
609 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv[i]));
610 }
611
612 argvGTK[argc] = NULL;
613
614 int argcGTK = argc;
615 gtk_init( &argcGTK, &argvGTK );
616
617 if ( argcGTK != argc )
618 {
619 // we have to drop the parameters which were consumed by GTK+
620 for ( i = 0; i < argcGTK; i++ )
621 {
622 while ( strcmp(wxConvUTF8.cWX2MB(argv[i]), argvGTK[i]) != 0 )
623 {
624 memmove(argv + i, argv + i + 1, argc - i);
625 }
626 }
627
628 argc = argcGTK;
629 }
630 //else: gtk_init() didn't modify our parameters
631
632 // free our copy
633 for ( i = 0; i < argcGTK; i++ )
634 {
635 free(argvGTK[i]);
636 }
637
638 delete [] argvGTK;
639 #else // !wxUSE_UNICODE
640 // gtk_init() shouldn't actually change argv itself (just its contents) so
641 // it's ok to pass pointer to it
642 gtk_init( &argc, &argv );
643 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
644
645 // we can not enter threads before gtk_init is done
646 gdk_threads_enter();
647
648 if ( !wxAppBase::Initialize(argc, argv) )
649 {
650 gdk_threads_leave();
651
652 return false;
653 }
654
655 wxSetDetectableAutoRepeat( TRUE );
656
657 #if wxUSE_INTL
658 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
659 #endif
660
661 wxGetRootWindow();
662
663 return true;
664 }
665
666 void wxApp::CleanUp()
667 {
668 gdk_threads_leave();
669
670 wxAppBase::CleanUp();
671 }
672
673 #ifdef __WXDEBUG__
674
675 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
676 {
677 m_isInAssert = TRUE;
678
679 wxAppBase::OnAssert(file, line, cond, msg);
680
681 m_isInAssert = FALSE;
682 }
683
684 #endif // __WXDEBUG__
685