1
/* LIBGIMP - The GIMP Library
2
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
5
* Copyright (C) 2004 Sven Neumann <sven@gimp.org>
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2 of the License, or (at your option) any later version.
12
* This library 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 GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the
19
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
* Boston, MA 02111-1307, USA.
29
#include "gimpwidgetstypes.h"
31
#include "gimpintcombobox.h"
32
#include "gimpintstore.h"
35
static void gimp_int_combo_box_init (GimpIntComboBox *combo_box);
39
gimp_int_combo_box_get_type (void)
41
static GType box_type = 0;
45
static const GTypeInfo box_info =
47
sizeof (GimpIntComboBoxClass),
49
NULL, /* base_finalize */
50
NULL, /* class_init */
51
NULL, /* class_finalize */
52
NULL, /* class_data */
53
sizeof (GimpIntComboBox),
55
(GInstanceInitFunc) gimp_int_combo_box_init
58
box_type = g_type_register_static (GTK_TYPE_COMBO_BOX,
67
gimp_int_combo_box_init (GimpIntComboBox *combo_box)
70
GtkCellRenderer *cell;
72
store = gimp_int_store_new ();
74
gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
76
g_object_unref (store);
78
cell = gtk_cell_renderer_pixbuf_new ();
79
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, FALSE);
80
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
81
"stock_id", GIMP_INT_STORE_STOCK_ID,
82
"pixbuf", GIMP_INT_STORE_PIXBUF,
85
cell = gtk_cell_renderer_text_new ();
86
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
87
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
88
"text", GIMP_INT_STORE_LABEL,
94
* gimp_int_combo_box_new:
95
* @first_label: the label of the first item
96
* @first_value: the value of the first item
97
* @...: a %NULL terminated list of more label, value pairs
99
* Creates a GtkComboBox that has integer values associated with each
100
* item. The items to fill the combo box with are specified as a %NULL
101
* terminated list of label/value pairs.
103
* Return value: a new #GimpIntComboBox.
108
gimp_int_combo_box_new (const gchar *first_label,
112
GtkWidget *combo_box;
115
va_start (args, first_value);
117
combo_box = gimp_int_combo_box_new_valist (first_label, first_value, args);
125
* gimp_int_combo_box_new_valist:
126
* @first_label: the label of the first item
127
* @first_value: the value of the first item
128
* @values: a va_list with more values
130
* A variant of gimp_int_combo_box_new() that takes a va_list of
131
* label/value pairs. Probably only useful for language bindings.
133
* Return value: a new #GimpIntComboBox.
138
gimp_int_combo_box_new_valist (const gchar *first_label,
142
GtkWidget *combo_box;
147
store = gimp_int_store_new ();
149
combo_box = g_object_new (GIMP_TYPE_INT_COMBO_BOX,
152
g_object_unref (store);
154
for (label = first_label, value = first_value;
156
label = va_arg (values, const gchar *), value = va_arg (values, gint))
160
gtk_list_store_append (store, &iter);
161
gtk_list_store_set (store, &iter,
162
GIMP_INT_STORE_VALUE, value,
163
GIMP_INT_STORE_LABEL, label,
171
* gimp_int_combo_box_new_array:
172
* @n_values: the number of values
173
* @labels: an array of labels (array length must be @n_values)
175
* A variant of gimp_int_combo_box_new() that takes an array of labels.
176
* The array indices are used as values.
178
* Return value: a new #GimpIntComboBox.
183
gimp_int_combo_box_new_array (gint n_values,
184
const gchar *labels[])
186
GtkWidget *combo_box;
190
g_return_val_if_fail (n_values > 0, NULL);
191
g_return_val_if_fail (labels != NULL, NULL);
193
store = gimp_int_store_new ();
195
combo_box = g_object_new (GIMP_TYPE_INT_COMBO_BOX,
198
g_object_unref (store);
200
for (i = 0; i < n_values; i++)
206
gtk_list_store_append (store, &iter);
207
gtk_list_store_set (store, &iter,
208
GIMP_INT_STORE_VALUE, i,
209
GIMP_INT_STORE_LABEL, gettext (labels[i]),
218
* gimp_int_combo_box_prepend:
219
* @combo_box: a #GimpIntComboBox
220
* @...: pairs of column number and value, terminated with -1
222
* This function provides a convenient way to prepend items to a
223
* #GimpIntComboBox. It prepends a row to the @combo_box's list store
224
* and calls gtk_list_store_set() for you.
226
* The column number must be taken from the enum #GimpIntStoreColumns.
231
gimp_int_combo_box_prepend (GimpIntComboBox *combo_box,
238
g_return_if_fail (GIMP_IS_INT_COMBO_BOX (combo_box));
240
store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
242
va_start (args, combo_box);
244
gtk_list_store_prepend (store, &iter);
245
gtk_list_store_set_valist (store, &iter, args);
251
* gimp_int_combo_box_append:
252
* @combo_box: a #GimpIntComboBox
253
* @...: pairs of column number and value, terminated with -1
255
* This function provides a convenient way to append items to a
256
* #GimpIntComboBox. It appends a row to the @combo_box's list store
257
* and calls gtk_list_store_set() for you.
259
* The column number must be taken from the enum #GimpIntStoreColumns.
264
gimp_int_combo_box_append (GimpIntComboBox *combo_box,
271
g_return_if_fail (GIMP_IS_INT_COMBO_BOX (combo_box));
273
store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
275
va_start (args, combo_box);
277
gtk_list_store_append (store, &iter);
278
gtk_list_store_set_valist (store, &iter, args);
284
* gimp_int_combo_box_set_active:
285
* @combo_box: a #GimpIntComboBox
286
* @value: an integer value
288
* Looks up the item that belongs to the given @value and makes it the
289
* selected item in the @combo_box.
291
* Return value: %TRUE on success or %FALSE if there was no item for
297
gimp_int_combo_box_set_active (GimpIntComboBox *combo_box,
303
g_return_val_if_fail (GIMP_IS_INT_COMBO_BOX (combo_box), FALSE);
305
model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
307
if (gimp_int_store_lookup_by_value (model, value, &iter))
309
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &iter);
317
* gimp_int_combo_box_get_active:
318
* @combo_box: a #GimpIntComboBox
319
* @value: return location for the integer value
321
* Retrieves the value of the selected (active) item in the @combo_box.
323
* Return value: %TRUE if @value has been set or %FALSE if no item was
329
gimp_int_combo_box_get_active (GimpIntComboBox *combo_box,
334
g_return_val_if_fail (GIMP_IS_INT_COMBO_BOX (combo_box), FALSE);
335
g_return_val_if_fail (value != NULL, FALSE);
337
if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
339
gtk_tree_model_get (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)),
341
GIMP_INT_STORE_VALUE, value,
350
* gimp_int_combo_box_connect:
351
* @combo_box: a #GimpIntComboBox
352
* @value: the value to set
353
* @callback: a callback to connect to the @combo_box's "changed" signal
354
* @data: a pointer passed as data to g_signal_connect()
356
* A convenience function that sets the inital @value of a
357
* #GimpIntComboBox and connects @callback to the "changed"
360
* This function also calls the @callback once after setting the
361
* initial @value. This is often convenient when working with combo
362
* boxes that select a default active item (like for example
363
* gimp_drawable_combo_box_new). If you pass an invalid initial
364
* @value, the @callback will be called with the default item active.
366
* Return value: the signal handler ID as returned by g_signal_connect()
371
gimp_int_combo_box_connect (GimpIntComboBox *combo_box,
378
g_return_val_if_fail (GIMP_IS_INT_COMBO_BOX (combo_box), 0);
381
handler = g_signal_connect (combo_box, "changed", callback, data);
383
if (! gimp_int_combo_box_set_active (combo_box, value))
384
g_signal_emit_by_name (combo_box, "changed", NULL);