]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/app.cpp
5331f64a782f57128e8cdd240a49746cc751f803
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 static gint
wxapp_idle_callback ( gpointer
WXUNUSED ( data
) )
193 // don't generate the idle events while the assert modal dialog is shown,
194 // this completely confuses the apps which don't expect to be reentered
195 // from some safely-looking functions
196 if ( wxTheApp
-> IsInAssert () )
198 #endif // __WXDEBUG__
200 // When getting called from GDK's time-out handler
201 // we are no longer within GDK's grab on the GUI
202 // thread so we must lock it here ourselves.
205 // Indicate that we are now in idle mode and event handlers
206 // will have to reinstall the idle handler again.
209 wxMutexLocker
lock ( gs_idleTagsMutex
);
212 wxTheApp
-> m_idleTag
= 0 ;
217 // Send idle event to all who request them as long as
218 // no events have popped up in the event queue.
219 while ( ( moreIdles
= wxTheApp
-> ProcessIdle ()) && gtk_events_pending () == 0 )
222 // Release lock again
225 // Return FALSE if no more idle events are to be sent
233 #define wxPollFd pollfd
236 typedef GPollFD wxPollFd
;
238 int wxPoll ( wxPollFd
* ufds
, unsigned int nfds
, int timeout
)
240 // convert timeout from ms to struct timeval (s/us)
242 tv_timeout
. tv_sec
= timeout
/ 1000 ;
243 tv_timeout
. tv_usec
= ( timeout%1000
)* 1000 ;
245 // remember the highest fd used here
248 // and fill the sets for select()
253 wxFD_ZERO (& writefds
);
254 wxFD_ZERO (& exceptfds
);
257 for ( i
= 0 ; i
< nfds
; i
++ )
259 wxASSERT_MSG ( ufds
[ i
]. fd
< wxFD_SETSIZE
, _T ( "fd out of range" ) );
261 if ( ufds
[ i
]. events
& G_IO_IN
)
262 wxFD_SET ( ufds
[ i
]. fd
, & readfds
);
264 if ( ufds
[ i
]. events
& G_IO_PRI
)
265 wxFD_SET ( ufds
[ i
]. fd
, & exceptfds
);
267 if ( ufds
[ i
]. events
& G_IO_OUT
)
268 wxFD_SET ( ufds
[ i
]. fd
, & writefds
);
270 if ( ufds
[ i
]. fd
> fdMax
)
275 int res
= select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);
277 // translate the results back
278 for ( i
= 0 ; i
< nfds
; i
++ )
282 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & readfds
) )
283 ufds
[ i
]. revents
|= G_IO_IN
;
285 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & exceptfds
) )
286 ufds
[ i
]. revents
|= G_IO_PRI
;
288 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & writefds
) )
289 ufds
[ i
]. revents
|= G_IO_OUT
;
295 #endif // HAVE_POLL/!HAVE_POLL
297 static gint
wxapp_poll_func ( GPollFD
* ufds
, guint nfds
, gint timeout
)
302 g_mainThreadLocked
= true ;
304 // we rely on the fact that glib GPollFD struct is really just pollfd but
305 // I wonder how wise is this in the long term (VZ)
306 gint res
= wxPoll ( ( wxPollFd
*) ufds
, nfds
, timeout
);
309 g_mainThreadLocked
= false ;
316 #endif // wxUSE_THREADS
320 void wxapp_install_idle_handler ()
323 wxMutexLocker
lock ( gs_idleTagsMutex
);
326 // Don't install the handler if it's already installed. This test *MUST*
327 // be done when gs_idleTagsMutex is locked!
331 // GD: this assert is raised when using the thread sample (which works)
332 // so the test is probably not so easy. Can widget callbacks be
333 // triggered from child threads and, if so, for which widgets?
334 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
336 wxASSERT_MSG ( wxTheApp
-> m_idleTag
== 0 , wxT ( "attempt to install idle handler twice" ) );
340 // This routine gets called by all event handlers
341 // indicating that the idle is over. It may also
342 // get called from other thread for sending events
343 // to the main thread (and processing these in
344 // idle time). Very low priority.
345 wxTheApp
-> m_idleTag
= g_idle_add_full ( G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
348 //-----------------------------------------------------------------------------
349 // Access to the root window global
350 //-----------------------------------------------------------------------------
352 GtkWidget
* wxGetRootWindow ()
354 if ( gs_RootWindow
== NULL
)
356 gs_RootWindow
= gtk_window_new ( GTK_WINDOW_TOPLEVEL
);
357 gtk_widget_realize ( gs_RootWindow
);
359 return gs_RootWindow
;
362 //-----------------------------------------------------------------------------
364 //-----------------------------------------------------------------------------
366 IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)
368 BEGIN_EVENT_TABLE ( wxApp
, wxEvtHandler
)
369 EVT_IDLE ( wxAppBase :: OnIdle
)
375 m_isInAssert
= false ;
376 #endif // __WXDEBUG__
380 wxapp_install_idle_handler ();
383 g_main_context_set_poll_func ( NULL
, wxapp_poll_func
);
386 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
387 m_glVisualInfo
= ( void *) NULL
;
388 m_glFBCInfo
= ( void *) NULL
;
394 g_source_remove ( m_idleTag
);
397 bool wxApp :: OnInitGui ()
399 if ( ! wxAppBase :: OnInitGui () )
402 // if this is a wxGLApp (derived from wxApp), and we've already
403 // chosen a specific visual, then derive the GdkVisual from that
404 if ( m_glVisualInfo
!= NULL
)
406 // seems gtk_widget_set_default_visual no longer exists?
407 GdkVisual
* vis
= gtk_widget_get_default_visual ();
409 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
410 gtk_widget_set_default_colormap ( colormap
);
413 // On some machines, the default visual is just 256 colours, so
414 // we make sure we get the best. This can sometimes be wasteful.
417 if (( gdk_visual_get_best () != gdk_visual_get_system ()) && ( m_useBestVisual
))
419 /* seems gtk_widget_set_default_visual no longer exists? */
420 GdkVisual
* vis
= gtk_widget_get_default_visual ();
422 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
423 gtk_widget_set_default_colormap ( colormap
);
429 GdkVisual
* wxApp :: GetGdkVisual ()
431 GdkVisual
* visual
= NULL
;
434 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
436 visual
= gdk_drawable_get_visual ( wxGetRootWindow ()-> window
);
443 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
448 if (! g_thread_supported ())
450 #endif // wxUSE_THREADS
454 // We should have the wxUSE_WCHAR_T test on the _outside_
456 // gtk+ 2.0 supports Unicode through UTF-8 strings
457 wxConvCurrent
= & wxConvUTF8
;
458 #else // !wxUSE_WCHAR_T
460 wxConvCurrent
= ( wxMBConv
*) NULL
;
461 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
463 // decide which conversion to use for the file names
465 // (1) this variable exists for the sole purpose of specifying the encoding
466 // of the filenames for GTK+ programs, so use it if it is set
467 wxString
encName ( wxGetenv ( _T ( "G_FILENAME_ENCODING" )));
468 encName
= encName
. BeforeFirst ( _T ( ',' ));
469 if ( encName
== _T ( "@locale" ))
475 // (2) if a non default locale is set, assume that the user wants his
476 // filenames in this locale too
477 encName
= wxLocale :: GetSystemEncodingName (). Upper ();
478 // (3) finally use UTF-8 by default
479 if ( encName
. empty () || encName
== _T ( "US-ASCII" ))
480 encName
= _T ( "UTF-8" );
481 wxSetEnv ( _T ( "G_FILENAME_ENCODING" ), encName
);
485 encName
= _T ( "UTF-8" );
487 static wxConvBrokenFileNames
fileconv ( encName
);
488 wxConvFileName
= & fileconv
;
491 // gtk_init() wants UTF-8, not wchar_t, so convert
493 char ** argvGTK
= new char *[ argc
+ 1 ];
494 for ( i
= 0 ; i
< argc
; i
++ )
496 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
499 argvGTK
[ argc
] = NULL
;
504 init_result
= true ; // is there a _check() version of this?
505 gpe_application_init ( & argcGTK
, & argvGTK
);
507 init_result
= gtk_init_check ( & argcGTK
, & argvGTK
);
510 if ( argcGTK
!= argc
)
512 // we have to drop the parameters which were consumed by GTK+
513 for ( i
= 0 ; i
< argcGTK
; i
++ )
515 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
517 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
523 //else: gtk_init() didn't modify our parameters
526 for ( i
= 0 ; i
< argcGTK
; i
++ )
532 #else // !wxUSE_UNICODE
533 // gtk_init() shouldn't actually change argv itself (just its contents) so
534 // it's ok to pass pointer to it
535 init_result
= gtk_init_check ( & argc
, & argv
);
536 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
539 wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));
543 // we can not enter threads before gtk_init is done
546 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
553 wxSetDetectableAutoRepeat ( true );
556 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
562 void wxApp :: CleanUp ()
566 wxAppBase :: CleanUp ();
571 void wxApp :: OnAssertFailure ( const wxChar
* file
,
579 wxAppBase :: OnAssertFailure ( file
, line
, func
, cond
, msg
);
581 m_isInAssert
= false ;
584 #endif // __WXDEBUG__
586 void wxApp :: RemoveIdleTag ()
589 wxMutexLocker
lock ( gs_idleTagsMutex
);
593 g_source_remove ( wxTheApp
-> m_idleTag
);
594 wxTheApp
-> m_idleTag
= 0 ;