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