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