~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to libgimpwidgets/gimpstringcombobox.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LIBGIMP - The GIMP Library
 
2
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 
3
 *
 
4
 * gimpstringcombobox.c
 
5
 * Copyright (C) 2007  Sven Neumann <sven@gimp.org>
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include <string.h>
 
26
 
 
27
#include <gtk/gtk.h>
 
28
 
 
29
#include "gimpwidgetstypes.h"
 
30
 
 
31
#include "gimpstringcombobox.h"
 
32
 
 
33
 
 
34
enum
 
35
{
 
36
  PROP_0,
 
37
  PROP_ID_COLUMN,
 
38
  PROP_LABEL_COLUMN,
 
39
  PROP_ELLIPSIZE
 
40
};
 
41
 
 
42
 
 
43
typedef struct
 
44
{
 
45
  gint             id_column;
 
46
  gint             label_column;
 
47
  GtkCellRenderer *text_renderer;
 
48
} GimpStringComboBoxPrivate;
 
49
 
 
50
#define GIMP_STRING_COMBO_BOX_GET_PRIVATE(obj) \
 
51
  ((GimpStringComboBoxPrivate *) ((GimpStringComboBox *) (obj))->priv)
 
52
 
 
53
 
 
54
static GObject * gimp_string_combo_box_constructor (GType                  type,
 
55
                                                    guint                  n_params,
 
56
                                                    GObjectConstructParam *params);
 
57
 
 
58
static void  gimp_string_combo_box_set_property (GObject         *object,
 
59
                                                 guint            property_id,
 
60
                                                 const GValue    *value,
 
61
                                                 GParamSpec      *pspec);
 
62
static void  gimp_string_combo_box_get_property (GObject         *object,
 
63
                                                 guint            property_id,
 
64
                                                 GValue          *value,
 
65
                                                 GParamSpec      *pspec);
 
66
 
 
67
 
 
68
G_DEFINE_TYPE (GimpStringComboBox, gimp_string_combo_box, GTK_TYPE_COMBO_BOX)
 
69
 
 
70
#define parent_class gimp_string_combo_box_parent_class
 
71
 
 
72
 
 
73
static void
 
74
gimp_string_combo_box_class_init (GimpStringComboBoxClass *klass)
 
75
{
 
76
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
77
 
 
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;
 
81
 
 
82
  /**
 
83
   * GimpStringComboBox:id-column:
 
84
   *
 
85
   * The column in the associated GtkTreeModel that holds unique
 
86
   * string IDs.
 
87
   *
 
88
   * Since: GIMP 2.4
 
89
   */
 
90
  g_object_class_install_property (object_class,
 
91
                                   PROP_ID_COLUMN,
 
92
                                   g_param_spec_int ("id-column", NULL, NULL,
 
93
                                                     0, G_MAXINT,
 
94
                                                     0,
 
95
                                                     GIMP_PARAM_READWRITE |
 
96
                                                     G_PARAM_CONSTRUCT_ONLY));
 
97
  /**
 
98
   * GimpStringComboBox:id-column:
 
99
   *
 
100
   * The column in the associated GtkTreeModel that holds strings to
 
101
   * be used as labels in the combo-box.
 
102
   *
 
103
   * Since: GIMP 2.4
 
104
   */
 
105
  g_object_class_install_property (object_class,
 
106
                                   PROP_LABEL_COLUMN,
 
107
                                   g_param_spec_int ("label-column", NULL, NULL,
 
108
                                                     0, G_MAXINT,
 
109
                                                     0,
 
110
                                                     GIMP_PARAM_READWRITE |
 
111
                                                     G_PARAM_CONSTRUCT_ONLY));
 
112
 
 
113
  /**
 
114
   * GimpStringComboBox:ellipsize:
 
115
   *
 
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
 
118
   * entire string.
 
119
   *
 
120
   * Since: GIMP 2.4
 
121
   */
 
122
  g_object_class_install_property (object_class,
 
123
                                   PROP_ELLIPSIZE,
 
124
                                   g_param_spec_enum ("ellipsize", NULL, NULL,
 
125
                                                      PANGO_TYPE_ELLIPSIZE_MODE,
 
126
                                                      PANGO_ELLIPSIZE_NONE,
 
127
                                                      GIMP_PARAM_READWRITE));
 
128
 
 
129
  g_type_class_add_private (object_class, sizeof (GimpStringComboBoxPrivate));
 
130
}
 
