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