gdkwindow.c 45 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19

Elliot Lee's avatar
Elliot Lee committed
20 21 22 23 24 25
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/extensions/shape.h>
#include <netinet/in.h>
#include "gdk.h"
26
#include "../config.h"
Elliot Lee's avatar
Elliot Lee committed
27 28
#include "gdkinput.h"
#include "gdkprivate.h"
29
#include "MwmUtil.h"
Elliot Lee's avatar
Elliot Lee committed
30
#include <stdlib.h>
31
#include <stdio.h>
Elliot Lee's avatar
Elliot Lee committed
32

33 34
int nevent_masks = 17;
int event_mask_table[19] =
Elliot Lee's avatar
Elliot Lee committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
{
  ExposureMask,
  PointerMotionMask,
  PointerMotionHintMask,
  ButtonMotionMask,
  Button1MotionMask,
  Button2MotionMask,
  Button3MotionMask,
  ButtonPressMask | OwnerGrabButtonMask,
  ButtonReleaseMask | OwnerGrabButtonMask,
  KeyPressMask,
  KeyReleaseMask,
  EnterWindowMask,
  LeaveWindowMask,
  FocusChangeMask,
  StructureNotifyMask,
  PropertyChangeMask,
52
  VisibilityChangeMask,
Elliot Lee's avatar
Elliot Lee committed
53
  0,				/* PROXIMITY_IN */
54
  0				/* PROXIMTY_OUT */
Elliot Lee's avatar
Elliot Lee committed
55 56 57
};


rhlabs's avatar
rhlabs committed
58 59
/* internal function created for and used by gdk_window_xid_at_coords */
Window
rhlabs's avatar
rhlabs committed
60 61
gdk_window_xid_at(Window base, gint bx, gint by, gint x, gint y, 
		  GList *excludes, gboolean excl_child)
rhlabs's avatar
rhlabs committed
62 63 64 65 66 67
{
   GdkWindow *window;
   GdkWindowPrivate *private;
   Display *disp;
   Window *list=NULL;
   Window child=0,parent_win=0,root_win=0;
Owen Taylor's avatar
Owen Taylor committed
68 69
   int i;
   guint num;
rhlabs's avatar
rhlabs committed
70
   int wx,wy;
Owen Taylor's avatar
Owen Taylor committed
71 72
   guint ww,wh,wb,wd;

rhlabs's avatar
rhlabs committed
73 74 75 76 77 78 79 80 81 82 83 84
   window=(GdkWindow*)&gdk_root_parent;
   private=(GdkWindowPrivate*)window;
   disp=private->xdisplay;
   if (!XGetGeometry(disp,base,&root_win,&wx,&wy,&ww,&wh,&wb,&wd))
     return 0;
   wx+=bx;wy+=by;
   if (!((x>=wx)&&(y>=wy)&&(x<(wx+ww))&&(y<(wy+wh))))
     return 0;
   if (!XQueryTree(disp,base,&root_win,&parent_win,&list,&num))
     return base;
   if (list)
     {
rhlabs's avatar
rhlabs committed
85
	for (i=num-1;;i--)
rhlabs's avatar
rhlabs committed
86
	  {
rhlabs's avatar
rhlabs committed
87
	     if ((!excl_child)||(!g_list_find(excludes,(gpointer *)list[i])))
rhlabs's avatar
rhlabs committed
88
	       {
89 90 91 92 93
		 if ((child=gdk_window_xid_at(list[i],wx,wy,x,y,excludes,excl_child))!=0)
		   {
		     XFree(list);
		     return child;
		   }
rhlabs's avatar
rhlabs committed
94
	       }
rhlabs's avatar
rhlabs committed
95
	     if (!i) break;
rhlabs's avatar
rhlabs committed
96
	  }
rhlabs's avatar
rhlabs committed
97
	XFree(list);
rhlabs's avatar
rhlabs committed
98
     }
rhlabs's avatar
rhlabs committed
99
   return base;
rhlabs's avatar
rhlabs committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
}

/* 
 * The following fucntion by The Rasterman <raster@redhat.com>
 * This function returns the X Window ID in which the x y location is in 
 * (x and y being relative to the root window), excluding any windows listed
 * in the GList excludes (this is a list of X Window ID's - gpointer being
 * the Window ID).
 * 
 * This is primarily designed for internal gdk use - for DND for example
 * when using a shaped icon window as the drag object - you exclude the
 * X Window ID of the "icon" (perhaps more if excludes may be needed) and
 * You can get back an X Window ID as to what X Window ID is infact under
 * those X,Y co-ordinates.
 */
Window
rhlabs's avatar
rhlabs committed
116
gdk_window_xid_at_coords(gint x, gint y, GList *excludes, gboolean excl_child)
rhlabs's avatar
rhlabs committed
117 118 119 120 121 122
{
   GdkWindow *window;
   GdkWindowPrivate *private;
   Display *disp;
   Window *list=NULL;
   Window root,child=0,parent_win=0,root_win=0;
123 124
   unsigned int num;
   int i;
rhlabs's avatar
rhlabs committed
125 126 127 128 129
   
   window=(GdkWindow*)&gdk_root_parent;
   private=(GdkWindowPrivate*)window;
   disp=private->xdisplay;
   root=private->xwindow;
rhlabs's avatar
rhlabs committed
130
   XGrabServer(disp);
rhlabs's avatar
rhlabs committed
131
   num=g_list_length(excludes);
rhlabs's avatar
rhlabs committed
132
   if (!XQueryTree(disp,root,&root_win,&parent_win,&list,&num))
rhlabs's avatar
rhlabs committed
133
	return root;
rhlabs's avatar
rhlabs committed
134 135
   if (list)
     {
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
       i = num - 1;
       do
	 {
	   XWindowAttributes xwa;

	   XGetWindowAttributes (disp, list [i], &xwa);

	   if (xwa.map_state != IsViewable)
	     continue;

	   if (excl_child && g_list_find(excludes,(gpointer *)list[i]))
	     continue;
	   
	   if ((child = gdk_window_xid_at (list[i], 0, 0, x, y, excludes, excl_child)) == 0)
	     continue;

	   if (excludes)
	     {
	       if (!g_list_find(excludes,(gpointer *)child))
		 {
		   XFree(list);
		   XUngrabServer(disp);
		   return child;
		 }
	     }
	   else
	     {
	       XFree(list);
	       XUngrabServer(disp);
	       return child;
	     }
	 } while (--i > 0);
rhlabs's avatar
rhlabs committed
168
	XFree(list);
rhlabs's avatar
rhlabs committed
169
     }
rhlabs's avatar
rhlabs committed
170
   XUngrabServer(disp);
rhlabs's avatar
rhlabs committed
171 172 173
   return root;
}

