1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * widget-color-combo.c - A color selector combo box
4 * Copyright 2000, 2001, Ximian, Inc.
7 * Miguel de Icaza (miguel@kernel.org)
8 * Dom Lachowicz (dominicl@seas.upenn.edu)
10 * Reworked and split up into a separate ColorPalette object:
11 * Michael Levy (mlevy@genoscope.cns.fr)
13 * And later revised and polished by:
14 * Almer S. Tigelaar (almer@gnome.org)
16 * Modified for gLabels by:
17 * Jim Evins <evins@snaught.com>
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.
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.
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
36 #include <gtk/gtkentry.h>
37 #include <gtk/gtksignal.h>
38 #include <gtk/gtkimage.h>
41 #include "widget-color-combo.h"
48 static guint color_combo_signals [LAST_SIGNAL] = { 0, };
50 #define PARENT_TYPE GTK_COMBO_BOX_TYPE
51 static GObjectClass *color_combo_parent_class;
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)
57 #define PREVIEW_SIZE 20
60 color_combo_set_color_internal (ColorCombo *cc, GdkColor *color)
62 guint color_y, color_height;
65 GdkPixbuf *color_pixbuf;
67 GdkColor *outline_color;
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;
73 pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (cc->preview_image));
78 width = gdk_pixbuf_get_width (pixbuf);
79 height = gdk_pixbuf_get_height (pixbuf);
81 if (cc->preview_is_icon) {
87 color_height = height;
90 color_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
94 gdk_pixbuf_fill (color_pixbuf, GDK_TO_UINT (*outline_color));
95 gdk_pixbuf_copy_area (color_pixbuf, 0, 0, width, color_height,
98 if (new_color != NULL)
99 gdk_pixbuf_fill (color_pixbuf, GDK_TO_UINT (*new_color));
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);
105 g_object_unref (color_pixbuf);
107 gtk_widget_queue_draw (GTK_WIDGET (cc));
111 color_combo_class_init (GObjectClass *object_class)
113 color_combo_parent_class = g_type_class_ref (PARENT_TYPE);
115 color_combo_signals [CHANGED] =
116 g_signal_new ("color_changed",
117 G_OBJECT_CLASS_TYPE (object_class),
119 G_STRUCT_OFFSET (ColorComboClass, color_changed),
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);
126 E_MAKE_TYPE (color_combo,
129 color_combo_class_init,
134 * Fires signal "color_changed" with the current color as its param
137 emit_color_changed (ColorCombo *cc, GdkColor *color,
138 gboolean is_custom, gboolean by_user, gboolean is_default)
141 color_combo_signals [CHANGED], 0,
142 color, is_custom, by_user, is_default);
143 gtk_combo_box_popup_hide (GTK_COMBO_BOX (cc));
147 cb_palette_color_changed (ColorPalette *P, GdkColor *color,
148 gboolean custom, gboolean by_user, gboolean is_default,
151 color_combo_set_color_internal (cc, color);
152 emit_color_changed (cc, color, custom, by_user, is_default);
156 preview_clicked (GtkWidget *button, ColorCombo *cc)
159 GdkColor *color = color_palette_get_current_color (cc->palette, &is_default);
160 emit_color_changed (cc, color, FALSE, TRUE, is_default);
162 gdk_color_free (color);
166 cb_cust_color_clicked (GtkWidget *widget, ColorCombo *cc)
168 gtk_combo_box_popup_hide (GTK_COMBO_BOX (cc));
172 * Creates the color table
175 color_table_setup (ColorCombo *cc,
176 char const *no_color_label, ColorGroup *color_group)
178 g_return_if_fail (cc != NULL);
180 /* Tell the palette that we will be changing it's custom colors */
182 COLOR_PALETTE (color_palette_new (no_color_label,
187 GtkWidget *picker = color_palette_get_color_picker (cc->palette);
188 g_signal_connect (picker, "clicked",
189 G_CALLBACK (cb_cust_color_clicked), cc);
192 g_signal_connect (cc->palette, "color_changed",
193 G_CALLBACK (cb_palette_color_changed), cc);
195 gtk_widget_show_all (GTK_WIDGET (cc->palette));
201 color_combo_box_set_preview_relief (ColorCombo *cc, GtkReliefStyle relief)
203 g_return_if_fail (cc != NULL);
204 g_return_if_fail (IS_COLOR_COMBO (cc));
206 gtk_button_set_relief (GTK_BUTTON (cc->preview_button), relief);
210 * Where the actual construction goes on
213 color_combo_construct (ColorCombo *cc, GdkPixbuf *icon,
214 char const *no_color_label,
215 ColorGroup *color_group)
218 GdkPixbuf *pixbuf = NULL;
220 g_return_if_fail (cc != NULL);
221 g_return_if_fail (IS_COLOR_COMBO (cc));
224 * Our button with the gtk_image preview
226 cc->preview_button = gtk_button_new ();
227 cc->preview_is_icon = FALSE;
230 /* use icon only if size > 4*4 */
231 if ((gdk_pixbuf_get_width (icon) > 4) &&
232 (gdk_pixbuf_get_height (icon) > 4))
234 cc->preview_is_icon = TRUE;
235 pixbuf = gdk_pixbuf_copy (icon);
239 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
244 cc->preview_image = gtk_image_new_from_pixbuf (pixbuf);
245 g_object_unref (pixbuf);
247 gtk_button_set_relief (GTK_BUTTON (cc->preview_button), GTK_RELIEF_NONE);
248 gtk_widget_show (cc->preview_image);
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);
254 color_table_setup (cc, no_color_label, color_group);
256 gtk_widget_show_all (cc->preview_button);
258 gtk_combo_box_construct (GTK_COMBO_BOX (cc),
260 GTK_WIDGET (cc->palette));
262 gtk_combo_box_set_tearable (GTK_COMBO_BOX (cc), FALSE);
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);
269 /* color_combo_get_color:
271 * Return current color, result must be freed with gdk_color_free !
274 color_combo_get_color (ColorCombo *cc, gboolean *is_default)
276 return color_palette_get_current_color (cc->palette, is_default);
280 * color_combo_set_color
284 * Set the color of the combo to the given color. Causes the color_changed
285 * signal to be emitted.
288 color_combo_set_color (ColorCombo *cc, GdkColor *color)
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
297 gdk_rgb_find_color (gtk_widget_get_colormap (GTK_WIDGET (cc)), color);
298 color_palette_set_current_color (cc->palette, color);
302 * color_combo_set_color_to_default
305 * Set the color of the combo to the default color. Causes the color_changed
306 * signal to be emitted.
309 color_combo_set_color_to_default (ColorCombo *cc)
311 color_palette_set_color_to_default (cc->palette);
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.
322 color_combo_new (GdkPixbuf *icon, char const *no_color_label,
323 GdkColor *default_color,
324 ColorGroup *color_group)
328 cc = g_object_new (COLOR_COMBO_TYPE, NULL);
330 cc->default_color = default_color;
332 color_combo_construct (cc, icon, no_color_label, color_group);
334 return GTK_WIDGET (cc);