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