Elliot Lee's avatar
Elliot Lee committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
void
gdk_window_init ()
{
  XWindowAttributes xattributes;
  unsigned int width;
  unsigned int height;
  unsigned int border_width;
  unsigned int depth;
  int x, y;

  XGetGeometry (gdk_display, gdk_root_window, &gdk_root_window,
		&x, &y, &width, &height, &border_width, &depth);
  XGetWindowAttributes (gdk_display, gdk_root_window, &xattributes);

  gdk_root_parent.xdisplay = gdk_display;
  gdk_root_parent.xwindow = gdk_root_window;
  gdk_root_parent.window_type = GDK_WINDOW_ROOT;
  gdk_root_parent.window.user_data = NULL;
192 193
  gdk_root_parent.width = width;
  gdk_root_parent.height = height;
Elliot Lee's avatar
Elliot Lee committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
}

GdkWindow*
gdk_window_new (GdkWindow     *parent,
		GdkWindowAttr *attributes,
		gint           attributes_mask)
{
  GdkWindow *window;
  GdkWindowPrivate *private;
  GdkWindowPrivate *parent_private;
  GdkVisual *visual;
  GdkColormap *colormap;
  Display *parent_display;
  Window xparent;
  Visual *xvisual;
  XSetWindowAttributes xattributes;
  long xattributes_mask;
  XSizeHints size_hints;
  XWMHints wm_hints;
  XClassHint *class_hint;
  int x, y, depth;
  unsigned int class;
  char *title;
  int i;

  g_return_val_if_fail (attributes != NULL, NULL);

  if (!parent)
    parent = (GdkWindow*) &gdk_root_parent;

  parent_private = (GdkWindowPrivate*) parent;
225 226 227
  if (parent_private->destroyed)
    return NULL;

Elliot Lee's avatar
Elliot Lee committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
  xparent = parent_private->xwindow;
  parent_display = parent_private->xdisplay;

  private = g_new (GdkWindowPrivate, 1);
  window = (GdkWindow*) private;

  private->parent = parent;
  private->xdisplay = parent_display;
  private->destroyed = FALSE;
  private->resize_count = 0;
  private->ref_count = 1;
  xattributes_mask = 0;

  if (attributes_mask & GDK_WA_X)
    x = attributes->x;
  else
    x = 0;

  if (attributes_mask & GDK_WA_Y)
    y = attributes->y;
  else
    y = 0;

  private->x = x;
  private->y = y;
  private->width = (attributes->width > 1) ? (attributes->width) : (1);
  private->height = (attributes->height > 1) ? (attributes->height) : (1);
  private->window_type = attributes->window_type;
  private->extension_events = FALSE;
  private->dnd_drag_data_type = None;
  private->dnd_drag_data_typesavail =
    private->dnd_drop_data_typesavail = NULL;
  private->dnd_drop_enabled = private->dnd_drag_enabled =
    private->dnd_drag_accepted = private->dnd_drag_datashow =
    private->dnd_drop_data_numtypesavail =
    private->dnd_drag_data_numtypesavail = 0;
  private->dnd_drag_eventmask = private->dnd_drag_savedeventmask = 0;

266 267
  private->filters = NULL;

Elliot Lee's avatar
Elliot Lee committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  window->user_data = NULL;

  if (attributes_mask & GDK_WA_VISUAL)
    visual = attributes->visual;
  else
    visual = gdk_visual_get_system ();
  xvisual = ((GdkVisualPrivate*) visual)->xvisual;

  xattributes.event_mask = StructureNotifyMask;
  for (i = 0; i < nevent_masks; i++)
    {
      if (attributes->event_mask & (1 << (i + 1)))
	xattributes.event_mask |= event_mask_table[i];
    }

  if (xattributes.event_mask)
    xattributes_mask |= CWEventMask;

286 287 288 289 290 291 292
  if(attributes_mask & GDK_WA_NOREDIR) {
	xattributes.override_redirect =
		(attributes->override_redirect == FALSE)?False:True;
	xattributes_mask |= CWOverrideRedirect;
  } else
    xattributes.override_redirect = False;

Elliot Lee's avatar
Elliot Lee committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
  if (attributes->wclass == GDK_INPUT_OUTPUT)
    {
      class = InputOutput;
      depth = visual->depth;

      if (attributes_mask & GDK_WA_COLORMAP)
	colormap = attributes->colormap;
      else
	colormap = gdk_colormap_get_system ();

      xattributes.background_pixel = BlackPixel (gdk_display, gdk_screen);
      xattributes.border_pixel = BlackPixel (gdk_display, gdk_screen);
      xattributes_mask |= CWBorderPixel | CWBackPixel;

      switch (private->window_type)
	{
	case GDK_WINDOW_TOPLEVEL:
	  xattributes.colormap = ((GdkColormapPrivate*) colormap)->xcolormap;
	  xattributes_mask |= CWColormap;

	  xparent = gdk_root_window;
	  break;

	case GDK_WINDOW_CHILD:
	  xattributes.colormap = ((GdkColormapPrivate*) colormap)->xcolormap;
	  xattributes_mask |= CWColormap;
	  break;

	case GDK_WINDOW_DIALOG:
	  xattributes.colormap = ((GdkColormapPrivate*) colormap)->xcolormap;
	  xattributes_mask |= CWColormap;

	  xparent = gdk_root_window;
	  break;

	case GDK_WINDOW_TEMP:
	  xattributes.colormap = ((GdkColormapPrivate*) colormap)->xcolormap;
	  xattributes_mask |= CWColormap;

	  xparent = gdk_root_window;

	  xattributes.save_under = True;
	  xattributes.override_redirect = True;
	  xattributes.cursor = None;
	  xattributes_mask |= CWSaveUnder | CWOverrideRedirect;
	  break;
	case GDK_WINDOW_ROOT:
	  g_error ("cannot make windows of type GDK_WINDOW_ROOT");
	  break;
	case GDK_WINDOW_PIXMAP:
	  g_error ("cannot make windows of type GDK_WINDOW_PIXMAP (use gdk_pixmap_new)");
	  break;
	}
    }
  else
    {
349
      depth = 0;
Elliot Lee's avatar
Elliot Lee committed
350 351 352 353 354 355 356 357
      class = InputOnly;
      colormap = NULL;
    }

  private->xwindow = XCreateWindow (private->xdisplay, xparent,
				    x, y, private->width, private->height,
				    0, depth, class, xvisual,
				    xattributes_mask, &xattributes);
358
  gdk_window_ref (window);
Elliot Lee's avatar
Elliot Lee committed
359 360
  gdk_xid_table_insert (&private->xwindow, window);

361 362 363 364
  gdk_window_set_cursor (window, ((attributes_mask & GDK_WA_CURSOR) ?
				  (attributes->cursor) :
				  NULL));

Elliot Lee's avatar
Elliot Lee committed
365 366 367 368 369 370 371 372 373 374 375 376 377
  switch (private->window_type)
    {
    case GDK_WINDOW_DIALOG:
      XSetTransientForHint (private->xdisplay, private->xwindow, xparent);
    case GDK_WINDOW_TOPLEVEL:
    case GDK_WINDOW_TEMP:
      XSetWMProtocols (private->xdisplay, private->xwindow, gdk_wm_window_protocols, 2);
      break;
    case GDK_WINDOW_CHILD:
      if ((attributes->wclass == GDK_INPUT_OUTPUT) &&
	  (colormap != gdk_colormap_get_system ()) &&
	  (colormap != gdk_window_get_colormap (gdk_window_get_toplevel (window))))
	{
378
	  GDK_NOTE (MISC, g_print ("adding colormap window\n"));
Elliot Lee's avatar
Elliot Lee committed
379 380
	  gdk_window_add_colormap_windows (window);
	}
381 382

      return window;
Elliot Lee's avatar
Elliot Lee committed
383
    default:
384 385

      return window;
Elliot Lee's avatar
Elliot Lee committed
386 387
    }

388
  size_hints.flags = PSize;
Elliot Lee's avatar
Elliot Lee committed
389 390 391 392 393 394 395 396
  size_hints.width = private->width;
  size_hints.height = private->height;

  wm_hints.flags = InputHint | StateHint | WindowGroupHint;
  wm_hints.window_group = gdk_leader_window;
  wm_hints.input = True;
  wm_hints.initial_state = NormalState;

397 398 399 400
  /* FIXME: Is there any point in doing this? Do any WM's pay
   * attention to PSize, and even if they do, is this the
   * correct value???
   */
Elliot Lee's avatar
Elliot Lee committed
401
  XSetWMNormalHints (private->xdisplay, private->xwindow, &size_hints);
402

Elliot Lee's avatar
Elliot Lee committed
403 404 405 406 407 408 409
  XSetWMHints (private->xdisplay, private->xwindow, &wm_hints);

  if (attributes_mask & GDK_WA_TITLE)
    title = attributes->title;
  else
    title = gdk_progname;

410 411 412 413
  XmbSetWMProperties (private->xdisplay, private->xwindow,
                      title, title,
                      NULL, 0,
                      NULL, NULL, NULL);
Elliot Lee's avatar
Elliot Lee committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433

  if (attributes_mask & GDK_WA_WMCLASS)
    {
      class_hint = XAllocClassHint ();
      class_hint->res_name = attributes->wmclass_name;
      class_hint->res_class = attributes->wmclass_class;
      XSetClassHint (private->xdisplay, private->xwindow, class_hint);
      XFree (class_hint);
    }


  return window;
}

