]>
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 SuspendIdleCallback ();
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
) )
211 // this does not look possible, but just in case...
215 bool moreIdles
= false ;
218 // don't generate the idle events while the assert modal dialog is shown,
219 // this matches the behavior of wxMSW
220 if (! wxTheApp
-> IsInAssert ())
221 #endif // __WXDEBUG__
225 // Allow another idle source to be added while this one is busy.
226 // Needed if an idle event handler runs a new event loop,
227 // for example by showing a dialog.
229 wxMutexLocker
lock ( gs_idleTagsMutex
);
231 idleID_save
= wxTheApp
-> m_idleTag
;
232 wxTheApp
-> m_idleTag
= 0 ;
237 // When getting called from GDK's time-out handler
238 // we are no longer within GDK's grab on the GUI
239 // thread so we must lock it here ourselves.
242 // Send idle event to all who request them as long as
243 // no events have popped up in the event queue.
245 moreIdles
= wxTheApp
-> ProcessIdle ();
246 } while ( moreIdles
&& gtk_events_pending () == 0 );
248 // Release lock again
252 // If another idle source was added, remove it
254 wxMutexLocker
lock ( gs_idleTagsMutex
);
256 if ( wxTheApp
-> m_idleTag
!= 0 )
257 g_source_remove ( wxTheApp
-> m_idleTag
);
258 wxTheApp
-> m_idleTag
= idleID_save
;
266 wxMutexLocker
lock ( gs_idleTagsMutex
);
268 // Indicate that we are now in idle mode and event handlers
269 // will have to reinstall the idle handler again.
271 wxTheApp
-> m_idleTag
= 0 ;
276 // Return FALSE if no more idle events are to be sent
284 #define wxPollFd pollfd
287 typedef GPollFD wxPollFd
;
289 int wxPoll ( wxPollFd
* ufds
, unsigned int nfds
, int timeout
)
291 // convert timeout from ms to struct timeval (s/us)
293 tv_timeout
. tv_sec
= timeout
/ 1000 ;
294 tv_timeout
. tv_usec
= ( timeout%1000
)* 1000 ;
296 // remember the highest fd used here
299 // and fill the sets for select()
304 wxFD_ZERO (& writefds
);
305 wxFD_ZERO (& exceptfds
);
308 for ( i
= 0 ; i
< nfds
; i
++ )
310 wxASSERT_MSG ( ufds
[ i
]. fd
< FD_SETSIZE
, _T ( "fd out of range" ) );
312 if ( ufds
[ i
]. events
& G_IO_IN
)
313 wxFD_SET ( ufds
[ i
]. fd
, & readfds
);
315 if ( ufds
[ i
]. events
& G_IO_PRI
)
316 wxFD_SET ( ufds
[ i
]. fd
, & exceptfds
);
318 if ( ufds
[ i
]. events
& G_IO_OUT
)
319 wxFD_SET ( ufds
[ i
]. fd
, & writefds
);
321 if ( ufds
[ i
]. fd
> fdMax
)
326 int res
= select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);
328 // translate the results back
329 for ( i
= 0 ; i
< nfds
; i
++ )
333 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & readfds
) )
334 ufds
[ i
]. revents
|= G_IO_IN
;
336 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & exceptfds
) )
337 ufds
[ i
]. revents
|= G_IO_PRI
;
339 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & writefds
) )
340 ufds
[ i
]. revents
|= G_IO_OUT
;
346 #endif // HAVE_POLL/!HAVE_POLL
348 static gint
wxapp_poll_func ( GPollFD
* ufds
, guint nfds
, gint timeout
)
353 g_mainThreadLocked
= true ;
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
);
360 g_mainThreadLocked
= false ;
367 #endif // wxUSE_THREADS
371 void wxapp_install_idle_handler ()
373 if ( wxTheApp
== NULL
)
377 wxMutexLocker
lock ( gs_idleTagsMutex
);
380 // Don't install the handler if it's already installed. This test *MUST*
381 // be done when gs_idleTagsMutex is locked!
385 // GD: this assert is raised when using the thread sample (which works)
386 // so the test is probably not so easy. Can widget callbacks be
387 // triggered from child threads and, if so, for which widgets?
388 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
390 wxASSERT_MSG ( wxTheApp
-> m_idleTag
== 0 , wxT ( "attempt to install idle handler twice" ) );
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
= g_idle_add_full ( G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
402 //-----------------------------------------------------------------------------
403 // Access to the root window global
404 //-----------------------------------------------------------------------------
406 GtkWidget
* wxGetRootWindow ()
408 if ( gs_RootWindow
== NULL
)
410 gs_RootWindow
= gtk_window_new ( GTK_WINDOW_TOPLEVEL
);
411 gtk_widget_realize ( gs_RootWindow
);
413 return gs_RootWindow
;
416 //-----------------------------------------------------------------------------
418 //-----------------------------------------------------------------------------
420 IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)
422 BEGIN_EVENT_TABLE ( wxApp
, wxEvtHandler
)
423 EVT_IDLE ( wxAppBase :: OnIdle
)
429 m_isInAssert
= false ;
430 #endif // __WXDEBUG__
434 wxapp_install_idle_handler ();
437 g_main_context_set_poll_func ( NULL
, wxapp_poll_func
);
440 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
441 m_glVisualInfo
= ( void *) NULL
;
442 m_glFBCInfo
= ( void *) NULL
;
448 g_source_remove ( m_idleTag
);
451 bool wxApp :: OnInitGui ()
453 if ( ! wxAppBase :: OnInitGui () )
456 // if this is a wxGLApp (derived from wxApp), and we've already
457 // chosen a specific visual, then derive the GdkVisual from that
458 if ( m_glVisualInfo
!= NULL
)
460 GdkVisual
* vis
= gtk_widget_get_default_visual ();
462 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
463 gtk_widget_set_default_colormap ( colormap
);
467 // On some machines, the default visual is just 256 colours, so
468 // we make sure we get the best. This can sometimes be wasteful.
471 if ( m_forceTrueColour
)
473 GdkVisual
* visual
= gdk_visual_get_best_with_both ( 24 , GDK_VISUAL_TRUE_COLOR
);
476 wxLogError ( wxT ( "Unable to initialize TrueColor visual." ));
479 GdkColormap
* colormap
= gdk_colormap_new ( visual
, FALSE
);
480 gtk_widget_set_default_colormap ( colormap
);
484 if ( gdk_visual_get_best () != gdk_visual_get_system ())
486 GdkVisual
* visual
= gdk_visual_get_best ();
487 GdkColormap
* colormap
= gdk_colormap_new ( visual
, FALSE
);
488 gtk_widget_set_default_colormap ( colormap
);
497 GdkVisual
* wxApp :: GetGdkVisual ()
499 GdkVisual
* visual
= NULL
;
502 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
504 visual
= gdk_drawable_get_visual ( wxGetRootWindow ()-> window
);
511 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
516 if (! g_thread_supported ())
518 #endif // wxUSE_THREADS
522 // We should have the wxUSE_WCHAR_T test on the _outside_
524 // gtk+ 2.0 supports Unicode through UTF-8 strings
525 wxConvCurrent
= & wxConvUTF8
;
526 #else // !wxUSE_WCHAR_T
528 wxConvCurrent
= ( wxMBConv
*) NULL
;
529 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
531 // decide which conversion to use for the file names
533 // (1) this variable exists for the sole purpose of specifying the encoding
534 // of the filenames for GTK+ programs, so use it if it is set
535 wxString
encName ( wxGetenv ( _T ( "G_FILENAME_ENCODING" )));
536 encName
= encName
. BeforeFirst ( _T ( ',' ));
537 if ( encName
. CmpNoCase ( _T ( "@locale" )) == 0 )
543 // (2) if a non default locale is set, assume that the user wants his
544 // filenames in this locale too
545 encName
= wxLocale :: GetSystemEncodingName (). Upper ();
546 // (3) finally use UTF-8 by default
547 if ( encName
. empty () || encName
== _T ( "US-ASCII" ))
548 encName
= _T ( "UTF-8" );
549 wxSetEnv ( _T ( "G_FILENAME_ENCODING" ), encName
);
553 encName
= _T ( "UTF-8" );
555 static wxConvBrokenFileNames
fileconv ( encName
);
556 wxConvFileName
= & fileconv
;
559 // gtk_init() wants UTF-8, not wchar_t, so convert
561 char ** argvGTK
= new char *[ argc
+ 1 ];
562 for ( i
= 0 ; i
< argc
; i
++ )
564 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
567 argvGTK
[ argc
] = NULL
;
572 init_result
= true ; // is there a _check() version of this?
573 gpe_application_init ( & argcGTK
, & argvGTK
);
575 init_result
= gtk_init_check ( & argcGTK
, & argvGTK
);
578 if ( argcGTK
!= argc
)
580 // we have to drop the parameters which were consumed by GTK+
581 for ( i
= 0 ; i
< argcGTK
; i
++ )
583 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
585 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
591 //else: gtk_init() didn't modify our parameters
594 for ( i
= 0 ; i
< argcGTK
; i
++ )
600 #else // !wxUSE_UNICODE
601 // gtk_init() shouldn't actually change argv itself (just its contents) so
602 // it's ok to pass pointer to it
603 init_result
= gtk_init_check ( & argc
, & argv
);
604 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
607 wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));
611 // we can not enter threads before gtk_init is done
614 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
621 wxSetDetectableAutoRepeat ( true );
624 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
630 void wxApp :: CleanUp ()
634 wxAppBase :: CleanUp ();
639 void wxApp :: OnAssertFailure ( const wxChar
* file
,
646 // block wx idle events while assert dialog is showing
649 wxAppBase :: OnAssertFailure ( file
, line
, func
, cond
, msg
);
651 m_isInAssert
= false ;
654 #endif // __WXDEBUG__
656 void wxApp :: SuspendIdleCallback ()
659 wxMutexLocker
lock ( gs_idleTagsMutex
);
663 g_source_remove ( m_idleTag
);