1
/* LIBGIMP - The GIMP Library
2
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
5
* Copyright (C) 2007 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 "gimpstringcombobox.h"
47
GtkCellRenderer *text_renderer;
48
} GimpStringComboBoxPrivate;
50
#define GIMP_STRING_COMBO_BOX_GET_PRIVATE(obj) \
51
((GimpStringComboBoxPrivate *) ((GimpStringComboBox *) (obj))->priv)
54
static GObject * gimp_string_combo_box_constructor (GType type,
56
GObjectConstructParam *params);
58
static void gimp_string_combo_box_set_property (GObject *object,
62
static void gimp_string_combo_box_get_property (GObject *object,
68
G_DEFINE_TYPE (GimpStringComboBox, gimp_string_combo_box, GTK_TYPE_COMBO_BOX)
70
#define parent_class gimp_string_combo_box_parent_class
74
gimp_string_combo_box_class_init (GimpStringComboBoxClass *klass)
76
GObjectClass *object_class = G_OBJECT_CLASS (klass);
78
object_class->constructor = gimp_string_combo_box_constructor;
79
object_class->set_property = gimp_string_combo_box_set_property;
80
object_class->get_property = gimp_string_combo_box_get_property;
83
* GimpStringComboBox:id-column:
85
* The column in the associated GtkTreeModel that holds unique
90
g_object_class_install_property (object_class,
92
g_param_spec_int ("id-column", NULL, NULL,
95
GIMP_PARAM_READWRITE |
96
G_PARAM_CONSTRUCT_ONLY));
98
* GimpStringComboBox:id-column:
100
* The column in the associated GtkTreeModel that holds strings to
101
* be used as labels in the combo-box.
105
g_object_class_install_property (object_class,
107
g_param_spec_int ("label-column", NULL, NULL,
110
GIMP_PARAM_READWRITE |
111
G_PARAM_CONSTRUCT_ONLY));
114
* GimpStringComboBox:ellipsize:
116
* Specifies the preferred place to ellipsize text in the combo-box,
117
* if the cell renderer does not have enough room to display the
122
g_object_class_install_property (object_class,
124
g_param_spec_enum ("ellipsize", NULL, NULL,
125
PANGO_TYPE_ELLIPSIZE_MODE,
126
PANGO_ELLIPSIZE_NONE,
127
GIMP_PARAM_READWRITE));
129
g_type_class_add_private (object_class, sizeof (GimpStringComboBoxPrivate));
133
gimp_string_combo_box_init (GimpStringComboBox *combo_box)
135
combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
136
GIMP_TYPE_STRING_COMBO_BOX,
137
GimpStringComboBoxPrivate);
141
gimp_string_combo_box_constructor (GType type,
143
GObjectConstructParam *params)
146
GimpStringComboBoxPrivate *priv;
147
GtkCellRenderer *cell;
149
object = G_OBJECT_CLASS (parent_class)->constructor (type, n_params, params);
151
priv = GIMP_STRING_COMBO_BOX_GET_PRIVATE (object);
153
priv->text_renderer = cell = gtk_cell_renderer_text_new ();
155
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), cell, TRUE);
156
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), cell,
157
"text", priv->label_column,
164
gimp_string_combo_box_set_property (GObject *object,
169
GimpStringComboBoxPrivate *priv = GIMP_STRING_COMBO_BOX_GET_PRIVATE (object);
174
priv->id_column = g_value_get_int (value);
177
case PROP_LABEL_COLUMN:
178
priv->label_column = g_value_get_int (value);
182
g_object_set_property (G_OBJECT (priv->text_renderer),
186
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
192
gimp_string_combo_box_get_property (GObject *object,
197
GimpStringComboBoxPrivate *priv = GIMP_STRING_COMBO_BOX_GET_PRIVATE (object);
202
g_value_set_int (value, priv->id_column);
205
case PROP_LABEL_COLUMN:
206
g_value_set_int (value, priv->label_column);
210
g_object_get_property (G_OBJECT (priv->text_renderer),
214
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
220
gimp_string_model_lookup (GtkTreeModel *model,
225
GValue value = { 0, };
228
/* This lookup could be backed up by a hash table or some other
229
* data structure instead of doing a list traversal. But since this
230
* is a GtkComboBox, there shouldn't be many entries anyway...
233
for (iter_valid = gtk_tree_model_get_iter_first (model, iter);
235
iter_valid = gtk_tree_model_iter_next (model, iter))
239
gtk_tree_model_get_value (model, iter, column, &value);
241
str = g_value_get_string (&value);
243
if (str && strcmp (str, id) == 0)
245
g_value_unset (&value);
249
g_value_unset (&value);
257
* gimp_string_combo_box_new:
261
* Return value: a new #GimpStringComboBox.
266
gimp_string_combo_box_new (GtkTreeModel *model,
270
g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
271
g_return_val_if_fail (gtk_tree_model_get_column_type (model,
272
id_column) == G_TYPE_STRING, NULL);
273
g_return_val_if_fail (gtk_tree_model_get_column_type (model,
274
label_column) == G_TYPE_STRING, NULL);
276
return g_object_new (GIMP_TYPE_STRING_COMBO_BOX,
278
"id-column", id_column,
279
"label-column", label_column,
284
* gimp_string_combo_box_set_active:
285
* @combo_box: a #GimpStringComboBox
288
* Looks up the item that belongs to the given @id 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_string_combo_box_set_active (GimpStringComboBox *combo_box,
300
g_return_val_if_fail (GIMP_IS_STRING_COMBO_BOX (combo_box), FALSE);
308
model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
310
column = GIMP_STRING_COMBO_BOX_GET_PRIVATE (combo_box)->id_column;
312
if (gimp_string_model_lookup (model, column, id, &iter))
314
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &iter);
322
gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
329
* gimp_string_combo_box_get_active:
330
* @combo_box: a #GimpStringComboBox
332
* Retrieves the value of the selected (active) item in the @combo_box.
334
* Return value: newly allocated ID string or %NULL if nothing was selected
339
gimp_string_combo_box_get_active (GimpStringComboBox *combo_box)
343
g_return_val_if_fail (GIMP_IS_STRING_COMBO_BOX (combo_box), NULL);
345
if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
347
GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
351
column = GIMP_STRING_COMBO_BOX_GET_PRIVATE (combo_box)->id_column;
353
gtk_tree_model_get (model, &iter,