]>
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"
27 #include "wx/gdicmn.h"
28 #include "wx/memory.h"
30 #include "wx/settings.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.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"
49 #include "wx/thread.h"
58 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
59 // in neither poll.h nor sys/poll.h which results in link errors later
72 // we implement poll() ourselves using select() which is supposed exist in
74 #include <sys/types.h>
77 #endif // HAVE_POLL/!HAVE_POLL
79 #include "wx/unix/private.h"
80 #include "wx/gtk/win_gtk.h"
81 #include "wx/gtk/private.h"
85 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
90 #include "wx/html/forcelnk.h"
94 //-----------------------------------------------------------------------------
96 //-----------------------------------------------------------------------------
98 bool g_mainThreadLocked
= false ;
99 gint g_pendingTag
= 0 ;
101 static GtkWidget
* gs_RootWindow
= ( GtkWidget
*) NULL
;
103 //-----------------------------------------------------------------------------
105 //-----------------------------------------------------------------------------
107 void wxapp_install_idle_handler ();
110 static wxMutex gs_idleTagsMutex
;
113 //-----------------------------------------------------------------------------
115 //-----------------------------------------------------------------------------
117 // not static because used by textctrl.cpp
120 bool wxIsInsideYield
= false ;
122 bool wxApp :: Yield ( bool onlyIfNeeded
)
124 if ( wxIsInsideYield
)
128 wxFAIL_MSG ( wxT ( "wxYield called recursively" ) );
135 if ( ! wxThread :: IsMain () )
137 // can't call gtk_main_iteration() from other threads like this
140 #endif // wxUSE_THREADS
142 wxIsInsideYield
= true ;
144 // We need to remove idle callbacks or the loop will
146 wxTheApp
-> RemoveIdleTag ();
149 // disable log flushing from here because a call to wxYield() shouldn't
150 // normally result in message boxes popping up &c
154 while ( gtk_events_pending ())
155 gtk_main_iteration ();
157 // It's necessary to call ProcessIdle() to update the frames sizes which
158 // might have been changed (it also will update other things set from
159 // OnUpdateUI() which is a nice (and desired) side effect). But we
160 // call ProcessIdle() only once since this is not meant for longish
161 // background jobs (controlled by wxIdleEvent::RequestMore() and the
162 // return value of Processidle().
166 // let the logs be flashed again
170 wxIsInsideYield
= false ;
175 //-----------------------------------------------------------------------------
177 //-----------------------------------------------------------------------------
179 // RR/KH: No wxMutexGui calls are needed here according to the GTK faq,
180 // http://www.gtk.org/faq/#AEN500 - this caused problems for wxPostEvent.
182 void wxApp :: WakeUpIdle ()
184 wxapp_install_idle_handler ();
187 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------
191 // the callback functions must be extern "C" to comply with GTK+ declarations
195 static gint
wxapp_pending_callback ( gpointer
WXUNUSED ( data
) )
197 if (! wxTheApp
) return TRUE
;
199 // When getting called from GDK's time-out handler
200 // we are no longer within GDK's grab on the GUI
201 // thread so we must lock it here ourselves.
204 // Sent idle event to all who request them.
205 wxTheApp
-> ProcessPendingEvents ();
209 wxMutexLocker
lock ( gs_idleTagsMutex
);
214 // Flush the logged messages if any.
216 wxLog :: FlushActive ();
219 // Release lock again
222 // Return FALSE to indicate that no more idle events are
223 // to be sent (single shot instead of continuous stream)
227 static gint
wxapp_idle_callback ( gpointer
WXUNUSED ( data
) )
233 // don't generate the idle events while the assert modal dialog is shown,
234 // this completely confuses the apps which don't expect to be reentered
235 // from some safely-looking functions
236 if ( wxTheApp
-> IsInAssert () )
238 // But repaint the assertion message if necessary
239 if ( wxTopLevelWindows
. GetCount () > 0 )
241 wxWindow
* win
= ( wxWindow
*) wxTopLevelWindows
. GetLast ()-> GetData ();
242 if ( win
-> IsKindOf ( CLASSINFO ( wxMessageDialog
)))
243 win
-> OnInternalIdle ();
247 #endif // __WXDEBUG__
249 // When getting called from GDK's time-out handler
250 // we are no longer within GDK's grab on the GUI
251 // thread so we must lock it here ourselves.
254 // Indicate that we are now in idle mode and event handlers
255 // will have to reinstall the idle handler again.
258 wxMutexLocker
lock ( gs_idleTagsMutex
);
261 wxTheApp
-> m_idleTag
= 0 ;
266 // Send idle event to all who request them as long as
267 // no events have popped up in the event queue.
268 while ( ( moreIdles
= wxTheApp
-> ProcessIdle ()) && gtk_events_pending () == 0 )
271 // Release lock again
274 // Return FALSE if no more idle events are to be sent
282 #define wxPollFd pollfd
285 typedef GPollFD wxPollFd
;
287 int wxPoll ( wxPollFd
* ufds
, unsigned int nfds
, int timeout
)
289 // convert timeout from ms to struct timeval (s/us)
291 tv_timeout
. tv_sec
= timeout
/ 1000 ;
292 tv_timeout
. tv_usec
= ( timeout%1000
)* 1000 ;
294 // remember the highest fd used here
297 // and fill the sets for select()
302 wxFD_ZERO (& writefds
);
303 wxFD_ZERO (& exceptfds
);
306 for ( i
= 0 ; i
< nfds
; i
++ )
308 wxASSERT_MSG ( ufds
[ i
]. fd
< wxFD_SETSIZE
, _T ( "fd out of range" ) );
310 if ( ufds
[ i
]. events
& G_IO_IN
)
311 wxFD_SET ( ufds
[ i
]. fd
, & readfds
);
313 if ( ufds
[ i
]. events
& G_IO_PRI
)
314 wxFD_SET ( ufds
[ i
]. fd
, & exceptfds
);
316 if ( ufds
[ i
]. events
& G_IO_OUT
)
317 wxFD_SET ( ufds
[ i
]. fd
, & writefds
);
319 if ( ufds
[ i
]. fd
> fdMax
)
324 int res
= select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);
326 // translate the results back
327 for ( i
= 0 ; i
< nfds
; i
++ )
331 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & readfds
) )
332 ufds
[ i
]. revents
|= G_IO_IN
;
334 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & exceptfds
) )
335 ufds
[ i
]. revents
|= G_IO_PRI
;
337 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & writefds
) )
338 ufds
[ i
]. revents
|= G_IO_OUT
;
344 #endif // HAVE_POLL/!HAVE_POLL
346 static gint
wxapp_poll_func ( GPollFD
* ufds
, guint nfds
, gint timeout
)
351 g_mainThreadLocked
= true ;
353 // we rely on the fact that glib GPollFD struct is really just pollfd but
354 // I wonder how wise is this in the long term (VZ)
355 gint res
= wxPoll ( ( wxPollFd
*) ufds
, nfds
, timeout
);
358 g_mainThreadLocked
= false ;
365 #endif // wxUSE_THREADS
369 void wxapp_install_idle_handler ()
372 wxMutexLocker
lock ( gs_idleTagsMutex
);
375 // Don't install the handler if it's already installed. This test *MUST*
376 // be done when gs_idleTagsMutex is locked!
380 // GD: this assert is raised when using the thread sample (which works)
381 // so the test is probably not so easy. Can widget callbacks be
382 // triggered from child threads and, if so, for which widgets?
383 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
385 wxASSERT_MSG ( wxTheApp
-> m_idleTag
== 0 , wxT ( "attempt to install idle handler twice" ) );
389 if ( g_pendingTag
== 0 )
390 g_pendingTag
= g_idle_add_full ( 900 , wxapp_pending_callback
, NULL
, NULL
);
392 // This routine gets called by all event handlers
393 // indicating that the idle is over. It may also
394 // get called from other thread for sending events
395 // to the main thread (and processing these in
396 // idle time). Very low priority.
397 wxTheApp
-> m_idleTag
= g_idle_add_full ( 1000 , wxapp_idle_callback
, NULL
, NULL
);
400 //-----------------------------------------------------------------------------
401 // Access to the root window global
402 //-----------------------------------------------------------------------------
404 GtkWidget
* wxGetRootWindow ()
406 if ( gs_RootWindow
== NULL
)
408 gs_RootWindow
= gtk_window_new ( GTK_WINDOW_TOPLEVEL
);
409 gtk_widget_realize ( gs_RootWindow
);
411 return gs_RootWindow
;
414 //-----------------------------------------------------------------------------
416 //-----------------------------------------------------------------------------
418 IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)
420 BEGIN_EVENT_TABLE ( wxApp
, wxEvtHandler
)
421 EVT_IDLE ( wxAppBase :: OnIdle
)
427 m_isInAssert
= false ;
428 #endif // __WXDEBUG__
432 wxapp_install_idle_handler ();
435 g_main_context_set_poll_func ( NULL
, wxapp_poll_func
);
438 m_colorCube
= ( unsigned char *) NULL
;
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
);
454 bool wxApp :: OnInitGui ()
456 if ( ! wxAppBase :: OnInitGui () )
459 GdkVisual
* visual
= gdk_visual_get_system ();
461 // if this is a wxGLApp (derived from wxApp), and we've already
462 // chosen a specific visual, then derive the GdkVisual from that
463 if ( m_glVisualInfo
!= NULL
)
465 // seems gtk_widget_set_default_visual no longer exists?
466 GdkVisual
* vis
= gtk_widget_get_default_visual ();
468 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
469 gtk_widget_set_default_colormap ( colormap
);
474 // On some machines, the default visual is just 256 colours, so
475 // we make sure we get the best. This can sometimes be wasteful.
478 if (( gdk_visual_get_best () != gdk_visual_get_system ()) && ( m_useBestVisual
))
480 /* seems gtk_widget_set_default_visual no longer exists? */
481 GdkVisual
* vis
= gtk_widget_get_default_visual ();
483 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
484 gtk_widget_set_default_colormap ( colormap
);
489 // Nothing to do for 15, 16, 24, 32 bit displays
490 if ( visual
-> depth
> 8 ) return true ;
492 // initialize color cube for 8-bit color reduction dithering
494 GdkColormap
* cmap
= gtk_widget_get_default_colormap ();
496 m_colorCube
= ( unsigned char *) malloc ( 32 * 32 * 32 );
498 for ( int r
= 0 ; r
< 32 ; r
++)
500 for ( int g
= 0 ; g
< 32 ; g
++)
502 for ( int b
= 0 ; b
< 32 ; b
++)
504 int rr
= ( r
<< 3 ) | ( r
>> 2 );
505 int gg
= ( g
<< 3 ) | ( g
>> 2 );
506 int bb
= ( b
<< 3 ) | ( b
>> 2 );
510 GdkColor
* colors
= cmap
-> colors
;
515 for ( int i
= 0 ; i
< cmap
-> size
; i
++)
517 int rdiff
= (( rr
<< 8 ) - colors
[ i
]. red
);
518 int gdiff
= (( gg
<< 8 ) - colors
[ i
]. green
);
519 int bdiff
= (( bb
<< 8 ) - colors
[ i
]. blue
);
520 int sum
= ABS ( rdiff
) + ABS ( gdiff
) + ABS ( bdiff
);
523 index
= i
; max
= sum
;
529 // assume 8-bit true or static colors. this really exists
530 GdkVisual
* vis
= gdk_colormap_get_visual ( cmap
);
531 index
= ( r
>> ( 5 - vis
-> red_prec
)) << vis
-> red_shift
;
532 index
|= ( g
>> ( 5 - vis
-> green_prec
)) << vis
-> green_shift
;
533 index
|= ( b
>> ( 5 - vis
-> blue_prec
)) << vis
-> blue_shift
;
535 m_colorCube
[ ( r
* 1024 ) + ( g
* 32 ) + b
] = index
;
543 GdkVisual
* wxApp :: GetGdkVisual ()
545 GdkVisual
* visual
= NULL
;
548 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
550 visual
= gdk_drawable_get_visual ( wxGetRootWindow ()-> window
);
557 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
562 if (! g_thread_supported ())
564 #endif // wxUSE_THREADS
568 // We should have the wxUSE_WCHAR_T test on the _outside_
570 // gtk+ 2.0 supports Unicode through UTF-8 strings
571 wxConvCurrent
= & wxConvUTF8
;
572 #else // !wxUSE_WCHAR_T
574 wxConvCurrent
= ( wxMBConv
*) NULL
;
575 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
577 // decide which conversion to use for the file names
579 // (1) this variable exists for the sole purpose of specifying the encoding
580 // of the filenames for GTK+ programs, so use it if it is set
581 wxString
encName ( wxGetenv ( _T ( "G_FILENAME_ENCODING" )));
582 encName
= encName
. BeforeFirst ( _T ( ',' ));
583 if ( encName
== _T ( "@locale" ))
589 // (2) if a non default locale is set, assume that the user wants his
590 // filenames in this locale too
591 encName
= wxLocale :: GetSystemEncodingName (). Upper ();
592 // (3) finally use UTF-8 by default
593 if ( encName
. empty () || encName
== _T ( "US-ASCII" ))
594 encName
= _T ( "UTF-8" );
595 wxSetEnv ( _T ( "G_FILENAME_ENCODING" ), encName
);
599 encName
= _T ( "UTF-8" );
601 static wxConvBrokenFileNames
fileconv ( encName
);
602 wxConvFileName
= & fileconv
;
605 // gtk_init() wants UTF-8, not wchar_t, so convert
607 char ** argvGTK
= new char *[ argc
+ 1 ];
608 for ( i
= 0 ; i
< argc
; i
++ )
610 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
613 argvGTK
[ argc
] = NULL
;
618 init_result
= true ; // is there a _check() version of this?
619 gpe_application_init ( & argcGTK
, & argvGTK
);
621 init_result
= gtk_init_check ( & argcGTK
, & argvGTK
);
624 if ( argcGTK
!= argc
)
626 // we have to drop the parameters which were consumed by GTK+
627 for ( i
= 0 ; i
< argcGTK
; i
++ )
629 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
631 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
637 //else: gtk_init() didn't modify our parameters
640 for ( i
= 0 ; i
< argcGTK
; i
++ )
646 #else // !wxUSE_UNICODE
647 // gtk_init() shouldn't actually change argv itself (just its contents) so
648 // it's ok to pass pointer to it
649 init_result
= gtk_init_check ( & argc
, & argv
);
650 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
653 wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));
657 // we can not enter threads before gtk_init is done
660 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
667 wxSetDetectableAutoRepeat ( true );
670 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
676 void wxApp :: CleanUp ()
680 wxAppBase :: CleanUp ();
685 void wxApp :: OnAssert ( const wxChar
* file
, int line
, const wxChar
* cond
, const wxChar
* msg
)
689 wxAppBase :: OnAssert ( file
, line
, cond
, msg
);
691 m_isInAssert
= false ;
694 #endif // __WXDEBUG__
696 void wxApp :: RemoveIdleTag ()
699 wxMutexLocker
lock ( gs_idleTagsMutex
);
703 g_source_remove ( wxTheApp
-> m_idleTag
);
704 wxTheApp
-> m_idleTag
= 0 ;