gdkwindow.c 51.9 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* 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
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser 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

20
/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

Owen Taylor's avatar
Owen Taylor committed
27
#include "gdkwindow.h"
28 29 30
#include "gdkinternals.h"
#include "gdk.h"		/* For gdk_rectangle_union() */
#include "gdkpixmap.h"
31 32
#include "gdkdrawable.h"
#include "gdkpixmap.h"
33

Tor Lillqvist's avatar
Tor Lillqvist committed
34
#define USE_BACKING_STORE	/* Appears to work on Win32, too, now. */
35

36 37 38 39 40 41 42 43 44
typedef struct _GdkWindowPaint GdkWindowPaint;

struct _GdkWindowPaint
{
  GdkRegion *region;
  GdkPixmap *pixmap;
  gint x_offset;
  gint y_offset;
};
45 46 47
static GdkGC *gdk_window_create_gc      (GdkDrawable     *drawable,
                                         GdkGCValues     *values,
                                         GdkGCValuesMask  mask);
48
static void   gdk_window_draw_rectangle (GdkDrawable     *drawable,
49 50 51 52 53 54
                                         GdkGC           *gc,
                                         gint             filled,
                                         gint             x,
                                         gint             y,
                                         gint             width,
                                         gint             height);
55
static void   gdk_window_draw_arc       (GdkDrawable     *drawable,
56 57 58 59 60 61 62 63
                                         GdkGC           *gc,
                                         gint             filled,
                                         gint             x,
                                         gint             y,
                                         gint             width,
                                         gint             height,
                                         gint             angle1,
                                         gint             angle2);
64
static void   gdk_window_draw_polygon   (GdkDrawable     *drawable,
65 66 67 68
                                         GdkGC           *gc,
                                         gint             filled,
                                         GdkPoint        *points,
                                         gint             npoints);
69
static void   gdk_window_draw_text      (GdkDrawable     *drawable,
70 71 72 73 74 75
                                         GdkFont         *font,
                                         GdkGC           *gc,
                                         gint             x,
                                         gint             y,
                                         const gchar     *text,
                                         gint             text_length);
76
static void   gdk_window_draw_text_wc   (GdkDrawable     *drawable,
77 78 79 80 81 82
                                         GdkFont         *font,
                                         GdkGC           *gc,
                                         gint             x,
                                         gint             y,
                                         const GdkWChar  *text,
                                         gint             text_length);
83
static void   gdk_window_draw_drawable  (GdkDrawable     *drawable,
84 85 86 87 88 89 90 91
                                         GdkGC           *gc,
                                         GdkPixmap       *src,
                                         gint             xsrc,
                                         gint             ysrc,
                                         gint             xdest,
                                         gint             ydest,
                                         gint             width,
                                         gint             height);
92
static void   gdk_window_draw_points    (GdkDrawable     *drawable,
93 94 95
                                         GdkGC           *gc,
                                         GdkPoint        *points,
                                         gint             npoints);
96
static void   gdk_window_draw_segments  (GdkDrawable     *drawable,
97 98 99
                                         GdkGC           *gc,
                                         GdkSegment      *segs,
                                         gint             nsegs);
100 101 102 103
static void   gdk_window_draw_lines     (GdkDrawable     *drawable,
					 GdkGC           *gc,
					 GdkPoint        *points,
					 gint             npoints);
104 105 106 107 108 109
static void   gdk_window_draw_glyphs    (GdkDrawable      *drawable,
					 GdkGC            *gc,
					 PangoFont        *font,
					 gint              x,
					 gint              y,
					 PangoGlyphString *glyphs);
110

111 112 113 114 115 116 117 118 119 120
static void   gdk_window_draw_image     (GdkDrawable     *drawable,
                                         GdkGC           *gc,
                                         GdkImage        *image,
                                         gint             xsrc,
                                         gint             ysrc,
                                         gint             xdest,
                                         gint             ydest,
                                         gint             width,
                                         gint             height);

121 122 123 124 125 126
static GdkImage*  gdk_window_get_image  (GdkDrawable     *drawable,
                                         gint             x,
                                         gint             y,
                                         gint             width,
                                         gint             height);

127 128 129 130 131 132 133 134 135 136

static void   gdk_window_real_get_size  (GdkDrawable     *drawable,
                                         gint            *width,
                                         gint            *height);

static GdkVisual*   gdk_window_real_get_visual   (GdkDrawable *drawable);
static gint         gdk_window_real_get_depth    (GdkDrawable *drawable);
static void         gdk_window_real_set_colormap (GdkDrawable *drawable,
                                             GdkColormap *cmap);
static GdkColormap* gdk_window_real_get_colormap (GdkDrawable *drawable);
137 138 139 140 141 142 143 144 145

static GdkDrawable* gdk_window_get_composite_drawable (GdkDrawable *drawable,
                                                       gint         x,
                                                       gint         y,
                                                       gint         width,
                                                       gint         height,
                                                       gint        *composite_x_offset,
                                                       gint        *composite_y_offset);

Owen Taylor's avatar
Owen Taylor committed
146
static void gdk_window_free_paint_stack (GdkWindow *window);
147

148 149 150
static void gdk_window_init       (GdkWindowObject      *window);
static void gdk_window_class_init (GdkWindowObjectClass *klass);
static void gdk_window_finalize   (GObject              *object);
151

152 153 154 155
static gpointer parent_class = NULL;

GType
gdk_window_object_get_type (void)
rhlabs's avatar
rhlabs committed
156
{
157
  static GType object_type = 0;
rhlabs's avatar
rhlabs committed
158

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  if (!object_type)
    {
      static const GTypeInfo object_info =
      {
        sizeof (GdkWindowObjectClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gdk_window_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GdkWindowObject),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gdk_window_init,
      };
      
      object_type = g_type_register_static (GDK_TYPE_DRAWABLE,
                                            "GdkWindow",
176
                                            &object_info, 0);
177 178 179 180
    }
  
  return object_type;
}
181

182 183 184 185
static void
gdk_window_init (GdkWindowObject *window)
{
  /* 0-initialization is good for all other fields. */
rhlabs's avatar
rhlabs committed
186

187
  window->window_type = GDK_WINDOW_CHILD;
Elliot Lee's avatar
Elliot Lee committed
188

189
  window->impl = g_object_new (_gdk_window_impl_get_type (), NULL);
190
}
191

