1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/utilsgtk.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11
12 #include "wx/utils.h"
13
14 #ifndef WX_PRECOMP
15 #include "wx/string.h"
16 #include "wx/intl.h"
17 #include "wx/log.h"
18 #endif
19
20 #include "wx/apptrait.h"
21 #include "wx/process.h"
22 #include "wx/sysopt.h"
23 #include "wx/vector.h"
24
25 #include "wx/gtk/private/timer.h"
26 #include "wx/evtloop.h"
27
28 #include "wx/gtk/private/wrapgtk.h"
29 #ifdef GDK_WINDOWING_WAYLAND
30 #include <gdk/gdkwayland.h>
31 #endif
32 #ifdef GDK_WINDOWING_WIN32
33 #include <gdk/gdkwin32.h>
34 #endif
35 #ifdef GDK_WINDOWING_X11
36 #include <gdk/gdkx.h>
37 #endif
38
39 #if wxDEBUG_LEVEL
40 #include "wx/gtk/assertdlg_gtk.h"
41 #if wxUSE_STACKWALKER
42 #include "wx/stackwalk.h"
43 #endif // wxUSE_STACKWALKER
44 #endif // wxDEBUG_LEVEL
45
46 #include <stdarg.h>
47 #include <string.h>
48 #include <sys/stat.h>
49 #include <sys/types.h>
50 #ifdef __UNIX__
51 #include <unistd.h>
52 #endif
53
54 #if wxUSE_DETECT_SM
55 #include <X11/SM/SMlib.h>
56
57 #include "wx/unix/utilsx11.h"
58 #endif
59
60 GdkWindow* wxGetTopLevelGDK();
61
62 //----------------------------------------------------------------------------
63 // misc.
64 //----------------------------------------------------------------------------
65
wxBell()66 void wxBell()
67 {
68 gdk_beep();
69 }
70
71 // ----------------------------------------------------------------------------
72 // display characteristics
73 // ----------------------------------------------------------------------------
74
75 #if defined(__UNIX__)
76
wxGetDisplay()77 void *wxGetDisplay()
78 {
79 return wxGetDisplayInfo().dpy;
80 }
81
wxGetDisplayInfo()82 wxDisplayInfo wxGetDisplayInfo()
83 {
84 wxDisplayInfo info = { NULL, wxDisplayNone };
85 GdkDisplay *display = gdk_window_get_display(wxGetTopLevelGDK());
86 #if defined(__WXGTK3__) && (defined(GDK_WINDOWING_WAYLAND) || defined(GDK_WINDOWING_X11))
87 const char* displayTypeName = g_type_name(G_TYPE_FROM_INSTANCE(display));
88 #endif
89
90 #ifdef GDK_WINDOWING_X11
91 #ifdef __WXGTK3__
92 if (strcmp("GdkX11Display", displayTypeName) == 0)
93 #endif
94 {
95 info.dpy = GDK_DISPLAY_XDISPLAY(display);
96 info.type = wxDisplayX11;
97 return info;
98 }
99 #endif
100 #ifdef GDK_WINDOWING_WAYLAND
101 if (strcmp("GdkWaylandDisplay", displayTypeName) == 0)
102 {
103 info.dpy = gdk_wayland_display_get_wl_display(display);
104 info.type = wxDisplayWayland;
105 return info;
106 }
107 #endif
108 return info;
109 }
110
111 #endif // __UNIX__
112
wxFindWindowAtPoint(const wxPoint & pt)113 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
114 {
115 return wxGenericFindWindowAtPoint(pt);
116 }
117
118 #if !wxUSE_UNICODE
119
120 WXDLLIMPEXP_CORE wxCharBuffer
wxConvertToGTK(const wxString & s,wxFontEncoding enc)121 wxConvertToGTK(const wxString& s, wxFontEncoding enc)
122 {
123 if (s.empty())
124 return wxCharBuffer("");
125
126 wxWCharBuffer wbuf;
127 if ( enc == wxFONTENCODING_SYSTEM || enc == wxFONTENCODING_DEFAULT )
128 {
129 wbuf = wxConvUI->cMB2WC(s.c_str());
130 }
131 else // another encoding, use generic conversion class
132 {
133 wbuf = wxCSConv(enc).cMB2WC(s.c_str());
134 }
135
136 if (wbuf.length() == 0)
137 {
138 // conversion failed, but we still want to show something to the user
139 // even if it's going to be wrong it is better than nothing
140 //
141 // we choose ISO8859-1 here arbitrarily, it's just the most common
142 // encoding probably and, also importantly here, conversion from it
143 // never fails as it's done internally by wxCSConv
144 wbuf = wxCSConv(wxFONTENCODING_ISO8859_1).cMB2WC(s.c_str());
145 }
146
147 return wxConvUTF8.cWC2MB(wbuf);
148 }
149
150 WXDLLIMPEXP_CORE wxCharBuffer
wxConvertFromGTK(const wxString & s,wxFontEncoding enc)151 wxConvertFromGTK(const wxString& s, wxFontEncoding enc)
152 {
153 // this conversion should never fail as GTK+ always uses UTF-8 internally
154 // so there are no complications here
155 const wxWCharBuffer wbuf(wxConvUTF8.cMB2WC(s.c_str()));
156 if ( enc == wxFONTENCODING_SYSTEM )
157 return wxConvUI->cWC2MB(wbuf);
158
159 return wxCSConv(enc).cWC2MB(wbuf);
160 }
161
162 #endif // !wxUSE_UNICODE
163
164 // Returns NULL if version is certainly greater or equal than major.minor.micro
165 // Returns string describing the error if version is lower than
166 // major.minor.micro OR it cannot be determined and one should not rely on the
167 // availability of pango version major.minor.micro, nor the non-availability
wx_pango_version_check(int major,int minor,int micro)168 const gchar *wx_pango_version_check (int major, int minor, int micro)
169 {
170 // NOTE: you don't need to use this macro to check for Pango features
171 // added in pango-1.4 or earlier since GTK 2.4 (our minimum requirement
172 // for GTK lib) required pango 1.4...
173
174 #ifdef __WXGTK3__
175 return pango_version_check(major, minor, micro);
176 #elif defined(PANGO_VERSION_MAJOR)
177 if (!gtk_check_version (2,11,0))
178 {
179 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
180 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
181 // using has the pango_version_check function:
182 return pango_version_check (major, minor, micro);
183 }
184
185 return "can't check";
186 #else // !PANGO_VERSION_MAJOR
187 wxUnusedVar(major);
188 wxUnusedVar(minor);
189 wxUnusedVar(micro);
190
191 return "too old headers";
192 #endif
193 }
194
195 // ----------------------------------------------------------------------------
196 // wxPlatformInfo-related
197 // ----------------------------------------------------------------------------
198
GetToolkitVersion(int * verMaj,int * verMin,int * verMicro) const199 wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj,
200 int *verMin,
201 int *verMicro) const
202 {
203 #ifdef __WXGTK3__
204 if (verMaj)
205 *verMaj = gtk_get_major_version();
206 if (verMin)
207 *verMin = gtk_get_minor_version();
208 if (verMicro)
209 *verMicro = gtk_get_micro_version();
210 #else
211 if ( verMaj )
212 *verMaj = gtk_major_version;
213 if ( verMin )
214 *verMin = gtk_minor_version;
215 if ( verMicro )
216 *verMicro = gtk_micro_version;
217 #endif
218
219 return wxPORT_GTK;
220 }
221
222 #if wxUSE_TIMER
223
CreateTimerImpl(wxTimer * timer)224 wxTimerImpl *wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
225 {
226 return new wxGTKTimerImpl(timer);
227 }
228
229 #endif // wxUSE_TIMER
230
231 #if wxUSE_DETECT_SM
GetSM()232 static wxString GetSM()
233 {
234 wxX11Display dpy;
235 if ( !dpy )
236 return wxEmptyString;
237
238 char smerr[256];
239 char *client_id;
240 SmcConn smc_conn = SmcOpenConnection(NULL, NULL,
241 999, 999,
242 0 /* mask */, NULL /* callbacks */,
243 NULL, &client_id,
244 WXSIZEOF(smerr), smerr);
245
246 if ( !smc_conn )
247 {
248 // Don't report error if there is no session manager at all
249 if (getenv("SESSION_MANAGER"))
250 {
251 wxLogDebug("Failed to connect to session manager: %s", smerr);
252 }
253 return wxEmptyString;
254 }
255
256 char *vendor = SmcVendor(smc_conn);
257 wxString ret = wxString::FromAscii( vendor );
258 free(vendor);
259
260 SmcCloseConnection(smc_conn, 0, NULL);
261 free(client_id);
262
263 return ret;
264 }
265 #endif // wxUSE_DETECT_SM
266
267
268 //-----------------------------------------------------------------------------
269 // wxGUIAppTraits
270 //-----------------------------------------------------------------------------
271
CreateEventLoop()272 wxEventLoopBase *wxGUIAppTraits::CreateEventLoop()
273 {
274 return new wxEventLoop();
275 }
276
277
278 #ifdef __UNIX__
279
280 #if wxDEBUG_LEVEL && wxUSE_STACKWALKER
281
282 // private helper class
283 class StackDump : public wxStackWalker
284 {
285 public:
StackDump(GtkAssertDialog * dlg)286 StackDump(GtkAssertDialog *dlg) { m_dlg=dlg; }
287
ShowStackInDialog()288 void ShowStackInDialog()
289 {
290 ProcessFrames(0);
291
292 for ( wxVector<Frame>::const_iterator it = m_frames.begin();
293 it != m_frames.end();
294 ++it )
295 {
296 gtk_assert_dialog_append_stack_frame(m_dlg,
297 it->name.utf8_str(),
298 it->file.utf8_str(),
299 it->line);
300 }
301
302 m_frames.clear();
303 }
304
305 protected:
OnStackFrame(const wxStackFrame & frame)306 virtual void OnStackFrame(const wxStackFrame& frame) wxOVERRIDE
307 {
308 const wxString name = frame.GetName();
309 if ( name.StartsWith("wxOnAssert") )
310 {
311 // Ignore all frames until the wxOnAssert() one, just as we do in
312 // wxAppTraitsBase::GetAssertStackTrace().
313 m_frames.clear();
314 return;
315 }
316
317 // Also ignore frames which don't have neither the function name nor
318 // the file name, showing them in the dialog wouldn't provide any
319 // useful information.
320 if ( name.empty() && frame.GetFileName().empty() )
321 return;
322
323 m_frames.push_back(Frame(frame));
324 }
325
326 private:
327 GtkAssertDialog *m_dlg;
328
329 struct Frame
330 {
FrameStackDump::Frame331 explicit Frame(const wxStackFrame& f)
332 : name(f.GetName()),
333 file(f.GetFileName()),
334 line(f.GetLine())
335 {
336 }
337
338 wxString name;
339 wxString file;
340 int line;
341 };
342
343 wxVector<Frame> m_frames;
344 };
345
get_stackframe_callback(void * p)346 static void get_stackframe_callback(void* p)
347 {
348 StackDump* dump = static_cast<StackDump*>(p);
349 dump->ShowStackInDialog();
350 }
351
352 #endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
353
ShowAssertDialog(const wxString & msg)354 bool wxGUIAppTraits::ShowAssertDialog(const wxString& msg)
355 {
356 #if wxDEBUG_LEVEL
357 // we can't show the dialog from another thread
358 if ( wxIsMainThread() )
359 {
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());
366
367 GdkDisplay* display = gtk_widget_get_display(dialog);
368 #ifdef __WXGTK4__
369 gdk_seat_ungrab(gdk_display_get_default_seat(display));
370 #elif defined(__WXGTK3__)
371 wxGCC_WARNING_SUPPRESS(deprecated-declarations)
372 GdkDeviceManager* manager = gdk_display_get_device_manager(display);
373 GdkDevice* device = gdk_device_manager_get_client_pointer(manager);
374 gdk_device_ungrab(device, unsigned(GDK_CURRENT_TIME));
375 wxGCC_WARNING_RESTORE()
376 #else
377 gdk_display_pointer_ungrab(display, unsigned(GDK_CURRENT_TIME));
378 #endif
379
380 #if wxUSE_STACKWALKER
381 // save the current stack ow...
382 StackDump dump(GTK_ASSERT_DIALOG(dialog));
383 dump.SaveStack(100); // showing more than 100 frames is not very useful
384
385 // ...but process it only if the user needs it
386 gtk_assert_dialog_set_backtrace_callback
387 (
388 GTK_ASSERT_DIALOG(dialog),
389 get_stackframe_callback,
390 &dump
391 );
392 #endif // wxUSE_STACKWALKER
393
394 gint result = gtk_dialog_run(GTK_DIALOG (dialog));
395 bool returnCode = false;
396 switch (result)
397 {
398 case GTK_ASSERT_DIALOG_STOP:
399 // Don't call wxTrap() directly from here to avoid having the
400 // functions between the occurrence of the assert in the code
401 // and this function in the call stack. Instead, just set a
402 // flag so that inline expansion of the assert macro we are
403 // called from calls wxTrap() itself, like this the debugger
404 // would break exactly at the assert position.
405 wxTrapInAssert = true;
406 break;
407 case GTK_ASSERT_DIALOG_CONTINUE:
408 // nothing to do
409 break;
410 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING:
411 // no more asserts
412 returnCode = true;
413 break;
414
415 default:
416 wxFAIL_MSG( wxT("unexpected return code from GtkAssertDialog") );
417 }
418
419 gtk_widget_destroy(dialog);
420 return returnCode;
421 }
422 #endif // wxDEBUG_LEVEL
423
424 return wxAppTraitsBase::ShowAssertDialog(msg);
425 }
426
427 #endif // __UNIX__
428
429 #if defined(__UNIX__)
430
GetDesktopEnvironment() const431 wxString wxGUIAppTraits::GetDesktopEnvironment() const
432 {
433 wxString de = wxSystemOptions::GetOption(wxT("gtk.desktop"));
434 #if wxUSE_DETECT_SM
435 if ( de.empty() )
436 {
437 static const wxString s_SM = GetSM();
438
439 if (s_SM == wxT("GnomeSM"))
440 de = wxT("GNOME");
441 else if (s_SM == wxT("KDE"))
442 de = wxT("KDE");
443 }
444 #endif // wxUSE_DETECT_SM
445
446 return de;
447 }
448
449 #endif // __UNIX__
450