1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/utilsgtk.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
16 #include "wx/string.h"
21 #include "wx/apptrait.h"
23 #include "wx/process.h"
24 #include "wx/sysopt.h"
26 #include "wx/unix/execute.h"
28 #include "wx/gtk/private/timer.h"
31 #include "wx/gtk/assertdlg_gtk.h"
33 #include "wx/stackwalk.h"
34 #endif // wxUSE_STACKWALKER
40 #include <sys/types.h>
41 #include <sys/wait.h> // for WNOHANG
49 #ifdef HAVE_X11_XKBLIB_H
50 /* under HP-UX and Solaris 2.6, at least, XKBlib.h defines structures with
51 * field named "explicit" - which is, of course, an error for a C++
52 * compiler. To be on the safe side, just redefine it everywhere. */
53 #define explicit __wx_explicit
55 #include "X11/XKBlib.h"
58 #endif // HAVE_X11_XKBLIB_H
63 #include "X11/SM/SMlib.h"
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
70 extern GtkWidget
*wxGetRootWindow();
72 //----------------------------------------------------------------------------
74 //----------------------------------------------------------------------------
76 // on OS/2, we use the wxBell from wxBase library
84 /* Don't synthesize KeyUp events holding down a key and producing
85 KeyDown events with autorepeat. */
86 #ifdef HAVE_X11_XKBLIB_H
87 bool wxSetDetectableAutoRepeat( bool flag
)
90 XkbSetDetectableAutoRepeat( GDK_DISPLAY(), flag
, &result
);
91 return result
; /* true if keyboard hardware supports this mode */
94 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag
) )
100 // Escapes string so that it is valid Pango markup XML string:
101 wxString
wxEscapeStringForPangoMarkup(const wxString
& str
)
103 size_t len
= str
.length();
106 for (size_t i
= 0; i
< len
; i
++)
135 // ----------------------------------------------------------------------------
136 // display characterstics
137 // ----------------------------------------------------------------------------
141 return GDK_DISPLAY();
144 void wxDisplaySize( int *width
, int *height
)
146 if (width
) *width
= gdk_screen_width();
147 if (height
) *height
= gdk_screen_height();
150 void wxDisplaySizeMM( int *width
, int *height
)
152 if (width
) *width
= gdk_screen_width_mm();
153 if (height
) *height
= gdk_screen_height_mm();
156 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
158 // This is supposed to return desktop dimensions minus any window
159 // manager panels, menus, taskbars, etc. If there is a way to do that
160 // for this platform please fix this function, otherwise it defaults
161 // to the entire desktop.
164 wxDisplaySize(width
, height
);
167 void wxGetMousePosition( int* x
, int* y
)
169 gdk_window_get_pointer( (GdkWindow
*) NULL
, x
, y
, (GdkModifierType
*) NULL
);
172 bool wxColourDisplay()
179 return gdk_drawable_get_visual( wxGetRootWindow()->window
)->depth
;
182 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
184 return wxGenericFindWindowAtPoint(pt
);
189 wxCharBuffer
wxConvertToGTK(const wxString
& s
, wxFontEncoding enc
)
192 if ( enc
== wxFONTENCODING_SYSTEM
|| enc
== wxFONTENCODING_DEFAULT
)
194 wbuf
= wxConvUI
->cMB2WC(s
);
196 else // another encoding, use generic conversion class
198 wbuf
= wxCSConv(enc
).cMB2WC(s
);
201 if ( !wbuf
&& !s
.empty() )
203 // conversion failed, but we still want to show something to the user
204 // even if it's going to be wrong it is better than nothing
206 // we choose ISO8859-1 here arbitrarily, it's just the most common
207 // encoding probably and, also importantly here, conversion from it
208 // never fails as it's done internally by wxCSConv
209 wbuf
= wxCSConv(wxFONTENCODING_ISO8859_1
).cMB2WC(s
);
212 return wxConvUTF8
.cWC2MB(wbuf
);
215 wxCharBuffer
wxConvertFromGTK(const wxString
& s
, wxFontEncoding enc
)
217 // this conversion should never fail as GTK+ always uses UTF-8 internally
218 // so there are no complications here
219 const wxWCharBuffer
wbuf(wxConvUTF8
.cMB2WC(s
));
220 if ( enc
== wxFONTENCODING_SYSTEM
)
221 return wxConvUI
->cWC2MB(wbuf
);
223 return wxCSConv(enc
).cWC2MB(wbuf
);
226 #endif // !wxUSE_UNICODE
228 // Returns false if version is certainly greater or equal than major.minor.micro
229 // Returns true if version is lower than major.minor.micro OR it cannot be
230 // determined and one should not rely on the availability of pango version
231 // major.minor.micro, nor the non-availability
232 const gchar
*wx_pango_version_check (int major
, int minor
, int micro
)
234 #ifdef PANGO_VERSION_MAJOR
235 if (!gtk_check_version (2,11,0))
237 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
238 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
239 // using has the pango_version_check function:
240 return pango_version_check (major
, minor
, micro
);
243 return "can't check";
244 #else // !PANGO_VERSION_MAJOR
245 return "too old headers";
250 // ----------------------------------------------------------------------------
251 // subprocess routines
252 // ----------------------------------------------------------------------------
256 void GTK_EndProcessDetector(gpointer data
, gint source
,
257 GdkInputCondition
WXUNUSED(condition
) )
259 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
261 // has the process really terminated? unfortunately GDK (or GLib) seem to
262 // generate G_IO_HUP notification even when it simply tries to read from a
263 // closed fd and hasn't terminated at all
264 int pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
266 int rc
= waitpid(pid
, &status
, WNOHANG
);
270 // no, it didn't exit yet, continue waiting
274 // set exit code to -1 if something bad happened
275 proc_data
->exitcode
= rc
!= -1 && WIFEXITED(status
) ? WEXITSTATUS(status
)
278 // child exited, end waiting
281 // don't call us again!
282 gdk_input_remove(proc_data
->tag
);
284 wxHandleProcessTermination(proc_data
);
288 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
290 int tag
= gdk_input_add(fd
,
292 GTK_EndProcessDetector
,
293 (gpointer
)proc_data
);
300 // ----------------------------------------------------------------------------
301 // wxPlatformInfo-related
302 // ----------------------------------------------------------------------------
304 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
307 *verMaj
= gtk_major_version
;
309 *verMin
= gtk_minor_version
;
314 wxTimerImpl
*wxGUIAppTraits::CreateTimerImpl(wxTimer
*timer
)
316 return new wxGTKTimerImpl(timer
);
320 static wxString
GetSM()
325 Dpy() { m_dpy
= XOpenDisplay(NULL
); }
326 ~Dpy() { if ( m_dpy
) XCloseDisplay(m_dpy
); }
328 operator Display
*() const { return m_dpy
; }
334 return wxEmptyString
;
338 SmcConn smc_conn
= SmcOpenConnection(NULL
, NULL
,
340 0 /* mask */, NULL
/* callbacks */,
342 WXSIZEOF(smerr
), smerr
);
346 wxLogWarning(_("Failed to connect to session manager: %s"), smerr
);
347 return wxEmptyString
;
350 char *vendor
= SmcVendor(smc_conn
);
351 wxString ret
= wxString::FromAscii( vendor
);
354 SmcCloseConnection(smc_conn
, 0, NULL
);
359 #endif // wxUSE_DETECT_SM
362 //-----------------------------------------------------------------------------
364 //-----------------------------------------------------------------------------
367 void wxGUIAppTraits::SetLocale()
370 wxUpdateLocaleIsUtf8();
376 #if wxUSE_STACKWALKER
378 // private helper class
379 class StackDump
: public wxStackWalker
382 StackDump(GtkAssertDialog
*dlg
) { m_dlg
=dlg
; }
385 virtual void OnStackFrame(const wxStackFrame
& frame
)
387 wxString fncname
= frame
.GetName();
388 wxString fncargs
= fncname
;
390 size_t n
= fncname
.find(wxT('('));
391 if (n
!= wxString::npos
)
393 // remove arguments from function name
396 // remove function name and brackets from arguments
397 fncargs
= fncargs
.substr(n
+1, fncargs
.length()-n
-2);
400 fncargs
= wxEmptyString
;
402 // append this stack frame's info in the dialog
403 if (!frame
.GetFileName().empty() || !fncname
.empty())
404 gtk_assert_dialog_append_stack_frame(m_dlg
,
407 frame
.GetFileName().mb_str(),
412 GtkAssertDialog
*m_dlg
;
415 // the callback functions must be extern "C" to comply with GTK+ declarations
418 void get_stackframe_callback(StackDump
*dump
)
420 // skip over frames up to including wxOnAssert()
421 dump
->ProcessFrames(3);
425 #endif // wxUSE_STACKWALKER
427 bool wxGUIAppTraits::ShowAssertDialog(const wxString
& msg
)
429 // under GTK2 we prefer to use a dialog widget written using directly GTK+;
430 // in fact we cannot use a dialog written using wxWidgets: it would need
431 // the wxWidgets idle processing to work correctly!
432 GtkWidget
*dialog
= gtk_assert_dialog_new();
433 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog
), msg
.mb_str());
435 #if wxUSE_STACKWALKER
436 // don't show more than maxLines or we could get a dialog too tall to be
437 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
438 // characters it is still only 300 pixels...
439 static const int maxLines
= 20;
441 // save current stack frame...
442 StackDump
dump(GTK_ASSERT_DIALOG(dialog
));
443 dump
.SaveStack(maxLines
);
445 // ...but process it only if the user needs it
446 gtk_assert_dialog_set_backtrace_callback(GTK_ASSERT_DIALOG(dialog
),
447 (GtkAssertDialogStackFrameCallback
)get_stackframe_callback
,
449 #endif // wxUSE_STACKWALKER
451 gint result
= gtk_dialog_run(GTK_DIALOG (dialog
));
452 bool returnCode
= false;
455 case GTK_ASSERT_DIALOG_STOP
:
458 case GTK_ASSERT_DIALOG_CONTINUE
:
461 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
:
467 wxFAIL_MSG( _T("unexpected return code from GtkAssertDialog") );
470 gtk_widget_destroy(dialog
);
474 #endif // __WXDEBUG__
476 wxString
wxGUIAppTraits::GetDesktopEnvironment() const
478 wxString de
= wxSystemOptions::GetOption(_T("gtk.desktop"));
482 static const wxString s_SM
= GetSM();
484 if (s_SM
== wxT("GnomeSM"))
486 else if (s_SM
== wxT("KDE"))
489 #endif // wxUSE_DETECT_SM
496 // see the hack below in wxCmdLineParser::GetUsageString().
497 // TODO: replace this hack with a g_option_group_get_entries()
498 // call as soon as such function exists
503 gchar
*help_description
;
505 GDestroyNotify destroy_notify
;
508 GTranslateFunc translate_func
;
509 GDestroyNotify translate_notify
;
510 gpointer translate_data
;
512 GOptionEntry
*entries
;
515 GOptionParseFunc pre_parse_func
;
516 GOptionParseFunc post_parse_func
;
517 GOptionErrorFunc error_func
;
520 wxString
wxGetNameFromGtkOptionEntry(const GOptionEntry
*opt
)
525 ret
<< _T("-") << opt
->short_name
;
530 ret
<< _T("--") << opt
->long_name
;
532 if (opt
->arg_description
)
533 ret
<< _T("=") << opt
->arg_description
;
536 return _T(" ") + ret
;
539 #endif // __WXGTK26__
542 wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString
& names
,
543 wxArrayString
& desc
) const
548 // check whether GTK version is greater than 2.6 but also lower than 2.12
549 // because, as we use the undocumented _GOptionGroup struct, we don't want
550 // to run this code with future versions which might change it (2.11 is the
551 // latest one at the time of this writing)
552 if (!gtk_check_version(2,6,0) &&
553 gtk_check_version(2,12,0))
555 usage
<< _("The following standard GTK+ options are also supported:\n");
557 // passing true here means that the function can open the default
558 // display while parsing (not really used here anyhow)
559 GOptionGroup
*gtkOpts
= gtk_get_option_group(true);
561 // WARNING: here we access the internals of GOptionGroup:
562 GOptionEntry
*entries
= ((_GOptionGroup
*)gtkOpts
)->entries
;
563 unsigned int n_entries
= ((_GOptionGroup
*)gtkOpts
)->n_entries
;
564 wxArrayString namesOptions
, descOptions
;
566 for ( size_t n
= 0; n
< n_entries
; n
++ )
568 if ( entries
[n
].flags
& G_OPTION_FLAG_HIDDEN
)
571 names
.push_back(wxGetNameFromGtkOptionEntry(&entries
[n
]));
573 const gchar
* const entryDesc
= entries
[n
].description
;
574 desc
.push_back(entryDesc
? wxString(entryDesc
) : _T(""));
577 g_option_group_free (gtkOpts
);
582 #endif // __WXGTK26__