192 193 194 195 196
static void
gdk_window_class_init (GdkWindowObjectClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
197
  
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  parent_class = g_type_class_peek_parent (klass);

  object_class->finalize = gdk_window_finalize;

  drawable_class->create_gc = gdk_window_create_gc;
  drawable_class->draw_rectangle = gdk_window_draw_rectangle;
  drawable_class->draw_arc = gdk_window_draw_arc;
  drawable_class->draw_polygon = gdk_window_draw_polygon;
  drawable_class->draw_text = gdk_window_draw_text;
  drawable_class->draw_text_wc = gdk_window_draw_text_wc;
  drawable_class->draw_drawable = gdk_window_draw_drawable;
  drawable_class->draw_points = gdk_window_draw_points;
  drawable_class->draw_segments = gdk_window_draw_segments;
  drawable_class->draw_lines = gdk_window_draw_lines;
  drawable_class->draw_glyphs = gdk_window_draw_glyphs;
  drawable_class->draw_image = gdk_window_draw_image;
  drawable_class->get_depth = gdk_window_real_get_depth;
  drawable_class->get_size = gdk_window_real_get_size;
  drawable_class->set_colormap = gdk_window_real_set_colormap;
  drawable_class->get_colormap = gdk_window_real_get_colormap;
  drawable_class->get_visual = gdk_window_real_get_visual;
219 220
  drawable_class->get_image = gdk_window_get_image;
  drawable_class->get_composite_drawable = gdk_window_get_composite_drawable;
221
}
222

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
static void
gdk_window_finalize (GObject *object)
{
  GdkWindow *window = GDK_WINDOW (object);
  GdkWindowObject *obj = (GdkWindowObject *) object;
  
  if (!GDK_WINDOW_DESTROYED (window))
    {
      if (GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
	{
	  g_warning ("losing last reference to undestroyed window\n");
	  _gdk_window_destroy (window, FALSE);
	}
      else
	/* We use TRUE here, to keep us from actually calling
	 * XDestroyWindow() on the window
	 */
	_gdk_window_destroy (window, TRUE);
    }
242

243 244
  g_object_unref (G_OBJECT (obj->impl));
  obj->impl = NULL;
245
  
246
  G_OBJECT_CLASS (parent_class)->finalize (object);
Elliot Lee's avatar
Elliot Lee committed
247 248
}

Owen Taylor's avatar
Owen Taylor committed
249
/**
250
 * _gdk_window_destroy_hierarchy:
Owen Taylor's avatar
Owen Taylor committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
 * @window: a #GdkWindow
 * @recursing: If TRUE, then this is being called because a parent
 *            was destroyed. This generally means that the call to the windowing system
 *            to destroy the window can be omitted, since it will be destroyed as a result
 *            of the parent being destroyed. Unless @foreign_destroy
 *            
 * foreign_destroy: If TRUE, the window or a parent was destroyed by some external 
 *            agency. The window has already been destroyed and no windowing
 *            system calls should be made. (This may never happen for some
 *            windowing systems.)
 *
 * Internal function to destroy a window. Like gdk_window_destroy(), but does not
 * drop the reference count created by gdk_window_new().
 **/
static void
266
_gdk_window_destroy_hierarchy (GdkWindow *window,
Owen Taylor's avatar
Owen Taylor committed
267 268 269
			       gboolean   recursing,
			       gboolean   foreign_destroy)
{
270 271
  GdkWindowObject *private;
  GdkWindowObject *temp_private;
Owen Taylor's avatar
Owen Taylor committed
272 273 274 275 276 277
  GdkWindow *temp_window;
  GList *children;
  GList *tmp;
  
  g_return_if_fail (window != NULL);
  
278
  private = (GdkWindowObject*) window;
Owen Taylor's avatar
Owen Taylor committed
279
  
280
  switch (GDK_WINDOW_TYPE (window))
Owen Taylor's avatar
Owen Taylor committed
281 282 283 284 285 286
    {
    case GDK_WINDOW_TOPLEVEL:
    case GDK_WINDOW_CHILD:
    case GDK_WINDOW_DIALOG:
    case GDK_WINDOW_TEMP:
    case GDK_WINDOW_FOREIGN:
287
      if (!GDK_WINDOW_DESTROYED (window))
Owen Taylor's avatar
Owen Taylor committed
288 289
	{
	  private->mapped = FALSE;
290
	  private->destroyed = TRUE;
Owen Taylor's avatar
Owen Taylor committed
291 292 293 294 295
	  
	  _gdk_windowing_window_destroy (window, recursing, foreign_destroy);

	  if (private->parent)
	    {
296
	      GdkWindowObject *parent_private = (GdkWindowObject *)private->parent;
Owen Taylor's avatar
Owen Taylor committed
297 298 299 300 301 302 303
	      if (parent_private->children)
		parent_private->children = g_list_remove (parent_private->children, window);
	    }

	  _gdk_window_clear_update_area (window);
	  gdk_window_free_paint_stack (window);
	  
304 305 306
	  if (private->bg_pixmap &&
              private->bg_pixmap != GDK_PARENT_RELATIVE_BG &&
              private->bg_pixmap != GDK_NO_BG)
Owen Taylor's avatar
Owen Taylor committed
307 308 309 310 311
	    {
	      gdk_pixmap_unref (private->bg_pixmap);
	      private->bg_pixmap = NULL;
	    }
	  
312
	  if (GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
Owen Taylor's avatar
Owen Taylor committed
313 314 315 316 317 318 319 320 321
	    {
	      children = tmp = private->children;
	      private->children = NULL;
	      
	      while (tmp)
		{
		  temp_window = tmp->data;
		  tmp = tmp->next;
		  
322
		  temp_private = (GdkWindowObject*) temp_window;
Owen Taylor's avatar
Owen Taylor committed
323
		  if (temp_private)
324
		    _gdk_window_destroy_hierarchy (temp_window, TRUE, foreign_destroy);
Owen Taylor's avatar
Owen Taylor committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
		}
	      
	      g_list_free (children);
	    }
	  
	  if (private->filters)
	    {
	      tmp = private->filters;
	      
	      while (tmp)
		{
		  g_free (tmp->data);
		  tmp = tmp->next;
		}
	      
	      g_list_free (private->filters);
	      private->filters = NULL;
	    }
343 344

          gdk_drawable_set_colormap (GDK_DRAWABLE (window), NULL);
Owen Taylor's avatar
Owen Taylor committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	}
      break;
      
    case GDK_WINDOW_ROOT:
      g_error ("attempted to destroy root window");
      break;
    }
}

/**
 * _gdk_window_destroy:
 * @window: a #GdkWindow
 * foreign_destroy: If TRUE, the window or a parent was destroyed by some external 
 *            agency. The window has already been destroyed and no windowing
 *            system calls should be made. (This may never happen for some
 *            windowing systems.)
 *
 * Internal function to destroy a window. Like gdk_window_destroy(), but does not
 * drop the reference count created by gdk_window_new().
 **/
void
_gdk_window_destroy (GdkWindow *window,
		     gboolean   foreign_destroy)
{
369
  _gdk_window_destroy_hierarchy (window, FALSE, foreign_destroy);
Owen Taylor's avatar
Owen Taylor committed
370 371 372 373 374
}

void
gdk_window_destroy (GdkWindow *window)
{
375
  _gdk_window_destroy_hierarchy (window, FALSE, FALSE);
Owen Taylor's avatar
Owen Taylor committed
376 377 378
  gdk_drawable_unref (window);
}

Elliot Lee's avatar
Elliot Lee committed
379
void
380 381
gdk_window_set_user_data (GdkWindow *window,
			  gpointer   user_data)
382 383
{
  g_return_if_fail (window != NULL);
384
  
385
  ((GdkWindowObject*)window)->user_data = user_data;
386 387
}

Elliot Lee's avatar
Elliot Lee committed
388
void
389 390
gdk_window_get_user_data (GdkWindow *window,
			  gpointer  *data)
Elliot Lee's avatar
Elliot Lee committed
391 392
{
  g_return_if_fail (window != NULL);
393
  
394 395 396 397 398 399 400 401 402
  *data = ((GdkWindowObject*)window)->user_data;
}

GdkWindowType
gdk_window_get_window_type (GdkWindow *window)
{
  g_return_val_if_fail (GDK_IS_WINDOW (window), (GdkWindowType) -1);
  
  return GDK_WINDOW_TYPE (window);
Elliot Lee's avatar
Elliot Lee committed
403 404 405 406 407 408
}

void
gdk_window_get_position (GdkWindow *window,
			 gint      *x,
			 gint      *y)
409
{
410
  GdkWindowObject *obj;
411
  
412
  g_return_if_fail (GDK_IS_WINDOW (window));
413
  
414
  obj = (GdkWindowObject*) window;
415
  
416
  if (x)
417
    *x = obj->x;
418
  if (y)
419
    *y = obj->y;
Elliot Lee's avatar
Elliot Lee committed
420 421
}

422 423
GdkWindow*
gdk_window_get_parent (GdkWindow *window)
424
{
425
  g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
426
  
427
  return (GdkWindow*) ((GdkWindowObject*) window)->parent;
428 429
}

430 431
GdkWindow*
gdk_window_get_toplevel (GdkWindow *window)
Elliot Lee's avatar
Elliot Lee committed
432
{
433
  GdkWindowObject *obj;
434
  
435 436
  g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);

437 438 439
  obj = (GdkWindowObject *)window;
  while (GDK_WINDOW_TYPE (obj) == GDK_WINDOW_CHILD)
    obj = (GdkWindowObject *)obj->parent;
440
  
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
  return GDK_WINDOW (obj);
}

GList*
gdk_window_get_children (GdkWindow *window)
{
  g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);

  if (GDK_WINDOW_DESTROYED (window))
    return NULL;

  return g_list_copy (GDK_WINDOW_OBJECT (window)->children);
}

