]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkwindow-x11.c
Supplement the existing WMNormalHints, don't overwrite them.
[~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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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 #include <X11/Xlib.h>
21 #include <X11/Xutil.h>
22 #include <X11/Xatom.h>
23 #include <netinet/in.h>
24 #include "gdk.h"
25 #include "config.h"
26 #include "gdkinput.h"
27 #include "gdkprivate.h"
28 #include "MwmUtil.h"
29
30 #if HAVE_CONFIG_H
31 #  include <config.h>
32 #  if STDC_HEADERS
33 #    include <stdlib.h>
34 #    include <stdio.h>
35 #    include <string.h>
36 #  endif
37 #else
38 #  include <stdlib.h>
39 #  include <stdio.h>
40 #endif
41
42
43 #ifdef HAVE_SHAPE_EXT
44 #include <X11/extensions/shape.h>
45 #endif
46
47 const int gdk_event_mask_table[20] =
48 {
49   ExposureMask,
50   PointerMotionMask,
51   PointerMotionHintMask,
52   ButtonMotionMask,
53   Button1MotionMask,
54   Button2MotionMask,
55   Button3MotionMask,
56   ButtonPressMask | OwnerGrabButtonMask,
57   ButtonReleaseMask | OwnerGrabButtonMask,
58   KeyPressMask,
59   KeyReleaseMask,
60   EnterWindowMask,
61   LeaveWindowMask,
62   FocusChangeMask,
63   StructureNotifyMask,
64   PropertyChangeMask,
65   VisibilityChangeMask,
66   0,                            /* PROXIMITY_IN */
67   0,                            /* PROXIMTY_OUT */
68   SubstructureNotifyMask
69 };
70 const int gdk_nevent_masks = sizeof (gdk_event_mask_table) / sizeof (int);
71
72 /* Forward declarations */
73 static gboolean gdk_window_gravity_works (void);
74 static void     gdk_window_set_static_win_gravity (GdkWindow *window, 
75                                                    gboolean   on);
76 static gboolean gdk_window_have_shape_ext (void);
77
78 /* internal function created for and used by gdk_window_xid_at_coords */
79 Window
80 gdk_window_xid_at (Window   base,
81                    gint     bx,
82                    gint     by,
83                    gint     x,
84                    gint     y, 
85                    GList   *excludes,
86                    gboolean excl_child)
87 {
88   GdkWindow *window;
89   GdkWindowPrivate *private;
90   Display *disp;
91   Window *list = NULL;
92   Window child = 0, parent_win = 0, root_win = 0;
93   int i;
94   unsigned int ww, wh, wb, wd, num;
95   int wx, wy;
96   
97   window = (GdkWindow*) &gdk_root_parent;
98   private = (GdkWindowPrivate*) window;
99   disp = private->xdisplay;
100   if (!XGetGeometry (disp, base, &root_win, &wx, &wy, &ww, &wh, &wb, &wd))
101     return 0;
102   wx += bx;
103   wy += by;
104   
105   if (!((x >= wx) &&
106         (y >= wy) &&
107         (x < (int) (wx + ww)) &&
108         (y < (int) (wy + wh))))
109     return 0;
110   
111   if (!XQueryTree (disp, base, &root_win, &parent_win, &list, &num))
112     return base;
113   
114   if (list)
115     {
116       for (i = num - 1; ; i--)
117         {
118           if ((!excl_child) || (!g_list_find (excludes, (gpointer *) list[i])))
119             {
120               if ((child = gdk_window_xid_at (list[i], wx, wy, x, y, excludes, excl_child)) != 0)
121                 {
122                   XFree (list);
123                   return child;
124                 }
125             }
126           if (!i)
127             break;
128         }
129       XFree (list);
130     }
131   return base;
132 }
133
134 /* 
135  * The following fucntion by The Rasterman <raster@redhat.com>
136  * This function returns the X Window ID in which the x y location is in 
137  * (x and y being relative to the root window), excluding any windows listed
138  * in the GList excludes (this is a list of X Window ID's - gpointer being
139  * the Window ID).
140  * 
141  * This is primarily designed for internal gdk use - for DND for example
142  * when using a shaped icon window as the drag object - you exclude the
143  * X Window ID of the "icon" (perhaps more if excludes may be needed) and
144  * You can get back an X Window ID as to what X Window ID is infact under
145  * those X,Y co-ordinates.
146  */
147 Window
148 gdk_window_xid_at_coords (gint     x,
149                           gint     y,
150                           GList   *excludes,
151                           gboolean excl_child)
152 {
153   GdkWindow *window;
154   GdkWindowPrivate *private;
155   Display *disp;
156   Window *list = NULL;
157   Window root, child = 0, parent_win = 0, root_win = 0;
158   unsigned int num;
159   int i;
160   
161   window = (GdkWindow*) &gdk_root_parent;
162   private = (GdkWindowPrivate*) window;
163   disp = private->xdisplay;
164   root = private->xwindow;
165   num = g_list_length (excludes);
166   
167   XGrabServer (disp);
168   if (!XQueryTree (disp, root, &root_win, &parent_win, &list, &num))
169     {
170       XUngrabServer (disp);
171       return root;
172     }
173   if (list)
174     {
175       i = num - 1;
176       do
177         {
178           XWindowAttributes xwa;
179           
180           XGetWindowAttributes (disp, list [i], &xwa);
181           
182           if (xwa.map_state != IsViewable)
183             continue;
184           
185           if (excl_child && g_list_find (excludes, (gpointer *) list[i]))
186             continue;
187           
188           if ((child = gdk_window_xid_at (list[i], 0, 0, x, y, excludes, excl_child)) == 0)
189             continue;
190           
191           if (excludes)
192             {
193               if (!g_list_find (excludes, (gpointer *) child))
194                 {
195                   XFree (list);
196                   XUngrabServer (disp);
197                   return child;
198                 }
199             }
200           else
201             {
202               XFree (list);
203               XUngrabServer (disp);
204               return child;
205             }
206         } while (--i > 0);
207       XFree (list);
208     }
209   XUngrabServer (disp);
210   return root;
211 }
212
213 void
214 gdk_window_init (void)
215 {
216   XWindowAttributes xattributes;
217   unsigned int width;
218   unsigned int height;
219   unsigned int border_width;
220   unsigned int depth;
221   int x, y;
222   
223   XGetGeometry (gdk_display, gdk_root_window, &gdk_root_window,
224                 &x, &y, &width, &height, &border_width, &depth);
225   XGetWindowAttributes (gdk_display, gdk_root_window, &xattributes);
226   
227   gdk_root_parent.xwindow = gdk_root_window;
228   gdk_root_parent.xdisplay = gdk_display;
229   gdk_root_parent.window_type = GDK_WINDOW_ROOT;
230   gdk_root_parent.window.user_data = NULL;
231   gdk_root_parent.width = width;
232   gdk_root_parent.height = height;
233   gdk_root_parent.children = NULL;
234   gdk_root_parent.colormap = NULL;
235   gdk_root_parent.ref_count = 1;
236   
237   gdk_xid_table_insert (&gdk_root_window, &gdk_root_parent);
238 }
239
240 static GdkAtom wm_client_leader_atom = GDK_NONE;
241
242 GdkWindow*
243 gdk_window_new (GdkWindow     *parent,
244                 GdkWindowAttr *attributes,
245                 gint           attributes_mask)
246 {
247   GdkWindow *window;
248   GdkWindowPrivate *private;
249   GdkWindowPrivate *parent_private;
250   GdkVisual *visual;
251   Display *parent_display;
252   Window xparent;
253   Visual *xvisual;
254   XSetWindowAttributes xattributes;
255   long xattributes_mask;
256   XSizeHints size_hints;
257   XWMHints wm_hints;
258   XClassHint *class_hint;
259   int x, y, depth;
260   unsigned int class;
261   char *title;
262   int i;
263   
264   g_return_val_if_fail (attributes != NULL, NULL);
265   
266   if (!parent)
267     parent = (GdkWindow*) &gdk_root_parent;
268   
269   parent_private = (GdkWindowPrivate*) parent;
270   if (parent_private->destroyed)
271     return NULL;
272   
273   xparent = parent_private->xwindow;
274   parent_display = parent_private->xdisplay;
275   
276   private = g_new (GdkWindowPrivate, 1);
277   window = (GdkWindow*) private;
278   
279   private->parent = parent;
280   
281   private->xdisplay = parent_display;
282   private->destroyed = FALSE;
283   private->mapped = FALSE;
284   private->guffaw_gravity = FALSE;
285   private->resize_count = 0;
286   private->ref_count = 1;
287   xattributes_mask = 0;
288   
289   if (attributes_mask & GDK_WA_X)
290     x = attributes->x;
291   else
292     x = 0;
293   
294   if (attributes_mask & GDK_WA_Y)
295     y = attributes->y;
296   else
297     y = 0;
298   
299   private->x = x;
300   private->y = y;
301   private->width = (attributes->width > 1) ? (attributes->width) : (1);
302   private->height = (attributes->height > 1) ? (attributes->height) : (1);
303   private->window_type = attributes->window_type;
304   private->extension_events = FALSE;
305   
306   private->filters = NULL;
307   private->children = NULL;
308   
309   window->user_data = NULL;
310   
311   if (attributes_mask & GDK_WA_VISUAL)
312     visual = attributes->visual;
313   else
314     visual = gdk_visual_get_system ();
315   xvisual = ((GdkVisualPrivate*) visual)->xvisual;
316   
317   xattributes.event_mask = StructureNotifyMask;
318   for (i = 0; i < gdk_nevent_masks; i++)
319     {
320       if (attributes->event_mask & (1 << (i + 1)))
321         xattributes.event_mask |= gdk_event_mask_table[i];
322     }
323   
324   if (xattributes.event_mask)
325     xattributes_mask |= CWEventMask;
326   
327   if (attributes_mask & GDK_WA_NOREDIR)
328     {
329       xattributes.override_redirect =
330         (attributes->override_redirect == FALSE)?False:True;
331       xattributes_mask |= CWOverrideRedirect;
332     } 
333   else
334     xattributes.override_redirect = False;
335   
336   if (parent_private && parent_private->guffaw_gravity)
337     {
338       xattributes.win_gravity = StaticGravity;
339       xattributes_mask |= CWWinGravity;
340     }
341   
342   if (attributes->wclass == GDK_INPUT_OUTPUT)
343     {
344       class = InputOutput;
345       depth = visual->depth;
346       
347       if (attributes_mask & GDK_WA_COLORMAP)
348         private->colormap = attributes->colormap;
349       else
350         {
351           if ((((GdkVisualPrivate*)gdk_visual_get_system ())->xvisual) == xvisual)
352             private->colormap = gdk_colormap_get_system ();
353           else
354             private->colormap = gdk_colormap_new (visual, False);
355         }
356       
357       xattributes.background_pixel = BlackPixel (gdk_display, gdk_screen);
358       xattributes.border_pixel = BlackPixel (gdk_display, gdk_screen);
359       xattributes_mask |= CWBorderPixel | CWBackPixel;
360       
361       switch (private->window_type)
362         {
363         case GDK_WINDOW_TOPLEVEL:
364           xattributes.colormap = ((GdkColormapPrivate*) private->colormap)->xcolormap;
365           xattributes_mask |= CWColormap;
366           
367           xparent = gdk_root_window;
368           break;
369           
370         case GDK_WINDOW_CHILD:
371           xattributes.colormap = ((GdkColormapPrivate*) private->colormap)->xcolormap;
372           xattributes_mask |= CWColormap;
373           break;
374           
375         case GDK_WINDOW_DIALOG:
376           xattributes.colormap = ((GdkColormapPrivate*) private->colormap)->xcolormap;
377           xattributes_mask |= CWColormap;
378           
379           xparent = gdk_root_window;
380           break;
381           
382         case GDK_WINDOW_TEMP:
383           xattributes.colormap = ((GdkColormapPrivate*) private->colormap)->xcolormap;
384           xattributes_mask |= CWColormap;
385           
386           xparent = gdk_root_window;
387           
388           xattributes.save_under = True;
389           xattributes.override_redirect = True;
390           xattributes.cursor = None;
391           xattributes_mask |= CWSaveUnder | CWOverrideRedirect;
392           break;
393         case GDK_WINDOW_ROOT:
394           g_error ("cannot make windows of type GDK_WINDOW_ROOT");
395           break;
396         case GDK_WINDOW_PIXMAP:
397           g_error ("cannot make windows of type GDK_WINDOW_PIXMAP (use gdk_pixmap_new)");
398           break;
399         }
400     }
401   else
402     {
403       depth = 0;
404       class = InputOnly;
405       private->colormap = NULL;
406     }
407   
408   private->xwindow = XCreateWindow (private->xdisplay, xparent,
409                                     x, y, private->width, private->height,
410                                     0, depth, class, xvisual,
411                                     xattributes_mask, &xattributes);
412   gdk_window_ref (window);
413   gdk_xid_table_insert (&private->xwindow, window);
414   
415   if (private->colormap)
416     gdk_colormap_ref (private->colormap);
417   
418   gdk_window_set_cursor (window, ((attributes_mask & GDK_WA_CURSOR) ?
419                                   (attributes->cursor) :
420                                   NULL));
421   
422   if (parent_private)
423     parent_private->children = g_list_prepend (parent_private->children, window);
424   
425   switch (private->window_type)
426     {
427     case GDK_WINDOW_DIALOG:
428       XSetTransientForHint (private->xdisplay, private->xwindow, xparent);
429     case GDK_WINDOW_TOPLEVEL:
430     case GDK_WINDOW_TEMP:
431       XSetWMProtocols (private->xdisplay, private->xwindow, gdk_wm_window_protocols, 2);
432       break;
433     case GDK_WINDOW_CHILD:
434       if ((attributes->wclass == GDK_INPUT_OUTPUT) &&
435           (private->colormap != gdk_colormap_get_system ()) &&
436           (private->colormap != gdk_window_get_colormap (gdk_window_get_toplevel (window))))
437         {
438           GDK_NOTE (MISC, g_message ("adding colormap window\n"));
439           gdk_window_add_colormap_windows (window);
440         }
441       
442       return window;
443     default:
444       
445       return window;
446     }
447   
448   size_hints.flags = PSize;
449   size_hints.width = private->width;
450   size_hints.height = private->height;
451   
452   wm_hints.flags = InputHint | StateHint | WindowGroupHint;
453   wm_hints.window_group = gdk_leader_window;
454   wm_hints.input = True;
455   wm_hints.initial_state = NormalState;
456   
457   /* FIXME: Is there any point in doing this? Do any WM's pay
458    * attention to PSize, and even if they do, is this the
459    * correct value???
460    */
461   XSetWMNormalHints (private->xdisplay, private->xwindow, &size_hints);
462   
463   XSetWMHints (private->xdisplay, private->xwindow, &wm_hints);
464   
465   if (!wm_client_leader_atom)
466     wm_client_leader_atom = gdk_atom_intern ("WM_CLIENT_LEADER", FALSE);
467   
468   XChangeProperty (private->xdisplay, private->xwindow,
469                    wm_client_leader_atom,
470                    XA_WINDOW, 32, PropModeReplace,
471                    (guchar*) &gdk_leader_window, 1);
472   
473   if (attributes_mask & GDK_WA_TITLE)
474     title = attributes->title;
475   else
476     title = g_get_prgname ();
477   
478   XmbSetWMProperties (private->xdisplay, private->xwindow,
479                       title, title,
480                       NULL, 0,
481                       NULL, NULL, NULL);
482   
483   if (attributes_mask & GDK_WA_WMCLASS)
484     {
485       class_hint = XAllocClassHint ();
486       class_hint->res_name = attributes->wmclass_name;
487       class_hint->res_class = attributes->wmclass_class;
488       XSetClassHint (private->xdisplay, private->xwindow, class_hint);
489       XFree (class_hint);
490     }
491   
492   
493   return window;
494 }
495
496 GdkWindow *
497 gdk_window_foreign_new (guint32 anid)
498 {
499   GdkWindow *window;
500   GdkWindowPrivate *private;
501   GdkWindowPrivate *parent_private;
502   XWindowAttributes attrs;
503   Window root, parent;
504   Window *children = NULL;
505   guint nchildren;
506   
507   if (!XGetWindowAttributes (gdk_display, anid, &attrs)) {
508     g_warning ("XGetWindowAttributes failed on window ID %d\n", anid);
509     return NULL;
510   }
511   
512   private = g_new (GdkWindowPrivate, 1);
513   window = (GdkWindow*) private;
514   
515   /* FIXME: This is pretty expensive. Maybe the caller should supply
516    *        the parent */
517   XQueryTree (gdk_display, anid, &root, &parent, &children, &nchildren);
518   
519   if (children)
520     XFree (children);
521   private->parent = gdk_xid_table_lookup (parent);
522   
523   parent_private = (GdkWindowPrivate *)private->parent;
524   
525   if (parent_private)
526     parent_private->children = g_list_prepend (parent_private->children, window);
527   
528   private->xwindow = anid;
529   private->xdisplay = gdk_display;
530   private->x = attrs.x;
531   private->y = attrs.y;
532   private->width = attrs.width;
533   private->height = attrs.height;
534   private->resize_count = 0;
535   private->ref_count = 1;
536   private->window_type = GDK_WINDOW_FOREIGN;
537   private->destroyed = FALSE;
538   private->mapped = (attrs.map_state != IsUnmapped);
539   private->guffaw_gravity = FALSE;
540   private->extension_events = 0;
541   
542   private->colormap = NULL;
543   
544   private->filters = NULL;
545   private->children = NULL;
546   
547   window->user_data = NULL;
548   
549   gdk_window_ref (window);
550   gdk_xid_table_insert (&private->xwindow, window);
551   
552   return window;
553 }
554
555 /* Call this function when you want a window and all its children to
556  * disappear.  When xdestroy is true, a request to destroy the XWindow
557  * is sent out.  When it is false, it is assumed that the XWindow has
558  * been or will be destroyed by destroying some ancestor of this
559  * window.
560  */
561 static void
562 gdk_window_internal_destroy (GdkWindow *window,
563                              gboolean   xdestroy,
564                              gboolean   our_destroy)
565 {
566   GdkWindowPrivate *private;
567   GdkWindowPrivate *temp_private;
568   GdkWindow *temp_window;
569   GList *children;
570   GList *tmp;
571   
572   g_return_if_fail (window != NULL);
573   
574   private = (GdkWindowPrivate*) window;
575   
576   switch (private->window_type)
577     {
578     case GDK_WINDOW_TOPLEVEL:
579     case GDK_WINDOW_CHILD:
580     case GDK_WINDOW_DIALOG:
581     case GDK_WINDOW_TEMP:
582     case GDK_WINDOW_FOREIGN:
583       if (!private->destroyed)
584         {
585           if (private->parent)
586             {
587               GdkWindowPrivate *parent_private = (GdkWindowPrivate *)private->parent;
588               if (parent_private->children)
589                 parent_private->children = g_list_remove (parent_private->children, window);
590             }
591           
592           if (private->window_type != GDK_WINDOW_FOREIGN)
593             {
594               children = tmp = private->children;
595               private->children = NULL;
596               
597               while (tmp)
598                 {
599                   temp_window = tmp->data;
600                   tmp = tmp->next;
601                   
602                   temp_private = (GdkWindowPrivate*) temp_window;
603                   if (temp_private)
604                     gdk_window_internal_destroy (temp_window, FALSE,
605                                                  our_destroy);
606                 }
607               
608               g_list_free (children);
609             }
610           
611           if (private->extension_events != 0)
612             gdk_input_window_destroy (window);
613           
614           if (private->filters)
615             {
616               tmp = private->filters;
617               
618               while (tmp)
619                 {
620                   g_free (tmp->data);
621                   tmp = tmp->next;
622                 }
623               
624               g_list_free (private->filters);
625               private->filters = NULL;
626             }
627           
628           if (private->window_type == GDK_WINDOW_FOREIGN)
629             {
630               if (our_destroy && (private->parent != NULL))
631                 {
632                   /* It's somebody elses window, but in our heirarchy,
633                    * so reparent it to the root window, and then send
634                    * it a delete event, as if we were a WM
635                    */
636                   XClientMessageEvent xevent;
637                   
638                   gdk_window_hide (window);
639                   gdk_window_reparent (window, NULL, 0, 0);
640                   
641                   xevent.type = ClientMessage;
642                   xevent.window = private->xwindow;
643                   xevent.message_type = gdk_wm_protocols;
644                   xevent.format = 32;
645                   xevent.data.l[0] = gdk_wm_delete_window;
646                   xevent.data.l[1] = CurrentTime;
647                   
648                   XSendEvent (private->xdisplay, private->xwindow,
649                               False, 0, (XEvent *)&xevent);
650                 }
651             }
652           else if (xdestroy)
653             XDestroyWindow (private->xdisplay, private->xwindow);
654           
655           if (private->colormap)
656             gdk_colormap_unref (private->colormap);
657           
658           private->mapped = FALSE;
659           private->destroyed = TRUE;
660         }
661       break;
662       
663     case GDK_WINDOW_ROOT:
664       g_error ("attempted to destroy root window");
665       break;
666       
667     case GDK_WINDOW_PIXMAP:
668       g_error ("called gdk_window_destroy on a pixmap (use gdk_pixmap_unref)");
669       break;
670     }
671 }
672
673 /* Like internal_destroy, but also destroys the reference created by
674    gdk_window_new. */
675
676 void
677 gdk_window_destroy (GdkWindow *window)
678 {
679   gdk_window_internal_destroy (window, TRUE, TRUE);
680   gdk_window_unref (window);
681 }
682
683 /* This function is called when the XWindow is really gone.  */
684
685 void
686 gdk_window_destroy_notify (GdkWindow *window)
687 {
688   GdkWindowPrivate *private;
689   
690   g_return_if_fail (window != NULL);
691   
692   private = (GdkWindowPrivate*) window;
693   
694   if (!private->destroyed)
695     {
696       if (private->window_type == GDK_WINDOW_FOREIGN)
697         gdk_window_internal_destroy (window, FALSE, FALSE);
698       else
699         g_warning ("GdkWindow %#lx unexpectedly destroyed", private->xwindow);
700     }
701   
702   gdk_xid_table_remove (private->xwindow);
703   gdk_window_unref (window);
704 }
705
706 GdkWindow*
707 gdk_window_ref (GdkWindow *window)
708 {
709   GdkWindowPrivate *private = (GdkWindowPrivate *)window;
710   g_return_val_if_fail (window != NULL, NULL);
711   
712   private->ref_count += 1;
713   return window;
714 }
715
716 void
717 gdk_window_unref (GdkWindow *window)
718 {
719   GdkWindowPrivate *private = (GdkWindowPrivate *)window;
720   g_return_if_fail (window != NULL);
721   
722   private->ref_count -= 1;
723   if (private->ref_count == 0)
724     {
725       if (!private->destroyed)
726         {
727           if (private->window_type == GDK_WINDOW_FOREIGN)
728             gdk_xid_table_remove (private->xwindow);
729           else
730             g_warning ("losing last reference to undestroyed window\n");
731         }
732       g_dataset_destroy (window);
733       g_free (window);
734     }
735 }
736
737 void
738 gdk_window_show (GdkWindow *window)
739 {
740   GdkWindowPrivate *private;
741   
742   g_return_if_fail (window != NULL);
743   
744   private = (GdkWindowPrivate*) window;
745   if (!private->destroyed)
746     {
747       private->mapped = TRUE;
748       XRaiseWindow (private->xdisplay, private->xwindow);
749       XMapWindow (private->xdisplay, private->xwindow);
750     }
751 }
752
753 void
754 gdk_window_hide (GdkWindow *window)
755 {
756   GdkWindowPrivate *private;
757   
758   g_return_if_fail (window != NULL);
759   
760   private = (GdkWindowPrivate*) window;
761   if (!private->destroyed)
762     {
763       private->mapped = FALSE;
764       XUnmapWindow (private->xdisplay, private->xwindow);
765     }
766 }
767
768 void
769 gdk_window_withdraw (GdkWindow *window)
770 {
771   GdkWindowPrivate *private;
772   
773   g_return_if_fail (window != NULL);
774   
775   private = (GdkWindowPrivate*) window;
776   if (!private->destroyed)
777     XWithdrawWindow (private->xdisplay, private->xwindow, 0);
778 }
779
780 void
781 gdk_window_move (GdkWindow *window,
782                  gint       x,
783                  gint       y)
784 {
785   GdkWindowPrivate *private;
786   
787   g_return_if_fail (window != NULL);
788   
789   private = (GdkWindowPrivate*) window;
790   if (!private->destroyed)
791     {
792       XMoveWindow (private->xdisplay, private->xwindow, x, y);
793       
794       if (private->window_type == GDK_WINDOW_CHILD)
795         {
796           private->x = x;
797           private->y = y;
798         }
799     }
800 }
801
802 void
803 gdk_window_resize (GdkWindow *window,
804                    gint       width,
805                    gint       height)
806 {
807   GdkWindowPrivate *private;
808   
809   g_return_if_fail (window != NULL);
810   
811   if (width < 1)
812     width = 1;
813   if (height < 1)
814     height = 1;
815   
816   private = (GdkWindowPrivate*) window;
817   
818   if (!private->destroyed &&
819       ((private->resize_count > 0) ||
820        (private->width != (guint16) width) ||
821        (private->height != (guint16) height)))
822     {
823       XResizeWindow (private->xdisplay, private->xwindow, width, height);
824       private->resize_count += 1;
825       
826       if (private->window_type == GDK_WINDOW_CHILD)
827         {
828           private->width = width;
829           private->height = height;
830         }
831     }
832 }
833
834 void
835 gdk_window_move_resize (GdkWindow *window,
836                         gint       x,
837                         gint       y,
838                         gint       width,
839                         gint       height)
840 {
841   GdkWindowPrivate *private;
842   
843   g_return_if_fail (window != NULL);
844   
845   if (width < 1)
846     width = 1;
847   if (height < 1)
848     height = 1;
849   
850   private = (GdkWindowPrivate*) window;
851   if (!private->destroyed)
852     {
853       XMoveResizeWindow (private->xdisplay, private->xwindow, x, y, width, height);
854       
855       if (private->guffaw_gravity)
856         {
857           GList *tmp_list = private->children;
858           while (tmp_list)
859             {
860               GdkWindowPrivate *child_private = tmp_list->data;
861               
862               child_private->x -= x - private->x;
863               child_private->y -= y - private->y;
864               
865               tmp_list = tmp_list->next;
866             }
867         }
868       
869       if (private->window_type == GDK_WINDOW_CHILD)
870         {
871           private->x = x;
872           private->y = y;
873           private->width = width;
874           private->height = height;
875         }
876     }
877 }
878
879 void
880 gdk_window_reparent (GdkWindow *window,
881                      GdkWindow *new_parent,
882                      gint       x,
883                      gint       y)
884 {
885   GdkWindowPrivate *window_private;
886   GdkWindowPrivate *parent_private;
887   GdkWindowPrivate *old_parent_private;
888   
889   g_return_if_fail (window != NULL);
890   
891   if (!new_parent)
892     new_parent = (GdkWindow*) &gdk_root_parent;
893   
894   window_private = (GdkWindowPrivate*) window;
895   old_parent_private = (GdkWindowPrivate*)window_private->parent;
896   parent_private = (GdkWindowPrivate*) new_parent;
897   
898   if (!window_private->destroyed && !parent_private->destroyed)
899     XReparentWindow (window_private->xdisplay,
900                      window_private->xwindow,
901                      parent_private->xwindow,
902                      x, y);
903   
904   window_private->parent = new_parent;
905   
906   if (old_parent_private)
907     old_parent_private->children = g_list_remove (old_parent_private->children, window);
908   
909   if ((old_parent_private &&
910        (!old_parent_private->guffaw_gravity != !parent_private->guffaw_gravity)) ||
911       (!old_parent_private && parent_private->guffaw_gravity))
912     gdk_window_set_static_win_gravity (window, parent_private->guffaw_gravity);
913   
914   parent_private->children = g_list_prepend (parent_private->children, window);
915 }
916
917 void
918 gdk_window_clear (GdkWindow *window)
919 {
920   GdkWindowPrivate *private;
921   
922   g_return_if_fail (window != NULL);
923   
924   private = (GdkWindowPrivate*) window;
925   
926   if (!private->destroyed)
927     XClearWindow (private->xdisplay, private->xwindow);
928 }
929
930 void
931 gdk_window_clear_area (GdkWindow *window,
932                        gint       x,
933                        gint       y,
934                        gint       width,
935                        gint       height)
936 {
937   GdkWindowPrivate *private;
938   
939   g_return_if_fail (window != NULL);
940   
941   private = (GdkWindowPrivate*) window;
942   
943   if (!private->destroyed)
944     XClearArea (private->xdisplay, private->xwindow,
945                 x, y, width, height, False);
946 }
947
948 void
949 gdk_window_clear_area_e (GdkWindow *window,
950                          gint       x,
951                          gint       y,
952                          gint       width,
953                          gint       height)
954 {
955   GdkWindowPrivate *private;
956   
957   g_return_if_fail (window != NULL);
958   
959   private = (GdkWindowPrivate*) window;
960   
961   if (!private->destroyed)
962     XClearArea (private->xdisplay, private->xwindow,
963                 x, y, width, height, True);
964 }
965
966 void
967 gdk_window_copy_area (GdkWindow    *window,
968                       GdkGC        *gc,
969                       gint          x,
970                       gint          y,
971                       GdkWindow    *source_window,
972                       gint          source_x,
973                       gint          source_y,
974                       gint          width,
975                       gint          height)
976 {
977   GdkWindowPrivate *src_private;
978   GdkWindowPrivate *dest_private;
979   GdkGCPrivate *gc_private;
980   
981   g_return_if_fail (window != NULL);
982   g_return_if_fail (gc != NULL);
983   
984   if (source_window == NULL)
985     source_window = window;
986   
987   src_private = (GdkWindowPrivate*) source_window;
988   dest_private = (GdkWindowPrivate*) window;
989   gc_private = (GdkGCPrivate*) gc;
990   
991   if (!src_private->destroyed && !dest_private->destroyed)
992     {
993       XCopyArea (dest_private->xdisplay, src_private->xwindow, dest_private->xwindow,
994                  gc_private->xgc,
995                  source_x, source_y,
996                  width, height,
997                  x, y);
998     }
999 }
1000
1001 void
1002 gdk_window_raise (GdkWindow *window)
1003 {
1004   GdkWindowPrivate *private;
1005   
1006   g_return_if_fail (window != NULL);
1007   
1008   private = (GdkWindowPrivate*) window;
1009   
1010   if (!private->destroyed)
1011     XRaiseWindow (private->xdisplay, private->xwindow);
1012 }
1013
1014 void
1015 gdk_window_lower (GdkWindow *window)
1016 {
1017   GdkWindowPrivate *private;
1018   
1019   g_return_if_fail (window != NULL);
1020   
1021   private = (GdkWindowPrivate*) window;
1022   
1023   if (!private->destroyed)
1024     XLowerWindow (private->xdisplay, private->xwindow);
1025 }
1026
1027 void
1028 gdk_window_set_user_data (GdkWindow *window,
1029                           gpointer   user_data)
1030 {
1031   g_return_if_fail (window != NULL);
1032   
1033   window->user_data = user_data;
1034 }
1035
1036 void
1037 gdk_window_set_hints (GdkWindow *window,
1038                       gint       x,
1039                       gint       y,
1040                       gint       min_width,
1041                       gint       min_height,
1042                       gint       max_width,
1043                       gint       max_height,
1044                       gint       flags)
1045 {
1046   GdkWindowPrivate *private;
1047   XSizeHints size_hints;
1048   
1049   g_return_if_fail (window != NULL);
1050   
1051   private = (GdkWindowPrivate*) window;
1052   if (private->destroyed)
1053     return;
1054   
1055   size_hints.flags = 0;
1056   
1057   if (flags & GDK_HINT_POS)
1058     {
1059       size_hints.flags |= PPosition;
1060       size_hints.x = x;
1061       size_hints.y = y;
1062     }
1063   
1064   if (flags & GDK_HINT_MIN_SIZE)
1065     {
1066       size_hints.flags |= PMinSize;
1067       size_hints.min_width = min_width;
1068       size_hints.min_height = min_height;
1069     }
1070   
1071   if (flags & GDK_HINT_MAX_SIZE)
1072     {
1073       size_hints.flags |= PMaxSize;
1074       size_hints.max_width = max_width;
1075       size_hints.max_height = max_height;
1076     }
1077   
1078   if (flags)
1079     XSetWMNormalHints (private->xdisplay, private->xwindow, &size_hints);
1080 }
1081
1082 void 
1083 gdk_window_set_geometry_hints (GdkWindow      *window,
1084                                GdkGeometry    *geometry,
1085                                GdkWindowHints  geom_mask)
1086 {
1087   GdkWindowPrivate *private;
1088   XSizeHints size_hints;
1089   
1090   g_return_if_fail (window != NULL);
1091   
1092   private = (GdkWindowPrivate*) window;
1093   if (private->destroyed)
1094     return;
1095   
1096   size_hints.flags = 0;
1097   
1098   if (geom_mask & GDK_HINT_POS)
1099     size_hints.flags |= PPosition;
1100   
1101   if (geom_mask & GDK_HINT_MIN_SIZE)
1102     {
1103       size_hints.flags |= PMinSize;
1104       size_hints.min_width = geometry->min_width;
1105       size_hints.min_height = geometry->min_height;
1106     }
1107   
1108   if (geom_mask & GDK_HINT_MAX_SIZE)
1109     {
1110       size_hints.flags |= PMaxSize;
1111       size_hints.max_width = geometry->max_width;
1112       size_hints.max_height = geometry->max_height;
1113     }
1114   
1115   if (geom_mask & GDK_HINT_BASE_SIZE)
1116     {
1117       size_hints.flags |= PBaseSize;
1118       size_hints.base_width = geometry->base_width;
1119       size_hints.base_height = geometry->base_height;
1120     }
1121   
1122   if (geom_mask & GDK_HINT_RESIZE_INC)
1123     {
1124       size_hints.flags |= PResizeInc;
1125       size_hints.width_inc = geometry->width_inc;
1126       size_hints.height_inc = geometry->height_inc;
1127     }
1128   
1129   if (geom_mask & GDK_HINT_ASPECT)
1130     {
1131       size_hints.flags |= PAspect;
1132       if (geometry->min_aspect <= 1)
1133         {
1134           size_hints.min_aspect.x = G_MAXINT * geometry->min_aspect;
1135           size_hints.min_aspect.y = G_MAXINT;
1136         }
1137       else
1138         {
1139           size_hints.min_aspect.x = G_MAXINT;
1140           size_hints.min_aspect.y = G_MAXINT / geometry->min_aspect;;
1141         }
1142       if (geometry->max_aspect <= 1)
1143         {
1144           size_hints.max_aspect.x = G_MAXINT * geometry->max_aspect;
1145           size_hints.max_aspect.y = G_MAXINT;
1146         }
1147       else
1148         {
1149           size_hints.max_aspect.x = G_MAXINT;
1150           size_hints.max_aspect.y = G_MAXINT / geometry->max_aspect;;
1151         }
1152     }
1153   
1154   if (geom_mask)
1155     XSetWMNormalHints (private->xdisplay, private->xwindow, &size_hints);
1156 }
1157
1158 void
1159 gdk_window_set_title (GdkWindow   *window,
1160                       const gchar *title)
1161 {
1162   GdkWindowPrivate *private;
1163   
1164   g_return_if_fail (window != NULL);
1165   
1166   private = (GdkWindowPrivate*) window;
1167   if (!private->destroyed)
1168     XmbSetWMProperties (private->xdisplay, private->xwindow,
1169                         title, title, NULL, 0, NULL, NULL, NULL);
1170 }
1171
1172 void          
1173 gdk_window_set_role (GdkWindow   *window,
1174                      const gchar *role)
1175 {
1176   GdkWindowPrivate *private;
1177   
1178   g_return_if_fail (window != NULL);
1179   
1180   private = (GdkWindowPrivate*) window;
1181   
1182   if (role)
1183     XChangeProperty (private->xdisplay, private->xwindow,
1184                      gdk_atom_intern ("WM_WINDOW_ROLE", FALSE), XA_STRING,
1185                      8, PropModeReplace, role, strlen (role));
1186   else
1187     XDeleteProperty (private->xdisplay, private->xwindow,
1188                      gdk_atom_intern ("WM_WINDOW_ROLE", FALSE));
1189 }
1190
1191 void          
1192 gdk_window_set_transient_for (GdkWindow *window, 
1193                               GdkWindow *parent)
1194 {
1195   GdkWindowPrivate *private;
1196   GdkWindowPrivate *parent_private;
1197   
1198   g_return_if_fail (window != NULL);
1199   
1200   private = (GdkWindowPrivate*) window;
1201   parent_private = (GdkWindowPrivate*) parent;
1202   
1203   if (!private->destroyed && !parent_private->destroyed)
1204     XSetTransientForHint (private->xdisplay, 
1205                           private->xwindow, parent_private->xwindow);
1206 }
1207
1208 void
1209 gdk_window_set_background (GdkWindow *window,
1210                            GdkColor  *color)
1211 {
1212   GdkWindowPrivate *private;
1213   
1214   g_return_if_fail (window != NULL);
1215   
1216   private = (GdkWindowPrivate*) window;
1217   if (!private->destroyed)
1218     XSetWindowBackground (private->xdisplay, private->xwindow, color->pixel);
1219 }
1220
1221 void
1222 gdk_window_set_back_pixmap (GdkWindow *window,
1223                             GdkPixmap *pixmap,
1224                             gint       parent_relative)
1225 {
1226   GdkWindowPrivate *window_private;
1227   GdkPixmapPrivate *pixmap_private;
1228   Pixmap xpixmap;
1229   
1230   g_return_if_fail (window != NULL);
1231   
1232   window_private = (GdkWindowPrivate*) window;
1233   pixmap_private = (GdkPixmapPrivate*) pixmap;
1234   
1235   if (pixmap)
1236     xpixmap = pixmap_private->xwindow;
1237   else
1238     xpixmap = None;
1239   
1240   if (parent_relative)
1241     xpixmap = ParentRelative;
1242   
1243   if (!window_private->destroyed)
1244     XSetWindowBackgroundPixmap (window_private->xdisplay, window_private->xwindow, xpixmap);
1245 }
1246
1247 void
1248 gdk_window_set_cursor (GdkWindow *window,
1249                        GdkCursor *cursor)
1250 {
1251   GdkWindowPrivate *window_private;
1252   GdkCursorPrivate *cursor_private;
1253   Cursor xcursor;
1254   
1255   g_return_if_fail (window != NULL);
1256   
1257   window_private = (GdkWindowPrivate*) window;
1258   cursor_private = (GdkCursorPrivate*) cursor;
1259   
1260   if (!cursor)
1261     xcursor = None;
1262   else
1263     xcursor = cursor_private->xcursor;
1264   
1265   if (!window_private->destroyed)
1266     XDefineCursor (window_private->xdisplay, window_private->xwindow, xcursor);
1267 }
1268
1269 void
1270 gdk_window_set_colormap (GdkWindow   *window,
1271                          GdkColormap *colormap)
1272 {
1273   GdkWindowPrivate *window_private;
1274   GdkColormapPrivate *colormap_private;
1275   
1276   g_return_if_fail (window != NULL);
1277   g_return_if_fail (colormap != NULL);
1278   
1279   window_private = (GdkWindowPrivate*) window;
1280   colormap_private = (GdkColormapPrivate*) colormap;
1281   
1282   if (!window_private->destroyed)
1283     {
1284       XSetWindowColormap (window_private->xdisplay,
1285                           window_private->xwindow,
1286                           colormap_private->xcolormap);
1287       
1288       if (window_private->colormap)
1289         gdk_colormap_unref (window_private->colormap);
1290       window_private->colormap = colormap;
1291       gdk_colormap_ref (window_private->colormap);
1292       
1293       if (window_private->window_type != GDK_WINDOW_TOPLEVEL)
1294         gdk_window_add_colormap_windows (window);
1295     }
1296 }
1297
1298 void
1299 gdk_window_get_user_data (GdkWindow *window,
1300                           gpointer  *data)
1301 {
1302   g_return_if_fail (window != NULL);
1303   
1304   *data = window->user_data;
1305 }
1306
1307 void
1308 gdk_window_get_geometry (GdkWindow *window,
1309                          gint      *x,
1310                          gint      *y,
1311                          gint      *width,
1312                          gint      *height,
1313                          gint      *depth)
1314 {
1315   GdkWindowPrivate *window_private;
1316   Window root;
1317   gint tx;
1318   gint ty;
1319   guint twidth;
1320   guint theight;
1321   guint tborder_width;
1322   guint tdepth;
1323   
1324   if (!window)
1325     window = (GdkWindow*) &gdk_root_parent;
1326   
1327   window_private = (GdkWindowPrivate*) window;
1328   
1329   if (!window_private->destroyed)
1330     {
1331       XGetGeometry (window_private->xdisplay, window_private->xwindow,
1332                     &root, &tx, &ty, &twidth, &theight, &tborder_width, &tdepth);
1333       
1334       if (x)
1335         *x = tx;
1336       if (y)
1337         *y = ty;
1338       if (width)
1339         *width = twidth;
1340       if (height)
1341         *height = theight;
1342       if (depth)
1343         *depth = tdepth;
1344     }
1345 }
1346
1347 void
1348 gdk_window_get_position (GdkWindow *window,
1349                          gint      *x,
1350                          gint      *y)
1351 {
1352   GdkWindowPrivate *window_private;
1353   
1354   g_return_if_fail (window != NULL);
1355   
1356   window_private = (GdkWindowPrivate*) window;
1357   
1358   if (x)
1359     *x = window_private->x;
1360   if (y)
1361     *y = window_private->y;
1362 }
1363
1364 void
1365 gdk_window_get_size (GdkWindow *window,
1366                      gint       *width,
1367                      gint       *height)
1368 {
1369   GdkWindowPrivate *window_private;
1370   
1371   g_return_if_fail (window != NULL);
1372   
1373   window_private = (GdkWindowPrivate*) window;
1374   
1375   if (width)
1376     *width = window_private->width;
1377   if (height)
1378     *height = window_private->height;
1379 }
1380
1381 GdkVisual*
1382 gdk_window_get_visual (GdkWindow *window)
1383 {
1384   GdkWindowPrivate *window_private;
1385   XWindowAttributes window_attributes;
1386   
1387   g_return_val_if_fail (window != NULL, NULL);
1388   
1389   window_private = (GdkWindowPrivate*) window;
1390   /* Huh? ->parent is never set for a pixmap. We should just return
1391    * null immeditately
1392    */
1393   while (window_private && (window_private->window_type == GDK_WINDOW_PIXMAP))
1394     window_private = (GdkWindowPrivate*) window_private->parent;
1395   
1396   if (window_private && !window_private->destroyed)
1397     {
1398       if (window_private->colormap == NULL)
1399         {
1400           XGetWindowAttributes (window_private->xdisplay,
1401                                 window_private->xwindow,
1402                                 &window_attributes);
1403           return gdk_visual_lookup (window_attributes.visual);
1404         }
1405       else
1406         return ((GdkColormapPrivate *)window_private->colormap)->visual;
1407     }
1408   
1409   return NULL;
1410 }
1411
1412 GdkColormap*
1413 gdk_window_get_colormap (GdkWindow *window)
1414 {
1415   GdkWindowPrivate *window_private;
1416   XWindowAttributes window_attributes;
1417   
1418   g_return_val_if_fail (window != NULL, NULL);
1419   window_private = (GdkWindowPrivate*) window;
1420   
1421   g_return_val_if_fail (window_private->window_type != GDK_WINDOW_PIXMAP, NULL);
1422   if (!window_private->destroyed)
1423     {
1424       if (window_private->colormap == NULL)
1425         {
1426           XGetWindowAttributes (window_private->xdisplay,
1427                                 window_private->xwindow,
1428                                 &window_attributes);
1429           return gdk_colormap_lookup (window_attributes.colormap);
1430         }
1431       else
1432         return window_private->colormap;
1433     }
1434   
1435   return NULL;
1436 }
1437
1438 GdkWindowType
1439 gdk_window_get_type (GdkWindow *window)
1440 {
1441   GdkWindowPrivate *window_private;
1442   
1443   g_return_val_if_fail (window != NULL, (GdkWindowType) -1);
1444   
1445   window_private = (GdkWindowPrivate*) window;
1446   return window_private->window_type;
1447 }
1448
1449 gint
1450 gdk_window_get_origin (GdkWindow *window,
1451                        gint      *x,
1452                        gint      *y)
1453 {
1454   GdkWindowPrivate *private;
1455   gint return_val;
1456   Window child;
1457   gint tx = 0;
1458   gint ty = 0;
1459   
1460   g_return_val_if_fail (window != NULL, 0);
1461   
1462   private = (GdkWindowPrivate*) window;
1463   
1464   if (!private->destroyed)
1465     {
1466       return_val = XTranslateCoordinates (private->xdisplay,
1467                                           private->xwindow,
1468                                           gdk_root_window,
1469                                           0, 0, &tx, &ty,
1470                                           &child);
1471       
1472     }
1473   else
1474     return_val = 0;
1475   
1476   if (x)
1477     *x = tx;
1478   if (y)
1479     *y = ty;
1480   
1481   return return_val;
1482 }
1483
1484 gboolean
1485 gdk_window_get_deskrelative_origin (GdkWindow *window,
1486                                     gint      *x,
1487                                     gint      *y)
1488 {
1489   GdkWindowPrivate *private;
1490   gboolean return_val = FALSE;
1491   gint num_children, format_return;
1492   Window win, *child, parent, root;
1493   gint tx = 0;
1494   gint ty = 0;
1495   Atom type_return;
1496   static Atom atom = 0;
1497   gulong number_return, bytes_after_return;
1498   guchar *data_return;
1499   
1500   g_return_val_if_fail (window != NULL, 0);
1501   
1502   private = (GdkWindowPrivate*) window;
1503   
1504   if (!private->destroyed)
1505     {
1506       if (!atom)
1507         atom = XInternAtom (private->xdisplay, "ENLIGHTENMENT_DESKTOP", False);
1508       win = private->xwindow;
1509       
1510       while (XQueryTree (private->xdisplay, win, &root, &parent,
1511                          &child, (unsigned int *)&num_children))
1512         {
1513           if ((child) && (num_children > 0))
1514             XFree (child);
1515           
1516           if (!parent)
1517             break;
1518           else
1519             win = parent;
1520           
1521           if (win == root)
1522             break;
1523           
1524           data_return = NULL;
1525           XGetWindowProperty (private->xdisplay, win, atom, 0, 0,
1526                               False, XA_CARDINAL, &type_return, &format_return,
1527                               &number_return, &bytes_after_return, &data_return);
1528           if (type_return == XA_CARDINAL)
1529             {
1530               XFree (data_return);
1531               break;
1532             }
1533         }
1534       
1535       return_val = XTranslateCoordinates (private->xdisplay,
1536                                           private->xwindow,
1537                                           win,
1538                                           0, 0, &tx, &ty,
1539                                           &root);
1540       if (x)
1541         *x = tx;
1542       if (y)
1543         *y = ty;
1544     }
1545   
1546   
1547   return return_val;
1548 }
1549
1550 void
1551 gdk_window_get_root_origin (GdkWindow *window,
1552                             gint      *x,
1553                             gint      *y)
1554 {
1555   GdkWindowPrivate *private;
1556   Window xwindow;
1557   Window xparent;
1558   Window root;
1559   Window *children;
1560   unsigned int nchildren;
1561   
1562   g_return_if_fail (window != NULL);
1563   
1564   private = (GdkWindowPrivate*) window;
1565   if (x)
1566     *x = 0;
1567   if (y)
1568     *y = 0;
1569   if (private->destroyed)
1570     return;
1571   
1572   while (private->parent && ((GdkWindowPrivate*) private->parent)->parent)
1573     private = (GdkWindowPrivate*) private->parent;
1574   if (private->destroyed)
1575     return;
1576   
1577   xparent = private->xwindow;
1578   do
1579     {
1580       xwindow = xparent;
1581       if (!XQueryTree (private->xdisplay, xwindow,
1582                        &root, &xparent,
1583                        &children, &nchildren))
1584         return;
1585       
1586       if (children)
1587         XFree (children);
1588     }
1589   while (xparent != root);
1590   
1591   if (xparent == root)
1592     {
1593       unsigned int ww, wh, wb, wd;
1594       int wx, wy;
1595       
1596       if (XGetGeometry (private->xdisplay, xwindow, &root, &wx, &wy, &ww, &wh, &wb, &wd))
1597         {
1598           if (x)
1599             *x = wx;
1600           if (y)
1601             *y = wy;
1602         }
1603     }
1604 }
1605
1606 GdkWindow*
1607 gdk_window_get_pointer (GdkWindow       *window,
1608                         gint            *x,
1609                         gint            *y,
1610                         GdkModifierType *mask)
1611 {
1612   GdkWindowPrivate *private;
1613   GdkWindow *return_val;
1614   Window root;
1615   Window child;
1616   int rootx, rooty;
1617   int winx = 0;
1618   int winy = 0;
1619   unsigned int xmask = 0;
1620   
1621   if (!window)
1622     window = (GdkWindow*) &gdk_root_parent;
1623   
1624   private = (GdkWindowPrivate*) window;
1625   
1626   return_val = NULL;
1627   if (!private->destroyed &&
1628       XQueryPointer (private->xdisplay, private->xwindow, &root, &child,
1629                      &rootx, &rooty, &winx, &winy, &xmask))
1630     {
1631       if (child)
1632         return_val = gdk_window_lookup (child);
1633     }
1634   
1635   if (x)
1636     *x = winx;
1637   if (y)
1638     *y = winy;
1639   if (mask)
1640     *mask = xmask;
1641   
1642   return return_val;
1643 }
1644
1645 GdkWindow*
1646 gdk_window_at_pointer (gint *win_x,
1647                        gint *win_y)
1648 {
1649   GdkWindowPrivate *private;
1650   GdkWindow *window;
1651   Window root;
1652   Window xwindow;
1653   Window xwindow_last = 0;
1654   int rootx = -1, rooty = -1;
1655   int winx, winy;
1656   unsigned int xmask;
1657   
1658   private = &gdk_root_parent;
1659   
1660   xwindow = private->xwindow;
1661   
1662   XGrabServer (private->xdisplay);
1663   while (xwindow)
1664     {
1665       xwindow_last = xwindow;
1666       XQueryPointer (private->xdisplay,
1667                      xwindow,
1668                      &root, &xwindow,
1669                      &rootx, &rooty,
1670                      &winx, &winy,
1671                      &xmask);
1672     }
1673   XUngrabServer (private->xdisplay);
1674   
1675   window = gdk_window_lookup (xwindow_last);
1676   
1677   if (win_x)
1678     *win_x = window ? winx : -1;
1679   if (win_y)
1680     *win_y = window ? winy : -1;
1681   
1682   return window;
1683 }
1684
1685 GdkWindow*
1686 gdk_window_get_parent (GdkWindow *window)
1687 {
1688   g_return_val_if_fail (window != NULL, NULL);
1689   
1690   return ((GdkWindowPrivate*) window)->parent;
1691 }
1692
1693 GdkWindow*
1694 gdk_window_get_toplevel (GdkWindow *window)
1695 {
1696   GdkWindowPrivate *private;
1697   
1698   g_return_val_if_fail (window != NULL, NULL);
1699   
1700   private = (GdkWindowPrivate*) window;
1701   
1702   while (private->window_type == GDK_WINDOW_CHILD)
1703     {
1704       window = ((GdkWindowPrivate*) window)->parent;
1705       private = (GdkWindowPrivate*) window;
1706     }
1707   
1708   return window;
1709 }
1710
1711 GList*
1712 gdk_window_get_children (GdkWindow *window)
1713 {
1714   GdkWindowPrivate *private;
1715   GdkWindow *child;
1716   GList *children;
1717   Window root;
1718   Window parent;
1719   Window *xchildren;
1720   unsigned int nchildren;
1721   unsigned int i;
1722   
1723   g_return_val_if_fail (window != NULL, NULL);
1724   
1725   private = (GdkWindowPrivate*) window;
1726   if (private->destroyed)
1727     return NULL;
1728   
1729   XQueryTree (private->xdisplay, private->xwindow,
1730               &root, &parent, &xchildren, &nchildren);
1731   
1732   children = NULL;
1733   
1734   if (nchildren > 0)
1735     {
1736       for (i = 0; i < nchildren; i++)
1737         {
1738           child = gdk_window_lookup (xchildren[i]);
1739           if (child)
1740             children = g_list_prepend (children, child);
1741         }
1742       
1743       if (xchildren)
1744         XFree (xchildren);
1745     }
1746   
1747   return children;
1748 }
1749
1750 GdkEventMask  
1751 gdk_window_get_events (GdkWindow *window)
1752 {
1753   GdkWindowPrivate *private;
1754   XWindowAttributes attrs;
1755   GdkEventMask event_mask;
1756   int i;
1757   
1758   g_return_val_if_fail (window != NULL, 0);
1759   
1760   private = (GdkWindowPrivate*) window;
1761   if (private->destroyed)
1762     return 0;
1763   
1764   XGetWindowAttributes (gdk_display, private->xwindow, 
1765                         &attrs);
1766   
1767   event_mask = 0;
1768   for (i = 0; i < gdk_nevent_masks; i++)
1769     {
1770       if (attrs.your_event_mask & gdk_event_mask_table[i])
1771         event_mask |= 1 << (i + 1);
1772     }
1773   
1774   return event_mask;
1775 }
1776
1777 void          
1778 gdk_window_set_events (GdkWindow       *window,
1779                        GdkEventMask     event_mask)
1780 {
1781   GdkWindowPrivate *private;
1782   long xevent_mask;
1783   int i;
1784   
1785   g_return_if_fail (window != NULL);
1786   
1787   private = (GdkWindowPrivate*) window;
1788   if (private->destroyed)
1789     return;
1790   
1791   xevent_mask = StructureNotifyMask;
1792   for (i = 0; i < gdk_nevent_masks; i++)
1793     {
1794       if (event_mask & (1 << (i + 1)))
1795         xevent_mask |= gdk_event_mask_table[i];
1796     }
1797   
1798   XSelectInput (gdk_display, private->xwindow, 
1799                 xevent_mask);
1800 }
1801
1802 void
1803 gdk_window_add_colormap_windows (GdkWindow *window)
1804 {
1805   GdkWindow *toplevel;
1806   GdkWindowPrivate *toplevel_private;
1807   GdkWindowPrivate *window_private;
1808   Window *old_windows;
1809   Window *new_windows;
1810   int i, count;
1811   
1812   g_return_if_fail (window != NULL);
1813   
1814   toplevel = gdk_window_get_toplevel (window);
1815   toplevel_private = (GdkWindowPrivate*) toplevel;
1816   window_private = (GdkWindowPrivate*) window;
1817   if (window_private->destroyed)
1818     return;
1819   
1820   old_windows = NULL;
1821   if (!XGetWMColormapWindows (toplevel_private->xdisplay,
1822                               toplevel_private->xwindow,
1823                               &old_windows, &count))
1824     {
1825       count = 0;
1826     }
1827   
1828   for (i = 0; i < count; i++)
1829     if (old_windows[i] == window_private->xwindow)
1830       {
1831         XFree (old_windows);
1832         return;
1833       }
1834   
1835   new_windows = g_new (Window, count + 1);
1836   
1837   for (i = 0; i < count; i++)
1838     new_windows[i] = old_windows[i];
1839   new_windows[count] = window_private->xwindow;
1840   
1841   XSetWMColormapWindows (toplevel_private->xdisplay,
1842                          toplevel_private->xwindow,
1843                          new_windows, count + 1);
1844   
1845   g_free (new_windows);
1846   if (old_windows)
1847     XFree (old_windows);
1848 }
1849
1850 static gboolean
1851 gdk_window_have_shape_ext (void)
1852 {
1853   enum { UNKNOWN, NO, YES };
1854   static gint have_shape = UNKNOWN;
1855   
1856   if (have_shape == UNKNOWN)
1857     {
1858       int ignore;
1859       if (XQueryExtension (gdk_display, "SHAPE", &ignore, &ignore, &ignore))
1860         have_shape = YES;
1861       else
1862         have_shape = NO;
1863     }
1864   
1865   return (have_shape == YES);
1866 }
1867
1868 /*
1869  * This needs the X11 shape extension.
1870  * If not available, shaped windows will look
1871  * ugly, but programs still work.    Stefan Wille
1872  */
1873 void
1874 gdk_window_shape_combine_mask (GdkWindow *window,
1875                                GdkBitmap *mask,
1876                                gint x, gint y)
1877 {
1878   GdkWindowPrivate *window_private;
1879   Pixmap pixmap;
1880   
1881   g_return_if_fail (window != NULL);
1882   
1883 #ifdef HAVE_SHAPE_EXT
1884   window_private = (GdkWindowPrivate*) window;
1885   if (window_private->destroyed)
1886     return;
1887   
1888   if (gdk_window_have_shape_ext ())
1889     {
1890       if (mask)
1891         {
1892           GdkWindowPrivate *pixmap_private;
1893           
1894           pixmap_private = (GdkWindowPrivate*) mask;
1895           pixmap = (Pixmap) pixmap_private->xwindow;
1896         }
1897       else
1898         {
1899           x = 0;
1900           y = 0;
1901           pixmap = None;
1902         }
1903       
1904       XShapeCombineMask (window_private->xdisplay,
1905                          window_private->xwindow,
1906                          ShapeBounding,
1907                          x, y,
1908                          pixmap,
1909                          ShapeSet);
1910     }
1911 #endif /* HAVE_SHAPE_EXT */
1912 }
1913
1914 void          
1915 gdk_window_add_filter (GdkWindow     *window,
1916                        GdkFilterFunc  function,
1917                        gpointer       data)
1918 {
1919   GdkWindowPrivate *private;
1920   GList *tmp_list;
1921   GdkEventFilter *filter;
1922   
1923   private = (GdkWindowPrivate*) window;
1924   if (private && private->destroyed)
1925     return;
1926   
1927   if (private)
1928     tmp_list = private->filters;
1929   else
1930     tmp_list = gdk_default_filters;
1931   
1932   while (tmp_list)
1933     {
1934       filter = (GdkEventFilter *)tmp_list->data;
1935       if ((filter->function == function) && (filter->data == data))
1936         return;
1937       tmp_list = tmp_list->next;
1938     }
1939   
1940   filter = g_new (GdkEventFilter, 1);
1941   filter->function = function;
1942   filter->data = data;
1943   
1944   if (private)
1945     private->filters = g_list_append (private->filters, filter);
1946   else
1947     gdk_default_filters = g_list_append (gdk_default_filters, filter);
1948 }
1949
1950 void
1951 gdk_window_remove_filter (GdkWindow     *window,
1952                           GdkFilterFunc  function,
1953                           gpointer       data)
1954 {
1955   GdkWindowPrivate *private;
1956   GList *tmp_list, *node;
1957   GdkEventFilter *filter;
1958   
1959   private = (GdkWindowPrivate*) window;
1960   
1961   if (private)
1962     tmp_list = private->filters;
1963   else
1964     tmp_list = gdk_default_filters;
1965   
1966   while (tmp_list)
1967     {
1968       filter = (GdkEventFilter *)tmp_list->data;
1969       node = tmp_list;
1970       tmp_list = tmp_list->next;
1971       
1972       if ((filter->function == function) && (filter->data == data))
1973         {
1974           if (private)
1975             private->filters = g_list_remove_link (private->filters, node);
1976           else
1977             gdk_default_filters = g_list_remove_link (gdk_default_filters, tmp_list);
1978           g_list_free_1 (node);
1979           g_free (filter);
1980           
1981           return;
1982         }
1983     }
1984 }
1985
1986 void
1987 gdk_window_set_override_redirect (GdkWindow *window,
1988                                   gboolean override_redirect)
1989 {
1990   GdkWindowPrivate *private;
1991   XSetWindowAttributes attr;
1992   
1993   g_return_if_fail (window != NULL);
1994   private = (GdkWindowPrivate*) window;
1995   if (private->destroyed)
1996     return;
1997   
1998   attr.override_redirect = (override_redirect == FALSE)?False:True;
1999   XChangeWindowAttributes (gdk_display,
2000                            ((GdkWindowPrivate *)window)->xwindow,
2001                            CWOverrideRedirect,
2002                            &attr);
2003 }
2004
2005 void          
2006 gdk_window_set_icon (GdkWindow *window, 
2007                      GdkWindow *icon_window,
2008                      GdkPixmap *pixmap,
2009                      GdkBitmap *mask)
2010 {
2011   XWMHints *wm_hints;
2012   GdkWindowPrivate *window_private;
2013   GdkWindowPrivate *private;
2014   
2015   g_return_if_fail (window != NULL);
2016   window_private = (GdkWindowPrivate*) window;
2017   if (window_private->destroyed)
2018     return;
2019
2020   wm_hints = XGetWMHints (window_private->xdisplay, window_private->xwindow);
2021   if (!wm_hints)
2022     wm_hints = XAllocWMHints ();
2023
2024   if (icon_window != NULL)
2025     {
2026       private = (GdkWindowPrivate *)icon_window;
2027       wm_hints->flags |= IconWindowHint;
2028       wm_hints->icon_window = private->xwindow;
2029     }
2030   
2031   if (pixmap != NULL)
2032     {
2033       private = (GdkWindowPrivate *)pixmap;
2034       wm_hints->flags |= IconPixmapHint;
2035       wm_hints->icon_pixmap = private->xwindow;
2036     }
2037   
2038   if (mask != NULL)
2039     {
2040       private = (GdkWindowPrivate *)mask;
2041       wm_hints->flags |= IconMaskHint;
2042       wm_hints->icon_mask = private->xwindow;
2043     }
2044
2045   XSetWMHints (window_private->xdisplay, window_private->xwindow, wm_hints);
2046   XFree (wm_hints);
2047 }
2048
2049 void          
2050 gdk_window_set_icon_name (GdkWindow *window, 
2051                           gchar *    name)
2052 {
2053   GdkWindowPrivate *window_private;
2054   XTextProperty property;
2055   gint res;
2056   
2057   g_return_if_fail (window != NULL);
2058   window_private = (GdkWindowPrivate*) window;
2059   if (window_private->destroyed)
2060     return;
2061   res = XmbTextListToTextProperty (window_private->xdisplay,
2062                                    &name, 1, XStdICCTextStyle,
2063                                    &property);
2064   if (res < 0)
2065     {
2066       g_warning ("Error converting icon name to text property: %d\n", res);
2067       return;
2068     }
2069   
2070   XSetWMIconName (window_private->xdisplay, window_private->xwindow,
2071                   &property);
2072   
2073   if (property.value)
2074     XFree (property.value);
2075 }
2076
2077 void          
2078 gdk_window_set_group (GdkWindow *window, 
2079                       GdkWindow *leader)
2080 {
2081   XWMHints *wm_hints;
2082   GdkWindowPrivate *window_private;
2083   GdkWindowPrivate *private;
2084   
2085   g_return_if_fail (window != NULL);
2086   g_return_if_fail (leader != NULL);
2087   window_private = (GdkWindowPrivate*) window;
2088   if (window_private->destroyed)
2089     return;
2090   
2091   private = (GdkWindowPrivate *)leader;
2092
2093   wm_hints = XGetWMHints (window_private->xdisplay, window_private->xwindow);
2094   if (!wm_hints)
2095     wm_hints = XAllocWMHints ();
2096
2097   wm_hints->flags |= WindowGroupHint;
2098   wm_hints->window_group = private->xwindow;
2099
2100   XSetWMHints (window_private->xdisplay, window_private->xwindow, wm_hints);
2101   XFree (wm_hints);
2102 }
2103
2104 static void
2105 gdk_window_set_mwm_hints (GdkWindow *window,
2106                           MotifWmHints *new_hints)
2107 {
2108   static Atom hints_atom = None;
2109   MotifWmHints *hints;
2110   Atom type;
2111   gint format;
2112   gulong nitems;
2113   gulong bytes_after;
2114   
2115   GdkWindowPrivate *window_private;
2116   
2117   g_return_if_fail (window != NULL);
2118   window_private = (GdkWindowPrivate*) window;
2119   if (window_private->destroyed)
2120     return;
2121   
2122   if (!hints_atom)
2123     hints_atom = XInternAtom (window_private->xdisplay, 
2124                               _XA_MOTIF_WM_HINTS, FALSE);
2125   
2126   XGetWindowProperty (window_private->xdisplay, window_private->xwindow,
2127                       hints_atom, 0, sizeof (MotifWmHints)/4,
2128                       False, AnyPropertyType, &type, &format, &nitems,
2129                       &bytes_after, (guchar **)&hints);
2130   
2131   if (type == None)
2132     hints = new_hints;
2133   else
2134     {
2135       if (new_hints->flags & MWM_HINTS_FUNCTIONS)
2136         {
2137           hints->flags |= MWM_HINTS_FUNCTIONS;
2138           hints->functions = new_hints->functions;
2139         }
2140       if (new_hints->flags & MWM_HINTS_DECORATIONS)
2141         {
2142           hints->flags |= MWM_HINTS_DECORATIONS;
2143           hints->decorations = new_hints->decorations;
2144         }
2145     }
2146   
2147   XChangeProperty (window_private->xdisplay, window_private->xwindow,
2148                    hints_atom, hints_atom, 32, PropModeReplace,
2149                    (guchar *)hints, sizeof (MotifWmHints)/4);
2150   
2151   if (hints != new_hints)
2152     XFree (hints);
2153 }
2154
2155 void
2156 gdk_window_set_decorations (GdkWindow      *window,
2157                             GdkWMDecoration decorations)
2158 {
2159   MotifWmHints hints;
2160   
2161   hints.flags = MWM_HINTS_DECORATIONS;
2162   hints.decorations = decorations;
2163   
2164   gdk_window_set_mwm_hints (window, &hints);
2165 }
2166
2167 void
2168 gdk_window_set_functions (GdkWindow    *window,
2169                           GdkWMFunction functions)
2170 {
2171   MotifWmHints hints;
2172   
2173   hints.flags = MWM_HINTS_FUNCTIONS;
2174   hints.functions = functions;
2175   
2176   gdk_window_set_mwm_hints (window, &hints);
2177 }
2178
2179 GList *
2180 gdk_window_get_toplevels (void)
2181 {
2182   GList *new_list = NULL;
2183   GList *tmp_list;
2184   
2185   tmp_list = gdk_root_parent.children;
2186   while (tmp_list)
2187     {
2188       new_list = g_list_prepend (new_list, tmp_list->data);
2189       tmp_list = tmp_list->next;
2190     }
2191   
2192   return new_list;
2193 }
2194
2195 /* 
2196  * propagate the shapes from all child windows of a GDK window to the parent 
2197  * window. Shamelessly ripped from Enlightenment's code
2198  * 
2199  * - Raster
2200  */
2201
2202 struct _gdk_span
2203 {
2204   gint                start;
2205   gint                end;
2206   struct _gdk_span    *next;
2207 };
2208
2209 static void
2210 gdk_add_to_span (struct _gdk_span **s,
2211                  gint               x,
2212                  gint               xx)
2213 {
2214   struct _gdk_span *ptr1, *ptr2, *noo, *ss;
2215   gchar             spanning;
2216   
2217   ptr2 = NULL;
2218   ptr1 = *s;
2219   spanning = 0;
2220   ss = NULL;
2221   /* scan the spans for this line */
2222   while (ptr1)
2223     {
2224       /* -- -> new span */
2225       /* == -> existing span */
2226       /* ## -> spans intersect */
2227       /* if we are in the middle of spanning the span into the line */
2228       if (spanning)
2229         {
2230           /* case: ---- ==== */
2231           if (xx < ptr1->start - 1)
2232             {
2233               /* ends before next span - extend to here */
2234               ss->end = xx;
2235               return;
2236             }
2237           /* case: ----##=== */
2238           else if (xx <= ptr1->end)
2239             {
2240               /* crosses into next span - delete next span and append */
2241               ss->end = ptr1->end;
2242               ss->next = ptr1->next;
2243               g_free (ptr1);
2244               return;
2245             }
2246           /* case: ---###--- */
2247           else
2248             {
2249               /* overlaps next span - delete and keep checking */
2250               ss->next = ptr1->next;
2251               g_free (ptr1);
2252               ptr1 = ss;
2253             }
2254         }
2255       /* otherwise havent started spanning it in yet */
2256       else
2257         {
2258           /* case: ---- ==== */
2259           if (xx < ptr1->start - 1)
2260             {
2261               /* insert span here in list */
2262               noo = g_malloc (sizeof (struct _gdk_span));
2263               
2264               if (noo)
2265                 {
2266                   noo->start = x;
2267                   noo->end = xx;
2268                   noo->next = ptr1;
2269                   if (ptr2)
2270                     ptr2->next = noo;
2271                   else
2272                     *s = noo;
2273                 }
2274               return;
2275             }
2276           /* case: ----##=== */
2277           else if ((x < ptr1->start) && (xx <= ptr1->end))
2278             {
2279               /* expand this span to the left point of the new one */
2280               ptr1->start = x;
2281               return;
2282             }
2283           /* case: ===###=== */
2284           else if ((x >= ptr1->start) && (xx <= ptr1->end))
2285             {
2286               /* throw the span away */
2287               return;
2288             }
2289           /* case: ---###--- */
2290           else if ((x < ptr1->start) && (xx > ptr1->end))
2291             {
2292               ss = ptr1;
2293               spanning = 1;
2294               ptr1->start = x;
2295               ptr1->end = xx;
2296             }
2297           /* case: ===##---- */
2298           else if ((x >= ptr1->start) && (x <= ptr1->end + 1) && (xx > ptr1->end))
2299             {
2300               ss = ptr1;
2301               spanning = 1;
2302               ptr1->end = xx;
2303             }
2304           /* case: ==== ---- */
2305           /* case handled by next loop iteration - first case */
2306         }
2307       ptr2 = ptr1;
2308       ptr1 = ptr1->next;
2309     }
2310   /* it started in the middle but spans beyond your current list */
2311   if (spanning)
2312     {
2313       ptr2->end = xx;
2314       return;
2315     }
2316   /* it does not start inside a span or in the middle, so add it to the end */
2317   noo = g_malloc (sizeof (struct _gdk_span));
2318   
2319   if (noo)
2320     {
2321       noo->start = x;
2322       noo->end = xx;
2323       if (ptr2)
2324         {
2325           noo->next = ptr2->next;
2326           ptr2->next = noo;
2327         }
2328       else
2329         {
2330           noo->next = NULL;
2331           *s = noo;
2332         }
2333     }
2334   return;
2335 }
2336
2337 static void
2338 gdk_add_rectangles (Display           *disp,
2339                     Window             win,
2340                     struct _gdk_span **spans,
2341                     gint               basew,
2342                     gint               baseh,
2343                     gint               x,
2344                     gint               y)
2345 {
2346   gint a, k;
2347   gint x1, y1, x2, y2;
2348   gint rn, ord;
2349   XRectangle *rl;
2350   
2351   rl = XShapeGetRectangles (disp, win, ShapeBounding, &rn, &ord);
2352   if (rl)
2353     {
2354       /* go through all clip rects in this window's shape */
2355       for (k = 0; k < rn; k++)
2356         {
2357           /* for each clip rect, add it to each line's spans */
2358           x1 = x + rl[k].x;
2359           x2 = x + rl[k].x + (rl[k].width - 1);
2360           y1 = y + rl[k].y;
2361           y2 = y + rl[k].y + (rl[k].height - 1);
2362           if (x1 < 0)
2363             x1 = 0;
2364           if (y1 < 0)
2365             y1 = 0;
2366           if (x2 >= basew)
2367             x2 = basew - 1;
2368           if (y2 >= baseh)
2369             y2 = baseh - 1;
2370           for (a = y1; a <= y2; a++)
2371             {
2372               if ((x2 - x1) >= 0)
2373                 gdk_add_to_span (&spans[a], x1, x2);
2374             }
2375         }
2376       XFree (rl);
2377     }
2378 }
2379
2380 static void
2381 gdk_propagate_shapes (Display *disp,
2382                       Window   win,
2383                       gboolean merge)
2384 {
2385   Window              rt, par, *list = NULL;
2386   gint                i, j, num = 0, num_rects = 0;
2387   gint                x, y, contig;
2388   guint               w, h, d;
2389   gint                baseh, basew;
2390   XRectangle         *rects = NULL;
2391   struct _gdk_span  **spans = NULL, *ptr1, *ptr2, *ptr3;
2392   XWindowAttributes   xatt;
2393   
2394   XGetGeometry (disp, win, &rt, &x, &y, &w, &h, &d, &d);
2395   if (h <= 0)
2396     return;
2397   basew = w;
2398   baseh = h;
2399   spans = g_malloc (sizeof (struct _gdk_span *) * h);
2400   
2401   for (i = 0; i < h; i++)
2402     spans[i] = NULL;
2403   XQueryTree (disp, win, &rt, &par, &list, (unsigned int *)&num);
2404   if (list)
2405     {
2406       /* go through all child windows and create/insert spans */
2407       for (i = 0; i < num; i++)
2408         {
2409           if (XGetWindowAttributes (disp, list[i], &xatt) && (xatt.map_state != IsUnmapped))
2410             if (XGetGeometry (disp, list[i], &rt, &x, &y, &w, &h, &d, &d))
2411               gdk_add_rectangles (disp, list[i], spans, basew, baseh, x, y);
2412         }
2413       if (merge)
2414         gdk_add_rectangles (disp, win, spans, basew, baseh, x, y);
2415       
2416       /* go through the spans list and build a list of rects */
2417       rects = g_malloc (sizeof (XRectangle) * 256);
2418       num_rects = 0;
2419       for (i = 0; i < baseh; i++)
2420         {
2421           ptr1 = spans[i];
2422           /* go through the line for all spans */
2423           while (ptr1)
2424             {
2425               rects[num_rects].x = ptr1->start;
2426               rects[num_rects].y = i;
2427               rects[num_rects].width = ptr1->end - ptr1->start + 1;
2428               rects[num_rects].height = 1;
2429               j = i + 1;
2430               /* if there are more lines */
2431               contig = 1;
2432               /* while contigous rects (same start/end coords) exist */
2433               while ((contig) && (j < baseh))
2434                 {
2435                   /* search next line for spans matching this one */
2436                   contig = 0;
2437                   ptr2 = spans[j];
2438                   ptr3 = NULL;
2439                   while (ptr2)
2440                     {
2441                       /* if we have an exact span match set contig */
2442                       if ((ptr2->start == ptr1->start) &&
2443                           (ptr2->end == ptr1->end))
2444                         {
2445                           contig = 1;
2446                           /* remove the span - not needed */
2447                           if (ptr3)
2448                             {
2449                               ptr3->next = ptr2->next;
2450                               g_free (ptr2);
2451                               ptr2 = NULL;
2452                             }
2453                           else
2454                             {
2455                               spans[j] = ptr2->next;
2456                               g_free (ptr2);
2457                               ptr2 = NULL;
2458                             }
2459                           break;
2460                         }
2461                       /* gone past the span point no point looking */
2462                       else if (ptr2->start < ptr1->start)
2463                         break;
2464                       if (ptr2)
2465                         {
2466                           ptr3 = ptr2;
2467                           ptr2 = ptr2->next;
2468                         }
2469                     }
2470                   /* if a contiguous span was found increase the rect h */
2471                   if (contig)
2472                     {
2473                       rects[num_rects].height++;
2474                       j++;
2475                     }
2476                 }
2477               /* up the rect count */
2478               num_rects++;
2479               /* every 256 new rects increase the rect array */
2480               if ((num_rects % 256) == 0)
2481                 rects = g_realloc (rects, sizeof (XRectangle) * (num_rects + 256));
2482               ptr1 = ptr1->next;
2483             }
2484         }
2485       /* set the rects as the shape mask */
2486       if (rects)
2487         {
2488           XShapeCombineRectangles (disp, win, ShapeBounding, 0, 0, rects, num_rects,
2489                                    ShapeSet, YXSorted);
2490           g_free (rects);
2491         }
2492       XFree (list);
2493     }
2494   /* free up all the spans we made */
2495   for (i = 0; i < baseh; i++)
2496     {
2497       ptr1 = spans[i];
2498       while (ptr1)
2499         {
2500           ptr2 = ptr1;
2501           ptr1 = ptr1->next;
2502           g_free (ptr2);
2503         }
2504     }
2505   g_free (spans);
2506 }
2507
2508 void
2509 gdk_window_set_child_shapes (GdkWindow *window)
2510 {
2511   GdkWindowPrivate *private;
2512   
2513   g_return_if_fail (window != NULL);
2514   
2515 #ifdef HAVE_SHAPE_EXT
2516   private = (GdkWindowPrivate*) window;
2517   if (private->destroyed)
2518     return;
2519   
2520   if (gdk_window_have_shape_ext ())
2521     gdk_propagate_shapes (private->xdisplay, private->xwindow, FALSE);
2522 #endif   
2523 }
2524
2525 void
2526 gdk_window_merge_child_shapes (GdkWindow *window)
2527 {
2528   GdkWindowPrivate *private;
2529   
2530   g_return_if_fail (window != NULL);
2531   
2532 #ifdef HAVE_SHAPE_EXT
2533   private = (GdkWindowPrivate*) window;
2534   if (private->destroyed)
2535     return;
2536   
2537   if (gdk_window_have_shape_ext ())
2538     gdk_propagate_shapes (private->xdisplay, private->xwindow, TRUE);
2539 #endif   
2540 }
2541
2542 /*************************************************************
2543  * gdk_window_is_visible:
2544  *     Check if the given window is mapped.
2545  *   arguments:
2546  *     window: 
2547  *   results:
2548  *     is the window mapped
2549  *************************************************************/
2550
2551 gboolean 
2552 gdk_window_is_visible (GdkWindow *window)
2553 {
2554   GdkWindowPrivate *private = (GdkWindowPrivate *)window;
2555   
2556   g_return_val_if_fail (window != NULL, FALSE);
2557   
2558   return private->mapped;
2559 }
2560
2561 /*************************************************************
2562  * gdk_window_is_viewable:
2563  *     Check if the window and all ancestors of the window
2564  *     are mapped. (This is not necessarily "viewable" in
2565  *     the X sense, since we only check as far as we have
2566  *     GDK window parents, not to the root window)
2567  *   arguments:
2568  *     window:
2569  *   results:
2570  *     is the window viewable
2571  *************************************************************/
2572
2573 gboolean 
2574 gdk_window_is_viewable (GdkWindow *window)
2575 {
2576   GdkWindowPrivate *private = (GdkWindowPrivate *)window;
2577   
2578   g_return_val_if_fail (window != NULL, FALSE);
2579   
2580   while (private && 
2581          (private != &gdk_root_parent) &&
2582          (private->window_type != GDK_WINDOW_FOREIGN))
2583     {
2584       if (!private->mapped)
2585         return FALSE;
2586       
2587       private = (GdkWindowPrivate *)private->parent;
2588     }
2589   
2590   return TRUE;
2591 }
2592
2593 void          
2594 gdk_drawable_set_data (GdkDrawable   *drawable,
2595                        const gchar   *key,
2596                        gpointer       data,
2597                        GDestroyNotify destroy_func)
2598 {
2599   g_dataset_set_data_full (drawable, key, data, destroy_func);
2600 }
2601
2602
2603 /* Support for windows that can be guffaw-scrolled
2604  * (See http://www.gtk.org/~otaylor/whitepapers/guffaw-scrolling.txt)
2605  */
2606
2607 static gboolean
2608 gdk_window_gravity_works (void)
2609 {
2610   enum { UNKNOWN, NO, YES };
2611   static gint gravity_works = UNKNOWN;
2612   
2613   if (gravity_works == UNKNOWN)
2614     {
2615       GdkWindowAttr attr;
2616       GdkWindow *parent;
2617       GdkWindow *child;
2618       gint y;
2619       
2620       /* This particular server apparently has a bug so that the test
2621        * works but the actual code crashes it
2622        */
2623       if ((!strcmp (XServerVendor (gdk_display), "Sun Microsystems, Inc.")) &&
2624           (VendorRelease (gdk_display) == 3400))
2625         {
2626           gravity_works = NO;
2627           return FALSE;
2628         }
2629       
2630       attr.window_type = GDK_WINDOW_TEMP;
2631       attr.wclass = GDK_INPUT_OUTPUT;
2632       attr.x = 0;
2633       attr.y = 0;
2634       attr.width = 100;
2635       attr.height = 100;
2636       attr.event_mask = 0;
2637       
2638       parent = gdk_window_new (NULL, &attr, GDK_WA_X | GDK_WA_Y);
2639       
2640       attr.window_type = GDK_WINDOW_CHILD;
2641       child = gdk_window_new (parent, &attr, GDK_WA_X | GDK_WA_Y);
2642       
2643       gdk_window_set_static_win_gravity (child, TRUE);
2644       
2645       gdk_window_resize (parent, 100, 110);
2646       gdk_window_move (parent, 0, -10);
2647       gdk_window_move_resize (parent, 0, 0, 100, 100);
2648       
2649       gdk_window_resize (parent, 100, 110);
2650       gdk_window_move (parent, 0, -10);
2651       gdk_window_move_resize (parent, 0, 0, 100, 100);
2652       
2653       gdk_window_get_geometry (child, NULL, &y, NULL, NULL, NULL);
2654       
2655       gdk_window_destroy (parent);
2656       gdk_window_destroy (child);
2657       
2658       gravity_works = ((y == -20) ? YES : NO);
2659     }
2660   
2661   return (gravity_works == YES);
2662 }
2663
2664 static void
2665 gdk_window_set_static_bit_gravity (GdkWindow *window, gboolean on)
2666 {
2667   GdkWindowPrivate *private = (GdkWindowPrivate *)window;
2668   XSetWindowAttributes xattributes;
2669   
2670   g_return_if_fail (window != NULL);
2671   
2672   xattributes.bit_gravity = on ? StaticGravity : ForgetGravity;
2673   XChangeWindowAttributes (private->xdisplay,
2674                            private->xwindow,
2675                            CWBitGravity,  &xattributes);
2676 }
2677
2678 static void
2679 gdk_window_set_static_win_gravity (GdkWindow *window, gboolean on)
2680 {
2681   GdkWindowPrivate *private = (GdkWindowPrivate *)window;
2682   XSetWindowAttributes xattributes;
2683   
2684   g_return_if_fail (window != NULL);
2685   
2686   xattributes.win_gravity = on ? StaticGravity : NorthWestGravity;
2687   
2688   XChangeWindowAttributes (private->xdisplay,
2689                            private->xwindow,
2690                            CWWinGravity,  &xattributes);
2691 }
2692
2693 /*************************************************************
2694  * gdk_window_set_static_gravities:
2695  *     Set the bit gravity of the given window to static,
2696  *     and flag it so all children get static subwindow
2697  *     gravity.
2698  *   arguments:
2699  *     window: window for which to set static gravity
2700  *     use_static: Whether to turn static gravity on or off.
2701  *   results:
2702  *     Does the XServer support static gravity?
2703  *************************************************************/
2704
2705 gboolean 
2706 gdk_window_set_static_gravities (GdkWindow *window,
2707                                  gboolean   use_static)
2708 {
2709   GdkWindowPrivate *private = (GdkWindowPrivate *)window;
2710   GList *tmp_list;
2711   
2712   g_return_val_if_fail (window != NULL, FALSE);
2713   
2714   if (!use_static == !private->guffaw_gravity)
2715     return TRUE;
2716   
2717   if (use_static && !gdk_window_gravity_works ())
2718     return FALSE;
2719   
2720   private->guffaw_gravity = use_static;
2721   
2722   gdk_window_set_static_bit_gravity (window, use_static);
2723   
2724   tmp_list = private->children;
2725   while (tmp_list)
2726     {
2727       gdk_window_set_static_win_gravity (window, use_static);
2728       
2729       tmp_list = tmp_list->next;
2730     }
2731   
2732   return TRUE;
2733 }