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/sysopt.h"
22 #include "wx/apptrait.h"
23 #include "wx/process.h"
24 #include "wx/sysopt.h"
25 #include "wx/unix/execute.h"
27 #include "wx/gtk/private/timer.h"
28 #include "wx/evtloop.h"
31 #include "wx/gtk/assertdlg_gtk.h"
33 #include "wx/stackwalk.h"
34 #endif // wxUSE_STACKWALKER
40 #include <sys/types.h>
41 #include <sys/wait.h> // for WNOHANG
49 #ifdef HAVE_X11_XKBLIB_H
50 /* under HP-UX and Solaris 2.6, at least, XKBlib.h defines structures with
51 * field named "explicit" - which is, of course, an error for a C++
52 * compiler. To be on the safe side, just redefine it everywhere. */
53 #define explicit __wx_explicit
55 #include "X11/XKBlib.h"
58 #endif // HAVE_X11_XKBLIB_H
63 #include "X11/SM/SMlib.h"
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
70 extern GtkWidget
*wxGetRootWindow();
72 //----------------------------------------------------------------------------
74 //----------------------------------------------------------------------------
76 // on OS/2, we use the wxBell from wxBase library
84 /* Don't synthesize KeyUp events holding down a key and producing
85 KeyDown events with autorepeat. */
86 #ifdef HAVE_X11_XKBLIB_H
87 bool wxSetDetectableAutoRepeat( bool flag
)
90 XkbSetDetectableAutoRepeat( GDK_DISPLAY(), flag
, &result
);
91 return result
; /* true if keyboard hardware supports this mode */
94 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag
) )
100 // Escapes string so that it is valid Pango markup XML string:
101 wxString
wxEscapeStringForPangoMarkup(const wxString
& str
)
103 size_t len
= str
.length();
106 for (size_t i
= 0; i
< len
; i
++)
135 // ----------------------------------------------------------------------------
136 // display characterstics
137 // ----------------------------------------------------------------------------
141 return GDK_DISPLAY();
144 void wxDisplaySize( int *width
, int *height
)
146 int marginX
= wxSystemOptions::GetOptionInt(wxT("gtk.desktopmargin.x"));
147 int marginY
= wxSystemOptions::GetOptionInt(wxT("gtk.desktopmargin.y"));
149 if (width
) *width
= gdk_screen_width() - marginX
;
150 if (height
) *height
= gdk_screen_height() - marginY
;
153 void wxDisplaySizeMM( int *width
, int *height
)
155 if (width
) *width
= gdk_screen_width_mm();
156 if (height
) *height
= gdk_screen_height_mm();
159 void wxGetMousePosition( int* x
, int* y
)
161 gdk_window_get_pointer( (GdkWindow
*) NULL
, x
, y
, (GdkModifierType
*) NULL
);
164 bool wxColourDisplay()
171 return gdk_drawable_get_visual( wxGetRootWindow()->window
)->depth
;
174 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
176 return wxGenericFindWindowAtPoint(pt
);
181 WXDLLIMPEXP_CORE wxCharBuffer
182 wxConvertToGTK(const wxString
& s
, wxFontEncoding enc
)
185 if ( enc
== wxFONTENCODING_SYSTEM
|| enc
== wxFONTENCODING_DEFAULT
)
187 wbuf
= wxConvUI
->cMB2WC(s
.c_str());
189 else // another encoding, use generic conversion class
191 wbuf
= wxCSConv(enc
).cMB2WC(s
.c_str());
194 if ( !wbuf
&& !s
.empty() )
196 // conversion failed, but we still want to show something to the user
197 // even if it's going to be wrong it is better than nothing
199 // we choose ISO8859-1 here arbitrarily, it's just the most common
200 // encoding probably and, also importantly here, conversion from it
201 // never fails as it's done internally by wxCSConv
202 wbuf
= wxCSConv(wxFONTENCODING_ISO8859_1
).cMB2WC(s
.c_str());
205 return wxConvUTF8
.cWC2MB(wbuf
);
208 WXDLLIMPEXP_CORE wxCharBuffer
209 wxConvertFromGTK(const wxString
& s
, wxFontEncoding enc
)
211 // this conversion should never fail as GTK+ always uses UTF-8 internally
212 // so there are no complications here
213 const wxWCharBuffer
wbuf(wxConvUTF8
.cMB2WC(s
.c_str()));
214 if ( enc
== wxFONTENCODING_SYSTEM
)
215 return wxConvUI
->cWC2MB(wbuf
);
217 return wxCSConv(enc
).cWC2MB(wbuf
);
220 #endif // !wxUSE_UNICODE
222 // Returns false if version is certainly greater or equal than major.minor.micro
223 // Returns true if version is lower than major.minor.micro OR it cannot be
224 // determined and one should not rely on the availability of pango version
225 // major.minor.micro, nor the non-availability
226 const gchar
*wx_pango_version_check (int major
, int minor
, int micro
)
228 #ifdef PANGO_VERSION_MAJOR
229 if (!gtk_check_version (2,11,0))
231 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
232 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
233 // using has the pango_version_check function:
234 return pango_version_check (major
, minor
, micro
);
237 return "can't check";
238 #else // !PANGO_VERSION_MAJOR
243 return "too old headers";
248 // ----------------------------------------------------------------------------
249 // subprocess routines
250 // ----------------------------------------------------------------------------
254 void GTK_EndProcessDetector(gpointer data
, gint source
,
255 GdkInputCondition
WXUNUSED(condition
) )
257 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
259 // has the process really terminated? unfortunately GDK (or GLib) seem to
260 // generate G_IO_HUP notification even when it simply tries to read from a
261 // closed fd and hasn't terminated at all
262 int pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
264 int rc
= waitpid(pid
, &status
, WNOHANG
);
268 // no, it didn't exit yet, continue waiting
272 // set exit code to -1 if something bad happened
273 proc_data
->exitcode
= rc
!= -1 && WIFEXITED(status
) ? WEXITSTATUS(status
)
276 // child exited, end waiting
279 // don't call us again!
280 gdk_input_remove(proc_data
->tag
);
282 wxHandleProcessTermination(proc_data
);
286 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
288 int tag
= gdk_input_add(fd
,
290 GTK_EndProcessDetector
,
291 (gpointer
)proc_data
);
298 // ----------------------------------------------------------------------------
299 // wxPlatformInfo-related
300 // ----------------------------------------------------------------------------
302 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
305 *verMaj
= gtk_major_version
;
307 *verMin
= gtk_minor_version
;
314 wxTimerImpl
*wxGUIAppTraits::CreateTimerImpl(wxTimer
*timer
)
316 return new wxGTKTimerImpl(timer
);
319 #endif // wxUSE_TIMER
322 static wxString
GetSM()
327 Dpy() { m_dpy
= XOpenDisplay(NULL
); }
328 ~Dpy() { if ( m_dpy
) XCloseDisplay(m_dpy
); }
330 operator Display
*() const { return m_dpy
; }
336 return wxEmptyString
;
340 SmcConn smc_conn
= SmcOpenConnection(NULL
, NULL
,
342 0 /* mask */, NULL
/* callbacks */,
344 WXSIZEOF(smerr
), smerr
);
348 wxLogWarning(_("Failed to connect to session manager: %s"), smerr
);
349 return wxEmptyString
;
352 char *vendor
= SmcVendor(smc_conn
);
353 wxString ret
= wxString::FromAscii( vendor
);
356 SmcCloseConnection(smc_conn
, 0, NULL
);
361 #endif // wxUSE_DETECT_SM
364 //-----------------------------------------------------------------------------
366 //-----------------------------------------------------------------------------
368 wxEventLoopBase
*wxGUIAppTraits::CreateEventLoop()
370 return new wxEventLoop();
375 void wxGUIAppTraits::SetLocale()
378 wxUpdateLocaleIsUtf8();
384 #if wxUSE_STACKWALKER
386 // private helper class
387 class StackDump
: public wxStackWalker
390 StackDump(GtkAssertDialog
*dlg
) { m_dlg
=dlg
; }
393 virtual void OnStackFrame(const wxStackFrame
& frame
)
395 wxString fncname
= frame
.GetName();
396 wxString fncargs
= fncname
;
398 size_t n
= fncname
.find(wxT('('));
399 if (n
!= wxString::npos
)
401 // remove arguments from function name
404 // remove function name and brackets from arguments
405 fncargs
= fncargs
.substr(n
+1, fncargs
.length()-n
-2);
408 fncargs
= wxEmptyString
;
410 // append this stack frame's info in the dialog
411 if (!frame
.GetFileName().empty() || !fncname
.empty())
412 gtk_assert_dialog_append_stack_frame(m_dlg
,
415 frame
.GetFileName().mb_str(),
420 GtkAssertDialog
*m_dlg
;
423 // the callback functions must be extern "C" to comply with GTK+ declarations
426 void get_stackframe_callback(StackDump
*dump
)
428 // skip over frames up to including wxOnAssert()
429 dump
->ProcessFrames(3);
433 #endif // wxUSE_STACKWALKER
435 bool wxGUIAppTraits::ShowAssertDialog(const wxString
& msg
)
437 // under GTK2 we prefer to use a dialog widget written using directly GTK+;
438 // in fact we cannot use a dialog written using wxWidgets: it would need
439 // the wxWidgets idle processing to work correctly!
440 GtkWidget
*dialog
= gtk_assert_dialog_new();
441 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog
), msg
.mb_str());
443 #if wxUSE_STACKWALKER
444 // don't show more than maxLines or we could get a dialog too tall to be
445 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
446 // characters it is still only 300 pixels...
447 static const int maxLines
= 20;
449 // save current stack frame...
450 StackDump
dump(GTK_ASSERT_DIALOG(dialog
));
451 dump
.SaveStack(maxLines
);
453 // ...but process it only if the user needs it
454 gtk_assert_dialog_set_backtrace_callback(GTK_ASSERT_DIALOG(dialog
),
455 (GtkAssertDialogStackFrameCallback
)get_stackframe_callback
,
457 #endif // wxUSE_STACKWALKER
459 gint result
= gtk_dialog_run(GTK_DIALOG (dialog
));
460 bool returnCode
= false;
463 case GTK_ASSERT_DIALOG_STOP
:
466 case GTK_ASSERT_DIALOG_CONTINUE
:
469 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
:
475 wxFAIL_MSG( _T("unexpected return code from GtkAssertDialog") );
478 gtk_widget_destroy(dialog
);
482 #endif // __WXDEBUG__
484 wxString
wxGUIAppTraits::GetDesktopEnvironment() const
486 wxString de
= wxSystemOptions::GetOption(_T("gtk.desktop"));
490 static const wxString s_SM
= GetSM();
492 if (s_SM
== wxT("GnomeSM"))
494 else if (s_SM
== wxT("KDE"))
497 #endif // wxUSE_DETECT_SM
504 // see the hack below in wxCmdLineParser::GetUsageString().
505 // TODO: replace this hack with a g_option_group_get_entries()
506 // call as soon as such function exists
511 gchar
*help_description
;
513 GDestroyNotify destroy_notify
;
516 GTranslateFunc translate_func
;
517 GDestroyNotify translate_notify
;
518 gpointer translate_data
;
520 GOptionEntry
*entries
;
523 GOptionParseFunc pre_parse_func
;
524 GOptionParseFunc post_parse_func
;
525 GOptionErrorFunc error_func
;
528 wxString
wxGetNameFromGtkOptionEntry(const GOptionEntry
*opt
)
533 ret
<< _T("-") << opt
->short_name
;
538 ret
<< _T("--") << opt
->long_name
;
540 if (opt
->arg_description
)
541 ret
<< _T("=") << opt
->arg_description
;
544 return _T(" ") + ret
;
547 #endif // __WXGTK26__
550 wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString
& names
,
551 wxArrayString
& desc
) const
556 // check whether GTK version is greater than 2.6 but also lower than 2.12
557 // because, as we use the undocumented _GOptionGroup struct, we don't want
558 // to run this code with future versions which might change it (2.11 is the
559 // latest one at the time of this writing)
560 if (!gtk_check_version(2,6,0) &&
561 gtk_check_version(2,12,0))
563 usage
<< _("The following standard GTK+ options are also supported:\n");
565 // passing true here means that the function can open the default
566 // display while parsing (not really used here anyhow)
567 GOptionGroup
*gtkOpts
= gtk_get_option_group(true);
569 // WARNING: here we access the internals of GOptionGroup:
570 GOptionEntry
*entries
= ((_GOptionGroup
*)gtkOpts
)->entries
;
571 unsigned int n_entries
= ((_GOptionGroup
*)gtkOpts
)->n_entries
;
572 wxArrayString namesOptions
, descOptions
;
574 for ( size_t n
= 0; n
< n_entries
; n
++ )
576 if ( entries
[n
].flags
& G_OPTION_FLAG_HIDDEN
)
579 names
.push_back(wxGetNameFromGtkOptionEntry(&entries
[n
]));
581 const gchar
* const entryDesc
= entries
[n
].description
;
582 desc
.push_back(entryDesc
? wxString(entryDesc
) : _T(""));
585 g_option_group_free (gtkOpts
);
590 #endif // __WXGTK26__