gdkgc.c 36.4 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GDK - The GIMP Drawing Kit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * 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
 * 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
11
 * 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/. 
 */

27
#include "config.h"
Elliot Lee's avatar
Elliot Lee committed
28
#include <string.h>
Owen Taylor's avatar
Started  
Owen Taylor committed
29

30
#include "gdkcairo.h"
Owen Taylor's avatar
Started  
Owen Taylor committed
31
#include "gdkgc.h"
32 33
#include "gdkinternals.h"
#include "gdkpixmap.h"
34
#include "gdkrgb.h"
Elliot Lee's avatar
Elliot Lee committed
35
#include "gdkprivate.h"
36
#include "gdkalias.h"
Elliot Lee's avatar
Elliot Lee committed
37

38 39
static void gdk_gc_finalize   (GObject      *object);

40
typedef struct _GdkGCPrivate GdkGCPrivate;
41

42
struct _GdkGCPrivate
Elliot Lee's avatar
Elliot Lee committed
43
{
44
  GdkRegion *clip_region;
45

46
  guint32 region_tag_applied;
47 48
  int region_tag_offset_x;
  int region_tag_offset_y;
Alexander Larsson's avatar
Alexander Larsson committed
49

50
  GdkRegion *old_clip_region;
51
  GdkPixmap *old_clip_mask;
Alexander Larsson's avatar
Alexander Larsson committed
52

53 54
  GdkBitmap *stipple;
  GdkPixmap *tile;
55 56

  GdkPixmap *clip_mask;
Alexander Larsson's avatar
Alexander Larsson committed
57

58 59
  guint32 fg_pixel;
  guint32 bg_pixel;
Alexander Larsson's avatar
Alexander Larsson committed
60 61 62 63

  guint subwindow_mode : 1;
  guint fill : 2;
  guint exposures : 2;
64 65 66 67
};

#define GDK_GC_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDK_TYPE_GC, GdkGCPrivate))

Matthias Clasen's avatar
Matthias Clasen committed
68
G_DEFINE_TYPE (GdkGC, gdk_gc, G_TYPE_OBJECT)
Elliot Lee's avatar
Elliot Lee committed
69

70
static void
71
gdk_gc_class_init (GdkGCClass *class)
72
{
73
  GObjectClass *object_class = G_OBJECT_CLASS (class);
74
  
75 76 77
  object_class->finalize = gdk_gc_finalize;

  g_type_class_add_private (object_class, sizeof (GdkGCPrivate));
78 79
}

80 81 82 83 84 85 86 87 88 89 90 91 92
static void
gdk_gc_init (GdkGC *gc)
{
  GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);

  priv->fill = GDK_SOLID;

  /* These are the default X11 value, which we match. They are clearly
   * wrong for TrueColor displays, so apps have to change them.
   */
  priv->fg_pixel = 0;
  priv->bg_pixel = 1;
}
Matthias Clasen's avatar
Matthias Clasen committed
93 94 95 96 97 98 99 100 101 102

/**
 * gdk_gc_new:
 * @drawable: a #GdkDrawable. The created GC must always be used
 *   with drawables of the same depth as this one.
 *
 * Create a new graphics context with default values. 
 *
 * Returns: the new graphics context.
 **/
Elliot Lee's avatar
Elliot Lee committed
103
GdkGC*
104
gdk_gc_new (GdkDrawable *drawable)
Elliot Lee's avatar
Elliot Lee committed
105
{
106
  g_return_val_if_fail (drawable != NULL, NULL);
107

108
  return gdk_gc_new_with_values (drawable, NULL, 0);
Elliot Lee's avatar
Elliot Lee committed
109 110
}

Matthias Clasen's avatar
Matthias Clasen committed
111 112 113 114 115 116 117 118 119 120 121 122
/**
 * gdk_gc_new_with_values:
 * @drawable: a #GdkDrawable. The created GC must always be used
 *   with drawables of the same depth as this one.
 * @values: a structure containing initial values for the GC.
 * @values_mask: a bit mask indicating which fields in @values
 *   are set.
 * 
 * Create a new GC with the given initial values.
 * 
 * Return value: the new graphics context.
 **/
123 124 125 126
GdkGC*
gdk_gc_new_with_values (GdkDrawable	*drawable,
			GdkGCValues	*values,
			GdkGCValuesMask	 values_mask)
Elliot Lee's avatar
Elliot Lee committed
127
{
128 129
  g_return_val_if_fail (drawable != NULL, NULL);

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  return GDK_DRAWABLE_GET_CLASS (drawable)->create_gc (drawable,
						       values,
						       values_mask);
}

/**
 * _gdk_gc_init:
 * @gc: a #GdkGC
 * @drawable: a #GdkDrawable.
 * @values: a structure containing initial values for the GC.
 * @values_mask: a bit mask indicating which fields in @values
 *   are set.
 * 
 * Does initialization of the generic portions of a #GdkGC
 * created with the specified values and values_mask. This
 * should be called out of the implementation of
 * GdkDrawable.create_gc() immediately after creating the
 * #GdkGC object.
 **/
