]>
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
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 // add emission hook for "event" signal, to re-install idle handler when needed
198 static inline void wxAddEmissionHook ()
200 GType widgetType
= GTK_TYPE_WIDGET
;
201 // if GtkWidget type is loaded
202 if ( g_type_class_peek ( widgetType
) != NULL
)
204 guint sig_id
= g_signal_lookup ( "event" , widgetType
);
205 g_signal_add_emission_hook ( sig_id
, 0 , event_emission_hook
, NULL
, NULL
);
209 static gint
wxapp_idle_callback ( gpointer
WXUNUSED ( data
) )
215 // don't generate the idle events while the assert modal dialog is shown,
216 // this completely confuses the apps which don't expect to be reentered
217 // from some safely-looking functions
218 if ( wxTheApp
-> IsInAssert () )
220 #endif // __WXDEBUG__
222 // When getting called from GDK's time-out handler
223 // we are no longer within GDK's grab on the GUI
224 // thread so we must lock it here ourselves.
229 // Send idle event to all who request them as long as
230 // no events have popped up in the event queue.
231 while ( ( moreIdles
= wxTheApp
-> ProcessIdle ()) && gtk_events_pending () == 0 )
234 // Release lock again
240 wxMutexLocker
lock ( gs_idleTagsMutex
);
242 // Indicate that we are now in idle mode and event handlers
243 // will have to reinstall the idle handler again.
245 wxTheApp
-> m_idleTag
= 0 ;
250 // Return FALSE if no more idle events are to be sent
258 #define wxPollFd pollfd
261 typedef GPollFD wxPollFd
;
263 int wxPoll ( wxPollFd
* ufds
, unsigned int nfds
, int timeout
)
265 // convert timeout from ms to struct timeval (s/us)
267 tv_timeout
. tv_sec
= timeout
/ 1000 ;
268 tv_timeout
. tv_usec
= ( timeout%1000
)* 1000 ;
270 // remember the highest fd used here
273 // and fill the sets for select()
278 wxFD_ZERO (& writefds
);
279 wxFD_ZERO (& exceptfds
);
282 for ( i
= 0 ; i
< nfds
; i
++ )
284 wxASSERT_MSG ( ufds
[ i
]. fd
< wxFD_SETSIZE
, _T ( "fd out of range" ) );
286 if ( ufds
[ i
]. events
& G_IO_IN
)
287 wxFD_SET ( ufds
[ i
]. fd
, & readfds
);
289 if ( ufds
[ i
]. events
& G_IO_PRI
)
290 wxFD_SET ( ufds
[ i
]. fd
, & exceptfds
);
292 if ( ufds
[ i
]. events
& G_IO_OUT
)
293 wxFD_SET ( ufds
[ i
]. fd
, & writefds
);
295 if ( ufds
[ i
]. fd
> fdMax
)
300 int res
= select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);
302 // translate the results back
303 for ( i
= 0 ; i
< nfds
; i
++ )
307 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & readfds
) )
308 ufds
[ i
]. revents
|= G_IO_IN
;
310 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & exceptfds
) )
311 ufds
[ i
]. revents
|= G_IO_PRI
;
313 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & writefds
) )
314 ufds
[ i
]. revents
|= G_IO_OUT
;
320 #endif // HAVE_POLL/!HAVE_POLL
322 static gint
wxapp_poll_func ( GPollFD
* ufds
, guint nfds
, gint timeout
)
327 g_mainThreadLocked
= true ;
329 // we rely on the fact that glib GPollFD struct is really just pollfd but
330 // I wonder how wise is this in the long term (VZ)
331 gint res
= wxPoll ( ( wxPollFd
*) ufds
, nfds
, timeout
);
334 g_mainThreadLocked
= false ;
341 #endif // wxUSE_THREADS
345 void wxapp_install_idle_handler ()
347 if ( wxTheApp
== NULL
)
351 wxMutexLocker
lock ( gs_idleTagsMutex
);
354 // Don't install the handler if it's already installed. This test *MUST*
355 // be done when gs_idleTagsMutex is locked!
359 // GD: this assert is raised when using the thread sample (which works)
360 // so the test is probably not so easy. Can widget callbacks be
361 // triggered from child threads and, if so, for which widgets?
362 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
364 wxASSERT_MSG ( wxTheApp
-> m_idleTag
== 0 , wxT ( "attempt to install idle handler twice" ) );
368 // This routine gets called by all event handlers
369 // indicating that the idle is over. It may also
370 // get called from other thread for sending events
371 // to the main thread (and processing these in
372 // idle time). Very low priority.
373 wxTheApp
-> m_idleTag
= g_idle_add_full ( G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
376 //-----------------------------------------------------------------------------
377 // Access to the root window global
378 //-----------------------------------------------------------------------------
380 GtkWidget
* wxGetRootWindow ()
382 if ( gs_RootWindow
== NULL
)
384 gs_RootWindow
= gtk_window_new ( GTK_WINDOW_TOPLEVEL
);
385 gtk_widget_realize ( gs_RootWindow
);
387 return gs_RootWindow
;
390 //-----------------------------------------------------------------------------
392 //-----------------------------------------------------------------------------
394 IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)
396 BEGIN_EVENT_TABLE ( wxApp
, wxEvtHandler
)
397 EVT_IDLE ( wxAppBase :: OnIdle
)
403 m_isInAssert
= false ;
404 #endif // __WXDEBUG__
408 wxapp_install_idle_handler ();
411 g_main_context_set_poll_func ( NULL
, wxapp_poll_func
);
414 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
415 m_glVisualInfo
= ( void *) NULL
;
416 m_glFBCInfo
= ( void *) NULL
;
422 g_source_remove ( m_idleTag
);
425 bool wxApp :: OnInitGui ()
427 if ( ! wxAppBase :: OnInitGui () )
430 // if this is a wxGLApp (derived from wxApp), and we've already
431 // chosen a specific visual, then derive the GdkVisual from that
432 if ( m_glVisualInfo
!= NULL
)
434 // seems gtk_widget_set_default_visual no longer exists?
435 GdkVisual
* vis
= gtk_widget_get_default_visual ();
437 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
438 gtk_widget_set_default_colormap ( colormap
);
441 // On some machines, the default visual is just 256 colours, so
442 // we make sure we get the best. This can sometimes be wasteful.
445 if (( gdk_visual_get_best () != gdk_visual_get_system ()) && ( m_useBestVisual
))
447 /* seems gtk_widget_set_default_visual no longer exists? */
448 GdkVisual
* vis
= gtk_widget_get_default_visual ();
450 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
451 gtk_widget_set_default_colormap ( colormap
);
457 GdkVisual
* wxApp :: GetGdkVisual ()
459 GdkVisual
* visual
= NULL
;
462 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
464 visual
= gdk_drawable_get_visual ( wxGetRootWindow ()-> window
);
471 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
476 if (! g_thread_supported ())
478 #endif // wxUSE_THREADS
482 // We should have the wxUSE_WCHAR_T test on the _outside_
484 // gtk+ 2.0 supports Unicode through UTF-8 strings
485 wxConvCurrent
= & wxConvUTF8
;
486 #else // !wxUSE_WCHAR_T
488 wxConvCurrent
= ( wxMBConv
*) NULL
;
489 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
491 // decide which conversion to use for the file names
493 // (1) this variable exists for the sole purpose of specifying the encoding
494 // of the filenames for GTK+ programs, so use it if it is set
495 wxString
encName ( wxGetenv ( _T ( "G_FILENAME_ENCODING" )));
496 encName
= encName
. BeforeFirst ( _T ( ',' ));
497 if ( encName
== _T ( "@locale" ))
503 // (2) if a non default locale is set, assume that the user wants his
504 // filenames in this locale too
505 encName
= wxLocale :: GetSystemEncodingName (). Upper ();
506 // (3) finally use UTF-8 by default
507 if ( encName
. empty () || encName
== _T ( "US-ASCII" ))
508 encName
= _T ( "UTF-8" );
509 wxSetEnv ( _T ( "G_FILENAME_ENCODING" ), encName
);
513 encName
= _T ( "UTF-8" );
515 static wxConvBrokenFileNames
fileconv ( encName
);
516 wxConvFileName
= & fileconv
;
519 // gtk_init() wants UTF-8, not wchar_t, so convert
521 char ** argvGTK
= new char *[ argc
+ 1 ];
522 for ( i
= 0 ; i
< argc
; i
++ )
524 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
527 argvGTK
[ argc
] = NULL
;
532 init_result
= true ; // is there a _check() version of this?
533 gpe_application_init ( & argcGTK
, & argvGTK
);
535 init_result
= gtk_init_check ( & argcGTK
, & argvGTK
);
538 if ( argcGTK
!= argc
)
540 // we have to drop the parameters which were consumed by GTK+
541 for ( i
= 0 ; i
< argcGTK
; i
++ )
543 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
545 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
551 //else: gtk_init() didn't modify our parameters
554 for ( i
= 0 ; i
< argcGTK
; i
++ )
560 #else // !wxUSE_UNICODE
561 // gtk_init() shouldn't actually change argv itself (just its contents) so
562 // it's ok to pass pointer to it
563 init_result
= gtk_init_check ( & argc
, & argv
);
564 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
567 wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));
571 // we can not enter threads before gtk_init is done
574 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
581 wxSetDetectableAutoRepeat ( true );
584 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
590 void wxApp :: CleanUp ()
594 wxAppBase :: CleanUp ();
599 void wxApp :: OnAssertFailure ( const wxChar
* file
,
607 wxAppBase :: OnAssertFailure ( file
, line
, func
, cond
, msg
);
609 m_isInAssert
= false ;
612 #endif // __WXDEBUG__
614 void wxApp :: RemoveIdleSource ()
617 wxMutexLocker
lock ( gs_idleTagsMutex
);
621 g_source_remove ( m_idleTag
);