131
 
 
132
static void
 
133
gimp_string_combo_box_init (GimpStringComboBox *combo_box)
 
134
{
 
135
  combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
 
136
                                                 GIMP_TYPE_STRING_COMBO_BOX,
 
137
                                                 GimpStringComboBoxPrivate);
 
138
}
 
139
 
 
140
static GObject *
 
141
gimp_string_combo_box_constructor (GType                  type,
 
142
                                   guint                  n_params,
 
143
                                   GObjectConstructParam *params)
 
144
{
 
145
  GObject                   *object;
 
146
  GimpStringComboBoxPrivate *priv;
 
147
  GtkCellRenderer           *cell;
 
148
 
 
149
  object = G_OBJECT_CLASS (parent_class)->constructor (type, n_params, params);
 
150
 
 
151
  priv = GIMP_STRING_COMBO_BOX_GET_PRIVATE (object);
 
152
 
 
153
  priv->text_renderer = cell = gtk_cell_renderer_text_new ();
 
154
 
 
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,
 
158
                                  NULL);
 
159
 
 
160
  return object;
 
161
}
 
162
 
 
163
static void
 
164
gimp_string_combo_box_set_property (GObject      *object,
 
165
                                    guint         property_id,
 
166
                                    const GValue *value,
 
167
                                    GParamSpec   *pspec)
 
168
{
 
169
  GimpStringComboBoxPrivate *priv = GIMP_STRING_COMBO_BOX_GET_PRIVATE (object);
 
170
 
 
171
  switch (property_id)
 
172
    {
 
173
    case PROP_ID_COLUMN:
 
174
      priv->id_column = g_value_get_int (value);
 
175
      break;
 
176
 
 
177
    case PROP_LABEL_COLUMN:
 
178
      priv->label_column = g_value_get_int (value);
 
179
      break;
 
180
 
 
181
    case PROP_ELLIPSIZE:
 
182
      g_object_set_property (G_OBJECT (priv->text_renderer),
 
183
                             pspec->name, value);
 
184
      break;
 
185
    default:
 
186
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
187
      break;
 
188
    }
 
189
}
 
190
 
 
191
static void
 
192
gimp_string_combo_box_get_property (GObject    *object,
 
193
                                    guint       property_id,
 
194
                                    GValue     *value,
 
195
                                    GParamSpec *pspec)
 
196
{
 
197
  GimpStringComboBoxPrivate *priv = GIMP_STRING_COMBO_BOX_GET_PRIVATE (object);
 
198
 
 
199
  switch (property_id)
 
200
    {
 
201
    case PROP_ID_COLUMN:
 
202
      g_value_set_int (value, priv->id_column);
 
203
      break;
 
204
 
 
205
    case PROP_LABEL_COLUMN:
 
206
      g_value_set_int (value, priv->label_column);
 
207
      break;
 
208
 
 
209
    case PROP_ELLIPSIZE:
 
210
      g_object_get_property (G_OBJECT (priv->text_renderer),
 
211
                             pspec->name, value);
 
212
      break;
 
213
    default:
 
214
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
215
      break;
 
216
    }
 
217
}
 
218
 
 
219
static gboolean
 
220
gimp_string_model_lookup (GtkTreeModel *model,
 
221
                          gint          column,
 
222
                          const gchar  *id,
 
223
                          GtkTreeIter  *iter)
 
