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

« back to all changes in this revision

Viewing changes to libgimpwidgets/gimpenumstore.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
 * gimpenumstore.c
 
5
 * Copyright (C) 2004-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 <gtk/gtk.h>
 
26
 
 
27
#include "libgimpbase/gimpbase.h"
 
28
 
 
29
#include "gimpwidgetstypes.h"
 
30
 
 
31
#include "gimpenumstore.h"
 
32
 
 
33
 
 
34
enum
 
35
{
 
36
  PROP_0,
 
37
  PROP_ENUM_TYPE
 
38
};
 
39
 
 
40
 
 
41
static void   gimp_enum_store_finalize     (GObject      *object);
 
42
static void   gimp_enum_store_set_property (GObject      *object,
 
43
                                            guint         property_id,
 
44
                                            const GValue *value,
 
45
                                            GParamSpec   *pspec);
 
46
static void   gimp_enum_store_get_property (GObject      *object,
 
47
                                            guint         property_id,
 
48
                                            GValue       *value,
 
49
                                            GParamSpec   *pspec);
 
50
 
 
51
static void   gimp_enum_store_add_value    (GtkListStore *store,
 
52
                                            GEnumValue   *value);
 
53
 
 
54
 
 
55
G_DEFINE_TYPE (GimpEnumStore, gimp_enum_store, GIMP_TYPE_INT_STORE)
 
56
 
 
57
#define parent_class gimp_enum_store_parent_class
 
58
 
 
59
 
 
60
static void
 
61
gimp_enum_store_class_init (GimpEnumStoreClass *klass)
 
62
{
 
63
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
64
 
 
65
  object_class->finalize     = gimp_enum_store_finalize;
 
66
  object_class->set_property = gimp_enum_store_set_property;
 
67
  object_class->get_property = gimp_enum_store_get_property;
 
68
 
 
69
  /**
 
70
   * GimpEnumStore:enum-type:
 
71
   *
 
72
   * Sets the #GType of the enum to be used in the store.
 
73
   *
 
74
   * Since: GIMP 2.4
 
75
   */
 
76
  g_object_class_install_property (object_class,
 
77
                                   PROP_ENUM_TYPE,
 
78
                                   g_param_spec_gtype ("enum-type",
 
79
                                                       NULL, NULL,
 
80
                                                       G_TYPE_ENUM,
 
81
                                                       G_PARAM_CONSTRUCT_ONLY |
 
82
                                                       GIMP_PARAM_READWRITE));
 
83
}
 
84
 
 
85
static void
 
86
gimp_enum_store_init (GimpEnumStore *store)
 
87
{
 
88
}
 
89
 
 
90
static void
 
91
gimp_enum_store_finalize (GObject *object)
 
92
{
 
93
  GimpEnumStore *store = GIMP_ENUM_STORE (object);
 
94
 
 
95
  if (store->enum_class)
 
96
    g_type_class_unref (store->enum_class);
 
97
 
 
98
  G_OBJECT_CLASS (parent_class)->finalize (object);
 
99
}
 
100
 
 
101
static void
 
102
gimp_enum_store_set_property (GObject      *object,
 
103
                              guint         property_id,
 
104
                              const GValue *value,
 
105
                              GParamSpec   *pspec)
 
106
{
 
107
  GimpEnumStore *store = GIMP_ENUM_STORE (object);
 
108
 
 
109
  switch (property_id)
 
110
    {
 
111
    case PROP_ENUM_TYPE:
 
112
      g_return_if_fail (store->enum_class == NULL);
 
113
      store->enum_class = g_type_class_ref (g_value_get_gtype (value));
 
114
      break;
 
115
    default:
 
116
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
117
      break;
 
118
    }
 
119
}
 
120
 
 
121
static void
 
122
gimp_enum_store_get_property (GObject    *object,
 
123
                              guint       property_id,
 
124
                              GValue     *value,
 
125
                              GParamSpec *pspec)
 
126
{
 
127
  GimpEnumStore *store = GIMP_ENUM_STORE (object);
 
128
 
 
129
  switch (property_id)
 
130
    {
 
131
    case PROP_ENUM_TYPE:
 
132
      g_value_set_gtype (value, (store->enum_class ?
 
133
                                 G_TYPE_FROM_CLASS (store->enum_class) :
 
134
                                 G_TYPE_NONE));
 
135
      break;
 
136
    default:
 
137
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
138
      break;
 
139
    }
 
140
}
 
