File: bonobo-ui-pixmap-cache.c

package info (click to toggle)
bonobo 1.0.22-2.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,412 kB
  • ctags: 5,445
  • sloc: ansic: 51,714; sh: 7,733; makefile: 1,425; yacc: 318; xml: 266; sed: 16
file content (91 lines) | stat: -rw-r--r-- 2,566 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
57
58
59
60
61
62
63
64
65
66
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
/* bonobo-ui-pixmap-cache.h: Pixmap cache for the Bonobo UI engine
 *
 * Copyright (C) 2001 Ximian, Inc.
 *
 * Author: Federico Mena-Quintero <federico@ximian.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "bonobo-ui-pixmap-cache.h"



/* Pair of pixmap/mask from a rendered pixbuf */
typedef struct {
	GdkPixmap *pixmap;
	GdkBitmap *mask;
} PixmapMask;

/* Mapping of pixbuf pointers to PixmapMask pairs */
static GHashTable *pixbuf_pixmap_hash = NULL;



/* Ensures that the pixbuf->pixmap hash table has been created */
static void
ensure_hash_table (void)
{
	if (pixbuf_pixmap_hash)
		return;

	pixbuf_pixmap_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
}

/**
 * bonobo_ui_pixmap_cache_get:
 * @source: Source pixbuf.
 * @pixmap_ret: Return value for the rendered pixmap.
 * @mask_ret: Return value for the opacity mask.
 * 
 * Gets a rendered pixmap/mask pair from a pixbuf.  The pixmap and mask are
 * cached based on the @source pixbuf pointer so that no duplicated rendering is
 * performed.  You should unref the returned pixmap and mask with
 * gdk_pixmap_unref() and gdk_bitmap_unref(), respectively, when you are done
 * with them.
 **/
void
bonobo_ui_pixmap_cache_get (GdkPixbuf *source, GdkPixmap **pixmap_ret, GdkBitmap **mask_ret)
{
	PixmapMask *pm;

	g_return_if_fail (source != NULL);
	g_return_if_fail (pixmap_ret != NULL);
	g_return_if_fail (mask_ret != NULL);

	ensure_hash_table ();

	pm = g_hash_table_lookup (pixbuf_pixmap_hash, source);

	if (!pm) {
		pm = g_new (PixmapMask, 1);
		gdk_pixbuf_render_pixmap_and_mask (source, &pm->pixmap, &pm->mask, 128);
		g_hash_table_insert (pixbuf_pixmap_hash, source, pm);
	}

	if (pm->pixmap)
		gdk_pixmap_ref (pm->pixmap);

	if (pm->mask)
		gdk_bitmap_ref (pm->mask);

	*pixmap_ret = pm->pixmap;
	*mask_ret = pm->mask;
}