GdkWindow *
gdk_window_foreign_new (guint32 anid)
{
  GdkWindow *window;
  GdkWindowPrivate *private;
  XWindowAttributes attrs;
434 435 436
  Window root, parent;
  Window *children;
  guint nchildren;
Elliot Lee's avatar
Elliot Lee committed
437 438 439 440 441 442

  private = g_new (GdkWindowPrivate, 1);
  window = (GdkWindow*) private;

  XGetWindowAttributes (gdk_display, anid, &attrs);

443 444 445 446 447 448
  /* FIXME: This is pretty expensive. Maybe the caller should supply
   *        the parent */
  XQueryTree (gdk_display, anid, &root, &parent, &children, &nchildren);
  XFree (children);
  private->parent = gdk_xid_table_lookup (parent);

Elliot Lee's avatar
Elliot Lee committed
449 450 451 452 453 454 455 456
  private->xwindow = anid;
  private->xdisplay = gdk_display;
  private->x = attrs.x;
  private->y = attrs.y;
  private->width = attrs.width;
  private->height = attrs.height;
  private->resize_count = 0;
  private->ref_count = 1;
457
  private->window_type = GDK_WINDOW_FOREIGN;
Elliot Lee's avatar
Elliot Lee committed
458 459
  private->destroyed = FALSE;
  private->extension_events = 0;
460 461 462 463 464 465 466 467 468 469 470


  private->dnd_drag_data_type = None;
  private->dnd_drag_data_typesavail =
    private->dnd_drop_data_typesavail = NULL;
  private->dnd_drop_enabled = private->dnd_drag_enabled =
    private->dnd_drag_accepted = private->dnd_drag_datashow =
    private->dnd_drop_data_numtypesavail =
    private->dnd_drag_data_numtypesavail = 0;
  private->dnd_drag_eventmask = private->dnd_drag_savedeventmask = 0;

471
  private->filters = NULL;
Elliot Lee's avatar
Elliot Lee committed
472 473 474

  window->user_data = NULL;

475
  gdk_window_ref (window);
Elliot Lee's avatar
Elliot Lee committed
476 477 478 479 480
  gdk_xid_table_insert (&private->xwindow, window);

  return window;
}

