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 <glib/gi18n.h>
28 #include "color-combo-menu.h"
29 #include "color-swatch.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 menu_color_changed_cb (glColorComboMenu *object,
86 menu_selection_done_cb (GtkMenuShell *object,
90 /*****************************************************************************/
91 /* Object infrastructure. */
92 /*****************************************************************************/
93 G_DEFINE_TYPE (glColorCombo, gl_color_combo, GTK_TYPE_TOGGLE_BUTTON)
96 /*****************************************************************************/
97 /* Class Init Function. */
98 /*****************************************************************************/
100 gl_color_combo_class_init (glColorComboClass *class)
102 GObjectClass *gobject_class = (GObjectClass *) class;
104 gl_color_combo_parent_class = g_type_class_peek_parent (class);
106 gobject_class->finalize = gl_color_combo_finalize;
108 signals[COLOR_CHANGED] =
109 g_signal_new ("color_changed",
110 G_OBJECT_CLASS_TYPE (gobject_class),
112 G_STRUCT_OFFSET (glColorComboClass, color_changed),
114 gl_marshal_VOID__UINT_BOOLEAN,
116 2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
121 /*****************************************************************************/
122 /* Object Instance Init Function. */
123 /*****************************************************************************/
125 gl_color_combo_init (glColorCombo *this)
130 this->priv = g_new0 (glColorComboPrivate, 1);
132 hbox = gtk_hbox_new (FALSE, 3);
133 gtk_container_add (GTK_CONTAINER (this), hbox);
135 this->priv->swatch = gl_color_swatch_new (IMAGE_W, IMAGE_H, GL_COLOR_NONE);
136 gtk_box_pack_start (GTK_BOX (hbox), this->priv->swatch, TRUE, TRUE, 0);
138 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_IN);
139 gtk_box_pack_end (GTK_BOX (hbox), arrow, FALSE, FALSE, 0);
141 g_signal_connect (this, "button_press_event",
142 G_CALLBACK(button_press_event_cb), this);
146 /*****************************************************************************/
147 /* Finalize Method. */
148 /*****************************************************************************/
150 gl_color_combo_finalize (GObject *object)
154 g_return_if_fail (object && IS_GL_COLOR_COMBO (object));
155 this = GL_COLOR_COMBO (object);
157 g_object_ref_sink (this->priv->menu);
160 G_OBJECT_CLASS (gl_color_combo_parent_class)->finalize (object);
164 /*****************************************************************************/
165 /** New Object Generator. */
166 /*****************************************************************************/
168 gl_color_combo_new (const gchar *default_label,
174 this = g_object_new (TYPE_GL_COLOR_COMBO, NULL);
178 default_label = _("Default Color");
181 this->priv->default_color = default_color;
182 this->priv->color = color;
184 gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch), color);
186 this->priv->menu = gl_color_combo_menu_new (default_label,
188 gtk_widget_show_all (this->priv->menu);
190 g_signal_connect (this->priv->menu, "color_changed",
191 G_CALLBACK (menu_color_changed_cb), this);
192 g_signal_connect (this->priv->menu, "selection_done",
193 G_CALLBACK (menu_selection_done_cb), this);
195 return GTK_WIDGET (this);
199 /*****************************************************************************/
201 /*****************************************************************************/
203 gl_color_combo_set_color (glColorCombo *this,
206 this->priv->color = color;
208 gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch), color);
212 /*****************************************************************************/
213 /* Set to default color. */
214 /*****************************************************************************/
216 gl_color_combo_set_to_default (glColorCombo *this)
218 gl_color_combo_set_color (this, this->priv->default_color);
221 /*****************************************************************************/
223 /*****************************************************************************/
225 gl_color_combo_get_color (glColorCombo *this,
226 gboolean *is_default)
230 *is_default = this->priv->is_default_flag;
233 return this->priv->color;
237 /*****************************************************************************/
238 /* Menu positioning function. */
239 /*****************************************************************************/
241 menu_position_function (GtkMenu *menu,
248 gint w_screen, h_screen;
250 gint x_window, y_window;
251 GtkAllocation allocation;
252 gint x_this, y_this, h_this;
253 GtkRequisition menu_requisition;
259 screen = gtk_widget_get_screen (GTK_WIDGET (this));
260 w_screen = gdk_screen_get_width (screen);
261 h_screen = gdk_screen_get_height (screen);
264 * Absolute position of "this" window on screen.
266 window = gtk_widget_get_window (GTK_WIDGET (this));
267 gdk_window_get_origin (window, &x_window, &y_window);
270 * Position and size of "this" inside window
272 gtk_widget_get_allocation (GTK_WIDGET (this), &allocation);
273 x_this = allocation.x;
274 y_this = allocation.y;
275 h_this = allocation.height;
280 gtk_widget_size_request (this->priv->menu, &menu_requisition);
281 h_menu = menu_requisition.height;
282 w_menu = menu_requisition.width;
285 * Default position anchored to lower left corner of "this".
287 *x = x_window + x_this;
288 *y = y_window + y_this + h_this;
291 * Adjust vertical position if menu if extends past bottom of screen.
293 if ( (*y + h_menu) > h_screen )
295 *y = y_window + y_this - h_menu;
299 *y = h_screen - h_menu;
304 * Adjust horizontal position if menu if extends past edge of screen.
306 if ( (*x + w_menu) > w_screen )
308 *x = w_screen - w_menu;
316 /*****************************************************************************/
317 /* Button "button_press_event" callback. */
318 /*****************************************************************************/
320 button_press_event_cb (GtkWidget *widget,
321 GdkEventButton *event,
324 switch (event->button)
328 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (this), TRUE);
330 gtk_menu_popup (GTK_MENU (this->priv->menu),
332 (GtkMenuPositionFunc)menu_position_function, this,
333 event->button, event->time);
345 /*****************************************************************************/
346 /* Menu "color changed" callback. */
347 /*****************************************************************************/
349 menu_color_changed_cb (glColorComboMenu *object,
356 this->priv->color = this->priv->default_color;
360 this->priv->color = color;
362 this->priv->is_default_flag = is_default;
364 gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch),
367 g_signal_emit (this, signals[COLOR_CHANGED], 0,
369 this->priv->is_default_flag);
373 /*****************************************************************************/
374 /* Menu "color changed" callback. */
375 /*****************************************************************************/
377 menu_selection_done_cb (GtkMenuShell *object,
380 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (this), FALSE);
386 * Local Variables: -- emacs
388 * c-basic-offset: 8 -- emacs
389 * tab-width: 8 -- emacs
390 * indent-tabs-mode: nil -- emacs