]>
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"
35 #include "wx/filename.h"
36 #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 #ifdef HAVE_SYS_SELECT_H
74 #include <sys/select.h>
76 #endif // HAVE_POLL/!HAVE_POLL
78 #include "wx/unix/private.h"
79 #include "wx/gtk/win_gtk.h"
80 #include "wx/gtk/private.h"
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
89 #include "wx/html/forcelnk.h"
93 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
97 bool g_mainThreadLocked
= false ;
98 gint g_pendingTag
= 0 ;
100 static GtkWidget
* gs_RootWindow
= ( GtkWidget
*) NULL
;
102 //-----------------------------------------------------------------------------
104 //-----------------------------------------------------------------------------
106 void wxapp_install_idle_handler ();
109 static wxMutex gs_idleTagsMutex
;
112 //-----------------------------------------------------------------------------
114 //-----------------------------------------------------------------------------
116 // not static because used by textctrl.cpp
119 bool wxIsInsideYield
= false ;
121 bool wxApp :: Yield ( bool onlyIfNeeded
)
123 if ( wxIsInsideYield
)
127 wxFAIL_MSG ( wxT ( "wxYield called recursively" ) );
134 if ( ! wxThread :: IsMain () )
136 // can't call gtk_main_iteration() from other threads like this
139 #endif // wxUSE_THREADS
141 wxIsInsideYield
= true ;
143 // We need to remove idle callbacks or the loop will
145 wxTheApp
-> RemoveIdleTag ();
148 // disable log flushing from here because a call to wxYield() shouldn't
149 // normally result in message boxes popping up &c
153 while ( gtk_events_pending ())
154 gtk_main_iteration ();
156 // It's necessary to call ProcessIdle() to update the frames sizes which
157 // might have been changed (it also will update other things set from
158 // OnUpdateUI() which is a nice (and desired) side effect). But we
159 // call ProcessIdle() only once since this is not meant for longish
160 // background jobs (controlled by wxIdleEvent::RequestMore() and the
161 // return value of Processidle().
165 // let the logs be flashed again
169 wxIsInsideYield
= false ;
174 //-----------------------------------------------------------------------------
176 //-----------------------------------------------------------------------------
178 // RR/KH: No wxMutexGui calls are needed here according to the GTK faq,
179 // http://www.gtk.org/faq/#AEN500 - this caused problems for wxPostEvent.
181 void wxApp :: WakeUpIdle ()
183 wxapp_install_idle_handler ();
186 //-----------------------------------------------------------------------------
188 //-----------------------------------------------------------------------------
190 // the callback functions must be extern "C" to comply with GTK+ declarations
194 static gint
wxapp_pending_callback ( gpointer
WXUNUSED ( data
) )
196 if (! wxTheApp
) return TRUE
;
198 // When getting called from GDK's time-out handler
199 // we are no longer within GDK's grab on the GUI
200 // thread so we must lock it here ourselves.
203 // Sent idle event to all who request them.
204 wxTheApp
-> ProcessPendingEvents ();
208 wxMutexLocker
lock ( gs_idleTagsMutex
);
213 // Flush the logged messages if any.
215 wxLog :: FlushActive ();
218 // Release lock again
221 // Return FALSE to indicate that no more idle events are
222 // to be sent (single shot instead of continuous stream)
226 static gint
wxapp_idle_callback ( gpointer
WXUNUSED ( data
) )
232 // don't generate the idle events while the assert modal dialog is shown,
233 // this completely confuses the apps which don't expect to be reentered
234 // from some safely-looking functions
235 if ( wxTheApp
-> IsInAssert () )
237 // But repaint the assertion message if necessary
238 if ( wxTopLevelWindows
. GetCount () > 0 )
240 wxWindow
* win
= ( wxWindow
*) wxTopLevelWindows
. GetLast ()-> GetData ();
241 if ( win
-> IsKindOf ( CLASSINFO ( wxMessageDialog
)))
242 win
-> OnInternalIdle ();
246 #endif // __WXDEBUG__
248 // When getting called from GDK's time-out handler
249 // we are no longer within GDK's grab on the GUI
250 // thread so we must lock it here ourselves.
253 // Indicate that we are now in idle mode and event handlers
254 // will have to reinstall the idle handler again.
257 wxMutexLocker
lock ( gs_idleTagsMutex
);
260 wxTheApp
-> m_idleTag
= 0 ;
265 // Send idle event to all who request them as long as
266 // no events have popped up in the event queue.
267 while ( ( moreIdles
= wxTheApp
-> ProcessIdle ()) && gtk_events_pending () == 0 )
270 // Release lock again
273 // Return FALSE if no more idle events are to be sent
281 #define wxPollFd pollfd
284 typedef GPollFD wxPollFd
;
286 int wxPoll ( wxPollFd
* ufds
, unsigned int nfds
, int timeout
)
288 // convert timeout from ms to struct timeval (s/us)
290 tv_timeout
. tv_sec
= timeout
/ 1000 ;
291 tv_timeout
. tv_usec
= ( timeout%1000
)* 1000 ;
293 // remember the highest fd used here
296 // and fill the sets for select()
301 wxFD_ZERO (& writefds
);
302 wxFD_ZERO (& exceptfds
);
305 for ( i
= 0 ; i
< nfds
; i
++ )
307 wxASSERT_MSG ( ufds
[ i
]. fd
< wxFD_SETSIZE
, _T ( "fd out of range" ) );
309 if ( ufds
[ i
]. events
& G_IO_IN
)
310 wxFD_SET ( ufds
[ i
]. fd
, & readfds
);
312 if ( ufds
[ i
]. events
& G_IO_PRI
)
313 wxFD_SET ( ufds
[ i
]. fd
, & exceptfds
);
315 if ( ufds
[ i
]. events
& G_IO_OUT
)
316 wxFD_SET ( ufds
[ i
]. fd
, & writefds
);
318 if ( ufds
[ i
]. fd
> fdMax
)
323 int res
= select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);
325 // translate the results back
326 for ( i
= 0 ; i
< nfds
; i
++ )
330 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & readfds
) )
331 ufds
[ i
]. revents
|= G_IO_IN
;
333 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & exceptfds
) )
334 ufds
[ i
]. revents
|= G_IO_PRI
;
336 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & writefds
) )
337 ufds
[ i
]. revents
|= G_IO_OUT
;
343 #endif // HAVE_POLL/!HAVE_POLL
345 static gint
wxapp_poll_func ( GPollFD
* ufds
, guint nfds
, gint timeout
)
350 g_mainThreadLocked
= true ;
352 // we rely on the fact that glib GPollFD struct is really just pollfd but
353 // I wonder how wise is this in the long term (VZ)
354 gint res
= wxPoll ( ( wxPollFd
*) ufds
, nfds
, timeout
);
357 g_mainThreadLocked
= false ;
364 #endif // wxUSE_THREADS
368 void wxapp_install_idle_handler ()
371 wxMutexLocker
lock ( gs_idleTagsMutex
);
374 // Don't install the handler if it's already installed. This test *MUST*
375 // be done when gs_idleTagsMutex is locked!
379 // GD: this assert is raised when using the thread sample (which works)
380 // so the test is probably not so easy. Can widget callbacks be
381 // triggered from child threads and, if so, for which widgets?
382 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
384 wxASSERT_MSG ( wxTheApp
-> m_idleTag
== 0 , wxT ( "attempt to install idle handler twice" ) );
388 if ( g_pendingTag
== 0 )
389 g_pendingTag
= g_idle_add_full ( 900 , wxapp_pending_callback
, NULL
, NULL
);
391 // This routine gets called by all event handlers
392 // indicating that the idle is over. It may also
393 // get called from other thread for sending events
394 // to the main thread (and processing these in
395 // idle time). Very low priority.
396 wxTheApp
-> m_idleTag
= g_idle_add_full ( 1000 , wxapp_idle_callback
, NULL
, NULL
);
399 //-----------------------------------------------------------------------------
400 // Access to the root window global
401 //-----------------------------------------------------------------------------
403 GtkWidget
* wxGetRootWindow ()
405 if ( gs_RootWindow
== NULL
)
407 gs_RootWindow
= gtk_window_new ( GTK_WINDOW_TOPLEVEL
);
408 gtk_widget_realize ( gs_RootWindow
);
410 return gs_RootWindow
;
413 //-----------------------------------------------------------------------------
415 //-----------------------------------------------------------------------------
417 IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)
419 BEGIN_EVENT_TABLE ( wxApp
, wxEvtHandler
)
420 EVT_IDLE ( wxAppBase :: OnIdle
)
426 m_isInAssert
= false ;
427 #endif // __WXDEBUG__
431 wxapp_install_idle_handler ();
434 g_main_context_set_poll_func ( NULL
, wxapp_poll_func
);
437 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
438 m_glVisualInfo
= ( void *) NULL
;
439 m_glFBCInfo
= ( void *) NULL
;
445 g_source_remove ( m_idleTag
);
448 bool wxApp :: OnInitGui ()
450 if ( ! wxAppBase :: OnInitGui () )
453 // if this is a wxGLApp (derived from wxApp), and we've already
454 // chosen a specific visual, then derive the GdkVisual from that
455 if ( m_glVisualInfo
!= NULL
)
457 // seems gtk_widget_set_default_visual no longer exists?
458 GdkVisual
* vis
= gtk_widget_get_default_visual ();
460 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
461 gtk_widget_set_default_colormap ( colormap
);
464 // On some machines, the default visual is just 256 colours, so
465 // we make sure we get the best. This can sometimes be wasteful.
468 if (( gdk_visual_get_best () != gdk_visual_get_system ()) && ( m_useBestVisual
))
470 /* seems gtk_widget_set_default_visual no longer exists? */
471 GdkVisual
* vis
= gtk_widget_get_default_visual ();
473 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
474 gtk_widget_set_default_colormap ( colormap
);
480 GdkVisual
* wxApp :: GetGdkVisual ()
482 GdkVisual
* visual
= NULL
;
485 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
487 visual
= gdk_drawable_get_visual ( wxGetRootWindow ()-> window
);
494 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
499 if (! g_thread_supported ())
501 #endif // wxUSE_THREADS
505 // We should have the wxUSE_WCHAR_T test on the _outside_
507 // gtk+ 2.0 supports Unicode through UTF-8 strings
508 wxConvCurrent
= & wxConvUTF8
;
509 #else // !wxUSE_WCHAR_T
511 wxConvCurrent
= ( wxMBConv
*) NULL
;
512 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
514 // decide which conversion to use for the file names
516 // (1) this variable exists for the sole purpose of specifying the encoding
517 // of the filenames for GTK+ programs, so use it if it is set
518 wxString
encName ( wxGetenv ( _T ( "G_FILENAME_ENCODING" )));
519 encName
= encName
. BeforeFirst ( _T ( ',' ));
520 if ( encName
== _T ( "@locale" ))
526 // (2) if a non default locale is set, assume that the user wants his
527 // filenames in this locale too
528 encName
= wxLocale :: GetSystemEncodingName (). Upper ();
529 // (3) finally use UTF-8 by default
530 if ( encName
. empty () || encName
== _T ( "US-ASCII" ))
531 encName
= _T ( "UTF-8" );
532 wxSetEnv ( _T ( "G_FILENAME_ENCODING" ), encName
);
536 encName
= _T ( "UTF-8" );
538 static wxConvBrokenFileNames
fileconv ( encName
);
539 wxConvFileName
= & fileconv
;
542 // gtk_init() wants UTF-8, not wchar_t, so convert
544 char ** argvGTK
= new char *[ argc
+ 1 ];
545 for ( i
= 0 ; i
< argc
; i
++ )
547 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
550 argvGTK
[ argc
] = NULL
;
555 init_result
= true ; // is there a _check() version of this?
556 gpe_application_init ( & argcGTK
, & argvGTK
);
558 init_result
= gtk_init_check ( & argcGTK
, & argvGTK
);
561 if ( argcGTK
!= argc
)
563 // we have to drop the parameters which were consumed by GTK+
564 for ( i
= 0 ; i
< argcGTK
; i
++ )
566 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
568 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
574 //else: gtk_init() didn't modify our parameters
577 for ( i
= 0 ; i
< argcGTK
; i
++ )
583 #else // !wxUSE_UNICODE
584 // gtk_init() shouldn't actually change argv itself (just its contents) so
585 // it's ok to pass pointer to it
586 init_result
= gtk_init_check ( & argc
, & argv
);
587 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
590 wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));
594 // we can not enter threads before gtk_init is done
597 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
604 wxSetDetectableAutoRepeat ( true );
607 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
613 void wxApp :: CleanUp ()
617 wxAppBase :: CleanUp ();
622 void wxApp :: OnAssertFailure ( const wxChar
* file
,
630 wxAppBase :: OnAssertFailure ( file
, line
, func
, cond
, msg
);
632 m_isInAssert
= false ;
635 #endif // __WXDEBUG__
637 void wxApp :: RemoveIdleTag ()
640 wxMutexLocker
lock ( gs_idleTagsMutex
);
644 g_source_remove ( wxTheApp
-> m_idleTag
);
645 wxTheApp
-> m_idleTag
= 0 ;