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/gtk/win_gtk.h"
77 #include "wx/gtk/private.h"
81 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
85 bool g_mainThreadLocked
= FALSE
;
86 gint g_pendingTag
= 0;
88 static GtkWidget
*gs_RootWindow
= (GtkWidget
*) NULL
;
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
94 void wxapp_install_idle_handler();
97 static wxMutex gs_idleTagsMutex
;
100 //-----------------------------------------------------------------------------
102 //-----------------------------------------------------------------------------
104 // not static because used by textctrl.cpp
107 bool wxIsInsideYield
= FALSE
;
109 bool wxApp::Yield(bool onlyIfNeeded
)
111 if ( wxIsInsideYield
)
115 wxFAIL_MSG( wxT("wxYield called recursively" ) );
122 if ( !wxThread::IsMain() )
124 // can't call gtk_main_iteration() from other threads like this
127 #endif // wxUSE_THREADS
129 wxIsInsideYield
= TRUE
;
131 // We need to remove idle callbacks or the loop will
133 wxTheApp
->RemoveIdleTag();
136 // disable log flushing from here because a call to wxYield() shouldn't
137 // normally result in message boxes popping up &c
141 while (gtk_events_pending())
142 gtk_main_iteration();
144 // It's necessary to call ProcessIdle() to update the frames sizes which
145 // might have been changed (it also will update other things set from
146 // OnUpdateUI() which is a nice (and desired) side effect). But we
147 // call ProcessIdle() only once since this is not meant for longish
148 // background jobs (controlled by wxIdleEvent::RequestMore() and the
149 // return value of Processidle().
153 // let the logs be flashed again
157 wxIsInsideYield
= FALSE
;
162 //-----------------------------------------------------------------------------
164 //-----------------------------------------------------------------------------
166 // RR/KH: No wxMutexGui calls are needed here according to the GTK faq,
167 // http://www.gtk.org/faq/#AEN500 - this caused problems for wxPostEvent.
169 void wxApp::WakeUpIdle()
171 wxapp_install_idle_handler();
174 //-----------------------------------------------------------------------------
176 //-----------------------------------------------------------------------------
178 // the callback functions must be extern "C" to comply with GTK+ declarations
182 static gint
wxapp_pending_callback( gpointer
WXUNUSED(data
) )
184 if (!wxTheApp
) return TRUE
;
186 // When getting called from GDK's time-out handler
187 // we are no longer within GDK's grab on the GUI
188 // thread so we must lock it here ourselves.
191 // Sent idle event to all who request them.
192 wxTheApp
->ProcessPendingEvents();
196 wxMutexLocker
lock(gs_idleTagsMutex
);
201 // Flush the logged messages if any.
203 wxLog::FlushActive();
206 // Release lock again
209 // Return FALSE to indicate that no more idle events are
210 // to be sent (single shot instead of continuous stream)
214 static gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
220 // don't generate the idle events while the assert modal dialog is shown,
221 // this completely confuses the apps which don't expect to be reentered
222 // from some safely-looking functions
223 if ( wxTheApp
->IsInAssert() )
225 // But repaint the assertion message if necessary
226 if (wxTopLevelWindows
.GetCount() > 0)
228 wxWindow
* win
= (wxWindow
*) wxTopLevelWindows
.GetLast()->GetData();
229 if (win
->IsKindOf(CLASSINFO(wxMessageDialog
)))
230 win
->OnInternalIdle();
234 #endif // __WXDEBUG__
236 // When getting called from GDK's time-out handler
237 // we are no longer within GDK's grab on the GUI
238 // thread so we must lock it here ourselves.
241 // Indicate that we are now in idle mode and event handlers
242 // will have to reinstall the idle handler again.
245 wxMutexLocker
lock(gs_idleTagsMutex
);
248 wxTheApp
->m_idleTag
= 0;
253 // Send idle event to all who request them as long as
254 // no events have popped up in the event queue.
255 while ( (moreIdles
= wxTheApp
->ProcessIdle()) && gtk_events_pending() == 0)
258 // Release lock again
261 // Return FALSE if no more idle events are to be sent
269 #define wxPollFd pollfd
272 typedef GPollFD wxPollFd
;
274 int wxPoll(wxPollFd
*ufds
, unsigned int nfds
, int timeout
)
276 // convert timeout from ms to struct timeval (s/us)
278 tv_timeout
.tv_sec
= timeout
/1000;
279 tv_timeout
.tv_usec
= (timeout%1000
)*1000;
281 // remember the highest fd used here
284 // and fill the sets for select()
289 wxFD_ZERO(&writefds
);
290 wxFD_ZERO(&exceptfds
);
293 for ( i
= 0; i
< nfds
; i
++ )
295 wxASSERT_MSG( ufds
[i
].fd
< wxFD_SETSIZE
, _T("fd out of range") );
297 if ( ufds
[i
].events
& G_IO_IN
)
298 wxFD_SET(ufds
[i
].fd
, &readfds
);
300 if ( ufds
[i
].events
& G_IO_PRI
)
301 wxFD_SET(ufds
[i
].fd
, &exceptfds
);
303 if ( ufds
[i
].events
& G_IO_OUT
)
304 wxFD_SET(ufds
[i
].fd
, &writefds
);
306 if ( ufds
[i
].fd
> fdMax
)
311 int res
= select(fdMax
, &readfds
, &writefds
, &exceptfds
, &tv_timeout
);
313 // translate the results back
314 for ( i
= 0; i
< nfds
; i
++ )
318 if ( wxFD_ISSET(ufds
[i
].fd
, &readfds
) )
319 ufds
[i
].revents
|= G_IO_IN
;
321 if ( wxFD_ISSET(ufds
[i
].fd
, &exceptfds
) )
322 ufds
[i
].revents
|= G_IO_PRI
;
324 if ( wxFD_ISSET(ufds
[i
].fd
, &writefds
) )
325 ufds
[i
].revents
|= G_IO_OUT
;
331 #endif // HAVE_POLL/!HAVE_POLL
333 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
338 g_mainThreadLocked
= TRUE
;
340 // we rely on the fact that glib GPollFD struct is really just pollfd but
341 // I wonder how wise is this in the long term (VZ)
342 gint res
= wxPoll( (wxPollFd
*) ufds
, nfds
, timeout
);
345 g_mainThreadLocked
= FALSE
;
352 #endif // wxUSE_THREADS
356 void wxapp_install_idle_handler()
359 wxMutexLocker
lock(gs_idleTagsMutex
);
362 // Don't install the handler if it's already installed. This test *MUST*
363 // be done when gs_idleTagsMutex is locked!
367 // GD: this assert is raised when using the thread sample (which works)
368 // so the test is probably not so easy. Can widget callbacks be
369 // triggered from child threads and, if so, for which widgets?
370 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
372 wxASSERT_MSG( wxTheApp
->m_idleTag
== 0, wxT("attempt to install idle handler twice") );
376 if (g_pendingTag
== 0)
377 g_pendingTag
= g_idle_add_full( 900, wxapp_pending_callback
, NULL
, NULL
);
379 // This routine gets called by all event handlers
380 // indicating that the idle is over. It may also
381 // get called from other thread for sending events
382 // to the main thread (and processing these in
383 // idle time). Very low priority.
384 wxTheApp
->m_idleTag
= g_idle_add_full( 1000, wxapp_idle_callback
, NULL
, NULL
);
387 //-----------------------------------------------------------------------------
388 // Access to the root window global
389 //-----------------------------------------------------------------------------
391 GtkWidget
* wxGetRootWindow()
393 if (gs_RootWindow
== NULL
)
395 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
396 gtk_widget_realize( gs_RootWindow
);
398 return gs_RootWindow
;
401 //-----------------------------------------------------------------------------
403 //-----------------------------------------------------------------------------
405 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
407 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
408 EVT_IDLE(wxAppBase::OnIdle
)
414 m_isInAssert
= FALSE
;
415 #endif // __WXDEBUG__
419 wxapp_install_idle_handler();
422 g_main_context_set_poll_func( NULL
, wxapp_poll_func
);
425 m_colorCube
= (unsigned char*) NULL
;
427 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
428 m_glVisualInfo
= (void *) NULL
;
429 m_glFBCInfo
= (void *) NULL
;
435 g_source_remove( m_idleTag
);
441 bool wxApp::OnInitGui()
443 if ( !wxAppBase::OnInitGui() )
446 GdkVisual
*visual
= gdk_visual_get_system();
448 // if this is a wxGLApp (derived from wxApp), and we've already
449 // chosen a specific visual, then derive the GdkVisual from that
450 if (m_glVisualInfo
!= NULL
)
452 // seems gtk_widget_set_default_visual no longer exists?
453 GdkVisual
* vis
= gtk_widget_get_default_visual();
455 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
456 gtk_widget_set_default_colormap( colormap
);
461 // On some machines, the default visual is just 256 colours, so
462 // we make sure we get the best. This can sometimes be wasteful.
465 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual
))
467 /* seems gtk_widget_set_default_visual no longer exists? */
468 GdkVisual
* vis
= gtk_widget_get_default_visual();
470 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
471 gtk_widget_set_default_colormap( colormap
);
476 // Nothing to do for 15, 16, 24, 32 bit displays
477 if (visual
->depth
> 8) return TRUE
;
479 // initialize color cube for 8-bit color reduction dithering
481 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
483 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
485 for (int r
= 0; r
< 32; r
++)
487 for (int g
= 0; g
< 32; g
++)
489 for (int b
= 0; b
< 32; b
++)
491 int rr
= (r
<< 3) | (r
>> 2);
492 int gg
= (g
<< 3) | (g
>> 2);
493 int bb
= (b
<< 3) | (b
>> 2);
497 GdkColor
*colors
= cmap
->colors
;
502 for (int i
= 0; i
< cmap
->size
; i
++)
504 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
505 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
506 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
507 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
510 index
= i
; max
= sum
;
516 // assume 8-bit true or static colors. this really exists
517 GdkVisual
* vis
= gdk_colormap_get_visual( cmap
);
518 index
= (r
>> (5 - vis
->red_prec
)) << vis
->red_shift
;
519 index
|= (g
>> (5 - vis
->green_prec
)) << vis
->green_shift
;
520 index
|= (b
>> (5 - vis
->blue_prec
)) << vis
->blue_shift
;
522 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
530 GdkVisual
*wxApp::GetGdkVisual()
532 GdkVisual
*visual
= NULL
;
535 visual
= gdkx_visual_get( ((XVisualInfo
*) m_glVisualInfo
)->visualid
);
537 visual
= gdk_window_get_visual( wxGetRootWindow()->window
);
544 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
549 // GTK 1.2 up to version 1.2.3 has broken threads
550 if ((gtk_major_version
== 1) &&
551 (gtk_minor_version
== 2) &&
552 (gtk_micro_version
< 4))
554 printf( "wxWidgets warning: GUI threading disabled due to outdated GTK version\n" );
558 if (!g_thread_supported())
561 #endif // wxUSE_THREADS
565 // We should have the wxUSE_WCHAR_T test on the _outside_
567 // gtk+ 2.0 supports Unicode through UTF-8 strings
568 wxConvCurrent
= &wxConvUTF8
;
569 #else // !wxUSE_WCHAR_T
571 wxConvCurrent
= (wxMBConv
*) NULL
;
572 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
574 // decide which conversion to use for the file names
576 // (1) this variable exists for the sole purpose of specifying the encoding
577 // of the filenames for GTK+ programs, so use it if it is set
578 wxString
encName(wxGetenv(_T("G_FILENAME_ENCODING")));
579 encName
= encName
.BeforeFirst(_T(','));
580 if (encName
== _T("@locale"))
586 // (2) if a non default locale is set, assume that the user wants his
587 // filenames in this locale too
588 encName
= wxLocale::GetSystemEncodingName().Upper();
589 // (3) finally use UTF-8 by default
590 if (encName
.empty() || encName
== _T("US-ASCII"))
591 encName
= _T("UTF-8");
592 wxSetEnv(_T("G_FILENAME_ENCODING"), encName
);
596 encName
= _T("UTF-8");
598 static wxConvBrokenFileNames
fileconv(encName
);
599 wxConvFileName
= &fileconv
;
602 // gtk_init() wants UTF-8, not wchar_t, so convert
604 char **argvGTK
= new char *[argc
+ 1];
605 for ( i
= 0; i
< argc
; i
++ )
607 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv
[i
]));
610 argvGTK
[argc
] = NULL
;
615 init_result
= true; // is there a _check() version of this?
616 gpe_application_init( &argcGTK
, &argvGTK
);
618 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
621 if ( argcGTK
!= argc
)
623 // we have to drop the parameters which were consumed by GTK+
624 for ( i
= 0; i
< argcGTK
; i
++ )
626 while ( strcmp(wxConvUTF8
.cWX2MB(argv
[i
]), argvGTK
[i
]) != 0 )
628 memmove(argv
+ i
, argv
+ i
+ 1, argc
- i
);
634 //else: gtk_init() didn't modify our parameters
637 for ( i
= 0; i
< argcGTK
; i
++ )
643 #else // !wxUSE_UNICODE
644 // gtk_init() shouldn't actually change argv itself (just its contents) so
645 // it's ok to pass pointer to it
646 init_result
= gtk_init_check( &argc
, &argv
);
647 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
650 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
654 // we can not enter threads before gtk_init is done
657 if ( !wxAppBase::Initialize(argc
, argv
) )
664 wxSetDetectableAutoRepeat( TRUE
);
667 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
673 void wxApp::CleanUp()
677 wxAppBase::CleanUp();
682 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
686 wxAppBase::OnAssert(file
, line
, cond
, msg
);
688 m_isInAssert
= FALSE
;
691 #endif // __WXDEBUG__
693 void wxApp::RemoveIdleTag()
696 wxMutexLocker
lock(gs_idleTagsMutex
);
700 g_source_remove( wxTheApp
->m_idleTag
);
701 wxTheApp
->m_idleTag
= 0;