GList *
gdk_window_peek_children (GdkWindow       *window)
{
  g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);

  if (GDK_WINDOW_DESTROYED (window))
    return NULL;

  return GDK_WINDOW_OBJECT (window)->children;
Elliot Lee's avatar
Elliot Lee committed
464 465
}

466
void          
467 468 469
gdk_window_add_filter (GdkWindow     *window,
		       GdkFilterFunc  function,
		       gpointer       data)
470
{
471
  GdkWindowObject *private;
472 473
  GList *tmp_list;
  GdkEventFilter *filter;
474
  
475 476 477
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));

478 479
  private = (GdkWindowObject*) window;
  if (private && GDK_WINDOW_DESTROYED (window))
480
    return;
481 482
  
  if (private)
483 484
    tmp_list = private->filters;
  else
485
    tmp_list = gdk_default_filters;
486
  
487 488 489 490 491 492 493
  while (tmp_list)
    {
      filter = (GdkEventFilter *)tmp_list->data;
      if ((filter->function == function) && (filter->data == data))
	return;
      tmp_list = tmp_list->next;
    }
494
  
495 496 497
  filter = g_new (GdkEventFilter, 1);
  filter->function = function;
  filter->data = data;
498 499
  
  if (private)
500 501
    private->filters = g_list_append (private->filters, filter);
  else
502
    gdk_default_filters = g_list_append (gdk_default_filters, filter);
503 504 505
}

void
506 507 508
gdk_window_remove_filter (GdkWindow     *window,
			  GdkFilterFunc  function,
			  gpointer       data)
509
{
510
  GdkWindowObject *private;
511
  GList *tmp_list, *node;
512
  GdkEventFilter *filter;
513
  
514 515 516
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));

517
  private = (GdkWindowObject*) window;
518 519
  
  if (private)
520 521
    tmp_list = private->filters;
  else
522
    tmp_list = gdk_default_filters;
523
  
524 525 526
  while (tmp_list)
    {
      filter = (GdkEventFilter *)tmp_list->data;
527
      node = tmp_list;
528
      tmp_list = tmp_list->next;
529
      
530 531
      if ((filter->function == function) && (filter->data == data))
	{
532
	  if (private)
533
	    private->filters = g_list_remove_link (private->filters, node);
534
	  else
535
	    gdk_default_filters = g_list_remove_link (gdk_default_filters, node);
536
	  g_list_free_1 (node);
537
	  g_free (filter);
538 539 540 541 542 543
	  
	  return;
	}
    }
}

544 545 546 547 548
GList *
gdk_window_get_toplevels (void)
{
  GList *new_list = NULL;
  GList *tmp_list;
549
  
550
  tmp_list = ((GdkWindowObject *)gdk_parent_root)->children;
551 552 553 554 555
  while (tmp_list)
    {
      new_list = g_list_prepend (new_list, tmp_list->data);
      tmp_list = tmp_list->next;
    }
556
  
557 558 559
  return new_list;
}

560 561 562 563 564 565 566 567 568 569 570 571
/*************************************************************
 * gdk_window_is_visible:
 *     Check if the given window is mapped.
 *   arguments:
 *     window: 
 *   results:
 *     is the window mapped
 *************************************************************/

