]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/app.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 // vms_jackets.h should for proper working be included before anything else
12 # include <vms_jackets.h>
13 #undef ConnectionNumber
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/memory.h"
30 #include "wx/filename.h"
31 #include "wx/thread.h"
37 #ifdef __WXUNIVERSAL__
38 #include "wx/univ/theme.h"
39 #include "wx/univ/renderer.h"
48 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
49 // in neither poll.h nor sys/poll.h which results in link errors later
62 // we implement poll() ourselves using select() which is supposed exist in
64 #include <sys/types.h>
67 #ifdef HAVE_SYS_SELECT_H
68 #include <sys/select.h>
70 #endif // HAVE_POLL/!HAVE_POLL
72 #include "wx/unix/private.h"
73 #include "wx/gtk/win_gtk.h"
74 #include "wx/gtk/private.h"
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
83 #include "wx/html/forcelnk.h"
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
91 bool g_mainThreadLocked
= false ;
93 static GtkWidget
* gs_RootWindow
= ( GtkWidget
*) NULL
;
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
99 void wxapp_install_idle_handler ();
102 static wxMutex gs_idleTagsMutex
;
105 //-----------------------------------------------------------------------------
107 //-----------------------------------------------------------------------------
109 // not static because used by textctrl.cpp
112 bool wxIsInsideYield
= false ;
114 bool wxApp :: Yield ( bool onlyIfNeeded
)
116 if ( wxIsInsideYield
)
120 wxFAIL_MSG ( wxT ( "wxYield called recursively" ) );
127 if ( ! wxThread :: IsMain () )
129 // can't call gtk_main_iteration() from other threads like this
132 #endif // wxUSE_THREADS
134 wxIsInsideYield
= true ;
136 // We need to remove idle callbacks or the loop will
138 wxTheApp
-> RemoveIdleTag ();
141 // disable log flushing from here because a call to wxYield() shouldn't
142 // normally result in message boxes popping up &c
146 while ( gtk_events_pending ())
147 gtk_main_iteration ();
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().
158 // let the logs be flashed again
162 wxIsInsideYield
= false ;
167 //-----------------------------------------------------------------------------
169 //-----------------------------------------------------------------------------
171 // RR/KH: No wxMutexGui calls are needed here according to the GTK faq,
172 // http://www.gtk.org/faq/#AEN500 - this caused problems for wxPostEvent.
174 void wxApp :: WakeUpIdle ()
176 wxapp_install_idle_handler ();
179 //-----------------------------------------------------------------------------
181 //-----------------------------------------------------------------------------
183 // the callback functions must be extern "C" to comply with GTK+ declarations
187 // One-shot emission hook for "event" signal, to install idle handler.
188 // This will be called when the "event" signal is issued on any GtkWidget object.
190 event_emission_hook ( GSignalInvocationHint
*, guint
, const GValue
*, gpointer
)
192 wxapp_install_idle_handler ();
197 static gint
wxapp_idle_callback ( gpointer
WXUNUSED ( data
) )
203 // don't generate the idle events while the assert modal dialog is shown,
204 // this completely confuses the apps which don't expect to be reentered
205 // from some safely-looking functions
206 if ( wxTheApp
-> IsInAssert () )
208 #endif // __WXDEBUG__
210 // When getting called from GDK's time-out handler
211 // we are no longer within GDK's grab on the GUI
212 // thread so we must lock it here ourselves.
215 // Indicate that we are now in idle mode and event handlers
216 // will have to reinstall the idle handler again.
219 wxMutexLocker
lock ( gs_idleTagsMutex
);
222 wxTheApp
-> m_idleTag
= 0 ;
227 // Send idle event to all who request them as long as
228 // no events have popped up in the event queue.
229 while ( ( moreIdles
= wxTheApp
-> ProcessIdle ()) && gtk_events_pending () == 0 )
232 // Release lock again
237 // add emission hook for "event" signal, to re-install idle handler when needed
238 guint sig_id
= g_signal_lookup ( "event" , GTK_TYPE_WIDGET
);
239 g_signal_add_emission_hook ( sig_id
, 0 , event_emission_hook
, NULL
, NULL
);
242 // Return FALSE if no more idle events are to be sent
250 #define wxPollFd pollfd
253 typedef GPollFD wxPollFd
;
255 int wxPoll ( wxPollFd
* ufds
, unsigned int nfds
, int timeout
)
257 // convert timeout from ms to struct timeval (s/us)
259 tv_timeout
. tv_sec
= timeout
/ 1000 ;
260 tv_timeout
. tv_usec
= ( timeout%1000
)* 1000 ;
262 // remember the highest fd used here
265 // and fill the sets for select()
270 wxFD_ZERO (& writefds
);
271 wxFD_ZERO (& exceptfds
);
274 for ( i
= 0 ; i
< nfds
; i
++ )
276 wxASSERT_MSG ( ufds
[ i
]. fd
< wxFD_SETSIZE
, _T ( "fd out of range" ) );
278 if ( ufds
[ i
]. events
& G_IO_IN
)
279 wxFD_SET ( ufds
[ i
]. fd
, & readfds
);
281 if ( ufds
[ i
]. events
& G_IO_PRI
)
282 wxFD_SET ( ufds
[ i
]. fd
, & exceptfds
);
284 if ( ufds
[ i
]. events
& G_IO_OUT
)
285 wxFD_SET ( ufds
[ i
]. fd
, & writefds
);
287 if ( ufds
[ i
]. fd
> fdMax
)
292 int res
= select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);
294 // translate the results back
295 for ( i
= 0 ; i
< nfds
; i
++ )
299 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & readfds
) )
300 ufds
[ i
]. revents
|= G_IO_IN
;
302 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & exceptfds
) )
303 ufds
[ i
]. revents
|= G_IO_PRI
;
305 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & writefds
) )
306 ufds
[ i
]. revents
|= G_IO_OUT
;
312 #endif // HAVE_POLL/!HAVE_POLL
314 static gint
wxapp_poll_func ( GPollFD
* ufds
, guint nfds
, gint timeout
)
319 g_mainThreadLocked
= true ;
321 // we rely on the fact that glib GPollFD struct is really just pollfd but
322 // I wonder how wise is this in the long term (VZ)
323 gint res
= wxPoll ( ( wxPollFd
*) ufds
, nfds
, timeout
);
326 g_mainThreadLocked
= false ;
333 #endif // wxUSE_THREADS
337 void wxapp_install_idle_handler ()
340 wxMutexLocker
lock ( gs_idleTagsMutex
);
343 // Don't install the handler if it's already installed. This test *MUST*
344 // be done when gs_idleTagsMutex is locked!
348 // GD: this assert is raised when using the thread sample (which works)
349 // so the test is probably not so easy. Can widget callbacks be
350 // triggered from child threads and, if so, for which widgets?
351 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
353 wxASSERT_MSG ( wxTheApp
-> m_idleTag
== 0 , wxT ( "attempt to install idle handler twice" ) );
357 // This routine gets called by all event handlers
358 // indicating that the idle is over. It may also
359 // get called from other thread for sending events
360 // to the main thread (and processing these in
361 // idle time). Very low priority.
362 wxTheApp
-> m_idleTag
= g_idle_add_full ( G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
365 //-----------------------------------------------------------------------------
366 // Access to the root window global
367 //-----------------------------------------------------------------------------
369 GtkWidget
* wxGetRootWindow ()
371 if ( gs_RootWindow
== NULL
)
373 gs_RootWindow
= gtk_window_new ( GTK_WINDOW_TOPLEVEL
);
374 gtk_widget_realize ( gs_RootWindow
);
376 return gs_RootWindow
;
379 //-----------------------------------------------------------------------------
381 //-----------------------------------------------------------------------------
383 IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)
385 BEGIN_EVENT_TABLE ( wxApp
, wxEvtHandler
)
386 EVT_IDLE ( wxAppBase :: OnIdle
)
392 m_isInAssert
= false ;
393 #endif // __WXDEBUG__
397 wxapp_install_idle_handler ();
400 g_main_context_set_poll_func ( NULL
, wxapp_poll_func
);
403 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
404 m_glVisualInfo
= ( void *) NULL
;
405 m_glFBCInfo
= ( void *) NULL
;
411 g_source_remove ( m_idleTag
);
414 bool wxApp :: OnInitGui ()
416 if ( ! wxAppBase :: OnInitGui () )
419 // if this is a wxGLApp (derived from wxApp), and we've already
420 // chosen a specific visual, then derive the GdkVisual from that
421 if ( m_glVisualInfo
!= NULL
)
423 // seems gtk_widget_set_default_visual no longer exists?
424 GdkVisual
* vis
= gtk_widget_get_default_visual ();
426 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
427 gtk_widget_set_default_colormap ( colormap
);
430 // On some machines, the default visual is just 256 colours, so
431 // we make sure we get the best. This can sometimes be wasteful.
434 if (( gdk_visual_get_best () != gdk_visual_get_system ()) && ( m_useBestVisual
))
436 /* seems gtk_widget_set_default_visual no longer exists? */
437 GdkVisual
* vis
= gtk_widget_get_default_visual ();
439 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
440 gtk_widget_set_default_colormap ( colormap
);
446 GdkVisual
* wxApp :: GetGdkVisual ()
448 GdkVisual
* visual
= NULL
;
451 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
453 visual
= gdk_drawable_get_visual ( wxGetRootWindow ()-> window
);
460 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
465 if (! g_thread_supported ())
467 #endif // wxUSE_THREADS
471 // We should have the wxUSE_WCHAR_T test on the _outside_
473 // gtk+ 2.0 supports Unicode through UTF-8 strings
474 wxConvCurrent
= & wxConvUTF8
;
475 #else // !wxUSE_WCHAR_T
477 wxConvCurrent
= ( wxMBConv
*) NULL
;
478 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
480 // decide which conversion to use for the file names
482 // (1) this variable exists for the sole purpose of specifying the encoding
483 // of the filenames for GTK+ programs, so use it if it is set
484 wxString
encName ( wxGetenv ( _T ( "G_FILENAME_ENCODING" )));
485 encName
= encName
. BeforeFirst ( _T ( ',' ));
486 if ( encName
== _T ( "@locale" ))
492 // (2) if a non default locale is set, assume that the user wants his
493 // filenames in this locale too
494 encName
= wxLocale :: GetSystemEncodingName (). Upper ();
495 // (3) finally use UTF-8 by default
496 if ( encName
. empty () || encName
== _T ( "US-ASCII" ))
497 encName
= _T ( "UTF-8" );
498 wxSetEnv ( _T ( "G_FILENAME_ENCODING" ), encName
);
502 encName
= _T ( "UTF-8" );
504 static wxConvBrokenFileNames
fileconv ( encName
);
505 wxConvFileName
= & fileconv
;
508 // gtk_init() wants UTF-8, not wchar_t, so convert
510 char ** argvGTK
= new char *[ argc
+ 1 ];
511 for ( i
= 0 ; i
< argc
; i
++ )
513 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
516 argvGTK
[ argc
] = NULL
;
521 init_result
= true ; // is there a _check() version of this?
522 gpe_application_init ( & argcGTK
, & argvGTK
);
524 init_result
= gtk_init_check ( & argcGTK
, & argvGTK
);
527 if ( argcGTK
!= argc
)
529 // we have to drop the parameters which were consumed by GTK+
530 for ( i
= 0 ; i
< argcGTK
; i
++ )
532 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
534 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
540 //else: gtk_init() didn't modify our parameters
543 for ( i
= 0 ; i
< argcGTK
; i
++ )
549 #else // !wxUSE_UNICODE
550 // gtk_init() shouldn't actually change argv itself (just its contents) so
551 // it's ok to pass pointer to it
552 init_result
= gtk_init_check ( & argc
, & argv
);
553 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
556 wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));
560 // we can not enter threads before gtk_init is done
563 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
570 wxSetDetectableAutoRepeat ( true );
573 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
579 void wxApp :: CleanUp ()
583 wxAppBase :: CleanUp ();
588 void wxApp :: OnAssertFailure ( const wxChar
* file
,
596 wxAppBase :: OnAssertFailure ( file
, line
, func
, cond
, msg
);
598 m_isInAssert
= false ;
601 #endif // __WXDEBUG__
603 void wxApp :: RemoveIdleTag ()
606 wxMutexLocker
lock ( gs_idleTagsMutex
);
610 g_source_remove ( wxTheApp
-> m_idleTag
);
611 wxTheApp
-> m_idleTag
= 0 ;