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/dialog.h"
26 #include "wx/settings.h"
27 #include "wx/msgdlg.h"
28 #include "wx/memory.h"
32 #include "wx/gdicmn.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"
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 //-----------------------------------------------------------------------------
86 #include "wx/html/forcelnk.h"
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
94 bool g_mainThreadLocked
= false;
95 gint g_pendingTag
= 0;
97 static GtkWidget
*gs_RootWindow
= (GtkWidget
*) NULL
;
99 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
103 void wxapp_install_idle_handler();
106 static wxMutex gs_idleTagsMutex
;
109 //-----------------------------------------------------------------------------
111 //-----------------------------------------------------------------------------
113 // not static because used by textctrl.cpp
116 bool wxIsInsideYield
= false;
118 bool wxApp::Yield(bool onlyIfNeeded
)
120 if ( wxIsInsideYield
)
124 wxFAIL_MSG( wxT("wxYield called recursively" ) );
131 if ( !wxThread::IsMain() )
133 // can't call gtk_main_iteration() from other threads like this
136 #endif // wxUSE_THREADS
138 wxIsInsideYield
= true;
140 // We need to remove idle callbacks or the loop will
142 wxTheApp
->RemoveIdleTag();
145 // disable log flushing from here because a call to wxYield() shouldn't
146 // normally result in message boxes popping up &c
150 while (gtk_events_pending())
151 gtk_main_iteration();
153 // It's necessary to call ProcessIdle() to update the frames sizes which
154 // might have been changed (it also will update other things set from
155 // OnUpdateUI() which is a nice (and desired) side effect). But we
156 // call ProcessIdle() only once since this is not meant for longish
157 // background jobs (controlled by wxIdleEvent::RequestMore() and the
158 // return value of Processidle().
162 // let the logs be flashed again
166 wxIsInsideYield
= false;
171 //-----------------------------------------------------------------------------
173 //-----------------------------------------------------------------------------
175 // RR/KH: No wxMutexGui calls are needed here according to the GTK faq,
176 // http://www.gtk.org/faq/#AEN500 - this caused problems for wxPostEvent.
178 void wxApp::WakeUpIdle()
180 wxapp_install_idle_handler();
183 //-----------------------------------------------------------------------------
185 //-----------------------------------------------------------------------------
187 // the callback functions must be extern "C" to comply with GTK+ declarations
191 static gint
wxapp_pending_callback( gpointer
WXUNUSED(data
) )
193 if (!wxTheApp
) return TRUE
;
195 // When getting called from GDK's time-out handler
196 // we are no longer within GDK's grab on the GUI
197 // thread so we must lock it here ourselves.
200 // Sent idle event to all who request them.
201 wxTheApp
->ProcessPendingEvents();
205 wxMutexLocker
lock(gs_idleTagsMutex
);
210 // Flush the logged messages if any.
212 wxLog::FlushActive();
215 // Release lock again
218 // Return FALSE to indicate that no more idle events are
219 // to be sent (single shot instead of continuous stream)
223 static gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
229 // don't generate the idle events while the assert modal dialog is shown,
230 // this completely confuses the apps which don't expect to be reentered
231 // from some safely-looking functions
232 if ( wxTheApp
->IsInAssert() )
234 // But repaint the assertion message if necessary
235 if (wxTopLevelWindows
.GetCount() > 0)
237 wxWindow
* win
= (wxWindow
*) wxTopLevelWindows
.GetLast()->GetData();
238 if (win
->IsKindOf(CLASSINFO(wxMessageDialog
)))
239 win
->OnInternalIdle();
243 #endif // __WXDEBUG__
245 // When getting called from GDK's time-out handler
246 // we are no longer within GDK's grab on the GUI
247 // thread so we must lock it here ourselves.
250 // Indicate that we are now in idle mode and event handlers
251 // will have to reinstall the idle handler again.
254 wxMutexLocker
lock(gs_idleTagsMutex
);
257 wxTheApp
->m_idleTag
= 0;
262 // Send idle event to all who request them as long as
263 // no events have popped up in the event queue.
264 while ( (moreIdles
= wxTheApp
->ProcessIdle()) && gtk_events_pending() == 0)
267 // Release lock again
270 // Return FALSE if no more idle events are to be sent
278 #define wxPollFd pollfd
281 typedef GPollFD wxPollFd
;
283 int wxPoll(wxPollFd
*ufds
, unsigned int nfds
, int timeout
)
285 // convert timeout from ms to struct timeval (s/us)
287 tv_timeout
.tv_sec
= timeout
/1000;
288 tv_timeout
.tv_usec
= (timeout%1000
)*1000;
290 // remember the highest fd used here
293 // and fill the sets for select()
298 wxFD_ZERO(&writefds
);
299 wxFD_ZERO(&exceptfds
);
302 for ( i
= 0; i
< nfds
; i
++ )
304 wxASSERT_MSG( ufds
[i
].fd
< wxFD_SETSIZE
, _T("fd out of range") );
306 if ( ufds
[i
].events
& G_IO_IN
)
307 wxFD_SET(ufds
[i
].fd
, &readfds
);
309 if ( ufds
[i
].events
& G_IO_PRI
)
310 wxFD_SET(ufds
[i
].fd
, &exceptfds
);
312 if ( ufds
[i
].events
& G_IO_OUT
)
313 wxFD_SET(ufds
[i
].fd
, &writefds
);
315 if ( ufds
[i
].fd
> fdMax
)
320 int res
= select(fdMax
, &readfds
, &writefds
, &exceptfds
, &tv_timeout
);
322 // translate the results back
323 for ( i
= 0; i
< nfds
; i
++ )
327 if ( wxFD_ISSET(ufds
[i
].fd
, &readfds
) )
328 ufds
[i
].revents
|= G_IO_IN
;
330 if ( wxFD_ISSET(ufds
[i
].fd
, &exceptfds
) )
331 ufds
[i
].revents
|= G_IO_PRI
;
333 if ( wxFD_ISSET(ufds
[i
].fd
, &writefds
) )
334 ufds
[i
].revents
|= G_IO_OUT
;
340 #endif // HAVE_POLL/!HAVE_POLL
342 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
347 g_mainThreadLocked
= true;
349 // we rely on the fact that glib GPollFD struct is really just pollfd but
350 // I wonder how wise is this in the long term (VZ)
351 gint res
= wxPoll( (wxPollFd
*) ufds
, nfds
, timeout
);
354 g_mainThreadLocked
= false;
361 #endif // wxUSE_THREADS
365 void wxapp_install_idle_handler()
368 wxMutexLocker
lock(gs_idleTagsMutex
);
371 // Don't install the handler if it's already installed. This test *MUST*
372 // be done when gs_idleTagsMutex is locked!
376 // GD: this assert is raised when using the thread sample (which works)
377 // so the test is probably not so easy. Can widget callbacks be
378 // triggered from child threads and, if so, for which widgets?
379 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
381 wxASSERT_MSG( wxTheApp
->m_idleTag
== 0, wxT("attempt to install idle handler twice") );
385 if (g_pendingTag
== 0)
386 g_pendingTag
= g_idle_add_full( 900, wxapp_pending_callback
, NULL
, NULL
);
388 // This routine gets called by all event handlers
389 // indicating that the idle is over. It may also
390 // get called from other thread for sending events
391 // to the main thread (and processing these in
392 // idle time). Very low priority.
393 wxTheApp
->m_idleTag
= g_idle_add_full( 1000, wxapp_idle_callback
, NULL
, NULL
);
396 //-----------------------------------------------------------------------------
397 // Access to the root window global
398 //-----------------------------------------------------------------------------
400 GtkWidget
* wxGetRootWindow()
402 if (gs_RootWindow
== NULL
)
404 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
405 gtk_widget_realize( gs_RootWindow
);
407 return gs_RootWindow
;
410 //-----------------------------------------------------------------------------
412 //-----------------------------------------------------------------------------
414 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
416 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
417 EVT_IDLE(wxAppBase::OnIdle
)
423 m_isInAssert
= false;
424 #endif // __WXDEBUG__
428 wxapp_install_idle_handler();
431 g_main_context_set_poll_func( NULL
, wxapp_poll_func
);
434 m_colorCube
= (unsigned char*) NULL
;
436 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
437 m_glVisualInfo
= (void *) NULL
;
438 m_glFBCInfo
= (void *) NULL
;
444 g_source_remove( m_idleTag
);
450 bool wxApp::OnInitGui()
452 if ( !wxAppBase::OnInitGui() )
455 GdkVisual
*visual
= gdk_visual_get_system();
457 // if this is a wxGLApp (derived from wxApp), and we've already
458 // chosen a specific visual, then derive the GdkVisual from that
459 if (m_glVisualInfo
!= NULL
)
461 // seems gtk_widget_set_default_visual no longer exists?
462 GdkVisual
* vis
= gtk_widget_get_default_visual();
464 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
465 gtk_widget_set_default_colormap( colormap
);
470 // On some machines, the default visual is just 256 colours, so
471 // we make sure we get the best. This can sometimes be wasteful.
474 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual
))
476 /* seems gtk_widget_set_default_visual no longer exists? */
477 GdkVisual
* vis
= gtk_widget_get_default_visual();
479 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
480 gtk_widget_set_default_colormap( colormap
);
485 // Nothing to do for 15, 16, 24, 32 bit displays
486 if (visual
->depth
> 8) return true;
488 // initialize color cube for 8-bit color reduction dithering
490 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
492 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
494 for (int r
= 0; r
< 32; r
++)
496 for (int g
= 0; g
< 32; g
++)
498 for (int b
= 0; b
< 32; b
++)
500 int rr
= (r
<< 3) | (r
>> 2);
501 int gg
= (g
<< 3) | (g
>> 2);
502 int bb
= (b
<< 3) | (b
>> 2);
506 GdkColor
*colors
= cmap
->colors
;
511 for (int i
= 0; i
< cmap
->size
; i
++)
513 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
514 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
515 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
516 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
519 index
= i
; max
= sum
;
525 // assume 8-bit true or static colors. this really exists
526 GdkVisual
* vis
= gdk_colormap_get_visual( cmap
);
527 index
= (r
>> (5 - vis
->red_prec
)) << vis
->red_shift
;
528 index
|= (g
>> (5 - vis
->green_prec
)) << vis
->green_shift
;
529 index
|= (b
>> (5 - vis
->blue_prec
)) << vis
->blue_shift
;
531 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
539 GdkVisual
*wxApp::GetGdkVisual()
541 GdkVisual
*visual
= NULL
;
544 visual
= gdkx_visual_get( ((XVisualInfo
*) m_glVisualInfo
)->visualid
);
546 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
553 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
558 if (!g_thread_supported())
560 #endif // wxUSE_THREADS
564 // We should have the wxUSE_WCHAR_T test on the _outside_
566 // gtk+ 2.0 supports Unicode through UTF-8 strings
567 wxConvCurrent
= &wxConvUTF8
;
568 #else // !wxUSE_WCHAR_T
570 wxConvCurrent
= (wxMBConv
*) NULL
;
571 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
573 // decide which conversion to use for the file names
575 // (1) this variable exists for the sole purpose of specifying the encoding
576 // of the filenames for GTK+ programs, so use it if it is set
577 wxString
encName(wxGetenv(_T("G_FILENAME_ENCODING")));
578 encName
= encName
.BeforeFirst(_T(','));
579 if (encName
== _T("@locale"))
585 // (2) if a non default locale is set, assume that the user wants his
586 // filenames in this locale too
587 encName
= wxLocale::GetSystemEncodingName().Upper();
588 // (3) finally use UTF-8 by default
589 if (encName
.empty() || encName
== _T("US-ASCII"))
590 encName
= _T("UTF-8");
591 wxSetEnv(_T("G_FILENAME_ENCODING"), encName
);
595 encName
= _T("UTF-8");
597 static wxConvBrokenFileNames
fileconv(encName
);
598 wxConvFileName
= &fileconv
;
601 // gtk_init() wants UTF-8, not wchar_t, so convert
603 char **argvGTK
= new char *[argc
+ 1];
604 for ( i
= 0; i
< argc
; i
++ )
606 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv
[i
]));
609 argvGTK
[argc
] = NULL
;
614 init_result
= true; // is there a _check() version of this?
615 gpe_application_init( &argcGTK
, &argvGTK
);
617 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
620 if ( argcGTK
!= argc
)
622 // we have to drop the parameters which were consumed by GTK+
623 for ( i
= 0; i
< argcGTK
; i
++ )
625 while ( strcmp(wxConvUTF8
.cWX2MB(argv
[i
]), argvGTK
[i
]) != 0 )
627 memmove(argv
+ i
, argv
+ i
+ 1, argc
- i
);
633 //else: gtk_init() didn't modify our parameters
636 for ( i
= 0; i
< argcGTK
; i
++ )
642 #else // !wxUSE_UNICODE
643 // gtk_init() shouldn't actually change argv itself (just its contents) so
644 // it's ok to pass pointer to it
645 init_result
= gtk_init_check( &argc
, &argv
);
646 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
649 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
653 // we can not enter threads before gtk_init is done
656 if ( !wxAppBase::Initialize(argc
, argv
) )
663 wxSetDetectableAutoRepeat( true );
666 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
672 void wxApp::CleanUp()
676 wxAppBase::CleanUp();
681 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
685 wxAppBase::OnAssert(file
, line
, cond
, msg
);
687 m_isInAssert
= false;
690 #endif // __WXDEBUG__
692 void wxApp::RemoveIdleTag()
695 wxMutexLocker
lock(gs_idleTagsMutex
);
699 g_source_remove( wxTheApp
->m_idleTag
);
700 wxTheApp
->m_idleTag
= 0;