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
34 #endif // wxDEBUG_LEVEL
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 // ----------------------------------------------------------------------------
100 // display characterstics
101 // ----------------------------------------------------------------------------
105 return GDK_DISPLAY();
108 void wxDisplaySize( int *width
, int *height
)
110 if (width
) *width
= gdk_screen_width();
111 if (height
) *height
= gdk_screen_height();
114 void wxDisplaySizeMM( int *width
, int *height
)
116 if (width
) *width
= gdk_screen_width_mm();
117 if (height
) *height
= gdk_screen_height_mm();
120 void wxGetMousePosition( int* x
, int* y
)
122 gdk_window_get_pointer( NULL
, x
, y
, NULL
);
125 bool wxColourDisplay()
132 return gdk_drawable_get_visual( wxGetRootWindow()->window
)->depth
;
135 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
137 return wxGenericFindWindowAtPoint(pt
);
142 WXDLLIMPEXP_CORE wxCharBuffer
143 wxConvertToGTK(const wxString
& s
, wxFontEncoding enc
)
146 if ( enc
== wxFONTENCODING_SYSTEM
|| enc
== wxFONTENCODING_DEFAULT
)
148 wbuf
= wxConvUI
->cMB2WC(s
.c_str());
150 else // another encoding, use generic conversion class
152 wbuf
= wxCSConv(enc
).cMB2WC(s
.c_str());
155 if ( !wbuf
&& !s
.empty() )
157 // conversion failed, but we still want to show something to the user
158 // even if it's going to be wrong it is better than nothing
160 // we choose ISO8859-1 here arbitrarily, it's just the most common
161 // encoding probably and, also importantly here, conversion from it
162 // never fails as it's done internally by wxCSConv
163 wbuf
= wxCSConv(wxFONTENCODING_ISO8859_1
).cMB2WC(s
.c_str());
166 return wxConvUTF8
.cWC2MB(wbuf
);
169 WXDLLIMPEXP_CORE wxCharBuffer
170 wxConvertFromGTK(const wxString
& s
, wxFontEncoding enc
)
172 // this conversion should never fail as GTK+ always uses UTF-8 internally
173 // so there are no complications here
174 const wxWCharBuffer
wbuf(wxConvUTF8
.cMB2WC(s
.c_str()));
175 if ( enc
== wxFONTENCODING_SYSTEM
)
176 return wxConvUI
->cWC2MB(wbuf
);
178 return wxCSConv(enc
).cWC2MB(wbuf
);
181 #endif // !wxUSE_UNICODE
183 // Returns NULL if version is certainly greater or equal than major.minor.micro
184 // Returns string describing the error if version is lower than
185 // major.minor.micro OR it cannot be determined and one should not rely on the
186 // availability of pango version major.minor.micro, nor the non-availability
187 const gchar
*wx_pango_version_check (int major
, int minor
, int micro
)
189 // NOTE: you don't need to use this macro to check for Pango features
190 // added in pango-1.4 or earlier since GTK 2.4 (our minimum requirement
191 // for GTK lib) required pango 1.4...
193 #ifdef PANGO_VERSION_MAJOR
194 if (!gtk_check_version (2,11,0))
196 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
197 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
198 // using has the pango_version_check function:
199 return pango_version_check (major
, minor
, micro
);
202 return "can't check";
203 #else // !PANGO_VERSION_MAJOR
208 return "too old headers";
213 // ----------------------------------------------------------------------------
214 // subprocess routines
215 // ----------------------------------------------------------------------------
219 void GTK_EndProcessDetector(gpointer data
, gint source
,
220 GdkInputCondition
WXUNUSED(condition
))
222 wxEndProcessData
* const
223 proc_data
= static_cast<wxEndProcessData
*>(data
);
225 // child exited, end waiting
228 // don't call us again!
229 gdk_input_remove(proc_data
->tag
);
231 wxHandleProcessTermination(proc_data
);
235 int wxGUIAppTraits::AddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
237 int tag
= gdk_input_add(fd
,
239 GTK_EndProcessDetector
,
240 (gpointer
)proc_data
);
247 // ----------------------------------------------------------------------------
248 // wxPlatformInfo-related
249 // ----------------------------------------------------------------------------
251 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
254 *verMaj
= gtk_major_version
;
256 *verMin
= gtk_minor_version
;
263 wxTimerImpl
*wxGUIAppTraits::CreateTimerImpl(wxTimer
*timer
)
265 return new wxGTKTimerImpl(timer
);
268 #endif // wxUSE_TIMER
271 static wxString
GetSM()
276 Dpy() { m_dpy
= XOpenDisplay(NULL
); }
277 ~Dpy() { if ( m_dpy
) XCloseDisplay(m_dpy
); }
279 operator Display
*() const { return m_dpy
; }
285 return wxEmptyString
;
289 SmcConn smc_conn
= SmcOpenConnection(NULL
, NULL
,
291 0 /* mask */, NULL
/* callbacks */,
293 WXSIZEOF(smerr
), smerr
);
297 wxLogDebug("Failed to connect to session manager: %s", smerr
);
298 return wxEmptyString
;
301 char *vendor
= SmcVendor(smc_conn
);
302 wxString ret
= wxString::FromAscii( vendor
);
305 SmcCloseConnection(smc_conn
, 0, NULL
);
310 #endif // wxUSE_DETECT_SM
313 //-----------------------------------------------------------------------------
315 //-----------------------------------------------------------------------------
317 wxEventLoopBase
*wxGUIAppTraits::CreateEventLoop()
319 return new wxEventLoop();
324 void wxGUIAppTraits::SetLocale()
327 wxUpdateLocaleIsUtf8();
331 #if wxDEBUG_LEVEL && wxUSE_STACKWALKER
333 // private helper class
334 class StackDump
: public wxStackWalker
337 StackDump(GtkAssertDialog
*dlg
) { m_dlg
=dlg
; }
340 virtual void OnStackFrame(const wxStackFrame
& frame
)
342 wxString fncname
= frame
.GetName();
343 wxString fncargs
= fncname
;
345 size_t n
= fncname
.find(wxT('('));
346 if (n
!= wxString::npos
)
348 // remove arguments from function name
351 // remove function name and brackets from arguments
352 fncargs
= fncargs
.substr(n
+1, fncargs
.length()-n
-2);
355 fncargs
= wxEmptyString
;
357 // append this stack frame's info in the dialog
358 if (!frame
.GetFileName().empty() || !fncname
.empty())
359 gtk_assert_dialog_append_stack_frame(m_dlg
,
362 frame
.GetFileName().mb_str(),
367 GtkAssertDialog
*m_dlg
;
370 // the callback functions must be extern "C" to comply with GTK+ declarations
373 void get_stackframe_callback(StackDump
*dump
)
375 // skip over frames up to including wxOnAssert()
376 dump
->ProcessFrames(3);
380 #endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
382 bool wxGUIAppTraits::ShowAssertDialog(const wxString
& msg
)
385 // under GTK2 we prefer to use a dialog widget written using directly in
386 // GTK+ as use a dialog written using wxWidgets would need the wxWidgets
387 // idle processing to work correctly which might not be the case when
389 GtkWidget
*dialog
= gtk_assert_dialog_new();
390 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog
), msg
.mb_str());
392 #if wxUSE_STACKWALKER
393 // don't show more than maxLines or we could get a dialog too tall to be
394 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
395 // characters it is still only 300 pixels...
396 static const int maxLines
= 20;
398 // save current stack frame...
399 StackDump
dump(GTK_ASSERT_DIALOG(dialog
));
400 dump
.SaveStack(maxLines
);
402 // ...but process it only if the user needs it
403 gtk_assert_dialog_set_backtrace_callback(GTK_ASSERT_DIALOG(dialog
),
404 (GtkAssertDialogStackFrameCallback
)get_stackframe_callback
,
406 #endif // wxUSE_STACKWALKER
408 gint result
= gtk_dialog_run(GTK_DIALOG (dialog
));
409 bool returnCode
= false;
412 case GTK_ASSERT_DIALOG_STOP
:
415 case GTK_ASSERT_DIALOG_CONTINUE
:
418 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
:
424 wxFAIL_MSG( _T("unexpected return code from GtkAssertDialog") );
427 gtk_widget_destroy(dialog
);
429 #else // !wxDEBUG_LEVEL
430 // this function is never called in this case
433 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
436 wxString
wxGUIAppTraits::GetDesktopEnvironment() const
438 wxString de
= wxSystemOptions::GetOption(_T("gtk.desktop"));
442 static const wxString s_SM
= GetSM();
444 if (s_SM
== wxT("GnomeSM"))
446 else if (s_SM
== wxT("KDE"))
449 #endif // wxUSE_DETECT_SM
456 // see the hack below in wxCmdLineParser::GetUsageString().
457 // TODO: replace this hack with a g_option_group_get_entries()
458 // call as soon as such function exists;
459 // see http://bugzilla.gnome.org/show_bug.cgi?id=431021 for the relative
465 gchar
*help_description
;
467 GDestroyNotify destroy_notify
;
470 GTranslateFunc translate_func
;
471 GDestroyNotify translate_notify
;
472 gpointer translate_data
;
474 GOptionEntry
*entries
;
477 GOptionParseFunc pre_parse_func
;
478 GOptionParseFunc post_parse_func
;
479 GOptionErrorFunc error_func
;
482 wxString
wxGetNameFromGtkOptionEntry(const GOptionEntry
*opt
)
487 ret
<< _T("-") << opt
->short_name
;
492 ret
<< _T("--") << opt
->long_name
;
494 if (opt
->arg_description
)
495 ret
<< _T("=") << opt
->arg_description
;
498 return _T(" ") + ret
;
501 #endif // __WXGTK26__
504 wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString
& names
,
505 wxArrayString
& desc
) const
510 if (!gtk_check_version(2,6,0))
512 // since GTK>=2.6, we can use the glib_check_version() symbol...
514 // check whether GLib version is greater than 2.6 but also lower than 2.19
515 // because, as we use the undocumented _GOptionGroup struct, we don't want
516 // to run this code with future versions which might change it (2.19 is the
517 // latest one at the time of this writing)
518 if (!glib_check_version(2,6,0) && glib_check_version(2,20,0))
520 usage
<< _("The following standard GTK+ options are also supported:\n");
522 // passing true here means that the function can open the default
523 // display while parsing (not really used here anyhow)
524 GOptionGroup
*gtkOpts
= gtk_get_option_group(true);
526 // WARNING: here we access the internals of GOptionGroup:
527 GOptionEntry
*entries
= ((_GOptionGroup
*)gtkOpts
)->entries
;
528 unsigned int n_entries
= ((_GOptionGroup
*)gtkOpts
)->n_entries
;
529 wxArrayString namesOptions
, descOptions
;
531 for ( size_t n
= 0; n
< n_entries
; n
++ )
533 if ( entries
[n
].flags
& G_OPTION_FLAG_HIDDEN
)
536 names
.push_back(wxGetNameFromGtkOptionEntry(&entries
[n
]));
538 const gchar
* const entryDesc
= entries
[n
].description
;
539 desc
.push_back(wxString(entryDesc
));
542 g_option_group_free (gtkOpts
);
548 #endif // __WXGTK26__