]>
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"
38 #ifdef __WXUNIVERSAL__
39 #include "wx/univ/theme.h"
40 #include "wx/univ/renderer.h"
44 #include "wx/thread.h"
53 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
54 // in neither poll.h nor sys/poll.h which results in link errors later
67 // we implement poll() ourselves using select() which is supposed exist in
69 #include <sys/types.h>
72 #endif // HAVE_POLL/!HAVE_POLL
74 #include "wx/gtk/win_gtk.h"
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
83 bool g_mainThreadLocked
= FALSE
;
84 gint g_pendingTag
= 0 ;
86 static GtkWidget
* gs_RootWindow
= ( GtkWidget
*) NULL
;
88 //-----------------------------------------------------------------------------
90 //-----------------------------------------------------------------------------
94 void wxapp_install_idle_handler ();
96 //-----------------------------------------------------------------------------
98 //-----------------------------------------------------------------------------
100 // not static because used by textctrl.cpp
103 bool wxIsInsideYield
= FALSE
;
105 bool wxApp :: Yield ( bool onlyIfNeeded
)
107 if ( wxIsInsideYield
)
111 wxFAIL_MSG ( wxT ( "wxYield called recursively" ) );
118 if ( ! wxThread :: IsMain () )
120 // can't call gtk_main_iteration() from other threads like this
123 #endif // wxUSE_THREADS
125 wxIsInsideYield
= TRUE
;
129 // We need to remove idle callbacks or the loop will
131 gtk_idle_remove ( m_idleTag
);
136 // disable log flushing from here because a call to wxYield() shouldn't
137 // normally result in message boxes popping up &c
140 while ( gtk_events_pending ())
141 gtk_main_iteration ();
143 // It's necessary to call ProcessIdle() to update the frames sizes which
144 // might have been changed (it also will update other things set from
145 // OnUpdateUI() which is a nice (and desired) side effect). But we
146 // call ProcessIdle() only once since this is not meant for longish
147 // background jobs (controlled by wxIdleEvent::RequestMore() and the
148 // return value of Processidle().
151 // let the logs be flashed again
154 wxIsInsideYield
= FALSE
;
159 //-----------------------------------------------------------------------------
161 //-----------------------------------------------------------------------------
163 void wxApp :: WakeUpIdle ()
166 if (! wxThread :: IsMain ())
171 wxapp_install_idle_handler ();
174 if (! wxThread :: IsMain ())
179 //-----------------------------------------------------------------------------
181 //-----------------------------------------------------------------------------
183 // the callback functions must be extern "C" to comply with GTK+ declarations
187 static gint
wxapp_pending_callback ( gpointer
WXUNUSED ( data
) )
189 if (! wxTheApp
) return TRUE
;
191 // When getting called from GDK's time-out handler
192 // we are no longer within GDK's grab on the GUI
193 // thread so we must lock it here ourselves.
196 // Sent idle event to all who request them.
197 wxTheApp
-> ProcessPendingEvents ();
201 // Flush the logged messages if any.
203 wxLog :: FlushActive ();
206 // Release lock again
209 // Return FALSE to indicate that no more idle events are
210 // to be sent (single shot instead of continuous stream)
214 static gint
wxapp_idle_callback ( gpointer
WXUNUSED ( data
) )
220 // don't generate the idle events while the assert modal dialog is shown,
221 // this completely confuses the apps which don't expect to be reentered
222 // from some safely-looking functions
223 if ( wxTheApp
-> IsInAssert () )
225 // But repaint the assertion message if necessary
226 if ( wxTopLevelWindows
. GetCount () > 0 )
228 wxWindow
* win
= ( wxWindow
*) wxTopLevelWindows
. GetLast ()-> GetData ();
230 if ( win
-> IsKindOf ( CLASSINFO ( wxMessageDialog
)))
232 if ( win
-> IsKindOf ( CLASSINFO ( wxGenericMessageDialog
)))
234 win
-> OnInternalIdle ();
238 #endif // __WXDEBUG__
240 // When getting called from GDK's time-out handler
241 // we are no longer within GDK's grab on the GUI
242 // thread so we must lock it here ourselves.
245 // Indicate that we are now in idle mode and event handlers
246 // will have to reinstall the idle handler again.
248 wxTheApp
-> m_idleTag
= 0 ;
250 // Send idle event to all who request them as long as
251 // no events have popped up in the event queue.
252 while ( wxTheApp
-> ProcessIdle () && ( gtk_events_pending () == 0 ))
255 // Release lock again
258 // Return FALSE to indicate that no more idle events are
259 // to be sent (single shot instead of continuous stream).
267 #define wxPollFd pollfd
270 typedef GPollFD wxPollFd
;
272 int wxPoll ( wxPollFd
* ufds
, unsigned int nfds
, int timeout
)
274 // convert timeout from ms to struct timeval (s/us)
276 tv_timeout
. tv_sec
= timeout
/ 1000 ;
277 tv_timeout
. tv_usec
= ( timeout%1000
)* 1000 ;
279 // remember the highest fd used here
282 // and fill the sets for select()
291 for ( i
= 0 ; i
< nfds
; i
++ )
293 wxASSERT_MSG ( ufds
[ i
]. fd
< FD_SETSIZE
, _T ( "fd out of range" ) );
295 if ( ufds
[ i
]. events
& G_IO_IN
)
296 FD_SET ( ufds
[ i
]. fd
, & readfds
);
298 if ( ufds
[ i
]. events
& G_IO_PRI
)
299 FD_SET ( ufds
[ i
]. fd
, & exceptfds
);
301 if ( ufds
[ i
]. events
& G_IO_OUT
)
302 FD_SET ( ufds
[ i
]. fd
, & writefds
);
304 if ( ufds
[ i
]. fd
> fdMax
)
309 int res
= select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);
311 // translate the results back
312 for ( i
= 0 ; i
< nfds
; i
++ )
316 if ( FD_ISSET ( ufds
[ i
]. fd
, & readfds
) )
317 ufds
[ i
]. revents
|= G_IO_IN
;
319 if ( FD_ISSET ( ufds
[ i
]. fd
, & exceptfds
) )
320 ufds
[ i
]. revents
|= G_IO_PRI
;
322 if ( FD_ISSET ( ufds
[ i
]. fd
, & writefds
) )
323 ufds
[ i
]. revents
|= G_IO_OUT
;
329 #endif // HAVE_POLL/!HAVE_POLL
331 static gint
wxapp_poll_func ( GPollFD
* ufds
, guint nfds
, gint timeout
)
336 g_mainThreadLocked
= TRUE
;
338 // we rely on the fact that glib GPollFD struct is really just pollfd but
339 // I wonder how wise is this in the long term (VZ)
340 gint res
= wxPoll ( ( wxPollFd
*) ufds
, nfds
, timeout
);
343 g_mainThreadLocked
= FALSE
;
350 #endif // wxUSE_THREADS
354 void wxapp_install_idle_handler ()
356 // GD: this assert is raised when using the thread sample (which works)
357 // so the test is probably not so easy. Can widget callbacks be
358 // triggered from child threads and, if so, for which widgets?
359 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
361 wxASSERT_MSG ( wxTheApp
-> m_idleTag
== 0 , wxT ( "attempt to install idle handler twice" ) );
365 if ( g_pendingTag
== 0 )
366 g_pendingTag
= gtk_idle_add_priority ( 900 , wxapp_pending_callback
, ( gpointer
) NULL
);
368 // This routine gets called by all event handlers
369 // indicating that the idle is over. It may also
370 // get called from other thread for sending events
371 // to the main thread (and processing these in
372 // idle time). Very low priority.
373 wxTheApp
-> m_idleTag
= gtk_idle_add_priority ( 1000 , wxapp_idle_callback
, ( gpointer
) NULL
);
376 //-----------------------------------------------------------------------------
377 // Access to the root window global
378 //-----------------------------------------------------------------------------
380 GtkWidget
* wxGetRootWindow ()
382 if ( gs_RootWindow
== NULL
)
384 gs_RootWindow
= gtk_window_new ( GTK_WINDOW_TOPLEVEL
);
385 gtk_widget_realize ( gs_RootWindow
);
387 return gs_RootWindow
;
390 //-----------------------------------------------------------------------------
392 //-----------------------------------------------------------------------------
394 IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)
396 BEGIN_EVENT_TABLE ( wxApp
, wxEvtHandler
)
397 EVT_IDLE ( wxAppBase :: OnIdle
)
402 m_initialized
= FALSE
;
404 m_isInAssert
= FALSE
;
405 #endif // __WXDEBUG__
408 wxapp_install_idle_handler ();
411 g_main_set_poll_func ( wxapp_poll_func
);
414 m_colorCube
= ( unsigned char *) NULL
;
416 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
417 m_glVisualInfo
= ( void *) NULL
;
422 if ( m_idleTag
) gtk_idle_remove ( m_idleTag
);
424 if ( m_colorCube
) free ( m_colorCube
);
427 bool wxApp :: OnInitGui ()
429 if ( ! wxAppBase :: OnInitGui () )
432 GdkVisual
* visual
= gdk_visual_get_system ();
434 // if this is a wxGLApp (derived from wxApp), and we've already
435 // chosen a specific visual, then derive the GdkVisual from that
436 if ( m_glVisualInfo
!= NULL
)
439 // seems gtk_widget_set_default_visual no longer exists?
440 GdkVisual
* vis
= gtk_widget_get_default_visual ();
442 GdkVisual
* vis
= gdkx_visual_get (
443 (( XVisualInfo
*) m_glVisualInfo
) -> visualid
);
444 gtk_widget_set_default_visual ( vis
);
447 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
448 gtk_widget_set_default_colormap ( colormap
);
453 // On some machines, the default visual is just 256 colours, so
454 // we make sure we get the best. This can sometimes be wasteful.
457 if (( gdk_visual_get_best () != gdk_visual_get_system ()) && ( m_useBestVisual
))
460 /* seems gtk_widget_set_default_visual no longer exists? */
461 GdkVisual
* vis
= gtk_widget_get_default_visual ();
463 GdkVisual
* vis
= gdk_visual_get_best ();
464 gtk_widget_set_default_visual ( vis
);
467 GdkColormap
* colormap
= gdk_colormap_new ( vis
, FALSE
);
468 gtk_widget_set_default_colormap ( colormap
);
473 // Nothing to do for 15, 16, 24, 32 bit displays
474 if ( visual
-> depth
> 8 ) return TRUE
;
476 // initialize color cube for 8-bit color reduction dithering
478 GdkColormap
* cmap
= gtk_widget_get_default_colormap ();
480 m_colorCube
= ( unsigned char *) malloc ( 32 * 32 * 32 );
482 for ( int r
= 0 ; r
< 32 ; r
++)
484 for ( int g
= 0 ; g
< 32 ; g
++)
486 for ( int b
= 0 ; b
< 32 ; b
++)
488 int rr
= ( r
<< 3 ) | ( r
>> 2 );
489 int gg
= ( g
<< 3 ) | ( g
>> 2 );
490 int bb
= ( b
<< 3 ) | ( b
>> 2 );
494 GdkColor
* colors
= cmap
-> colors
;
499 for ( int i
= 0 ; i
< cmap
-> size
; i
++)
501 int rdiff
= (( rr
<< 8 ) - colors
[ i
]. red
);
502 int gdiff
= (( gg
<< 8 ) - colors
[ i
]. green
);
503 int bdiff
= (( bb
<< 8 ) - colors
[ i
]. blue
);
504 int sum
= ABS ( rdiff
) + ABS ( gdiff
) + ABS ( bdiff
);
507 index
= i
; max
= sum
;
513 // assume 8-bit true or static colors. this really exists
514 GdkVisual
* vis
= gdk_colormap_get_visual ( cmap
);
515 index
= ( r
>> ( 5 - vis
-> red_prec
)) << vis
-> red_shift
;
516 index
|= ( g
>> ( 5 - vis
-> green_prec
)) << vis
-> green_shift
;
517 index
|= ( b
>> ( 5 - vis
-> blue_prec
)) << vis
-> blue_shift
;
519 m_colorCube
[ ( r
* 1024 ) + ( g
* 32 ) + b
] = index
;
527 GdkVisual
* wxApp :: GetGdkVisual ()
529 GdkVisual
* visual
= NULL
;
532 visual
= gdkx_visual_get ( (( XVisualInfo
*) m_glVisualInfo
)-> visualid
);
534 visual
= gdk_window_get_visual ( wxGetRootWindow ()-> window
);
541 int wxApp :: MainLoop ()
549 // VZ: no idea why is it different from ExitMainLoop() but this is what
550 // wxExit() used to do
554 void wxApp :: ExitMainLoop ()
556 if ( gtk_main_level () > 0 )
560 bool wxApp :: Initialized ()
562 return m_initialized
;
565 bool wxApp :: Pending ()
567 return ( gtk_events_pending () > 0 );
570 bool wxApp :: Dispatch ()
572 gtk_main_iteration ();
577 bool wxApp :: Initialize ( int & argc
, wxChar
** argv
)
580 // GTK 1.2 up to version 1.2.3 has broken threads
581 if (( gtk_major_version
== 1 ) &&
582 ( gtk_minor_version
== 2 ) &&
583 ( gtk_micro_version
< 4 ))
585 printf ( "wxWindows warning: GUI threading disabled due to outdated GTK version \n " );
589 if (! g_thread_supported ())
592 #endif // wxUSE_THREADS
596 // We should have the wxUSE_WCHAR_T test on the _outside_
598 #if defined(__WXGTK20__)
599 // gtk+ 2.0 supports Unicode through UTF-8 strings
600 wxConvCurrent
= & wxConvUTF8
;
603 wxConvCurrent
= & wxConvLocal
;
605 #else // !wxUSE_WCHAR_T
607 wxConvCurrent
= ( wxMBConv
*) NULL
;
608 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
611 // gtk_init() wants UTF-8, not wchar_t, so convert
613 char ** argvGTK
= new char *[ argc
+ 1 ];
614 for ( i
= 0 ; i
< argc
; i
++ )
616 argvGTK
[ i
] = wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));
619 argvGTK
[ argc
] = NULL
;
622 gtk_init ( & argcGTK
, & argvGTK
);
624 if ( argcGTK
!= argc
)
626 // we have to drop the parameters which were consumed by GTK+
627 for ( i
= 0 ; i
< argcGTK
; i
++ )
629 while ( strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]), argvGTK
[ i
]) != 0 )
631 memmove ( argv
+ i
, argv
+ i
+ 1 , argc
- i
);
637 //else: gtk_init() didn't modify our parameters
640 for ( i
= 0 ; i
< argcGTK
; i
++ )
646 #else // !wxUSE_UNICODE
647 // gtk_init() shouldn't actually change argv itself (just its contents) so
648 // it's ok to pass pointer to it
649 gtk_init ( & argc
, & argv
);
650 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
652 // we can not enter threads before gtk_init is done
655 if ( ! wxAppBase :: Initialize ( argc
, argv
) )
662 wxSetDetectableAutoRepeat ( TRUE
);
665 wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());
673 void wxApp :: CleanUp ()
677 wxAppBase :: CleanUp ();
682 void wxApp :: OnAssert ( const wxChar
* file
, int line
, const wxChar
* cond
, const wxChar
* msg
)
686 wxAppBase :: OnAssert ( file
, line
, cond
, msg
);
688 m_isInAssert
= FALSE
;
691 #endif // __WXDEBUG__