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