gtkgc.c 9.21 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
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 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/. 
 */

27
#include "config.h"
Elliot Lee's avatar
Elliot Lee committed
28
#include "gtkgc.h"
29
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56


typedef struct _GtkGCKey       GtkGCKey;
typedef struct _GtkGCDrawable  GtkGCDrawable;

struct _GtkGCKey
{
  gint depth;
  GdkColormap *colormap;
  GdkGCValues values;
  GdkGCValuesMask mask;
};

struct _GtkGCDrawable
{
  gint depth;
  GdkPixmap *drawable;
};


static void      gtk_gc_init             (void);
static GtkGCKey* gtk_gc_key_dup          (GtkGCKey      *key);
static void      gtk_gc_key_destroy      (GtkGCKey      *key);
static gpointer  gtk_gc_new              (gpointer       key);
static void      gtk_gc_destroy          (gpointer       value);
static guint     gtk_gc_key_hash         (gpointer       key);
static guint     gtk_gc_value_hash       (gpointer       value);
57
static gint      gtk_gc_key_equal        (gpointer       a,
Elliot Lee's avatar
Elliot Lee committed
58 59
					  gpointer       b);
static guint     gtk_gc_drawable_hash    (GtkGCDrawable *d);
60
static gint      gtk_gc_drawable_equal   (GtkGCDrawable *a,
Elliot Lee's avatar
Elliot Lee committed
61 62 63 64 65
					  GtkGCDrawable *b);


static gint initialize = TRUE;
static GCache *gc_cache = NULL;
66
static GQuark quark_gtk_gc_drawable_ht = 0;
Elliot Lee's avatar
Elliot Lee committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

GdkGC*
gtk_gc_get (gint             depth,
	    GdkColormap     *colormap,
	    GdkGCValues     *values,
	    GdkGCValuesMask  values_mask)
{
  GtkGCKey key;
  GdkGC *gc;

  if (initialize)
    gtk_gc_init ();

  key.depth = depth;
  key.colormap = colormap;
  key.values = *values;
  key.mask = values_mask;

  gc = g_cache_insert (gc_cache, &key);

  return gc;
}

void
gtk_gc_release (GdkGC *gc)
{
  if (initialize)
    gtk_gc_init ();

  g_cache_remove (gc_cache, gc);
}

99 100 101 102
static void 
free_gc_drawable (gpointer data)
{
  GtkGCDrawable *drawable = data;
Manish Singh's avatar
Manish Singh committed
103
  g_object_unref (drawable->drawable);
104
  g_slice_free (GtkGCDrawable, drawable);
105 106 107 108 109
}

static GHashTable*
gtk_gc_get_drawable_ht (GdkScreen *screen)
{
110
  GHashTable *ht = g_object_get_qdata (G_OBJECT (screen), quark_gtk_gc_drawable_ht);
111 112 113 114 115
  if (!ht)
    {
      ht = g_hash_table_new_full ((GHashFunc) gtk_gc_drawable_hash,
				  (GEqualFunc) gtk_gc_drawable_equal,
				  NULL, free_gc_drawable);
116 117 118
      g_object_set_qdata_full (G_OBJECT (screen), 
			       quark_gtk_gc_drawable_ht, ht, 
			       (GDestroyNotify)g_hash_table_destroy);
119 120 121 122
    }
  
  return ht;
}
Elliot Lee's avatar
Elliot Lee committed
123 124

static void
125
gtk_gc_init (void)
Elliot Lee's avatar
Elliot Lee committed
126 127 128
{
  initialize = FALSE;

129 130
  quark_gtk_gc_drawable_ht = g_quark_from_static_string ("gtk-gc-drawable-ht");

Elliot Lee's avatar
Elliot Lee committed
131 132 133 134 135 136
  gc_cache = g_cache_new ((GCacheNewFunc) gtk_gc_new,
			  (GCacheDestroyFunc) gtk_gc_destroy,
			  (GCacheDupFunc) gtk_gc_key_dup,
			  (GCacheDestroyFunc) gtk_gc_key_destroy,
			  (GHashFunc) gtk_gc_key_hash,
			  (GHashFunc) gtk_gc_value_hash,
137
			  (GEqualFunc) gtk_gc_key_equal);
Elliot Lee's avatar
Elliot Lee committed
138 139 140 141 142 143 144
}

