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"
25 #include "wx/unix/execute.h"
28 #include "wx/gtk/private/timer.h"
29 #include "wx/evtloop.h"
32 #ifdef GDK_WINDOWING_WIN32
33 #include <gdk/gdkwin32.h>
35 #ifdef GDK_WINDOWING_X11
40 #include "wx/gtk/assertdlg_gtk.h"
42 #include "wx/stackwalk.h"
43 #endif // wxUSE_STACKWALKER
44 #endif // wxDEBUG_LEVEL
49 #include <sys/types.h>
55 #include <X11/SM/SMlib.h>
57 #include "wx/unix/utilsx11.h"
60 #include "wx/gtk/private/gtk2-compat.h"
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
66 extern GtkWidget
*wxGetRootWindow();
68 //----------------------------------------------------------------------------
70 //----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
78 // display characteristics
79 // ----------------------------------------------------------------------------
81 #ifdef GDK_WINDOWING_X11
84 return GDK_DISPLAY_XDISPLAY(gtk_widget_get_display(wxGetRootWindow()));
88 void wxDisplaySize( int *width
, int *height
)
90 if (width
) *width
= gdk_screen_width();
91 if (height
) *height
= gdk_screen_height();
94 void wxDisplaySizeMM( int *width
, int *height
)
96 if (width
) *width
= gdk_screen_width_mm();
97 if (height
) *height
= gdk_screen_height_mm();
100 void wxGetMousePosition( int* x
, int* y
)
102 gdk_window_get_pointer(gtk_widget_get_root_window(wxGetRootWindow()), x
, y
, NULL
);
105 bool wxColourDisplay()
112 return gdk_visual_get_depth(gtk_widget_get_visual(wxGetRootWindow()));
115 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
117 return wxGenericFindWindowAtPoint(pt
);
122 WXDLLIMPEXP_CORE wxCharBuffer
123 wxConvertToGTK(const wxString
& s
, wxFontEncoding enc
)
126 if ( enc
== wxFONTENCODING_SYSTEM
|| enc
== wxFONTENCODING_DEFAULT
)
128 wbuf
= wxConvUI
->cMB2WC(s
);
130 else // another encoding, use generic conversion class
132 wbuf
= wxCSConv(enc
).cMB2WC(s
.c_str());
135 if ( !wbuf
&& !s
.empty() )
137 // conversion failed, but we still want to show something to the user
138 // even if it's going to be wrong it is better than nothing
140 // we choose ISO8859-1 here arbitrarily, it's just the most common
141 // encoding probably and, also importantly here, conversion from it
142 // never fails as it's done internally by wxCSConv
143 wbuf
= wxCSConv(wxFONTENCODING_ISO8859_1
).cMB2WC(s
.c_str());
146 return wxConvUTF8
.cWC2MB(wbuf
);
149 WXDLLIMPEXP_CORE wxCharBuffer
150 wxConvertFromGTK(const wxString
& s
, wxFontEncoding enc
)
152 // this conversion should never fail as GTK+ always uses UTF-8 internally
153 // so there are no complications here
154 const wxWCharBuffer
wbuf(wxConvUTF8
.cMB2WC(s
.c_str()));
155 if ( enc
== wxFONTENCODING_SYSTEM
)
156 return wxConvUI
->cWC2MB(wbuf
);
158 return wxCSConv(enc
).cWC2MB(wbuf
);
161 #endif // !wxUSE_UNICODE
163 // Returns NULL if version is certainly greater or equal than major.minor.micro
164 // Returns string describing the error if version is lower than
165 // major.minor.micro OR it cannot be determined and one should not rely on the
166 // availability of pango version major.minor.micro, nor the non-availability
167 const gchar
*wx_pango_version_check (int major
, int minor
, int micro
)
169 // NOTE: you don't need to use this macro to check for Pango features
170 // added in pango-1.4 or earlier since GTK 2.4 (our minimum requirement
171 // for GTK lib) required pango 1.4...
174 return pango_version_check(major
, minor
, micro
);
175 #elif defined(PANGO_VERSION_MAJOR)
176 if (!gtk_check_version (2,11,0))
178 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
179 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
180 // using has the pango_version_check function:
181 return pango_version_check (major
, minor
, micro
);
184 return "can't check";
185 #else // !PANGO_VERSION_MAJOR
190 return "too old headers";
194 // ----------------------------------------------------------------------------
195 // subprocess routines
196 // ----------------------------------------------------------------------------
201 static gboolean
EndProcessDetector(GIOChannel
* source
, GIOCondition
, void* data
)
203 wxEndProcessData
* const
204 proc_data
= static_cast<wxEndProcessData
*>(data
);
206 // child exited, end waiting
207 close(g_io_channel_unix_get_fd(source
));
209 wxHandleProcessTermination(proc_data
);
211 // don't call us again!
216 int wxGUIAppTraits::AddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
218 GIOChannel
* channel
= g_io_channel_unix_new(fd
);
219 GIOCondition cond
= GIOCondition(G_IO_IN
| G_IO_HUP
| G_IO_ERR
);
220 unsigned id
= g_io_add_watch(channel
, cond
, EndProcessDetector
, proc_data
);
221 g_io_channel_unref(channel
);
227 // ----------------------------------------------------------------------------
228 // wxPlatformInfo-related
229 // ----------------------------------------------------------------------------
231 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
234 *verMaj
= gtk_major_version
;
236 *verMin
= gtk_minor_version
;
243 wxTimerImpl
*wxGUIAppTraits::CreateTimerImpl(wxTimer
*timer
)
245 return new wxGTKTimerImpl(timer
);
248 #endif // wxUSE_TIMER
251 static wxString
GetSM()
255 return wxEmptyString
;
259 SmcConn smc_conn
= SmcOpenConnection(NULL
, NULL
,
261 0 /* mask */, NULL
/* callbacks */,
263 WXSIZEOF(smerr
), smerr
);
267 wxLogDebug("Failed to connect to session manager: %s", smerr
);
268 return wxEmptyString
;
271 char *vendor
= SmcVendor(smc_conn
);
272 wxString ret
= wxString::FromAscii( vendor
);
275 SmcCloseConnection(smc_conn
, 0, NULL
);
280 #endif // wxUSE_DETECT_SM
283 //-----------------------------------------------------------------------------
285 //-----------------------------------------------------------------------------
287 wxEventLoopBase
*wxGUIAppTraits::CreateEventLoop()
289 return new wxEventLoop();
295 #if wxDEBUG_LEVEL && wxUSE_STACKWALKER
297 // private helper class
298 class StackDump
: public wxStackWalker
301 StackDump(GtkAssertDialog
*dlg
) { m_dlg
=dlg
; }
304 virtual void OnStackFrame(const wxStackFrame
& frame
)
306 wxString fncname
= frame
.GetName();
308 // append this stack frame's info in the dialog
309 if (!frame
.GetFileName().empty() || !fncname
.empty())
311 gtk_assert_dialog_append_stack_frame(m_dlg
,
313 frame
.GetFileName().utf8_str(),
319 GtkAssertDialog
*m_dlg
;
322 static void get_stackframe_callback(void* p
)
324 StackDump
* dump
= static_cast<StackDump
*>(p
);
325 // skip over frames up to including wxOnAssert()
326 dump
->ProcessFrames(7);
329 #endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
331 bool wxGUIAppTraits::ShowAssertDialog(const wxString
& msg
)
334 // we can't show the dialog from another thread
335 if ( wxIsMainThread() )
337 // under GTK2 we prefer to use a dialog widget written using directly
338 // in GTK+ as use a dialog written using wxWidgets would need the
339 // wxWidgets idle processing to work correctly which might not be the
340 // case when assert happens
341 GtkWidget
*dialog
= gtk_assert_dialog_new();
342 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog
), msg
.mb_str());
344 #if wxUSE_STACKWALKER
345 // save the current stack ow...
346 StackDump
dump(GTK_ASSERT_DIALOG(dialog
));
347 dump
.SaveStack(100); // showing more than 100 frames is not very useful
349 // ...but process it only if the user needs it
350 gtk_assert_dialog_set_backtrace_callback
352 GTK_ASSERT_DIALOG(dialog
),
353 get_stackframe_callback
,
356 #endif // wxUSE_STACKWALKER
358 gint result
= gtk_dialog_run(GTK_DIALOG (dialog
));
359 bool returnCode
= false;
362 case GTK_ASSERT_DIALOG_STOP
:
365 case GTK_ASSERT_DIALOG_CONTINUE
:
368 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
:
374 wxFAIL_MSG( wxT("unexpected return code from GtkAssertDialog") );
377 gtk_widget_destroy(dialog
);
380 #endif // wxDEBUG_LEVEL
382 return wxAppTraitsBase::ShowAssertDialog(msg
);
387 #if defined(__UNIX__) || defined(__OS2__)
389 wxString
wxGUIAppTraits::GetDesktopEnvironment() const
391 wxString de
= wxSystemOptions::GetOption(wxT("gtk.desktop"));
395 static const wxString s_SM
= GetSM();
397 if (s_SM
== wxT("GnomeSM"))
399 else if (s_SM
== wxT("KDE"))
402 #endif // wxUSE_DETECT_SM
407 #endif // __UNIX__ || __OS2__
409 // see the hack below in wxCmdLineParser::GetUsageString().
410 // TODO: replace this hack with a g_option_group_get_entries()
411 // call as soon as such function exists;
412 // see http://bugzilla.gnome.org/show_bug.cgi?id=431021 for the relative
418 gchar
*help_description
;
420 GDestroyNotify destroy_notify
;
423 GTranslateFunc translate_func
;
424 GDestroyNotify translate_notify
;
425 gpointer translate_data
;
427 GOptionEntry
*entries
;
430 GOptionParseFunc pre_parse_func
;
431 GOptionParseFunc post_parse_func
;
432 GOptionErrorFunc error_func
;
435 wxString
wxGetNameFromGtkOptionEntry(const GOptionEntry
*opt
)
440 ret
<< wxT("-") << opt
->short_name
;
445 ret
<< wxT("--") << opt
->long_name
;
447 if (opt
->arg_description
)
448 ret
<< wxT("=") << opt
->arg_description
;
451 return wxT(" ") + ret
;
457 wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString
& names
,
458 wxArrayString
& desc
) const
462 // check whether GLib version is greater than 2.6 but also lower than 2.33
463 // because, as we use the undocumented _GOptionGroup struct, we don't want
464 // to run this code with future versions which might change it (2.32 is the
465 // latest one at the time of this writing)
466 if (glib_check_version(2,33,0))
468 usage
<< _("The following standard GTK+ options are also supported:\n");
470 // passing true here means that the function can open the default
471 // display while parsing (not really used here anyhow)
472 GOptionGroup
*gtkOpts
= gtk_get_option_group(true);
474 // WARNING: here we access the internals of GOptionGroup:
475 GOptionEntry
*entries
= ((_GOptionGroup
*)gtkOpts
)->entries
;
476 unsigned int n_entries
= ((_GOptionGroup
*)gtkOpts
)->n_entries
;
477 wxArrayString namesOptions
, descOptions
;
479 for ( size_t n
= 0; n
< n_entries
; n
++ )
481 if ( entries
[n
].flags
& G_OPTION_FLAG_HIDDEN
)
484 names
.push_back(wxGetNameFromGtkOptionEntry(&entries
[n
]));
486 const gchar
* const entryDesc
= entries
[n
].description
;
487 desc
.push_back(wxString(entryDesc
));
490 g_option_group_free (gtkOpts
);