]> git.saurik.com Git - wxWidgets.git/blame - src/x11/utils.cpp
Added wxToggleBitmapButton (it compiles).
[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"
cd478a95 23#include "wx/apptrait.h"
83df96d6
JS
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>
2b5f62a0 38// #include <netdb.h>
83df96d6
JS
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
bc797f4c 48
83df96d6
JS
49#include "wx/unix/execute.h"
50
bc797f4c 51#include "wx/x11/private.h"
256d631a 52
7266b672 53#include "X11/Xutil.h"
83df96d6 54
256d631a
JS
55#ifdef __VMS__
56#pragma message enable nosimpint
57#endif
58
83df96d6
JS
59// ----------------------------------------------------------------------------
60// async event processing
61// ----------------------------------------------------------------------------
62
63// Consume all events until no more left
64void wxFlushEvents()
65{
66 Display *display = (Display*) wxGetDisplay();
67
68 XSync (display, FALSE);
69
5e29f97a
JS
70 // TODO for X11
71 // ??
83df96d6
JS
72}
73
83df96d6
JS
74bool wxCheckForInterrupt(wxWindow *wnd)
75{
54385bdb 76 wxASSERT_MSG(FALSE, wxT("wxCheckForInterrupt not yet implemented."));
5e29f97a 77 return FALSE;
83df96d6
JS
78}
79
80// ----------------------------------------------------------------------------
81// wxExecute stuff
82// ----------------------------------------------------------------------------
83df96d6
JS
83
84int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
85{
707440dc
JS
86 // TODO
87 return 0;
707440dc 88}
83df96d6
JS
89
90// ----------------------------------------------------------------------------
91// misc
92// ----------------------------------------------------------------------------
93
94// Emit a beeeeeep
189d1ae7
SN
95#ifndef __EMX__
96// on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
83df96d6
JS
97void wxBell()
98{
99 // Use current setting for the bell
100 XBell ((Display*) wxGetDisplay(), 0);
101}
189d1ae7 102#endif
83df96d6 103
324899f6 104wxToolkitInfo& wxGUIAppTraits::GetToolkitInfo()
83df96d6 105{
a8eaaeb2
VS
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;
324899f6 112 return info;
83df96d6
JS
113}
114
83df96d6
JS
115// ----------------------------------------------------------------------------
116// display info
117// ----------------------------------------------------------------------------
118
119void wxGetMousePosition( int* x, int* y )
120{
461e93f9
JS
121#if wxUSE_NANOX
122 // TODO
123 *x = 0;
124 *y = 0;
125#else
83df96d6
JS
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;
461e93f9 136#endif
83df96d6
JS
137};
138
139// Return TRUE if we have a colour display
140bool wxColourDisplay()
141{
142 return wxDisplayDepth() > 1;
143}
144
145// Returns depth of screen
146int wxDisplayDepth()
147{
148 Display *dpy = (Display*) wxGetDisplay();
149
150 return DefaultDepth (dpy, DefaultScreen (dpy));
151}
152
153// Get size of display
154void 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
164void 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
174void 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
5e29f97a 186// Configurable display in wxX11 and wxMotif
83df96d6
JS
187static WXDisplay *gs_currentDisplay = NULL;
188static wxString gs_displayName;
189
190WXDisplay *wxGetDisplay()
191{
192 if (gs_currentDisplay)
193 return gs_currentDisplay;
5e29f97a 194 return wxApp::GetDisplay();
83df96d6
JS
195}
196
197bool wxSetDisplay(const wxString& display_name)
198{
199 gs_displayName = display_name;
200
bc797f4c 201 if ( display_name.IsEmpty() )
83df96d6
JS
202 {
203 gs_currentDisplay = NULL;
204
205 return TRUE;
206 }
207 else
208 {
461e93f9 209 Display* display = XOpenDisplay((char*) display_name.c_str());
83df96d6
JS
210
211 if (display)
212 {
213 gs_currentDisplay = (WXDisplay*) display;
214 return TRUE;
215 }
216 else
217 return FALSE;
218 }
219}
220
221wxString wxGetDisplayName()
222{
223 return gs_displayName;
224}
225
226wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
227{
228 return wxGenericFindWindowAtPoint(pt);
229}
230
83df96d6
JS
231// ----------------------------------------------------------------------------
232// keycode translations
233// ----------------------------------------------------------------------------
234
235#include <X11/keysym.h>
236
237// FIXME what about tables??
238
239int 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
396KeySym 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
478void 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
509void 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
538void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
539{
461e93f9 540#if !wxUSE_NANOX
83df96d6
JS
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;
461e93f9 577#endif
83df96d6
JS
578}
579
580void 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
45ff6421
JS
589#ifdef __WXDEBUG__
590wxString wxGetXEventName(XEvent& event)
591{
461e93f9
JS
592#if wxUSE_NANOX
593 wxString str(wxT("(some event)"));
594 return str;
595#else
45ff6421
JS
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);
54385bdb 611 return wxString::FromAscii(event_name[type]);
461e93f9
JS
612#endif
613}
45ff6421
JS
614#endif
615
3a0b23eb
JS
616bool wxWindowIsVisible(Window win)
617{
618 XWindowAttributes wa;
619 XGetWindowAttributes(wxGlobalDisplay(), win, &wa);
620
621 return (wa.map_state == IsViewable);
622}