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