void
_gdk_gc_init (GdkGC           *gc,
	      GdkDrawable     *drawable,
	      GdkGCValues     *values,
	      GdkGCValuesMask  values_mask)
{
  GdkGCPrivate *priv;

  g_return_if_fail (GDK_IS_GC (gc));

  priv = GDK_GC_GET_PRIVATE (gc);
160

161
  if (values_mask & GDK_GC_CLIP_X_ORIGIN)
162
    gc->clip_x_origin = values->clip_x_origin;
163
  if (values_mask & GDK_GC_CLIP_Y_ORIGIN)
164
    gc->clip_y_origin = values->clip_y_origin;
165 166
  if ((values_mask & GDK_GC_CLIP_MASK) && values->clip_mask)
    priv->clip_mask = g_object_ref (values->clip_mask);
167
  if (values_mask & GDK_GC_TS_X_ORIGIN)
168
    gc->ts_x_origin = values->ts_x_origin;
169
  if (values_mask & GDK_GC_TS_Y_ORIGIN)
170
    gc->ts_y_origin = values->ts_y_origin;
171 172 173
  if (values_mask & GDK_GC_FILL)
    priv->fill = values->fill;
  if (values_mask & GDK_GC_STIPPLE)
174
    {
175 176 177
      priv->stipple = values->stipple;
      if (priv->stipple)
	g_object_ref (priv->stipple);
178
    }
179 180 181 182 183 184 185 186 187 188
  if (values_mask & GDK_GC_TILE)
    {
      priv->tile = values->tile;
      if (priv->tile)
	g_object_ref (priv->tile);
    }
  if (values_mask & GDK_GC_FOREGROUND)
    priv->fg_pixel = values->foreground.pixel;
  if (values_mask & GDK_GC_BACKGROUND)
    priv->bg_pixel = values->background.pixel;
189 190
  if (values_mask & GDK_GC_SUBWINDOW)
    priv->subwindow_mode = values->subwindow_mode;
Alexander Larsson's avatar
Alexander Larsson committed
191 192 193 194
  if (values_mask & GDK_GC_EXPOSURES)
    priv->exposures = values->graphics_exposures;
  else
    priv->exposures = TRUE;
195 196 197 198

  gc->colormap = gdk_drawable_get_colormap (drawable);
  if (gc->colormap)
    g_object_ref (gc->colormap);
199
}
Elliot Lee's avatar
Elliot Lee committed
200

201 202 203 204
static void
gdk_gc_finalize (GObject *object)
{
  GdkGC *gc = GDK_GC (object);
205
  GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
206

207
  if (priv->clip_region)
Benjamin Otte's avatar
Benjamin Otte committed
208
    cairo_region_destroy (priv->clip_region);
209
  if (priv->old_clip_region)
Benjamin Otte's avatar
Benjamin Otte committed
210
    cairo_region_destroy (priv->old_clip_region);
211 212 213 214
  if (priv->clip_mask)
    g_object_unref (priv->clip_mask);
  if (priv->old_clip_mask)
    g_object_unref (priv->old_clip_mask);
215
  if (gc->colormap)
216
    g_object_unref (gc->colormap);
217 218 219 220
  if (priv->tile)
    g_object_unref (priv->tile);
  if (priv->stipple)
    g_object_unref (priv->stipple);
221

222
  G_OBJECT_CLASS (gdk_gc_parent_class)->finalize (object);
223 224
}

Matthias Clasen's avatar
Matthias Clasen committed
225 226 227 228 229 230 231 232 233 234
/**
 * gdk_gc_get_values:
 * @gc:  a #GdkGC.
 * @values: the #GdkGCValues structure in which to store the results.
 * 
 * Retrieves the current values from a graphics context. Note that 
 * only the pixel values of the @values->foreground and @values->background
 * are filled, use gdk_colormap_query_color() to obtain the rgb values
 * if you need them.
 **/
Elliot Lee's avatar
Elliot Lee committed
235 236 237 238
void
gdk_gc_get_values (GdkGC       *gc,
		   GdkGCValues *values)
{
239
  g_return_if_fail (GDK_IS_GC (gc));
240
  g_return_if_fail (values != NULL);
Elliot Lee's avatar
Elliot Lee committed
241

242
  GDK_GC_GET_CLASS (gc)->get_values (gc, values);
243 244
}

Havoc Pennington's avatar
Havoc Pennington committed
245 246 247 248 249 250 251 252 253 254 255 256 257
/**
 * gdk_gc_set_values:
 * @gc: a #GdkGC
 * @values: struct containing the new values
 * @values_mask: mask indicating which struct fields are to be used
 *
 * Sets attributes of a graphics context in bulk. For each flag set in
 * @values_mask, the corresponding field will be read from @values and
 * set as the new value for @gc. If you're only setting a few values
 * on @gc, calling individual "setter" functions is likely more
 * convenient.
 * 
 **/
