]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/utilsgtk.cpp
ensure that wxToolBar has one of wxTB_TOP/LEFT/RIGHT/BOTTOM styles set, otherwise...
[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 #include <stdarg.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/wait.h> // for WNOHANG
32 #include <unistd.h>
33
34 #include "glib.h"
35 #include "gdk/gdk.h"
36 #include "gtk/gtk.h"
37 #include "gdk/gdkx.h"
38
39 #ifdef HAVE_X11_XKBLIB_H
40 /* under HP-UX and Solaris 2.6, at least, XKBlib.h defines structures with
41 * field named "explicit" - which is, of course, an error for a C++
42 * compiler. To be on the safe side, just redefine it everywhere. */
43 #define explicit __wx_explicit
44
45 #include "X11/XKBlib.h"
46
47 #undef explicit
48 #endif // HAVE_X11_XKBLIB_H
49
50
51 #if wxUSE_DETECT_SM
52 #include "X11/Xlib.h"
53 #include "X11/SM/SMlib.h"
54 #endif
55
56 //-----------------------------------------------------------------------------
57 // data
58 //-----------------------------------------------------------------------------
59
60 extern GtkWidget *wxGetRootWindow();
61
62 //----------------------------------------------------------------------------
63 // misc.
64 //----------------------------------------------------------------------------
65 #ifndef __EMX__
66 // on OS/2, we use the wxBell from wxBase library
67
68 void wxBell()
69 {
70 gdk_beep();
71 }
72 #endif
73
74 /* Don't synthesize KeyUp events holding down a key and producing
75 KeyDown events with autorepeat. */
76 #ifdef HAVE_X11_XKBLIB_H
77 bool wxSetDetectableAutoRepeat( bool flag )
78 {
79 Bool result;
80 XkbSetDetectableAutoRepeat( GDK_DISPLAY(), flag, &result );
81 return result; /* true if keyboard hardware supports this mode */
82 }
83 #else
84 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) )
85 {
86 return false;
87 }
88 #endif
89
90 // Escapes string so that it is valid Pango markup XML string:
91 wxString wxEscapeStringForPangoMarkup(const wxString& str)
92 {
93 size_t len = str.length();
94 wxString out;
95 out.Alloc(len);
96 for (size_t i = 0; i < len; i++)
97 {
98 wxChar c = str[i];
99 switch (c)
100 {
101 case _T('&'):
102 out << _T("&amp;");
103 break;
104 case _T('<'):
105 out << _T("&lt;");
106 break;
107 case _T('>'):
108 out << _T("&gt;");
109 break;
110 case _T('\''):
111 out << _T("&apos;");
112 break;
113 case _T('"'):
114 out << _T("&quot;");
115 break;
116 default:
117 out << c;
118 break;
119 }
120 }
121 return out;
122 }
123
124
125 // ----------------------------------------------------------------------------
126 // display characterstics
127 // ----------------------------------------------------------------------------
128
129 void *wxGetDisplay()
130 {
131 return GDK_DISPLAY();
132 }
133
134 void wxDisplaySize( int *width, int *height )
135 {
136 if (width) *width = gdk_screen_width();
137 if (height) *height = gdk_screen_height();
138 }
139
140 void wxDisplaySizeMM( int *width, int *height )
141 {
142 if (width) *width = gdk_screen_width_mm();
143 if (height) *height = gdk_screen_height_mm();
144 }
145
146 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
147 {
148 // This is supposed to return desktop dimensions minus any window
149 // manager panels, menus, taskbars, etc. If there is a way to do that
150 // for this platform please fix this function, otherwise it defaults
151 // to the entire desktop.
152 if (x) *x = 0;
153 if (y) *y = 0;
154 wxDisplaySize(width, height);
155 }
156
157 void wxGetMousePosition( int* x, int* y )
158 {
159 gdk_window_get_pointer( (GdkWindow*) NULL, x, y, (GdkModifierType*) NULL );
160 }
161
162 bool wxColourDisplay()
163 {
164 return true;
165 }
166
167 int wxDisplayDepth()
168 {
169 return gdk_drawable_get_visual( wxGetRootWindow()->window )->depth;
170 }
171
172 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
173 {
174 return wxGenericFindWindowAtPoint(pt);
175 }
176
177 #if !wxUSE_UNICODE
178
179 wxCharBuffer wxConvertToGTK(const wxString& s, wxFontEncoding enc)
180 {
181 wxCharBuffer buf;
182 if ( enc == wxFONTENCODING_UTF8 )
183 {
184 // no need for conversion at all, but do check that we have a valid
185 // UTF-8 string because passing invalid UTF-8 to GTK+ is going to
186 // result in a GTK+ error message and, especially, loss of data which
187 // was supposed to be shown in the GUI
188 if ( wxConvUTF8.ToWChar(NULL, 0, s, s.length()) == wxCONV_FAILED )
189 {
190 // warn the programmer that something is probably wrong in his code
191 //
192 // NB: don't include the string in output because chances are that
193 // this invalid UTF-8 string could result in more errors itself
194 // if the application shows logs in the GUI and so we get into
195 // an infinite loop
196 wxLogDebug(_T("Invalid UTF-8 string in wxConvertToGTK()"));
197
198 // but still try to show at least something on the screen
199 wxMBConvUTF8 utf8permissive(wxMBConvUTF8::MAP_INVALID_UTF8_TO_OCTAL);
200 wxWCharBuffer wbuf(utf8permissive.cMB2WC(s));
201 buf = wxConvUTF8.cWC2MB(wbuf);
202 }
203 else // valid UTF-8 string, no need to convert
204 {
205 buf = wxCharBuffer(s);
206 }
207 }
208 else // !UTF-8
209 {
210 wxWCharBuffer wbuf;
211 if ( enc == wxFONTENCODING_SYSTEM || enc == wxFONTENCODING_DEFAULT )
212 {
213 wbuf = wxConvUI->cMB2WC(s);
214 }
215 else // another encoding, use generic conversion class
216 {
217 wbuf = wxCSConv(enc).cMB2WC(s);
218 }
219
220 if ( wbuf )
221 buf = wxConvUTF8.cWC2MB(wbuf);
222 }
223
224 return buf;
225 }
226
227 #endif // !wxUSE_UNICODE
228
229 // ----------------------------------------------------------------------------
230 // subprocess routines
231 // ----------------------------------------------------------------------------
232
233 extern "C" {
234 static
235 void GTK_EndProcessDetector(gpointer data, gint source,
236 GdkInputCondition WXUNUSED(condition) )
237 {
238 wxEndProcessData *proc_data = (wxEndProcessData *)data;
239
240 // has the process really terminated? unfortunately GDK (or GLib) seem to
241 // generate G_IO_HUP notification even when it simply tries to read from a
242 // closed fd and hasn't terminated at all
243 int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
244 int status = 0;
245 int rc = waitpid(pid, &status, WNOHANG);
246
247 if ( rc == 0 )
248 {
249 // no, it didn't exit yet, continue waiting
250 return;
251 }
252
253 // set exit code to -1 if something bad happened
254 proc_data->exitcode = rc != -1 && WIFEXITED(status) ? WEXITSTATUS(status)
255 : -1;
256
257 // child exited, end waiting
258 close(source);
259
260 // don't call us again!
261 gdk_input_remove(proc_data->tag);
262
263 wxHandleProcessTermination(proc_data);
264 }
265 }
266
267 int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
268 {
269 int tag = gdk_input_add(fd,
270 GDK_INPUT_READ,
271 GTK_EndProcessDetector,
272 (gpointer)proc_data);
273
274 return tag;
275 }
276
277
278
279 // ----------------------------------------------------------------------------
280 // wxPlatformInfo-related
281 // ----------------------------------------------------------------------------
282
283 wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
284 {
285 if ( verMaj )
286 *verMaj = gtk_major_version;
287 if ( verMin )
288 *verMin = gtk_minor_version;
289
290 return wxPORT_GTK;
291 }
292
293 #if wxUSE_DETECT_SM
294 static wxString GetSM()
295 {
296 class Dpy
297 {
298 public:
299 Dpy() { m_dpy = XOpenDisplay(NULL); }
300 ~Dpy() { if ( m_dpy ) XCloseDisplay(m_dpy); }
301
302 operator Display *() const { return m_dpy; }
303 private:
304 Display *m_dpy;
305 } dpy;
306
307 if ( !dpy )
308 return wxEmptyString;
309
310 char *client_id;
311 SmcConn smc_conn = SmcOpenConnection(NULL, NULL,
312 999, 999,
313 0 /* mask */, NULL /* callbacks */,
314 NULL, &client_id,
315 0, NULL);
316
317 if ( !smc_conn )
318 return wxEmptyString;
319
320 char *vendor = SmcVendor(smc_conn);
321 wxString ret = wxString::FromAscii( vendor );
322 free(vendor);
323
324 SmcCloseConnection(smc_conn, 0, NULL);
325 free(client_id);
326
327 return ret;
328 }
329 #endif // wxUSE_DETECT_SM
330
331 wxString wxGUIAppTraits::GetDesktopEnvironment() const
332 {
333 #if wxUSE_DETECT_SM
334 const wxString SM = GetSM();
335
336 if (SM == wxT("GnomeSM"))
337 return wxT("GNOME");
338
339 if (SM == wxT("KDE"))
340 return wxT("KDE");
341 #endif // wxUSE_DETECT_SM
342
343 return wxEmptyString;
344 }
345
346
347