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"
24 #include "wx/sysopt.h"
26 #include "wx/unix/execute.h"
28 #include "wx/gtk/private/timer.h"
29 #include "wx/evtloop.h"
32 #include "wx/gtk/assertdlg_gtk.h"
34 #include "wx/stackwalk.h"
35 #endif // wxUSE_STACKWALKER
41 #include <sys/types.h>
42 #include <sys/wait.h> // for WNOHANG
50 #ifdef HAVE_X11_XKBLIB_H
51 /* under HP-UX and Solaris 2.6, at least, XKBlib.h defines structures with
52 * field named "explicit" - which is, of course, an error for a C++
53 * compiler. To be on the safe side, just redefine it everywhere. */
54 #define explicit __wx_explicit
56 #include "X11/XKBlib.h"
59 #endif // HAVE_X11_XKBLIB_H
64 #include "X11/SM/SMlib.h"
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 extern GtkWidget
*wxGetRootWindow();
73 //----------------------------------------------------------------------------
75 //----------------------------------------------------------------------------
77 // on OS/2, we use the wxBell from wxBase library
85 /* Don't synthesize KeyUp events holding down a key and producing
86 KeyDown events with autorepeat. */
87 #ifdef HAVE_X11_XKBLIB_H
88 bool wxSetDetectableAutoRepeat( bool flag
)
91 XkbSetDetectableAutoRepeat( GDK_DISPLAY(), flag
, &result
);
92 return result
; /* true if keyboard hardware supports this mode */
95 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag
) )
101 // Escapes string so that it is valid Pango markup XML string:
102 wxString
wxEscapeStringForPangoMarkup(const wxString
& str
)
104 size_t len
= str
.length();
107 for (size_t i
= 0; i
< len
; i
++)
136 // ----------------------------------------------------------------------------
137 // display characterstics
138 // ----------------------------------------------------------------------------
142 return GDK_DISPLAY();
145 void wxDisplaySize( int *width
, int *height
)
147 if (width
) *width
= gdk_screen_width();
148 if (height
) *height
= gdk_screen_height();
151 void wxDisplaySizeMM( int *width
, int *height
)
153 if (width
) *width
= gdk_screen_width_mm();
154 if (height
) *height
= gdk_screen_height_mm();
157 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
159 // This is supposed to return desktop dimensions minus any window
160 // manager panels, menus, taskbars, etc. If there is a way to do that
161 // for this platform please fix this function, otherwise it defaults
162 // to the entire desktop.
165 wxDisplaySize(width
, height
);
168 void wxGetMousePosition( int* x
, int* y
)
170 gdk_window_get_pointer( (GdkWindow
*) NULL
, x
, y
, (GdkModifierType
*) NULL
);
173 bool wxColourDisplay()
180 return gdk_drawable_get_visual( wxGetRootWindow()->window
)->depth
;
183 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
185 return wxGenericFindWindowAtPoint(pt
);
190 WXDLLIMPEXP_CORE wxCharBuffer
191 wxConvertToGTK(const wxString
& s
, wxFontEncoding enc
)
194 if ( enc
== wxFONTENCODING_SYSTEM
|| enc
== wxFONTENCODING_DEFAULT
)
196 wbuf
= wxConvUI
->cMB2WC(s
.c_str());
198 else // another encoding, use generic conversion class
200 wbuf
= wxCSConv(enc
).cMB2WC(s
.c_str());
203 if ( !wbuf
&& !s
.empty() )
205 // conversion failed, but we still want to show something to the user
206 // even if it's going to be wrong it is better than nothing
208 // we choose ISO8859-1 here arbitrarily, it's just the most common
209 // encoding probably and, also importantly here, conversion from it
210 // never fails as it's done internally by wxCSConv
211 wbuf
= wxCSConv(wxFONTENCODING_ISO8859_1
).cMB2WC(s
.c_str());
214 return wxConvUTF8
.cWC2MB(wbuf
);
217 WXDLLIMPEXP_CORE wxCharBuffer
218 wxConvertFromGTK(const wxString
& s
, wxFontEncoding enc
)
220 // this conversion should never fail as GTK+ always uses UTF-8 internally
221 // so there are no complications here
222 const wxWCharBuffer
wbuf(wxConvUTF8
.cMB2WC(s
.c_str()));
223 if ( enc
== wxFONTENCODING_SYSTEM
)
224 return wxConvUI
->cWC2MB(wbuf
);
226 return wxCSConv(enc
).cWC2MB(wbuf
);
229 #endif // !wxUSE_UNICODE
231 // Returns false if version is certainly greater or equal than major.minor.micro
232 // Returns true if version is lower than major.minor.micro OR it cannot be
233 // determined and one should not rely on the availability of pango version
234 // major.minor.micro, nor the non-availability
235 const gchar
*wx_pango_version_check (int major
, int minor
, int micro
)
237 #ifdef PANGO_VERSION_MAJOR
238 if (!gtk_check_version (2,11,0))
240 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
241 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
242 // using has the pango_version_check function:
243 return pango_version_check (major
, minor
, micro
);
246 return "can't check";
247 #else // !PANGO_VERSION_MAJOR
252 return "too old headers";
257 // ----------------------------------------------------------------------------
258 // subprocess routines
259 // ----------------------------------------------------------------------------
263 void GTK_EndProcessDetector(gpointer data
, gint source
,
264 GdkInputCondition
WXUNUSED(condition
) )
266 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
268 // has the process really terminated? unfortunately GDK (or GLib) seem to
269 // generate G_IO_HUP notification even when it simply tries to read from a
270 // closed fd and hasn't terminated at all
271 int pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
273 int rc
= waitpid(pid
, &status
, WNOHANG
);
277 // no, it didn't exit yet, continue waiting
281 // set exit code to -1 if something bad happened
282 proc_data
->exitcode
= rc
!= -1 && WIFEXITED(status
) ? WEXITSTATUS(status
)
285 // child exited, end waiting
288 // don't call us again!
289 gdk_input_remove(proc_data
->tag
);
291 wxHandleProcessTermination(proc_data
);
295 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
297 int tag
= gdk_input_add(fd
,
299 GTK_EndProcessDetector
,
300 (gpointer
)proc_data
);
307 // ----------------------------------------------------------------------------
308 // wxPlatformInfo-related
309 // ----------------------------------------------------------------------------
311 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
314 *verMaj
= gtk_major_version
;
316 *verMin
= gtk_minor_version
;
323 wxTimerImpl
*wxGUIAppTraits::CreateTimerImpl(wxTimer
*timer
)
325 return new wxGTKTimerImpl(timer
);
328 #endif // wxUSE_TIMER
331 static wxString
GetSM()
336 Dpy() { m_dpy
= XOpenDisplay(NULL
); }
337 ~Dpy() { if ( m_dpy
) XCloseDisplay(m_dpy
); }
339 operator Display
*() const { return m_dpy
; }
345 return wxEmptyString
;
349 SmcConn smc_conn
= SmcOpenConnection(NULL
, NULL
,
351 0 /* mask */, NULL
/* callbacks */,
353 WXSIZEOF(smerr
), smerr
);
357 wxLogWarning(_("Failed to connect to session manager: %s"), smerr
);
358 return wxEmptyString
;
361 char *vendor
= SmcVendor(smc_conn
);
362 wxString ret
= wxString::FromAscii( vendor
);
365 SmcCloseConnection(smc_conn
, 0, NULL
);
370 #endif // wxUSE_DETECT_SM
373 //-----------------------------------------------------------------------------
375 //-----------------------------------------------------------------------------
377 wxEventLoopBase
*wxGUIAppTraits::CreateEventLoop()
379 return new wxEventLoop();
384 void wxGUIAppTraits::SetLocale()
387 wxUpdateLocaleIsUtf8();
393 #if wxUSE_STACKWALKER
395 // private helper class
396 class StackDump
: public wxStackWalker
399 StackDump(GtkAssertDialog
*dlg
) { m_dlg
=dlg
; }
402 virtual void OnStackFrame(const wxStackFrame
& frame
)
404 wxString fncname
= frame
.GetName();
405 wxString fncargs
= fncname
;
407 size_t n
= fncname
.find(wxT('('));
408 if (n
!= wxString::npos
)
410 // remove arguments from function name
413 // remove function name and brackets from arguments
414 fncargs
= fncargs
.substr(n
+1, fncargs
.length()-n
-2);
417 fncargs
= wxEmptyString
;
419 // append this stack frame's info in the dialog
420 if (!frame
.GetFileName().empty() || !fncname
.empty())
421 gtk_assert_dialog_append_stack_frame(m_dlg
,
424 frame
.GetFileName().mb_str(),
429 GtkAssertDialog
*m_dlg
;
432 // the callback functions must be extern "C" to comply with GTK+ declarations
435 void get_stackframe_callback(StackDump
*dump
)
437 // skip over frames up to including wxOnAssert()
438 dump
->ProcessFrames(3);
442 #endif // wxUSE_STACKWALKER
444 bool wxGUIAppTraits::ShowAssertDialog(const wxString
& msg
)
446 // under GTK2 we prefer to use a dialog widget written using directly GTK+;
447 // in fact we cannot use a dialog written using wxWidgets: it would need
448 // the wxWidgets idle processing to work correctly!
449 GtkWidget
*dialog
= gtk_assert_dialog_new();
450 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog
), msg
.mb_str());
452 #if wxUSE_STACKWALKER
453 // don't show more than maxLines or we could get a dialog too tall to be
454 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
455 // characters it is still only 300 pixels...
456 static const int maxLines
= 20;
458 // save current stack frame...
459 StackDump
dump(GTK_ASSERT_DIALOG(dialog
));
460 dump
.SaveStack(maxLines
);
462 // ...but process it only if the user needs it
463 gtk_assert_dialog_set_backtrace_callback(GTK_ASSERT_DIALOG(dialog
),
464 (GtkAssertDialogStackFrameCallback
)get_stackframe_callback
,
466 #endif // wxUSE_STACKWALKER
468 gint result
= gtk_dialog_run(GTK_DIALOG (dialog
));
469 bool returnCode
= false;
472 case GTK_ASSERT_DIALOG_STOP
:
475 case GTK_ASSERT_DIALOG_CONTINUE
:
478 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
:
484 wxFAIL_MSG( _T("unexpected return code from GtkAssertDialog") );
487 gtk_widget_destroy(dialog
);
491 #endif // __WXDEBUG__
493 wxString
wxGUIAppTraits::GetDesktopEnvironment() const
495 wxString de
= wxSystemOptions::GetOption(_T("gtk.desktop"));
499 static const wxString s_SM
= GetSM();
501 if (s_SM
== wxT("GnomeSM"))
503 else if (s_SM
== wxT("KDE"))
506 #endif // wxUSE_DETECT_SM
513 // see the hack below in wxCmdLineParser::GetUsageString().
514 // TODO: replace this hack with a g_option_group_get_entries()
515 // call as soon as such function exists
520 gchar
*help_description
;
522 GDestroyNotify destroy_notify
;
525 GTranslateFunc translate_func
;
526 GDestroyNotify translate_notify
;
527 gpointer translate_data
;
529 GOptionEntry
*entries
;
532 GOptionParseFunc pre_parse_func
;
533 GOptionParseFunc post_parse_func
;
534 GOptionErrorFunc error_func
;
537 wxString
wxGetNameFromGtkOptionEntry(const GOptionEntry
*opt
)
542 ret
<< _T("-") << opt
->short_name
;
547 ret
<< _T("--") << opt
->long_name
;
549 if (opt
->arg_description
)
550 ret
<< _T("=") << opt
->arg_description
;
553 return _T(" ") + ret
;
556 #endif // __WXGTK26__
559 wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString
& names
,
560 wxArrayString
& desc
) const
565 // check whether GTK version is greater than 2.6 but also lower than 2.12
566 // because, as we use the undocumented _GOptionGroup struct, we don't want
567 // to run this code with future versions which might change it (2.11 is the
568 // latest one at the time of this writing)
569 if (!gtk_check_version(2,6,0) &&
570 gtk_check_version(2,12,0))
572 usage
<< _("The following standard GTK+ options are also supported:\n");
574 // passing true here means that the function can open the default
575 // display while parsing (not really used here anyhow)
576 GOptionGroup
*gtkOpts
= gtk_get_option_group(true);
578 // WARNING: here we access the internals of GOptionGroup:
579 GOptionEntry
*entries
= ((_GOptionGroup
*)gtkOpts
)->entries
;
580 unsigned int n_entries
= ((_GOptionGroup
*)gtkOpts
)->n_entries
;
581 wxArrayString namesOptions
, descOptions
;
583 for ( size_t n
= 0; n
< n_entries
; n
++ )
585 if ( entries
[n
].flags
& G_OPTION_FLAG_HIDDEN
)
588 names
.push_back(wxGetNameFromGtkOptionEntry(&entries
[n
]));
590 const gchar
* const entryDesc
= entries
[n
].description
;
591 desc
.push_back(entryDesc
? wxString(entryDesc
) : _T(""));
594 g_option_group_free (gtkOpts
);
599 #endif // __WXGTK26__