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