]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/utilsgtk.cpp
added wxMSW-specific wxEntryStart() overload taking WinMain-like parameters
[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
190wxCharBuffer wxConvertToGTK(const wxString& s, wxFontEncoding enc)
191{
404ac4c6
VZ
192 wxWCharBuffer wbuf;
193 if ( enc == wxFONTENCODING_SYSTEM || enc == wxFONTENCODING_DEFAULT )
5f11fef5 194 {
404ac4c6 195 wbuf = wxConvUI->cMB2WC(s);
12bc5f9a 196 }
404ac4c6 197 else // another encoding, use generic conversion class
12bc5f9a 198 {
404ac4c6
VZ
199 wbuf = wxCSConv(enc).cMB2WC(s);
200 }
12bc5f9a 201
404ac4c6
VZ
202 if ( !wbuf && !s.empty() )
203 {
204 // conversion failed, but we still want to show something to the user
205 // even if it's going to be wrong it is better than nothing
206 //
207 // we choose ISO8859-1 here arbitrarily, it's just the most common
208 // encoding probably and, also importantly here, conversion from it
209 // never fails as it's done internally by wxCSConv
210 wbuf = wxCSConv(wxFONTENCODING_ISO8859_1).cMB2WC(s);
27dee9ae 211 }
5f11fef5 212
404ac4c6 213 return wxConvUTF8.cWC2MB(wbuf);
5f11fef5
VZ
214}
215
30083ad8
VZ
216wxCharBuffer wxConvertFromGTK(const wxString& s, wxFontEncoding enc)
217{
218 // this conversion should never fail as GTK+ always uses UTF-8 internally
219 // so there are no complications here
220 const wxWCharBuffer wbuf(wxConvUTF8.cMB2WC(s));
221 if ( enc == wxFONTENCODING_SYSTEM )
222 return wxConvUI->cWC2MB(wbuf);
223
224 return wxCSConv(enc).cWC2MB(wbuf);
225}
226
5f11fef5 227#endif // !wxUSE_UNICODE
57591e0e 228
5f4d1820
VZ
229// Returns false if version is certainly greater or equal than major.minor.micro
230// Returns true if version is lower than major.minor.micro OR it cannot be
231// determined and one should not rely on the availability of pango version
232// major.minor.micro, nor the non-availability
233const gchar *wx_pango_version_check (int major, int minor, int micro)
234{
235#ifdef PANGO_VERSION_MAJOR
236 if (!gtk_check_version (2,11,0))
237 {
238 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
239 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
240 // using has the pango_version_check function:
241 return pango_version_check (major, minor, micro);
242 }
243
244 return "can't check";
245#else // !PANGO_VERSION_MAJOR
246 return "too old headers";
247#endif
248}
249
250
518b5d2f 251// ----------------------------------------------------------------------------
c801d85f 252// subprocess routines
518b5d2f 253// ----------------------------------------------------------------------------
cf447356 254
865bb325
VZ
255extern "C" {
256static
90350682
VZ
257void GTK_EndProcessDetector(gpointer data, gint source,
258 GdkInputCondition WXUNUSED(condition) )
cf447356 259{
ab857a4e 260 wxEndProcessData *proc_data = (wxEndProcessData *)data;
dbd25330
VZ
261
262 // has the process really terminated? unfortunately GDK (or GLib) seem to
263 // generate G_IO_HUP notification even when it simply tries to read from a
264 // closed fd and hasn't terminated at all
265 int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
447f908a
VZ
266 int status = 0;
267 int rc = waitpid(pid, &status, WNOHANG);
268
269 if ( rc == 0 )
dbd25330
VZ
270 {
271 // no, it didn't exit yet, continue waiting
272 return;
273 }
274
447f908a
VZ
275 // set exit code to -1 if something bad happened
276 proc_data->exitcode = rc != -1 && WIFEXITED(status) ? WEXITSTATUS(status)
277 : -1;
278
dbd25330 279 // child exited, end waiting
ab857a4e 280 close(source);
dbd25330
VZ
281
282 // don't call us again!
ab857a4e
KB
283 gdk_input_remove(proc_data->tag);
284
ab857a4e 285 wxHandleProcessTermination(proc_data);
3069ac4e 286}
865bb325 287}
cf447356 288
518b5d2f 289int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
c801d85f 290{
518b5d2f
VZ
291 int tag = gdk_input_add(fd,
292 GDK_INPUT_READ,
293 GTK_EndProcessDetector,
294 (gpointer)proc_data);
c801d85f 295
518b5d2f 296 return tag;
3069ac4e 297}
8bb6b2c0
VZ
298
299
300
301// ----------------------------------------------------------------------------
302// wxPlatformInfo-related
303// ----------------------------------------------------------------------------
304
305wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
306{
307 if ( verMaj )
308 *verMaj = gtk_major_version;
309 if ( verMin )
310 *verMin = gtk_minor_version;
311
312 return wxPORT_GTK;
313}
88bbc332 314
c2ca375c
VZ
315wxTimerImpl *wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
316{
317 return new wxGTKTimerImpl(timer);
318}
319
88bbc332
RR
320#if wxUSE_DETECT_SM
321static wxString GetSM()
322{
391bf008 323 class Dpy
cb1bf052 324 {
391bf008
VZ
325 public:
326 Dpy() { m_dpy = XOpenDisplay(NULL); }
327 ~Dpy() { if ( m_dpy ) XCloseDisplay(m_dpy); }
88bbc332 328
391bf008
VZ
329 operator Display *() const { return m_dpy; }
330 private:
331 Display *m_dpy;
332 } dpy;
88bbc332 333
391bf008
VZ
334 if ( !dpy )
335 return wxEmptyString;
336
dee28fdd 337 char smerr[256];
391bf008
VZ
338 char *client_id;
339 SmcConn smc_conn = SmcOpenConnection(NULL, NULL,
340 999, 999,
341 0 /* mask */, NULL /* callbacks */,
342 NULL, &client_id,
dee28fdd 343 WXSIZEOF(smerr), smerr);
391bf008
VZ
344
345 if ( !smc_conn )
dee28fdd
VZ
346 {
347 wxLogWarning(_("Failed to connect to session manager: %s"), smerr);
391bf008 348 return wxEmptyString;
dee28fdd 349 }
391bf008
VZ
350
351 char *vendor = SmcVendor(smc_conn);
352 wxString ret = wxString::FromAscii( vendor );
353 free(vendor);
354
355 SmcCloseConnection(smc_conn, 0, NULL);
356 free(client_id);
357
358 return ret;
88bbc332 359}
391bf008 360#endif // wxUSE_DETECT_SM
88bbc332 361
db9febdf
RR
362
363//-----------------------------------------------------------------------------
364// wxGUIAppTraits
365//-----------------------------------------------------------------------------
366
2ddff00c 367wxEventLoopBase *wxGUIAppTraits::CreateEventLoop()
b46b1d59
VZ
368{
369 return new wxEventLoop();
370}
371
372
6c4f5ea5 373#if wxUSE_INTL
d774f916
VZ
374void wxGUIAppTraits::SetLocale()
375{
376 gtk_set_locale();
cb352236 377 wxUpdateLocaleIsUtf8();
d774f916 378}
6c4f5ea5 379#endif
d774f916 380
db9febdf
RR
381#ifdef __WXDEBUG__
382
383#if wxUSE_STACKWALKER
384
385// private helper class
386class StackDump : public wxStackWalker
387{
388public:
389 StackDump(GtkAssertDialog *dlg) { m_dlg=dlg; }
390
391protected:
392 virtual void OnStackFrame(const wxStackFrame& frame)
393 {
394 wxString fncname = frame.GetName();
395 wxString fncargs = fncname;
396
397 size_t n = fncname.find(wxT('('));
398 if (n != wxString::npos)
399 {
400 // remove arguments from function name
401 fncname.erase(n);
402
403 // remove function name and brackets from arguments
404 fncargs = fncargs.substr(n+1, fncargs.length()-n-2);
405 }
406 else
407 fncargs = wxEmptyString;
408
409 // append this stack frame's info in the dialog
410 if (!frame.GetFileName().empty() || !fncname.empty())
411 gtk_assert_dialog_append_stack_frame(m_dlg,
412 fncname.mb_str(),
413 fncargs.mb_str(),
414 frame.GetFileName().mb_str(),
415 frame.GetLine());
416 }
417
418private:
419 GtkAssertDialog *m_dlg;
420};
421
422// the callback functions must be extern "C" to comply with GTK+ declarations
423extern "C"
424{
425 void get_stackframe_callback(StackDump *dump)
426 {
62710178
VZ
427 // skip over frames up to including wxOnAssert()
428 dump->ProcessFrames(3);
db9febdf
RR
429 }
430}
431
432#endif // wxUSE_STACKWALKER
433
434bool wxGUIAppTraits::ShowAssertDialog(const wxString& msg)
435{
436 // under GTK2 we prefer to use a dialog widget written using directly GTK+;
437 // in fact we cannot use a dialog written using wxWidgets: it would need
438 // the wxWidgets idle processing to work correctly!
439 GtkWidget *dialog = gtk_assert_dialog_new();
440 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog), msg.mb_str());
441
442#if wxUSE_STACKWALKER
443 // don't show more than maxLines or we could get a dialog too tall to be
444 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
445 // characters it is still only 300 pixels...
446 static const int maxLines = 20;
447
448 // save current stack frame...
449 StackDump dump(GTK_ASSERT_DIALOG(dialog));
450 dump.SaveStack(maxLines);
451
452 // ...but process it only if the user needs it
453 gtk_assert_dialog_set_backtrace_callback(GTK_ASSERT_DIALOG(dialog),
454 (GtkAssertDialogStackFrameCallback)get_stackframe_callback,
455 &dump);
456#endif // wxUSE_STACKWALKER
457
458 gint result = gtk_dialog_run(GTK_DIALOG (dialog));
459 bool returnCode = false;
460 switch (result)
461 {
462 case GTK_ASSERT_DIALOG_STOP:
463 wxTrap();
464 break;
465 case GTK_ASSERT_DIALOG_CONTINUE:
466 // nothing to do
467 break;
468 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING:
469 // no more asserts
470 returnCode = true;
471 break;
472
473 default:
474 wxFAIL_MSG( _T("unexpected return code from GtkAssertDialog") );
475 }
476
477 gtk_widget_destroy(dialog);
478 return returnCode;
479}
480
481#endif // __WXDEBUG__
482
88bbc332
RR
483wxString wxGUIAppTraits::GetDesktopEnvironment() const
484{
9d0e0377
VZ
485 wxString de = wxSystemOptions::GetOption(_T("gtk.desktop"));
486 if ( de.empty() )
487 {
88bbc332 488#if wxUSE_DETECT_SM
9d0e0377 489 static const wxString s_SM = GetSM();
391bf008 490
9d0e0377
VZ
491 if (s_SM == wxT("GnomeSM"))
492 de = wxT("GNOME");
493 else if (s_SM == wxT("KDE"))
494 de = wxT("KDE");
495 }
391bf008 496#endif // wxUSE_DETECT_SM
88bbc332 497
9d0e0377 498 return de;
88bbc332
RR
499}
500
d3a0a0ee 501#ifdef __WXGTK26__
88bbc332 502
d3a0a0ee
VZ
503// see the hack below in wxCmdLineParser::GetUsageString().
504// TODO: replace this hack with a g_option_group_get_entries()
505// call as soon as such function exists
506struct _GOptionGroup
507{
508 gchar *name;
509 gchar *description;
510 gchar *help_description;
511
512 GDestroyNotify destroy_notify;
513 gpointer user_data;
514
515 GTranslateFunc translate_func;
516 GDestroyNotify translate_notify;
517 gpointer translate_data;
518
519 GOptionEntry *entries;
520 gint n_entries;
521
522 GOptionParseFunc pre_parse_func;
523 GOptionParseFunc post_parse_func;
524 GOptionErrorFunc error_func;
525};
526
527wxString wxGetNameFromGtkOptionEntry(const GOptionEntry *opt)
528{
529 wxString ret;
530
531 if (opt->short_name)
532 ret << _T("-") << opt->short_name;
533 if (opt->long_name)
534 {
535 if (!ret.empty())
536 ret << _T(", ");
537 ret << _T("--") << opt->long_name;
538
539 if (opt->arg_description)
540 ret << _T("=") << opt->arg_description;
541 }
542
543 return _T(" ") + ret;
544}
545
546#endif // __WXGTK26__
547
548wxString
549wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString& names,
550 wxArrayString& desc) const
551{
552 wxString usage;
553
554#ifdef __WXGTK26__
555 // check whether GTK version is greater than 2.6 but also lower than 2.12
556 // because, as we use the undocumented _GOptionGroup struct, we don't want
557 // to run this code with future versions which might change it (2.11 is the
558 // latest one at the time of this writing)
559 if (!gtk_check_version(2,6,0) &&
560 gtk_check_version(2,12,0))
561 {
562 usage << _("The following standard GTK+ options are also supported:\n");
563
564 // passing true here means that the function can open the default
565 // display while parsing (not really used here anyhow)
566 GOptionGroup *gtkOpts = gtk_get_option_group(true);
567
568 // WARNING: here we access the internals of GOptionGroup:
569 GOptionEntry *entries = ((_GOptionGroup*)gtkOpts)->entries;
570 unsigned int n_entries = ((_GOptionGroup*)gtkOpts)->n_entries;
571 wxArrayString namesOptions, descOptions;
572
573 for ( size_t n = 0; n < n_entries; n++ )
574 {
575 if ( entries[n].flags & G_OPTION_FLAG_HIDDEN )
576 continue; // skip
577
578 names.push_back(wxGetNameFromGtkOptionEntry(&entries[n]));
579
580 const gchar * const entryDesc = entries[n].description;
581 desc.push_back(entryDesc ? wxString(entryDesc) : _T(""));
582 }
583
584 g_option_group_free (gtkOpts);
585 }
586#else
587 wxUnusedVar(names);
588 wxUnusedVar(desc);
589#endif // __WXGTK26__
590
591 return usage;
592}
88bbc332 593