]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/app.cpp
added conversion to and from std::string and std::wstring (if wxUSE_STD_STRING)
[wxWidgets.git] / src / gtk / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/app.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __VMS
11 // vms_jackets.h should for proper working be included before anything else
12 # include <vms_jackets.h>
13 #undef ConnectionNumber
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #include "wx/app.h"
20
21 #ifndef WX_PRECOMP
22 #include "wx/intl.h"
23 #include "wx/log.h"
24 #include "wx/utils.h"
25 #include "wx/memory.h"
26 #include "wx/font.h"
27 #endif
28
29 #include "wx/thread.h"
30
31 #ifdef __WXGPE__
32 #include <gpe/init.h>
33 #endif
34
35 #include "wx/gtk/win_gtk.h"
36 #include "wx/gtk/private.h"
37
38 #include <gdk/gdkx.h>
39
40 //-----------------------------------------------------------------------------
41 // link GnomeVFS
42 //-----------------------------------------------------------------------------
43
44 #if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
45 #include "wx/link.h"
46 wxFORCE_LINK_MODULE(gnome_vfs)
47 #endif
48
49 //-----------------------------------------------------------------------------
50 // global data
51 //-----------------------------------------------------------------------------
52
53 bool g_mainThreadLocked = false;
54
55 static GtkWidget *gs_RootWindow = (GtkWidget*) NULL;
56
57 //-----------------------------------------------------------------------------
58 // idle system
59 //-----------------------------------------------------------------------------
60
61 void wxapp_install_idle_handler();
62
63 #if wxUSE_THREADS
64 static wxMutex gs_idleTagsMutex;
65 #endif
66
67 //-----------------------------------------------------------------------------
68 // wxYield
69 //-----------------------------------------------------------------------------
70
71 // not static because used by textctrl.cpp
72 //
73 // MT-FIXME
74 bool wxIsInsideYield = false;
75
76 bool wxApp::Yield(bool onlyIfNeeded)
77 {
78 if ( wxIsInsideYield )
79 {
80 if ( !onlyIfNeeded )
81 {
82 wxFAIL_MSG( wxT("wxYield called recursively" ) );
83 }
84
85 return false;
86 }
87
88 #if wxUSE_THREADS
89 if ( !wxThread::IsMain() )
90 {
91 // can't call gtk_main_iteration() from other threads like this
92 return true;
93 }
94 #endif // wxUSE_THREADS
95
96 wxIsInsideYield = true;
97
98 // We need to remove idle callbacks or the loop will
99 // never finish.
100 SuspendIdleCallback();
101
102 #if wxUSE_LOG
103 // disable log flushing from here because a call to wxYield() shouldn't
104 // normally result in message boxes popping up &c
105 wxLog::Suspend();
106 #endif
107
108 while (gtk_events_pending())
109 gtk_main_iteration();
110
111 // It's necessary to call ProcessIdle() to update the frames sizes which
112 // might have been changed (it also will update other things set from
113 // OnUpdateUI() which is a nice (and desired) side effect). But we
114 // call ProcessIdle() only once since this is not meant for longish
115 // background jobs (controlled by wxIdleEvent::RequestMore() and the
116 // return value of Processidle().
117 ProcessIdle();
118
119 #if wxUSE_LOG
120 // let the logs be flashed again
121 wxLog::Resume();
122 #endif
123
124 wxIsInsideYield = false;
125
126 return true;
127 }
128
129 //-----------------------------------------------------------------------------
130 // wxWakeUpIdle
131 //-----------------------------------------------------------------------------
132
133 // RR/KH: No wxMutexGui calls are needed here according to the GTK faq,
134 // http://www.gtk.org/faq/#AEN500 - this caused problems for wxPostEvent.
135
136 void wxApp::WakeUpIdle()
137 {
138 wxapp_install_idle_handler();
139 }
140
141 //-----------------------------------------------------------------------------
142 // local functions
143 //-----------------------------------------------------------------------------
144
145 // the callback functions must be extern "C" to comply with GTK+ declarations
146 extern "C"
147 {
148
149 // One-shot emission hook for "event" signal, to install idle handler.
150 // This will be called when the "event" signal is issued on any GtkWidget object.
151 static gboolean
152 event_emission_hook(GSignalInvocationHint*, guint, const GValue*, gpointer)
153 {
154 wxapp_install_idle_handler();
155 // remove hook
156 return false;
157 }
158
159 // add emission hook for "event" signal, to re-install idle handler when needed
160 static inline void wxAddEmissionHook()
161 {
162 GType widgetType = GTK_TYPE_WIDGET;
163 // if GtkWidget type is loaded
164 if (g_type_class_peek(widgetType) != NULL)
165 {
166 guint sig_id = g_signal_lookup("event", widgetType);
167 g_signal_add_emission_hook(sig_id, 0, event_emission_hook, NULL, NULL);
168 }
169 }
170
171 static gint wxapp_idle_callback( gpointer WXUNUSED(data) )
172 {
173 // this does not look possible, but just in case...
174 if (!wxTheApp)
175 return false;
176
177 bool moreIdles = false;
178
179 #ifdef __WXDEBUG__
180 // don't generate the idle events while the assert modal dialog is shown,
181 // this matches the behavior of wxMSW
182 if (!wxTheApp->IsInAssert())
183 #endif // __WXDEBUG__
184 {
185 guint idleID_save;
186 {
187 // Allow another idle source to be added while this one is busy.
188 // Needed if an idle event handler runs a new event loop,
189 // for example by showing a dialog.
190 #if wxUSE_THREADS
191 wxMutexLocker lock(gs_idleTagsMutex);
192 #endif
193 idleID_save = wxTheApp->m_idleTag;
194 wxTheApp->m_idleTag = 0;
195 g_isIdle = true;
196 wxAddEmissionHook();
197 }
198
199 // When getting called from GDK's time-out handler
200 // we are no longer within GDK's grab on the GUI
201 // thread so we must lock it here ourselves.
202 gdk_threads_enter();
203
204 // Send idle event to all who request them as long as
205 // no events have popped up in the event queue.
206 do {
207 moreIdles = wxTheApp->ProcessIdle();
208 } while (moreIdles && gtk_events_pending() == 0);
209
210 // Release lock again
211 gdk_threads_leave();
212
213 {
214 // If another idle source was added, remove it
215 #if wxUSE_THREADS
216 wxMutexLocker lock(gs_idleTagsMutex);
217 #endif
218 if (wxTheApp->m_idleTag != 0)
219 g_source_remove(wxTheApp->m_idleTag);
220 wxTheApp->m_idleTag = idleID_save;
221 g_isIdle = false;
222 }
223 }
224
225 if (!moreIdles)
226 {
227 #if wxUSE_THREADS
228 wxMutexLocker lock(gs_idleTagsMutex);
229 #endif
230 // Indicate that we are now in idle mode and event handlers
231 // will have to reinstall the idle handler again.
232 g_isIdle = true;
233 wxTheApp->m_idleTag = 0;
234
235 wxAddEmissionHook();
236 }
237
238 // Return FALSE if no more idle events are to be sent
239 return moreIdles;
240 }
241 } // extern "C"
242
243 #if wxUSE_THREADS
244
245 static GPollFunc wxgs_poll_func;
246
247 extern "C" {
248 static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout )
249 {
250 gdk_threads_enter();
251
252 wxMutexGuiLeave();
253 g_mainThreadLocked = true;
254
255 gint res = (*wxgs_poll_func)(ufds, nfds, timeout);
256
257 wxMutexGuiEnter();
258 g_mainThreadLocked = false;
259
260 gdk_threads_leave();
261
262 return res;
263 }
264 }
265
266 #endif // wxUSE_THREADS
267
268 void wxapp_install_idle_handler()
269 {
270 if (wxTheApp == NULL)
271 return;
272
273 #if wxUSE_THREADS
274 wxMutexLocker lock(gs_idleTagsMutex);
275 #endif
276
277 // Don't install the handler if it's already installed. This test *MUST*
278 // be done when gs_idleTagsMutex is locked!
279 if (!g_isIdle)
280 return;
281
282 // GD: this assert is raised when using the thread sample (which works)
283 // so the test is probably not so easy. Can widget callbacks be
284 // triggered from child threads and, if so, for which widgets?
285 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
286
287 wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") );
288
289 g_isIdle = false;
290
291 // This routine gets called by all event handlers
292 // indicating that the idle is over. It may also
293 // get called from other thread for sending events
294 // to the main thread (and processing these in
295 // idle time). Very low priority.
296 wxTheApp->m_idleTag = g_idle_add_full(G_PRIORITY_LOW, wxapp_idle_callback, NULL, NULL);
297 }
298
299 //-----------------------------------------------------------------------------
300 // Access to the root window global
301 //-----------------------------------------------------------------------------
302
303 GtkWidget* wxGetRootWindow()
304 {
305 if (gs_RootWindow == NULL)
306 {
307 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
308 gtk_widget_realize( gs_RootWindow );
309 }
310 return gs_RootWindow;
311 }
312
313 //-----------------------------------------------------------------------------
314 // wxApp
315 //-----------------------------------------------------------------------------
316
317 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
318
319 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
320 EVT_IDLE(wxAppBase::OnIdle)
321 END_EVENT_TABLE()
322
323 wxApp::wxApp()
324 {
325 #ifdef __WXDEBUG__
326 m_isInAssert = false;
327 #endif // __WXDEBUG__
328
329 m_idleTag = 0;
330 g_isIdle = true;
331 wxapp_install_idle_handler();
332 }
333
334 wxApp::~wxApp()
335 {
336 if (m_idleTag)
337 g_source_remove( m_idleTag );
338 }
339
340 bool wxApp::OnInitGui()
341 {
342 if ( !wxAppBase::OnInitGui() )
343 return false;
344
345 // if this is a wxGLApp (derived from wxApp), and we've already
346 // chosen a specific visual, then derive the GdkVisual from that
347 if ( GetXVisualInfo() )
348 {
349 GdkVisual* vis = gtk_widget_get_default_visual();
350
351 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
352 gtk_widget_set_default_colormap( colormap );
353 }
354 else
355 {
356 // On some machines, the default visual is just 256 colours, so
357 // we make sure we get the best. This can sometimes be wasteful.
358 if (m_useBestVisual)
359 {
360 if (m_forceTrueColour)
361 {
362 GdkVisual* visual = gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR );
363 if (!visual)
364 {
365 wxLogError(wxT("Unable to initialize TrueColor visual."));
366 return false;
367 }
368 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
369 gtk_widget_set_default_colormap( colormap );
370 }
371 else
372 {
373 if (gdk_visual_get_best() != gdk_visual_get_system())
374 {
375 GdkVisual* visual = gdk_visual_get_best();
376 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
377 gtk_widget_set_default_colormap( colormap );
378 }
379 }
380 }
381 }
382
383 return true;
384 }
385
386 GdkVisual *wxApp::GetGdkVisual()
387 {
388 GdkVisual *visual = NULL;
389
390 XVisualInfo *xvi = (XVisualInfo *)GetXVisualInfo();
391 if ( xvi )
392 visual = gdkx_visual_get( xvi->visualid );
393 else
394 visual = gdk_drawable_get_visual( wxGetRootWindow()->window );
395
396 wxASSERT( visual );
397
398 return visual;
399 }
400
401 bool wxApp::Initialize(int& argc, wxChar **argv)
402 {
403 if ( !wxAppBase::Initialize(argc, argv) )
404 return false;
405
406 #if wxUSE_THREADS
407 if (!g_thread_supported())
408 g_thread_init(NULL);
409
410 wxgs_poll_func = g_main_context_get_poll_func(NULL);
411 g_main_context_set_poll_func(NULL, wxapp_poll_func);
412 #endif // wxUSE_THREADS
413
414 // We should have the wxUSE_WCHAR_T test on the _outside_
415 #if wxUSE_WCHAR_T
416 // gtk+ 2.0 supports Unicode through UTF-8 strings
417 wxConvCurrent = &wxConvUTF8;
418 #else // !wxUSE_WCHAR_T
419 if (!wxOKlibc())
420 wxConvCurrent = (wxMBConv*) NULL;
421 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
422
423 // decide which conversion to use for the file names
424
425 // (1) this variable exists for the sole purpose of specifying the encoding
426 // of the filenames for GTK+ programs, so use it if it is set
427 wxString encName(wxGetenv(_T("G_FILENAME_ENCODING")));
428 encName = encName.BeforeFirst(_T(','));
429 if (encName.CmpNoCase(_T("@locale")) == 0)
430 encName.clear();
431 encName.MakeUpper();
432 #if wxUSE_INTL
433 if (encName.empty())
434 {
435 // (2) if a non default locale is set, assume that the user wants his
436 // filenames in this locale too
437 encName = wxLocale::GetSystemEncodingName().Upper();
438 // (3) finally use UTF-8 by default
439 if (encName.empty() || encName == _T("US-ASCII"))
440 encName = _T("UTF-8");
441 wxSetEnv(_T("G_FILENAME_ENCODING"), encName);
442 }
443 #else
444 if (encName.empty())
445 encName = _T("UTF-8");
446
447 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
448 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
449 // from gtk_init_check() as it does by default
450 gtk_disable_setlocale();
451
452 #endif // wxUSE_INTL
453 static wxConvBrokenFileNames fileconv(encName);
454 wxConvFileName = &fileconv;
455
456
457 bool init_result;
458
459 #if wxUSE_UNICODE
460 // gtk_init() wants UTF-8, not wchar_t, so convert
461 int i;
462 char **argvGTK = new char *[argc + 1];
463 for ( i = 0; i < argc; i++ )
464 {
465 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv[i]));
466 }
467
468 argvGTK[argc] = NULL;
469
470 int argcGTK = argc;
471
472 #ifdef __WXGPE__
473 init_result = true; // is there a _check() version of this?
474 gpe_application_init( &argcGTK, &argvGTK );
475 #else
476 init_result = gtk_init_check( &argcGTK, &argvGTK );
477 #endif
478
479 if ( argcGTK != argc )
480 {
481 // we have to drop the parameters which were consumed by GTK+
482 for ( i = 0; i < argcGTK; i++ )
483 {
484 while ( strcmp(wxConvUTF8.cWX2MB(argv[i]), argvGTK[i]) != 0 )
485 {
486 memmove(argv + i, argv + i + 1, (argc - i)*sizeof(*argv));
487 }
488 }
489
490 argc = argcGTK;
491 }
492 //else: gtk_init() didn't modify our parameters
493
494 // free our copy
495 for ( i = 0; i < argcGTK; i++ )
496 {
497 free(argvGTK[i]);
498 }
499
500 delete [] argvGTK;
501 #else // !wxUSE_UNICODE
502 // gtk_init() shouldn't actually change argv itself (just its contents) so
503 // it's ok to pass pointer to it
504 init_result = gtk_init_check( &argc, &argv );
505 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
506
507 if (!init_result) {
508 wxLogError(wxT("Unable to initialize gtk, is DISPLAY set properly?"));
509 return false;
510 }
511
512 // we can not enter threads before gtk_init is done
513 gdk_threads_enter();
514
515 wxSetDetectableAutoRepeat( true );
516
517 #if wxUSE_INTL
518 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
519 #endif
520
521 return true;
522 }
523
524 void wxApp::CleanUp()
525 {
526 gdk_threads_leave();
527
528 wxAppBase::CleanUp();
529 }
530
531 #ifdef __WXDEBUG__
532
533 void wxApp::OnAssertFailure(const wxChar *file,
534 int line,
535 const wxChar* func,
536 const wxChar* cond,
537 const wxChar *msg)
538 {
539
540 // block wx idle events while assert dialog is showing
541 m_isInAssert = true;
542
543 wxAppBase::OnAssertFailure(file, line, func, cond, msg);
544
545 m_isInAssert = false;
546 }
547
548 #endif // __WXDEBUG__
549
550 void wxApp::SuspendIdleCallback()
551 {
552 #if wxUSE_THREADS
553 wxMutexLocker lock(gs_idleTagsMutex);
554 #endif
555 if (m_idleTag != 0)
556 {
557 g_source_remove(m_idleTag);
558 m_idleTag = 0;
559 g_isIdle = true;
560 wxAddEmissionHook();
561 }
562 }