481 482 483 484 485 486 487
/* Call this function when you want a window and all its children to
   disappear.  When xdestroy is true, a request to destroy the XWindow
   is sent out.  When it is false, it is assumed that the XWindow has
   been or will be destroyed by destroying some ancestor of this
   window.  */

static void
488 489
gdk_window_internal_destroy (GdkWindow *window, gboolean xdestroy,
			     gboolean our_destroy)
Elliot Lee's avatar
Elliot Lee committed
490 491 492 493 494 495 496 497 498 499
{
  GdkWindowPrivate *private;
  GdkWindowPrivate *temp_private;
  GdkWindow *temp_window;
  GList *children;
  GList *tmp;

  g_return_if_fail (window != NULL);

  private = (GdkWindowPrivate*) window;
500

Elliot Lee's avatar
Elliot Lee committed
501 502 503 504 505 506
  switch (private->window_type)
    {
    case GDK_WINDOW_TOPLEVEL:
    case GDK_WINDOW_CHILD:
    case GDK_WINDOW_DIALOG:
    case GDK_WINDOW_TEMP:
507
    case GDK_WINDOW_FOREIGN:
508
      if (!private->destroyed)
Elliot Lee's avatar
Elliot Lee committed
509
	{
510
	  if (private->window_type != GDK_WINDOW_FOREIGN)
Elliot Lee's avatar
Elliot Lee committed
511
	    {
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
	      children = gdk_window_get_children (window);
	      tmp = children;

	      while (tmp)
		{
		  temp_window = tmp->data;
		  tmp = tmp->next;
		  
		  temp_private = (GdkWindowPrivate*) temp_window;
		  if (temp_private)
		    gdk_window_internal_destroy (temp_window, FALSE,
						 our_destroy);
		}
	      
	      g_list_free (children);
Elliot Lee's avatar
Elliot Lee committed
527 528
	    }

529 530 531
	  if (private->extension_events != 0)
	    gdk_input_window_destroy (window);

532 533 534 535 536 537 538 539 540 541 542
	  if(private->dnd_drag_data_numtypesavail > 0) 
	    {
	      g_free (private->dnd_drag_data_typesavail);
	      private->dnd_drag_data_typesavail = NULL;
	    }
	  if(private->dnd_drop_data_numtypesavail > 0) 
	    {
	      g_free (private->dnd_drop_data_typesavail);
	      private->dnd_drop_data_typesavail = NULL;
	    }

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
	  if (private->filters)
	    {
	      tmp = private->filters;

	      while (tmp)
		{
		  g_free (tmp->data);
		  tmp = tmp->next;
		}

	      g_list_free (private->filters);
	      private->filters = NULL;
	    }
	  
	  if (private->window_type == GDK_WINDOW_FOREIGN)
	    {
	      if (our_destroy && (private->parent != NULL))
		{
		  /* It's somebody elses window, but in our heirarchy,
		   * so reparent it to the root window, and then send
		   * it a delete event, as if we were a WM
		   */
		  XClientMessageEvent xevent;
		  
		  gdk_window_hide (window);
		  gdk_window_reparent (window, NULL, 0, 0);
		  
		  xevent.type = ClientMessage;
		  xevent.window = private->xwindow;
		  xevent.message_type = gdk_wm_protocols;
		  xevent.format = 32;
		  xevent.data.l[0] = gdk_wm_delete_window;
		  xevent.data.l[1] = CurrentTime;
		  
		  XSendEvent (private->xdisplay, private->xwindow,
			      False, 0, (XEvent *)&xevent);
		}
	    }
	  else if (xdestroy)
Elliot Lee's avatar
Elliot Lee committed
582
	    XDestroyWindow (private->xdisplay, private->xwindow);
583

Elliot Lee's avatar
Elliot Lee committed
584 585 586 587 588 589 590 591 592
	  private->destroyed = TRUE;
	}
      break;

    case GDK_WINDOW_ROOT:
      g_error ("attempted to destroy root window");
      break;

    case GDK_WINDOW_PIXMAP:
593
      g_error ("called gdk_window_destroy on a pixmap (use gdk_pixmap_unref)");
Elliot Lee's avatar
Elliot Lee committed
594 595 596 597
      break;
    }
}

598 599 600
/* Like internal_destroy, but also destroys the reference created by
   gdk_window_new. */

Elliot Lee's avatar
Elliot Lee committed
601
void
602 603
gdk_window_destroy (GdkWindow *window)
{
604
  gdk_window_internal_destroy (window, TRUE, TRUE);
605 606 607 608 609 610 611
  gdk_window_unref (window);
}

/* This function is called when the XWindow is really gone.  */

void
gdk_window_destroy_notify (GdkWindow *window)
Elliot Lee's avatar
Elliot Lee committed
612 613 614 615 616 617 618
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  private = (GdkWindowPrivate*) window;

619 620 621 622 623
  if (!private->destroyed)
    {
      if (private->window_type == GDK_WINDOW_FOREIGN)
	gdk_window_internal_destroy (window, FALSE, FALSE);
      else
624
	g_warning ("GdkWindow %#lx unexpectedly destroyed", private->xwindow);
625 626
    }
  
627 628
  gdk_xid_table_remove (private->xwindow);
  gdk_window_unref (window);
Elliot Lee's avatar
Elliot Lee committed
629 630 631 632 633 634
}

GdkWindow*
gdk_window_ref (GdkWindow *window)
{
  GdkWindowPrivate *private = (GdkWindowPrivate *)window;
Tim Janik's avatar
GLib:  
Tim Janik committed
635
  g_return_val_if_fail (window != NULL, NULL);
Elliot Lee's avatar
Elliot Lee committed
636 637 638 639 640 641 642 643 644 645 646 647 648

  private->ref_count += 1;
  return window;
}

