]> git.sur5r.net Git - glabels/blob - glabels2/src/color-combo.c
02008b2e9980fb58bb06096458edd1b2ef5e4806
[glabels] / glabels2 / src / color-combo.c
1 /*
2  *  color-combo.c
3  *  Copyright (C) 2008-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include <config.h>
22
23 #include "color-combo.h"
24
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>
30 #include "marshal.h"
31 #include "color.h"
32
33
34 #define IMAGE_W 24
35 #define IMAGE_H 24
36
37
38 /*========================================================*/
39 /* Private types.                                         */
40 /*========================================================*/
41
42 /** GL_COLOR_COMBO Private fields */
43 struct _glColorComboPrivate {
44
45         guint       color;
46         gboolean    is_default_flag;
47
48         guint       default_color;
49
50         GtkWidget  *swatch;
51
52         GtkWidget  *menu;
53 };
54
55 enum {
56         COLOR_CHANGED,
57         LAST_SIGNAL
58 };
59
60
61 /*========================================================*/
62 /* Private globals.                                       */
63 /*========================================================*/
64
65 static guint signals[LAST_SIGNAL] = {0};
66
67
68 /*========================================================*/
69 /* Private function prototypes.                           */
70 /*========================================================*/
71
72 static void gl_color_combo_finalize      (GObject             *object);
73
74 static gboolean
75 button_press_event_cb (GtkWidget      *widget,
76                        GdkEventButton *event,
77                        glColorCombo   *this);
78
79 static void
80 menu_color_changed_cb (glColorComboMenu   *object,
81                        guint               color,
82                        gboolean            is_default,
83                        glColorCombo       *this);
84
85 static void
86 menu_selection_done_cb (GtkMenuShell      *object,
87                         glColorCombo      *this);
88
89
90 /*****************************************************************************/
91 /* Object infrastructure.                                                    */
92 /*****************************************************************************/
93 G_DEFINE_TYPE (glColorCombo, gl_color_combo, GTK_TYPE_TOGGLE_BUTTON);
94
95
96 /*****************************************************************************/
97 /* Class Init Function.                                                      */
98 /*****************************************************************************/
99 static void
100 gl_color_combo_class_init (glColorComboClass *class)
101 {
102         GObjectClass       *gobject_class = (GObjectClass *) class;
103
104         gl_color_combo_parent_class = g_type_class_peek_parent (class);
105
106         gobject_class->finalize = gl_color_combo_finalize;
107
108         signals[COLOR_CHANGED] =
109                 g_signal_new ("color_changed",
110                               G_OBJECT_CLASS_TYPE (gobject_class),
111                               G_SIGNAL_RUN_LAST,
112                               G_STRUCT_OFFSET (glColorComboClass, color_changed),
113                               NULL, NULL,
114                               gl_marshal_VOID__UINT_BOOLEAN,
115                               G_TYPE_NONE,
116                               2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
117
118 }
119
120
121 /*****************************************************************************/
122 /* Object Instance Init Function.                                            */
123 /*****************************************************************************/
124 static void
125 gl_color_combo_init (glColorCombo *this)
126 {
127         GtkWidget *hbox;
128         GtkWidget *arrow;
129
130         this->priv = g_new0 (glColorComboPrivate, 1);
131
132         hbox = gtk_hbox_new (FALSE, 3);
133         gtk_container_add (GTK_CONTAINER (this), hbox);
134         
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);
137
138         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_IN);
139         gtk_box_pack_end (GTK_BOX (hbox), arrow, FALSE, FALSE, 0);
140
141         g_signal_connect (this, "button_press_event",
142                           G_CALLBACK(button_press_event_cb), this);
143 }
144
145
146 /*****************************************************************************/
147 /* Finalize Method.                                                          */
148 /*****************************************************************************/
149 static void
150 gl_color_combo_finalize (GObject *object)
151 {
152         glColorCombo    *this;
153
154         g_return_if_fail (object && IS_GL_COLOR_COMBO (object));
155         this = GL_COLOR_COMBO (object);
156
157         g_object_ref_sink (this->priv->menu);
158         g_free (this->priv);
159
160         G_OBJECT_CLASS (gl_color_combo_parent_class)->finalize (object);
161 }
162
163
164 /*****************************************************************************/
165 /** New Object Generator.                                                    */
166 /*****************************************************************************/
167 GtkWidget *
168 gl_color_combo_new (const gchar  *default_label,
169                     guint         default_color,
170                     guint         color)
171 {
172         glColorCombo *this;
173
174         this = g_object_new (TYPE_GL_COLOR_COMBO, NULL);
175
176         if (!default_label)
177         {
178                 default_label = _("Default Color");
179         }
180
181         this->priv->default_color = default_color;
182         this->priv->color = color;
183
184         gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch), color);
185
186         this->priv->menu = gl_color_combo_menu_new (default_label,
187                                                     color);
188         gtk_widget_show_all (this->priv->menu);
189
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);
194
195         return GTK_WIDGET (this);
196 }
197
198
199 /*****************************************************************************/
200 /* Set color.                                                                */
201 /*****************************************************************************/
202 void
203 gl_color_combo_set_color (glColorCombo  *this,
204                           guint          color)
205 {
206         this->priv->color = color;
207
208         gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch), color);
209 }
210
211
212 /*****************************************************************************/
213 /* Set to default color.                                                     */
214 /*****************************************************************************/
215 void
216 gl_color_combo_set_to_default (glColorCombo  *this)
217 {
218         gl_color_combo_set_color (this, this->priv->default_color);
219 }
220
221 /*****************************************************************************/
222 /* Get color.                                                                */
223 /*****************************************************************************/
224 guint
225 gl_color_combo_get_color (glColorCombo  *this,
226                           gboolean      *is_default)
227 {
228         if (is_default)
229         {
230                 *is_default = this->priv->is_default_flag;
231         }
232
233         return this->priv->color;
234 }
235
236
237 /*****************************************************************************/
238 /* Menu positioning function.                                                */
239 /*****************************************************************************/
240 static void
241 menu_position_function (GtkMenu  *menu,
242                         gint     *x,
243                         gint     *y,
244                         gboolean *push_in,
245                         gpointer  user_data)
246 {
247         glColorCombo *this = GL_COLOR_COMBO (user_data);
248         gint          x1, y1;
249         gint          menu_h, menu_w;
250
251         gdk_window_get_origin (GTK_WIDGET (this)->window, &x1, &y1);
252         *x = x1 + GTK_WIDGET (this)->allocation.x;
253         *y = y1 + GTK_WIDGET (this)->allocation.y +
254                 GTK_WIDGET (this)->allocation.height;
255                 
256         menu_h = this->priv->menu->allocation.height;
257         menu_w = this->priv->menu->allocation.width;
258
259         if ((*y + menu_h) > gdk_screen_height ())
260         {
261                 *y = y1 + GTK_WIDGET (this)->allocation.y - menu_h;
262                 if ( *y < 0 )
263                 {
264                         *y = gdk_screen_height () - menu_h;
265                 }
266         }
267
268         if ((*x + menu_w) > gdk_screen_width ())
269         {
270                 *x = gdk_screen_width () - menu_w;
271         }
272
273         *push_in = TRUE;
274 }
275
276
277 /*****************************************************************************/
278 /* Button "button_press_event" callback.                                     */
279 /*****************************************************************************/
280 static gboolean
281 button_press_event_cb (GtkWidget      *widget,
282                        GdkEventButton *event,
283                        glColorCombo   *this)
284 {
285         switch (event->button)
286         {
287
288         case 1:
289                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (this), TRUE);
290
291                 gtk_menu_popup (GTK_MENU (this->priv->menu),
292                                 NULL, NULL,
293                                 menu_position_function, this,
294                                 event->button, event->time);
295                 break;
296
297         default:
298                 break;
299
300         }
301
302         return FALSE;
303 }
304
305
306 /*****************************************************************************/
307 /* Menu "color changed" callback.                                            */
308 /*****************************************************************************/
309 static void
310 menu_color_changed_cb (glColorComboMenu     *object,
311                        guint                 color,
312                        gboolean              is_default,
313                        glColorCombo         *this)
314 {
315         if (is_default)
316         {
317                 this->priv->color = this->priv->default_color;
318         }
319         else
320         {
321                 this->priv->color = color;
322         }
323         this->priv->is_default_flag = is_default;
324
325         gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch),
326                                    this->priv->color);
327
328         g_signal_emit (this, signals[COLOR_CHANGED], 0,
329                        this->priv->color,
330                        this->priv->is_default_flag);
331 }
332
333
334 /*****************************************************************************/
335 /* Menu "color changed" callback.                                            */
336 /*****************************************************************************/
337 static void
338 menu_selection_done_cb (GtkMenuShell         *object,
339                         glColorCombo         *this)
340 {
341         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (this), FALSE);
342 }
343
344
345
346 /*
347  * Local Variables:       -- emacs
348  * mode: C                -- emacs
349  * c-basic-offset: 8      -- emacs
350  * tab-width: 8           -- emacs
351  * indent-tabs-mode: nil  -- emacs
352  * End:                   -- emacs
353  */