]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/app.cpp
Use DESTDIR properly and fix change-install-names script. Closes #12461
[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
c259ffdf 41#include <gdk/gdkx.h>
24178e4a 42
2b850ae1
RR
43//-----------------------------------------------------------------------------
44// link GnomeVFS
45//-----------------------------------------------------------------------------
46
09a09455
PC
47#if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
48 #include "wx/link.h"
49 wxFORCE_LINK_MODULE(gnome_vfs)
2b850ae1
RR
50#endif
51
bf9e3e73
RR
52//-----------------------------------------------------------------------------
53// local functions
54//-----------------------------------------------------------------------------
55
a1abca32
PC
56// One-shot signal emission hook, to install idle handler.
57extern "C" {
14819684 58static gboolean
a1abca32 59wx_emission_hook(GSignalInvocationHint*, guint, const GValue*, gpointer data)
14819684 60{
a1abca32
PC
61 wxApp* app = wxTheApp;
62 if (app != NULL)
63 app->WakeUpIdle();
4d4ec2a5 64 bool* hook_installed = (bool*)data;
a1abca32 65 // record that hook is not installed
4d4ec2a5 66 *hook_installed = false;
14819684
PC
67 // remove hook
68 return false;
69}
a1abca32 70}
14819684 71
a1abca32
PC
72// Add signal emission hooks, to re-install idle handler when needed.
73static void wx_add_idle_hooks()
8a378a9e 74{
a1abca32 75 // "event" hook
176d9824 76 {
4d4ec2a5
PC
77 static bool hook_installed;
78 if (!hook_installed)
a1abca32 79 {
4d4ec2a5 80 static guint sig_id;
a1abca32
PC
81 if (sig_id == 0)
82 sig_id = g_signal_lookup("event", GTK_TYPE_WIDGET);
4d4ec2a5
PC
83 hook_installed = true;
84 g_signal_add_emission_hook(
85 sig_id, 0, wx_emission_hook, &hook_installed, NULL);
a1abca32
PC
86 }
87 }
88 // "size_allocate" hook
4c51a665 89 // Needed to match the behaviour of the old idle system,
a1abca32
PC
90 // but probably not necessary.
91 {
4d4ec2a5
PC
92 static bool hook_installed;
93 if (!hook_installed)
a1abca32 94 {
4d4ec2a5 95 static guint sig_id;
a1abca32
PC
96 if (sig_id == 0)
97 sig_id = g_signal_lookup("size_allocate", GTK_TYPE_WIDGET);
4d4ec2a5
PC
98 hook_installed = true;
99 g_signal_add_emission_hook(
100 sig_id, 0, wx_emission_hook, &hook_installed, NULL);
a1abca32 101 }
176d9824 102 }
8a378a9e
PC
103}
104
a1abca32
PC
105extern "C" {
106static gboolean wxapp_idle_callback(gpointer)
96997d65 107{
a1abca32
PC
108 return wxTheApp->DoIdle();
109}
110}
ec439571 111
a1abca32
PC
112bool wxApp::DoIdle()
113{
114 guint id_save;
ec439571 115 {
a1abca32
PC
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.
046c2f14 119#if wxUSE_THREADS
4d4ec2a5 120 wxMutexLocker lock(m_idleMutex);
046c2f14 121#endif
a1abca32
PC
122 id_save = m_idleSourceId;
123 m_idleSourceId = 0;
124 wx_add_idle_hooks();
657a8a35
VZ
125
126#if wxDEBUG_LEVEL
a1abca32 127 // don't generate the idle events while the assert modal dialog is shown,
4c51a665 128 // this matches the behaviour of wxMSW
a1abca32
PC
129 if (m_isInAssert)
130 return false;
131#endif
132 }
ec439571 133
a1abca32
PC
134 gdk_threads_enter();
135 bool needMore;
136 do {
26bacb82
VZ
137 ProcessPendingEvents();
138
a1abca32
PC
139 needMore = ProcessIdle();
140 } while (needMore && gtk_events_pending() == 0);
141 gdk_threads_leave();
f4322df6 142
046c2f14 143#if wxUSE_THREADS
4d4ec2a5 144 wxMutexLocker lock(m_idleMutex);
046c2f14 145#endif
a1abca32
PC
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;
ec439571 152 }
81a2edf9
PC
153
154 // Pending events can be added asynchronously,
155 // need to keep idle source if any have appeared
4d4ec2a5
PC
156 if (HasPendingEvents())
157 needMore = true;
81a2edf9 158
a1abca32
PC
159 // if more idle processing requested
160 if (needMore)
9a5c9a0c 161 {
a1abca32
PC
162 // keep this source installed
163 m_idleSourceId = id_save;
164 return true;
9a5c9a0c 165 }
a1abca32
PC
166 // add hooks and remove this source
167 wx_add_idle_hooks();
168 return false;
acfd422a 169}
8801832d 170
005f5d18
RR
171//-----------------------------------------------------------------------------
172// Access to the root window global
173//-----------------------------------------------------------------------------
174
175GtkWidget* wxGetRootWindow()
176{
dde19c21
FM
177 static GtkWidget *s_RootWindow = NULL;
178
179 if (s_RootWindow == NULL)
005f5d18 180 {
dde19c21
FM
181 s_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
182 gtk_widget_realize( s_RootWindow );
005f5d18 183 }
dde19c21 184 return s_RootWindow;
005f5d18
RR
185}
186
c801d85f
KB
187//-----------------------------------------------------------------------------
188// wxApp
189//-----------------------------------------------------------------------------
190
191IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
192
c801d85f
KB
193wxApp::wxApp()
194{
de6185e2 195 m_isInAssert = false;
a1abca32 196 m_idleSourceId = 0;
ff7b1510 197}
c801d85f 198
60acb947 199wxApp::~wxApp()
c801d85f 200{
ff7b1510 201}
c801d85f 202
c50c6fb2
VZ
203bool 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
0d2a2b60 227bool wxApp::OnInitGui()
c801d85f 228{
1e6feb95 229 if ( !wxAppBase::OnInitGui() )
de6185e2 230 return false;
1e6feb95 231
a6f5aa49
VZ
232 // if this is a wxGLApp (derived from wxApp), and we've already
233 // chosen a specific visual, then derive the GdkVisual from that
498ace9e 234 if ( GetXVisualInfo() )
005f5d18 235 {
a6f5aa49 236 GdkVisual* vis = gtk_widget_get_default_visual();
a6f5aa49
VZ
237
238 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
239 gtk_widget_set_default_colormap( colormap );
a6f5aa49 240 }
005f5d18 241 else
b134516c 242 {
c77eea28
RR
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 }
f6fcbb63 268 }
c801d85f 269
426d19f1
JS
270#if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
271 if ( !GetHildonProgram() )
e2f3bc41
VZ
272 {
273 wxLogError(_("Unable to initialize Hildon program"));
274 return false;
275 }
426d19f1 276#endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2
e2f3bc41 277
88a7a4e1 278 return true;
bbe0af5b
RR
279}
280
291b0f5b
VZ
281// use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
282bool wxApp::Initialize(int& argc_, wxChar **argv_)
c801d85f 283{
291b0f5b 284 if ( !wxAppBase::Initialize(argc_, argv_) )
d774f916 285 return false;
68567a96 286
924ef850 287#if wxUSE_THREADS
ac131bab 288 if (!g_thread_supported())
d254213e 289 {
ac131bab 290 g_thread_init(NULL);
d254213e
PC
291 gdk_threads_init();
292 }
05e2b077 293#endif // wxUSE_THREADS
2286341c 294
68567a96
MR
295 // gtk+ 2.0 supports Unicode through UTF-8 strings
296 wxConvCurrent = &wxConvUTF8;
002f4218 297
845905d5
MW
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
9a83f860
VZ
302 wxString encName(wxGetenv(wxT("G_FILENAME_ENCODING")));
303 encName = encName.BeforeFirst(wxT(','));
304 if (encName.CmpNoCase(wxT("@locale")) == 0)
d24b23b7 305 encName.clear();
845905d5 306 encName.MakeUpper();
68567a96 307#if wxUSE_INTL
845905d5
MW
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();
f32848c5
VZ
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
845905d5 329 // (3) finally use UTF-8 by default
f32848c5 330 if ( encName.empty() )
9a83f860
VZ
331 encName = wxT("UTF-8");
332 wxSetEnv(wxT("G_FILENAME_ENCODING"), encName);
845905d5
MW
333 }
334#else
335 if (encName.empty())
9a83f860 336 encName = wxT("UTF-8");
7ecb75b7
VZ
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
845905d5
MW
343#endif // wxUSE_INTL
344 static wxConvBrokenFileNames fileconv(encName);
345 wxConvFileName = &fileconv;
66bf0099 346
d774f916
VZ
347
348 bool init_result;
4b4e81ee 349 int i;
d774f916 350
05e2b077
VZ
351#if wxUSE_UNICODE
352 // gtk_init() wants UTF-8, not wchar_t, so convert
291b0f5b
VZ
353 char **argvGTK = new char *[argc_ + 1];
354 for ( i = 0; i < argc_; i++ )
924ef850 355 {
291b0f5b 356 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv_[i]));
924ef850 357 }
0cf2cb36 358
291b0f5b 359 argvGTK[argc_] = NULL;
954de0f1 360
291b0f5b 361 int argcGTK = argc_;
68567a96 362
62be94e1 363#ifdef __WXGPE__
c156411a 364 init_result = true; // is there a _check() version of this?
62be94e1
RR
365 gpe_application_init( &argcGTK, &argvGTK );
366#else
c156411a 367 init_result = gtk_init_check( &argcGTK, &argvGTK );
62be94e1 368#endif
cb352236 369 wxUpdateLocaleIsUtf8();
954de0f1 370
291b0f5b 371 if ( argcGTK != argc_ )
924ef850 372 {
05e2b077
VZ
373 // we have to drop the parameters which were consumed by GTK+
374 for ( i = 0; i < argcGTK; i++ )
375 {
291b0f5b 376 while ( strcmp(wxConvUTF8.cWX2MB(argv_[i]), argvGTK[i]) != 0 )
05e2b077 377 {
291b0f5b 378 memmove(argv_ + i, argv_ + i + 1, (argc_ - i)*sizeof(*argv_));
05e2b077
VZ
379 }
380 }
0cf2cb36 381
291b0f5b 382 argc_ = argcGTK;
4f6b94a3 383 argv_[argc_] = NULL;
2b5f62a0 384 }
05e2b077 385 //else: gtk_init() didn't modify our parameters
e0253070 386
05e2b077
VZ
387 // free our copy
388 for ( i = 0; i < argcGTK; i++ )
0151c3eb 389 {
05e2b077 390 free(argvGTK[i]);
0151c3eb 391 }
0cf2cb36 392
05e2b077
VZ
393 delete [] argvGTK;
394#else // !wxUSE_UNICODE
291b0f5b 395 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
05e2b077 396 // it's ok to pass pointer to it
291b0f5b 397 init_result = gtk_init_check( &argc_, &argv_ );
05e2b077 398#endif // wxUSE_UNICODE/!wxUSE_UNICODE
0cf2cb36 399
f7a3c9be 400 // update internal arg[cv] as GTK+ may have removed processed options:
291b0f5b
VZ
401 this->argc = argc_;
402 this->argv = argv_;
f7a3c9be 403
b045eff2
VZ
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
4b4e81ee 413 for ( i = 0; i < argc_; i++ )
b045eff2
VZ
414 {
415 // leave just the names of the options with values
291b0f5b 416 const wxString str = wxString(argv_[i]).BeforeFirst('=');
b045eff2
VZ
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\""),
291b0f5b 429 argv_[0]);
b045eff2
VZ
430 return false;
431 }
432 }
433 }
434 }
435
f7a3c9be
VZ
436 if ( !init_result )
437 {
438 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
c156411a
RD
439 return false;
440 }
68567a96 441
4c51a665 442 // we cannot enter threads before gtk_init is done
05e2b077 443 gdk_threads_enter();
0cf2cb36 444
05e2b077
VZ
445#if wxUSE_INTL
446 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
447#endif
be88a6ad 448
a1abca32
PC
449 // make sure GtkWidget type is loaded, idle hooks need it
450 g_type_class_ref(GTK_TYPE_WIDGET);
451 WakeUpIdle();
452
05e2b077
VZ
453 return true;
454}
0cf2cb36 455
05e2b077
VZ
456void wxApp::CleanUp()
457{
a1abca32
PC
458 if (m_idleSourceId != 0)
459 g_source_remove(m_idleSourceId);
c114eb7a 460
a1abca32
PC
461 // release reference acquired by Initialize()
462 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET));
463
05e2b077 464 gdk_threads_leave();
abca8ebf
VZ
465
466 wxAppBase::CleanUp();
ff7b1510 467}
1a56f55c 468
a1abca32
PC
469void wxApp::WakeUpIdle()
470{
471#if wxUSE_THREADS
4d4ec2a5 472 wxMutexLocker lock(m_idleMutex);
a1abca32
PC
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.
480bool wxApp::EventsPending()
481{
482#if wxUSE_THREADS
4d4ec2a5 483 wxMutexLocker lock(m_idleMutex);
a1abca32
PC
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
2d97237d
VZ
494void wxApp::OnAssertFailure(const wxChar *file,
495 int line,
496 const wxChar* func,
497 const wxChar* cond,
498 const wxChar *msg)
a5f1fd3e 499{
657a8a35
VZ
500 // there is no need to do anything if asserts are disabled in this build
501 // anyhow
502#if wxDEBUG_LEVEL
ec439571
PC
503 // block wx idle events while assert dialog is showing
504 m_isInAssert = true;
a5f1fd3e 505
2d97237d 506 wxAppBase::OnAssertFailure(file, line, func, cond, msg);
a5f1fd3e 507
ec439571 508 m_isInAssert = false;
657a8a35
VZ
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
a5f1fd3e
VZ
516}
517
d254213e
PC
518#if wxUSE_THREADS
519void wxGUIAppTraits::MutexGuiEnter()
520{
521 gdk_threads_enter();
522}
523
524void wxGUIAppTraits::MutexGuiLeave()
525{
526 gdk_threads_leave();
527}
528#endif // wxUSE_THREADS
426d19f1
JS
529
530#if wxUSE_LIBHILDON || wxUSE_LIBHILDON2
531// Maemo-specific method: get the main program object
532HildonProgram *wxApp::GetHildonProgram()
ce00f59b
VZ
533{
534 return hildon_program_get_instance();
426d19f1 535}
ce00f59b 536
426d19f1 537#endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2