static GtkGCKey*
gtk_gc_key_dup (GtkGCKey *key)
{
  GtkGCKey *new_key;

145
  new_key = g_slice_new (GtkGCKey);
Elliot Lee's avatar
Elliot Lee committed
146 147 148 149 150 151 152 153 154

  *new_key = *key;

  return new_key;
}

static void
gtk_gc_key_destroy (GtkGCKey *key)
{
155
  g_slice_free (GtkGCKey, key);
Elliot Lee's avatar
Elliot Lee committed
156 157 158 159 160 161 162 163
}

static gpointer
gtk_gc_new (gpointer key)
{
  GtkGCKey *keyval;
  GtkGCDrawable *drawable;
  GdkGC *gc;
164
  GHashTable *ht;
165
  GdkScreen *screen;
Elliot Lee's avatar
Elliot Lee committed
166 167

  keyval = key;
168 169 170
  screen = gdk_colormap_get_screen (keyval->colormap);
  
  ht = gtk_gc_get_drawable_ht (screen);
171
  drawable = g_hash_table_lookup (ht, &keyval->depth);
Elliot Lee's avatar
Elliot Lee committed
172 173
  if (!drawable)
    {
174
      drawable = g_slice_new (GtkGCDrawable);
Elliot Lee's avatar
Elliot Lee committed
175
      drawable->depth = keyval->depth;
176
      drawable->drawable = gdk_pixmap_new (gdk_screen_get_root_window (screen), 
177 178
					   1, 1, drawable->depth);
      g_hash_table_insert (ht, &drawable->depth, drawable);
Elliot Lee's avatar
Elliot Lee committed
179 180 181
    }

  gc = gdk_gc_new_with_values (drawable->drawable, &keyval->values, keyval->mask);
182
  gdk_gc_set_colormap (gc, keyval->colormap);
Elliot Lee's avatar
Elliot Lee committed
183 184 185 186 187 188 189

  return (gpointer) gc;
}

static void
gtk_gc_destroy (gpointer value)
{
Manish Singh's avatar
Manish Singh committed
190
  g_object_unref (value);
Elliot Lee's avatar
Elliot Lee committed
191 192 193 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
}

static guint
gtk_gc_key_hash (gpointer key)
{
  GtkGCKey *keyval;
  guint hash_val;

  keyval = key;
  hash_val = 0;

  if (keyval->mask & GDK_GC_FOREGROUND)
    {
      hash_val += keyval->values.foreground.pixel;
    }
  if (keyval->mask & GDK_GC_BACKGROUND)
    {
      hash_val += keyval->values.background.pixel;
    }
  if (keyval->mask & GDK_GC_FUNCTION)
    {
      hash_val += (gint) keyval->values.function;
    }
  if (keyval->mask & GDK_GC_FILL)
    {
      hash_val += (gint) keyval->values.fill;
    }
  if (keyval->mask & GDK_GC_TILE)
    {
220
      hash_val += (gintptr) keyval->values.tile;
Elliot Lee's avatar
Elliot Lee committed
221 222 223
    }
  if (keyval->mask & GDK_GC_STIPPLE)
    {
224
      hash_val += (gintptr) keyval->values.stipple;
Elliot Lee's avatar
Elliot Lee committed
225 226 227
    }
  if (keyval->mask & GDK_GC_CLIP_MASK)
    {
228
      hash_val += (gintptr) keyval->values.clip_mask;
Elliot Lee's avatar
Elliot Lee committed
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 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    }
  if (keyval->mask & GDK_GC_SUBWINDOW)
    {
      hash_val += (gint) keyval->values.subwindow_mode;
    }
  if (keyval->mask & GDK_GC_TS_X_ORIGIN)
    {
      hash_val += (gint) keyval->values.ts_x_origin;
    }
  if (keyval->mask & GDK_GC_TS_Y_ORIGIN)
    {
      hash_val += (gint) keyval->values.ts_y_origin;
    }
  if (keyval->mask & GDK_GC_CLIP_X_ORIGIN)
    {
      hash_val += (gint) keyval->values.clip_x_origin;
    }
  if (keyval->mask & GDK_GC_CLIP_Y_ORIGIN)
    {
      hash_val += (gint) keyval->values.clip_y_origin;
    }
  if (keyval->mask & GDK_GC_EXPOSURES)
    {
      hash_val += (gint) keyval->values.graphics_exposures;
    }
  if (keyval->mask & GDK_GC_LINE_WIDTH)
    {
      hash_val += (gint) keyval->values.line_width;
    }
  if (keyval->mask & GDK_GC_LINE_STYLE)
    {
      hash_val += (gint) keyval->values.line_style;
    }
  if (keyval->mask & GDK_GC_CAP_STYLE)
    {
      hash_val += (gint) keyval->values.cap_style;
    }
  if (keyval->mask & GDK_GC_JOIN_STYLE)
    {
      hash_val += (gint) keyval->values.join_style;
    }

  return hash_val;
}

