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