1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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/memory.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"
48 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
49 // in neither poll.h nor sys/poll.h which results in link errors later
62 // we implement poll() ourselves using select() which is supposed exist in
64 #include <sys/types.h>
67 #ifdef HAVE_SYS_SELECT_H
68 #include <sys/select.h>
70 #endif // HAVE_POLL/!HAVE_POLL
72 #include "wx/unix/private.h"
73 #include "wx/gtk/win_gtk.h"
74 #include "wx/gtk/private.h"
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
83 #include "wx/html/forcelnk.h"
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
91 bool g_mainThreadLocked
= false;
93 static GtkWidget
*gs_RootWindow
= (GtkWidget
*) NULL
;
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
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 SuspendIdleCallback();
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: No wxMutexGui calls are needed here according to the GTK faq,
172 // http://www.gtk.org/faq/#AEN500 - this caused problems for wxPostEvent.
174 void wxApp::WakeUpIdle()
176 wxapp_install_idle_handler();
179 //-----------------------------------------------------------------------------
181 //-----------------------------------------------------------------------------
183 // the callback functions must be extern "C" to comply with GTK+ declarations
187 // One-shot emission hook for "event" signal, to install idle handler.
188 // This will be called when the "event" signal is issued on any GtkWidget object.
190 event_emission_hook(GSignalInvocationHint
*, guint
, const GValue
*, gpointer
)
192 wxapp_install_idle_handler();
197 // add emission hook for "event" signal, to re-install idle handler when needed
198 static inline void wxAddEmissionHook()
200 GType widgetType
= GTK_TYPE_WIDGET
;
201 // if GtkWidget type is loaded
202 if (g_type_class_peek(widgetType
) != NULL
)
204 guint sig_id
= g_signal_lookup("event", widgetType
);
205 g_signal_add_emission_hook(sig_id
, 0, event_emission_hook
, NULL
, NULL
);
209 static gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
211 // this does not look possible, but just in case...
215 bool moreIdles
= false;
218 // don't generate the idle events while the assert modal dialog is shown,
219 // this matches the behavior of wxMSW
220 if (!wxTheApp
->IsInAssert())
221 #endif // __WXDEBUG__
223 // When getting called from GDK's time-out handler
224 // we are no longer within GDK's grab on the GUI
225 // thread so we must lock it here ourselves.
228 // Send idle event to all who request them as long as
229 // no events have popped up in the event queue.
231 moreIdles
= wxTheApp
->ProcessIdle();
232 } while (moreIdles
&& gtk_events_pending() == 0);
234 // Release lock again
241 wxMutexLocker
lock(gs_idleTagsMutex
);
243 // Indicate that we are now in idle mode and event handlers
244 // will have to reinstall the idle handler again.
246 wxTheApp
->m_idleTag
= 0;
251 // Return FALSE if no more idle events are to be sent
259 #define wxPollFd pollfd
262 typedef GPollFD wxPollFd
;
264 int wxPoll(wxPollFd
*ufds
, unsigned int nfds
, int timeout
)
266 // convert timeout from ms to struct timeval (s/us)
268 tv_timeout
.tv_sec
= timeout
/1000;
269 tv_timeout
.tv_usec
= (timeout%1000
)*1000;
271 // remember the highest fd used here
274 // and fill the sets for select()
279 wxFD_ZERO(&writefds
);
280 wxFD_ZERO(&exceptfds
);
283 for ( i
= 0; i
< nfds
; i
++ )
285 wxASSERT_MSG( ufds
[i
].fd
< FD_SETSIZE
, _T("fd out of range") );
287 if ( ufds
[i
].events
& G_IO_IN
)
288 wxFD_SET(ufds
[i
].fd
, &readfds
);
290 if ( ufds
[i
].events
& G_IO_PRI
)
291 wxFD_SET(ufds
[i
].fd
, &exceptfds
);
293 if ( ufds
[i
].events
& G_IO_OUT
)
294 wxFD_SET(ufds
[i
].fd
, &writefds
);
296 if ( ufds
[i
].fd
> fdMax
)
301 int res
= select(fdMax
, &readfds
, &writefds
, &exceptfds
, &tv_timeout
);
303 // translate the results back
304 for ( i
= 0; i
< nfds
; i
++ )
308 if ( wxFD_ISSET(ufds
[i
].fd
, &readfds
) )
309 ufds
[i
].revents
|= G_IO_IN
;
311 if ( wxFD_ISSET(ufds
[i
].fd
, &exceptfds
) )
312 ufds
[i
].revents
|= G_IO_PRI
;
314 if ( wxFD_ISSET(ufds
[i
].fd
, &writefds
) )
315 ufds
[i
].revents
|= G_IO_OUT
;
321 #endif // HAVE_POLL/!HAVE_POLL
323 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
328 g_mainThreadLocked
= true;
330 // we rely on the fact that glib GPollFD struct is really just pollfd but
331 // I wonder how wise is this in the long term (VZ)
332 gint res
= wxPoll( (wxPollFd
*) ufds
, nfds
, timeout
);
335 g_mainThreadLocked
= false;
342 #endif // wxUSE_THREADS
346 void wxapp_install_idle_handler()
348 if (wxTheApp
== NULL
)
352 wxMutexLocker
lock(gs_idleTagsMutex
);
355 // Don't install the handler if it's already installed. This test *MUST*
356 // be done when gs_idleTagsMutex is locked!
360 // GD: this assert is raised when using the thread sample (which works)
361 // so the test is probably not so easy. Can widget callbacks be
362 // triggered from child threads and, if so, for which widgets?
363 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
365 wxASSERT_MSG( wxTheApp
->m_idleTag
== 0, wxT("attempt to install idle handler twice") );
369 // This routine gets called by all event handlers
370 // indicating that the idle is over. It may also
371 // get called from other thread for sending events
372 // to the main thread (and processing these in
373 // idle time). Very low priority.
374 wxTheApp
->m_idleTag
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
377 //-----------------------------------------------------------------------------
378 // Access to the root window global
379 //-----------------------------------------------------------------------------
381 GtkWidget
* wxGetRootWindow()
383 if (gs_RootWindow
== NULL
)
385 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
386 gtk_widget_realize( gs_RootWindow
);
388 return gs_RootWindow
;
391 //-----------------------------------------------------------------------------
393 //-----------------------------------------------------------------------------
395 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
397 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
398 EVT_IDLE(wxAppBase::OnIdle
)
404 m_isInAssert
= false;
405 #endif // __WXDEBUG__
409 wxapp_install_idle_handler();
412 g_main_context_set_poll_func( NULL
, wxapp_poll_func
);
415 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
416 m_glVisualInfo
= (void *) NULL
;
417 m_glFBCInfo
= (void *) NULL
;
423 g_source_remove( m_idleTag
);
426 bool wxApp::OnInitGui()
428 if ( !wxAppBase::OnInitGui() )
431 // if this is a wxGLApp (derived from wxApp), and we've already
432 // chosen a specific visual, then derive the GdkVisual from that
433 if (m_glVisualInfo
!= NULL
)
435 GdkVisual
* vis
= gtk_widget_get_default_visual();
437 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
438 gtk_widget_set_default_colormap( colormap
);
442 // On some machines, the default visual is just 256 colours, so
443 // we make sure we get the best. This can sometimes be wasteful.
446 if (m_forceTrueColour
)
448 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
451 wxLogError(wxT("Unable to initialize TrueColor visual."));
454 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
455 gtk_widget_set_default_colormap( colormap
);
459 if (gdk_visual_get_best() != gdk_visual_get_system())
461 GdkVisual
* visual
= gdk_visual_get_best();
462 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
463 gtk_widget_set_default_colormap( colormap
);
472 GdkVisual
*wxApp::GetGdkVisual()
474 GdkVisual
*visual
= NULL
;
477 visual
= gdkx_visual_get( ((XVisualInfo
*) m_glVisualInfo
)->visualid
);
479 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
486 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
491 if (!g_thread_supported())
493 #endif // wxUSE_THREADS
497 // We should have the wxUSE_WCHAR_T test on the _outside_
499 // gtk+ 2.0 supports Unicode through UTF-8 strings
500 wxConvCurrent
= &wxConvUTF8
;
501 #else // !wxUSE_WCHAR_T
503 wxConvCurrent
= (wxMBConv
*) NULL
;
504 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
506 // decide which conversion to use for the file names
508 // (1) this variable exists for the sole purpose of specifying the encoding
509 // of the filenames for GTK+ programs, so use it if it is set
510 wxString
encName(wxGetenv(_T("G_FILENAME_ENCODING")));
511 encName
= encName
.BeforeFirst(_T(','));
512 if (encName
.CmpNoCase(_T("@locale")) == 0)
518 // (2) if a non default locale is set, assume that the user wants his
519 // filenames in this locale too
520 encName
= wxLocale::GetSystemEncodingName().Upper();
521 // (3) finally use UTF-8 by default
522 if (encName
.empty() || encName
== _T("US-ASCII"))
523 encName
= _T("UTF-8");
524 wxSetEnv(_T("G_FILENAME_ENCODING"), encName
);
528 encName
= _T("UTF-8");
530 static wxConvBrokenFileNames
fileconv(encName
);
531 wxConvFileName
= &fileconv
;
534 // gtk_init() wants UTF-8, not wchar_t, so convert
536 char **argvGTK
= new char *[argc
+ 1];
537 for ( i
= 0; i
< argc
; i
++ )
539 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv
[i
]));
542 argvGTK
[argc
] = NULL
;
547 init_result
= true; // is there a _check() version of this?
548 gpe_application_init( &argcGTK
, &argvGTK
);
550 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
553 if ( argcGTK
!= argc
)
555 // we have to drop the parameters which were consumed by GTK+
556 for ( i
= 0; i
< argcGTK
; i
++ )
558 while ( strcmp(wxConvUTF8
.cWX2MB(argv
[i
]), argvGTK
[i
]) != 0 )
560 memmove(argv
+ i
, argv
+ i
+ 1, argc
- i
);
566 //else: gtk_init() didn't modify our parameters
569 for ( i
= 0; i
< argcGTK
; i
++ )
575 #else // !wxUSE_UNICODE
576 // gtk_init() shouldn't actually change argv itself (just its contents) so
577 // it's ok to pass pointer to it
578 init_result
= gtk_init_check( &argc
, &argv
);
579 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
582 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
586 // we can not enter threads before gtk_init is done
589 if ( !wxAppBase::Initialize(argc
, argv
) )
596 wxSetDetectableAutoRepeat( true );
599 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
605 void wxApp::CleanUp()
609 wxAppBase::CleanUp();
614 void wxApp::OnAssertFailure(const wxChar
*file
,
621 // block wx idle events while assert dialog is showing
624 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
626 m_isInAssert
= false;
629 #endif // __WXDEBUG__
631 void wxApp::SuspendIdleCallback()
634 wxMutexLocker
lock(gs_idleTagsMutex
);
638 g_source_remove(m_idleTag
);