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"
33 #include "wx/gtk/assertdlg_gtk.h"
35 #include "wx/stackwalk.h"
36 #endif // wxUSE_STACKWALKER
37 #endif // wxDEBUG_LEVEL
42 #include <sys/types.h>
43 #include <sys/wait.h> // for WNOHANG
47 #include <X11/SM/SMlib.h>
49 #include "wx/unix/utilsx11.h"
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 extern GtkWidget
*wxGetRootWindow();
58 //----------------------------------------------------------------------------
60 //----------------------------------------------------------------------------
62 // on OS/2, we use the wxBell from wxBase library
70 // ----------------------------------------------------------------------------
71 // display characterstics
72 // ----------------------------------------------------------------------------
79 void wxDisplaySize( int *width
, int *height
)
81 if (width
) *width
= gdk_screen_width();
82 if (height
) *height
= gdk_screen_height();
85 void wxDisplaySizeMM( int *width
, int *height
)
87 if (width
) *width
= gdk_screen_width_mm();
88 if (height
) *height
= gdk_screen_height_mm();
91 void wxGetMousePosition( int* x
, int* y
)
93 gdk_window_get_pointer( NULL
, x
, y
, NULL
);
96 bool wxColourDisplay()
103 return gtk_widget_get_visual(wxGetRootWindow())->depth
;
106 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
108 return wxGenericFindWindowAtPoint(pt
);
113 WXDLLIMPEXP_CORE wxCharBuffer
114 wxConvertToGTK(const wxString
& s
, wxFontEncoding enc
)
117 if ( enc
== wxFONTENCODING_SYSTEM
|| enc
== wxFONTENCODING_DEFAULT
)
119 wbuf
= wxConvUI
->cMB2WC(s
.c_str());
121 else // another encoding, use generic conversion class
123 wbuf
= wxCSConv(enc
).cMB2WC(s
.c_str());
126 if ( !wbuf
&& !s
.empty() )
128 // conversion failed, but we still want to show something to the user
129 // even if it's going to be wrong it is better than nothing
131 // we choose ISO8859-1 here arbitrarily, it's just the most common
132 // encoding probably and, also importantly here, conversion from it
133 // never fails as it's done internally by wxCSConv
134 wbuf
= wxCSConv(wxFONTENCODING_ISO8859_1
).cMB2WC(s
.c_str());
137 return wxConvUTF8
.cWC2MB(wbuf
);
140 WXDLLIMPEXP_CORE wxCharBuffer
141 wxConvertFromGTK(const wxString
& s
, wxFontEncoding enc
)
143 // this conversion should never fail as GTK+ always uses UTF-8 internally
144 // so there are no complications here
145 const wxWCharBuffer
wbuf(wxConvUTF8
.cMB2WC(s
.c_str()));
146 if ( enc
== wxFONTENCODING_SYSTEM
)
147 return wxConvUI
->cWC2MB(wbuf
);
149 return wxCSConv(enc
).cWC2MB(wbuf
);
152 #endif // !wxUSE_UNICODE
154 // Returns NULL if version is certainly greater or equal than major.minor.micro
155 // Returns string describing the error if version is lower than
156 // major.minor.micro OR it cannot be determined and one should not rely on the
157 // availability of pango version major.minor.micro, nor the non-availability
158 const gchar
*wx_pango_version_check (int major
, int minor
, int micro
)
160 // NOTE: you don't need to use this macro to check for Pango features
161 // added in pango-1.4 or earlier since GTK 2.4 (our minimum requirement
162 // for GTK lib) required pango 1.4...
164 #ifdef PANGO_VERSION_MAJOR
165 if (!gtk_check_version (2,11,0))
167 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
168 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
169 // using has the pango_version_check function:
170 return pango_version_check (major
, minor
, micro
);
173 return "can't check";
174 #else // !PANGO_VERSION_MAJOR
179 return "too old headers";
184 // ----------------------------------------------------------------------------
185 // subprocess routines
186 // ----------------------------------------------------------------------------
189 static gboolean
EndProcessDetector(GIOChannel
* source
, GIOCondition
, void* data
)
191 wxEndProcessData
* const
192 proc_data
= static_cast<wxEndProcessData
*>(data
);
194 // child exited, end waiting
195 close(g_io_channel_unix_get_fd(source
));
197 wxHandleProcessTermination(proc_data
);
199 // don't call us again!
204 int wxGUIAppTraits::AddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
206 GIOChannel
* channel
= g_io_channel_unix_new(fd
);
207 GIOCondition cond
= GIOCondition(G_IO_IN
| G_IO_HUP
| G_IO_ERR
);
208 unsigned id
= g_io_add_watch(channel
, cond
, EndProcessDetector
, proc_data
);
209 g_io_channel_unref(channel
);
215 // ----------------------------------------------------------------------------
216 // wxPlatformInfo-related
217 // ----------------------------------------------------------------------------
219 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
222 *verMaj
= gtk_major_version
;
224 *verMin
= gtk_minor_version
;
231 wxTimerImpl
*wxGUIAppTraits::CreateTimerImpl(wxTimer
*timer
)
233 return new wxGTKTimerImpl(timer
);
236 #endif // wxUSE_TIMER
239 static wxString
GetSM()
243 return wxEmptyString
;
247 SmcConn smc_conn
= SmcOpenConnection(NULL
, NULL
,
249 0 /* mask */, NULL
/* callbacks */,
251 WXSIZEOF(smerr
), smerr
);
255 wxLogDebug("Failed to connect to session manager: %s", smerr
);
256 return wxEmptyString
;
259 char *vendor
= SmcVendor(smc_conn
);
260 wxString ret
= wxString::FromAscii( vendor
);
263 SmcCloseConnection(smc_conn
, 0, NULL
);
268 #endif // wxUSE_DETECT_SM
271 //-----------------------------------------------------------------------------
273 //-----------------------------------------------------------------------------
275 wxEventLoopBase
*wxGUIAppTraits::CreateEventLoop()
277 return new wxEventLoop();
282 void wxGUIAppTraits::SetLocale()
285 wxUpdateLocaleIsUtf8();
289 #if wxDEBUG_LEVEL && wxUSE_STACKWALKER
291 // private helper class
292 class StackDump
: public wxStackWalker
295 StackDump(GtkAssertDialog
*dlg
) { m_dlg
=dlg
; }
298 virtual void OnStackFrame(const wxStackFrame
& frame
)
300 wxString fncname
= frame
.GetName();
301 wxString fncargs
= fncname
;
303 size_t n
= fncname
.find(wxT('('));
304 if (n
!= wxString::npos
)
306 // remove arguments from function name
309 // remove function name and brackets from arguments
310 fncargs
= fncargs
.substr(n
+1, fncargs
.length()-n
-2);
313 fncargs
= wxEmptyString
;
315 // append this stack frame's info in the dialog
316 if (!frame
.GetFileName().empty() || !fncname
.empty())
317 gtk_assert_dialog_append_stack_frame(m_dlg
,
320 frame
.GetFileName().mb_str(),
325 GtkAssertDialog
*m_dlg
;
328 static void get_stackframe_callback(void* p
)
330 StackDump
* dump
= static_cast<StackDump
*>(p
);
331 // skip over frames up to including wxOnAssert()
332 dump
->ProcessFrames(3);
335 #endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
337 bool wxGUIAppTraits::ShowAssertDialog(const wxString
& msg
)
340 // we can't show the dialog from another thread
341 if ( wxIsMainThread() )
343 // under GTK2 we prefer to use a dialog widget written using directly
344 // in GTK+ as use a dialog written using wxWidgets would need the
345 // wxWidgets idle processing to work correctly which might not be the
346 // case when assert happens
347 GtkWidget
*dialog
= gtk_assert_dialog_new();
348 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog
), msg
.mb_str());
350 #if wxUSE_STACKWALKER
351 // save the current stack ow...
352 StackDump
dump(GTK_ASSERT_DIALOG(dialog
));
353 dump
.SaveStack(100); // showing more than 100 frames is not very useful
355 // ...but process it only if the user needs it
356 gtk_assert_dialog_set_backtrace_callback
358 GTK_ASSERT_DIALOG(dialog
),
359 get_stackframe_callback
,
362 #endif // wxUSE_STACKWALKER
364 gint result
= gtk_dialog_run(GTK_DIALOG (dialog
));
365 bool returnCode
= false;
368 case GTK_ASSERT_DIALOG_STOP
:
371 case GTK_ASSERT_DIALOG_CONTINUE
:
374 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
:
380 wxFAIL_MSG( wxT("unexpected return code from GtkAssertDialog") );
383 gtk_widget_destroy(dialog
);
386 #endif // wxDEBUG_LEVEL
388 return wxAppTraitsBase::ShowAssertDialog(msg
);
391 wxString
wxGUIAppTraits::GetDesktopEnvironment() const
393 wxString de
= wxSystemOptions::GetOption(wxT("gtk.desktop"));
397 static const wxString s_SM
= GetSM();
399 if (s_SM
== wxT("GnomeSM"))
401 else if (s_SM
== wxT("KDE"))
404 #endif // wxUSE_DETECT_SM
411 // see the hack below in wxCmdLineParser::GetUsageString().
412 // TODO: replace this hack with a g_option_group_get_entries()
413 // call as soon as such function exists;
414 // see http://bugzilla.gnome.org/show_bug.cgi?id=431021 for the relative
420 gchar
*help_description
;
422 GDestroyNotify destroy_notify
;
425 GTranslateFunc translate_func
;
426 GDestroyNotify translate_notify
;
427 gpointer translate_data
;
429 GOptionEntry
*entries
;
432 GOptionParseFunc pre_parse_func
;
433 GOptionParseFunc post_parse_func
;
434 GOptionErrorFunc error_func
;
437 wxString
wxGetNameFromGtkOptionEntry(const GOptionEntry
*opt
)
442 ret
<< wxT("-") << opt
->short_name
;
447 ret
<< wxT("--") << opt
->long_name
;
449 if (opt
->arg_description
)
450 ret
<< wxT("=") << opt
->arg_description
;
453 return wxT(" ") + ret
;
456 #endif // __WXGTK26__
459 wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString
& names
,
460 wxArrayString
& desc
) const
465 if (!gtk_check_version(2,6,0))
467 // since GTK>=2.6, we can use the glib_check_version() symbol...
469 // check whether GLib version is greater than 2.6 but also lower than 2.19
470 // because, as we use the undocumented _GOptionGroup struct, we don't want
471 // to run this code with future versions which might change it (2.19 is the
472 // latest one at the time of this writing)
473 if (!glib_check_version(2,6,0) && glib_check_version(2,20,0))
475 usage
<< _("The following standard GTK+ options are also supported:\n");
477 // passing true here means that the function can open the default
478 // display while parsing (not really used here anyhow)
479 GOptionGroup
*gtkOpts
= gtk_get_option_group(true);
481 // WARNING: here we access the internals of GOptionGroup:
482 GOptionEntry
*entries
= ((_GOptionGroup
*)gtkOpts
)->entries
;
483 unsigned int n_entries
= ((_GOptionGroup
*)gtkOpts
)->n_entries
;
484 wxArrayString namesOptions
, descOptions
;
486 for ( size_t n
= 0; n
< n_entries
; n
++ )
488 if ( entries
[n
].flags
& G_OPTION_FLAG_HIDDEN
)
491 names
.push_back(wxGetNameFromGtkOptionEntry(&entries
[n
]));
493 const gchar
* const entryDesc
= entries
[n
].description
;
494 desc
.push_back(wxString(entryDesc
));
497 g_option_group_free (gtkOpts
);
503 #endif // __WXGTK26__