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