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