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/gtk1/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()
177 if (!wxThread::IsMain())
179 #endif // wxUSE_THREADS_
181 wxapp_install_idle_handler();
184 if (!wxThread::IsMain())
186 #endif // wxUSE_THREADS_
189 //-----------------------------------------------------------------------------
191 //-----------------------------------------------------------------------------
193 // the callback functions must be extern "C" to comply with GTK+ declarations
197 static gint
wxapp_pending_callback( gpointer
WXUNUSED(data
) )
199 if (!wxTheApp
) return TRUE
;
201 // When getting called from GDK's time-out handler
202 // we are no longer within GDK's grab on the GUI
203 // thread so we must lock it here ourselves.
206 // Sent idle event to all who request them.
207 wxTheApp
->ProcessPendingEvents();
211 wxMutexLocker
lock(gs_idleTagsMutex
);
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();
244 if (win
->IsKindOf(CLASSINFO(wxGenericMessageDialog
)))
245 win
->OnInternalIdle();
249 #endif // __WXDEBUG__
251 // When getting called from GDK's time-out handler
252 // we are no longer within GDK's grab on the GUI
253 // thread so we must lock it here ourselves.
256 // Indicate that we are now in idle mode and event handlers
257 // will have to reinstall the idle handler again.
260 wxMutexLocker
lock(gs_idleTagsMutex
);
263 wxTheApp
->m_idleTag
= 0;
266 // Send idle event to all who request them as long as
267 // no events have popped up in the event queue.
268 while (wxTheApp
->ProcessIdle() && (gtk_events_pending() == 0))
271 // Release lock again
274 // Return FALSE to indicate that no more idle events are
275 // to be sent (single shot instead of continuous stream).
283 #define wxPollFd pollfd
286 typedef GPollFD wxPollFd
;
288 int wxPoll(wxPollFd
*ufds
, unsigned int nfds
, int timeout
)
290 // convert timeout from ms to struct timeval (s/us)
292 tv_timeout
.tv_sec
= timeout
/1000;
293 tv_timeout
.tv_usec
= (timeout%1000
)*1000;
295 // remember the highest fd used here
298 // and fill the sets for select()
303 wxFD_ZERO(&writefds
);
304 wxFD_ZERO(&exceptfds
);
307 for ( i
= 0; i
< nfds
; i
++ )
309 wxASSERT_MSG( ufds
[i
].fd
< wxFD_SETSIZE
, _T("fd out of range") );
311 if ( ufds
[i
].events
& G_IO_IN
)
312 wxFD_SET(ufds
[i
].fd
, &readfds
);
314 if ( ufds
[i
].events
& G_IO_PRI
)
315 wxFD_SET(ufds
[i
].fd
, &exceptfds
);
317 if ( ufds
[i
].events
& G_IO_OUT
)
318 wxFD_SET(ufds
[i
].fd
, &writefds
);
320 if ( ufds
[i
].fd
> fdMax
)
325 int res
= select(fdMax
, &readfds
, &writefds
, &exceptfds
, &tv_timeout
);
327 // translate the results back
328 for ( i
= 0; i
< nfds
; i
++ )
332 if ( wxFD_ISSET(ufds
[i
].fd
, &readfds
) )
333 ufds
[i
].revents
|= G_IO_IN
;
335 if ( wxFD_ISSET(ufds
[i
].fd
, &exceptfds
) )
336 ufds
[i
].revents
|= G_IO_PRI
;
338 if ( wxFD_ISSET(ufds
[i
].fd
, &writefds
) )
339 ufds
[i
].revents
|= G_IO_OUT
;
345 #endif // HAVE_POLL/!HAVE_POLL
347 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
352 g_mainThreadLocked
= TRUE
;
354 // we rely on the fact that glib GPollFD struct is really just pollfd but
355 // I wonder how wise is this in the long term (VZ)
356 gint res
= wxPoll( (wxPollFd
*) ufds
, nfds
, timeout
);
359 g_mainThreadLocked
= FALSE
;
366 #endif // wxUSE_THREADS
370 void wxapp_install_idle_handler()
373 wxMutexLocker
lock(gs_idleTagsMutex
);
376 // Don't install the handler if it's already installed. This test *MUST*
377 // be done when gs_idleTagsMutex is locked!
381 // GD: this assert is raised when using the thread sample (which works)
382 // so the test is probably not so easy. Can widget callbacks be
383 // triggered from child threads and, if so, for which widgets?
384 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
386 wxASSERT_MSG( wxTheApp
->m_idleTag
== 0, wxT("attempt to install idle handler twice") );
390 if (g_pendingTag
== 0)
391 g_pendingTag
= gtk_idle_add_priority( 900, wxapp_pending_callback
, (gpointer
) NULL
);
393 // This routine gets called by all event handlers
394 // indicating that the idle is over. It may also
395 // get called from other thread for sending events
396 // to the main thread (and processing these in
397 // idle time). Very low priority.
398 wxTheApp
->m_idleTag
= gtk_idle_add_priority( 1000, wxapp_idle_callback
, (gpointer
) NULL
);
401 //-----------------------------------------------------------------------------
402 // Access to the root window global
403 //-----------------------------------------------------------------------------
405 GtkWidget
* wxGetRootWindow()
407 if (gs_RootWindow
== NULL
)
409 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
410 gtk_widget_realize( gs_RootWindow
);
412 return gs_RootWindow
;
415 //-----------------------------------------------------------------------------
417 //-----------------------------------------------------------------------------
419 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
421 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
422 EVT_IDLE(wxAppBase::OnIdle
)
428 m_isInAssert
= FALSE
;
429 #endif // __WXDEBUG__
433 wxapp_install_idle_handler();
436 g_main_set_poll_func( wxapp_poll_func
);
439 m_colorCube
= (unsigned char*) NULL
;
441 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
442 m_glVisualInfo
= (void *) NULL
;
443 m_glFBCInfo
= (void *) NULL
;
448 if (m_idleTag
) gtk_idle_remove( m_idleTag
);
450 if (m_colorCube
) free(m_colorCube
);
453 bool wxApp::OnInitGui()
455 if ( !wxAppBase::OnInitGui() )
458 GdkVisual
*visual
= gdk_visual_get_system();
460 // if this is a wxGLApp (derived from wxApp), and we've already
461 // chosen a specific visual, then derive the GdkVisual from that
462 if (m_glVisualInfo
!= NULL
)
464 GdkVisual
* vis
= gdkx_visual_get(
465 ((XVisualInfo
*) m_glVisualInfo
) ->visualid
);
466 gtk_widget_set_default_visual( vis
);
468 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
469 gtk_widget_set_default_colormap( colormap
);
474 // On some machines, the default visual is just 256 colours, so
475 // we make sure we get the best. This can sometimes be wasteful.
478 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual
))
480 GdkVisual
* vis
= gdk_visual_get_best();
481 gtk_widget_set_default_visual( vis
);
483 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
484 gtk_widget_set_default_colormap( colormap
);
489 // Nothing to do for 15, 16, 24, 32 bit displays
490 if (visual
->depth
> 8) return TRUE
;
492 // initialize color cube for 8-bit color reduction dithering
494 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
496 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
498 for (int r
= 0; r
< 32; r
++)
500 for (int g
= 0; g
< 32; g
++)
502 for (int b
= 0; b
< 32; b
++)
504 int rr
= (r
<< 3) | (r
>> 2);
505 int gg
= (g
<< 3) | (g
>> 2);
506 int bb
= (b
<< 3) | (b
>> 2);
510 GdkColor
*colors
= cmap
->colors
;
515 for (int i
= 0; i
< cmap
->size
; i
++)
517 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
518 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
519 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
520 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
523 index
= i
; max
= sum
;
529 // assume 8-bit true or static colors. this really exists
530 GdkVisual
* vis
= gdk_colormap_get_visual( cmap
);
531 index
= (r
>> (5 - vis
->red_prec
)) << vis
->red_shift
;
532 index
|= (g
>> (5 - vis
->green_prec
)) << vis
->green_shift
;
533 index
|= (b
>> (5 - vis
->blue_prec
)) << vis
->blue_shift
;
535 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
543 GdkVisual
*wxApp::GetGdkVisual()
545 GdkVisual
*visual
= NULL
;
548 visual
= gdkx_visual_get( ((XVisualInfo
*) m_glVisualInfo
)->visualid
);
550 visual
= gdk_window_get_visual( wxGetRootWindow()->window
);
557 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
562 // GTK 1.2 up to version 1.2.3 has broken threads
563 if ((gtk_major_version
== 1) &&
564 (gtk_minor_version
== 2) &&
565 (gtk_micro_version
< 4))
567 printf( "wxWidgets warning: GUI threading disabled due to outdated GTK version\n" );
571 if (!g_thread_supported())
574 #endif // wxUSE_THREADS
578 // We should have the wxUSE_WCHAR_T test on the _outside_
581 wxConvCurrent
= &wxConvLocal
;
582 #else // !wxUSE_WCHAR_T
584 wxConvCurrent
= (wxMBConv
*) NULL
;
585 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
588 // gtk_init() wants UTF-8, not wchar_t, so convert
590 char **argvGTK
= new char *[argc
+ 1];
591 for ( i
= 0; i
< argc
; i
++ )
593 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv
[i
]));
596 argvGTK
[argc
] = NULL
;
601 init_result
= true; // is there a _check() version of this?
602 gpe_application_init( &argcGTK
, &argvGTK
);
604 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
607 if ( argcGTK
!= argc
)
609 // we have to drop the parameters which were consumed by GTK+
610 for ( i
= 0; i
< argcGTK
; i
++ )
612 while ( strcmp(wxConvUTF8
.cWX2MB(argv
[i
]), argvGTK
[i
]) != 0 )
614 memmove(argv
+ i
, argv
+ i
+ 1, argc
- i
);
620 //else: gtk_init() didn't modify our parameters
623 for ( i
= 0; i
< argcGTK
; i
++ )
629 #else // !wxUSE_UNICODE
630 // gtk_init() shouldn't actually change argv itself (just its contents) so
631 // it's ok to pass pointer to it
632 init_result
= gtk_init_check( &argc
, &argv
);
633 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
636 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
640 // we can not enter threads before gtk_init is done
643 if ( !wxAppBase::Initialize(argc
, argv
) )
650 wxSetDetectableAutoRepeat( TRUE
);
653 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
659 void wxApp::CleanUp()
663 wxAppBase::CleanUp();
668 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
672 wxAppBase::OnAssert(file
, line
, cond
, msg
);
674 m_isInAssert
= FALSE
;
677 #endif // __WXDEBUG__
679 void wxApp::RemoveIdleTag()
682 wxMutexLocker
lock(gs_idleTagsMutex
);
686 gtk_idle_remove( wxTheApp
->m_idleTag
);
687 wxTheApp
->m_idleTag
= 0;