]>
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 /////////////////////////////////////////////////////////////////////////////
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"
42 #ifdef __WXUNIVERSAL__
43 #include "wx/univ/theme.h"
44 #include "wx/univ/renderer.h"
48 #include "wx/thread.h"
57 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
58 // in neither poll.h nor sys/poll.h which results in link errors later
71 // we implement poll() ourselves using select() which is supposed exist in
73 #include <sys/types.h>
76 #endif // HAVE_POLL/!HAVE_POLL
78 #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 ();
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
;
133 // We need to remove idle callbacks or the loop will
135 gtk_idle_remove ( m_idleTag
);
140 // disable log flushing from here because a call to wxYield() shouldn't
141 // normally result in message boxes popping up &c
144 while ( gtk_events_pending ())
145 gtk_main_iteration ();
147 // It's necessary to call ProcessIdle() to update the frames sizes which
148 // might have been changed (it also will update other things set from
149 // OnUpdateUI() which is a nice (and desired) side effect). But we
150 // call ProcessIdle() only once since this is not meant for longish
151 // background jobs (controlled by wxIdleEvent::RequestMore() and the
152 // return value of Processidle().
155 // let the logs be flashed again
158 wxIsInsideYield
= FALSE
;
163 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
167 // RR/KH: The wxMutexGui calls are not needed on GTK2 according to
168 // the GTK faq, http://www.gtk.org/faq/#AEN500
169 // The calls to gdk_threads_enter() and leave() are specifically noted
170 // as not being necessary. The MutexGui calls are still left in for GTK1.
171 // Eliminating the MutexGui calls fixes the long-standing "random" lockup
172 // when using wxPostEvent (which calls WakeUpIdle) from a thread.
174 void wxApp :: WakeUpIdle ()
178 if (! wxThread :: IsMain ())
180 #endif // wxUSE_THREADS_
184 wxapp_install_idle_handler ();
188 if (! wxThread :: IsMain ())
190 #endif // wxUSE_THREADS_
194 //-----------------------------------------------------------------------------
196 //-----------------------------------------------------------------------------
198 // the callback functions must be extern "C" to comply with GTK+ declarations
202 static gint
wxapp_pending_callback ( gpointer
WXUNUSED ( data
) )
204 if (! wxTheApp
) return TRUE
;
206 // When getting called from GDK's time-out handler
207 // we are no longer within GDK's grab on the GUI
208 // thread so we must lock it here ourselves.
211 // Sent idle event to all who request them.
212 wxTheApp
-> ProcessPendingEvents ();
216 // Flush the logged messages if any.
218 wxLog :: FlushActive ();
221 // Release lock again
224 // Return FALSE to indicate that no more idle events are
225 // to be sent (single shot instead of continuous stream)
229 static gint
wxapp_idle_callback ( gpointer
WXUNUSED ( data
) )
235 // don't generate the idle events while the assert modal dialog is shown,
236 // this completely confuses the apps which don't expect to be reentered
237 // from some safely-looking functions
238 if ( wxTheApp
-> IsInAssert () )
240 // But repaint the assertion message if necessary
241 if ( wxTopLevelWindows
. GetCount () > 0 )
243 wxWindow
* win
= ( wxWindow
*) wxTopLevelWindows
. GetLast ()-> GetData ();
245 if ( win
-> IsKindOf ( CLASSINFO ( wxMessageDialog
)))
247 if ( win
-> IsKindOf ( CLASSINFO ( wxGenericMessageDialog
)))
249 win
-> OnInternalIdle ();
253 #endif // __WXDEBUG__
255 // When getting called from GDK's time-out handler
256 // we are no longer within GDK's grab on the GUI
257 // thread so we must lock it here ourselves.
260 // Indicate that we are now in idle mode and event handlers
261 // will have to reinstall the idle handler again.
263 wxTheApp
-> m_idleTag
= 0 ;
265 // Send idle event to all who request them as long as
266 // no events have popped up in the event queue.
267 while ( wxTheApp
-> ProcessIdle () && ( gtk_events_pending () == 0 ))
270 // Release lock again
273 // Return FALSE to indicate that no more idle events are
274 // to be sent (single shot instead of continuous stream).
282 #define wxPollFd pollfd
285 typedef GPollFD wxPollFd
;
287 int wxPoll ( wxPollFd
* ufds
, unsigned int nfds
, int timeout
)
289 // convert timeout from ms to struct timeval (s/us)
291 tv_timeout
. tv_sec
= timeout
/ 1000 ;
292 tv_timeout
. tv_usec
= ( timeout%1000
)* 1000 ;
294 // remember the highest fd used here
297 // and fill the sets for select()
306 for ( i
= 0 ; i
< nfds
; i
++ )
308 wxASSERT_MSG ( ufds
[ i
]. fd
< FD_SETSIZE
, _T ( "fd out of range" ) );
310 if ( ufds
[ i
]. events
& G_IO_IN
)
311 FD_SET ( ufds
[ i
]. fd
, & readfds
);
313 if ( ufds
[ i
]. events
& G_IO_PRI
)
314 FD_SET ( ufds
[ i
]. fd
, & exceptfds
);
316 if ( ufds
[ i
]. events
& G_IO_OUT
)
317 FD_SET ( ufds
[ i
]. fd
, & writefds
);
319 if ( ufds
[ i
]. fd
> fdMax
)
324 int res
= select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);
326 // translate the results back
327 for ( i
= 0 ; i
< nfds
; i
++ )
331 if ( FD_ISSET ( ufds
[ i
]. fd
, & readfds
) )
332 ufds
[ i
]. revents
|= G_IO_IN
;
334 if ( FD_ISSET ( ufds
[ i
]. fd
, & exceptfds
) )
335 ufds
[ i
]. revents
|= G_IO_PRI
;
337 if ( FD_ISSET ( ufds
[ i
]. fd
, & writefds
) )
338 ufds
[ i
]. revents
|= G_IO_OUT
;
344 #endif // HAVE_POLL/!HAVE_POLL
346 static gint
wxapp_poll_func ( GPollFD
* ufds
, guint nfds
, gint timeout
)
351 g_mainThreadLocked
= TRUE
;
353 // we rely on the fact that glib GPollFD struct is really just pollfd but
354 // I wonder how wise is this in the long term (VZ)
355 gint res
= wxPoll ( ( wxPollFd
*) ufds
, nfds
, timeout
);
358 g_mainThreadLocked
= FALSE
;
365 #endif // wxUSE_THREADS
369 void wxapp_install_idle_handler ()
371 // GD: this assert is raised when using the thread sample (which works)
372 // so the test is probably not so easy. Can widget callbacks be
373 // triggered from child threads and, if so, for which widgets?
374 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
376 wxASSERT_MSG ( wxTheApp
-> m_idleTag
== 0 , wxT ( "attempt to install idle handler twice" ) );
380 if ( g_pendingTag
== 0 )
381 g_pendingTag
= gtk_idle_add_priority ( 900 , wxapp_pending_callback
, ( gpointer
) NULL
);
383 // This routine gets called by all event handlers
384 // indicating that the idle is over. It may also
385 // get called from other thread for sending events
386 // to the main thread (and processing these in
387 // idle time). Very low priority.
388 wxTheApp
-> m_idleTag
= gtk_idle_add_priority ( 1000 , wxapp_idle_callback
, ( gpointer
) NULL
);
391 //-----------------------------------------------------------------------------
392 // Access to the root window global
393 //-----------------------------------------------------------------------------
395 GtkWidget
* wxGetRootWindow ()
397 if ( gs_RootWindow
== NULL
)
399 gs_RootWindow
= gtk_window_new ( GTK_WINDOW_TOPLEVEL
);
400 gtk_widget_realize ( gs_RootWindow
);
402 return gs_RootWindow
;
405 //-----------------------------------------------------------------------------
407 //-----------------------------------------------------------------------------
409 IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)
411 BEGIN_EVENT_TABLE ( wxApp
, wxEvtHandler
)
412 EVT_IDLE ( wxAppBase :: OnIdle
)
418 m_isInAssert
= FALSE
;
419 #endif // __WXDEBUG__
422 wxapp_install_idle_handler ();
425 g_main_set_poll_func ( wxapp_poll_func
);
428 m_colorCube
= ( unsigned char *) NULL
;
430 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
431 m_glVisualInfo
= ( void *) NULL
;
436 if ( m_idleTag
) gtk_idle_remove ( m_idleTag
);
438 if ( m_colorCube
) free ( m_colorCube
);
441 bool wxApp :: OnInitGui ()
443 if ( ! wxAppBase :: OnInitGui () )
446 GdkVisual
* visual
= gdk_visual_get_system ();
448 // if this is a wxGLApp (derived from wxApp), and we've already
449 // chosen a specific visual, then derive the GdkVisual from that
450 if ( m_glVisualInfo
!= NULL
)
453 // seems gtk_widget_set_default_visual no longer exists?
454 GdkVisual
* vis
= gtk_widget_get_default_visual ();
456 GdkVisual
* vis
= gdkx_visual_get (
457 (( XVisualInfo
*) m_glVisualInfo
) -> visualid
);
458 gtk_widget_set_default_visual ( vis
);
461 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
462 gtk_widget_set_default_colormap ( colormap
);
467 // On some machines, the default visual is just 256 colours, so
468 // we make sure we get the best. This can sometimes be wasteful.
471 if (( gdk_visual_get_best () != gdk_visual_get_system ()) && ( m_useBestVisual
))
474 /* seems gtk_widget_set_default_visual no longer exists? */
475 GdkVisual
* vis
= gtk_widget_get_default_visual ();
477 GdkVisual
* vis
= gdk_visual_get_best ();
478 gtk_widget_set_default_visual ( vis
);
481 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
482 gtk_widget_set_default_colormap ( colormap
);
487 // Nothing to do for 15, 16, 24, 32 bit displays
488 if ( visual
-> depth
> 8 ) return TRUE
;
490 // initialize color cube for 8-bit color reduction dithering
492 GdkColormap
* cmap
= gtk_widget_get_default_colormap ();
494 m_colorCube
= ( unsigned char *) malloc ( 32 * 32 * 32 );
496 for ( int r
= 0 ; r
< 32 ; r
++)
498 for ( int g
= 0 ; g
< 32 ; g
++)
500 for ( int b
= 0 ; b
< 32 ; b
++)
502 int rr
= ( r
<< 3 ) | ( r
>> 2 );
503 int gg
= ( g
<< 3 ) | ( g
>> 2 );
504 int bb
= ( b
<< 3 ) | ( b
>> 2 );
508 GdkColor
* colors
= cmap
-> colors
;
513 for ( int i
= 0 ; i
< cmap
-> size
; i
++)
515 int rdiff
= (( rr
<< 8 ) - colors
[ i
]. red
);
516 int gdiff
= (( gg
<< 8 ) - colors
[ i
]. green
);
517 int bdiff
= (( bb
<< 8 ) - colors
[ i
]. blue
);
518 int sum
= ABS ( rdiff
) + ABS ( gdiff
) + ABS ( bdiff
);
521 index
= i
; max
= sum
;
527 // assume 8-bit true or static colors. this really exists
528 GdkVisual
* vis
= gdk_colormap_get_visual ( cmap
);
529 index
= ( r
>> ( 5 - vis
-> red_prec
)) << vis
-> red_shift
;
530 index
|= ( g
>> ( 5 - vis
-> green_prec
)) << vis
-> green_shift
;
531 index
|= ( b
>> ( 5 - vis
-> blue_prec
)) << vis
-> blue_shift
;
533 m_colorCube
[ ( r
* 1024 ) + ( g
* 32 ) + b
] = index
;
541 GdkVisual
* wxApp :: GetGdkVisual ()
543 GdkVisual
* visual
= NULL
;
546 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
548 visual
= gdk_window_get_visual ( wxGetRootWindow ()-> window
);
555 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
560 // GTK 1.2 up to version 1.2.3 has broken threads
561 if (( gtk_major_version
== 1 ) &&
562 ( gtk_minor_version
== 2 ) &&
563 ( gtk_micro_version
< 4 ))
565 printf ( "wxWidgets warning: GUI threading disabled due to outdated GTK version \n " );
569 if (! g_thread_supported ())
572 #endif // wxUSE_THREADS
576 // We should have the wxUSE_WCHAR_T test on the _outside_
578 #if defined(__WXGTK20__)
579 // gtk+ 2.0 supports Unicode through UTF-8 strings
580 wxConvCurrent
= & wxConvUTF8
;
583 wxConvCurrent
= & wxConvLocal
;
585 #else // !wxUSE_WCHAR_T
587 wxConvCurrent
= ( wxMBConv
*) NULL
;
588 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
591 // gtk_init() wants UTF-8, not wchar_t, so convert
593 char ** argvGTK
= new char *[ argc
+ 1 ];
594 for ( i
= 0 ; i
< argc
; i
++ )
596 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
599 argvGTK
[ argc
] = NULL
;
604 init_result
= true ; // is there a _check() version of this?
605 gpe_application_init ( & argcGTK
, & argvGTK
);
607 init_result
= gtk_init_check ( & argcGTK
, & argvGTK
);
610 if ( argcGTK
!= argc
)
612 // we have to drop the parameters which were consumed by GTK+
613 for ( i
= 0 ; i
< argcGTK
; i
++ )
615 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
617 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
623 //else: gtk_init() didn't modify our parameters
626 for ( i
= 0 ; i
< argcGTK
; i
++ )
632 #else // !wxUSE_UNICODE
633 // gtk_init() shouldn't actually change argv itself (just its contents) so
634 // it's ok to pass pointer to it
635 init_result
= gtk_init_check ( & argc
, & argv
);
636 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
639 wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));
643 // we can not enter threads before gtk_init is done
646 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
653 wxSetDetectableAutoRepeat ( TRUE
);
656 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
662 void wxApp :: CleanUp ()
666 wxAppBase :: CleanUp ();
671 void wxApp :: OnAssert ( const wxChar
* file
, int line
, const wxChar
* cond
, const wxChar
* msg
)
675 wxAppBase :: OnAssert ( file
, line
, cond
, msg
);
677 m_isInAssert
= FALSE
;
680 #endif // __WXDEBUG__