gboolean 
gdk_window_is_visible (GdkWindow *window)
{
572
  GdkWindowObject *private = (GdkWindowObject *)window;
573
  
574
  g_return_val_if_fail (window != NULL, FALSE);
575
  g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
576
  
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
  return private->mapped;
}

/*************************************************************
 * gdk_window_is_viewable:
 *     Check if the window and all ancestors of the window
 *     are mapped. (This is not necessarily "viewable" in
 *     the X sense, since we only check as far as we have
 *     GDK window parents, not to the root window)
 *   arguments:
 *     window:
 *   results:
 *     is the window viewable
 *************************************************************/

gboolean 
gdk_window_is_viewable (GdkWindow *window)
{
595
  GdkWindowObject *private = (GdkWindowObject *)window;
596
  
597
  g_return_val_if_fail (window != NULL, FALSE);
598
  g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
599
  
600
  while (private && 
601 602
	 (private != (GdkWindowObject *)gdk_parent_root) &&
	 (GDK_WINDOW_TYPE (private) != GDK_WINDOW_FOREIGN))
603 604 605
    {
      if (!private->mapped)
	return FALSE;
606
      
607
      private = (GdkWindowObject *)private->parent;
608
    }
609
  
610 611 612
  return TRUE;
}

613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
void
gdk_window_begin_paint_rect (GdkWindow    *window,
			     GdkRectangle *rectangle)
{
  GdkRegion *region;

  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));

  region = gdk_region_rectangle (rectangle);
  gdk_window_begin_paint_region (window, region);
  gdk_region_destroy (region);
}

static GdkGC *
gdk_window_get_bg_gc (GdkWindow *window, GdkWindowPaint *paint)
{
630
  GdkWindowObject *private = (GdkWindowObject *)window;
631 632 633 634 635 636 637 638 639 640

  guint gc_mask = 0;
  GdkGCValues gc_values;

  if (private->bg_pixmap == GDK_PARENT_RELATIVE_BG && private->parent)
    {
      GdkWindowPaint tmp_paint = *paint;
      tmp_paint.x_offset += private->x;
      tmp_paint.y_offset += private->y;
      
641
      return gdk_window_get_bg_gc (GDK_WINDOW (private->parent), &tmp_paint);
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
    }
  else if (private->bg_pixmap && private->bg_pixmap != GDK_PARENT_RELATIVE_BG && private->bg_pixmap != GDK_NO_BG)
    {
      gc_values.fill = GDK_TILED;
      gc_values.tile = private->bg_pixmap;
      gc_values.ts_x_origin = - paint->x_offset;
      gc_values.ts_y_origin = - paint->y_offset;
      
      gc_mask = GDK_GC_FILL | GDK_GC_TILE | GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN;
    }
  else
    {
      gc_values.foreground = private->bg_color;
      gc_mask = GDK_GC_FOREGROUND;
    }

  return gdk_gc_new_with_values (paint->pixmap, &gc_values, gc_mask);
}

static void
gdk_window_paint_init_bg (GdkWindow      *window,
			  GdkWindowPaint *paint,
			  GdkRegion      *init_region)
{
  GdkGC *tmp_gc;
667
  
668
  tmp_gc = gdk_window_get_bg_gc (window, paint);
669 670 671 672 673 674

  gdk_region_offset (init_region,
                     - paint->x_offset,
                     - paint->y_offset);
  gdk_gc_set_clip_region (tmp_gc, init_region);

675 676 677
  gdk_draw_rectangle (paint->pixmap, tmp_gc, TRUE, 0, 0, -1, -1);
  gdk_gc_unref (tmp_gc);
}
678

679
#ifdef GDK_WINDOWING_X11
680
#include "x11/gdkx.h"
681 682
#endif

683 684 685 686
void	      
gdk_window_begin_paint_region (GdkWindow *window,
			       GdkRegion *region)
{
687
#ifdef USE_BACKING_STORE
688
  GdkWindowObject *private = (GdkWindowObject *)window;
689 690 691 692 693 694 695 696
  GdkRectangle clip_box;
  GdkWindowPaint *paint;
  GdkRegion *init_region;
  GdkGC *tmp_gc;
  
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));

697
  if (GDK_WINDOW_DESTROYED (window))
Owen Taylor's avatar
Owen Taylor committed
698 699
    return;
  
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
  paint = g_new (GdkWindowPaint, 1);

  paint->region = gdk_region_copy (region);

  init_region = gdk_region_copy (region);
  gdk_region_get_clipbox (paint->region, &clip_box);

  if (private->paint_stack)
    {
      gint old_width, old_height;
      GdkWindowPaint *tmp_paint = private->paint_stack->data;
      GdkRectangle old_rect, new_rect;
      GSList *tmp_list;

      gdk_drawable_get_size (tmp_paint->pixmap, &old_width, &old_height);
      old_rect.x = tmp_paint->x_offset;
      old_rect.y = tmp_paint->y_offset;
      old_rect.width = old_width;
      old_rect.height = old_height;

      gdk_rectangle_union (&clip_box, &old_rect, &new_rect);

      if (new_rect.width > old_rect.width || new_rect.height > old_rect.height)
	{
724 725
	  paint->pixmap = gdk_pixmap_new (window,
                                          new_rect.width, new_rect.height, -1);
726 727
          tmp_gc = gdk_gc_new (paint->pixmap);
	  gdk_draw_drawable (paint->pixmap, tmp_gc, tmp_paint->pixmap,
728 729 730
			     0, 0,
			     old_rect.x - new_rect.x, old_rect.y - new_rect.y,
                             old_rect.width, old_rect.height);
731 732 733 734 735 736 737 738 739
          gdk_gc_unref (tmp_gc);
	  gdk_drawable_unref (tmp_paint->pixmap);

	  paint->x_offset = new_rect.x;
	  paint->y_offset = new_rect.y;
	  
	  tmp_list = private->paint_stack;
	  while (tmp_list)
	    {
740
	      tmp_paint = tmp_list->data;
741 742 743 744
	      gdk_region_subtract (init_region, tmp_paint->region);

	      tmp_paint->pixmap = paint->pixmap;
	      tmp_paint->x_offset = paint->x_offset;
745 746
	      tmp_paint->y_offset = paint->y_offset;
              
747 748 749 750 751 752 753 754 755 756 757 758
	      tmp_list = tmp_list->next;
	    }
	}
      else
	{
	  paint->x_offset = tmp_paint->x_offset;
	  paint->y_offset = tmp_paint->y_offset;
	  paint->pixmap = tmp_paint->pixmap;

	  tmp_list = private->paint_stack;
	  while (tmp_list)
	    {
759
	      tmp_paint = tmp_list->data;
760
	      gdk_region_subtract (init_region, tmp_paint->region);
Elliot Lee's avatar
Elliot Lee committed
761

762 763 764 765 766 767 768 769 770 771 772 773 774
	      tmp_list = tmp_list->next;
	    }
	}
    }
  else
    {
      paint->x_offset = clip_box.x;
      paint->y_offset = clip_box.y;
      paint->pixmap = gdk_pixmap_new (window, clip_box.width, clip_box.height, -1);
    }

  if (!gdk_region_empty (init_region))
    gdk_window_paint_init_bg (window, paint, init_region);