void
gdk_window_unref (GdkWindow *window)
{
  GdkWindowPrivate *private = (GdkWindowPrivate *)window;
  g_return_if_fail (window != NULL);

  private->ref_count -= 1;
  if (private->ref_count == 0)
649 650 651 652 653
    {
      if (!private->destroyed)
	g_warning ("losing last reference to undestroyed window\n");
      g_free (window);
    }
Elliot Lee's avatar
Elliot Lee committed
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
}

void
gdk_window_show (GdkWindow *window)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  private = (GdkWindowPrivate*) window;
  if (!private->destroyed)
    {
      XRaiseWindow (private->xdisplay, private->xwindow);
      XMapWindow (private->xdisplay, private->xwindow);
    }
}

void
gdk_window_hide (GdkWindow *window)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  private = (GdkWindowPrivate*) window;
  if (!private->destroyed)
    XUnmapWindow (private->xdisplay, private->xwindow);
}

683 684 685 686 687 688 689 690 691 692 693 694
void
gdk_window_withdraw (GdkWindow *window)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  private = (GdkWindowPrivate*) window;
  if (!private->destroyed)
    XWithdrawWindow (private->xdisplay, private->xwindow, 0);
}

Elliot Lee's avatar
Elliot Lee committed
695 696 697 698 699 700 701 702 703 704
void
gdk_window_move (GdkWindow *window,
		 gint       x,
		 gint       y)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  private = (GdkWindowPrivate*) window;
705
  if (!private->destroyed)
Elliot Lee's avatar
Elliot Lee committed
706
    {
707 708 709 710 711 712 713
      XMoveWindow (private->xdisplay, private->xwindow, x, y);
      
      if (private->window_type == GDK_WINDOW_CHILD)
	{
	  private->x = x;
	  private->y = y;
	}
Elliot Lee's avatar
Elliot Lee committed
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
    }
}

void
gdk_window_resize (GdkWindow *window,
		   gint       width,
		   gint       height)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  if (width < 1)
    width = 1;
  if (height < 1)
    height = 1;

  private = (GdkWindowPrivate*) window;

  if (!private->destroyed &&
      ((private->resize_count > 0) ||
       (private->width != (guint16) width) ||
       (private->height != (guint16) height)))
    {
      XResizeWindow (private->xdisplay, private->xwindow, width, height);
      private->resize_count += 1;

      if (private->window_type == GDK_WINDOW_CHILD)
	{
	  private->width = width;
	  private->height = height;
	}
    }
}

void
gdk_window_move_resize (GdkWindow *window,
			gint       x,
			gint       y,
			gint       width,
			gint       height)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  if (width < 1)
    width = 1;
  if (height < 1)
    height = 1;

  private = (GdkWindowPrivate*) window;
766
  if (!private->destroyed)
Elliot Lee's avatar
Elliot Lee committed
767
    {
768 769 770 771 772 773 774 775 776
      XMoveResizeWindow (private->xdisplay, private->xwindow, x, y, width, height);
      
      if (private->window_type == GDK_WINDOW_CHILD)
	{
	  private->x = x;
	  private->y = y;
	  private->width = width;
	  private->height = height;
	}
Elliot Lee's avatar
Elliot Lee committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
    }
}

void
gdk_window_reparent (GdkWindow *window,
		     GdkWindow *new_parent,
		     gint       x,
		     gint       y)
{
  GdkWindowPrivate *window_private;
  GdkWindowPrivate *parent_private;

  g_return_if_fail (window != NULL);

  if (!new_parent)
    new_parent = (GdkWindow*) &gdk_root_parent;

  window_private = (GdkWindowPrivate*) window;
  parent_private = (GdkWindowPrivate*) new_parent;

797 798 799 800 801
  if (!window_private->destroyed && !parent_private->destroyed)
    XReparentWindow (window_private->xdisplay,
		     window_private->xwindow,
		     parent_private->xwindow,
		     x, y);
Elliot Lee's avatar
Elliot Lee committed
802 803 804 805 806 807 808 809 810 811 812
}

void
gdk_window_clear (GdkWindow *window)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  private = (GdkWindowPrivate*) window;

813 814
  if (!private->destroyed)
    XClearWindow (private->xdisplay, private->xwindow);
Elliot Lee's avatar
Elliot Lee committed
815 816 817 818 819 820 821 822 823 824
}

void
gdk_window_clear_area (GdkWindow *window,
		       gint       x,
		       gint       y,
		       gint       width,
		       gint       height)
{
  GdkWindowPrivate *private;
825
  
Elliot Lee's avatar
Elliot Lee committed
826
  g_return_if_fail (window != NULL);
827
  
Elliot Lee's avatar
Elliot Lee committed
828
  private = (GdkWindowPrivate*) window;
829
  
Elliot Lee's avatar
Elliot Lee committed
830 831 832 833 834 835 836 837 838 839 840 841 842
  if (!private->destroyed)
    XClearArea (private->xdisplay, private->xwindow,
		x, y, width, height, False);
}

void
gdk_window_clear_area_e (GdkWindow *window,
		         gint       x,
		         gint       y,
		         gint       width,
		         gint       height)
{
  GdkWindowPrivate *private;
843
  
Elliot Lee's avatar
Elliot Lee committed
844
  g_return_if_fail (window != NULL);
845
  
Elliot Lee's avatar
Elliot Lee committed
846
  private = (GdkWindowPrivate*) window;
847
  
Elliot Lee's avatar
Elliot Lee committed
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
  if (!private->destroyed)
    XClearArea (private->xdisplay, private->xwindow,
		x, y, width, height, True);
}

