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_SYSTEM
|| enc
== wxFONTENCODING_DEFAULT
)
191 wbuf
= wxConvUI
->cMB2WC(s
);
193 else // another encoding, use generic conversion class
195 wbuf
= wxCSConv(enc
).cMB2WC(s
);
198 if ( !wbuf
&& !s
.empty() )
200 // conversion failed, but we still want to show something to the user
201 // even if it's going to be wrong it is better than nothing
203 // we choose ISO8859-1 here arbitrarily, it's just the most common
204 // encoding probably and, also importantly here, conversion from it
205 // never fails as it's done internally by wxCSConv
206 wbuf
= wxCSConv(wxFONTENCODING_ISO8859_1
).cMB2WC(s
);
209 return wxConvUTF8
.cWC2MB(wbuf
);
212 wxCharBuffer
wxConvertFromGTK(const wxString
& s
, wxFontEncoding enc
)
214 // this conversion should never fail as GTK+ always uses UTF-8 internally
215 // so there are no complications here
216 const wxWCharBuffer
wbuf(wxConvUTF8
.cMB2WC(s
));
217 if ( enc
== wxFONTENCODING_SYSTEM
)
218 return wxConvUI
->cWC2MB(wbuf
);
220 return wxCSConv(enc
).cWC2MB(wbuf
);
223 #endif // !wxUSE_UNICODE
225 // ----------------------------------------------------------------------------
226 // subprocess routines
227 // ----------------------------------------------------------------------------
231 void GTK_EndProcessDetector(gpointer data
, gint source
,
232 GdkInputCondition
WXUNUSED(condition
) )
234 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
236 // has the process really terminated? unfortunately GDK (or GLib) seem to
237 // generate G_IO_HUP notification even when it simply tries to read from a
238 // closed fd and hasn't terminated at all
239 int pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
241 int rc
= waitpid(pid
, &status
, WNOHANG
);
245 // no, it didn't exit yet, continue waiting
249 // set exit code to -1 if something bad happened
250 proc_data
->exitcode
= rc
!= -1 && WIFEXITED(status
) ? WEXITSTATUS(status
)
253 // child exited, end waiting
256 // don't call us again!
257 gdk_input_remove(proc_data
->tag
);
259 wxHandleProcessTermination(proc_data
);
263 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
265 int tag
= gdk_input_add(fd
,
267 GTK_EndProcessDetector
,
268 (gpointer
)proc_data
);
275 // ----------------------------------------------------------------------------
276 // wxPlatformInfo-related
277 // ----------------------------------------------------------------------------
279 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
282 *verMaj
= gtk_major_version
;
284 *verMin
= gtk_minor_version
;
290 static wxString
GetSM()
295 Dpy() { m_dpy
= XOpenDisplay(NULL
); }
296 ~Dpy() { if ( m_dpy
) XCloseDisplay(m_dpy
); }
298 operator Display
*() const { return m_dpy
; }
304 return wxEmptyString
;
307 SmcConn smc_conn
= SmcOpenConnection(NULL
, NULL
,
309 0 /* mask */, NULL
/* callbacks */,
314 return wxEmptyString
;
316 char *vendor
= SmcVendor(smc_conn
);
317 wxString ret
= wxString::FromAscii( vendor
);
320 SmcCloseConnection(smc_conn
, 0, NULL
);
325 #endif // wxUSE_DETECT_SM
328 //-----------------------------------------------------------------------------
330 //-----------------------------------------------------------------------------
333 void wxGUIAppTraits::SetLocale()
341 #if wxUSE_STACKWALKER
343 // private helper class
344 class StackDump
: public wxStackWalker
347 StackDump(GtkAssertDialog
*dlg
) { m_dlg
=dlg
; }
350 virtual void OnStackFrame(const wxStackFrame
& frame
)
352 wxString fncname
= frame
.GetName();
353 wxString fncargs
= fncname
;
355 size_t n
= fncname
.find(wxT('('));
356 if (n
!= wxString::npos
)
358 // remove arguments from function name
361 // remove function name and brackets from arguments
362 fncargs
= fncargs
.substr(n
+1, fncargs
.length()-n
-2);
365 fncargs
= wxEmptyString
;
367 // append this stack frame's info in the dialog
368 if (!frame
.GetFileName().empty() || !fncname
.empty())
369 gtk_assert_dialog_append_stack_frame(m_dlg
,
372 frame
.GetFileName().mb_str(),
377 GtkAssertDialog
*m_dlg
;
380 // the callback functions must be extern "C" to comply with GTK+ declarations
383 void get_stackframe_callback(StackDump
*dump
)
385 // skip over frames up to including wxOnAssert()
386 dump
->ProcessFrames(3);
390 #endif // wxUSE_STACKWALKER
392 bool wxGUIAppTraits::ShowAssertDialog(const wxString
& msg
)
394 // under GTK2 we prefer to use a dialog widget written using directly GTK+;
395 // in fact we cannot use a dialog written using wxWidgets: it would need
396 // the wxWidgets idle processing to work correctly!
397 GtkWidget
*dialog
= gtk_assert_dialog_new();
398 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog
), msg
.mb_str());
400 #if wxUSE_STACKWALKER
401 // don't show more than maxLines or we could get a dialog too tall to be
402 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
403 // characters it is still only 300 pixels...
404 static const int maxLines
= 20;
406 // save current stack frame...
407 StackDump
dump(GTK_ASSERT_DIALOG(dialog
));
408 dump
.SaveStack(maxLines
);
410 // ...but process it only if the user needs it
411 gtk_assert_dialog_set_backtrace_callback(GTK_ASSERT_DIALOG(dialog
),
412 (GtkAssertDialogStackFrameCallback
)get_stackframe_callback
,
414 #endif // wxUSE_STACKWALKER
416 gint result
= gtk_dialog_run(GTK_DIALOG (dialog
));
417 bool returnCode
= false;
420 case GTK_ASSERT_DIALOG_STOP
:
423 case GTK_ASSERT_DIALOG_CONTINUE
:
426 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
:
432 wxFAIL_MSG( _T("unexpected return code from GtkAssertDialog") );
435 gtk_widget_destroy(dialog
);
439 #endif // __WXDEBUG__
441 wxString
wxGUIAppTraits::GetDesktopEnvironment() const
444 const wxString SM
= GetSM();
446 if (SM
== wxT("GnomeSM"))
449 if (SM
== wxT("KDE"))
451 #endif // wxUSE_DETECT_SM
453 return wxEmptyString
;