1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 // vms_jackets.h should for proper working be included before anything else
12 # include <vms_jackets.h>
13 #undef ConnectionNumber
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #include "wx/gdicmn.h"
24 #include "wx/memory.h"
26 #include "wx/settings.h"
27 #include "wx/dialog.h"
28 #include "wx/msgdlg.h"
30 #include "wx/filename.h"
31 #include "wx/module.h"
33 #include "wx/thread.h"
39 #ifdef __WXUNIVERSAL__
40 #include "wx/univ/theme.h"
41 #include "wx/univ/renderer.h"
45 #include "wx/thread.h"
54 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
55 // in neither poll.h nor sys/poll.h which results in link errors later
68 // we implement poll() ourselves using select() which is supposed exist in
70 #include <sys/types.h>
73 #endif // HAVE_POLL/!HAVE_POLL
75 #include "wx/unix/private.h"
76 #include "wx/gtk/win_gtk.h"
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
84 bool g_mainThreadLocked
= FALSE
;
85 gint g_pendingTag
= 0;
87 static GtkWidget
*gs_RootWindow
= (GtkWidget
*) NULL
;
89 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
95 void wxapp_install_idle_handler();
98 static wxMutex gs_idleTagsMutex
;
101 //-----------------------------------------------------------------------------
103 //-----------------------------------------------------------------------------
105 // not static because used by textctrl.cpp
108 bool wxIsInsideYield
= FALSE
;
110 bool wxApp::Yield(bool onlyIfNeeded
)
112 if ( wxIsInsideYield
)
116 wxFAIL_MSG( wxT("wxYield called recursively" ) );
123 if ( !wxThread::IsMain() )
125 // can't call gtk_main_iteration() from other threads like this
128 #endif // wxUSE_THREADS
130 wxIsInsideYield
= TRUE
;
132 // We need to remove idle callbacks or the loop will
134 wxTheApp
->RemoveIdleTag();
137 // disable log flushing from here because a call to wxYield() shouldn't
138 // normally result in message boxes popping up &c
142 while (gtk_events_pending())
143 gtk_main_iteration();
145 // It's necessary to call ProcessIdle() to update the frames sizes which
146 // might have been changed (it also will update other things set from
147 // OnUpdateUI() which is a nice (and desired) side effect). But we
148 // call ProcessIdle() only once since this is not meant for longish
149 // background jobs (controlled by wxIdleEvent::RequestMore() and the
150 // return value of Processidle().
154 // 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_
183 wxapp_install_idle_handler();
187 if (!wxThread::IsMain())
189 #endif // wxUSE_THREADS_
193 //-----------------------------------------------------------------------------
195 //-----------------------------------------------------------------------------
197 // the callback functions must be extern "C" to comply with GTK+ declarations
201 static gint
wxapp_pending_callback( gpointer
WXUNUSED(data
) )
203 if (!wxTheApp
) return TRUE
;
205 // When getting called from GDK's time-out handler
206 // we are no longer within GDK's grab on the GUI
207 // thread so we must lock it here ourselves.
210 // Sent idle event to all who request them.
211 wxTheApp
->ProcessPendingEvents();
215 wxMutexLocker
lock(gs_idleTagsMutex
);
220 // Flush the logged messages if any.
222 wxLog::FlushActive();
225 // Release lock again
228 // Return FALSE to indicate that no more idle events are
229 // to be sent (single shot instead of continuous stream)
233 static gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
239 // don't generate the idle events while the assert modal dialog is shown,
240 // this completely confuses the apps which don't expect to be reentered
241 // from some safely-looking functions
242 if ( wxTheApp
->IsInAssert() )
244 // But repaint the assertion message if necessary
245 if (wxTopLevelWindows
.GetCount() > 0)
247 wxWindow
* win
= (wxWindow
*) wxTopLevelWindows
.GetLast()->GetData();
249 if (win
->IsKindOf(CLASSINFO(wxMessageDialog
)))
251 if (win
->IsKindOf(CLASSINFO(wxGenericMessageDialog
)))
253 win
->OnInternalIdle();
257 #endif // __WXDEBUG__
259 // When getting called from GDK's time-out handler
260 // we are no longer within GDK's grab on the GUI
261 // thread so we must lock it here ourselves.
264 // Indicate that we are now in idle mode and event handlers
265 // will have to reinstall the idle handler again.
268 wxMutexLocker
lock(gs_idleTagsMutex
);
271 wxTheApp
->m_idleTag
= 0;
274 // Send idle event to all who request them as long as
275 // no events have popped up in the event queue.
276 while (wxTheApp
->ProcessIdle() && (gtk_events_pending() == 0))
279 // Release lock again
282 // Return FALSE to indicate that no more idle events are
283 // to be sent (single shot instead of continuous stream).
291 #define wxPollFd pollfd
294 typedef GPollFD wxPollFd
;
296 int wxPoll(wxPollFd
*ufds
, unsigned int nfds
, int timeout
)
298 // convert timeout from ms to struct timeval (s/us)
300 tv_timeout
.tv_sec
= timeout
/1000;
301 tv_timeout
.tv_usec
= (timeout%1000
)*1000;
303 // remember the highest fd used here
306 // and fill the sets for select()
311 wxFD_ZERO(&writefds
);
312 wxFD_ZERO(&exceptfds
);
315 for ( i
= 0; i
< nfds
; i
++ )
317 wxASSERT_MSG( ufds
[i
].fd
< wxFD_SETSIZE
, _T("fd out of range") );
319 if ( ufds
[i
].events
& G_IO_IN
)
320 wxFD_SET(ufds
[i
].fd
, &readfds
);
322 if ( ufds
[i
].events
& G_IO_PRI
)
323 wxFD_SET(ufds
[i
].fd
, &exceptfds
);
325 if ( ufds
[i
].events
& G_IO_OUT
)
326 wxFD_SET(ufds
[i
].fd
, &writefds
);
328 if ( ufds
[i
].fd
> fdMax
)
333 int res
= select(fdMax
, &readfds
, &writefds
, &exceptfds
, &tv_timeout
);
335 // translate the results back
336 for ( i
= 0; i
< nfds
; i
++ )
340 if ( wxFD_ISSET(ufds
[i
].fd
, &readfds
) )
341 ufds
[i
].revents
|= G_IO_IN
;
343 if ( wxFD_ISSET(ufds
[i
].fd
, &exceptfds
) )
344 ufds
[i
].revents
|= G_IO_PRI
;
346 if ( wxFD_ISSET(ufds
[i
].fd
, &writefds
) )
347 ufds
[i
].revents
|= G_IO_OUT
;
353 #endif // HAVE_POLL/!HAVE_POLL
355 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
360 g_mainThreadLocked
= TRUE
;
362 // we rely on the fact that glib GPollFD struct is really just pollfd but
363 // I wonder how wise is this in the long term (VZ)
364 gint res
= wxPoll( (wxPollFd
*) ufds
, nfds
, timeout
);
367 g_mainThreadLocked
= FALSE
;
374 #endif // wxUSE_THREADS
378 void wxapp_install_idle_handler()
381 wxMutexLocker
lock(gs_idleTagsMutex
);
384 // Don't install the handler if it's already installed. This test *MUST*
385 // be done when gs_idleTagsMutex is locked!
389 // GD: this assert is raised when using the thread sample (which works)
390 // so the test is probably not so easy. Can widget callbacks be
391 // triggered from child threads and, if so, for which widgets?
392 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
394 wxASSERT_MSG( wxTheApp
->m_idleTag
== 0, wxT("attempt to install idle handler twice") );
398 if (g_pendingTag
== 0)
399 g_pendingTag
= gtk_idle_add_priority( 900, wxapp_pending_callback
, (gpointer
) NULL
);
401 // This routine gets called by all event handlers
402 // indicating that the idle is over. It may also
403 // get called from other thread for sending events
404 // to the main thread (and processing these in
405 // idle time). Very low priority.
406 wxTheApp
->m_idleTag
= gtk_idle_add_priority( 1000, wxapp_idle_callback
, (gpointer
) NULL
);
409 //-----------------------------------------------------------------------------
410 // Access to the root window global
411 //-----------------------------------------------------------------------------
413 GtkWidget
* wxGetRootWindow()
415 if (gs_RootWindow
== NULL
)
417 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
418 gtk_widget_realize( gs_RootWindow
);
420 return gs_RootWindow
;
423 //-----------------------------------------------------------------------------
425 //-----------------------------------------------------------------------------
427 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
429 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
430 EVT_IDLE(wxAppBase::OnIdle
)
436 m_isInAssert
= FALSE
;
437 #endif // __WXDEBUG__
441 wxapp_install_idle_handler();
444 g_main_set_poll_func( wxapp_poll_func
);
447 m_colorCube
= (unsigned char*) NULL
;
449 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
450 m_glVisualInfo
= (void *) NULL
;
451 m_glFBCInfo
= (void *) NULL
;
456 if (m_idleTag
) gtk_idle_remove( m_idleTag
);
458 if (m_colorCube
) free(m_colorCube
);
461 bool wxApp::OnInitGui()
463 if ( !wxAppBase::OnInitGui() )
466 GdkVisual
*visual
= gdk_visual_get_system();
468 // if this is a wxGLApp (derived from wxApp), and we've already
469 // chosen a specific visual, then derive the GdkVisual from that
470 if (m_glVisualInfo
!= NULL
)
473 // seems gtk_widget_set_default_visual no longer exists?
474 GdkVisual
* vis
= gtk_widget_get_default_visual();
476 GdkVisual
* vis
= gdkx_visual_get(
477 ((XVisualInfo
*) m_glVisualInfo
) ->visualid
);
478 gtk_widget_set_default_visual( vis
);
481 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
482 gtk_widget_set_default_colormap( colormap
);
487 // On some machines, the default visual is just 256 colours, so
488 // we make sure we get the best. This can sometimes be wasteful.
491 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual
))
494 /* seems gtk_widget_set_default_visual no longer exists? */
495 GdkVisual
* vis
= gtk_widget_get_default_visual();
497 GdkVisual
* vis
= gdk_visual_get_best();
498 gtk_widget_set_default_visual( vis
);
501 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
502 gtk_widget_set_default_colormap( colormap
);
507 // Nothing to do for 15, 16, 24, 32 bit displays
508 if (visual
->depth
> 8) return TRUE
;
510 // initialize color cube for 8-bit color reduction dithering
512 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
514 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
516 for (int r
= 0; r
< 32; r
++)
518 for (int g
= 0; g
< 32; g
++)
520 for (int b
= 0; b
< 32; b
++)
522 int rr
= (r
<< 3) | (r
>> 2);
523 int gg
= (g
<< 3) | (g
>> 2);
524 int bb
= (b
<< 3) | (b
>> 2);
528 GdkColor
*colors
= cmap
->colors
;
533 for (int i
= 0; i
< cmap
->size
; i
++)
535 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
536 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
537 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
538 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
541 index
= i
; max
= sum
;
547 // assume 8-bit true or static colors. this really exists
548 GdkVisual
* vis
= gdk_colormap_get_visual( cmap
);
549 index
= (r
>> (5 - vis
->red_prec
)) << vis
->red_shift
;
550 index
|= (g
>> (5 - vis
->green_prec
)) << vis
->green_shift
;
551 index
|= (b
>> (5 - vis
->blue_prec
)) << vis
->blue_shift
;
553 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
561 GdkVisual
*wxApp::GetGdkVisual()
563 GdkVisual
*visual
= NULL
;
566 visual
= gdkx_visual_get( ((XVisualInfo
*) m_glVisualInfo
)->visualid
);
568 visual
= gdk_window_get_visual( wxGetRootWindow()->window
);
575 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( "wxWidgets 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 // decide which conversion to use for the file names
613 // (1) this variable exists for the sole purpose of specifying the encoding
614 // of the filenames for GTK+ programs, so use it if it is set
615 wxString
encName(wxGetenv(_T("G_FILENAME_ENCODING")));
616 encName
= encName
.BeforeFirst(_T(','));
617 if (encName
== _T("@locale"))
623 // (2) if a non default locale is set, assume that the user wants his
624 // filenames in this locale too
625 encName
= wxLocale::GetSystemEncodingName().Upper();
626 // (3) finally use UTF-8 by default
627 if (encName
.empty() || encName
== _T("US-ASCII"))
628 encName
= _T("UTF-8");
629 wxSetEnv(_T("G_FILENAME_ENCODING"), encName
);
633 encName
= _T("UTF-8");
635 static wxConvBrokenFileNames
fileconv(encName
);
636 wxConvFileName
= &fileconv
;
637 #endif // __WXGTK20__
640 // gtk_init() wants UTF-8, not wchar_t, so convert
642 char **argvGTK
= new char *[argc
+ 1];
643 for ( i
= 0; i
< argc
; i
++ )
645 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv
[i
]));
648 argvGTK
[argc
] = NULL
;
653 init_result
= true; // is there a _check() version of this?
654 gpe_application_init( &argcGTK
, &argvGTK
);
656 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
659 if ( argcGTK
!= argc
)
661 // we have to drop the parameters which were consumed by GTK+
662 for ( i
= 0; i
< argcGTK
; i
++ )
664 while ( strcmp(wxConvUTF8
.cWX2MB(argv
[i
]), argvGTK
[i
]) != 0 )
666 memmove(argv
+ i
, argv
+ i
+ 1, argc
- i
);
672 //else: gtk_init() didn't modify our parameters
675 for ( i
= 0; i
< argcGTK
; i
++ )
681 #else // !wxUSE_UNICODE
682 // gtk_init() shouldn't actually change argv itself (just its contents) so
683 // it's ok to pass pointer to it
684 init_result
= gtk_init_check( &argc
, &argv
);
685 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
688 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
692 // we can not enter threads before gtk_init is done
695 if ( !wxAppBase::Initialize(argc
, argv
) )
702 wxSetDetectableAutoRepeat( TRUE
);
705 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
711 void wxApp::CleanUp()
715 wxAppBase::CleanUp();
720 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
724 wxAppBase::OnAssert(file
, line
, cond
, msg
);
726 m_isInAssert
= FALSE
;
729 #endif // __WXDEBUG__
731 void wxApp::RemoveIdleTag()
734 wxMutexLocker
lock(gs_idleTagsMutex
);
738 gtk_idle_remove( wxTheApp
->m_idleTag
);
739 wxTheApp
->m_idleTag
= 0;