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