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