]>
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 /////////////////////////////////////////////////////////////////////////////
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 #if wxUSE_LIBGNOMEPRINT
85 #include "wx/html/forcelnk.h"
86 FORCE_LINK ( gnome_print
)
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
94 bool g_mainThreadLocked
= FALSE
;
95 gint g_pendingTag
= 0 ;
97 static GtkWidget
* gs_RootWindow
= ( GtkWidget
*) NULL
;
99 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
103 extern bool g_isIdle
;
105 void wxapp_install_idle_handler ();
108 static wxMutex gs_idleTagsMutex
;
111 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
115 // not static because used by textctrl.cpp
118 bool wxIsInsideYield
= FALSE
;
120 bool wxApp :: Yield ( bool onlyIfNeeded
)
122 if ( wxIsInsideYield
)
126 wxFAIL_MSG ( wxT ( "wxYield called recursively" ) );
133 if ( ! wxThread :: IsMain () )
135 // can't call gtk_main_iteration() from other threads like this
138 #endif // wxUSE_THREADS
140 wxIsInsideYield
= TRUE
;
142 // We need to remove idle callbacks or the loop will
144 wxTheApp
-> RemoveIdleTag ();
146 // disable log flushing from here because a call to wxYield() shouldn't
147 // 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().
161 // let the logs be flashed again
164 wxIsInsideYield
= FALSE
;
169 //-----------------------------------------------------------------------------
171 //-----------------------------------------------------------------------------
173 // RR/KH: The wxMutexGui calls are not needed on GTK2 according to
174 // the GTK faq, http://www.gtk.org/faq/#AEN500
175 // The calls to gdk_threads_enter() and leave() are specifically noted
176 // as not being necessary. The MutexGui calls are still left in for GTK1.
177 // Eliminating the MutexGui calls fixes the long-standing "random" lockup
178 // when using wxPostEvent (which calls WakeUpIdle) from a thread.
180 void wxApp :: WakeUpIdle ()
184 if (! wxThread :: IsMain ())
186 #endif // wxUSE_THREADS_
189 wxapp_install_idle_handler ();
193 if (! wxThread :: IsMain ())
195 #endif // wxUSE_THREADS_
199 //-----------------------------------------------------------------------------
201 //-----------------------------------------------------------------------------
203 // the callback functions must be extern "C" to comply with GTK+ declarations
207 static gint
wxapp_pending_callback ( gpointer
WXUNUSED ( data
) )
209 if (! wxTheApp
) return TRUE
;
211 // When getting called from GDK's time-out handler
212 // we are no longer within GDK's grab on the GUI
213 // thread so we must lock it here ourselves.
216 // Sent idle event to all who request them.
217 wxTheApp
-> ProcessPendingEvents ();
221 wxMutexLocker
lock ( gs_idleTagsMutex
);
226 // Flush the logged messages if any.
228 wxLog :: FlushActive ();
231 // Release lock again
234 // Return FALSE to indicate that no more idle events are
235 // to be sent (single shot instead of continuous stream)
239 static gint
wxapp_idle_callback ( gpointer
WXUNUSED ( data
) )
245 // don't generate the idle events while the assert modal dialog is shown,
246 // this completely confuses the apps which don't expect to be reentered
247 // from some safely-looking functions
248 if ( wxTheApp
-> IsInAssert () )
250 // But repaint the assertion message if necessary
251 if ( wxTopLevelWindows
. GetCount () > 0 )
253 wxWindow
* win
= ( wxWindow
*) wxTopLevelWindows
. GetLast ()-> GetData ();
255 if ( win
-> IsKindOf ( CLASSINFO ( wxMessageDialog
)))
257 if ( win
-> IsKindOf ( CLASSINFO ( wxGenericMessageDialog
)))
259 win
-> OnInternalIdle ();
263 #endif // __WXDEBUG__
265 // When getting called from GDK's time-out handler
266 // we are no longer within GDK's grab on the GUI
267 // thread so we must lock it here ourselves.
270 // Indicate that we are now in idle mode and event handlers
271 // will have to reinstall the idle handler again.
274 wxMutexLocker
lock ( gs_idleTagsMutex
);
277 wxTheApp
-> m_idleTag
= 0 ;
280 // Send idle event to all who request them as long as
281 // no events have popped up in the event queue.
282 while ( wxTheApp
-> ProcessIdle () && ( gtk_events_pending () == 0 ))
285 // Release lock again
288 // Return FALSE to indicate that no more idle events are
289 // to be sent (single shot instead of continuous stream).
297 #define wxPollFd pollfd
300 typedef GPollFD wxPollFd
;
302 int wxPoll ( wxPollFd
* ufds
, unsigned int nfds
, int timeout
)
304 // convert timeout from ms to struct timeval (s/us)
306 tv_timeout
. tv_sec
= timeout
/ 1000 ;
307 tv_timeout
. tv_usec
= ( timeout%1000
)* 1000 ;
309 // remember the highest fd used here
312 // and fill the sets for select()
321 for ( i
= 0 ; i
< nfds
; i
++ )
323 wxASSERT_MSG ( ufds
[ i
]. fd
< FD_SETSIZE
, _T ( "fd out of range" ) );
325 if ( ufds
[ i
]. events
& G_IO_IN
)
326 FD_SET ( ufds
[ i
]. fd
, & readfds
);
328 if ( ufds
[ i
]. events
& G_IO_PRI
)
329 FD_SET ( ufds
[ i
]. fd
, & exceptfds
);
331 if ( ufds
[ i
]. events
& G_IO_OUT
)
332 FD_SET ( ufds
[ i
]. fd
, & writefds
);
334 if ( ufds
[ i
]. fd
> fdMax
)
339 int res
= select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);
341 // translate the results back
342 for ( i
= 0 ; i
< nfds
; i
++ )
346 if ( FD_ISSET ( ufds
[ i
]. fd
, & readfds
) )
347 ufds
[ i
]. revents
|= G_IO_IN
;
349 if ( FD_ISSET ( ufds
[ i
]. fd
, & exceptfds
) )
350 ufds
[ i
]. revents
|= G_IO_PRI
;
352 if ( FD_ISSET ( ufds
[ i
]. fd
, & writefds
) )
353 ufds
[ i
]. revents
|= G_IO_OUT
;
359 #endif // HAVE_POLL/!HAVE_POLL
361 static gint
wxapp_poll_func ( GPollFD
* ufds
, guint nfds
, gint timeout
)
366 g_mainThreadLocked
= TRUE
;
368 // we rely on the fact that glib GPollFD struct is really just pollfd but
369 // I wonder how wise is this in the long term (VZ)
370 gint res
= wxPoll ( ( wxPollFd
*) ufds
, nfds
, timeout
);
373 g_mainThreadLocked
= FALSE
;
380 #endif // wxUSE_THREADS
384 void wxapp_install_idle_handler ()
387 wxMutexLocker
lock ( gs_idleTagsMutex
);
390 // Don't install the handler if it's already installed. This test *MUST*
391 // be done when gs_idleTagsMutex is locked!
395 // GD: this assert is raised when using the thread sample (which works)
396 // so the test is probably not so easy. Can widget callbacks be
397 // triggered from child threads and, if so, for which widgets?
398 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
400 wxASSERT_MSG ( wxTheApp
-> m_idleTag
== 0 , wxT ( "attempt to install idle handler twice" ) );
404 if ( g_pendingTag
== 0 )
405 g_pendingTag
= gtk_idle_add_priority ( 900 , wxapp_pending_callback
, ( gpointer
) NULL
);
407 // This routine gets called by all event handlers
408 // indicating that the idle is over. It may also
409 // get called from other thread for sending events
410 // to the main thread (and processing these in
411 // idle time). Very low priority.
412 wxTheApp
-> m_idleTag
= gtk_idle_add_priority ( 1000 , wxapp_idle_callback
, ( gpointer
) NULL
);
415 //-----------------------------------------------------------------------------
416 // Access to the root window global
417 //-----------------------------------------------------------------------------
419 GtkWidget
* wxGetRootWindow ()
421 if ( gs_RootWindow
== NULL
)
423 gs_RootWindow
= gtk_window_new ( GTK_WINDOW_TOPLEVEL
);
424 gtk_widget_realize ( gs_RootWindow
);
426 return gs_RootWindow
;
429 //-----------------------------------------------------------------------------
431 //-----------------------------------------------------------------------------
433 IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)
435 BEGIN_EVENT_TABLE ( wxApp
, wxEvtHandler
)
436 EVT_IDLE ( wxAppBase :: OnIdle
)
442 m_isInAssert
= FALSE
;
443 #endif // __WXDEBUG__
447 wxapp_install_idle_handler ();
450 g_main_set_poll_func ( wxapp_poll_func
);
453 m_colorCube
= ( unsigned char *) NULL
;
455 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
456 m_glVisualInfo
= ( void *) NULL
;
461 if ( m_idleTag
) gtk_idle_remove ( m_idleTag
);
463 if ( m_colorCube
) free ( m_colorCube
);
466 bool wxApp :: OnInitGui ()
468 if ( ! wxAppBase :: OnInitGui () )
471 GdkVisual
* visual
= gdk_visual_get_system ();
473 // if this is a wxGLApp (derived from wxApp), and we've already
474 // chosen a specific visual, then derive the GdkVisual from that
475 if ( m_glVisualInfo
!= NULL
)
478 // seems gtk_widget_set_default_visual no longer exists?
479 GdkVisual
* vis
= gtk_widget_get_default_visual ();
481 GdkVisual
* vis
= gdkx_visual_get (
482 (( XVisualInfo
*) m_glVisualInfo
) -> visualid
);
483 gtk_widget_set_default_visual ( vis
);
486 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
487 gtk_widget_set_default_colormap ( colormap
);
492 // On some machines, the default visual is just 256 colours, so
493 // we make sure we get the best. This can sometimes be wasteful.
496 if (( gdk_visual_get_best () != gdk_visual_get_system ()) && ( m_useBestVisual
))
499 /* seems gtk_widget_set_default_visual no longer exists? */
500 GdkVisual
* vis
= gtk_widget_get_default_visual ();
502 GdkVisual
* vis
= gdk_visual_get_best ();
503 gtk_widget_set_default_visual ( vis
);
506 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
507 gtk_widget_set_default_colormap ( colormap
);
512 // Nothing to do for 15, 16, 24, 32 bit displays
513 if ( visual
-> depth
> 8 ) return TRUE
;
515 // initialize color cube for 8-bit color reduction dithering
517 GdkColormap
* cmap
= gtk_widget_get_default_colormap ();
519 m_colorCube
= ( unsigned char *) malloc ( 32 * 32 * 32 );
521 for ( int r
= 0 ; r
< 32 ; r
++)
523 for ( int g
= 0 ; g
< 32 ; g
++)
525 for ( int b
= 0 ; b
< 32 ; b
++)
527 int rr
= ( r
<< 3 ) | ( r
>> 2 );
528 int gg
= ( g
<< 3 ) | ( g
>> 2 );
529 int bb
= ( b
<< 3 ) | ( b
>> 2 );
533 GdkColor
* colors
= cmap
-> colors
;
538 for ( int i
= 0 ; i
< cmap
-> size
; i
++)
540 int rdiff
= (( rr
<< 8 ) - colors
[ i
]. red
);
541 int gdiff
= (( gg
<< 8 ) - colors
[ i
]. green
);
542 int bdiff
= (( bb
<< 8 ) - colors
[ i
]. blue
);
543 int sum
= ABS ( rdiff
) + ABS ( gdiff
) + ABS ( bdiff
);
546 index
= i
; max
= sum
;
552 // assume 8-bit true or static colors. this really exists
553 GdkVisual
* vis
= gdk_colormap_get_visual ( cmap
);
554 index
= ( r
>> ( 5 - vis
-> red_prec
)) << vis
-> red_shift
;
555 index
|= ( g
>> ( 5 - vis
-> green_prec
)) << vis
-> green_shift
;
556 index
|= ( b
>> ( 5 - vis
-> blue_prec
)) << vis
-> blue_shift
;
558 m_colorCube
[ ( r
* 1024 ) + ( g
* 32 ) + b
] = index
;
566 GdkVisual
* wxApp :: GetGdkVisual ()
568 GdkVisual
* visual
= NULL
;
571 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
573 visual
= gdk_window_get_visual ( wxGetRootWindow ()-> window
);
580 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
585 // GTK 1.2 up to version 1.2.3 has broken threads
586 if (( gtk_major_version
== 1 ) &&
587 ( gtk_minor_version
== 2 ) &&
588 ( gtk_micro_version
< 4 ))
590 printf ( "wxWidgets warning: GUI threading disabled due to outdated GTK version \n " );
594 if (! g_thread_supported ())
597 #endif // wxUSE_THREADS
601 // We should have the wxUSE_WCHAR_T test on the _outside_
603 #if defined(__WXGTK20__)
604 // gtk+ 2.0 supports Unicode through UTF-8 strings
605 wxConvCurrent
= & wxConvUTF8
;
608 wxConvCurrent
= & wxConvLocal
;
610 #else // !wxUSE_WCHAR_T
612 wxConvCurrent
= ( wxMBConv
*) NULL
;
613 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
616 // gtk_init() wants UTF-8, not wchar_t, so convert
618 char ** argvGTK
= new char *[ argc
+ 1 ];
619 for ( i
= 0 ; i
< argc
; i
++ )
621 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
624 argvGTK
[ argc
] = NULL
;
629 init_result
= true ; // is there a _check() version of this?
630 gpe_application_init ( & argcGTK
, & argvGTK
);
632 init_result
= gtk_init_check ( & argcGTK
, & argvGTK
);
635 if ( argcGTK
!= argc
)
637 // we have to drop the parameters which were consumed by GTK+
638 for ( i
= 0 ; i
< argcGTK
; i
++ )
640 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
642 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
648 //else: gtk_init() didn't modify our parameters
651 for ( i
= 0 ; i
< argcGTK
; i
++ )
657 #else // !wxUSE_UNICODE
658 // gtk_init() shouldn't actually change argv itself (just its contents) so
659 // it's ok to pass pointer to it
660 init_result
= gtk_init_check ( & argc
, & argv
);
661 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
664 wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));
668 // we can not enter threads before gtk_init is done
671 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
678 wxSetDetectableAutoRepeat ( TRUE
);
681 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
687 void wxApp :: CleanUp ()
691 wxAppBase :: CleanUp ();
696 void wxApp :: OnAssert ( const wxChar
* file
, int line
, const wxChar
* cond
, const wxChar
* msg
)
700 wxAppBase :: OnAssert ( file
, line
, cond
, msg
);
702 m_isInAssert
= FALSE
;
705 #endif // __WXDEBUG__
707 void wxApp :: RemoveIdleTag ()
710 wxMutexLocker
lock ( gs_idleTagsMutex
);
714 gtk_idle_remove ( wxTheApp
-> m_idleTag
);
715 wxTheApp
-> m_idleTag
= 0 ;