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"
25 #include "wx/unix/execute.h"
28 #include "wx/gtk/assertdlg_gtk.h"
30 #include "wx/stackwalk.h"
31 #endif // wxUSE_STACKWALKER
37 #include <sys/types.h>
38 #include <sys/wait.h> // for WNOHANG
46 #ifdef HAVE_X11_XKBLIB_H
47 /* under HP-UX and Solaris 2.6, at least, XKBlib.h defines structures with
48 * field named "explicit" - which is, of course, an error for a C++
49 * compiler. To be on the safe side, just redefine it everywhere. */
50 #define explicit __wx_explicit
52 #include "X11/XKBlib.h"
55 #endif // HAVE_X11_XKBLIB_H
60 #include "X11/SM/SMlib.h"
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 extern GtkWidget
*wxGetRootWindow();
69 //----------------------------------------------------------------------------
71 //----------------------------------------------------------------------------
73 // on OS/2, we use the wxBell from wxBase library
81 /* Don't synthesize KeyUp events holding down a key and producing
82 KeyDown events with autorepeat. */
83 #ifdef HAVE_X11_XKBLIB_H
84 bool wxSetDetectableAutoRepeat( bool flag
)
87 XkbSetDetectableAutoRepeat( GDK_DISPLAY(), flag
, &result
);
88 return result
; /* true if keyboard hardware supports this mode */
91 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag
) )
97 // Escapes string so that it is valid Pango markup XML string:
98 wxString
wxEscapeStringForPangoMarkup(const wxString
& str
)
100 size_t len
= str
.length();
103 for (size_t i
= 0; i
< len
; i
++)
132 // ----------------------------------------------------------------------------
133 // display characterstics
134 // ----------------------------------------------------------------------------
138 return GDK_DISPLAY();
141 void wxDisplaySize( int *width
, int *height
)
143 if (width
) *width
= gdk_screen_width();
144 if (height
) *height
= gdk_screen_height();
147 void wxDisplaySizeMM( int *width
, int *height
)
149 if (width
) *width
= gdk_screen_width_mm();
150 if (height
) *height
= gdk_screen_height_mm();
153 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
155 // This is supposed to return desktop dimensions minus any window
156 // manager panels, menus, taskbars, etc. If there is a way to do that
157 // for this platform please fix this function, otherwise it defaults
158 // to the entire desktop.
161 wxDisplaySize(width
, height
);
164 void wxGetMousePosition( int* x
, int* y
)
166 gdk_window_get_pointer( (GdkWindow
*) NULL
, x
, y
, (GdkModifierType
*) NULL
);
169 bool wxColourDisplay()
176 return gdk_drawable_get_visual( wxGetRootWindow()->window
)->depth
;
179 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
181 return wxGenericFindWindowAtPoint(pt
);
186 wxCharBuffer
wxConvertToGTK(const wxString
& s
, wxFontEncoding enc
)
189 if ( enc
== wxFONTENCODING_UTF8
)
191 // no need for conversion at all, but do check that we have a valid
192 // UTF-8 string because passing invalid UTF-8 to GTK+ is going to
193 // result in a GTK+ error message and, especially, loss of data which
194 // was supposed to be shown in the GUI
195 if ( wxConvUTF8
.ToWChar(NULL
, 0, s
, s
.length()) == wxCONV_FAILED
)
197 // warn the programmer that something is probably wrong in his code
199 // NB: don't include the string in output because chances are that
200 // this invalid UTF-8 string could result in more errors itself
201 // if the application shows logs in the GUI and so we get into
203 wxLogDebug(_T("Invalid UTF-8 string in wxConvertToGTK()"));
205 // but still try to show at least something on the screen
206 wxMBConvUTF8
utf8permissive(wxMBConvUTF8::MAP_INVALID_UTF8_TO_OCTAL
);
207 wxWCharBuffer
wbuf(utf8permissive
.cMB2WC(s
));
208 buf
= wxConvUTF8
.cWC2MB(wbuf
);
210 else // valid UTF-8 string, no need to convert
212 buf
= wxCharBuffer(s
);
218 if ( enc
== wxFONTENCODING_SYSTEM
|| enc
== wxFONTENCODING_DEFAULT
)
220 wbuf
= wxConvUI
->cMB2WC(s
);
222 else // another encoding, use generic conversion class
224 wbuf
= wxCSConv(enc
).cMB2WC(s
);
228 buf
= wxConvUTF8
.cWC2MB(wbuf
);
234 #endif // !wxUSE_UNICODE
236 // ----------------------------------------------------------------------------
237 // subprocess routines
238 // ----------------------------------------------------------------------------
242 void GTK_EndProcessDetector(gpointer data
, gint source
,
243 GdkInputCondition
WXUNUSED(condition
) )
245 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
247 // has the process really terminated? unfortunately GDK (or GLib) seem to
248 // generate G_IO_HUP notification even when it simply tries to read from a
249 // closed fd and hasn't terminated at all
250 int pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
252 int rc
= waitpid(pid
, &status
, WNOHANG
);
256 // no, it didn't exit yet, continue waiting
260 // set exit code to -1 if something bad happened
261 proc_data
->exitcode
= rc
!= -1 && WIFEXITED(status
) ? WEXITSTATUS(status
)
264 // child exited, end waiting
267 // don't call us again!
268 gdk_input_remove(proc_data
->tag
);
270 wxHandleProcessTermination(proc_data
);
274 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
276 int tag
= gdk_input_add(fd
,
278 GTK_EndProcessDetector
,
279 (gpointer
)proc_data
);
286 // ----------------------------------------------------------------------------
287 // wxPlatformInfo-related
288 // ----------------------------------------------------------------------------
290 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
293 *verMaj
= gtk_major_version
;
295 *verMin
= gtk_minor_version
;
301 static wxString
GetSM()
306 Dpy() { m_dpy
= XOpenDisplay(NULL
); }
307 ~Dpy() { if ( m_dpy
) XCloseDisplay(m_dpy
); }
309 operator Display
*() const { return m_dpy
; }
315 return wxEmptyString
;
318 SmcConn smc_conn
= SmcOpenConnection(NULL
, NULL
,
320 0 /* mask */, NULL
/* callbacks */,
325 return wxEmptyString
;
327 char *vendor
= SmcVendor(smc_conn
);
328 wxString ret
= wxString::FromAscii( vendor
);
331 SmcCloseConnection(smc_conn
, 0, NULL
);
336 #endif // wxUSE_DETECT_SM
339 //-----------------------------------------------------------------------------
341 //-----------------------------------------------------------------------------
345 #if wxUSE_STACKWALKER
347 // private helper class
348 class StackDump
: public wxStackWalker
351 StackDump(GtkAssertDialog
*dlg
) { m_dlg
=dlg
; }
354 virtual void OnStackFrame(const wxStackFrame
& frame
)
356 wxString fncname
= frame
.GetName();
357 wxString fncargs
= fncname
;
359 size_t n
= fncname
.find(wxT('('));
360 if (n
!= wxString::npos
)
362 // remove arguments from function name
365 // remove function name and brackets from arguments
366 fncargs
= fncargs
.substr(n
+1, fncargs
.length()-n
-2);
369 fncargs
= wxEmptyString
;
371 // append this stack frame's info in the dialog
372 if (!frame
.GetFileName().empty() || !fncname
.empty())
373 gtk_assert_dialog_append_stack_frame(m_dlg
,
376 frame
.GetFileName().mb_str(),
381 GtkAssertDialog
*m_dlg
;
384 // the callback functions must be extern "C" to comply with GTK+ declarations
387 void get_stackframe_callback(StackDump
*dump
)
389 dump
->ProcessFrames(2); // don't show ShowAssertDialog() call itself
393 #endif // wxUSE_STACKWALKER
395 bool wxGUIAppTraits::ShowAssertDialog(const wxString
& msg
)
397 // under GTK2 we prefer to use a dialog widget written using directly GTK+;
398 // in fact we cannot use a dialog written using wxWidgets: it would need
399 // the wxWidgets idle processing to work correctly!
400 GtkWidget
*dialog
= gtk_assert_dialog_new();
401 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog
), msg
.mb_str());
403 #if wxUSE_STACKWALKER
404 // don't show more than maxLines or we could get a dialog too tall to be
405 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
406 // characters it is still only 300 pixels...
407 static const int maxLines
= 20;
409 // save current stack frame...
410 StackDump
dump(GTK_ASSERT_DIALOG(dialog
));
411 dump
.SaveStack(maxLines
);
413 // ...but process it only if the user needs it
414 gtk_assert_dialog_set_backtrace_callback(GTK_ASSERT_DIALOG(dialog
),
415 (GtkAssertDialogStackFrameCallback
)get_stackframe_callback
,
417 #endif // wxUSE_STACKWALKER
419 gint result
= gtk_dialog_run(GTK_DIALOG (dialog
));
420 bool returnCode
= false;
423 case GTK_ASSERT_DIALOG_STOP
:
426 case GTK_ASSERT_DIALOG_CONTINUE
:
429 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
:
435 wxFAIL_MSG( _T("unexpected return code from GtkAssertDialog") );
438 gtk_widget_destroy(dialog
);
442 #endif // __WXDEBUG__
444 wxString
wxGUIAppTraits::GetDesktopEnvironment() const
447 const wxString SM
= GetSM();
449 if (SM
== wxT("GnomeSM"))
452 if (SM
== wxT("KDE"))
454 #endif // wxUSE_DETECT_SM
456 return wxEmptyString
;