1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/utilsgtk.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
15 #include "wx/string.h"
20 #include "wx/apptrait.h"
21 #include "wx/process.h"
22 #include "wx/sysopt.h"
24 #include "wx/gtk/private/timer.h"
25 #include "wx/evtloop.h"
28 #ifdef GDK_WINDOWING_WIN32
29 #include <gdk/gdkwin32.h>
31 #ifdef GDK_WINDOWING_X11
36 #include "wx/gtk/assertdlg_gtk.h"
38 #include "wx/stackwalk.h"
39 #endif // wxUSE_STACKWALKER
40 #endif // wxDEBUG_LEVEL
45 #include <sys/types.h>
51 #include <X11/SM/SMlib.h>
53 #include "wx/unix/utilsx11.h"
56 #include "wx/gtk/private/gtk2-compat.h"
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 extern GtkWidget
*wxGetRootWindow();
64 //----------------------------------------------------------------------------
66 //----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
74 // display characteristics
75 // ----------------------------------------------------------------------------
77 #ifdef GDK_WINDOWING_X11
80 return GDK_DISPLAY_XDISPLAY(gtk_widget_get_display(wxGetRootWindow()));
84 void wxDisplaySize( int *width
, int *height
)
86 if (width
) *width
= gdk_screen_width();
87 if (height
) *height
= gdk_screen_height();
90 void wxDisplaySizeMM( int *width
, int *height
)
92 if (width
) *width
= gdk_screen_width_mm();
93 if (height
) *height
= gdk_screen_height_mm();
96 bool wxColourDisplay()
103 return gdk_visual_get_depth(gtk_widget_get_visual(wxGetRootWindow()));
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
);
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...
165 return pango_version_check(major
, minor
, micro
);
166 #elif defined(PANGO_VERSION_MAJOR)
167 if (!gtk_check_version (2,11,0))
169 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
170 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
171 // using has the pango_version_check function:
172 return pango_version_check (major
, minor
, micro
);
175 return "can't check";
176 #else // !PANGO_VERSION_MAJOR
181 return "too old headers";
185 // ----------------------------------------------------------------------------
186 // wxPlatformInfo-related
187 // ----------------------------------------------------------------------------
189 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
192 *verMaj
= gtk_major_version
;
194 *verMin
= gtk_minor_version
;
201 wxTimerImpl
*wxGUIAppTraits::CreateTimerImpl(wxTimer
*timer
)
203 return new wxGTKTimerImpl(timer
);
206 #endif // wxUSE_TIMER
209 static wxString
GetSM()
213 return wxEmptyString
;
217 SmcConn smc_conn
= SmcOpenConnection(NULL
, NULL
,
219 0 /* mask */, NULL
/* callbacks */,
221 WXSIZEOF(smerr
), smerr
);
225 wxLogDebug("Failed to connect to session manager: %s", smerr
);
226 return wxEmptyString
;
229 char *vendor
= SmcVendor(smc_conn
);
230 wxString ret
= wxString::FromAscii( vendor
);
233 SmcCloseConnection(smc_conn
, 0, NULL
);
238 #endif // wxUSE_DETECT_SM
241 //-----------------------------------------------------------------------------
243 //-----------------------------------------------------------------------------
245 wxEventLoopBase
*wxGUIAppTraits::CreateEventLoop()
247 return new wxEventLoop();
253 #if wxDEBUG_LEVEL && wxUSE_STACKWALKER
255 // private helper class
256 class StackDump
: public wxStackWalker
259 StackDump(GtkAssertDialog
*dlg
) { m_dlg
=dlg
; }
262 virtual void OnStackFrame(const wxStackFrame
& frame
)
264 wxString fncname
= frame
.GetName();
266 // append this stack frame's info in the dialog
267 if (!frame
.GetFileName().empty() || !fncname
.empty())
269 gtk_assert_dialog_append_stack_frame(m_dlg
,
271 frame
.GetFileName().utf8_str(),
277 GtkAssertDialog
*m_dlg
;
280 static void get_stackframe_callback(void* p
)
282 StackDump
* dump
= static_cast<StackDump
*>(p
);
283 // skip over frames up to including wxOnAssert()
284 dump
->ProcessFrames(6);
287 #endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
289 bool wxGUIAppTraits::ShowAssertDialog(const wxString
& msg
)
292 // we can't show the dialog from another thread
293 if ( wxIsMainThread() )
295 // under GTK2 we prefer to use a dialog widget written using directly
296 // in GTK+ as use a dialog written using wxWidgets would need the
297 // wxWidgets idle processing to work correctly which might not be the
298 // case when assert happens
299 GtkWidget
*dialog
= gtk_assert_dialog_new();
300 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog
), msg
.mb_str());
302 #if wxUSE_STACKWALKER
303 // save the current stack ow...
304 StackDump
dump(GTK_ASSERT_DIALOG(dialog
));
305 dump
.SaveStack(100); // showing more than 100 frames is not very useful
307 // ...but process it only if the user needs it
308 gtk_assert_dialog_set_backtrace_callback
310 GTK_ASSERT_DIALOG(dialog
),
311 get_stackframe_callback
,
314 #endif // wxUSE_STACKWALKER
316 gint result
= gtk_dialog_run(GTK_DIALOG (dialog
));
317 bool returnCode
= false;
320 case GTK_ASSERT_DIALOG_STOP
:
323 case GTK_ASSERT_DIALOG_CONTINUE
:
326 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
:
332 wxFAIL_MSG( wxT("unexpected return code from GtkAssertDialog") );
335 gtk_widget_destroy(dialog
);
338 #endif // wxDEBUG_LEVEL
340 return wxAppTraitsBase::ShowAssertDialog(msg
);
345 #if defined(__UNIX__) || defined(__OS2__)
347 wxString
wxGUIAppTraits::GetDesktopEnvironment() const
349 wxString de
= wxSystemOptions::GetOption(wxT("gtk.desktop"));
353 static const wxString s_SM
= GetSM();
355 if (s_SM
== wxT("GnomeSM"))
357 else if (s_SM
== wxT("KDE"))
360 #endif // wxUSE_DETECT_SM
365 #endif // __UNIX__ || __OS2__
369 // see the hack below in wxCmdLineParser::GetUsageString().
370 // TODO: replace this hack with a g_option_group_get_entries()
371 // call as soon as such function exists;
372 // see http://bugzilla.gnome.org/show_bug.cgi?id=431021 for the relative
378 gchar
*help_description
;
380 GDestroyNotify destroy_notify
;
383 GTranslateFunc translate_func
;
384 GDestroyNotify translate_notify
;
385 gpointer translate_data
;
387 GOptionEntry
*entries
;
390 GOptionParseFunc pre_parse_func
;
391 GOptionParseFunc post_parse_func
;
392 GOptionErrorFunc error_func
;
396 wxString
wxGetNameFromGtkOptionEntry(const GOptionEntry
*opt
)
401 ret
<< wxT("-") << opt
->short_name
;
406 ret
<< wxT("--") << opt
->long_name
;
408 if (opt
->arg_description
)
409 ret
<< wxT("=") << opt
->arg_description
;
412 return wxT(" ") + ret
;
416 wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString
& names
,
417 wxArrayString
& desc
) const
421 // check whether GLib version is greater than 2.6 but also lower than 2.33
422 // because, as we use the undocumented _GOptionGroup struct, we don't want
423 // to run this code with future versions which might change it (2.32 is the
424 // latest one at the time of this writing)
425 if (glib_check_version(2,33,0))
427 usage
<< _("The following standard GTK+ options are also supported:\n");
429 // passing true here means that the function can open the default
430 // display while parsing (not really used here anyhow)
431 GOptionGroup
*gtkOpts
= gtk_get_option_group(true);
433 // WARNING: here we access the internals of GOptionGroup:
434 GOptionEntry
*entries
= ((_GOptionGroup
*)gtkOpts
)->entries
;
435 unsigned int n_entries
= ((_GOptionGroup
*)gtkOpts
)->n_entries
;
436 wxArrayString namesOptions
, descOptions
;
438 for ( size_t n
= 0; n
< n_entries
; n
++ )
440 if ( entries
[n
].flags
& G_OPTION_FLAG_HIDDEN
)
443 names
.push_back(wxGetNameFromGtkOptionEntry(&entries
[n
]));
445 const gchar
* const entryDesc
= entries
[n
].description
;
446 desc
.push_back(wxString(entryDesc
));
449 g_option_group_free (gtkOpts
);