]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkwindow-x11.c
gdk: Remove GdkDrawable->create_cairo_surface vfunc
[~andy/gtk] / gdk / x11 / gdkwindow-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-2007 Peter Mattis, Spencer Kimball,
3  * Josh MacDonald, Ryan Lortie
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include "config.h"
29
30 #include "gdkwindow-x11.h"
31
32 #include "gdkx.h"
33 #include "gdkwindow.h"
34 #include "gdkwindowimpl.h"
35 #include "gdkasync.h"
36 #include "gdkdisplay-x11.h"
37 #include "gdkprivate-x11.h"
38 #include "gdkinternals.h"
39 #include "gdkdeviceprivate.h"
40 #include "gdkeventsource.h"
41
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <netinet/in.h>
46 #include <unistd.h>
47
48 #include <cairo-xlib.h>
49
50 #include "MwmUtil.h"
51
52 #include <X11/Xlib.h>
53 #include <X11/Xutil.h>
54 #include <X11/Xatom.h>
55
56 #include <X11/extensions/shape.h>
57
58 #ifdef HAVE_XKB
59 #include <X11/XKBlib.h>
60 #endif
61
62 #ifdef HAVE_XCOMPOSITE
63 #include <X11/extensions/Xcomposite.h>
64 #endif
65
66 #ifdef HAVE_XFIXES
67 #include <X11/extensions/Xfixes.h>
68 #endif
69
70 #ifdef HAVE_XDAMAGE
71 #include <X11/extensions/Xdamage.h>
72 #endif
73
74 const int _gdk_event_mask_table[21] =
75 {
76   ExposureMask,
77   PointerMotionMask,
78   PointerMotionHintMask,
79   ButtonMotionMask,
80   Button1MotionMask,
81   Button2MotionMask,
82   Button3MotionMask,
83   ButtonPressMask,
84   ButtonReleaseMask,
85   KeyPressMask,
86   KeyReleaseMask,
87   EnterWindowMask,
88   LeaveWindowMask,
89   FocusChangeMask,
90   StructureNotifyMask,
91   PropertyChangeMask,
92   VisibilityChangeMask,
93   0,                            /* PROXIMITY_IN */
94   0,                            /* PROXIMTY_OUT */
95   SubstructureNotifyMask,
96   ButtonPressMask      /* SCROLL; on X mouse wheel events is treated as mouse button 4/5 */
97 };
98 const int _gdk_nenvent_masks = sizeof (_gdk_event_mask_table) / sizeof (int);
99
100 /* Forward declarations */
101 static void     gdk_window_set_static_win_gravity (GdkWindow  *window,
102                                                    gboolean    on);
103 static gboolean gdk_window_icon_name_set          (GdkWindow  *window);
104 static void     set_wm_name                       (GdkDisplay  *display,
105                                                    Window       xwindow,
106                                                    const gchar *name);
107 static void     move_to_current_desktop           (GdkWindow *window);
108 static void     gdk_window_x11_set_background     (GdkWindow      *window,
109                                                    cairo_pattern_t *pattern);
110
111 static void        gdk_window_impl_x11_finalize   (GObject            *object);
112
113 #define WINDOW_IS_TOPLEVEL_OR_FOREIGN(window) \
114   (GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&   \
115    GDK_WINDOW_TYPE (window) != GDK_WINDOW_OFFSCREEN)
116
117 #define WINDOW_IS_TOPLEVEL(window)                   \
118   (GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&   \
119    GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN && \
120    GDK_WINDOW_TYPE (window) != GDK_WINDOW_OFFSCREEN)
121
122 /* Return whether time1 is considered later than time2 as far as xserver
123  * time is concerned.  Accounts for wraparound.
124  */
125 #define XSERVER_TIME_IS_LATER(time1, time2)                        \
126   ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) ||  \
127     (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 ))     \
128   )
129
130 G_DEFINE_TYPE (GdkWindowImplX11, gdk_window_impl_x11, GDK_TYPE_WINDOW_IMPL)
131
132 static void
133 gdk_window_impl_x11_init (GdkWindowImplX11 *impl)
134 {  
135   impl->toplevel_window_type = -1;
136   impl->device_cursor = g_hash_table_new_full (NULL, NULL, NULL,
137                                                (GDestroyNotify) gdk_cursor_unref);
138 }
139
140 GdkToplevelX11 *
141 _gdk_x11_window_get_toplevel (GdkWindow *window)
142 {
143   GdkWindowImplX11 *impl;
144   
145   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
146
147   if (!WINDOW_IS_TOPLEVEL (window))
148     return NULL;
149
150   impl = GDK_WINDOW_IMPL_X11 (window->impl);
151
152   if (!impl->toplevel)
153     impl->toplevel = g_new0 (GdkToplevelX11, 1);
154
155   return impl->toplevel;
156 }
157
158 static const cairo_user_data_key_t gdk_x11_cairo_key;
159
160 /**
161  * _gdk_x11_window_update_size:
162  * @impl: a #GdkWindowImplX11.
163  * 
164  * Updates the state of the window (in particular the drawable's
165  * cairo surface) when its size has changed.
166  **/
167 void
168 _gdk_x11_window_update_size (GdkWindowImplX11 *impl)
169 {
170   if (impl->cairo_surface)
171     {
172       cairo_xlib_surface_set_size (impl->cairo_surface,
173                                    gdk_window_get_width (impl->wrapper),
174                                    gdk_window_get_height (impl->wrapper));
175     }
176 }
177
178 /*****************************************************
179  * X11 specific implementations of generic functions *
180  *****************************************************/
181
182 static void
183 gdk_x11_cairo_surface_destroy (void *data)
184 {
185   GdkWindowImplX11 *impl = data;
186
187   impl->cairo_surface = NULL;
188 }
189
190 static cairo_surface_t *
191 gdk_x11_create_cairo_surface (GdkDrawable *drawable,
192                               int width,
193                               int height)
194 {
195   GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (drawable);
196   GdkVisual *visual;
197     
198   visual = gdk_window_get_visual (impl->wrapper);
199   return cairo_xlib_surface_create (GDK_WINDOW_XDISPLAY (impl->wrapper),
200                                     GDK_WINDOW_IMPL_X11 (impl)->xid,
201                                     GDK_VISUAL_XVISUAL (visual),
202                                     width, height);
203 }
204
205 static cairo_surface_t *
206 gdk_x11_ref_cairo_surface (GdkDrawable *drawable)
207 {
208   GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (drawable);
209
210   if (GDK_IS_WINDOW_IMPL_X11 (drawable) &&
211       GDK_WINDOW_DESTROYED (impl->wrapper))
212     return NULL;
213
214   if (!impl->cairo_surface)
215     {
216       impl->cairo_surface = gdk_x11_create_cairo_surface (drawable,
217                                                           gdk_window_get_width (impl->wrapper),
218                                                           gdk_window_get_height (impl->wrapper));
219       
220       if (impl->cairo_surface)
221         cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
222                                      drawable, gdk_x11_cairo_surface_destroy);
223     }
224   else
225     cairo_surface_reference (impl->cairo_surface);
226
227   return impl->cairo_surface;
228 }
229
230 static void
231 gdk_window_impl_x11_finalize (GObject *object)
232 {
233   GdkWindow *wrapper;
234   GdkWindowImplX11 *impl;
235   
236   g_return_if_fail (GDK_IS_WINDOW_IMPL_X11 (object));
237
238   impl = GDK_WINDOW_IMPL_X11 (object);
239   
240   wrapper = impl->wrapper;
241
242   _gdk_xgrab_check_destroy (wrapper);
243
244   if (!GDK_WINDOW_DESTROYED (wrapper))
245     {
246       GdkDisplay *display = GDK_WINDOW_DISPLAY (wrapper);
247       
248       _gdk_xid_table_remove (display, impl->xid);
249       if (impl->toplevel && impl->toplevel->focus_window)
250         _gdk_xid_table_remove (display, impl->toplevel->focus_window);
251     }
252
253   g_free (impl->toplevel);
254
255   if (impl->cursor)
256     gdk_cursor_unref (impl->cursor);
257
258   g_hash_table_destroy (impl->device_cursor);
259
260   G_OBJECT_CLASS (gdk_window_impl_x11_parent_class)->finalize (object);
261 }
262
263 typedef struct {
264   GdkDisplay *display;
265   Pixmap pixmap;
266 } FreePixmapData;
267
268 static void
269 free_pixmap (gpointer datap)
270 {
271   FreePixmapData *data = datap;
272
273   if (!gdk_display_is_closed (data->display))
274     {
275       XFreePixmap (GDK_DISPLAY_XDISPLAY (data->display),
276                    data->pixmap);
277     }
278
279   g_object_unref (data->display);
280   g_slice_free (FreePixmapData, data);
281 }
282
283 static void
284 attach_free_pixmap_handler (cairo_surface_t *surface,
285                             GdkDisplay      *display,
286                             Pixmap           pixmap)
287 {
288   static const cairo_user_data_key_t key;
289   FreePixmapData *data;
290   
291   data = g_slice_new (FreePixmapData);
292   data->display = g_object_ref (display);
293   data->pixmap = pixmap;
294
295   cairo_surface_set_user_data (surface, &key, data, free_pixmap);
296 }
297
298 /* Cairo does not guarantee we get an xlib surface if we call
299  * cairo_surface_create_similar(). In some cases however, we must use a
300  * pixmap or bitmap in the X11 API.
301  * These functions ensure an Xlib surface.
302  */
303 cairo_surface_t *
304 _gdk_x11_window_create_bitmap_surface (GdkWindow *window,
305                                        int        width,
306                                        int        height)
307 {
308   cairo_surface_t *surface;
309   Pixmap pixmap;
310
311   pixmap = XCreatePixmap (GDK_WINDOW_XDISPLAY (window),
312                           GDK_WINDOW_XID (window),
313                           width, height, 1);
314   surface = cairo_xlib_surface_create_for_bitmap (GDK_WINDOW_XDISPLAY (window),
315                                                   pixmap,
316                                                   GDK_SCREEN_XSCREEN (GDK_WINDOW_SCREEN (window)),
317                                                   width, height);
318   attach_free_pixmap_handler (surface, GDK_WINDOW_DISPLAY (window), pixmap);
319
320   return surface;
321 }
322
323 /* Create a surface backed with a pixmap without alpha on the same screen as window */
324 static cairo_surface_t *
325 gdk_x11_window_create_pixmap_surface (GdkWindow *window,
326                                       int        width,
327                                       int        height)
328 {
329   GdkScreen *screen = gdk_window_get_screen (window);
330   GdkVisual *visual = gdk_screen_get_system_visual (screen);
331   cairo_surface_t *surface;
332   Pixmap pixmap;
333
334   pixmap = XCreatePixmap (GDK_WINDOW_XDISPLAY (window),
335                           GDK_WINDOW_XID (window),
336                           width, height,
337                           gdk_visual_get_depth (visual));
338   surface = cairo_xlib_surface_create (GDK_WINDOW_XDISPLAY (window),
339                                        pixmap,
340                                        GDK_VISUAL_XVISUAL (visual),
341                                        width, height);
342   attach_free_pixmap_handler (surface, GDK_WINDOW_DISPLAY (window), pixmap);
343
344   return surface;
345 }
346
347 static void
348 tmp_unset_bg (GdkWindow *window)
349 {
350   GdkWindowImplX11 *impl;
351
352   impl = GDK_WINDOW_IMPL_X11 (window->impl);
353
354   impl->no_bg = TRUE;
355
356   XSetWindowBackgroundPixmap (GDK_WINDOW_XDISPLAY (window),
357                               GDK_WINDOW_XID (window), None);
358 }
359
360 static void
361 tmp_reset_bg (GdkWindow *window)
362 {
363   GdkWindowImplX11 *impl;
364
365   impl = GDK_WINDOW_IMPL_X11 (window->impl);
366
367   impl->no_bg = FALSE;
368
369   gdk_window_x11_set_background (window, window->background);
370 }
371
372 /* Unsetting and resetting window backgrounds.
373  *
374  * In many cases it is possible to avoid flicker by unsetting the
375  * background of windows. For example if the background of the
376  * parent window is unset when a window is unmapped, a brief flicker
377  * of background painting is avoided.
378  */
379 void
380 _gdk_x11_window_tmp_unset_bg (GdkWindow *window,
381                               gboolean   recurse)
382 {
383   g_return_if_fail (GDK_IS_WINDOW (window));
384   
385   if (window->input_only || window->destroyed ||
386       (window->window_type != GDK_WINDOW_ROOT &&
387        !GDK_WINDOW_IS_MAPPED (window)))
388     return;
389   
390   if (_gdk_window_has_impl (window) &&
391       GDK_WINDOW_IS_X11 (window) &&
392       window->window_type != GDK_WINDOW_ROOT &&
393       window->window_type != GDK_WINDOW_FOREIGN)
394     tmp_unset_bg (window);
395
396   if (recurse)
397     {
398       GList *l;
399
400       for (l = window->children; l != NULL; l = l->next)
401         _gdk_x11_window_tmp_unset_bg (l->data, TRUE);
402     }
403 }
404
405 void
406 _gdk_x11_window_tmp_unset_parent_bg (GdkWindow *window)
407 {
408   if (GDK_WINDOW_TYPE (window->parent) == GDK_WINDOW_ROOT)
409     return;
410   
411   window = _gdk_window_get_impl_window (window->parent);
412   _gdk_x11_window_tmp_unset_bg (window, FALSE);
413 }
414
415 void
416 _gdk_x11_window_tmp_reset_bg (GdkWindow *window,
417                               gboolean   recurse)
418 {
419   g_return_if_fail (GDK_IS_WINDOW (window));
420
421   if (window->input_only || window->destroyed ||
422       (window->window_type != GDK_WINDOW_ROOT &&
423        !GDK_WINDOW_IS_MAPPED (window)))
424     return;
425
426   
427   if (_gdk_window_has_impl (window) &&
428       GDK_WINDOW_IS_X11 (window) &&
429       window->window_type != GDK_WINDOW_ROOT &&
430       window->window_type != GDK_WINDOW_FOREIGN)
431     tmp_reset_bg (window);
432
433   if (recurse)
434     {
435       GList *l;
436
437       for (l = window->children; l != NULL; l = l->next)
438         _gdk_x11_window_tmp_reset_bg (l->data, TRUE);
439     }
440 }
441
442 void
443 _gdk_x11_window_tmp_reset_parent_bg (GdkWindow *window)
444 {
445   if (GDK_WINDOW_TYPE (window->parent) == GDK_WINDOW_ROOT)
446     return;
447   
448   window = _gdk_window_get_impl_window (window->parent);
449
450   _gdk_x11_window_tmp_reset_bg (window, FALSE);
451 }
452
453 void
454 _gdk_windowing_window_init (GdkScreen * screen)
455 {
456   GdkWindow *window;
457   GdkWindowImplX11 *impl;
458   GdkScreenX11 *screen_x11;
459
460   screen_x11 = GDK_SCREEN_X11 (screen);
461
462   g_assert (screen_x11->root_window == NULL);
463
464   window = screen_x11->root_window = g_object_new (GDK_TYPE_WINDOW, NULL);
465
466   window->impl = g_object_new (GDK_TYPE_WINDOW_IMPL_X11, NULL);
467   window->impl_window = window;
468   window->visual = gdk_screen_get_system_visual (screen);
469
470   impl = GDK_WINDOW_IMPL_X11 (window->impl);
471   
472   impl->xid = screen_x11->xroot_window;
473   impl->wrapper = window;
474   
475   window->window_type = GDK_WINDOW_ROOT;
476   window->depth = DefaultDepthOfScreen (screen_x11->xscreen);
477
478   window->x = 0;
479   window->y = 0;
480   window->abs_x = 0;
481   window->abs_y = 0;
482   window->width = WidthOfScreen (screen_x11->xscreen);
483   window->height = HeightOfScreen (screen_x11->xscreen);
484   window->viewable = TRUE;
485
486   /* see init_randr_support() in gdkscreen-x11.c */
487   window->event_mask = GDK_STRUCTURE_MASK;
488
489   _gdk_window_update_size (screen_x11->root_window);
490   
491   _gdk_xid_table_insert (screen_x11->display,
492                          &screen_x11->xroot_window,
493                          screen_x11->root_window);
494 }
495
496 static void
497 set_wm_protocols (GdkWindow *window)
498 {
499   GdkDisplay *display = gdk_window_get_display (window);
500   Atom protocols[4];
501   int n = 0;
502   
503   protocols[n++] = gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW");
504   protocols[n++] = gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS");
505   protocols[n++] = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING");
506
507 #ifdef HAVE_XSYNC
508   if (GDK_DISPLAY_X11 (display)->use_sync)
509     protocols[n++] = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_SYNC_REQUEST");
510 #endif
511   
512   XSetWMProtocols (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window), protocols, n);
513 }
514
515 static const gchar *
516 get_default_title (void)
517 {
518   const char *title;
519
520   title = g_get_application_name ();
521   if (!title)
522     title = g_get_prgname ();
523   if (!title)
524     title = "";
525
526   return title;
527 }
528
529 static void
530 check_leader_window_title (GdkDisplay *display)
531 {
532   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
533
534   if (display_x11->leader_window && !display_x11->leader_window_title_set)
535     {
536       set_wm_name (display,
537                    display_x11->leader_window,
538                    get_default_title ());
539       
540       display_x11->leader_window_title_set = TRUE;
541     }
542 }
543
544 static Window
545 create_focus_window (GdkDisplay *display,
546                      XID         parent)
547 {
548   GdkDisplayX11 *display_x11;
549   GdkEventMask event_mask;
550   Display *xdisplay;
551   Window focus_window;
552
553   xdisplay = GDK_DISPLAY_XDISPLAY (display);
554   display_x11 = GDK_DISPLAY_X11 (display);
555
556   focus_window = XCreateSimpleWindow (xdisplay, parent,
557                                       -1, -1, 1, 1, 0,
558                                       0, 0);
559
560   /* FIXME: probably better to actually track the requested event mask for the toplevel
561    */
562   event_mask = (GDK_KEY_PRESS_MASK |
563                 GDK_KEY_RELEASE_MASK |
564                 GDK_FOCUS_CHANGE_MASK);
565
566   gdk_event_source_select_events ((GdkEventSource *) display_x11->event_source,
567                                   focus_window,
568                                   event_mask, 0);
569
570   XMapWindow (xdisplay, focus_window);
571
572   return focus_window;
573 }
574
575 static void
576 ensure_sync_counter (GdkWindow *window)
577 {
578 #ifdef HAVE_XSYNC
579   if (!GDK_WINDOW_DESTROYED (window))
580     {
581       GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
582       GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
583       GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (window->impl);
584
585       if (toplevel && impl->use_synchronized_configure &&
586           toplevel->update_counter == None &&
587           GDK_DISPLAY_X11 (display)->use_sync)
588         {
589           Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
590           XSyncValue value;
591           Atom atom;
592
593           XSyncIntToValue (&value, 0);
594           
595           toplevel->update_counter = XSyncCreateCounter (xdisplay, value);
596           
597           atom = gdk_x11_get_xatom_by_name_for_display (display,
598                                                         "_NET_WM_SYNC_REQUEST_COUNTER");
599           
600           XChangeProperty (xdisplay, GDK_WINDOW_XID (window),
601                            atom, XA_CARDINAL,
602                            32, PropModeReplace,
603                            (guchar *)&toplevel->update_counter, 1);
604           
605           XSyncIntToValue (&toplevel->current_counter_value, 0);
606         }
607     }
608 #endif
609 }
610
611 static void
612 setup_toplevel_window (GdkWindow *window, 
613                        GdkWindow *parent)
614 {
615   GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
616   GdkDisplay *display = gdk_window_get_display (window);
617   Display *xdisplay = GDK_WINDOW_XDISPLAY (window);
618   XID xid = GDK_WINDOW_XID (window);
619   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (GDK_WINDOW_SCREEN (parent));
620   XSizeHints size_hints;
621   long pid;
622   Window leader_window;
623
624   set_wm_protocols (window);
625
626   if (!window->input_only)
627     {
628       /* The focus window is off the visible area, and serves to receive key
629        * press events so they don't get sent to child windows.
630        */
631       toplevel->focus_window = create_focus_window (display, xid);
632       _gdk_xid_table_insert (screen_x11->display, &toplevel->focus_window, window);
633     }
634   
635   check_leader_window_title (screen_x11->display);
636   
637   /* FIXME: Is there any point in doing this? Do any WM's pay
638    * attention to PSize, and even if they do, is this the
639    * correct value???
640    */
641   size_hints.flags = PSize;
642   size_hints.width = window->width;
643   size_hints.height = window->height;
644   
645   XSetWMNormalHints (xdisplay, xid, &size_hints);
646   
647   /* This will set WM_CLIENT_MACHINE and WM_LOCALE_NAME */
648   XSetWMProperties (xdisplay, xid, NULL, NULL, NULL, 0, NULL, NULL, NULL);
649   
650   pid = getpid ();
651   XChangeProperty (xdisplay, xid,
652                    gdk_x11_get_xatom_by_name_for_display (screen_x11->display, "_NET_WM_PID"),
653                    XA_CARDINAL, 32,
654                    PropModeReplace,
655                    (guchar *)&pid, 1);
656
657   leader_window = GDK_DISPLAY_X11 (screen_x11->display)->leader_window;
658   if (!leader_window)
659     leader_window = xid;
660   XChangeProperty (xdisplay, xid, 
661                    gdk_x11_get_xatom_by_name_for_display (screen_x11->display, "WM_CLIENT_LEADER"),
662                    XA_WINDOW, 32, PropModeReplace,
663                    (guchar *) &leader_window, 1);
664
665   if (toplevel->focus_window != None)
666     XChangeProperty (xdisplay, xid, 
667                      gdk_x11_get_xatom_by_name_for_display (screen_x11->display, "_NET_WM_USER_TIME_WINDOW"),
668                      XA_WINDOW, 32, PropModeReplace,
669                      (guchar *) &toplevel->focus_window, 1);
670
671   if (!window->focus_on_map)
672     gdk_x11_window_set_user_time (window, 0);
673   else if (GDK_DISPLAY_X11 (screen_x11->display)->user_time != 0)
674     gdk_x11_window_set_user_time (window, GDK_DISPLAY_X11 (screen_x11->display)->user_time);
675
676   ensure_sync_counter (window);
677 }
678
679 void
680 _gdk_window_impl_new (GdkWindow     *window,
681                       GdkWindow     *real_parent,
682                       GdkScreen     *screen,
683                       GdkEventMask   event_mask,
684                       GdkWindowAttr *attributes,
685                       gint           attributes_mask)
686 {
687   GdkWindowImplX11 *impl;
688   GdkScreenX11 *screen_x11;
689   GdkDisplayX11 *display_x11;
690   
691   Window xparent;
692   Visual *xvisual;
693   Display *xdisplay;
694
695   XSetWindowAttributes xattributes;
696   long xattributes_mask;
697   XClassHint *class_hint;
698   
699   unsigned int class;
700   const char *title;
701   
702   screen_x11 = GDK_SCREEN_X11 (screen);
703   xparent = GDK_WINDOW_XID (real_parent);
704   display_x11 = GDK_DISPLAY_X11 (GDK_SCREEN_DISPLAY (screen));
705   
706   impl = g_object_new (GDK_TYPE_WINDOW_IMPL_X11, NULL);
707   window->impl = (GdkDrawable *)impl;
708   impl->wrapper = GDK_WINDOW (window);
709   
710   xdisplay = screen_x11->xdisplay;
711
712   xattributes_mask = 0;
713
714   xvisual = gdk_x11_visual_get_xvisual (window->visual);
715   
716   if (attributes_mask & GDK_WA_NOREDIR)
717     {
718       xattributes.override_redirect =
719         (attributes->override_redirect == FALSE)?False:True;
720       xattributes_mask |= CWOverrideRedirect;
721     } 
722   else
723     xattributes.override_redirect = False;
724
725   impl->override_redirect = xattributes.override_redirect;
726   
727   if (window->parent && window->parent->guffaw_gravity)
728     {
729       xattributes.win_gravity = StaticGravity;
730       xattributes_mask |= CWWinGravity;
731     }
732   
733   /* Sanity checks */
734   switch (window->window_type)
735     {
736     case GDK_WINDOW_TOPLEVEL:
737     case GDK_WINDOW_TEMP:
738       if (GDK_WINDOW_TYPE (window->parent) != GDK_WINDOW_ROOT)
739         {
740           /* The common code warns for this case */
741           xparent = GDK_SCREEN_XROOTWIN (screen);
742         }
743     }
744           
745   if (!window->input_only)
746     {
747       class = InputOutput;
748
749       xattributes.background_pixel = BlackPixel (xdisplay, screen_x11->screen_num);
750
751       xattributes.border_pixel = BlackPixel (xdisplay, screen_x11->screen_num);
752       xattributes_mask |= CWBorderPixel | CWBackPixel;
753
754       if (window->guffaw_gravity)
755         xattributes.bit_gravity = StaticGravity;
756       else
757         xattributes.bit_gravity = NorthWestGravity;
758       
759       xattributes_mask |= CWBitGravity;
760
761       xattributes.colormap = _gdk_visual_get_x11_colormap (window->visual);
762       xattributes_mask |= CWColormap;
763
764       if (window->window_type == GDK_WINDOW_TEMP)
765         {
766           xattributes.save_under = True;
767           xattributes.override_redirect = True;
768           xattributes.cursor = None;
769           xattributes_mask |= CWSaveUnder | CWOverrideRedirect;
770
771           impl->override_redirect = TRUE;
772         }
773     }
774   else
775     {
776       class = InputOnly;
777     }
778
779   if (window->width > 65535 ||
780       window->height > 65535)
781     {
782       g_warning ("Native Windows wider or taller than 65535 pixels are not supported");
783       
784       if (window->width > 65535)
785         window->width = 65535;
786       if (window->height > 65535)
787         window->height = 65535;
788     }
789   
790   impl->xid = XCreateWindow (xdisplay, xparent,
791                              window->x + window->parent->abs_x,
792                              window->y + window->parent->abs_y,
793                              window->width, window->height,
794                              0, window->depth, class, xvisual,
795                              xattributes_mask, &xattributes);
796
797   g_object_ref (window);
798   _gdk_xid_table_insert (screen_x11->display, &impl->xid, window);
799
800   switch (GDK_WINDOW_TYPE (window))
801     {
802     case GDK_WINDOW_TOPLEVEL:
803     case GDK_WINDOW_TEMP:
804       if (attributes_mask & GDK_WA_TITLE)
805         title = attributes->title;
806       else
807         title = get_default_title ();
808       
809       gdk_window_set_title (window, title);
810       
811       if (attributes_mask & GDK_WA_WMCLASS)
812         {
813           class_hint = XAllocClassHint ();
814           class_hint->res_name = attributes->wmclass_name;
815           class_hint->res_class = attributes->wmclass_class;
816           XSetClassHint (xdisplay, impl->xid, class_hint);
817           XFree (class_hint);
818         }
819   
820       setup_toplevel_window (window, window->parent);
821       break;
822
823     case GDK_WINDOW_CHILD:
824     default:
825       break;
826     }
827
828   if (attributes_mask & GDK_WA_TYPE_HINT)
829     gdk_window_set_type_hint (window, attributes->type_hint);
830
831   gdk_event_source_select_events ((GdkEventSource *) display_x11->event_source,
832                                   GDK_WINDOW_XID (window), event_mask,
833                                   StructureNotifyMask | PropertyChangeMask);
834 }
835
836 static GdkEventMask
837 x_event_mask_to_gdk_event_mask (long mask)
838 {
839   GdkEventMask event_mask = 0;
840   int i;
841
842   for (i = 0; i < _gdk_nenvent_masks; i++)
843     {
844       if (mask & _gdk_event_mask_table[i])
845         event_mask |= 1 << (i + 1);
846     }
847
848   return event_mask;
849 }
850
851 /**
852  * gdk_window_foreign_new_for_display:
853  * @display: the #GdkDisplay where the window handle comes from.
854  * @anid: a native window handle.
855  * 
856  * Wraps a native window in a #GdkWindow.
857  * This may fail if the window has been destroyed. If the window
858  * was already known to GDK, a new reference to the existing 
859  * #GdkWindow is returned.
860  *
861  * For example in the X backend, a native window handle is an Xlib
862  * <type>XID</type>.
863  * 
864  * Return value: (transfer full): a #GdkWindow wrapper for the native
865  *   window, or %NULL if the window has been destroyed. The wrapper
866  *   will be newly created, if one doesn't exist already.
867  *
868  * Since: 2.2
869  **/
870 GdkWindow *
871 gdk_window_foreign_new_for_display (GdkDisplay     *display,
872                                     GdkNativeWindow anid)
873 {
874   GdkScreen *screen;
875   GdkWindow *window;
876   GdkWindowImplX11 *impl;
877   GdkDisplayX11 *display_x11;
878   XWindowAttributes attrs;
879   Window root, parent;
880   Window *children = NULL;
881   guint nchildren;
882   gboolean result;
883
884   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
885
886   display_x11 = GDK_DISPLAY_X11 (display);
887
888   if ((window = gdk_xid_table_lookup_for_display (display, anid)) != NULL)
889     return g_object_ref (window);
890
891   gdk_error_trap_push ();
892   result = XGetWindowAttributes (display_x11->xdisplay, anid, &attrs);
893   if (gdk_error_trap_pop () || !result)
894     return NULL;
895
896   /* FIXME: This is pretty expensive. Maybe the caller should supply
897    *        the parent */
898   gdk_error_trap_push ();
899   result = XQueryTree (display_x11->xdisplay, anid, &root, &parent, &children, &nchildren);
900   if (gdk_error_trap_pop () || !result)
901     return NULL;
902
903   if (children)
904     XFree (children);
905   
906   screen = _gdk_x11_display_screen_for_xrootwin (display, root);
907
908   window = g_object_new (GDK_TYPE_WINDOW, NULL);
909
910   window->impl = g_object_new (GDK_TYPE_WINDOW_IMPL_X11, NULL);
911   window->impl_window = window;
912   window->visual = gdk_x11_screen_lookup_visual (screen,
913                                                   XVisualIDFromVisual (attrs.visual));
914
915   impl = GDK_WINDOW_IMPL_X11 (window->impl);
916   impl->wrapper = window;
917   
918   window->parent = gdk_xid_table_lookup_for_display (display, parent);
919   
920   if (!window->parent || GDK_WINDOW_TYPE (window->parent) == GDK_WINDOW_FOREIGN)
921     window->parent = gdk_screen_get_root_window (screen);
922   
923   window->parent->children = g_list_prepend (window->parent->children, window);
924
925   impl->xid = anid;
926
927   window->x = attrs.x;
928   window->y = attrs.y;
929   window->width = attrs.width;
930   window->height = attrs.height;
931   window->window_type = GDK_WINDOW_FOREIGN;
932   window->destroyed = FALSE;
933
934   window->event_mask = x_event_mask_to_gdk_event_mask (attrs.your_event_mask);
935
936   if (attrs.map_state == IsUnmapped)
937     window->state = GDK_WINDOW_STATE_WITHDRAWN;
938   else
939     window->state = 0;
940   window->viewable = TRUE;
941
942   window->depth = attrs.depth;
943   
944   g_object_ref (window);
945   _gdk_xid_table_insert (display, &GDK_WINDOW_XID (window), window);
946
947   /* Update the clip region, etc */
948   _gdk_window_update_size (window);
949
950   return window;
951 }
952
953 /**
954  * gdk_window_lookup_for_display:
955  * @display: the #GdkDisplay corresponding to the window handle
956  * @anid: a native window handle.
957  *
958  * Looks up the #GdkWindow that wraps the given native window handle.
959  *
960  * For example in the X backend, a native window handle is an Xlib
961  * <type>XID</type>.
962  *
963  * Return value: (transfer none): the #GdkWindow wrapper for the native
964  *    window, or %NULL if there is none.
965  *
966  * Since: 2.2
967  **/
968 GdkWindow *
969 gdk_window_lookup_for_display (GdkDisplay *display, GdkNativeWindow anid)
970 {
971   return (GdkWindow*) gdk_xid_table_lookup_for_display (display, anid);
972 }
973
974 /**
975  * gdk_window_lookup:
976  * @anid: a native window handle.
977  *
978  * Looks up the #GdkWindow that wraps the given native window handle. 
979  *
980  * For example in the X backend, a native window handle is an Xlib
981  * <type>XID</type>.
982  *
983  * Return value: (transfer none): the #GdkWindow wrapper for the native
984  *    window, or %NULL if there is none.
985  **/
986 GdkWindow *
987 gdk_window_lookup (GdkNativeWindow anid)
988 {
989   return (GdkWindow*) gdk_xid_table_lookup (anid);
990 }
991
992 static void
993 gdk_toplevel_x11_free_contents (GdkDisplay *display,
994                                 GdkToplevelX11 *toplevel)
995 {
996   if (toplevel->icon_pixmap)
997     {
998       cairo_surface_destroy (toplevel->icon_pixmap);
999       toplevel->icon_pixmap = NULL;
1000     }
1001   if (toplevel->icon_mask)
1002     {
1003       cairo_surface_destroy (toplevel->icon_mask);
1004       toplevel->icon_mask = NULL;
1005     }
1006   if (toplevel->group_leader)
1007     {
1008       g_object_unref (toplevel->group_leader);
1009       toplevel->group_leader = NULL;
1010     }
1011 #ifdef HAVE_XSYNC
1012   if (toplevel->update_counter != None)
1013     {
1014       XSyncDestroyCounter (GDK_DISPLAY_XDISPLAY (display), 
1015                            toplevel->update_counter);
1016       toplevel->update_counter = None;
1017
1018       XSyncIntToValue (&toplevel->current_counter_value, 0);
1019     }
1020 #endif
1021 }
1022
1023 static void
1024 _gdk_x11_window_destroy (GdkWindow *window,
1025                          gboolean   recursing,
1026                          gboolean   foreign_destroy)
1027 {
1028   GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (window->impl);
1029   GdkToplevelX11 *toplevel;
1030   
1031   g_return_if_fail (GDK_IS_WINDOW (window));
1032
1033   _gdk_selection_window_destroyed (window);
1034   
1035   toplevel = _gdk_x11_window_get_toplevel (window);
1036   if (toplevel)
1037     gdk_toplevel_x11_free_contents (GDK_WINDOW_DISPLAY (window), toplevel);
1038
1039   if (impl->cairo_surface)
1040     {
1041       cairo_surface_finish (impl->cairo_surface);
1042       cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
1043                                    NULL, NULL);
1044     }
1045
1046   if (!recursing && !foreign_destroy)
1047     {
1048       XDestroyWindow (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window));
1049     }
1050 }
1051
1052 static cairo_surface_t *
1053 gdk_window_x11_resize_cairo_surface (GdkDrawable     *window,
1054                                      cairo_surface_t *surface,
1055                                      gint             width,
1056                                      gint             height)
1057 {
1058   cairo_xlib_surface_set_size (surface, width, height);
1059
1060   return surface;
1061 }
1062
1063 void
1064 _gdk_windowing_window_destroy_foreign (GdkWindow *window)
1065 {
1066   /* It's somebody else's window, but in our hierarchy,
1067    * so reparent it to the root window, and then send
1068    * it a delete event, as if we were a WM
1069    */
1070   XClientMessageEvent xclient;
1071   
1072   gdk_error_trap_push ();
1073   gdk_window_hide (window);
1074   gdk_window_reparent (window, NULL, 0, 0);
1075   
1076   memset (&xclient, 0, sizeof (xclient));
1077   xclient.type = ClientMessage;
1078   xclient.window = GDK_WINDOW_XID (window);
1079   xclient.message_type = gdk_x11_get_xatom_by_name_for_display (GDK_WINDOW_DISPLAY (window),
1080                                                                "WM_PROTOCOLS");
1081   xclient.format = 32;
1082   xclient.data.l[0] = gdk_x11_get_xatom_by_name_for_display (GDK_WINDOW_DISPLAY (window),
1083                                                             "WM_DELETE_WINDOW");
1084   xclient.data.l[1] = CurrentTime;
1085   xclient.data.l[2] = 0;
1086   xclient.data.l[3] = 0;
1087   xclient.data.l[4] = 0;
1088   
1089   XSendEvent (GDK_WINDOW_XDISPLAY (window),
1090               GDK_WINDOW_XID (window),
1091               False, 0, (XEvent *)&xclient);
1092   gdk_error_trap_pop_ignored ();
1093 }
1094
1095 static GdkWindow *
1096 get_root (GdkWindow *window)
1097 {
1098   GdkScreen *screen = gdk_window_get_screen (window);
1099
1100   return gdk_screen_get_root_window (screen);
1101 }
1102
1103 /* This function is called when the XWindow is really gone.
1104  */
1105 void
1106 gdk_window_destroy_notify (GdkWindow *window)
1107 {
1108   GdkWindowImplX11 *window_impl;
1109
1110   window_impl = GDK_WINDOW_IMPL_X11 ((window)->impl);
1111
1112   if (!GDK_WINDOW_DESTROYED (window))
1113     {
1114       if (GDK_WINDOW_TYPE(window) != GDK_WINDOW_FOREIGN)
1115         g_warning ("GdkWindow %#lx unexpectedly destroyed", GDK_WINDOW_XID (window));
1116
1117       _gdk_window_destroy (window, TRUE);
1118     }
1119   
1120   _gdk_xid_table_remove (GDK_WINDOW_DISPLAY (window), GDK_WINDOW_XID (window));
1121   if (window_impl->toplevel && window_impl->toplevel->focus_window)
1122     _gdk_xid_table_remove (GDK_WINDOW_DISPLAY (window), window_impl->toplevel->focus_window);
1123
1124   _gdk_xgrab_check_destroy (window);
1125   
1126   g_object_unref (window);
1127 }
1128
1129 static void
1130 update_wm_hints (GdkWindow *window,
1131                  gboolean   force)
1132 {
1133   GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
1134   GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
1135   XWMHints wm_hints;
1136
1137   if (!force &&
1138       !toplevel->is_leader &&
1139       window->state & GDK_WINDOW_STATE_WITHDRAWN)
1140     return;
1141
1142   wm_hints.flags = StateHint | InputHint;
1143   wm_hints.input = window->accept_focus ? True : False;
1144   wm_hints.initial_state = NormalState;
1145   
1146   if (window->state & GDK_WINDOW_STATE_ICONIFIED)
1147     {
1148       wm_hints.flags |= StateHint;
1149       wm_hints.initial_state = IconicState;
1150     }
1151
1152   if (toplevel->icon_pixmap)
1153     {
1154       wm_hints.flags |= IconPixmapHint;
1155       wm_hints.icon_pixmap = cairo_xlib_surface_get_drawable (toplevel->icon_pixmap);
1156     }
1157
1158   if (toplevel->icon_mask)
1159     {
1160       wm_hints.flags |= IconMaskHint;
1161       wm_hints.icon_mask = cairo_xlib_surface_get_drawable (toplevel->icon_mask);
1162     }
1163   
1164   wm_hints.flags |= WindowGroupHint;
1165   if (toplevel->group_leader && !GDK_WINDOW_DESTROYED (toplevel->group_leader))
1166     {
1167       wm_hints.flags |= WindowGroupHint;
1168       wm_hints.window_group = GDK_WINDOW_XID (toplevel->group_leader);
1169     }
1170   else
1171     wm_hints.window_group = GDK_DISPLAY_X11 (display)->leader_window;
1172
1173   if (toplevel->urgency_hint)
1174     wm_hints.flags |= XUrgencyHint;
1175   
1176   XSetWMHints (GDK_WINDOW_XDISPLAY (window),
1177                GDK_WINDOW_XID (window),
1178                &wm_hints);
1179 }
1180
1181 static void
1182 set_initial_hints (GdkWindow *window)
1183 {
1184   GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
1185   Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
1186   Window xwindow = GDK_WINDOW_XID (window);  
1187   GdkToplevelX11 *toplevel;
1188   Atom atoms[9];
1189   gint i;
1190
1191   toplevel = _gdk_x11_window_get_toplevel (window);
1192
1193   if (!toplevel)
1194     return;
1195
1196   update_wm_hints (window, TRUE);
1197   
1198   /* We set the spec hints regardless of whether the spec is supported,
1199    * since it can't hurt and it's kind of expensive to check whether
1200    * it's supported.
1201    */
1202   
1203   i = 0;
1204
1205   if (window->state & GDK_WINDOW_STATE_MAXIMIZED)
1206     {
1207       atoms[i] = gdk_x11_get_xatom_by_name_for_display (display,
1208                                                         "_NET_WM_STATE_MAXIMIZED_VERT");
1209       ++i;
1210       atoms[i] = gdk_x11_get_xatom_by_name_for_display (display,
1211                                                         "_NET_WM_STATE_MAXIMIZED_HORZ");
1212       ++i;
1213       toplevel->have_maxhorz = toplevel->have_maxvert = TRUE;
1214     }
1215
1216   if (window->state & GDK_WINDOW_STATE_ABOVE)
1217     {
1218       atoms[i] = gdk_x11_get_xatom_by_name_for_display (display,
1219                                                         "_NET_WM_STATE_ABOVE");
1220       ++i;
1221     }
1222   
1223   if (window->state & GDK_WINDOW_STATE_BELOW)
1224     {
1225       atoms[i] = gdk_x11_get_xatom_by_name_for_display (display,
1226                                                         "_NET_WM_STATE_BELOW");
1227       ++i;
1228     }
1229   
1230   if (window->state & GDK_WINDOW_STATE_STICKY)
1231     {
1232       atoms[i] = gdk_x11_get_xatom_by_name_for_display (display,
1233                                                         "_NET_WM_STATE_STICKY");
1234       ++i;
1235       toplevel->have_sticky = TRUE;
1236     }
1237
1238   if (window->state & GDK_WINDOW_STATE_FULLSCREEN)
1239     {
1240       atoms[i] = gdk_x11_get_xatom_by_name_for_display (display,
1241                                                         "_NET_WM_STATE_FULLSCREEN");
1242       ++i;
1243       toplevel->have_fullscreen = TRUE;
1244     }
1245
1246   if (window->modal_hint)
1247     {
1248       atoms[i] = gdk_x11_get_xatom_by_name_for_display (display,
1249                                                         "_NET_WM_STATE_MODAL");
1250       ++i;
1251     }
1252
1253   if (toplevel->skip_taskbar_hint)
1254     {
1255       atoms[i] = gdk_x11_get_xatom_by_name_for_display (display,
1256                                                         "_NET_WM_STATE_SKIP_TASKBAR");
1257       ++i;
1258     }
1259
1260   if (toplevel->skip_pager_hint)
1261     {
1262       atoms[i] = gdk_x11_get_xatom_by_name_for_display (display,
1263                                                         "_NET_WM_STATE_SKIP_PAGER");
1264       ++i;
1265     }
1266
1267   if (i > 0)
1268     {
1269       XChangeProperty (xdisplay,
1270                        xwindow,
1271                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
1272                        XA_ATOM, 32, PropModeReplace,
1273                        (guchar*) atoms, i);
1274     }
1275   else 
1276     {
1277       XDeleteProperty (xdisplay,
1278                        xwindow,
1279                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"));
1280     }
1281
1282   if (window->state & GDK_WINDOW_STATE_STICKY)
1283     {
1284       atoms[0] = 0xFFFFFFFF;
1285       XChangeProperty (xdisplay,
1286                        xwindow,
1287                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"),
1288                        XA_CARDINAL, 32, PropModeReplace,
1289                        (guchar*) atoms, 1);
1290       toplevel->on_all_desktops = TRUE;
1291     }
1292   else
1293     {
1294       XDeleteProperty (xdisplay,
1295                        xwindow,
1296                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"));
1297     }
1298
1299   toplevel->map_serial = NextRequest (xdisplay);
1300 }
1301
1302 static void
1303 gdk_window_x11_show (GdkWindow *window, gboolean already_mapped)
1304 {
1305   GdkDisplay *display;
1306   GdkDisplayX11 *display_x11;
1307   GdkToplevelX11 *toplevel;
1308   GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (window->impl);
1309   Display *xdisplay = GDK_WINDOW_XDISPLAY (window);
1310   Window xwindow = GDK_WINDOW_XID (window);
1311   gboolean unset_bg;
1312
1313   if (!already_mapped)
1314     set_initial_hints (window);
1315       
1316   if (WINDOW_IS_TOPLEVEL (window))
1317     {
1318       display = gdk_window_get_display (window);
1319       display_x11 = GDK_DISPLAY_X11 (display);
1320       toplevel = _gdk_x11_window_get_toplevel (window);
1321       
1322       if (toplevel->user_time != 0 &&
1323               display_x11->user_time != 0 &&
1324           XSERVER_TIME_IS_LATER (display_x11->user_time, toplevel->user_time))
1325         gdk_x11_window_set_user_time (window, display_x11->user_time);
1326     }
1327   
1328   unset_bg = !window->input_only &&
1329     (window->window_type == GDK_WINDOW_CHILD ||
1330      impl->override_redirect) &&
1331     gdk_window_is_viewable (window);
1332   
1333   if (unset_bg)
1334     _gdk_x11_window_tmp_unset_bg (window, TRUE);
1335   
1336   XMapWindow (xdisplay, xwindow);
1337   
1338   if (unset_bg)
1339     _gdk_x11_window_tmp_reset_bg (window, TRUE);
1340 }
1341
1342 static void
1343 pre_unmap (GdkWindow *window)
1344 {
1345   GdkWindow *start_window = NULL;
1346
1347   if (window->input_only)
1348     return;
1349
1350   if (window->window_type == GDK_WINDOW_CHILD)
1351     start_window = _gdk_window_get_impl_window ((GdkWindow *)window->parent);
1352   else if (window->window_type == GDK_WINDOW_TEMP)
1353     start_window = get_root (window);
1354
1355   if (start_window)
1356     _gdk_x11_window_tmp_unset_bg (start_window, TRUE);
1357 }
1358
1359 static void
1360 post_unmap (GdkWindow *window)
1361 {
1362   GdkWindow *start_window = NULL;
1363   
1364   if (window->input_only)
1365     return;
1366
1367   if (window->window_type == GDK_WINDOW_CHILD)
1368     start_window = _gdk_window_get_impl_window ((GdkWindow *)window->parent);
1369   else if (window->window_type == GDK_WINDOW_TEMP)
1370     start_window = get_root (window);
1371
1372   if (start_window)
1373     {
1374       _gdk_x11_window_tmp_reset_bg (start_window, TRUE);
1375
1376       if (window->window_type == GDK_WINDOW_CHILD && window->parent)
1377         {
1378           GdkRectangle invalid_rect;
1379       
1380           gdk_window_get_position (window, &invalid_rect.x, &invalid_rect.y);
1381           invalid_rect.width = gdk_window_get_width (window);
1382           invalid_rect.height = gdk_window_get_height (window);
1383           gdk_window_invalidate_rect ((GdkWindow *)window->parent,
1384                                       &invalid_rect, TRUE);
1385         }
1386     }
1387 }
1388
1389 static void
1390 gdk_window_x11_hide (GdkWindow *window)
1391 {
1392   /* We'll get the unmap notify eventually, and handle it then,
1393    * but checking here makes things more consistent if we are
1394    * just doing stuff ourself.
1395    */
1396   _gdk_xgrab_check_unmap (window,
1397                           NextRequest (GDK_WINDOW_XDISPLAY (window)));
1398
1399   /* You can't simply unmap toplevel windows. */
1400   switch (window->window_type)
1401     {
1402     case GDK_WINDOW_TOPLEVEL:
1403     case GDK_WINDOW_TEMP: /* ? */
1404       gdk_window_withdraw (window);
1405       return;
1406       
1407     case GDK_WINDOW_FOREIGN:
1408     case GDK_WINDOW_ROOT:
1409     case GDK_WINDOW_CHILD:
1410       break;
1411     }
1412   
1413   _gdk_window_clear_update_area (window);
1414   
1415   pre_unmap (window);
1416   XUnmapWindow (GDK_WINDOW_XDISPLAY (window),
1417                 GDK_WINDOW_XID (window));
1418   post_unmap (window);
1419 }
1420
1421 static void
1422 gdk_window_x11_withdraw (GdkWindow *window)
1423 {
1424   if (!window->destroyed)
1425     {
1426       if (GDK_WINDOW_IS_MAPPED (window))
1427         gdk_synthesize_window_state (window,
1428                                      0,
1429                                      GDK_WINDOW_STATE_WITHDRAWN);
1430
1431       g_assert (!GDK_WINDOW_IS_MAPPED (window));
1432
1433       pre_unmap (window);
1434       
1435       XWithdrawWindow (GDK_WINDOW_XDISPLAY (window),
1436                        GDK_WINDOW_XID (window), 0);
1437
1438       post_unmap (window);
1439     }
1440 }
1441
1442 static inline void
1443 window_x11_move (GdkWindow *window,
1444                  gint       x,
1445                  gint       y)
1446 {
1447   GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (window->impl);
1448
1449   if (GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD)
1450     {
1451       _gdk_window_move_resize_child (window,
1452                                      x, y,
1453                                      window->width, window->height);
1454     }
1455   else
1456     {
1457       XMoveWindow (GDK_WINDOW_XDISPLAY (window),
1458                    GDK_WINDOW_XID (window),
1459                    x, y);
1460
1461       if (impl->override_redirect)
1462         {
1463           window->x = x;
1464           window->y = y;
1465         }
1466     }
1467 }
1468
1469 static inline void
1470 window_x11_resize (GdkWindow *window,
1471                    gint       width,
1472                    gint       height)
1473 {
1474   if (width < 1)
1475     width = 1;
1476
1477   if (height < 1)
1478     height = 1;
1479
1480   if (GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD)
1481     {
1482       _gdk_window_move_resize_child (window,
1483                                      window->x, window->y,
1484                                      width, height);
1485     }
1486   else
1487     {
1488       GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (window->impl);
1489
1490       XResizeWindow (GDK_WINDOW_XDISPLAY (window),
1491                      GDK_WINDOW_XID (window),
1492                      width, height);
1493
1494       if (impl->override_redirect)
1495         {
1496           window->width = width;
1497           window->height = height;
1498           _gdk_x11_window_update_size (GDK_WINDOW_IMPL_X11 (window->impl));
1499         }
1500       else
1501         {
1502           if (width != window->width || height != window->height)
1503             window->resize_count += 1;
1504         }
1505     }
1506
1507   _gdk_x11_window_update_size (GDK_WINDOW_IMPL_X11 (window->impl));
1508 }
1509
1510 static inline void
1511 window_x11_move_resize (GdkWindow *window,
1512                         gint       x,
1513                         gint       y,
1514                         gint       width,
1515                         gint       height)
1516 {
1517   if (width < 1)
1518     width = 1;
1519
1520   if (height < 1)
1521     height = 1;
1522
1523   if (GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD)
1524     {
1525       _gdk_window_move_resize_child (window, x, y, width, height);
1526       _gdk_x11_window_update_size (GDK_WINDOW_IMPL_X11 (window->impl));
1527     }
1528   else
1529     {
1530       GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (window->impl);
1531
1532       XMoveResizeWindow (GDK_WINDOW_XDISPLAY (window),
1533                          GDK_WINDOW_XID (window),
1534                          x, y, width, height);
1535
1536       if (impl->override_redirect)
1537         {
1538           window->x = x;
1539           window->y = y;
1540
1541           window->width = width;
1542           window->height = height;
1543
1544           _gdk_x11_window_update_size (GDK_WINDOW_IMPL_X11 (window->impl));
1545         }
1546       else
1547         {
1548           if (width != window->width || height != window->height)
1549             window->resize_count += 1;
1550         }
1551     }
1552 }
1553
1554 static void
1555 gdk_window_x11_move_resize (GdkWindow *window,
1556                             gboolean   with_move,
1557                             gint       x,
1558                             gint       y,
1559                             gint       width,
1560                             gint       height)
1561 {
1562   if (with_move && (width < 0 && height < 0))
1563     window_x11_move (window, x, y);
1564   else
1565     {
1566       if (with_move)
1567         window_x11_move_resize (window, x, y, width, height);
1568       else
1569         window_x11_resize (window, width, height);
1570     }
1571 }
1572
1573 static gboolean
1574 gdk_window_x11_reparent (GdkWindow *window,
1575                          GdkWindow *new_parent,
1576                          gint       x,
1577                          gint       y)
1578 {
1579   GdkWindowImplX11 *impl;
1580
1581   impl = GDK_WINDOW_IMPL_X11 (window->impl);
1582
1583   _gdk_x11_window_tmp_unset_bg (window, TRUE);
1584   _gdk_x11_window_tmp_unset_parent_bg (window);
1585   XReparentWindow (GDK_WINDOW_XDISPLAY (window),
1586                    GDK_WINDOW_XID (window),
1587                    GDK_WINDOW_XID (new_parent),
1588                    new_parent->abs_x + x, new_parent->abs_y + y);
1589   _gdk_x11_window_tmp_reset_parent_bg (window);
1590   _gdk_x11_window_tmp_reset_bg (window, TRUE);
1591
1592   if (GDK_WINDOW_TYPE (new_parent) == GDK_WINDOW_FOREIGN)
1593     new_parent = gdk_screen_get_root_window (GDK_WINDOW_SCREEN (window));
1594
1595   window->parent = new_parent;
1596
1597   /* Switch the window type as appropriate */
1598
1599   switch (GDK_WINDOW_TYPE (new_parent))
1600     {
1601     case GDK_WINDOW_ROOT:
1602     case GDK_WINDOW_FOREIGN:
1603       /* Reparenting to toplevel */
1604       
1605       if (!WINDOW_IS_TOPLEVEL (window) &&
1606           GDK_WINDOW_TYPE (new_parent) == GDK_WINDOW_FOREIGN)
1607         {
1608           /* This is also done in common code at a later stage, but we
1609              need it in setup_toplevel, so do it here too */
1610           if (window->toplevel_window_type != -1)
1611             GDK_WINDOW_TYPE (window) = window->toplevel_window_type;
1612           else if (GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD)
1613             GDK_WINDOW_TYPE (window) = GDK_WINDOW_TOPLEVEL;
1614           
1615           /* Wasn't a toplevel, set up */
1616           setup_toplevel_window (window, new_parent);
1617         }
1618
1619       break;
1620       
1621     case GDK_WINDOW_TOPLEVEL:
1622     case GDK_WINDOW_CHILD:
1623     case GDK_WINDOW_TEMP:
1624       if (WINDOW_IS_TOPLEVEL (window) &&
1625           impl->toplevel)
1626         {
1627           if (impl->toplevel->focus_window)
1628             {
1629               XDestroyWindow (GDK_WINDOW_XDISPLAY (window), impl->toplevel->focus_window);
1630               _gdk_xid_table_remove (GDK_WINDOW_DISPLAY (window), impl->toplevel->focus_window);
1631             }
1632           
1633           gdk_toplevel_x11_free_contents (GDK_WINDOW_DISPLAY (window), 
1634                                           impl->toplevel);
1635           g_free (impl->toplevel);
1636           impl->toplevel = NULL;
1637         }
1638     }
1639
1640   return FALSE;
1641 }
1642
1643 static void
1644 gdk_window_x11_raise (GdkWindow *window)
1645 {
1646   XRaiseWindow (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window));
1647 }
1648
1649 static void
1650 gdk_window_x11_restack_under (GdkWindow *window,
1651                               GList *native_siblings /* in requested order, first is bottom-most */)
1652 {
1653   Window *windows;
1654   int n_windows, i;
1655   GList *l;
1656
1657   n_windows = g_list_length (native_siblings) + 1;
1658   windows = g_new (Window, n_windows);
1659
1660   windows[0] = GDK_WINDOW_XID (window);
1661   /* Reverse order, as input order is bottom-most first */
1662   i = n_windows - 1;
1663   for (l = native_siblings; l != NULL; l = l->next)
1664     windows[i--] = GDK_WINDOW_XID (l->data);
1665  
1666   XRestackWindows (GDK_WINDOW_XDISPLAY (window), windows, n_windows);
1667   
1668   g_free (windows);
1669 }
1670
1671 static void
1672 gdk_window_x11_restack_toplevel (GdkWindow *window,
1673                                  GdkWindow *sibling,
1674                                  gboolean   above)
1675 {
1676   XWindowChanges changes;
1677
1678   changes.sibling = GDK_WINDOW_XID (sibling);
1679   changes.stack_mode = above ? Above : Below;
1680   XReconfigureWMWindow (GDK_WINDOW_XDISPLAY (window),
1681                         GDK_WINDOW_XID (window),
1682                         gdk_screen_get_number (GDK_WINDOW_SCREEN (window)),
1683                         CWStackMode | CWSibling, &changes);
1684 }
1685
1686 static void
1687 gdk_window_x11_lower (GdkWindow *window)
1688 {
1689   XLowerWindow (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window));
1690 }
1691
1692 /**
1693  * gdk_x11_window_move_to_current_desktop:
1694  * @window: a #GdkWindow
1695  * 
1696  * Moves the window to the correct workspace when running under a 
1697  * window manager that supports multiple workspaces, as described
1698  * in the <ulink url="http://www.freedesktop.org/Standards/wm-spec">Extended 
1699  * Window Manager Hints</ulink>.  Will not do anything if the
1700  * window is already on all workspaces.
1701  * 
1702  * Since: 2.8
1703  */
1704 void
1705 gdk_x11_window_move_to_current_desktop (GdkWindow *window)
1706 {
1707   GdkToplevelX11 *toplevel;
1708
1709   g_return_if_fail (GDK_IS_WINDOW (window));
1710   g_return_if_fail (GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD);
1711
1712   toplevel = _gdk_x11_window_get_toplevel (window);
1713
1714   if (toplevel->on_all_desktops)
1715     return;
1716   
1717   move_to_current_desktop (window);
1718 }
1719
1720 static void
1721 move_to_current_desktop (GdkWindow *window)
1722 {
1723   if (gdk_x11_screen_supports_net_wm_hint (GDK_WINDOW_SCREEN (window),
1724                                            gdk_atom_intern_static_string ("_NET_WM_DESKTOP")))
1725     {
1726       Atom type;
1727       gint format;
1728       gulong nitems;
1729       gulong bytes_after;
1730       guchar *data;
1731       gulong *current_desktop;
1732       GdkDisplay *display;
1733       
1734       display = gdk_window_get_display (window);
1735
1736       /* Get current desktop, then set it; this is a race, but not
1737        * one that matters much in practice.
1738        */
1739       XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), 
1740                           GDK_WINDOW_XROOTWIN (window),
1741                           gdk_x11_get_xatom_by_name_for_display (display, "_NET_CURRENT_DESKTOP"),
1742                           0, G_MAXLONG,
1743                           False, XA_CARDINAL, &type, &format, &nitems,
1744                           &bytes_after, &data);
1745
1746       if (type == XA_CARDINAL)
1747         {
1748           XClientMessageEvent xclient;
1749           current_desktop = (gulong *)data;
1750           
1751           memset (&xclient, 0, sizeof (xclient));
1752           xclient.type = ClientMessage;
1753           xclient.serial = 0;
1754           xclient.send_event = True;
1755           xclient.window = GDK_WINDOW_XID (window);
1756           xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP");
1757           xclient.format = 32;
1758
1759           xclient.data.l[0] = *current_desktop;
1760           xclient.data.l[1] = 0;
1761           xclient.data.l[2] = 0;
1762           xclient.data.l[3] = 0;
1763           xclient.data.l[4] = 0;
1764       
1765           XSendEvent (GDK_DISPLAY_XDISPLAY (display), 
1766                       GDK_WINDOW_XROOTWIN (window), 
1767                       False,
1768                       SubstructureRedirectMask | SubstructureNotifyMask,
1769                       (XEvent *)&xclient);
1770
1771           XFree (current_desktop);
1772         }
1773     }
1774 }
1775
1776 /**
1777  * gdk_window_focus:
1778  * @window: a #GdkWindow
1779  * @timestamp: timestamp of the event triggering the window focus
1780  *
1781  * Sets keyboard focus to @window. In most cases, gtk_window_present() 
1782  * should be used on a #GtkWindow, rather than calling this function.
1783  * 
1784  **/
1785 void
1786 gdk_window_focus (GdkWindow *window,
1787                   guint32    timestamp)
1788 {
1789   GdkDisplay *display;
1790
1791   g_return_if_fail (GDK_IS_WINDOW (window));
1792
1793   if (GDK_WINDOW_DESTROYED (window) ||
1794       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
1795     return;
1796
1797   display = GDK_WINDOW_DISPLAY (window);
1798
1799   if (gdk_x11_screen_supports_net_wm_hint (GDK_WINDOW_SCREEN (window),
1800                                            gdk_atom_intern_static_string ("_NET_ACTIVE_WINDOW")))
1801     {
1802       XClientMessageEvent xclient;
1803
1804       memset (&xclient, 0, sizeof (xclient));
1805       xclient.type = ClientMessage;
1806       xclient.window = GDK_WINDOW_XID (window);
1807       xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
1808                                                                         "_NET_ACTIVE_WINDOW");
1809       xclient.format = 32;
1810       xclient.data.l[0] = 1; /* requestor type; we're an app */
1811       xclient.data.l[1] = timestamp;
1812       xclient.data.l[2] = None; /* currently active window */
1813       xclient.data.l[3] = 0;
1814       xclient.data.l[4] = 0;
1815       
1816       XSendEvent (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XROOTWIN (window), False,
1817                   SubstructureRedirectMask | SubstructureNotifyMask,
1818                   (XEvent *)&xclient);
1819     }
1820   else
1821     {
1822       XRaiseWindow (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window));
1823
1824       /* There is no way of knowing reliably whether we are viewable;
1825        * _gdk_x11_set_input_focus_safe() traps errors asynchronously.
1826        */
1827       _gdk_x11_set_input_focus_safe (display, GDK_WINDOW_XID (window),
1828                                      RevertToParent,
1829                                      timestamp);
1830     }
1831 }
1832
1833 /**
1834  * gdk_window_set_type_hint:
1835  * @window: A toplevel #GdkWindow
1836  * @hint: A hint of the function this window will have
1837  *
1838  * The application can use this call to provide a hint to the window
1839  * manager about the functionality of a window. The window manager
1840  * can use this information when determining the decoration and behaviour
1841  * of the window.
1842  *
1843  * The hint must be set before the window is mapped.
1844  **/
1845 void
1846 gdk_window_set_type_hint (GdkWindow        *window,
1847                           GdkWindowTypeHint hint)
1848 {
1849   GdkDisplay *display;
1850   Atom atom;
1851   
1852   if (GDK_WINDOW_DESTROYED (window) ||
1853       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
1854     return;
1855
1856   display = gdk_window_get_display (window);
1857
1858   switch (hint)
1859     {
1860     case GDK_WINDOW_TYPE_HINT_DIALOG:
1861       atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_DIALOG");
1862       break;
1863     case GDK_WINDOW_TYPE_HINT_MENU:
1864       atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_MENU");
1865       break;
1866     case GDK_WINDOW_TYPE_HINT_TOOLBAR:
1867       atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_TOOLBAR");
1868       break;
1869     case GDK_WINDOW_TYPE_HINT_UTILITY:
1870       atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_UTILITY");
1871       break;
1872     case GDK_WINDOW_TYPE_HINT_SPLASHSCREEN:
1873       atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_SPLASH");
1874       break;
1875     case GDK_WINDOW_TYPE_HINT_DOCK:
1876       atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_DOCK");
1877       break;
1878     case GDK_WINDOW_TYPE_HINT_DESKTOP:
1879       atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_DESKTOP");
1880       break;
1881     case GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU:
1882       atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU");
1883       break;
1884     case GDK_WINDOW_TYPE_HINT_POPUP_MENU:
1885       atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_POPUP_MENU");
1886       break;
1887     case GDK_WINDOW_TYPE_HINT_TOOLTIP:
1888       atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_TOOLTIP");
1889       break;
1890     case GDK_WINDOW_TYPE_HINT_NOTIFICATION:
1891       atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_NOTIFICATION");
1892       break;
1893     case GDK_WINDOW_TYPE_HINT_COMBO:
1894       atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_COMBO");
1895       break;
1896     case GDK_WINDOW_TYPE_HINT_DND:
1897       atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_DND");
1898       break;
1899     default:
1900       g_warning ("Unknown hint %d passed to gdk_window_set_type_hint", hint);
1901       /* Fall thru */
1902     case GDK_WINDOW_TYPE_HINT_NORMAL:
1903       atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_NORMAL");
1904       break;
1905     }
1906
1907   XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
1908                    gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE"),
1909                    XA_ATOM, 32, PropModeReplace,
1910                    (guchar *)&atom, 1);
1911 }
1912
1913 /**
1914  * gdk_window_get_type_hint:
1915  * @window: A toplevel #GdkWindow
1916  *
1917  * This function returns the type hint set for a window.
1918  *
1919  * Return value: The type hint set for @window
1920  *
1921  * Since: 2.10
1922  **/
1923 GdkWindowTypeHint
1924 gdk_window_get_type_hint (GdkWindow *window)
1925 {
1926   GdkDisplay *display;
1927   GdkWindowTypeHint type;
1928   Atom type_return;
1929   gint format_return;
1930   gulong nitems_return;
1931   gulong bytes_after_return;
1932   guchar *data = NULL;
1933
1934   g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_WINDOW_TYPE_HINT_NORMAL);
1935
1936   if (GDK_WINDOW_DESTROYED (window) ||
1937       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
1938     return GDK_WINDOW_TYPE_HINT_NORMAL;
1939
1940   type = GDK_WINDOW_TYPE_HINT_NORMAL;
1941
1942   display = gdk_window_get_display (window);
1943
1944   if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
1945                           gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE"),
1946                           0, G_MAXLONG, False, XA_ATOM, &type_return,
1947                           &format_return, &nitems_return, &bytes_after_return,
1948                           &data) == Success)
1949     {
1950       if ((type_return == XA_ATOM) && (format_return == 32) &&
1951           (data) && (nitems_return == 1))
1952         {
1953           Atom atom = *(Atom*)data;
1954
1955           if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_DIALOG"))
1956             type = GDK_WINDOW_TYPE_HINT_DIALOG;
1957           else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_MENU"))
1958             type = GDK_WINDOW_TYPE_HINT_MENU;
1959           else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_TOOLBAR"))
1960             type = GDK_WINDOW_TYPE_HINT_TOOLBAR;
1961           else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_UTILITY"))
1962             type = GDK_WINDOW_TYPE_HINT_UTILITY;
1963           else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_SPLASH"))
1964             type = GDK_WINDOW_TYPE_HINT_SPLASHSCREEN;
1965           else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_DOCK"))
1966             type = GDK_WINDOW_TYPE_HINT_DOCK;
1967           else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_DESKTOP"))
1968             type = GDK_WINDOW_TYPE_HINT_DESKTOP;
1969           else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU"))
1970             type = GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU;
1971           else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_POPUP_MENU"))
1972             type = GDK_WINDOW_TYPE_HINT_POPUP_MENU;
1973           else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_TOOLTIP"))
1974             type = GDK_WINDOW_TYPE_HINT_TOOLTIP;
1975           else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_NOTIFICATION"))
1976             type = GDK_WINDOW_TYPE_HINT_NOTIFICATION;
1977           else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_COMBO"))
1978             type = GDK_WINDOW_TYPE_HINT_COMBO;
1979           else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE_DND"))
1980             type = GDK_WINDOW_TYPE_HINT_DND;
1981         }
1982
1983       if (type_return != None && data != NULL)
1984         XFree (data);
1985     }
1986
1987   return type;
1988 }
1989
1990 static void
1991 gdk_wmspec_change_state (gboolean   add,
1992                          GdkWindow *window,
1993                          GdkAtom    state1,
1994                          GdkAtom    state2)
1995 {
1996   GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
1997   XClientMessageEvent xclient;
1998   
1999 #define _NET_WM_STATE_REMOVE        0    /* remove/unset property */
2000 #define _NET_WM_STATE_ADD           1    /* add/set property */
2001 #define _NET_WM_STATE_TOGGLE        2    /* toggle property  */  
2002   
2003   memset (&xclient, 0, sizeof (xclient));
2004   xclient.type = ClientMessage;
2005   xclient.window = GDK_WINDOW_XID (window);
2006   xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE");
2007   xclient.format = 32;
2008   xclient.data.l[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
2009   xclient.data.l[1] = gdk_x11_atom_to_xatom_for_display (display, state1);
2010   xclient.data.l[2] = gdk_x11_atom_to_xatom_for_display (display, state2);
2011   xclient.data.l[3] = 0;
2012   xclient.data.l[4] = 0;
2013   
2014   XSendEvent (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XROOTWIN (window), False,
2015               SubstructureRedirectMask | SubstructureNotifyMask,
2016               (XEvent *)&xclient);
2017 }
2018
2019 /**
2020  * gdk_window_set_modal_hint:
2021  * @window: A toplevel #GdkWindow
2022  * @modal: %TRUE if the window is modal, %FALSE otherwise.
2023  *
2024  * The application can use this hint to tell the window manager
2025  * that a certain window has modal behaviour. The window manager
2026  * can use this information to handle modal windows in a special
2027  * way.
2028  *
2029  * You should only use this on windows for which you have
2030  * previously called gdk_window_set_transient_for()
2031  **/
2032 void
2033 gdk_window_set_modal_hint (GdkWindow *window,
2034                            gboolean   modal)
2035 {
2036   if (GDK_WINDOW_DESTROYED (window) ||
2037       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
2038     return;
2039
2040   window->modal_hint = modal;
2041
2042   if (GDK_WINDOW_IS_MAPPED (window))
2043     gdk_wmspec_change_state (modal, window,
2044                              gdk_atom_intern_static_string ("_NET_WM_STATE_MODAL"), 
2045                              GDK_NONE);
2046 }
2047
2048 /**
2049  * gdk_window_set_skip_taskbar_hint:
2050  * @window: a toplevel #GdkWindow
2051  * @skips_taskbar: %TRUE to skip the taskbar
2052  * 
2053  * Toggles whether a window should appear in a task list or window
2054  * list. If a window's semantic type as specified with
2055  * gdk_window_set_type_hint() already fully describes the window, this
2056  * function should <emphasis>not</emphasis> be called in addition, 
2057  * instead you should allow the window to be treated according to 
2058  * standard policy for its semantic type.
2059  *
2060  * Since: 2.2
2061  **/
2062 void
2063 gdk_window_set_skip_taskbar_hint (GdkWindow *window,
2064                                   gboolean   skips_taskbar)
2065 {
2066   GdkToplevelX11 *toplevel;
2067   
2068   g_return_if_fail (GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD);
2069   
2070   if (GDK_WINDOW_DESTROYED (window) ||
2071       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
2072     return;
2073
2074   toplevel = _gdk_x11_window_get_toplevel (window);
2075   toplevel->skip_taskbar_hint = skips_taskbar;
2076
2077   if (GDK_WINDOW_IS_MAPPED (window))
2078     gdk_wmspec_change_state (skips_taskbar, window,
2079                              gdk_atom_intern_static_string ("_NET_WM_STATE_SKIP_TASKBAR"),
2080                              GDK_NONE);
2081 }
2082
2083 /**
2084  * gdk_window_set_skip_pager_hint:
2085  * @window: a toplevel #GdkWindow
2086  * @skips_pager: %TRUE to skip the pager
2087  * 
2088  * Toggles whether a window should appear in a pager (workspace
2089  * switcher, or other desktop utility program that displays a small
2090  * thumbnail representation of the windows on the desktop). If a
2091  * window's semantic type as specified with gdk_window_set_type_hint()
2092  * already fully describes the window, this function should 
2093  * <emphasis>not</emphasis> be called in addition, instead you should 
2094  * allow the window to be treated according to standard policy for 
2095  * its semantic type.
2096  *
2097  * Since: 2.2
2098  **/
2099 void
2100 gdk_window_set_skip_pager_hint (GdkWindow *window,
2101                                 gboolean   skips_pager)
2102 {
2103   GdkToplevelX11 *toplevel;
2104     
2105   g_return_if_fail (GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD);
2106   
2107   if (GDK_WINDOW_DESTROYED (window) ||
2108       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
2109     return;
2110
2111   toplevel = _gdk_x11_window_get_toplevel (window);
2112   toplevel->skip_pager_hint = skips_pager;
2113   
2114   if (GDK_WINDOW_IS_MAPPED (window))
2115     gdk_wmspec_change_state (skips_pager, window,
2116                              gdk_atom_intern_static_string ("_NET_WM_STATE_SKIP_PAGER"), 
2117                              GDK_NONE);
2118 }
2119
2120 /**
2121  * gdk_window_set_urgency_hint:
2122  * @window: a toplevel #GdkWindow
2123  * @urgent: %TRUE if the window is urgent
2124  * 
2125  * Toggles whether a window needs the user's
2126  * urgent attention.
2127  *
2128  * Since: 2.8
2129  **/
2130 void
2131 gdk_window_set_urgency_hint (GdkWindow *window,
2132                              gboolean   urgent)
2133 {
2134   GdkToplevelX11 *toplevel;
2135     
2136   g_return_if_fail (GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD);
2137   
2138   if (GDK_WINDOW_DESTROYED (window) ||
2139       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
2140     return;
2141
2142   toplevel = _gdk_x11_window_get_toplevel (window);
2143   toplevel->urgency_hint = urgent;
2144   
2145   update_wm_hints (window, FALSE);
2146 }
2147
2148 /**
2149  * gdk_window_set_geometry_hints:
2150  * @window: a toplevel #GdkWindow
2151  * @geometry: geometry hints
2152  * @geom_mask: bitmask indicating fields of @geometry to pay attention to
2153  *
2154  * Sets the geometry hints for @window. Hints flagged in @geom_mask
2155  * are set, hints not flagged in @geom_mask are unset.
2156  * To unset all hints, use a @geom_mask of 0 and a @geometry of %NULL.
2157  *
2158  * This function provides hints to the windowing system about
2159  * acceptable sizes for a toplevel window. The purpose of 
2160  * this is to constrain user resizing, but the windowing system
2161  * will typically  (but is not required to) also constrain the
2162  * current size of the window to the provided values and
2163  * constrain programatic resizing via gdk_window_resize() or
2164  * gdk_window_move_resize().
2165  * 
2166  * Note that on X11, this effect has no effect on windows
2167  * of type %GDK_WINDOW_TEMP or windows where override redirect
2168  * has been turned on via gdk_window_set_override_redirect()
2169  * since these windows are not resizable by the user.
2170  * 
2171  * Since you can't count on the windowing system doing the
2172  * constraints for programmatic resizes, you should generally
2173  * call gdk_window_constrain_size() yourself to determine
2174  * appropriate sizes.
2175  *
2176  **/
2177 void 
2178 gdk_window_set_geometry_hints (GdkWindow         *window,
2179                                const GdkGeometry *geometry,
2180                                GdkWindowHints     geom_mask)
2181 {
2182   XSizeHints size_hints;
2183   
2184   if (GDK_WINDOW_DESTROYED (window) ||
2185       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
2186     return;
2187   
2188   size_hints.flags = 0;
2189   
2190   if (geom_mask & GDK_HINT_POS)
2191     {
2192       size_hints.flags |= PPosition;
2193       /* We need to initialize the following obsolete fields because KWM 
2194        * apparently uses these fields if they are non-zero.
2195        * #@#!#!$!.
2196        */
2197       size_hints.x = 0;
2198       size_hints.y = 0;
2199     }
2200
2201   if (geom_mask & GDK_HINT_USER_POS)
2202     {
2203       size_hints.flags |= USPosition;
2204     }
2205
2206   if (geom_mask & GDK_HINT_USER_SIZE)
2207     {
2208       size_hints.flags |= USSize;
2209     }
2210   
2211   if (geom_mask & GDK_HINT_MIN_SIZE)
2212     {
2213       size_hints.flags |= PMinSize;
2214       size_hints.min_width = geometry->min_width;
2215       size_hints.min_height = geometry->min_height;
2216     }
2217   
2218   if (geom_mask & GDK_HINT_MAX_SIZE)
2219     {
2220       size_hints.flags |= PMaxSize;
2221       size_hints.max_width = MAX (geometry->max_width, 1);
2222       size_hints.max_height = MAX (geometry->max_height, 1);
2223     }
2224   
2225   if (geom_mask & GDK_HINT_BASE_SIZE)
2226     {
2227       size_hints.flags |= PBaseSize;
2228       size_hints.base_width = geometry->base_width;
2229       size_hints.base_height = geometry->base_height;
2230     }
2231   
2232   if (geom_mask & GDK_HINT_RESIZE_INC)
2233     {
2234       size_hints.flags |= PResizeInc;
2235       size_hints.width_inc = geometry->width_inc;
2236       size_hints.height_inc = geometry->height_inc;
2237     }
2238   
2239   if (geom_mask & GDK_HINT_ASPECT)
2240     {
2241       size_hints.flags |= PAspect;
2242       if (geometry->min_aspect <= 1)
2243         {
2244           size_hints.min_aspect.x = 65536 * geometry->min_aspect;
2245           size_hints.min_aspect.y = 65536;
2246         }
2247       else
2248         {
2249           size_hints.min_aspect.x = 65536;
2250           size_hints.min_aspect.y = 65536 / geometry->min_aspect;;
2251         }
2252       if (geometry->max_aspect <= 1)
2253         {
2254           size_hints.max_aspect.x = 65536 * geometry->max_aspect;
2255           size_hints.max_aspect.y = 65536;
2256         }
2257       else
2258         {
2259           size_hints.max_aspect.x = 65536;
2260           size_hints.max_aspect.y = 65536 / geometry->max_aspect;;
2261         }
2262     }
2263
2264   if (geom_mask & GDK_HINT_WIN_GRAVITY)
2265     {
2266       size_hints.flags |= PWinGravity;
2267       size_hints.win_gravity = geometry->win_gravity;
2268     }
2269   
2270   /* FIXME: Would it be better to delete this property if
2271    *        geom_mask == 0? It would save space on the server
2272    */
2273   XSetWMNormalHints (GDK_WINDOW_XDISPLAY (window),
2274                      GDK_WINDOW_XID (window),
2275                      &size_hints);
2276 }
2277
2278 static void
2279 gdk_window_get_geometry_hints (GdkWindow      *window,
2280                                GdkGeometry    *geometry,
2281                                GdkWindowHints *geom_mask)
2282 {
2283   XSizeHints *size_hints;  
2284   glong junk_supplied_mask = 0;
2285
2286   g_return_if_fail (GDK_IS_WINDOW (window));
2287   g_return_if_fail (geometry != NULL);
2288   g_return_if_fail (geom_mask != NULL);
2289
2290   *geom_mask = 0;
2291   
2292   if (GDK_WINDOW_DESTROYED (window) ||
2293       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
2294     return;
2295
2296   size_hints = XAllocSizeHints ();
2297   if (!size_hints)
2298     return;
2299   
2300   if (!XGetWMNormalHints (GDK_WINDOW_XDISPLAY (window),
2301                           GDK_WINDOW_XID (window),
2302                           size_hints,
2303                           &junk_supplied_mask))
2304     size_hints->flags = 0;
2305
2306   if (size_hints->flags & PMinSize)
2307     {
2308       *geom_mask |= GDK_HINT_MIN_SIZE;
2309       geometry->min_width = size_hints->min_width;
2310       geometry->min_height = size_hints->min_height;
2311     }
2312
2313   if (size_hints->flags & PMaxSize)
2314     {
2315       *geom_mask |= GDK_HINT_MAX_SIZE;
2316       geometry->max_width = MAX (size_hints->max_width, 1);
2317       geometry->max_height = MAX (size_hints->max_height, 1);
2318     }
2319
2320   if (size_hints->flags & PResizeInc)
2321     {
2322       *geom_mask |= GDK_HINT_RESIZE_INC;
2323       geometry->width_inc = size_hints->width_inc;
2324       geometry->height_inc = size_hints->height_inc;
2325     }
2326
2327   if (size_hints->flags & PAspect)
2328     {
2329       *geom_mask |= GDK_HINT_ASPECT;
2330
2331       geometry->min_aspect = (gdouble) size_hints->min_aspect.x / (gdouble) size_hints->min_aspect.y;
2332       geometry->max_aspect = (gdouble) size_hints->max_aspect.x / (gdouble) size_hints->max_aspect.y;
2333     }
2334
2335   if (size_hints->flags & PWinGravity)
2336     {
2337       *geom_mask |= GDK_HINT_WIN_GRAVITY;
2338       geometry->win_gravity = size_hints->win_gravity;
2339     }
2340
2341   XFree (size_hints);
2342 }
2343
2344 static gboolean
2345 utf8_is_latin1 (const gchar *str)
2346 {
2347   const char *p = str;
2348
2349   while (*p)
2350     {
2351       gunichar ch = g_utf8_get_char (p);
2352
2353       if (ch > 0xff)
2354         return FALSE;
2355       
2356       p = g_utf8_next_char (p);
2357     }
2358
2359   return TRUE;
2360 }
2361
2362 /* Set the property to @utf8_str as STRING if the @utf8_str is fully
2363  * convertable to STRING, otherwise, set it as compound text
2364  */
2365 static void
2366 set_text_property (GdkDisplay  *display,
2367                    Window       xwindow,
2368                    Atom         property,
2369                    const gchar *utf8_str)
2370 {
2371   gchar *prop_text = NULL;
2372   Atom prop_type;
2373   gint prop_length;
2374   gint prop_format;
2375   gboolean is_compound_text;
2376   
2377   if (utf8_is_latin1 (utf8_str))
2378     {
2379       prop_type = XA_STRING;
2380       prop_text = gdk_utf8_to_string_target (utf8_str);
2381       prop_length = prop_text ? strlen (prop_text) : 0;
2382       prop_format = 8;
2383       is_compound_text = FALSE;
2384     }
2385   else
2386     {
2387       GdkAtom gdk_type;
2388       
2389       gdk_utf8_to_compound_text_for_display (display,
2390                                              utf8_str, &gdk_type, &prop_format,
2391                                              (guchar **)&prop_text, &prop_length);
2392       prop_type = gdk_x11_atom_to_xatom_for_display (display, gdk_type);
2393       is_compound_text = TRUE;
2394     }
2395
2396   if (prop_text)
2397     {
2398       XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
2399                        xwindow,
2400                        property,
2401                        prop_type, prop_format,
2402                        PropModeReplace, (guchar *)prop_text,
2403                        prop_length);
2404
2405       if (is_compound_text)
2406         gdk_free_compound_text ((guchar *)prop_text);
2407       else
2408         g_free (prop_text);
2409     }
2410 }
2411
2412 /* Set WM_NAME and _NET_WM_NAME
2413  */
2414 static void
2415 set_wm_name (GdkDisplay  *display,
2416              Window       xwindow,
2417              const gchar *name)
2418 {
2419   XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xwindow,
2420                    gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_NAME"),
2421                    gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
2422                    PropModeReplace, (guchar *)name, strlen (name));
2423   
2424   set_text_property (display, xwindow,
2425                      gdk_x11_get_xatom_by_name_for_display (display, "WM_NAME"),
2426                      name);
2427 }
2428
2429 /**
2430  * gdk_window_set_title:
2431  * @window: a toplevel #GdkWindow
2432  * @title: title of @window
2433  *
2434  * Sets the title of a toplevel window, to be displayed in the titlebar.
2435  * If you haven't explicitly set the icon name for the window
2436  * (using gdk_window_set_icon_name()), the icon name will be set to
2437  * @title as well. @title must be in UTF-8 encoding (as with all
2438  * user-readable strings in GDK/GTK+). @title may not be %NULL.
2439  **/
2440 void
2441 gdk_window_set_title (GdkWindow   *window,
2442                       const gchar *title)
2443 {
2444   GdkDisplay *display;
2445   Display *xdisplay;
2446   Window xwindow;
2447   
2448   g_return_if_fail (title != NULL);
2449
2450   if (GDK_WINDOW_DESTROYED (window) ||
2451       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
2452     return;
2453   
2454   display = gdk_window_get_display (window);
2455   xdisplay = GDK_DISPLAY_XDISPLAY (display);
2456   xwindow = GDK_WINDOW_XID (window);
2457
2458   set_wm_name (display, xwindow, title);
2459   
2460   if (!gdk_window_icon_name_set (window))
2461     {
2462       XChangeProperty (xdisplay, xwindow,
2463                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_ICON_NAME"),
2464                        gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
2465                        PropModeReplace, (guchar *)title, strlen (title));
2466       
2467       set_text_property (display, xwindow,
2468                          gdk_x11_get_xatom_by_name_for_display (display, "WM_ICON_NAME"),
2469                          title);
2470     }
2471 }
2472
2473 /**
2474  * gdk_window_set_role:
2475  * @window: a toplevel #GdkWindow
2476  * @role: a string indicating its role
2477  *
2478  * When using GTK+, typically you should use gtk_window_set_role() instead
2479  * of this low-level function.
2480  * 
2481  * The window manager and session manager use a window's role to
2482  * distinguish it from other kinds of window in the same application.
2483  * When an application is restarted after being saved in a previous
2484  * session, all windows with the same title and role are treated as
2485  * interchangeable.  So if you have two windows with the same title
2486  * that should be distinguished for session management purposes, you
2487  * should set the role on those windows. It doesn't matter what string
2488  * you use for the role, as long as you have a different role for each
2489  * non-interchangeable kind of window.
2490  * 
2491  **/
2492 void          
2493 gdk_window_set_role (GdkWindow   *window,
2494                      const gchar *role)
2495 {
2496   GdkDisplay *display;
2497
2498   display = gdk_window_get_display (window);
2499
2500   if (GDK_WINDOW_DESTROYED (window) ||
2501       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
2502     return;
2503
2504   if (role)
2505     XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
2506                      gdk_x11_get_xatom_by_name_for_display (display, "WM_WINDOW_ROLE"),
2507                      XA_STRING, 8, PropModeReplace, (guchar *)role, strlen (role));
2508   else
2509     XDeleteProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
2510                      gdk_x11_get_xatom_by_name_for_display (display, "WM_WINDOW_ROLE"));
2511 }
2512
2513 /**
2514  * gdk_window_set_startup_id:
2515  * @window: a toplevel #GdkWindow
2516  * @startup_id: a string with startup-notification identifier
2517  *
2518  * When using GTK+, typically you should use gtk_window_set_startup_id()
2519  * instead of this low-level function.
2520  *
2521  * Since: 2.12
2522  *
2523  **/
2524 void
2525 gdk_window_set_startup_id (GdkWindow   *window,
2526                            const gchar *startup_id)
2527 {
2528   GdkDisplay *display;
2529
2530   g_return_if_fail (GDK_IS_WINDOW (window));
2531
2532   display = gdk_window_get_display (window);
2533
2534   if (GDK_WINDOW_DESTROYED (window) ||
2535       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
2536     return;
2537
2538   if (startup_id)
2539     XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
2540                      gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"), 
2541                      gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
2542                      PropModeReplace, (unsigned char *)startup_id, strlen (startup_id));
2543   else
2544     XDeleteProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
2545                      gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"));
2546 }
2547
2548 /**
2549  * gdk_window_set_transient_for:
2550  * @window: a toplevel #GdkWindow
2551  * @parent: another toplevel #GdkWindow
2552  *
2553  * Indicates to the window manager that @window is a transient dialog
2554  * associated with the application window @parent. This allows the
2555  * window manager to do things like center @window on @parent and
2556  * keep @window above @parent.
2557  *
2558  * See gtk_window_set_transient_for() if you're using #GtkWindow or
2559  * #GtkDialog.
2560  **/
2561 void
2562 gdk_window_set_transient_for (GdkWindow *window,
2563                               GdkWindow *parent)
2564 {
2565   if (!GDK_WINDOW_DESTROYED (window) && !GDK_WINDOW_DESTROYED (parent) &&
2566       WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
2567     XSetTransientForHint (GDK_WINDOW_XDISPLAY (window), 
2568                           GDK_WINDOW_XID (window),
2569                           GDK_WINDOW_XID (parent));
2570 }
2571
2572 static gboolean
2573 gdk_window_x11_set_back_color (GdkWindow *window,
2574                                double     red,
2575                                double     green,
2576                                double     blue,
2577                                double     alpha)
2578 {
2579   GdkVisual *visual = gdk_window_get_visual (window);
2580
2581   /* I suppose we could handle these, but that'd require fiddling with 
2582    * xrender formats... */
2583   if (alpha != 1.0)
2584     return FALSE;
2585
2586   switch (visual->type)
2587     {
2588     case GDK_VISUAL_DIRECT_COLOR:
2589     case GDK_VISUAL_TRUE_COLOR:
2590         {
2591           /* If bits not used for color are used for something other than padding,
2592            * it's likely alpha, so we set them to 1s.
2593            */
2594           guint padding, pixel;
2595
2596           /* Shifting by >= width-of-type isn't defined in C */
2597           if (visual->depth >= 32)
2598             padding = 0;
2599           else
2600             padding = ((~(guint32)0)) << visual->depth;
2601           
2602           pixel = ~ (visual->red_mask | visual->green_mask | visual->blue_mask | padding);
2603           
2604           pixel += (((int) (red   * ((1 << visual->red_prec  ) - 1))) << visual->red_shift  ) +
2605                    (((int) (green * ((1 << visual->green_prec) - 1))) << visual->green_shift) +
2606                    (((int) (blue  * ((1 << visual->blue_prec ) - 1))) << visual->blue_shift );
2607
2608           XSetWindowBackground (GDK_WINDOW_XDISPLAY (window),
2609                                 GDK_WINDOW_XID (window), pixel);
2610         }
2611       return TRUE;
2612
2613     /* These require fiddling with the colormap, and as they're essentially unused
2614      * we're just gonna skip them for now.
2615      */
2616     case GDK_VISUAL_PSEUDO_COLOR:
2617     case GDK_VISUAL_GRAYSCALE:
2618     case GDK_VISUAL_STATIC_GRAY:
2619     case GDK_VISUAL_STATIC_COLOR:
2620     default:
2621       break;
2622     }
2623
2624   return FALSE;
2625 }
2626
2627 static gboolean
2628 matrix_is_identity (cairo_matrix_t *matrix)
2629 {
2630   return matrix->xx == 1.0 && matrix->yy == 1.0 &&
2631     matrix->yx == 0.0 && matrix->xy == 0.0 &&
2632     matrix->x0 == 0.0 && matrix->y0 == 0.0;
2633 }
2634
2635 static void
2636 gdk_window_x11_set_background (GdkWindow      *window,
2637                                cairo_pattern_t *pattern)
2638 {
2639   double r, g, b, a;
2640   cairo_surface_t *surface;
2641   cairo_matrix_t matrix;
2642
2643   if (GDK_WINDOW_DESTROYED (window))
2644     return;
2645
2646   if (pattern == NULL)
2647     {
2648       GdkWindow *parent;
2649
2650       /* X throws BadMatch if the parent has a different visual when
2651        * using ParentRelative */
2652       parent = gdk_window_get_parent (window);
2653       if (parent && gdk_window_get_visual (parent) == gdk_window_get_visual (window))
2654         XSetWindowBackgroundPixmap (GDK_WINDOW_XDISPLAY (window),
2655                                     GDK_WINDOW_XID (window), ParentRelative);
2656       else
2657         XSetWindowBackgroundPixmap (GDK_WINDOW_XDISPLAY (window),
2658                                     GDK_WINDOW_XID (window), None);
2659       return;
2660     }
2661
2662   switch (cairo_pattern_get_type (pattern))
2663     {
2664     case CAIRO_PATTERN_TYPE_SOLID:
2665       cairo_pattern_get_rgba (pattern, &r, &g, &b, &a);
2666       if (gdk_window_x11_set_back_color (window, r, g, b, a))
2667         return;
2668       break;
2669     case CAIRO_PATTERN_TYPE_SURFACE:
2670       cairo_pattern_get_matrix (pattern, &matrix);
2671       if (cairo_pattern_get_surface (pattern, &surface) == CAIRO_STATUS_SUCCESS &&
2672           matrix_is_identity (&matrix) &&
2673           cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_XLIB &&
2674           cairo_xlib_surface_get_visual (surface) == GDK_VISUAL_XVISUAL (gdk_window_get_visual ((window))) &&
2675           cairo_xlib_surface_get_display (surface) == GDK_WINDOW_XDISPLAY (window))
2676         {
2677           double x, y;
2678
2679           cairo_surface_get_device_offset (surface, &x, &y);
2680           /* XXX: This still bombs for non-pixmaps, but there's no way to
2681            * detect we're not a pixmap in Cairo... */
2682           if (x == 0.0 && y == 0.0)
2683             {
2684               XSetWindowBackgroundPixmap (GDK_WINDOW_XDISPLAY (window),
2685                                           GDK_WINDOW_XID (window),
2686                                           cairo_xlib_surface_get_drawable (surface));
2687               return;
2688             }
2689         }
2690       /* fall through */
2691     case CAIRO_PATTERN_TYPE_LINEAR:
2692     case CAIRO_PATTERN_TYPE_RADIAL:
2693     default:
2694       /* fallback: just use black */
2695       break;
2696     }
2697
2698   XSetWindowBackgroundPixmap (GDK_WINDOW_XDISPLAY (window),
2699                               GDK_WINDOW_XID (window), None);
2700 }
2701
2702 static void
2703 gdk_window_x11_set_device_cursor (GdkWindow *window,
2704                                   GdkDevice *device,
2705                                   GdkCursor *cursor)
2706 {
2707   GdkWindowImplX11 *impl;
2708
2709   g_return_if_fail (GDK_IS_WINDOW (window));
2710   g_return_if_fail (GDK_IS_DEVICE (device));
2711
2712   impl = GDK_WINDOW_IMPL_X11 (window->impl);
2713
2714   if (!cursor)
2715     g_hash_table_remove (impl->device_cursor, device);
2716   else
2717     {
2718       _gdk_x11_cursor_update_theme (cursor);
2719       g_hash_table_replace (impl->device_cursor,
2720                             device, gdk_cursor_ref (cursor));
2721     }
2722
2723   if (!GDK_WINDOW_DESTROYED (window))
2724     GDK_DEVICE_GET_CLASS (device)->set_window_cursor (device, window, cursor);
2725 }
2726
2727 GdkCursor *
2728 _gdk_x11_window_get_cursor (GdkWindow *window)
2729 {
2730   GdkWindowImplX11 *impl;
2731   
2732   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
2733     
2734   impl = GDK_WINDOW_IMPL_X11 (window->impl);
2735
2736   return impl->cursor;
2737 }
2738
2739 static void
2740 gdk_window_x11_get_geometry (GdkWindow *window,
2741                              gint      *x,
2742                              gint      *y,
2743                              gint      *width,
2744                              gint      *height,
2745                              gint      *depth)
2746 {
2747   Window root;
2748   gint tx;
2749   gint ty;
2750   guint twidth;
2751   guint theight;
2752   guint tborder_width;
2753   guint tdepth;
2754   
2755   if (!GDK_WINDOW_DESTROYED (window))
2756     {
2757       XGetGeometry (GDK_WINDOW_XDISPLAY (window),
2758                     GDK_WINDOW_XID (window),
2759                     &root, &tx, &ty, &twidth, &theight, &tborder_width, &tdepth);
2760       
2761       if (x)
2762         *x = tx;
2763       if (y)
2764         *y = ty;
2765       if (width)
2766         *width = twidth;
2767       if (height)
2768         *height = theight;
2769       if (depth)
2770         *depth = tdepth;
2771     }
2772 }
2773
2774 static gint
2775 gdk_window_x11_get_root_coords (GdkWindow *window,
2776                                 gint       x,
2777                                 gint       y,
2778                                 gint      *root_x,
2779                                 gint      *root_y)
2780 {
2781   gint return_val;
2782   Window child;
2783   gint tx;
2784   gint ty;
2785   
2786   return_val = XTranslateCoordinates (GDK_WINDOW_XDISPLAY (window),
2787                                       GDK_WINDOW_XID (window),
2788                                       GDK_WINDOW_XROOTWIN (window),
2789                                       x, y, &tx, &ty,
2790                                       &child);
2791   
2792   if (root_x)
2793     *root_x = tx;
2794   if (root_y)
2795     *root_y = ty;
2796   
2797   return return_val;
2798 }
2799
2800 /**
2801  * gdk_window_get_root_origin:
2802  * @window: a toplevel #GdkWindow
2803  * @x: return location for X position of window frame
2804  * @y: return location for Y position of window frame
2805  *
2806  * Obtains the top-left corner of the window manager frame in root
2807  * window coordinates.
2808  * 
2809  **/
2810 void
2811 gdk_window_get_root_origin (GdkWindow *window,
2812                             gint      *x,
2813                             gint      *y)
2814 {
2815   GdkRectangle rect;
2816
2817   gdk_window_get_frame_extents (window, &rect);
2818
2819   if (x)
2820     *x = rect.x;
2821
2822   if (y)
2823     *y = rect.y;
2824 }
2825
2826 /**
2827  * gdk_window_get_frame_extents:
2828  * @window: a toplevel #GdkWindow
2829  * @rect: rectangle to fill with bounding box of the window frame
2830  *
2831  * Obtains the bounding box of the window, including window manager
2832  * titlebar/borders if any. The frame position is given in root window
2833  * coordinates. To get the position of the window itself (rather than
2834  * the frame) in root window coordinates, use gdk_window_get_origin().
2835  * 
2836  **/
2837 void
2838 gdk_window_get_frame_extents (GdkWindow    *window,
2839                               GdkRectangle *rect)
2840 {
2841   GdkDisplay *display;
2842   GdkWindowImplX11 *impl;
2843   Window xwindow;
2844   Window xparent;
2845   Window root;
2846   Window child;
2847   Window *children;
2848   guchar *data;
2849   Window *vroots;
2850   Atom type_return;
2851   guint nchildren;
2852   guint nvroots;
2853   gulong nitems_return;
2854   gulong bytes_after_return;
2855   gint format_return;
2856   gint i;
2857   guint ww, wh, wb, wd;
2858   gint wx, wy;
2859   gboolean got_frame_extents = FALSE;
2860   
2861   g_return_if_fail (rect != NULL);
2862   
2863   rect->x = 0;
2864   rect->y = 0;
2865   rect->width = 1;
2866   rect->height = 1;
2867   
2868   while (window->parent && (window->parent)->parent)
2869     window = window->parent;
2870
2871   /* Refine our fallback answer a bit using local information */
2872   rect->x = window->x;
2873   rect->y = window->y;
2874   rect->width = window->width;
2875   rect->height = window->height;
2876
2877   impl = GDK_WINDOW_IMPL_X11 (window->impl);
2878   if (GDK_WINDOW_DESTROYED (window) || impl->override_redirect)
2879     return;
2880
2881   nvroots = 0;
2882   vroots = NULL;
2883
2884   gdk_error_trap_push();
2885   
2886   display = gdk_window_get_display (window);
2887   xwindow = GDK_WINDOW_XID (window);
2888
2889   /* first try: use _NET_FRAME_EXTENTS */
2890   if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xwindow,
2891                           gdk_x11_get_xatom_by_name_for_display (display,
2892                                                                  "_NET_FRAME_EXTENTS"),
2893                           0, G_MAXLONG, False, XA_CARDINAL, &type_return,
2894                           &format_return, &nitems_return, &bytes_after_return,
2895                           &data)
2896       == Success)
2897     {
2898       if ((type_return == XA_CARDINAL) && (format_return == 32) &&
2899           (nitems_return == 4) && (data))
2900         {
2901           gulong *ldata = (gulong *) data;
2902           got_frame_extents = TRUE;
2903
2904           /* try to get the real client window geometry */
2905           if (XGetGeometry (GDK_DISPLAY_XDISPLAY (display), xwindow,
2906                             &root, &wx, &wy, &ww, &wh, &wb, &wd) &&
2907               XTranslateCoordinates (GDK_DISPLAY_XDISPLAY (display),
2908                                      xwindow, root, 0, 0, &wx, &wy, &child))
2909             {
2910               rect->x = wx;
2911               rect->y = wy;
2912               rect->width = ww;
2913               rect->height = wh;
2914             }
2915
2916           /* _NET_FRAME_EXTENTS format is left, right, top, bottom */
2917           rect->x -= ldata[0];
2918           rect->y -= ldata[2];
2919           rect->width += ldata[0] + ldata[1];
2920           rect->height += ldata[2] + ldata[3];
2921         }
2922
2923       if (data)
2924         XFree (data);
2925     }
2926
2927   if (got_frame_extents)
2928     goto out;
2929
2930   /* no frame extents property available, which means we either have a WM that
2931      is not EWMH compliant or is broken - try fallback and walk up the window
2932      tree to get our window's parent which hopefully is the window frame */
2933
2934   /* use NETWM_VIRTUAL_ROOTS if available */
2935   root = GDK_WINDOW_XROOTWIN (window);
2936
2937   if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), root,
2938                           gdk_x11_get_xatom_by_name_for_display (display, 
2939                                                                  "_NET_VIRTUAL_ROOTS"),
2940                           0, G_MAXLONG, False, XA_WINDOW, &type_return,
2941                           &format_return, &nitems_return, &bytes_after_return,
2942                           &data)
2943       == Success)
2944     {
2945       if ((type_return == XA_WINDOW) && (format_return == 32) && (data))
2946         {
2947           nvroots = nitems_return;
2948           vroots = (Window *)data;
2949         }
2950     }
2951
2952   xparent = GDK_WINDOW_XID (window);
2953
2954   do
2955     {
2956       xwindow = xparent;
2957
2958       if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), xwindow,
2959                        &root, &xparent,
2960                        &children, &nchildren))
2961         goto out;
2962       
2963       if (children)
2964         XFree (children);
2965
2966       /* check virtual roots */
2967       for (i = 0; i < nvroots; i++)
2968         {
2969           if (xparent == vroots[i])
2970             {
2971               root = xparent;
2972               break;
2973            }
2974         }
2975     }
2976   while (xparent != root);
2977   
2978   if (XGetGeometry (GDK_DISPLAY_XDISPLAY (display), xwindow, 
2979                     &root, &wx, &wy, &ww, &wh, &wb, &wd))
2980     {
2981       rect->x = wx;
2982       rect->y = wy;
2983       rect->width = ww;
2984       rect->height = wh;
2985     }
2986
2987  out:
2988   if (vroots)
2989     XFree (vroots);
2990
2991   gdk_error_trap_pop_ignored ();
2992 }
2993
2994 void
2995 _gdk_windowing_get_device_state (GdkDisplay       *display,
2996                                  GdkDevice        *device,
2997                                  GdkScreen       **screen,
2998                                  gint             *x,
2999                                  gint             *y,
3000                                  GdkModifierType  *mask)
3001 {
3002   GdkScreen *default_screen;
3003
3004   if (display->closed)
3005     return;
3006
3007   default_screen = gdk_display_get_default_screen (display);
3008
3009
3010   if (G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
3011     {
3012       GdkWindow *root;
3013
3014       GDK_DEVICE_GET_CLASS (device)->query_state (device,
3015                                                   gdk_screen_get_root_window (default_screen),
3016                                                   &root, NULL,
3017                                                   x, y,
3018                                                   NULL, NULL,
3019                                                   mask);
3020       *screen = gdk_window_get_screen (root);
3021     }
3022   else
3023     {
3024       XSetWindowAttributes attributes;
3025       Display *xdisplay;
3026       Window xwindow, w, root, child;
3027       int rootx, rooty, winx, winy;
3028       unsigned int xmask;
3029
3030       /* FIXME: untrusted clients not multidevice-safe */
3031
3032       xdisplay = GDK_SCREEN_XDISPLAY (default_screen);
3033       xwindow = GDK_SCREEN_XROOTWIN (default_screen);
3034
3035       w = XCreateWindow (xdisplay, xwindow, 0, 0, 1, 1, 0,
3036                          CopyFromParent, InputOnly, CopyFromParent,
3037                          0, &attributes);
3038       XQueryPointer (xdisplay, w,
3039                      &root, &child, &rootx, &rooty, &winx, &winy, &xmask);
3040       XDestroyWindow (xdisplay, w);
3041
3042       if (root != None)
3043         {
3044           GdkWindow *gdk_root = gdk_window_lookup_for_display (display, root);
3045           *screen = gdk_window_get_screen (gdk_root);
3046         }
3047
3048       *x = rootx;
3049       *y = rooty;
3050       *mask = xmask;
3051     }
3052 }
3053
3054 static gboolean
3055 gdk_window_x11_get_device_state (GdkWindow       *window,
3056                                  GdkDevice       *device,
3057                                  gint            *x,
3058                                  gint            *y,
3059                                  GdkModifierType *mask)
3060 {
3061   GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
3062   gboolean return_val;
3063
3064   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), FALSE);
3065
3066   return_val = TRUE;
3067
3068   if (!GDK_WINDOW_DESTROYED (window))
3069     {
3070       if (G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
3071         {
3072           GdkWindow *child;
3073
3074           GDK_DEVICE_GET_CLASS (device)->query_state (device, window,
3075                                                       NULL, &child,
3076                                                       NULL, NULL,
3077                                                       x, y, mask);
3078           return_val = (child != NULL);
3079         }
3080       else
3081         {
3082           GdkScreen *screen;
3083           int originx, originy;
3084           int rootx, rooty;
3085           int winx = 0;
3086           int winy = 0;
3087           unsigned int xmask = 0;
3088
3089           _gdk_windowing_get_device_state (gdk_window_get_display (window), device,
3090                                            &screen, &rootx, &rooty, &xmask);
3091           gdk_window_get_origin (window, &originx, &originy);
3092           winx = rootx - originx;
3093           winy = rooty - originy;
3094
3095           *x = winx;
3096           *y = winy;
3097           *mask = xmask;
3098         }
3099     }
3100
3101   return return_val;
3102 }
3103
3104 /**
3105  * gdk_display_warp_pointer:
3106  * @display: a #GdkDisplay
3107  * @screen: the screen of @display to warp the pointer to
3108  * @x: the x coordinate of the destination
3109  * @y: the y coordinate of the destination
3110  * 
3111  * Warps the pointer of @display to the point @x,@y on 
3112  * the screen @screen, unless the pointer is confined
3113  * to a window by a grab, in which case it will be moved
3114  * as far as allowed by the grab. Warping the pointer 
3115  * creates events as if the user had moved the mouse 
3116  * instantaneously to the destination.
3117  * 
3118  * Note that the pointer should normally be under the
3119  * control of the user. This function was added to cover
3120  * some rare use cases like keyboard navigation support
3121  * for the color picker in the #GtkColorSelectionDialog.
3122  *
3123  * Since: 2.8
3124  *
3125  * Deprecated: 3.0: Use gdk_display_warp_device() instead.
3126  */ 
3127 void
3128 gdk_display_warp_pointer (GdkDisplay *display,
3129                           GdkScreen  *screen,
3130                           gint        x,
3131                           gint        y)
3132 {
3133   GdkDevice *device;
3134
3135   g_return_if_fail (GDK_IS_DISPLAY (display));
3136   g_return_if_fail (GDK_IS_SCREEN (screen));
3137
3138   device = display->core_pointer;
3139   GDK_DEVICE_GET_CLASS (device)->warp (device, screen, x, y);
3140 }
3141
3142 /**
3143  * gdk_display_warp_device:
3144  * @display: a #GdkDisplay.
3145  * @device: a #GdkDevice.
3146  * @screen: the screen of @display to warp @device to.
3147  * @x: the X coordinate of the destination.
3148  * @y: the Y coordinate of the destination.
3149  *
3150  * Warps @device in @display to the point @x,@y on
3151  * the screen @screen, unless the device is confined
3152  * to a window by a grab, in which case it will be moved
3153  * as far as allowed by the grab. Warping the pointer
3154  * creates events as if the user had moved the mouse
3155  * instantaneously to the destination.
3156  *
3157  * Note that the pointer should normally be under the
3158  * control of the user. This function was added to cover
3159  * some rare use cases like keyboard navigation support
3160  * for the color picker in the #GtkColorSelectionDialog.
3161  *
3162  * Since: 3.0
3163  **/
3164 void
3165 gdk_display_warp_device (GdkDisplay *display,
3166                          GdkDevice  *device,
3167                          GdkScreen  *screen,
3168                          gint        x,
3169                          gint        y)
3170 {
3171   g_return_if_fail (GDK_IS_DISPLAY (display));
3172   g_return_if_fail (GDK_IS_DEVICE (device));
3173   g_return_if_fail (GDK_IS_SCREEN (screen));
3174   g_return_if_fail (display == gdk_device_get_display (device));
3175
3176   GDK_DEVICE_GET_CLASS (device)->warp (device, screen, x, y);
3177 }
3178
3179 GdkWindow*
3180 _gdk_windowing_window_at_device_position (GdkDisplay      *display,
3181                                           GdkDevice       *device,
3182                                           gint            *win_x,
3183                                           gint            *win_y,
3184                                           GdkModifierType *mask,
3185                                           gboolean         get_toplevel)
3186 {
3187   GdkWindow *window;
3188   GdkScreen *screen;
3189
3190   screen = gdk_display_get_default_screen (display);
3191
3192   /* This function really only works if the mouse pointer is held still
3193    * during its operation. If it moves from one leaf window to another
3194    * than we'll end up with inaccurate values for win_x, win_y
3195    * and the result.
3196    */
3197   gdk_x11_display_grab (display);
3198   if (G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
3199     window = GDK_DEVICE_GET_CLASS (device)->window_at_position (device, win_x, win_y, mask, get_toplevel);
3200   else
3201     {
3202       gint i, screens, width, height;
3203       GList *toplevels, *list;
3204       Window pointer_window, root, xwindow, child;
3205       Window xwindow_last = 0;
3206       Display *xdisplay;
3207       int rootx = -1, rooty = -1;
3208       int winx, winy;
3209       unsigned int xmask;
3210
3211       /* FIXME: untrusted clients case not multidevice-safe */
3212
3213       xwindow = GDK_SCREEN_XROOTWIN (screen);
3214       xdisplay = GDK_SCREEN_XDISPLAY (screen);
3215
3216       pointer_window = None;
3217       screens = gdk_display_get_n_screens (display);
3218       for (i = 0; i < screens; ++i) {
3219         screen = gdk_display_get_screen (display, i);
3220         toplevels = gdk_screen_get_toplevel_windows (screen);
3221         for (list = toplevels; list != NULL; list = g_list_next (list)) {
3222           window = GDK_WINDOW (list->data);
3223           xwindow = GDK_WINDOW_XID (window);
3224           gdk_error_trap_push ();
3225           XQueryPointer (xdisplay, xwindow,
3226                          &root, &child, &rootx, &rooty, &winx, &winy, &xmask);
3227           if (gdk_error_trap_pop ())
3228             continue;
3229           if (child != None) 
3230             {
3231               pointer_window = child;
3232               break;
3233             }
3234           gdk_window_get_geometry (window, NULL, NULL, &width, &height, NULL);
3235           if (winx >= 0 && winy >= 0 && winx < width && winy < height) 
3236             {
3237               /* A childless toplevel, or below another window? */
3238               XSetWindowAttributes attributes;
3239               Window w;
3240               
3241               w = XCreateWindow (xdisplay, xwindow, winx, winy, 1, 1, 0, 
3242                                  CopyFromParent, InputOnly, CopyFromParent, 
3243                                  0, &attributes);
3244               XMapWindow (xdisplay, w);
3245               XQueryPointer (xdisplay, xwindow, 
3246                              &root, &child, &rootx, &rooty, &winx, &winy, &xmask);
3247               XDestroyWindow (xdisplay, w);
3248               if (child == w) 
3249                 {
3250                   pointer_window = xwindow;
3251                   break;
3252                 }
3253             }
3254         }
3255         g_list_free (toplevels);
3256         if (pointer_window != None)
3257           break;
3258       }
3259       xwindow = pointer_window;
3260
3261       while (xwindow)
3262         {
3263           xwindow_last = xwindow;
3264           gdk_error_trap_push ();
3265           XQueryPointer (xdisplay, xwindow,
3266                          &root, &xwindow, &rootx, &rooty, &winx, &winy, &xmask);
3267           if (gdk_error_trap_pop ())
3268             break;
3269           if (get_toplevel && xwindow_last != root &&
3270               (window = gdk_window_lookup_for_display (display, xwindow_last)) != NULL &&
3271               GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
3272             break;
3273         }
3274
3275       window = gdk_window_lookup_for_display (display, xwindow_last);
3276
3277       *win_x = window ? winx : -1;
3278       *win_y = window ? winy : -1;
3279       if (mask)
3280         *mask = xmask;
3281     }
3282
3283   gdk_x11_display_ungrab (display);
3284
3285   return window;
3286 }
3287
3288 static GdkEventMask
3289 gdk_window_x11_get_events (GdkWindow *window)
3290 {
3291   XWindowAttributes attrs;
3292   GdkEventMask event_mask;
3293   GdkEventMask filtered;
3294
3295   if (GDK_WINDOW_DESTROYED (window))
3296     return 0;
3297   else
3298     {
3299       XGetWindowAttributes (GDK_WINDOW_XDISPLAY (window),
3300                             GDK_WINDOW_XID (window),
3301                             &attrs);
3302       event_mask = x_event_mask_to_gdk_event_mask (attrs.your_event_mask);
3303       /* if property change was filtered out before, keep it filtered out */
3304       filtered = GDK_STRUCTURE_MASK | GDK_PROPERTY_CHANGE_MASK;
3305       window->event_mask = event_mask & ((window->event_mask & filtered) | ~filtered);
3306
3307       return event_mask;
3308     }
3309 }
3310 static void
3311 gdk_window_x11_set_events (GdkWindow    *window,
3312                            GdkEventMask  event_mask)
3313 {
3314   long xevent_mask = 0;
3315   
3316   if (!GDK_WINDOW_DESTROYED (window))
3317     {
3318       GdkDisplayX11 *display_x11;
3319
3320       if (GDK_WINDOW_XID (window) != GDK_WINDOW_XROOTWIN (window))
3321         xevent_mask = StructureNotifyMask | PropertyChangeMask;
3322
3323       display_x11 = GDK_DISPLAY_X11 (gdk_window_get_display (window));
3324       gdk_event_source_select_events ((GdkEventSource *) display_x11->event_source,
3325                                       GDK_WINDOW_XID (window), event_mask,
3326                                       xevent_mask);
3327     }
3328 }
3329
3330 static inline void
3331 do_shape_combine_region (GdkWindow       *window,
3332                          const cairo_region_t *shape_region,
3333                          gint             offset_x,
3334                          gint             offset_y,
3335                          gint             shape)
3336 {
3337   if (GDK_WINDOW_DESTROYED (window))
3338     return;
3339
3340   if (shape_region == NULL)
3341     {
3342       /* Use NULL mask to unset the shape */
3343       if (shape == ShapeBounding
3344           ? gdk_display_supports_shapes (GDK_WINDOW_DISPLAY (window))
3345           : gdk_display_supports_input_shapes (GDK_WINDOW_DISPLAY (window)))
3346         {
3347           if (shape == ShapeBounding)
3348             {
3349               _gdk_x11_window_tmp_unset_parent_bg (window);
3350               _gdk_x11_window_tmp_unset_bg (window, TRUE);
3351             }
3352           XShapeCombineMask (GDK_WINDOW_XDISPLAY (window),
3353                              GDK_WINDOW_XID (window),
3354                              shape,
3355                              0, 0,
3356                              None,
3357                              ShapeSet);
3358           if (shape == ShapeBounding)
3359             {
3360               _gdk_x11_window_tmp_reset_parent_bg (window);
3361               _gdk_x11_window_tmp_reset_bg (window, TRUE);
3362             }
3363         }
3364       return;
3365     }
3366   
3367   if (shape == ShapeBounding
3368       ? gdk_display_supports_shapes (GDK_WINDOW_DISPLAY (window))
3369       : gdk_display_supports_input_shapes (GDK_WINDOW_DISPLAY (window)))
3370     {
3371       gint n_rects = 0;
3372       XRectangle *xrects = NULL;
3373
3374       _gdk_region_get_xrectangles (shape_region,
3375                                    0, 0,
3376                                    &xrects, &n_rects);
3377       
3378       if (shape == ShapeBounding)
3379         {
3380           _gdk_x11_window_tmp_unset_parent_bg (window);
3381           _gdk_x11_window_tmp_unset_bg (window, TRUE);
3382         }
3383       XShapeCombineRectangles (GDK_WINDOW_XDISPLAY (window),
3384                                GDK_WINDOW_XID (window),
3385                                shape,
3386                                offset_x, offset_y,
3387                                xrects, n_rects,
3388                                ShapeSet,
3389                                YXBanded);
3390
3391       if (shape == ShapeBounding)
3392         {
3393           _gdk_x11_window_tmp_reset_parent_bg (window);
3394           _gdk_x11_window_tmp_reset_bg (window, TRUE);
3395         }
3396       
3397       g_free (xrects);
3398     }
3399 }
3400
3401 static void
3402 gdk_window_x11_shape_combine_region (GdkWindow       *window,
3403                                      const cairo_region_t *shape_region,
3404                                      gint             offset_x,
3405                                      gint             offset_y)
3406 {
3407   do_shape_combine_region (window, shape_region, offset_x, offset_y, ShapeBounding);
3408 }
3409
3410 static void 
3411 gdk_window_x11_input_shape_combine_region (GdkWindow       *window,
3412                                            const cairo_region_t *shape_region,
3413                                            gint             offset_x,
3414                                            gint             offset_y)
3415 {
3416 #ifdef ShapeInput
3417   do_shape_combine_region (window, shape_region, offset_x, offset_y, ShapeInput);
3418 #endif
3419 }
3420
3421
3422 /**
3423  * gdk_window_set_override_redirect:
3424  * @window: a toplevel #GdkWindow
3425  * @override_redirect: %TRUE if window should be override redirect
3426  *
3427  * An override redirect window is not under the control of the window manager.
3428  * This means it won't have a titlebar, won't be minimizable, etc. - it will
3429  * be entirely under the control of the application. The window manager
3430  * can't see the override redirect window at all.
3431  *
3432  * Override redirect should only be used for short-lived temporary
3433  * windows, such as popup menus. #GtkMenu uses an override redirect
3434  * window in its implementation, for example.
3435  * 
3436  **/
3437 void
3438 gdk_window_set_override_redirect (GdkWindow *window,
3439                                   gboolean override_redirect)
3440 {
3441   XSetWindowAttributes attr;
3442   
3443   if (!GDK_WINDOW_DESTROYED (window) &&
3444       WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
3445     {
3446       GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (window->impl);
3447
3448       attr.override_redirect = (override_redirect? True : False);
3449       XChangeWindowAttributes (GDK_WINDOW_XDISPLAY (window),
3450                                GDK_WINDOW_XID (window),
3451                                CWOverrideRedirect,
3452                                &attr);
3453
3454       impl->override_redirect = attr.override_redirect;
3455     }
3456 }
3457
3458 /**
3459  * gdk_window_set_accept_focus:
3460  * @window: a toplevel #GdkWindow
3461  * @accept_focus: %TRUE if the window should receive input focus
3462  *
3463  * Setting @accept_focus to %FALSE hints the desktop environment that the
3464  * window doesn't want to receive input focus. 
3465  *
3466  * On X, it is the responsibility of the window manager to interpret this 
3467  * hint. ICCCM-compliant window manager usually respect it.
3468  *
3469  * Since: 2.4 
3470  **/
3471 void
3472 gdk_window_set_accept_focus (GdkWindow *window,
3473                              gboolean accept_focus)
3474 {
3475   accept_focus = accept_focus != FALSE;
3476
3477   if (window->accept_focus != accept_focus)
3478     {
3479       window->accept_focus = accept_focus;
3480
3481       if (!GDK_WINDOW_DESTROYED (window) &&
3482           WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
3483         update_wm_hints (window, FALSE);
3484     }
3485 }
3486
3487 /**
3488  * gdk_window_set_focus_on_map:
3489  * @window: a toplevel #GdkWindow
3490  * @focus_on_map: %TRUE if the window should receive input focus when mapped
3491  *
3492  * Setting @focus_on_map to %FALSE hints the desktop environment that the
3493  * window doesn't want to receive input focus when it is mapped.  
3494  * focus_on_map should be turned off for windows that aren't triggered
3495  * interactively (such as popups from network activity).
3496  *
3497  * On X, it is the responsibility of the window manager to interpret
3498  * this hint. Window managers following the freedesktop.org window
3499  * manager extension specification should respect it.
3500  *
3501  * Since: 2.6 
3502  **/
3503 void
3504 gdk_window_set_focus_on_map (GdkWindow *window,
3505                              gboolean focus_on_map)
3506 {
3507   focus_on_map = focus_on_map != FALSE;
3508
3509   if (window->focus_on_map != focus_on_map)
3510     {
3511       window->focus_on_map = focus_on_map;
3512       
3513       if ((!GDK_WINDOW_DESTROYED (window)) &&
3514           (!window->focus_on_map) &&
3515           WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
3516         gdk_x11_window_set_user_time (window, 0);
3517     }
3518 }
3519
3520 /**
3521  * gdk_x11_window_set_user_time:
3522  * @window: A toplevel #GdkWindow
3523  * @timestamp: An XServer timestamp to which the property should be set
3524  *
3525  * The application can use this call to update the _NET_WM_USER_TIME
3526  * property on a toplevel window.  This property stores an Xserver
3527  * time which represents the time of the last user input event
3528  * received for this window.  This property may be used by the window
3529  * manager to alter the focus, stacking, and/or placement behavior of
3530  * windows when they are mapped depending on whether the new window
3531  * was created by a user action or is a "pop-up" window activated by a
3532  * timer or some other event.
3533  *
3534  * Note that this property is automatically updated by GDK, so this
3535  * function should only be used by applications which handle input
3536  * events bypassing GDK.
3537  *
3538  * Since: 2.6
3539  **/
3540 void
3541 gdk_x11_window_set_user_time (GdkWindow *window,
3542                               guint32    timestamp)
3543 {
3544   GdkDisplay *display;
3545   GdkDisplayX11 *display_x11;
3546   GdkToplevelX11 *toplevel;
3547   glong timestamp_long = (glong)timestamp;
3548   Window xid;
3549
3550   if (GDK_WINDOW_DESTROYED (window) ||
3551       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
3552     return;
3553
3554   display = gdk_window_get_display (window);
3555   display_x11 = GDK_DISPLAY_X11 (display);
3556   toplevel = _gdk_x11_window_get_toplevel (window);
3557
3558   if (!toplevel)
3559     {
3560       g_warning ("gdk_window_set_user_time called on non-toplevel\n");
3561       return;
3562     }
3563
3564   if (toplevel->focus_window != None &&
3565       gdk_x11_screen_supports_net_wm_hint (GDK_WINDOW_SCREEN (window),
3566                                            gdk_atom_intern_static_string ("_NET_WM_USER_TIME_WINDOW")))
3567     xid = toplevel->focus_window;
3568   else
3569     xid = GDK_WINDOW_XID (window);
3570
3571   XChangeProperty (GDK_DISPLAY_XDISPLAY (display), xid,
3572                    gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_USER_TIME"),
3573                    XA_CARDINAL, 32, PropModeReplace,
3574                    (guchar *)&timestamp_long, 1);
3575
3576   if (timestamp_long != GDK_CURRENT_TIME &&
3577       (display_x11->user_time == GDK_CURRENT_TIME ||
3578        XSERVER_TIME_IS_LATER (timestamp_long, display_x11->user_time)))
3579     display_x11->user_time = timestamp_long;
3580
3581   if (toplevel)
3582     toplevel->user_time = timestamp_long;
3583 }
3584
3585 #define GDK_SELECTION_MAX_SIZE(display)                                 \
3586   MIN(262144,                                                           \
3587       XExtendedMaxRequestSize (GDK_DISPLAY_XDISPLAY (display)) == 0     \
3588        ? XMaxRequestSize (GDK_DISPLAY_XDISPLAY (display)) - 100         \
3589        : XExtendedMaxRequestSize (GDK_DISPLAY_XDISPLAY (display)) - 100)
3590
3591 static void
3592 gdk_window_update_icon (GdkWindow *window,
3593                         GList     *icon_list)
3594 {
3595   GdkToplevelX11 *toplevel;
3596   GdkPixbuf *best_icon;
3597   GList *tmp_list;
3598   int best_size;
3599   
3600   toplevel = _gdk_x11_window_get_toplevel (window);
3601
3602   if (toplevel->icon_pixmap != NULL)
3603     {
3604       cairo_surface_destroy (toplevel->icon_pixmap);
3605       toplevel->icon_pixmap = NULL;
3606     }
3607   
3608   if (toplevel->icon_mask != NULL)
3609     {
3610       cairo_surface_destroy (toplevel->icon_mask);
3611       toplevel->icon_mask = NULL;
3612     }
3613   
3614 #define IDEAL_SIZE 48
3615   
3616   best_size = G_MAXINT;
3617   best_icon = NULL;
3618   for (tmp_list = icon_list; tmp_list; tmp_list = tmp_list->next)
3619     {
3620       GdkPixbuf *pixbuf = tmp_list->data;
3621       int this;
3622   
3623       /* average width and height - if someone passes in a rectangular
3624        * icon they deserve what they get.
3625        */
3626       this = gdk_pixbuf_get_width (pixbuf) + gdk_pixbuf_get_height (pixbuf);
3627       this /= 2;
3628   
3629       if (best_icon == NULL)
3630         {
3631           best_icon = pixbuf;
3632           best_size = this;
3633         }
3634       else
3635         {
3636           /* icon is better if it's 32 pixels or larger, and closer to
3637            * the ideal size than the current best.
3638            */
3639           if (this >= 32 &&
3640               (ABS (best_size - IDEAL_SIZE) <
3641                ABS (this - IDEAL_SIZE)))
3642             {
3643               best_icon = pixbuf;
3644               best_size = this;
3645             }
3646         }
3647     }
3648
3649   if (best_icon)
3650     {
3651       int width = gdk_pixbuf_get_width (best_icon);
3652       int height = gdk_pixbuf_get_height (best_icon);
3653       cairo_t *cr;
3654
3655       toplevel->icon_pixmap = gdk_x11_window_create_pixmap_surface (window,
3656                                                                     width,
3657                                                                     height);
3658
3659       cr = cairo_create (toplevel->icon_pixmap);
3660       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
3661       gdk_cairo_set_source_pixbuf (cr, best_icon, 0, 0);
3662       if (gdk_pixbuf_get_has_alpha (best_icon))
3663         {
3664           /* Saturate the image, so it has bilevel alpha */
3665           cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
3666           cairo_paint (cr);
3667           cairo_set_operator (cr, CAIRO_OPERATOR_SATURATE);
3668           cairo_paint (cr);
3669           cairo_pop_group_to_source (cr);
3670         }
3671       cairo_paint (cr);
3672       cairo_destroy (cr);
3673
3674       if (gdk_pixbuf_get_has_alpha (best_icon))
3675         {
3676           toplevel->icon_mask = _gdk_x11_window_create_bitmap_surface (window,
3677                                                                        width,
3678                                                                        height);
3679
3680           cr = cairo_create (toplevel->icon_mask);
3681           gdk_cairo_set_source_pixbuf (cr, best_icon, 0, 0);
3682           cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
3683           cairo_paint (cr);
3684           cairo_destroy (cr);
3685         }
3686     }
3687
3688   update_wm_hints (window, FALSE);
3689 }
3690
3691 /**
3692  * gdk_window_set_icon_list:
3693  * @window: The #GdkWindow toplevel window to set the icon of.
3694  * @pixbufs: (transfer none) (element-type GdkPixbuf):
3695  *     A list of pixbufs, of different sizes.
3696  *
3697  * Sets a list of icons for the window. One of these will be used
3698  * to represent the window when it has been iconified. The icon is
3699  * usually shown in an icon box or some sort of task bar. Which icon
3700  * size is shown depends on the window manager. The window manager
3701  * can scale the icon  but setting several size icons can give better
3702  * image quality since the window manager may only need to scale the
3703  * icon by a small amount or not at all.
3704  *
3705  **/
3706 void
3707 gdk_window_set_icon_list (GdkWindow *window,
3708                           GList     *pixbufs)
3709 {
3710   gulong *data;
3711   guchar *pixels;
3712   gulong *p;
3713   gint size;
3714   GList *l;
3715   GdkPixbuf *pixbuf;
3716   gint width, height, stride;
3717   gint x, y;
3718   gint n_channels;
3719   GdkDisplay *display;
3720   gint n;
3721   
3722   if (GDK_WINDOW_DESTROYED (window) ||
3723       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
3724     return;
3725
3726   display = gdk_window_get_display (window);
3727   
3728   l = pixbufs;
3729   size = 0;
3730   n = 0;
3731   while (l)
3732     {
3733       pixbuf = l->data;
3734       g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3735
3736       width = gdk_pixbuf_get_width (pixbuf);
3737       height = gdk_pixbuf_get_height (pixbuf);
3738       
3739       /* silently ignore overlarge icons */
3740       if (size + 2 + width * height > GDK_SELECTION_MAX_SIZE(display))
3741         {
3742           g_warning ("gdk_window_set_icon_list: icons too large");
3743           break;
3744         }
3745      
3746       n++;
3747       size += 2 + width * height;
3748       
3749       l = g_list_next (l);
3750     }
3751
3752   data = g_malloc (size * sizeof (gulong));
3753
3754   l = pixbufs;
3755   p = data;
3756   while (l && n > 0)
3757     {
3758       pixbuf = l->data;
3759       
3760       width = gdk_pixbuf_get_width (pixbuf);
3761       height = gdk_pixbuf_get_height (pixbuf);
3762       stride = gdk_pixbuf_get_rowstride (pixbuf);
3763       n_channels = gdk_pixbuf_get_n_channels (pixbuf);
3764       
3765       *p++ = width;
3766       *p++ = height;
3767
3768       pixels = gdk_pixbuf_get_pixels (pixbuf);
3769
3770       for (y = 0; y < height; y++)
3771         {
3772           for (x = 0; x < width; x++)
3773             {
3774               guchar r, g, b, a;
3775               
3776               r = pixels[y*stride + x*n_channels + 0];
3777               g = pixels[y*stride + x*n_channels + 1];
3778               b = pixels[y*stride + x*n_channels + 2];
3779               if (n_channels >= 4)
3780                 a = pixels[y*stride + x*n_channels + 3];
3781               else
3782                 a = 255;
3783               
3784               *p++ = a << 24 | r << 16 | g << 8 | b ;
3785             }
3786         }
3787
3788       l = g_list_next (l);
3789       n--;
3790     }
3791
3792   if (size > 0)
3793     {
3794       XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3795                        GDK_WINDOW_XID (window),
3796                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_ICON"),
3797                        XA_CARDINAL, 32,
3798                        PropModeReplace,
3799                        (guchar*) data, size);
3800     }
3801   else
3802     {
3803       XDeleteProperty (GDK_DISPLAY_XDISPLAY (display),
3804                        GDK_WINDOW_XID (window),
3805                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_ICON"));
3806     }
3807   
3808   g_free (data);
3809
3810   gdk_window_update_icon (window, pixbufs);
3811 }
3812
3813 static gboolean
3814 gdk_window_icon_name_set (GdkWindow *window)
3815 {
3816   return GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (window),
3817                                                g_quark_from_static_string ("gdk-icon-name-set")));
3818 }
3819
3820 /**
3821  * gdk_window_set_icon_name:
3822  * @window: a toplevel #GdkWindow
3823  * @name: name of window while iconified (minimized)
3824  *
3825  * Windows may have a name used while minimized, distinct from the
3826  * name they display in their titlebar. Most of the time this is a bad
3827  * idea from a user interface standpoint. But you can set such a name
3828  * with this function, if you like.
3829  *
3830  * After calling this with a non-%NULL @name, calls to gdk_window_set_title()
3831  * will not update the icon title.
3832  *
3833  * Using %NULL for @name unsets the icon title; further calls to
3834  * gdk_window_set_title() will again update the icon title as well.
3835  **/
3836 void
3837 gdk_window_set_icon_name (GdkWindow   *window, 
3838                           const gchar *name)
3839 {
3840   GdkDisplay *display;
3841
3842   if (GDK_WINDOW_DESTROYED (window) ||
3843       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
3844     return;
3845
3846   display = gdk_window_get_display (window);
3847
3848   g_object_set_qdata (G_OBJECT (window), g_quark_from_static_string ("gdk-icon-name-set"),
3849                       GUINT_TO_POINTER (name != NULL));
3850
3851   if (name != NULL)
3852     {
3853       XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3854                        GDK_WINDOW_XID (window),
3855                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_ICON_NAME"),
3856                        gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
3857                        PropModeReplace, (guchar *)name, strlen (name));
3858
3859       set_text_property (display, GDK_WINDOW_XID (window),
3860                          gdk_x11_get_xatom_by_name_for_display (display, "WM_ICON_NAME"),
3861                          name);
3862     }
3863   else
3864     {
3865       XDeleteProperty (GDK_DISPLAY_XDISPLAY (display),
3866                        GDK_WINDOW_XID (window),
3867                        gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_ICON_NAME"));
3868       XDeleteProperty (GDK_DISPLAY_XDISPLAY (display),
3869                        GDK_WINDOW_XID (window),
3870                        gdk_x11_get_xatom_by_name_for_display (display, "WM_ICON_NAME"));
3871     }
3872 }
3873
3874 /**
3875  * gdk_window_iconify:
3876  * @window: a toplevel #GdkWindow
3877  * 
3878  * Asks to iconify (minimize) @window. The window manager may choose
3879  * to ignore the request, but normally will honor it. Using
3880  * gtk_window_iconify() is preferred, if you have a #GtkWindow widget.
3881  *
3882  * This function only makes sense when @window is a toplevel window.
3883  *
3884  **/
3885 void
3886 gdk_window_iconify (GdkWindow *window)
3887 {
3888   if (GDK_WINDOW_DESTROYED (window) ||
3889       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
3890     return;
3891
3892   if (GDK_WINDOW_IS_MAPPED (window))
3893     {  
3894       XIconifyWindow (GDK_WINDOW_XDISPLAY (window),
3895                       GDK_WINDOW_XID (window),
3896                       gdk_screen_get_number (GDK_WINDOW_SCREEN (window)));
3897     }
3898   else
3899     {
3900       /* Flip our client side flag, the real work happens on map. */
3901       gdk_synthesize_window_state (window,
3902                                    0,
3903                                    GDK_WINDOW_STATE_ICONIFIED);
3904     }
3905 }
3906
3907 /**
3908  * gdk_window_deiconify:
3909  * @window: a toplevel #GdkWindow
3910  *
3911  * Attempt to deiconify (unminimize) @window. On X11 the window manager may
3912  * choose to ignore the request to deiconify. When using GTK+,
3913  * use gtk_window_deiconify() instead of the #GdkWindow variant. Or better yet,
3914  * you probably want to use gtk_window_present(), which raises the window, focuses it,
3915  * unminimizes it, and puts it on the current desktop.
3916  *
3917  **/
3918 void
3919 gdk_window_deiconify (GdkWindow *window)
3920 {
3921   if (GDK_WINDOW_DESTROYED (window) ||
3922       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
3923     return;
3924
3925   if (GDK_WINDOW_IS_MAPPED (window))
3926     {  
3927       gdk_window_show (window);
3928     }
3929   else
3930     {
3931       /* Flip our client side flag, the real work happens on map. */
3932       gdk_synthesize_window_state (window,
3933                                    GDK_WINDOW_STATE_ICONIFIED,
3934                                    0);
3935     }
3936 }
3937
3938 /**
3939  * gdk_window_stick:
3940  * @window: a toplevel #GdkWindow
3941  *
3942  * "Pins" a window such that it's on all workspaces and does not scroll
3943  * with viewports, for window managers that have scrollable viewports.
3944  * (When using #GtkWindow, gtk_window_stick() may be more useful.)
3945  *
3946  * On the X11 platform, this function depends on window manager
3947  * support, so may have no effect with many window managers. However,
3948  * GDK will do the best it can to convince the window manager to stick
3949  * the window. For window managers that don't support this operation,
3950  * there's nothing you can do to force it to happen.
3951  * 
3952  **/
3953 void
3954 gdk_window_stick (GdkWindow *window)
3955 {
3956   if (GDK_WINDOW_DESTROYED (window) ||
3957       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
3958     return;
3959
3960   if (GDK_WINDOW_IS_MAPPED (window))
3961     {
3962       /* "stick" means stick to all desktops _and_ do not scroll with the
3963        * viewport. i.e. glue to the monitor glass in all cases.
3964        */
3965       
3966       XClientMessageEvent xclient;
3967
3968       /* Request stick during viewport scroll */
3969       gdk_wmspec_change_state (TRUE, window,
3970                                gdk_atom_intern_static_string ("_NET_WM_STATE_STICKY"),
3971                                GDK_NONE);
3972
3973       /* Request desktop 0xFFFFFFFF */
3974       memset (&xclient, 0, sizeof (xclient));
3975       xclient.type = ClientMessage;
3976       xclient.window = GDK_WINDOW_XID (window);
3977       xclient.display = GDK_WINDOW_XDISPLAY (window);
3978       xclient.message_type = gdk_x11_get_xatom_by_name_for_display (GDK_WINDOW_DISPLAY (window), 
3979                                                                         "_NET_WM_DESKTOP");
3980       xclient.format = 32;
3981
3982       xclient.data.l[0] = 0xFFFFFFFF;
3983       xclient.data.l[1] = 0;
3984       xclient.data.l[2] = 0;
3985       xclient.data.l[3] = 0;
3986       xclient.data.l[4] = 0;
3987
3988       XSendEvent (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XROOTWIN (window), False,
3989                   SubstructureRedirectMask | SubstructureNotifyMask,
3990                   (XEvent *)&xclient);
3991     }
3992   else
3993     {
3994       /* Flip our client side flag, the real work happens on map. */
3995       gdk_synthesize_window_state (window,
3996                                    0,
3997                                    GDK_WINDOW_STATE_STICKY);
3998     }
3999 }
4000
4001 /**
4002  * gdk_window_unstick:
4003  * @window: a toplevel #GdkWindow
4004  *
4005  * Reverse operation for gdk_window_stick(); see gdk_window_stick(),
4006  * and gtk_window_unstick().
4007  * 
4008  **/
4009 void
4010 gdk_window_unstick (GdkWindow *window)
4011 {
4012   if (GDK_WINDOW_DESTROYED (window) ||
4013       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
4014     return;
4015
4016   if (GDK_WINDOW_IS_MAPPED (window))
4017     {
4018       /* Request unstick from viewport */
4019       gdk_wmspec_change_state (FALSE, window,
4020                                gdk_atom_intern_static_string ("_NET_WM_STATE_STICKY"),
4021                                GDK_NONE);
4022
4023       move_to_current_desktop (window);
4024     }
4025   else
4026     {
4027       /* Flip our client side flag, the real work happens on map. */
4028       gdk_synthesize_window_state (window,
4029                                    GDK_WINDOW_STATE_STICKY,
4030                                    0);
4031
4032     }
4033 }
4034
4035 /**
4036  * gdk_window_maximize:
4037  * @window: a toplevel #GdkWindow
4038  *
4039  * Maximizes the window. If the window was already maximized, then
4040  * this function does nothing.
4041  * 
4042  * On X11, asks the window manager to maximize @window, if the window
4043  * manager supports this operation. Not all window managers support
4044  * this, and some deliberately ignore it or don't have a concept of
4045  * "maximized"; so you can't rely on the maximization actually
4046  * happening. But it will happen with most standard window managers,
4047  * and GDK makes a best effort to get it to happen.
4048  *
4049  * On Windows, reliably maximizes the window.
4050  * 
4051  **/
4052 void
4053 gdk_window_maximize (GdkWindow *window)
4054 {
4055   if (GDK_WINDOW_DESTROYED (window) ||
4056       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
4057     return;
4058
4059   if (GDK_WINDOW_IS_MAPPED (window))
4060     gdk_wmspec_change_state (TRUE, window,
4061                              gdk_atom_intern_static_string ("_NET_WM_STATE_MAXIMIZED_VERT"),
4062                              gdk_atom_intern_static_string ("_NET_WM_STATE_MAXIMIZED_HORZ"));
4063   else
4064     gdk_synthesize_window_state (window,
4065                                  0,
4066                                  GDK_WINDOW_STATE_MAXIMIZED);
4067 }
4068
4069 /**
4070  * gdk_window_unmaximize:
4071  * @window: a toplevel #GdkWindow
4072  *
4073  * Unmaximizes the window. If the window wasn't maximized, then this
4074  * function does nothing.
4075  * 
4076  * On X11, asks the window manager to unmaximize @window, if the
4077  * window manager supports this operation. Not all window managers
4078  * support this, and some deliberately ignore it or don't have a
4079  * concept of "maximized"; so you can't rely on the unmaximization
4080  * actually happening. But it will happen with most standard window
4081  * managers, and GDK makes a best effort to get it to happen.
4082  *
4083  * On Windows, reliably unmaximizes the window.
4084  * 
4085  **/
4086 void
4087 gdk_window_unmaximize (GdkWindow *window)
4088 {
4089   if (GDK_WINDOW_DESTROYED (window) ||
4090       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
4091     return;
4092
4093   if (GDK_WINDOW_IS_MAPPED (window))
4094     gdk_wmspec_change_state (FALSE, window,
4095                              gdk_atom_intern_static_string ("_NET_WM_STATE_MAXIMIZED_VERT"),
4096                              gdk_atom_intern_static_string ("_NET_WM_STATE_MAXIMIZED_HORZ"));
4097   else
4098     gdk_synthesize_window_state (window,
4099                                  GDK_WINDOW_STATE_MAXIMIZED,
4100                                  0);
4101 }
4102
4103 /**
4104  * gdk_window_fullscreen:
4105  * @window: a toplevel #GdkWindow
4106  *
4107  * Moves the window into fullscreen mode. This means the
4108  * window covers the entire screen and is above any panels
4109  * or task bars.
4110  *
4111  * If the window was already fullscreen, then this function does nothing.
4112  * 
4113  * On X11, asks the window manager to put @window in a fullscreen
4114  * state, if the window manager supports this operation. Not all
4115  * window managers support this, and some deliberately ignore it or
4116  * don't have a concept of "fullscreen"; so you can't rely on the
4117  * fullscreenification actually happening. But it will happen with
4118  * most standard window managers, and GDK makes a best effort to get
4119  * it to happen.
4120  *
4121  * Since: 2.2
4122  **/
4123 void
4124 gdk_window_fullscreen (GdkWindow *window)
4125 {
4126   if (GDK_WINDOW_DESTROYED (window) ||
4127       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
4128     return;
4129
4130   if (GDK_WINDOW_IS_MAPPED (window))
4131     gdk_wmspec_change_state (TRUE, window,
4132                              gdk_atom_intern_static_string ("_NET_WM_STATE_FULLSCREEN"),
4133                              GDK_NONE);
4134
4135   else
4136     gdk_synthesize_window_state (window,
4137                                  0,
4138                                  GDK_WINDOW_STATE_FULLSCREEN);
4139 }
4140
4141 /**
4142  * gdk_window_unfullscreen:
4143  * @window: a toplevel #GdkWindow
4144  *
4145  * Moves the window out of fullscreen mode. If the window was not
4146  * fullscreen, does nothing.
4147  * 
4148  * On X11, asks the window manager to move @window out of the fullscreen
4149  * state, if the window manager supports this operation. Not all
4150  * window managers support this, and some deliberately ignore it or
4151  * don't have a concept of "fullscreen"; so you can't rely on the
4152  * unfullscreenification actually happening. But it will happen with
4153  * most standard window managers, and GDK makes a best effort to get
4154  * it to happen. 
4155  *
4156  * Since: 2.2
4157  **/
4158 void
4159 gdk_window_unfullscreen (GdkWindow *window)
4160 {
4161   if (GDK_WINDOW_DESTROYED (window) ||
4162       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
4163     return;
4164
4165   if (GDK_WINDOW_IS_MAPPED (window))
4166     gdk_wmspec_change_state (FALSE, window,
4167                              gdk_atom_intern_static_string ("_NET_WM_STATE_FULLSCREEN"),
4168                              GDK_NONE);
4169
4170   else
4171     gdk_synthesize_window_state (window,
4172                                  GDK_WINDOW_STATE_FULLSCREEN,
4173                                  0);
4174 }
4175
4176 /**
4177  * gdk_window_set_keep_above:
4178  * @window: a toplevel #GdkWindow
4179  * @setting: whether to keep @window above other windows
4180  *
4181  * Set if @window must be kept above other windows. If the
4182  * window was already above, then this function does nothing.
4183  * 
4184  * On X11, asks the window manager to keep @window above, if the window
4185  * manager supports this operation. Not all window managers support
4186  * this, and some deliberately ignore it or don't have a concept of
4187  * "keep above"; so you can't rely on the window being kept above.
4188  * But it will happen with most standard window managers,
4189  * and GDK makes a best effort to get it to happen.
4190  *
4191  * Since: 2.4
4192  **/
4193 void
4194 gdk_window_set_keep_above (GdkWindow *window,
4195                            gboolean   setting)
4196 {
4197   g_return_if_fail (GDK_IS_WINDOW (window));
4198
4199   if (GDK_WINDOW_DESTROYED (window) ||
4200       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
4201     return;
4202
4203   if (GDK_WINDOW_IS_MAPPED (window))
4204     {
4205       if (setting)
4206         gdk_wmspec_change_state (FALSE, window,
4207                                  gdk_atom_intern_static_string ("_NET_WM_STATE_BELOW"),
4208                                  GDK_NONE);
4209       gdk_wmspec_change_state (setting, window,
4210                                gdk_atom_intern_static_string ("_NET_WM_STATE_ABOVE"),
4211                                GDK_NONE);
4212     }
4213   else
4214     gdk_synthesize_window_state (window,
4215                                  setting ? GDK_WINDOW_STATE_BELOW : GDK_WINDOW_STATE_ABOVE,
4216                                  setting ? GDK_WINDOW_STATE_ABOVE : 0);
4217 }
4218
4219 /**
4220  * gdk_window_set_keep_below:
4221  * @window: a toplevel #GdkWindow
4222  * @setting: whether to keep @window below other windows
4223  *
4224  * Set if @window must be kept below other windows. If the
4225  * window was already below, then this function does nothing.
4226  * 
4227  * On X11, asks the window manager to keep @window below, if the window
4228  * manager supports this operation. Not all window managers support
4229  * this, and some deliberately ignore it or don't have a concept of
4230  * "keep below"; so you can't rely on the window being kept below.
4231  * But it will happen with most standard window managers,
4232  * and GDK makes a best effort to get it to happen.
4233  *
4234  * Since: 2.4
4235  **/
4236 void
4237 gdk_window_set_keep_below (GdkWindow *window, gboolean setting)
4238 {
4239   g_return_if_fail (GDK_IS_WINDOW (window));
4240
4241   if (GDK_WINDOW_DESTROYED (window) ||
4242       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
4243     return;
4244
4245   if (GDK_WINDOW_IS_MAPPED (window))
4246     {
4247       if (setting)
4248         gdk_wmspec_change_state (FALSE, window,
4249                                  gdk_atom_intern_static_string ("_NET_WM_STATE_ABOVE"),
4250                                  GDK_NONE);
4251       gdk_wmspec_change_state (setting, window,
4252                                gdk_atom_intern_static_string ("_NET_WM_STATE_BELOW"),
4253                                GDK_NONE);
4254     }
4255   else
4256     gdk_synthesize_window_state (window,
4257                                  setting ? GDK_WINDOW_STATE_ABOVE : GDK_WINDOW_STATE_BELOW,
4258                                  setting ? GDK_WINDOW_STATE_BELOW : 0);
4259 }
4260
4261 /**
4262  * gdk_window_get_group:
4263  * @window: a toplevel #GdkWindow
4264  * 
4265  * Returns the group leader window for @window. See gdk_window_set_group().
4266  * 
4267  * Return value: (transfer none): the group leader window for @window
4268  *
4269  * Since: 2.4
4270  **/
4271 GdkWindow *
4272 gdk_window_get_group (GdkWindow *window)
4273 {
4274   GdkToplevelX11 *toplevel;
4275   
4276   if (GDK_WINDOW_DESTROYED (window) ||
4277       !WINDOW_IS_TOPLEVEL (window))
4278     return NULL;
4279   
4280   toplevel = _gdk_x11_window_get_toplevel (window);
4281
4282   return toplevel->group_leader;
4283 }
4284
4285 /**
4286  * gdk_window_set_group:
4287  * @window: a toplevel #GdkWindow
4288  * @leader: group leader window, or %NULL to restore the default group leader window
4289  *
4290  * Sets the group leader window for @window. By default,
4291  * GDK sets the group leader for all toplevel windows
4292  * to a global window implicitly created by GDK. With this function
4293  * you can override this default.
4294  *
4295  * The group leader window allows the window manager to distinguish
4296  * all windows that belong to a single application. It may for example
4297  * allow users to minimize/unminimize all windows belonging to an
4298  * application at once. You should only set a non-default group window
4299  * if your application pretends to be multiple applications.
4300  **/
4301 void          
4302 gdk_window_set_group (GdkWindow *window,
4303                       GdkWindow *leader)
4304 {
4305   GdkToplevelX11 *toplevel;
4306   
4307   g_return_if_fail (GDK_IS_WINDOW (window));
4308   g_return_if_fail (GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD);
4309   g_return_if_fail (leader == NULL || GDK_IS_WINDOW (leader));
4310
4311   if (GDK_WINDOW_DESTROYED (window) ||
4312       (leader != NULL && GDK_WINDOW_DESTROYED (leader)) ||
4313       !WINDOW_IS_TOPLEVEL (window))
4314     return;
4315
4316   toplevel = _gdk_x11_window_get_toplevel (window);
4317
4318   if (leader == NULL)
4319     leader = gdk_display_get_default_group (gdk_window_get_display (window));
4320   
4321   if (toplevel->group_leader != leader)
4322     {
4323       if (toplevel->group_leader)
4324         g_object_unref (toplevel->group_leader);
4325       toplevel->group_leader = g_object_ref (leader);
4326       (_gdk_x11_window_get_toplevel (leader))->is_leader = TRUE;      
4327     }
4328
4329   update_wm_hints (window, FALSE);
4330 }
4331
4332 static MotifWmHints *
4333 gdk_window_get_mwm_hints (GdkWindow *window)
4334 {
4335   GdkDisplay *display;
4336   Atom hints_atom = None;
4337   guchar *data;
4338   Atom type;
4339   gint format;
4340   gulong nitems;
4341   gulong bytes_after;
4342   
4343   if (GDK_WINDOW_DESTROYED (window))
4344     return NULL;
4345
4346   display = gdk_window_get_display (window);
4347   
4348   hints_atom = gdk_x11_get_xatom_by_name_for_display (display, _XA_MOTIF_WM_HINTS);
4349
4350   XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
4351                       hints_atom, 0, sizeof (MotifWmHints)/sizeof (long),
4352                       False, AnyPropertyType, &type, &format, &nitems,
4353                       &bytes_after, &data);
4354
4355   if (type == None)
4356     return NULL;
4357   
4358   return (MotifWmHints *)data;
4359 }
4360
4361 static void
4362 gdk_window_set_mwm_hints (GdkWindow *window,
4363                           MotifWmHints *new_hints)
4364 {
4365   GdkDisplay *display;
4366   Atom hints_atom = None;
4367   guchar *data;
4368   MotifWmHints *hints;
4369   Atom type;
4370   gint format;
4371   gulong nitems;
4372   gulong bytes_after;
4373   
4374   if (GDK_WINDOW_DESTROYED (window))
4375     return;
4376   
4377   display = gdk_window_get_display (window);
4378   
4379   hints_atom = gdk_x11_get_xatom_by_name_for_display (display, _XA_MOTIF_WM_HINTS);
4380
4381   XGetWindowProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
4382                       hints_atom, 0, sizeof (MotifWmHints)/sizeof (long),
4383                       False, AnyPropertyType, &type, &format, &nitems,
4384                       &bytes_after, &data);
4385   
4386   if (type == None)
4387     hints = new_hints;
4388   else
4389     {
4390       hints = (MotifWmHints *)data;
4391         
4392       if (new_hints->flags & MWM_HINTS_FUNCTIONS)
4393         {
4394           hints->flags |= MWM_HINTS_FUNCTIONS;
4395           hints->functions = new_hints->functions;
4396         }
4397       if (new_hints->flags & MWM_HINTS_DECORATIONS)
4398         {
4399           hints->flags |= MWM_HINTS_DECORATIONS;
4400           hints->decorations = new_hints->decorations;
4401         }
4402     }
4403   
4404   XChangeProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
4405                    hints_atom, hints_atom, 32, PropModeReplace,
4406                    (guchar *)hints, sizeof (MotifWmHints)/sizeof (long));
4407   
4408   if (hints != new_hints)
4409     XFree (hints);
4410 }
4411
4412 /**
4413  * gdk_window_set_decorations:
4414  * @window: a toplevel #GdkWindow
4415  * @decorations: decoration hint mask
4416  *
4417  * "Decorations" are the features the window manager adds to a toplevel #GdkWindow.
4418  * This function sets the traditional Motif window manager hints that tell the
4419  * window manager which decorations you would like your window to have.
4420  * Usually you should use gtk_window_set_decorated() on a #GtkWindow instead of
4421  * using the GDK function directly.
4422  *
4423  * The @decorations argument is the logical OR of the fields in
4424  * the #GdkWMDecoration enumeration. If #GDK_DECOR_ALL is included in the
4425  * mask, the other bits indicate which decorations should be turned off.
4426  * If #GDK_DECOR_ALL is not included, then the other bits indicate
4427  * which decorations should be turned on.
4428  *
4429  * Most window managers honor a decorations hint of 0 to disable all decorations,
4430  * but very few honor all possible combinations of bits.
4431  * 
4432  **/
4433 void
4434 gdk_window_set_decorations (GdkWindow      *window,
4435                             GdkWMDecoration decorations)
4436 {
4437   MotifWmHints hints;
4438
4439   if (GDK_WINDOW_DESTROYED (window) ||
4440       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
4441     return;
4442   
4443   /* initialize to zero to avoid writing uninitialized data to socket */
4444   memset(&hints, 0, sizeof(hints));
4445   hints.flags = MWM_HINTS_DECORATIONS;
4446   hints.decorations = decorations;
4447   
4448   gdk_window_set_mwm_hints (window, &hints);
4449 }
4450
4451 /**
4452  * gdk_window_get_decorations:
4453  * @window: The toplevel #GdkWindow to get the decorations from
4454  * @decorations: The window decorations will be written here
4455  *
4456  * Returns the decorations set on the GdkWindow with
4457  * gdk_window_set_decorations().
4458  *
4459  * Returns: %TRUE if the window has decorations set, %FALSE otherwise.
4460  **/
4461 gboolean
4462 gdk_window_get_decorations(GdkWindow       *window,
4463                            GdkWMDecoration *decorations)
4464 {
4465   MotifWmHints *hints;
4466   gboolean result = FALSE;
4467
4468   if (GDK_WINDOW_DESTROYED (window) ||
4469       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
4470     return FALSE;
4471   
4472   hints = gdk_window_get_mwm_hints (window);
4473   
4474   if (hints)
4475     {
4476       if (hints->flags & MWM_HINTS_DECORATIONS)
4477         {
4478           if (decorations)
4479             *decorations = hints->decorations;
4480           result = TRUE;
4481         }
4482       
4483       XFree (hints);
4484     }
4485
4486   return result;
4487 }
4488
4489 /**
4490  * gdk_window_set_functions:
4491  * @window: a toplevel #GdkWindow
4492  * @functions: bitmask of operations to allow on @window
4493  *
4494  * Sets hints about the window management functions to make available
4495  * via buttons on the window frame.
4496  * 
4497  * On the X backend, this function sets the traditional Motif window 
4498  * manager hint for this purpose. However, few window managers do
4499  * anything reliable or interesting with this hint. Many ignore it
4500  * entirely.
4501  *
4502  * The @functions argument is the logical OR of values from the
4503  * #GdkWMFunction enumeration. If the bitmask includes #GDK_FUNC_ALL,
4504  * then the other bits indicate which functions to disable; if
4505  * it doesn't include #GDK_FUNC_ALL, it indicates which functions to
4506  * enable.
4507  * 
4508  **/
4509 void
4510 gdk_window_set_functions (GdkWindow    *window,
4511                           GdkWMFunction functions)
4512 {
4513   MotifWmHints hints;
4514   
4515   g_return_if_fail (GDK_IS_WINDOW (window));
4516
4517   if (GDK_WINDOW_DESTROYED (window) ||
4518       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
4519     return;
4520   
4521   /* initialize to zero to avoid writing uninitialized data to socket */
4522   memset(&hints, 0, sizeof(hints));
4523   hints.flags = MWM_HINTS_FUNCTIONS;
4524   hints.functions = functions;
4525   
4526   gdk_window_set_mwm_hints (window, &hints);
4527 }
4528
4529 cairo_region_t *
4530 _xwindow_get_shape (Display *xdisplay,
4531                     Window window,
4532                     gint shape_type)
4533 {
4534   cairo_region_t *shape;
4535   GdkRectangle *rl;
4536   XRectangle *xrl;
4537   gint rn, ord, i;
4538
4539   shape = NULL;
4540   rn = 0;
4541
4542   xrl = XShapeGetRectangles (xdisplay,
4543                              window,
4544                              shape_type, &rn, &ord);
4545
4546   if (xrl == NULL || rn == 0)
4547     return cairo_region_create (); /* Empty */
4548
4549   if (ord != YXBanded)
4550     {
4551       /* This really shouldn't happen with any xserver, as they
4552          generally convert regions to YXBanded internally */
4553       g_warning ("non YXBanded shape masks not supported");
4554       XFree (xrl);
4555       return NULL;
4556     }
4557
4558   rl = g_new (GdkRectangle, rn);
4559   for (i = 0; i < rn; i++)
4560     {
4561       rl[i].x = xrl[i].x;
4562       rl[i].y = xrl[i].y;
4563       rl[i].width = xrl[i].width;
4564       rl[i].height = xrl[i].height;
4565     }
4566   XFree (xrl);
4567   
4568   shape = cairo_region_create_rectangles (rl, rn);
4569   g_free (rl);
4570   
4571   return shape;
4572 }
4573
4574
4575 cairo_region_t *
4576 _gdk_windowing_window_get_shape (GdkWindow *window)
4577 {
4578   if (!GDK_WINDOW_DESTROYED (window) &&
4579       gdk_display_supports_shapes (GDK_WINDOW_DISPLAY (window)))
4580     return _xwindow_get_shape (GDK_WINDOW_XDISPLAY (window),
4581                               GDK_WINDOW_XID (window), ShapeBounding);
4582
4583   return NULL;
4584 }
4585
4586 cairo_region_t *
4587 _gdk_windowing_window_get_input_shape (GdkWindow *window)
4588 {
4589 #if defined(ShapeInput)
4590   if (!GDK_WINDOW_DESTROYED (window) &&
4591       gdk_display_supports_shapes (GDK_WINDOW_DISPLAY (window)))
4592     return _xwindow_get_shape (GDK_WINDOW_XDISPLAY (window),
4593                               GDK_WINDOW_XID (window),
4594                               ShapeInput);
4595 #endif
4596
4597   return NULL;
4598 }
4599
4600 static void
4601 gdk_window_set_static_bit_gravity (GdkWindow *window,
4602                                    gboolean   on)
4603 {
4604   XSetWindowAttributes xattributes;
4605   guint xattributes_mask = 0;
4606   
4607   g_return_if_fail (GDK_IS_WINDOW (window));
4608
4609   if (window->input_only)
4610     return;
4611   
4612   xattributes.bit_gravity = StaticGravity;
4613   xattributes_mask |= CWBitGravity;
4614   xattributes.bit_gravity = on ? StaticGravity : ForgetGravity;
4615   XChangeWindowAttributes (GDK_WINDOW_XDISPLAY (window),
4616                            GDK_WINDOW_XID (window),
4617                            CWBitGravity,  &xattributes);
4618 }
4619
4620 static void
4621 gdk_window_set_static_win_gravity (GdkWindow *window,
4622                                    gboolean   on)
4623 {
4624   XSetWindowAttributes xattributes;
4625   
4626   g_return_if_fail (GDK_IS_WINDOW (window));
4627   
4628   xattributes.win_gravity = on ? StaticGravity : NorthWestGravity;
4629   
4630   XChangeWindowAttributes (GDK_WINDOW_XDISPLAY (window),
4631                            GDK_WINDOW_XID (window),
4632                            CWWinGravity,  &xattributes);
4633 }
4634
4635 static gboolean
4636 gdk_window_x11_set_static_gravities (GdkWindow *window,
4637                                      gboolean   use_static)
4638 {
4639   GList *tmp_list;
4640   
4641   if (!use_static == !window->guffaw_gravity)
4642     return TRUE;
4643
4644   window->guffaw_gravity = use_static;
4645   
4646   if (!GDK_WINDOW_DESTROYED (window))
4647     {
4648       gdk_window_set_static_bit_gravity (window, use_static);
4649       
4650       tmp_list = window->children;
4651       while (tmp_list)
4652         {
4653           gdk_window_set_static_win_gravity (tmp_list->data, use_static);
4654           
4655           tmp_list = tmp_list->next;
4656         }
4657     }
4658   
4659   return TRUE;
4660 }
4661
4662 static void
4663 wmspec_moveresize (GdkWindow *window,
4664                    gint       direction,
4665                    gint       root_x,
4666                    gint       root_y,
4667                    guint32    timestamp)     
4668 {
4669   GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
4670   
4671   XClientMessageEvent xclient;
4672
4673   /* Release passive grab */
4674   gdk_display_pointer_ungrab (display, timestamp);
4675
4676   memset (&xclient, 0, sizeof (xclient));
4677   xclient.type = ClientMessage;
4678   xclient.window = GDK_WINDOW_XID (window);
4679   xclient.message_type =
4680     gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_MOVERESIZE");
4681   xclient.format = 32;
4682   xclient.data.l[0] = root_x;
4683   xclient.data.l[1] = root_y;
4684   xclient.data.l[2] = direction;
4685   xclient.data.l[3] = 0;
4686   xclient.data.l[4] = 0;
4687   
4688   XSendEvent (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XROOTWIN (window), False,
4689               SubstructureRedirectMask | SubstructureNotifyMask,
4690               (XEvent *)&xclient);
4691 }
4692
4693 typedef struct _MoveResizeData MoveResizeData;
4694
4695 struct _MoveResizeData
4696 {
4697   GdkDisplay *display;
4698   
4699   GdkWindow *moveresize_window;
4700   GdkWindow *moveresize_emulation_window;
4701   gboolean is_resize;
4702   GdkWindowEdge resize_edge;
4703   gint moveresize_button;
4704   gint moveresize_x;
4705   gint moveresize_y;
4706   gint moveresize_orig_x;
4707   gint moveresize_orig_y;
4708   gint moveresize_orig_width;
4709   gint moveresize_orig_height;
4710   GdkWindowHints moveresize_geom_mask;
4711   GdkGeometry moveresize_geometry;
4712   Time moveresize_process_time;
4713   XEvent *moveresize_pending_event;
4714 };
4715
4716 /* From the WM spec */
4717 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT      0
4718 #define _NET_WM_MOVERESIZE_SIZE_TOP          1
4719 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT     2
4720 #define _NET_WM_MOVERESIZE_SIZE_RIGHT        3
4721 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT  4
4722 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM       5
4723 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT   6
4724 #define _NET_WM_MOVERESIZE_SIZE_LEFT         7
4725 #define _NET_WM_MOVERESIZE_MOVE              8
4726
4727 static void
4728 wmspec_resize_drag (GdkWindow     *window,
4729                     GdkWindowEdge  edge,
4730                     gint           button,
4731                     gint           root_x,
4732                     gint           root_y,
4733                     guint32        timestamp)
4734 {
4735   gint direction;
4736   
4737   /* Let the compiler turn a switch into a table, instead
4738    * of doing the table manually, this way is easier to verify.
4739    */
4740   switch (edge)
4741     {
4742     case GDK_WINDOW_EDGE_NORTH_WEST:
4743       direction = _NET_WM_MOVERESIZE_SIZE_TOPLEFT;
4744       break;
4745
4746     case GDK_WINDOW_EDGE_NORTH:
4747       direction = _NET_WM_MOVERESIZE_SIZE_TOP;
4748       break;
4749
4750     case GDK_WINDOW_EDGE_NORTH_EAST:
4751       direction = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT;
4752       break;
4753
4754     case GDK_WINDOW_EDGE_WEST:
4755       direction = _NET_WM_MOVERESIZE_SIZE_LEFT;
4756       break;
4757
4758     case GDK_WINDOW_EDGE_EAST:
4759       direction = _NET_WM_MOVERESIZE_SIZE_RIGHT;
4760       break;
4761
4762     case GDK_WINDOW_EDGE_SOUTH_WEST:
4763       direction = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT;
4764       break;
4765
4766     case GDK_WINDOW_EDGE_SOUTH:
4767       direction = _NET_WM_MOVERESIZE_SIZE_BOTTOM;
4768       break;
4769
4770     case GDK_WINDOW_EDGE_SOUTH_EAST:
4771       direction = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT;
4772       break;
4773
4774     default:
4775       g_warning ("gdk_window_begin_resize_drag: bad resize edge %d!",
4776                  edge);
4777       return;
4778     }
4779   
4780   wmspec_moveresize (window, direction, root_x, root_y, timestamp);
4781 }
4782
4783 static MoveResizeData *
4784 get_move_resize_data (GdkDisplay *display,
4785                       gboolean    create)
4786 {
4787   MoveResizeData *mv_resize;
4788   static GQuark move_resize_quark = 0;
4789
4790   if (!move_resize_quark)
4791     move_resize_quark = g_quark_from_static_string ("gdk-window-moveresize");
4792   
4793   mv_resize = g_object_get_qdata (G_OBJECT (display), move_resize_quark);
4794
4795   if (!mv_resize && create)
4796     {
4797       mv_resize = g_new0 (MoveResizeData, 1);
4798       mv_resize->display = display;
4799       
4800       g_object_set_qdata (G_OBJECT (display), move_resize_quark, mv_resize);
4801     }
4802
4803   return mv_resize;
4804 }
4805
4806 static void
4807 update_pos (MoveResizeData *mv_resize,
4808             gint            new_root_x,
4809             gint            new_root_y)
4810 {
4811   gint dx, dy;
4812
4813   dx = new_root_x - mv_resize->moveresize_x;
4814   dy = new_root_y - mv_resize->moveresize_y;
4815
4816   if (mv_resize->is_resize)
4817     {
4818       gint x, y, w, h;
4819
4820       x = mv_resize->moveresize_orig_x;
4821       y = mv_resize->moveresize_orig_y;
4822
4823       w = mv_resize->moveresize_orig_width;
4824       h = mv_resize->moveresize_orig_height;
4825
4826       switch (mv_resize->resize_edge)
4827         {
4828         case GDK_WINDOW_EDGE_NORTH_WEST:
4829           x += dx;
4830           y += dy;
4831           w -= dx;
4832           h -= dy;
4833           break;
4834         case GDK_WINDOW_EDGE_NORTH:
4835           y += dy;
4836           h -= dy;
4837           break;
4838         case GDK_WINDOW_EDGE_NORTH_EAST:
4839           y += dy;
4840           h -= dy;
4841           w += dx;
4842           break;
4843         case GDK_WINDOW_EDGE_SOUTH_WEST:
4844           h += dy;
4845           x += dx;
4846           w -= dx;
4847           break;
4848         case GDK_WINDOW_EDGE_SOUTH_EAST:
4849           w += dx;
4850           h += dy;
4851           break;
4852         case GDK_WINDOW_EDGE_SOUTH:
4853           h += dy;
4854           break;
4855         case GDK_WINDOW_EDGE_EAST:
4856           w += dx;
4857           break;
4858         case GDK_WINDOW_EDGE_WEST:
4859           x += dx;
4860           w -= dx;
4861           break;
4862         }
4863
4864       x = MAX (x, 0);
4865       y = MAX (y, 0);
4866       w = MAX (w, 1);
4867       h = MAX (h, 1);
4868
4869       if (mv_resize->moveresize_geom_mask)
4870         {
4871           gdk_window_constrain_size (&mv_resize->moveresize_geometry,
4872                                      mv_resize->moveresize_geom_mask,
4873                                      w, h, &w, &h);
4874         }
4875
4876       gdk_window_move_resize (mv_resize->moveresize_window, x, y, w, h);
4877     }
4878   else
4879     {
4880       gint x, y;
4881
4882       x = mv_resize->moveresize_orig_x + dx;
4883       y = mv_resize->moveresize_orig_y + dy;
4884
4885       gdk_window_move (mv_resize->moveresize_window, x, y);
4886     }
4887 }
4888
4889 static void
4890 finish_drag (MoveResizeData *mv_resize)
4891 {
4892   gdk_window_destroy (mv_resize->moveresize_emulation_window);
4893   mv_resize->moveresize_emulation_window = NULL;
4894   g_object_unref (mv_resize->moveresize_window);
4895   mv_resize->moveresize_window = NULL;
4896
4897   if (mv_resize->moveresize_pending_event)
4898     {
4899       g_free (mv_resize->moveresize_pending_event);
4900       mv_resize->moveresize_pending_event = NULL;
4901     }
4902 }
4903
4904 static int
4905 lookahead_motion_predicate (Display *xdisplay,
4906                             XEvent  *event,
4907                             XPointer arg)
4908 {
4909   gboolean *seen_release = (gboolean *)arg;
4910   GdkDisplay *display = gdk_x11_lookup_xdisplay (xdisplay);
4911   MoveResizeData *mv_resize = get_move_resize_data (display, FALSE);
4912
4913   if (*seen_release)
4914     return False;
4915
4916   switch (event->xany.type)
4917     {
4918     case ButtonRelease:
4919       *seen_release = TRUE;
4920       break;
4921     case MotionNotify:
4922       mv_resize->moveresize_process_time = event->xmotion.time;
4923       break;
4924     default:
4925       break;
4926     }
4927
4928   return False;
4929 }
4930
4931 static gboolean
4932 moveresize_lookahead (MoveResizeData *mv_resize,
4933                       XEvent         *event)
4934 {
4935   XEvent tmp_event;
4936   gboolean seen_release = FALSE;
4937
4938   if (mv_resize->moveresize_process_time)
4939     {
4940       if (event->xmotion.time == mv_resize->moveresize_process_time)
4941         {
4942           mv_resize->moveresize_process_time = 0;
4943           return TRUE;
4944         }
4945       else
4946         return FALSE;
4947     }
4948
4949   XCheckIfEvent (event->xany.display, &tmp_event,
4950                  lookahead_motion_predicate, (XPointer) & seen_release);
4951
4952   return mv_resize->moveresize_process_time == 0;
4953 }
4954         
4955 gboolean
4956 _gdk_moveresize_handle_event (XEvent *event)
4957 {
4958   guint button_mask = 0;
4959   GdkDisplay *display = gdk_x11_lookup_xdisplay (event->xany.display);
4960   MoveResizeData *mv_resize = get_move_resize_data (display, FALSE);
4961
4962   if (!mv_resize || !mv_resize->moveresize_window)
4963     return FALSE;
4964
4965   button_mask = GDK_BUTTON1_MASK << (mv_resize->moveresize_button - 1);
4966
4967   switch (event->xany.type)
4968     {
4969     case MotionNotify:
4970       if (mv_resize->moveresize_window->resize_count > 0)
4971         {
4972           if (mv_resize->moveresize_pending_event)
4973             *mv_resize->moveresize_pending_event = *event;
4974           else
4975             mv_resize->moveresize_pending_event =
4976               g_memdup (event, sizeof (XEvent));
4977
4978           break;
4979         }
4980       if (!moveresize_lookahead (mv_resize, event))
4981         break;
4982
4983       update_pos (mv_resize,
4984                   event->xmotion.x_root,
4985                   event->xmotion.y_root);
4986
4987       /* This should never be triggered in normal cases, but in the
4988        * case where the drag started without an implicit grab being
4989        * in effect, we could miss the release if it occurs before
4990        * we grab the pointer; this ensures that we will never
4991        * get a permanently stuck grab.
4992        */
4993       if ((event->xmotion.state & button_mask) == 0)
4994         finish_drag (mv_resize);
4995       break;
4996
4997     case ButtonRelease:
4998       update_pos (mv_resize,
4999                   event->xbutton.x_root,
5000                   event->xbutton.y_root);
5001
5002       if (event->xbutton.button == mv_resize->moveresize_button)
5003         finish_drag (mv_resize);
5004       break;
5005     }
5006   return TRUE;
5007 }
5008
5009 gboolean 
5010 _gdk_moveresize_configure_done (GdkDisplay *display,
5011                                 GdkWindow  *window)
5012 {
5013   XEvent *tmp_event;
5014   MoveResizeData *mv_resize = get_move_resize_data (display, FALSE);
5015   
5016   if (!mv_resize || window != mv_resize->moveresize_window)
5017     return FALSE;
5018
5019   if (mv_resize->moveresize_pending_event)
5020     {
5021       tmp_event = mv_resize->moveresize_pending_event;
5022       mv_resize->moveresize_pending_event = NULL;
5023       _gdk_moveresize_handle_event (tmp_event);
5024       g_free (tmp_event);
5025     }
5026   
5027   return TRUE;
5028 }
5029
5030 static void
5031 create_moveresize_window (MoveResizeData *mv_resize,
5032                           guint32         timestamp)
5033 {
5034   GdkWindowAttr attributes;
5035   gint attributes_mask;
5036   GdkGrabStatus status;
5037
5038   g_assert (mv_resize->moveresize_emulation_window == NULL);
5039
5040   attributes.x = -100;
5041   attributes.y = -100;
5042   attributes.width = 10;
5043   attributes.height = 10;
5044   attributes.window_type = GDK_WINDOW_TEMP;
5045   attributes.wclass = GDK_INPUT_ONLY;
5046   attributes.override_redirect = TRUE;
5047   attributes.event_mask = 0;
5048
5049   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5050
5051   mv_resize->moveresize_emulation_window = 
5052     gdk_window_new (gdk_screen_get_root_window (gdk_display_get_default_screen (mv_resize->display)),
5053                     &attributes,
5054                     attributes_mask);
5055
5056   gdk_window_show (mv_resize->moveresize_emulation_window);
5057
5058   status = gdk_pointer_grab (mv_resize->moveresize_emulation_window,
5059                              FALSE,
5060                              GDK_BUTTON_RELEASE_MASK |
5061                              GDK_POINTER_MOTION_MASK,
5062                              NULL,
5063                              NULL,
5064                              timestamp);
5065
5066   if (status != GDK_GRAB_SUCCESS)
5067     {
5068       /* If this fails, some other client has grabbed the window
5069        * already.
5070        */
5071       finish_drag (mv_resize);
5072     }
5073
5074   mv_resize->moveresize_process_time = 0;
5075 }
5076
5077 /* 
5078    Calculate mv_resize->moveresize_orig_x and mv_resize->moveresize_orig_y
5079    so that calling XMoveWindow with these coordinates will not move the 
5080    window.
5081    Note that this depends on the WM to implement ICCCM-compliant reference
5082    point handling.
5083 */
5084 static void 
5085 calculate_unmoving_origin (MoveResizeData *mv_resize)
5086 {
5087   GdkRectangle rect;
5088   gint width, height;
5089
5090   if (mv_resize->moveresize_geom_mask & GDK_HINT_WIN_GRAVITY &&
5091       mv_resize->moveresize_geometry.win_gravity == GDK_GRAVITY_STATIC)
5092     {
5093       gdk_window_get_origin (mv_resize->moveresize_window,
5094                              &mv_resize->moveresize_orig_x,
5095                              &mv_resize->moveresize_orig_y);
5096     }
5097   else
5098     {
5099       gdk_window_get_frame_extents (mv_resize->moveresize_window, &rect);
5100       gdk_window_get_geometry (mv_resize->moveresize_window, 
5101                                NULL, NULL, &width, &height, NULL);
5102       
5103       switch (mv_resize->moveresize_geometry.win_gravity) 
5104         {
5105         case GDK_GRAVITY_NORTH_WEST:
5106           mv_resize->moveresize_orig_x = rect.x;
5107           mv_resize->moveresize_orig_y = rect.y;
5108           break;
5109         case GDK_GRAVITY_NORTH:
5110           mv_resize->moveresize_orig_x = rect.x + rect.width / 2 - width / 2;
5111           mv_resize->moveresize_orig_y = rect.y;
5112           break;          
5113         case GDK_GRAVITY_NORTH_EAST:
5114           mv_resize->moveresize_orig_x = rect.x + rect.width - width;
5115           mv_resize->moveresize_orig_y = rect.y;
5116           break;
5117         case GDK_GRAVITY_WEST:
5118           mv_resize->moveresize_orig_x = rect.x;
5119           mv_resize->moveresize_orig_y = rect.y + rect.height / 2 - height / 2;
5120           break;
5121         case GDK_GRAVITY_CENTER:
5122           mv_resize->moveresize_orig_x = rect.x + rect.width / 2 - width / 2;
5123           mv_resize->moveresize_orig_y = rect.y + rect.height / 2 - height / 2;
5124           break;
5125         case GDK_GRAVITY_EAST:
5126           mv_resize->moveresize_orig_x = rect.x + rect.width - width;
5127           mv_resize->moveresize_orig_y = rect.y + rect.height / 2 - height / 2;
5128           break;
5129         case GDK_GRAVITY_SOUTH_WEST:
5130           mv_resize->moveresize_orig_x = rect.x;
5131           mv_resize->moveresize_orig_y = rect.y + rect.height - height;
5132           break;
5133         case GDK_GRAVITY_SOUTH:
5134           mv_resize->moveresize_orig_x = rect.x + rect.width / 2 - width / 2;
5135           mv_resize->moveresize_orig_y = rect.y + rect.height - height;
5136           break;
5137         case GDK_GRAVITY_SOUTH_EAST:
5138           mv_resize->moveresize_orig_x = rect.x + rect.width - width;
5139           mv_resize->moveresize_orig_y = rect.y + rect.height - height;
5140           break;
5141         default:
5142           mv_resize->moveresize_orig_x = rect.x;
5143           mv_resize->moveresize_orig_y = rect.y;
5144           break; 
5145         }
5146     }  
5147 }
5148
5149 static void
5150 emulate_resize_drag (GdkWindow     *window,
5151                      GdkWindowEdge  edge,
5152                      gint           button,
5153                      gint           root_x,
5154                      gint           root_y,
5155                      guint32        timestamp)
5156 {
5157   MoveResizeData *mv_resize = get_move_resize_data (GDK_WINDOW_DISPLAY (window), TRUE);
5158
5159   mv_resize->is_resize = TRUE;
5160   mv_resize->moveresize_button = button;
5161   mv_resize->resize_edge = edge;
5162   mv_resize->moveresize_x = root_x;
5163   mv_resize->moveresize_y = root_y;
5164   mv_resize->moveresize_window = g_object_ref (window);
5165
5166   mv_resize->moveresize_orig_width = gdk_window_get_width (window);
5167   mv_resize->moveresize_orig_height = gdk_window_get_height (window);
5168
5169   mv_resize->moveresize_geom_mask = 0;
5170   gdk_window_get_geometry_hints (window,
5171                                  &mv_resize->moveresize_geometry,
5172                                  &mv_resize->moveresize_geom_mask);
5173
5174   calculate_unmoving_origin (mv_resize);
5175
5176   create_moveresize_window (mv_resize, timestamp);
5177 }
5178
5179 static void
5180 emulate_move_drag (GdkWindow     *window,
5181                    gint           button,
5182                    gint           root_x,
5183                    gint           root_y,
5184                    guint32        timestamp)
5185 {
5186   MoveResizeData *mv_resize = get_move_resize_data (GDK_WINDOW_DISPLAY (window), TRUE);
5187   
5188   mv_resize->is_resize = FALSE;
5189   mv_resize->moveresize_button = button;
5190   mv_resize->moveresize_x = root_x;
5191   mv_resize->moveresize_y = root_y;
5192
5193   mv_resize->moveresize_window = g_object_ref (window);
5194
5195   calculate_unmoving_origin (mv_resize);
5196
5197   create_moveresize_window (mv_resize, timestamp);
5198 }
5199
5200 /**
5201  * gdk_window_begin_resize_drag:
5202  * @window: a toplevel #GdkWindow
5203  * @edge: the edge or corner from which the drag is started
5204  * @button: the button being used to drag
5205  * @root_x: root window X coordinate of mouse click that began the drag
5206  * @root_y: root window Y coordinate of mouse click that began the drag
5207  * @timestamp: timestamp of mouse click that began the drag (use gdk_event_get_time())
5208  *
5209  * Begins a window resize operation (for a toplevel window).
5210  * You might use this function to implement a "window resize grip," for
5211  * example; in fact #GtkStatusbar uses it. The function works best
5212  * with window managers that support the <ulink url="http://www.freedesktop.org/Standards/wm-spec">Extended Window Manager Hints</ulink>, but has a 
5213  * fallback implementation for other window managers.
5214  * 
5215  **/
5216 void
5217 gdk_window_begin_resize_drag (GdkWindow     *window,
5218                               GdkWindowEdge  edge,
5219                               gint           button,
5220                               gint           root_x,
5221                               gint           root_y,
5222                               guint32        timestamp)
5223 {
5224   if (GDK_WINDOW_DESTROYED (window) ||
5225       !WINDOW_IS_TOPLEVEL_OR_FOREIGN (window))
5226     return;
5227
5228   if (gdk_x11_screen_supports_net_wm_hint (GDK_WINDOW_SCREEN (window),
5229                                            gdk_atom_intern_static_string ("_NET_WM_MOVERESIZE")))
5230     wmspec_resize_drag (window, edge, button, root_x, root_y, timestamp);
5231   else
5232     emulate_resize_drag (window, edge, button, root_x, root_y, timestamp);
5233 }
5234
5235 /**
5236  * gdk_window_begin_move_drag:
5237  * @window: a toplevel #GdkWindow
5238  * @button: the button being used to drag
5239  * @root_x: root window X coordinate of mouse click that began the drag
5240  * @root_y: root window Y coordinate of mouse click that began the drag
5241  * @timestamp: timestamp of mouse click that began the drag
5242  *
5243  * Begins a window move operation (for a toplevel window).  You might
5244  * use this function to implement a "window move grip," for
5245  * example. The function works best with window managers that support
5246  * the <ulink url="http://www.freedesktop.org/Standards/wm-spec">Extended 
5247  * Window Manager Hints</ulink>, but has a fallback implementation for
5248  * other window managers.
5249  * 
5250  **/
5251 void
5252 gdk_window_begin_move_drag (GdkWindow *window,
5253                             gint       button,
5254                             gint       root_x,
5255                             gint       root_y,
5256                             guint32    timestamp)
5257 {
5258   if (GDK_WINDOW_DESTROYED (window) ||
5259       !WINDOW_IS_TOPLEVEL (window))
5260     return;
5261
5262   if (gdk_x11_screen_supports_net_wm_hint (GDK_WINDOW_SCREEN (window),
5263                                            gdk_atom_intern_static_string ("_NET_WM_MOVERESIZE")))
5264     wmspec_moveresize (window, _NET_WM_MOVERESIZE_MOVE, root_x, root_y,
5265                        timestamp);
5266   else
5267     emulate_move_drag (window, button, root_x, root_y, timestamp);
5268 }
5269
5270 /**
5271  * gdk_window_enable_synchronized_configure:
5272  * @window: a toplevel #GdkWindow
5273  * 
5274  * Indicates that the application will cooperate with the window
5275  * system in synchronizing the window repaint with the window
5276  * manager during resizing operations. After an application calls
5277  * this function, it must call gdk_window_configure_finished() every
5278  * time it has finished all processing associated with a set of
5279  * Configure events. Toplevel GTK+ windows automatically use this
5280  * protocol.
5281  * 
5282  * On X, calling this function makes @window participate in the
5283  * _NET_WM_SYNC_REQUEST window manager protocol.
5284  * 
5285  * Since: 2.6
5286  **/
5287 void
5288 gdk_window_enable_synchronized_configure (GdkWindow *window)
5289 {
5290   GdkWindowImplX11 *impl;
5291
5292   if (!GDK_IS_WINDOW_IMPL_X11 (window->impl))
5293     return;
5294   
5295   impl = GDK_WINDOW_IMPL_X11 (window->impl);
5296           
5297   if (!impl->use_synchronized_configure)
5298     {
5299       /* This basically means you want to do fancy X specific stuff, so
5300          ensure we have a native window */
5301       gdk_window_ensure_native (window);
5302
5303       impl->use_synchronized_configure = TRUE;
5304       ensure_sync_counter (window);
5305     }
5306 }
5307
5308 /**
5309  * gdk_window_configure_finished:
5310  * @window: a toplevel #GdkWindow
5311  * 
5312  * Signal to the window system that the application has finished
5313  * handling Configure events it has received. Window Managers can
5314  * use this to better synchronize the frame repaint with the
5315  * application. GTK+ applications will automatically call this
5316  * function when appropriate.
5317  *
5318  * This function can only be called if gdk_window_enable_synchronized_configure()
5319  * was called previously.
5320  *
5321  * Since: 2.6
5322  **/
5323 void
5324 gdk_window_configure_finished (GdkWindow *window)
5325 {
5326   GdkWindowImplX11 *impl;
5327
5328   if (!WINDOW_IS_TOPLEVEL (window))
5329     return;
5330   
5331   impl = GDK_WINDOW_IMPL_X11 (window->impl);
5332   if (!impl->use_synchronized_configure)
5333     return;
5334   
5335 #ifdef HAVE_XSYNC
5336   if (!GDK_WINDOW_DESTROYED (window))
5337     {
5338       GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
5339       GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
5340
5341       if (toplevel && toplevel->update_counter != None &&
5342           GDK_DISPLAY_X11 (display)->use_sync &&
5343           !XSyncValueIsZero (toplevel->current_counter_value))
5344         {
5345           XSyncSetCounter (GDK_WINDOW_XDISPLAY (window), 
5346                            toplevel->update_counter,
5347                            toplevel->current_counter_value);
5348           
5349           XSyncIntToValue (&toplevel->current_counter_value, 0);
5350         }
5351     }
5352 #endif
5353 }
5354
5355 void
5356 _gdk_windowing_window_beep (GdkWindow *window)
5357 {
5358   GdkDisplay *display;
5359
5360   g_return_if_fail (GDK_IS_WINDOW (window));
5361
5362   display = GDK_WINDOW_DISPLAY (window);
5363
5364 #ifdef HAVE_XKB
5365   if (GDK_DISPLAY_X11 (display)->use_xkb)
5366     XkbBell (GDK_DISPLAY_XDISPLAY (display),
5367              GDK_WINDOW_XID (window),
5368              0,
5369              None);
5370   else
5371 #endif
5372     gdk_display_beep (display);
5373 }
5374
5375 /**
5376  * gdk_window_set_opacity:
5377  * @window: a top-level #GdkWindow
5378  * @opacity: opacity
5379  *
5380  * Request the windowing system to make @window partially transparent,
5381  * with opacity 0 being fully transparent and 1 fully opaque. (Values
5382  * of the opacity parameter are clamped to the [0,1] range.) 
5383  *
5384  * On X11, this works only on X screens with a compositing manager 
5385  * running.
5386  *
5387  * For setting up per-pixel alpha, see gdk_screen_get_rgba_visual().
5388  * For making non-toplevel windows translucent, see 
5389  * gdk_window_set_composited().
5390  *
5391  * Since: 2.12
5392  */
5393 void
5394 gdk_window_set_opacity (GdkWindow *window,
5395                         gdouble    opacity)
5396 {
5397   GdkDisplay *display;
5398   guint32 cardinal;
5399   
5400   g_return_if_fail (GDK_IS_WINDOW (window));
5401
5402   if (GDK_WINDOW_DESTROYED (window) ||
5403       !WINDOW_IS_TOPLEVEL (window))
5404     return;
5405
5406   display = gdk_window_get_display (window);
5407
5408   if (opacity < 0)
5409     opacity = 0;
5410   else if (opacity > 1)
5411     opacity = 1;
5412
5413   cardinal = opacity * 0xffffffff;
5414
5415   if (cardinal == 0xffffffff)
5416     XDeleteProperty (GDK_DISPLAY_XDISPLAY (display),
5417                      GDK_WINDOW_XID (window),
5418                      gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_OPACITY"));
5419   else
5420     XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
5421                      GDK_WINDOW_XID (window),
5422                      gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_OPACITY"),
5423                      XA_CARDINAL, 32,
5424                      PropModeReplace,
5425                      (guchar *) &cardinal, 1);
5426 }
5427
5428 void
5429 _gdk_windowing_window_set_composited (GdkWindow *window,
5430                                       gboolean   composited)
5431 {
5432 #if defined(HAVE_XCOMPOSITE) && defined(HAVE_XDAMAGE) && defined (HAVE_XFIXES)
5433   GdkWindowImplX11 *impl;
5434   GdkDisplay *display;
5435   Display *dpy;
5436   Window xid;
5437
5438   impl = GDK_WINDOW_IMPL_X11 (window->impl);
5439
5440   display = gdk_window_get_display (window);
5441   dpy = GDK_DISPLAY_XDISPLAY (display);
5442   xid = GDK_WINDOW_XID (window);
5443
5444   if (composited)
5445     {
5446       XCompositeRedirectWindow (dpy, xid, CompositeRedirectManual);
5447       impl->damage = XDamageCreate (dpy, xid, XDamageReportBoundingBox);
5448     }
5449   else
5450     {
5451       XCompositeUnredirectWindow (dpy, xid, CompositeRedirectManual);
5452       XDamageDestroy (dpy, impl->damage);
5453       impl->damage = None;
5454     }
5455 #endif
5456 }
5457
5458 void
5459 _gdk_windowing_window_process_updates_recurse (GdkWindow *window,
5460                                                cairo_region_t *region)
5461 {
5462   _gdk_window_process_updates_recurse (window, region);
5463 }
5464
5465 void
5466 _gdk_windowing_before_process_all_updates (void)
5467 {
5468 }
5469
5470 void
5471 _gdk_windowing_after_process_all_updates (void)
5472 {
5473 }
5474
5475 static Bool
5476 timestamp_predicate (Display *display,
5477                      XEvent  *xevent,
5478                      XPointer arg)
5479 {
5480   Window xwindow = GPOINTER_TO_UINT (arg);
5481   GdkDisplay *gdk_display = gdk_x11_lookup_xdisplay (display);
5482
5483   if (xevent->type == PropertyNotify &&
5484       xevent->xproperty.window == xwindow &&
5485       xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (gdk_display,
5486                                                                        "GDK_TIMESTAMP_PROP"))
5487     return True;
5488
5489   return False;
5490 }
5491
5492 /**
5493  * gdk_x11_get_server_time:
5494  * @window: a #GdkWindow, used for communication with the server.
5495  *          The window must have GDK_PROPERTY_CHANGE_MASK in its
5496  *          events mask or a hang will result.
5497  *
5498  * Routine to get the current X server time stamp.
5499  *
5500  * Return value: the time stamp.
5501  **/
5502 guint32
5503 gdk_x11_get_server_time (GdkWindow *window)
5504 {
5505   Display *xdisplay;
5506   Window   xwindow;
5507   guchar c = 'a';
5508   XEvent xevent;
5509   Atom timestamp_prop_atom;
5510
5511   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
5512   g_return_val_if_fail (!GDK_WINDOW_DESTROYED (window), 0);
5513
5514   xdisplay = GDK_WINDOW_XDISPLAY (window);
5515   xwindow = GDK_WINDOW_XID (window);
5516   timestamp_prop_atom =
5517     gdk_x11_get_xatom_by_name_for_display (GDK_WINDOW_DISPLAY (window),
5518                                            "GDK_TIMESTAMP_PROP");
5519
5520   XChangeProperty (xdisplay, xwindow, timestamp_prop_atom,
5521                    timestamp_prop_atom,
5522                    8, PropModeReplace, &c, 1);
5523
5524   XIfEvent (xdisplay, &xevent,
5525             timestamp_predicate, GUINT_TO_POINTER(xwindow));
5526
5527   return xevent.xproperty.time;
5528 }
5529
5530 /**
5531  * gdk_x11_window_get_xid:
5532  * @window: a native #GdkWindow.
5533  * 
5534  * Returns the X resource (window) belonging to a #GdkWindow.
5535  * 
5536  * Return value: the ID of @drawable's X resource.
5537  **/
5538 XID
5539 gdk_x11_window_get_xid (GdkWindow *window)
5540 {
5541   GdkDrawable *impl;
5542       
5543   /* Try to ensure the window has a native window */
5544   if (!_gdk_window_has_impl (window))
5545     {
5546       gdk_window_ensure_native (window);
5547
5548       /* We sync here to ensure the window is created in the Xserver when
5549        * this function returns. This is required because the returned XID
5550        * for this window must be valid immediately, even with another
5551        * connection to the Xserver */
5552       gdk_display_sync (gdk_window_get_display (window));
5553     }
5554   
5555   if (!GDK_WINDOW_IS_X11 (window))
5556     {
5557       g_warning (G_STRLOC " drawable is not a native X11 window");
5558       return None;
5559     }
5560   
5561   impl = window->impl;
5562
5563   return ((GdkWindowImplX11 *)impl)->xid;
5564 }
5565
5566 static void
5567 gdk_window_impl_x11_class_init (GdkWindowImplX11Class *klass)
5568 {
5569   GObjectClass *object_class = G_OBJECT_CLASS (klass);
5570   GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
5571   GdkWindowImplClass *impl_class = GDK_WINDOW_IMPL_CLASS (klass);
5572   
5573   object_class->finalize = gdk_window_impl_x11_finalize;
5574   
5575   drawable_class->ref_cairo_surface = gdk_x11_ref_cairo_surface;
5576
5577   impl_class->show = gdk_window_x11_show;
5578   impl_class->hide = gdk_window_x11_hide;
5579   impl_class->withdraw = gdk_window_x11_withdraw;
5580   impl_class->set_events = gdk_window_x11_set_events;
5581   impl_class->get_events = gdk_window_x11_get_events;
5582   impl_class->raise = gdk_window_x11_raise;
5583   impl_class->lower = gdk_window_x11_lower;
5584   impl_class->restack_under = gdk_window_x11_restack_under;
5585   impl_class->restack_toplevel = gdk_window_x11_restack_toplevel;
5586   impl_class->move_resize = gdk_window_x11_move_resize;
5587   impl_class->set_background = gdk_window_x11_set_background;
5588   impl_class->reparent = gdk_window_x11_reparent;
5589   impl_class->set_device_cursor = gdk_window_x11_set_device_cursor;
5590   impl_class->get_geometry = gdk_window_x11_get_geometry;
5591   impl_class->get_root_coords = gdk_window_x11_get_root_coords;
5592   impl_class->get_device_state = gdk_window_x11_get_device_state;
5593   impl_class->shape_combine_region = gdk_window_x11_shape_combine_region;
5594   impl_class->input_shape_combine_region = gdk_window_x11_input_shape_combine_region;
5595   impl_class->set_static_gravities = gdk_window_x11_set_static_gravities;
5596   impl_class->queue_antiexpose = _gdk_x11_window_queue_antiexpose;
5597   impl_class->translate = _gdk_x11_window_translate;
5598   impl_class->destroy = _gdk_x11_window_destroy;
5599   impl_class->resize_cairo_surface = gdk_window_x11_resize_cairo_surface;
5600 }
5601