void
gdk_window_copy_area (GdkWindow    *window,
		      GdkGC        *gc,
		      gint          x,
		      gint          y,
		      GdkWindow    *source_window,
		      gint          source_x,
		      gint          source_y,
		      gint          width,
		      gint          height)
{
  GdkWindowPrivate *src_private;
  GdkWindowPrivate *dest_private;
  GdkGCPrivate *gc_private;
867 868
  
  g_return_if_fail (window != NULL);
Elliot Lee's avatar
Elliot Lee committed
869 870 871 872
  g_return_if_fail (gc != NULL);
  
  if (source_window == NULL)
    source_window = window;
873
  
Elliot Lee's avatar
Elliot Lee committed
874 875 876 877 878
  src_private = (GdkWindowPrivate*) source_window;
  dest_private = (GdkWindowPrivate*) window;
  gc_private = (GdkGCPrivate*) gc;
  
  if (!src_private->destroyed && !dest_private->destroyed)
879 880 881 882 883 884 885
    {
      XCopyArea (dest_private->xdisplay, src_private->xwindow, dest_private->xwindow,
		 gc_private->xgc,
		 source_x, source_y,
		 width, height,
		 x, y);
    }
Elliot Lee's avatar
Elliot Lee committed
886 887 888 889 890 891
}

void
gdk_window_raise (GdkWindow *window)
{
  GdkWindowPrivate *private;
892
  
Elliot Lee's avatar
Elliot Lee committed
893
  g_return_if_fail (window != NULL);
894
  
Elliot Lee's avatar
Elliot Lee committed
895
  private = (GdkWindowPrivate*) window;
896
  
Elliot Lee's avatar
Elliot Lee committed
897 898 899 900 901 902 903 904
  if (!private->destroyed)
    XRaiseWindow (private->xdisplay, private->xwindow);
}

void
gdk_window_lower (GdkWindow *window)
{
  GdkWindowPrivate *private;
905
  
Elliot Lee's avatar
Elliot Lee committed
906
  g_return_if_fail (window != NULL);
907
  
Elliot Lee's avatar
Elliot Lee committed
908
  private = (GdkWindowPrivate*) window;
909
  
Elliot Lee's avatar
Elliot Lee committed
910 911 912 913 914 915 916 917 918
  if (!private->destroyed)
    XLowerWindow (private->xdisplay, private->xwindow);
}

void
gdk_window_set_user_data (GdkWindow *window,
			  gpointer   user_data)
{
  g_return_if_fail (window != NULL);
919
  
Elliot Lee's avatar
Elliot Lee committed
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
  window->user_data = user_data;
}

void
gdk_window_set_hints (GdkWindow *window,
		      gint       x,
		      gint       y,
		      gint       min_width,
		      gint       min_height,
		      gint       max_width,
		      gint       max_height,
		      gint       flags)
{
  GdkWindowPrivate *private;
  XSizeHints size_hints;
935
  
Elliot Lee's avatar
Elliot Lee committed
936
  g_return_if_fail (window != NULL);
937
  
Elliot Lee's avatar
Elliot Lee committed
938
  private = (GdkWindowPrivate*) window;
939 940
  if (private->destroyed)
    return;
941
  
Elliot Lee's avatar
Elliot Lee committed
942
  size_hints.flags = 0;
943
  
Elliot Lee's avatar
Elliot Lee committed
944 945 946 947 948 949
  if (flags & GDK_HINT_POS)
    {
      size_hints.flags |= PPosition;
      size_hints.x = x;
      size_hints.y = y;
    }
950
  
Elliot Lee's avatar
Elliot Lee committed
951 952 953 954 955 956
  if (flags & GDK_HINT_MIN_SIZE)
    {
      size_hints.flags |= PMinSize;
      size_hints.min_width = min_width;
      size_hints.min_height = min_height;
    }
957
  
Elliot Lee's avatar
Elliot Lee committed
958 959 960 961 962 963
  if (flags & GDK_HINT_MAX_SIZE)
    {
      size_hints.flags |= PMaxSize;
      size_hints.max_width = max_width;
      size_hints.max_height = max_height;
    }
964
  
Elliot Lee's avatar
Elliot Lee committed
965 966 967 968 969 970 971 972 973
  if (flags)
    XSetWMNormalHints (private->xdisplay, private->xwindow, &size_hints);
}

void
gdk_window_set_title (GdkWindow   *window,
		      const gchar *title)
{
  GdkWindowPrivate *private;
974
  
Elliot Lee's avatar
Elliot Lee committed
975
  g_return_if_fail (window != NULL);
976
  
Elliot Lee's avatar
Elliot Lee committed
977
  private = (GdkWindowPrivate*) window;
978 979 980
  if (!private->destroyed)
    XmbSetWMProperties (private->xdisplay, private->xwindow,
			title, title, NULL, 0, NULL, NULL, NULL);
Elliot Lee's avatar
Elliot Lee committed
981 982 983 984 985 986 987
}

void
gdk_window_set_background (GdkWindow *window,
			   GdkColor  *color)
{
  GdkWindowPrivate *private;
988
  
Elliot Lee's avatar
Elliot Lee committed
989
  g_return_if_fail (window != NULL);
990
  
Elliot Lee's avatar
Elliot Lee committed
991
  private = (GdkWindowPrivate*) window;
992 993
  if (!private->destroyed)
    XSetWindowBackground (private->xdisplay, private->xwindow, color->pixel);
Elliot Lee's avatar
Elliot Lee committed
994 995 996 997 998 999 1000 1001 1002 1003
}

void
gdk_window_set_back_pixmap (GdkWindow *window,
			    GdkPixmap *pixmap,
			    gint       parent_relative)
{
  GdkWindowPrivate *window_private;
  GdkPixmapPrivate *pixmap_private;
  Pixmap xpixmap;
1004
  
Elliot Lee's avatar
Elliot Lee committed
1005
  g_return_if_fail (window != NULL);
1006
  
Elliot Lee's avatar
Elliot Lee committed
1007 1008
  window_private = (GdkWindowPrivate*) window;
  pixmap_private = (GdkPixmapPrivate*) pixmap;
1009
  
Elliot Lee's avatar
Elliot Lee committed
1010 1011 1012 1013
  if (pixmap)
    xpixmap = pixmap_private->xwindow;
  else
    xpixmap = None;
1014
  
Elliot Lee's avatar
Elliot Lee committed
1015 1016
  if (parent_relative)
    xpixmap = ParentRelative;
1017
  
1018 1019
  if (!window_private->destroyed)
    XSetWindowBackgroundPixmap (window_private->xdisplay, window_private->xwindow, xpixmap);
Elliot Lee's avatar
Elliot Lee committed
1020 1021 1022 1023 1024 1025 1026 1027 1028
}

