]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/utilsgtk.cpp
Allow user code to override key events in generic wxListCtrl.
[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 #if wxDEBUG_LEVEL
30 #include "wx/gtk/assertdlg_gtk.h"
31 #if wxUSE_STACKWALKER
32 #include "wx/stackwalk.h"
33 #endif // wxUSE_STACKWALKER
34 #endif // wxDEBUG_LEVEL
35
36 #include <stdarg.h>
37 #include <string.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <sys/wait.h> // for WNOHANG
41 #include <unistd.h>
42
43 #include "glib.h"
44 #include "gdk/gdk.h"
45 #include "gtk/gtk.h"
46 #include "gdk/gdkx.h"
47
48 #if wxUSE_DETECT_SM
49 #include "X11/Xlib.h"
50 #include "X11/SM/SMlib.h"
51 #endif
52
53 //-----------------------------------------------------------------------------
54 // data
55 //-----------------------------------------------------------------------------
56
57 extern GtkWidget *wxGetRootWindow();
58
59 //----------------------------------------------------------------------------
60 // misc.
61 //----------------------------------------------------------------------------
62 #ifndef __EMX__
63 // on OS/2, we use the wxBell from wxBase library
64
65 void wxBell()
66 {
67 gdk_beep();
68 }
69 #endif
70
71 // ----------------------------------------------------------------------------
72 // display characterstics
73 // ----------------------------------------------------------------------------
74
75 void *wxGetDisplay()
76 {
77 return GDK_DISPLAY();
78 }
79
80 void wxDisplaySize( int *width, int *height )
81 {
82 if (width) *width = gdk_screen_width();
83 if (height) *height = gdk_screen_height();
84 }
85
86 void wxDisplaySizeMM( int *width, int *height )
87 {
88 if (width) *width = gdk_screen_width_mm();
89 if (height) *height = gdk_screen_height_mm();
90 }
91
92 void wxGetMousePosition( int* x, int* y )
93 {
94 gdk_window_get_pointer( NULL, x, y, NULL );
95 }
96
97 bool wxColourDisplay()
98 {
99 return true;
100 }
101
102 int wxDisplayDepth()
103 {
104 return gdk_drawable_get_visual( wxGetRootWindow()->window )->depth;
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.c_str());
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 PANGO_VERSION_MAJOR
166 if (!gtk_check_version (2,11,0))
167 {
168 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
169 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
170 // using has the pango_version_check function:
171 return pango_version_check (major, minor, micro);
172 }
173
174 return "can't check";
175 #else // !PANGO_VERSION_MAJOR
176 wxUnusedVar(major);
177 wxUnusedVar(minor);
178 wxUnusedVar(micro);
179
180 return "too old headers";
181 #endif
182 }
183
184
185 // ----------------------------------------------------------------------------
186 // subprocess routines
187 // ----------------------------------------------------------------------------
188
189 extern "C" {
190 static
191 void GTK_EndProcessDetector(gpointer data, gint source,
192 GdkInputCondition WXUNUSED(condition))
193 {
194 wxEndProcessData * const
195 proc_data = static_cast<wxEndProcessData *>(data);
196
197 // child exited, end waiting
198 close(source);
199
200 // don't call us again!
201 gdk_input_remove(proc_data->tag);
202
203 wxHandleProcessTermination(proc_data);
204 }
205 }
206
207 int wxGUIAppTraits::AddProcessCallback(wxEndProcessData *proc_data, int fd)
208 {
209 int tag = gdk_input_add(fd,
210 GDK_INPUT_READ,
211 GTK_EndProcessDetector,
212 (gpointer)proc_data);
213
214 return tag;
215 }
216
217
218
219 // ----------------------------------------------------------------------------
220 // wxPlatformInfo-related
221 // ----------------------------------------------------------------------------
222
223 wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
224 {
225 if ( verMaj )
226 *verMaj = gtk_major_version;
227 if ( verMin )
228 *verMin = gtk_minor_version;
229
230 return wxPORT_GTK;
231 }
232
233 #if wxUSE_TIMER
234
235 wxTimerImpl *wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
236 {
237 return new wxGTKTimerImpl(timer);
238 }
239
240 #endif // wxUSE_TIMER
241
242 #if wxUSE_DETECT_SM
243 static wxString GetSM()
244 {
245 class Dpy
246 {
247 public:
248 Dpy() { m_dpy = XOpenDisplay(NULL); }
249 ~Dpy() { if ( m_dpy ) XCloseDisplay(m_dpy); }
250
251 operator Display *() const { return m_dpy; }
252 private:
253 Display *m_dpy;
254 } dpy;
255
256 if ( !dpy )
257 return wxEmptyString;
258
259 char smerr[256];
260 char *client_id;
261 SmcConn smc_conn = SmcOpenConnection(NULL, NULL,
262 999, 999,
263 0 /* mask */, NULL /* callbacks */,
264 NULL, &client_id,
265 WXSIZEOF(smerr), smerr);
266
267 if ( !smc_conn )
268 {
269 wxLogDebug("Failed to connect to session manager: %s", smerr);
270 return wxEmptyString;
271 }
272
273 char *vendor = SmcVendor(smc_conn);
274 wxString ret = wxString::FromAscii( vendor );
275 free(vendor);
276
277 SmcCloseConnection(smc_conn, 0, NULL);
278 free(client_id);
279
280 return ret;
281 }
282 #endif // wxUSE_DETECT_SM
283
284
285 //-----------------------------------------------------------------------------
286 // wxGUIAppTraits
287 //-----------------------------------------------------------------------------
288
289 wxEventLoopBase *wxGUIAppTraits::CreateEventLoop()
290 {
291 return new wxEventLoop();
292 }
293
294
295 #if wxUSE_INTL
296 void wxGUIAppTraits::SetLocale()
297 {
298 gtk_set_locale();
299 wxUpdateLocaleIsUtf8();
300 }
301 #endif
302
303 #if wxDEBUG_LEVEL && wxUSE_STACKWALKER
304
305 // private helper class
306 class StackDump : public wxStackWalker
307 {
308 public:
309 StackDump(GtkAssertDialog *dlg) { m_dlg=dlg; }
310
311 protected:
312 virtual void OnStackFrame(const wxStackFrame& frame)
313 {
314 wxString fncname = frame.GetName();
315 wxString fncargs = fncname;
316
317 size_t n = fncname.find(wxT('('));
318 if (n != wxString::npos)
319 {
320 // remove arguments from function name
321 fncname.erase(n);
322
323 // remove function name and brackets from arguments
324 fncargs = fncargs.substr(n+1, fncargs.length()-n-2);
325 }
326 else
327 fncargs = wxEmptyString;
328
329 // append this stack frame's info in the dialog
330 if (!frame.GetFileName().empty() || !fncname.empty())
331 gtk_assert_dialog_append_stack_frame(m_dlg,
332 fncname.mb_str(),
333 fncargs.mb_str(),
334 frame.GetFileName().mb_str(),
335 frame.GetLine());
336 }
337
338 private:
339 GtkAssertDialog *m_dlg;
340 };
341
342 // the callback functions must be extern "C" to comply with GTK+ declarations
343 extern "C"
344 {
345 void get_stackframe_callback(StackDump *dump)
346 {
347 // skip over frames up to including wxOnAssert()
348 dump->ProcessFrames(3);
349 }
350 }
351
352 #endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
353
354 bool wxGUIAppTraits::ShowAssertDialog(const wxString& msg)
355 {
356 #if wxDEBUG_LEVEL
357 // we can't show the dialog from another thread
358 if ( wxIsMainThread() )
359 {
360 // under GTK2 we prefer to use a dialog widget written using directly
361 // in GTK+ as use a dialog written using wxWidgets would need the
362 // wxWidgets idle processing to work correctly which might not be the
363 // case when assert happens
364 GtkWidget *dialog = gtk_assert_dialog_new();
365 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog), msg.mb_str());
366
367 #if wxUSE_STACKWALKER
368 // save the current stack ow...
369 StackDump dump(GTK_ASSERT_DIALOG(dialog));
370 dump.SaveStack(100); // showing more than 100 frames is not very useful
371
372 // ...but process it only if the user needs it
373 gtk_assert_dialog_set_backtrace_callback
374 (
375 GTK_ASSERT_DIALOG(dialog),
376 (GtkAssertDialogStackFrameCallback)get_stackframe_callback,
377 &dump
378 );
379 #endif // wxUSE_STACKWALKER
380
381 gint result = gtk_dialog_run(GTK_DIALOG (dialog));
382 bool returnCode = false;
383 switch (result)
384 {
385 case GTK_ASSERT_DIALOG_STOP:
386 wxTrap();
387 break;
388 case GTK_ASSERT_DIALOG_CONTINUE:
389 // nothing to do
390 break;
391 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING:
392 // no more asserts
393 returnCode = true;
394 break;
395
396 default:
397 wxFAIL_MSG( wxT("unexpected return code from GtkAssertDialog") );
398 }
399
400 gtk_widget_destroy(dialog);
401 return returnCode;
402 }
403 #endif // wxDEBUG_LEVEL
404
405 return wxAppTraitsBase::ShowAssertDialog(msg);
406 }
407
408 wxString wxGUIAppTraits::GetDesktopEnvironment() const
409 {
410 wxString de = wxSystemOptions::GetOption(wxT("gtk.desktop"));
411 #if wxUSE_DETECT_SM
412 if ( de.empty() )
413 {
414 static const wxString s_SM = GetSM();
415
416 if (s_SM == wxT("GnomeSM"))
417 de = wxT("GNOME");
418 else if (s_SM == wxT("KDE"))
419 de = wxT("KDE");
420 }
421 #endif // wxUSE_DETECT_SM
422
423 return de;
424 }
425
426 #ifdef __WXGTK26__
427
428 // see the hack below in wxCmdLineParser::GetUsageString().
429 // TODO: replace this hack with a g_option_group_get_entries()
430 // call as soon as such function exists;
431 // see http://bugzilla.gnome.org/show_bug.cgi?id=431021 for the relative
432 // feature request
433 struct _GOptionGroup
434 {
435 gchar *name;
436 gchar *description;
437 gchar *help_description;
438
439 GDestroyNotify destroy_notify;
440 gpointer user_data;
441
442 GTranslateFunc translate_func;
443 GDestroyNotify translate_notify;
444 gpointer translate_data;
445
446 GOptionEntry *entries;
447 gint n_entries;
448
449 GOptionParseFunc pre_parse_func;
450 GOptionParseFunc post_parse_func;
451 GOptionErrorFunc error_func;
452 };
453
454 wxString wxGetNameFromGtkOptionEntry(const GOptionEntry *opt)
455 {
456 wxString ret;
457
458 if (opt->short_name)
459 ret << wxT("-") << opt->short_name;
460 if (opt->long_name)
461 {
462 if (!ret.empty())
463 ret << wxT(", ");
464 ret << wxT("--") << opt->long_name;
465
466 if (opt->arg_description)
467 ret << wxT("=") << opt->arg_description;
468 }
469
470 return wxT(" ") + ret;
471 }
472
473 #endif // __WXGTK26__
474
475 wxString
476 wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString& names,
477 wxArrayString& desc) const
478 {
479 wxString usage;
480
481 #ifdef __WXGTK26__
482 if (!gtk_check_version(2,6,0))
483 {
484 // since GTK>=2.6, we can use the glib_check_version() symbol...
485
486 // check whether GLib version is greater than 2.6 but also lower than 2.19
487 // because, as we use the undocumented _GOptionGroup struct, we don't want
488 // to run this code with future versions which might change it (2.19 is the
489 // latest one at the time of this writing)
490 if (!glib_check_version(2,6,0) && glib_check_version(2,20,0))
491 {
492 usage << _("The following standard GTK+ options are also supported:\n");
493
494 // passing true here means that the function can open the default
495 // display while parsing (not really used here anyhow)
496 GOptionGroup *gtkOpts = gtk_get_option_group(true);
497
498 // WARNING: here we access the internals of GOptionGroup:
499 GOptionEntry *entries = ((_GOptionGroup*)gtkOpts)->entries;
500 unsigned int n_entries = ((_GOptionGroup*)gtkOpts)->n_entries;
501 wxArrayString namesOptions, descOptions;
502
503 for ( size_t n = 0; n < n_entries; n++ )
504 {
505 if ( entries[n].flags & G_OPTION_FLAG_HIDDEN )
506 continue; // skip
507
508 names.push_back(wxGetNameFromGtkOptionEntry(&entries[n]));
509
510 const gchar * const entryDesc = entries[n].description;
511 desc.push_back(wxString(entryDesc));
512 }
513
514 g_option_group_free (gtkOpts);
515 }
516 }
517 #else
518 wxUnusedVar(names);
519 wxUnusedVar(desc);
520 #endif // __WXGTK26__
521
522 return usage;
523 }
524