224
{
 
225
  GValue    value = { 0, };
 
226
  gboolean  iter_valid;
 
227
 
 
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...
 
231
   */
 
232
 
 
233
  for (iter_valid = gtk_tree_model_get_iter_first (model, iter);
 
234
       iter_valid;
 
235
       iter_valid = gtk_tree_model_iter_next (model, iter))
 
236
    {
 
237
      const gchar *str;
 
238
 
 
239
      gtk_tree_model_get_value (model, iter, column, &value);
 
240
 
 
241
      str = g_value_get_string (&value);
 
242
 
 
243
      if (str && strcmp (str, id) == 0)
 
244
        {
 
245
          g_value_unset (&value);
 
246
          break;
 
247
        }
 
248
 
 
249
      g_value_unset (&value);
 
250
    }
 
251
 
 
252
  return iter_valid;
 
253
}
 
254
 
 
255
 
 
256
/**
 
257
 * gimp_string_combo_box_new:
 
258
 * @id_column:
 
259
 * @label_column:
 
260
 *
 
261
 * Return value: a new #GimpStringComboBox.
 
262
 *
 
263
 * Since: GIMP 2.4
 
264
 **/
 
265
GtkWidget *
 
266
gimp_string_combo_box_new (GtkTreeModel *model,
 
267
                           gint          id_column,
 
268
                           gint          label_column)
 
269
{
 
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);
 
275
 
 
276
  return g_object_new (GIMP_TYPE_STRING_COMBO_BOX,
 
277
                       "model",        model,
 
278
                       "id-column",    id_column,
 
279
                       "label-column", label_column,
 
280
                       NULL);
 
281
}
 
282
 
 
283
/**
 
284
 * gimp_string_combo_box_set_active:
 
285
 * @combo_box: a #GimpStringComboBox
 
286
 * @id:
 
287
 *
 
288
 * Looks up the item that belongs to the given @id and makes it the
 
289
 * selected item in the @combo_box.
 
290
 *
 
291
 * Return value: %TRUE on success or %FALSE if there was no item for
 
292
 *               this value.
 
293
 *
 
294
 * Since: GIMP 2.4
 
295
 **/
 
296
gboolean
 
297
gimp_string_combo_box_set_active (GimpStringComboBox *combo_box,
 
298
                                  const gchar        *id)
 
299
{
 
300
  g_return_val_if_fail (GIMP_IS_STRING_COMBO_BOX (combo_box), FALSE);
 
301
 
 
302
  if (id)
 
303
    {
 
304
      GtkTreeModel *model;
 
305
      GtkTreeIter   iter;
 
306
      gint          column;
 
307
 
 
308
      model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
 
309
 
 
310
      column = GIMP_STRING_COMBO_BOX_GET_PRIVATE (combo_box)->id_column;
 
311
 
 
312
      if (gimp_string_model_lookup (model, column, id, &iter))
 
313
        {
 
314
          gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &iter);
 
315
          return TRUE;
 
316
        }
 
317
 
 
318
      return FALSE;
 
319
    }
 
320
  else
 
321
    {
 
322
      gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), -1);
 
323
 
 
324
      return TRUE;
 
325
    }
 
326
}
 
327
 
 
328
/**
 
329
 * gimp_string_combo_box_get_active:
 
330
 * @combo_box: a #GimpStringComboBox
 
331
 *
 
332
 * Retrieves the value of the selected (active) item in the @combo_box.
 
333
 *
 
334
 * Return value: newly allocated ID string or %NULL if nothing was selected
 
335
 *
 
336
 * Since: GIMP 2.4
 
337
 **/
 
338
gchar *
 
339
gimp_string_combo_box_get_active (GimpStringComboBox *combo_box)
 
340
{
 
341
  GtkTreeIter  iter;
 
342
 
 
343
  g_return_val_if_fail (GIMP_IS_STRING_COMBO_BOX (combo_box), NULL);
 
344
 
 
345
  if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
 
346
    {
 
347
      GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
 
348
      gchar        *value;
 
349
      gint          column;
 
350
 
 
351
      column = GIMP_STRING_COMBO_BOX_GET_PRIVATE (combo_box)->id_column;
 
352
 
 
353
      gtk_tree_model_get (model, &iter,
 
354
                          column, &value,
 
355
                          -1);
 
356
 
 
357
      return value;
 
358
    }
 
359
 
 
360
  return NULL;
 
361
}