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