]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkwindow-x11.c
new function for mapping a window without fooling with stacking order, but
[~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 "gdk.h"
32 #include "config.h"
33
34 #include "gdkwindow.h"
35 #include "gdkinputprivate.h"
36 #include "gdkprivate-x11.h"
37 #include "gdkregion.h"
38 #include "gdkinternals.h"
39 #include "MwmUtil.h"
40 #include "gdkwindow-x11.h"
41
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45
46
47 #ifdef HAVE_SHAPE_EXT
48 #include <X11/extensions/shape.h>
49 #endif
50
51 const int gdk_event_mask_table[21] =
52 {
53   ExposureMask,
54   PointerMotionMask,
55   PointerMotionHintMask,
56   ButtonMotionMask,
57   Button1MotionMask,
58   Button2MotionMask,
59   Button3MotionMask,
60   ButtonPressMask,
61   ButtonReleaseMask,
62   KeyPressMask,
63   KeyReleaseMask,
64   EnterWindowMask,
65   LeaveWindowMask,
66   FocusChangeMask,
67   StructureNotifyMask,
68   PropertyChangeMask,
69   VisibilityChangeMask,
70   0,                            /* PROXIMITY_IN */
71   0,                            /* PROXIMTY_OUT */
72   SubstructureNotifyMask,
73   ButtonPressMask      /* SCROLL; on X mouse wheel events is treated as mouse button 4/5 */
74 };
75 const int gdk_nevent_masks = sizeof (gdk_event_mask_table) / sizeof (int);
76
77 /* Forward declarations */
78 static gboolean gdk_window_gravity_works          (void);
79 static void     gdk_window_set_static_win_gravity (GdkWindow *window,
80                                                    gboolean   on);
81 static gboolean gdk_window_have_shape_ext         (void);
82 static gboolean gdk_window_icon_name_set          (GdkWindow *window);
83
84 static GdkColormap* gdk_window_impl_x11_get_colormap (GdkDrawable *drawable);
85 static void         gdk_window_impl_x11_set_colormap (GdkDrawable *drawable,
86                                                       GdkColormap *cmap);
87 static void         gdk_window_impl_x11_get_size    (GdkDrawable *drawable,
88                                                      gint *width,
89                                                      gint *height);
90 static GdkRegion*  gdk_window_impl_x11_get_visible_region (GdkDrawable *drawable);
91 static void gdk_window_impl_x11_init       (GdkWindowImplX11      *window);
92 static void gdk_window_impl_x11_class_init (GdkWindowImplX11Class *klass);
93 static void gdk_window_impl_x11_finalize   (GObject            *object);
94
95 static gpointer parent_class = NULL;
96
97 GType
98 gdk_window_impl_x11_get_type (void)
99 {
100   static GType object_type = 0;
101
102   if (!object_type)
103     {
104       static const GTypeInfo object_info =
105       {
106         sizeof (GdkWindowImplX11Class),
107         (GBaseInitFunc) NULL,
108         (GBaseFinalizeFunc) NULL,
109         (GClassInitFunc) gdk_window_impl_x11_class_init,
110         NULL,           /* class_finalize */
111         NULL,           /* class_data */
112         sizeof (GdkWindowImplX11),
113         0,              /* n_preallocs */
114         (GInstanceInitFunc) gdk_window_impl_x11_init,
115       };
116       
117       object_type = g_type_register_static (GDK_TYPE_DRAWABLE_IMPL_X11,
118                                             "GdkWindowImplX11",
119                                             &object_info, 0);
120     }
121   
122   return object_type;
123 }
124
125 GType
126 _gdk_window_impl_get_type (void)
127 {
128   return gdk_window_impl_x11_get_type ();
129 }
130
131 static void
132 gdk_window_impl_x11_init (GdkWindowImplX11 *impl)
133 {
134   impl->width = 1;
135   impl->height = 1;
136 }
137
138 static void
139 gdk_window_impl_x11_class_init (GdkWindowImplX11Class *klass)
140 {
141   GObjectClass *object_class = G_OBJECT_CLASS (klass);
142   GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
143   
144   parent_class = g_type_class_peek_parent (klass);
145
146   object_class->finalize = gdk_window_impl_x11_finalize;
147
148   drawable_class->set_colormap = gdk_window_impl_x11_set_colormap;
149   drawable_class->get_colormap = gdk_window_impl_x11_get_colormap;
150   drawable_class->get_size = gdk_window_impl_x11_get_size;
151
152   /* Visible and clip regions are the same */
153   drawable_class->get_clip_region = gdk_window_impl_x11_get_visible_region;
154   drawable_class->get_visible_region = gdk_window_impl_x11_get_visible_region;
155 }
156
157 static void
158 gdk_window_impl_x11_finalize (GObject *object)
159 {
160   GdkWindowObject *wrapper;
161   GdkDrawableImplX11 *draw_impl;
162   GdkWindowImplX11 *window_impl;
163   
164   g_return_if_fail (GDK_IS_WINDOW_IMPL_X11 (object));
165
166   draw_impl = GDK_DRAWABLE_IMPL_X11 (object);
167   window_impl = GDK_WINDOW_IMPL_X11 (object);
168   
169   wrapper = (GdkWindowObject*) draw_impl->wrapper;
170
171   if (!GDK_WINDOW_DESTROYED (wrapper))
172     {
173       gdk_xid_table_remove (draw_impl->xid);
174       if (window_impl->focus_window)
175         gdk_xid_table_remove (window_impl->focus_window);
176     }
177
178   G_OBJECT_CLASS (parent_class)->finalize (object);
179 }
180
181 static GdkColormap*
182 gdk_window_impl_x11_get_colormap (GdkDrawable *drawable)
183 {
184   GdkDrawableImplX11 *drawable_impl;
185   GdkWindowImplX11 *window_impl;
186   
187   g_return_val_if_fail (GDK_IS_WINDOW_IMPL_X11 (drawable), NULL);
188
189   drawable_impl = GDK_DRAWABLE_IMPL_X11 (drawable);
190   window_impl = GDK_WINDOW_IMPL_X11 (drawable);
191
192   if (!((GdkWindowObject *) drawable_impl->wrapper)->input_only && 
193       drawable_impl->colormap == NULL)
194     {
195       XWindowAttributes window_attributes;
196       
197       XGetWindowAttributes (drawable_impl->xdisplay,
198                             drawable_impl->xid,
199                             &window_attributes);
200       drawable_impl->colormap =
201         gdk_colormap_lookup (window_attributes.colormap);
202     }
203   
204   return drawable_impl->colormap;
205 }
206
207 static void
208 gdk_window_impl_x11_set_colormap (GdkDrawable *drawable,
209                                   GdkColormap *cmap)
210 {
211   GdkWindowImplX11 *impl;
212   GdkDrawableImplX11 *draw_impl;
213   
214   g_return_if_fail (GDK_IS_WINDOW_IMPL_X11 (drawable));
215
216   impl = GDK_WINDOW_IMPL_X11 (drawable);
217   draw_impl = GDK_DRAWABLE_IMPL_X11 (drawable);
218
219   /* chain up */
220   GDK_DRAWABLE_CLASS (parent_class)->set_colormap (drawable, cmap);
221
222   if (cmap)
223     {
224       XSetWindowColormap (draw_impl->xdisplay,
225                           draw_impl->xid,
226                           GDK_COLORMAP_XCOLORMAP (cmap));
227       
228       if (((GdkWindowObject*)draw_impl->wrapper)->window_type !=
229           GDK_WINDOW_TOPLEVEL)
230         gdk_window_add_colormap_windows (GDK_WINDOW (draw_impl->wrapper));
231     }
232 }
233
234
235 static void
236 gdk_window_impl_x11_get_size (GdkDrawable *drawable,
237                               gint        *width,
238                               gint        *height)
239 {
240   g_return_if_fail (GDK_IS_WINDOW_IMPL_X11 (drawable));
241
242   if (width)
243     *width = GDK_WINDOW_IMPL_X11 (drawable)->width;
244   if (height)
245     *height = GDK_WINDOW_IMPL_X11 (drawable)->height;
246 }
247
248 static GdkRegion*
249 gdk_window_impl_x11_get_visible_region (GdkDrawable *drawable)
250 {
251   GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (drawable);
252   GdkRectangle result_rect;
253
254   result_rect.x = 0;
255   result_rect.y = 0;
256   result_rect.width = impl->width;
257   result_rect.height = impl->height;
258
259   gdk_rectangle_intersect (&result_rect, &impl->position_info.clip_rect, &result_rect);
260
261   return gdk_region_rectangle (&result_rect);
262 }
263
264 void
265 _gdk_windowing_window_init (void)
266 {
267   GdkWindowObject *private;
268   GdkWindowImplX11 *impl;
269   GdkDrawableImplX11 *draw_impl;
270   XWindowAttributes xattributes;
271   unsigned int width;
272   unsigned int height;
273   unsigned int border_width;
274   unsigned int depth;
275   int x, y;
276
277   g_assert (gdk_parent_root == NULL);
278   
279   XGetGeometry (gdk_display, gdk_root_window, &gdk_root_window,
280                 &x, &y, &width, &height, &border_width, &depth);
281   XGetWindowAttributes (gdk_display, gdk_root_window, &xattributes);
282
283   gdk_parent_root = g_object_new (GDK_TYPE_WINDOW, NULL);
284   private = (GdkWindowObject *)gdk_parent_root;
285   impl = GDK_WINDOW_IMPL_X11 (private->impl);
286   draw_impl = GDK_DRAWABLE_IMPL_X11 (private->impl);
287   
288   draw_impl->xdisplay = gdk_display;
289   draw_impl->xid = gdk_root_window;
290   draw_impl->wrapper = GDK_DRAWABLE (private);
291   
292   private->window_type = GDK_WINDOW_ROOT;
293   private->depth = depth;
294   impl->width = width;
295   impl->height = height;
296   
297   gdk_xid_table_insert (&gdk_root_window, gdk_parent_root);
298 }
299
300 static GdkAtom wm_client_leader_atom = GDK_NONE;
301
302 GdkWindow*
303 gdk_window_new (GdkWindow     *parent,
304                 GdkWindowAttr *attributes,
305                 gint           attributes_mask)
306 {
307   GdkWindow *window;
308   GdkWindowObject *private;
309   GdkWindowObject *parent_private;
310   GdkWindowImplX11 *impl;
311   GdkDrawableImplX11 *draw_impl;
312   
313   GdkVisual *visual;
314   Window xparent;
315   Visual *xvisual;
316   Display *xdisplay;
317   Window xid;
318
319   XSetWindowAttributes xattributes;
320   long xattributes_mask;
321   XSizeHints size_hints;
322   XWMHints wm_hints;
323   XClassHint *class_hint;
324   int x, y, depth;
325   
326   unsigned int class;
327   char *title;
328   int i;
329   
330   g_return_val_if_fail (attributes != NULL, NULL);
331   
332   if (!parent)
333     parent = gdk_parent_root;
334
335   g_return_val_if_fail (GDK_IS_WINDOW (parent), NULL);
336   
337   parent_private = (GdkWindowObject*) parent;
338   if (GDK_WINDOW_DESTROYED (parent))
339     return NULL;
340   
341   xparent = GDK_WINDOW_XID (parent);
342   
343   window = g_object_new (GDK_TYPE_WINDOW, NULL);
344   private = (GdkWindowObject *)window;
345   impl = GDK_WINDOW_IMPL_X11 (private->impl);
346   draw_impl = GDK_DRAWABLE_IMPL_X11 (private->impl);
347   draw_impl->wrapper = GDK_DRAWABLE (window);
348   
349   xdisplay = draw_impl->xdisplay = GDK_WINDOW_XDISPLAY (parent); 
350   
351   private->parent = (GdkWindowObject *)parent;
352
353   xattributes_mask = 0;
354   
355   if (attributes_mask & GDK_WA_X)
356     x = attributes->x;
357   else
358     x = 0;
359   
360   if (attributes_mask & GDK_WA_Y)
361     y = attributes->y;
362   else
363     y = 0;
364   
365   private->x = x;
366   private->y = y;
367   impl->width = (attributes->width > 1) ? (attributes->width) : (1);
368   impl->height = (attributes->height > 1) ? (attributes->height) : (1);
369   private->window_type = attributes->window_type;
370
371   _gdk_window_init_position (GDK_WINDOW (private));
372   if (impl->position_info.big)
373     private->guffaw_gravity = TRUE;
374   
375   if (attributes_mask & GDK_WA_VISUAL)
376     visual = attributes->visual;
377   else
378     visual = gdk_visual_get_system ();
379   xvisual = ((GdkVisualPrivate*) visual)->xvisual;
380   
381   xattributes.event_mask = StructureNotifyMask;
382   for (i = 0; i < gdk_nevent_masks; i++)
383     {
384       if (attributes->event_mask & (1 << (i + 1)))
385         xattributes.event_mask |= gdk_event_mask_table[i];
386     }
387   
388   if (xattributes.event_mask)
389     xattributes_mask |= CWEventMask;
390   
391   if (attributes_mask & GDK_WA_NOREDIR)
392     {
393       xattributes.override_redirect =
394         (attributes->override_redirect == FALSE)?False:True;
395       xattributes_mask |= CWOverrideRedirect;
396     } 
397   else
398     xattributes.override_redirect = False;
399
400   if (parent_private && parent_private->guffaw_gravity)
401     {
402       xattributes.win_gravity = StaticGravity;
403       xattributes_mask |= CWWinGravity;
404     }
405   
406   if (attributes->wclass == GDK_INPUT_OUTPUT)
407     {
408       class = InputOutput;
409       depth = visual->depth;
410
411       private->input_only = FALSE;
412       private->depth = depth;
413       
414       if (attributes_mask & GDK_WA_COLORMAP)
415         {
416           draw_impl->colormap = attributes->colormap;
417           gdk_colormap_ref (attributes->colormap);
418         }
419       else
420         {
421           if ((((GdkVisualPrivate*)gdk_visual_get_system ())->xvisual) == xvisual)
422             {
423               draw_impl->colormap =
424                 gdk_colormap_get_system ();
425               gdk_colormap_ref (draw_impl->colormap);
426             }
427           else
428             {
429               draw_impl->colormap =
430                 gdk_colormap_new (visual, FALSE);
431             }
432         }
433       
434       private->bg_color.pixel = BlackPixel (gdk_display, gdk_screen);
435       xattributes.background_pixel = private->bg_color.pixel;
436
437       private->bg_pixmap = NULL;
438       
439       xattributes.border_pixel = BlackPixel (gdk_display, gdk_screen);
440       xattributes_mask |= CWBorderPixel | CWBackPixel;
441
442       if (private->guffaw_gravity)
443         xattributes.bit_gravity = StaticGravity;
444       else
445         xattributes.bit_gravity = NorthWestGravity;
446       
447       xattributes_mask |= CWBitGravity;
448   
449       switch (private->window_type)
450         {
451         case GDK_WINDOW_TOPLEVEL:
452           xattributes.colormap = GDK_COLORMAP_XCOLORMAP (draw_impl->colormap);
453           xattributes_mask |= CWColormap;
454           
455           xparent = gdk_root_window;
456           break;
457           
458         case GDK_WINDOW_CHILD:
459           xattributes.colormap = GDK_COLORMAP_XCOLORMAP (draw_impl->colormap);
460           xattributes_mask |= CWColormap;
461           break;
462           
463         case GDK_WINDOW_DIALOG:
464           xattributes.colormap = GDK_COLORMAP_XCOLORMAP (draw_impl->colormap);
465           xattributes_mask |= CWColormap;
466           
467           xparent = gdk_root_window;
468           break;
469           
470         case GDK_WINDOW_TEMP:
471           xattributes.colormap = GDK_COLORMAP_XCOLORMAP (draw_impl->colormap);
472           xattributes_mask |= CWColormap;
473           
474           xparent = gdk_root_window;
475           
476           xattributes.save_under = True;
477           xattributes.override_redirect = True;
478           xattributes.cursor = None;
479           xattributes_mask |= CWSaveUnder | CWOverrideRedirect;
480           break;
481         case GDK_WINDOW_ROOT:
482           g_error ("cannot make windows of type GDK_WINDOW_ROOT");
483           break;
484         }
485     }
486   else
487     {
488       depth = 0;
489       private->depth = 0;
490       class = InputOnly;
491       private->input_only = TRUE;
492       draw_impl->colormap = gdk_colormap_get_system ();
493       gdk_colormap_ref (draw_impl->colormap);
494     }
495
496   xid = draw_impl->xid = XCreateWindow (xdisplay, xparent,
497                                         impl->position_info.x, impl->position_info.y,
498                                         impl->position_info.width, impl->position_info.height,
499                                         0, depth, class, xvisual,
500                                         xattributes_mask, &xattributes);
501
502   gdk_drawable_ref (window);
503   gdk_xid_table_insert (&draw_impl->xid, window);
504   
505   gdk_window_set_cursor (window, ((attributes_mask & GDK_WA_CURSOR) ?
506                                   (attributes->cursor) :
507                                   NULL));
508   
509   if (parent_private)
510     parent_private->children = g_list_prepend (parent_private->children, window);
511   
512   switch (GDK_WINDOW_TYPE (private))
513     {
514     case GDK_WINDOW_DIALOG:
515       XSetTransientForHint (xdisplay, xid, xparent);
516     case GDK_WINDOW_TOPLEVEL:
517     case GDK_WINDOW_TEMP:
518       XSetWMProtocols (xdisplay, xid,
519                        gdk_wm_window_protocols, 3);
520       break;
521     case GDK_WINDOW_CHILD:
522       if ((attributes->wclass == GDK_INPUT_OUTPUT) &&
523           (draw_impl->colormap != gdk_colormap_get_system ()) &&
524           (draw_impl->colormap != gdk_window_get_colormap (gdk_window_get_toplevel (window))))
525         {
526           GDK_NOTE (MISC, g_message ("adding colormap window\n"));
527           gdk_window_add_colormap_windows (window);
528         }
529       
530       return window;
531     default:
532       
533       return window;
534     }
535
536   if (class != InputOnly)
537     {
538       /* The focus window is off the visible area, and serves to receive key
539        * press events so they don't get sent to child windows.
540        */
541       impl->focus_window = XCreateSimpleWindow (xdisplay, xid,
542                                                 -1, -1, 1, 1, 0,
543                                                 xattributes.background_pixel,
544                                                 xattributes.background_pixel);
545       /* FIXME: probably better to actually track the requested event mask for the toplevel
546        */
547       XSelectInput (xdisplay, impl->focus_window,
548                     KeyPressMask | KeyReleaseMask | FocusChangeMask);
549       
550       XMapWindow (xdisplay, impl->focus_window);
551       gdk_xid_table_insert (&impl->focus_window, window);
552     }
553
554   size_hints.flags = PSize;
555   size_hints.width = impl->width;
556   size_hints.height = impl->height;
557   
558   wm_hints.flags = StateHint | WindowGroupHint;
559   wm_hints.window_group = gdk_leader_window;
560   wm_hints.input = True;
561   wm_hints.initial_state = NormalState;
562   
563   /* FIXME: Is there any point in doing this? Do any WM's pay
564    * attention to PSize, and even if they do, is this the
565    * correct value???
566    */
567   XSetWMNormalHints (xdisplay, xid, &size_hints);
568   
569   XSetWMHints (xdisplay, xid, &wm_hints);
570   
571   if (!wm_client_leader_atom)
572     wm_client_leader_atom = gdk_atom_intern ("WM_CLIENT_LEADER", FALSE);
573   
574   XChangeProperty (xdisplay, xid,
575                    wm_client_leader_atom,
576                    XA_WINDOW, 32, PropModeReplace,
577                    (guchar*) &gdk_leader_window, 1);
578   
579   if (attributes_mask & GDK_WA_TITLE)
580     title = attributes->title;
581   else
582     title = g_get_prgname ();
583
584   gdk_window_set_title (window, title);
585   
586   if (attributes_mask & GDK_WA_WMCLASS)
587     {
588       class_hint = XAllocClassHint ();
589       class_hint->res_name = attributes->wmclass_name;
590       class_hint->res_class = attributes->wmclass_class;
591       XSetClassHint (xdisplay, xid, class_hint);
592       XFree (class_hint);
593     }
594   
595   return window;
596 }
597
598 GdkWindow *
599 gdk_window_foreign_new (GdkNativeWindow anid)
600 {
601   GdkWindow *window;
602   GdkWindowObject *private;
603   GdkWindowObject *parent_private;
604   GdkWindowImplX11 *impl;
605   GdkDrawableImplX11 *draw_impl;
606   XWindowAttributes attrs;
607   Window root, parent;
608   Window *children = NULL;
609   guint nchildren;
610   gboolean result;
611
612   gdk_error_trap_push ();
613   result = XGetWindowAttributes (gdk_display, anid, &attrs);
614   if (gdk_error_trap_pop () || !result)
615     return NULL;
616
617   /* FIXME: This is pretty expensive. Maybe the caller should supply
618    *        the parent */
619   gdk_error_trap_push ();
620   result = XQueryTree (gdk_display, anid, &root, &parent, &children, &nchildren);
621   if (gdk_error_trap_pop () || !result)
622     return NULL;
623
624   if (children)
625     XFree (children);
626   
627   window = g_object_new (GDK_TYPE_WINDOW, NULL);
628   private = (GdkWindowObject *)window;
629   impl = GDK_WINDOW_IMPL_X11 (private->impl);
630   draw_impl = GDK_DRAWABLE_IMPL_X11 (private->impl);
631   draw_impl->wrapper = GDK_DRAWABLE (window);
632   
633   private->parent = gdk_xid_table_lookup (parent);
634   
635   parent_private = (GdkWindowObject *)private->parent;
636   
637   if (parent_private)
638     parent_private->children = g_list_prepend (parent_private->children, window);
639
640   draw_impl->xid = anid;
641   draw_impl->xdisplay = gdk_display;
642
643   private->x = attrs.x;
644   private->y = attrs.y;
645   impl->width = attrs.width;
646   impl->height = attrs.height;
647   private->window_type = GDK_WINDOW_FOREIGN;
648   private->destroyed = FALSE;
649
650   if (attrs.map_state == IsUnmapped)
651     private->state = GDK_WINDOW_STATE_WITHDRAWN;
652   else
653     private->state = 0;
654
655   private->depth = attrs.depth;
656   
657   _gdk_window_init_position (GDK_WINDOW (private));
658
659   gdk_drawable_ref (window);
660   gdk_xid_table_insert (&GDK_WINDOW_XID (window), window);
661   
662   return window;
663 }
664
665 void
666 _gdk_windowing_window_destroy (GdkWindow *window,
667                                gboolean   recursing,
668                                gboolean   foreign_destroy)
669 {
670   GdkWindowObject *private = (GdkWindowObject *)window;
671
672   g_return_if_fail (GDK_IS_WINDOW (window));
673
674   _gdk_selection_window_destroyed (window);
675   
676   if (private->extension_events != 0)
677     gdk_input_window_destroy (window);
678
679   if (private->window_type == GDK_WINDOW_FOREIGN)
680     {
681       if (!foreign_destroy && (private->parent != NULL))
682         {
683           /* It's somebody else's window, but in our heirarchy,
684            * so reparent it to the root window, and then send
685            * it a delete event, as if we were a WM
686            */
687           XClientMessageEvent xevent;
688           
689           gdk_error_trap_push ();
690           gdk_window_hide (window);
691           gdk_window_reparent (window, NULL, 0, 0);
692           
693           xevent.type = ClientMessage;
694           xevent.window = GDK_WINDOW_XID (window);
695           xevent.message_type = gdk_wm_protocols;
696           xevent.format = 32;
697           xevent.data.l[0] = gdk_wm_delete_window;
698           xevent.data.l[1] = CurrentTime;
699           
700           XSendEvent (GDK_WINDOW_XDISPLAY (window),
701                       GDK_WINDOW_XID (window),
702                       False, 0, (XEvent *)&xevent);
703           gdk_flush ();
704           gdk_error_trap_pop ();
705         }
706     }
707   else if (!recursing && !foreign_destroy)
708     XDestroyWindow (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window));
709 }
710
711 /* This function is called when the XWindow is really gone.
712  */
713 void
714 gdk_window_destroy_notify (GdkWindow *window)
715 {
716   GdkWindowImplX11 *window_impl;
717
718   g_return_if_fail (window != NULL);
719   
720   window_impl = GDK_WINDOW_IMPL_X11 (((GdkWindowObject *)window)->impl);
721
722   if (!GDK_WINDOW_DESTROYED (window))
723     {
724       if (GDK_WINDOW_TYPE(window) != GDK_WINDOW_FOREIGN)
725         g_warning ("GdkWindow %#lx unexpectedly destroyed", GDK_WINDOW_XID (window));
726
727       _gdk_window_destroy (window, TRUE);
728     }
729   
730   gdk_xid_table_remove (GDK_WINDOW_XID (window));
731   if (window_impl->focus_window)
732     gdk_xid_table_remove (window_impl->focus_window);
733   
734   gdk_drawable_unref (window);
735 }
736
737 static void
738 set_initial_hints (GdkWindow *window)
739 {
740   GdkWindowObject *private;
741   GdkAtom atoms[5];
742   gint i;
743   
744   private = (GdkWindowObject*) window;
745
746   if (private->state & GDK_WINDOW_STATE_ICONIFIED)
747     {
748       XWMHints *wm_hints;
749       
750       wm_hints = XGetWMHints (GDK_WINDOW_XDISPLAY (window),
751                               GDK_WINDOW_XID (window));
752       if (!wm_hints)
753         wm_hints = XAllocWMHints ();
754
755       wm_hints->flags |= StateHint;
756       wm_hints->initial_state = IconicState;
757       
758       XSetWMHints (GDK_WINDOW_XDISPLAY (window),
759                    GDK_WINDOW_XID (window), wm_hints);
760       XFree (wm_hints);
761     }
762
763   /* We set the spec hints regardless of whether the spec is supported,
764    * since it can't hurt and it's kind of expensive to check whether
765    * it's supported.
766    */
767   
768   i = 0;
769
770   if (private->state & GDK_WINDOW_STATE_MAXIMIZED)
771     {
772       atoms[i] = gdk_atom_intern ("_NET_WM_STATE_MAXIMIZED_VERT", FALSE);
773       ++i;
774       atoms[i] = gdk_atom_intern ("_NET_WM_STATE_MAXIMIZED_HORZ", FALSE);
775       ++i;
776     }
777
778   if (private->state & GDK_WINDOW_STATE_STICKY)
779     {
780       atoms[i] = gdk_atom_intern ("_NET_WM_STATE_STICKY", FALSE);
781       ++i;
782     }
783
784   if (private->modal_hint)
785     {
786       atoms[i] = gdk_atom_intern ("_NET_WM_STATE_MODAL", FALSE);
787       ++i;
788     }
789
790   if (i > 0)
791     {
792       XChangeProperty (GDK_WINDOW_XDISPLAY (window),
793                        GDK_WINDOW_XID (window),
794                        gdk_atom_intern ("_NET_WM_STATE", FALSE),
795                        XA_ATOM, 32, PropModeReplace,
796                        (guchar*) atoms, i);
797     }
798
799   if (private->state & GDK_WINDOW_STATE_STICKY)
800     {
801       atoms[0] = 0xFFFFFFFF;
802       XChangeProperty (GDK_WINDOW_XDISPLAY (window),
803                        GDK_WINDOW_XID (window),
804                        gdk_atom_intern ("_NET_WM_DESKTOP", FALSE),
805                        XA_CARDINAL, 32, PropModeReplace,
806                        (guchar*) atoms, 1);
807     }
808 }
809
810 static void
811 show_window_internal (GdkWindow *window,
812                       gboolean   raise)
813 {
814   GdkWindowObject *private;
815   
816   g_return_if_fail (GDK_IS_WINDOW (window));
817   
818   private = (GdkWindowObject*) window;
819   if (!private->destroyed)
820     {
821       if (raise)
822         XRaiseWindow (GDK_WINDOW_XDISPLAY (window),
823                       GDK_WINDOW_XID (window));
824
825       if (!GDK_WINDOW_IS_MAPPED (window))
826         {
827           set_initial_hints (window);
828           
829           gdk_synthesize_window_state (window,
830                                        GDK_WINDOW_STATE_WITHDRAWN,
831                                        0);
832         }
833       
834       g_assert (GDK_WINDOW_IS_MAPPED (window));
835       
836       if (GDK_WINDOW_IMPL_X11 (private->impl)->position_info.mapped)
837         XMapWindow (GDK_WINDOW_XDISPLAY (window),
838                     GDK_WINDOW_XID (window));
839     }
840 }
841
842 /**
843  * gdk_window_show_unraised:
844  * @window: a #GdkWindow
845  *
846  * Shows a #GdkWindow onscreen, but does not modify its stacking
847  * order. In contrast, gdk_window_show() will raise the window
848  * to the top of the window stack.
849  * 
850  **/
851 void
852 gdk_window_show_unraised (GdkWindow *window)
853 {
854   g_return_if_fail (GDK_IS_WINDOW (window));
855   
856   show_window_internal (window, FALSE);
857 }
858
859 void
860 gdk_window_show (GdkWindow *window)
861 {
862   g_return_if_fail (GDK_IS_WINDOW (window));
863
864   show_window_internal (window, TRUE);
865 }
866
867 void
868 gdk_window_hide (GdkWindow *window)
869 {
870   GdkWindowObject *private;
871   
872   g_return_if_fail (window != NULL);
873
874   private = (GdkWindowObject*) window;
875
876   /* You can't simply unmap toplevel windows. */
877   switch (private->window_type)
878     {
879     case GDK_WINDOW_TOPLEVEL:
880     case GDK_WINDOW_DIALOG:
881     case GDK_WINDOW_TEMP: /* ? */
882       gdk_window_withdraw (window);
883       return;
884       break;
885       
886     case GDK_WINDOW_FOREIGN:
887     case GDK_WINDOW_ROOT:
888     case GDK_WINDOW_CHILD:
889       break;
890     }
891   
892   if (!private->destroyed)
893     {
894       if (GDK_WINDOW_IS_MAPPED (window))
895         gdk_synthesize_window_state (window,
896                                      0,
897                                      GDK_WINDOW_STATE_WITHDRAWN);
898
899       g_assert (!GDK_WINDOW_IS_MAPPED (window));
900       
901       _gdk_window_clear_update_area (window);
902       
903       XUnmapWindow (GDK_WINDOW_XDISPLAY (window),
904                     GDK_WINDOW_XID (window));
905     }
906 }
907
908 void
909 gdk_window_withdraw (GdkWindow *window)
910 {
911   GdkWindowObject *private;
912   
913   g_return_if_fail (window != NULL);
914   
915   private = (GdkWindowObject*) window;
916   if (!private->destroyed)
917     {
918       if (GDK_WINDOW_IS_MAPPED (window))
919         gdk_synthesize_window_state (window,
920                                      0,
921                                      GDK_WINDOW_STATE_WITHDRAWN);
922
923       g_assert (!GDK_WINDOW_IS_MAPPED (window));
924       
925       XWithdrawWindow (GDK_WINDOW_XDISPLAY (window),
926                        GDK_WINDOW_XID (window), 0);
927     }
928 }
929
930 void
931 gdk_window_move (GdkWindow *window,
932                  gint       x,
933                  gint       y)
934 {
935   GdkWindowObject *private = (GdkWindowObject *)window;
936   GdkWindowImplX11 *impl;
937
938   g_return_if_fail (window != NULL);
939   g_return_if_fail (GDK_IS_WINDOW (window));
940
941   impl = GDK_WINDOW_IMPL_X11 (private->impl);
942
943   if (!GDK_WINDOW_DESTROYED (window))
944     {
945       if (GDK_WINDOW_TYPE (private) == GDK_WINDOW_CHILD)
946         _gdk_window_move_resize_child (window, x, y,
947                                        impl->width, impl->height);
948       else
949         {
950           XMoveWindow (GDK_WINDOW_XDISPLAY (window),
951                        GDK_WINDOW_XID (window),
952                        x, y);
953         }
954     }
955 }
956
957 void
958 gdk_window_resize (GdkWindow *window,
959                    gint       width,
960                    gint       height)
961 {
962   GdkWindowObject *private;
963   
964   g_return_if_fail (window != NULL);
965   g_return_if_fail (GDK_IS_WINDOW (window));
966   
967   if (width < 1)
968     width = 1;
969   if (height < 1)
970     height = 1;
971
972   private = (GdkWindowObject*) window;
973   
974   if (!GDK_WINDOW_DESTROYED (window))
975     {
976       if (GDK_WINDOW_TYPE (private) == GDK_WINDOW_CHILD)
977         _gdk_window_move_resize_child (window, private->x, private->y,
978                                        width, height);
979       else
980         {
981           GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (private->impl);
982           
983           if (width != impl->width || height != impl->height)
984             private->resize_count += 1;
985
986           XResizeWindow (GDK_WINDOW_XDISPLAY (window),
987                          GDK_WINDOW_XID (window),
988                          width, height);
989         }
990     }
991 }
992
993 void
994 gdk_window_move_resize (GdkWindow *window,
995                         gint       x,
996                         gint       y,
997                         gint       width,
998                         gint       height)
999 {
1000   GdkWindowObject *private;
1001   
1002   g_return_if_fail (window != NULL);
1003   g_return_if_fail (GDK_IS_WINDOW (window));
1004
1005   if (width < 1)
1006     width = 1;
1007   if (height < 1)
1008     height = 1;
1009   
1010   private = (GdkWindowObject*) window;
1011
1012   if (!GDK_WINDOW_DESTROYED (window))
1013     {
1014       if (GDK_WINDOW_TYPE (private) == GDK_WINDOW_CHILD)
1015         _gdk_window_move_resize_child (window, x, y, width, height);
1016       else
1017         {
1018           GdkWindowImplX11 *impl = GDK_WINDOW_IMPL_X11 (private->impl);
1019           
1020           if (width != impl->width || height != impl->height)
1021             private->resize_count += 1;
1022           
1023           XMoveResizeWindow (GDK_WINDOW_XDISPLAY (window),
1024                              GDK_WINDOW_XID (window),
1025                              x, y, width, height);
1026         }
1027     }
1028 }
1029
1030 void
1031 gdk_window_reparent (GdkWindow *window,
1032                      GdkWindow *new_parent,
1033                      gint       x,
1034                      gint       y)
1035 {
1036   GdkWindowObject *window_private;
1037   GdkWindowObject *parent_private;
1038   GdkWindowObject *old_parent_private;
1039   
1040   g_return_if_fail (window != NULL);
1041   g_return_if_fail (GDK_IS_WINDOW (window));
1042   g_return_if_fail (new_parent == NULL || GDK_IS_WINDOW (new_parent));
1043   
1044   if (!new_parent)
1045     new_parent = gdk_parent_root;
1046   
1047   window_private = (GdkWindowObject*) window;
1048   old_parent_private = (GdkWindowObject*)window_private->parent;
1049   parent_private = (GdkWindowObject*) new_parent;
1050   
1051   if (!GDK_WINDOW_DESTROYED (window) && !GDK_WINDOW_DESTROYED (new_parent))
1052     XReparentWindow (GDK_WINDOW_XDISPLAY (window),
1053                      GDK_WINDOW_XID (window),
1054                      GDK_WINDOW_XID (new_parent),
1055                      x, y);
1056   
1057   window_private->parent = (GdkWindowObject *)new_parent;
1058   
1059   if (old_parent_private)
1060     old_parent_private->children = g_list_remove (old_parent_private->children, window);
1061   
1062   if ((old_parent_private &&
1063        (!old_parent_private->guffaw_gravity != !parent_private->guffaw_gravity)) ||
1064       (!old_parent_private && parent_private->guffaw_gravity))
1065     gdk_window_set_static_win_gravity (window, parent_private->guffaw_gravity);
1066   
1067   parent_private->children = g_list_prepend (parent_private->children, window);
1068   _gdk_window_init_position (GDK_WINDOW (window_private));
1069 }
1070
1071 void
1072 _gdk_windowing_window_clear_area (GdkWindow *window,
1073                                   gint       x,
1074                                   gint       y,
1075                                   gint       width,
1076                                   gint       height)
1077 {
1078   g_return_if_fail (window != NULL);
1079   g_return_if_fail (GDK_IS_WINDOW (window));
1080   
1081   if (!GDK_WINDOW_DESTROYED (window))
1082     XClearArea (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
1083                 x, y, width, height, False);
1084 }
1085
1086 void
1087 _gdk_windowing_window_clear_area_e (GdkWindow *window,
1088                                     gint       x,
1089                                     gint       y,
1090                                     gint       width,
1091                                     gint       height)
1092 {
1093   g_return_if_fail (window != NULL);
1094   g_return_if_fail (GDK_IS_WINDOW (window));
1095   
1096   if (!GDK_WINDOW_DESTROYED (window))
1097     XClearArea (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
1098                 x, y, width, height, True);
1099 }
1100
1101 void
1102 gdk_window_raise (GdkWindow *window)
1103 {
1104   g_return_if_fail (window != NULL);
1105   g_return_if_fail (GDK_IS_WINDOW (window));
1106   
1107   if (!GDK_WINDOW_DESTROYED (window))
1108     XRaiseWindow (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window));
1109 }
1110
1111 void
1112 gdk_window_lower (GdkWindow *window)
1113 {
1114   g_return_if_fail (window != NULL);
1115   g_return_if_fail (GDK_IS_WINDOW (window));
1116   
1117   if (!GDK_WINDOW_DESTROYED (window))
1118     XLowerWindow (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window));
1119 }
1120
1121 void
1122 gdk_window_focus (GdkWindow *window,
1123                   guint32    timestamp)
1124 {
1125   g_return_if_fail (GDK_IS_WINDOW (window));
1126
1127   if (GDK_WINDOW_DESTROYED (window))
1128     return;
1129   
1130   if (gdk_net_wm_supports (gdk_atom_intern ("_NET_ACTIVE_WINDOW", FALSE)))
1131     {
1132       XEvent xev;
1133
1134       xev.xclient.type = ClientMessage;
1135       xev.xclient.serial = 0;
1136       xev.xclient.send_event = True;
1137       xev.xclient.window = GDK_WINDOW_XWINDOW (window);
1138       xev.xclient.display = gdk_display;
1139       xev.xclient.message_type = gdk_atom_intern ("_NET_ACTIVE_WINDOW", FALSE);
1140       xev.xclient.format = 32;
1141       xev.xclient.data.l[0] = 0;
1142       
1143       XSendEvent (gdk_display, gdk_root_window, False,
1144                   SubstructureRedirectMask | SubstructureNotifyMask,
1145                   &xev);
1146     }
1147   else
1148     {
1149       XRaiseWindow (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window));
1150
1151       /* There is no way of knowing reliably whether we are viewable so we need
1152        * to trap errors so we don't cause a BadMatch.
1153        */
1154       gdk_error_trap_push ();
1155       XSetInputFocus (GDK_WINDOW_XDISPLAY (window),
1156                       GDK_WINDOW_XWINDOW (window),
1157                       RevertToNone,
1158                       timestamp);
1159       XSync (GDK_WINDOW_XDISPLAY (window), False);
1160       gdk_error_trap_pop ();
1161     }
1162 }
1163
1164 void
1165 gdk_window_set_hints (GdkWindow *window,
1166                       gint       x,
1167                       gint       y,
1168                       gint       min_width,
1169                       gint       min_height,
1170                       gint       max_width,
1171                       gint       max_height,
1172                       gint       flags)
1173 {
1174   XSizeHints size_hints;
1175   
1176   g_return_if_fail (window != NULL);
1177   g_return_if_fail (GDK_IS_WINDOW (window));
1178   
1179   if (GDK_WINDOW_DESTROYED (window))
1180     return;
1181   
1182   size_hints.flags = 0;
1183   
1184   if (flags & GDK_HINT_POS)
1185     {
1186       size_hints.flags |= PPosition;
1187       size_hints.x = x;
1188       size_hints.y = y;
1189     }
1190   
1191   if (flags & GDK_HINT_MIN_SIZE)
1192     {
1193       size_hints.flags |= PMinSize;
1194       size_hints.min_width = min_width;
1195       size_hints.min_height = min_height;
1196     }
1197   
1198   if (flags & GDK_HINT_MAX_SIZE)
1199     {
1200       size_hints.flags |= PMaxSize;
1201       size_hints.max_width = max_width;
1202       size_hints.max_height = max_height;
1203     }
1204   
1205   /* FIXME: Would it be better to delete this property if
1206    *        flags == 0? It would save space on the server
1207    */
1208   XSetWMNormalHints (GDK_WINDOW_XDISPLAY (window),
1209                      GDK_WINDOW_XID (window),
1210                      &size_hints);
1211 }
1212
1213 /**
1214  * gdk_window_set_type_hint:
1215  * @window: A #GdkWindow
1216  * @hint: A hint of the function this window will have
1217  *
1218  * The application can use this call to provide a hint to the window
1219  * manager about the functionality of a window. The window manager
1220  * can use this information when determining the decoration and behaviour
1221  * of the window.
1222  *
1223  * The hint must be set before the window is mapped.
1224  **/
1225 void
1226 gdk_window_set_type_hint (GdkWindow        *window,
1227                           GdkWindowTypeHint hint)
1228 {
1229   GdkAtom atom;
1230   
1231   g_return_if_fail (window != NULL);
1232   g_return_if_fail (GDK_IS_WINDOW (window));
1233   
1234   if (GDK_WINDOW_DESTROYED (window))
1235     return;
1236
1237   switch (hint)
1238     {
1239     case GDK_WINDOW_TYPE_HINT_DIALOG:
1240       atom = gdk_atom_intern ("_NET_WM_WINDOW_TYPE_DIALOG", FALSE);
1241       break;
1242     case GDK_WINDOW_TYPE_HINT_MENU:
1243       atom = gdk_atom_intern ("_NET_WM_WINDOW_TYPE_MENU", FALSE);
1244       break;
1245     case GDK_WINDOW_TYPE_HINT_TOOLBAR:
1246       atom = gdk_atom_intern ("_NET_WM_WINDOW_TYPE_TOOLBAR", FALSE);
1247       break;
1248     default:
1249       g_warning ("Unknown hint %d passed to gdk_window_set_type_hint", hint);
1250       /* Fall thru */
1251     case GDK_WINDOW_TYPE_HINT_NORMAL:
1252       atom = gdk_atom_intern ("_NET_WM_WINDOW_TYPE_NORMAL", FALSE);
1253       break;
1254     }
1255
1256   XChangeProperty (GDK_WINDOW_XDISPLAY (window),
1257                    GDK_WINDOW_XID (window),
1258                    gdk_atom_intern ("_NET_WM_WINDOW_TYPE", FALSE),
1259                    XA_ATOM, 32, PropModeReplace,
1260                    (guchar *)&atom, 1);
1261 }
1262
1263
1264 static void
1265 gdk_wmspec_change_state (gboolean add,
1266                          GdkWindow *window,
1267                          GdkAtom state1,
1268                          GdkAtom state2)
1269 {
1270   XEvent xev;
1271   Atom op;
1272
1273   if (add)
1274     op = gdk_atom_intern ("_NET_WM_STATE_ADD", FALSE);
1275   else
1276     op = gdk_atom_intern ("_NET_WM_STATE_REMOVE", FALSE);
1277   
1278   xev.xclient.type = ClientMessage;
1279   xev.xclient.serial = 0;
1280   xev.xclient.send_event = True;
1281   xev.xclient.display = gdk_display;
1282   xev.xclient.window = GDK_WINDOW_XID (window);
1283   xev.xclient.message_type = gdk_atom_intern ("_NET_WM_STATE", FALSE);
1284   xev.xclient.format = 32;
1285   xev.xclient.data.l[0] = op;
1286   xev.xclient.data.l[1] = state1;
1287   xev.xclient.data.l[2] = state2;
1288   
1289   XSendEvent (gdk_display, gdk_root_window, False,
1290               SubstructureRedirectMask | SubstructureNotifyMask,
1291               &xev);
1292 }
1293 /**
1294  * gdk_window_set_modal_hint:
1295  * @window: A #GdkWindow
1296  * @modal: TRUE if the window is modal, FALSE otherwise.
1297  *
1298  * The application can use this hint to tell the window manager
1299  * that a certain window has modal behaviour. The window manager
1300  * can use this information to handle modal windows in a special
1301  * way.
1302  *
1303  * You should only use this on windows for which you have
1304  * previously called #gdk_window_set_transient_for()
1305  **/
1306 void
1307 gdk_window_set_modal_hint (GdkWindow *window,
1308                            gboolean   modal)
1309 {
1310   GdkWindowObject *private;
1311
1312   g_return_if_fail (window != NULL);
1313   g_return_if_fail (GDK_IS_WINDOW (window));
1314   
1315   if (GDK_WINDOW_DESTROYED (window))
1316     return;
1317
1318   private = (GdkWindowObject*) window;
1319
1320   private->modal_hint = modal;
1321
1322   if (GDK_WINDOW_IS_MAPPED (window))
1323     gdk_wmspec_change_state (modal, window,
1324                              gdk_atom_intern ("_NET_WM_STATE_MODAL", FALSE),
1325                              0);
1326 }
1327
1328 void 
1329 gdk_window_set_geometry_hints (GdkWindow      *window,
1330                                GdkGeometry    *geometry,
1331                                GdkWindowHints  geom_mask)
1332 {
1333   XSizeHints size_hints;
1334   
1335   g_return_if_fail (window != NULL);
1336   g_return_if_fail (GDK_IS_WINDOW (window));
1337   
1338   if (GDK_WINDOW_DESTROYED (window))
1339     return;
1340   
1341   size_hints.flags = 0;
1342   
1343   if (geom_mask & GDK_HINT_POS)
1344     {
1345       size_hints.flags |= PPosition;
1346       /* We need to initialize the following obsolete fields because KWM 
1347        * apparently uses these fields if they are non-zero.
1348        * #@#!#!$!.
1349        */
1350       size_hints.x = 0;
1351       size_hints.y = 0;
1352     }
1353   
1354   if (geom_mask & GDK_HINT_MIN_SIZE)
1355     {
1356       size_hints.flags |= PMinSize;
1357       size_hints.min_width = geometry->min_width;
1358       size_hints.min_height = geometry->min_height;
1359     }
1360   
1361   if (geom_mask & GDK_HINT_MAX_SIZE)
1362     {
1363       size_hints.flags |= PMaxSize;
1364       size_hints.max_width = MAX (geometry->max_width, 1);
1365       size_hints.max_height = MAX (geometry->max_height, 1);
1366     }
1367   
1368   if (geom_mask & GDK_HINT_BASE_SIZE)
1369     {
1370       size_hints.flags |= PBaseSize;
1371       size_hints.base_width = geometry->base_width;
1372       size_hints.base_height = geometry->base_height;
1373     }
1374   
1375   if (geom_mask & GDK_HINT_RESIZE_INC)
1376     {
1377       size_hints.flags |= PResizeInc;
1378       size_hints.width_inc = geometry->width_inc;
1379       size_hints.height_inc = geometry->height_inc;
1380     }
1381   
1382   if (geom_mask & GDK_HINT_ASPECT)
1383     {
1384       size_hints.flags |= PAspect;
1385       if (geometry->min_aspect <= 1)
1386         {
1387           size_hints.min_aspect.x = 65536 * geometry->min_aspect;
1388           size_hints.min_aspect.y = 65536;
1389         }
1390       else
1391         {
1392           size_hints.min_aspect.x = 65536;
1393           size_hints.min_aspect.y = 65536 / geometry->min_aspect;;
1394         }
1395       if (geometry->max_aspect <= 1)
1396         {
1397           size_hints.max_aspect.x = 65536 * geometry->max_aspect;
1398           size_hints.max_aspect.y = 65536;
1399         }
1400       else
1401         {
1402           size_hints.max_aspect.x = 65536;
1403           size_hints.max_aspect.y = 65536 / geometry->max_aspect;;
1404         }
1405     }
1406
1407   if (geom_mask & GDK_HINT_WIN_GRAVITY)
1408     {
1409       size_hints.flags |= PWinGravity;
1410       size_hints.win_gravity = geometry->win_gravity;
1411     }
1412   
1413   /* FIXME: Would it be better to delete this property if
1414    *        geom_mask == 0? It would save space on the server
1415    */
1416   XSetWMNormalHints (GDK_WINDOW_XDISPLAY (window),
1417                      GDK_WINDOW_XID (window),
1418                      &size_hints);
1419 }
1420
1421 static void
1422 gdk_window_get_geometry_hints (GdkWindow      *window,
1423                                GdkGeometry    *geometry,
1424                                GdkWindowHints *geom_mask)
1425 {
1426   XSizeHints size_hints;  
1427   glong junk_size_mask = 0;
1428
1429   g_return_if_fail (GDK_IS_WINDOW (window));
1430   g_return_if_fail (geometry != NULL);
1431   g_return_if_fail (geom_mask != NULL);
1432
1433   *geom_mask = 0;
1434   
1435   if (GDK_WINDOW_DESTROYED (window))
1436     return;
1437   
1438   if (!XGetWMNormalHints (GDK_WINDOW_XDISPLAY (window),
1439                           GDK_WINDOW_XID (window),
1440                           &size_hints,
1441                           &junk_size_mask))
1442     return;                   
1443
1444   if (size_hints.flags & PMinSize)
1445     {
1446       *geom_mask |= GDK_HINT_MIN_SIZE;
1447       geometry->min_width = size_hints.min_width;
1448       geometry->min_height = size_hints.min_height;
1449     }
1450
1451   if (size_hints.flags & PMaxSize)
1452     {
1453       *geom_mask |= GDK_HINT_MAX_SIZE;
1454       geometry->max_width = MAX (size_hints.max_width, 1);
1455       geometry->max_height = MAX (size_hints.max_height, 1);
1456     }
1457
1458   if (size_hints.flags & PResizeInc)
1459     {
1460       *geom_mask |= GDK_HINT_RESIZE_INC;
1461       geometry->width_inc = size_hints.width_inc;
1462       geometry->height_inc = size_hints.height_inc;
1463     }
1464
1465   if (size_hints.flags & PAspect)
1466     {
1467       *geom_mask |= GDK_HINT_ASPECT;
1468
1469       geometry->min_aspect = (gdouble) size_hints.min_aspect.x / (gdouble) size_hints.min_aspect.y;
1470       geometry->max_aspect = (gdouble) size_hints.max_aspect.x / (gdouble) size_hints.max_aspect.y;
1471     }
1472
1473   if (size_hints.flags & PWinGravity)
1474     {
1475       *geom_mask |= GDK_HINT_WIN_GRAVITY;
1476       geometry->win_gravity = size_hints.win_gravity;
1477     }
1478 }
1479
1480 static gboolean
1481 utf8_is_latin1 (const gchar *str)
1482 {
1483   const char *p = str;
1484
1485   while (*p)
1486     {
1487       gunichar ch = g_utf8_get_char (p);
1488
1489       if (ch > 0xff)
1490         return FALSE;
1491       
1492       p = g_utf8_next_char (p);
1493     }
1494
1495   return TRUE;
1496 }
1497
1498 /* Set the property to @utf8_str as STRING if the @utf8_str is fully
1499  * convertable to STRING, otherwise, set it as compound text
1500  */
1501 static void
1502 set_text_property (GdkWindow   *window,
1503                    GdkAtom      property,
1504                    const gchar *utf8_str)
1505 {
1506   guchar *prop_text = NULL;
1507   GdkAtom prop_type;
1508   gint prop_length;
1509   gint prop_format;
1510   
1511   if (utf8_is_latin1 (utf8_str))
1512     {
1513       prop_type = GDK_TARGET_STRING;
1514       prop_text = gdk_utf8_to_string_target (utf8_str);
1515       prop_length = strlen (prop_text);
1516       prop_format = 8;
1517     }
1518   else
1519     {
1520       gdk_utf8_to_compound_text (utf8_str, &prop_type, &prop_format,
1521                                  &prop_text, &prop_length);
1522     }
1523
1524   if (prop_text)
1525     {
1526       XChangeProperty (GDK_WINDOW_XDISPLAY (window),
1527                        GDK_WINDOW_XID (window),
1528                        property,
1529                        prop_type, prop_format,
1530                        PropModeReplace, prop_text,
1531                        prop_length);
1532
1533       g_free (prop_text);
1534     }
1535 }
1536
1537 void
1538 gdk_window_set_title (GdkWindow   *window,
1539                       const gchar *title)
1540 {
1541   g_return_if_fail (window != NULL);
1542   g_return_if_fail (GDK_IS_WINDOW (window));
1543
1544   if (GDK_WINDOW_DESTROYED (window))
1545     return;
1546   
1547   XChangeProperty (GDK_WINDOW_XDISPLAY (window),
1548                    GDK_WINDOW_XID (window),
1549                    gdk_atom_intern ("_NET_WM_NAME", FALSE),
1550                    gdk_atom_intern ("UTF8_STRING", FALSE), 8,
1551                    PropModeReplace, title,
1552                    strlen (title));
1553
1554   set_text_property (window, gdk_atom_intern ("WM_NAME", FALSE), title);
1555   if (!gdk_window_icon_name_set (window))
1556     {
1557       XChangeProperty (GDK_WINDOW_XDISPLAY (window),
1558                        GDK_WINDOW_XID (window),
1559                        gdk_atom_intern ("_NET_WM_ICON_NAME", FALSE),
1560                        gdk_atom_intern ("UTF8_STRING", FALSE), 8,
1561                        PropModeReplace, title,
1562                        strlen (title));
1563       set_text_property (window, gdk_atom_intern ("WM_ICON_NAME", FALSE), title);
1564     }
1565 }
1566
1567 void          
1568 gdk_window_set_role (GdkWindow   *window,
1569                      const gchar *role)
1570 {
1571   g_return_if_fail (window != NULL);
1572   g_return_if_fail (GDK_IS_WINDOW (window));
1573
1574   if (!GDK_WINDOW_DESTROYED (window))
1575     {
1576       if (role)
1577         XChangeProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
1578                          gdk_atom_intern ("WM_WINDOW_ROLE", FALSE), XA_STRING,
1579                          8, PropModeReplace, role, strlen (role));
1580       else
1581         XDeleteProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
1582                          gdk_atom_intern ("WM_WINDOW_ROLE", FALSE));
1583     }
1584 }
1585
1586 void          
1587 gdk_window_set_transient_for (GdkWindow *window, 
1588                               GdkWindow *parent)
1589 {
1590   GdkWindowObject *private;
1591   GdkWindowObject *parent_private;
1592   
1593   g_return_if_fail (window != NULL);
1594   g_return_if_fail (GDK_IS_WINDOW (window));
1595   
1596   private = (GdkWindowObject*) window;
1597   parent_private = (GdkWindowObject*) parent;
1598   
1599   if (!GDK_WINDOW_DESTROYED (window) && !GDK_WINDOW_DESTROYED (parent))
1600     XSetTransientForHint (GDK_WINDOW_XDISPLAY (window), 
1601                           GDK_WINDOW_XID (window),
1602                           GDK_WINDOW_XID (parent));
1603 }
1604
1605 void
1606 gdk_window_set_background (GdkWindow *window,
1607                            GdkColor  *color)
1608 {
1609   GdkWindowObject *private = (GdkWindowObject *)window;
1610   
1611   g_return_if_fail (window != NULL);
1612   g_return_if_fail (GDK_IS_WINDOW (window));
1613   
1614   if (!GDK_WINDOW_DESTROYED (window))
1615     XSetWindowBackground (GDK_WINDOW_XDISPLAY (window),
1616                           GDK_WINDOW_XID (window), color->pixel);
1617
1618   private->bg_color = *color;
1619
1620   if (private->bg_pixmap &&
1621       private->bg_pixmap != GDK_PARENT_RELATIVE_BG &&
1622       private->bg_pixmap != GDK_NO_BG)
1623     {
1624       gdk_pixmap_unref (private->bg_pixmap);
1625       private->bg_pixmap = NULL;
1626     }
1627 }
1628
1629 void
1630 gdk_window_set_back_pixmap (GdkWindow *window,
1631                             GdkPixmap *pixmap,
1632                             gboolean   parent_relative)
1633 {
1634   GdkWindowObject *private = (GdkWindowObject *)window;
1635   Pixmap xpixmap;
1636   
1637   g_return_if_fail (window != NULL);
1638   g_return_if_fail (GDK_IS_WINDOW (window));
1639   g_return_if_fail (pixmap == NULL || !parent_relative);
1640
1641   if (private->bg_pixmap &&
1642       private->bg_pixmap != GDK_PARENT_RELATIVE_BG &&
1643       private->bg_pixmap != GDK_NO_BG)
1644     gdk_pixmap_unref (private->bg_pixmap);
1645
1646   if (parent_relative)
1647     {
1648       xpixmap = ParentRelative;
1649       private->bg_pixmap = GDK_PARENT_RELATIVE_BG;
1650     }
1651   else
1652     {
1653       if (pixmap)
1654         {
1655           gdk_pixmap_ref (pixmap);
1656           private->bg_pixmap = pixmap;
1657           xpixmap = GDK_PIXMAP_XID (pixmap);
1658         }
1659       else
1660         {
1661           xpixmap = None;
1662           private->bg_pixmap = GDK_NO_BG;
1663         }
1664     }
1665   
1666   if (!GDK_WINDOW_DESTROYED (window))
1667     XSetWindowBackgroundPixmap (GDK_WINDOW_XDISPLAY (window),
1668                                 GDK_WINDOW_XID (window), xpixmap);
1669 }
1670
1671 void
1672 gdk_window_set_cursor (GdkWindow *window,
1673                        GdkCursor *cursor)
1674 {
1675   GdkCursorPrivate *cursor_private;
1676   Cursor xcursor;
1677   
1678   g_return_if_fail (window != NULL);
1679   g_return_if_fail (GDK_IS_WINDOW (window));
1680   
1681   cursor_private = (GdkCursorPrivate*) cursor;
1682   
1683   if (!cursor)
1684     xcursor = None;
1685   else
1686     xcursor = cursor_private->xcursor;
1687   
1688   if (!GDK_WINDOW_DESTROYED (window))
1689     XDefineCursor (GDK_WINDOW_XDISPLAY (window),
1690                    GDK_WINDOW_XID (window),
1691                    xcursor);
1692 }
1693
1694 void
1695 gdk_window_get_geometry (GdkWindow *window,
1696                          gint      *x,
1697                          gint      *y,
1698                          gint      *width,
1699                          gint      *height,
1700                          gint      *depth)
1701 {
1702   Window root;
1703   gint tx;
1704   gint ty;
1705   guint twidth;
1706   guint theight;
1707   guint tborder_width;
1708   guint tdepth;
1709   
1710   g_return_if_fail (window == NULL || GDK_IS_WINDOW (window));
1711   
1712   if (!window)
1713     window = gdk_parent_root;
1714   
1715   if (!GDK_WINDOW_DESTROYED (window))
1716     {
1717       XGetGeometry (GDK_WINDOW_XDISPLAY (window),
1718                     GDK_WINDOW_XID (window),
1719                     &root, &tx, &ty, &twidth, &theight, &tborder_width, &tdepth);
1720       
1721       if (x)
1722         *x = tx;
1723       if (y)
1724         *y = ty;
1725       if (width)
1726         *width = twidth;
1727       if (height)
1728         *height = theight;
1729       if (depth)
1730         *depth = tdepth;
1731     }
1732 }
1733
1734 /**
1735  * gdk_window_get_origin:
1736  * @window: a #GdkWindow
1737  * @x: return location for X coordinate
1738  * @y: return location for Y coordinate
1739  * 
1740  * Obtains the position of a window in root window coordinates.
1741  * (Compare with gdk_window_get_position() and
1742  * gdk_window_get_geometry() which return the position of a window
1743  * relative to its parent window.)
1744  * 
1745  * Return value: not meaningful, ignore
1746  **/
1747 gint
1748 gdk_window_get_origin (GdkWindow *window,
1749                        gint      *x,
1750                        gint      *y)
1751 {
1752   gint return_val;
1753   Window child;
1754   gint tx = 0;
1755   gint ty = 0;
1756   
1757   g_return_val_if_fail (window != NULL, 0);
1758   
1759   if (!GDK_WINDOW_DESTROYED (window))
1760     {
1761       return_val = XTranslateCoordinates (GDK_WINDOW_XDISPLAY (window),
1762                                           GDK_WINDOW_XID (window),
1763                                           gdk_root_window,
1764                                           0, 0, &tx, &ty,
1765                                           &child);
1766       
1767     }
1768   else
1769     return_val = 0;
1770   
1771   if (x)
1772     *x = tx;
1773   if (y)
1774     *y = ty;
1775   
1776   return return_val;
1777 }
1778
1779 /**
1780  * gdk_window_get_deskrelative_origin:
1781  * @window: a #GdkWindow
1782  * @x: return location for X coordinate
1783  * @y: return location for Y coordinate
1784  * 
1785  * This gets the origin of a #GdkWindow relative to
1786  * an Enlightenment-window-manager desktop. As long as you don't
1787  * assume that the user's desktop/workspace covers the entire
1788  * root window (i.e. you don't assume that the desktop begins
1789  * at root window coordinate 0,0) this function is not necessary.
1790  * It's deprecated for that reason.
1791  * 
1792  * Return value: not meaningful
1793  **/
1794 gboolean
1795 gdk_window_get_deskrelative_origin (GdkWindow *window,
1796                                     gint      *x,
1797                                     gint      *y)
1798 {
1799   gboolean return_val = FALSE;
1800   gint num_children, format_return;
1801   Window win, *child, parent, root;
1802   gint tx = 0;
1803   gint ty = 0;
1804   Atom type_return;
1805   static Atom atom = 0;
1806   gulong number_return, bytes_after_return;
1807   guchar *data_return;
1808   
1809   g_return_val_if_fail (window != NULL, 0);
1810   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
1811   
1812   if (!GDK_WINDOW_DESTROYED (window))
1813     {
1814       if (!atom)
1815         atom = gdk_atom_intern ("ENLIGHTENMENT_DESKTOP", FALSE);
1816       win = GDK_WINDOW_XID (window);
1817       
1818       while (XQueryTree (GDK_WINDOW_XDISPLAY (window), win, &root, &parent,
1819                          &child, (unsigned int *)&num_children))
1820         {
1821           if ((child) && (num_children > 0))
1822             XFree (child);
1823           
1824           if (!parent)
1825             break;
1826           else
1827             win = parent;
1828           
1829           if (win == root)
1830             break;
1831           
1832           data_return = NULL;
1833           XGetWindowProperty (GDK_WINDOW_XDISPLAY (window), win, atom, 0, 0,
1834                               False, XA_CARDINAL, &type_return, &format_return,
1835                               &number_return, &bytes_after_return, &data_return);
1836           if (type_return == XA_CARDINAL)
1837             {
1838               XFree (data_return);
1839               break;
1840             }
1841         }
1842       
1843       return_val = XTranslateCoordinates (GDK_WINDOW_XDISPLAY (window),
1844                                           GDK_WINDOW_XID (window),
1845                                           win,
1846                                           0, 0, &tx, &ty,
1847                                           &root);
1848       if (x)
1849         *x = tx;
1850       if (y)
1851         *y = ty;
1852     }
1853   
1854   
1855   return return_val;
1856 }
1857
1858 /**
1859  * gdk_window_get_root_origin:
1860  * @window: a #GdkWindow
1861  * @x: return location for X position of window frame
1862  * @y: return location for Y position of window frame
1863  *
1864  * Obtains the top-left corner of the window manager frame in root
1865  * window coordinates.
1866  * 
1867  **/
1868 void
1869 gdk_window_get_root_origin (GdkWindow *window,
1870                             gint      *x,
1871                             gint      *y)
1872 {
1873   GdkRectangle rect;
1874
1875   g_return_if_fail (GDK_IS_WINDOW (window));
1876
1877   gdk_window_get_frame_extents (window, &rect);
1878
1879   if (x)
1880     *x = rect.x;
1881
1882   if (y)
1883     *y = rect.y;
1884 }
1885
1886 /**
1887  * gdk_window_get_frame_extents:
1888  * @window: a #GdkWindow
1889  * @rect: rectangle to fill with bounding box of the window frame
1890  *
1891  * Obtains the bounding box of the window, including window manager
1892  * titlebar/borders if any. The frame position is given in root window
1893  * coordinates. To get the position of the window itself (rather than
1894  * the frame) in root window coordinates, use gdk_window_get_origin().
1895  * 
1896  **/
1897 void
1898 gdk_window_get_frame_extents (GdkWindow    *window,
1899                               GdkRectangle *rect)
1900 {
1901   GdkWindowObject *private;
1902   Window xwindow;
1903   Window xparent;
1904   Window root;
1905   Window *children;
1906   unsigned int nchildren;
1907   
1908   g_return_if_fail (GDK_IS_WINDOW (window));
1909   g_return_if_fail (rect != NULL);
1910   
1911   private = (GdkWindowObject*) window;
1912   
1913   rect->x = 0;
1914   rect->y = 0;
1915   rect->width = 1;
1916   rect->height = 1;
1917   
1918   if (GDK_WINDOW_DESTROYED (window))
1919     return;
1920   
1921   while (private->parent && ((GdkWindowObject*) private->parent)->parent)
1922     private = (GdkWindowObject*) private->parent;
1923   if (GDK_WINDOW_DESTROYED (window))
1924     return;
1925   
1926   xparent = GDK_WINDOW_XID (window);
1927   do
1928     {
1929       xwindow = xparent;
1930       if (!XQueryTree (GDK_WINDOW_XDISPLAY (window), xwindow,
1931                        &root, &xparent,
1932                        &children, &nchildren))
1933         return;
1934       
1935       if (children)
1936         XFree (children);
1937     }
1938   while (xparent != root);
1939   
1940   if (xparent == root)
1941     {
1942       unsigned int ww, wh, wb, wd;
1943       int wx, wy;
1944       
1945       if (XGetGeometry (GDK_WINDOW_XDISPLAY (window), xwindow, &root, &wx, &wy, &ww, &wh, &wb, &wd))
1946         {
1947           rect->x = wx;
1948           rect->y = wy;
1949           rect->width = ww;
1950           rect->height = wh;
1951         }
1952     }
1953 }
1954
1955 GdkWindow*
1956 gdk_window_get_pointer (GdkWindow       *window,
1957                         gint            *x,
1958                         gint            *y,
1959                         GdkModifierType *mask)
1960 {
1961   GdkWindow *return_val;
1962   Window root;
1963   Window child;
1964   int rootx, rooty;
1965   int winx = 0;
1966   int winy = 0;
1967   unsigned int xmask = 0;
1968   gint xoffset, yoffset;
1969
1970   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
1971   
1972   if (!window)
1973     window = gdk_parent_root;
1974   
1975   _gdk_windowing_window_get_offsets (window, &xoffset, &yoffset);
1976
1977   return_val = NULL;
1978   if (!GDK_WINDOW_DESTROYED (window) &&
1979       XQueryPointer (GDK_WINDOW_XDISPLAY (window),
1980                      GDK_WINDOW_XID (window),
1981                      &root, &child, &rootx, &rooty, &winx, &winy, &xmask))
1982     {
1983       if (child)
1984         return_val = gdk_window_lookup (child);
1985     }
1986   
1987   if (x)
1988     *x = winx + xoffset;
1989   if (y)
1990     *y = winy + yoffset;
1991   if (mask)
1992     *mask = xmask;
1993   
1994   return return_val;
1995 }
1996
1997 GdkWindow*
1998 gdk_window_at_pointer (gint *win_x,
1999                        gint *win_y)
2000 {
2001   GdkWindow *window;
2002   Window root;
2003   Window xwindow;
2004   Window xwindow_last = 0;
2005   Display *xdisplay;
2006   int rootx = -1, rooty = -1;
2007   int winx, winy;
2008   unsigned int xmask;
2009   
2010   xwindow = GDK_ROOT_WINDOW ();
2011   xdisplay = GDK_DISPLAY ();
2012
2013   gdk_x11_grab_server ();
2014   while (xwindow)
2015     {
2016       xwindow_last = xwindow;
2017       XQueryPointer (xdisplay, xwindow,
2018                      &root, &xwindow,
2019                      &rootx, &rooty,
2020                      &winx, &winy,
2021                      &xmask);
2022     }
2023   gdk_x11_ungrab_server ();
2024   
2025   window = gdk_window_lookup (xwindow_last);
2026   
2027   if (win_x)
2028     *win_x = window ? winx : -1;
2029   if (win_y)
2030     *win_y = window ? winy : -1;
2031   
2032   return window;
2033 }
2034
2035 GdkEventMask  
2036 gdk_window_get_events (GdkWindow *window)
2037 {
2038   XWindowAttributes attrs;
2039   GdkEventMask event_mask;
2040   int i;
2041   
2042   g_return_val_if_fail (window != NULL, 0);
2043   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
2044
2045   if (GDK_WINDOW_DESTROYED (window))
2046     return 0;
2047   else
2048     {
2049       XGetWindowAttributes (GDK_WINDOW_XDISPLAY (window),
2050                             GDK_WINDOW_XID (window), 
2051                             &attrs);
2052       
2053       event_mask = 0;
2054       for (i = 0; i < gdk_nevent_masks; i++)
2055         {
2056           if (attrs.your_event_mask & gdk_event_mask_table[i])
2057             event_mask |= 1 << (i + 1);
2058         }
2059   
2060       return event_mask;
2061     }
2062 }
2063
2064 void          
2065 gdk_window_set_events (GdkWindow       *window,
2066                        GdkEventMask     event_mask)
2067 {
2068   long xevent_mask;
2069   int i;
2070   
2071   g_return_if_fail (window != NULL);
2072   g_return_if_fail (GDK_IS_WINDOW (window));
2073   
2074   if (!GDK_WINDOW_DESTROYED (window))
2075     {
2076       xevent_mask = StructureNotifyMask;
2077       for (i = 0; i < gdk_nevent_masks; i++)
2078         {
2079           if (event_mask & (1 << (i + 1)))
2080             xevent_mask |= gdk_event_mask_table[i];
2081         }
2082       
2083       XSelectInput (GDK_WINDOW_XDISPLAY (window),
2084                     GDK_WINDOW_XID (window),
2085                     xevent_mask);
2086     }
2087 }
2088
2089 void
2090 gdk_window_add_colormap_windows (GdkWindow *window)
2091 {
2092   GdkWindow *toplevel;
2093   Window *old_windows;
2094   Window *new_windows;
2095   int i, count;
2096   
2097   g_return_if_fail (window != NULL);
2098   g_return_if_fail (GDK_IS_WINDOW (window));
2099   
2100   toplevel = gdk_window_get_toplevel (window);
2101   if (GDK_WINDOW_DESTROYED (toplevel))
2102     return;
2103   
2104   old_windows = NULL;
2105   if (!XGetWMColormapWindows (GDK_WINDOW_XDISPLAY (toplevel),
2106                               GDK_WINDOW_XID (toplevel),
2107                               &old_windows, &count))
2108     {
2109       count = 0;
2110     }
2111   
2112   for (i = 0; i < count; i++)
2113     if (old_windows[i] == GDK_WINDOW_XID (window))
2114       {
2115         XFree (old_windows);
2116         return;
2117       }
2118   
2119   new_windows = g_new (Window, count + 1);
2120   
2121   for (i = 0; i < count; i++)
2122     new_windows[i] = old_windows[i];
2123   new_windows[count] = GDK_WINDOW_XID (window);
2124   
2125   XSetWMColormapWindows (GDK_WINDOW_XDISPLAY (toplevel),
2126                          GDK_WINDOW_XID (toplevel),
2127                          new_windows, count + 1);
2128   
2129   g_free (new_windows);
2130   if (old_windows)
2131     XFree (old_windows);
2132 }
2133
2134 static gboolean
2135 gdk_window_have_shape_ext (void)
2136 {
2137   enum { UNKNOWN, NO, YES };
2138   static gint have_shape = UNKNOWN;
2139   
2140   if (have_shape == UNKNOWN)
2141     {
2142       int ignore;
2143       if (XQueryExtension (gdk_display, "SHAPE", &ignore, &ignore, &ignore))
2144         have_shape = YES;
2145       else
2146         have_shape = NO;
2147     }
2148   
2149   return (have_shape == YES);
2150 }
2151
2152 #define WARN_SHAPE_TOO_BIG() g_warning ("GdkWindow is too large to allow the use of shape masks or shape regions.")
2153
2154 /*
2155  * This needs the X11 shape extension.
2156  * If not available, shaped windows will look
2157  * ugly, but programs still work.    Stefan Wille
2158  */
2159 void
2160 gdk_window_shape_combine_mask (GdkWindow *window,
2161                                GdkBitmap *mask,
2162                                gint x, gint y)
2163 {
2164   Pixmap pixmap;
2165   gint xoffset, yoffset;
2166   
2167   g_return_if_fail (window != NULL);
2168   g_return_if_fail (GDK_IS_WINDOW (window));
2169   
2170 #ifdef HAVE_SHAPE_EXT
2171   if (GDK_WINDOW_DESTROYED (window))
2172     return;
2173
2174   _gdk_windowing_window_get_offsets (window, &xoffset, &yoffset);
2175
2176   if (xoffset != 0 || yoffset != 0)
2177     {
2178       WARN_SHAPE_TOO_BIG ();
2179       return;
2180     }
2181   
2182   if (gdk_window_have_shape_ext ())
2183     {
2184       if (mask)
2185         {
2186           pixmap = GDK_PIXMAP_XID (mask);
2187         }
2188       else
2189         {
2190           x = 0;
2191           y = 0;
2192           pixmap = None;
2193         }
2194       
2195       XShapeCombineMask (GDK_WINDOW_XDISPLAY (window),
2196                          GDK_WINDOW_XID (window),
2197                          ShapeBounding,
2198                          x, y,
2199                          pixmap,
2200                          ShapeSet);
2201     }
2202 #endif /* HAVE_SHAPE_EXT */
2203 }
2204
2205 void
2206 gdk_window_shape_combine_region (GdkWindow *window,
2207                                  GdkRegion *shape_region,
2208                                  gint       offset_x,
2209                                  gint       offset_y)
2210 {
2211   gint xoffset, yoffset;
2212   
2213   g_return_if_fail (GDK_IS_WINDOW (window));
2214   
2215 #ifdef HAVE_SHAPE_EXT
2216   if (GDK_WINDOW_DESTROYED (window))
2217     return;
2218
2219   _gdk_windowing_window_get_offsets (window, &xoffset, &yoffset);
2220
2221   if (xoffset != 0 || yoffset != 0)
2222     {
2223       WARN_SHAPE_TOO_BIG ();
2224       return;
2225     }
2226   
2227   if (shape_region == NULL)
2228     {
2229       /* Use NULL mask to unset the shape */
2230       gdk_window_shape_combine_mask (window, NULL, 0, 0);
2231       return;
2232     }
2233   
2234   if (gdk_window_have_shape_ext ())
2235     {
2236       gint n_rects = 0;
2237       XRectangle *xrects = NULL;
2238
2239       _gdk_region_get_xrectangles (shape_region,
2240                                    0, 0,
2241                                    &xrects, &n_rects);
2242       
2243       XShapeCombineRectangles (GDK_WINDOW_XDISPLAY (window),
2244                                GDK_WINDOW_XID (window),
2245                                ShapeBounding,
2246                                offset_x, offset_y,
2247                                xrects, n_rects,
2248                                ShapeSet,
2249                                YXBanded);
2250
2251       g_free (xrects);
2252     }
2253 #endif /* HAVE_SHAPE_EXT */
2254 }
2255
2256
2257 void
2258 gdk_window_set_override_redirect (GdkWindow *window,
2259                                   gboolean override_redirect)
2260 {
2261   XSetWindowAttributes attr;
2262   
2263   g_return_if_fail (window != NULL);
2264   g_return_if_fail (GDK_IS_WINDOW (window));
2265
2266   if (GDK_WINDOW_DESTROYED (window))
2267     {
2268       attr.override_redirect = (override_redirect == FALSE)?False:True;
2269       XChangeWindowAttributes (GDK_WINDOW_XDISPLAY (window),
2270                                GDK_WINDOW_XID (window),
2271                                CWOverrideRedirect,
2272                                &attr);
2273     }
2274 }
2275
2276
2277 /**
2278  * gdk_window_set_icon_list:
2279  * @window: The #GdkWindow toplevel window to set the icon of.
2280  * @pixbufs: A list of pixbufs, of different sizes.
2281  * @Returns: TRUE if the icons were set, false otherwise
2282  *
2283  * Sets a list of icons for the window. One of these will be used
2284  * to represent the window when it has been iconified. The icon is
2285  * usually shown in an icon box or some sort of task bar. Which icon
2286  * size is shown depends on the window manager. The window manager
2287  * can scale the icon  but setting several size icons can give better
2288  * image quality since the window manager may only need to scale the
2289  * icon by a small amount or not at all.
2290  *
2291  * On the X11 backend this call might fail if the window manager
2292  * doesn't support the Extended Window Manager Hints. Then this
2293  * function returns FALSE, and the application should fall back
2294  * to #gdk_window_set_icon().
2295  **/
2296 gboolean
2297 gdk_window_set_icon_list (GdkWindow *window,
2298                           GList     *pixbufs)
2299 {
2300   guint *data;
2301   guchar *pixels;
2302   guint *p;
2303   gint size;
2304   GList *l;
2305   GdkPixbuf *pixbuf;
2306   gint width, height, stride;
2307   gint x, y;
2308   gint n_channels;
2309   
2310   g_return_val_if_fail (window != NULL, FALSE);
2311   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
2312
2313   if (GDK_WINDOW_DESTROYED (window))
2314     return FALSE;
2315
2316   if (!gdk_net_wm_supports (gdk_atom_intern ("_NET_WM_ICON", FALSE)))
2317     return FALSE;
2318   
2319   l = pixbufs;
2320   size = 0;
2321   
2322   while (l)
2323     {
2324       pixbuf = l->data;
2325       g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
2326
2327       width = gdk_pixbuf_get_width (pixbuf);
2328       height = gdk_pixbuf_get_height (pixbuf);
2329       
2330       size += 2 + width * height;
2331
2332       l = g_list_next (l);
2333     }
2334
2335   data = g_malloc (size*4);
2336
2337   l = pixbufs;
2338   p = data;
2339   while (l)
2340     {
2341       pixbuf = l->data;
2342       
2343       width = gdk_pixbuf_get_width (pixbuf);
2344       height = gdk_pixbuf_get_height (pixbuf);
2345       stride = gdk_pixbuf_get_rowstride (pixbuf);
2346       n_channels = gdk_pixbuf_get_n_channels (pixbuf);
2347       
2348       *p++ = width;
2349       *p++ = height;
2350
2351       pixels = gdk_pixbuf_get_pixels (pixbuf);
2352
2353       for (y = 0; y < height; y++)
2354         {
2355           for (x = 0; x < width; x++)
2356             {
2357               guchar r, g, b, a;
2358               
2359               r = pixels[y*stride + x*n_channels + 0];
2360               g = pixels[y*stride + x*n_channels + 1];
2361               b = pixels[y*stride + x*n_channels + 2];
2362               if (n_channels >= 4)
2363                 a = pixels[y*stride + x*n_channels + 3];
2364               else
2365                 a = 255;
2366               
2367               *p++ = a << 24 | r << 16 | g << 8 | b ;
2368             }
2369         }
2370
2371       l = g_list_next (l);
2372     }
2373
2374   XChangeProperty (GDK_WINDOW_XDISPLAY (window),
2375                    GDK_WINDOW_XID (window),
2376                    gdk_atom_intern ("_NET_WM_ICON", FALSE),
2377                    XA_CARDINAL, 32,
2378                    PropModeReplace,
2379                    (guchar*) data, size);
2380
2381   return TRUE;
2382 }
2383
2384 void          
2385 gdk_window_set_icon (GdkWindow *window, 
2386                      GdkWindow *icon_window,
2387                      GdkPixmap *pixmap,
2388                      GdkBitmap *mask)
2389 {
2390   XWMHints *wm_hints;
2391   
2392   g_return_if_fail (window != NULL);
2393   g_return_if_fail (GDK_IS_WINDOW (window));
2394
2395   if (GDK_WINDOW_DESTROYED (window))
2396     return;
2397
2398   wm_hints = XGetWMHints (GDK_WINDOW_XDISPLAY (window),
2399                           GDK_WINDOW_XID (window));
2400   if (!wm_hints)
2401     wm_hints = XAllocWMHints ();
2402
2403   if (icon_window != NULL)
2404     {
2405       wm_hints->flags |= IconWindowHint;
2406       wm_hints->icon_window = GDK_WINDOW_XID (icon_window);
2407     }
2408   
2409   if (pixmap != NULL)
2410     {
2411       wm_hints->flags |= IconPixmapHint;
2412       wm_hints->icon_pixmap = GDK_PIXMAP_XID (pixmap);
2413     }
2414   
2415   if (mask != NULL)
2416     {
2417       wm_hints->flags |= IconMaskHint;
2418       wm_hints->icon_mask = GDK_PIXMAP_XID (mask);
2419     }
2420
2421   XSetWMHints (GDK_WINDOW_XDISPLAY (window),
2422                GDK_WINDOW_XID (window), wm_hints);
2423   XFree (wm_hints);
2424 }
2425
2426 static gboolean
2427 gdk_window_icon_name_set (GdkWindow *window)
2428 {
2429   return GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (window),
2430                                                g_quark_from_static_string ("gdk-icon-name-set")));
2431 }
2432
2433 void          
2434 gdk_window_set_icon_name (GdkWindow   *window, 
2435                           const gchar *name)
2436 {
2437   g_return_if_fail (window != NULL);
2438   g_return_if_fail (GDK_IS_WINDOW (window));
2439
2440   if (GDK_WINDOW_DESTROYED (window))
2441     return;
2442
2443   g_object_set_qdata (G_OBJECT (window), g_quark_from_static_string ("gdk-icon-name-set"),
2444                       GUINT_TO_POINTER (TRUE));
2445
2446   XChangeProperty (GDK_WINDOW_XDISPLAY (window),
2447                    GDK_WINDOW_XID (window),
2448                    gdk_atom_intern ("_NET_WM_ICON_NAME", FALSE),
2449                    gdk_atom_intern ("UTF8_STRING", FALSE), 8,
2450                    PropModeReplace, name,
2451                    strlen (name));
2452   set_text_property (window, gdk_atom_intern ("WM_ICON_NAME", FALSE), name);
2453 }
2454
2455 void
2456 gdk_window_iconify (GdkWindow *window)
2457 {
2458   Display *display;
2459   GdkWindowObject *private;
2460   
2461   g_return_if_fail (window != NULL);
2462   g_return_if_fail (GDK_IS_WINDOW (window));
2463
2464   if (GDK_WINDOW_DESTROYED (window))
2465     return;
2466
2467   display = GDK_WINDOW_XDISPLAY (window);
2468
2469   private = (GdkWindowObject*) window;
2470
2471   if (GDK_WINDOW_IS_MAPPED (window))
2472     {  
2473       XIconifyWindow (display, GDK_WINDOW_XWINDOW (window), DefaultScreen (display));
2474
2475     }
2476   else
2477     {
2478       /* Flip our client side flag, the real work happens on map. */
2479       gdk_synthesize_window_state (window,
2480                                    0,
2481                                    GDK_WINDOW_STATE_ICONIFIED);
2482     }
2483 }
2484
2485 void
2486 gdk_window_deiconify (GdkWindow *window)
2487 {
2488   Display *display;
2489   GdkWindowObject *private;
2490   
2491   g_return_if_fail (window != NULL);
2492   g_return_if_fail (GDK_IS_WINDOW (window));
2493
2494   if (GDK_WINDOW_DESTROYED (window))
2495     return;
2496
2497   display = GDK_WINDOW_XDISPLAY (window);
2498
2499   private = (GdkWindowObject*) window;
2500
2501   if (GDK_WINDOW_IS_MAPPED (window))
2502     {  
2503       gdk_window_show (window);
2504     }
2505   else
2506     {
2507       /* Flip our client side flag, the real work happens on map. */
2508       gdk_synthesize_window_state (window,
2509                                    GDK_WINDOW_STATE_ICONIFIED,
2510                                    0);
2511     }
2512 }
2513
2514 void
2515 gdk_window_stick (GdkWindow *window)
2516 {
2517   g_return_if_fail (GDK_IS_WINDOW (window));
2518
2519   if (GDK_WINDOW_DESTROYED (window))
2520     return;
2521
2522   if (GDK_WINDOW_IS_MAPPED (window))
2523     {
2524       /* "stick" means stick to all desktops _and_ do not scroll with the
2525        * viewport. i.e. glue to the monitor glass in all cases.
2526        */
2527       
2528       XEvent xev;
2529
2530       /* Request stick during viewport scroll */
2531       gdk_wmspec_change_state (TRUE, window,
2532                                gdk_atom_intern ("_NET_WM_STATE_STICKY", FALSE),
2533                                0);
2534
2535       /* Request desktop 0xFFFFFFFF */
2536       xev.xclient.type = ClientMessage;
2537       xev.xclient.serial = 0;
2538       xev.xclient.send_event = True;
2539       xev.xclient.window = GDK_WINDOW_XWINDOW (window);
2540       xev.xclient.display = gdk_display;
2541       xev.xclient.message_type = gdk_atom_intern ("_NET_WM_DESKTOP", FALSE);
2542       xev.xclient.format = 32;
2543
2544       xev.xclient.data.l[0] = 0xFFFFFFFF;
2545       
2546       XSendEvent (gdk_display, gdk_root_window, False,
2547                   SubstructureRedirectMask | SubstructureNotifyMask,
2548                   &xev);
2549     }
2550   else
2551     {
2552       /* Flip our client side flag, the real work happens on map. */
2553       gdk_synthesize_window_state (window,
2554                                    0,
2555                                    GDK_WINDOW_STATE_STICKY);
2556     }
2557 }
2558
2559 void
2560 gdk_window_unstick (GdkWindow *window)
2561 {
2562   g_return_if_fail (GDK_IS_WINDOW (window));
2563
2564   if (GDK_WINDOW_DESTROYED (window))
2565     return;
2566
2567   if (GDK_WINDOW_IS_MAPPED (window))
2568     {
2569       XEvent xev;
2570       Atom type;
2571       gint format;
2572       gulong nitems;
2573       gulong bytes_after;
2574       gulong *current_desktop;
2575       
2576       /* Request unstick from viewport */
2577       gdk_wmspec_change_state (FALSE, window,
2578                                gdk_atom_intern ("_NET_WM_STATE_STICKY", FALSE),
2579                                0);
2580
2581       /* Get current desktop, then set it; this is a race, but not
2582        * one that matters much in practice.
2583        */
2584       XGetWindowProperty (gdk_display, gdk_root_window,
2585                           gdk_atom_intern ("_NET_CURRENT_DESKTOP", FALSE),
2586                           0, G_MAXLONG,
2587                           False, XA_CARDINAL, &type, &format, &nitems,
2588                           &bytes_after, (guchar **)&current_desktop);
2589
2590       if (type == XA_CARDINAL)
2591         {
2592           xev.xclient.type = ClientMessage;
2593           xev.xclient.serial = 0;
2594           xev.xclient.send_event = True;
2595           xev.xclient.window = GDK_WINDOW_XWINDOW (window);
2596           xev.xclient.display = gdk_display;
2597           xev.xclient.message_type = gdk_atom_intern ("_NET_WM_DESKTOP", FALSE);
2598           xev.xclient.format = 32;
2599
2600           xev.xclient.data.l[0] = *current_desktop;
2601       
2602           XSendEvent (gdk_display, gdk_root_window, False,
2603                       SubstructureRedirectMask | SubstructureNotifyMask,
2604                       &xev);
2605
2606           XFree (current_desktop);
2607         }
2608     }
2609   else
2610     {
2611       /* Flip our client side flag, the real work happens on map. */
2612       gdk_synthesize_window_state (window,
2613                                    GDK_WINDOW_STATE_STICKY,
2614                                    0);
2615
2616     }
2617 }
2618
2619 void
2620 gdk_window_maximize (GdkWindow *window)
2621 {
2622   g_return_if_fail (GDK_IS_WINDOW (window));
2623
2624   if (GDK_WINDOW_DESTROYED (window))
2625     return;
2626
2627   if (GDK_WINDOW_IS_MAPPED (window))
2628     gdk_wmspec_change_state (TRUE, window,
2629                              gdk_atom_intern ("_NET_WM_STATE_MAXIMIZED_VERT", FALSE),
2630                              gdk_atom_intern ("_NET_WM_STATE_MAXIMIZED_HORZ", FALSE));
2631   else
2632     gdk_synthesize_window_state (window,
2633                                  0,
2634                                  GDK_WINDOW_STATE_MAXIMIZED);
2635 }
2636
2637 void
2638 gdk_window_unmaximize (GdkWindow *window)
2639 {
2640   g_return_if_fail (GDK_IS_WINDOW (window));
2641
2642   if (GDK_WINDOW_DESTROYED (window))
2643     return;
2644
2645   if (GDK_WINDOW_IS_MAPPED (window))
2646     gdk_wmspec_change_state (FALSE, window,
2647                              gdk_atom_intern ("_NET_WM_STATE_MAXIMIZED_VERT", FALSE),
2648                              gdk_atom_intern ("_NET_WM_STATE_MAXIMIZED_HORZ", FALSE));
2649   else
2650     gdk_synthesize_window_state (window,
2651                                  GDK_WINDOW_STATE_MAXIMIZED,
2652                                  0);
2653 }
2654
2655 void          
2656 gdk_window_set_group (GdkWindow *window, 
2657                       GdkWindow *leader)
2658 {
2659   XWMHints *wm_hints;
2660   
2661   g_return_if_fail (window != NULL);
2662   g_return_if_fail (GDK_IS_WINDOW (window));
2663   g_return_if_fail (leader != NULL);
2664   g_return_if_fail (GDK_IS_WINDOW (leader));
2665
2666   if (GDK_WINDOW_DESTROYED (window) || GDK_WINDOW_DESTROYED (leader))
2667     return;
2668   
2669   wm_hints = XGetWMHints (GDK_WINDOW_XDISPLAY (window),
2670                           GDK_WINDOW_XID (window));
2671   if (!wm_hints)
2672     wm_hints = XAllocWMHints ();
2673
2674   wm_hints->flags |= WindowGroupHint;
2675   wm_hints->window_group = GDK_WINDOW_XID (leader);
2676
2677   XSetWMHints (GDK_WINDOW_XDISPLAY (window),
2678                GDK_WINDOW_XID (window), wm_hints);
2679   XFree (wm_hints);
2680 }
2681
2682 static MotifWmHints *
2683 gdk_window_get_mwm_hints (GdkWindow *window)
2684 {
2685   static Atom hints_atom = None;
2686   MotifWmHints *hints;
2687   Atom type;
2688   gint format;
2689   gulong nitems;
2690   gulong bytes_after;
2691   
2692   if (GDK_WINDOW_DESTROYED (window))
2693     return NULL;
2694   
2695   if (!hints_atom)
2696     hints_atom = XInternAtom (GDK_WINDOW_XDISPLAY (window), 
2697                               _XA_MOTIF_WM_HINTS, FALSE);
2698   
2699   XGetWindowProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
2700                       hints_atom, 0, sizeof (MotifWmHints)/sizeof (long),
2701                       False, AnyPropertyType, &type, &format, &nitems,
2702                       &bytes_after, (guchar **)&hints);
2703
2704   if (type == None)
2705     return NULL;
2706   
2707   return hints;
2708 }
2709
2710 static void
2711 gdk_window_set_mwm_hints (GdkWindow *window,
2712                           MotifWmHints *new_hints)
2713 {
2714   static Atom hints_atom = None;
2715   MotifWmHints *hints;
2716   Atom type;
2717   gint format;
2718   gulong nitems;
2719   gulong bytes_after;
2720   
2721   if (GDK_WINDOW_DESTROYED (window))
2722     return;
2723   
2724   if (!hints_atom)
2725     hints_atom = XInternAtom (GDK_WINDOW_XDISPLAY (window), 
2726                               _XA_MOTIF_WM_HINTS, FALSE);
2727   
2728   XGetWindowProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
2729                       hints_atom, 0, sizeof (MotifWmHints)/sizeof (long),
2730                       False, AnyPropertyType, &type, &format, &nitems,
2731                       &bytes_after, (guchar **)&hints);
2732   
2733   if (type == None)
2734     hints = new_hints;
2735   else
2736     {
2737       if (new_hints->flags & MWM_HINTS_FUNCTIONS)
2738         {
2739           hints->flags |= MWM_HINTS_FUNCTIONS;
2740           hints->functions = new_hints->functions;
2741         }
2742       if (new_hints->flags & MWM_HINTS_DECORATIONS)
2743         {
2744           hints->flags |= MWM_HINTS_DECORATIONS;
2745           hints->decorations = new_hints->decorations;
2746         }
2747     }
2748   
2749   XChangeProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
2750                    hints_atom, hints_atom, 32, PropModeReplace,
2751                    (guchar *)hints, sizeof (MotifWmHints)/sizeof (long));
2752   
2753   if (hints != new_hints)
2754     XFree (hints);
2755 }
2756
2757 void
2758 gdk_window_set_decorations (GdkWindow      *window,
2759                             GdkWMDecoration decorations)
2760 {
2761   MotifWmHints hints;
2762   
2763   g_return_if_fail (window != NULL);
2764   g_return_if_fail (GDK_IS_WINDOW (window));
2765   
2766   hints.flags = MWM_HINTS_DECORATIONS;
2767   hints.decorations = decorations;
2768   
2769   gdk_window_set_mwm_hints (window, &hints);
2770 }
2771
2772 /**
2773  * gdk_window_get_decorations:
2774  * @window: The #GdkWindow to get the decorations from
2775  * @decorations: The window decorations will be written here
2776  *
2777  * Returns the decorations set on the GdkWindow with #gdk_window_set_decorations
2778  * Returns: TRUE if the window has decorations set, FALSE otherwise.
2779  **/
2780 gboolean
2781 gdk_window_get_decorations(GdkWindow *window,
2782                            GdkWMDecoration *decorations)
2783 {
2784   MotifWmHints *hints;
2785   gboolean result = FALSE;
2786
2787   hints = gdk_window_get_mwm_hints (window);
2788   
2789   if (hints)
2790     {
2791       if (hints->flags & MWM_HINTS_DECORATIONS)
2792         {
2793           *decorations = hints->decorations;
2794           result = TRUE;
2795         }
2796       
2797       XFree (hints);
2798     }
2799
2800   return result;
2801 }
2802
2803 void
2804 gdk_window_set_functions (GdkWindow    *window,
2805                           GdkWMFunction functions)
2806 {
2807   MotifWmHints hints;
2808   
2809   g_return_if_fail (window != NULL);
2810   g_return_if_fail (GDK_IS_WINDOW (window));
2811   
2812   hints.flags = MWM_HINTS_FUNCTIONS;
2813   hints.functions = functions;
2814   
2815   gdk_window_set_mwm_hints (window, &hints);
2816 }
2817
2818 #ifdef HAVE_SHAPE_EXT
2819
2820 /* 
2821  * propagate the shapes from all child windows of a GDK window to the parent 
2822  * window. Shamelessly ripped from Enlightenment's code
2823  * 
2824  * - Raster
2825  */
2826 struct _gdk_span
2827 {
2828   gint                start;
2829   gint                end;
2830   struct _gdk_span    *next;
2831 };
2832
2833 static void
2834 gdk_add_to_span (struct _gdk_span **s,
2835                  gint               x,
2836                  gint               xx)
2837 {
2838   struct _gdk_span *ptr1, *ptr2, *noo, *ss;
2839   gchar             spanning;
2840   
2841   ptr2 = NULL;
2842   ptr1 = *s;
2843   spanning = 0;
2844   ss = NULL;
2845   /* scan the spans for this line */
2846   while (ptr1)
2847     {
2848       /* -- -> new span */
2849       /* == -> existing span */
2850       /* ## -> spans intersect */
2851       /* if we are in the middle of spanning the span into the line */
2852       if (spanning)
2853         {
2854           /* case: ---- ==== */
2855           if (xx < ptr1->start - 1)
2856             {
2857               /* ends before next span - extend to here */
2858               ss->end = xx;
2859               return;
2860             }
2861           /* case: ----##=== */
2862           else if (xx <= ptr1->end)
2863             {
2864               /* crosses into next span - delete next span and append */
2865               ss->end = ptr1->end;
2866               ss->next = ptr1->next;
2867               g_free (ptr1);
2868               return;
2869             }
2870           /* case: ---###--- */
2871           else
2872             {
2873               /* overlaps next span - delete and keep checking */
2874               ss->next = ptr1->next;
2875               g_free (ptr1);
2876               ptr1 = ss;
2877             }
2878         }
2879       /* otherwise havent started spanning it in yet */
2880       else
2881         {
2882           /* case: ---- ==== */
2883           if (xx < ptr1->start - 1)
2884             {
2885               /* insert span here in list */
2886               noo = g_malloc (sizeof (struct _gdk_span));
2887               
2888               if (noo)
2889                 {
2890                   noo->start = x;
2891                   noo->end = xx;
2892                   noo->next = ptr1;
2893                   if (ptr2)
2894                     ptr2->next = noo;
2895                   else
2896                     *s = noo;
2897                 }
2898               return;
2899             }
2900           /* case: ----##=== */
2901           else if ((x < ptr1->start) && (xx <= ptr1->end))
2902             {
2903               /* expand this span to the left point of the new one */
2904               ptr1->start = x;
2905               return;
2906             }
2907           /* case: ===###=== */
2908           else if ((x >= ptr1->start) && (xx <= ptr1->end))
2909             {
2910               /* throw the span away */
2911               return;
2912             }
2913           /* case: ---###--- */
2914           else if ((x < ptr1->start) && (xx > ptr1->end))
2915             {
2916               ss = ptr1;
2917               spanning = 1;
2918               ptr1->start = x;
2919               ptr1->end = xx;
2920             }
2921           /* case: ===##---- */
2922           else if ((x >= ptr1->start) && (x <= ptr1->end + 1) && (xx > ptr1->end))
2923             {
2924               ss = ptr1;
2925               spanning = 1;
2926               ptr1->end = xx;
2927             }
2928           /* case: ==== ---- */
2929           /* case handled by next loop iteration - first case */
2930         }
2931       ptr2 = ptr1;
2932       ptr1 = ptr1->next;
2933     }
2934   /* it started in the middle but spans beyond your current list */
2935   if (spanning)
2936     {
2937       ptr2->end = xx;
2938       return;
2939     }
2940   /* it does not start inside a span or in the middle, so add it to the end */
2941   noo = g_malloc (sizeof (struct _gdk_span));
2942   
2943   if (noo)
2944     {
2945       noo->start = x;
2946       noo->end = xx;
2947       if (ptr2)
2948         {
2949           noo->next = ptr2->next;
2950           ptr2->next = noo;
2951         }
2952       else
2953         {
2954           noo->next = NULL;
2955           *s = noo;
2956         }
2957     }
2958   return;
2959 }
2960
2961 static void
2962 gdk_add_rectangles (Display           *disp,
2963                     Window             win,
2964                     struct _gdk_span **spans,
2965                     gint               basew,
2966                     gint               baseh,
2967                     gint               x,
2968                     gint               y)
2969 {
2970   gint a, k;
2971   gint x1, y1, x2, y2;
2972   gint rn, ord;
2973   XRectangle *rl;
2974   
2975   rl = XShapeGetRectangles (disp, win, ShapeBounding, &rn, &ord);
2976   if (rl)
2977     {
2978       /* go through all clip rects in this window's shape */
2979       for (k = 0; k < rn; k++)
2980         {
2981           /* for each clip rect, add it to each line's spans */
2982           x1 = x + rl[k].x;
2983           x2 = x + rl[k].x + (rl[k].width - 1);
2984           y1 = y + rl[k].y;
2985           y2 = y + rl[k].y + (rl[k].height - 1);
2986           if (x1 < 0)
2987             x1 = 0;
2988           if (y1 < 0)
2989             y1 = 0;
2990           if (x2 >= basew)
2991             x2 = basew - 1;
2992           if (y2 >= baseh)
2993             y2 = baseh - 1;
2994           for (a = y1; a <= y2; a++)
2995             {
2996               if ((x2 - x1) >= 0)
2997                 gdk_add_to_span (&spans[a], x1, x2);
2998             }
2999         }
3000       XFree (rl);
3001     }
3002 }
3003
3004 static void
3005 gdk_propagate_shapes (Display *disp,
3006                       Window   win,
3007                       gboolean merge)
3008 {
3009   Window              rt, par, *list = NULL;
3010   gint                i, j, num = 0, num_rects = 0;
3011   gint                x, y, contig;
3012   guint               w, h, d;
3013   gint                baseh, basew;
3014   XRectangle         *rects = NULL;
3015   struct _gdk_span  **spans = NULL, *ptr1, *ptr2, *ptr3;
3016   XWindowAttributes   xatt;
3017   
3018   XGetGeometry (disp, win, &rt, &x, &y, &w, &h, &d, &d);
3019   if (h <= 0)
3020     return;
3021   basew = w;
3022   baseh = h;
3023   spans = g_malloc (sizeof (struct _gdk_span *) * h);
3024   
3025   for (i = 0; i < h; i++)
3026     spans[i] = NULL;
3027   XQueryTree (disp, win, &rt, &par, &list, (unsigned int *)&num);
3028   if (list)
3029     {
3030       /* go through all child windows and create/insert spans */
3031       for (i = 0; i < num; i++)
3032         {
3033           if (XGetWindowAttributes (disp, list[i], &xatt) && (xatt.map_state != IsUnmapped))
3034             if (XGetGeometry (disp, list[i], &rt, &x, &y, &w, &h, &d, &d))
3035               gdk_add_rectangles (disp, list[i], spans, basew, baseh, x, y);
3036         }
3037       if (merge)
3038         gdk_add_rectangles (disp, win, spans, basew, baseh, x, y);
3039       
3040       /* go through the spans list and build a list of rects */
3041       rects = g_malloc (sizeof (XRectangle) * 256);
3042       num_rects = 0;
3043       for (i = 0; i < baseh; i++)
3044         {
3045           ptr1 = spans[i];
3046           /* go through the line for all spans */
3047           while (ptr1)
3048             {
3049               rects[num_rects].x = ptr1->start;
3050               rects[num_rects].y = i;
3051               rects[num_rects].width = ptr1->end - ptr1->start + 1;
3052               rects[num_rects].height = 1;
3053               j = i + 1;
3054               /* if there are more lines */
3055               contig = 1;
3056               /* while contigous rects (same start/end coords) exist */
3057               while ((contig) && (j < baseh))
3058                 {
3059                   /* search next line for spans matching this one */
3060                   contig = 0;
3061                   ptr2 = spans[j];
3062                   ptr3 = NULL;
3063                   while (ptr2)
3064                     {
3065                       /* if we have an exact span match set contig */
3066                       if ((ptr2->start == ptr1->start) &&
3067                           (ptr2->end == ptr1->end))
3068                         {
3069                           contig = 1;
3070                           /* remove the span - not needed */
3071                           if (ptr3)
3072                             {
3073                               ptr3->next = ptr2->next;
3074                               g_free (ptr2);
3075                               ptr2 = NULL;
3076                             }
3077                           else
3078                             {
3079                               spans[j] = ptr2->next;
3080                               g_free (ptr2);
3081                               ptr2 = NULL;
3082                             }
3083                           break;
3084                         }
3085                       /* gone past the span point no point looking */
3086                       else if (ptr2->start < ptr1->start)
3087                         break;
3088                       if (ptr2)
3089                         {
3090                           ptr3 = ptr2;
3091                           ptr2 = ptr2->next;
3092                         }
3093                     }
3094                   /* if a contiguous span was found increase the rect h */
3095                   if (contig)
3096                     {
3097                       rects[num_rects].height++;
3098                       j++;
3099                     }
3100                 }
3101               /* up the rect count */
3102               num_rects++;
3103               /* every 256 new rects increase the rect array */
3104               if ((num_rects % 256) == 0)
3105                 rects = g_realloc (rects, sizeof (XRectangle) * (num_rects + 256));
3106               ptr1 = ptr1->next;
3107             }
3108         }
3109       /* set the rects as the shape mask */
3110       if (rects)
3111         {
3112           XShapeCombineRectangles (disp, win, ShapeBounding, 0, 0, rects, num_rects,
3113                                    ShapeSet, YXSorted);
3114           g_free (rects);
3115         }
3116       XFree (list);
3117     }
3118   /* free up all the spans we made */
3119   for (i = 0; i < baseh; i++)
3120     {
3121       ptr1 = spans[i];
3122       while (ptr1)
3123         {
3124           ptr2 = ptr1;
3125           ptr1 = ptr1->next;
3126           g_free (ptr2);
3127         }
3128     }
3129   g_free (spans);
3130 }
3131
3132 #endif /* HAVE_SHAPE_EXT */
3133
3134 void
3135 gdk_window_set_child_shapes (GdkWindow *window)
3136 {
3137   g_return_if_fail (window != NULL);
3138   g_return_if_fail (GDK_IS_WINDOW (window));
3139   
3140 #ifdef HAVE_SHAPE_EXT
3141   if (!GDK_WINDOW_DESTROYED (window) &&
3142       gdk_window_have_shape_ext ())
3143     gdk_propagate_shapes (GDK_WINDOW_XDISPLAY (window),
3144                           GDK_WINDOW_XID (window), FALSE);
3145 #endif   
3146 }
3147
3148 void
3149 gdk_window_merge_child_shapes (GdkWindow *window)
3150 {
3151   g_return_if_fail (window != NULL);
3152   g_return_if_fail (GDK_IS_WINDOW (window));
3153   
3154 #ifdef HAVE_SHAPE_EXT
3155   if (!GDK_WINDOW_DESTROYED (window) &&
3156       gdk_window_have_shape_ext ())
3157     gdk_propagate_shapes (GDK_WINDOW_XDISPLAY (window),
3158                           GDK_WINDOW_XID (window), TRUE);
3159 #endif   
3160 }
3161
3162 /* Support for windows that can be guffaw-scrolled
3163  * (See http://www.gtk.org/~otaylor/whitepapers/guffaw-scrolling.txt)
3164  */
3165
3166 static gboolean
3167 gdk_window_gravity_works (void)
3168 {
3169   enum { UNKNOWN, NO, YES };
3170   static gint gravity_works = UNKNOWN;
3171   
3172   if (gravity_works == UNKNOWN)
3173     {
3174       GdkWindowAttr attr;
3175       GdkWindow *parent;
3176       GdkWindow *child;
3177       gint y;
3178       
3179       /* This particular server apparently has a bug so that the test
3180        * works but the actual code crashes it
3181        */
3182       if ((!strcmp (XServerVendor (gdk_display), "Sun Microsystems, Inc.")) &&
3183           (VendorRelease (gdk_display) == 3400))
3184         {
3185           gravity_works = NO;
3186           return FALSE;
3187         }
3188       
3189       attr.window_type = GDK_WINDOW_TEMP;
3190       attr.wclass = GDK_INPUT_OUTPUT;
3191       attr.x = 0;
3192       attr.y = 0;
3193       attr.width = 100;
3194       attr.height = 100;
3195       attr.event_mask = 0;
3196       
3197       parent = gdk_window_new (NULL, &attr, GDK_WA_X | GDK_WA_Y);
3198       
3199       attr.window_type = GDK_WINDOW_CHILD;
3200       child = gdk_window_new (parent, &attr, GDK_WA_X | GDK_WA_Y);
3201       
3202       gdk_window_set_static_win_gravity (child, TRUE);
3203       
3204       gdk_window_resize (parent, 100, 110);
3205       gdk_window_move (parent, 0, -10);
3206       gdk_window_move_resize (parent, 0, 0, 100, 100);
3207       
3208       gdk_window_resize (parent, 100, 110);
3209       gdk_window_move (parent, 0, -10);
3210       gdk_window_move_resize (parent, 0, 0, 100, 100);
3211       
3212       gdk_window_get_geometry (child, NULL, &y, NULL, NULL, NULL);
3213       
3214       gdk_window_destroy (parent);
3215       gdk_window_destroy (child);
3216       
3217       gravity_works = ((y == -20) ? YES : NO);
3218     }
3219   
3220   return (gravity_works == YES);
3221 }
3222
3223 static void
3224 gdk_window_set_static_bit_gravity (GdkWindow *window, gboolean on)
3225 {
3226   XSetWindowAttributes xattributes;
3227   guint xattributes_mask = 0;
3228   
3229   g_return_if_fail (window != NULL);
3230   
3231   xattributes.bit_gravity = StaticGravity;
3232   xattributes_mask |= CWBitGravity;
3233   xattributes.bit_gravity = on ? StaticGravity : ForgetGravity;
3234   XChangeWindowAttributes (GDK_WINDOW_XDISPLAY (window),
3235                            GDK_WINDOW_XID (window),
3236                            CWBitGravity,  &xattributes);
3237 }
3238
3239 static void
3240 gdk_window_set_static_win_gravity (GdkWindow *window, gboolean on)
3241 {
3242   XSetWindowAttributes xattributes;
3243   
3244   g_return_if_fail (window != NULL);
3245   
3246   xattributes.win_gravity = on ? StaticGravity : NorthWestGravity;
3247   
3248   XChangeWindowAttributes (GDK_WINDOW_XDISPLAY (window),
3249                            GDK_WINDOW_XID (window),
3250                            CWWinGravity,  &xattributes);
3251 }
3252
3253 /*************************************************************
3254  * gdk_window_set_static_gravities:
3255  *     Set the bit gravity of the given window to static,
3256  *     and flag it so all children get static subwindow
3257  *     gravity.
3258  *   arguments:
3259  *     window: window for which to set static gravity
3260  *     use_static: Whether to turn static gravity on or off.
3261  *   results:
3262  *     Does the XServer support static gravity?
3263  *************************************************************/
3264
3265 gboolean 
3266 gdk_window_set_static_gravities (GdkWindow *window,
3267                                  gboolean   use_static)
3268 {
3269   GdkWindowObject *private = (GdkWindowObject *)window;
3270   GList *tmp_list;
3271   
3272   g_return_val_if_fail (window != NULL, FALSE);
3273   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
3274
3275   if (!use_static == !private->guffaw_gravity)
3276     return TRUE;
3277   
3278   if (use_static && !gdk_window_gravity_works ())
3279     return FALSE;
3280   
3281   private->guffaw_gravity = use_static;
3282   
3283   if (!GDK_WINDOW_DESTROYED (window))
3284     {
3285       gdk_window_set_static_bit_gravity (window, use_static);
3286       
3287       tmp_list = private->children;
3288       while (tmp_list)
3289         {
3290           gdk_window_set_static_win_gravity (window, use_static);
3291           
3292           tmp_list = tmp_list->next;
3293         }
3294     }
3295   
3296   return TRUE;
3297 }
3298
3299 /* internal function created for and used by gdk_window_xid_at_coords */
3300 Window
3301 gdk_window_xid_at (Window   base,
3302                    gint     bx,
3303                    gint     by,
3304                    gint     x,
3305                    gint     y, 
3306                    GList   *excludes,
3307                    gboolean excl_child)
3308 {
3309   Display *xdisplay;
3310   Window *list = NULL;
3311   Window child = 0, parent_win = 0, root_win = 0;
3312   int i;
3313   unsigned int ww, wh, wb, wd, num;
3314   int wx, wy;
3315   
3316   xdisplay = GDK_DISPLAY ();
3317   if (!XGetGeometry (xdisplay, base, &root_win, &wx, &wy, &ww, &wh, &wb, &wd))
3318     return 0;
3319   wx += bx;
3320   wy += by;
3321   
3322   if (!((x >= wx) &&
3323         (y >= wy) &&
3324         (x < (int) (wx + ww)) &&
3325         (y < (int) (wy + wh))))
3326     return 0;
3327   
3328   if (!XQueryTree (xdisplay, base, &root_win, &parent_win, &list, &num))
3329     return base;
3330   
3331   if (list)
3332     {
3333       for (i = num - 1; ; i--)
3334         {
3335           if ((!excl_child) || (!g_list_find (excludes, (gpointer *) list[i])))
3336             {
3337               if ((child = gdk_window_xid_at (list[i], wx, wy, x, y, excludes, excl_child)) != 0)
3338                 {
3339                   XFree (list);
3340                   return child;
3341                 }
3342             }
3343           if (!i)
3344             break;
3345         }
3346       XFree (list);
3347     }
3348   return base;
3349 }
3350
3351 /* 
3352  * The following fucntion by The Rasterman <raster@redhat.com>
3353  * This function returns the X Window ID in which the x y location is in 
3354  * (x and y being relative to the root window), excluding any windows listed
3355  * in the GList excludes (this is a list of X Window ID's - gpointer being
3356  * the Window ID).
3357  * 
3358  * This is primarily designed for internal gdk use - for DND for example
3359  * when using a shaped icon window as the drag object - you exclude the
3360  * X Window ID of the "icon" (perhaps more if excludes may be needed) and
3361  * You can get back an X Window ID as to what X Window ID is infact under
3362  * those X,Y co-ordinates.
3363  */
3364 Window
3365 gdk_window_xid_at_coords (gint     x,
3366                           gint     y,
3367                           GList   *excludes,
3368                           gboolean excl_child)
3369 {
3370   GdkWindow *window;
3371   Display *xdisplay;
3372   Window *list = NULL;
3373   Window root, child = 0, parent_win = 0, root_win = 0;
3374   unsigned int num;
3375   int i;
3376
3377   window = gdk_parent_root;
3378   xdisplay = GDK_WINDOW_XDISPLAY (window);
3379   root = GDK_WINDOW_XID (window);
3380   num = g_list_length (excludes);
3381   
3382   gdk_x11_grab_server ();
3383   if (!XQueryTree (xdisplay, root, &root_win, &parent_win, &list, &num))
3384     {
3385       gdk_x11_ungrab_server ();
3386       return root;
3387     }
3388   if (list)
3389     {
3390       i = num - 1;
3391       do
3392         {
3393           XWindowAttributes xwa;
3394           
3395           XGetWindowAttributes (xdisplay, list [i], &xwa);
3396           
3397           if (xwa.map_state != IsViewable)
3398             continue;
3399           
3400           if (excl_child && g_list_find (excludes, (gpointer *) list[i]))
3401             continue;
3402           
3403           if ((child = gdk_window_xid_at (list[i], 0, 0, x, y, excludes, excl_child)) == 0)
3404             continue;
3405           
3406           if (excludes)
3407             {
3408               if (!g_list_find (excludes, (gpointer *) child))
3409                 {
3410                   XFree (list);
3411                   gdk_x11_ungrab_server ();
3412                   return child;
3413                 }
3414             }
3415           else
3416             {
3417               XFree (list);
3418               gdk_x11_ungrab_server ();
3419               return child;
3420             }
3421         } while (--i > 0);
3422       XFree (list);
3423     }
3424   gdk_x11_ungrab_server ();
3425   return root;
3426 }
3427
3428 static void
3429 wmspec_moveresize (GdkWindow *window,
3430                    gint       direction,
3431                    gint       root_x,
3432                    gint       root_y,
3433                    guint32    timestamp)     
3434 {
3435   XEvent xev;
3436
3437   /* Release passive grab */
3438   gdk_pointer_ungrab (timestamp);
3439   
3440   xev.xclient.type = ClientMessage;
3441   xev.xclient.serial = 0;
3442   xev.xclient.send_event = True;
3443   xev.xclient.display = gdk_display;
3444   xev.xclient.window = GDK_WINDOW_XID (window);
3445   xev.xclient.message_type = gdk_atom_intern ("_NET_WM_MOVERESIZE", FALSE);
3446   xev.xclient.format = 32;
3447   xev.xclient.data.l[0] = root_x;
3448   xev.xclient.data.l[1] = root_y;
3449   xev.xclient.data.l[2] = direction;
3450   xev.xclient.data.l[3] = 0;
3451   xev.xclient.data.l[4] = 0;
3452   
3453   XSendEvent (gdk_display, gdk_root_window, False,
3454               SubstructureRedirectMask | SubstructureNotifyMask,
3455               &xev);
3456 }
3457
3458 /* From the WM spec */
3459 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT      0
3460 #define _NET_WM_MOVERESIZE_SIZE_TOP          1
3461 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT     2
3462 #define _NET_WM_MOVERESIZE_SIZE_RIGHT        3
3463 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT  4
3464 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM       5
3465 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT   6
3466 #define _NET_WM_MOVERESIZE_SIZE_LEFT         7
3467 #define _NET_WM_MOVERESIZE_MOVE              8
3468
3469 static void
3470 wmspec_resize_drag (GdkWindow     *window,
3471                     GdkWindowEdge  edge,
3472                     gint           button,
3473                     gint           root_x,
3474                     gint           root_y,
3475                     guint32        timestamp)
3476 {
3477   gint direction;
3478   
3479   /* Let the compiler turn a switch into a table, instead
3480    * of doing the table manually, this way is easier to verify.
3481    */
3482   switch (edge)
3483     {
3484     case GDK_WINDOW_EDGE_NORTH_WEST:
3485       direction = _NET_WM_MOVERESIZE_SIZE_TOPLEFT;
3486       break;
3487
3488     case GDK_WINDOW_EDGE_NORTH:
3489       direction = _NET_WM_MOVERESIZE_SIZE_TOP;
3490       break;
3491
3492     case GDK_WINDOW_EDGE_NORTH_EAST:
3493       direction = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT;
3494       break;
3495
3496     case GDK_WINDOW_EDGE_WEST:
3497       direction = _NET_WM_MOVERESIZE_SIZE_LEFT;
3498       break;
3499
3500     case GDK_WINDOW_EDGE_EAST:
3501       direction = _NET_WM_MOVERESIZE_SIZE_RIGHT;
3502       break;
3503
3504     case GDK_WINDOW_EDGE_SOUTH_WEST:
3505       direction = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT;
3506       break;
3507
3508     case GDK_WINDOW_EDGE_SOUTH:
3509       direction = _NET_WM_MOVERESIZE_SIZE_BOTTOM;
3510       break;
3511
3512     case GDK_WINDOW_EDGE_SOUTH_EAST:
3513       direction = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT;
3514       break;
3515
3516     default:
3517       g_warning ("gdk_window_begin_resize_drag: bad resize edge %d!",
3518                  edge);
3519       return;
3520       break;
3521     }
3522   
3523   wmspec_moveresize (window, direction, root_x, root_y, timestamp);
3524 }
3525
3526 /* This is global for use in gdkevents-x11.c */
3527 GdkWindow *_gdk_moveresize_window;
3528
3529 static GdkWindow *moveresize_emulation_window = NULL;
3530 static gboolean is_resize = FALSE;
3531 static GdkWindowEdge resize_edge;
3532 static gint moveresize_button;
3533 static gint moveresize_x;
3534 static gint moveresize_y;
3535 static gint moveresize_orig_x;
3536 static gint moveresize_orig_y;
3537 static gint moveresize_orig_width;
3538 static gint moveresize_orig_height;
3539 static GdkWindowHints moveresize_geom_mask = 0;
3540 static GdkGeometry moveresize_geometry;
3541 static Time moveresize_process_time;
3542
3543 static XEvent *moveresize_pending_event;
3544
3545 static void
3546 update_pos (gint new_root_x,
3547             gint new_root_y)
3548 {
3549   gint dx, dy;
3550   
3551   dx = new_root_x - moveresize_x;
3552   dy = new_root_y - moveresize_y;
3553
3554   if (is_resize)
3555     {
3556       gint w, h;
3557
3558       w = moveresize_orig_width;
3559       h = moveresize_orig_height;
3560       
3561       switch (resize_edge)
3562         {
3563         case GDK_WINDOW_EDGE_SOUTH_EAST:
3564           w += dx;
3565           h += dy;
3566           break;
3567         }
3568
3569       w = MAX (w, 1);
3570       h = MAX (h, 1);
3571       
3572       if (moveresize_geom_mask)
3573         {
3574           gdk_window_constrain_size (&moveresize_geometry,
3575                                      moveresize_geom_mask,
3576                                      w, h,
3577                                      &w, &h);
3578         }
3579       
3580       gdk_window_resize (_gdk_moveresize_window, w, h);
3581     }
3582   else
3583     {
3584       gint x, y;
3585
3586       x = moveresize_orig_x + dx;
3587       y = moveresize_orig_y + dy;
3588       
3589       gdk_window_move (_gdk_moveresize_window, x, y);
3590     }
3591 }
3592
3593 static void
3594 finish_drag (void)
3595 {
3596   gdk_window_destroy (moveresize_emulation_window);
3597   moveresize_emulation_window = NULL;
3598   _gdk_moveresize_window = NULL;
3599
3600   if (moveresize_pending_event)
3601     {
3602       g_free (moveresize_pending_event);
3603       moveresize_pending_event = NULL;
3604     }
3605 }
3606
3607 static int
3608 lookahead_motion_predicate (Display *display,
3609                             XEvent  *event,
3610                             XPointer arg)
3611 {
3612   gboolean *seen_release = (gboolean *)arg;
3613   
3614   if (*seen_release)
3615     return False;
3616
3617   switch (event->xany.type)
3618     {
3619     case ButtonRelease:
3620       *seen_release = TRUE;
3621       break;
3622     case MotionNotify:
3623       moveresize_process_time = event->xmotion.time;
3624       break;
3625     default:
3626       break;
3627     }
3628
3629   return False;
3630 }
3631
3632 static gboolean
3633 moveresize_lookahead (XEvent *event)
3634 {
3635   XEvent tmp_event;
3636   gboolean seen_release = FALSE;
3637
3638   if (moveresize_process_time)
3639     {
3640       if (event->xmotion.time == moveresize_process_time)
3641         {
3642           moveresize_process_time = 0;
3643           return TRUE;
3644         }
3645       else
3646         return FALSE;
3647     }
3648
3649   XCheckIfEvent (gdk_display, &tmp_event,
3650                  lookahead_motion_predicate, (XPointer)&seen_release);
3651
3652   return moveresize_process_time == 0;
3653 }
3654         
3655 void
3656 _gdk_moveresize_handle_event (XEvent *event)
3657 {
3658   guint button_mask = 0;
3659   GdkWindowObject *window_private = (GdkWindowObject *) _gdk_moveresize_window;
3660   
3661   button_mask = GDK_BUTTON1_MASK << (moveresize_button - 1);
3662   
3663   switch (event->xany.type)
3664     {
3665     case MotionNotify:
3666       if (window_private->resize_count > 0)
3667         {
3668           if (moveresize_pending_event)
3669             *moveresize_pending_event = *event;
3670           else
3671             moveresize_pending_event = g_memdup (event, sizeof (XEvent));
3672
3673           break;
3674         }
3675       if (!moveresize_lookahead (event))
3676         break;
3677       
3678       update_pos (event->xmotion.x_root,
3679                   event->xmotion.y_root);
3680       
3681       /* This should never be triggered in normal cases, but in the
3682        * case where the drag started without an implicit grab being
3683        * in effect, we could miss the release if it occurs before
3684        * we grab the pointer; this ensures that we will never
3685        * get a permanently stuck grab.
3686        */
3687       if ((event->xmotion.state & button_mask) == 0)
3688         finish_drag ();
3689       break;
3690
3691     case ButtonRelease:
3692       update_pos (event->xbutton.x_root,
3693                   event->xbutton.y_root);
3694       
3695       if (event->xbutton.button == moveresize_button)
3696         finish_drag ();
3697       break;
3698     }
3699 }
3700
3701 void
3702 _gdk_moveresize_configure_done (void)
3703 {
3704   XEvent *tmp_event;
3705   
3706   if (moveresize_pending_event)
3707     {
3708       tmp_event = moveresize_pending_event;
3709       moveresize_pending_event = NULL;
3710       _gdk_moveresize_handle_event (tmp_event);
3711       g_free (tmp_event);
3712     }
3713 }
3714
3715 static void
3716 create_moveresize_window (guint32 timestamp)
3717 {
3718   GdkWindowAttr attributes;
3719   gint attributes_mask;
3720   GdkGrabStatus status;
3721
3722   g_assert (moveresize_emulation_window == NULL);
3723   
3724   attributes.x = -100;
3725   attributes.y = -100;
3726   attributes.width = 10;
3727   attributes.height = 10;
3728   attributes.window_type = GDK_WINDOW_TEMP;
3729   attributes.wclass = GDK_INPUT_ONLY;
3730   attributes.override_redirect = TRUE;
3731   attributes.event_mask = 0;
3732
3733   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
3734
3735   moveresize_emulation_window =
3736     gdk_window_new (NULL, &attributes, attributes_mask);
3737
3738   gdk_window_show (moveresize_emulation_window);
3739
3740   status = gdk_pointer_grab (moveresize_emulation_window,
3741                              FALSE,
3742                              GDK_BUTTON_RELEASE_MASK |
3743                              GDK_POINTER_MOTION_MASK,
3744                              FALSE,
3745                              NULL,
3746                              timestamp);
3747
3748   if (status != GDK_GRAB_SUCCESS)
3749     {
3750       /* If this fails, some other client has grabbed the window
3751        * already.
3752        */
3753       gdk_window_destroy (moveresize_emulation_window);
3754       moveresize_emulation_window = NULL;
3755     }
3756
3757   moveresize_process_time = 0;
3758 }
3759
3760 static void
3761 emulate_resize_drag (GdkWindow     *window,
3762                      GdkWindowEdge  edge,
3763                      gint           button,
3764                      gint           root_x,
3765                      gint           root_y,
3766                      guint32        timestamp)
3767 {
3768   is_resize = TRUE;
3769   moveresize_button = button;
3770   resize_edge = edge;
3771   moveresize_x = root_x;
3772   moveresize_y = root_y;
3773   _gdk_moveresize_window = GDK_WINDOW (g_object_ref (G_OBJECT (window)));
3774
3775   gdk_window_get_size (window, &moveresize_orig_width, &moveresize_orig_height);
3776   
3777   moveresize_geom_mask = 0;
3778   gdk_window_get_geometry_hints (window,
3779                                  &moveresize_geometry,
3780                                  &moveresize_geom_mask);
3781   
3782   create_moveresize_window (timestamp);
3783 }
3784
3785 static void
3786 emulate_move_drag (GdkWindow     *window,
3787                    gint           button,
3788                    gint           root_x,
3789                    gint           root_y,
3790                    guint32        timestamp)
3791 {
3792   is_resize = FALSE;
3793   moveresize_button = button;
3794   moveresize_x = root_x;
3795   moveresize_y = root_y;
3796   _gdk_moveresize_window = GDK_WINDOW (g_object_ref (G_OBJECT (window)));
3797
3798   gdk_window_get_deskrelative_origin (_gdk_moveresize_window,
3799                                       &moveresize_orig_x,
3800                                       &moveresize_orig_y);
3801   
3802   create_moveresize_window (timestamp);
3803 }
3804
3805 void
3806 gdk_window_begin_resize_drag (GdkWindow     *window,
3807                               GdkWindowEdge  edge,
3808                               gint           button,
3809                               gint           root_x,
3810                               gint           root_y,
3811                               guint32        timestamp)
3812 {
3813   g_return_if_fail (GDK_IS_WINDOW (window));
3814   g_return_if_fail (moveresize_emulation_window == NULL);
3815   
3816   if (GDK_WINDOW_DESTROYED (window))
3817     return;
3818
3819   if (gdk_net_wm_supports (gdk_atom_intern ("_NET_WM_MOVERESIZE", FALSE)))
3820     wmspec_resize_drag (window, edge, button, root_x, root_y, timestamp);
3821   else
3822     emulate_resize_drag (window, edge, button, root_x, root_y, timestamp);
3823 }
3824
3825 void
3826 gdk_window_begin_move_drag (GdkWindow *window,
3827                             gint       button,
3828                             gint       root_x,
3829                             gint       root_y,
3830                             guint32    timestamp)
3831 {
3832   g_return_if_fail (GDK_IS_WINDOW (window));
3833   g_return_if_fail (moveresize_emulation_window == NULL);
3834   
3835   if (GDK_WINDOW_DESTROYED (window))
3836     return;
3837
3838   if (gdk_net_wm_supports (gdk_atom_intern ("_NET_WM_MOVERESIZE", FALSE)))
3839     wmspec_moveresize (window, _NET_WM_MOVERESIZE_MOVE,
3840                        root_x, root_y, timestamp);
3841   else
3842     emulate_move_drag (window, button, root_x, root_y, timestamp);
3843 }
3844