258 259 260 261 262
void
gdk_gc_set_values (GdkGC           *gc,
		   GdkGCValues	   *values,
		   GdkGCValuesMask  values_mask)
{
263 264
  GdkGCPrivate *priv;

265
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
266 267
  g_return_if_fail (values != NULL);

268 269
  priv = GDK_GC_GET_PRIVATE (gc);

270 271 272 273 274 275
  if ((values_mask & GDK_GC_CLIP_X_ORIGIN) ||
      (values_mask & GDK_GC_CLIP_Y_ORIGIN) ||
      (values_mask & GDK_GC_CLIP_MASK) ||
      (values_mask & GDK_GC_SUBWINDOW))
    _gdk_gc_remove_drawable_clip (gc);
  
276
  if (values_mask & GDK_GC_CLIP_X_ORIGIN)
277
    gc->clip_x_origin = values->clip_x_origin;
278
  if (values_mask & GDK_GC_CLIP_Y_ORIGIN)
279
    gc->clip_y_origin = values->clip_y_origin;
280
  if (values_mask & GDK_GC_TS_X_ORIGIN)
281
    gc->ts_x_origin = values->ts_x_origin;
282
  if (values_mask & GDK_GC_TS_Y_ORIGIN)
283
    gc->ts_y_origin = values->ts_y_origin;
284 285
  if (values_mask & GDK_GC_CLIP_MASK)
    {
286 287 288 289 290 291 292 293
      if (priv->clip_mask)
	{
	  g_object_unref (priv->clip_mask);
	  priv->clip_mask = NULL;
	}
      if (values->clip_mask)
	priv->clip_mask = g_object_ref (values->clip_mask);
      
294 295
      if (priv->clip_region)
	{
Benjamin Otte's avatar
Benjamin Otte committed
296
	  cairo_region_destroy (priv->clip_region);
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
	  priv->clip_region = NULL;
	}
    }
  if (values_mask & GDK_GC_FILL)
    priv->fill = values->fill;
  if (values_mask & GDK_GC_STIPPLE)
    {
      if (priv->stipple != values->stipple)
	{
	  if (priv->stipple)
	    g_object_unref (priv->stipple);
	  priv->stipple = values->stipple;
	  if (priv->stipple)
	    g_object_ref (priv->stipple);
	}
    }
  if (values_mask & GDK_GC_TILE)
    {
      if (priv->tile != values->tile)
	{
	  if (priv->tile)
	    g_object_unref (priv->tile);
	  priv->tile = values->tile;
	  if (priv->tile)
	    g_object_ref (priv->tile);
	}
    }
  if (values_mask & GDK_GC_FOREGROUND)
    priv->fg_pixel = values->foreground.pixel;
  if (values_mask & GDK_GC_BACKGROUND)
    priv->bg_pixel = values->background.pixel;
328 329
  if (values_mask & GDK_GC_SUBWINDOW)
    priv->subwindow_mode = values->subwindow_mode;
Alexander Larsson's avatar
Alexander Larsson committed
330 331
  if (values_mask & GDK_GC_EXPOSURES)
    priv->exposures = values->graphics_exposures;
332
  
333
  GDK_GC_GET_CLASS (gc)->set_values (gc, values, values_mask);
Elliot Lee's avatar
Elliot Lee committed
334 335
}

Matthias Clasen's avatar
Matthias Clasen committed
336 337 338 339 340 341 342 343 344 345
/**
 * gdk_gc_set_foreground:
 * @gc: a #GdkGC.
 * @color: the new foreground color.
 * 
 * Sets the foreground color for a graphics context.
 * Note that this function uses @color->pixel, use 
 * gdk_gc_set_rgb_fg_color() to specify the foreground 
 * color as red, green, blue components.
 **/
Elliot Lee's avatar
Elliot Lee committed
346
void
347 348
gdk_gc_set_foreground (GdkGC	      *gc,
		       const GdkColor *color)
Elliot Lee's avatar
Elliot Lee committed
349
{
350
  GdkGCValues values;
Elliot Lee's avatar
Elliot Lee committed
351

352
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
353 354
  g_return_if_fail (color != NULL);

355 356
  values.foreground = *color;
  gdk_gc_set_values (gc, &values, GDK_GC_FOREGROUND);
Elliot Lee's avatar
Elliot Lee committed
357 358
}

Matthias Clasen's avatar
Matthias Clasen committed
359 360 361 362 363 364 365 366 367 368
/**
 * gdk_gc_set_background:
 * @gc: a #GdkGC.
 * @color: the new background color.
 * 
 * Sets the background color for a graphics context.
 * Note that this function uses @color->pixel, use 
 * gdk_gc_set_rgb_bg_color() to specify the background 
 * color as red, green, blue components.
 **/
Elliot Lee's avatar
Elliot Lee committed
369
void
370 371
gdk_gc_set_background (GdkGC	      *gc,
		       const GdkColor *color)
Elliot Lee's avatar
Elliot Lee committed
372
{
373
  GdkGCValues values;
Elliot Lee's avatar
Elliot Lee committed
374

375
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
376 377
  g_return_if_fail (color != NULL);

378 379
  values.background = *color;
  gdk_gc_set_values (gc, &values, GDK_GC_BACKGROUND);
Elliot Lee's avatar
Elliot Lee committed
380 381
}

Matthias Clasen's avatar
Matthias Clasen committed
382 383 384 385 386 387 388 389 390
/**
 * gdk_gc_set_function:
 * @gc: a #GdkGC.
 * @function: the #GdkFunction to use
 * 
 * Determines how the current pixel values and the
 * pixel values being drawn are combined to produce
 * the final pixel values.
 **/
Elliot Lee's avatar
Elliot Lee committed
391
void
392
gdk_gc_set_function (GdkGC	 *gc,
Elliot Lee's avatar
Elliot Lee committed
393 394
		     GdkFunction  function)
{
395
  GdkGCValues values;
Elliot Lee's avatar
Elliot Lee committed
396

397
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
398

399 400
  values.function = function;
  gdk_gc_set_values (gc, &values, GDK_GC_FUNCTION);
Elliot Lee's avatar
Elliot Lee committed
401 402
}

Matthias Clasen's avatar
Matthias Clasen committed
403 404 405 406 407 408 409
/**
 * gdk_gc_set_fill:
 * @gc: a #GdkGC.
 * @fill: the new fill mode.
 * 
 * Set the fill mode for a graphics context.
 **/
Elliot Lee's avatar
Elliot Lee committed
410
void
411
gdk_gc_set_fill (GdkGC	 *gc,
Elliot Lee's avatar
Elliot Lee committed
412 413
		 GdkFill  fill)
{
414
  GdkGCValues values;
Elliot Lee's avatar
Elliot Lee committed
415

416
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
417

418 419
  values.fill = fill;
  gdk_gc_set_values (gc, &values, GDK_GC_FILL);
Elliot Lee's avatar
Elliot Lee committed
420 421
}

