]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/app.cpp
restore checks for SM_SWAPBUTTON, it is not defined in WinCE SDK
[wxWidgets.git] / src / gtk / app.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/app.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling, Julian Smart
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#include "wx/app.h"
14
15#ifndef WX_PRECOMP
16 #include "wx/intl.h"
17 #include "wx/log.h"
18 #include "wx/utils.h"
19 #include "wx/memory.h"
20 #include "wx/font.h"
21#endif
22
23#include "wx/thread.h"
24
25#ifdef __WXGPE__
26 #include <gpe/init.h>
27#endif
28
29#include "wx/gtk/private.h"
30#include "wx/apptrait.h"
31
32#if wxUSE_LIBHILDON
33 #include <hildon-widgets/hildon-program.h>
34#endif // wxUSE_LIBHILDON
35
36#include <gdk/gdkx.h>
37
38//-----------------------------------------------------------------------------
39// link GnomeVFS
40//-----------------------------------------------------------------------------
41
42#if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
43 #include "wx/link.h"
44 wxFORCE_LINK_MODULE(gnome_vfs)
45#endif
46
47//-----------------------------------------------------------------------------
48// global data
49//-----------------------------------------------------------------------------
50
51static GtkWidget *gs_RootWindow = (GtkWidget*) NULL;
52
53//-----------------------------------------------------------------------------
54// wxYield
55//-----------------------------------------------------------------------------
56
57// not static because used by textctrl.cpp
58//
59// MT-FIXME
60bool wxIsInsideYield = false;
61
62bool wxApp::Yield(bool onlyIfNeeded)
63{
64 if ( wxIsInsideYield )
65 {
66 if ( !onlyIfNeeded )
67 {
68 wxFAIL_MSG( wxT("wxYield called recursively" ) );
69 }
70
71 return false;
72 }
73
74#if wxUSE_THREADS
75 if ( !wxThread::IsMain() )
76 {
77 // can't call gtk_main_iteration() from other threads like this
78 return true;
79 }
80#endif // wxUSE_THREADS
81
82 wxIsInsideYield = true;
83
84#if wxUSE_LOG
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();
88#endif
89
90 while (EventsPending())
91 gtk_main_iteration();
92
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
95 // OnUpdateUI() which is a nice (and desired) side effect). But we
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();
100
101#if wxUSE_LOG
102 // let the logs be flashed again
103 wxLog::Resume();
104#endif
105
106 wxIsInsideYield = false;
107
108 return true;
109}
110
111//-----------------------------------------------------------------------------
112// local functions
113//-----------------------------------------------------------------------------
114
115// One-shot signal emission hook, to install idle handler.
116extern "C" {
117static gboolean
118wx_emission_hook(GSignalInvocationHint*, guint, const GValue*, gpointer data)
119{
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;
126 // remove hook
127 return false;
128}
129}
130
131// Add signal emission hooks, to re-install idle handler when needed.
132static void wx_add_idle_hooks()
133{
134 // "event" hook
135 {
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 }
159 }
160}
161
162extern "C" {
163static gboolean wxapp_idle_callback(gpointer)
164{
165 return wxTheApp->DoIdle();
166}
167}
168
169bool wxApp::DoIdle()
170{
171 guint id_save;
172 {
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.
176#if wxUSE_THREADS
177 wxMutexLocker lock(*m_idleMutex);
178#endif
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 }
189
190 gdk_threads_enter();
191 bool needMore;
192 do {
193 needMore = ProcessIdle();
194 } while (needMore && gtk_events_pending() == 0);
195 gdk_threads_leave();
196
197#if wxUSE_THREADS
198 wxMutexLocker lock(*m_idleMutex);
199#endif
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;
206 }
207 // if more idle processing requested
208 if (needMore)
209 {
210 // keep this source installed
211 m_idleSourceId = id_save;
212 return true;
213 }
214 // add hooks and remove this source
215 wx_add_idle_hooks();
216 return false;
217}
218
219//-----------------------------------------------------------------------------
220// Access to the root window global
221//-----------------------------------------------------------------------------
222
223GtkWidget* wxGetRootWindow()
224{
225 if (gs_RootWindow == NULL)
226 {
227 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
228 gtk_widget_realize( gs_RootWindow );
229 }
230 return gs_RootWindow;
231}
232
233//-----------------------------------------------------------------------------
234// wxApp
235//-----------------------------------------------------------------------------
236
237IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
238
239wxApp::wxApp()
240{
241#ifdef __WXDEBUG__
242 m_isInAssert = false;
243#endif // __WXDEBUG__
244#if wxUSE_THREADS
245 m_idleMutex = NULL;
246#endif
247 m_idleSourceId = 0;
248}
249
250wxApp::~wxApp()
251{
252}
253
254bool wxApp::OnInitGui()
255{
256 if ( !wxAppBase::OnInitGui() )
257 return false;
258
259 // if this is a wxGLApp (derived from wxApp), and we've already
260 // chosen a specific visual, then derive the GdkVisual from that
261 if ( GetXVisualInfo() )
262 {
263 GdkVisual* vis = gtk_widget_get_default_visual();
264
265 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
266 gtk_widget_set_default_colormap( colormap );
267 }
268 else
269 {
270 // On some machines, the default visual is just 256 colours, so
271 // we make sure we get the best. This can sometimes be wasteful.
272 if (m_useBestVisual)
273 {
274 if (m_forceTrueColour)
275 {
276 GdkVisual* visual = gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR );
277 if (!visual)
278 {
279 wxLogError(wxT("Unable to initialize TrueColor visual."));
280 return false;
281 }
282 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
283 gtk_widget_set_default_colormap( colormap );
284 }
285 else
286 {
287 if (gdk_visual_get_best() != gdk_visual_get_system())
288 {
289 GdkVisual* visual = gdk_visual_get_best();
290 GdkColormap *colormap = gdk_colormap_new( visual, FALSE );
291 gtk_widget_set_default_colormap( colormap );
292 }
293 }
294 }
295 }
296
297#if wxUSE_LIBHILDON
298 m_hildonProgram = hildon_program_get_instance();
299 if ( !m_hildonProgram )
300 {
301 wxLogError(_("Unable to initialize Hildon program"));
302 return false;
303 }
304#endif // wxUSE_LIBHILDON
305
306 return true;
307}
308
309GdkVisual *wxApp::GetGdkVisual()
310{
311 GdkVisual *visual = NULL;
312
313 XVisualInfo *xvi = (XVisualInfo *)GetXVisualInfo();
314 if ( xvi )
315 visual = gdkx_visual_get( xvi->visualid );
316 else
317 visual = gdk_drawable_get_visual( wxGetRootWindow()->window );
318
319 wxASSERT( visual );
320
321 return visual;
322}
323
324// use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
325bool wxApp::Initialize(int& argc_, wxChar **argv_)
326{
327 if ( !wxAppBase::Initialize(argc_, argv_) )
328 return false;
329
330#if wxUSE_THREADS
331 if (!g_thread_supported())
332 {
333 g_thread_init(NULL);
334 gdk_threads_init();
335 }
336#endif // wxUSE_THREADS
337
338 // We should have the wxUSE_WCHAR_T test on the _outside_
339#if wxUSE_WCHAR_T
340 // gtk+ 2.0 supports Unicode through UTF-8 strings
341 wxConvCurrent = &wxConvUTF8;
342#else // !wxUSE_WCHAR_T
343 if (!wxOKlibc())
344 wxConvCurrent = (wxMBConv*) NULL;
345#endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
346
347 // decide which conversion to use for the file names
348
349 // (1) this variable exists for the sole purpose of specifying the encoding
350 // of the filenames for GTK+ programs, so use it if it is set
351 wxString encName(wxGetenv(_T("G_FILENAME_ENCODING")));
352 encName = encName.BeforeFirst(_T(','));
353 if (encName.CmpNoCase(_T("@locale")) == 0)
354 encName.clear();
355 encName.MakeUpper();
356#if wxUSE_INTL
357 if (encName.empty())
358 {
359 // (2) if a non default locale is set, assume that the user wants his
360 // filenames in this locale too
361 encName = wxLocale::GetSystemEncodingName().Upper();
362 // (3) finally use UTF-8 by default
363 if (encName.empty() || encName == _T("US-ASCII"))
364 encName = _T("UTF-8");
365 wxSetEnv(_T("G_FILENAME_ENCODING"), encName);
366 }
367#else
368 if (encName.empty())
369 encName = _T("UTF-8");
370
371 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
372 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
373 // from gtk_init_check() as it does by default
374 gtk_disable_setlocale();
375
376#endif // wxUSE_INTL
377 static wxConvBrokenFileNames fileconv(encName);
378 wxConvFileName = &fileconv;
379
380
381 bool init_result;
382 int i;
383
384#if wxUSE_UNICODE
385 // gtk_init() wants UTF-8, not wchar_t, so convert
386 char **argvGTK = new char *[argc_ + 1];
387 for ( i = 0; i < argc_; i++ )
388 {
389 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv_[i]));
390 }
391
392 argvGTK[argc_] = NULL;
393
394 int argcGTK = argc_;
395
396#ifdef __WXGPE__
397 init_result = true; // is there a _check() version of this?
398 gpe_application_init( &argcGTK, &argvGTK );
399#else
400 init_result = gtk_init_check( &argcGTK, &argvGTK );
401#endif
402 wxUpdateLocaleIsUtf8();
403
404 if ( argcGTK != argc_ )
405 {
406 // we have to drop the parameters which were consumed by GTK+
407 for ( i = 0; i < argcGTK; i++ )
408 {
409 while ( strcmp(wxConvUTF8.cWX2MB(argv_[i]), argvGTK[i]) != 0 )
410 {
411 memmove(argv_ + i, argv_ + i + 1, (argc_ - i)*sizeof(*argv_));
412 }
413 }
414
415 argc_ = argcGTK;
416 }
417 //else: gtk_init() didn't modify our parameters
418
419 // free our copy
420 for ( i = 0; i < argcGTK; i++ )
421 {
422 free(argvGTK[i]);
423 }
424
425 delete [] argvGTK;
426#else // !wxUSE_UNICODE
427 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
428 // it's ok to pass pointer to it
429 init_result = gtk_init_check( &argc_, &argv_ );
430#endif // wxUSE_UNICODE/!wxUSE_UNICODE
431
432 // update internal arg[cv] as GTK+ may have removed processed options:
433 this->argc = argc_;
434 this->argv = argv_;
435
436 if ( m_traits )
437 {
438 // if there are still GTK+ standard options unparsed in the command
439 // line, it means that they were not syntactically correct and GTK+
440 // already printed a warning on the command line and we should now
441 // exit:
442 wxArrayString opt, desc;
443 m_traits->GetStandardCmdLineOptions(opt, desc);
444
445 for ( i = 0; i < argc_; i++ )
446 {
447 // leave just the names of the options with values
448 const wxString str = wxString(argv_[i]).BeforeFirst('=');
449
450 for ( size_t j = 0; j < opt.size(); j++ )
451 {
452 // remove the leading spaces from the option string as it does
453 // have them
454 if ( opt[j].Trim(false).BeforeFirst('=') == str )
455 {
456 // a GTK+ option can be left on the command line only if
457 // there was an error in (or before, in another standard
458 // options) it, so abort, just as we do if incorrect
459 // program option is given
460 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
461 argv_[0]);
462 return false;
463 }
464 }
465 }
466 }
467
468 if ( !init_result )
469 {
470 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
471 return false;
472 }
473
474 // we can not enter threads before gtk_init is done
475 gdk_threads_enter();
476
477 wxSetDetectableAutoRepeat( true );
478
479#if wxUSE_INTL
480 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
481#endif
482
483#if wxUSE_THREADS
484 m_idleMutex = new wxMutex;
485#endif
486 // make sure GtkWidget type is loaded, idle hooks need it
487 g_type_class_ref(GTK_TYPE_WIDGET);
488 WakeUpIdle();
489
490 return true;
491}
492
493void wxApp::CleanUp()
494{
495 if (m_idleSourceId != 0)
496 g_source_remove(m_idleSourceId);
497
498 // release reference acquired by Initialize()
499 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET));
500
501 gdk_threads_leave();
502
503 wxAppBase::CleanUp();
504
505 // delete this mutex as late as possible as it's used from WakeUpIdle(), in
506 // particular do it after calling the base class CleanUp() which can result
507 // in it being called
508#if wxUSE_THREADS
509 delete m_idleMutex;
510 m_idleMutex = NULL;
511#endif
512}
513
514void wxApp::WakeUpIdle()
515{
516#if wxUSE_THREADS
517 wxMutexLocker lock(*m_idleMutex);
518#endif
519 if (m_idleSourceId == 0)
520 m_idleSourceId = g_idle_add_full(G_PRIORITY_LOW, wxapp_idle_callback, NULL, NULL);
521}
522
523// Checking for pending events requires first removing our idle source,
524// otherwise it will cause the check to always return true.
525bool wxApp::EventsPending()
526{
527#if wxUSE_THREADS
528 wxMutexLocker lock(*m_idleMutex);
529#endif
530 if (m_idleSourceId != 0)
531 {
532 g_source_remove(m_idleSourceId);
533 m_idleSourceId = 0;
534 wx_add_idle_hooks();
535 }
536 return gtk_events_pending() != 0;
537}
538
539#ifdef __WXDEBUG__
540
541void wxApp::OnAssertFailure(const wxChar *file,
542 int line,
543 const wxChar* func,
544 const wxChar* cond,
545 const wxChar *msg)
546{
547
548 // block wx idle events while assert dialog is showing
549 m_isInAssert = true;
550
551 wxAppBase::OnAssertFailure(file, line, func, cond, msg);
552
553 m_isInAssert = false;
554}
555
556#endif // __WXDEBUG__
557
558#if wxUSE_THREADS
559void wxGUIAppTraits::MutexGuiEnter()
560{
561 gdk_threads_enter();
562}
563
564void wxGUIAppTraits::MutexGuiLeave()
565{
566 gdk_threads_leave();
567}
568#endif // wxUSE_THREADS