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