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