]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/app.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
19 #include "wx/memory.h"
23 #include "wx/thread.h"
29 #include "wx/gtk/private.h"
30 #include "wx/apptrait.h"
33 #include <hildon-widgets/hildon-program.h>
34 #endif // wxUSE_LIBHILDON
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 #if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
44 wxFORCE_LINK_MODULE(gnome_vfs
)
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 bool g_mainThreadLocked
= false;
53 static GtkWidget
*gs_RootWindow
= (GtkWidget
*) NULL
;
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 // not static because used by textctrl.cpp
62 bool wxIsInsideYield
= false;
64 bool wxApp::Yield(bool onlyIfNeeded
)
66 if ( wxIsInsideYield
)
70 wxFAIL_MSG( wxT("wxYield called recursively" ) );
77 if ( !wxThread::IsMain() )
79 // can't call gtk_main_iteration() from other threads like this
82 #endif // wxUSE_THREADS
84 wxIsInsideYield
= true;
87 // disable log flushing from here because a call to wxYield() shouldn't
88 // normally result in message boxes popping up &c
92 while (EventsPending())
95 // It's necessary to call ProcessIdle() to update the frames sizes which
96 // might have been changed (it also will update other things set from
97 // OnUpdateUI() which is a nice (and desired) side effect). But we
98 // call ProcessIdle() only once since this is not meant for longish
99 // background jobs (controlled by wxIdleEvent::RequestMore() and the
100 // return value of Processidle().
104 // let the logs be flashed again
108 wxIsInsideYield
= false;
113 //-----------------------------------------------------------------------------
115 //-----------------------------------------------------------------------------
117 // One-shot signal emission hook, to install idle handler.
120 wx_emission_hook(GSignalInvocationHint
*, guint
, const GValue
*, gpointer data
)
122 wxApp
* app
= wxTheApp
;
125 gulong
* hook_id
= (gulong
*)data
;
126 // record that hook is not installed
133 // Add signal emission hooks, to re-install idle handler when needed.
134 static void wx_add_idle_hooks()
138 static gulong hook_id
= 0;
141 static guint sig_id
= 0;
143 sig_id
= g_signal_lookup("event", GTK_TYPE_WIDGET
);
144 hook_id
= g_signal_add_emission_hook(
145 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
148 // "size_allocate" hook
149 // Needed to match the behavior of the old idle system,
150 // but probably not necessary.
152 static gulong hook_id
= 0;
155 static guint sig_id
= 0;
157 sig_id
= g_signal_lookup("size_allocate", GTK_TYPE_WIDGET
);
158 hook_id
= g_signal_add_emission_hook(
159 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
165 static gboolean
wxapp_idle_callback(gpointer
)
167 return wxTheApp
->DoIdle();
175 // Allow another idle source to be added while this one is busy.
176 // Needed if an idle event handler runs a new event loop,
177 // for example by showing a dialog.
179 wxMutexLocker
lock(*m_idleMutex
);
181 id_save
= m_idleSourceId
;
185 // don't generate the idle events while the assert modal dialog is shown,
186 // this matches the behavior of wxMSW
195 needMore
= ProcessIdle();
196 } while (needMore
&& gtk_events_pending() == 0);
200 wxMutexLocker
lock(*m_idleMutex
);
202 // if a new idle source was added during ProcessIdle
203 if (m_idleSourceId
!= 0)
206 g_source_remove(m_idleSourceId
);
209 // if more idle processing requested
212 // keep this source installed
213 m_idleSourceId
= id_save
;
216 // add hooks and remove this source
223 static GPollFunc wxgs_poll_func
;
226 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
231 g_mainThreadLocked
= true;
233 gint res
= (*wxgs_poll_func
)(ufds
, nfds
, timeout
);
236 g_mainThreadLocked
= false;
244 #endif // wxUSE_THREADS
246 //-----------------------------------------------------------------------------
247 // Access to the root window global
248 //-----------------------------------------------------------------------------
250 GtkWidget
* wxGetRootWindow()
252 if (gs_RootWindow
== NULL
)
254 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
255 gtk_widget_realize( gs_RootWindow
);
257 return gs_RootWindow
;
260 //-----------------------------------------------------------------------------
262 //-----------------------------------------------------------------------------
264 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
269 m_isInAssert
= false;
270 #endif // __WXDEBUG__
281 bool wxApp::OnInitGui()
283 if ( !wxAppBase::OnInitGui() )
286 // if this is a wxGLApp (derived from wxApp), and we've already
287 // chosen a specific visual, then derive the GdkVisual from that
288 if ( GetXVisualInfo() )
290 GdkVisual
* vis
= gtk_widget_get_default_visual();
292 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
293 gtk_widget_set_default_colormap( colormap
);
297 // On some machines, the default visual is just 256 colours, so
298 // we make sure we get the best. This can sometimes be wasteful.
301 if (m_forceTrueColour
)
303 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
306 wxLogError(wxT("Unable to initialize TrueColor visual."));
309 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
310 gtk_widget_set_default_colormap( colormap
);
314 if (gdk_visual_get_best() != gdk_visual_get_system())
316 GdkVisual
* visual
= gdk_visual_get_best();
317 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
318 gtk_widget_set_default_colormap( colormap
);
325 m_hildonProgram
= hildon_program_get_instance();
326 if ( !m_hildonProgram
)
328 wxLogError(_("Unable to initialize Hildon program"));
331 #endif // wxUSE_LIBHILDON
336 GdkVisual
*wxApp::GetGdkVisual()
338 GdkVisual
*visual
= NULL
;
340 XVisualInfo
*xvi
= (XVisualInfo
*)GetXVisualInfo();
342 visual
= gdkx_visual_get( xvi
->visualid
);
344 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
351 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
352 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
354 if ( !wxAppBase::Initialize(argc_
, argv_
) )
358 if (!g_thread_supported())
361 wxgs_poll_func
= g_main_context_get_poll_func(NULL
);
362 g_main_context_set_poll_func(NULL
, wxapp_poll_func
);
363 #endif // wxUSE_THREADS
365 // We should have the wxUSE_WCHAR_T test on the _outside_
367 // gtk+ 2.0 supports Unicode through UTF-8 strings
368 wxConvCurrent
= &wxConvUTF8
;
369 #else // !wxUSE_WCHAR_T
371 wxConvCurrent
= (wxMBConv
*) NULL
;
372 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
374 // decide which conversion to use for the file names
376 // (1) this variable exists for the sole purpose of specifying the encoding
377 // of the filenames for GTK+ programs, so use it if it is set
378 wxString
encName(wxGetenv(_T("G_FILENAME_ENCODING")));
379 encName
= encName
.BeforeFirst(_T(','));
380 if (encName
.CmpNoCase(_T("@locale")) == 0)
386 // (2) if a non default locale is set, assume that the user wants his
387 // filenames in this locale too
388 encName
= wxLocale::GetSystemEncodingName().Upper();
389 // (3) finally use UTF-8 by default
390 if (encName
.empty() || encName
== _T("US-ASCII"))
391 encName
= _T("UTF-8");
392 wxSetEnv(_T("G_FILENAME_ENCODING"), encName
);
396 encName
= _T("UTF-8");
398 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
399 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
400 // from gtk_init_check() as it does by default
401 gtk_disable_setlocale();
404 static wxConvBrokenFileNames
fileconv(encName
);
405 wxConvFileName
= &fileconv
;
412 // gtk_init() wants UTF-8, not wchar_t, so convert
413 char **argvGTK
= new char *[argc_
+ 1];
414 for ( i
= 0; i
< argc_
; i
++ )
416 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv_
[i
]));
419 argvGTK
[argc_
] = NULL
;
424 init_result
= true; // is there a _check() version of this?
425 gpe_application_init( &argcGTK
, &argvGTK
);
427 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
429 wxUpdateLocaleIsUtf8();
431 if ( argcGTK
!= argc_
)
433 // we have to drop the parameters which were consumed by GTK+
434 for ( i
= 0; i
< argcGTK
; i
++ )
436 while ( strcmp(wxConvUTF8
.cWX2MB(argv_
[i
]), argvGTK
[i
]) != 0 )
438 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc_
- i
)*sizeof(*argv_
));
444 //else: gtk_init() didn't modify our parameters
447 for ( i
= 0; i
< argcGTK
; i
++ )
453 #else // !wxUSE_UNICODE
454 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
455 // it's ok to pass pointer to it
456 init_result
= gtk_init_check( &argc_
, &argv_
);
457 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
459 // update internal arg[cv] as GTK+ may have removed processed options:
465 // if there are still GTK+ standard options unparsed in the command
466 // line, it means that they were not syntactically correct and GTK+
467 // already printed a warning on the command line and we should now
469 wxArrayString opt
, desc
;
470 m_traits
->GetStandardCmdLineOptions(opt
, desc
);
472 for ( i
= 0; i
< argc_
; i
++ )
474 // leave just the names of the options with values
475 const wxString str
= wxString(argv_
[i
]).BeforeFirst('=');
477 for ( size_t j
= 0; j
< opt
.size(); j
++ )
479 // remove the leading spaces from the option string as it does
481 if ( opt
[j
].Trim(false).BeforeFirst('=') == str
)
483 // a GTK+ option can be left on the command line only if
484 // there was an error in (or before, in another standard
485 // options) it, so abort, just as we do if incorrect
486 // program option is given
487 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
497 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
501 // we can not enter threads before gtk_init is done
504 wxSetDetectableAutoRepeat( true );
507 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
511 m_idleMutex
= new wxMutex
;
513 // make sure GtkWidget type is loaded, idle hooks need it
514 g_type_class_ref(GTK_TYPE_WIDGET
);
520 void wxApp::CleanUp()
522 if (m_idleSourceId
!= 0)
523 g_source_remove(m_idleSourceId
);
525 // release reference acquired by Initialize()
526 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET
));
530 wxAppBase::CleanUp();
532 // delete this mutex as late as possible as it's used from WakeUpIdle(), in
533 // particular do it after calling the base class CleanUp() which can result
534 // in it being called
541 void wxApp::WakeUpIdle()
544 wxMutexLocker
lock(*m_idleMutex
);
546 if (m_idleSourceId
== 0)
547 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
550 // Checking for pending events requires first removing our idle source,
551 // otherwise it will cause the check to always return true.
552 bool wxApp::EventsPending()
555 wxMutexLocker
lock(*m_idleMutex
);
557 if (m_idleSourceId
!= 0)
559 g_source_remove(m_idleSourceId
);
563 return gtk_events_pending() != 0;
568 void wxApp::OnAssertFailure(const wxChar
*file
,
575 // block wx idle events while assert dialog is showing
578 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
580 m_isInAssert
= false;
583 #endif // __WXDEBUG__