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