Matthias Clasen's avatar
Matthias Clasen committed
422 423 424 425 426 427 428 429 430
/**
 * gdk_gc_set_tile:
 * @gc:  a #GdkGC.
 * @tile:  the new tile pixmap.
 * 
 * Set a tile pixmap for a graphics context.
 * This will only be used if the fill mode
 * is %GDK_TILED.
 **/
Elliot Lee's avatar
Elliot Lee committed
431
void
432
gdk_gc_set_tile (GdkGC	   *gc,
Elliot Lee's avatar
Elliot Lee committed
433 434
		 GdkPixmap *tile)
{
435
  GdkGCValues values;
Elliot Lee's avatar
Elliot Lee committed
436

437
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
438

439 440
  values.tile = tile;
  gdk_gc_set_values (gc, &values, GDK_GC_TILE);
Elliot Lee's avatar
Elliot Lee committed
441 442
}

Matthias Clasen's avatar
Matthias Clasen committed
443 444 445 446 447 448 449 450 451
/**
 * gdk_gc_set_stipple:
 * @gc: a #GdkGC.
 * @stipple: the new stipple bitmap.
 * 
 * Set the stipple bitmap for a graphics context. The
 * stipple will only be used if the fill mode is
 * %GDK_STIPPLED or %GDK_OPAQUE_STIPPLED.
 **/
Elliot Lee's avatar
Elliot Lee committed
452 453 454 455
void
gdk_gc_set_stipple (GdkGC     *gc,
		    GdkPixmap *stipple)
{
456
  GdkGCValues values;
Elliot Lee's avatar
Elliot Lee committed
457

458
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
459

460 461
  values.stipple = stipple;
  gdk_gc_set_values (gc, &values, GDK_GC_STIPPLE);
Elliot Lee's avatar
Elliot Lee committed
462 463
}

Matthias Clasen's avatar
Matthias Clasen committed
464 465 466 467 468 469 470 471 472 473 474
/**
 * gdk_gc_set_ts_origin:
 * @gc:  a #GdkGC.
 * @x: the x-coordinate of the origin.
 * @y: the y-coordinate of the origin.
 * 
 * Set the origin when using tiles or stipples with
 * the GC. The tile or stipple will be aligned such
 * that the upper left corner of the tile or stipple
 * will coincide with this point.
 **/
Elliot Lee's avatar
Elliot Lee committed
475 476 477 478 479
void
gdk_gc_set_ts_origin (GdkGC *gc,
		      gint   x,
		      gint   y)
{
480
  GdkGCValues values;
Elliot Lee's avatar
Elliot Lee committed
481

482
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
483

484
  values.ts_x_origin = x;
Tor Lillqvist's avatar
Tor Lillqvist committed
485
  values.ts_y_origin = y;
486 487 488
  
  gdk_gc_set_values (gc, &values,
		     GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN);
Elliot Lee's avatar
Elliot Lee committed
489 490
}

Matthias Clasen's avatar
Matthias Clasen committed
491 492 493 494 495 496 497 498 499 500
/**
 * gdk_gc_set_clip_origin:
 * @gc: a #GdkGC.
 * @x: the x-coordinate of the origin.
 * @y: the y-coordinate of the origin.
 * 
 * Sets the origin of the clip mask. The coordinates are
 * interpreted relative to the upper-left corner of
 * the destination drawable of the current operation.
 **/
Elliot Lee's avatar
Elliot Lee committed
501 502 503 504 505
void
gdk_gc_set_clip_origin (GdkGC *gc,
			gint   x,
			gint   y)
{
506
  GdkGCValues values;
Elliot Lee's avatar
Elliot Lee committed
507

508
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
509

510
  values.clip_x_origin = x;
Tor Lillqvist's avatar
Tor Lillqvist committed
511
  values.clip_y_origin = y;
512 513 514
  
  gdk_gc_set_values (gc, &values,
		     GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
Elliot Lee's avatar
Elliot Lee committed
515 516
}

Matthias Clasen's avatar
Matthias Clasen committed
517 518 519 520 521 522 523 524 525
/**
 * gdk_gc_set_clip_mask:
 * @gc: the #GdkGC.
 * @mask: a bitmap.
 * 
 * Sets the clip mask for a graphics context from a bitmap.
 * The clip mask is interpreted relative to the clip
 * origin. (See gdk_gc_set_clip_origin()).
 **/
Elliot Lee's avatar
Elliot Lee committed
526
void
527
gdk_gc_set_clip_mask (GdkGC	*gc,
Elliot Lee's avatar
Elliot Lee committed
528 529
		      GdkBitmap *mask)
{
530
  GdkGCValues values;
531
  
532
  g_return_if_fail (GDK_IS_GC (gc));
533
  
534 535
  values.clip_mask = mask;
  gdk_gc_set_values (gc, &values, GDK_GC_CLIP_MASK);
Elliot Lee's avatar
Elliot Lee committed
536 537
}

538
/* Takes ownership of passed in region */
539 540 541 542
static void
_gdk_gc_set_clip_region_real (GdkGC     *gc,
			      GdkRegion *region,
			      gboolean reset_origin)
543 544 545
{
  GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);

546 547 548 549 550 551
  if (priv->clip_mask)
    {
      g_object_unref (priv->clip_mask);
      priv->clip_mask = NULL;
    }
  
552
  if (priv->clip_region)
Benjamin Otte's avatar
Benjamin Otte committed
553
    cairo_region_destroy (priv->clip_region);
554 555 556

  priv->clip_region = region;

557 558 559
  _gdk_windowing_gc_set_clip_region (gc, region, reset_origin);
}

