1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/app.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling, Julian Smart
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
18 #include "wx/dialog.h"
19 #include "wx/settings.h"
20 #include "wx/msgdlg.h"
21 #include "wx/memory.h"
23 #include "wx/gdicmn.h"
25 #include "wx/module.h"
29 #include "wx/filename.h"
30 #include "wx/thread.h"
36 #ifdef __WXUNIVERSAL__
37 #include "wx/univ/theme.h"
38 #include "wx/univ/renderer.h"
42 #include "wx/thread.h"
51 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
52 // in neither poll.h nor sys/poll.h which results in link errors later
65 // we implement poll() ourselves using select() which is supposed exist in
67 #include <sys/types.h>
70 #ifdef HAVE_SYS_SELECT_H
71 #include <sys/select.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
= NULL
;
89 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
95 void wxapp_install_idle_handler();
98 static wxMutex gs_idleTagsMutex
;
102 //-----------------------------------------------------------------------------
104 //-----------------------------------------------------------------------------
106 // RR/KH: The wxMutexGui calls are not needed on GTK2 according to
107 // the GTK faq, http://www.gtk.org/faq/#AEN500
108 // The calls to gdk_threads_enter() and leave() are specifically noted
109 // as not being necessary. The MutexGui calls are still left in for GTK1.
110 // Eliminating the MutexGui calls fixes the long-standing "random" lockup
111 // when using wxPostEvent (which calls WakeUpIdle) from a thread.
113 void wxApp
::WakeUpIdle()
116 if (!wxThread
::IsMain())
118 #endif // wxUSE_THREADS
120 wxapp_install_idle_handler();
123 if (!wxThread
::IsMain())
125 #endif // wxUSE_THREADS
128 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
132 // the callback functions must be extern "C" to comply with GTK+ declarations
136 static gint
wxapp_pending_callback( gpointer
WXUNUSED(data
) )
138 if (!wxTheApp
) return TRUE
;
140 // When getting called from GDK's time-out handler
141 // we are no longer within GDK's grab on the GUI
142 // thread so we must lock it here ourselves.
145 // Sent idle event to all who request them.
146 wxTheApp
->ProcessPendingEvents();
150 wxMutexLocker
lock(gs_idleTagsMutex
);
155 // Flush the logged messages if any.
157 wxLog
::FlushActive();
160 // Release lock again
163 // Return FALSE to indicate that no more idle events are
164 // to be sent (single shot instead of continuous stream)
168 static gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
174 // don't generate the idle events while the assert modal dialog is shown,
175 // this completely confuses the apps which don't expect to be reentered
176 // from some safely-looking functions
177 if ( wxTheApp
->IsInAssert() )
179 // But repaint the assertion message if necessary
180 if (wxTopLevelWindows
.GetCount() > 0)
182 wxWindow
* win
= (wxWindow
*) wxTopLevelWindows
.GetLast()->GetData();
183 if (win
->IsKindOf(CLASSINFO(wxGenericMessageDialog
)))
184 win
->OnInternalIdle();
188 #endif // wxDEBUG_LEVEL
190 // When getting called from GDK's time-out handler
191 // we are no longer within GDK's grab on the GUI
192 // thread so we must lock it here ourselves.
195 // Indicate that we are now in idle mode and event handlers
196 // will have to reinstall the idle handler again.
199 wxMutexLocker
lock(gs_idleTagsMutex
);
202 wxTheApp
->m_idleTag
= 0;
205 // Send idle event to all who request them as long as
206 // no events have popped up in the event queue.
207 while (wxTheApp
->ProcessIdle() && (gtk_events_pending() == 0))
210 // Release lock again
213 // Return FALSE to indicate that no more idle events are
214 // to be sent (single shot instead of continuous stream).
222 #define wxPollFd pollfd
225 typedef GPollFD wxPollFd
;
227 int wxPoll(wxPollFd
*ufds
, unsigned int nfds
, int timeout
)
229 // convert timeout from ms to struct timeval (s/us)
231 tv_timeout
.tv_sec
= timeout
/1000;
232 tv_timeout
.tv_usec
= (timeout
%1000)*1000;
234 // remember the highest fd used here
237 // and fill the sets for select()
242 wxFD_ZERO(&writefds
);
243 wxFD_ZERO(&exceptfds
);
246 for ( i
= 0; i
< nfds
; i
++ )
248 wxASSERT_MSG( ufds
[i
].fd
< FD_SETSIZE
, wxT("fd out of range") );
250 if ( ufds
[i
].events
& G_IO_IN
)
251 wxFD_SET(ufds
[i
].fd
, &readfds
);
253 if ( ufds
[i
].events
& G_IO_PRI
)
254 wxFD_SET(ufds
[i
].fd
, &exceptfds
);
256 if ( ufds
[i
].events
& G_IO_OUT
)
257 wxFD_SET(ufds
[i
].fd
, &writefds
);
259 if ( ufds
[i
].fd
> fdMax
)
264 int res
= select(fdMax
, &readfds
, &writefds
, &exceptfds
, &tv_timeout
);
266 // translate the results back
267 for ( i
= 0; i
< nfds
; i
++ )
271 if ( wxFD_ISSET(ufds
[i
].fd
, &readfds
) )
272 ufds
[i
].revents
|= G_IO_IN
;
274 if ( wxFD_ISSET(ufds
[i
].fd
, &exceptfds
) )
275 ufds
[i
].revents
|= G_IO_PRI
;
277 if ( wxFD_ISSET(ufds
[i
].fd
, &writefds
) )
278 ufds
[i
].revents
|= G_IO_OUT
;
284 #endif // HAVE_POLL/!HAVE_POLL
286 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
291 g_mainThreadLocked
= true;
293 // we rely on the fact that glib GPollFD struct is really just pollfd but
294 // I wonder how wise is this in the long term (VZ)
295 gint res
= wxPoll( (wxPollFd
*) ufds
, nfds
, timeout
);
298 g_mainThreadLocked
= false;
305 #endif // wxUSE_THREADS
309 void wxapp_install_idle_handler()
312 wxMutexLocker
lock(gs_idleTagsMutex
);
315 // Don't install the handler if it's already installed. This test *MUST*
316 // be done when gs_idleTagsMutex is locked!
320 // GD: this assert is raised when using the thread sample (which works)
321 // so the test is probably not so easy. Can widget callbacks be
322 // triggered from child threads and, if so, for which widgets?
323 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
325 wxASSERT_MSG( wxTheApp
->m_idleTag
== 0, wxT("attempt to install idle handler twice") );
329 if (g_pendingTag
== 0)
330 g_pendingTag
= gtk_idle_add_priority( 900, wxapp_pending_callback
, (gpointer
) NULL
);
332 // This routine gets called by all event handlers
333 // indicating that the idle is over. It may also
334 // get called from other thread for sending events
335 // to the main thread (and processing these in
336 // idle time). Very low priority.
337 wxTheApp
->m_idleTag
= gtk_idle_add_priority( 1000, wxapp_idle_callback
, (gpointer
) NULL
);
340 static bool wxOKlibc()
342 #if defined(__UNIX__) && defined(__GLIBC__)
343 // glibc 2.0 uses UTF-8 even when it shouldn't
345 if ((MB_CUR_MAX
== 2) &&
346 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
349 // this is UTF-8 allright, check whether that's what we want
350 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
351 if ((strlen(cur_locale
) < 4) ||
352 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
353 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
354 // nope, don't use libc conversion
362 //-----------------------------------------------------------------------------
363 // Access to the root window global
364 //-----------------------------------------------------------------------------
366 GtkWidget
* wxGetRootWindow()
368 if (gs_RootWindow
== NULL
)
370 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
371 gtk_widget_realize( gs_RootWindow
);
373 return gs_RootWindow
;
376 //-----------------------------------------------------------------------------
378 //-----------------------------------------------------------------------------
380 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
384 m_isInAssert
= false;
388 wxapp_install_idle_handler();
391 g_main_set_poll_func( wxapp_poll_func
);
394 m_colorCube
= (unsigned char*) NULL
;
396 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
397 m_glVisualInfo
= NULL
;
403 if (m_idleTag
) gtk_idle_remove( m_idleTag
);
405 if (m_colorCube
) free(m_colorCube
);
408 bool wxApp
::OnInitGui()
410 if ( !wxAppBase
::OnInitGui() )
413 GdkVisual
*visual
= gdk_visual_get_system();
415 // if this is a wxGLApp (derived from wxApp), and we've already
416 // chosen a specific visual, then derive the GdkVisual from that
417 if (m_glVisualInfo
!= NULL
)
419 GdkVisual
* vis
= gdkx_visual_get(
420 ((XVisualInfo
*) m_glVisualInfo
) ->visualid
);
421 gtk_widget_set_default_visual( vis
);
423 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
424 gtk_widget_set_default_colormap( colormap
);
429 // On some machines, the default visual is just 256 colours, so
430 // we make sure we get the best. This can sometimes be wasteful.
433 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual
))
435 GdkVisual
* vis
= gdk_visual_get_best();
436 gtk_widget_set_default_visual( vis
);
438 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
439 gtk_widget_set_default_colormap( colormap
);
444 // Nothing to do for 15, 16, 24, 32 bit displays
445 if (visual
->depth
> 8) return TRUE
;
447 // initialize color cube for 8-bit color reduction dithering
449 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
451 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
453 for (int r
= 0; r
< 32; r
++)
455 for (int g
= 0; g
< 32; g
++)
457 for (int b
= 0; b
< 32; b
++)
459 int rr
= (r
<< 3) | (r
>> 2);
460 int gg
= (g
<< 3) | (g
>> 2);
461 int bb
= (b
<< 3) | (b
>> 2);
465 GdkColor
*colors
= cmap
->colors
;
470 for (int i
= 0; i
< cmap
->size
; i
++)
472 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
473 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
474 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
475 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
478 index
= i
; max
= sum
;
484 // assume 8-bit true or static colors. this really exists
485 GdkVisual
* vis
= gdk_colormap_get_visual( cmap
);
486 index
= (r
>> (5 - vis
->red_prec
)) << vis
->red_shift
;
487 index
|= (g
>> (5 - vis
->green_prec
)) << vis
->green_shift
;
488 index
|= (b
>> (5 - vis
->blue_prec
)) << vis
->blue_shift
;
490 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
498 GdkVisual
*wxApp
::GetGdkVisual()
500 GdkVisual
*visual
= NULL
;
503 visual
= gdkx_visual_get( ((XVisualInfo
*) m_glVisualInfo
)->visualid
);
505 visual
= gdk_window_get_visual( wxGetRootWindow()->window
);
512 bool wxApp
::Initialize(int& argc
, wxChar
**argv
)
517 // GTK 1.2 up to version 1.2.3 has broken threads
518 if ((gtk_major_version
== 1) &&
519 (gtk_minor_version
== 2) &&
520 (gtk_micro_version
< 4))
522 printf( "wxWidgets warning: GUI threading disabled due to outdated GTK version\n" );
526 if (!g_thread_supported())
529 #endif // wxUSE_THREADS
534 wxConvCurrent
= &wxConvLocal
;
537 // gtk_init() wants UTF-8, not wchar_t, so convert
539 char **argvGTK
= new char *[argc
+ 1];
540 for ( i
= 0; i
< argc
; i
++ )
542 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv
[i
]));
545 argvGTK
[argc
] = NULL
;
550 init_result
= true; // is there a _check() version of this?
551 gpe_application_init( &argcGTK
, &argvGTK
);
553 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
556 if ( argcGTK
!= argc
)
558 // we have to drop the parameters which were consumed by GTK+
559 for ( i
= 0; i
< argcGTK
; i
++ )
561 while ( strcmp(wxConvUTF8
.cWX2MB(argv
[i
]), argvGTK
[i
]) != 0 )
563 memmove(argv
+ i
, argv
+ i
+ 1, argc
- i
);
569 //else: gtk_init() didn't modify our parameters
572 for ( i
= 0; i
< argcGTK
; i
++ )
578 #else // !wxUSE_UNICODE
579 // gtk_init() shouldn't actually change argv itself (just its contents) so
580 // it's ok to pass pointer to it
581 init_result
= gtk_init_check( &argc
, &argv
);
582 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
585 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
589 // we cannot enter threads before gtk_init is done
592 if ( !wxAppBase
::Initialize(argc
, argv
) )
599 wxSetDetectableAutoRepeat( TRUE
);
602 wxFont
::SetDefaultEncoding(wxLocale
::GetSystemEncoding());
608 void wxApp
::CleanUp()
612 wxAppBase
::CleanUp();
615 void wxApp
::OnAssertFailure(const wxChar
*file
,
621 // there is no need to do anything if asserts are disabled in this build
624 // block wx idle events while assert dialog is showing
627 wxAppBase
::OnAssertFailure(file
, line
, func
, cond
, msg
);
629 m_isInAssert
= false;
630 #else // !wxDEBUG_LEVEL
636 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
639 void wxApp
::RemoveIdleTag()
642 wxMutexLocker
lock(gs_idleTagsMutex
);
646 gtk_idle_remove( wxTheApp
->m_idleTag
);
647 wxTheApp
->m_idleTag
= 0;