]>
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"
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 m_colorCube
= ( unsigned char *) NULL
;
436 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
437 m_glVisualInfo
= ( void *) NULL
;
438 m_glFBCInfo
= ( void *) NULL
;
444 g_source_remove ( m_idleTag
);
450 bool wxApp :: OnInitGui ()
452 if ( ! wxAppBase :: OnInitGui () )
455 GdkVisual
* visual
= gdk_visual_get_system ();
457 // if this is a wxGLApp (derived from wxApp), and we've already
458 // chosen a specific visual, then derive the GdkVisual from that
459 if ( m_glVisualInfo
!= NULL
)
461 // seems gtk_widget_set_default_visual no longer exists?
462 GdkVisual
* vis
= gtk_widget_get_default_visual ();
464 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
465 gtk_widget_set_default_colormap ( colormap
);
470 // On some machines, the default visual is just 256 colours, so
471 // we make sure we get the best. This can sometimes be wasteful.
474 if (( gdk_visual_get_best () != gdk_visual_get_system ()) && ( m_useBestVisual
))
476 /* seems gtk_widget_set_default_visual no longer exists? */
477 GdkVisual
* vis
= gtk_widget_get_default_visual ();
479 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
480 gtk_widget_set_default_colormap ( colormap
);
485 // Nothing to do for 15, 16, 24, 32 bit displays
486 if ( visual
-> depth
> 8 ) return TRUE
;
488 // initialize color cube for 8-bit color reduction dithering
490 GdkColormap
* cmap
= gtk_widget_get_default_colormap ();
492 m_colorCube
= ( unsigned char *) malloc ( 32 * 32 * 32 );
494 for ( int r
= 0 ; r
< 32 ; r
++)
496 for ( int g
= 0 ; g
< 32 ; g
++)
498 for ( int b
= 0 ; b
< 32 ; b
++)
500 int rr
= ( r
<< 3 ) | ( r
>> 2 );
501 int gg
= ( g
<< 3 ) | ( g
>> 2 );
502 int bb
= ( b
<< 3 ) | ( b
>> 2 );
506 GdkColor
* colors
= cmap
-> colors
;
511 for ( int i
= 0 ; i
< cmap
-> size
; i
++)
513 int rdiff
= (( rr
<< 8 ) - colors
[ i
]. red
);
514 int gdiff
= (( gg
<< 8 ) - colors
[ i
]. green
);
515 int bdiff
= (( bb
<< 8 ) - colors
[ i
]. blue
);
516 int sum
= ABS ( rdiff
) + ABS ( gdiff
) + ABS ( bdiff
);
519 index
= i
; max
= sum
;
525 // assume 8-bit true or static colors. this really exists
526 GdkVisual
* vis
= gdk_colormap_get_visual ( cmap
);
527 index
= ( r
>> ( 5 - vis
-> red_prec
)) << vis
-> red_shift
;
528 index
|= ( g
>> ( 5 - vis
-> green_prec
)) << vis
-> green_shift
;
529 index
|= ( b
>> ( 5 - vis
-> blue_prec
)) << vis
-> blue_shift
;
531 m_colorCube
[ ( r
* 1024 ) + ( g
* 32 ) + b
] = index
;
539 GdkVisual
* wxApp :: GetGdkVisual ()
541 GdkVisual
* visual
= NULL
;
544 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
546 visual
= gdk_window_get_visual ( wxGetRootWindow ()-> window
);
553 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
558 // GTK 1.2 up to version 1.2.3 has broken threads
559 if (( gtk_major_version
== 1 ) &&
560 ( gtk_minor_version
== 2 ) &&
561 ( gtk_micro_version
< 4 ))
563 printf ( "wxWidgets warning: GUI threading disabled due to outdated GTK version \n " );
567 if (! g_thread_supported ())
570 #endif // wxUSE_THREADS
574 // We should have the wxUSE_WCHAR_T test on the _outside_
576 // gtk+ 2.0 supports Unicode through UTF-8 strings
577 wxConvCurrent
= & wxConvUTF8
;
578 #else // !wxUSE_WCHAR_T
580 wxConvCurrent
= ( wxMBConv
*) NULL
;
581 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
583 // decide which conversion to use for the file names
585 // (1) this variable exists for the sole purpose of specifying the encoding
586 // of the filenames for GTK+ programs, so use it if it is set
587 wxString
encName ( wxGetenv ( _T ( "G_FILENAME_ENCODING" )));
588 encName
= encName
. BeforeFirst ( _T ( ',' ));
589 if ( encName
== _T ( "@locale" ))
595 // (2) if a non default locale is set, assume that the user wants his
596 // filenames in this locale too
597 encName
= wxLocale :: GetSystemEncodingName (). Upper ();
598 // (3) finally use UTF-8 by default
599 if ( encName
. empty () || encName
== _T ( "US-ASCII" ))
600 encName
= _T ( "UTF-8" );
601 wxSetEnv ( _T ( "G_FILENAME_ENCODING" ), encName
);
605 encName
= _T ( "UTF-8" );
607 static wxConvBrokenFileNames
fileconv ( encName
);
608 wxConvFileName
= & fileconv
;
611 // gtk_init() wants UTF-8, not wchar_t, so convert
613 char ** argvGTK
= new char *[ argc
+ 1 ];
614 for ( i
= 0 ; i
< argc
; i
++ )
616 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
619 argvGTK
[ argc
] = NULL
;
624 init_result
= true ; // is there a _check() version of this?
625 gpe_application_init ( & argcGTK
, & argvGTK
);
627 init_result
= gtk_init_check ( & argcGTK
, & argvGTK
);
630 if ( argcGTK
!= argc
)
632 // we have to drop the parameters which were consumed by GTK+
633 for ( i
= 0 ; i
< argcGTK
; i
++ )
635 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
637 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
643 //else: gtk_init() didn't modify our parameters
646 for ( i
= 0 ; i
< argcGTK
; i
++ )
652 #else // !wxUSE_UNICODE
653 // gtk_init() shouldn't actually change argv itself (just its contents) so
654 // it's ok to pass pointer to it
655 init_result
= gtk_init_check ( & argc
, & argv
);
656 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
659 wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));
663 // we can not enter threads before gtk_init is done
666 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
673 wxSetDetectableAutoRepeat ( TRUE
);
676 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
682 void wxApp :: CleanUp ()
686 wxAppBase :: CleanUp ();
691 void wxApp :: OnAssert ( const wxChar
* file
, int line
, const wxChar
* cond
, const wxChar
* msg
)
695 wxAppBase :: OnAssert ( file
, line
, cond
, msg
);
697 m_isInAssert
= FALSE
;
700 #endif // __WXDEBUG__
702 void wxApp :: RemoveIdleTag ()
705 wxMutexLocker
lock ( gs_idleTagsMutex
);
709 g_source_remove ( wxTheApp
-> m_idleTag
);
710 wxTheApp
-> m_idleTag
= 0 ;