560 561 562 563 564 565 566 567 568 569 570
/* Doesn't copy region, allows not to reset origin */
void
_gdk_gc_set_clip_region_internal (GdkGC     *gc,
				  GdkRegion *region,
				  gboolean reset_origin)
{
  _gdk_gc_remove_drawable_clip (gc);
  _gdk_gc_set_clip_region_real (gc, region, reset_origin);
}


571
void
572 573 574 575 576
_gdk_gc_add_drawable_clip (GdkGC     *gc,
			   guint32    region_tag,
			   GdkRegion *region,
			   int        offset_x,
			   int        offset_y)
577 578 579
{
  GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);

580 581 582 583
  if (priv->region_tag_applied == region_tag &&
      offset_x == priv->region_tag_offset_x &&
      offset_y == priv->region_tag_offset_y)
    return; /* Already appied this drawable region */
584
  
585 586 587
  if (priv->region_tag_applied)
    _gdk_gc_remove_drawable_clip (gc);

Benjamin Otte's avatar
Benjamin Otte committed
588
  region = cairo_region_copy (region);
589
  if (offset_x != 0 || offset_y != 0)
Benjamin Otte's avatar
Benjamin Otte committed
590
    cairo_region_translate (region, offset_x, offset_y);
591

592 593 594 595 596 597
  if (priv->clip_mask)
    {
      int w, h;
      GdkPixmap *new_mask;
      GdkGC *tmp_gc;
      GdkColor black = {0, 0, 0, 0};
598
      GdkRectangle r;
Benjamin Otte's avatar
Benjamin Otte committed
599
      cairo_region_overlap_t overlap;
600

601 602 603 604 605 606 607 608 609 610
      gdk_drawable_get_size (priv->clip_mask, &w, &h);

      r.x = 0;
      r.y = 0;
      r.width = w;
      r.height = h;

      /* Its quite common to expose areas that are completely in or outside
       * the region, so we try to avoid allocating bitmaps that are just fully
       * set or completely unset.
611
       */
Benjamin Otte's avatar
Benjamin Otte committed
612 613
      overlap = cairo_region_contains_rectangle (region, &r);
      if (overlap == CAIRO_REGION_OVERLAP_PART)
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
	{
	   /* The region and the mask intersect, create a new clip mask that
	      includes both areas */
	  priv->old_clip_mask = g_object_ref (priv->clip_mask);
	  new_mask = gdk_pixmap_new (priv->old_clip_mask, w, h, -1);
	  tmp_gc = _gdk_drawable_get_scratch_gc ((GdkDrawable *)new_mask, FALSE);

	  gdk_gc_set_foreground (tmp_gc, &black);
	  gdk_draw_rectangle (new_mask, tmp_gc, TRUE, 0, 0, -1, -1);
	  _gdk_gc_set_clip_region_internal (tmp_gc, region, TRUE); /* Takes ownership of region */
	  gdk_draw_drawable  (new_mask,
			      tmp_gc,
			      priv->old_clip_mask,
			      0, 0,
			      0, 0,
			      -1, -1);
	  gdk_gc_set_clip_region (tmp_gc, NULL);
	  gdk_gc_set_clip_mask (gc, new_mask);
Matthias Clasen's avatar
Matthias Clasen committed
632
	  g_object_unref (new_mask);
633
	}
Benjamin Otte's avatar
Benjamin Otte committed
634
      else if (overlap == CAIRO_REGION_OVERLAP_OUT)
635
	{
636
	  /* No intersection, set empty clip region */
Benjamin Otte's avatar
Benjamin Otte committed
637
	  GdkRegion *empty = cairo_region_create ();
638

Benjamin Otte's avatar
Benjamin Otte committed
639
	  cairo_region_destroy (region);
640
	  priv->old_clip_mask = g_object_ref (priv->clip_mask);
641
	  priv->clip_region = empty;
642
	  _gdk_windowing_gc_set_clip_region (gc, empty, FALSE);
643 644 645 646
	}
      else
	{
	  /* Completely inside region, don't set unnecessary clip */
Benjamin Otte's avatar
Benjamin Otte committed
647
	  cairo_region_destroy (region);
648
	  return;
649
	}
650 651 652 653 654 655
    }
  else
    {
      priv->old_clip_region = priv->clip_region;
      priv->clip_region = region;
      if (priv->old_clip_region)
Benjamin Otte's avatar
Benjamin Otte committed
656
	cairo_region_intersect (region, priv->old_clip_region);
657

658 659 660 661 662 663
      _gdk_windowing_gc_set_clip_region (gc, priv->clip_region, FALSE);
    }

  priv->region_tag_applied = region_tag;
  priv->region_tag_offset_x = offset_x;
  priv->region_tag_offset_y = offset_y;
664 665 666 667 668 669 670 671 672 673
}

void
_gdk_gc_remove_drawable_clip (GdkGC *gc)
{
  GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);

  if (priv->region_tag_applied)
    {
      priv->region_tag_applied = 0;
674 675 676 677 678
      if (priv->old_clip_mask)
	{
	  gdk_gc_set_clip_mask (gc, priv->old_clip_mask);
	  g_object_unref (priv->old_clip_mask);
	  priv->old_clip_mask = NULL;
679 680 681 682 683 684

	  if (priv->clip_region)
	    {
	      g_object_unref (priv->clip_region);
	      priv->clip_region = NULL;
	    }
685 686 687 688 689 690
	}
      else
	{
	  _gdk_gc_set_clip_region_real (gc, priv->old_clip_region, FALSE);
	  priv->old_clip_region = NULL;
	}
691
    }
