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