]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/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/gtk/win_gtk.h"
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
83 bool g_mainThreadLocked
= FALSE
;
84 gint g_pendingTag
= 0 ;
86 static GtkWidget
* gs_RootWindow
= ( GtkWidget
*) NULL
;
88 //-----------------------------------------------------------------------------
90 //-----------------------------------------------------------------------------
94 void wxapp_install_idle_handler ();
97 static wxMutex gs_idleTagsMutex
;
100 //-----------------------------------------------------------------------------
102 //-----------------------------------------------------------------------------
104 // not static because used by textctrl.cpp
107 bool wxIsInsideYield
= FALSE
;
109 bool wxApp :: Yield ( bool onlyIfNeeded
)
111 if ( wxIsInsideYield
)
115 wxFAIL_MSG ( wxT ( "wxYield called recursively" ) );
122 if ( ! wxThread :: IsMain () )
124 // can't call gtk_main_iteration() from other threads like this
127 #endif // wxUSE_THREADS
129 wxIsInsideYield
= TRUE
;
131 // We need to remove idle callbacks or the loop will
133 wxTheApp
-> RemoveIdleTag ();
136 // disable log flushing from here because a call to wxYield() shouldn't
137 // normally result in message boxes popping up &c
141 while ( gtk_events_pending ())
142 gtk_main_iteration ();
144 // It's necessary to call ProcessIdle() to update the frames sizes which
145 // might have been changed (it also will update other things set from
146 // OnUpdateUI() which is a nice (and desired) side effect). But we
147 // call ProcessIdle() only once since this is not meant for longish
148 // background jobs (controlled by wxIdleEvent::RequestMore() and the
149 // return value of Processidle().
153 // let the logs be flashed again
157 wxIsInsideYield
= FALSE
;
162 //-----------------------------------------------------------------------------
164 //-----------------------------------------------------------------------------
166 // RR/KH: The wxMutexGui calls are not needed on GTK2 according to
167 // the GTK faq, http://www.gtk.org/faq/#AEN500
168 // The calls to gdk_threads_enter() and leave() are specifically noted
169 // as not being necessary. The MutexGui calls are still left in for GTK1.
170 // Eliminating the MutexGui calls fixes the long-standing "random" lockup
171 // when using wxPostEvent (which calls WakeUpIdle) from a thread.
173 void wxApp :: WakeUpIdle ()
177 if (! wxThread :: IsMain ())
179 #endif // wxUSE_THREADS_
182 wxapp_install_idle_handler ();
186 if (! wxThread :: IsMain ())
188 #endif // wxUSE_THREADS_
192 //-----------------------------------------------------------------------------
194 //-----------------------------------------------------------------------------
196 // the callback functions must be extern "C" to comply with GTK+ declarations
200 static gint
wxapp_pending_callback ( gpointer
WXUNUSED ( data
) )
202 if (! wxTheApp
) return TRUE
;
204 // When getting called from GDK's time-out handler
205 // we are no longer within GDK's grab on the GUI
206 // thread so we must lock it here ourselves.
209 // Sent idle event to all who request them.
210 wxTheApp
-> ProcessPendingEvents ();
214 wxMutexLocker
lock ( gs_idleTagsMutex
);
219 // Flush the logged messages if any.
221 wxLog :: FlushActive ();
224 // Release lock again
227 // Return FALSE to indicate that no more idle events are
228 // to be sent (single shot instead of continuous stream)
232 static gint
wxapp_idle_callback ( gpointer
WXUNUSED ( data
) )
238 // don't generate the idle events while the assert modal dialog is shown,
239 // this completely confuses the apps which don't expect to be reentered
240 // from some safely-looking functions
241 if ( wxTheApp
-> IsInAssert () )
243 // But repaint the assertion message if necessary
244 if ( wxTopLevelWindows
. GetCount () > 0 )
246 wxWindow
* win
= ( wxWindow
*) wxTopLevelWindows
. GetLast ()-> GetData ();
248 if ( win
-> IsKindOf ( CLASSINFO ( wxMessageDialog
)))
250 if ( win
-> IsKindOf ( CLASSINFO ( wxGenericMessageDialog
)))
252 win
-> OnInternalIdle ();
256 #endif // __WXDEBUG__
258 // When getting called from GDK's time-out handler
259 // we are no longer within GDK's grab on the GUI
260 // thread so we must lock it here ourselves.
263 // Indicate that we are now in idle mode and event handlers
264 // will have to reinstall the idle handler again.
267 wxMutexLocker
lock ( gs_idleTagsMutex
);
270 wxTheApp
-> m_idleTag
= 0 ;
273 // Send idle event to all who request them as long as
274 // no events have popped up in the event queue.
275 while ( wxTheApp
-> ProcessIdle () && ( gtk_events_pending () == 0 ))
278 // Release lock again
281 // Return FALSE to indicate that no more idle events are
282 // to be sent (single shot instead of continuous stream).
290 #define wxPollFd pollfd
293 typedef GPollFD wxPollFd
;
295 int wxPoll ( wxPollFd
* ufds
, unsigned int nfds
, int timeout
)
297 // convert timeout from ms to struct timeval (s/us)
299 tv_timeout
. tv_sec
= timeout
/ 1000 ;
300 tv_timeout
. tv_usec
= ( timeout%1000
)* 1000 ;
302 // remember the highest fd used here
305 // and fill the sets for select()
314 for ( i
= 0 ; i
< nfds
; i
++ )
316 wxASSERT_MSG ( ufds
[ i
]. fd
< FD_SETSIZE
, _T ( "fd out of range" ) );
318 if ( ufds
[ i
]. events
& G_IO_IN
)
319 FD_SET ( ufds
[ i
]. fd
, & readfds
);
321 if ( ufds
[ i
]. events
& G_IO_PRI
)
322 FD_SET ( ufds
[ i
]. fd
, & exceptfds
);
324 if ( ufds
[ i
]. events
& G_IO_OUT
)
325 FD_SET ( ufds
[ i
]. fd
, & writefds
);
327 if ( ufds
[ i
]. fd
> fdMax
)
332 int res
= select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);
334 // translate the results back
335 for ( i
= 0 ; i
< nfds
; i
++ )
339 if ( FD_ISSET ( ufds
[ i
]. fd
, & readfds
) )
340 ufds
[ i
]. revents
|= G_IO_IN
;
342 if ( FD_ISSET ( ufds
[ i
]. fd
, & exceptfds
) )
343 ufds
[ i
]. revents
|= G_IO_PRI
;
345 if ( FD_ISSET ( ufds
[ i
]. fd
, & writefds
) )
346 ufds
[ i
]. revents
|= G_IO_OUT
;
352 #endif // HAVE_POLL/!HAVE_POLL
354 static gint
wxapp_poll_func ( GPollFD
* ufds
, guint nfds
, gint timeout
)
359 g_mainThreadLocked
= TRUE
;
361 // we rely on the fact that glib GPollFD struct is really just pollfd but
362 // I wonder how wise is this in the long term (VZ)
363 gint res
= wxPoll ( ( wxPollFd
*) ufds
, nfds
, timeout
);
366 g_mainThreadLocked
= FALSE
;
373 #endif // wxUSE_THREADS
377 void wxapp_install_idle_handler ()
380 wxMutexLocker
lock ( gs_idleTagsMutex
);
383 // Don't install the handler if it's already installed. This test *MUST*
384 // be done when gs_idleTagsMutex is locked!
388 // GD: this assert is raised when using the thread sample (which works)
389 // so the test is probably not so easy. Can widget callbacks be
390 // triggered from child threads and, if so, for which widgets?
391 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
393 wxASSERT_MSG ( wxTheApp
-> m_idleTag
== 0 , wxT ( "attempt to install idle handler twice" ) );
397 if ( g_pendingTag
== 0 )
398 g_pendingTag
= gtk_idle_add_priority ( 900 , wxapp_pending_callback
, ( gpointer
) NULL
);
400 // This routine gets called by all event handlers
401 // indicating that the idle is over. It may also
402 // get called from other thread for sending events
403 // to the main thread (and processing these in
404 // idle time). Very low priority.
405 wxTheApp
-> m_idleTag
= gtk_idle_add_priority ( 1000 , wxapp_idle_callback
, ( gpointer
) NULL
);
408 //-----------------------------------------------------------------------------
409 // Access to the root window global
410 //-----------------------------------------------------------------------------
412 GtkWidget
* wxGetRootWindow ()
414 if ( gs_RootWindow
== NULL
)
416 gs_RootWindow
= gtk_window_new ( GTK_WINDOW_TOPLEVEL
);
417 gtk_widget_realize ( gs_RootWindow
);
419 return gs_RootWindow
;
422 //-----------------------------------------------------------------------------
424 //-----------------------------------------------------------------------------
426 IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)
428 BEGIN_EVENT_TABLE ( wxApp
, wxEvtHandler
)
429 EVT_IDLE ( wxAppBase :: OnIdle
)
435 m_isInAssert
= FALSE
;
436 #endif // __WXDEBUG__
440 wxapp_install_idle_handler ();
443 g_main_set_poll_func ( wxapp_poll_func
);
446 m_colorCube
= ( unsigned char *) NULL
;
448 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
449 m_glVisualInfo
= ( void *) NULL
;
450 m_glFBCInfo
= ( void *) NULL
;
455 if ( m_idleTag
) gtk_idle_remove ( m_idleTag
);
457 if ( m_colorCube
) free ( m_colorCube
);
460 bool wxApp :: OnInitGui ()
462 if ( ! wxAppBase :: OnInitGui () )
465 GdkVisual
* visual
= gdk_visual_get_system ();
467 // if this is a wxGLApp (derived from wxApp), and we've already
468 // chosen a specific visual, then derive the GdkVisual from that
469 if ( m_glVisualInfo
!= NULL
)
472 // seems gtk_widget_set_default_visual no longer exists?
473 GdkVisual
* vis
= gtk_widget_get_default_visual ();
475 GdkVisual
* vis
= gdkx_visual_get (
476 (( XVisualInfo
*) m_glVisualInfo
) -> visualid
);
477 gtk_widget_set_default_visual ( vis
);
480 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
481 gtk_widget_set_default_colormap ( colormap
);
486 // On some machines, the default visual is just 256 colours, so
487 // we make sure we get the best. This can sometimes be wasteful.
490 if (( gdk_visual_get_best () != gdk_visual_get_system ()) && ( m_useBestVisual
))
493 /* seems gtk_widget_set_default_visual no longer exists? */
494 GdkVisual
* vis
= gtk_widget_get_default_visual ();
496 GdkVisual
* vis
= gdk_visual_get_best ();
497 gtk_widget_set_default_visual ( vis
);
500 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
501 gtk_widget_set_default_colormap ( colormap
);
506 // Nothing to do for 15, 16, 24, 32 bit displays
507 if ( visual
-> depth
> 8 ) return TRUE
;
509 // initialize color cube for 8-bit color reduction dithering
511 GdkColormap
* cmap
= gtk_widget_get_default_colormap ();
513 m_colorCube
= ( unsigned char *) malloc ( 32 * 32 * 32 );
515 for ( int r
= 0 ; r
< 32 ; r
++)
517 for ( int g
= 0 ; g
< 32 ; g
++)
519 for ( int b
= 0 ; b
< 32 ; b
++)
521 int rr
= ( r
<< 3 ) | ( r
>> 2 );
522 int gg
= ( g
<< 3 ) | ( g
>> 2 );
523 int bb
= ( b
<< 3 ) | ( b
>> 2 );
527 GdkColor
* colors
= cmap
-> colors
;
532 for ( int i
= 0 ; i
< cmap
-> size
; i
++)
534 int rdiff
= (( rr
<< 8 ) - colors
[ i
]. red
);
535 int gdiff
= (( gg
<< 8 ) - colors
[ i
]. green
);
536 int bdiff
= (( bb
<< 8 ) - colors
[ i
]. blue
);
537 int sum
= ABS ( rdiff
) + ABS ( gdiff
) + ABS ( bdiff
);
540 index
= i
; max
= sum
;
546 // assume 8-bit true or static colors. this really exists
547 GdkVisual
* vis
= gdk_colormap_get_visual ( cmap
);
548 index
= ( r
>> ( 5 - vis
-> red_prec
)) << vis
-> red_shift
;
549 index
|= ( g
>> ( 5 - vis
-> green_prec
)) << vis
-> green_shift
;
550 index
|= ( b
>> ( 5 - vis
-> blue_prec
)) << vis
-> blue_shift
;
552 m_colorCube
[ ( r
* 1024 ) + ( g
* 32 ) + b
] = index
;
560 GdkVisual
* wxApp :: GetGdkVisual ()
562 GdkVisual
* visual
= NULL
;
565 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
567 visual
= gdk_window_get_visual ( wxGetRootWindow ()-> window
);
574 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
579 // GTK 1.2 up to version 1.2.3 has broken threads
580 if (( gtk_major_version
== 1 ) &&
581 ( gtk_minor_version
== 2 ) &&
582 ( gtk_micro_version
< 4 ))
584 printf ( "wxWidgets warning: GUI threading disabled due to outdated GTK version \n " );
588 if (! g_thread_supported ())
591 #endif // wxUSE_THREADS
595 // We should have the wxUSE_WCHAR_T test on the _outside_
597 #if defined(__WXGTK20__)
598 // gtk+ 2.0 supports Unicode through UTF-8 strings
599 wxConvCurrent
= & wxConvUTF8
;
602 wxConvCurrent
= & wxConvLocal
;
604 #else // !wxUSE_WCHAR_T
606 wxConvCurrent
= ( wxMBConv
*) NULL
;
607 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
610 // decide which conversion to use for the file names
612 // (1) this variable exists for the sole purpose of specifying the encoding
613 // of the filenames for GTK+ programs, so use it if it is set
614 wxString
encName ( wxGetenv ( _T ( "G_FILENAME_ENCODING" )));
615 encName
= encName
. BeforeFirst ( _T ( ',' ));
616 if ( encName
== _T ( "@locale" ))
622 // (2) if a non default locale is set, assume that the user wants his
623 // filenames in this locale too
624 encName
= wxLocale :: GetSystemEncodingName (). Upper ();
625 // (3) finally use UTF-8 by default
626 if ( encName
. empty () || encName
== _T ( "US-ASCII" ))
627 encName
= _T ( "UTF-8" );
628 wxSetEnv ( _T ( "G_FILENAME_ENCODING" ), encName
);
632 encName
= _T ( "UTF-8" );
634 static wxConvBrokenFileNames
fileconv ( encName
);
635 wxConvFileName
= & fileconv
;
636 #endif // __WXGTK20__
639 // gtk_init() wants UTF-8, not wchar_t, so convert
641 char ** argvGTK
= new char *[ argc
+ 1 ];
642 for ( i
= 0 ; i
< argc
; i
++ )
644 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
647 argvGTK
[ argc
] = NULL
;
652 init_result
= true ; // is there a _check() version of this?
653 gpe_application_init ( & argcGTK
, & argvGTK
);
655 init_result
= gtk_init_check ( & argcGTK
, & argvGTK
);
658 if ( argcGTK
!= argc
)
660 // we have to drop the parameters which were consumed by GTK+
661 for ( i
= 0 ; i
< argcGTK
; i
++ )
663 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
665 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
671 //else: gtk_init() didn't modify our parameters
674 for ( i
= 0 ; i
< argcGTK
; i
++ )
680 #else // !wxUSE_UNICODE
681 // gtk_init() shouldn't actually change argv itself (just its contents) so
682 // it's ok to pass pointer to it
683 init_result
= gtk_init_check ( & argc
, & argv
);
684 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
687 wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));
691 // we can not enter threads before gtk_init is done
694 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
701 wxSetDetectableAutoRepeat ( TRUE
);
704 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
710 void wxApp :: CleanUp ()
714 wxAppBase :: CleanUp ();
719 void wxApp :: OnAssert ( const wxChar
* file
, int line
, const wxChar
* cond
, const wxChar
* msg
)
723 wxAppBase :: OnAssert ( file
, line
, cond
, msg
);
725 m_isInAssert
= FALSE
;
728 #endif // __WXDEBUG__
730 void wxApp :: RemoveIdleTag ()
733 wxMutexLocker
lock ( gs_idleTagsMutex
);
737 gtk_idle_remove ( wxTheApp
-> m_idleTag
);
738 wxTheApp
-> m_idleTag
= 0 ;