]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/app.cpp
1 /////////////////////////////////////////////////////////////////////////////
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"
20 #include "wx/gdicmn.h"
24 #include "wx/memory.h"
26 #include "wx/settings.h"
27 #include "wx/dialog.h"
28 #include "wx/msgdlg.h"
30 #include "wx/filename.h"
31 #include "wx/module.h"
33 #include "wx/thread.h"
39 #ifdef __WXUNIVERSAL__
40 #include "wx/univ/theme.h"
41 #include "wx/univ/renderer.h"
45 #include "wx/thread.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"
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
84 bool g_mainThreadLocked
= FALSE
;
85 gint g_pendingTag
= 0 ;
87 static GtkWidget
* gs_RootWindow
= ( GtkWidget
*) NULL
;
89 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
95 void wxapp_install_idle_handler ();
98 static wxMutex gs_idleTagsMutex
;
101 //-----------------------------------------------------------------------------
103 //-----------------------------------------------------------------------------
105 // not static because used by textctrl.cpp
108 bool wxIsInsideYield
= FALSE
;
110 bool wxApp :: Yield ( bool onlyIfNeeded
)
112 if ( wxIsInsideYield
)
116 wxFAIL_MSG ( wxT ( "wxYield called recursively" ) );
123 if ( ! wxThread :: IsMain () )
125 // can't call gtk_main_iteration() from other threads like this
128 #endif // wxUSE_THREADS
130 wxIsInsideYield
= TRUE
;
132 // We need to remove idle callbacks or the loop will
134 wxTheApp
-> RemoveIdleTag ();
137 // disable log flushing from here because a call to wxYield() shouldn't
138 // normally result in message boxes popping up &c
142 while ( gtk_events_pending ())
143 gtk_main_iteration ();
145 // It's necessary to call ProcessIdle() to update the frames sizes which
146 // might have been changed (it also will update other things set from
147 // OnUpdateUI() which is a nice (and desired) side effect). But we
148 // call ProcessIdle() only once since this is not meant for longish
149 // background jobs (controlled by wxIdleEvent::RequestMore() and the
150 // return value of Processidle().
154 // let the logs be flashed again
158 wxIsInsideYield
= FALSE
;
163 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
167 // RR/KH: No wxMutexGui calls are needed here according to the GTK faq,
168 // http://www.gtk.org/faq/#AEN500 - this caused problems for wxPostEvent.
170 void wxApp :: WakeUpIdle ()
172 wxapp_install_idle_handler ();
175 //-----------------------------------------------------------------------------
177 //-----------------------------------------------------------------------------
179 // the callback functions must be extern "C" to comply with GTK+ declarations
183 static gint
wxapp_pending_callback ( gpointer
WXUNUSED ( data
) )
185 if (! wxTheApp
) return TRUE
;
187 // When getting called from GDK's time-out handler
188 // we are no longer within GDK's grab on the GUI
189 // thread so we must lock it here ourselves.
192 // Sent idle event to all who request them.
193 wxTheApp
-> ProcessPendingEvents ();
197 wxMutexLocker
lock ( gs_idleTagsMutex
);
202 // Flush the logged messages if any.
204 wxLog :: FlushActive ();
207 // Release lock again
210 // Return FALSE to indicate that no more idle events are
211 // to be sent (single shot instead of continuous stream)
215 static gint
wxapp_idle_callback ( gpointer
WXUNUSED ( data
) )
221 // don't generate the idle events while the assert modal dialog is shown,
222 // this completely confuses the apps which don't expect to be reentered
223 // from some safely-looking functions
224 if ( wxTheApp
-> IsInAssert () )
226 // But repaint the assertion message if necessary
227 if ( wxTopLevelWindows
. GetCount () > 0 )
229 wxWindow
* win
= ( wxWindow
*) wxTopLevelWindows
. GetLast ()-> GetData ();
230 if ( win
-> IsKindOf ( CLASSINFO ( wxMessageDialog
)))
231 win
-> OnInternalIdle ();
235 #endif // __WXDEBUG__
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 // Indicate that we are now in idle mode and event handlers
243 // will have to reinstall the idle handler again.
246 wxMutexLocker
lock ( gs_idleTagsMutex
);
249 wxTheApp
-> m_idleTag
= 0 ;
252 // Send idle event to all who request them as long as
253 // no events have popped up in the event queue.
254 while ( wxTheApp
-> ProcessIdle () && ( gtk_events_pending () == 0 ))
257 // Release lock again
260 // Return FALSE to indicate that no more idle events are
261 // to be sent (single shot instead of continuous stream).
269 #define wxPollFd pollfd
272 typedef GPollFD wxPollFd
;
274 int wxPoll ( wxPollFd
* ufds
, unsigned int nfds
, int timeout
)
276 // convert timeout from ms to struct timeval (s/us)
278 tv_timeout
. tv_sec
= timeout
/ 1000 ;
279 tv_timeout
. tv_usec
= ( timeout%1000
)* 1000 ;
281 // remember the highest fd used here
284 // and fill the sets for select()
289 wxFD_ZERO (& writefds
);
290 wxFD_ZERO (& exceptfds
);
293 for ( i
= 0 ; i
< nfds
; i
++ )
295 wxASSERT_MSG ( ufds
[ i
]. fd
< wxFD_SETSIZE
, _T ( "fd out of range" ) );
297 if ( ufds
[ i
]. events
& G_IO_IN
)
298 wxFD_SET ( ufds
[ i
]. fd
, & readfds
);
300 if ( ufds
[ i
]. events
& G_IO_PRI
)
301 wxFD_SET ( ufds
[ i
]. fd
, & exceptfds
);
303 if ( ufds
[ i
]. events
& G_IO_OUT
)
304 wxFD_SET ( ufds
[ i
]. fd
, & writefds
);
306 if ( ufds
[ i
]. fd
> fdMax
)
311 int res
= select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);
313 // translate the results back
314 for ( i
= 0 ; i
< nfds
; i
++ )
318 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & readfds
) )
319 ufds
[ i
]. revents
|= G_IO_IN
;
321 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & exceptfds
) )
322 ufds
[ i
]. revents
|= G_IO_PRI
;
324 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & writefds
) )
325 ufds
[ i
]. revents
|= G_IO_OUT
;
331 #endif // HAVE_POLL/!HAVE_POLL
333 static gint
wxapp_poll_func ( GPollFD
* ufds
, guint nfds
, gint timeout
)
338 g_mainThreadLocked
= TRUE
;
340 // we rely on the fact that glib GPollFD struct is really just pollfd but
341 // I wonder how wise is this in the long term (VZ)
342 gint res
= wxPoll ( ( wxPollFd
*) ufds
, nfds
, timeout
);
345 g_mainThreadLocked
= FALSE
;
352 #endif // wxUSE_THREADS
356 void wxapp_install_idle_handler ()
359 wxMutexLocker
lock ( gs_idleTagsMutex
);
362 // Don't install the handler if it's already installed. This test *MUST*
363 // be done when gs_idleTagsMutex is locked!
367 // GD: this assert is raised when using the thread sample (which works)
368 // so the test is probably not so easy. Can widget callbacks be
369 // triggered from child threads and, if so, for which widgets?
370 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
372 wxASSERT_MSG ( wxTheApp
-> m_idleTag
== 0 , wxT ( "attempt to install idle handler twice" ) );
376 if ( g_pendingTag
== 0 )
377 g_pendingTag
= gtk_idle_add_priority ( 900 , wxapp_pending_callback
, ( gpointer
) NULL
);
379 // This routine gets called by all event handlers
380 // indicating that the idle is over. It may also
381 // get called from other thread for sending events
382 // to the main thread (and processing these in
383 // idle time). Very low priority.
384 wxTheApp
-> m_idleTag
= gtk_idle_add_priority ( 1000 , wxapp_idle_callback
, ( gpointer
) NULL
);
387 //-----------------------------------------------------------------------------
388 // Access to the root window global
389 //-----------------------------------------------------------------------------
391 GtkWidget
* wxGetRootWindow ()
393 if ( gs_RootWindow
== NULL
)
395 gs_RootWindow
= gtk_window_new ( GTK_WINDOW_TOPLEVEL
);
396 gtk_widget_realize ( gs_RootWindow
);
398 return gs_RootWindow
;
401 //-----------------------------------------------------------------------------
403 //-----------------------------------------------------------------------------
405 IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)
407 BEGIN_EVENT_TABLE ( wxApp
, wxEvtHandler
)
408 EVT_IDLE ( wxAppBase :: OnIdle
)
414 m_isInAssert
= FALSE
;
415 #endif // __WXDEBUG__
419 wxapp_install_idle_handler ();
422 g_main_context_set_poll_func ( NULL
, wxapp_poll_func
);
425 m_colorCube
= ( unsigned char *) NULL
;
427 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
428 m_glVisualInfo
= ( void *) NULL
;
429 m_glFBCInfo
= ( void *) NULL
;
434 if ( m_idleTag
) gtk_idle_remove ( m_idleTag
);
436 if ( m_colorCube
) free ( m_colorCube
);
439 bool wxApp :: OnInitGui ()
441 if ( ! wxAppBase :: OnInitGui () )
444 GdkVisual
* visual
= gdk_visual_get_system ();
446 // if this is a wxGLApp (derived from wxApp), and we've already
447 // chosen a specific visual, then derive the GdkVisual from that
448 if ( m_glVisualInfo
!= NULL
)
450 // seems gtk_widget_set_default_visual no longer exists?
451 GdkVisual
* vis
= gtk_widget_get_default_visual ();
453 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
454 gtk_widget_set_default_colormap ( colormap
);
459 // On some machines, the default visual is just 256 colours, so
460 // we make sure we get the best. This can sometimes be wasteful.
463 if (( gdk_visual_get_best () != gdk_visual_get_system ()) && ( m_useBestVisual
))
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 // Nothing to do for 15, 16, 24, 32 bit displays
475 if ( visual
-> depth
> 8 ) return TRUE
;
477 // initialize color cube for 8-bit color reduction dithering
479 GdkColormap
* cmap
= gtk_widget_get_default_colormap ();
481 m_colorCube
= ( unsigned char *) malloc ( 32 * 32 * 32 );
483 for ( int r
= 0 ; r
< 32 ; r
++)
485 for ( int g
= 0 ; g
< 32 ; g
++)
487 for ( int b
= 0 ; b
< 32 ; b
++)
489 int rr
= ( r
<< 3 ) | ( r
>> 2 );
490 int gg
= ( g
<< 3 ) | ( g
>> 2 );
491 int bb
= ( b
<< 3 ) | ( b
>> 2 );
495 GdkColor
* colors
= cmap
-> colors
;
500 for ( int i
= 0 ; i
< cmap
-> size
; i
++)
502 int rdiff
= (( rr
<< 8 ) - colors
[ i
]. red
);
503 int gdiff
= (( gg
<< 8 ) - colors
[ i
]. green
);
504 int bdiff
= (( bb
<< 8 ) - colors
[ i
]. blue
);
505 int sum
= ABS ( rdiff
) + ABS ( gdiff
) + ABS ( bdiff
);
508 index
= i
; max
= sum
;
514 // assume 8-bit true or static colors. this really exists
515 GdkVisual
* vis
= gdk_colormap_get_visual ( cmap
);
516 index
= ( r
>> ( 5 - vis
-> red_prec
)) << vis
-> red_shift
;
517 index
|= ( g
>> ( 5 - vis
-> green_prec
)) << vis
-> green_shift
;
518 index
|= ( b
>> ( 5 - vis
-> blue_prec
)) << vis
-> blue_shift
;
520 m_colorCube
[ ( r
* 1024 ) + ( g
* 32 ) + b
] = index
;
528 GdkVisual
* wxApp :: GetGdkVisual ()
530 GdkVisual
* visual
= NULL
;
533 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
535 visual
= gdk_window_get_visual ( wxGetRootWindow ()-> window
);
542 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
547 // GTK 1.2 up to version 1.2.3 has broken threads
548 if (( gtk_major_version
== 1 ) &&
549 ( gtk_minor_version
== 2 ) &&
550 ( gtk_micro_version
< 4 ))
552 printf ( "wxWidgets warning: GUI threading disabled due to outdated GTK version \n " );
556 if (! g_thread_supported ())
559 #endif // wxUSE_THREADS
563 // We should have the wxUSE_WCHAR_T test on the _outside_
565 // gtk+ 2.0 supports Unicode through UTF-8 strings
566 wxConvCurrent
= & wxConvUTF8
;
567 #else // !wxUSE_WCHAR_T
569 wxConvCurrent
= ( wxMBConv
*) NULL
;
570 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
572 // decide which conversion to use for the file names
574 // (1) this variable exists for the sole purpose of specifying the encoding
575 // of the filenames for GTK+ programs, so use it if it is set
576 wxString
encName ( wxGetenv ( _T ( "G_FILENAME_ENCODING" )));
577 encName
= encName
. BeforeFirst ( _T ( ',' ));
578 if ( encName
== _T ( "@locale" ))
584 // (2) if a non default locale is set, assume that the user wants his
585 // filenames in this locale too
586 encName
= wxLocale :: GetSystemEncodingName (). Upper ();
587 // (3) finally use UTF-8 by default
588 if ( encName
. empty () || encName
== _T ( "US-ASCII" ))
589 encName
= _T ( "UTF-8" );
590 wxSetEnv ( _T ( "G_FILENAME_ENCODING" ), encName
);
594 encName
= _T ( "UTF-8" );
596 static wxConvBrokenFileNames
fileconv ( encName
);
597 wxConvFileName
= & fileconv
;
600 // gtk_init() wants UTF-8, not wchar_t, so convert
602 char ** argvGTK
= new char *[ argc
+ 1 ];
603 for ( i
= 0 ; i
< argc
; i
++ )
605 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
608 argvGTK
[ argc
] = NULL
;
613 init_result
= true ; // is there a _check() version of this?
614 gpe_application_init ( & argcGTK
, & argvGTK
);
616 init_result
= gtk_init_check ( & argcGTK
, & argvGTK
);
619 if ( argcGTK
!= argc
)
621 // we have to drop the parameters which were consumed by GTK+
622 for ( i
= 0 ; i
< argcGTK
; i
++ )
624 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
626 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
632 //else: gtk_init() didn't modify our parameters
635 for ( i
= 0 ; i
< argcGTK
; i
++ )
641 #else // !wxUSE_UNICODE
642 // gtk_init() shouldn't actually change argv itself (just its contents) so
643 // it's ok to pass pointer to it
644 init_result
= gtk_init_check ( & argc
, & argv
);
645 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
648 wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));
652 // we can not enter threads before gtk_init is done
655 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
662 wxSetDetectableAutoRepeat ( TRUE
);
665 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
671 void wxApp :: CleanUp ()
675 wxAppBase :: CleanUp ();
680 void wxApp :: OnAssert ( const wxChar
* file
, int line
, const wxChar
* cond
, const wxChar
* msg
)
684 wxAppBase :: OnAssert ( file
, line
, cond
, msg
);
686 m_isInAssert
= FALSE
;
689 #endif // __WXDEBUG__
691 void wxApp :: RemoveIdleTag ()
694 wxMutexLocker
lock ( gs_idleTagsMutex
);
698 gtk_idle_remove ( wxTheApp
-> m_idleTag
);
699 wxTheApp
-> m_idleTag
= 0 ;