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