3 * Copyright (C) 2008-2009 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.h"
25 #include "color-combo-menu.h"
26 #include <glib/gi18n.h>
27 #include <gtk/gtkhbox.h>
28 #include "color-swatch.h"
29 #include <gtk/gtkarrow.h>
38 /*========================================================*/
40 /*========================================================*/
42 /** GL_COLOR_COMBO Private fields */
43 struct _glColorComboPrivate {
46 gboolean is_default_flag;
61 /*========================================================*/
62 /* Private globals. */
63 /*========================================================*/
65 static guint signals[LAST_SIGNAL] = {0};
68 /*========================================================*/
69 /* Private function prototypes. */
70 /*========================================================*/
72 static void gl_color_combo_finalize (GObject *object);
75 button_press_event_cb (GtkWidget *widget,
76 GdkEventButton *event,
80 create_pixbuf (glColorCombo *this,
85 menu_color_changed_cb (glColorComboMenu *object,
91 menu_selection_done_cb (GtkMenuShell *object,
95 /*****************************************************************************/
96 /* Object infrastructure. */
97 /*****************************************************************************/
98 G_DEFINE_TYPE (glColorCombo, gl_color_combo, GTK_TYPE_TOGGLE_BUTTON);
101 /*****************************************************************************/
102 /* Class Init Function. */
103 /*****************************************************************************/
105 gl_color_combo_class_init (glColorComboClass *class)
107 GObjectClass *gobject_class = (GObjectClass *) class;
108 GtkWidgetClass *widget_class = (GtkWidgetClass *) class;
109 glColorComboClass *object_class = (glColorComboClass *) class;
111 gl_color_combo_parent_class = g_type_class_peek_parent (class);
113 gobject_class->finalize = gl_color_combo_finalize;
115 signals[COLOR_CHANGED] =
116 g_signal_new ("color_changed",
117 G_OBJECT_CLASS_TYPE (gobject_class),
119 G_STRUCT_OFFSET (glColorComboClass, color_changed),
121 gl_marshal_VOID__UINT_BOOLEAN,
123 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
128 /*****************************************************************************/
129 /* Object Instance Init Function. */
130 /*****************************************************************************/
132 gl_color_combo_init (glColorCombo *this)
137 this->priv = g_new0 (glColorComboPrivate, 1);
139 hbox = gtk_hbox_new (FALSE, 3);
140 gtk_container_add (GTK_CONTAINER (this), hbox);
142 this->priv->swatch = gl_color_swatch_new (IMAGE_W, IMAGE_H, GL_COLOR_NONE);
143 gtk_box_pack_start (GTK_BOX (hbox), this->priv->swatch, FALSE, FALSE, 0);
145 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_IN);
146 gtk_box_pack_end (GTK_BOX (hbox), arrow, FALSE, FALSE, 0);
148 g_signal_connect (this, "button_press_event",
149 G_CALLBACK(button_press_event_cb), this);
153 /*****************************************************************************/
154 /* Finalize Method. */
155 /*****************************************************************************/
157 gl_color_combo_finalize (GObject *object)
161 g_return_if_fail (object && IS_GL_COLOR_COMBO (object));
162 this = GL_COLOR_COMBO (object);
164 g_object_ref_sink (this->priv->menu);
167 G_OBJECT_CLASS (gl_color_combo_parent_class)->finalize (object);
171 /*****************************************************************************/
172 /** New Object Generator. */
173 /*****************************************************************************/
175 gl_color_combo_new (const gchar *default_label,
183 this = g_object_new (TYPE_GL_COLOR_COMBO, NULL);
187 default_label = _("Default Color");
190 this->priv->default_color = default_color;
191 this->priv->color = color;
193 gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch), color);
195 this->priv->menu = gl_color_combo_menu_new (default_label,
197 gtk_widget_show_all (this->priv->menu);
199 g_signal_connect (this->priv->menu, "color_changed",
200 G_CALLBACK (menu_color_changed_cb), this);
201 g_signal_connect (this->priv->menu, "selection_done",
202 G_CALLBACK (menu_selection_done_cb), this);
204 return GTK_WIDGET (this);
208 /*****************************************************************************/
210 /*****************************************************************************/
212 gl_color_combo_set_color (glColorCombo *this,
215 this->priv->color = color;
217 gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch), color);
221 /*****************************************************************************/
222 /* Set to default color. */
223 /*****************************************************************************/
225 gl_color_combo_set_to_default (glColorCombo *this)
227 gl_color_combo_set_color (this, this->priv->default_color);
230 /*****************************************************************************/
232 /*****************************************************************************/
234 gl_color_combo_get_color (glColorCombo *this,
235 gboolean *is_default)
239 *is_default = this->priv->is_default_flag;
242 return this->priv->color;
246 /*****************************************************************************/
247 /* Menu positioning function. */
248 /*****************************************************************************/
250 menu_position_function (GtkMenu *menu,
256 glColorCombo *this = GL_COLOR_COMBO (user_data);
260 gdk_window_get_origin (GTK_WIDGET (this)->window, &x1, &y1);
261 *x = x1 + GTK_WIDGET (this)->allocation.x;
262 *y = y1 + GTK_WIDGET (this)->allocation.y +
263 GTK_WIDGET (this)->allocation.height;
265 menu_h = this->priv->menu->allocation.height;
266 menu_w = this->priv->menu->allocation.width;
268 if ((*y + menu_h) > gdk_screen_height ())
270 *y = y1 + GTK_WIDGET (this)->allocation.y - menu_h;
273 *y = gdk_screen_height () - menu_h;
277 if ((*x + menu_w) > gdk_screen_width ())
279 *x = gdk_screen_width () - menu_w;
286 /*****************************************************************************/
287 /* Button "button_press_event" callback. */
288 /*****************************************************************************/
290 button_press_event_cb (GtkWidget *widget,
291 GdkEventButton *event,
294 switch (event->button)
298 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (this), TRUE);
300 gtk_menu_popup (GTK_MENU (this->priv->menu),
302 menu_position_function, this,
303 event->button, event->time);
315 /*****************************************************************************/
316 /* Menu "color changed" callback. */
317 /*****************************************************************************/
319 menu_color_changed_cb (glColorComboMenu *object,
326 this->priv->color = this->priv->default_color;
330 this->priv->color = color;
332 this->priv->is_default_flag = is_default;
334 gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch),
337 g_signal_emit (this, signals[COLOR_CHANGED], 0,
339 this->priv->is_default_flag);
343 /*****************************************************************************/
344 /* Menu "color changed" callback. */
345 /*****************************************************************************/
347 menu_selection_done_cb (GtkMenuShell *object,
350 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (this), FALSE);
356 * Local Variables: -- emacs
358 * c-basic-offset: 8 -- emacs
359 * tab-width: 8 -- emacs
360 * indent-tabs-mode: nil -- emacs