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"
22 #include "wx/process.h"
23 #include "wx/sysopt.h"
24 #include "wx/unix/execute.h"
26 #include "wx/gtk/private/timer.h"
27 #include "wx/evtloop.h"
30 #include "wx/gtk/assertdlg_gtk.h"
32 #include "wx/stackwalk.h"
33 #endif // wxUSE_STACKWALKER
39 #include <sys/types.h>
40 #include <sys/wait.h> // for WNOHANG
48 #ifdef HAVE_X11_XKBLIB_H
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. */
52 #define explicit __wx_explicit
54 #include "X11/XKBlib.h"
57 #endif // HAVE_X11_XKBLIB_H
62 #include "X11/SM/SMlib.h"
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
69 extern GtkWidget
*wxGetRootWindow();
71 //----------------------------------------------------------------------------
73 //----------------------------------------------------------------------------
75 // on OS/2, we use the wxBell from wxBase library
83 /* Don't synthesize KeyUp events holding down a key and producing
84 KeyDown events with autorepeat. */
85 #ifdef HAVE_X11_XKBLIB_H
86 bool wxSetDetectableAutoRepeat( bool flag
)
89 XkbSetDetectableAutoRepeat( GDK_DISPLAY(), flag
, &result
);
90 return result
; /* true if keyboard hardware supports this mode */
93 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag
) )
99 // Escapes string so that it is valid Pango markup XML string:
100 wxString
wxEscapeStringForPangoMarkup(const wxString
& str
)
102 size_t len
= str
.length();
105 for (size_t i
= 0; i
< len
; i
++)
134 // ----------------------------------------------------------------------------
135 // display characterstics
136 // ----------------------------------------------------------------------------
140 return GDK_DISPLAY();
143 void wxDisplaySize( int *width
, int *height
)
145 if (width
) *width
= gdk_screen_width();
146 if (height
) *height
= gdk_screen_height();
149 void wxDisplaySizeMM( int *width
, int *height
)
151 if (width
) *width
= gdk_screen_width_mm();
152 if (height
) *height
= gdk_screen_height_mm();
155 void wxGetMousePosition( int* x
, int* y
)
157 gdk_window_get_pointer( (GdkWindow
*) NULL
, x
, y
, (GdkModifierType
*) NULL
);
160 bool wxColourDisplay()
167 return gdk_drawable_get_visual( wxGetRootWindow()->window
)->depth
;
170 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
172 return wxGenericFindWindowAtPoint(pt
);
177 WXDLLIMPEXP_CORE wxCharBuffer
178 wxConvertToGTK(const wxString
& s
, wxFontEncoding enc
)
181 if ( enc
== wxFONTENCODING_SYSTEM
|| enc
== wxFONTENCODING_DEFAULT
)
183 wbuf
= wxConvUI
->cMB2WC(s
.c_str());
185 else // another encoding, use generic conversion class
187 wbuf
= wxCSConv(enc
).cMB2WC(s
.c_str());
190 if ( !wbuf
&& !s
.empty() )
192 // conversion failed, but we still want to show something to the user
193 // even if it's going to be wrong it is better than nothing
195 // we choose ISO8859-1 here arbitrarily, it's just the most common
196 // encoding probably and, also importantly here, conversion from it
197 // never fails as it's done internally by wxCSConv
198 wbuf
= wxCSConv(wxFONTENCODING_ISO8859_1
).cMB2WC(s
.c_str());
201 return wxConvUTF8
.cWC2MB(wbuf
);
204 WXDLLIMPEXP_CORE wxCharBuffer
205 wxConvertFromGTK(const wxString
& s
, wxFontEncoding enc
)
207 // this conversion should never fail as GTK+ always uses UTF-8 internally
208 // so there are no complications here
209 const wxWCharBuffer
wbuf(wxConvUTF8
.cMB2WC(s
.c_str()));
210 if ( enc
== wxFONTENCODING_SYSTEM
)
211 return wxConvUI
->cWC2MB(wbuf
);
213 return wxCSConv(enc
).cWC2MB(wbuf
);
216 #endif // !wxUSE_UNICODE
218 // Returns false if version is certainly greater or equal than major.minor.micro
219 // Returns true if version is lower than major.minor.micro OR it cannot be
220 // determined and one should not rely on the availability of pango version
221 // major.minor.micro, nor the non-availability
222 const gchar
*wx_pango_version_check (int major
, int minor
, int micro
)
224 #ifdef PANGO_VERSION_MAJOR
225 if (!gtk_check_version (2,11,0))
227 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
228 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
229 // using has the pango_version_check function:
230 return pango_version_check (major
, minor
, micro
);
233 return "can't check";
234 #else // !PANGO_VERSION_MAJOR
239 return "too old headers";
244 // ----------------------------------------------------------------------------
245 // subprocess routines
246 // ----------------------------------------------------------------------------
250 void GTK_EndProcessDetector(gpointer data
, gint source
,
251 GdkInputCondition
WXUNUSED(condition
) )
253 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
255 // has the process really terminated? unfortunately GDK (or GLib) seem to
256 // generate G_IO_HUP notification even when it simply tries to read from a
257 // closed fd and hasn't terminated at all
258 int pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
260 int rc
= waitpid(pid
, &status
, WNOHANG
);
264 // no, it didn't exit yet, continue waiting
268 // set exit code to -1 if something bad happened
269 proc_data
->exitcode
= rc
!= -1 && WIFEXITED(status
) ? WEXITSTATUS(status
)
272 // child exited, end waiting
275 // don't call us again!
276 gdk_input_remove(proc_data
->tag
);
278 wxHandleProcessTermination(proc_data
);
282 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
284 int tag
= gdk_input_add(fd
,
286 GTK_EndProcessDetector
,
287 (gpointer
)proc_data
);
294 // ----------------------------------------------------------------------------
295 // wxPlatformInfo-related
296 // ----------------------------------------------------------------------------
298 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
301 *verMaj
= gtk_major_version
;
303 *verMin
= gtk_minor_version
;
310 wxTimerImpl
*wxGUIAppTraits::CreateTimerImpl(wxTimer
*timer
)
312 return new wxGTKTimerImpl(timer
);
315 #endif // wxUSE_TIMER
318 static wxString
GetSM()
323 Dpy() { m_dpy
= XOpenDisplay(NULL
); }
324 ~Dpy() { if ( m_dpy
) XCloseDisplay(m_dpy
); }
326 operator Display
*() const { return m_dpy
; }
332 return wxEmptyString
;
336 SmcConn smc_conn
= SmcOpenConnection(NULL
, NULL
,
338 0 /* mask */, NULL
/* callbacks */,
340 WXSIZEOF(smerr
), smerr
);
344 wxLogWarning(_("Failed to connect to session manager: %s"), smerr
);
345 return wxEmptyString
;
348 char *vendor
= SmcVendor(smc_conn
);
349 wxString ret
= wxString::FromAscii( vendor
);
352 SmcCloseConnection(smc_conn
, 0, NULL
);
357 #endif // wxUSE_DETECT_SM
360 //-----------------------------------------------------------------------------
362 //-----------------------------------------------------------------------------
364 wxEventLoopBase
*wxGUIAppTraits::CreateEventLoop()
366 return new wxEventLoop();
371 void wxGUIAppTraits::SetLocale()
374 wxUpdateLocaleIsUtf8();
380 #if wxUSE_STACKWALKER
382 // private helper class
383 class StackDump
: public wxStackWalker
386 StackDump(GtkAssertDialog
*dlg
) { m_dlg
=dlg
; }
389 virtual void OnStackFrame(const wxStackFrame
& frame
)
391 wxString fncname
= frame
.GetName();
392 wxString fncargs
= fncname
;
394 size_t n
= fncname
.find(wxT('('));
395 if (n
!= wxString::npos
)
397 // remove arguments from function name
400 // remove function name and brackets from arguments
401 fncargs
= fncargs
.substr(n
+1, fncargs
.length()-n
-2);
404 fncargs
= wxEmptyString
;
406 // append this stack frame's info in the dialog
407 if (!frame
.GetFileName().empty() || !fncname
.empty())
408 gtk_assert_dialog_append_stack_frame(m_dlg
,
411 frame
.GetFileName().mb_str(),
416 GtkAssertDialog
*m_dlg
;
419 // the callback functions must be extern "C" to comply with GTK+ declarations
422 void get_stackframe_callback(StackDump
*dump
)
424 // skip over frames up to including wxOnAssert()
425 dump
->ProcessFrames(3);
429 #endif // wxUSE_STACKWALKER
431 bool wxGUIAppTraits::ShowAssertDialog(const wxString
& msg
)
433 // under GTK2 we prefer to use a dialog widget written using directly GTK+;
434 // in fact we cannot use a dialog written using wxWidgets: it would need
435 // the wxWidgets idle processing to work correctly!
436 GtkWidget
*dialog
= gtk_assert_dialog_new();
437 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog
), msg
.mb_str());
439 #if wxUSE_STACKWALKER
440 // don't show more than maxLines or we could get a dialog too tall to be
441 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
442 // characters it is still only 300 pixels...
443 static const int maxLines
= 20;
445 // save current stack frame...
446 StackDump
dump(GTK_ASSERT_DIALOG(dialog
));
447 dump
.SaveStack(maxLines
);
449 // ...but process it only if the user needs it
450 gtk_assert_dialog_set_backtrace_callback(GTK_ASSERT_DIALOG(dialog
),
451 (GtkAssertDialogStackFrameCallback
)get_stackframe_callback
,
453 #endif // wxUSE_STACKWALKER
455 gint result
= gtk_dialog_run(GTK_DIALOG (dialog
));
456 bool returnCode
= false;
459 case GTK_ASSERT_DIALOG_STOP
:
462 case GTK_ASSERT_DIALOG_CONTINUE
:
465 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
:
471 wxFAIL_MSG( _T("unexpected return code from GtkAssertDialog") );
474 gtk_widget_destroy(dialog
);
478 #endif // __WXDEBUG__
480 wxString
wxGUIAppTraits::GetDesktopEnvironment() const
482 wxString de
= wxSystemOptions::GetOption(_T("gtk.desktop"));
486 static const wxString s_SM
= GetSM();
488 if (s_SM
== wxT("GnomeSM"))
490 else if (s_SM
== wxT("KDE"))
493 #endif // wxUSE_DETECT_SM
500 // see the hack below in wxCmdLineParser::GetUsageString().
501 // TODO: replace this hack with a g_option_group_get_entries()
502 // call as soon as such function exists
507 gchar
*help_description
;
509 GDestroyNotify destroy_notify
;
512 GTranslateFunc translate_func
;
513 GDestroyNotify translate_notify
;
514 gpointer translate_data
;
516 GOptionEntry
*entries
;
519 GOptionParseFunc pre_parse_func
;
520 GOptionParseFunc post_parse_func
;
521 GOptionErrorFunc error_func
;
524 wxString
wxGetNameFromGtkOptionEntry(const GOptionEntry
*opt
)
529 ret
<< _T("-") << opt
->short_name
;
534 ret
<< _T("--") << opt
->long_name
;
536 if (opt
->arg_description
)
537 ret
<< _T("=") << opt
->arg_description
;
540 return _T(" ") + ret
;
543 #endif // __WXGTK26__
546 wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString
& names
,
547 wxArrayString
& desc
) const
552 // check whether GTK version is greater than 2.6 but also lower than 2.12
553 // because, as we use the undocumented _GOptionGroup struct, we don't want
554 // to run this code with future versions which might change it (2.11 is the
555 // latest one at the time of this writing)
556 if (!gtk_check_version(2,6,0) &&
557 gtk_check_version(2,12,0))
559 usage
<< _("The following standard GTK+ options are also supported:\n");
561 // passing true here means that the function can open the default
562 // display while parsing (not really used here anyhow)
563 GOptionGroup
*gtkOpts
= gtk_get_option_group(true);
565 // WARNING: here we access the internals of GOptionGroup:
566 GOptionEntry
*entries
= ((_GOptionGroup
*)gtkOpts
)->entries
;
567 unsigned int n_entries
= ((_GOptionGroup
*)gtkOpts
)->n_entries
;
568 wxArrayString namesOptions
, descOptions
;
570 for ( size_t n
= 0; n
< n_entries
; n
++ )
572 if ( entries
[n
].flags
& G_OPTION_FLAG_HIDDEN
)
575 names
.push_back(wxGetNameFromGtkOptionEntry(&entries
[n
]));
577 const gchar
* const entryDesc
= entries
[n
].description
;
578 desc
.push_back(entryDesc
? wxString(entryDesc
) : _T(""));
581 g_option_group_free (gtkOpts
);
586 #endif // __WXGTK26__