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