]>
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 void wxApp :: WakeUpIdle ()
170 if (! wxThread :: IsMain ())
175 wxapp_install_idle_handler ();
178 if (! wxThread :: IsMain ())
183 //-----------------------------------------------------------------------------
185 //-----------------------------------------------------------------------------
187 // the callback functions must be extern "C" to comply with GTK+ declarations
191 static gint
wxapp_pending_callback ( gpointer
WXUNUSED ( data
) )
193 if (! wxTheApp
) return TRUE
;
195 // When getting called from GDK's time-out handler
196 // we are no longer within GDK's grab on the GUI
197 // thread so we must lock it here ourselves.
200 // Sent idle event to all who request them.
201 wxTheApp
-> ProcessPendingEvents ();
205 // Flush the logged messages if any.
207 wxLog :: FlushActive ();
210 // Release lock again
213 // Return FALSE to indicate that no more idle events are
214 // to be sent (single shot instead of continuous stream)
218 static gint
wxapp_idle_callback ( gpointer
WXUNUSED ( data
) )
224 // don't generate the idle events while the assert modal dialog is shown,
225 // this completely confuses the apps which don't expect to be reentered
226 // from some safely-looking functions
227 if ( wxTheApp
-> IsInAssert () )
229 // But repaint the assertion message if necessary
230 if ( wxTopLevelWindows
. GetCount () > 0 )
232 wxWindow
* win
= ( wxWindow
*) wxTopLevelWindows
. GetLast ()-> GetData ();
234 if ( win
-> IsKindOf ( CLASSINFO ( wxMessageDialog
)))
236 if ( win
-> IsKindOf ( CLASSINFO ( wxGenericMessageDialog
)))
238 win
-> OnInternalIdle ();
242 #endif // __WXDEBUG__
244 // When getting called from GDK's time-out handler
245 // we are no longer within GDK's grab on the GUI
246 // thread so we must lock it here ourselves.
249 // Indicate that we are now in idle mode and event handlers
250 // will have to reinstall the idle handler again.
252 wxTheApp
-> m_idleTag
= 0 ;
254 // Send idle event to all who request them as long as
255 // no events have popped up in the event queue.
256 while ( wxTheApp
-> ProcessIdle () && ( gtk_events_pending () == 0 ))
259 // Release lock again
262 // Return FALSE to indicate that no more idle events are
263 // to be sent (single shot instead of continuous stream).
271 #define wxPollFd pollfd
274 typedef GPollFD wxPollFd
;
276 int wxPoll ( wxPollFd
* ufds
, unsigned int nfds
, int timeout
)
278 // convert timeout from ms to struct timeval (s/us)
280 tv_timeout
. tv_sec
= timeout
/ 1000 ;
281 tv_timeout
. tv_usec
= ( timeout%1000
)* 1000 ;
283 // remember the highest fd used here
286 // and fill the sets for select()
295 for ( i
= 0 ; i
< nfds
; i
++ )
297 wxASSERT_MSG ( ufds
[ i
]. fd
< FD_SETSIZE
, _T ( "fd out of range" ) );
299 if ( ufds
[ i
]. events
& G_IO_IN
)
300 FD_SET ( ufds
[ i
]. fd
, & readfds
);
302 if ( ufds
[ i
]. events
& G_IO_PRI
)
303 FD_SET ( ufds
[ i
]. fd
, & exceptfds
);
305 if ( ufds
[ i
]. events
& G_IO_OUT
)
306 FD_SET ( ufds
[ i
]. fd
, & writefds
);
308 if ( ufds
[ i
]. fd
> fdMax
)
313 int res
= select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);
315 // translate the results back
316 for ( i
= 0 ; i
< nfds
; i
++ )
320 if ( FD_ISSET ( ufds
[ i
]. fd
, & readfds
) )
321 ufds
[ i
]. revents
|= G_IO_IN
;
323 if ( FD_ISSET ( ufds
[ i
]. fd
, & exceptfds
) )
324 ufds
[ i
]. revents
|= G_IO_PRI
;
326 if ( FD_ISSET ( ufds
[ i
]. fd
, & writefds
) )
327 ufds
[ i
]. revents
|= G_IO_OUT
;
333 #endif // HAVE_POLL/!HAVE_POLL
335 static gint
wxapp_poll_func ( GPollFD
* ufds
, guint nfds
, gint timeout
)
340 g_mainThreadLocked
= TRUE
;
342 // we rely on the fact that glib GPollFD struct is really just pollfd but
343 // I wonder how wise is this in the long term (VZ)
344 gint res
= wxPoll ( ( wxPollFd
*) ufds
, nfds
, timeout
);
347 g_mainThreadLocked
= FALSE
;
354 #endif // wxUSE_THREADS
358 void wxapp_install_idle_handler ()
360 // GD: this assert is raised when using the thread sample (which works)
361 // so the test is probably not so easy. Can widget callbacks be
362 // triggered from child threads and, if so, for which widgets?
363 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
365 wxASSERT_MSG ( wxTheApp
-> m_idleTag
== 0 , wxT ( "attempt to install idle handler twice" ) );
369 if ( g_pendingTag
== 0 )
370 g_pendingTag
= gtk_idle_add_priority ( 900 , wxapp_pending_callback
, ( gpointer
) NULL
);
372 // This routine gets called by all event handlers
373 // indicating that the idle is over. It may also
374 // get called from other thread for sending events
375 // to the main thread (and processing these in
376 // idle time). Very low priority.
377 wxTheApp
-> m_idleTag
= gtk_idle_add_priority ( 1000 , wxapp_idle_callback
, ( gpointer
) NULL
);
380 //-----------------------------------------------------------------------------
381 // Access to the root window global
382 //-----------------------------------------------------------------------------
384 GtkWidget
* wxGetRootWindow ()
386 if ( gs_RootWindow
== NULL
)
388 gs_RootWindow
= gtk_window_new ( GTK_WINDOW_TOPLEVEL
);
389 gtk_widget_realize ( gs_RootWindow
);
391 return gs_RootWindow
;
394 //-----------------------------------------------------------------------------
396 //-----------------------------------------------------------------------------
398 IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)
400 BEGIN_EVENT_TABLE ( wxApp
, wxEvtHandler
)
401 EVT_IDLE ( wxAppBase :: OnIdle
)
407 m_isInAssert
= FALSE
;
408 #endif // __WXDEBUG__
411 wxapp_install_idle_handler ();
414 g_main_set_poll_func ( wxapp_poll_func
);
417 m_colorCube
= ( unsigned char *) NULL
;
419 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
420 m_glVisualInfo
= ( void *) NULL
;
425 if ( m_idleTag
) gtk_idle_remove ( m_idleTag
);
427 if ( m_colorCube
) free ( m_colorCube
);
430 bool wxApp :: OnInitGui ()
432 if ( ! wxAppBase :: OnInitGui () )
435 GdkVisual
* visual
= gdk_visual_get_system ();
437 // if this is a wxGLApp (derived from wxApp), and we've already
438 // chosen a specific visual, then derive the GdkVisual from that
439 if ( m_glVisualInfo
!= NULL
)
442 // seems gtk_widget_set_default_visual no longer exists?
443 GdkVisual
* vis
= gtk_widget_get_default_visual ();
445 GdkVisual
* vis
= gdkx_visual_get (
446 (( XVisualInfo
*) m_glVisualInfo
) -> visualid
);
447 gtk_widget_set_default_visual ( vis
);
450 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
451 gtk_widget_set_default_colormap ( colormap
);
456 // On some machines, the default visual is just 256 colours, so
457 // we make sure we get the best. This can sometimes be wasteful.
460 if (( gdk_visual_get_best () != gdk_visual_get_system ()) && ( m_useBestVisual
))
463 /* seems gtk_widget_set_default_visual no longer exists? */
464 GdkVisual
* vis
= gtk_widget_get_default_visual ();
466 GdkVisual
* vis
= gdk_visual_get_best ();
467 gtk_widget_set_default_visual ( vis
);
470 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
471 gtk_widget_set_default_colormap ( colormap
);
476 // Nothing to do for 15, 16, 24, 32 bit displays
477 if ( visual
-> depth
> 8 ) return TRUE
;
479 // initialize color cube for 8-bit color reduction dithering
481 GdkColormap
* cmap
= gtk_widget_get_default_colormap ();
483 m_colorCube
= ( unsigned char *) malloc ( 32 * 32 * 32 );
485 for ( int r
= 0 ; r
< 32 ; r
++)
487 for ( int g
= 0 ; g
< 32 ; g
++)
489 for ( int b
= 0 ; b
< 32 ; b
++)
491 int rr
= ( r
<< 3 ) | ( r
>> 2 );
492 int gg
= ( g
<< 3 ) | ( g
>> 2 );
493 int bb
= ( b
<< 3 ) | ( b
>> 2 );
497 GdkColor
* colors
= cmap
-> colors
;
502 for ( int i
= 0 ; i
< cmap
-> size
; i
++)
504 int rdiff
= (( rr
<< 8 ) - colors
[ i
]. red
);
505 int gdiff
= (( gg
<< 8 ) - colors
[ i
]. green
);
506 int bdiff
= (( bb
<< 8 ) - colors
[ i
]. blue
);
507 int sum
= ABS ( rdiff
) + ABS ( gdiff
) + ABS ( bdiff
);
510 index
= i
; max
= sum
;
516 // assume 8-bit true or static colors. this really exists
517 GdkVisual
* vis
= gdk_colormap_get_visual ( cmap
);
518 index
= ( r
>> ( 5 - vis
-> red_prec
)) << vis
-> red_shift
;
519 index
|= ( g
>> ( 5 - vis
-> green_prec
)) << vis
-> green_shift
;
520 index
|= ( b
>> ( 5 - vis
-> blue_prec
)) << vis
-> blue_shift
;
522 m_colorCube
[ ( r
* 1024 ) + ( g
* 32 ) + b
] = index
;
530 GdkVisual
* wxApp :: GetGdkVisual ()
532 GdkVisual
* visual
= NULL
;
535 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
537 visual
= gdk_window_get_visual ( wxGetRootWindow ()-> window
);
544 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
549 // GTK 1.2 up to version 1.2.3 has broken threads
550 if (( gtk_major_version
== 1 ) &&
551 ( gtk_minor_version
== 2 ) &&
552 ( gtk_micro_version
< 4 ))
554 printf ( "wxWidgets warning: GUI threading disabled due to outdated GTK version \n " );
558 if (! g_thread_supported ())
561 #endif // wxUSE_THREADS
565 // We should have the wxUSE_WCHAR_T test on the _outside_
567 #if defined(__WXGTK20__)
568 // gtk+ 2.0 supports Unicode through UTF-8 strings
569 wxConvCurrent
= & wxConvUTF8
;
572 wxConvCurrent
= & wxConvLocal
;
574 #else // !wxUSE_WCHAR_T
576 wxConvCurrent
= ( wxMBConv
*) NULL
;
577 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
580 // gtk_init() wants UTF-8, not wchar_t, so convert
582 char ** argvGTK
= new char *[ argc
+ 1 ];
583 for ( i
= 0 ; i
< argc
; i
++ )
585 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
588 argvGTK
[ argc
] = NULL
;
593 init_result
= true ; // is there a _check() version of this?
594 gpe_application_init ( & argcGTK
, & argvGTK
);
596 init_result
= gtk_init_check ( & argcGTK
, & argvGTK
);
599 if ( argcGTK
!= argc
)
601 // we have to drop the parameters which were consumed by GTK+
602 for ( i
= 0 ; i
< argcGTK
; i
++ )
604 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
606 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
612 //else: gtk_init() didn't modify our parameters
615 for ( i
= 0 ; i
< argcGTK
; i
++ )
621 #else // !wxUSE_UNICODE
622 // gtk_init() shouldn't actually change argv itself (just its contents) so
623 // it's ok to pass pointer to it
624 init_result
= gtk_init_check ( & argc
, & argv
);
625 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
628 wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));
632 // we can not enter threads before gtk_init is done
635 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
642 wxSetDetectableAutoRepeat ( TRUE
);
645 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
653 void wxApp :: CleanUp ()
657 wxAppBase :: CleanUp ();
662 void wxApp :: OnAssert ( const wxChar
* file
, int line
, const wxChar
* cond
, const wxChar
* msg
)
666 wxAppBase :: OnAssert ( file
, line
, cond
, msg
);
668 m_isInAssert
= FALSE
;
671 #endif // __WXDEBUG__