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