692 693 694 695 696 697 698 699 700 701 702 703
}

/**
 * gdk_gc_set_clip_rectangle:
 * @gc: a #GdkGC.
 * @rectangle: the rectangle to clip to.
 * 
 * Sets the clip mask for a graphics context from a
 * rectangle. The clip mask is interpreted relative to the clip
 * origin. (See gdk_gc_set_clip_origin()).
 **/
void
704 705
gdk_gc_set_clip_rectangle (GdkGC              *gc,
			   const GdkRectangle *rectangle)
706 707 708 709 710
{
  GdkRegion *region;
  
  g_return_if_fail (GDK_IS_GC (gc));

711 712
  _gdk_gc_remove_drawable_clip (gc);
  
713
  if (rectangle)
Benjamin Otte's avatar
Benjamin Otte committed
714
    region = cairo_region_create_rectangle (rectangle);
715 716 717
  else
    region = NULL;

718
  _gdk_gc_set_clip_region_real (gc, region, TRUE);
719 720 721 722 723 724 725 726 727 728 729 730
}

/**
 * gdk_gc_set_clip_region:
 * @gc: a #GdkGC.
 * @region: the #GdkRegion. 
 * 
 * Sets the clip mask for a graphics context from a region structure.
 * The clip mask is interpreted relative to the clip origin. (See
 * gdk_gc_set_clip_origin()).
 **/
void
731 732
gdk_gc_set_clip_region (GdkGC           *gc,
			const GdkRegion *region)
733
{
734 735
  GdkRegion *copy;

736 737
  g_return_if_fail (GDK_IS_GC (gc));

738 739
  _gdk_gc_remove_drawable_clip (gc);
  
740
  if (region)
Benjamin Otte's avatar
Benjamin Otte committed
741
    copy = cairo_region_copy (region);
742 743 744
  else
    copy = NULL;

745
  _gdk_gc_set_clip_region_real (gc, copy, TRUE);
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
}

/**
 * _gdk_gc_get_clip_region:
 * @gc: a #GdkGC
 * 
 * Gets the current clip region for @gc, if any.
 * 
 * Return value: the clip region for the GC, or %NULL.
 *   (if a clip mask is set, the return will be %NULL)
 *   This value is owned by the GC and must not be freed.
 **/
GdkRegion *
_gdk_gc_get_clip_region (GdkGC *gc)
{
  g_return_val_if_fail (GDK_IS_GC (gc), NULL);

  return GDK_GC_GET_PRIVATE (gc)->clip_region;
}

Alexander Larsson's avatar
Alexander Larsson committed
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
/**
 * _gdk_gc_get_clip_mask:
 * @gc: a #GdkGC
 *
 * Gets the current clip mask for @gc, if any.
 *
 * Return value: the clip mask for the GC, or %NULL.
 *   (if a clip region is set, the return will be %NULL)
 *   This value is owned by the GC and must not be freed.
 **/
GdkBitmap *
_gdk_gc_get_clip_mask (GdkGC *gc)
{
  g_return_val_if_fail (GDK_IS_GC (gc), NULL);

  return GDK_GC_GET_PRIVATE (gc)->clip_mask;
}

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
/**
 * _gdk_gc_get_fill:
 * @gc: a #GdkGC
 * 
 * Gets the current file style for the GC
 * 
 * Return value: the file style for the GC
 **/
GdkFill
_gdk_gc_get_fill (GdkGC *gc)
{
  g_return_val_if_fail (GDK_IS_GC (gc), GDK_SOLID);

  return GDK_GC_GET_PRIVATE (gc)->fill;
}

Alexander Larsson's avatar
Alexander Larsson committed
800 801 802 803 804 805 806 807
gboolean
_gdk_gc_get_exposures (GdkGC *gc)
{
  g_return_val_if_fail (GDK_IS_GC (gc), FALSE);

  return GDK_GC_GET_PRIVATE (gc)->exposures;
}

808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
/**
 * _gdk_gc_get_tile:
 * @gc: a #GdkGC
 * 
 * Gets the tile pixmap for @gc, if any
 * 
 * Return value: the tile set on the GC, or %NULL. The
 *   value is owned by the GC and must not be freed.
 **/
GdkPixmap *
_gdk_gc_get_tile (GdkGC *gc)
{
  g_return_val_if_fail (GDK_IS_GC (gc), NULL);

  return GDK_GC_GET_PRIVATE (gc)->tile;
}

/**
 * _gdk_gc_get_stipple:
 * @gc: a #GdkGC
 * 
 * Gets the stipple pixmap for @gc, if any
 * 
 * Return value: the stipple set on the GC, or %NULL. The
 *   value is owned by the GC and must not be freed.
 **/
GdkBitmap *
_gdk_gc_get_stipple (GdkGC *gc)
{
  g_return_val_if_fail (GDK_IS_GC (gc), NULL);

  return GDK_GC_GET_PRIVATE (gc)->stipple;
}

/**
 * _gdk_gc_get_fg_pixel:
 * @gc: a #GdkGC
 * 
 * Gets the foreground pixel value for @gc. If the
 * foreground pixel has never been set, returns the
 * default value 0.
 * 
 * Return value: the foreground pixel value of the GC
 **/
