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