]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/utilsgtk.cpp
f9c83e3166ac6eb56ceee38c827e0f3dd398e901
[wxWidgets.git] / src / gtk / utilsgtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/utilsgtk.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/utils.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/string.h"
17 #include "wx/intl.h"
18 #include "wx/log.h"
19 #endif
20
21 #include "wx/apptrait.h"
22
23 #include "wx/process.h"
24
25 #include "wx/unix/execute.h"
26
27 #ifdef __WXDEBUG__
28 #include "wx/gtk/assertdlg_gtk.h"
29 #if wxUSE_STACKWALKER
30 #include "wx/stackwalk.h"
31 #endif // wxUSE_STACKWALKER
32 #endif // __WXDEBUG__
33
34 #include <stdarg.h>
35 #include <string.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <sys/wait.h> // for WNOHANG
39 #include <unistd.h>
40
41 #include "glib.h"
42 #include "gdk/gdk.h"
43 #include "gtk/gtk.h"
44 #include "gdk/gdkx.h"
45
46 #ifdef HAVE_X11_XKBLIB_H
47 /* under HP-UX and Solaris 2.6, at least, XKBlib.h defines structures with
48 * field named "explicit" - which is, of course, an error for a C++
49 * compiler. To be on the safe side, just redefine it everywhere. */
50 #define explicit __wx_explicit
51
52 #include "X11/XKBlib.h"
53
54 #undef explicit
55 #endif // HAVE_X11_XKBLIB_H
56
57
58 #if wxUSE_DETECT_SM
59 #include "X11/Xlib.h"
60 #include "X11/SM/SMlib.h"
61 #endif
62
63 //-----------------------------------------------------------------------------
64 // data
65 //-----------------------------------------------------------------------------
66
67 extern GtkWidget *wxGetRootWindow();
68
69 //----------------------------------------------------------------------------
70 // misc.
71 //----------------------------------------------------------------------------
72 #ifndef __EMX__
73 // on OS/2, we use the wxBell from wxBase library
74
75 void wxBell()
76 {
77 gdk_beep();
78 }
79 #endif
80
81 /* Don't synthesize KeyUp events holding down a key and producing
82 KeyDown events with autorepeat. */
83 #ifdef HAVE_X11_XKBLIB_H
84 bool wxSetDetectableAutoRepeat( bool flag )
85 {
86 Bool result;
87 XkbSetDetectableAutoRepeat( GDK_DISPLAY(), flag, &result );
88 return result; /* true if keyboard hardware supports this mode */
89 }
90 #else
91 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) )
92 {
93 return false;
94 }
95 #endif
96
97 // Escapes string so that it is valid Pango markup XML string:
98 wxString wxEscapeStringForPangoMarkup(const wxString& str)
99 {
100 size_t len = str.length();
101 wxString out;
102 out.Alloc(len);
103 for (size_t i = 0; i < len; i++)
104 {
105 wxChar c = str[i];
106 switch (c)
107 {
108 case _T('&'):
109 out << _T("&amp;");
110 break;
111 case _T('<'):
112 out << _T("&lt;");
113 break;
114 case _T('>'):
115 out << _T("&gt;");
116 break;
117 case _T('\''):
118 out << _T("&apos;");
119 break;
120 case _T('"'):
121 out << _T("&quot;");
122 break;
123 default:
124 out << c;
125 break;
126 }
127 }
128 return out;
129 }
130
131
132 // ----------------------------------------------------------------------------
133 // display characterstics
134 // ----------------------------------------------------------------------------
135
136 void *wxGetDisplay()
137 {
138 return GDK_DISPLAY();
139 }
140
141 void wxDisplaySize( int *width, int *height )
142 {
143 if (width) *width = gdk_screen_width();
144 if (height) *height = gdk_screen_height();
145 }
146
147 void wxDisplaySizeMM( int *width, int *height )
148 {
149 if (width) *width = gdk_screen_width_mm();
150 if (height) *height = gdk_screen_height_mm();
151 }
152
153 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
154 {
155 // This is supposed to return desktop dimensions minus any window
156 // manager panels, menus, taskbars, etc. If there is a way to do that
157 // for this platform please fix this function, otherwise it defaults
158 // to the entire desktop.
159 if (x) *x = 0;
160 if (y) *y = 0;
161 wxDisplaySize(width, height);
162 }
163
164 void wxGetMousePosition( int* x, int* y )
165 {
166 gdk_window_get_pointer( (GdkWindow*) NULL, x, y, (GdkModifierType*) NULL );
167 }
168
169 bool wxColourDisplay()
170 {
171 return true;
172 }
173
174 int wxDisplayDepth()
175 {
176 return gdk_drawable_get_visual( wxGetRootWindow()->window )->depth;
177 }
178
179 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
180 {
181 return wxGenericFindWindowAtPoint(pt);
182 }
183
184 #if !wxUSE_UNICODE
185
186 wxCharBuffer wxConvertToGTK(const wxString& s, wxFontEncoding enc)
187 {
188 wxCharBuffer buf;
189 if ( enc == wxFONTENCODING_UTF8 )
190 {
191 // no need for conversion at all, but do check that we have a valid
192 // UTF-8 string because passing invalid UTF-8 to GTK+ is going to
193 // result in a GTK+ error message and, especially, loss of data which
194 // was supposed to be shown in the GUI
195 if ( wxConvUTF8.ToWChar(NULL, 0, s, s.length()) == wxCONV_FAILED )
196 {
197 // warn the programmer that something is probably wrong in his code
198 //
199 // NB: don't include the string in output because chances are that
200 // this invalid UTF-8 string could result in more errors itself
201 // if the application shows logs in the GUI and so we get into
202 // an infinite loop
203 wxLogDebug(_T("Invalid UTF-8 string in wxConvertToGTK()"));
204
205 // but still try to show at least something on the screen
206 wxMBConvUTF8 utf8permissive(wxMBConvUTF8::MAP_INVALID_UTF8_TO_OCTAL);
207 wxWCharBuffer wbuf(utf8permissive.cMB2WC(s));
208 buf = wxConvUTF8.cWC2MB(wbuf);
209 }
210 else // valid UTF-8 string, no need to convert
211 {
212 buf = wxCharBuffer(s);
213 }
214 }
215 else // !UTF-8
216 {
217 wxWCharBuffer wbuf;
218 if ( enc == wxFONTENCODING_SYSTEM || enc == wxFONTENCODING_DEFAULT )
219 {
220 wbuf = wxConvUI->cMB2WC(s);
221 }
222 else // another encoding, use generic conversion class
223 {
224 wbuf = wxCSConv(enc).cMB2WC(s);
225 }
226
227 if ( wbuf )
228 buf = wxConvUTF8.cWC2MB(wbuf);
229 }
230
231 return buf;
232 }
233
234 #endif // !wxUSE_UNICODE
235
236 // ----------------------------------------------------------------------------
237 // subprocess routines
238 // ----------------------------------------------------------------------------
239
240 extern "C" {
241 static
242 void GTK_EndProcessDetector(gpointer data, gint source,
243 GdkInputCondition WXUNUSED(condition) )
244 {
245 wxEndProcessData *proc_data = (wxEndProcessData *)data;
246
247 // has the process really terminated? unfortunately GDK (or GLib) seem to
248 // generate G_IO_HUP notification even when it simply tries to read from a
249 // closed fd and hasn't terminated at all
250 int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
251 int status = 0;
252 int rc = waitpid(pid, &status, WNOHANG);
253
254 if ( rc == 0 )
255 {
256 // no, it didn't exit yet, continue waiting
257 return;
258 }
259
260 // set exit code to -1 if something bad happened
261 proc_data->exitcode = rc != -1 && WIFEXITED(status) ? WEXITSTATUS(status)
262 : -1;
263
264 // child exited, end waiting
265 close(source);
266
267 // don't call us again!
268 gdk_input_remove(proc_data->tag);
269
270 wxHandleProcessTermination(proc_data);
271 }
272 }
273
274 int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
275 {
276 int tag = gdk_input_add(fd,
277 GDK_INPUT_READ,
278 GTK_EndProcessDetector,
279 (gpointer)proc_data);
280
281 return tag;
282 }
283
284
285
286 // ----------------------------------------------------------------------------
287 // wxPlatformInfo-related
288 // ----------------------------------------------------------------------------
289
290 wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
291 {
292 if ( verMaj )
293 *verMaj = gtk_major_version;
294 if ( verMin )
295 *verMin = gtk_minor_version;
296
297 return wxPORT_GTK;
298 }
299
300 #if wxUSE_DETECT_SM
301 static wxString GetSM()
302 {
303 class Dpy
304 {
305 public:
306 Dpy() { m_dpy = XOpenDisplay(NULL); }
307 ~Dpy() { if ( m_dpy ) XCloseDisplay(m_dpy); }
308
309 operator Display *() const { return m_dpy; }
310 private:
311 Display *m_dpy;
312 } dpy;
313
314 if ( !dpy )
315 return wxEmptyString;
316
317 char *client_id;
318 SmcConn smc_conn = SmcOpenConnection(NULL, NULL,
319 999, 999,
320 0 /* mask */, NULL /* callbacks */,
321 NULL, &client_id,
322 0, NULL);
323
324 if ( !smc_conn )
325 return wxEmptyString;
326
327 char *vendor = SmcVendor(smc_conn);
328 wxString ret = wxString::FromAscii( vendor );
329 free(vendor);
330
331 SmcCloseConnection(smc_conn, 0, NULL);
332 free(client_id);
333
334 return ret;
335 }
336 #endif // wxUSE_DETECT_SM
337
338
339 //-----------------------------------------------------------------------------
340 // wxGUIAppTraits
341 //-----------------------------------------------------------------------------
342
343 #ifdef __WXDEBUG__
344
345 #if wxUSE_STACKWALKER
346
347 // private helper class
348 class StackDump : public wxStackWalker
349 {
350 public:
351 StackDump(GtkAssertDialog *dlg) { m_dlg=dlg; }
352
353 protected:
354 virtual void OnStackFrame(const wxStackFrame& frame)
355 {
356 wxString fncname = frame.GetName();
357 wxString fncargs = fncname;
358
359 size_t n = fncname.find(wxT('('));
360 if (n != wxString::npos)
361 {
362 // remove arguments from function name
363 fncname.erase(n);
364
365 // remove function name and brackets from arguments
366 fncargs = fncargs.substr(n+1, fncargs.length()-n-2);
367 }
368 else
369 fncargs = wxEmptyString;
370
371 // append this stack frame's info in the dialog
372 if (!frame.GetFileName().empty() || !fncname.empty())
373 gtk_assert_dialog_append_stack_frame(m_dlg,
374 fncname.mb_str(),
375 fncargs.mb_str(),
376 frame.GetFileName().mb_str(),
377 frame.GetLine());
378 }
379
380 private:
381 GtkAssertDialog *m_dlg;
382 };
383
384 // the callback functions must be extern "C" to comply with GTK+ declarations
385 extern "C"
386 {
387 void get_stackframe_callback(StackDump *dump)
388 {
389 // skip over frames up to including wxOnAssert()
390 dump->ProcessFrames(3);
391 }
392 }
393
394 #endif // wxUSE_STACKWALKER
395
396 bool wxGUIAppTraits::ShowAssertDialog(const wxString& msg)
397 {
398 // under GTK2 we prefer to use a dialog widget written using directly GTK+;
399 // in fact we cannot use a dialog written using wxWidgets: it would need
400 // the wxWidgets idle processing to work correctly!
401 GtkWidget *dialog = gtk_assert_dialog_new();
402 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog), msg.mb_str());
403
404 #if wxUSE_STACKWALKER
405 // don't show more than maxLines or we could get a dialog too tall to be
406 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
407 // characters it is still only 300 pixels...
408 static const int maxLines = 20;
409
410 // save current stack frame...
411 StackDump dump(GTK_ASSERT_DIALOG(dialog));
412 dump.SaveStack(maxLines);
413
414 // ...but process it only if the user needs it
415 gtk_assert_dialog_set_backtrace_callback(GTK_ASSERT_DIALOG(dialog),
416 (GtkAssertDialogStackFrameCallback)get_stackframe_callback,
417 &dump);
418 #endif // wxUSE_STACKWALKER
419
420 gint result = gtk_dialog_run(GTK_DIALOG (dialog));
421 bool returnCode = false;
422 switch (result)
423 {
424 case GTK_ASSERT_DIALOG_STOP:
425 wxTrap();
426 break;
427 case GTK_ASSERT_DIALOG_CONTINUE:
428 // nothing to do
429 break;
430 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING:
431 // no more asserts
432 returnCode = true;
433 break;
434
435 default:
436 wxFAIL_MSG( _T("unexpected return code from GtkAssertDialog") );
437 }
438
439 gtk_widget_destroy(dialog);
440 return returnCode;
441 }
442
443 #endif // __WXDEBUG__
444
445 wxString wxGUIAppTraits::GetDesktopEnvironment() const
446 {
447 #if wxUSE_DETECT_SM
448 const wxString SM = GetSM();
449
450 if (SM == wxT("GnomeSM"))
451 return wxT("GNOME");
452
453 if (SM == wxT("KDE"))
454 return wxT("KDE");
455 #endif // wxUSE_DETECT_SM
456
457 return wxEmptyString;
458 }
459
460
461