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