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