]> git.saurik.com Git - wxWidgets.git/blob - src/unix/utilsx11.cpp
unicode fixes, enabling notebook images again
[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) wxWindows 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 __WXGTK20__
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 class wxX11ErrorsSuspender
69 {
70 public:
71 wxX11ErrorsSuspender(Display *d) : m_display(d)
72 {
73 m_old = XSetErrorHandler(handler);
74 }
75 ~wxX11ErrorsSuspender()
76 {
77 XFlush(m_display);
78 XSetErrorHandler(m_old);
79 }
80 private:
81 Display *m_display;
82 int (*m_old)(Display*, XErrorEvent *);
83 static int handler(Display *, XErrorEvent *) { return 0; }
84 };
85
86
87
88 // ----------------------------------------------------------------------------
89 // Setting icons for window manager:
90 // ----------------------------------------------------------------------------
91
92 void wxSetIconsX11( WXDisplay* display, WXWindow window,
93 const wxIconBundle& ib )
94 {
95 #if !wxUSE_NANOX
96 size_t size = 0;
97 size_t i, max = ib.m_icons.GetCount();
98
99 for( i = 0; i < max; ++i )
100 if( ib.m_icons[i].Ok() )
101 size += 2 + ib.m_icons[i].GetWidth() * ib.m_icons[i].GetHeight();
102
103 wxMAKE_ATOM(_NET_WM_ICON, (Display*)display);
104
105 if( size > 0 )
106 {
107 wxUint32* data = new wxUint32[size];
108 wxUint32* ptr = data;
109
110 for( i = 0; i < max; ++i )
111 {
112 const wxImage image = ib.m_icons[i].ConvertToImage();
113 int width = image.GetWidth(), height = image.GetHeight();
114 unsigned char* imageData = image.GetData();
115 unsigned char* imageDataEnd = imageData + ( width * height * 3 );
116 bool hasMask = image.HasMask();
117 unsigned char rMask, gMask, bMask;
118 unsigned char r, g, b, a;
119
120 if( hasMask )
121 {
122 rMask = image.GetMaskRed();
123 gMask = image.GetMaskGreen();
124 bMask = image.GetMaskBlue();
125 }
126 else // no mask, but still init the variables to avoid warnings
127 {
128 rMask =
129 gMask =
130 bMask = 0;
131 }
132
133 *ptr++ = width;
134 *ptr++ = height;
135
136 while( imageData < imageDataEnd ) {
137 r = imageData[0];
138 g = imageData[1];
139 b = imageData[2];
140 if( hasMask && r == rMask && g == gMask && b == bMask )
141 a = 0;
142 else
143 a = 255;
144
145 *ptr++ = ( a << 24 ) | ( r << 16 ) | ( g << 8 ) | b;
146
147 imageData += 3;
148 }
149 }
150
151 XChangeProperty( (Display*)display,
152 (Window)window,
153 _NET_WM_ICON,
154 XA_CARDINAL, 32,
155 PropModeReplace,
156 (unsigned char*)data, size );
157 delete[] data;
158 }
159 else
160 {
161 XDeleteProperty( (Display*)display,
162 (Window)window,
163 _NET_WM_ICON );
164 }
165 #endif
166 }
167
168
169 // ----------------------------------------------------------------------------
170 // Fullscreen mode:
171 // ----------------------------------------------------------------------------
172
173 // NB: Setting fullscreen mode under X11 is a complicated matter. There was
174 // no standard way of doing it until recently. ICCCM doesn't know the
175 // concept of fullscreen windows and the only way to make a window
176 // fullscreen is to remove decorations, resize it to cover entire screen
177 // and set WIN_LAYER_ABOVE_DOCK.
178 //
179 // This doesn't always work, though. Specifically, at least kwin from
180 // KDE 3 ignores the hint. The only way to make kwin accept our request
181 // is to emulate the way Qt does it. That is, unmap the window, set
182 // _NET_WM_WINDOW_TYPE to _KDE_NET_WM_WINDOW_TYPE_OVERRIDE (KDE extension),
183 // add _NET_WM_STATE_STAYS_ON_TOP (ditto) to _NET_WM_STATE and map
184 // the window again.
185 //
186 // Version 1.2 of Window Manager Specification (aka wm-spec aka
187 // Extended Window Manager Hints) introduced _NET_WM_STATE_FULLSCREEN
188 // window state which provides cleanest and simplest possible way of
189 // making a window fullscreen. WM-spec is a de-facto standard adopted
190 // by GNOME and KDE folks, but _NET_WM_STATE_FULLSCREEN isn't yet widely
191 // supported. As of January 2003, only GNOME 2's default WM Metacity
192 // implements, KDE will support it from version 3.2. At toolkits level,
193 // GTK+ >= 2.1.2 uses it as the only method of making windows fullscreen
194 // (that's why wxGTK will *not* switch to using gtk_window_fullscreen
195 // unless it has better compatiblity with older WMs).
196 //
197 //
198 // This is what wxWindows does in wxSetFullScreenStateX11:
199 // 1) if _NET_WM_STATE_FULLSCREEN is supported, use it
200 // 2) otherwise try WM-specific hacks (KDE, IceWM)
201 // 3) use _WIN_LAYER and hope that the WM will recognize it
202 // The code was tested with:
203 // twm, IceWM, WindowMaker, Metacity, kwin, sawfish, lesstif-mwm
204
205
206 #define WIN_LAYER_NORMAL 4
207 #define WIN_LAYER_ABOVE_DOCK 10
208
209 static void wxWinHintsSetLayer(Display *display, Window rootWnd,
210 Window window, int layer)
211 {
212 wxX11ErrorsSuspender noerrors(display);
213
214 XEvent xev;
215
216 wxMAKE_ATOM( _WIN_LAYER, display );
217
218 if (IsMapped(display, window))
219 {
220 xev.type = ClientMessage;
221 xev.xclient.type = ClientMessage;
222 xev.xclient.window = window;
223 xev.xclient.message_type = _WIN_LAYER;
224 xev.xclient.format = 32;
225 xev.xclient.data.l[0] = (long)layer;
226 xev.xclient.data.l[1] = CurrentTime;
227
228 XSendEvent(display, rootWnd, False,
229 SubstructureNotifyMask, (XEvent*) &xev);
230 }
231 else
232 {
233 long data[1];
234
235 data[0] = layer;
236 XChangeProperty(display, window,
237 _WIN_LAYER, XA_CARDINAL, 32,
238 PropModeReplace, (unsigned char *)data, 1);
239 }
240 }
241
242
243
244 #ifdef __WXGTK20__
245 static bool wxQueryWMspecSupport(Display* WXUNUSED(display),
246 Window WXUNUSED(rootWnd),
247 Atom (feature))
248 {
249 GdkAtom gatom = gdk_x11_xatom_to_atom(feature);
250 return gdk_net_wm_supports(gatom);
251 }
252 #else
253 static bool wxQueryWMspecSupport(Display *display, Window rootWnd, Atom feature)
254 {
255 wxMAKE_ATOM(_NET_SUPPORTING_WM_CHECK, display);
256 wxMAKE_ATOM(_NET_SUPPORTED, display);
257
258 // FIXME: We may want to cache these checks. Note that we can't simply
259 // remember the results in global variable because the WM may go
260 // away and be replaced by another one! One possible approach
261 // would be invalidate the case every 15 seconds or so. Since this
262 // code is currently only used by wxTopLevelWindow::ShowFullScreen,
263 // it is not important that it is not optimized.
264 //
265 // If the WM supports ICCCM (i.e. the root window has
266 // _NET_SUPPORTING_WM_CHECK property that points to a WM-owned
267 // window), we could watch for DestroyNotify event on the window
268 // and invalidate our cache when the windows goes away (= WM
269 // is replaced by another one). This is what GTK+ 2 does.
270 // Let's do it only if it is needed, it requires changes to
271 // the event loop.
272
273 Atom type;
274 Window *wins;
275 Atom *atoms;
276 int format;
277 unsigned long after;
278 unsigned long nwins, natoms;
279
280 // Is the WM ICCCM supporting?
281 XGetWindowProperty(display, rootWnd,
282 _NET_SUPPORTING_WM_CHECK, 0, LONG_MAX,
283 False, XA_WINDOW, &type, &format, &nwins,
284 &after, (unsigned char **)&wins);
285 if ( type != XA_WINDOW || nwins <= 0 || wins[0] == None )
286 return FALSE;
287 XFree(wins);
288
289 // Query for supported features:
290 XGetWindowProperty(display, rootWnd,
291 _NET_SUPPORTED, 0, LONG_MAX,
292 False, XA_ATOM, &type, &format, &natoms,
293 &after, (unsigned char **)&atoms);
294 if ( type != XA_ATOM || atoms == NULL )
295 return FALSE;
296
297 // Lookup the feature we want:
298 for (unsigned i = 0; i < natoms; i++)
299 {
300 if ( atoms[i] == feature )
301 {
302 XFree(atoms);
303 return TRUE;
304 }
305 }
306 XFree(atoms);
307 return FALSE;
308 }
309 #endif
310
311
312 #define _NET_WM_STATE_REMOVE 0
313 #define _NET_WM_STATE_ADD 1
314
315 static void wxWMspecSetState(Display *display, Window rootWnd,
316 Window window, int operation, Atom state)
317 {
318 wxMAKE_ATOM(_NET_WM_STATE, display);
319
320 if ( IsMapped(display, window) )
321 {
322 XEvent xev;
323 xev.type = ClientMessage;
324 xev.xclient.type = ClientMessage;
325 xev.xclient.serial = 0;
326 xev.xclient.send_event = True;
327 xev.xclient.display = display;
328 xev.xclient.window = window;
329 xev.xclient.message_type = _NET_WM_STATE;
330 xev.xclient.format = 32;
331 xev.xclient.data.l[0] = operation;
332 xev.xclient.data.l[1] = state;
333 xev.xclient.data.l[2] = None;
334
335 XSendEvent(display, rootWnd,
336 False,
337 SubstructureRedirectMask | SubstructureNotifyMask,
338 &xev);
339 }
340 // FIXME - must modify _NET_WM_STATE property list if the window
341 // wasn't mapped!
342 }
343
344 static void wxWMspecSetFullscreen(Display *display, Window rootWnd,
345 Window window, bool fullscreen)
346 {
347 wxMAKE_ATOM(_NET_WM_STATE_FULLSCREEN, display);
348 wxWMspecSetState(display, rootWnd,
349 window,
350 fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE,
351 _NET_WM_STATE_FULLSCREEN);
352 }
353
354
355 // Is the user running KDE's kwin window manager? At least kwin from KDE 3
356 // sets KWIN_RUNNING property on the root window.
357 static bool wxKwinRunning(Display *display, Window rootWnd)
358 {
359 wxMAKE_ATOM(KWIN_RUNNING, display);
360
361 long *data;
362 Atom type;
363 int format;
364 unsigned long nitems, after;
365 if (XGetWindowProperty(display, rootWnd,
366 KWIN_RUNNING, 0, 1, False, KWIN_RUNNING,
367 &type, &format, &nitems, &after,
368 (unsigned char**)&data) != Success)
369 {
370 return FALSE;
371 }
372
373 bool retval = (type == KWIN_RUNNING &&
374 nitems == 1 && data && data[0] == 1);
375 XFree(data);
376 return retval;
377 }
378
379 // KDE's kwin is Qt-centric so much than no normal method of fullscreen
380 // mode will work with it. We have to carefully emulate the Qt way.
381 static void wxSetKDEFullscreen(Display *display, Window rootWnd,
382 Window w, bool fullscreen, wxRect *origRect)
383 {
384 long data[2];
385 unsigned lng;
386
387 wxMAKE_ATOM(_NET_WM_WINDOW_TYPE, display);
388 wxMAKE_ATOM(_NET_WM_WINDOW_TYPE_NORMAL, display);
389 wxMAKE_ATOM(_KDE_NET_WM_WINDOW_TYPE_OVERRIDE, display);
390 wxMAKE_ATOM(_NET_WM_STATE_STAYS_ON_TOP, display);
391
392 if (fullscreen)
393 {
394 data[0] = _KDE_NET_WM_WINDOW_TYPE_OVERRIDE;
395 data[1] = _NET_WM_WINDOW_TYPE_NORMAL;
396 lng = 2;
397 }
398 else
399 {
400 data[0] = _NET_WM_WINDOW_TYPE_NORMAL;
401 data[1] = None;
402 lng = 1;
403 }
404
405 // it is neccessary to unmap the window, otherwise kwin will ignore us:
406 XSync(display, False);
407
408 bool wasMapped = IsMapped(display, w);
409 if (wasMapped)
410 {
411 XUnmapWindow(display, w);
412 XSync(display, False);
413 }
414
415 XChangeProperty(display, w, _NET_WM_WINDOW_TYPE, XA_ATOM, 32,
416 PropModeReplace, (unsigned char *) &data, lng);
417 XSync(display, False);
418
419 if (wasMapped)
420 {
421 XMapRaised(display, w);
422 XSync(display, False);
423 }
424
425 wxWMspecSetState(display, rootWnd, w,
426 fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE,
427 _NET_WM_STATE_STAYS_ON_TOP);
428 XSync(display, False);
429
430 if (!fullscreen)
431 {
432 // NB: like many other WMs, kwin ignores first request for window
433 // position change after the window was mapped. This additional
434 // move+resize event will ensure that the window is restored in
435 // exactly same position as before it was made fullscreen (because
436 // wxTopLevelWindow::ShowFullScreen will call SetSize, thus
437 // setting the position for second time).
438 XMoveResizeWindow(display, w,
439 origRect->x, origRect->y,
440 origRect->width, origRect->height);
441 XSync(display, False);
442 }
443 }
444
445
446 wxX11FullScreenMethod wxGetFullScreenMethodX11(WXDisplay* display,
447 WXWindow rootWindow)
448 {
449 Window root = (Window)rootWindow;
450 Display *disp = (Display*)display;
451
452 // if WM supports _NET_WM_STATE_FULLSCREEN from wm-spec 1.2, use it:
453 wxMAKE_ATOM(_NET_WM_STATE_FULLSCREEN, disp);
454 if (wxQueryWMspecSupport(disp, root, _NET_WM_STATE_FULLSCREEN))
455 {
456 wxLogTrace(_T("fullscreen"),
457 _T("detected _NET_WM_STATE_FULLSCREEN support"));
458 return wxX11_FS_WMSPEC;
459 }
460
461 // if the user is running KDE's kwin WM, use a legacy hack because
462 // kwin doesn't understand any other method:
463 if (wxKwinRunning(disp, root))
464 {
465 wxLogTrace(_T("fullscreen"), _T("detected kwin"));
466 return wxX11_FS_KDE;
467 }
468
469 // finally, fall back to ICCCM heuristic method:
470 wxLogTrace(_T("fullscreen"), _T("unknown WM, using _WIN_LAYER"));
471 return wxX11_FS_GENERIC;
472 }
473
474
475 void wxSetFullScreenStateX11(WXDisplay* display, WXWindow rootWindow,
476 WXWindow window, bool show,
477 wxRect *origRect,
478 wxX11FullScreenMethod method)
479 {
480 // NB: please see the comment under "Fullscreen mode:" title above
481 // for implications of changing this code.
482
483 Window wnd = (Window)window;
484 Window root = (Window)rootWindow;
485 Display *disp = (Display*)display;
486
487 if (method == wxX11_FS_AUTODETECT)
488 method = wxGetFullScreenMethodX11(display, rootWindow);
489
490 switch (method)
491 {
492 case wxX11_FS_WMSPEC:
493 wxWMspecSetFullscreen(disp, root, wnd, show);
494 break;
495 case wxX11_FS_KDE:
496 wxSetKDEFullscreen(disp, root, wnd, show, origRect);
497 break;
498 default:
499 wxWinHintsSetLayer(disp, root, wnd,
500 show ? WIN_LAYER_ABOVE_DOCK : WIN_LAYER_NORMAL);
501 break;
502 }
503 }
504
505 #endif
506