void
gdk_window_set_cursor (GdkWindow *window,
		       GdkCursor *cursor)
{
  GdkWindowPrivate *window_private;
  GdkCursorPrivate *cursor_private;
  Cursor xcursor;
1029
  
Elliot Lee's avatar
Elliot Lee committed
1030
  g_return_if_fail (window != NULL);
1031
  
Elliot Lee's avatar
Elliot Lee committed
1032 1033
  window_private = (GdkWindowPrivate*) window;
  cursor_private = (GdkCursorPrivate*) cursor;
1034
  
Elliot Lee's avatar
Elliot Lee committed
1035 1036 1037 1038
  if (!cursor)
    xcursor = None;
  else
    xcursor = cursor_private->xcursor;
1039
  
1040 1041
  if (!window_private->destroyed)
    XDefineCursor (window_private->xdisplay, window_private->xwindow, xcursor);
Elliot Lee's avatar
Elliot Lee committed
1042 1043 1044 1045 1046 1047 1048 1049
}

void
gdk_window_set_colormap (GdkWindow   *window,
			 GdkColormap *colormap)
{
  GdkWindowPrivate *window_private;
  GdkColormapPrivate *colormap_private;
1050
  
Elliot Lee's avatar
Elliot Lee committed
1051 1052
  g_return_if_fail (window != NULL);
  g_return_if_fail (colormap != NULL);
1053
  
Elliot Lee's avatar
Elliot Lee committed
1054 1055
  window_private = (GdkWindowPrivate*) window;
  colormap_private = (GdkColormapPrivate*) colormap;
1056
  
1057 1058 1059 1060 1061 1062 1063 1064 1065
  if (!window_private->destroyed)
    {
      XSetWindowColormap (window_private->xdisplay,
			  window_private->xwindow,
			  colormap_private->xcolormap);
      
      if (window_private->window_type != GDK_WINDOW_TOPLEVEL)
	gdk_window_add_colormap_windows (window);
    }
Elliot Lee's avatar
Elliot Lee committed
1066 1067 1068 1069 1070 1071 1072
}

void
gdk_window_get_user_data (GdkWindow *window,
			  gpointer  *data)
{
  g_return_if_fail (window != NULL);
1073
  
Elliot Lee's avatar
Elliot Lee committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
  *data = window->user_data;
}

void
gdk_window_get_geometry (GdkWindow *window,
			 gint      *x,
			 gint      *y,
			 gint      *width,
			 gint      *height,
			 gint      *depth)
{
  GdkWindowPrivate *window_private;
  Window root;
  gint tx;
  gint ty;
  guint twidth;
  guint theight;
  guint tborder_width;
  guint tdepth;
1093
  
Elliot Lee's avatar
Elliot Lee committed
1094 1095
  if (!window)
    window = (GdkWindow*) &gdk_root_parent;
1096
  
Elliot Lee's avatar
Elliot Lee committed
1097
  window_private = (GdkWindowPrivate*) window;
1098
  
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
  if (!window_private->destroyed)
    {
      XGetGeometry (window_private->xdisplay, window_private->xwindow,
		    &root, &tx, &ty, &twidth, &theight, &tborder_width, &tdepth);
      
      if (x)
	*x = tx;
      if (y)
	*y = ty;
      if (width)
	*width = twidth;
      if (height)
	*height = theight;
      if (depth)
	*depth = tdepth;
    }
Elliot Lee's avatar
Elliot Lee committed
1115 1116 1117 1118 1119 1120 1121 1122
}

void
gdk_window_get_position (GdkWindow *window,
			 gint      *x,
			 gint      *y)
{
  GdkWindowPrivate *window_private;
1123
  
Elliot Lee's avatar
Elliot Lee committed
1124
  g_return_if_fail (window != NULL);
1125
  
Elliot Lee's avatar
Elliot Lee committed
1126
  window_private = (GdkWindowPrivate*) window;
1127
  
Elliot Lee's avatar
Elliot Lee committed
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
  if (x)
    *x = window_private->x;
  if (y)
    *y = window_private->y;
}

void
gdk_window_get_size (GdkWindow *window,
		     gint       *width,
		     gint       *height)
{
  GdkWindowPrivate *window_private;
1140
  
Elliot Lee's avatar
Elliot Lee committed
1141
  g_return_if_fail (window != NULL);
1142
  
Elliot Lee's avatar
Elliot Lee committed
1143
  window_private = (GdkWindowPrivate*) window;
1144
  
Elliot Lee's avatar
Elliot Lee committed
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
  if (width)
    *width = window_private->width;
  if (height)
    *height = window_private->height;
}

GdkVisual*
gdk_window_get_visual (GdkWindow *window)
{
  GdkWindowPrivate *window_private;
  XWindowAttributes window_attributes;
1156
  
Elliot Lee's avatar
Elliot Lee committed
1157
  g_return_val_if_fail (window != NULL, NULL);
1158
  
Elliot Lee's avatar
Elliot Lee committed
1159 1160 1161
  window_private = (GdkWindowPrivate*) window;
  while (window_private && (window_private->window_type == GDK_WINDOW_PIXMAP))
    window_private = (GdkWindowPrivate*) window_private->parent;
1162
  
1163
  if (window_private && !window_private->destroyed)
Elliot Lee's avatar
Elliot Lee committed
1164 1165 1166 1167
    {
      XGetWindowAttributes (window_private->xdisplay,
			    window_private->xwindow,
			    &window_attributes);
1168
      
Elliot Lee's avatar
Elliot Lee committed
1169 1170
      return gdk_visual_lookup (window_attributes.visual);
    }
1171
  
Elliot Lee's avatar
Elliot Lee committed
1172 1173 1174 1175 1176 1177 1178 1179
  return NULL;
}

