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