guint32
_gdk_gc_get_fg_pixel (GdkGC *gc)
{
  g_return_val_if_fail (GDK_IS_GC (gc), 0);
  
  return GDK_GC_GET_PRIVATE (gc)->fg_pixel;
}

/**
 * _gdk_gc_get_bg_pixel:
 * @gc: a #GdkGC
 * 
 * Gets the background pixel value for @gc.If the
 * foreground pixel has never been set, returns the
 * default value 1.
 * 
 * Return value: the foreground pixel value of the GC
 **/
guint32
_gdk_gc_get_bg_pixel (GdkGC *gc)
{
  g_return_val_if_fail (GDK_IS_GC (gc), 0);
  
  return GDK_GC_GET_PRIVATE (gc)->bg_pixel;
}
Elliot Lee's avatar
Elliot Lee committed
877

Matthias Clasen's avatar
Matthias Clasen committed
878 879 880 881 882 883 884 885
/**
 * gdk_gc_set_subwindow:
 * @gc: a #GdkGC.
 * @mode: the subwindow mode.
 * 
 * Sets how drawing with this GC on a window will affect child
 * windows of that window. 
 **/
886 887 888
void
gdk_gc_set_subwindow (GdkGC	       *gc,
		      GdkSubwindowMode	mode)
Elliot Lee's avatar
Elliot Lee committed
889
{
890
  GdkGCValues values;
891
  GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
Elliot Lee's avatar
Elliot Lee committed
892

893
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
894

895 896 897 898 899
  /* This could get called a lot to reset the subwindow mode in
     the client side clipping, so bail out early */ 
  if (priv->subwindow_mode == mode)
    return;
  
900 901
  values.subwindow_mode = mode;
  gdk_gc_set_values (gc, &values, GDK_GC_SUBWINDOW);
Elliot Lee's avatar
Elliot Lee committed
902 903
}

904 905 906 907 908 909 910 911
GdkSubwindowMode
_gdk_gc_get_subwindow (GdkGC *gc)
{
  GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);

  return priv->subwindow_mode;
}

Matthias Clasen's avatar
Matthias Clasen committed
912 913 914 915 916 917 918 919 920 921
/**
 * gdk_gc_set_exposures:
 * @gc: a #GdkGC.
 * @exposures: if %TRUE, exposure events will be generated.
 * 
 * Sets whether copying non-visible portions of a drawable
 * using this graphics context generate exposure events
 * for the corresponding regions of the destination
 * drawable. (See gdk_draw_drawable()).
 **/
Elliot Lee's avatar
Elliot Lee committed
922
void
923 924
gdk_gc_set_exposures (GdkGC     *gc,
		      gboolean   exposures)
Elliot Lee's avatar
Elliot Lee committed
925
{
926
  GdkGCValues values;
Elliot Lee's avatar
Elliot Lee committed
927

928
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
929

930 931
  values.graphics_exposures = exposures;
  gdk_gc_set_values (gc, &values, GDK_GC_EXPOSURES);
Elliot Lee's avatar
Elliot Lee committed
932 933
}

Matthias Clasen's avatar
Matthias Clasen committed
934 935 936 937 938 939 940 941 942 943 944 945
/**
 * gdk_gc_set_line_attributes:
 * @gc: a #GdkGC.
 * @line_width: the width of lines.
 * @line_style: the dash-style for lines.
 * @cap_style: the manner in which the ends of lines are drawn.
 * @join_style: the in which lines are joined together.
 * 
 * Sets various attributes of how lines are drawn. See
 * the corresponding members of #GdkGCValues for full
 * explanations of the arguments.
 **/
Elliot Lee's avatar
Elliot Lee committed
946
void
947 948
gdk_gc_set_line_attributes (GdkGC	*gc,
			    gint	 line_width,
Elliot Lee's avatar
Elliot Lee committed
949
			    GdkLineStyle line_style,
950
			    GdkCapStyle	 cap_style,
Elliot Lee's avatar
Elliot Lee committed
951 952
			    GdkJoinStyle join_style)
{
953 954 955 956 957 958 959 960 961 962 963 964
  GdkGCValues values;

  values.line_width = line_width;
  values.line_style = line_style;
  values.cap_style = cap_style;
  values.join_style = join_style;

  gdk_gc_set_values (gc, &values,
		     GDK_GC_LINE_WIDTH |
		     GDK_GC_LINE_STYLE |
		     GDK_GC_CAP_STYLE |
		     GDK_GC_JOIN_STYLE);
Elliot Lee's avatar
Elliot Lee committed
965
}
966

Matthias Clasen's avatar
Matthias Clasen committed
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
/**
 * gdk_gc_set_dashes:
 * @gc: a #GdkGC.
 * @dash_offset: the phase of the dash pattern.
 * @dash_list: an array of dash lengths.
 * @n: the number of elements in @dash_list.
 * 
 * Sets the way dashed-lines are drawn. Lines will be
 * drawn with alternating on and off segments of the
 * lengths specified in @dash_list. The manner in
 * which the on and off segments are drawn is determined
 * by the @line_style value of the GC. (This can
 * be changed with gdk_gc_set_line_attributes().)
 *
 * The @dash_offset defines the phase of the pattern, 
 * specifying how many pixels into the dash-list the pattern 
 * should actually begin.
 **/
985 986 987
void
gdk_gc_set_dashes (GdkGC *gc,
		   gint	  dash_offset,
Owen Taylor's avatar
Owen Taylor committed
988
		   gint8  dash_list[],
989 990
		   gint   n)
{
991
  g_return_if_fail (GDK_IS_GC (gc));
992 993
  g_return_if_fail (dash_list != NULL);

994
  GDK_GC_GET_CLASS (gc)->set_dashes (gc, dash_offset, dash_list, n);
995
}
996

