]> git.saurik.com Git - wxWidgets.git/blob - src/x11/utils.cpp
use wxFileName::MakeAbsolute istead of Normalize
[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/msgdlg.h"
24 #include "wx/cursor.h"
25 #include "wx/window.h" // for wxTopLevelWindows
26
27 #include <ctype.h>
28 #include <stdarg.h>
29 #include <dirent.h>
30 #include <string.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <sys/wait.h>
35 #include <pwd.h>
36 #include <errno.h>
37 // #include <netdb.h>
38 #include <signal.h>
39
40 #if (defined(__SUNCC__) || defined(__CLCC__))
41 #include <sysent.h>
42 #endif
43
44 #ifdef __VMS__
45 #pragma message disable nosimpint
46 #endif
47
48 #include "wx/unix/execute.h"
49
50 #include "wx/x11/private.h"
51
52 #include "X11/Xutil.h"
53
54 #ifdef __VMS__
55 #pragma message enable nosimpint
56 #endif
57
58 // ----------------------------------------------------------------------------
59 // async event processing
60 // ----------------------------------------------------------------------------
61
62 // Consume all events until no more left
63 void wxFlushEvents()
64 {
65 Display *display = (Display*) wxGetDisplay();
66
67 XSync (display, FALSE);
68
69 #ifdef __WXMOTIF__
70 // XtAppPending returns availability of events AND timers/inputs, which
71 // are processed via callbacks, so XtAppNextEvent will not return if
72 // there are no events. So added '& XtIMXEvent' - Sergey.
73 while (XtAppPending ((XtAppContext) wxTheApp->GetAppContext()) & XtIMXEvent)
74 {
75 XFlush (XtDisplay ((Widget) wxTheApp->GetTopLevelWidget()));
76 // Jan Lessner: works better when events are non-X events
77 XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMXEvent);
78 }
79 #endif
80 #ifdef __WXX11__
81 // TODO for X11
82 // ??
83 #endif
84 }
85
86 bool wxCheckForInterrupt(wxWindow *wnd)
87 {
88 #ifdef __WXMOTIF__
89 wxCHECK_MSG( wnd, FALSE, "NULL window in wxCheckForInterrupt" );
90
91 Display *dpy=(Display*) wnd->GetXDisplay();
92 Window win=(Window) wnd->GetXWindow();
93 XEvent event;
94 XFlush(dpy);
95 if (wnd->GetMainWidget())
96 {
97 XmUpdateDisplay((Widget)(wnd->GetMainWidget()));
98 }
99
100 bool hadEvents = FALSE;
101 while( XCheckMaskEvent(dpy,
102 ButtonPressMask|ButtonReleaseMask|ButtonMotionMask|
103 PointerMotionMask|KeyPressMask|KeyReleaseMask,
104 &event) )
105 {
106 if ( event.xany.window == win )
107 {
108 hadEvents = TRUE;
109
110 XtDispatchEvent(&event);
111 }
112 }
113
114 return hadEvents;
115 #else
116 wxASSERT_MSG(FALSE, "wxCheckForInterrupt not yet implemented.");
117 return FALSE;
118 #endif
119 }
120
121 // ----------------------------------------------------------------------------
122 // wxExecute stuff
123 // ----------------------------------------------------------------------------
124
125 int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
126 {
127 #ifdef __WXMOTIF__
128 XtInputId id = XtAppAddInput((XtAppContext) wxTheApp->GetAppContext(),
129 fd,
130 (XtPointer *) XtInputReadMask,
131 (XtInputCallbackProc) xt_notify_end_process,
132 (XtPointer) proc_data);
133
134 return (int)id;
135 #endif
136 #ifdef __WXX11__
137 // TODO
138 return 0;
139 #endif
140 }
141
142 // ----------------------------------------------------------------------------
143 // misc
144 // ----------------------------------------------------------------------------
145
146 // Emit a beeeeeep
147 void wxBell()
148 {
149 // Use current setting for the bell
150 XBell ((Display*) wxGetDisplay(), 0);
151 }
152
153 int wxGetOsVersion(int *majorVsn, int *minorVsn)
154 {
155 #ifdef __WXX11__
156 if (majorVsn)
157 *majorVsn = 0;
158
159 if (minorVsn)
160 *minorVsn = 0;
161
162 return wxX11;
163 #endif
164 }
165
166 // ----------------------------------------------------------------------------
167 // display info
168 // ----------------------------------------------------------------------------
169
170 void wxGetMousePosition( int* x, int* y )
171 {
172 #if wxUSE_NANOX
173 // TODO
174 *x = 0;
175 *y = 0;
176 #else
177 XMotionEvent xev;
178 Window root, child;
179 XQueryPointer((Display*) wxGetDisplay(),
180 DefaultRootWindow((Display*) wxGetDisplay()),
181 &root, &child,
182 &(xev.x_root), &(xev.y_root),
183 &(xev.x), &(xev.y),
184 &(xev.state));
185 *x = xev.x_root;
186 *y = xev.y_root;
187 #endif
188 };
189
190 // Return TRUE if we have a colour display
191 bool wxColourDisplay()
192 {
193 return wxDisplayDepth() > 1;
194 }
195
196 // Returns depth of screen
197 int wxDisplayDepth()
198 {
199 Display *dpy = (Display*) wxGetDisplay();
200
201 return DefaultDepth (dpy, DefaultScreen (dpy));
202 }
203
204 // Get size of display
205 void wxDisplaySize(int *width, int *height)
206 {
207 Display *dpy = (Display*) wxGetDisplay();
208
209 if ( width )
210 *width = DisplayWidth (dpy, DefaultScreen (dpy));
211 if ( height )
212 *height = DisplayHeight (dpy, DefaultScreen (dpy));
213 }
214
215 void wxDisplaySizeMM(int *width, int *height)
216 {
217 Display *dpy = (Display*) wxGetDisplay();
218
219 if ( width )
220 *width = DisplayWidthMM(dpy, DefaultScreen (dpy));
221 if ( height )
222 *height = DisplayHeightMM(dpy, DefaultScreen (dpy));
223 }
224
225 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
226 {
227 // This is supposed to return desktop dimensions minus any window
228 // manager panels, menus, taskbars, etc. If there is a way to do that
229 // for this platform please fix this function, otherwise it defaults
230 // to the entire desktop.
231 if (x) *x = 0;
232 if (y) *y = 0;
233 wxDisplaySize(width, height);
234 }
235
236
237 // Configurable display in wxX11 and wxMotif
238 static WXDisplay *gs_currentDisplay = NULL;
239 static wxString gs_displayName;
240
241 WXDisplay *wxGetDisplay()
242 {
243 if (gs_currentDisplay)
244 return gs_currentDisplay;
245 #ifdef __WXMOTIF__
246 if (wxTheApp && wxTheApp->GetTopLevelWidget())
247 return XtDisplay ((Widget) wxTheApp->GetTopLevelWidget());
248 else if (wxTheApp)
249 return wxTheApp->GetInitialDisplay();
250 return NULL;
251 #endif
252 #ifdef __WXX11__
253 return wxApp::GetDisplay();
254 #endif
255 }
256
257 bool wxSetDisplay(const wxString& display_name)
258 {
259 gs_displayName = display_name;
260
261 if ( display_name.IsEmpty() )
262 {
263 gs_currentDisplay = NULL;
264
265 return TRUE;
266 }
267 else
268 {
269 Display* display = XOpenDisplay((char*) display_name.c_str());
270
271 if (display)
272 {
273 gs_currentDisplay = (WXDisplay*) display;
274 return TRUE;
275 }
276 else
277 return FALSE;
278 }
279 }
280
281 wxString wxGetDisplayName()
282 {
283 return gs_displayName;
284 }
285
286 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
287 {
288 return wxGenericFindWindowAtPoint(pt);
289 }
290
291 // ----------------------------------------------------------------------------
292 // keycode translations
293 // ----------------------------------------------------------------------------
294
295 #include <X11/keysym.h>
296
297 // FIXME what about tables??
298
299 int wxCharCodeXToWX(KeySym keySym)
300 {
301 int id;
302 switch (keySym)
303 {
304 case XK_Shift_L:
305 case XK_Shift_R:
306 id = WXK_SHIFT; break;
307 case XK_Control_L:
308 case XK_Control_R:
309 id = WXK_CONTROL; break;
310 case XK_BackSpace:
311 id = WXK_BACK; break;
312 case XK_Delete:
313 id = WXK_DELETE; break;
314 case XK_Clear:
315 id = WXK_CLEAR; break;
316 case XK_Tab:
317 id = WXK_TAB; break;
318 case XK_numbersign:
319 id = '#'; break;
320 case XK_Return:
321 id = WXK_RETURN; break;
322 case XK_Escape:
323 id = WXK_ESCAPE; break;
324 case XK_Pause:
325 case XK_Break:
326 id = WXK_PAUSE; break;
327 case XK_Num_Lock:
328 id = WXK_NUMLOCK; break;
329 case XK_Scroll_Lock:
330 id = WXK_SCROLL; break;
331
332 case XK_Home:
333 id = WXK_HOME; break;
334 case XK_End:
335 id = WXK_END; break;
336 case XK_Left:
337 id = WXK_LEFT; break;
338 case XK_Right:
339 id = WXK_RIGHT; break;
340 case XK_Up:
341 id = WXK_UP; break;
342 case XK_Down:
343 id = WXK_DOWN; break;
344 case XK_Next:
345 id = WXK_NEXT; break;
346 case XK_Prior:
347 id = WXK_PRIOR; break;
348 case XK_Menu:
349 id = WXK_MENU; break;
350 case XK_Select:
351 id = WXK_SELECT; break;
352 case XK_Cancel:
353 id = WXK_CANCEL; break;
354 case XK_Print:
355 id = WXK_PRINT; break;
356 case XK_Execute:
357 id = WXK_EXECUTE; break;
358 case XK_Insert:
359 id = WXK_INSERT; break;
360 case XK_Help:
361 id = WXK_HELP; break;
362
363 case XK_KP_Multiply:
364 id = WXK_MULTIPLY; break;
365 case XK_KP_Add:
366 id = WXK_ADD; break;
367 case XK_KP_Subtract:
368 id = WXK_SUBTRACT; break;
369 case XK_KP_Divide:
370 id = WXK_DIVIDE; break;
371 case XK_KP_Decimal:
372 id = WXK_DECIMAL; break;
373 case XK_KP_Equal:
374 id = '='; break;
375 case XK_KP_Space:
376 id = ' '; break;
377 case XK_KP_Tab:
378 id = WXK_TAB; break;
379 case XK_KP_Enter:
380 id = WXK_RETURN; break;
381 case XK_KP_0:
382 id = WXK_NUMPAD0; break;
383 case XK_KP_1:
384 id = WXK_NUMPAD1; break;
385 case XK_KP_2:
386 id = WXK_NUMPAD2; break;
387 case XK_KP_3:
388 id = WXK_NUMPAD3; break;
389 case XK_KP_4:
390 id = WXK_NUMPAD4; break;
391 case XK_KP_5:
392 id = WXK_NUMPAD5; break;
393 case XK_KP_6:
394 id = WXK_NUMPAD6; break;
395 case XK_KP_7:
396 id = WXK_NUMPAD7; break;
397 case XK_KP_8:
398 id = WXK_NUMPAD8; break;
399 case XK_KP_9:
400 id = WXK_NUMPAD9; break;
401 case XK_F1:
402 id = WXK_F1; break;
403 case XK_F2:
404 id = WXK_F2; break;
405 case XK_F3:
406 id = WXK_F3; break;
407 case XK_F4:
408 id = WXK_F4; break;
409 case XK_F5:
410 id = WXK_F5; break;
411 case XK_F6:
412 id = WXK_F6; break;
413 case XK_F7:
414 id = WXK_F7; break;
415 case XK_F8:
416 id = WXK_F8; break;
417 case XK_F9:
418 id = WXK_F9; break;
419 case XK_F10:
420 id = WXK_F10; break;
421 case XK_F11:
422 id = WXK_F11; break;
423 case XK_F12:
424 id = WXK_F12; break;
425 case XK_F13:
426 id = WXK_F13; break;
427 case XK_F14:
428 id = WXK_F14; break;
429 case XK_F15:
430 id = WXK_F15; break;
431 case XK_F16:
432 id = WXK_F16; break;
433 case XK_F17:
434 id = WXK_F17; break;
435 case XK_F18:
436 id = WXK_F18; break;
437 case XK_F19:
438 id = WXK_F19; break;
439 case XK_F20:
440 id = WXK_F20; break;
441 case XK_F21:
442 id = WXK_F21; break;
443 case XK_F22:
444 id = WXK_F22; break;
445 case XK_F23:
446 id = WXK_F23; break;
447 case XK_F24:
448 id = WXK_F24; break;
449 default:
450 id = (keySym <= 255) ? (int)keySym : -1;
451 }
452
453 return id;
454 }
455
456 KeySym wxCharCodeWXToX(int id)
457 {
458 KeySym keySym;
459
460 switch (id)
461 {
462 case WXK_CANCEL: keySym = XK_Cancel; break;
463 case WXK_BACK: keySym = XK_BackSpace; break;
464 case WXK_TAB: keySym = XK_Tab; break;
465 case WXK_CLEAR: keySym = XK_Clear; break;
466 case WXK_RETURN: keySym = XK_Return; break;
467 case WXK_SHIFT: keySym = XK_Shift_L; break;
468 case WXK_CONTROL: keySym = XK_Control_L; break;
469 case WXK_MENU : keySym = XK_Menu; break;
470 case WXK_PAUSE: keySym = XK_Pause; break;
471 case WXK_ESCAPE: keySym = XK_Escape; break;
472 case WXK_SPACE: keySym = ' '; break;
473 case WXK_PRIOR: keySym = XK_Prior; break;
474 case WXK_NEXT : keySym = XK_Next; break;
475 case WXK_END: keySym = XK_End; break;
476 case WXK_HOME : keySym = XK_Home; break;
477 case WXK_LEFT : keySym = XK_Left; break;
478 case WXK_UP: keySym = XK_Up; break;
479 case WXK_RIGHT: keySym = XK_Right; break;
480 case WXK_DOWN : keySym = XK_Down; break;
481 case WXK_SELECT: keySym = XK_Select; break;
482 case WXK_PRINT: keySym = XK_Print; break;
483 case WXK_EXECUTE: keySym = XK_Execute; break;
484 case WXK_INSERT: keySym = XK_Insert; break;
485 case WXK_DELETE: keySym = XK_Delete; break;
486 case WXK_HELP : keySym = XK_Help; break;
487 case WXK_NUMPAD0: keySym = XK_KP_0; break;
488 case WXK_NUMPAD1: keySym = XK_KP_1; break;
489 case WXK_NUMPAD2: keySym = XK_KP_2; break;
490 case WXK_NUMPAD3: keySym = XK_KP_3; break;
491 case WXK_NUMPAD4: keySym = XK_KP_4; break;
492 case WXK_NUMPAD5: keySym = XK_KP_5; break;
493 case WXK_NUMPAD6: keySym = XK_KP_6; break;
494 case WXK_NUMPAD7: keySym = XK_KP_7; break;
495 case WXK_NUMPAD8: keySym = XK_KP_8; break;
496 case WXK_NUMPAD9: keySym = XK_KP_9; break;
497 case WXK_MULTIPLY: keySym = XK_KP_Multiply; break;
498 case WXK_ADD: keySym = XK_KP_Add; break;
499 case WXK_SUBTRACT: keySym = XK_KP_Subtract; break;
500 case WXK_DECIMAL: keySym = XK_KP_Decimal; break;
501 case WXK_DIVIDE: keySym = XK_KP_Divide; break;
502 case WXK_F1: keySym = XK_F1; break;
503 case WXK_F2: keySym = XK_F2; break;
504 case WXK_F3: keySym = XK_F3; break;
505 case WXK_F4: keySym = XK_F4; break;
506 case WXK_F5: keySym = XK_F5; break;
507 case WXK_F6: keySym = XK_F6; break;
508 case WXK_F7: keySym = XK_F7; break;
509 case WXK_F8: keySym = XK_F8; break;
510 case WXK_F9: keySym = XK_F9; break;
511 case WXK_F10: keySym = XK_F10; break;
512 case WXK_F11: keySym = XK_F11; break;
513 case WXK_F12: keySym = XK_F12; break;
514 case WXK_F13: keySym = XK_F13; break;
515 case WXK_F14: keySym = XK_F14; break;
516 case WXK_F15: keySym = XK_F15; break;
517 case WXK_F16: keySym = XK_F16; break;
518 case WXK_F17: keySym = XK_F17; break;
519 case WXK_F18: keySym = XK_F18; break;
520 case WXK_F19: keySym = XK_F19; break;
521 case WXK_F20: keySym = XK_F20; break;
522 case WXK_F21: keySym = XK_F21; break;
523 case WXK_F22: keySym = XK_F22; break;
524 case WXK_F23: keySym = XK_F23; break;
525 case WXK_F24: keySym = XK_F24; break;
526 case WXK_NUMLOCK: keySym = XK_Num_Lock; break;
527 case WXK_SCROLL: keySym = XK_Scroll_Lock; break;
528 default: keySym = id <= 255 ? (KeySym)id : 0;
529 }
530
531 return keySym;
532 }
533
534 // ----------------------------------------------------------------------------
535 // Some colour manipulation routines
536 // ----------------------------------------------------------------------------
537
538 void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
539 {
540 int h = hsv->h;
541 int s = hsv->s;
542 int v = hsv->v;
543 int r = 0, g = 0, b = 0;
544 int i, f;
545 int p, q, t;
546 s = (s * wxMAX_RGB) / wxMAX_SV;
547 v = (v * wxMAX_RGB) / wxMAX_SV;
548 if (h == 360) h = 0;
549 if (s == 0) { h = 0; r = g = b = v; }
550 i = h / 60;
551 f = h % 60;
552 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
553 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
554 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
555 switch (i)
556 {
557 case 0: r = v, g = t, b = p; break;
558 case 1: r = q, g = v, b = p; break;
559 case 2: r = p, g = v, b = t; break;
560 case 3: r = p, g = q, b = v; break;
561 case 4: r = t, g = p, b = v; break;
562 case 5: r = v, g = p, b = q; break;
563 }
564 rgb->red = r << 8;
565 rgb->green = g << 8;
566 rgb->blue = b << 8;
567 }
568
569 void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
570 {
571 int r = rgb->red >> 8;
572 int g = rgb->green >> 8;
573 int b = rgb->blue >> 8;
574 int maxv = wxMax3(r, g, b);
575 int minv = wxMin3(r, g, b);
576 int h = 0, s, v;
577 v = maxv;
578 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
579 else s = 0;
580 if (s == 0) h = 0;
581 else
582 {
583 int rc, gc, bc, hex = 0;
584 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
585 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
586 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
587 if (r == maxv) { h = bc - gc, hex = 0; }
588 else if (g == maxv) { h = rc - bc, hex = 2; }
589 else if (b == maxv) { h = gc - rc, hex = 4; }
590 h = hex * 60 + (h * 60 / wxMAX_RGB);
591 if (h < 0) h += 360;
592 }
593 hsv->h = h;
594 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
595 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
596 }
597
598 void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
599 {
600 #if !wxUSE_NANOX
601 int llp;
602
603 int screen = DefaultScreen(d);
604 int num_colors = DisplayCells(d,screen);
605
606 XColor *color_defs = new XColor[num_colors];
607 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
608 XQueryColors(d,cmp,color_defs,num_colors);
609
610 wxHSV hsv_defs, hsv;
611 wxXColorToHSV(&hsv,xc);
612
613 int diff, min_diff = 0, pixel = 0;
614
615 for(llp = 0;llp < num_colors;llp++)
616 {
617 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
618 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
619 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
620 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
621 if (llp == 0) min_diff = diff;
622 if (min_diff > diff) { min_diff = diff; pixel = llp; }
623 if (min_diff == 0) break;
624 }
625
626 xc -> red = color_defs[pixel].red;
627 xc -> green = color_defs[pixel].green;
628 xc -> blue = color_defs[pixel].blue;
629 xc -> flags = DoRed | DoGreen | DoBlue;
630
631 /* FIXME, TODO
632 if (!XAllocColor(d,cmp,xc))
633 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
634 */
635
636 delete[] color_defs;
637 #endif
638 }
639
640 void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
641 {
642 if (!XAllocColor(d,cmp,xc))
643 {
644 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
645 wxAllocNearestColor(d,cmp,xc);
646 }
647 }
648
649 #ifdef __WXDEBUG__
650 wxString wxGetXEventName(XEvent& event)
651 {
652 #if wxUSE_NANOX
653 wxString str(wxT("(some event)"));
654 return str;
655 #else
656 int type = event.xany.type;
657 static char* event_name[] = {
658 "", "unknown(-)", // 0-1
659 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
660 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
661 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
662 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
663 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
664 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
665 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
666 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
667 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
668 "ClientMessage", "MappingNotify", // 33-34
669 "unknown(+)"}; // 35
670 type = wxMin(35, type); type = wxMax(1, type);
671 wxString str(event_name[type]);
672 return str;
673 #endif
674 }
675 #endif
676
677 bool wxWindowIsVisible(Window win)
678 {
679 XWindowAttributes wa;
680 XGetWindowAttributes(wxGlobalDisplay(), win, &wa);
681
682 return (wa.map_state == IsViewable);
683 }