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