]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/utilsgtk.cpp
Allow customizing wxDebugReportCompress output file.
[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"
5336ece4 22#include "wx/process.h"
9d0e0377 23#include "wx/sysopt.h"
518b5d2f
VZ
24#include "wx/unix/execute.h"
25
c2ca375c 26#include "wx/gtk/private/timer.h"
b46b1d59 27#include "wx/evtloop.h"
c2ca375c 28
4b6a582b 29#if wxDEBUG_LEVEL
92696e94 30 #include "wx/gtk/assertdlg_gtk.h"
db9febdf 31 #if wxUSE_STACKWALKER
db9febdf
RR
32 #include "wx/stackwalk.h"
33 #endif // wxUSE_STACKWALKER
4b6a582b 34#endif // wxDEBUG_LEVEL
db9febdf 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
518b5d2f
VZ
99// ----------------------------------------------------------------------------
100// display characterstics
101// ----------------------------------------------------------------------------
82052aff 102
d111a89a
VZ
103void *wxGetDisplay()
104{
27df579a 105 return GDK_DISPLAY();
d111a89a
VZ
106}
107
c0392997
RR
108void wxDisplaySize( int *width, int *height )
109{
ef26a5c4
JS
110 if (width) *width = gdk_screen_width();
111 if (height) *height = gdk_screen_height();
c0392997
RR
112}
113
904a68b6
RL
114void wxDisplaySizeMM( int *width, int *height )
115{
116 if (width) *width = gdk_screen_width_mm();
117 if (height) *height = gdk_screen_height_mm();
118}
119
6de97a3b
RR
120void wxGetMousePosition( int* x, int* y )
121{
d3b9f782 122 gdk_window_get_pointer( NULL, x, y, NULL );
e52f60e6 123}
6de97a3b 124
518b5d2f 125bool wxColourDisplay()
6de97a3b 126{
df91131c 127 return true;
6de97a3b
RR
128}
129
518b5d2f 130int wxDisplayDepth()
6de97a3b 131{
22a3bce4 132 return gdk_drawable_get_visual( wxGetRootWindow()->window )->depth;
6de97a3b
RR
133}
134
57591e0e
JS
135wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
136{
137 return wxGenericFindWindowAtPoint(pt);
138}
139
5f11fef5
VZ
140#if !wxUSE_UNICODE
141
afb6a92f
VS
142WXDLLIMPEXP_CORE wxCharBuffer
143wxConvertToGTK(const wxString& s, wxFontEncoding enc)
5f11fef5 144{
404ac4c6
VZ
145 wxWCharBuffer wbuf;
146 if ( enc == wxFONTENCODING_SYSTEM || enc == wxFONTENCODING_DEFAULT )
5f11fef5 147 {
de004174 148 wbuf = wxConvUI->cMB2WC(s.c_str());
12bc5f9a 149 }
404ac4c6 150 else // another encoding, use generic conversion class
12bc5f9a 151 {
de004174 152 wbuf = wxCSConv(enc).cMB2WC(s.c_str());
404ac4c6 153 }
12bc5f9a 154
404ac4c6
VZ
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
de004174 163 wbuf = wxCSConv(wxFONTENCODING_ISO8859_1).cMB2WC(s.c_str());
27dee9ae 164 }
5f11fef5 165
404ac4c6 166 return wxConvUTF8.cWC2MB(wbuf);
5f11fef5
VZ
167}
168
afb6a92f
VS
169WXDLLIMPEXP_CORE wxCharBuffer
170wxConvertFromGTK(const wxString& s, wxFontEncoding enc)
30083ad8
VZ
171{
172 // this conversion should never fail as GTK+ always uses UTF-8 internally
173 // so there are no complications here
de004174 174 const wxWCharBuffer wbuf(wxConvUTF8.cMB2WC(s.c_str()));
30083ad8
VZ
175 if ( enc == wxFONTENCODING_SYSTEM )
176 return wxConvUI->cWC2MB(wbuf);
177
178 return wxCSConv(enc).cWC2MB(wbuf);
179}
180
5f11fef5 181#endif // !wxUSE_UNICODE
57591e0e 182
573a2a4c
VS
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
5f4d1820
VZ
187const gchar *wx_pango_version_check (int major, int minor, int micro)
188{
b5791cc7
FM
189 // NOTE: you don't need to use this macro to check for Pango features
190 // added in pango-1.4 or earlier since GTK 2.4 (our minimum requirement
191 // for GTK lib) required pango 1.4...
192
5f4d1820
VZ
193#ifdef PANGO_VERSION_MAJOR
194 if (!gtk_check_version (2,11,0))
195 {
196 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
197 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
198 // using has the pango_version_check function:
199 return pango_version_check (major, minor, micro);
200 }
201
202 return "can't check";
203#else // !PANGO_VERSION_MAJOR
e4161a2a
VZ
204 wxUnusedVar(major);
205 wxUnusedVar(minor);
206 wxUnusedVar(micro);
207
5f4d1820
VZ
208 return "too old headers";
209#endif
210}
211
212
518b5d2f 213// ----------------------------------------------------------------------------
c801d85f 214// subprocess routines
518b5d2f 215// ----------------------------------------------------------------------------
cf447356 216
865bb325
VZ
217extern "C" {
218static
90350682 219void GTK_EndProcessDetector(gpointer data, gint source,
b827d647 220 GdkInputCondition WXUNUSED(condition))
cf447356 221{
b827d647 222 wxEndProcessData * const
5c33522f 223 proc_data = static_cast<wxEndProcessData *>(data);
dbd25330 224
b827d647
VZ
225 // child exited, end waiting
226 close(source);
447f908a 227
b827d647
VZ
228 // don't call us again!
229 gdk_input_remove(proc_data->tag);
ab857a4e 230
b827d647 231 wxHandleProcessTermination(proc_data);
3069ac4e 232}
865bb325 233}
cf447356 234
1d043598 235int wxGUIAppTraits::AddProcessCallback(wxEndProcessData *proc_data, int fd)
c801d85f 236{
518b5d2f
VZ
237 int tag = gdk_input_add(fd,
238 GDK_INPUT_READ,
239 GTK_EndProcessDetector,
240 (gpointer)proc_data);
c801d85f 241
518b5d2f 242 return tag;
3069ac4e 243}
8bb6b2c0
VZ
244
245
246
247// ----------------------------------------------------------------------------
248// wxPlatformInfo-related
249// ----------------------------------------------------------------------------
250
251wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
252{
253 if ( verMaj )
254 *verMaj = gtk_major_version;
255 if ( verMin )
256 *verMin = gtk_minor_version;
257
258 return wxPORT_GTK;
259}
88bbc332 260
3c029873
VZ
261#if wxUSE_TIMER
262
c2ca375c
VZ
263wxTimerImpl *wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
264{
265 return new wxGTKTimerImpl(timer);
266}
267
3c029873
VZ
268#endif // wxUSE_TIMER
269
88bbc332
RR
270#if wxUSE_DETECT_SM
271static wxString GetSM()
272{
391bf008 273 class Dpy
cb1bf052 274 {
391bf008
VZ
275 public:
276 Dpy() { m_dpy = XOpenDisplay(NULL); }
277 ~Dpy() { if ( m_dpy ) XCloseDisplay(m_dpy); }
88bbc332 278
391bf008
VZ
279 operator Display *() const { return m_dpy; }
280 private:
281 Display *m_dpy;
282 } dpy;
88bbc332 283
391bf008
VZ
284 if ( !dpy )
285 return wxEmptyString;
286
dee28fdd 287 char smerr[256];
391bf008
VZ
288 char *client_id;
289 SmcConn smc_conn = SmcOpenConnection(NULL, NULL,
290 999, 999,
291 0 /* mask */, NULL /* callbacks */,
292 NULL, &client_id,
dee28fdd 293 WXSIZEOF(smerr), smerr);
391bf008
VZ
294
295 if ( !smc_conn )
dee28fdd 296 {
366721d7 297 wxLogDebug("Failed to connect to session manager: %s", smerr);
391bf008 298 return wxEmptyString;
dee28fdd 299 }
391bf008
VZ
300
301 char *vendor = SmcVendor(smc_conn);
302 wxString ret = wxString::FromAscii( vendor );
303 free(vendor);
304
305 SmcCloseConnection(smc_conn, 0, NULL);
306 free(client_id);
307
308 return ret;
88bbc332 309}
391bf008 310#endif // wxUSE_DETECT_SM
88bbc332 311
db9febdf
RR
312
313//-----------------------------------------------------------------------------
314// wxGUIAppTraits
315//-----------------------------------------------------------------------------
316
2ddff00c 317wxEventLoopBase *wxGUIAppTraits::CreateEventLoop()
b46b1d59
VZ
318{
319 return new wxEventLoop();
320}
321
322
6c4f5ea5 323#if wxUSE_INTL
d774f916
VZ
324void wxGUIAppTraits::SetLocale()
325{
326 gtk_set_locale();
cb352236 327 wxUpdateLocaleIsUtf8();
d774f916 328}
6c4f5ea5 329#endif
d774f916 330
4b6a582b 331#if wxDEBUG_LEVEL && wxUSE_STACKWALKER
db9febdf
RR
332
333// private helper class
334class StackDump : public wxStackWalker
335{
336public:
337 StackDump(GtkAssertDialog *dlg) { m_dlg=dlg; }
338
339protected:
340 virtual void OnStackFrame(const wxStackFrame& frame)
341 {
342 wxString fncname = frame.GetName();
343 wxString fncargs = fncname;
344
345 size_t n = fncname.find(wxT('('));
346 if (n != wxString::npos)
347 {
348 // remove arguments from function name
349 fncname.erase(n);
350
351 // remove function name and brackets from arguments
352 fncargs = fncargs.substr(n+1, fncargs.length()-n-2);
353 }
354 else
355 fncargs = wxEmptyString;
356
357 // append this stack frame's info in the dialog
358 if (!frame.GetFileName().empty() || !fncname.empty())
359 gtk_assert_dialog_append_stack_frame(m_dlg,
360 fncname.mb_str(),
361 fncargs.mb_str(),
362 frame.GetFileName().mb_str(),
363 frame.GetLine());
364 }
365
366private:
367 GtkAssertDialog *m_dlg;
368};
369
370// the callback functions must be extern "C" to comply with GTK+ declarations
371extern "C"
372{
373 void get_stackframe_callback(StackDump *dump)
374 {
62710178
VZ
375 // skip over frames up to including wxOnAssert()
376 dump->ProcessFrames(3);
db9febdf
RR
377 }
378}
379
4b6a582b 380#endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
db9febdf
RR
381
382bool wxGUIAppTraits::ShowAssertDialog(const wxString& msg)
383{
4b6a582b
VZ
384#if wxDEBUG_LEVEL
385 // under GTK2 we prefer to use a dialog widget written using directly in
386 // GTK+ as use a dialog written using wxWidgets would need the wxWidgets
387 // idle processing to work correctly which might not be the case when
388 // assert happens
db9febdf
RR
389 GtkWidget *dialog = gtk_assert_dialog_new();
390 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog), msg.mb_str());
391
392#if wxUSE_STACKWALKER
393 // don't show more than maxLines or we could get a dialog too tall to be
394 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
395 // characters it is still only 300 pixels...
396 static const int maxLines = 20;
397
398 // save current stack frame...
399 StackDump dump(GTK_ASSERT_DIALOG(dialog));
400 dump.SaveStack(maxLines);
401
402 // ...but process it only if the user needs it
403 gtk_assert_dialog_set_backtrace_callback(GTK_ASSERT_DIALOG(dialog),
404 (GtkAssertDialogStackFrameCallback)get_stackframe_callback,
405 &dump);
406#endif // wxUSE_STACKWALKER
407
408 gint result = gtk_dialog_run(GTK_DIALOG (dialog));
409 bool returnCode = false;
410 switch (result)
411 {
412 case GTK_ASSERT_DIALOG_STOP:
413 wxTrap();
414 break;
415 case GTK_ASSERT_DIALOG_CONTINUE:
416 // nothing to do
417 break;
418 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING:
419 // no more asserts
420 returnCode = true;
421 break;
422
423 default:
9a83f860 424 wxFAIL_MSG( wxT("unexpected return code from GtkAssertDialog") );
db9febdf
RR
425 }
426
427 gtk_widget_destroy(dialog);
428 return returnCode;
4b6a582b
VZ
429#else // !wxDEBUG_LEVEL
430 // this function is never called in this case
431 wxUnusedVar(msg);
432 return false;
433#endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
db9febdf
RR
434}
435
88bbc332
RR
436wxString wxGUIAppTraits::GetDesktopEnvironment() const
437{
9a83f860 438 wxString de = wxSystemOptions::GetOption(wxT("gtk.desktop"));
3c029873 439#if wxUSE_DETECT_SM
9d0e0377
VZ
440 if ( de.empty() )
441 {
9d0e0377 442 static const wxString s_SM = GetSM();
391bf008 443
9d0e0377
VZ
444 if (s_SM == wxT("GnomeSM"))
445 de = wxT("GNOME");
446 else if (s_SM == wxT("KDE"))
447 de = wxT("KDE");
448 }
391bf008 449#endif // wxUSE_DETECT_SM
88bbc332 450
9d0e0377 451 return de;
88bbc332
RR
452}
453
d3a0a0ee 454#ifdef __WXGTK26__
88bbc332 455
d3a0a0ee
VZ
456// see the hack below in wxCmdLineParser::GetUsageString().
457// TODO: replace this hack with a g_option_group_get_entries()
baa4a967
FM
458// call as soon as such function exists;
459// see http://bugzilla.gnome.org/show_bug.cgi?id=431021 for the relative
460// feature request
d3a0a0ee
VZ
461struct _GOptionGroup
462{
463 gchar *name;
464 gchar *description;
465 gchar *help_description;
466
467 GDestroyNotify destroy_notify;
468 gpointer user_data;
469
470 GTranslateFunc translate_func;
471 GDestroyNotify translate_notify;
472 gpointer translate_data;
473
474 GOptionEntry *entries;
475 gint n_entries;
476
477 GOptionParseFunc pre_parse_func;
478 GOptionParseFunc post_parse_func;
479 GOptionErrorFunc error_func;
480};
481
482wxString wxGetNameFromGtkOptionEntry(const GOptionEntry *opt)
483{
484 wxString ret;
485
486 if (opt->short_name)
9a83f860 487 ret << wxT("-") << opt->short_name;
d3a0a0ee
VZ
488 if (opt->long_name)
489 {
490 if (!ret.empty())
9a83f860
VZ
491 ret << wxT(", ");
492 ret << wxT("--") << opt->long_name;
d3a0a0ee
VZ
493
494 if (opt->arg_description)
9a83f860 495 ret << wxT("=") << opt->arg_description;
d3a0a0ee
VZ
496 }
497
9a83f860 498 return wxT(" ") + ret;
d3a0a0ee
VZ
499}
500
501#endif // __WXGTK26__
502
503wxString
504wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString& names,
505 wxArrayString& desc) const
506{
507 wxString usage;
508
509#ifdef __WXGTK26__
baa4a967 510 if (!gtk_check_version(2,6,0))
d3a0a0ee 511 {
baa4a967 512 // since GTK>=2.6, we can use the glib_check_version() symbol...
d3a0a0ee 513
baa4a967
FM
514 // check whether GLib version is greater than 2.6 but also lower than 2.19
515 // because, as we use the undocumented _GOptionGroup struct, we don't want
516 // to run this code with future versions which might change it (2.19 is the
517 // latest one at the time of this writing)
3fb2bf54 518 if (!glib_check_version(2,6,0) && glib_check_version(2,20,0))
baa4a967
FM
519 {
520 usage << _("The following standard GTK+ options are also supported:\n");
d3a0a0ee 521
baa4a967
FM
522 // passing true here means that the function can open the default
523 // display while parsing (not really used here anyhow)
524 GOptionGroup *gtkOpts = gtk_get_option_group(true);
d3a0a0ee 525
baa4a967
FM
526 // WARNING: here we access the internals of GOptionGroup:
527 GOptionEntry *entries = ((_GOptionGroup*)gtkOpts)->entries;
528 unsigned int n_entries = ((_GOptionGroup*)gtkOpts)->n_entries;
529 wxArrayString namesOptions, descOptions;
d3a0a0ee 530
baa4a967
FM
531 for ( size_t n = 0; n < n_entries; n++ )
532 {
533 if ( entries[n].flags & G_OPTION_FLAG_HIDDEN )
534 continue; // skip
d3a0a0ee 535
baa4a967 536 names.push_back(wxGetNameFromGtkOptionEntry(&entries[n]));
d3a0a0ee 537
baa4a967
FM
538 const gchar * const entryDesc = entries[n].description;
539 desc.push_back(wxString(entryDesc));
540 }
541
542 g_option_group_free (gtkOpts);
543 }
d3a0a0ee
VZ
544 }
545#else
546 wxUnusedVar(names);
547 wxUnusedVar(desc);
548#endif // __WXGTK26__
549
550 return usage;
551}
88bbc332 552