]>
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"
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 #if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
40 wxFORCE_LINK_MODULE(gnome_vfs
)
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 bool g_mainThreadLocked
= false;
49 static GtkWidget
*gs_RootWindow
= (GtkWidget
*) NULL
;
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 // not static because used by textctrl.cpp
58 bool wxIsInsideYield
= false;
60 bool wxApp::Yield(bool onlyIfNeeded
)
62 if ( wxIsInsideYield
)
66 wxFAIL_MSG( wxT("wxYield called recursively" ) );
73 if ( !wxThread::IsMain() )
75 // can't call gtk_main_iteration() from other threads like this
78 #endif // wxUSE_THREADS
80 wxIsInsideYield
= true;
83 // disable log flushing from here because a call to wxYield() shouldn't
84 // normally result in message boxes popping up &c
88 while (EventsPending())
91 // It's necessary to call ProcessIdle() to update the frames sizes which
92 // might have been changed (it also will update other things set from
93 // OnUpdateUI() which is a nice (and desired) side effect). But we
94 // call ProcessIdle() only once since this is not meant for longish
95 // background jobs (controlled by wxIdleEvent::RequestMore() and the
96 // return value of Processidle().
100 // let the logs be flashed again
104 wxIsInsideYield
= false;
109 //-----------------------------------------------------------------------------
111 //-----------------------------------------------------------------------------
113 // One-shot signal emission hook, to install idle handler.
116 wx_emission_hook(GSignalInvocationHint
*, guint
, const GValue
*, gpointer data
)
118 wxApp
* app
= wxTheApp
;
121 gulong
* hook_id
= (gulong
*)data
;
122 // record that hook is not installed
129 // Add signal emission hooks, to re-install idle handler when needed.
130 static void wx_add_idle_hooks()
134 static gulong hook_id
= 0;
137 static guint sig_id
= 0;
139 sig_id
= g_signal_lookup("event", GTK_TYPE_WIDGET
);
140 hook_id
= g_signal_add_emission_hook(
141 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
144 // "size_allocate" hook
145 // Needed to match the behavior of the old idle system,
146 // but probably not necessary.
148 static gulong hook_id
= 0;
151 static guint sig_id
= 0;
153 sig_id
= g_signal_lookup("size_allocate", GTK_TYPE_WIDGET
);
154 hook_id
= g_signal_add_emission_hook(
155 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
161 static gboolean
wxapp_idle_callback(gpointer
)
163 return wxTheApp
->DoIdle();
171 // Allow another idle source to be added while this one is busy.
172 // Needed if an idle event handler runs a new event loop,
173 // for example by showing a dialog.
175 wxMutexLocker
lock(*m_idleMutex
);
177 id_save
= m_idleSourceId
;
181 // don't generate the idle events while the assert modal dialog is shown,
182 // this matches the behavior of wxMSW
191 needMore
= ProcessIdle();
192 } while (needMore
&& gtk_events_pending() == 0);
196 wxMutexLocker
lock(*m_idleMutex
);
198 // if a new idle source was added during ProcessIdle
199 if (m_idleSourceId
!= 0)
202 g_source_remove(m_idleSourceId
);
205 // if more idle processing requested
208 // keep this source installed
209 m_idleSourceId
= id_save
;
212 // add hooks and remove this source
219 static GPollFunc wxgs_poll_func
;
222 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
227 g_mainThreadLocked
= true;
229 gint res
= (*wxgs_poll_func
)(ufds
, nfds
, timeout
);
232 g_mainThreadLocked
= false;
240 #endif // wxUSE_THREADS
242 //-----------------------------------------------------------------------------
243 // Access to the root window global
244 //-----------------------------------------------------------------------------
246 GtkWidget
* wxGetRootWindow()
248 if (gs_RootWindow
== NULL
)
250 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
251 gtk_widget_realize( gs_RootWindow
);
253 return gs_RootWindow
;
256 //-----------------------------------------------------------------------------
258 //-----------------------------------------------------------------------------
260 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
265 m_isInAssert
= false;
266 #endif // __WXDEBUG__
277 bool wxApp::OnInitGui()
279 if ( !wxAppBase::OnInitGui() )
282 // if this is a wxGLApp (derived from wxApp), and we've already
283 // chosen a specific visual, then derive the GdkVisual from that
284 if ( GetXVisualInfo() )
286 GdkVisual
* vis
= gtk_widget_get_default_visual();
288 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
289 gtk_widget_set_default_colormap( colormap
);
293 // On some machines, the default visual is just 256 colours, so
294 // we make sure we get the best. This can sometimes be wasteful.
297 if (m_forceTrueColour
)
299 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
302 wxLogError(wxT("Unable to initialize TrueColor visual."));
305 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
306 gtk_widget_set_default_colormap( colormap
);
310 if (gdk_visual_get_best() != gdk_visual_get_system())
312 GdkVisual
* visual
= gdk_visual_get_best();
313 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
314 gtk_widget_set_default_colormap( colormap
);
323 GdkVisual
*wxApp::GetGdkVisual()
325 GdkVisual
*visual
= NULL
;
327 XVisualInfo
*xvi
= (XVisualInfo
*)GetXVisualInfo();
329 visual
= gdkx_visual_get( xvi
->visualid
);
331 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
338 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
339 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
341 if ( !wxAppBase::Initialize(argc_
, argv_
) )
345 if (!g_thread_supported())
348 wxgs_poll_func
= g_main_context_get_poll_func(NULL
);
349 g_main_context_set_poll_func(NULL
, wxapp_poll_func
);
350 #endif // wxUSE_THREADS
352 // We should have the wxUSE_WCHAR_T test on the _outside_
354 // gtk+ 2.0 supports Unicode through UTF-8 strings
355 wxConvCurrent
= &wxConvUTF8
;
356 #else // !wxUSE_WCHAR_T
358 wxConvCurrent
= (wxMBConv
*) NULL
;
359 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
361 // decide which conversion to use for the file names
363 // (1) this variable exists for the sole purpose of specifying the encoding
364 // of the filenames for GTK+ programs, so use it if it is set
365 wxString
encName(wxGetenv(_T("G_FILENAME_ENCODING")));
366 encName
= encName
.BeforeFirst(_T(','));
367 if (encName
.CmpNoCase(_T("@locale")) == 0)
373 // (2) if a non default locale is set, assume that the user wants his
374 // filenames in this locale too
375 encName
= wxLocale::GetSystemEncodingName().Upper();
376 // (3) finally use UTF-8 by default
377 if (encName
.empty() || encName
== _T("US-ASCII"))
378 encName
= _T("UTF-8");
379 wxSetEnv(_T("G_FILENAME_ENCODING"), encName
);
383 encName
= _T("UTF-8");
385 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
386 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
387 // from gtk_init_check() as it does by default
388 gtk_disable_setlocale();
391 static wxConvBrokenFileNames
fileconv(encName
);
392 wxConvFileName
= &fileconv
;
399 // gtk_init() wants UTF-8, not wchar_t, so convert
400 char **argvGTK
= new char *[argc_
+ 1];
401 for ( i
= 0; i
< argc_
; i
++ )
403 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv_
[i
]));
406 argvGTK
[argc_
] = NULL
;
411 init_result
= true; // is there a _check() version of this?
412 gpe_application_init( &argcGTK
, &argvGTK
);
414 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
416 wxUpdateLocaleIsUtf8();
418 if ( argcGTK
!= argc_
)
420 // we have to drop the parameters which were consumed by GTK+
421 for ( i
= 0; i
< argcGTK
; i
++ )
423 while ( strcmp(wxConvUTF8
.cWX2MB(argv_
[i
]), argvGTK
[i
]) != 0 )
425 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc_
- i
)*sizeof(*argv_
));
431 //else: gtk_init() didn't modify our parameters
434 for ( i
= 0; i
< argcGTK
; i
++ )
440 #else // !wxUSE_UNICODE
441 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
442 // it's ok to pass pointer to it
443 init_result
= gtk_init_check( &argc_
, &argv_
);
444 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
446 // update internal arg[cv] as GTK+ may have removed processed options:
452 // if there are still GTK+ standard options unparsed in the command
453 // line, it means that they were not syntactically correct and GTK+
454 // already printed a warning on the command line and we should now
456 wxArrayString opt
, desc
;
457 m_traits
->GetStandardCmdLineOptions(opt
, desc
);
459 for ( i
= 0; i
< argc_
; i
++ )
461 // leave just the names of the options with values
462 const wxString str
= wxString(argv_
[i
]).BeforeFirst('=');
464 for ( size_t j
= 0; j
< opt
.size(); j
++ )
466 // remove the leading spaces from the option string as it does
468 if ( opt
[j
].Trim(false).BeforeFirst('=') == str
)
470 // a GTK+ option can be left on the command line only if
471 // there was an error in (or before, in another standard
472 // options) it, so abort, just as we do if incorrect
473 // program option is given
474 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
484 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
488 // we can not enter threads before gtk_init is done
491 wxSetDetectableAutoRepeat( true );
494 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
498 m_idleMutex
= new wxMutex
;
500 // make sure GtkWidget type is loaded, idle hooks need it
501 g_type_class_ref(GTK_TYPE_WIDGET
);
507 void wxApp::CleanUp()
509 if (m_idleSourceId
!= 0)
510 g_source_remove(m_idleSourceId
);
515 // release reference acquired by Initialize()
516 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET
));
520 wxAppBase::CleanUp();
523 void wxApp::WakeUpIdle()
526 wxMutexLocker
lock(*m_idleMutex
);
528 if (m_idleSourceId
== 0)
529 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
532 // Checking for pending events requires first removing our idle source,
533 // otherwise it will cause the check to always return true.
534 bool wxApp::EventsPending()
537 wxMutexLocker
lock(*m_idleMutex
);
539 if (m_idleSourceId
!= 0)
541 g_source_remove(m_idleSourceId
);
545 return gtk_events_pending() != 0;
550 void wxApp::OnAssertFailure(const wxChar
*file
,
557 // block wx idle events while assert dialog is showing
560 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
562 m_isInAssert
= false;
565 #endif // __WXDEBUG__