1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/app.cpp
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"
25 #include "wx/dialog.h"
26 #include "wx/settings.h"
27 #include "wx/msgdlg.h"
30 #include "wx/gdicmn.h"
31 #include "wx/memory.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/unix/private.h"
80 #include "wx/gtk1/win_gtk.h"
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 bool g_mainThreadLocked
= false;
89 gint g_pendingTag
= 0;
91 static GtkWidget
*gs_RootWindow
= (GtkWidget
*) NULL
;
93 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
99 void wxapp_install_idle_handler();
102 static wxMutex gs_idleTagsMutex
;
105 //-----------------------------------------------------------------------------
107 //-----------------------------------------------------------------------------
109 // not static because used by textctrl.cpp
112 bool wxIsInsideYield
= false;
114 bool wxApp::Yield(bool onlyIfNeeded
)
116 if ( wxIsInsideYield
)
120 wxFAIL_MSG( wxT("wxYield called recursively" ) );
127 if ( !wxThread::IsMain() )
129 // can't call gtk_main_iteration() from other threads like this
132 #endif // wxUSE_THREADS
134 wxIsInsideYield
= true;
136 // We need to remove idle callbacks or the loop will
138 wxTheApp
->RemoveIdleTag();
141 // disable log flushing from here because a call to wxYield() shouldn't
142 // normally result in message boxes popping up &c
146 while (gtk_events_pending())
147 gtk_main_iteration();
149 // It's necessary to call ProcessIdle() to update the frames sizes which
150 // might have been changed (it also will update other things set from
151 // OnUpdateUI() which is a nice (and desired) side effect). But we
152 // call ProcessIdle() only once since this is not meant for longish
153 // background jobs (controlled by wxIdleEvent::RequestMore() and the
154 // return value of Processidle().
158 // let the logs be flashed again
162 wxIsInsideYield
= false;
167 //-----------------------------------------------------------------------------
169 //-----------------------------------------------------------------------------
171 // RR/KH: The wxMutexGui calls are not needed on GTK2 according to
172 // the GTK faq, http://www.gtk.org/faq/#AEN500
173 // The calls to gdk_threads_enter() and leave() are specifically noted
174 // as not being necessary. The MutexGui calls are still left in for GTK1.
175 // Eliminating the MutexGui calls fixes the long-standing "random" lockup
176 // when using wxPostEvent (which calls WakeUpIdle) from a thread.
178 void wxApp::WakeUpIdle()
181 if (!wxThread::IsMain())
183 #endif // wxUSE_THREADS_
185 wxapp_install_idle_handler();
188 if (!wxThread::IsMain())
190 #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();
248 if (win
->IsKindOf(CLASSINFO(wxGenericMessageDialog
)))
249 win
->OnInternalIdle();
253 #endif // __WXDEBUG__
255 // When getting called from GDK's time-out handler
256 // we are no longer within GDK's grab on the GUI
257 // thread so we must lock it here ourselves.
260 // Indicate that we are now in idle mode and event handlers
261 // will have to reinstall the idle handler again.
264 wxMutexLocker
lock(gs_idleTagsMutex
);
267 wxTheApp
->m_idleTag
= 0;
270 // Send idle event to all who request them as long as
271 // no events have popped up in the event queue.
272 while (wxTheApp
->ProcessIdle() && (gtk_events_pending() == 0))
275 // Release lock again
278 // Return FALSE to indicate that no more idle events are
279 // to be sent (single shot instead of continuous stream).
287 #define wxPollFd pollfd
290 typedef GPollFD wxPollFd
;
292 int wxPoll(wxPollFd
*ufds
, unsigned int nfds
, int timeout
)
294 // convert timeout from ms to struct timeval (s/us)
296 tv_timeout
.tv_sec
= timeout
/1000;
297 tv_timeout
.tv_usec
= (timeout%1000
)*1000;
299 // remember the highest fd used here
302 // and fill the sets for select()
307 wxFD_ZERO(&writefds
);
308 wxFD_ZERO(&exceptfds
);
311 for ( i
= 0; i
< nfds
; i
++ )
313 wxASSERT_MSG( ufds
[i
].fd
< wxFD_SETSIZE
, _T("fd out of range") );
315 if ( ufds
[i
].events
& G_IO_IN
)
316 wxFD_SET(ufds
[i
].fd
, &readfds
);
318 if ( ufds
[i
].events
& G_IO_PRI
)
319 wxFD_SET(ufds
[i
].fd
, &exceptfds
);
321 if ( ufds
[i
].events
& G_IO_OUT
)
322 wxFD_SET(ufds
[i
].fd
, &writefds
);
324 if ( ufds
[i
].fd
> fdMax
)
329 int res
= select(fdMax
, &readfds
, &writefds
, &exceptfds
, &tv_timeout
);
331 // translate the results back
332 for ( i
= 0; i
< nfds
; i
++ )
336 if ( wxFD_ISSET(ufds
[i
].fd
, &readfds
) )
337 ufds
[i
].revents
|= G_IO_IN
;
339 if ( wxFD_ISSET(ufds
[i
].fd
, &exceptfds
) )
340 ufds
[i
].revents
|= G_IO_PRI
;
342 if ( wxFD_ISSET(ufds
[i
].fd
, &writefds
) )
343 ufds
[i
].revents
|= G_IO_OUT
;
349 #endif // HAVE_POLL/!HAVE_POLL
351 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
356 g_mainThreadLocked
= true;
358 // we rely on the fact that glib GPollFD struct is really just pollfd but
359 // I wonder how wise is this in the long term (VZ)
360 gint res
= wxPoll( (wxPollFd
*) ufds
, nfds
, timeout
);
363 g_mainThreadLocked
= false;
370 #endif // wxUSE_THREADS
374 void wxapp_install_idle_handler()
377 wxMutexLocker
lock(gs_idleTagsMutex
);
380 // Don't install the handler if it's already installed. This test *MUST*
381 // be done when gs_idleTagsMutex is locked!
385 // GD: this assert is raised when using the thread sample (which works)
386 // so the test is probably not so easy. Can widget callbacks be
387 // triggered from child threads and, if so, for which widgets?
388 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
390 wxASSERT_MSG( wxTheApp
->m_idleTag
== 0, wxT("attempt to install idle handler twice") );
394 if (g_pendingTag
== 0)
395 g_pendingTag
= gtk_idle_add_priority( 900, wxapp_pending_callback
, (gpointer
) NULL
);
397 // This routine gets called by all event handlers
398 // indicating that the idle is over. It may also
399 // get called from other thread for sending events
400 // to the main thread (and processing these in
401 // idle time). Very low priority.
402 wxTheApp
->m_idleTag
= gtk_idle_add_priority( 1000, wxapp_idle_callback
, (gpointer
) NULL
);
405 //-----------------------------------------------------------------------------
406 // Access to the root window global
407 //-----------------------------------------------------------------------------
409 GtkWidget
* wxGetRootWindow()
411 if (gs_RootWindow
== NULL
)
413 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
414 gtk_widget_realize( gs_RootWindow
);
416 return gs_RootWindow
;
419 //-----------------------------------------------------------------------------
421 //-----------------------------------------------------------------------------
423 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
425 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
426 EVT_IDLE(wxAppBase::OnIdle
)
432 m_isInAssert
= false;
433 #endif // __WXDEBUG__
437 wxapp_install_idle_handler();
440 g_main_set_poll_func( wxapp_poll_func
);
443 m_colorCube
= (unsigned char*) NULL
;
445 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
446 m_glVisualInfo
= (void *) NULL
;
447 m_glFBCInfo
= (void *) NULL
;
452 if (m_idleTag
) gtk_idle_remove( m_idleTag
);
454 if (m_colorCube
) free(m_colorCube
);
457 bool wxApp::OnInitGui()
459 if ( !wxAppBase::OnInitGui() )
462 GdkVisual
*visual
= gdk_visual_get_system();
464 // if this is a wxGLApp (derived from wxApp), and we've already
465 // chosen a specific visual, then derive the GdkVisual from that
466 if (m_glVisualInfo
!= NULL
)
468 GdkVisual
* vis
= gdkx_visual_get(
469 ((XVisualInfo
*) m_glVisualInfo
) ->visualid
);
470 gtk_widget_set_default_visual( vis
);
472 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
473 gtk_widget_set_default_colormap( colormap
);
478 // On some machines, the default visual is just 256 colours, so
479 // we make sure we get the best. This can sometimes be wasteful.
482 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual
))
484 GdkVisual
* vis
= gdk_visual_get_best();
485 gtk_widget_set_default_visual( vis
);
487 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
488 gtk_widget_set_default_colormap( colormap
);
493 // Nothing to do for 15, 16, 24, 32 bit displays
494 if (visual
->depth
> 8) return TRUE
;
496 // initialize color cube for 8-bit color reduction dithering
498 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
500 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
502 for (int r
= 0; r
< 32; r
++)
504 for (int g
= 0; g
< 32; g
++)
506 for (int b
= 0; b
< 32; b
++)
508 int rr
= (r
<< 3) | (r
>> 2);
509 int gg
= (g
<< 3) | (g
>> 2);
510 int bb
= (b
<< 3) | (b
>> 2);
514 GdkColor
*colors
= cmap
->colors
;
519 for (int i
= 0; i
< cmap
->size
; i
++)
521 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
522 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
523 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
524 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
527 index
= i
; max
= sum
;
533 // assume 8-bit true or static colors. this really exists
534 GdkVisual
* vis
= gdk_colormap_get_visual( cmap
);
535 index
= (r
>> (5 - vis
->red_prec
)) << vis
->red_shift
;
536 index
|= (g
>> (5 - vis
->green_prec
)) << vis
->green_shift
;
537 index
|= (b
>> (5 - vis
->blue_prec
)) << vis
->blue_shift
;
539 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
547 GdkVisual
*wxApp::GetGdkVisual()
549 GdkVisual
*visual
= NULL
;
552 visual
= gdkx_visual_get( ((XVisualInfo
*) m_glVisualInfo
)->visualid
);
554 visual
= gdk_window_get_visual( wxGetRootWindow()->window
);
561 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
566 // GTK 1.2 up to version 1.2.3 has broken threads
567 if ((gtk_major_version
== 1) &&
568 (gtk_minor_version
== 2) &&
569 (gtk_micro_version
< 4))
571 printf( "wxWidgets warning: GUI threading disabled due to outdated GTK version\n" );
575 if (!g_thread_supported())
578 #endif // wxUSE_THREADS
582 // We should have the wxUSE_WCHAR_T test on the _outside_
585 wxConvCurrent
= &wxConvLocal
;
586 #else // !wxUSE_WCHAR_T
588 wxConvCurrent
= (wxMBConv
*) NULL
;
589 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
592 // gtk_init() wants UTF-8, not wchar_t, so convert
594 char **argvGTK
= new char *[argc
+ 1];
595 for ( i
= 0; i
< argc
; i
++ )
597 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv
[i
]));
600 argvGTK
[argc
] = NULL
;
605 init_result
= true; // is there a _check() version of this?
606 gpe_application_init( &argcGTK
, &argvGTK
);
608 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
611 if ( argcGTK
!= argc
)
613 // we have to drop the parameters which were consumed by GTK+
614 for ( i
= 0; i
< argcGTK
; i
++ )
616 while ( strcmp(wxConvUTF8
.cWX2MB(argv
[i
]), argvGTK
[i
]) != 0 )
618 memmove(argv
+ i
, argv
+ i
+ 1, argc
- i
);
624 //else: gtk_init() didn't modify our parameters
627 for ( i
= 0; i
< argcGTK
; i
++ )
633 #else // !wxUSE_UNICODE
634 // gtk_init() shouldn't actually change argv itself (just its contents) so
635 // it's ok to pass pointer to it
636 init_result
= gtk_init_check( &argc
, &argv
);
637 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
640 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
644 // we can not enter threads before gtk_init is done
647 if ( !wxAppBase::Initialize(argc
, argv
) )
654 wxSetDetectableAutoRepeat( TRUE
);
657 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
663 void wxApp::CleanUp()
667 wxAppBase::CleanUp();
672 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
676 wxAppBase::OnAssert(file
, line
, cond
, msg
);
678 m_isInAssert
= false;
681 #endif // __WXDEBUG__
683 void wxApp::RemoveIdleTag()
686 wxMutexLocker
lock(gs_idleTagsMutex
);
690 gtk_idle_remove( wxTheApp
->m_idleTag
);
691 wxTheApp
->m_idleTag
= 0;