1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/*
* color-combo.c
* Copyright (C) 2008-2009 Jim Evins <evins@snaught.com>.
*
* This file is part of gLabels.
*
* gLabels is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gLabels is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gLabels. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include "color-combo.h"
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include "color-combo-menu.h"
#include "color-swatch.h"
#include "color.h"
#include "marshal.h"
#define IMAGE_W 24
#define IMAGE_H 24
/*========================================================*/
/* Private types. */
/*========================================================*/
/** GL_COLOR_COMBO Private fields */
struct _glColorComboPrivate {
guint color;
gboolean is_default_flag;
guint default_color;
GtkWidget *swatch;
GtkWidget *menu;
};
enum {
COLOR_CHANGED,
LAST_SIGNAL
};
/*========================================================*/
/* Private globals. */
/*========================================================*/
static guint signals[LAST_SIGNAL] = {0};
/*========================================================*/
/* Private function prototypes. */
/*========================================================*/
static void gl_color_combo_finalize (GObject *object);
static gboolean
button_press_event_cb (GtkWidget *widget,
GdkEventButton *event,
glColorCombo *this);
static void
menu_color_changed_cb (glColorComboMenu *object,
guint color,
gboolean is_default,
glColorCombo *this);
static void
menu_selection_done_cb (GtkMenuShell *object,
glColorCombo *this);
/*****************************************************************************/
/* Object infrastructure. */
/*****************************************************************************/
G_DEFINE_TYPE (glColorCombo, gl_color_combo, GTK_TYPE_TOGGLE_BUTTON);
/*****************************************************************************/
/* Class Init Function. */
/*****************************************************************************/
static void
gl_color_combo_class_init (glColorComboClass *class)
{
GObjectClass *gobject_class = (GObjectClass *) class;
gl_color_combo_parent_class = g_type_class_peek_parent (class);
gobject_class->finalize = gl_color_combo_finalize;
signals[COLOR_CHANGED] =
g_signal_new ("color_changed",
G_OBJECT_CLASS_TYPE (gobject_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (glColorComboClass, color_changed),
NULL, NULL,
gl_marshal_VOID__UINT_BOOLEAN,
G_TYPE_NONE,
2, G_TYPE_POINTER, G_TYPE_BOOLEAN);
}
/*****************************************************************************/
/* Object Instance Init Function. */
/*****************************************************************************/
static void
gl_color_combo_init (glColorCombo *this)
{
GtkWidget *hbox;
GtkWidget *arrow;
this->priv = g_new0 (glColorComboPrivate, 1);
hbox = gtk_hbox_new (FALSE, 3);
gtk_container_add (GTK_CONTAINER (this), hbox);
this->priv->swatch = gl_color_swatch_new (IMAGE_W, IMAGE_H, GL_COLOR_NONE);
gtk_box_pack_start (GTK_BOX (hbox), this->priv->swatch, TRUE, TRUE, 0);
arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_IN);
gtk_box_pack_end (GTK_BOX (hbox), arrow, FALSE, FALSE, 0);
g_signal_connect (this, "button_press_event",
G_CALLBACK(button_press_event_cb), this);
}
/*****************************************************************************/
/* Finalize Method. */
/*****************************************************************************/
static void
gl_color_combo_finalize (GObject *object)
{
glColorCombo *this;
g_return_if_fail (object && IS_GL_COLOR_COMBO (object));
this = GL_COLOR_COMBO (object);
g_object_ref_sink (this->priv->menu);
g_free (this->priv);
G_OBJECT_CLASS (gl_color_combo_parent_class)->finalize (object);
}
/*****************************************************************************/
/** New Object Generator. */
/*****************************************************************************/
GtkWidget *
gl_color_combo_new (const gchar *default_label,
guint default_color,
guint color)
{
glColorCombo *this;
this = g_object_new (TYPE_GL_COLOR_COMBO, NULL);
if (!default_label)
{
default_label = _("Default Color");
}
this->priv->default_color = default_color;
this->priv->color = color;
gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch), color);
this->priv->menu = gl_color_combo_menu_new (default_label,
color);
gtk_widget_show_all (this->priv->menu);
g_signal_connect (this->priv->menu, "color_changed",
G_CALLBACK (menu_color_changed_cb), this);
g_signal_connect (this->priv->menu, "selection_done",
G_CALLBACK (menu_selection_done_cb), this);
return GTK_WIDGET (this);
}
/*****************************************************************************/
/* Set color. */
/*****************************************************************************/
void
gl_color_combo_set_color (glColorCombo *this,
guint color)
{
this->priv->color = color;
gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch), color);
}
/*****************************************************************************/
/* Set to default color. */
/*****************************************************************************/
void
gl_color_combo_set_to_default (glColorCombo *this)
{
gl_color_combo_set_color (this, this->priv->default_color);
}
/*****************************************************************************/
/* Get color. */
/*****************************************************************************/
guint
gl_color_combo_get_color (glColorCombo *this,
gboolean *is_default)
{
if (is_default)
{
*is_default = this->priv->is_default_flag;
}
return this->priv->color;
}
/*****************************************************************************/
/* Menu positioning function. */
/*****************************************************************************/
static void
menu_position_function (GtkMenu *menu,
gint *x,
gint *y,
gboolean *push_in,
glColorCombo *this)
{
GdkScreen *screen;
gint w_screen, h_screen;
GdkWindow *window;
gint x_window, y_window;
GtkAllocation allocation;
gint x_this, y_this, h_this;
GtkRequisition menu_requisition;
gint h_menu, w_menu;
/*
* Screen size
*/
screen = gtk_widget_get_screen (GTK_WIDGET (this));
w_screen = gdk_screen_get_width (screen);
h_screen = gdk_screen_get_height (screen);
/*
* Absolute position of "this" window on screen.
*/
window = gtk_widget_get_window (GTK_WIDGET (this));
gdk_window_get_origin (window, &x_window, &y_window);
/*
* Position and size of "this" inside window
*/
gtk_widget_get_allocation (GTK_WIDGET (this), &allocation);
x_this = allocation.x;
y_this = allocation.y;
h_this = allocation.height;
/*
* Size of menu.
*/
gtk_widget_size_request (this->priv->menu, &menu_requisition);
h_menu = menu_requisition.height;
w_menu = menu_requisition.width;
/*
* Default position anchored to lower left corner of "this".
*/
*x = x_window + x_this;
*y = y_window + y_this + h_this;
/*
* Adjust vertical position if menu if extends past bottom of screen.
*/
if ( (*y + h_menu) > h_screen )
{
*y = y_window + y_this - h_menu;
if ( *y < 0 )
{
*y = h_screen - h_menu;
}
}
/*
* Adjust horizontal position if menu if extends past edge of screen.
*/
if ( (*x + w_menu) > w_screen )
{
*x = w_screen - w_menu;
}
*push_in = TRUE;
}
/*****************************************************************************/
/* Button "button_press_event" callback. */
/*****************************************************************************/
static gboolean
button_press_event_cb (GtkWidget *widget,
GdkEventButton *event,
glColorCombo *this)
{
switch (event->button)
{
case 1:
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (this), TRUE);
gtk_menu_popup (GTK_MENU (this->priv->menu),
NULL, NULL,
(GtkMenuPositionFunc)menu_position_function, this,
event->button, event->time);
break;
default:
break;
}
return FALSE;
}
/*****************************************************************************/
/* Menu "color changed" callback. */
/*****************************************************************************/
static void
menu_color_changed_cb (glColorComboMenu *object,
guint color,
gboolean is_default,
glColorCombo *this)
{
if (is_default)
{
this->priv->color = this->priv->default_color;
}
else
{
this->priv->color = color;
}
this->priv->is_default_flag = is_default;
gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch),
this->priv->color);
g_signal_emit (this, signals[COLOR_CHANGED], 0,
this->priv->color,
this->priv->is_default_flag);
}
/*****************************************************************************/
/* Menu "color changed" callback. */
/*****************************************************************************/
static void
menu_selection_done_cb (GtkMenuShell *object,
glColorCombo *this)
{
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (this), FALSE);
}
/*
* Local Variables: -- emacs
* mode: C -- emacs
* c-basic-offset: 8 -- emacs
* tab-width: 8 -- emacs
* indent-tabs-mode: nil -- emacs
* End: -- emacs
*/
|