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