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