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