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