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