]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/app.cpp
a75aabe611f1c84fbe7e81b293db5dd9afc6e333
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "app.h"
15 // vms_jackets.h should for proper working be included before anything else
16 # include <vms_jackets.h>
17 #undef ConnectionNumber
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
24 #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/gtk/win_gtk.h"
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
87 bool g_mainThreadLocked
= FALSE
;
88 gint g_pendingTag
= 0 ;
90 static GtkWidget
* gs_RootWindow
= ( GtkWidget
*) NULL
;
92 //-----------------------------------------------------------------------------
94 //-----------------------------------------------------------------------------
98 void wxapp_install_idle_handler ();
101 static wxMutex gs_idleTagsMutex
;
104 //-----------------------------------------------------------------------------
106 //-----------------------------------------------------------------------------
108 // not static because used by textctrl.cpp
111 bool wxIsInsideYield
= FALSE
;
113 bool wxApp :: Yield ( bool onlyIfNeeded
)
115 if ( wxIsInsideYield
)
119 wxFAIL_MSG ( wxT ( "wxYield called recursively" ) );
126 if ( ! wxThread :: IsMain () )
128 // can't call gtk_main_iteration() from other threads like this
131 #endif // wxUSE_THREADS
133 wxIsInsideYield
= TRUE
;
135 // We need to remove idle callbacks or the loop will
137 wxTheApp
-> RemoveIdleTag ();
140 // disable log flushing from here because a call to wxYield() shouldn't
141 // normally result in message boxes popping up &c
145 while ( gtk_events_pending ())
146 gtk_main_iteration ();
148 // It's necessary to call ProcessIdle() to update the frames sizes which
149 // might have been changed (it also will update other things set from
150 // OnUpdateUI() which is a nice (and desired) side effect). But we
151 // call ProcessIdle() only once since this is not meant for longish
152 // background jobs (controlled by wxIdleEvent::RequestMore() and the
153 // return value of Processidle().
157 // let the logs be flashed again
161 wxIsInsideYield
= FALSE
;
166 //-----------------------------------------------------------------------------
168 //-----------------------------------------------------------------------------
170 // RR/KH: The wxMutexGui calls are not needed on GTK2 according to
171 // the GTK faq, http://www.gtk.org/faq/#AEN500
172 // The calls to gdk_threads_enter() and leave() are specifically noted
173 // as not being necessary. The MutexGui calls are still left in for GTK1.
174 // Eliminating the MutexGui calls fixes the long-standing "random" lockup
175 // when using wxPostEvent (which calls WakeUpIdle) from a thread.
177 void wxApp :: WakeUpIdle ()
181 if (! wxThread :: IsMain ())
183 #endif // wxUSE_THREADS_
186 wxapp_install_idle_handler ();
190 if (! wxThread :: IsMain ())
192 #endif // wxUSE_THREADS_
196 //-----------------------------------------------------------------------------
198 //-----------------------------------------------------------------------------
200 // the callback functions must be extern "C" to comply with GTK+ declarations
204 static gint
wxapp_pending_callback ( gpointer
WXUNUSED ( data
) )
206 if (! wxTheApp
) return TRUE
;
208 // When getting called from GDK's time-out handler
209 // we are no longer within GDK's grab on the GUI
210 // thread so we must lock it here ourselves.
213 // Sent idle event to all who request them.
214 wxTheApp
-> ProcessPendingEvents ();
218 wxMutexLocker
lock ( gs_idleTagsMutex
);
223 // Flush the logged messages if any.
225 wxLog :: FlushActive ();
228 // Release lock again
231 // Return FALSE to indicate that no more idle events are
232 // to be sent (single shot instead of continuous stream)
236 static gint
wxapp_idle_callback ( gpointer
WXUNUSED ( data
) )
242 // don't generate the idle events while the assert modal dialog is shown,
243 // this completely confuses the apps which don't expect to be reentered
244 // from some safely-looking functions
245 if ( wxTheApp
-> IsInAssert () )
247 // But repaint the assertion message if necessary
248 if ( wxTopLevelWindows
. GetCount () > 0 )
250 wxWindow
* win
= ( wxWindow
*) wxTopLevelWindows
. GetLast ()-> GetData ();
252 if ( win
-> IsKindOf ( CLASSINFO ( wxMessageDialog
)))
254 if ( win
-> IsKindOf ( CLASSINFO ( wxGenericMessageDialog
)))
256 win
-> OnInternalIdle ();
260 #endif // __WXDEBUG__
262 // When getting called from GDK's time-out handler
263 // we are no longer within GDK's grab on the GUI
264 // thread so we must lock it here ourselves.
267 // Indicate that we are now in idle mode and event handlers
268 // will have to reinstall the idle handler again.
271 wxMutexLocker
lock ( gs_idleTagsMutex
);
274 wxTheApp
-> m_idleTag
= 0 ;
277 // Send idle event to all who request them as long as
278 // no events have popped up in the event queue.
279 while ( wxTheApp
-> ProcessIdle () && ( gtk_events_pending () == 0 ))
282 // Release lock again
285 // Return FALSE to indicate that no more idle events are
286 // to be sent (single shot instead of continuous stream).
294 #define wxPollFd pollfd
297 typedef GPollFD wxPollFd
;
299 int wxPoll ( wxPollFd
* ufds
, unsigned int nfds
, int timeout
)
301 // convert timeout from ms to struct timeval (s/us)
303 tv_timeout
. tv_sec
= timeout
/ 1000 ;
304 tv_timeout
. tv_usec
= ( timeout%1000
)* 1000 ;
306 // remember the highest fd used here
309 // and fill the sets for select()
318 for ( i
= 0 ; i
< nfds
; i
++ )
320 wxASSERT_MSG ( ufds
[ i
]. fd
< FD_SETSIZE
, _T ( "fd out of range" ) );
322 if ( ufds
[ i
]. events
& G_IO_IN
)
323 FD_SET ( ufds
[ i
]. fd
, & readfds
);
325 if ( ufds
[ i
]. events
& G_IO_PRI
)
326 FD_SET ( ufds
[ i
]. fd
, & exceptfds
);
328 if ( ufds
[ i
]. events
& G_IO_OUT
)
329 FD_SET ( ufds
[ i
]. fd
, & writefds
);
331 if ( ufds
[ i
]. fd
> fdMax
)
336 int res
= select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);
338 // translate the results back
339 for ( i
= 0 ; i
< nfds
; i
++ )
343 if ( FD_ISSET ( ufds
[ i
]. fd
, & readfds
) )
344 ufds
[ i
]. revents
|= G_IO_IN
;
346 if ( FD_ISSET ( ufds
[ i
]. fd
, & exceptfds
) )
347 ufds
[ i
]. revents
|= G_IO_PRI
;
349 if ( FD_ISSET ( ufds
[ i
]. fd
, & writefds
) )
350 ufds
[ i
]. revents
|= G_IO_OUT
;
356 #endif // HAVE_POLL/!HAVE_POLL
358 static gint
wxapp_poll_func ( GPollFD
* ufds
, guint nfds
, gint timeout
)
363 g_mainThreadLocked
= TRUE
;
365 // we rely on the fact that glib GPollFD struct is really just pollfd but
366 // I wonder how wise is this in the long term (VZ)
367 gint res
= wxPoll ( ( wxPollFd
*) ufds
, nfds
, timeout
);
370 g_mainThreadLocked
= FALSE
;
377 #endif // wxUSE_THREADS
381 void wxapp_install_idle_handler ()
384 wxMutexLocker
lock ( gs_idleTagsMutex
);
387 // Don't install the handler if it's already installed. This test *MUST*
388 // be done when gs_idleTagsMutex is locked!
392 // GD: this assert is raised when using the thread sample (which works)
393 // so the test is probably not so easy. Can widget callbacks be
394 // triggered from child threads and, if so, for which widgets?
395 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
397 wxASSERT_MSG ( wxTheApp
-> m_idleTag
== 0 , wxT ( "attempt to install idle handler twice" ) );
401 if ( g_pendingTag
== 0 )
402 g_pendingTag
= gtk_idle_add_priority ( 900 , wxapp_pending_callback
, ( gpointer
) NULL
);
404 // This routine gets called by all event handlers
405 // indicating that the idle is over. It may also
406 // get called from other thread for sending events
407 // to the main thread (and processing these in
408 // idle time). Very low priority.
409 wxTheApp
-> m_idleTag
= gtk_idle_add_priority ( 1000 , wxapp_idle_callback
, ( gpointer
) NULL
);
412 //-----------------------------------------------------------------------------
413 // Access to the root window global
414 //-----------------------------------------------------------------------------
416 GtkWidget
* wxGetRootWindow ()
418 if ( gs_RootWindow
== NULL
)
420 gs_RootWindow
= gtk_window_new ( GTK_WINDOW_TOPLEVEL
);
421 gtk_widget_realize ( gs_RootWindow
);
423 return gs_RootWindow
;
426 //-----------------------------------------------------------------------------
428 //-----------------------------------------------------------------------------
430 IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)
432 BEGIN_EVENT_TABLE ( wxApp
, wxEvtHandler
)
433 EVT_IDLE ( wxAppBase :: OnIdle
)
439 m_isInAssert
= FALSE
;
440 #endif // __WXDEBUG__
444 wxapp_install_idle_handler ();
447 g_main_set_poll_func ( wxapp_poll_func
);
450 m_colorCube
= ( unsigned char *) NULL
;
452 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
453 m_glVisualInfo
= ( void *) NULL
;
454 m_glFBCInfo
= ( void *) NULL
;
459 if ( m_idleTag
) gtk_idle_remove ( m_idleTag
);
461 if ( m_colorCube
) free ( m_colorCube
);
464 bool wxApp :: OnInitGui ()
466 if ( ! wxAppBase :: OnInitGui () )
469 GdkVisual
* visual
= gdk_visual_get_system ();
471 // if this is a wxGLApp (derived from wxApp), and we've already
472 // chosen a specific visual, then derive the GdkVisual from that
473 if ( m_glVisualInfo
!= NULL
)
476 // seems gtk_widget_set_default_visual no longer exists?
477 GdkVisual
* vis
= gtk_widget_get_default_visual ();
479 GdkVisual
* vis
= gdkx_visual_get (
480 (( XVisualInfo
*) m_glVisualInfo
) -> visualid
);
481 gtk_widget_set_default_visual ( vis
);
484 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
485 gtk_widget_set_default_colormap ( colormap
);
490 // On some machines, the default visual is just 256 colours, so
491 // we make sure we get the best. This can sometimes be wasteful.
494 if (( gdk_visual_get_best () != gdk_visual_get_system ()) && ( m_useBestVisual
))
497 /* seems gtk_widget_set_default_visual no longer exists? */
498 GdkVisual
* vis
= gtk_widget_get_default_visual ();
500 GdkVisual
* vis
= gdk_visual_get_best ();
501 gtk_widget_set_default_visual ( vis
);
504 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
505 gtk_widget_set_default_colormap ( colormap
);
510 // Nothing to do for 15, 16, 24, 32 bit displays
511 if ( visual
-> depth
> 8 ) return TRUE
;
513 // initialize color cube for 8-bit color reduction dithering
515 GdkColormap
* cmap
= gtk_widget_get_default_colormap ();
517 m_colorCube
= ( unsigned char *) malloc ( 32 * 32 * 32 );
519 for ( int r
= 0 ; r
< 32 ; r
++)
521 for ( int g
= 0 ; g
< 32 ; g
++)
523 for ( int b
= 0 ; b
< 32 ; b
++)
525 int rr
= ( r
<< 3 ) | ( r
>> 2 );
526 int gg
= ( g
<< 3 ) | ( g
>> 2 );
527 int bb
= ( b
<< 3 ) | ( b
>> 2 );
531 GdkColor
* colors
= cmap
-> colors
;
536 for ( int i
= 0 ; i
< cmap
-> size
; i
++)
538 int rdiff
= (( rr
<< 8 ) - colors
[ i
]. red
);
539 int gdiff
= (( gg
<< 8 ) - colors
[ i
]. green
);
540 int bdiff
= (( bb
<< 8 ) - colors
[ i
]. blue
);
541 int sum
= ABS ( rdiff
) + ABS ( gdiff
) + ABS ( bdiff
);
544 index
= i
; max
= sum
;
550 // assume 8-bit true or static colors. this really exists
551 GdkVisual
* vis
= gdk_colormap_get_visual ( cmap
);
552 index
= ( r
>> ( 5 - vis
-> red_prec
)) << vis
-> red_shift
;
553 index
|= ( g
>> ( 5 - vis
-> green_prec
)) << vis
-> green_shift
;
554 index
|= ( b
>> ( 5 - vis
-> blue_prec
)) << vis
-> blue_shift
;
556 m_colorCube
[ ( r
* 1024 ) + ( g
* 32 ) + b
] = index
;
564 GdkVisual
* wxApp :: GetGdkVisual ()
566 GdkVisual
* visual
= NULL
;
569 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
571 visual
= gdk_window_get_visual ( wxGetRootWindow ()-> window
);
578 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
583 // GTK 1.2 up to version 1.2.3 has broken threads
584 if (( gtk_major_version
== 1 ) &&
585 ( gtk_minor_version
== 2 ) &&
586 ( gtk_micro_version
< 4 ))
588 printf ( "wxWidgets warning: GUI threading disabled due to outdated GTK version \n " );
592 if (! g_thread_supported ())
595 #endif // wxUSE_THREADS
599 // We should have the wxUSE_WCHAR_T test on the _outside_
601 #if defined(__WXGTK20__)
602 // gtk+ 2.0 supports Unicode through UTF-8 strings
603 wxConvCurrent
= & wxConvUTF8
;
606 wxConvCurrent
= & wxConvLocal
;
608 #else // !wxUSE_WCHAR_T
610 wxConvCurrent
= ( wxMBConv
*) NULL
;
611 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
614 // decide which conversion to use for the file names
616 // (1) this variable exists for the sole purpose of specifying the encoding
617 // of the filenames for GTK+ programs, so use it if it is set
618 wxString
encName ( wxGetenv ( _T ( "G_FILENAME_ENCODING" )));
619 encName
= encName
. BeforeFirst ( _T ( ',' ));
620 if ( encName
== _T ( "@locale" ))
626 // (2) if a non default locale is set, assume that the user wants his
627 // filenames in this locale too
628 encName
= wxLocale :: GetSystemEncodingName (). Upper ();
629 // (3) finally use UTF-8 by default
630 if ( encName
. empty () || encName
== _T ( "US-ASCII" ))
631 encName
= _T ( "UTF-8" );
632 wxSetEnv ( _T ( "G_FILENAME_ENCODING" ), encName
);
636 encName
= _T ( "UTF-8" );
638 static wxConvBrokenFileNames
fileconv ( encName
);
639 wxConvFileName
= & fileconv
;
640 #endif // __WXGTK20__
643 // gtk_init() wants UTF-8, not wchar_t, so convert
645 char ** argvGTK
= new char *[ argc
+ 1 ];
646 for ( i
= 0 ; i
< argc
; i
++ )
648 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
651 argvGTK
[ argc
] = NULL
;
656 init_result
= true ; // is there a _check() version of this?
657 gpe_application_init ( & argcGTK
, & argvGTK
);
659 init_result
= gtk_init_check ( & argcGTK
, & argvGTK
);
662 if ( argcGTK
!= argc
)
664 // we have to drop the parameters which were consumed by GTK+
665 for ( i
= 0 ; i
< argcGTK
; i
++ )
667 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
669 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
675 //else: gtk_init() didn't modify our parameters
678 for ( i
= 0 ; i
< argcGTK
; i
++ )
684 #else // !wxUSE_UNICODE
685 // gtk_init() shouldn't actually change argv itself (just its contents) so
686 // it's ok to pass pointer to it
687 init_result
= gtk_init_check ( & argc
, & argv
);
688 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
691 wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));
695 // we can not enter threads before gtk_init is done
698 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
705 wxSetDetectableAutoRepeat ( TRUE
);
708 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
714 void wxApp :: CleanUp ()
718 wxAppBase :: CleanUp ();
723 void wxApp :: OnAssert ( const wxChar
* file
, int line
, const wxChar
* cond
, const wxChar
* msg
)
727 wxAppBase :: OnAssert ( file
, line
, cond
, msg
);
729 m_isInAssert
= FALSE
;
732 #endif // __WXDEBUG__
734 void wxApp :: RemoveIdleTag ()
737 wxMutexLocker
lock ( gs_idleTagsMutex
);
741 gtk_idle_remove ( wxTheApp
-> m_idleTag
);
742 wxTheApp
-> m_idleTag
= 0 ;