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