1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/app.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
19 #include "wx/dialog.h"
20 #include "wx/settings.h"
21 #include "wx/msgdlg.h"
22 #include "wx/memory.h"
24 #include "wx/gdicmn.h"
26 #include "wx/module.h"
30 #include "wx/filename.h"
31 #include "wx/thread.h"
37 #ifdef __WXUNIVERSAL__
38 #include "wx/univ/theme.h"
39 #include "wx/univ/renderer.h"
43 #include "wx/thread.h"
52 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
53 // in neither poll.h nor sys/poll.h which results in link errors later
66 // we implement poll() ourselves using select() which is supposed exist in
68 #include <sys/types.h>
71 #ifdef HAVE_SYS_SELECT_H
72 #include <sys/select.h>
74 #endif // HAVE_POLL/!HAVE_POLL
76 #include "wx/unix/private.h"
77 #include "wx/gtk1/win_gtk.h"
81 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
85 bool g_mainThreadLocked
= false;
86 gint g_pendingTag
= 0;
88 static GtkWidget
*gs_RootWindow
= NULL
;
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
96 void wxapp_install_idle_handler();
99 static wxMutex gs_idleTagsMutex
;
102 //-----------------------------------------------------------------------------
104 //-----------------------------------------------------------------------------
106 bool wxApp::DoYield(bool onlyIfNeeded
, long eventsToProcess
)
108 if ( m_isInsideYield
)
112 wxFAIL_MSG( wxT("wxYield called recursively" ) );
119 if ( !wxThread::IsMain() )
121 // can't call gtk_main_iteration() from other threads like this
124 #endif // wxUSE_THREADS
126 m_isInsideYield
= true;
127 m_eventsToProcessInsideYield
= eventsToProcess
;
129 // We need to remove idle callbacks or the loop will
131 wxTheApp
->RemoveIdleTag();
134 // disable log flushing from here because a call to wxYield() shouldn't
135 // normally result in message boxes popping up &c
139 // TODO: implement event filtering using the eventsToProcess mask
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().
152 // let the logs be flashed again
156 m_isInsideYield
= false;
161 //-----------------------------------------------------------------------------
163 //-----------------------------------------------------------------------------
165 // RR/KH: The wxMutexGui calls are not needed on GTK2 according to
166 // the GTK faq, http://www.gtk.org/faq/#AEN500
167 // The calls to gdk_threads_enter() and leave() are specifically noted
168 // as not being necessary. The MutexGui calls are still left in for GTK1.
169 // Eliminating the MutexGui calls fixes the long-standing "random" lockup
170 // when using wxPostEvent (which calls WakeUpIdle) from a thread.
172 void wxApp::WakeUpIdle()
175 if (!wxThread::IsMain())
177 #endif // wxUSE_THREADS
179 wxapp_install_idle_handler();
182 if (!wxThread::IsMain())
184 #endif // wxUSE_THREADS
187 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------
191 // the callback functions must be extern "C" to comply with GTK+ declarations
195 static gint
wxapp_pending_callback( gpointer
WXUNUSED(data
) )
197 if (!wxTheApp
) return TRUE
;
199 // When getting called from GDK's time-out handler
200 // we are no longer within GDK's grab on the GUI
201 // thread so we must lock it here ourselves.
204 // Sent idle event to all who request them.
205 wxTheApp
->ProcessPendingEvents();
209 wxMutexLocker
lock(gs_idleTagsMutex
);
214 // Flush the logged messages if any.
216 wxLog::FlushActive();
219 // Release lock again
222 // Return FALSE to indicate that no more idle events are
223 // to be sent (single shot instead of continuous stream)
227 static gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
233 // don't generate the idle events while the assert modal dialog is shown,
234 // this completely confuses the apps which don't expect to be reentered
235 // from some safely-looking functions
236 if ( wxTheApp
->IsInAssert() )
238 // But repaint the assertion message if necessary
239 if (wxTopLevelWindows
.GetCount() > 0)
241 wxWindow
* win
= (wxWindow
*) wxTopLevelWindows
.GetLast()->GetData();
242 if (win
->IsKindOf(CLASSINFO(wxGenericMessageDialog
)))
243 win
->OnInternalIdle();
247 #endif // __WXDEBUG__
249 // When getting called from GDK's time-out handler
250 // we are no longer within GDK's grab on the GUI
251 // thread so we must lock it here ourselves.
254 // Indicate that we are now in idle mode and event handlers
255 // will have to reinstall the idle handler again.
258 wxMutexLocker
lock(gs_idleTagsMutex
);
261 wxTheApp
->m_idleTag
= 0;
264 // Send idle event to all who request them as long as
265 // no events have popped up in the event queue.
266 while (wxTheApp
->ProcessIdle() && (gtk_events_pending() == 0))
269 // Release lock again
272 // Return FALSE to indicate that no more idle events are
273 // to be sent (single shot instead of continuous stream).
281 #define wxPollFd pollfd
284 typedef GPollFD wxPollFd
;
286 int wxPoll(wxPollFd
*ufds
, unsigned int nfds
, int timeout
)
288 // convert timeout from ms to struct timeval (s/us)
290 tv_timeout
.tv_sec
= timeout
/1000;
291 tv_timeout
.tv_usec
= (timeout%1000
)*1000;
293 // remember the highest fd used here
296 // and fill the sets for select()
301 wxFD_ZERO(&writefds
);
302 wxFD_ZERO(&exceptfds
);
305 for ( i
= 0; i
< nfds
; i
++ )
307 wxASSERT_MSG( ufds
[i
].fd
< FD_SETSIZE
, _T("fd out of range") );
309 if ( ufds
[i
].events
& G_IO_IN
)
310 wxFD_SET(ufds
[i
].fd
, &readfds
);
312 if ( ufds
[i
].events
& G_IO_PRI
)
313 wxFD_SET(ufds
[i
].fd
, &exceptfds
);
315 if ( ufds
[i
].events
& G_IO_OUT
)
316 wxFD_SET(ufds
[i
].fd
, &writefds
);
318 if ( ufds
[i
].fd
> fdMax
)
323 int res
= select(fdMax
, &readfds
, &writefds
, &exceptfds
, &tv_timeout
);
325 // translate the results back
326 for ( i
= 0; i
< nfds
; i
++ )
330 if ( wxFD_ISSET(ufds
[i
].fd
, &readfds
) )
331 ufds
[i
].revents
|= G_IO_IN
;
333 if ( wxFD_ISSET(ufds
[i
].fd
, &exceptfds
) )
334 ufds
[i
].revents
|= G_IO_PRI
;
336 if ( wxFD_ISSET(ufds
[i
].fd
, &writefds
) )
337 ufds
[i
].revents
|= G_IO_OUT
;
343 #endif // HAVE_POLL/!HAVE_POLL
345 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
350 g_mainThreadLocked
= true;
352 // we rely on the fact that glib GPollFD struct is really just pollfd but
353 // I wonder how wise is this in the long term (VZ)
354 gint res
= wxPoll( (wxPollFd
*) ufds
, nfds
, timeout
);
357 g_mainThreadLocked
= false;
364 #endif // wxUSE_THREADS
368 void wxapp_install_idle_handler()
371 wxMutexLocker
lock(gs_idleTagsMutex
);
374 // Don't install the handler if it's already installed. This test *MUST*
375 // be done when gs_idleTagsMutex is locked!
379 // GD: this assert is raised when using the thread sample (which works)
380 // so the test is probably not so easy. Can widget callbacks be
381 // triggered from child threads and, if so, for which widgets?
382 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
384 wxASSERT_MSG( wxTheApp
->m_idleTag
== 0, wxT("attempt to install idle handler twice") );
388 if (g_pendingTag
== 0)
389 g_pendingTag
= gtk_idle_add_priority( 900, wxapp_pending_callback
, (gpointer
) NULL
);
391 // This routine gets called by all event handlers
392 // indicating that the idle is over. It may also
393 // get called from other thread for sending events
394 // to the main thread (and processing these in
395 // idle time). Very low priority.
396 wxTheApp
->m_idleTag
= gtk_idle_add_priority( 1000, wxapp_idle_callback
, (gpointer
) NULL
);
399 static bool wxOKlibc()
401 #if defined(__UNIX__) && defined(__GLIBC__)
402 // glibc 2.0 uses UTF-8 even when it shouldn't
404 if ((MB_CUR_MAX
== 2) &&
405 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
408 // this is UTF-8 allright, check whether that's what we want
409 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
410 if ((strlen(cur_locale
) < 4) ||
411 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
412 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
413 // nope, don't use libc conversion
421 //-----------------------------------------------------------------------------
422 // Access to the root window global
423 //-----------------------------------------------------------------------------
425 GtkWidget
* wxGetRootWindow()
427 if (gs_RootWindow
== NULL
)
429 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
430 gtk_widget_realize( gs_RootWindow
);
432 return gs_RootWindow
;
435 //-----------------------------------------------------------------------------
437 //-----------------------------------------------------------------------------
439 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
444 m_isInAssert
= false;
445 #endif // __WXDEBUG__
449 wxapp_install_idle_handler();
452 g_main_set_poll_func( wxapp_poll_func
);
455 m_colorCube
= (unsigned char*) NULL
;
457 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
458 m_glVisualInfo
= NULL
;
464 if (m_idleTag
) gtk_idle_remove( m_idleTag
);
466 if (m_colorCube
) free(m_colorCube
);
469 bool wxApp::OnInitGui()
471 if ( !wxAppBase::OnInitGui() )
474 GdkVisual
*visual
= gdk_visual_get_system();
476 // if this is a wxGLApp (derived from wxApp), and we've already
477 // chosen a specific visual, then derive the GdkVisual from that
478 if (m_glVisualInfo
!= NULL
)
480 GdkVisual
* vis
= gdkx_visual_get(
481 ((XVisualInfo
*) m_glVisualInfo
) ->visualid
);
482 gtk_widget_set_default_visual( vis
);
484 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
485 gtk_widget_set_default_colormap( colormap
);
490 // On some machines, the default visual is just 256 colours, so
491 // we make sure we get the best. This can sometimes be wasteful.
494 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual
))
496 GdkVisual
* vis
= gdk_visual_get_best();
497 gtk_widget_set_default_visual( vis
);
499 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
500 gtk_widget_set_default_colormap( colormap
);
505 // Nothing to do for 15, 16, 24, 32 bit displays
506 if (visual
->depth
> 8) return TRUE
;
508 // initialize color cube for 8-bit color reduction dithering
510 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
512 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
514 for (int r
= 0; r
< 32; r
++)
516 for (int g
= 0; g
< 32; g
++)
518 for (int b
= 0; b
< 32; b
++)
520 int rr
= (r
<< 3) | (r
>> 2);
521 int gg
= (g
<< 3) | (g
>> 2);
522 int bb
= (b
<< 3) | (b
>> 2);
526 GdkColor
*colors
= cmap
->colors
;
531 for (int i
= 0; i
< cmap
->size
; i
++)
533 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
534 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
535 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
536 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
539 index
= i
; max
= sum
;
545 // assume 8-bit true or static colors. this really exists
546 GdkVisual
* vis
= gdk_colormap_get_visual( cmap
);
547 index
= (r
>> (5 - vis
->red_prec
)) << vis
->red_shift
;
548 index
|= (g
>> (5 - vis
->green_prec
)) << vis
->green_shift
;
549 index
|= (b
>> (5 - vis
->blue_prec
)) << vis
->blue_shift
;
551 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
559 GdkVisual
*wxApp::GetGdkVisual()
561 GdkVisual
*visual
= NULL
;
564 visual
= gdkx_visual_get( ((XVisualInfo
*) m_glVisualInfo
)->visualid
);
566 visual
= gdk_window_get_visual( wxGetRootWindow()->window
);
573 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
578 // GTK 1.2 up to version 1.2.3 has broken threads
579 if ((gtk_major_version
== 1) &&
580 (gtk_minor_version
== 2) &&
581 (gtk_micro_version
< 4))
583 printf( "wxWidgets warning: GUI threading disabled due to outdated GTK version\n" );
587 if (!g_thread_supported())
590 #endif // wxUSE_THREADS
594 // We should have the wxUSE_WCHAR_T test on the _outside_
596 wxConvCurrent
= &wxConvLocal
;
599 // gtk_init() wants UTF-8, not wchar_t, so convert
601 char **argvGTK
= new char *[argc
+ 1];
602 for ( i
= 0; i
< argc
; i
++ )
604 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv
[i
]));
607 argvGTK
[argc
] = NULL
;
612 init_result
= true; // is there a _check() version of this?
613 gpe_application_init( &argcGTK
, &argvGTK
);
615 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
618 if ( argcGTK
!= argc
)
620 // we have to drop the parameters which were consumed by GTK+
621 for ( i
= 0; i
< argcGTK
; i
++ )
623 while ( strcmp(wxConvUTF8
.cWX2MB(argv
[i
]), argvGTK
[i
]) != 0 )
625 memmove(argv
+ i
, argv
+ i
+ 1, argc
- i
);
631 //else: gtk_init() didn't modify our parameters
634 for ( i
= 0; i
< argcGTK
; i
++ )
640 #else // !wxUSE_UNICODE
641 // gtk_init() shouldn't actually change argv itself (just its contents) so
642 // it's ok to pass pointer to it
643 init_result
= gtk_init_check( &argc
, &argv
);
644 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
647 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
651 // we can not enter threads before gtk_init is done
654 if ( !wxAppBase::Initialize(argc
, argv
) )
661 wxSetDetectableAutoRepeat( TRUE
);
664 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
670 void wxApp::CleanUp()
674 wxAppBase::CleanUp();
679 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
683 wxAppBase::OnAssert(file
, line
, cond
, msg
);
685 m_isInAssert
= false;
688 #endif // __WXDEBUG__
690 void wxApp::RemoveIdleTag()
693 wxMutexLocker
lock(gs_idleTagsMutex
);
697 gtk_idle_remove( wxTheApp
->m_idleTag
);
698 wxTheApp
->m_idleTag
= 0;