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