]>
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/win_gtk.h"
30 #include "wx/gtk/private.h"
31 #include "wx/apptrait.h"
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 #if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
41 wxFORCE_LINK_MODULE(gnome_vfs
)
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 bool g_mainThreadLocked
= false;
50 static GtkWidget
*gs_RootWindow
= (GtkWidget
*) NULL
;
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 // not static because used by textctrl.cpp
59 bool wxIsInsideYield
= false;
61 bool wxApp::Yield(bool onlyIfNeeded
)
63 if ( wxIsInsideYield
)
67 wxFAIL_MSG( wxT("wxYield called recursively" ) );
74 if ( !wxThread::IsMain() )
76 // can't call gtk_main_iteration() from other threads like this
79 #endif // wxUSE_THREADS
81 wxIsInsideYield
= true;
84 // disable log flushing from here because a call to wxYield() shouldn't
85 // normally result in message boxes popping up &c
89 while (EventsPending())
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
94 // OnUpdateUI() which is a nice (and desired) side effect). But we
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().
101 // let the logs be flashed again
105 wxIsInsideYield
= false;
110 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
114 // One-shot signal emission hook, to install idle handler.
117 wx_emission_hook(GSignalInvocationHint
*, guint
, const GValue
*, gpointer data
)
119 wxApp
* app
= wxTheApp
;
122 gulong
* hook_id
= (gulong
*)data
;
123 // record that hook is not installed
130 // Add signal emission hooks, to re-install idle handler when needed.
131 static void wx_add_idle_hooks()
135 static gulong hook_id
= 0;
138 static guint 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
);
145 // "size_allocate" hook
146 // Needed to match the behavior of the old idle system,
147 // but probably not necessary.
149 static gulong hook_id
= 0;
152 static guint 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
);
162 static gboolean
wxapp_idle_callback(gpointer
)
164 return wxTheApp
->DoIdle();
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.
176 wxMutexLocker
lock(*m_idleMutex
);
178 id_save
= m_idleSourceId
;
182 // don't generate the idle events while the assert modal dialog is shown,
183 // this matches the behavior of wxMSW
192 needMore
= ProcessIdle();
193 } while (needMore
&& gtk_events_pending() == 0);
197 wxMutexLocker
lock(*m_idleMutex
);
199 // if a new idle source was added during ProcessIdle
200 if (m_idleSourceId
!= 0)
203 g_source_remove(m_idleSourceId
);
206 // if more idle processing requested
209 // keep this source installed
210 m_idleSourceId
= id_save
;
213 // add hooks and remove this source
220 static GPollFunc wxgs_poll_func
;
223 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
228 g_mainThreadLocked
= true;
230 gint res
= (*wxgs_poll_func
)(ufds
, nfds
, timeout
);
233 g_mainThreadLocked
= false;
241 #endif // wxUSE_THREADS
243 //-----------------------------------------------------------------------------
244 // Access to the root window global
245 //-----------------------------------------------------------------------------
247 GtkWidget
* wxGetRootWindow()
249 if (gs_RootWindow
== NULL
)
251 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
252 gtk_widget_realize( gs_RootWindow
);
254 return gs_RootWindow
;
257 //-----------------------------------------------------------------------------
259 //-----------------------------------------------------------------------------
261 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
266 m_isInAssert
= false;
267 #endif // __WXDEBUG__
278 bool wxApp::OnInitGui()
280 if ( !wxAppBase::OnInitGui() )
283 // if this is a wxGLApp (derived from wxApp), and we've already
284 // chosen a specific visual, then derive the GdkVisual from that
285 if ( GetXVisualInfo() )
287 GdkVisual
* vis
= gtk_widget_get_default_visual();
289 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
290 gtk_widget_set_default_colormap( colormap
);
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.
298 if (m_forceTrueColour
)
300 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
303 wxLogError(wxT("Unable to initialize TrueColor visual."));
306 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
307 gtk_widget_set_default_colormap( colormap
);
311 if (gdk_visual_get_best() != gdk_visual_get_system())
313 GdkVisual
* visual
= gdk_visual_get_best();
314 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
315 gtk_widget_set_default_colormap( colormap
);
324 GdkVisual
*wxApp::GetGdkVisual()
326 GdkVisual
*visual
= NULL
;
328 XVisualInfo
*xvi
= (XVisualInfo
*)GetXVisualInfo();
330 visual
= gdkx_visual_get( xvi
->visualid
);
332 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
339 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
340 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
342 if ( !wxAppBase::Initialize(argc_
, argv_
) )
346 if (!g_thread_supported())
349 wxgs_poll_func
= g_main_context_get_poll_func(NULL
);
350 g_main_context_set_poll_func(NULL
, wxapp_poll_func
);
351 #endif // wxUSE_THREADS
353 // We should have the wxUSE_WCHAR_T test on the _outside_
355 // gtk+ 2.0 supports Unicode through UTF-8 strings
356 wxConvCurrent
= &wxConvUTF8
;
357 #else // !wxUSE_WCHAR_T
359 wxConvCurrent
= (wxMBConv
*) NULL
;
360 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
362 // decide which conversion to use for the file names
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")));
367 encName
= encName
.BeforeFirst(_T(','));
368 if (encName
.CmpNoCase(_T("@locale")) == 0)
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
);
384 encName
= _T("UTF-8");
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();
392 static wxConvBrokenFileNames
fileconv(encName
);
393 wxConvFileName
= &fileconv
;
400 // gtk_init() wants UTF-8, not wchar_t, so convert
401 char **argvGTK
= new char *[argc_
+ 1];
402 for ( i
= 0; i
< argc_
; i
++ )
404 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv_
[i
]));
407 argvGTK
[argc_
] = NULL
;
412 init_result
= true; // is there a _check() version of this?
413 gpe_application_init( &argcGTK
, &argvGTK
);
415 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
417 wxUpdateLocaleIsUtf8();
419 if ( argcGTK
!= argc_
)
421 // we have to drop the parameters which were consumed by GTK+
422 for ( i
= 0; i
< argcGTK
; i
++ )
424 while ( strcmp(wxConvUTF8
.cWX2MB(argv_
[i
]), argvGTK
[i
]) != 0 )
426 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc_
- i
)*sizeof(*argv_
));
432 //else: gtk_init() didn't modify our parameters
435 for ( i
= 0; i
< argcGTK
; i
++ )
441 #else // !wxUSE_UNICODE
442 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
443 // it's ok to pass pointer to it
444 init_result
= gtk_init_check( &argc_
, &argv_
);
445 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
447 // update internal arg[cv] as GTK+ may have removed processed options:
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
457 wxArrayString opt
, desc
;
458 m_traits
->GetStandardCmdLineOptions(opt
, desc
);
460 for ( i
= 0; i
< argc_
; i
++ )
462 // leave just the names of the options with values
463 const wxString str
= wxString(argv_
[i
]).BeforeFirst('=');
465 for ( size_t j
= 0; j
< opt
.size(); j
++ )
467 // remove the leading spaces from the option string as it does
469 if ( opt
[j
].Trim(false).BeforeFirst('=') == str
)
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\""),
485 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
489 // we can not enter threads before gtk_init is done
492 wxSetDetectableAutoRepeat( true );
495 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
499 m_idleMutex
= new wxMutex
;
501 // make sure GtkWidget type is loaded, idle hooks need it
502 g_type_class_ref(GTK_TYPE_WIDGET
);
508 void wxApp::CleanUp()
510 if (m_idleSourceId
!= 0)
511 g_source_remove(m_idleSourceId
);
516 // release reference acquired by Initialize()
517 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET
));
521 wxAppBase::CleanUp();
524 void wxApp::WakeUpIdle()
527 wxMutexLocker
lock(*m_idleMutex
);
529 if (m_idleSourceId
== 0)
530 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
533 // Checking for pending events requires first removing our idle source,
534 // otherwise it will cause the check to always return true.
535 bool wxApp::EventsPending()
538 wxMutexLocker
lock(*m_idleMutex
);
540 if (m_idleSourceId
!= 0)
542 g_source_remove(m_idleSourceId
);
546 return gtk_events_pending() != 0;
551 void wxApp::OnAssertFailure(const wxChar
*file
,
558 // block wx idle events while assert dialog is showing
561 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
563 m_isInAssert
= false;
566 #endif // __WXDEBUG__