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