1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "app.h"
15 // vms_jackets.h should for proper working be included before anything else
16 # include <vms_jackets.h>
17 #undef ConnectionNumber
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
24 #include "wx/gdicmn.h"
28 #include "wx/memory.h"
30 #include "wx/settings.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.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/gtk/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
;
138 // We need to remove idle callbacks or the loop will
140 wxTheApp
->RemoveIdleTag();
144 // disable log flushing from here because a call to wxYield() shouldn't
145 // normally result in message boxes popping up &c
148 while (gtk_events_pending())
149 gtk_main_iteration();
151 // It's necessary to call ProcessIdle() to update the frames sizes which
152 // might have been changed (it also will update other things set from
153 // OnUpdateUI() which is a nice (and desired) side effect). But we
154 // call ProcessIdle() only once since this is not meant for longish
155 // background jobs (controlled by wxIdleEvent::RequestMore() and the
156 // return value of Processidle().
159 // 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()
182 if (!wxThread::IsMain())
184 #endif // wxUSE_THREADS_
188 wxapp_install_idle_handler();
192 if (!wxThread::IsMain())
194 #endif // wxUSE_THREADS_
198 //-----------------------------------------------------------------------------
200 //-----------------------------------------------------------------------------
202 // the callback functions must be extern "C" to comply with GTK+ declarations
206 static gint
wxapp_pending_callback( gpointer
WXUNUSED(data
) )
208 if (!wxTheApp
) return TRUE
;
210 // When getting called from GDK's time-out handler
211 // we are no longer within GDK's grab on the GUI
212 // thread so we must lock it here ourselves.
215 // Sent idle event to all who request them.
216 wxTheApp
->ProcessPendingEvents();
220 wxMutexLocker
lock(gs_idleTagsMutex
);
225 // Flush the logged messages if any.
227 wxLog::FlushActive();
230 // Release lock again
233 // Return FALSE to indicate that no more idle events are
234 // to be sent (single shot instead of continuous stream)
238 static gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
244 // don't generate the idle events while the assert modal dialog is shown,
245 // this completely confuses the apps which don't expect to be reentered
246 // from some safely-looking functions
247 if ( wxTheApp
->IsInAssert() )
249 // But repaint the assertion message if necessary
250 if (wxTopLevelWindows
.GetCount() > 0)
252 wxWindow
* win
= (wxWindow
*) wxTopLevelWindows
.GetLast()->GetData();
254 if (win
->IsKindOf(CLASSINFO(wxMessageDialog
)))
256 if (win
->IsKindOf(CLASSINFO(wxGenericMessageDialog
)))
258 win
->OnInternalIdle();
262 #endif // __WXDEBUG__
264 // When getting called from GDK's time-out handler
265 // we are no longer within GDK's grab on the GUI
266 // thread so we must lock it here ourselves.
269 // Indicate that we are now in idle mode and event handlers
270 // will have to reinstall the idle handler again.
273 wxMutexLocker
lock(gs_idleTagsMutex
);
276 wxTheApp
->m_idleTag
= 0;
279 // Send idle event to all who request them as long as
280 // no events have popped up in the event queue.
281 while (wxTheApp
->ProcessIdle() && (gtk_events_pending() == 0))
284 // Release lock again
287 // Return FALSE to indicate that no more idle events are
288 // to be sent (single shot instead of continuous stream).
296 #define wxPollFd pollfd
299 typedef GPollFD wxPollFd
;
301 int wxPoll(wxPollFd
*ufds
, unsigned int nfds
, int timeout
)
303 // convert timeout from ms to struct timeval (s/us)
305 tv_timeout
.tv_sec
= timeout
/1000;
306 tv_timeout
.tv_usec
= (timeout%1000
)*1000;
308 // remember the highest fd used here
311 // and fill the sets for select()
320 for ( i
= 0; i
< nfds
; i
++ )
322 wxASSERT_MSG( ufds
[i
].fd
< FD_SETSIZE
, _T("fd out of range") );
324 if ( ufds
[i
].events
& G_IO_IN
)
325 FD_SET(ufds
[i
].fd
, &readfds
);
327 if ( ufds
[i
].events
& G_IO_PRI
)
328 FD_SET(ufds
[i
].fd
, &exceptfds
);
330 if ( ufds
[i
].events
& G_IO_OUT
)
331 FD_SET(ufds
[i
].fd
, &writefds
);
333 if ( ufds
[i
].fd
> fdMax
)
338 int res
= select(fdMax
, &readfds
, &writefds
, &exceptfds
, &tv_timeout
);
340 // translate the results back
341 for ( i
= 0; i
< nfds
; i
++ )
345 if ( FD_ISSET(ufds
[i
].fd
, &readfds
) )
346 ufds
[i
].revents
|= G_IO_IN
;
348 if ( FD_ISSET(ufds
[i
].fd
, &exceptfds
) )
349 ufds
[i
].revents
|= G_IO_PRI
;
351 if ( FD_ISSET(ufds
[i
].fd
, &writefds
) )
352 ufds
[i
].revents
|= G_IO_OUT
;
358 #endif // HAVE_POLL/!HAVE_POLL
360 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
365 g_mainThreadLocked
= TRUE
;
367 // we rely on the fact that glib GPollFD struct is really just pollfd but
368 // I wonder how wise is this in the long term (VZ)
369 gint res
= wxPoll( (wxPollFd
*) ufds
, nfds
, timeout
);
372 g_mainThreadLocked
= FALSE
;
379 #endif // wxUSE_THREADS
383 void wxapp_install_idle_handler()
386 wxMutexLocker
lock(gs_idleTagsMutex
);
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__
440 wxapp_install_idle_handler();
443 g_main_set_poll_func( wxapp_poll_func
);
446 m_colorCube
= (unsigned char*) NULL
;
448 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
449 m_glVisualInfo
= (void *) NULL
;
454 if (m_idleTag
) gtk_idle_remove( m_idleTag
);
456 if (m_colorCube
) free(m_colorCube
);
459 bool wxApp::OnInitGui()
461 if ( !wxAppBase::OnInitGui() )
464 GdkVisual
*visual
= gdk_visual_get_system();
466 // if this is a wxGLApp (derived from wxApp), and we've already
467 // chosen a specific visual, then derive the GdkVisual from that
468 if (m_glVisualInfo
!= NULL
)
471 // seems gtk_widget_set_default_visual no longer exists?
472 GdkVisual
* vis
= gtk_widget_get_default_visual();
474 GdkVisual
* vis
= gdkx_visual_get(
475 ((XVisualInfo
*) m_glVisualInfo
) ->visualid
);
476 gtk_widget_set_default_visual( vis
);
479 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
480 gtk_widget_set_default_colormap( colormap
);
485 // On some machines, the default visual is just 256 colours, so
486 // we make sure we get the best. This can sometimes be wasteful.
489 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual
))
492 /* seems gtk_widget_set_default_visual no longer exists? */
493 GdkVisual
* vis
= gtk_widget_get_default_visual();
495 GdkVisual
* vis
= gdk_visual_get_best();
496 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 #if defined(__WXGTK20__)
597 // gtk+ 2.0 supports Unicode through UTF-8 strings
598 wxConvCurrent
= &wxConvUTF8
;
601 wxConvCurrent
= &wxConvLocal
;
603 #else // !wxUSE_WCHAR_T
605 wxConvCurrent
= (wxMBConv
*) NULL
;
606 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
609 // gtk_init() wants UTF-8, not wchar_t, so convert
611 char **argvGTK
= new char *[argc
+ 1];
612 for ( i
= 0; i
< argc
; i
++ )
614 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv
[i
]));
617 argvGTK
[argc
] = NULL
;
622 init_result
= true; // is there a _check() version of this?
623 gpe_application_init( &argcGTK
, &argvGTK
);
625 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
628 if ( argcGTK
!= argc
)
630 // we have to drop the parameters which were consumed by GTK+
631 for ( i
= 0; i
< argcGTK
; i
++ )
633 while ( strcmp(wxConvUTF8
.cWX2MB(argv
[i
]), argvGTK
[i
]) != 0 )
635 memmove(argv
+ i
, argv
+ i
+ 1, argc
- i
);
641 //else: gtk_init() didn't modify our parameters
644 for ( i
= 0; i
< argcGTK
; i
++ )
650 #else // !wxUSE_UNICODE
651 // gtk_init() shouldn't actually change argv itself (just its contents) so
652 // it's ok to pass pointer to it
653 init_result
= gtk_init_check( &argc
, &argv
);
654 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
657 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
661 // we can not enter threads before gtk_init is done
664 if ( !wxAppBase::Initialize(argc
, argv
) )
671 wxSetDetectableAutoRepeat( TRUE
);
674 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
680 void wxApp::CleanUp()
684 wxAppBase::CleanUp();
689 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
693 wxAppBase::OnAssert(file
, line
, cond
, msg
);
695 m_isInAssert
= FALSE
;
698 #endif // __WXDEBUG__
700 void wxApp::RemoveIdleTag()
703 wxMutexLocker
lock(gs_idleTagsMutex
);
705 gtk_idle_remove( wxTheApp
->m_idleTag
);
706 wxTheApp
->m_idleTag
= 0;