775
  
776 777 778
  gdk_region_destroy (init_region);
  
  private->paint_stack = g_slist_prepend (private->paint_stack, paint);
779
#endif /* USE_BACKING_STORE */
780 781 782 783 784
}

void
gdk_window_end_paint (GdkWindow *window)
{
785
#ifdef USE_BACKING_STORE
786
  GdkWindowObject *private = (GdkWindowObject *)window;
787 788 789 790 791 792 793
  GdkWindowPaint *paint;
  GdkGC *tmp_gc;
  GdkRectangle clip_box;
  gint x_offset, y_offset;

  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
Owen Taylor's avatar
Owen Taylor committed
794

795
  if (GDK_WINDOW_DESTROYED (window))
Owen Taylor's avatar
Owen Taylor committed
796 797
    return;
  
798 799 800 801 802 803 804 805 806 807 808 809 810 811
  g_return_if_fail (private->paint_stack != NULL);

  paint = private->paint_stack->data;
  private->paint_stack = g_slist_delete_link (private->paint_stack, private->paint_stack);

  gdk_region_get_clipbox (paint->region, &clip_box);

  tmp_gc = gdk_gc_new (window);

  _gdk_windowing_window_get_offsets (window, &x_offset, &y_offset);

  gdk_gc_set_clip_region (tmp_gc, paint->region);
  gdk_gc_set_clip_origin (tmp_gc, -x_offset, -y_offset);

812 813 814 815 816
  gdk_draw_drawable (private->impl, tmp_gc, paint->pixmap,
                     clip_box.x - paint->x_offset,
                     clip_box.y - paint->y_offset,
                     clip_box.x - x_offset, clip_box.y - y_offset,
                     clip_box.width, clip_box.height);
817 818 819 820 821 822 823 824 825
  gdk_gc_unref (tmp_gc);

  if (private->paint_stack)
    {
      GSList *tmp_list = private->paint_stack;
      while (tmp_list)
	{
	  GdkWindowPaint *tmp_paint = tmp_list->data;
	  gdk_region_subtract (tmp_paint->region, paint->region);
826
          
827 828 829 830 831 832 833 834
	  tmp_list = tmp_list->next;
	}
    }
  else
    gdk_drawable_unref (paint->pixmap);

  gdk_region_destroy (paint->region);
  g_free (paint);
835
#endif /* USE_BACKING_STORE */
836 837
}

Owen Taylor's avatar
Owen Taylor committed
838 839 840
static void
gdk_window_free_paint_stack (GdkWindow *window)
{
841
  GdkWindowObject *private = (GdkWindowObject *)window;
Owen Taylor's avatar
Owen Taylor committed
842 843 844 845 846 847 848 849
  
  if (private->paint_stack)
    {
      GSList *tmp_list = private->paint_stack;

      while (tmp_list)
	{
	  GdkWindowPaint *paint = tmp_list->data;
Elliot Lee's avatar
Elliot Lee committed
850

Owen Taylor's avatar
Owen Taylor committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864
	  if (tmp_list == private->paint_stack)
	    gdk_drawable_unref (paint->pixmap);
		  
	  gdk_region_destroy (paint->region);
	  g_free (paint);

	  tmp_list = tmp_list->next;
	}

      g_slist_free (private->paint_stack);
      private->paint_stack = NULL;
    }
}

865 866 867 868 869
static void
gdk_window_get_offsets (GdkWindow *window,
			gint      *x_offset,
			gint      *y_offset)
{
870
  GdkWindowObject *private = (GdkWindowObject *)window;
871 872 873 874 875 876 877 878 879 880 881 882 883
  
  if (private->paint_stack)
    {
      GdkWindowPaint *paint = private->paint_stack->data;
      *x_offset = paint->x_offset;
      *y_offset = paint->y_offset;
    }
  else
    _gdk_windowing_window_get_offsets (window, x_offset, y_offset);
}

#define OFFSET_GC(gc)                                         \
    gint x_offset, y_offset; 				      \
884 885 886 887
    gint old_clip_x = gc->clip_x_origin;    \
    gint old_clip_y = gc->clip_y_origin;    \
    gint old_ts_x = gc->ts_x_origin;        \
    gint old_ts_y = gc->ts_y_origin;        \
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
    gdk_window_get_offsets (drawable, &x_offset, &y_offset);  \
    if (x_offset != 0 || y_offset != 0)             	      \
      {                                                       \
        gdk_gc_set_clip_origin (gc, old_clip_x - x_offset,    \
	  	                old_clip_y - y_offset);       \
        gdk_gc_set_ts_origin (gc, old_ts_x - x_offset,        \
	  	              old_ts_y - y_offset);           \
      }

#define RESTORE_GC(gc)                                      \
    if (x_offset != 0 || y_offset != 0)       		    \
     {                                                      \
       gdk_gc_set_clip_origin (gc, old_clip_x, old_clip_y); \
       gdk_gc_set_ts_origin (gc, old_ts_x, old_ts_y);       \
     }

static GdkGC *
905 906 907
gdk_window_create_gc (GdkDrawable     *drawable,
                      GdkGCValues     *values,
                      GdkGCValuesMask  mask)
908
{
909 910 911 912 913 914 915
  g_return_val_if_fail (GDK_IS_WINDOW (drawable), NULL);
  
  if (GDK_WINDOW_DESTROYED (drawable))
    return NULL;

  return gdk_gc_new_with_values (((GdkWindowObject *) drawable)->impl,
                                 values, mask);
916 917 918 919 920 921 922 923 924 925 926
}

