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
50 #include "X11/SM/SMlib.h"
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 extern GtkWidget
*wxGetRootWindow();
59 //----------------------------------------------------------------------------
61 //----------------------------------------------------------------------------
63 // on OS/2, we use the wxBell from wxBase library
71 // ----------------------------------------------------------------------------
72 // display characterstics
73 // ----------------------------------------------------------------------------
80 void wxDisplaySize( int *width
, int *height
)
82 if (width
) *width
= gdk_screen_width();
83 if (height
) *height
= gdk_screen_height();
86 void wxDisplaySizeMM( int *width
, int *height
)
88 if (width
) *width
= gdk_screen_width_mm();
89 if (height
) *height
= gdk_screen_height_mm();
92 void wxGetMousePosition( int* x
, int* y
)
94 gdk_window_get_pointer( NULL
, x
, y
, NULL
);
97 bool wxColourDisplay()
104 return gdk_drawable_get_visual( wxGetRootWindow()->window
)->depth
;
107 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
109 return wxGenericFindWindowAtPoint(pt
);
114 WXDLLIMPEXP_CORE wxCharBuffer
115 wxConvertToGTK(const wxString
& s
, wxFontEncoding enc
)
118 if ( enc
== wxFONTENCODING_SYSTEM
|| enc
== wxFONTENCODING_DEFAULT
)
120 wbuf
= wxConvUI
->cMB2WC(s
.c_str());
122 else // another encoding, use generic conversion class
124 wbuf
= wxCSConv(enc
).cMB2WC(s
.c_str());
127 if ( !wbuf
&& !s
.empty() )
129 // conversion failed, but we still want to show something to the user
130 // even if it's going to be wrong it is better than nothing
132 // we choose ISO8859-1 here arbitrarily, it's just the most common
133 // encoding probably and, also importantly here, conversion from it
134 // never fails as it's done internally by wxCSConv
135 wbuf
= wxCSConv(wxFONTENCODING_ISO8859_1
).cMB2WC(s
.c_str());
138 return wxConvUTF8
.cWC2MB(wbuf
);
141 WXDLLIMPEXP_CORE wxCharBuffer
142 wxConvertFromGTK(const wxString
& s
, wxFontEncoding enc
)
144 // this conversion should never fail as GTK+ always uses UTF-8 internally
145 // so there are no complications here
146 const wxWCharBuffer
wbuf(wxConvUTF8
.cMB2WC(s
.c_str()));
147 if ( enc
== wxFONTENCODING_SYSTEM
)
148 return wxConvUI
->cWC2MB(wbuf
);
150 return wxCSConv(enc
).cWC2MB(wbuf
);
153 #endif // !wxUSE_UNICODE
155 // Returns NULL if version is certainly greater or equal than major.minor.micro
156 // Returns string describing the error if version is lower than
157 // major.minor.micro OR it cannot be determined and one should not rely on the
158 // availability of pango version major.minor.micro, nor the non-availability
159 const gchar
*wx_pango_version_check (int major
, int minor
, int micro
)
161 // NOTE: you don't need to use this macro to check for Pango features
162 // added in pango-1.4 or earlier since GTK 2.4 (our minimum requirement
163 // for GTK lib) required pango 1.4...
165 #ifdef PANGO_VERSION_MAJOR
166 if (!gtk_check_version (2,11,0))
168 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
169 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
170 // using has the pango_version_check function:
171 return pango_version_check (major
, minor
, micro
);
174 return "can't check";
175 #else // !PANGO_VERSION_MAJOR
180 return "too old headers";
185 // ----------------------------------------------------------------------------
186 // subprocess routines
187 // ----------------------------------------------------------------------------
191 void GTK_EndProcessDetector(gpointer data
, gint source
,
192 GdkInputCondition
WXUNUSED(condition
))
194 wxEndProcessData
* const
195 proc_data
= static_cast<wxEndProcessData
*>(data
);
197 // child exited, end waiting
200 // don't call us again!
201 gdk_input_remove(proc_data
->tag
);
203 wxHandleProcessTermination(proc_data
);
207 int wxGUIAppTraits::AddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
209 int tag
= gdk_input_add(fd
,
211 GTK_EndProcessDetector
,
212 (gpointer
)proc_data
);
219 // ----------------------------------------------------------------------------
220 // wxPlatformInfo-related
221 // ----------------------------------------------------------------------------
223 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
226 *verMaj
= gtk_major_version
;
228 *verMin
= gtk_minor_version
;
235 wxTimerImpl
*wxGUIAppTraits::CreateTimerImpl(wxTimer
*timer
)
237 return new wxGTKTimerImpl(timer
);
240 #endif // wxUSE_TIMER
243 static wxString
GetSM()
248 Dpy() { m_dpy
= XOpenDisplay(NULL
); }
249 ~Dpy() { if ( m_dpy
) XCloseDisplay(m_dpy
); }
251 operator Display
*() const { return m_dpy
; }
257 return wxEmptyString
;
261 SmcConn smc_conn
= SmcOpenConnection(NULL
, NULL
,
263 0 /* mask */, NULL
/* callbacks */,
265 WXSIZEOF(smerr
), smerr
);
269 wxLogDebug("Failed to connect to session manager: %s", smerr
);
270 return wxEmptyString
;
273 char *vendor
= SmcVendor(smc_conn
);
274 wxString ret
= wxString::FromAscii( vendor
);
277 SmcCloseConnection(smc_conn
, 0, NULL
);
282 #endif // wxUSE_DETECT_SM
285 //-----------------------------------------------------------------------------
287 //-----------------------------------------------------------------------------
289 wxEventLoopBase
*wxGUIAppTraits::CreateEventLoop()
291 return new wxEventLoop();
296 void wxGUIAppTraits::SetLocale()
299 wxUpdateLocaleIsUtf8();
303 #if wxDEBUG_LEVEL && wxUSE_STACKWALKER
305 // private helper class
306 class StackDump
: public wxStackWalker
309 StackDump(GtkAssertDialog
*dlg
) { m_dlg
=dlg
; }
312 virtual void OnStackFrame(const wxStackFrame
& frame
)
314 wxString fncname
= frame
.GetName();
315 wxString fncargs
= fncname
;
317 size_t n
= fncname
.find(wxT('('));
318 if (n
!= wxString::npos
)
320 // remove arguments from function name
323 // remove function name and brackets from arguments
324 fncargs
= fncargs
.substr(n
+1, fncargs
.length()-n
-2);
327 fncargs
= wxEmptyString
;
329 // append this stack frame's info in the dialog
330 if (!frame
.GetFileName().empty() || !fncname
.empty())
331 gtk_assert_dialog_append_stack_frame(m_dlg
,
334 frame
.GetFileName().mb_str(),
339 GtkAssertDialog
*m_dlg
;
342 // the callback functions must be extern "C" to comply with GTK+ declarations
345 void get_stackframe_callback(StackDump
*dump
)
347 // skip over frames up to including wxOnAssert()
348 dump
->ProcessFrames(3);
352 #endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
354 bool wxGUIAppTraits::ShowAssertDialog(const wxString
& msg
)
357 // we can't show the dialog from another thread
358 if ( wxIsMainThread() )
360 // under GTK2 we prefer to use a dialog widget written using directly
361 // in GTK+ as use a dialog written using wxWidgets would need the
362 // wxWidgets idle processing to work correctly which might not be the
363 // case when assert happens
364 GtkWidget
*dialog
= gtk_assert_dialog_new();
365 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog
), msg
.mb_str());
367 #if wxUSE_STACKWALKER
368 // save the current stack ow...
369 StackDump
dump(GTK_ASSERT_DIALOG(dialog
));
370 dump
.SaveStack(100); // showing more than 100 frames is not very useful
372 // ...but process it only if the user needs it
373 gtk_assert_dialog_set_backtrace_callback
375 GTK_ASSERT_DIALOG(dialog
),
376 (GtkAssertDialogStackFrameCallback
)get_stackframe_callback
,
379 #endif // wxUSE_STACKWALKER
381 gint result
= gtk_dialog_run(GTK_DIALOG (dialog
));
382 bool returnCode
= false;
385 case GTK_ASSERT_DIALOG_STOP
:
388 case GTK_ASSERT_DIALOG_CONTINUE
:
391 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
:
397 wxFAIL_MSG( wxT("unexpected return code from GtkAssertDialog") );
400 gtk_widget_destroy(dialog
);
403 #endif // wxDEBUG_LEVEL
405 return wxAppTraitsBase::ShowAssertDialog(msg
);
408 wxString
wxGUIAppTraits::GetDesktopEnvironment() const
410 wxString de
= wxSystemOptions::GetOption(wxT("gtk.desktop"));
414 static const wxString s_SM
= GetSM();
416 if (s_SM
== wxT("GnomeSM"))
418 else if (s_SM
== wxT("KDE"))
421 #endif // wxUSE_DETECT_SM
428 // see the hack below in wxCmdLineParser::GetUsageString().
429 // TODO: replace this hack with a g_option_group_get_entries()
430 // call as soon as such function exists;
431 // see http://bugzilla.gnome.org/show_bug.cgi?id=431021 for the relative
437 gchar
*help_description
;
439 GDestroyNotify destroy_notify
;
442 GTranslateFunc translate_func
;
443 GDestroyNotify translate_notify
;
444 gpointer translate_data
;
446 GOptionEntry
*entries
;
449 GOptionParseFunc pre_parse_func
;
450 GOptionParseFunc post_parse_func
;
451 GOptionErrorFunc error_func
;
454 wxString
wxGetNameFromGtkOptionEntry(const GOptionEntry
*opt
)
459 ret
<< wxT("-") << opt
->short_name
;
464 ret
<< wxT("--") << opt
->long_name
;
466 if (opt
->arg_description
)
467 ret
<< wxT("=") << opt
->arg_description
;
470 return wxT(" ") + ret
;
473 #endif // __WXGTK26__
476 wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString
& names
,
477 wxArrayString
& desc
) const
482 if (!gtk_check_version(2,6,0))
484 // since GTK>=2.6, we can use the glib_check_version() symbol...
486 // check whether GLib version is greater than 2.6 but also lower than 2.19
487 // because, as we use the undocumented _GOptionGroup struct, we don't want
488 // to run this code with future versions which might change it (2.19 is the
489 // latest one at the time of this writing)
490 if (!glib_check_version(2,6,0) && glib_check_version(2,20,0))
492 usage
<< _("The following standard GTK+ options are also supported:\n");
494 // passing true here means that the function can open the default
495 // display while parsing (not really used here anyhow)
496 GOptionGroup
*gtkOpts
= gtk_get_option_group(true);
498 // WARNING: here we access the internals of GOptionGroup:
499 GOptionEntry
*entries
= ((_GOptionGroup
*)gtkOpts
)->entries
;
500 unsigned int n_entries
= ((_GOptionGroup
*)gtkOpts
)->n_entries
;
501 wxArrayString namesOptions
, descOptions
;
503 for ( size_t n
= 0; n
< n_entries
; n
++ )
505 if ( entries
[n
].flags
& G_OPTION_FLAG_HIDDEN
)
508 names
.push_back(wxGetNameFromGtkOptionEntry(&entries
[n
]));
510 const gchar
* const entryDesc
= entries
[n
].description
;
511 desc
.push_back(wxString(entryDesc
));
514 g_option_group_free (gtkOpts
);
520 #endif // __WXGTK26__