static guint
gtk_gc_value_hash (gpointer value)
{
  return (gulong) value;
}

280 281 282
static gboolean
gtk_gc_key_equal (gpointer a,
		  gpointer b)
Elliot Lee's avatar
Elliot Lee committed
283 284 285 286 287 288 289 290 291 292 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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
{
  GtkGCKey *akey;
  GtkGCKey *bkey;
  GdkGCValues *avalues;
  GdkGCValues *bvalues;

  akey = a;
  bkey = b;

  avalues = &akey->values;
  bvalues = &bkey->values;

  if (akey->mask != bkey->mask)
    return FALSE;

  if (akey->depth != bkey->depth)
    return FALSE;

  if (akey->colormap != bkey->colormap)
    return FALSE;

  if (akey->mask & GDK_GC_FOREGROUND)
    {
      if (avalues->foreground.pixel != bvalues->foreground.pixel)
	return FALSE;
    }
  if (akey->mask & GDK_GC_BACKGROUND)
    {
      if (avalues->background.pixel != bvalues->background.pixel)
	return FALSE;
    }
  if (akey->mask & GDK_GC_FUNCTION)
    {
      if (avalues->function != bvalues->function)
	return FALSE;
    }
  if (akey->mask & GDK_GC_FILL)
    {
      if (avalues->fill != bvalues->fill)
	return FALSE;
    }
  if (akey->mask & GDK_GC_TILE)
    {
      if (avalues->tile != bvalues->tile)
	return FALSE;
    }
  if (akey->mask & GDK_GC_STIPPLE)
    {
      if (avalues->stipple != bvalues->stipple)
	return FALSE;
    }
  if (akey->mask & GDK_GC_CLIP_MASK)
    {
      if (avalues->clip_mask != bvalues->clip_mask)
	return FALSE;
    }
  if (akey->mask & GDK_GC_SUBWINDOW)
    {
      if (avalues->subwindow_mode != bvalues->subwindow_mode)
	return FALSE;
    }
  if (akey->mask & GDK_GC_TS_X_ORIGIN)
    {
      if (avalues->ts_x_origin != bvalues->ts_x_origin)
	return FALSE;
    }
  if (akey->mask & GDK_GC_TS_Y_ORIGIN)
    {
      if (avalues->ts_y_origin != bvalues->ts_y_origin)
	return FALSE;
    }
  if (akey->mask & GDK_GC_CLIP_X_ORIGIN)
    {
      if (avalues->clip_x_origin != bvalues->clip_x_origin)
	return FALSE;
    }
  if (akey->mask & GDK_GC_CLIP_Y_ORIGIN)
    {
      if (avalues->clip_y_origin != bvalues->clip_y_origin)
	return FALSE;
    }
  if (akey->mask & GDK_GC_EXPOSURES)
    {
      if (avalues->graphics_exposures != bvalues->graphics_exposures)
	return FALSE;
    }
  if (akey->mask & GDK_GC_LINE_WIDTH)
    {
      if (avalues->line_width != bvalues->line_width)
	return FALSE;
    }
  if (akey->mask & GDK_GC_LINE_STYLE)
    {
      if (avalues->line_style != bvalues->line_style)
	return FALSE;
    }
  if (akey->mask & GDK_GC_CAP_STYLE)
    {
      if (avalues->cap_style != bvalues->cap_style)
	return FALSE;
    }
  if (akey->mask & GDK_GC_JOIN_STYLE)
    {
      if (avalues->join_style != bvalues->join_style)
	return FALSE;
    }

  return TRUE;
}

static guint
gtk_gc_drawable_hash (GtkGCDrawable *d)
{
  return d->depth;
}

399 400 401
static gboolean
gtk_gc_drawable_equal (GtkGCDrawable *a,
		       GtkGCDrawable *b)
Elliot Lee's avatar
Elliot Lee committed
402 403 404
{
  return (a->depth == b->depth);
}