static void
gdk_window_draw_rectangle (GdkDrawable *drawable,
			   GdkGC       *gc,
			   gint         filled,
			   gint         x,
			   gint         y,
			   gint         width,
			   gint         height)
{
927
  GdkWindowObject *private = (GdkWindowObject *)drawable;
928
  OFFSET_GC (gc);
929 930 931

  if (GDK_WINDOW_DESTROYED (drawable))
    return;
932 933 934 935 936 937 938 939
  
  if (private->paint_stack)
    {
      GdkWindowPaint *paint = private->paint_stack->data;
      gdk_draw_rectangle (paint->pixmap, gc, filled,
			  x - x_offset, y - y_offset, width, height);
    }
  else
940 941
    gdk_draw_rectangle (private->impl, gc, filled,
                        x - x_offset, y - y_offset, width, height);
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956

  RESTORE_GC (gc);
}

static void
gdk_window_draw_arc (GdkDrawable *drawable,
		     GdkGC       *gc,
		     gint         filled,
		     gint         x,
		     gint         y,
		     gint         width,
		     gint         height,
		     gint         angle1,
		     gint         angle2)
{
957
  GdkWindowObject *private = (GdkWindowObject *)drawable;
958 959
  OFFSET_GC (gc);

960 961 962
  if (GDK_WINDOW_DESTROYED (drawable))
    return;
  
963 964 965 966
  if (private->paint_stack)
    {
      GdkWindowPaint *paint = private->paint_stack->data;
      gdk_draw_arc (paint->pixmap, gc, filled,
967
		    x - x_offset, y - y_offset,
968 969 970
		    width, height, angle1, angle2);
    }
  else
971 972 973
    gdk_draw_arc (private->impl, gc, filled,
                  x - x_offset, y - y_offset,
                  width, height, angle1, angle2);
974 975 976 977 978 979 980 981 982 983
  RESTORE_GC (gc);
}

static void
gdk_window_draw_polygon (GdkDrawable *drawable,
			 GdkGC       *gc,
			 gint         filled,
			 GdkPoint    *points,
			 gint         npoints)
{
984
  GdkWindowObject *private = (GdkWindowObject *)drawable;
985 986 987 988
  GdkPoint *new_points;
  
  OFFSET_GC (gc);

989 990 991
  if (GDK_WINDOW_DESTROYED (drawable))
    return;
  
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
  if (x_offset != 0 || y_offset != 0)
    {
      int i;
      
      new_points = g_new (GdkPoint, npoints);
      for (i=0; i<npoints; i++)
	{
	  new_points[i].x = points[i].x - x_offset;
	  new_points[i].y = points[i].y - y_offset;
	}
    }
  else
    new_points = points;

  if (private->paint_stack)
    {
      GdkWindowPaint *paint = private->paint_stack->data;
      gdk_draw_polygon (paint->pixmap, gc, filled, new_points, npoints);

    }
  else
1013 1014
    gdk_draw_polygon (private->impl, gc, filled, new_points, npoints);
  
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
  if (new_points != points)
    g_free (new_points);

  RESTORE_GC (gc);
}

static void
gdk_window_draw_text (GdkDrawable *drawable,
		      GdkFont     *font,
		      GdkGC       *gc,
		      gint         x,
		      gint         y,
		      const gchar *text,
		      gint         text_length)
{
1030
  GdkWindowObject *private = (GdkWindowObject *)drawable;
1031 1032
  OFFSET_GC (gc);

1033 1034 1035
  if (GDK_WINDOW_DESTROYED (drawable))
    return;
  
1036 1037 1038 1039 1040 1041 1042 1043
  if (private->paint_stack)
    {
      GdkWindowPaint *paint = private->paint_stack->data;
      gdk_draw_text (paint->pixmap, font, gc, 
		     x - x_offset, y - y_offset, text, text_length);

    }
  else
1044 1045
    gdk_draw_text (private->impl, font, gc,
                   x - x_offset, y - y_offset, text, text_length);
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058

  RESTORE_GC (gc);
}

static void
gdk_window_draw_text_wc (GdkDrawable    *drawable,
			 GdkFont        *font,
			 GdkGC          *gc,
			 gint            x,
			 gint            y,
			 const GdkWChar *text,
			 gint            text_length)
{
1059
  GdkWindowObject *private = (GdkWindowObject *)drawable;
1060 1061
  OFFSET_GC (gc);

1062 1063 1064
  if (GDK_WINDOW_DESTROYED (drawable))
    return;
  
1065 1066 1067 1068 1069 1070 1071
  if (private->paint_stack)
    {
      GdkWindowPaint *paint = private->paint_stack->data;
      gdk_draw_text_wc (paint->pixmap, font, gc, 
			x - x_offset, y - y_offset, text, text_length);
    }
  else
1072 1073 1074
    gdk_draw_text_wc (private->impl, font, gc,
                      x - x_offset, y - y_offset, text, text_length);
  
1075 1076 1077
  RESTORE_GC (gc);
}

