]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
dbd25330 | 2 | // Name: src/gtk/utilsgtk.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
dfcb1ae0 | 5 | // Id: $Id$ |
6c9a19aa | 6 | // Copyright: (c) 1998 Robert Roebling |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
c801d85f | 13 | #include "wx/utils.h" |
df91131c WS |
14 | |
15 | #ifndef WX_PRECOMP | |
16 | #include "wx/string.h" | |
88a7a4e1 | 17 | #include "wx/intl.h" |
e4db172a | 18 | #include "wx/log.h" |
df91131c | 19 | #endif |
c801d85f | 20 | |
46446cc2 | 21 | #include "wx/apptrait.h" |
02847e59 | 22 | |
5336ece4 VZ |
23 | #include "wx/process.h" |
24 | ||
518b5d2f VZ |
25 | #include "wx/unix/execute.h" |
26 | ||
db9febdf RR |
27 | #ifdef __WXDEBUG__ |
28 | #if wxUSE_STACKWALKER | |
29 | #include "wx/gtk/assertdlg_gtk.h" | |
30 | #include "wx/stackwalk.h" | |
31 | #endif // wxUSE_STACKWALKER | |
32 | #endif // __WXDEBUG__ | |
33 | ||
c801d85f | 34 | #include <stdarg.h> |
c801d85f KB |
35 | #include <string.h> |
36 | #include <sys/stat.h> | |
37 | #include <sys/types.h> | |
dbd25330 | 38 | #include <sys/wait.h> // for WNOHANG |
c801d85f | 39 | #include <unistd.h> |
91b8de8d RR |
40 | |
41 | #include "glib.h" | |
42 | #include "gdk/gdk.h" | |
43 | #include "gtk/gtk.h" | |
91b8de8d | 44 | #include "gdk/gdkx.h" |
88ac883a | 45 | |
27933891 | 46 | #ifdef HAVE_X11_XKBLIB_H |
154c4aa1 VZ |
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. */ | |
88ac883a | 50 | #define explicit __wx_explicit |
ec5d7799 | 51 | |
154c4aa1 | 52 | #include "X11/XKBlib.h" |
ec5d7799 | 53 | |
88ac883a | 54 | #undef explicit |
27933891 | 55 | #endif // HAVE_X11_XKBLIB_H |
dfcb1ae0 | 56 | |
88bbc332 RR |
57 | |
58 | #if wxUSE_DETECT_SM | |
59 | #include "X11/Xlib.h" | |
60 | #include "X11/SM/SMlib.h" | |
61 | #endif | |
62 | ||
d76fe38b RR |
63 | //----------------------------------------------------------------------------- |
64 | // data | |
65 | //----------------------------------------------------------------------------- | |
66 | ||
c2fa61e8 | 67 | extern GtkWidget *wxGetRootWindow(); |
d76fe38b RR |
68 | |
69 | //---------------------------------------------------------------------------- | |
c801d85f | 70 | // misc. |
d76fe38b | 71 | //---------------------------------------------------------------------------- |
189d1ae7 SN |
72 | #ifndef __EMX__ |
73 | // on OS/2, we use the wxBell from wxBase library | |
c801d85f | 74 | |
518b5d2f | 75 | void wxBell() |
c801d85f | 76 | { |
e52f60e6 RR |
77 | gdk_beep(); |
78 | } | |
189d1ae7 | 79 | #endif |
c801d85f | 80 | |
27933891 RR |
81 | /* Don't synthesize KeyUp events holding down a key and producing |
82 | KeyDown events with autorepeat. */ | |
83 | #ifdef HAVE_X11_XKBLIB_H | |
f0492f7d RR |
84 | bool wxSetDetectableAutoRepeat( bool flag ) |
85 | { | |
86 | Bool result; | |
87 | XkbSetDetectableAutoRepeat( GDK_DISPLAY(), flag, &result ); | |
df91131c | 88 | return result; /* true if keyboard hardware supports this mode */ |
27933891 RR |
89 | } |
90 | #else | |
91 | bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) ) | |
92 | { | |
df91131c | 93 | return false; |
f0492f7d | 94 | } |
27933891 | 95 | #endif |
f0492f7d | 96 | |
66bd83b4 VS |
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("&"); | |
110 | break; | |
111 | case _T('<'): | |
112 | out << _T("<"); | |
113 | break; | |
114 | case _T('>'): | |
115 | out << _T(">"); | |
116 | break; | |
117 | case _T('\''): | |
118 | out << _T("'"); | |
119 | break; | |
120 | case _T('"'): | |
121 | out << _T("""); | |
122 | break; | |
123 | default: | |
124 | out << c; | |
125 | break; | |
126 | } | |
127 | } | |
128 | return out; | |
129 | } | |
66bd83b4 VS |
130 | |
131 | ||
518b5d2f VZ |
132 | // ---------------------------------------------------------------------------- |
133 | // display characterstics | |
134 | // ---------------------------------------------------------------------------- | |
82052aff | 135 | |
d111a89a VZ |
136 | void *wxGetDisplay() |
137 | { | |
27df579a | 138 | return GDK_DISPLAY(); |
d111a89a VZ |
139 | } |
140 | ||
c0392997 RR |
141 | void wxDisplaySize( int *width, int *height ) |
142 | { | |
e52f60e6 RR |
143 | if (width) *width = gdk_screen_width(); |
144 | if (height) *height = gdk_screen_height(); | |
c0392997 RR |
145 | } |
146 | ||
904a68b6 RL |
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 | ||
ec5d7799 RD |
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 | ||
6de97a3b RR |
164 | void wxGetMousePosition( int* x, int* y ) |
165 | { | |
bbe0af5b | 166 | gdk_window_get_pointer( (GdkWindow*) NULL, x, y, (GdkModifierType*) NULL ); |
e52f60e6 | 167 | } |
6de97a3b | 168 | |
518b5d2f | 169 | bool wxColourDisplay() |
6de97a3b | 170 | { |
df91131c | 171 | return true; |
6de97a3b RR |
172 | } |
173 | ||
518b5d2f | 174 | int wxDisplayDepth() |
6de97a3b | 175 | { |
22a3bce4 | 176 | return gdk_drawable_get_visual( wxGetRootWindow()->window )->depth; |
6de97a3b RR |
177 | } |
178 | ||
57591e0e JS |
179 | wxWindow* wxFindWindowAtPoint(const wxPoint& pt) |
180 | { | |
181 | return wxGenericFindWindowAtPoint(pt); | |
182 | } | |
183 | ||
5f11fef5 VZ |
184 | #if !wxUSE_UNICODE |
185 | ||
186 | wxCharBuffer wxConvertToGTK(const wxString& s, wxFontEncoding enc) | |
187 | { | |
27dee9ae | 188 | wxCharBuffer buf; |
5f11fef5 VZ |
189 | if ( enc == wxFONTENCODING_UTF8 ) |
190 | { | |
27dee9ae VZ |
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 | } | |
12bc5f9a | 214 | } |
27dee9ae | 215 | else // !UTF-8 |
12bc5f9a | 216 | { |
27dee9ae VZ |
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 | } | |
12bc5f9a | 226 | |
27dee9ae VZ |
227 | if ( wbuf ) |
228 | buf = wxConvUTF8.cWC2MB(wbuf); | |
229 | } | |
5f11fef5 VZ |
230 | |
231 | return buf; | |
232 | } | |
233 | ||
234 | #endif // !wxUSE_UNICODE | |
57591e0e | 235 | |
518b5d2f | 236 | // ---------------------------------------------------------------------------- |
c801d85f | 237 | // subprocess routines |
518b5d2f | 238 | // ---------------------------------------------------------------------------- |
cf447356 | 239 | |
865bb325 VZ |
240 | extern "C" { |
241 | static | |
90350682 VZ |
242 | void GTK_EndProcessDetector(gpointer data, gint source, |
243 | GdkInputCondition WXUNUSED(condition) ) | |
cf447356 | 244 | { |
ab857a4e | 245 | wxEndProcessData *proc_data = (wxEndProcessData *)data; |
dbd25330 VZ |
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); | |
447f908a VZ |
251 | int status = 0; |
252 | int rc = waitpid(pid, &status, WNOHANG); | |
253 | ||
254 | if ( rc == 0 ) | |
dbd25330 VZ |
255 | { |
256 | // no, it didn't exit yet, continue waiting | |
257 | return; | |
258 | } | |
259 | ||
447f908a VZ |
260 | // set exit code to -1 if something bad happened |
261 | proc_data->exitcode = rc != -1 && WIFEXITED(status) ? WEXITSTATUS(status) | |
262 | : -1; | |
263 | ||
dbd25330 | 264 | // child exited, end waiting |
ab857a4e | 265 | close(source); |
dbd25330 VZ |
266 | |
267 | // don't call us again! | |
ab857a4e KB |
268 | gdk_input_remove(proc_data->tag); |
269 | ||
ab857a4e | 270 | wxHandleProcessTermination(proc_data); |
3069ac4e | 271 | } |
865bb325 | 272 | } |
cf447356 | 273 | |
518b5d2f | 274 | int wxAddProcessCallback(wxEndProcessData *proc_data, int fd) |
c801d85f | 275 | { |
518b5d2f VZ |
276 | int tag = gdk_input_add(fd, |
277 | GDK_INPUT_READ, | |
278 | GTK_EndProcessDetector, | |
279 | (gpointer)proc_data); | |
c801d85f | 280 | |
518b5d2f | 281 | return tag; |
3069ac4e | 282 | } |
8bb6b2c0 VZ |
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 | } | |
88bbc332 RR |
299 | |
300 | #if wxUSE_DETECT_SM | |
301 | static wxString GetSM() | |
302 | { | |
391bf008 | 303 | class Dpy |
cb1bf052 | 304 | { |
391bf008 VZ |
305 | public: |
306 | Dpy() { m_dpy = XOpenDisplay(NULL); } | |
307 | ~Dpy() { if ( m_dpy ) XCloseDisplay(m_dpy); } | |
88bbc332 | 308 | |
391bf008 VZ |
309 | operator Display *() const { return m_dpy; } |
310 | private: | |
311 | Display *m_dpy; | |
312 | } dpy; | |
88bbc332 | 313 | |
391bf008 VZ |
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; | |
88bbc332 | 335 | } |
391bf008 | 336 | #endif // wxUSE_DETECT_SM |
88bbc332 | 337 | |
db9febdf RR |
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 | dump->ProcessFrames(2); // don't show ShowAssertDialog() call itself | |
390 | } | |
391 | } | |
392 | ||
393 | #endif // wxUSE_STACKWALKER | |
394 | ||
395 | bool wxGUIAppTraits::ShowAssertDialog(const wxString& msg) | |
396 | { | |
397 | // under GTK2 we prefer to use a dialog widget written using directly GTK+; | |
398 | // in fact we cannot use a dialog written using wxWidgets: it would need | |
399 | // the wxWidgets idle processing to work correctly! | |
400 | GtkWidget *dialog = gtk_assert_dialog_new(); | |
401 | gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog), msg.mb_str()); | |
402 | ||
403 | #if wxUSE_STACKWALKER | |
404 | // don't show more than maxLines or we could get a dialog too tall to be | |
405 | // shown on screen: 20 should be ok everywhere as even with 15 pixel high | |
406 | // characters it is still only 300 pixels... | |
407 | static const int maxLines = 20; | |
408 | ||
409 | // save current stack frame... | |
410 | StackDump dump(GTK_ASSERT_DIALOG(dialog)); | |
411 | dump.SaveStack(maxLines); | |
412 | ||
413 | // ...but process it only if the user needs it | |
414 | gtk_assert_dialog_set_backtrace_callback(GTK_ASSERT_DIALOG(dialog), | |
415 | (GtkAssertDialogStackFrameCallback)get_stackframe_callback, | |
416 | &dump); | |
417 | #endif // wxUSE_STACKWALKER | |
418 | ||
419 | gint result = gtk_dialog_run(GTK_DIALOG (dialog)); | |
420 | bool returnCode = false; | |
421 | switch (result) | |
422 | { | |
423 | case GTK_ASSERT_DIALOG_STOP: | |
424 | wxTrap(); | |
425 | break; | |
426 | case GTK_ASSERT_DIALOG_CONTINUE: | |
427 | // nothing to do | |
428 | break; | |
429 | case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING: | |
430 | // no more asserts | |
431 | returnCode = true; | |
432 | break; | |
433 | ||
434 | default: | |
435 | wxFAIL_MSG( _T("unexpected return code from GtkAssertDialog") ); | |
436 | } | |
437 | ||
438 | gtk_widget_destroy(dialog); | |
439 | return returnCode; | |
440 | } | |
441 | ||
442 | #endif // __WXDEBUG__ | |
443 | ||
88bbc332 RR |
444 | wxString wxGUIAppTraits::GetDesktopEnvironment() const |
445 | { | |
446 | #if wxUSE_DETECT_SM | |
391bf008 VZ |
447 | const wxString SM = GetSM(); |
448 | ||
88bbc332 RR |
449 | if (SM == wxT("GnomeSM")) |
450 | return wxT("GNOME"); | |
391bf008 | 451 | |
88bbc332 RR |
452 | if (SM == wxT("KDE")) |
453 | return wxT("KDE"); | |
391bf008 | 454 | #endif // wxUSE_DETECT_SM |
88bbc332 RR |
455 | |
456 | return wxEmptyString; | |
457 | } | |
458 | ||
459 | ||
460 |