]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/utilsgtk.cpp
missing commit
[wxWidgets.git] / src / gtk / utilsgtk.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/utilsgtk.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#include "wx/utils.h"
14
15#ifndef WX_PRECOMP
16 #include "wx/string.h"
17 #include "wx/intl.h"
18 #include "wx/log.h"
19#endif
20
21#include "wx/apptrait.h"
22#include "wx/process.h"
23#include "wx/sysopt.h"
24#ifdef __UNIX__
25#include "wx/unix/execute.h"
26#endif
27
28#include "wx/gtk/private/timer.h"
29#include "wx/evtloop.h"
30
31#include <gtk/gtk.h>
32#ifdef GDK_WINDOWING_WIN32
33#include <gdk/gdkwin32.h>
34#endif
35#ifdef GDK_WINDOWING_X11
36#include <gdk/gdkx.h>
37#endif
38
39#if wxDEBUG_LEVEL
40 #include "wx/gtk/assertdlg_gtk.h"
41 #if wxUSE_STACKWALKER
42 #include "wx/stackwalk.h"
43 #endif // wxUSE_STACKWALKER
44#endif // wxDEBUG_LEVEL
45
46#include <stdarg.h>
47#include <string.h>
48#include <sys/stat.h>
49#include <sys/types.h>
50#ifdef __UNIX__
51#include <unistd.h>
52#endif
53
54#if wxUSE_DETECT_SM
55 #include <X11/SM/SMlib.h>
56
57 #include "wx/unix/utilsx11.h"
58#endif
59
60#include "wx/gtk/private/gtk2-compat.h"
61
62//-----------------------------------------------------------------------------
63// data
64//-----------------------------------------------------------------------------
65
66extern GtkWidget *wxGetRootWindow();
67
68//----------------------------------------------------------------------------
69// misc.
70//----------------------------------------------------------------------------
71
72void wxBell()
73{
74 gdk_beep();
75}
76
77// ----------------------------------------------------------------------------
78// display characteristics
79// ----------------------------------------------------------------------------
80
81#ifdef GDK_WINDOWING_X11
82void *wxGetDisplay()
83{
84 return GDK_DISPLAY_XDISPLAY(gtk_widget_get_display(wxGetRootWindow()));
85}
86#endif
87
88void wxDisplaySize( int *width, int *height )
89{
90 if (width) *width = gdk_screen_width();
91 if (height) *height = gdk_screen_height();
92}
93
94void wxDisplaySizeMM( int *width, int *height )
95{
96 if (width) *width = gdk_screen_width_mm();
97 if (height) *height = gdk_screen_height_mm();
98}
99
100bool wxColourDisplay()
101{
102 return true;
103}
104
105int wxDisplayDepth()
106{
107 return gdk_visual_get_depth(gtk_widget_get_visual(wxGetRootWindow()));
108}
109
110wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
111{
112 return wxGenericFindWindowAtPoint(pt);
113}
114
115#if !wxUSE_UNICODE
116
117WXDLLIMPEXP_CORE wxCharBuffer
118wxConvertToGTK(const wxString& s, wxFontEncoding enc)
119{
120 wxWCharBuffer wbuf;
121 if ( enc == wxFONTENCODING_SYSTEM || enc == wxFONTENCODING_DEFAULT )
122 {
123 wbuf = wxConvUI->cMB2WC(s);
124 }
125 else // another encoding, use generic conversion class
126 {
127 wbuf = wxCSConv(enc).cMB2WC(s.c_str());
128 }
129
130 if ( !wbuf && !s.empty() )
131 {
132 // conversion failed, but we still want to show something to the user
133 // even if it's going to be wrong it is better than nothing
134 //
135 // we choose ISO8859-1 here arbitrarily, it's just the most common
136 // encoding probably and, also importantly here, conversion from it
137 // never fails as it's done internally by wxCSConv
138 wbuf = wxCSConv(wxFONTENCODING_ISO8859_1).cMB2WC(s.c_str());
139 }
140
141 return wxConvUTF8.cWC2MB(wbuf);
142}
143
144WXDLLIMPEXP_CORE wxCharBuffer
145wxConvertFromGTK(const wxString& s, wxFontEncoding enc)
146{
147 // this conversion should never fail as GTK+ always uses UTF-8 internally
148 // so there are no complications here
149 const wxWCharBuffer wbuf(wxConvUTF8.cMB2WC(s.c_str()));
150 if ( enc == wxFONTENCODING_SYSTEM )
151 return wxConvUI->cWC2MB(wbuf);
152
153 return wxCSConv(enc).cWC2MB(wbuf);
154}
155
156#endif // !wxUSE_UNICODE
157
158// Returns NULL if version is certainly greater or equal than major.minor.micro
159// Returns string describing the error if version is lower than
160// major.minor.micro OR it cannot be determined and one should not rely on the
161// availability of pango version major.minor.micro, nor the non-availability
162const gchar *wx_pango_version_check (int major, int minor, int micro)
163{
164 // NOTE: you don't need to use this macro to check for Pango features
165 // added in pango-1.4 or earlier since GTK 2.4 (our minimum requirement
166 // for GTK lib) required pango 1.4...
167
168#ifdef __WXGTK3__
169 return pango_version_check(major, minor, micro);
170#elif defined(PANGO_VERSION_MAJOR)
171 if (!gtk_check_version (2,11,0))
172 {
173 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
174 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
175 // using has the pango_version_check function:
176 return pango_version_check (major, minor, micro);
177 }
178
179 return "can't check";
180#else // !PANGO_VERSION_MAJOR
181 wxUnusedVar(major);
182 wxUnusedVar(minor);
183 wxUnusedVar(micro);
184
185 return "too old headers";
186#endif
187}
188
189// ----------------------------------------------------------------------------
190// subprocess routines
191// ----------------------------------------------------------------------------
192
193#ifdef __UNIX__
194
195extern "C" {
196static gboolean EndProcessDetector(GIOChannel* source, GIOCondition, void* data)
197{
198 wxEndProcessData * const
199 proc_data = static_cast<wxEndProcessData *>(data);
200
201 // child exited, end waiting
202 close(g_io_channel_unix_get_fd(source));
203
204 wxHandleProcessTermination(proc_data);
205
206 // don't call us again!
207 return false;
208}
209}
210
211int wxGUIAppTraits::AddProcessCallback(wxEndProcessData *proc_data, int fd)
212{
213 GIOChannel* channel = g_io_channel_unix_new(fd);
214 GIOCondition cond = GIOCondition(G_IO_IN | G_IO_HUP | G_IO_ERR);
215 unsigned id = g_io_add_watch(channel, cond, EndProcessDetector, proc_data);
216 g_io_channel_unref(channel);
217 return int(id);
218}
219
220#endif // __UNIX__
221
222// ----------------------------------------------------------------------------
223// wxPlatformInfo-related
224// ----------------------------------------------------------------------------
225
226wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
227{
228 if ( verMaj )
229 *verMaj = gtk_major_version;
230 if ( verMin )
231 *verMin = gtk_minor_version;
232
233 return wxPORT_GTK;
234}
235
236#if wxUSE_TIMER
237
238wxTimerImpl *wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
239{
240 return new wxGTKTimerImpl(timer);
241}
242
243#endif // wxUSE_TIMER
244
245#if wxUSE_DETECT_SM
246static wxString GetSM()
247{
248 wxX11Display dpy;
249 if ( !dpy )
250 return wxEmptyString;
251
252 char smerr[256];
253 char *client_id;
254 SmcConn smc_conn = SmcOpenConnection(NULL, NULL,
255 999, 999,
256 0 /* mask */, NULL /* callbacks */,
257 NULL, &client_id,
258 WXSIZEOF(smerr), smerr);
259
260 if ( !smc_conn )
261 {
262 wxLogDebug("Failed to connect to session manager: %s", smerr);
263 return wxEmptyString;
264 }
265
266 char *vendor = SmcVendor(smc_conn);
267 wxString ret = wxString::FromAscii( vendor );
268 free(vendor);
269
270 SmcCloseConnection(smc_conn, 0, NULL);
271 free(client_id);
272
273 return ret;
274}
275#endif // wxUSE_DETECT_SM
276
277
278//-----------------------------------------------------------------------------
279// wxGUIAppTraits
280//-----------------------------------------------------------------------------
281
282wxEventLoopBase *wxGUIAppTraits::CreateEventLoop()
283{
284 return new wxEventLoop();
285}
286
287
288#ifdef __UNIX__
289
290#if wxDEBUG_LEVEL && wxUSE_STACKWALKER
291
292// private helper class
293class StackDump : public wxStackWalker
294{
295public:
296 StackDump(GtkAssertDialog *dlg) { m_dlg=dlg; }
297
298protected:
299 virtual void OnStackFrame(const wxStackFrame& frame)
300 {
301 wxString fncname = frame.GetName();
302
303 // append this stack frame's info in the dialog
304 if (!frame.GetFileName().empty() || !fncname.empty())
305 {
306 gtk_assert_dialog_append_stack_frame(m_dlg,
307 fncname.utf8_str(),
308 frame.GetFileName().utf8_str(),
309 frame.GetLine());
310 }
311 }
312
313private:
314 GtkAssertDialog *m_dlg;
315};
316
317static void get_stackframe_callback(void* p)
318{
319 StackDump* dump = static_cast<StackDump*>(p);
320 // skip over frames up to including wxOnAssert()
321 dump->ProcessFrames(6);
322}
323
324#endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
325
326bool wxGUIAppTraits::ShowAssertDialog(const wxString& msg)
327{
328#if wxDEBUG_LEVEL
329 // we can't show the dialog from another thread
330 if ( wxIsMainThread() )
331 {
332 // under GTK2 we prefer to use a dialog widget written using directly
333 // in GTK+ as use a dialog written using wxWidgets would need the
334 // wxWidgets idle processing to work correctly which might not be the
335 // case when assert happens
336 GtkWidget *dialog = gtk_assert_dialog_new();
337 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog), msg.mb_str());
338
339#if wxUSE_STACKWALKER
340 // save the current stack ow...
341 StackDump dump(GTK_ASSERT_DIALOG(dialog));
342 dump.SaveStack(100); // showing more than 100 frames is not very useful
343
344 // ...but process it only if the user needs it
345 gtk_assert_dialog_set_backtrace_callback
346 (
347 GTK_ASSERT_DIALOG(dialog),
348 get_stackframe_callback,
349 &dump
350 );
351#endif // wxUSE_STACKWALKER
352
353 gint result = gtk_dialog_run(GTK_DIALOG (dialog));
354 bool returnCode = false;
355 switch (result)
356 {
357 case GTK_ASSERT_DIALOG_STOP:
358 wxTrap();
359 break;
360 case GTK_ASSERT_DIALOG_CONTINUE:
361 // nothing to do
362 break;
363 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING:
364 // no more asserts
365 returnCode = true;
366 break;
367
368 default:
369 wxFAIL_MSG( wxT("unexpected return code from GtkAssertDialog") );
370 }
371
372 gtk_widget_destroy(dialog);
373 return returnCode;
374 }
375#endif // wxDEBUG_LEVEL
376
377 return wxAppTraitsBase::ShowAssertDialog(msg);
378}
379
380#endif // __UNIX__
381
382#if defined(__UNIX__) || defined(__OS2__)
383
384wxString wxGUIAppTraits::GetDesktopEnvironment() const
385{
386 wxString de = wxSystemOptions::GetOption(wxT("gtk.desktop"));
387#if wxUSE_DETECT_SM
388 if ( de.empty() )
389 {
390 static const wxString s_SM = GetSM();
391
392 if (s_SM == wxT("GnomeSM"))
393 de = wxT("GNOME");
394 else if (s_SM == wxT("KDE"))
395 de = wxT("KDE");
396 }
397#endif // wxUSE_DETECT_SM
398
399 return de;
400}
401
402#endif // __UNIX__ || __OS2__
403
404#ifdef __UNIX__
405
406// see the hack below in wxCmdLineParser::GetUsageString().
407// TODO: replace this hack with a g_option_group_get_entries()
408// call as soon as such function exists;
409// see http://bugzilla.gnome.org/show_bug.cgi?id=431021 for the relative
410// feature request
411struct _GOptionGroup
412{
413 gchar *name;
414 gchar *description;
415 gchar *help_description;
416
417 GDestroyNotify destroy_notify;
418 gpointer user_data;
419
420 GTranslateFunc translate_func;
421 GDestroyNotify translate_notify;
422 gpointer translate_data;
423
424 GOptionEntry *entries;
425 gint n_entries;
426
427 GOptionParseFunc pre_parse_func;
428 GOptionParseFunc post_parse_func;
429 GOptionErrorFunc error_func;
430};
431
432static
433wxString wxGetNameFromGtkOptionEntry(const GOptionEntry *opt)
434{
435 wxString ret;
436
437 if (opt->short_name)
438 ret << wxT("-") << opt->short_name;
439 if (opt->long_name)
440 {
441 if (!ret.empty())
442 ret << wxT(", ");
443 ret << wxT("--") << opt->long_name;
444
445 if (opt->arg_description)
446 ret << wxT("=") << opt->arg_description;
447 }
448
449 return wxT(" ") + ret;
450}
451
452wxString
453wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString& names,
454 wxArrayString& desc) const
455{
456 wxString usage;
457
458 // check whether GLib version is greater than 2.6 but also lower than 2.33
459 // because, as we use the undocumented _GOptionGroup struct, we don't want
460 // to run this code with future versions which might change it (2.32 is the
461 // latest one at the time of this writing)
462 if (glib_check_version(2,33,0))
463 {
464 usage << _("The following standard GTK+ options are also supported:\n");
465
466 // passing true here means that the function can open the default
467 // display while parsing (not really used here anyhow)
468 GOptionGroup *gtkOpts = gtk_get_option_group(true);
469
470 // WARNING: here we access the internals of GOptionGroup:
471 GOptionEntry *entries = ((_GOptionGroup*)gtkOpts)->entries;
472 unsigned int n_entries = ((_GOptionGroup*)gtkOpts)->n_entries;
473 wxArrayString namesOptions, descOptions;
474
475 for ( size_t n = 0; n < n_entries; n++ )
476 {
477 if ( entries[n].flags & G_OPTION_FLAG_HIDDEN )
478 continue; // skip
479
480 names.push_back(wxGetNameFromGtkOptionEntry(&entries[n]));
481
482 const gchar * const entryDesc = entries[n].description;
483 desc.push_back(wxString(entryDesc));
484 }
485
486 g_option_group_free (gtkOpts);
487 }
488
489 return usage;
490}
491
492#endif // __UNIX__