GdkColormap*
gdk_window_get_colormap (GdkWindow *window)
{
  GdkWindowPrivate *window_private;
  XWindowAttributes window_attributes;
1180
  
Elliot Lee's avatar
Elliot Lee committed
1181
  g_return_val_if_fail (window != NULL, NULL);
1182
  
Elliot Lee's avatar
Elliot Lee committed
1183
  window_private = (GdkWindowPrivate*) window;
1184
  
1185 1186 1187 1188 1189 1190 1191 1192
  if (!window_private->destroyed)
    {
      XGetWindowAttributes (window_private->xdisplay,
			    window_private->xwindow,
			    &window_attributes);
      
      return gdk_colormap_lookup (window_attributes.colormap);
    }
1193
  
1194
  return NULL;
Elliot Lee's avatar
Elliot Lee committed
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
}

GdkWindowType
gdk_window_get_type (GdkWindow *window)
{
  GdkWindowPrivate *window_private;

  g_return_val_if_fail (window != NULL, (GdkWindowType) -1);

  window_private = (GdkWindowPrivate*) window;
  return window_private->window_type;
}

gint
gdk_window_get_origin (GdkWindow *window,
		       gint      *x,
		       gint      *y)
{
  GdkWindowPrivate *private;
  gint return_val;
  Window child;
  gint tx, ty;

  g_return_val_if_fail (window != NULL, 0);

  private = (GdkWindowPrivate*) window;

1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
  if (!private->destroyed)
    {
      return_val = XTranslateCoordinates (private->xdisplay,
					  private->xwindow,
					  gdk_root_window,
					  0, 0, &tx, &ty,
					  &child);
      
      if (x)
	*x = tx;
      if (y)
	*y = ty;
    }
  else
    return_val = 0;
  
Elliot Lee's avatar
Elliot Lee committed
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
  return return_val;
}

GdkWindow*
gdk_window_get_pointer (GdkWindow       *window,
			gint            *x,
			gint            *y,
			GdkModifierType *mask)
{
  GdkWindowPrivate *private;
  GdkWindow *return_val;
  Window root;
  Window child;
  int rootx, rooty;
  int winx, winy;
  unsigned int xmask;

  if (!window)
    window = (GdkWindow*) &gdk_root_parent;

  private = (GdkWindowPrivate*) window;

  return_val = NULL;
1261 1262
  if (!private->destroyed &&
      XQueryPointer (private->xdisplay, private->xwindow, &root, &child,
Elliot Lee's avatar
Elliot Lee committed
1263 1264 1265 1266 1267
		     &rootx, &rooty, &winx, &winy, &xmask))
    {
      if (x) *x = winx;
      if (y) *y = winy;
      if (mask) *mask = xmask;
1268
      
Elliot Lee's avatar
Elliot Lee committed
1269 1270 1271
      if (child)
	return_val = gdk_window_lookup (child);
    }
1272
  
Elliot Lee's avatar
Elliot Lee committed
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
  return return_val;
}

GdkWindow*
gdk_window_get_parent (GdkWindow *window)
{
  g_return_val_if_fail (window != NULL, NULL);

  return ((GdkWindowPrivate*) window)->parent;
}

GdkWindow*
gdk_window_get_toplevel (GdkWindow *window)
{
  GdkWindowPrivate *private;

  g_return_val_if_fail (window != NULL, NULL);

  private = (GdkWindowPrivate*) window;

  while (private->window_type == GDK_WINDOW_CHILD)
    {
      window = ((GdkWindowPrivate*) window)->parent;
      private = (GdkWindowPrivate*) window;
    }

  return window;
}

GList*
gdk_window_get_children (GdkWindow *window)
{
  GdkWindowPrivate *private;
  GdkWindow *child;
  GList *children;
  Window root;
  Window parent;
  Window *xchildren;
  unsigned int nchildren;
  unsigned int i;

  g_return_val_if_fail (window != NULL, NULL);

  private = (GdkWindowPrivate*) window;
1317 1318
  if (private->destroyed)
    return NULL;
Elliot Lee's avatar
Elliot Lee committed
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342

  XQueryTree (private->xdisplay, private->xwindow,
	      &root, &parent, &xchildren, &nchildren);

  children = NULL;

  if (nchildren > 0)
    {
      for (i = 0; i < nchildren; i++)
	{
	  child = gdk_window_lookup (xchildren[i]);
          if (child)
            children = g_list_prepend (children, child);
	}

      XFree (xchildren);
    }

  return children;
}

GdkEventMask  
gdk_window_get_events      (GdkWindow       *window)
{
1343
  GdkWindowPrivate *private;
Elliot Lee's avatar
Elliot Lee committed
1344 1345 1346 1347
  XWindowAttributes attrs;
  GdkEventMask event_mask;
  int i;

1348 1349 1350 1351 1352 1353 1354
  g_return_val_if_fail (window != NULL, 0);

  private = (GdkWindowPrivate*) window;
  if (private->destroyed)
    return 0;

  XGetWindowAttributes (gdk_display, private->xwindow, 
Elliot Lee's avatar
Elliot Lee committed
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
			&attrs);

  event_mask = 0;
  for (i = 0; i < nevent_masks; i++)
    {
      if (attrs.your_event_mask & event_mask_table[i])
	event_mask |= 1 << (i + 1);
    }

  return event_mask;
}

void          
gdk_window_set_events      (GdkWindow       *window,
			    GdkEventMask     event_mask)
{
1371
  GdkWindowPrivate *private;
Elliot Lee's avatar
Elliot Lee committed
1372 1373 1374
  long xevent_mask;
  int i;

1375 1376 1377 1378 1379 1380
  g_return_if_fail (window != NULL);

  private = (GdkWindowPrivate*) window;
  if (private->destroyed)
    return;

Elliot Lee's avatar
Elliot Lee committed
1381 1382 1383