]>
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/dialog.h"
26 #include "wx/settings.h"
27 #include "wx/msgdlg.h"
28 #include "wx/memory.h"
30 #include "wx/gdicmn.h"
34 #include "wx/filename.h"
35 #include "wx/module.h"
37 #include "wx/thread.h"
43 #ifdef __WXUNIVERSAL__
44 #include "wx/univ/theme.h"
45 #include "wx/univ/renderer.h"
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
68 // we implement poll() ourselves using select() which is supposed exist in
70 #include <sys/types.h>
73 #endif // HAVE_POLL/!HAVE_POLL
75 #include "wx/unix/private.h"
76 #include "wx/gtk/win_gtk.h"
77 #include "wx/gtk/private.h"
81 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
86 #include "wx/html/forcelnk.h"
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
94 bool g_mainThreadLocked
= false ;
95 gint g_pendingTag
= 0 ;
97 static GtkWidget
* gs_RootWindow
= ( GtkWidget
*) NULL
;
99 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
103 void wxapp_install_idle_handler ();
106 static wxMutex gs_idleTagsMutex
;
109 //-----------------------------------------------------------------------------
111 //-----------------------------------------------------------------------------
113 // not static because used by textctrl.cpp
116 bool wxIsInsideYield
= false ;
118 bool wxApp :: Yield ( bool onlyIfNeeded
)
120 if ( wxIsInsideYield
)
124 wxFAIL_MSG ( wxT ( "wxYield called recursively" ) );
131 if ( ! wxThread :: IsMain () )
133 // can't call gtk_main_iteration() from other threads like this
136 #endif // wxUSE_THREADS
138 wxIsInsideYield
= true ;
140 // We need to remove idle callbacks or the loop will
142 wxTheApp
-> RemoveIdleTag ();
145 // disable log flushing from here because a call to wxYield() shouldn't
146 // normally result in message boxes popping up &c
150 while ( gtk_events_pending ())
151 gtk_main_iteration ();
153 // It's necessary to call ProcessIdle() to update the frames sizes which
154 // might have been changed (it also will update other things set from
155 // OnUpdateUI() which is a nice (and desired) side effect). But we
156 // call ProcessIdle() only once since this is not meant for longish
157 // background jobs (controlled by wxIdleEvent::RequestMore() and the
158 // return value of Processidle().
162 // let the logs be flashed again
166 wxIsInsideYield
= false ;
171 //-----------------------------------------------------------------------------
173 //-----------------------------------------------------------------------------
175 // RR/KH: No wxMutexGui calls are needed here according to the GTK faq,
176 // http://www.gtk.org/faq/#AEN500 - this caused problems for wxPostEvent.
178 void wxApp :: WakeUpIdle ()
180 wxapp_install_idle_handler ();
183 //-----------------------------------------------------------------------------
185 //-----------------------------------------------------------------------------
187 // the callback functions must be extern "C" to comply with GTK+ declarations
191 static gint
wxapp_pending_callback ( gpointer
WXUNUSED ( data
) )
193 if (! wxTheApp
) return TRUE
;
195 // When getting called from GDK's time-out handler
196 // we are no longer within GDK's grab on the GUI
197 // thread so we must lock it here ourselves.
200 // Sent idle event to all who request them.
201 wxTheApp
-> ProcessPendingEvents ();
205 wxMutexLocker
lock ( gs_idleTagsMutex
);
210 // Flush the logged messages if any.
212 wxLog :: FlushActive ();
215 // Release lock again
218 // Return FALSE to indicate that no more idle events are
219 // to be sent (single shot instead of continuous stream)
223 static gint
wxapp_idle_callback ( gpointer
WXUNUSED ( data
) )
229 // don't generate the idle events while the assert modal dialog is shown,
230 // this completely confuses the apps which don't expect to be reentered
231 // from some safely-looking functions
232 if ( wxTheApp
-> IsInAssert () )
234 // But repaint the assertion message if necessary
235 if ( wxTopLevelWindows
. GetCount () > 0 )
237 wxWindow
* win
= ( wxWindow
*) wxTopLevelWindows
. GetLast ()-> GetData ();
238 if ( win
-> IsKindOf ( CLASSINFO ( wxMessageDialog
)))
239 win
-> OnInternalIdle ();
243 #endif // __WXDEBUG__
245 // When getting called from GDK's time-out handler
246 // we are no longer within GDK's grab on the GUI
247 // thread so we must lock it here ourselves.
250 // Indicate that we are now in idle mode and event handlers
251 // will have to reinstall the idle handler again.
254 wxMutexLocker
lock ( gs_idleTagsMutex
);
257 wxTheApp
-> m_idleTag
= 0 ;
262 // Send idle event to all who request them as long as
263 // no events have popped up in the event queue.
264 while ( ( moreIdles
= wxTheApp
-> ProcessIdle ()) && gtk_events_pending () == 0 )
267 // Release lock again
270 // Return FALSE if no more idle events are to be sent
278 #define wxPollFd pollfd
281 typedef GPollFD wxPollFd
;
283 int wxPoll ( wxPollFd
* ufds
, unsigned int nfds
, int timeout
)
285 // convert timeout from ms to struct timeval (s/us)
287 tv_timeout
. tv_sec
= timeout
/ 1000 ;
288 tv_timeout
. tv_usec
= ( timeout%1000
)* 1000 ;
290 // remember the highest fd used here
293 // and fill the sets for select()
298 wxFD_ZERO (& writefds
);
299 wxFD_ZERO (& exceptfds
);
302 for ( i
= 0 ; i
< nfds
; i
++ )
304 wxASSERT_MSG ( ufds
[ i
]. fd
< wxFD_SETSIZE
, _T ( "fd out of range" ) );
306 if ( ufds
[ i
]. events
& G_IO_IN
)
307 wxFD_SET ( ufds
[ i
]. fd
, & readfds
);
309 if ( ufds
[ i
]. events
& G_IO_PRI
)
310 wxFD_SET ( ufds
[ i
]. fd
, & exceptfds
);
312 if ( ufds
[ i
]. events
& G_IO_OUT
)
313 wxFD_SET ( ufds
[ i
]. fd
, & writefds
);
315 if ( ufds
[ i
]. fd
> fdMax
)
320 int res
= select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);
322 // translate the results back
323 for ( i
= 0 ; i
< nfds
; i
++ )
327 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & readfds
) )
328 ufds
[ i
]. revents
|= G_IO_IN
;
330 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & exceptfds
) )
331 ufds
[ i
]. revents
|= G_IO_PRI
;
333 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & writefds
) )
334 ufds
[ i
]. revents
|= G_IO_OUT
;
340 #endif // HAVE_POLL/!HAVE_POLL
342 static gint
wxapp_poll_func ( GPollFD
* ufds
, guint nfds
, gint timeout
)
347 g_mainThreadLocked
= true ;
349 // we rely on the fact that glib GPollFD struct is really just pollfd but
350 // I wonder how wise is this in the long term (VZ)
351 gint res
= wxPoll ( ( wxPollFd
*) ufds
, nfds
, timeout
);
354 g_mainThreadLocked
= false ;
361 #endif // wxUSE_THREADS
365 void wxapp_install_idle_handler ()
368 wxMutexLocker
lock ( gs_idleTagsMutex
);
371 // Don't install the handler if it's already installed. This test *MUST*
372 // be done when gs_idleTagsMutex is locked!
376 // GD: this assert is raised when using the thread sample (which works)
377 // so the test is probably not so easy. Can widget callbacks be
378 // triggered from child threads and, if so, for which widgets?
379 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
381 wxASSERT_MSG ( wxTheApp
-> m_idleTag
== 0 , wxT ( "attempt to install idle handler twice" ) );
385 if ( g_pendingTag
== 0 )
386 g_pendingTag
= g_idle_add_full ( 900 , wxapp_pending_callback
, NULL
, NULL
);
388 // This routine gets called by all event handlers
389 // indicating that the idle is over. It may also
390 // get called from other thread for sending events
391 // to the main thread (and processing these in
392 // idle time). Very low priority.
393 wxTheApp
-> m_idleTag
= g_idle_add_full ( 1000 , wxapp_idle_callback
, NULL
, NULL
);
396 //-----------------------------------------------------------------------------
397 // Access to the root window global
398 //-----------------------------------------------------------------------------
400 GtkWidget
* wxGetRootWindow ()
402 if ( gs_RootWindow
== NULL
)
404 gs_RootWindow
= gtk_window_new ( GTK_WINDOW_TOPLEVEL
);
405 gtk_widget_realize ( gs_RootWindow
);
407 return gs_RootWindow
;
410 //-----------------------------------------------------------------------------
412 //-----------------------------------------------------------------------------
414 IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)
416 BEGIN_EVENT_TABLE ( wxApp
, wxEvtHandler
)
417 EVT_IDLE ( wxAppBase :: OnIdle
)
423 m_isInAssert
= false ;
424 #endif // __WXDEBUG__
428 wxapp_install_idle_handler ();
431 g_main_context_set_poll_func ( NULL
, wxapp_poll_func
);
434 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
435 m_glVisualInfo
= ( void *) NULL
;
436 m_glFBCInfo
= ( void *) NULL
;
442 g_source_remove ( m_idleTag
);
445 bool wxApp :: OnInitGui ()
447 if ( ! wxAppBase :: OnInitGui () )
450 // if this is a wxGLApp (derived from wxApp), and we've already
451 // chosen a specific visual, then derive the GdkVisual from that
452 if ( m_glVisualInfo
!= NULL
)
454 // seems gtk_widget_set_default_visual no longer exists?
455 GdkVisual
* vis
= gtk_widget_get_default_visual ();
457 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
458 gtk_widget_set_default_colormap ( colormap
);
461 // On some machines, the default visual is just 256 colours, so
462 // we make sure we get the best. This can sometimes be wasteful.
465 if (( gdk_visual_get_best () != gdk_visual_get_system ()) && ( m_useBestVisual
))
467 /* seems gtk_widget_set_default_visual no longer exists? */
468 GdkVisual
* vis
= gtk_widget_get_default_visual ();
470 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
471 gtk_widget_set_default_colormap ( colormap
);
477 GdkVisual
* wxApp :: GetGdkVisual ()
479 GdkVisual
* visual
= NULL
;
482 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
484 visual
= gdk_drawable_get_visual ( wxGetRootWindow ()-> window
);
491 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
496 if (! g_thread_supported ())
498 #endif // wxUSE_THREADS
502 // We should have the wxUSE_WCHAR_T test on the _outside_
504 // gtk+ 2.0 supports Unicode through UTF-8 strings
505 wxConvCurrent
= & wxConvUTF8
;
506 #else // !wxUSE_WCHAR_T
508 wxConvCurrent
= ( wxMBConv
*) NULL
;
509 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
511 // decide which conversion to use for the file names
513 // (1) this variable exists for the sole purpose of specifying the encoding
514 // of the filenames for GTK+ programs, so use it if it is set
515 wxString
encName ( wxGetenv ( _T ( "G_FILENAME_ENCODING" )));
516 encName
= encName
. BeforeFirst ( _T ( ',' ));
517 if ( encName
== _T ( "@locale" ))
523 // (2) if a non default locale is set, assume that the user wants his
524 // filenames in this locale too
525 encName
= wxLocale :: GetSystemEncodingName (). Upper ();
526 // (3) finally use UTF-8 by default
527 if ( encName
. empty () || encName
== _T ( "US-ASCII" ))
528 encName
= _T ( "UTF-8" );
529 wxSetEnv ( _T ( "G_FILENAME_ENCODING" ), encName
);
533 encName
= _T ( "UTF-8" );
535 static wxConvBrokenFileNames
fileconv ( encName
);
536 wxConvFileName
= & fileconv
;
539 // gtk_init() wants UTF-8, not wchar_t, so convert
541 char ** argvGTK
= new char *[ argc
+ 1 ];
542 for ( i
= 0 ; i
< argc
; i
++ )
544 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
547 argvGTK
[ argc
] = NULL
;
552 init_result
= true ; // is there a _check() version of this?
553 gpe_application_init ( & argcGTK
, & argvGTK
);
555 init_result
= gtk_init_check ( & argcGTK
, & argvGTK
);
558 if ( argcGTK
!= argc
)
560 // we have to drop the parameters which were consumed by GTK+
561 for ( i
= 0 ; i
< argcGTK
; i
++ )
563 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
565 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
571 //else: gtk_init() didn't modify our parameters
574 for ( i
= 0 ; i
< argcGTK
; i
++ )
580 #else // !wxUSE_UNICODE
581 // gtk_init() shouldn't actually change argv itself (just its contents) so
582 // it's ok to pass pointer to it
583 init_result
= gtk_init_check ( & argc
, & argv
);
584 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
587 wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));
591 // we can not enter threads before gtk_init is done
594 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
601 wxSetDetectableAutoRepeat ( true );
604 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
610 void wxApp :: CleanUp ()
614 wxAppBase :: CleanUp ();
619 void wxApp :: OnAssert ( const wxChar
* file
, int line
, const wxChar
* cond
, const wxChar
* msg
)
623 wxAppBase :: OnAssert ( file
, line
, cond
, msg
);
625 m_isInAssert
= false ;
628 #endif // __WXDEBUG__
630 void wxApp :: RemoveIdleTag ()
633 wxMutexLocker
lock ( gs_idleTagsMutex
);
637 g_source_remove ( wxTheApp
-> m_idleTag
);
638 wxTheApp
-> m_idleTag
= 0 ;