1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
static GdkDrawable*
gdk_window_get_composite_drawable (GdkDrawable *window,
                                   gint         x,
                                   gint         y,
                                   gint         width,
                                   gint         height,
                                   gint        *composite_x_offset,
                                   gint        *composite_y_offset)
{
  GdkWindowObject *private = (GdkWindowObject *)window;
  GdkWindowPaint *paint;
  GdkRegion *buffered_region;
  GSList *tmp_list;
  GdkPixmap *buffer;
  GdkPixmap *tmp_pixmap;
  GdkRectangle rect;
  GdkRegion *rect_region;
  GdkGC *tmp_gc;
  gint windowing_x_offset, windowing_y_offset;
  gint buffer_x_offset, buffer_y_offset;

  if (GDK_WINDOW_DESTROYED (window) || private->paint_stack == NULL)
    {
      /* No backing store */
      _gdk_windowing_window_get_offsets (window,
                                         composite_x_offset,
                                         composite_y_offset);
      
      return GDK_DRAWABLE (g_object_ref (G_OBJECT (window)));
    }
  
  buffered_region = NULL;
  buffer = NULL;

  /* All GtkWindowPaint structs have the same pixmap and offsets, just
   * get the first one. (should probably be cleaned up so that the
   * pixmap is stored in the window)
   */
  paint = private->paint_stack->data;
  buffer = paint->pixmap;
  buffer_x_offset = paint->x_offset;
  buffer_y_offset = paint->y_offset;
  
  tmp_list = private->paint_stack;
  while (tmp_list != NULL)
    {
      paint = tmp_list->data;
      
      if (buffered_region == NULL)
        buffered_region = gdk_region_copy (paint->region);
      else
        gdk_region_union (buffered_region, paint->region);

      tmp_list = g_slist_next (tmp_list);
    }

  /* See if the buffered part is overlapping the part we want
   * to get
   */
  rect.x = x;
  rect.y = y;
  rect.width = width;
  rect.height = height;

  rect_region = gdk_region_rectangle (&rect);
  
  gdk_region_intersect (buffered_region, rect_region);

  gdk_region_destroy (rect_region);

  if (gdk_region_empty (buffered_region))
    {
      gdk_region_destroy (buffered_region);

      _gdk_windowing_window_get_offsets (window,
                                         composite_x_offset,
                                         composite_y_offset);

      return GDK_DRAWABLE (g_object_ref (G_OBJECT (window)));
    }
  
  tmp_pixmap = gdk_pixmap_new (window,
                               width, height,
                               -1);

  tmp_gc = gdk_gc_new (tmp_pixmap);

  _gdk_windowing_window_get_offsets (window,
                                     &windowing_x_offset,
                                     &windowing_y_offset);
  
  /* Copy the current window contents */
  gdk_draw_drawable (tmp_pixmap,
                     tmp_gc,
                     private->impl,
                     x - windowing_x_offset,
                     y - windowing_y_offset,
                     0, 0,
                     width, height);

  /* Make buffered_region relative to the tmp_pixmap */
  gdk_region_offset (buffered_region,
                     - x,
                     - y);
  
  /* Set the clip mask to avoid drawing over non-buffered areas of
   * tmp_pixmap. 
   */
  
  gdk_gc_set_clip_region (tmp_gc, buffered_region);
  gdk_region_destroy (buffered_region);
  
  /* Draw backing pixmap onto the tmp_pixmap, offsetting
   * appropriately.
   */
  gdk_draw_drawable (tmp_pixmap,
                     tmp_gc,
                     buffer,
                     x - buffer_x_offset,
                     y - buffer_y_offset,
                     0, 0,
                     width, height);
  
  /* Set these to location of tmp_pixmap within the window */
  *composite_x_offset = x;
  *composite_y_offset = y;

  g_object_unref (G_OBJECT (tmp_gc));
  
  return tmp_pixmap;
}

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
static void
gdk_window_draw_drawable (GdkDrawable *drawable,
			  GdkGC       *gc,
			  GdkPixmap   *src,
			  gint         xsrc,
			  gint         ysrc,
			  gint         xdest,
			  gint         ydest,
			  gint         width,
			  gint         height)
{
1221
  GdkWindowObject *private = (GdkWindowObject *)drawable;
1222
  OFFSET_GC (gc);
1223
  
1224 1225
  if (GDK_WINDOW_DESTROYED (drawable))
    return;
1226 1227

  /* If we have a backing pixmap draw to that */
1228 1229 1230
  if (private->paint_stack)
    {
      GdkWindowPaint *paint = private->paint_stack->data;
1231 1232
      gdk_draw_drawable (paint->pixmap, gc,
                         src, xsrc, ysrc,
1233 1234 1235 1236
			 xdest - x_offset, ydest - y_offset, width, height);

    }
  else
1237 1238
    gdk_draw_drawable (private->impl, gc,
                       src, xsrc, ysrc,
1239 1240
                       xdest - x_offset, ydest - y_offset,
                       width, height);
1241

1242 1243 1244 1245 1246 1247 1248 1249 1250
  RESTORE_GC (gc);
}

static void
gdk_window_draw_points (GdkDrawable *drawable,
			GdkGC       *gc,
			GdkPoint    *points,
			gint         npoints)
{
1251
  GdkWindowObject *private = (GdkWindowObject *)drawable;
1252 1253 1254 1255
  GdkPoint *new_points;
  
  OFFSET_GC (gc);

1256 1257 1258
  if (GDK_WINDOW_DESTROYED (drawable))
    return;
  
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
  if (x_offset != 0 || y_offset != 0)
    {
      gint i;

      new_points = g_new (GdkPoint, npoints);
      for (i=0; i<npoints; i++)
	{
	  new_points[i].x = points[i].x - x_offset;
	  new_points[i].y = points[i].y - y_offset;
	}
    }
  else
    new_points = points;

  if (private->paint_stack)
    {
      GdkWindowPaint *paint = private->paint_stack->data;
      gdk_draw_points (paint->pixmap, gc, new_points, npoints);
    }
  else
1279
    gdk_draw_points (private->impl, gc, points, npoints);
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292

  if (new_points != points)
    g_free (new_points);

  RESTORE_GC (gc);
}

static void
gdk_window_draw_segments (GdkDrawable *drawable,
			  GdkGC       *gc,
			  GdkSegment  *segs,
			  gint         nsegs)
{
1293
  GdkWindowObject *private = (GdkWindowObject *)drawable;
1294 1295 1296 1297
  GdkSegment *new_segs;

  OFFSET_GC (gc);

1298 1299 1300
  if (GDK_WINDOW_DESTROYED (drawable))
    return;
  
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
  if (x_offset != 0 || y_offset != 0)
    {
      gint i;

      new_segs = g_new (GdkSegment, nsegs);
      for (i=0; i<nsegs; i++)
	{
	  new_segs[i].x1 = segs[i].x1 - x_offset;
	  new_segs[i].y1 = segs[i].y1 - y_offset;
	  new_segs[i].x2 = segs[i].x2 - x_offset;
	  new_segs[i].y2 = segs[i].y2 - y_offset;
	}
    }
  else
    new_segs = segs;

  if (private->paint_stack)
    {
      GdkWindowPaint *paint = private->paint_stack->data;
      gdk_draw_segments (paint->pixmap, gc, new_segs, nsegs);
    }
  else
1323 1324
    gdk_draw_segments (private->impl, gc, new_segs, nsegs);
  
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
  if (new_segs != segs)
    g_free (new_segs);

  RESTORE_GC (gc);
}

static void
gdk_window_draw_lines (GdkDrawable *drawable,
		       GdkGC       *gc,
		       GdkPoint    *points,
		       gint         npoints)
{
1337
  GdkWindowObject *private = (GdkWindowObject *)drawable;
1338 1339 1340 1341
  GdkPoint *new_points;

  OFFSET_GC (gc);

1342 1343 1344
  if (GDK_WINDOW_DESTROYED (drawable))
    return;
  
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
  if (x_offset != 0 || y_offset != 0)
    {
      gint i;

      new_points = g_new (GdkPoint, npoints);
      for (i=0; i<npoints; i++)
	{
	  new_points[i].x = points[i].x - x_offset;
	  new_points[i].y = points[i].y - y_offset;
	}
    }
  else
    new_points = points;

  if (private->paint_stack)
    {
      GdkWindowPaint *paint = private->paint_stack->data;
      gdk_draw_lines (paint->pixmap, gc, new_points, npoints);
    }
  else
1365
    gdk_draw_lines (private->impl, gc, new_points, npoints);
1366 1367 1368 1369 1370 1371 1372

  if (new_points != points)
    g_free (new_points);

  RESTORE_GC (gc);
}

