]> git.saurik.com Git - wxWidgets.git/blob - src/x11/utils.cpp
wxStrdup(NULL) doesn't work
[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, "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 void wxBell()
96 {
97 // Use current setting for the bell
98 XBell ((Display*) wxGetDisplay(), 0);
99 }
100
101 wxToolkitInfo& wxGUIAppTraits::GetToolkitInfo()
102 {
103 static wxToolkitInfo info;
104 info.shortName = _T("x11univ");
105 info.name = _T("wxX11");
106 info.versionMajor = 0;
107 info.versionMinor = 0;
108 info.os = wxX11;
109 return info;
110 }
111
112 // ----------------------------------------------------------------------------
113 // display info
114 // ----------------------------------------------------------------------------
115
116 void wxGetMousePosition( int* x, int* y )
117 {
118 #if wxUSE_NANOX
119 // TODO
120 *x = 0;
121 *y = 0;
122 #else
123 XMotionEvent xev;
124 Window root, child;
125 XQueryPointer((Display*) wxGetDisplay(),
126 DefaultRootWindow((Display*) wxGetDisplay()),
127 &root, &child,
128 &(xev.x_root), &(xev.y_root),
129 &(xev.x), &(xev.y),
130 &(xev.state));
131 *x = xev.x_root;
132 *y = xev.y_root;
133 #endif
134 };
135
136 // Return TRUE if we have a colour display
137 bool wxColourDisplay()
138 {
139 return wxDisplayDepth() > 1;
140 }
141
142 // Returns depth of screen
143 int wxDisplayDepth()
144 {
145 Display *dpy = (Display*) wxGetDisplay();
146
147 return DefaultDepth (dpy, DefaultScreen (dpy));
148 }
149
150 // Get size of display
151 void wxDisplaySize(int *width, int *height)
152 {
153 Display *dpy = (Display*) wxGetDisplay();
154
155 if ( width )
156 *width = DisplayWidth (dpy, DefaultScreen (dpy));
157 if ( height )
158 *height = DisplayHeight (dpy, DefaultScreen (dpy));
159 }
160
161 void wxDisplaySizeMM(int *width, int *height)
162 {
163 Display *dpy = (Display*) wxGetDisplay();
164
165 if ( width )
166 *width = DisplayWidthMM(dpy, DefaultScreen (dpy));
167 if ( height )
168 *height = DisplayHeightMM(dpy, DefaultScreen (dpy));
169 }
170
171 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
172 {
173 // This is supposed to return desktop dimensions minus any window
174 // manager panels, menus, taskbars, etc. If there is a way to do that
175 // for this platform please fix this function, otherwise it defaults
176 // to the entire desktop.
177 if (x) *x = 0;
178 if (y) *y = 0;
179 wxDisplaySize(width, height);
180 }
181
182
183 // Configurable display in wxX11 and wxMotif
184 static WXDisplay *gs_currentDisplay = NULL;
185 static wxString gs_displayName;
186
187 WXDisplay *wxGetDisplay()
188 {
189 if (gs_currentDisplay)
190 return gs_currentDisplay;
191 return wxApp::GetDisplay();
192 }
193
194 bool wxSetDisplay(const wxString& display_name)
195 {
196 gs_displayName = display_name;
197
198 if ( display_name.IsEmpty() )
199 {
200 gs_currentDisplay = NULL;
201
202 return TRUE;
203 }
204 else
205 {
206 Display* display = XOpenDisplay((char*) display_name.c_str());
207
208 if (display)
209 {
210 gs_currentDisplay = (WXDisplay*) display;
211 return TRUE;
212 }
213 else
214 return FALSE;
215 }
216 }
217
218 wxString wxGetDisplayName()
219 {
220 return gs_displayName;
221 }
222
223 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
224 {
225 return wxGenericFindWindowAtPoint(pt);
226 }
227
228 // ----------------------------------------------------------------------------
229 // keycode translations
230 // ----------------------------------------------------------------------------
231
232 #include <X11/keysym.h>
233
234 // FIXME what about tables??
235
236 int wxCharCodeXToWX(KeySym keySym)
237 {
238 int id;
239 switch (keySym)
240 {
241 case XK_Shift_L:
242 case XK_Shift_R:
243 id = WXK_SHIFT; break;
244 case XK_Control_L:
245 case XK_Control_R:
246 id = WXK_CONTROL; break;
247 case XK_BackSpace:
248 id = WXK_BACK; break;
249 case XK_Delete:
250 id = WXK_DELETE; break;
251 case XK_Clear:
252 id = WXK_CLEAR; break;
253 case XK_Tab:
254 id = WXK_TAB; break;
255 case XK_numbersign:
256 id = '#'; break;
257 case XK_Return:
258 id = WXK_RETURN; break;
259 case XK_Escape:
260 id = WXK_ESCAPE; break;
261 case XK_Pause:
262 case XK_Break:
263 id = WXK_PAUSE; break;
264 case XK_Num_Lock:
265 id = WXK_NUMLOCK; break;
266 case XK_Scroll_Lock:
267 id = WXK_SCROLL; break;
268
269 case XK_Home:
270 id = WXK_HOME; break;
271 case XK_End:
272 id = WXK_END; break;
273 case XK_Left:
274 id = WXK_LEFT; break;
275 case XK_Right:
276 id = WXK_RIGHT; break;
277 case XK_Up:
278 id = WXK_UP; break;
279 case XK_Down:
280 id = WXK_DOWN; break;
281 case XK_Next:
282 id = WXK_NEXT; break;
283 case XK_Prior:
284 id = WXK_PRIOR; break;
285 case XK_Menu:
286 id = WXK_MENU; break;
287 case XK_Select:
288 id = WXK_SELECT; break;
289 case XK_Cancel:
290 id = WXK_CANCEL; break;
291 case XK_Print:
292 id = WXK_PRINT; break;
293 case XK_Execute:
294 id = WXK_EXECUTE; break;
295 case XK_Insert:
296 id = WXK_INSERT; break;
297 case XK_Help:
298 id = WXK_HELP; break;
299
300 case XK_KP_Multiply:
301 id = WXK_MULTIPLY; break;
302 case XK_KP_Add:
303 id = WXK_ADD; break;
304 case XK_KP_Subtract:
305 id = WXK_SUBTRACT; break;
306 case XK_KP_Divide:
307 id = WXK_DIVIDE; break;
308 case XK_KP_Decimal:
309 id = WXK_DECIMAL; break;
310 case XK_KP_Equal:
311 id = '='; break;
312 case XK_KP_Space:
313 id = ' '; break;
314 case XK_KP_Tab:
315 id = WXK_TAB; break;
316 case XK_KP_Enter:
317 id = WXK_RETURN; break;
318 case XK_KP_0:
319 id = WXK_NUMPAD0; break;
320 case XK_KP_1:
321 id = WXK_NUMPAD1; break;
322 case XK_KP_2:
323 id = WXK_NUMPAD2; break;
324 case XK_KP_3:
325 id = WXK_NUMPAD3; break;
326 case XK_KP_4:
327 id = WXK_NUMPAD4; break;
328 case XK_KP_5:
329 id = WXK_NUMPAD5; break;
330 case XK_KP_6:
331 id = WXK_NUMPAD6; break;
332 case XK_KP_7:
333 id = WXK_NUMPAD7; break;
334 case XK_KP_8:
335 id = WXK_NUMPAD8; break;
336 case XK_KP_9:
337 id = WXK_NUMPAD9; break;
338 case XK_F1:
339 id = WXK_F1; break;
340 case XK_F2:
341 id = WXK_F2; break;
342 case XK_F3:
343 id = WXK_F3; break;
344 case XK_F4:
345 id = WXK_F4; break;
346 case XK_F5:
347 id = WXK_F5; break;
348 case XK_F6:
349 id = WXK_F6; break;
350 case XK_F7:
351 id = WXK_F7; break;
352 case XK_F8:
353 id = WXK_F8; break;
354 case XK_F9:
355 id = WXK_F9; break;
356 case XK_F10:
357 id = WXK_F10; break;
358 case XK_F11:
359 id = WXK_F11; break;
360 case XK_F12:
361 id = WXK_F12; break;
362 case XK_F13:
363 id = WXK_F13; break;
364 case XK_F14:
365 id = WXK_F14; break;
366 case XK_F15:
367 id = WXK_F15; break;
368 case XK_F16:
369 id = WXK_F16; break;
370 case XK_F17:
371 id = WXK_F17; break;
372 case XK_F18:
373 id = WXK_F18; break;
374 case XK_F19:
375 id = WXK_F19; break;
376 case XK_F20:
377 id = WXK_F20; break;
378 case XK_F21:
379 id = WXK_F21; break;
380 case XK_F22:
381 id = WXK_F22; break;
382 case XK_F23:
383 id = WXK_F23; break;
384 case XK_F24:
385 id = WXK_F24; break;
386 default:
387 id = (keySym <= 255) ? (int)keySym : -1;
388 }
389
390 return id;
391 }
392
393 KeySym wxCharCodeWXToX(int id)
394 {
395 KeySym keySym;
396
397 switch (id)
398 {
399 case WXK_CANCEL: keySym = XK_Cancel; break;
400 case WXK_BACK: keySym = XK_BackSpace; break;
401 case WXK_TAB: keySym = XK_Tab; break;
402 case WXK_CLEAR: keySym = XK_Clear; break;
403 case WXK_RETURN: keySym = XK_Return; break;
404 case WXK_SHIFT: keySym = XK_Shift_L; break;
405 case WXK_CONTROL: keySym = XK_Control_L; break;
406 case WXK_MENU : keySym = XK_Menu; break;
407 case WXK_PAUSE: keySym = XK_Pause; break;
408 case WXK_ESCAPE: keySym = XK_Escape; break;
409 case WXK_SPACE: keySym = ' '; break;
410 case WXK_PRIOR: keySym = XK_Prior; break;
411 case WXK_NEXT : keySym = XK_Next; break;
412 case WXK_END: keySym = XK_End; break;
413 case WXK_HOME : keySym = XK_Home; break;
414 case WXK_LEFT : keySym = XK_Left; break;
415 case WXK_UP: keySym = XK_Up; break;
416 case WXK_RIGHT: keySym = XK_Right; break;
417 case WXK_DOWN : keySym = XK_Down; break;
418 case WXK_SELECT: keySym = XK_Select; break;
419 case WXK_PRINT: keySym = XK_Print; break;
420 case WXK_EXECUTE: keySym = XK_Execute; break;
421 case WXK_INSERT: keySym = XK_Insert; break;
422 case WXK_DELETE: keySym = XK_Delete; break;
423 case WXK_HELP : keySym = XK_Help; break;
424 case WXK_NUMPAD0: keySym = XK_KP_0; break;
425 case WXK_NUMPAD1: keySym = XK_KP_1; break;
426 case WXK_NUMPAD2: keySym = XK_KP_2; break;
427 case WXK_NUMPAD3: keySym = XK_KP_3; break;
428 case WXK_NUMPAD4: keySym = XK_KP_4; break;
429 case WXK_NUMPAD5: keySym = XK_KP_5; break;
430 case WXK_NUMPAD6: keySym = XK_KP_6; break;
431 case WXK_NUMPAD7: keySym = XK_KP_7; break;
432 case WXK_NUMPAD8: keySym = XK_KP_8; break;
433 case WXK_NUMPAD9: keySym = XK_KP_9; break;
434 case WXK_MULTIPLY: keySym = XK_KP_Multiply; break;
435 case WXK_ADD: keySym = XK_KP_Add; break;
436 case WXK_SUBTRACT: keySym = XK_KP_Subtract; break;
437 case WXK_DECIMAL: keySym = XK_KP_Decimal; break;
438 case WXK_DIVIDE: keySym = XK_KP_Divide; break;
439 case WXK_F1: keySym = XK_F1; break;
440 case WXK_F2: keySym = XK_F2; break;
441 case WXK_F3: keySym = XK_F3; break;
442 case WXK_F4: keySym = XK_F4; break;
443 case WXK_F5: keySym = XK_F5; break;
444 case WXK_F6: keySym = XK_F6; break;
445 case WXK_F7: keySym = XK_F7; break;
446 case WXK_F8: keySym = XK_F8; break;
447 case WXK_F9: keySym = XK_F9; break;
448 case WXK_F10: keySym = XK_F10; break;
449 case WXK_F11: keySym = XK_F11; break;
450 case WXK_F12: keySym = XK_F12; break;
451 case WXK_F13: keySym = XK_F13; break;
452 case WXK_F14: keySym = XK_F14; break;
453 case WXK_F15: keySym = XK_F15; break;
454 case WXK_F16: keySym = XK_F16; break;
455 case WXK_F17: keySym = XK_F17; break;
456 case WXK_F18: keySym = XK_F18; break;
457 case WXK_F19: keySym = XK_F19; break;
458 case WXK_F20: keySym = XK_F20; break;
459 case WXK_F21: keySym = XK_F21; break;
460 case WXK_F22: keySym = XK_F22; break;
461 case WXK_F23: keySym = XK_F23; break;
462 case WXK_F24: keySym = XK_F24; break;
463 case WXK_NUMLOCK: keySym = XK_Num_Lock; break;
464 case WXK_SCROLL: keySym = XK_Scroll_Lock; break;
465 default: keySym = id <= 255 ? (KeySym)id : 0;
466 }
467
468 return keySym;
469 }
470
471 // ----------------------------------------------------------------------------
472 // Some colour manipulation routines
473 // ----------------------------------------------------------------------------
474
475 void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
476 {
477 int h = hsv->h;
478 int s = hsv->s;
479 int v = hsv->v;
480 int r = 0, g = 0, b = 0;
481 int i, f;
482 int p, q, t;
483 s = (s * wxMAX_RGB) / wxMAX_SV;
484 v = (v * wxMAX_RGB) / wxMAX_SV;
485 if (h == 360) h = 0;
486 if (s == 0) { h = 0; r = g = b = v; }
487 i = h / 60;
488 f = h % 60;
489 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
490 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
491 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
492 switch (i)
493 {
494 case 0: r = v, g = t, b = p; break;
495 case 1: r = q, g = v, b = p; break;
496 case 2: r = p, g = v, b = t; break;
497 case 3: r = p, g = q, b = v; break;
498 case 4: r = t, g = p, b = v; break;
499 case 5: r = v, g = p, b = q; break;
500 }
501 rgb->red = r << 8;
502 rgb->green = g << 8;
503 rgb->blue = b << 8;
504 }
505
506 void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
507 {
508 int r = rgb->red >> 8;
509 int g = rgb->green >> 8;
510 int b = rgb->blue >> 8;
511 int maxv = wxMax3(r, g, b);
512 int minv = wxMin3(r, g, b);
513 int h = 0, s, v;
514 v = maxv;
515 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
516 else s = 0;
517 if (s == 0) h = 0;
518 else
519 {
520 int rc, gc, bc, hex = 0;
521 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
522 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
523 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
524 if (r == maxv) { h = bc - gc, hex = 0; }
525 else if (g == maxv) { h = rc - bc, hex = 2; }
526 else if (b == maxv) { h = gc - rc, hex = 4; }
527 h = hex * 60 + (h * 60 / wxMAX_RGB);
528 if (h < 0) h += 360;
529 }
530 hsv->h = h;
531 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
532 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
533 }
534
535 void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
536 {
537 #if !wxUSE_NANOX
538 int llp;
539
540 int screen = DefaultScreen(d);
541 int num_colors = DisplayCells(d,screen);
542
543 XColor *color_defs = new XColor[num_colors];
544 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
545 XQueryColors(d,cmp,color_defs,num_colors);
546
547 wxHSV hsv_defs, hsv;
548 wxXColorToHSV(&hsv,xc);
549
550 int diff, min_diff = 0, pixel = 0;
551
552 for(llp = 0;llp < num_colors;llp++)
553 {
554 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
555 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
556 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
557 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
558 if (llp == 0) min_diff = diff;
559 if (min_diff > diff) { min_diff = diff; pixel = llp; }
560 if (min_diff == 0) break;
561 }
562
563 xc -> red = color_defs[pixel].red;
564 xc -> green = color_defs[pixel].green;
565 xc -> blue = color_defs[pixel].blue;
566 xc -> flags = DoRed | DoGreen | DoBlue;
567
568 /* FIXME, TODO
569 if (!XAllocColor(d,cmp,xc))
570 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
571 */
572
573 delete[] color_defs;
574 #endif
575 }
576
577 void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
578 {
579 if (!XAllocColor(d,cmp,xc))
580 {
581 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
582 wxAllocNearestColor(d,cmp,xc);
583 }
584 }
585
586 #ifdef __WXDEBUG__
587 wxString wxGetXEventName(XEvent& event)
588 {
589 #if wxUSE_NANOX
590 wxString str(wxT("(some event)"));
591 return str;
592 #else
593 int type = event.xany.type;
594 static char* event_name[] = {
595 "", "unknown(-)", // 0-1
596 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
597 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
598 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
599 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
600 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
601 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
602 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
603 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
604 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
605 "ClientMessage", "MappingNotify", // 33-34
606 "unknown(+)"}; // 35
607 type = wxMin(35, type); type = wxMax(1, type);
608 wxString str(event_name[type]);
609 return str;
610 #endif
611 }
612 #endif
613
614 bool wxWindowIsVisible(Window win)
615 {
616 XWindowAttributes wa;
617 XGetWindowAttributes(wxGlobalDisplay(), win, &wa);
618
619 return (wa.map_state == IsViewable);
620 }