141
 
 
142
static void
 
143
gimp_enum_store_add_value (GtkListStore *store,
 
144
                           GEnumValue   *value)
 
145
{
 
146
  GtkTreeIter  iter;
 
147
  const gchar *desc;
 
148
 
 
149
  desc = gimp_enum_value_get_desc (GIMP_ENUM_STORE (store)->enum_class, value);
 
150
 
 
151
  gtk_list_store_append (store, &iter);
 
152
  gtk_list_store_set (store, &iter,
 
153
                      GIMP_INT_STORE_VALUE, value->value,
 
154
                      GIMP_INT_STORE_LABEL, desc,
 
155
                      -1);
 
156
}
 
157
 
 
158
 
 
159
/**
 
160
 * gimp_enum_store_new:
 
161
 * @enum_type: the #GType of an enum.
 
162
 *
 
163
 * Creates a new #GimpEnumStore, derived from #GtkListStore and fills
 
164
 * it with enum values. The enum needs to be registered to the type
 
165
 * system and should have translatable value names.
 
166
 *
 
167
 * Return value: a new #GimpEnumStore.
 
168
 *
 
169
 * Since: GIMP 2.4
 
170
 **/
 
171
GtkListStore *
 
172
gimp_enum_store_new (GType enum_type)
 
173
{
 
174
  GtkListStore *store;
 
175
  GEnumClass   *enum_class;
 
176
 
 
177
  g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), NULL);
 
178
 
 
179
  enum_class = g_type_class_ref (enum_type);
 
180
 
 
181
  store = gimp_enum_store_new_with_range (enum_type,
 
182
                                          enum_class->minimum,
 
183
                                          enum_class->maximum);
 
184
 
 
185
  g_type_class_unref (enum_class);
 
186
 
 
187
  return store;
 
188
}
 
189
 
 
190
/**
 
191
 * gimp_enum_store_new_with_range:
 
192
 * @enum_type: the #GType of an enum.
 
193
 * @minimum: the minimum value to include
 
194
 * @maximum: the maximum value to include
 
195
 *
 
196
 * Creates a new #GimpEnumStore like gimp_enum_store_new() but allows
 
197
 * to limit the enum values to a certain range. Values smaller than
 
198
 * @minimum or larger than @maximum are not added to the store.
 
199
 *
 
200
 * Return value: a new #GimpEnumStore.
 
201
 *
 
202
 * Since: GIMP 2.4
 
203
 **/
 
204
GtkListStore *
 
205
gimp_enum_store_new_with_range (GType  enum_type,
 
206
                                gint   minimum,
 
207
                                gint   maximum)
 
208
{
 
209
  GtkListStore *store;
 
210
  GEnumValue   *value;
 
211
 
 
212
  g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), NULL);
 
213
 
 
214
  store = g_object_new (GIMP_TYPE_ENUM_STORE,
 
215
                        "enum-type", enum_type,
 
216
                        NULL);
 
217
 
 
218
  for (value = GIMP_ENUM_STORE (store)->enum_class->values;
 
219
       value->value_name;
 
220
       value++)
 
221
    {
 
222
      if (value->value < minimum || value->value > maximum)
 
223
        continue;
 
224
 
 
225
      gimp_enum_store_add_value (store, value);
 
226
    }
 
227
 
 
228
  return store;
 
229
}
 
230
 
 
231
/**
 
232
 * gimp_enum_store_new_with_values
 
233
 * @enum_type: the #GType of an enum.
 
234
 * @n_values:  the number of enum values to include
 
235
 * @...:       a list of enum values (exactly @n_values)
 
236
 *
 
237
 * Creates a new #GimpEnumStore like gimp_enum_store_new() but allows
 
238
 * to expliticely list the enum values that should be added to the
 
239
 * store.
 
240
 *
 
241
 * Return value: a new #GimpEnumStore.
 
242
 *
 
243
 * Since: GIMP 2.4
 
244
 **/
 
245
GtkListStore *
 
