]> git.sur5r.net Git - glabels/blob - src/mygal/widget-color-combo.c
Imported Upstream version 2.2.8
[glabels] / src / mygal / widget-color-combo.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * widget-color-combo.c - A color selector combo box
4  * Copyright 2000, 2001, Ximian, Inc.
5  *
6  * Authors:
7  *   Miguel de Icaza (miguel@kernel.org)
8  *   Dom Lachowicz (dominicl@seas.upenn.edu)
9  *
10  * Reworked and split up into a separate ColorPalette object:
11  *   Michael Levy (mlevy@genoscope.cns.fr)
12  *
13  * And later revised and polished by:
14  *   Almer S. Tigelaar (almer@gnome.org)
15  *
16  * Modified for gLabels by:
17  *   Jim Evins <evins@snaught.com>
18  *
19  * This library is free software; you can redistribute it and/or
20  * modify it under the terms of the GNU General Public
21  * License, version 2, as published by the Free Software Foundation.
22  *
23  * This library is distributed in the hope that it will be useful, but
24  * WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26  * General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public
29  * License along with this library; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
31  * 02111-1307, USA.
32  */
33
34 #include <config.h>
35
36 #include <gtk/gtkentry.h>
37 #include <gtk/gtksignal.h>
38 #include <gtk/gtkimage.h>
39 #include "e-util.h"
40 #include "e-colors.h"
41 #include "widget-color-combo.h"
42
43 enum {
44         CHANGED,
45         LAST_SIGNAL
46 };
47
48 static guint color_combo_signals [LAST_SIGNAL] = { 0, };
49
50 #define PARENT_TYPE MYGAL_COMBO_BOX_TYPE
51 static GObjectClass *color_combo_parent_class;
52
53 #define make_color(CC,COL) (((COL) != NULL) ? (COL) : ((CC) ? ((CC)->default_color) : NULL))
54 #define RGBA_TO_UINT(r,g,b,a)   ((((guint)(r))<<24)|(((guint)(g))<<16)|(((guint)(b))<<8)|(guint)(a))
55 #define GDK_TO_UINT(c)  RGBA_TO_UINT(((c).red>>8), ((c).green>>8), ((c).blue>>8), 0xff)
56
57 #define PREVIEW_SIZE 20
58
59 static void
60 color_combo_set_color_internal (ColorCombo *cc, GdkColor *color)
61 {
62         guint color_y, color_height;
63         guint height, width;
64         GdkPixbuf *pixbuf;
65         GdkPixbuf *color_pixbuf;
66         GdkColor *new_color;
67         GdkColor *outline_color;
68
69         new_color = make_color (cc,color);
70         /* If the new and the default are NULL draw an outline */
71         outline_color = (new_color) ? new_color : &e_dark_gray;
72
73         pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (cc->preview_image));
74
75         if (!pixbuf)
76                 return;
77
78         width = gdk_pixbuf_get_width (pixbuf);
79         height = gdk_pixbuf_get_height (pixbuf);
80
81         if (cc->preview_is_icon) {
82                 color_y = height - 4;
83                 color_height = 4;
84         }
85         else {
86                 color_y = 0;
87                 color_height = height;
88         }
89
90         color_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
91                                        TRUE, 8,
92                                        width,
93                                        color_height);
94         gdk_pixbuf_fill (color_pixbuf, GDK_TO_UINT (*outline_color));
95         gdk_pixbuf_copy_area (color_pixbuf, 0, 0, width, color_height,
96                               pixbuf, 0, color_y);
97
98         if (new_color != NULL)
99                 gdk_pixbuf_fill (color_pixbuf, GDK_TO_UINT (*new_color));
100         else
101                 gdk_pixbuf_fill (color_pixbuf, 0xffffff00);
102         gdk_pixbuf_copy_area (color_pixbuf, 0, 0, width - 2, color_height -2,
103                               pixbuf, 1, color_y + 1);
104
105         g_object_unref (color_pixbuf);
106
107         gtk_widget_queue_draw (GTK_WIDGET (cc));
108 }
109
110 static void
111 color_combo_class_init (GObjectClass *object_class)
112 {
113         color_combo_parent_class = g_type_class_ref (PARENT_TYPE);
114
115         color_combo_signals [CHANGED] =
116                 g_signal_new ("color_changed",
117                               G_OBJECT_CLASS_TYPE (object_class),
118                               G_SIGNAL_RUN_LAST,
119                               G_STRUCT_OFFSET (ColorComboClass, color_changed),
120                               NULL, NULL,
121                               e_marshal_NONE__POINTER_BOOLEAN_BOOLEAN_BOOLEAN,
122                               G_TYPE_NONE, 4, G_TYPE_POINTER,
123                               G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
124 }
125
126 E_MAKE_TYPE (color_combo,
127              "ColorCombo",
128              ColorCombo,
129              color_combo_class_init,
130              NULL,
131              PARENT_TYPE)
132
133 /*
134  * Fires signal "color_changed" with the current color as its param
135  */
136 static void
137 emit_color_changed (ColorCombo *cc, GdkColor *color,
138                     gboolean is_custom, gboolean by_user, gboolean is_default)
139 {
140         g_signal_emit (cc,
141                        color_combo_signals [CHANGED], 0,
142                        color, is_custom, by_user, is_default);
143         mygal_combo_box_popup_hide (MYGAL_COMBO_BOX (cc));
144 }
145
146 static void
147 cb_palette_color_changed (ColorPalette *P, GdkColor *color,
148                  gboolean custom, gboolean by_user, gboolean is_default,
149                  ColorCombo *cc)
150 {
151         color_combo_set_color_internal (cc, color);
152         emit_color_changed (cc, color, custom, by_user, is_default);
153 }
154
155 static void
156 preview_clicked (GtkWidget *button, ColorCombo *cc)
157 {
158         gboolean is_default;
159         GdkColor *color = color_palette_get_current_color (cc->palette, &is_default);
160         emit_color_changed (cc, color, FALSE, TRUE, is_default);
161         if (color)
162                 gdk_color_free (color);
163 }
164
165 static void
166 cb_cust_color_clicked (GtkWidget *widget, ColorCombo *cc)
167 {
168         mygal_combo_box_popup_hide (MYGAL_COMBO_BOX (cc));
169 }
170
171 /*
172  * Creates the color table
173  */
174 static void
175 color_table_setup (ColorCombo *cc,
176                    char const *no_color_label, ColorGroup *color_group)
177 {
178         g_return_if_fail (cc != NULL);
179
180         /* Tell the palette that we will be changing it's custom colors */
181         cc->palette =
182                 COLOR_PALETTE (color_palette_new (no_color_label,
183                                                   cc->default_color,
184                                                   color_group));
185
186         {
187                 GtkWidget *picker = color_palette_get_color_picker (cc->palette);
188                 g_signal_connect (picker, "clicked",
189                                   G_CALLBACK (cb_cust_color_clicked), cc);
190         }
191
192         g_signal_connect (cc->palette, "color_changed",
193                           G_CALLBACK (cb_palette_color_changed), cc);
194
195         gtk_widget_show_all (GTK_WIDGET (cc->palette));
196
197         return;
198 }
199
200 void
201 color_combo_box_set_preview_relief (ColorCombo *cc, GtkReliefStyle relief)
202 {
203         g_return_if_fail (cc != NULL);
204         g_return_if_fail (IS_COLOR_COMBO (cc));
205
206         gtk_button_set_relief (GTK_BUTTON (cc->preview_button), relief);
207 }
208
209 /*
210  * Where the actual construction goes on
211  */
212 static void
213 color_combo_construct (ColorCombo *cc, GdkPixbuf *icon,
214                        char const *no_color_label,
215                        ColorGroup *color_group)
216 {
217         GdkColor *color;
218         GdkPixbuf *pixbuf = NULL;
219
220         g_return_if_fail (cc != NULL);
221         g_return_if_fail (IS_COLOR_COMBO (cc));
222
223         /*
224          * Our button with the gtk_image preview
225          */
226         cc->preview_button = gtk_button_new ();
227         cc->preview_is_icon = FALSE;
228         
229         if (icon)
230                 /* use icon only if size > 4*4 */
231                 if ((gdk_pixbuf_get_width (icon) > 4) && 
232                     (gdk_pixbuf_get_height (icon) > 4))
233                 {
234                         cc->preview_is_icon = TRUE;
235                         pixbuf = gdk_pixbuf_copy (icon);
236                 }
237         
238         if (pixbuf == NULL)
239                 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
240                                          TRUE, 8, 
241                                          PREVIEW_SIZE, 
242                                          PREVIEW_SIZE);
243
244         cc->preview_image = gtk_image_new_from_pixbuf (pixbuf);
245         g_object_unref (pixbuf);
246
247         gtk_button_set_relief (GTK_BUTTON (cc->preview_button), GTK_RELIEF_NONE);
248         gtk_widget_show (cc->preview_image);
249         
250         gtk_container_add (GTK_CONTAINER (cc->preview_button), cc->preview_image);
251         g_signal_connect (cc->preview_button, "clicked",
252                           G_CALLBACK (preview_clicked), cc);
253
254         color_table_setup (cc, no_color_label, color_group);
255
256         gtk_widget_show_all (cc->preview_button);
257
258         mygal_combo_box_construct (MYGAL_COMBO_BOX (cc),
259                                    cc->preview_button,
260                                    GTK_WIDGET (cc->palette));
261
262         mygal_combo_box_set_tearable (MYGAL_COMBO_BOX (cc), FALSE);
263
264         color = color_palette_get_current_color (cc->palette, NULL);
265         color_combo_set_color_internal (cc, color);
266         if (color) gdk_color_free (color);
267 }
268
269 /* color_combo_get_color:
270  *
271  * Return current color, result must be freed with gdk_color_free !
272  */
273 GdkColor *
274 color_combo_get_color (ColorCombo *cc, gboolean *is_default)
275 {
276         return color_palette_get_current_color (cc->palette, is_default);
277 }
278
279 /**
280  * color_combo_set_color
281  * @cc     The combo
282  * @color  The color
283  *
284  * Set the color of the combo to the given color. Causes the color_changed
285  * signal to be emitted.
286  */
287 void
288 color_combo_set_color (ColorCombo *cc, GdkColor *color)
289 {
290         /* This will change the color on the palette than it will invoke
291          * cb_palette_color_changed which will call emit_color_changed and
292          * set_color_internal which will change the color on our preview and
293          * will let the users of the combo know that the current color has
294          * changed
295          */
296         if (color != NULL)
297                 gdk_rgb_find_color (gtk_widget_get_colormap (GTK_WIDGET (cc)), color);
298         color_palette_set_current_color (cc->palette, color);
299 }
300
301 /**
302  * color_combo_set_color_to_default
303  * @cc  The combo
304  *
305  * Set the color of the combo to the default color. Causes the color_changed
306  * signal to be emitted.
307  */
308 void
309 color_combo_set_color_to_default (ColorCombo *cc)
310 {
311         color_palette_set_color_to_default (cc->palette);
312 }
313
314 /**
315  * color_combo_new :
316  * icon : optionally NULL.
317  * , const char *no_color_label,
318  * Default constructor. Pass an optional icon and an optional label for the
319  * no/auto color button.
320  */
321 GtkWidget *
322 color_combo_new (GdkPixbuf *icon, char const *no_color_label,
323                  GdkColor *default_color,
324                  ColorGroup *color_group)
325 {
326         ColorCombo *cc;
327
328         cc = g_object_new (COLOR_COMBO_TYPE, NULL);
329
330         if ( default_color )
331         {
332                 cc->default_color = gdk_color_copy (default_color);
333         }
334
335         color_combo_construct (cc, icon, no_color_label, color_group);
336
337         return GTK_WIDGET (cc);
338 }