3 * Copyright (C) 2008 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
7 * gLabels is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * gLabels is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with gLabels. If not, see <http://www.gnu.org/licenses/>.
23 #include "color-combo-menu.h"
25 #include <glib/gi18n.h>
28 #include "color-combo-color-menu-item.h"
29 #include "color-combo-history.h"
34 /*===========================================*/
35 /* Private macros and constants. */
36 /*===========================================*/
38 #define PALETTE_COLS 7
39 #define PALETTE_ROWS (G_N_ELEMENTS(color_table)/PALETTE_COLS + (G_N_ELEMENTS(color_table)%PALETTE_COLS ? 1 : 0))
41 #define ROW_DEFAULT (0)
42 #define ROW_SEP_1 (ROW_DEFAULT + 1)
43 #define ROW_PALETTE (ROW_SEP_1 + 1)
44 #define ROW_SEP_2 (ROW_PALETTE + PALETTE_ROWS)
45 #define ROW_HISTORY (ROW_SEP_2 + 1)
46 #define ROW_SEP_3 (ROW_HISTORY + 1)
47 #define ROW_CUSTOM (ROW_SEP_3 + 1)
49 /*===========================================*/
51 /*===========================================*/
53 struct _glColorComboMenuPrivate {
55 GtkWidget *default_menu_item;
56 GtkWidget *custom_menu_item;
58 GtkWidget *history_menu_item[PALETTE_COLS];
74 /*===========================================*/
76 /*===========================================*/
78 static guint signals[LAST_SIGNAL] = {0};
80 static ColorTableEntry color_table[] =
83 { GL_COLOR (139, 0, 0), N_("Dark Red") },
84 { GL_COLOR (165, 42, 42), N_("Brown") },
85 { GL_COLOR (205, 149, 12), N_("Dark Goldenrod") },
86 { GL_COLOR ( 0, 100, 0), N_("Dark Green") },
87 { GL_COLOR ( 0, 139, 139), N_("Dark Cyan") },
88 { GL_COLOR ( 0, 0, 128), N_("Navy Blue") },
89 { GL_COLOR (148, 0, 211), N_("Dark Violet") },
91 { GL_COLOR (255, 0, 0), N_("Red") },
92 { GL_COLOR (255, 165, 0), N_("Orange") },
93 { GL_COLOR (205, 205, 0), N_("Dark Yellow") },
94 { GL_COLOR ( 0, 205, 0), N_("Medium green") },
95 { GL_COLOR ( 64, 224, 208), N_("Turquoise") },
96 { GL_COLOR ( 0, 0, 255), N_("Blue") },
97 { GL_COLOR (160, 32, 240), N_("Purple") },
99 { GL_COLOR (250, 128, 114), N_("Salmon") },
100 { GL_COLOR (255, 215, 0), N_("Gold") },
101 { GL_COLOR (255, 255, 0), N_("Yellow") },
102 { GL_COLOR ( 0, 255, 0), N_("Green") },
103 { GL_COLOR ( 0, 255, 255), N_("Cyan") },
104 { GL_COLOR (135, 206, 235), N_("SkyBlue") },
105 { GL_COLOR (238, 130, 238), N_("Violet") },
107 { GL_COLOR (255, 192, 203), N_("Pink") },
108 { GL_COLOR (255, 246, 143), N_("Khaki") },
109 { GL_COLOR (255, 255, 224), N_("Light Yellow") },
110 { GL_COLOR (144, 238, 144), N_("Light Green") },
111 { GL_COLOR (224, 255, 255), N_("Light Cyan") },
112 { GL_COLOR (198, 226, 255), N_("Slate Gray") },
113 { GL_COLOR (216, 191, 216), N_("Thistle") },
115 { GL_COLOR (255, 255, 255), N_("White") },
116 { GL_COLOR (230, 230, 230), N_("10% Gray") },
117 { GL_COLOR (192, 192, 192), N_("25% Gray") },
118 { GL_COLOR (153, 153, 153), N_("40% Gray") },
119 { GL_COLOR (128, 128, 128), N_("50% Gray") },
120 { GL_COLOR (102, 102, 102), N_("60% Gray") },
121 { GL_COLOR ( 0, 0, 0), N_("Black") },
125 static glColorComboHistory *custom_color_history = NULL;
128 /*===========================================*/
129 /* Local function prototypes */
130 /*===========================================*/
132 static void gl_color_combo_menu_finalize (GObject *object);
134 static void load_custom_color_history (glColorComboMenu *this);
136 static gboolean map_event_cb (GtkWidget *widget,
139 static void default_menu_item_activate_cb (glColorComboMenu *this);
140 static void custom_menu_item_activate_cb (glColorComboMenu *this);
142 static void palette_menu_item_activate_cb (GtkMenuItem *item,
143 glColorComboMenu *this);
145 static void history_menu_item_activate_cb (GtkMenuItem *item,
146 glColorComboMenu *this);
149 /****************************************************************************/
150 /* Boilerplate Object stuff. */
151 /****************************************************************************/
152 G_DEFINE_TYPE (glColorComboMenu, gl_color_combo_menu, GTK_TYPE_MENU);
155 /*****************************************************************************/
156 /* Class Init Function. */
157 /*****************************************************************************/
159 gl_color_combo_menu_class_init (glColorComboMenuClass *class)
161 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
162 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
164 gl_color_combo_menu_parent_class = g_type_class_peek_parent (class);
166 gobject_class->finalize = gl_color_combo_menu_finalize;
168 widget_class->map_event = map_event_cb;
170 signals[COLOR_CHANGED] =
171 g_signal_new ("color_changed",
172 G_OBJECT_CLASS_TYPE (gobject_class),
174 G_STRUCT_OFFSET (glColorComboMenuClass, color_changed),
176 gl_marshal_VOID__UINT_BOOLEAN,
178 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
182 /*****************************************************************************/
183 /* Object Instance Init Function. */
184 /*****************************************************************************/
186 gl_color_combo_menu_init (glColorComboMenu *this)
188 GtkWidget *separator_menu_item;
189 gint i, i_row, i_col;
190 GtkWidget *palette_menu_item;
194 * Initialize history, if needed.
196 if ( !custom_color_history )
198 custom_color_history = gl_color_combo_history_new (PALETTE_COLS);
202 this->priv = g_new0 (glColorComboMenuPrivate, 1);
204 this->priv->default_menu_item = gtk_menu_item_new_with_label ("");
205 gtk_menu_attach (GTK_MENU (this), this->priv->default_menu_item,
206 0, PALETTE_COLS, ROW_DEFAULT, ROW_DEFAULT+1);
208 separator_menu_item = gtk_separator_menu_item_new ();
209 gtk_menu_attach (GTK_MENU (this), separator_menu_item,
210 0, PALETTE_COLS, ROW_SEP_1, ROW_SEP_1+1);
212 for ( i=0; i < G_N_ELEMENTS(color_table); i++ )
215 i_row = i / PALETTE_COLS;
216 i_col = i % PALETTE_COLS;
218 palette_menu_item = gl_color_combo_color_menu_item_new (i,
219 color_table[i].color,
220 color_table[i].name);
221 g_signal_connect (palette_menu_item, "activate",
222 G_CALLBACK (palette_menu_item_activate_cb), this);
224 gtk_menu_attach (GTK_MENU (this), palette_menu_item,
226 ROW_PALETTE+i_row, ROW_PALETTE+i_row+1);
229 separator_menu_item = gtk_separator_menu_item_new ();
230 gtk_menu_attach (GTK_MENU (this), separator_menu_item,
231 0, PALETTE_COLS, ROW_SEP_2, ROW_SEP_2+1);
233 for ( i=0; i < PALETTE_COLS; i++ )
235 this->priv->history_menu_item[i] =
236 gl_color_combo_color_menu_item_new (i, 0, NULL);
237 gtk_widget_set_sensitive (this->priv->history_menu_item[i], FALSE);
238 g_signal_connect (this->priv->history_menu_item[i], "activate",
239 G_CALLBACK (history_menu_item_activate_cb), this);
240 gtk_menu_attach (GTK_MENU (this), this->priv->history_menu_item[i],
242 ROW_HISTORY, ROW_HISTORY+1);
245 separator_menu_item = gtk_separator_menu_item_new ();
246 gtk_menu_attach (GTK_MENU (this), separator_menu_item,
247 0, PALETTE_COLS, ROW_SEP_3, ROW_SEP_3+1);
249 this->priv->custom_menu_item = gtk_menu_item_new_with_label (_("Custom Color"));
250 gtk_menu_attach (GTK_MENU (this), this->priv->custom_menu_item,
251 0, PALETTE_COLS, ROW_CUSTOM, ROW_CUSTOM+1);
253 g_signal_connect_swapped (this->priv->default_menu_item, "activate",
254 G_CALLBACK (default_menu_item_activate_cb), this);
255 g_signal_connect_swapped (this->priv->custom_menu_item, "activate",
256 G_CALLBACK (custom_menu_item_activate_cb), this);
261 /*****************************************************************************/
262 /* Finalize Method. */
263 /*****************************************************************************/
265 gl_color_combo_menu_finalize (GObject *object)
267 glColorComboMenu *this = GL_COLOR_COMBO_MENU (object);
269 g_return_if_fail (object != NULL);
270 g_return_if_fail (GL_IS_COLOR_COMBO_MENU (object));
274 G_OBJECT_CLASS (gl_color_combo_menu_parent_class)->finalize (object);
278 /*****************************************************************************/
279 /** New Object Generator. */
280 /*****************************************************************************/
282 gl_color_combo_menu_new (const gchar *default_label,
285 glColorComboMenu *this;
287 this = g_object_new (gl_color_combo_menu_get_type (), NULL);
289 gtk_label_set_text ( GTK_LABEL (gtk_bin_get_child (GTK_BIN (this->priv->default_menu_item))),
292 this->priv->color = color;
294 return GTK_WIDGET (this);
298 /*****************************************************************************/
299 /* Load menu with custom color history. */
300 /*****************************************************************************/
302 load_custom_color_history (glColorComboMenu *this)
308 for ( i=0; i < PALETTE_COLS; i++ )
310 color = gl_color_combo_history_get_color (custom_color_history, i);
314 tip = g_strdup_printf (_("Custom Color #%u"), i+1);
316 gl_color_combo_color_menu_item_set_color (GL_COLOR_COMBO_COLOR_MENU_ITEM (this->priv->history_menu_item[i]),
321 gtk_widget_set_sensitive (this->priv->history_menu_item[i], TRUE);
327 /*****************************************************************************/
328 /* Map event callback. */
329 /*****************************************************************************/
331 map_event_cb (GtkWidget *widget,
334 glColorComboMenu *this = GL_COLOR_COMBO_MENU (widget);
336 load_custom_color_history (this);
342 /*****************************************************************************/
343 /* "Default" menu_item activate callback. */
344 /*****************************************************************************/
346 default_menu_item_activate_cb (glColorComboMenu *this)
348 g_signal_emit (this, signals[COLOR_CHANGED], 0,
351 gtk_widget_hide (GTK_WIDGET (this));
355 /*****************************************************************************/
356 /* "Custom color" menu_item activate callback. */
357 /*****************************************************************************/
359 custom_menu_item_activate_cb (glColorComboMenu *this)
361 GtkWidget *custom_dialog;
366 gtk_widget_hide (GTK_WIDGET (this));
368 custom_dialog = gtk_color_selection_dialog_new (_("Custom Color"));
370 colorsel = GTK_COLOR_SELECTION_DIALOG (custom_dialog)->colorsel;
371 color.red = GL_COLOR_F_RED (this->priv->color) * 65535;
372 color.green = GL_COLOR_F_GREEN (this->priv->color) * 65535;
373 color.blue = GL_COLOR_F_BLUE (this->priv->color) * 65535;
375 gtk_color_selection_set_current_color (GTK_COLOR_SELECTION (colorsel),
378 response = gtk_dialog_run (GTK_DIALOG (custom_dialog));
382 case GTK_RESPONSE_OK:
383 gtk_color_selection_get_current_color (GTK_COLOR_SELECTION (colorsel),
385 this->priv->color = GL_COLOR ((color.red >>8),
389 gl_color_combo_history_add_color (custom_color_history,
392 g_signal_emit (this, signals[COLOR_CHANGED], 0,
393 this->priv->color, FALSE);
395 gtk_widget_destroy (custom_dialog);
398 gtk_widget_destroy (custom_dialog);
404 /*****************************************************************************/
405 /* Palette color changed callback. */
406 /*****************************************************************************/
408 palette_menu_item_activate_cb (GtkMenuItem *item,
409 glColorComboMenu *this)
413 i = gl_color_combo_color_menu_item_get_id (GL_COLOR_COMBO_COLOR_MENU_ITEM (item));
415 this->priv->color = color_table[i].color;
417 g_signal_emit (this, signals[COLOR_CHANGED], 0,
418 this->priv->color, FALSE);
420 gtk_widget_hide (GTK_WIDGET (this));
423 /*****************************************************************************/
424 /* History color menu item callback. */
425 /*****************************************************************************/
427 history_menu_item_activate_cb (GtkMenuItem *item,
428 glColorComboMenu *this)
432 i = gl_color_combo_color_menu_item_get_id (GL_COLOR_COMBO_COLOR_MENU_ITEM (item));
434 this->priv->color = gl_color_combo_history_get_color (custom_color_history,
437 g_signal_emit (this, signals[COLOR_CHANGED], 0,
438 this->priv->color, FALSE);
440 gtk_widget_hide (GTK_WIDGET (this));
446 * Local Variables: -- emacs
448 * c-basic-offset: 8 -- emacs
449 * tab-width: 8 -- emacs
450 * indent-tabs-mode: nil -- emacs