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