]> git.saurik.com Git - wxWidgets.git/blob - src/unix/utilsx11.cpp
position is always unsigned in InsetPage(), no need to compare it with 0
[wxWidgets.git] / src / unix / utilsx11.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/utilsx11.cpp
3 // Purpose: Miscellaneous X11 functions
4 // Author: Mattia Barbon, Vaclav Slavik, Robert Roebling
5 // Modified by:
6 // Created: 25.03.02
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__WXX11__) || defined(__WXGTK__) || defined(__WXMOTIF__)
13
14 // for compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #include "wx/unix/utilsx11.h"
18 #include "wx/iconbndl.h"
19 #include "wx/image.h"
20 #include "wx/icon.h"
21 #include "wx/log.h"
22
23 #ifdef __VMS
24 #pragma message disable nosimpint
25 #endif
26 #include <X11/Xlib.h>
27 #include <X11/Xatom.h>
28 #ifdef __VMS
29 #pragma message enable nosimpint
30 #endif
31
32 #ifdef __WXGTK__
33 #include <gdk/gdk.h>
34 #include <gdk/gdkx.h>
35 #endif
36
37 // Various X11 Atoms used in this file:
38 static Atom _NET_WM_ICON = 0;
39 static Atom _NET_WM_STATE = 0;
40 static Atom _NET_WM_STATE_FULLSCREEN = 0;
41 static Atom _NET_WM_STATE_STAYS_ON_TOP = 0;
42 static Atom _NET_WM_WINDOW_TYPE = 0;
43 static Atom _NET_WM_WINDOW_TYPE_NORMAL = 0;
44 static Atom _KDE_NET_WM_WINDOW_TYPE_OVERRIDE = 0;
45 static Atom _WIN_LAYER = 0;
46 static Atom KWIN_RUNNING = 0;
47 #ifndef __WXGTK20__
48 static Atom _NET_SUPPORTING_WM_CHECK = 0;
49 static Atom _NET_SUPPORTED = 0;
50 #endif
51
52 #define wxMAKE_ATOM(name, display) \
53 if (name == 0) name = XInternAtom((display), #name, False)
54
55
56 // Is the window mapped?
57 static bool IsMapped(Display *display, Window window)
58 {
59 XWindowAttributes attr;
60 XGetWindowAttributes(display, window, &attr);
61 return (attr.map_state != IsUnmapped);
62 }
63
64
65
66 // Suspends X11 errors. Used when we expect errors but they are not fatal
67 // for us.
68 extern "C"
69 {
70 typedef int (*wxX11ErrorHandler)(Display *, XErrorEvent *);
71
72 static int wxX11ErrorsSuspender_handler(Display*, XErrorEvent*) { return 0; }
73 }
74
75 class wxX11ErrorsSuspender
76 {
77 public:
78 wxX11ErrorsSuspender(Display *d) : m_display(d)
79 {
80 m_old = XSetErrorHandler(wxX11ErrorsSuspender_handler);
81 }
82 ~wxX11ErrorsSuspender()
83 {
84 XFlush(m_display);
85 XSetErrorHandler(m_old);
86 }
87
88 private:
89 Display *m_display;
90 wxX11ErrorHandler m_old;
91 };
92
93
94
95 // ----------------------------------------------------------------------------
96 // Setting icons for window manager:
97 // ----------------------------------------------------------------------------
98
99 void wxSetIconsX11( WXDisplay* display, WXWindow window,
100 const wxIconBundle& ib )
101 {
102 #if !wxUSE_NANOX
103 size_t size = 0;
104 size_t i, max = ib.m_icons.GetCount();
105
106 for( i = 0; i < max; ++i )
107 if( ib.m_icons[i].Ok() )
108 size += 2 + ib.m_icons[i].GetWidth() * ib.m_icons[i].GetHeight();
109
110 wxMAKE_ATOM(_NET_WM_ICON, (Display*)display);
111
112 if( size > 0 )
113 {
114 // The code below is correct for 64-bit machines also.
115 // wxUint32* data = new wxUint32[size];
116 // wxUint32* ptr = data;
117 unsigned long* data = new unsigned long[size];
118 unsigned long* ptr = data;
119
120 for( i = 0; i < max; ++i )
121 {
122 const wxImage image = ib.m_icons[i].ConvertToImage();
123 int width = image.GetWidth(), height = image.GetHeight();
124 unsigned char* imageData = image.GetData();
125 unsigned char* imageDataEnd = imageData + ( width * height * 3 );
126 bool hasMask = image.HasMask();
127 unsigned char rMask, gMask, bMask;
128 unsigned char r, g, b, a;
129
130 if( hasMask )
131 {
132 rMask = image.GetMaskRed();
133 gMask = image.GetMaskGreen();
134 bMask = image.GetMaskBlue();
135 }
136 else // no mask, but still init the variables to avoid warnings
137 {
138 rMask =
139 gMask =
140 bMask = 0;
141 }
142
143 *ptr++ = width;
144 *ptr++ = height;
145
146 while( imageData < imageDataEnd ) {
147 r = imageData[0];
148 g = imageData[1];
149 b = imageData[2];
150 if( hasMask && r == rMask && g == gMask && b == bMask )
151 a = 0;
152 else
153 a = 255;
154
155 *ptr++ = ( a << 24 ) | ( r << 16 ) | ( g << 8 ) | b;
156
157 imageData += 3;
158 }
159 }
160
161 XChangeProperty( (Display*)display,
162 (Window)window,
163 _NET_WM_ICON,
164 XA_CARDINAL, 32,
165 PropModeReplace,
166 (unsigned char*)data, size );
167 delete[] data;
168 }
169 else
170 {
171 XDeleteProperty( (Display*)display,
172 (Window)window,
173 _NET_WM_ICON );
174 }
175 #endif
176 }
177
178
179 // ----------------------------------------------------------------------------
180 // Fullscreen mode:
181 // ----------------------------------------------------------------------------
182
183 // NB: Setting fullscreen mode under X11 is a complicated matter. There was
184 // no standard way of doing it until recently. ICCCM doesn't know the
185 // concept of fullscreen windows and the only way to make a window
186 // fullscreen is to remove decorations, resize it to cover entire screen
187 // and set WIN_LAYER_ABOVE_DOCK.
188 //
189 // This doesn't always work, though. Specifically, at least kwin from
190 // KDE 3 ignores the hint. The only way to make kwin accept our request
191 // is to emulate the way Qt does it. That is, unmap the window, set
192 // _NET_WM_WINDOW_TYPE to _KDE_NET_WM_WINDOW_TYPE_OVERRIDE (KDE extension),
193 // add _NET_WM_STATE_STAYS_ON_TOP (ditto) to _NET_WM_STATE and map
194 // the window again.
195 //
196 // Version 1.2 of Window Manager Specification (aka wm-spec aka
197 // Extended Window Manager Hints) introduced _NET_WM_STATE_FULLSCREEN
198 // window state which provides cleanest and simplest possible way of
199 // making a window fullscreen. WM-spec is a de-facto standard adopted
200 // by GNOME and KDE folks, but _NET_WM_STATE_FULLSCREEN isn't yet widely
201 // supported. As of January 2003, only GNOME 2's default WM Metacity
202 // implements, KDE will support it from version 3.2. At toolkits level,
203 // GTK+ >= 2.1.2 uses it as the only method of making windows fullscreen
204 // (that's why wxGTK will *not* switch to using gtk_window_fullscreen
205 // unless it has better compatibility with older WMs).
206 //
207 //
208 // This is what wxWidgets does in wxSetFullScreenStateX11:
209 // 1) if _NET_WM_STATE_FULLSCREEN is supported, use it
210 // 2) otherwise try WM-specific hacks (KDE, IceWM)
211 // 3) use _WIN_LAYER and hope that the WM will recognize it
212 // The code was tested with:
213 // twm, IceWM, WindowMaker, Metacity, kwin, sawfish, lesstif-mwm
214
215
216 #define WIN_LAYER_NORMAL 4
217 #define WIN_LAYER_ABOVE_DOCK 10
218
219 static void wxWinHintsSetLayer(Display *display, Window rootWnd,
220 Window window, int layer)
221 {
222 wxX11ErrorsSuspender noerrors(display);
223
224 XEvent xev;
225
226 wxMAKE_ATOM( _WIN_LAYER, display );
227
228 if (IsMapped(display, window))
229 {
230 xev.type = ClientMessage;
231 xev.xclient.type = ClientMessage;
232 xev.xclient.window = window;
233 xev.xclient.message_type = _WIN_LAYER;
234 xev.xclient.format = 32;
235 xev.xclient.data.l[0] = (long)layer;
236 xev.xclient.data.l[1] = CurrentTime;
237
238 XSendEvent(display, rootWnd, False,
239 SubstructureNotifyMask, (XEvent*) &xev);
240 }
241 else
242 {
243 long data[1];
244
245 data[0] = layer;
246 XChangeProperty(display, window,
247 _WIN_LAYER, XA_CARDINAL, 32,
248 PropModeReplace, (unsigned char *)data, 1);
249 }
250 }
251
252
253
254 #ifdef __WXGTK20__
255 static bool wxQueryWMspecSupport(Display* WXUNUSED(display),
256 Window WXUNUSED(rootWnd),
257 Atom (feature))
258 {
259 GdkAtom gatom = gdk_x11_xatom_to_atom(feature);
260 return gdk_net_wm_supports(gatom);
261 }
262 #else
263 static bool wxQueryWMspecSupport(Display *display, Window rootWnd, Atom feature)
264 {
265 wxMAKE_ATOM(_NET_SUPPORTING_WM_CHECK, display);
266 wxMAKE_ATOM(_NET_SUPPORTED, display);
267
268 // FIXME: We may want to cache these checks. Note that we can't simply
269 // remember the results in global variable because the WM may go
270 // away and be replaced by another one! One possible approach
271 // would be invalidate the case every 15 seconds or so. Since this
272 // code is currently only used by wxTopLevelWindow::ShowFullScreen,
273 // it is not important that it is not optimized.
274 //
275 // If the WM supports ICCCM (i.e. the root window has
276 // _NET_SUPPORTING_WM_CHECK property that points to a WM-owned
277 // window), we could watch for DestroyNotify event on the window
278 // and invalidate our cache when the windows goes away (= WM
279 // is replaced by another one). This is what GTK+ 2 does.
280 // Let's do it only if it is needed, it requires changes to
281 // the event loop.
282
283 Atom type;
284 Window *wins;
285 Atom *atoms;
286 int format;
287 unsigned long after;
288 unsigned long nwins, natoms;
289
290 // Is the WM ICCCM supporting?
291 XGetWindowProperty(display, rootWnd,
292 _NET_SUPPORTING_WM_CHECK, 0, LONG_MAX,
293 False, XA_WINDOW, &type, &format, &nwins,
294 &after, (unsigned char **)&wins);
295 if ( type != XA_WINDOW || nwins <= 0 || wins[0] == None )
296 return FALSE;
297 XFree(wins);
298
299 // Query for supported features:
300 XGetWindowProperty(display, rootWnd,
301 _NET_SUPPORTED, 0, LONG_MAX,
302 False, XA_ATOM, &type, &format, &natoms,
303 &after, (unsigned char **)&atoms);
304 if ( type != XA_ATOM || atoms == NULL )
305 return FALSE;
306
307 // Lookup the feature we want:
308 for (unsigned i = 0; i < natoms; i++)
309 {
310 if ( atoms[i] == feature )
311 {
312 XFree(atoms);
313 return TRUE;
314 }
315 }
316 XFree(atoms);
317 return FALSE;
318 }
319 #endif
320
321
322 #define _NET_WM_STATE_REMOVE 0
323 #define _NET_WM_STATE_ADD 1
324
325 static void wxWMspecSetState(Display *display, Window rootWnd,
326 Window window, int operation, Atom state)
327 {
328 wxMAKE_ATOM(_NET_WM_STATE, display);
329
330 if ( IsMapped(display, window) )
331 {
332 XEvent xev;
333 xev.type = ClientMessage;
334 xev.xclient.type = ClientMessage;
335 xev.xclient.serial = 0;
336 xev.xclient.send_event = True;
337 xev.xclient.display = display;
338 xev.xclient.window = window;
339 xev.xclient.message_type = _NET_WM_STATE;
340 xev.xclient.format = 32;
341 xev.xclient.data.l[0] = operation;
342 xev.xclient.data.l[1] = state;
343 xev.xclient.data.l[2] = None;
344
345 XSendEvent(display, rootWnd,
346 False,
347 SubstructureRedirectMask | SubstructureNotifyMask,
348 &xev);
349 }
350 // FIXME - must modify _NET_WM_STATE property list if the window
351 // wasn't mapped!
352 }
353
354 static void wxWMspecSetFullscreen(Display *display, Window rootWnd,
355 Window window, bool fullscreen)
356 {
357 wxMAKE_ATOM(_NET_WM_STATE_FULLSCREEN, display);
358 wxWMspecSetState(display, rootWnd,
359 window,
360 fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE,
361 _NET_WM_STATE_FULLSCREEN);
362 }
363
364
365 // Is the user running KDE's kwin window manager? At least kwin from KDE 3
366 // sets KWIN_RUNNING property on the root window.
367 static bool wxKwinRunning(Display *display, Window rootWnd)
368 {
369 wxMAKE_ATOM(KWIN_RUNNING, display);
370
371 long *data;
372 Atom type;
373 int format;
374 unsigned long nitems, after;
375 if (XGetWindowProperty(display, rootWnd,
376 KWIN_RUNNING, 0, 1, False, KWIN_RUNNING,
377 &type, &format, &nitems, &after,
378 (unsigned char**)&data) != Success)
379 {
380 return FALSE;
381 }
382
383 bool retval = (type == KWIN_RUNNING &&
384 nitems == 1 && data && data[0] == 1);
385 XFree(data);
386 return retval;
387 }
388
389 // KDE's kwin is Qt-centric so much than no normal method of fullscreen
390 // mode will work with it. We have to carefully emulate the Qt way.
391 static void wxSetKDEFullscreen(Display *display, Window rootWnd,
392 Window w, bool fullscreen, wxRect *origRect)
393 {
394 long data[2];
395 unsigned lng;
396
397 wxMAKE_ATOM(_NET_WM_WINDOW_TYPE, display);
398 wxMAKE_ATOM(_NET_WM_WINDOW_TYPE_NORMAL, display);
399 wxMAKE_ATOM(_KDE_NET_WM_WINDOW_TYPE_OVERRIDE, display);
400 wxMAKE_ATOM(_NET_WM_STATE_STAYS_ON_TOP, display);
401
402 if (fullscreen)
403 {
404 data[0] = _KDE_NET_WM_WINDOW_TYPE_OVERRIDE;
405 data[1] = _NET_WM_WINDOW_TYPE_NORMAL;
406 lng = 2;
407 }
408 else
409 {
410 data[0] = _NET_WM_WINDOW_TYPE_NORMAL;
411 data[1] = None;
412 lng = 1;
413 }
414
415 // it is necessary to unmap the window, otherwise kwin will ignore us:
416 XSync(display, False);
417
418 bool wasMapped = IsMapped(display, w);
419 if (wasMapped)
420 {
421 XUnmapWindow(display, w);
422 XSync(display, False);
423 }
424
425 XChangeProperty(display, w, _NET_WM_WINDOW_TYPE, XA_ATOM, 32,
426 PropModeReplace, (unsigned char *) &data, lng);
427 XSync(display, False);
428
429 if (wasMapped)
430 {
431 XMapRaised(display, w);
432 XSync(display, False);
433 }
434
435 wxWMspecSetState(display, rootWnd, w,
436 fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE,
437 _NET_WM_STATE_STAYS_ON_TOP);
438 XSync(display, False);
439
440 if (!fullscreen)
441 {
442 // NB: like many other WMs, kwin ignores the first request for a window
443 // position change after the window was mapped. This additional
444 // move+resize event will ensure that the window is restored in
445 // exactly the same position as before it was made fullscreen
446 // (because wxTopLevelWindow::ShowFullScreen will call SetSize, thus
447 // setting the position for the second time).
448 XMoveResizeWindow(display, w,
449 origRect->x, origRect->y,
450 origRect->width, origRect->height);
451 XSync(display, False);
452 }
453 }
454
455
456 wxX11FullScreenMethod wxGetFullScreenMethodX11(WXDisplay* display,
457 WXWindow rootWindow)
458 {
459 Window root = (Window)rootWindow;
460 Display *disp = (Display*)display;
461
462 // if WM supports _NET_WM_STATE_FULLSCREEN from wm-spec 1.2, use it:
463 wxMAKE_ATOM(_NET_WM_STATE_FULLSCREEN, disp);
464 if (wxQueryWMspecSupport(disp, root, _NET_WM_STATE_FULLSCREEN))
465 {
466 wxLogTrace(_T("fullscreen"),
467 _T("detected _NET_WM_STATE_FULLSCREEN support"));
468 return wxX11_FS_WMSPEC;
469 }
470
471 // if the user is running KDE's kwin WM, use a legacy hack because
472 // kwin doesn't understand any other method:
473 if (wxKwinRunning(disp, root))
474 {
475 wxLogTrace(_T("fullscreen"), _T("detected kwin"));
476 return wxX11_FS_KDE;
477 }
478
479 // finally, fall back to ICCCM heuristic method:
480 wxLogTrace(_T("fullscreen"), _T("unknown WM, using _WIN_LAYER"));
481 return wxX11_FS_GENERIC;
482 }
483
484
485 void wxSetFullScreenStateX11(WXDisplay* display, WXWindow rootWindow,
486 WXWindow window, bool show,
487 wxRect *origRect,
488 wxX11FullScreenMethod method)
489 {
490 // NB: please see the comment under "Fullscreen mode:" title above
491 // for implications of changing this code.
492
493 Window wnd = (Window)window;
494 Window root = (Window)rootWindow;
495 Display *disp = (Display*)display;
496
497 if (method == wxX11_FS_AUTODETECT)
498 method = wxGetFullScreenMethodX11(display, rootWindow);
499
500 switch (method)
501 {
502 case wxX11_FS_WMSPEC:
503 wxWMspecSetFullscreen(disp, root, wnd, show);
504 break;
505 case wxX11_FS_KDE:
506 wxSetKDEFullscreen(disp, root, wnd, show, origRect);
507 break;
508 default:
509 wxWinHintsSetLayer(disp, root, wnd,
510 show ? WIN_LAYER_ABOVE_DOCK : WIN_LAYER_NORMAL);
511 break;
512 }
513 }
514
515
516
517 // ----------------------------------------------------------------------------
518 // keycode translations
519 // ----------------------------------------------------------------------------
520
521 #include <X11/keysym.h>
522
523 // FIXME what about tables??
524
525 int wxCharCodeXToWX(KeySym keySym)
526 {
527 int id;
528 switch (keySym)
529 {
530 case XK_Shift_L:
531 case XK_Shift_R:
532 id = WXK_SHIFT; break;
533 case XK_Control_L:
534 case XK_Control_R:
535 id = WXK_CONTROL; break;
536 case XK_Meta_L:
537 case XK_Meta_R:
538 id = WXK_ALT; break;
539 case XK_BackSpace:
540 id = WXK_BACK; break;
541 case XK_Delete:
542 id = WXK_DELETE; break;
543 case XK_Clear:
544 id = WXK_CLEAR; break;
545 case XK_Tab:
546 id = WXK_TAB; break;
547 case XK_numbersign:
548 id = '#'; break;
549 case XK_Return:
550 id = WXK_RETURN; break;
551 case XK_Escape:
552 id = WXK_ESCAPE; break;
553 case XK_Pause:
554 case XK_Break:
555 id = WXK_PAUSE; break;
556 case XK_Num_Lock:
557 id = WXK_NUMLOCK; break;
558 case XK_Scroll_Lock:
559 id = WXK_SCROLL; break;
560
561 case XK_Home:
562 id = WXK_HOME; break;
563 case XK_End:
564 id = WXK_END; break;
565 case XK_Left:
566 id = WXK_LEFT; break;
567 case XK_Right:
568 id = WXK_RIGHT; break;
569 case XK_Up:
570 id = WXK_UP; break;
571 case XK_Down:
572 id = WXK_DOWN; break;
573 case XK_Next:
574 id = WXK_NEXT; break;
575 case XK_Prior:
576 id = WXK_PRIOR; break;
577 case XK_Menu:
578 id = WXK_MENU; break;
579 case XK_Select:
580 id = WXK_SELECT; break;
581 case XK_Cancel:
582 id = WXK_CANCEL; break;
583 case XK_Print:
584 id = WXK_PRINT; break;
585 case XK_Execute:
586 id = WXK_EXECUTE; break;
587 case XK_Insert:
588 id = WXK_INSERT; break;
589 case XK_Help:
590 id = WXK_HELP; break;
591
592 case XK_KP_Multiply:
593 id = WXK_MULTIPLY; break;
594 case XK_KP_Add:
595 id = WXK_ADD; break;
596 case XK_KP_Subtract:
597 id = WXK_SUBTRACT; break;
598 case XK_KP_Divide:
599 id = WXK_DIVIDE; break;
600 case XK_KP_Decimal:
601 id = WXK_DECIMAL; break;
602 case XK_KP_Equal:
603 id = '='; break;
604 case XK_KP_Space:
605 id = ' '; break;
606 case XK_KP_Tab:
607 id = WXK_TAB; break;
608 case XK_KP_Enter:
609 id = WXK_RETURN; break;
610 case XK_KP_0:
611 id = WXK_NUMPAD0; break;
612 case XK_KP_1:
613 id = WXK_NUMPAD1; break;
614 case XK_KP_2:
615 id = WXK_NUMPAD2; break;
616 case XK_KP_3:
617 id = WXK_NUMPAD3; break;
618 case XK_KP_4:
619 id = WXK_NUMPAD4; break;
620 case XK_KP_5:
621 id = WXK_NUMPAD5; break;
622 case XK_KP_6:
623 id = WXK_NUMPAD6; break;
624 case XK_KP_7:
625 id = WXK_NUMPAD7; break;
626 case XK_KP_8:
627 id = WXK_NUMPAD8; break;
628 case XK_KP_9:
629 id = WXK_NUMPAD9; break;
630 case XK_F1:
631 id = WXK_F1; break;
632 case XK_F2:
633 id = WXK_F2; break;
634 case XK_F3:
635 id = WXK_F3; break;
636 case XK_F4:
637 id = WXK_F4; break;
638 case XK_F5:
639 id = WXK_F5; break;
640 case XK_F6:
641 id = WXK_F6; break;
642 case XK_F7:
643 id = WXK_F7; break;
644 case XK_F8:
645 id = WXK_F8; break;
646 case XK_F9:
647 id = WXK_F9; break;
648 case XK_F10:
649 id = WXK_F10; break;
650 case XK_F11:
651 id = WXK_F11; break;
652 case XK_F12:
653 id = WXK_F12; break;
654 case XK_F13:
655 id = WXK_F13; break;
656 case XK_F14:
657 id = WXK_F14; break;
658 case XK_F15:
659 id = WXK_F15; break;
660 case XK_F16:
661 id = WXK_F16; break;
662 case XK_F17:
663 id = WXK_F17; break;
664 case XK_F18:
665 id = WXK_F18; break;
666 case XK_F19:
667 id = WXK_F19; break;
668 case XK_F20:
669 id = WXK_F20; break;
670 case XK_F21:
671 id = WXK_F21; break;
672 case XK_F22:
673 id = WXK_F22; break;
674 case XK_F23:
675 id = WXK_F23; break;
676 case XK_F24:
677 id = WXK_F24; break;
678 default:
679 id = (keySym <= 255) ? (int)keySym : -1;
680 }
681
682 return id;
683 }
684
685 KeySym wxCharCodeWXToX(int id)
686 {
687 KeySym keySym;
688
689 switch (id)
690 {
691 case WXK_CANCEL: keySym = XK_Cancel; break;
692 case WXK_BACK: keySym = XK_BackSpace; break;
693 case WXK_TAB: keySym = XK_Tab; break;
694 case WXK_CLEAR: keySym = XK_Clear; break;
695 case WXK_RETURN: keySym = XK_Return; break;
696 case WXK_SHIFT: keySym = XK_Shift_L; break;
697 case WXK_CONTROL: keySym = XK_Control_L; break;
698 case WXK_ALT: keySym = XK_Meta_L; break;
699 case WXK_MENU : keySym = XK_Menu; break;
700 case WXK_PAUSE: keySym = XK_Pause; break;
701 case WXK_ESCAPE: keySym = XK_Escape; break;
702 case WXK_SPACE: keySym = ' '; break;
703 case WXK_PRIOR: keySym = XK_Prior; break;
704 case WXK_NEXT : keySym = XK_Next; break;
705 case WXK_END: keySym = XK_End; break;
706 case WXK_HOME : keySym = XK_Home; break;
707 case WXK_LEFT : keySym = XK_Left; break;
708 case WXK_UP: keySym = XK_Up; break;
709 case WXK_RIGHT: keySym = XK_Right; break;
710 case WXK_DOWN : keySym = XK_Down; break;
711 case WXK_SELECT: keySym = XK_Select; break;
712 case WXK_PRINT: keySym = XK_Print; break;
713 case WXK_EXECUTE: keySym = XK_Execute; break;
714 case WXK_INSERT: keySym = XK_Insert; break;
715 case WXK_DELETE: keySym = XK_Delete; break;
716 case WXK_HELP : keySym = XK_Help; break;
717 case WXK_NUMPAD0: keySym = XK_KP_0; break;
718 case WXK_NUMPAD1: keySym = XK_KP_1; break;
719 case WXK_NUMPAD2: keySym = XK_KP_2; break;
720 case WXK_NUMPAD3: keySym = XK_KP_3; break;
721 case WXK_NUMPAD4: keySym = XK_KP_4; break;
722 case WXK_NUMPAD5: keySym = XK_KP_5; break;
723 case WXK_NUMPAD6: keySym = XK_KP_6; break;
724 case WXK_NUMPAD7: keySym = XK_KP_7; break;
725 case WXK_NUMPAD8: keySym = XK_KP_8; break;
726 case WXK_NUMPAD9: keySym = XK_KP_9; break;
727 case WXK_MULTIPLY: keySym = XK_KP_Multiply; break;
728 case WXK_ADD: keySym = XK_KP_Add; break;
729 case WXK_SUBTRACT: keySym = XK_KP_Subtract; break;
730 case WXK_DECIMAL: keySym = XK_KP_Decimal; break;
731 case WXK_DIVIDE: keySym = XK_KP_Divide; break;
732 case WXK_F1: keySym = XK_F1; break;
733 case WXK_F2: keySym = XK_F2; break;
734 case WXK_F3: keySym = XK_F3; break;
735 case WXK_F4: keySym = XK_F4; break;
736 case WXK_F5: keySym = XK_F5; break;
737 case WXK_F6: keySym = XK_F6; break;
738 case WXK_F7: keySym = XK_F7; break;
739 case WXK_F8: keySym = XK_F8; break;
740 case WXK_F9: keySym = XK_F9; break;
741 case WXK_F10: keySym = XK_F10; break;
742 case WXK_F11: keySym = XK_F11; break;
743 case WXK_F12: keySym = XK_F12; break;
744 case WXK_F13: keySym = XK_F13; break;
745 case WXK_F14: keySym = XK_F14; break;
746 case WXK_F15: keySym = XK_F15; break;
747 case WXK_F16: keySym = XK_F16; break;
748 case WXK_F17: keySym = XK_F17; break;
749 case WXK_F18: keySym = XK_F18; break;
750 case WXK_F19: keySym = XK_F19; break;
751 case WXK_F20: keySym = XK_F20; break;
752 case WXK_F21: keySym = XK_F21; break;
753 case WXK_F22: keySym = XK_F22; break;
754 case WXK_F23: keySym = XK_F23; break;
755 case WXK_F24: keySym = XK_F24; break;
756 case WXK_NUMLOCK: keySym = XK_Num_Lock; break;
757 case WXK_SCROLL: keySym = XK_Scroll_Lock; break;
758 default: keySym = id <= 255 ? (KeySym)id : 0;
759 }
760
761 return keySym;
762 }
763
764
765 // ----------------------------------------------------------------------------
766 // check current state of a key
767 // ----------------------------------------------------------------------------
768
769 #include <wx/app.h>
770
771 bool wxGetKeyState(wxKeyCode key)
772 {
773 wxASSERT_MSG(key != WXK_LBUTTON && key != WXK_RBUTTON && key !=
774 WXK_MBUTTON, wxT("can't use wxGetKeyState() for mouse buttons"));
775
776 #if defined(__WXX11__)
777 Display *pDisplay = (Display*) wxApp::GetDisplay();
778 #elif defined(__WXGTK__)
779 Display *pDisplay = GDK_DISPLAY();
780 #elif defined(__WXMOTIF__)
781 Display *pDisplay = (Display*) (wxTheApp ? wxTheApp->GetInitialDisplay() : NULL);
782 #else
783 #error Add code to get the DISPLAY for this platform
784 #endif
785
786 int iKey = wxCharCodeWXToX(key);
787 int iKeyMask = 0;
788 Window wDummy1, wDummy2;
789 int iDummy3, iDummy4, iDummy5, iDummy6;
790 unsigned int iMask;
791 XModifierKeymap* map = XGetModifierMapping(pDisplay);
792 KeyCode keyCode = XKeysymToKeycode(pDisplay,iKey);
793 if (keyCode == NoSymbol)
794 return false;
795
796 for (int i = 0; i < 8; ++i)
797 {
798 if ( map->modifiermap[map->max_keypermod * i] == keyCode)
799 {
800 iKeyMask = 1 << i;
801 }
802 }
803
804 XQueryPointer(pDisplay, DefaultRootWindow(pDisplay), &wDummy1, &wDummy2,
805 &iDummy3, &iDummy4, &iDummy5, &iDummy6, &iMask );
806 XFreeModifiermap(map);
807 return (iMask & iKeyMask) != 0;
808 }
809
810 #endif
811
812
813
814
815