246
gimp_enum_store_new_with_values (GType enum_type,
 
247
                                 gint  n_values,
 
248
                                 ...)
 
249
{
 
250
  GtkListStore *store;
 
251
  va_list       args;
 
252
 
 
253
  va_start (args, n_values);
 
254
 
 
255
  store = gimp_enum_store_new_with_values_valist (enum_type, n_values, args);
 
256
 
 
257
  va_end (args);
 
258
 
 
259
  return store;
 
260
}
 
261
 
 
262
/**
 
263
 * gimp_enum_store_new_with_values_valist:
 
264
 * @enum_type: the #GType of an enum.
 
265
 * @n_values:  the number of enum values to include
 
266
 * @args:      a va_list of enum values (exactly @n_values)
 
267
 *
 
268
 * See gimp_enum_store_new_with_values().
 
269
 *
 
270
 * Return value: a new #GimpEnumStore.
 
271
 *
 
272
 * Since: GIMP 2.4
 
273
 **/
 
274
GtkListStore *
 
275
gimp_enum_store_new_with_values_valist (GType     enum_type,
 
276
                                        gint      n_values,
 
277
                                        va_list   args)
 
278
{
 
279
  GtkListStore *store;
 
280
  GEnumValue   *value;
 
281
  gint          i;
 
282
 
 
283
  g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), NULL);
 
284
  g_return_val_if_fail (n_values > 1, NULL);
 
285
 
 
286
  store = g_object_new (GIMP_TYPE_ENUM_STORE,
 
287
                        "enum-type", enum_type,
 
288
                        NULL);
 
289
 
 
290
  for (i = 0; i < n_values; i++)
 
291
    {
 
292
      value = g_enum_get_value (GIMP_ENUM_STORE (store)->enum_class,
 
293
                                va_arg (args, gint));
 
294
 
 
295
      if (value)
 
296
        gimp_enum_store_add_value (store, value);
 
297
    }
 
298
 
 
299
  return store;
 
300
}
 
301
 
 
302
/**
 
303
 * gimp_enum_store_set_stock_prefix:
 
304
 * @store:        a #GimpEnumStore
 
305
 * @stock_prefix: a prefix to create icon stock ID from enum values
 
306
 *
 
307
 * Creates a stock ID for each enum value in the @store by appending
 
308
 * the value's nick to the given @stock_prefix, separated by a hyphen.
 
309
 *
 
310
 * See also: gimp_enum_combo_box_set_stock_prefix().
 
311
 *
 
312
 * Since: GIMP 2.4
 
313
 **/
 
314
void
 
315
gimp_enum_store_set_stock_prefix (GimpEnumStore *store,
 
316
                                  const gchar   *stock_prefix)
 
317
{
 
318
  GtkTreeModel *model;
 
319
  GtkTreeIter   iter;
 
320
  gboolean      iter_valid;
 
321
 
 
322
  g_return_if_fail (GIMP_IS_ENUM_STORE (store));
 
323
 
 
324
  model = GTK_TREE_MODEL (store);
 
325
 
 
326
  for (iter_valid = gtk_tree_model_get_iter_first (model, &iter);
 
327
       iter_valid;
 
328
       iter_valid = gtk_tree_model_iter_next (model, &iter))
 
329
    {
 
330
      gchar *stock_id = NULL;
 
331
 
 
332
      if (stock_prefix)
 
333
        {
 
334
          GEnumValue *enum_value;
 
335
          gint        value;
 
336
 
 
337
          gtk_tree_model_get (model, &iter,
 
338
                              GIMP_INT_STORE_VALUE, &value,
 
339
                              -1);
 
340
 
 
341
          enum_value = g_enum_get_value (store->enum_class, value);
 
342
 
 
343
          stock_id = g_strconcat (stock_prefix, "-",
 
344
                                  enum_value->value_nick,
 
345
                                  NULL);
 
346
        }
 
347
 
 
348
      gtk_list_store_set (GTK_LIST_STORE (store), &iter,
 
349
                          GIMP_INT_STORE_STOCK_ID, stock_id,
 
350
                          -1);
 
351
 
 
352
      if (stock_id)
 
353
        g_free (stock_id);
 
354
    }
 
355
}