Implemented wxGetKeyState for wxGTK (and wxX11 too, but I havn't
[wxWidgets.git] / src / x11 / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose: Various utilities
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/setup.h"
21 #include "wx/utils.h"
22 #include "wx/app.h"
23 #include "wx/apptrait.h"
24 #include "wx/msgdlg.h"
25 #include "wx/cursor.h"
26 #include "wx/window.h" // for wxTopLevelWindows
27
28 #include <ctype.h>
29 #include <stdarg.h>
30 #include <dirent.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <sys/wait.h>
36 #include <pwd.h>
37 #include <errno.h>
38 // #include <netdb.h>
39 #include <signal.h>
40
41 #if (defined(__SUNCC__) || defined(__CLCC__))
42 #include <sysent.h>
43 #endif
44
45 #ifdef __VMS__
46 #pragma message disable nosimpint
47 #endif
48
49 #include "wx/unix/execute.h"
50
51 #include "wx/x11/private.h"
52
53 #include "X11/Xutil.h"
54
55 #ifdef __VMS__
56 #pragma message enable nosimpint
57 #endif
58
59 // ----------------------------------------------------------------------------
60 // async event processing
61 // ----------------------------------------------------------------------------
62
63 // Consume all events until no more left
64 void wxFlushEvents()
65 {
66 Display *display = (Display*) wxGetDisplay();
67
68 XSync (display, FALSE);
69
70 // TODO for X11
71 // ??
72 }
73
74 bool wxCheckForInterrupt(wxWindow *wnd)
75 {
76 wxASSERT_MSG(FALSE, wxT("wxCheckForInterrupt not yet implemented."));
77 return FALSE;
78 }
79
80 // ----------------------------------------------------------------------------
81 // wxExecute stuff
82 // ----------------------------------------------------------------------------
83
84 int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
85 {
86 // TODO
87 return 0;
88 }
89
90 // ----------------------------------------------------------------------------
91 // misc
92 // ----------------------------------------------------------------------------
93
94 // Emit a beeeeeep
95 #ifndef __EMX__
96 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
97 void wxBell()
98 {
99 // Use current setting for the bell
100 XBell ((Display*) wxGetDisplay(), 0);
101 }
102 #endif
103
104 wxToolkitInfo& wxGUIAppTraits::GetToolkitInfo()
105 {
106 static wxToolkitInfo info;
107 info.shortName = _T("x11univ");
108 info.name = _T("wxX11");
109 info.versionMajor = 0;
110 info.versionMinor = 0;
111 info.os = wxX11;
112 return info;
113 }
114
115 // ----------------------------------------------------------------------------
116 // display info
117 // ----------------------------------------------------------------------------
118
119 void wxGetMousePosition( int* x, int* y )
120 {
121 #if wxUSE_NANOX
122 // TODO
123 *x = 0;
124 *y = 0;
125 #else
126 XMotionEvent xev;
127 Window root, child;
128 XQueryPointer((Display*) wxGetDisplay(),
129 DefaultRootWindow((Display*) wxGetDisplay()),
130 &root, &child,
131 &(xev.x_root), &(xev.y_root),
132 &(xev.x), &(xev.y),
133 &(xev.state));
134 *x = xev.x_root;
135 *y = xev.y_root;
136 #endif
137 };
138
139 // Return TRUE if we have a colour display
140 bool wxColourDisplay()
141 {
142 return wxDisplayDepth() > 1;
143 }
144
145 // Returns depth of screen
146 int wxDisplayDepth()
147 {
148 Display *dpy = (Display*) wxGetDisplay();
149
150 return DefaultDepth (dpy, DefaultScreen (dpy));
151 }
152
153 // Get size of display
154 void wxDisplaySize(int *width, int *height)
155 {
156 Display *dpy = (Display*) wxGetDisplay();
157
158 if ( width )
159 *width = DisplayWidth (dpy, DefaultScreen (dpy));
160 if ( height )
161 *height = DisplayHeight (dpy, DefaultScreen (dpy));
162 }
163
164 void wxDisplaySizeMM(int *width, int *height)
165 {
166 Display *dpy = (Display*) wxGetDisplay();
167
168 if ( width )
169 *width = DisplayWidthMM(dpy, DefaultScreen (dpy));
170 if ( height )
171 *height = DisplayHeightMM(dpy, DefaultScreen (dpy));
172 }
173
174 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
175 {
176 // This is supposed to return desktop dimensions minus any window
177 // manager panels, menus, taskbars, etc. If there is a way to do that
178 // for this platform please fix this function, otherwise it defaults
179 // to the entire desktop.
180 if (x) *x = 0;
181 if (y) *y = 0;
182 wxDisplaySize(width, height);
183 }
184
185
186 // Configurable display in wxX11 and wxMotif
187 static WXDisplay *gs_currentDisplay = NULL;
188 static wxString gs_displayName;
189
190 WXDisplay *wxGetDisplay()
191 {
192 if (gs_currentDisplay)
193 return gs_currentDisplay;
194 return wxApp::GetDisplay();
195 }
196
197 bool wxSetDisplay(const wxString& display_name)
198 {
199 gs_displayName = display_name;
200
201 if ( display_name.IsEmpty() )
202 {
203 gs_currentDisplay = NULL;
204
205 return TRUE;
206 }
207 else
208 {
209 Display* display = XOpenDisplay((char*) display_name.c_str());
210
211 if (display)
212 {
213 gs_currentDisplay = (WXDisplay*) display;
214 return TRUE;
215 }
216 else
217 return FALSE;
218 }
219 }
220
221 wxString wxGetDisplayName()
222 {
223 return gs_displayName;
224 }
225
226 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
227 {
228 return wxGenericFindWindowAtPoint(pt);
229 }
230
231 // ----------------------------------------------------------------------------
232 // Some colour manipulation routines
233 // ----------------------------------------------------------------------------
234
235 void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
236 {
237 int h = hsv->h;
238 int s = hsv->s;
239 int v = hsv->v;
240 int r = 0, g = 0, b = 0;
241 int i, f;
242 int p, q, t;
243 s = (s * wxMAX_RGB) / wxMAX_SV;
244 v = (v * wxMAX_RGB) / wxMAX_SV;
245 if (h == 360) h = 0;
246 if (s == 0) { h = 0; r = g = b = v; }
247 i = h / 60;
248 f = h % 60;
249 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
250 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
251 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
252 switch (i)
253 {
254 case 0: r = v, g = t, b = p; break;
255 case 1: r = q, g = v, b = p; break;
256 case 2: r = p, g = v, b = t; break;
257 case 3: r = p, g = q, b = v; break;
258 case 4: r = t, g = p, b = v; break;
259 case 5: r = v, g = p, b = q; break;
260 }
261 rgb->red = r << 8;
262 rgb->green = g << 8;
263 rgb->blue = b << 8;
264 }
265
266 void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
267 {
268 int r = rgb->red >> 8;
269 int g = rgb->green >> 8;
270 int b = rgb->blue >> 8;
271 int maxv = wxMax3(r, g, b);
272 int minv = wxMin3(r, g, b);
273 int h = 0, s, v;
274 v = maxv;
275 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
276 else s = 0;
277 if (s == 0) h = 0;
278 else
279 {
280 int rc, gc, bc, hex = 0;
281 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
282 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
283 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
284 if (r == maxv) { h = bc - gc, hex = 0; }
285 else if (g == maxv) { h = rc - bc, hex = 2; }
286 else if (b == maxv) { h = gc - rc, hex = 4; }
287 h = hex * 60 + (h * 60 / wxMAX_RGB);
288 if (h < 0) h += 360;
289 }
290 hsv->h = h;
291 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
292 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
293 }
294
295 void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
296 {
297 #if !wxUSE_NANOX
298 int llp;
299
300 int screen = DefaultScreen(d);
301 int num_colors = DisplayCells(d,screen);
302
303 XColor *color_defs = new XColor[num_colors];
304 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
305 XQueryColors(d,cmp,color_defs,num_colors);
306
307 wxHSV hsv_defs, hsv;
308 wxXColorToHSV(&hsv,xc);
309
310 int diff, min_diff = 0, pixel = 0;
311
312 for(llp = 0;llp < num_colors;llp++)
313 {
314 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
315 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
316 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
317 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
318 if (llp == 0) min_diff = diff;
319 if (min_diff > diff) { min_diff = diff; pixel = llp; }
320 if (min_diff == 0) break;
321 }
322
323 xc -> red = color_defs[pixel].red;
324 xc -> green = color_defs[pixel].green;
325 xc -> blue = color_defs[pixel].blue;
326 xc -> flags = DoRed | DoGreen | DoBlue;
327
328 /* FIXME, TODO
329 if (!XAllocColor(d,cmp,xc))
330 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
331 */
332
333 delete[] color_defs;
334 #endif
335 }
336
337 void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
338 {
339 if (!XAllocColor(d,cmp,xc))
340 {
341 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
342 wxAllocNearestColor(d,cmp,xc);
343 }
344 }
345
346 #ifdef __WXDEBUG__
347 wxString wxGetXEventName(XEvent& event)
348 {
349 #if wxUSE_NANOX
350 wxString str(wxT("(some event)"));
351 return str;
352 #else
353 int type = event.xany.type;
354 static char* event_name[] = {
355 "", "unknown(-)", // 0-1
356 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
357 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
358 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
359 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
360 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
361 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
362 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
363 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
364 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
365 "ClientMessage", "MappingNotify", // 33-34
366 "unknown(+)"}; // 35
367 type = wxMin(35, type); type = wxMax(1, type);
368 return wxString::FromAscii(event_name[type]);
369 #endif
370 }
371 #endif
372
373 bool wxWindowIsVisible(Window win)
374 {
375 XWindowAttributes wa;
376 XGetWindowAttributes(wxGlobalDisplay(), win, &wa);
377
378 return (wa.map_state == IsViewable);
379 }