]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/app.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
19 #include "wx/dialog.h"
20 #include "wx/settings.h"
21 #include "wx/msgdlg.h"
22 #include "wx/memory.h"
24 #include "wx/gdicmn.h"
26 #include "wx/module.h"
30 #include "wx/filename.h"
31 #include "wx/thread.h"
37 #ifdef __WXUNIVERSAL__
38 #include "wx/univ/theme.h"
39 #include "wx/univ/renderer.h"
43 #include "wx/thread.h"
52 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
53 // in neither poll.h nor sys/poll.h which results in link errors later
66 // we implement poll() ourselves using select() which is supposed exist in
68 #include <sys/types.h>
71 #ifdef HAVE_SYS_SELECT_H
72 #include <sys/select.h>
74 #endif // HAVE_POLL/!HAVE_POLL
76 #include "wx/unix/private.h"
77 #include "wx/gtk1/win_gtk.h"
81 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
85 bool g_mainThreadLocked
= false ;
86 gint g_pendingTag
= 0 ;
88 static GtkWidget
* gs_RootWindow
= ( GtkWidget
*) NULL
;
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
96 void wxapp_install_idle_handler ();
99 static wxMutex gs_idleTagsMutex
;
102 //-----------------------------------------------------------------------------
104 //-----------------------------------------------------------------------------
106 // not static because used by textctrl.cpp
109 bool wxIsInsideYield
= false ;
111 bool wxApp :: Yield ( bool onlyIfNeeded
)
113 if ( wxIsInsideYield
)
117 wxFAIL_MSG ( wxT ( "wxYield called recursively" ) );
124 if ( ! wxThread :: IsMain () )
126 // can't call gtk_main_iteration() from other threads like this
129 #endif // wxUSE_THREADS
131 wxIsInsideYield
= true ;
133 // We need to remove idle callbacks or the loop will
135 wxTheApp
-> RemoveIdleTag ();
138 // disable log flushing from here because a call to wxYield() shouldn't
139 // normally result in message boxes popping up &c
143 while ( gtk_events_pending ())
144 gtk_main_iteration ();
146 // It's necessary to call ProcessIdle() to update the frames sizes which
147 // might have been changed (it also will update other things set from
148 // OnUpdateUI() which is a nice (and desired) side effect). But we
149 // call ProcessIdle() only once since this is not meant for longish
150 // background jobs (controlled by wxIdleEvent::RequestMore() and the
151 // return value of Processidle().
155 // let the logs be flashed again
159 wxIsInsideYield
= false ;
164 //-----------------------------------------------------------------------------
166 //-----------------------------------------------------------------------------
168 // RR/KH: The wxMutexGui calls are not needed on GTK2 according to
169 // the GTK faq, http://www.gtk.org/faq/#AEN500
170 // The calls to gdk_threads_enter() and leave() are specifically noted
171 // as not being necessary. The MutexGui calls are still left in for GTK1.
172 // Eliminating the MutexGui calls fixes the long-standing "random" lockup
173 // when using wxPostEvent (which calls WakeUpIdle) from a thread.
175 void wxApp :: WakeUpIdle ()
178 if (! wxThread :: IsMain ())
180 #endif // wxUSE_THREADS
182 wxapp_install_idle_handler ();
185 if (! wxThread :: IsMain ())
187 #endif // wxUSE_THREADS
190 //-----------------------------------------------------------------------------
192 //-----------------------------------------------------------------------------
194 // the callback functions must be extern "C" to comply with GTK+ declarations
198 static gint
wxapp_pending_callback ( gpointer
WXUNUSED ( data
) )
200 if (! wxTheApp
) return TRUE
;
202 // When getting called from GDK's time-out handler
203 // we are no longer within GDK's grab on the GUI
204 // thread so we must lock it here ourselves.
207 // Sent idle event to all who request them.
208 wxTheApp
-> ProcessPendingEvents ();
212 wxMutexLocker
lock ( gs_idleTagsMutex
);
217 // Flush the logged messages if any.
219 wxLog :: FlushActive ();
222 // Release lock again
225 // Return FALSE to indicate that no more idle events are
226 // to be sent (single shot instead of continuous stream)
230 static gint
wxapp_idle_callback ( gpointer
WXUNUSED ( data
) )
236 // don't generate the idle events while the assert modal dialog is shown,
237 // this completely confuses the apps which don't expect to be reentered
238 // from some safely-looking functions
239 if ( wxTheApp
-> IsInAssert () )
241 // But repaint the assertion message if necessary
242 if ( wxTopLevelWindows
. GetCount () > 0 )
244 wxWindow
* win
= ( wxWindow
*) wxTopLevelWindows
. GetLast ()-> GetData ();
245 if ( win
-> IsKindOf ( CLASSINFO ( wxGenericMessageDialog
)))
246 win
-> OnInternalIdle ();
250 #endif // __WXDEBUG__
252 // When getting called from GDK's time-out handler
253 // we are no longer within GDK's grab on the GUI
254 // thread so we must lock it here ourselves.
257 // Indicate that we are now in idle mode and event handlers
258 // will have to reinstall the idle handler again.
261 wxMutexLocker
lock ( gs_idleTagsMutex
);
264 wxTheApp
-> m_idleTag
= 0 ;
267 // Send idle event to all who request them as long as
268 // no events have popped up in the event queue.
269 while ( wxTheApp
-> ProcessIdle () && ( gtk_events_pending () == 0 ))
272 // Release lock again
275 // Return FALSE to indicate that no more idle events are
276 // to be sent (single shot instead of continuous stream).
284 #define wxPollFd pollfd
287 typedef GPollFD wxPollFd
;
289 int wxPoll ( wxPollFd
* ufds
, unsigned int nfds
, int timeout
)
291 // convert timeout from ms to struct timeval (s/us)
293 tv_timeout
. tv_sec
= timeout
/ 1000 ;
294 tv_timeout
. tv_usec
= ( timeout%1000
)* 1000 ;
296 // remember the highest fd used here
299 // and fill the sets for select()
304 wxFD_ZERO (& writefds
);
305 wxFD_ZERO (& exceptfds
);
308 for ( i
= 0 ; i
< nfds
; i
++ )
310 wxASSERT_MSG ( ufds
[ i
]. fd
< FD_SETSIZE
, _T ( "fd out of range" ) );
312 if ( ufds
[ i
]. events
& G_IO_IN
)
313 wxFD_SET ( ufds
[ i
]. fd
, & readfds
);
315 if ( ufds
[ i
]. events
& G_IO_PRI
)
316 wxFD_SET ( ufds
[ i
]. fd
, & exceptfds
);
318 if ( ufds
[ i
]. events
& G_IO_OUT
)
319 wxFD_SET ( ufds
[ i
]. fd
, & writefds
);
321 if ( ufds
[ i
]. fd
> fdMax
)
326 int res
= select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);
328 // translate the results back
329 for ( i
= 0 ; i
< nfds
; i
++ )
333 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & readfds
) )
334 ufds
[ i
]. revents
|= G_IO_IN
;
336 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & exceptfds
) )
337 ufds
[ i
]. revents
|= G_IO_PRI
;
339 if ( wxFD_ISSET ( ufds
[ i
]. fd
, & writefds
) )
340 ufds
[ i
]. revents
|= G_IO_OUT
;
346 #endif // HAVE_POLL/!HAVE_POLL
348 static gint
wxapp_poll_func ( GPollFD
* ufds
, guint nfds
, gint timeout
)
353 g_mainThreadLocked
= true ;
355 // we rely on the fact that glib GPollFD struct is really just pollfd but
356 // I wonder how wise is this in the long term (VZ)
357 gint res
= wxPoll ( ( wxPollFd
*) ufds
, nfds
, timeout
);
360 g_mainThreadLocked
= false ;
367 #endif // wxUSE_THREADS
371 void wxapp_install_idle_handler ()
374 wxMutexLocker
lock ( gs_idleTagsMutex
);
377 // Don't install the handler if it's already installed. This test *MUST*
378 // be done when gs_idleTagsMutex is locked!
382 // GD: this assert is raised when using the thread sample (which works)
383 // so the test is probably not so easy. Can widget callbacks be
384 // triggered from child threads and, if so, for which widgets?
385 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
387 wxASSERT_MSG ( wxTheApp
-> m_idleTag
== 0 , wxT ( "attempt to install idle handler twice" ) );
391 if ( g_pendingTag
== 0 )
392 g_pendingTag
= gtk_idle_add_priority ( 900 , wxapp_pending_callback
, ( gpointer
) NULL
);
394 // This routine gets called by all event handlers
395 // indicating that the idle is over. It may also
396 // get called from other thread for sending events
397 // to the main thread (and processing these in
398 // idle time). Very low priority.
399 wxTheApp
-> m_idleTag
= gtk_idle_add_priority ( 1000 , wxapp_idle_callback
, ( gpointer
) NULL
);
402 //-----------------------------------------------------------------------------
403 // Access to the root window global
404 //-----------------------------------------------------------------------------
406 GtkWidget
* wxGetRootWindow ()
408 if ( gs_RootWindow
== NULL
)
410 gs_RootWindow
= gtk_window_new ( GTK_WINDOW_TOPLEVEL
);
411 gtk_widget_realize ( gs_RootWindow
);
413 return gs_RootWindow
;
416 //-----------------------------------------------------------------------------
418 //-----------------------------------------------------------------------------
420 IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)
425 m_isInAssert
= false ;
426 #endif // __WXDEBUG__
430 wxapp_install_idle_handler ();
433 g_main_set_poll_func ( wxapp_poll_func
);
436 m_colorCube
= ( unsigned char *) NULL
;
438 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
439 m_glVisualInfo
= ( void *) NULL
;
440 m_glFBCInfo
= ( void *) NULL
;
445 if ( m_idleTag
) gtk_idle_remove ( m_idleTag
);
447 if ( m_colorCube
) free ( m_colorCube
);
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 GdkVisual
* vis
= gdkx_visual_get (
462 (( XVisualInfo
*) m_glVisualInfo
) -> visualid
);
463 gtk_widget_set_default_visual ( vis
);
465 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
466 gtk_widget_set_default_colormap ( colormap
);
471 // On some machines, the default visual is just 256 colours, so
472 // we make sure we get the best. This can sometimes be wasteful.
475 if (( gdk_visual_get_best () != gdk_visual_get_system ()) && ( m_useBestVisual
))
477 GdkVisual
* vis
= gdk_visual_get_best ();
478 gtk_widget_set_default_visual ( vis
);
480 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
481 gtk_widget_set_default_colormap ( colormap
);
486 // Nothing to do for 15, 16, 24, 32 bit displays
487 if ( visual
-> depth
> 8 ) return TRUE
;
489 // initialize color cube for 8-bit color reduction dithering
491 GdkColormap
* cmap
= gtk_widget_get_default_colormap ();
493 m_colorCube
= ( unsigned char *) malloc ( 32 * 32 * 32 );
495 for ( int r
= 0 ; r
< 32 ; r
++)
497 for ( int g
= 0 ; g
< 32 ; g
++)
499 for ( int b
= 0 ; b
< 32 ; b
++)
501 int rr
= ( r
<< 3 ) | ( r
>> 2 );
502 int gg
= ( g
<< 3 ) | ( g
>> 2 );
503 int bb
= ( b
<< 3 ) | ( b
>> 2 );
507 GdkColor
* colors
= cmap
-> colors
;
512 for ( int i
= 0 ; i
< cmap
-> size
; i
++)
514 int rdiff
= (( rr
<< 8 ) - colors
[ i
]. red
);
515 int gdiff
= (( gg
<< 8 ) - colors
[ i
]. green
);
516 int bdiff
= (( bb
<< 8 ) - colors
[ i
]. blue
);
517 int sum
= ABS ( rdiff
) + ABS ( gdiff
) + ABS ( bdiff
);
520 index
= i
; max
= sum
;
526 // assume 8-bit true or static colors. this really exists
527 GdkVisual
* vis
= gdk_colormap_get_visual ( cmap
);
528 index
= ( r
>> ( 5 - vis
-> red_prec
)) << vis
-> red_shift
;
529 index
|= ( g
>> ( 5 - vis
-> green_prec
)) << vis
-> green_shift
;
530 index
|= ( b
>> ( 5 - vis
-> blue_prec
)) << vis
-> blue_shift
;
532 m_colorCube
[ ( r
* 1024 ) + ( g
* 32 ) + b
] = index
;
540 GdkVisual
* wxApp :: GetGdkVisual ()
542 GdkVisual
* visual
= NULL
;
545 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
547 visual
= gdk_window_get_visual ( wxGetRootWindow ()-> window
);
554 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
559 // GTK 1.2 up to version 1.2.3 has broken threads
560 if (( gtk_major_version
== 1 ) &&
561 ( gtk_minor_version
== 2 ) &&
562 ( gtk_micro_version
< 4 ))
564 printf ( "wxWidgets warning: GUI threading disabled due to outdated GTK version \n " );
568 if (! g_thread_supported ())
571 #endif // wxUSE_THREADS
575 // We should have the wxUSE_WCHAR_T test on the _outside_
578 wxConvCurrent
= & wxConvLocal
;
579 #else // !wxUSE_WCHAR_T
581 wxConvCurrent
= ( wxMBConv
*) NULL
;
582 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
585 // gtk_init() wants UTF-8, not wchar_t, so convert
587 char ** argvGTK
= new char *[ argc
+ 1 ];
588 for ( i
= 0 ; i
< argc
; i
++ )
590 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
593 argvGTK
[ argc
] = NULL
;
598 init_result
= true ; // is there a _check() version of this?
599 gpe_application_init ( & argcGTK
, & argvGTK
);
601 init_result
= gtk_init_check ( & argcGTK
, & argvGTK
);
604 if ( argcGTK
!= argc
)
606 // we have to drop the parameters which were consumed by GTK+
607 for ( i
= 0 ; i
< argcGTK
; i
++ )
609 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
611 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
617 //else: gtk_init() didn't modify our parameters
620 for ( i
= 0 ; i
< argcGTK
; i
++ )
626 #else // !wxUSE_UNICODE
627 // gtk_init() shouldn't actually change argv itself (just its contents) so
628 // it's ok to pass pointer to it
629 init_result
= gtk_init_check ( & argc
, & argv
);
630 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
633 wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));
637 // we can not enter threads before gtk_init is done
640 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
647 wxSetDetectableAutoRepeat ( TRUE
);
650 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
656 void wxApp :: CleanUp ()
660 wxAppBase :: CleanUp ();
665 void wxApp :: OnAssert ( const wxChar
* file
, int line
, const wxChar
* cond
, const wxChar
* msg
)
669 wxAppBase :: OnAssert ( file
, line
, cond
, msg
);
671 m_isInAssert
= false ;
674 #endif // __WXDEBUG__
676 void wxApp :: RemoveIdleTag ()
679 wxMutexLocker
lock ( gs_idleTagsMutex
);
683 gtk_idle_remove ( wxTheApp
-> m_idleTag
);
684 wxTheApp
-> m_idleTag
= 0 ;