added wxConvUI which determines the conversion used for the UI elements and can be...
[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 #include "wx/string.h"
15
16 #include "wx/apptrait.h"
17 #include "wx/intl.h"
18 #include "wx/log.h"
19
20 #include "wx/process.h"
21
22 #include "wx/unix/execute.h"
23
24 #include <stdarg.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <sys/wait.h> // for WNOHANG
29 #include <unistd.h>
30
31 #include "glib.h"
32 #include "gdk/gdk.h"
33 #include "gtk/gtk.h"
34 #include "gdk/gdkx.h"
35
36 #ifdef HAVE_X11_XKBLIB_H
37 /* under HP-UX and Solaris 2.6, at least, XKBlib.h defines structures with
38 * field named "explicit" - which is, of course, an error for a C++
39 * compiler. To be on the safe side, just redefine it everywhere. */
40 #define explicit __wx_explicit
41
42 #include "X11/XKBlib.h"
43
44 #undef explicit
45 #endif // HAVE_X11_XKBLIB_H
46
47 //-----------------------------------------------------------------------------
48 // data
49 //-----------------------------------------------------------------------------
50
51 extern GtkWidget *wxGetRootWindow();
52
53 //----------------------------------------------------------------------------
54 // misc.
55 //----------------------------------------------------------------------------
56 #ifndef __EMX__
57 // on OS/2, we use the wxBell from wxBase library
58
59 void wxBell()
60 {
61 gdk_beep();
62 }
63 #endif
64
65 /* Don't synthesize KeyUp events holding down a key and producing
66 KeyDown events with autorepeat. */
67 #ifdef HAVE_X11_XKBLIB_H
68 bool wxSetDetectableAutoRepeat( bool flag )
69 {
70 Bool result;
71 XkbSetDetectableAutoRepeat( GDK_DISPLAY(), flag, &result );
72 return result; /* TRUE if keyboard hardware supports this mode */
73 }
74 #else
75 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) )
76 {
77 return FALSE;
78 }
79 #endif
80
81 // Escapes string so that it is valid Pango markup XML string:
82 wxString wxEscapeStringForPangoMarkup(const wxString& str)
83 {
84 size_t len = str.length();
85 wxString out;
86 out.Alloc(len);
87 for (size_t i = 0; i < len; i++)
88 {
89 wxChar c = str[i];
90 switch (c)
91 {
92 case _T('&'):
93 out << _T("&amp;");
94 break;
95 case _T('<'):
96 out << _T("&lt;");
97 break;
98 case _T('>'):
99 out << _T("&gt;");
100 break;
101 case _T('\''):
102 out << _T("&apos;");
103 break;
104 case _T('"'):
105 out << _T("&quot;");
106 break;
107 default:
108 out << c;
109 break;
110 }
111 }
112 return out;
113 }
114
115
116 // ----------------------------------------------------------------------------
117 // display characterstics
118 // ----------------------------------------------------------------------------
119
120 void *wxGetDisplay()
121 {
122 return GDK_DISPLAY();
123 }
124
125 void wxDisplaySize( int *width, int *height )
126 {
127 if (width) *width = gdk_screen_width();
128 if (height) *height = gdk_screen_height();
129 }
130
131 void wxDisplaySizeMM( int *width, int *height )
132 {
133 if (width) *width = gdk_screen_width_mm();
134 if (height) *height = gdk_screen_height_mm();
135 }
136
137 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
138 {
139 // This is supposed to return desktop dimensions minus any window
140 // manager panels, menus, taskbars, etc. If there is a way to do that
141 // for this platform please fix this function, otherwise it defaults
142 // to the entire desktop.
143 if (x) *x = 0;
144 if (y) *y = 0;
145 wxDisplaySize(width, height);
146 }
147
148 void wxGetMousePosition( int* x, int* y )
149 {
150 gdk_window_get_pointer( (GdkWindow*) NULL, x, y, (GdkModifierType*) NULL );
151 }
152
153 bool wxColourDisplay()
154 {
155 return TRUE;
156 }
157
158 int wxDisplayDepth()
159 {
160 return gdk_drawable_get_visual( wxGetRootWindow()->window )->depth;
161 }
162
163 wxToolkitInfo& wxGUIAppTraits::GetToolkitInfo()
164 {
165 static wxToolkitInfo info;
166 info.shortName = _T("gtk2");
167 info.name = _T("wxGTK");
168 #ifdef __WXUNIVERSAL__
169 info.shortName << _T("univ");
170 info.name << _T("/wxUniversal");
171 #endif
172 info.versionMajor = gtk_major_version;
173 info.versionMinor = gtk_minor_version;
174 info.os = wxGTK;
175 return info;
176 }
177
178 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
179 {
180 return wxGenericFindWindowAtPoint(pt);
181 }
182
183 #if !wxUSE_UNICODE
184
185 wxCharBuffer wxConvertToGTK(const wxString& s, wxFontEncoding enc)
186 {
187 if ( enc == wxFONTENCODING_UTF8 )
188 {
189 // no need for conversion at all
190 return wxCharBuffer(s);
191 }
192
193 wxWCharBuffer wbuf;
194 if ( enc == wxFONTENCODING_SYSTEM || enc == wxFONTENCODING_DEFAULT )
195 {
196 wbuf = wxConvUI->cMB2WC(s);
197 }
198 else // another encoding, use generic conversion class
199 {
200 wbuf = wxCSConv(enc).cMB2WC(s);
201 }
202
203 wxCharBuffer buf;
204 if ( wbuf )
205 buf = wxConvUTF8.cWC2MB(wbuf);
206
207 return buf;
208 }
209
210 #endif // !wxUSE_UNICODE
211
212 // ----------------------------------------------------------------------------
213 // subprocess routines
214 // ----------------------------------------------------------------------------
215
216 extern "C" {
217 static
218 void GTK_EndProcessDetector(gpointer data, gint source,
219 GdkInputCondition WXUNUSED(condition) )
220 {
221 wxEndProcessData *proc_data = (wxEndProcessData *)data;
222
223 // has the process really terminated? unfortunately GDK (or GLib) seem to
224 // generate G_IO_HUP notification even when it simply tries to read from a
225 // closed fd and hasn't terminated at all
226 int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
227 int status = 0;
228 int rc = waitpid(pid, &status, WNOHANG);
229
230 if ( rc == 0 )
231 {
232 // no, it didn't exit yet, continue waiting
233 return;
234 }
235
236 // set exit code to -1 if something bad happened
237 proc_data->exitcode = rc != -1 && WIFEXITED(status) ? WEXITSTATUS(status)
238 : -1;
239
240 // child exited, end waiting
241 close(source);
242
243 // don't call us again!
244 gdk_input_remove(proc_data->tag);
245
246 wxHandleProcessTermination(proc_data);
247 }
248 }
249
250 int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
251 {
252 int tag = gdk_input_add(fd,
253 GDK_INPUT_READ,
254 GTK_EndProcessDetector,
255 (gpointer)proc_data);
256
257 return tag;
258 }
259