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