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