997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
/**
 * gdk_gc_offset:
 * @gc: a #GdkGC
 * @x_offset: amount by which to offset the GC in the X direction
 * @y_offset: amount by which to offset the GC in the Y direction
 * 
 * Offset attributes such as the clip and tile-stipple origins
 * of the GC so that drawing at x - x_offset, y - y_offset with
 * the offset GC  has the same effect as drawing at x, y with the original
 * GC.
 **/
void
gdk_gc_offset (GdkGC *gc,
	       gint   x_offset,
	       gint   y_offset)
{
  if (x_offset != 0 || y_offset != 0)
    {
      GdkGCValues values;

      values.clip_x_origin = gc->clip_x_origin - x_offset;
      values.clip_y_origin = gc->clip_y_origin - y_offset;
      values.ts_x_origin = gc->ts_x_origin - x_offset;
      values.ts_y_origin = gc->ts_y_origin - y_offset;
      
      gdk_gc_set_values (gc, &values,
			 GDK_GC_CLIP_X_ORIGIN |
			 GDK_GC_CLIP_Y_ORIGIN |
			 GDK_GC_TS_X_ORIGIN |
			 GDK_GC_TS_Y_ORIGIN);
    }
}

1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
/**
 * gdk_gc_copy:
 * @dst_gc: the destination graphics context.
 * @src_gc: the source graphics context.
 * 
 * Copy the set of values from one graphics context
 * onto another graphics context.
 **/
void
gdk_gc_copy (GdkGC *dst_gc,
	     GdkGC *src_gc)
{
  GdkGCPrivate *dst_priv, *src_priv;
  
  g_return_if_fail (GDK_IS_GC (dst_gc));
  g_return_if_fail (GDK_IS_GC (src_gc));

  dst_priv = GDK_GC_GET_PRIVATE (dst_gc);
  src_priv = GDK_GC_GET_PRIVATE (src_gc);

  _gdk_windowing_gc_copy (dst_gc, src_gc);

  dst_gc->clip_x_origin = src_gc->clip_x_origin;
  dst_gc->clip_y_origin = src_gc->clip_y_origin;
  dst_gc->ts_x_origin = src_gc->ts_x_origin;
  dst_gc->ts_y_origin = src_gc->ts_y_origin;

  if (src_gc->colormap)
    g_object_ref (src_gc->colormap);

  if (dst_gc->colormap)
    g_object_unref (dst_gc->colormap);

  dst_gc->colormap = src_gc->colormap;

  if (dst_priv->clip_region)
Benjamin Otte's avatar
Benjamin Otte committed
1066
    cairo_region_destroy (dst_priv->clip_region);
1067 1068

  if (src_priv->clip_region)
Benjamin Otte's avatar
Benjamin Otte committed
1069
    dst_priv->clip_region = cairo_region_copy (src_priv->clip_region);
1070 1071
  else
    dst_priv->clip_region = NULL;
1072 1073 1074 1075

  dst_priv->region_tag_applied = src_priv->region_tag_applied;
  
  if (dst_priv->old_clip_region)
Benjamin Otte's avatar
Benjamin Otte committed
1076
    cairo_region_destroy (dst_priv->old_clip_region);
1077 1078

  if (src_priv->old_clip_region)
Benjamin Otte's avatar
Benjamin Otte committed
1079
    dst_priv->old_clip_region = cairo_region_copy (src_priv->old_clip_region);
1080 1081
  else
    dst_priv->old_clip_region = NULL;
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091

  if (src_priv->clip_mask)
    dst_priv->clip_mask = g_object_ref (src_priv->clip_mask);
  else
    dst_priv->clip_mask = NULL;
  
  if (src_priv->old_clip_mask)
    dst_priv->old_clip_mask = g_object_ref (src_priv->old_clip_mask);
  else
    dst_priv->old_clip_mask = NULL;
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
  
  dst_priv->fill = src_priv->fill;
  
  if (dst_priv->stipple)
    g_object_unref (dst_priv->stipple);
  dst_priv->stipple = src_priv->stipple;
  if (dst_priv->stipple)
    g_object_ref (dst_priv->stipple);
  
  if (dst_priv->tile)
    g_object_unref (dst_priv->tile);
  dst_priv->tile = src_priv->tile;
  if (dst_priv->tile)
    g_object_ref (dst_priv->tile);

  dst_priv->fg_pixel = src_priv->fg_pixel;
  dst_priv->bg_pixel = src_priv->bg_pixel;
1109
  dst_priv->subwindow_mode = src_priv->subwindow_mode;
Alexander Larsson's avatar
Alexander Larsson committed
1110
  dst_priv->exposures = src_priv->exposures;
1111 1112
}

1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
/**
 * gdk_gc_set_colormap:
 * @gc: a #GdkGC
 * @colormap: a #GdkColormap
 * 
 * Sets the colormap for the GC to the given colormap. The depth
 * of the colormap's visual must match the depth of the drawable
 * for which the GC was created.
 **/
void
gdk_gc_set_colormap (GdkGC       *gc,
		     GdkColormap *colormap)
{
  g_return_if_fail (GDK_IS_GC (gc));
  g_return_if_fail (GDK_IS_COLORMAP (colormap));

  if (gc->colormap != colormap)
    {
      if (gc->colormap)
1132
	g_object_unref (gc->colormap);
1133 1134

      gc->colormap = colormap;
1135
      g_object_ref (gc->colormap);