]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/app.cpp
Allow entering minus sign in wxMSW wxSpinCtrl if needed.
[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();
657a8a35
VZ
118
119#if wxDEBUG_LEVEL
a1abca32
PC
120 // don't generate the idle events while the assert modal dialog is shown,
121 // this matches the behavior of wxMSW
122 if (m_isInAssert)
123 return false;
124#endif
125 }
ec439571 126
a1abca32
PC
127 gdk_threads_enter();
128 bool needMore;
129 do {
26bacb82
VZ
130 ProcessPendingEvents();
131
a1abca32
PC
132 needMore = ProcessIdle();
133 } while (needMore && gtk_events_pending() == 0);
134 gdk_threads_leave();
f4322df6 135
046c2f14 136#if wxUSE_THREADS
a1abca32 137 wxMutexLocker lock(*m_idleMutex);
046c2f14 138#endif
a1abca32
PC
139 // if a new idle source was added during ProcessIdle
140 if (m_idleSourceId != 0)
141 {
142 // remove it
143 g_source_remove(m_idleSourceId);
144 m_idleSourceId = 0;
ec439571 145 }
81a2edf9
PC
146
147 // Pending events can be added asynchronously,
148 // need to keep idle source if any have appeared
149 needMore = needMore || HasPendingEvents();
150
a1abca32
PC
151 // if more idle processing requested
152 if (needMore)
9a5c9a0c 153 {
a1abca32
PC
154 // keep this source installed
155 m_idleSourceId = id_save;
156 return true;
9a5c9a0c 157 }
a1abca32
PC
158 // add hooks and remove this source
159 wx_add_idle_hooks();
160 return false;
acfd422a 161}
8801832d 162
005f5d18
RR
163//-----------------------------------------------------------------------------
164// Access to the root window global
165//-----------------------------------------------------------------------------
166
167GtkWidget* wxGetRootWindow()
168{
dde19c21
FM
169 static GtkWidget *s_RootWindow = NULL;
170
171 if (s_RootWindow == NULL)
005f5d18 172 {
dde19c21
FM
173 s_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
174 gtk_widget_realize( s_RootWindow );
005f5d18 175 }
dde19c21 176 return s_RootWindow;
005f5d18
RR
177}
178
c801d85f
KB
179//-----------------------------------------------------------------------------
180// wxApp
181//-----------------------------------------------------------------------------
182
183IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
184
c801d85f
KB
185wxApp::wxApp()
186{
de6185e2 187 m_isInAssert = false;
657a8a35 188
a1abca32
PC
189#if wxUSE_THREADS
190 m_idleMutex = NULL;
191#endif
192 m_idleSourceId = 0;
ff7b1510 193}
c801d85f 194
60acb947 195wxApp::~wxApp()
c801d85f 196{
ff7b1510 197}
c801d85f 198
c50c6fb2
VZ
199bool wxApp::SetNativeTheme(const wxString& theme)
200{
201 wxString path;
202 path = gtk_rc_get_theme_dir();
203 path += "/";
204 path += theme.utf8_str();
205 path += "/gtk-2.0/gtkrc";
206
207 if ( wxFileExists(path.utf8_str()) )
208 gtk_rc_add_default_file(path.utf8_str());
209 else if ( wxFileExists(theme.utf8_str()) )
210 gtk_rc_add_default_file(theme.utf8_str());
211 else
212 {
213 wxLogWarning("Theme \"%s\" not available.", theme);
214
215 return false;
216 }
217
218 gtk_rc_reparse_all_for_settings(gtk_settings_get_default(), TRUE);
219
220 return true;
221}
222
0d2a2b60 223bool wxApp::OnInitGui()
c801d85f 224{
1e6feb95 225 if ( !wxAppBase::OnInitGui() )
de6185e2 226 return false;
1e6feb95 227
a6f5aa49
VZ
228 // if this is a wxGLApp (derived from wxApp), and we've already
229 // chosen a specific visual, then derive the GdkVisual from that
498ace9e 230 if ( GetXVisualInfo() )
005f5d18 231 {
a6f5aa49 232 GdkVisual* vis = gtk_widget_get_default_visual();
a6f5aa49
VZ
233
234 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
235 gtk_widget_set_default_colormap( colormap );
a6f5aa49 236 }
005f5d18 237 else
b134516c 238 {
c77eea28
RR
239 // On some machines, the default visual is just 256 colours, so
240 // we make sure we get the best. This can sometimes be wasteful.
241 if (m_useBestVisual)
242 {
243 if (m_forceTrueColour)
244 {
245 GdkVisual* visual = gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR );
246 if (!visual)
247 {
248 wxLogError(wxT("Unable to initialize TrueColor visual."));
249 return false;
250 }
251 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
252 gtk_widget_set_default_colormap( colormap );
253 }
254 else
255 {
256 if (gdk_visual_get_best() != gdk_visual_get_system())
257 {
258 GdkVisual* visual = gdk_visual_get_best();
259 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
260 gtk_widget_set_default_colormap( colormap );
261 }
262 }
263 }
f6fcbb63 264 }
c801d85f 265
e2f3bc41
VZ
266#if wxUSE_LIBHILDON
267 m_hildonProgram = hildon_program_get_instance();
268 if ( !m_hildonProgram )
269 {
270 wxLogError(_("Unable to initialize Hildon program"));
271 return false;
272 }
273#endif // wxUSE_LIBHILDON
274
88a7a4e1 275 return true;
bbe0af5b
RR
276}
277
005f5d18
RR
278GdkVisual *wxApp::GetGdkVisual()
279{
280 GdkVisual *visual = NULL;
be88a6ad 281
498ace9e
VZ
282 XVisualInfo *xvi = (XVisualInfo *)GetXVisualInfo();
283 if ( xvi )
284 visual = gdkx_visual_get( xvi->visualid );
005f5d18 285 else
22a3bce4 286 visual = gdk_drawable_get_visual( wxGetRootWindow()->window );
be88a6ad 287
005f5d18 288 wxASSERT( visual );
be88a6ad 289
005f5d18
RR
290 return visual;
291}
292
291b0f5b
VZ
293// use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
294bool wxApp::Initialize(int& argc_, wxChar **argv_)
c801d85f 295{
291b0f5b 296 if ( !wxAppBase::Initialize(argc_, argv_) )
d774f916 297 return false;
68567a96 298
924ef850 299#if wxUSE_THREADS
ac131bab 300 if (!g_thread_supported())
d254213e 301 {
ac131bab 302 g_thread_init(NULL);
d254213e
PC
303 gdk_threads_init();
304 }
05e2b077 305#endif // wxUSE_THREADS
2286341c 306
68567a96
MR
307 // gtk+ 2.0 supports Unicode through UTF-8 strings
308 wxConvCurrent = &wxConvUTF8;
002f4218 309
845905d5
MW
310 // decide which conversion to use for the file names
311
312 // (1) this variable exists for the sole purpose of specifying the encoding
313 // of the filenames for GTK+ programs, so use it if it is set
9a83f860
VZ
314 wxString encName(wxGetenv(wxT("G_FILENAME_ENCODING")));
315 encName = encName.BeforeFirst(wxT(','));
316 if (encName.CmpNoCase(wxT("@locale")) == 0)
d24b23b7 317 encName.clear();
845905d5 318 encName.MakeUpper();
68567a96 319#if wxUSE_INTL
845905d5
MW
320 if (encName.empty())
321 {
322 // (2) if a non default locale is set, assume that the user wants his
323 // filenames in this locale too
324 encName = wxLocale::GetSystemEncodingName().Upper();
325 // (3) finally use UTF-8 by default
9a83f860
VZ
326 if (encName.empty() || encName == wxT("US-ASCII"))
327 encName = wxT("UTF-8");
328 wxSetEnv(wxT("G_FILENAME_ENCODING"), encName);
845905d5
MW
329 }
330#else
331 if (encName.empty())
9a83f860 332 encName = wxT("UTF-8");
7ecb75b7
VZ
333
334 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
335 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
336 // from gtk_init_check() as it does by default
337 gtk_disable_setlocale();
338
845905d5
MW
339#endif // wxUSE_INTL
340 static wxConvBrokenFileNames fileconv(encName);
341 wxConvFileName = &fileconv;
66bf0099 342
d774f916
VZ
343
344 bool init_result;
4b4e81ee 345 int i;
d774f916 346
05e2b077
VZ
347#if wxUSE_UNICODE
348 // gtk_init() wants UTF-8, not wchar_t, so convert
291b0f5b
VZ
349 char **argvGTK = new char *[argc_ + 1];
350 for ( i = 0; i < argc_; i++ )
924ef850 351 {
291b0f5b 352 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv_[i]));
924ef850 353 }
0cf2cb36 354
291b0f5b 355 argvGTK[argc_] = NULL;
954de0f1 356
291b0f5b 357 int argcGTK = argc_;
68567a96 358
62be94e1 359#ifdef __WXGPE__
c156411a 360 init_result = true; // is there a _check() version of this?
62be94e1
RR
361 gpe_application_init( &argcGTK, &argvGTK );
362#else
c156411a 363 init_result = gtk_init_check( &argcGTK, &argvGTK );
62be94e1 364#endif
cb352236 365 wxUpdateLocaleIsUtf8();
954de0f1 366
291b0f5b 367 if ( argcGTK != argc_ )
924ef850 368 {
05e2b077
VZ
369 // we have to drop the parameters which were consumed by GTK+
370 for ( i = 0; i < argcGTK; i++ )
371 {
291b0f5b 372 while ( strcmp(wxConvUTF8.cWX2MB(argv_[i]), argvGTK[i]) != 0 )
05e2b077 373 {
291b0f5b 374 memmove(argv_ + i, argv_ + i + 1, (argc_ - i)*sizeof(*argv_));
05e2b077
VZ
375 }
376 }
0cf2cb36 377
291b0f5b 378 argc_ = argcGTK;
4f6b94a3 379 argv_[argc_] = NULL;
2b5f62a0 380 }
05e2b077 381 //else: gtk_init() didn't modify our parameters
e0253070 382
05e2b077
VZ
383 // free our copy
384 for ( i = 0; i < argcGTK; i++ )
0151c3eb 385 {
05e2b077 386 free(argvGTK[i]);
0151c3eb 387 }
0cf2cb36 388
05e2b077
VZ
389 delete [] argvGTK;
390#else // !wxUSE_UNICODE
291b0f5b 391 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
05e2b077 392 // it's ok to pass pointer to it
291b0f5b 393 init_result = gtk_init_check( &argc_, &argv_ );
05e2b077 394#endif // wxUSE_UNICODE/!wxUSE_UNICODE
0cf2cb36 395
f7a3c9be 396 // update internal arg[cv] as GTK+ may have removed processed options:
291b0f5b
VZ
397 this->argc = argc_;
398 this->argv = argv_;
f7a3c9be 399
b045eff2
VZ
400 if ( m_traits )
401 {
402 // if there are still GTK+ standard options unparsed in the command
403 // line, it means that they were not syntactically correct and GTK+
404 // already printed a warning on the command line and we should now
405 // exit:
406 wxArrayString opt, desc;
407 m_traits->GetStandardCmdLineOptions(opt, desc);
408
4b4e81ee 409 for ( i = 0; i < argc_; i++ )
b045eff2
VZ
410 {
411 // leave just the names of the options with values
291b0f5b 412 const wxString str = wxString(argv_[i]).BeforeFirst('=');
b045eff2
VZ
413
414 for ( size_t j = 0; j < opt.size(); j++ )
415 {
416 // remove the leading spaces from the option string as it does
417 // have them
418 if ( opt[j].Trim(false).BeforeFirst('=') == str )
419 {
420 // a GTK+ option can be left on the command line only if
421 // there was an error in (or before, in another standard
422 // options) it, so abort, just as we do if incorrect
423 // program option is given
424 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
291b0f5b 425 argv_[0]);
b045eff2
VZ
426 return false;
427 }
428 }
429 }
430 }
431
f7a3c9be
VZ
432 if ( !init_result )
433 {
434 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
c156411a
RD
435 return false;
436 }
68567a96 437
05e2b077
VZ
438 // we can not enter threads before gtk_init is done
439 gdk_threads_enter();
0cf2cb36 440
e4db172a 441 wxSetDetectableAutoRepeat( true );
be88a6ad 442
05e2b077
VZ
443#if wxUSE_INTL
444 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
445#endif
be88a6ad 446
a1abca32
PC
447#if wxUSE_THREADS
448 m_idleMutex = new wxMutex;
449#endif
450 // make sure GtkWidget type is loaded, idle hooks need it
451 g_type_class_ref(GTK_TYPE_WIDGET);
452 WakeUpIdle();
453
05e2b077
VZ
454 return true;
455}
0cf2cb36 456
05e2b077
VZ
457void wxApp::CleanUp()
458{
a1abca32
PC
459 if (m_idleSourceId != 0)
460 g_source_remove(m_idleSourceId);
c114eb7a 461
a1abca32
PC
462 // release reference acquired by Initialize()
463 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET));
464
05e2b077 465 gdk_threads_leave();
abca8ebf
VZ
466
467 wxAppBase::CleanUp();
c114eb7a
VZ
468
469 // delete this mutex as late as possible as it's used from WakeUpIdle(), in
470 // particular do it after calling the base class CleanUp() which can result
471 // in it being called
472#if wxUSE_THREADS
473 delete m_idleMutex;
474 m_idleMutex = NULL;
475#endif
ff7b1510 476}
1a56f55c 477
a1abca32
PC
478void wxApp::WakeUpIdle()
479{
480#if wxUSE_THREADS
481 wxMutexLocker lock(*m_idleMutex);
482#endif
483 if (m_idleSourceId == 0)
484 m_idleSourceId = g_idle_add_full(G_PRIORITY_LOW, wxapp_idle_callback, NULL, NULL);
485}
486
487// Checking for pending events requires first removing our idle source,
488// otherwise it will cause the check to always return true.
489bool wxApp::EventsPending()
490{
491#if wxUSE_THREADS
492 wxMutexLocker lock(*m_idleMutex);
493#endif
494 if (m_idleSourceId != 0)
495 {
496 g_source_remove(m_idleSourceId);
497 m_idleSourceId = 0;
498 wx_add_idle_hooks();
499 }
500 return gtk_events_pending() != 0;
501}
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{
657a8a35
VZ
509 // there is no need to do anything if asserts are disabled in this build
510 // anyhow
511#if wxDEBUG_LEVEL
ec439571
PC
512 // block wx idle events while assert dialog is showing
513 m_isInAssert = true;
a5f1fd3e 514
2d97237d 515 wxAppBase::OnAssertFailure(file, line, func, cond, msg);
a5f1fd3e 516
ec439571 517 m_isInAssert = false;
657a8a35
VZ
518#else // !wxDEBUG_LEVEL
519 wxUnusedVar(file);
520 wxUnusedVar(line);
521 wxUnusedVar(func);
522 wxUnusedVar(cond);
523 wxUnusedVar(msg);
524#endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
a5f1fd3e
VZ
525}
526
d254213e
PC
527#if wxUSE_THREADS
528void wxGUIAppTraits::MutexGuiEnter()
529{
530 gdk_threads_enter();
531}
532
533void wxGUIAppTraits::MutexGuiLeave()
534{
535 gdk_threads_leave();
536}
537#endif // wxUSE_THREADS