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