1373 1374 1375 1376 1377 1378 1379 1380
static void
gdk_window_draw_glyphs (GdkDrawable      *drawable,
			GdkGC            *gc,
			PangoFont        *font,
			gint              x,
			gint              y,
			PangoGlyphString *glyphs)
{
1381
  GdkWindowObject *private = (GdkWindowObject *)drawable;
1382 1383 1384

  OFFSET_GC (gc);

1385 1386 1387
  if (GDK_WINDOW_DESTROYED (drawable))
    return;
  
1388 1389 1390
  if (private->paint_stack)
    {
      GdkWindowPaint *paint = private->paint_stack->data;
Elliot Lee's avatar
Elliot Lee committed
1391

1392 1393 1394
      gdk_draw_glyphs (paint->pixmap, gc, font, x - x_offset, y - y_offset, glyphs);
    }
  else
1395 1396
    gdk_draw_glyphs (private->impl, gc, font,
                     x - x_offset, y - y_offset, glyphs);
1397 1398 1399 1400

  RESTORE_GC (gc);
}

1401 1402 1403 1404 1405 1406 1407 1408
/* Fixme - this is just like gdk_window_paint_init_bg */
static void
gdk_window_clear_backing_rect (GdkWindow *window,
			       gint       x,
			       gint       y,
			       gint       width,
			       gint       height)
{
1409
  GdkWindowObject *private = (GdkWindowObject *)window;
1410 1411 1412
  GdkWindowPaint *paint = private->paint_stack->data;
  GdkGC *tmp_gc;

1413 1414 1415
  if (GDK_WINDOW_DESTROYED (window))
    return;
  
1416 1417 1418 1419 1420 1421
  tmp_gc = gdk_window_get_bg_gc (window, paint);
  gdk_draw_rectangle (paint->pixmap, tmp_gc, TRUE,
		      x - paint->x_offset, y - paint->y_offset, width, height);
  gdk_gc_unref (tmp_gc);
}

1422 1423 1424
void
gdk_window_clear (GdkWindow *window)
{
1425 1426
  gint width, height;
  
1427 1428 1429
  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));

1430 1431
  gdk_drawable_get_size (GDK_DRAWABLE (window), &width, &height);
  
1432
  gdk_window_clear_area (window, 0, 0,
1433
                         width, height);
1434 1435
}

1436 1437 1438 1439 1440 1441 1442
void
gdk_window_clear_area (GdkWindow *window,
		       gint       x,
		       gint       y,
		       gint       width,
		       gint       height)
{
1443
  GdkWindowObject *private = (GdkWindowObject *)window;
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460

  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
  
  if (private->paint_stack)
    gdk_window_clear_backing_rect (window, x, y, width, height);
  else
    _gdk_windowing_window_clear_area (window, x, y, width, height);
}

void
gdk_window_clear_area_e (GdkWindow *window,
		         gint       x,
		         gint       y,
		         gint       width,
		         gint       height)
{
1461
  GdkWindowObject *private = (GdkWindowObject *)window;
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471

  g_return_if_fail (window != NULL);
  g_return_if_fail (GDK_IS_WINDOW (window));
  
  if (private->paint_stack)
    gdk_window_clear_backing_rect (window, x, y, width, height);

  _gdk_windowing_window_clear_area_e (window, x, y, width, height);
}

1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
static void
gdk_window_draw_image (GdkDrawable *drawable,
                       GdkGC       *gc,
                       GdkImage    *image,
                       gint         xsrc,
                       gint         ysrc,
                       gint         xdest,
                       gint         ydest,
                       gint         width,
                       gint         height)
1482
{
1483
  GdkWindowObject *private = (GdkWindowObject *)drawable;
1484 1485

  OFFSET_GC (gc);
1486 1487 1488

  if (GDK_WINDOW_DESTROYED (drawable))
    return;
1489 1490 1491 1492
  
  if (private->paint_stack)
    {
      GdkWindowPaint *paint = private->paint_stack->data;
1493 1494 1495
      gdk_draw_image (paint->pixmap, gc, image, xsrc, ysrc,
                      xdest - x_offset, ydest - y_offset,
                      width, height);
1496 1497 1498

    }
  else
1499 1500 1501
    gdk_draw_image (private->impl, gc, image, xsrc, ysrc,
                    xdest - x_offset, ydest - y_offset,
                    width, height);
1502 1503 1504 1505

  RESTORE_GC (gc);
}

1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568

static void
gdk_window_real_get_size (GdkDrawable *drawable,
                          gint *width,
                          gint *height)
{
  g_return_if_fail (GDK_IS_WINDOW (drawable));

  gdk_drawable_get_size (GDK_WINDOW_OBJECT (drawable)->impl,
                         width, height);
}

static GdkVisual*
gdk_window_real_get_visual (GdkDrawable *drawable)
{
  GdkColormap *colormap;

  g_return_val_if_fail (GDK_IS_WINDOW (drawable), NULL);

  colormap = gdk_drawable_get_colormap (drawable);
  return colormap ? gdk_colormap_get_visual (colormap) : NULL;
}

static gint
gdk_window_real_get_depth (GdkDrawable *drawable)
{
  gint depth;
  
  g_return_val_if_fail (GDK_IS_WINDOW (drawable), 0);

  depth = ((GdkWindowObject *)GDK_WINDOW (drawable))->depth;

  if (depth == 0)
    {
      g_print ("0 depth for type %s\n", g_type_name (G_OBJECT_TYPE (drawable)));
      G_BREAKPOINT ();
    }

  return depth;
}

static void
gdk_window_real_set_colormap (GdkDrawable *drawable,
                              GdkColormap *cmap)
{
  g_return_if_fail (GDK_IS_WINDOW (drawable));  

  if (GDK_WINDOW_DESTROYED (drawable))
    return;
  
  gdk_drawable_set_colormap (((GdkWindowObject*)drawable)->impl, cmap);
}

static GdkColormap*
gdk_window_real_get_colormap (GdkDrawable *drawable)
{
  g_return_val_if_fail (GDK_IS_WINDOW