GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnucash-color.c
1 /********************************************************************\
2  * gnucash-color.c -- color handling for table cells *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License as *
6  * published by the Free Software Foundation; either version 2 of *
7  * the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License*
15  * along with this program; if not, contact: *
16  * *
17  * Free Software Foundation Voice: +1-617-542-5942 *
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19  * Boston, MA 02110-1301, USA [email protected] *
20  * *
21 \********************************************************************/
22 
23 /*
24  * Shamelessly stolen from Gnumeric and modified
25  *
26  * Heath Martin <[email protected]>
27  *
28  * color.c: Color allocation on the Gnumeric spreadsheet
29  *
30  * Author:
31  * Miguel de Icaza ([email protected])
32  *
33  * We keep our own color context, as the color allocation might take place
34  * before any of our Canvases are realized.
35  */
36 
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40 
41 #include <gtk/gtk.h>
42 #include <gdk/gdk.h>
43 #include "gnucash-color.h"
44 
45 static int color_inited;
46 
47 /* Public Colors */
48 GdkColor gn_white, gn_black, gn_light_gray;
49 GdkColor gn_dark_gray, gn_blue, gn_red, gn_yellow;
50 
51 static GHashTable *color_hash_table = NULL;
52 
53 static guint
54 color_hash (gconstpointer v)
55 {
56  const guint32 *c = (guint32 *) v;
57 
58  return *c;
59 }
60 
61 
62 static gint
63 color_equal (gconstpointer v, gconstpointer w)
64 {
65  const guint32 *c1 = (guint32 *) v;
66  const guint32 *c2 = (guint32 *) w;
67 
68  return (*c1 == *c2);
69 }
70 
71 
72 gulong
73 gnucash_color_alloc (gushort red, gushort green, gushort blue)
74 {
75  GdkColormap *colormap = gtk_widget_get_default_colormap ();
76  GdkColor *c;
77 
78  if (!color_inited)
79  gnucash_color_init ();
80 
81  c = g_new0 (GdkColor, 1);
82  c->red = red;
83  c->green = green;
84  c->blue = blue;
85 
86  g_return_val_if_fail (gdk_colormap_alloc_color (colormap, c, FALSE, TRUE), 0);
87 
88  return c->pixel;
89 }
90 
91 
92 void
93 gnucash_color_alloc_gdk (GdkColor *c)
94 {
95  GdkColormap *colormap = gtk_widget_get_default_colormap ();
96 
97  g_return_if_fail (c != NULL);
98 
99  g_assert (gdk_colormap_alloc_color (colormap, c,
100  FALSE, TRUE));
101 }
102 
103 
104 void
105 gnucash_color_alloc_name (const char *name, GdkColor *c)
106 {
107  GdkColormap *colormap = gtk_widget_get_default_colormap ();
108 
109  g_return_if_fail (name != NULL);
110  g_return_if_fail (c != NULL);
111 
112  gdk_color_parse (name, c);
113  c->pixel = 0;
114  g_assert (gdk_colormap_alloc_color (colormap, c,
115  FALSE, TRUE));
116 }
117 
118 
119 /* This function takes an argb spec for a color and returns an
120  * allocated GdkColor. We take care of allocating and managing
121  * the colors. Caller must not touch the returned color.
122  */
123 GdkColor *
124 gnucash_color_argb_to_gdk (guint32 argb)
125 {
126  GdkColor *color;
127  const guint32 key = argb;
128  guint32 *newkey;
129 
130  color = g_hash_table_lookup (color_hash_table, &key);
131 
132  if (color)
133  return color;
134 
135  color = g_new0(GdkColor, 1);
136  newkey = g_new0(guint32, 1);
137 
138  *newkey = key;
139 
140  color->red = (argb & 0xff0000) >> 8;
141  color->green = argb & 0xff00;
142  color->blue = (argb & 0xff) << 8;
143 
144  gnucash_color_alloc_gdk(color);
145 
146  g_hash_table_insert (color_hash_table, newkey, color);
147 
148  return color;
149 }
150 
151 
152 void
153 gnucash_color_init (void)
154 {
155  /* Allocate the default colors */
156  gnucash_color_alloc_name ("white", &gn_white);
157  gnucash_color_alloc_name ("black", &gn_black);
158 
159  gnucash_color_alloc_name ("gray60", &gn_light_gray);
160  gnucash_color_alloc_name ("gray40", &gn_dark_gray);
161  gnucash_color_alloc_name ("blue", &gn_blue);
162  gnucash_color_alloc_name ("red", &gn_red);
163  gnucash_color_alloc_name ("yellow", &gn_yellow);
164 
165  if (!color_hash_table)
166  color_hash_table = g_hash_table_new (color_hash, color_equal);
167 
168  color_inited = 1;
169 }
170 
171