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/gtk/private/timer.h"
26 #include "wx/evtloop.h"
29 #ifdef GDK_WINDOWING_WIN32
30 #include <gdk/gdkwin32.h>
32 #ifdef GDK_WINDOWING_X11
37 #include "wx/gtk/assertdlg_gtk.h"
39 #include "wx/stackwalk.h"
40 #endif // wxUSE_STACKWALKER
41 #endif // wxDEBUG_LEVEL
46 #include <sys/types.h>
52 #include <X11/SM/SMlib.h>
54 #include "wx/unix/utilsx11.h"
57 #include "wx/gtk/private/gtk2-compat.h"
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 extern GtkWidget
*wxGetRootWindow();
65 //----------------------------------------------------------------------------
67 //----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
75 // display characteristics
76 // ----------------------------------------------------------------------------
78 #ifdef GDK_WINDOWING_X11
81 return GDK_DISPLAY_XDISPLAY(gtk_widget_get_display(wxGetRootWindow()));
85 void wxDisplaySize( int *width
, int *height
)
87 if (width
) *width
= gdk_screen_width();
88 if (height
) *height
= gdk_screen_height();
91 void wxDisplaySizeMM( int *width
, int *height
)
93 if (width
) *width
= gdk_screen_width_mm();
94 if (height
) *height
= gdk_screen_height_mm();
97 bool wxColourDisplay()
104 return gdk_visual_get_depth(gtk_widget_get_visual(wxGetRootWindow()));
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
);
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...
166 return pango_version_check(major
, minor
, micro
);
167 #elif defined(PANGO_VERSION_MAJOR)
168 if (!gtk_check_version (2,11,0))
170 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
171 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
172 // using has the pango_version_check function:
173 return pango_version_check (major
, minor
, micro
);
176 return "can't check";
177 #else // !PANGO_VERSION_MAJOR
182 return "too old headers";
186 // ----------------------------------------------------------------------------
187 // wxPlatformInfo-related
188 // ----------------------------------------------------------------------------
190 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
193 *verMaj
= gtk_major_version
;
195 *verMin
= gtk_minor_version
;
202 wxTimerImpl
*wxGUIAppTraits::CreateTimerImpl(wxTimer
*timer
)
204 return new wxGTKTimerImpl(timer
);
207 #endif // wxUSE_TIMER
210 static wxString
GetSM()
214 return wxEmptyString
;
218 SmcConn smc_conn
= SmcOpenConnection(NULL
, NULL
,
220 0 /* mask */, NULL
/* callbacks */,
222 WXSIZEOF(smerr
), smerr
);
226 wxLogDebug("Failed to connect to session manager: %s", smerr
);
227 return wxEmptyString
;
230 char *vendor
= SmcVendor(smc_conn
);
231 wxString ret
= wxString::FromAscii( vendor
);
234 SmcCloseConnection(smc_conn
, 0, NULL
);
239 #endif // wxUSE_DETECT_SM
242 //-----------------------------------------------------------------------------
244 //-----------------------------------------------------------------------------
246 wxEventLoopBase
*wxGUIAppTraits::CreateEventLoop()
248 return new wxEventLoop();
254 #if wxDEBUG_LEVEL && wxUSE_STACKWALKER
256 // private helper class
257 class StackDump
: public wxStackWalker
260 StackDump(GtkAssertDialog
*dlg
) { m_dlg
=dlg
; }
263 virtual void OnStackFrame(const wxStackFrame
& frame
)
265 wxString fncname
= frame
.GetName();
267 // append this stack frame's info in the dialog
268 if (!frame
.GetFileName().empty() || !fncname
.empty())
270 gtk_assert_dialog_append_stack_frame(m_dlg
,
272 frame
.GetFileName().utf8_str(),
278 GtkAssertDialog
*m_dlg
;
281 static void get_stackframe_callback(void* p
)
283 StackDump
* dump
= static_cast<StackDump
*>(p
);
284 // skip over frames up to including wxOnAssert()
285 dump
->ProcessFrames(6);
288 #endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
290 bool wxGUIAppTraits::ShowAssertDialog(const wxString
& msg
)
293 // we can't show the dialog from another thread
294 if ( wxIsMainThread() )
296 // under GTK2 we prefer to use a dialog widget written using directly
297 // in GTK+ as use a dialog written using wxWidgets would need the
298 // wxWidgets idle processing to work correctly which might not be the
299 // case when assert happens
300 GtkWidget
*dialog
= gtk_assert_dialog_new();
301 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog
), msg
.mb_str());
303 #if wxUSE_STACKWALKER
304 // save the current stack ow...
305 StackDump
dump(GTK_ASSERT_DIALOG(dialog
));
306 dump
.SaveStack(100); // showing more than 100 frames is not very useful
308 // ...but process it only if the user needs it
309 gtk_assert_dialog_set_backtrace_callback
311 GTK_ASSERT_DIALOG(dialog
),
312 get_stackframe_callback
,
315 #endif // wxUSE_STACKWALKER
317 gint result
= gtk_dialog_run(GTK_DIALOG (dialog
));
318 bool returnCode
= false;
321 case GTK_ASSERT_DIALOG_STOP
:
324 case GTK_ASSERT_DIALOG_CONTINUE
:
327 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
:
333 wxFAIL_MSG( wxT("unexpected return code from GtkAssertDialog") );
336 gtk_widget_destroy(dialog
);
339 #endif // wxDEBUG_LEVEL
341 return wxAppTraitsBase::ShowAssertDialog(msg
);
346 #if defined(__UNIX__) || defined(__OS2__)
348 wxString
wxGUIAppTraits::GetDesktopEnvironment() const
350 wxString de
= wxSystemOptions::GetOption(wxT("gtk.desktop"));
354 static const wxString s_SM
= GetSM();
356 if (s_SM
== wxT("GnomeSM"))
358 else if (s_SM
== wxT("KDE"))
361 #endif // wxUSE_DETECT_SM
366 #endif // __UNIX__ || __OS2__
370 // see the hack below in wxCmdLineParser::GetUsageString().
371 // TODO: replace this hack with a g_option_group_get_entries()
372 // call as soon as such function exists;
373 // see http://bugzilla.gnome.org/show_bug.cgi?id=431021 for the relative
379 gchar
*help_description
;
381 GDestroyNotify destroy_notify
;
384 GTranslateFunc translate_func
;
385 GDestroyNotify translate_notify
;
386 gpointer translate_data
;
388 GOptionEntry
*entries
;
391 GOptionParseFunc pre_parse_func
;
392 GOptionParseFunc post_parse_func
;
393 GOptionErrorFunc error_func
;
397 wxString
wxGetNameFromGtkOptionEntry(const GOptionEntry
*opt
)
402 ret
<< wxT("-") << opt
->short_name
;
407 ret
<< wxT("--") << opt
->long_name
;
409 if (opt
->arg_description
)
410 ret
<< wxT("=") << opt
->arg_description
;
413 return wxT(" ") + ret
;
417 wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString
& names
,
418 wxArrayString
& desc
) const
422 // check whether GLib version is greater than 2.6 but also lower than 2.33
423 // because, as we use the undocumented _GOptionGroup struct, we don't want
424 // to run this code with future versions which might change it (2.32 is the
425 // latest one at the time of this writing)
426 if (glib_check_version(2,33,0))
428 usage
<< _("The following standard GTK+ options are also supported:\n");
430 // passing true here means that the function can open the default
431 // display while parsing (not really used here anyhow)
432 GOptionGroup
*gtkOpts
= gtk_get_option_group(true);
434 // WARNING: here we access the internals of GOptionGroup:
435 GOptionEntry
*entries
= ((_GOptionGroup
*)gtkOpts
)->entries
;
436 unsigned int n_entries
= ((_GOptionGroup
*)gtkOpts
)->n_entries
;
437 wxArrayString namesOptions
, descOptions
;
439 for ( size_t n
= 0; n
< n_entries
; n
++ )
441 if ( entries
[n
].flags
& G_OPTION_FLAG_HIDDEN
)
444 names
.push_back(wxGetNameFromGtkOptionEntry(&entries
[n
]));
446 const gchar
* const entryDesc
= entries
[n
].description
;
447 desc
.push_back(wxString(entryDesc
));
450 g_option_group_free (gtkOpts
);