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"
27 #include "wx/gtk/private/timer.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 wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
157 // This is supposed to return desktop dimensions minus any window
158 // manager panels, menus, taskbars, etc. If there is a way to do that
159 // for this platform please fix this function, otherwise it defaults
160 // to the entire desktop.
163 wxDisplaySize(width
, height
);
166 void wxGetMousePosition( int* x
, int* y
)
168 gdk_window_get_pointer( (GdkWindow
*) NULL
, x
, y
, (GdkModifierType
*) NULL
);
171 bool wxColourDisplay()
178 return gdk_drawable_get_visual( wxGetRootWindow()->window
)->depth
;
181 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
183 return wxGenericFindWindowAtPoint(pt
);
188 wxCharBuffer
wxConvertToGTK(const wxString
& s
, wxFontEncoding enc
)
191 if ( enc
== wxFONTENCODING_SYSTEM
|| enc
== wxFONTENCODING_DEFAULT
)
193 wbuf
= wxConvUI
->cMB2WC(s
);
195 else // another encoding, use generic conversion class
197 wbuf
= wxCSConv(enc
).cMB2WC(s
);
200 if ( !wbuf
&& !s
.empty() )
202 // conversion failed, but we still want to show something to the user
203 // even if it's going to be wrong it is better than nothing
205 // we choose ISO8859-1 here arbitrarily, it's just the most common
206 // encoding probably and, also importantly here, conversion from it
207 // never fails as it's done internally by wxCSConv
208 wbuf
= wxCSConv(wxFONTENCODING_ISO8859_1
).cMB2WC(s
);
211 return wxConvUTF8
.cWC2MB(wbuf
);
214 wxCharBuffer
wxConvertFromGTK(const wxString
& s
, wxFontEncoding enc
)
216 // this conversion should never fail as GTK+ always uses UTF-8 internally
217 // so there are no complications here
218 const wxWCharBuffer
wbuf(wxConvUTF8
.cMB2WC(s
));
219 if ( enc
== wxFONTENCODING_SYSTEM
)
220 return wxConvUI
->cWC2MB(wbuf
);
222 return wxCSConv(enc
).cWC2MB(wbuf
);
225 #endif // !wxUSE_UNICODE
227 // Returns false if version is certainly greater or equal than major.minor.micro
228 // Returns true if version is lower than major.minor.micro OR it cannot be
229 // determined and one should not rely on the availability of pango version
230 // major.minor.micro, nor the non-availability
231 const gchar
*wx_pango_version_check (int major
, int minor
, int micro
)
233 #ifdef PANGO_VERSION_MAJOR
234 if (!gtk_check_version (2,11,0))
236 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
237 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
238 // using has the pango_version_check function:
239 return pango_version_check (major
, minor
, micro
);
242 return "can't check";
243 #else // !PANGO_VERSION_MAJOR
244 return "too old headers";
249 // ----------------------------------------------------------------------------
250 // subprocess routines
251 // ----------------------------------------------------------------------------
255 void GTK_EndProcessDetector(gpointer data
, gint source
,
256 GdkInputCondition
WXUNUSED(condition
) )
258 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
260 // has the process really terminated? unfortunately GDK (or GLib) seem to
261 // generate G_IO_HUP notification even when it simply tries to read from a
262 // closed fd and hasn't terminated at all
263 int pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
265 int rc
= waitpid(pid
, &status
, WNOHANG
);
269 // no, it didn't exit yet, continue waiting
273 // set exit code to -1 if something bad happened
274 proc_data
->exitcode
= rc
!= -1 && WIFEXITED(status
) ? WEXITSTATUS(status
)
277 // child exited, end waiting
280 // don't call us again!
281 gdk_input_remove(proc_data
->tag
);
283 wxHandleProcessTermination(proc_data
);
287 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
289 int tag
= gdk_input_add(fd
,
291 GTK_EndProcessDetector
,
292 (gpointer
)proc_data
);
299 // ----------------------------------------------------------------------------
300 // wxPlatformInfo-related
301 // ----------------------------------------------------------------------------
303 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
306 *verMaj
= gtk_major_version
;
308 *verMin
= gtk_minor_version
;
313 wxTimerImpl
*wxGUIAppTraits::CreateTimerImpl(wxTimer
*timer
)
315 return new wxGTKTimerImpl(timer
);
319 static wxString
GetSM()
324 Dpy() { m_dpy
= XOpenDisplay(NULL
); }
325 ~Dpy() { if ( m_dpy
) XCloseDisplay(m_dpy
); }
327 operator Display
*() const { return m_dpy
; }
333 return wxEmptyString
;
336 SmcConn smc_conn
= SmcOpenConnection(NULL
, NULL
,
338 0 /* mask */, NULL
/* callbacks */,
343 return wxEmptyString
;
345 char *vendor
= SmcVendor(smc_conn
);
346 wxString ret
= wxString::FromAscii( vendor
);
349 SmcCloseConnection(smc_conn
, 0, NULL
);
354 #endif // wxUSE_DETECT_SM
357 //-----------------------------------------------------------------------------
359 //-----------------------------------------------------------------------------
362 void wxGUIAppTraits::SetLocale()
365 wxUpdateLocaleIsUtf8();
371 #if wxUSE_STACKWALKER
373 // private helper class
374 class StackDump
: public wxStackWalker
377 StackDump(GtkAssertDialog
*dlg
) { m_dlg
=dlg
; }
380 virtual void OnStackFrame(const wxStackFrame
& frame
)
382 wxString fncname
= frame
.GetName();
383 wxString fncargs
= fncname
;
385 size_t n
= fncname
.find(wxT('('));
386 if (n
!= wxString::npos
)
388 // remove arguments from function name
391 // remove function name and brackets from arguments
392 fncargs
= fncargs
.substr(n
+1, fncargs
.length()-n
-2);
395 fncargs
= wxEmptyString
;
397 // append this stack frame's info in the dialog
398 if (!frame
.GetFileName().empty() || !fncname
.empty())
399 gtk_assert_dialog_append_stack_frame(m_dlg
,
402 frame
.GetFileName().mb_str(),
407 GtkAssertDialog
*m_dlg
;
410 // the callback functions must be extern "C" to comply with GTK+ declarations
413 void get_stackframe_callback(StackDump
*dump
)
415 // skip over frames up to including wxOnAssert()
416 dump
->ProcessFrames(3);
420 #endif // wxUSE_STACKWALKER
422 bool wxGUIAppTraits::ShowAssertDialog(const wxString
& msg
)
424 // under GTK2 we prefer to use a dialog widget written using directly GTK+;
425 // in fact we cannot use a dialog written using wxWidgets: it would need
426 // the wxWidgets idle processing to work correctly!
427 GtkWidget
*dialog
= gtk_assert_dialog_new();
428 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog
), msg
.mb_str());
430 #if wxUSE_STACKWALKER
431 // don't show more than maxLines or we could get a dialog too tall to be
432 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
433 // characters it is still only 300 pixels...
434 static const int maxLines
= 20;
436 // save current stack frame...
437 StackDump
dump(GTK_ASSERT_DIALOG(dialog
));
438 dump
.SaveStack(maxLines
);
440 // ...but process it only if the user needs it
441 gtk_assert_dialog_set_backtrace_callback(GTK_ASSERT_DIALOG(dialog
),
442 (GtkAssertDialogStackFrameCallback
)get_stackframe_callback
,
444 #endif // wxUSE_STACKWALKER
446 gint result
= gtk_dialog_run(GTK_DIALOG (dialog
));
447 bool returnCode
= false;
450 case GTK_ASSERT_DIALOG_STOP
:
453 case GTK_ASSERT_DIALOG_CONTINUE
:
456 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
:
462 wxFAIL_MSG( _T("unexpected return code from GtkAssertDialog") );
465 gtk_widget_destroy(dialog
);
469 #endif // __WXDEBUG__
471 wxString
wxGUIAppTraits::GetDesktopEnvironment() const
474 const wxString SM
= GetSM();
476 if (SM
== wxT("GnomeSM"))
479 if (SM
== wxT("KDE"))
481 #endif // wxUSE_DETECT_SM
483 return wxEmptyString
;
488 // see the hack below in wxCmdLineParser::GetUsageString().
489 // TODO: replace this hack with a g_option_group_get_entries()
490 // call as soon as such function exists
495 gchar
*help_description
;
497 GDestroyNotify destroy_notify
;
500 GTranslateFunc translate_func
;
501 GDestroyNotify translate_notify
;
502 gpointer translate_data
;
504 GOptionEntry
*entries
;
507 GOptionParseFunc pre_parse_func
;
508 GOptionParseFunc post_parse_func
;
509 GOptionErrorFunc error_func
;
512 wxString
wxGetNameFromGtkOptionEntry(const GOptionEntry
*opt
)
517 ret
<< _T("-") << opt
->short_name
;
522 ret
<< _T("--") << opt
->long_name
;
524 if (opt
->arg_description
)
525 ret
<< _T("=") << opt
->arg_description
;
528 return _T(" ") + ret
;
531 #endif // __WXGTK26__
534 wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString
& names
,
535 wxArrayString
& desc
) const
540 // check whether GTK version is greater than 2.6 but also lower than 2.12
541 // because, as we use the undocumented _GOptionGroup struct, we don't want
542 // to run this code with future versions which might change it (2.11 is the
543 // latest one at the time of this writing)
544 if (!gtk_check_version(2,6,0) &&
545 gtk_check_version(2,12,0))
547 usage
<< _("The following standard GTK+ options are also supported:\n");
549 // passing true here means that the function can open the default
550 // display while parsing (not really used here anyhow)
551 GOptionGroup
*gtkOpts
= gtk_get_option_group(true);
553 // WARNING: here we access the internals of GOptionGroup:
554 GOptionEntry
*entries
= ((_GOptionGroup
*)gtkOpts
)->entries
;
555 unsigned int n_entries
= ((_GOptionGroup
*)gtkOpts
)->n_entries
;
556 wxArrayString namesOptions
, descOptions
;
558 for ( size_t n
= 0; n
< n_entries
; n
++ )
560 if ( entries
[n
].flags
& G_OPTION_FLAG_HIDDEN
)
563 names
.push_back(wxGetNameFromGtkOptionEntry(&entries
[n
]));
565 const gchar
* const entryDesc
= entries
[n
].description
;
566 desc
.push_back(entryDesc
? wxString(entryDesc
) : _T(""));
569 g_option_group_free (gtkOpts
);
574 #endif // __WXGTK26__