1 /* LIBGIMP - The GIMP Library
2  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3  *
4  * gimpenumcombobox.c
5  * Copyright (C) 2004  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 3 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, see
19  * <https://www.gnu.org/licenses/>.
20  */
21 
22 #include "config.h"
23 
24 #include <gtk/gtk.h>
25 
26 #include "gimpwidgetstypes.h"
27 
28 #include "gimpenumcombobox.h"
29 #include "gimpenumstore.h"
30 
31 #include "libgimp/libgimp-intl.h"
32 
33 
34 /**
35  * SECTION: gimpenumcombobox
36  * @title: GimpEnumComboBox
37  * @short_description: A #GimpIntComboBox subclass for selecting an enum value.
38  *
39  * A #GtkComboBox subclass for selecting an enum value.
40  **/
41 
42 
43 enum
44 {
45   PROP_0,
46   PROP_MODEL
47 };
48 
49 
50 static void  gimp_enum_combo_box_set_property (GObject      *object,
51                                                guint         prop_id,
52                                                const GValue *value,
53                                                GParamSpec   *pspec);
54 static void  gimp_enum_combo_box_get_property (GObject      *object,
55                                                guint         prop_id,
56                                                GValue       *value,
57                                                GParamSpec   *pspec);
58 
59 
G_DEFINE_TYPE(GimpEnumComboBox,gimp_enum_combo_box,GIMP_TYPE_INT_COMBO_BOX)60 G_DEFINE_TYPE (GimpEnumComboBox, gimp_enum_combo_box,
61                GIMP_TYPE_INT_COMBO_BOX)
62 
63 #define parent_class gimp_enum_combo_box_parent_class
64 
65 
66 static void
67 gimp_enum_combo_box_class_init (GimpEnumComboBoxClass *klass)
68 {
69   GObjectClass *object_class = G_OBJECT_CLASS (klass);
70 
71   object_class->set_property = gimp_enum_combo_box_set_property;
72   object_class->get_property = gimp_enum_combo_box_get_property;
73 
74   /*  override the "model" property of GtkComboBox  */
75   g_object_class_install_property (object_class,
76                                    PROP_MODEL,
77                                    g_param_spec_object ("model",
78                                                         "Model",
79                                                         "The enum store used by this combo box",
80                                                         GIMP_TYPE_ENUM_STORE,
81                                                         GIMP_PARAM_READWRITE));
82 }
83 
84 static void
gimp_enum_combo_box_init(GimpEnumComboBox * combo_box)85 gimp_enum_combo_box_init (GimpEnumComboBox *combo_box)
86 {
87 }
88 
89 static void
gimp_enum_combo_box_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)90 gimp_enum_combo_box_set_property (GObject      *object,
91                                   guint         prop_id,
92                                   const GValue *value,
93                                   GParamSpec   *pspec)
94 {
95   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
96 
97   switch (prop_id)
98     {
99     case PROP_MODEL:
100       gtk_combo_box_set_model (combo_box, g_value_get_object (value));
101       break;
102 
103     default:
104       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
105       break;
106     }
107 }
108 
109 static void
gimp_enum_combo_box_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)110 gimp_enum_combo_box_get_property (GObject    *object,
111                                   guint       prop_id,
112                                   GValue     *value,
113                                   GParamSpec *pspec)
114 {
115   GtkComboBox *combo_box = GTK_COMBO_BOX (object);
116 
117   switch (prop_id)
118     {
119     case PROP_MODEL:
120       g_value_set_object (value, gtk_combo_box_get_model (combo_box));
121       break;
122 
123     default:
124       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
125       break;
126     }
127 }
128 
129 
130 /**
131  * gimp_enum_combo_box_new:
132  * @enum_type: the #GType of an enum.
133  *
134  * Creates a #GtkComboBox readily filled with all enum values from a
135  * given @enum_type. The enum needs to be registered to the type
136  * system. It should also have %GimpEnumDesc descriptions registered
137  * that contain translatable value names. This is the case for the
138  * enums used in the GIMP PDB functions.
139  *
140  * This is just a convenience function. If you need more control over
141  * the enum values that appear in the combo_box, you can create your
142  * own #GimpEnumStore and use gimp_enum_combo_box_new_with_model().
143  *
144  * Return value: a new #GimpEnumComboBox.
145  *
146  * Since: 2.4
147  **/
148 GtkWidget *
gimp_enum_combo_box_new(GType enum_type)149 gimp_enum_combo_box_new (GType enum_type)
150 {
151   GtkListStore *store;
152   GtkWidget    *combo_box;
153 
154   g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), NULL);
155 
156   store = gimp_enum_store_new (enum_type);
157 
158   combo_box = g_object_new (GIMP_TYPE_ENUM_COMBO_BOX,
159                             "model", store,
160                             NULL);
161 
162   g_object_unref (store);
163 
164   return combo_box;
165 }
166 
167 /**
168  * gimp_enum_combo_box_new_with_model
169  * @enum_store: a #GimpEnumStore to use as the model
170  *
171  * Creates a #GtkComboBox for the given @enum_store.
172  *
173  * Return value: a new #GimpEnumComboBox.
174  *
175  * Since: 2.4
176  **/
177 GtkWidget *
gimp_enum_combo_box_new_with_model(GimpEnumStore * enum_store)178 gimp_enum_combo_box_new_with_model (GimpEnumStore *enum_store)
179 {
180   g_return_val_if_fail (GIMP_IS_ENUM_STORE (enum_store), NULL);
181 
182   return g_object_new (GIMP_TYPE_ENUM_COMBO_BOX,
183                        "model", enum_store,
184                        NULL);
185 }
186 
187 /**
188  * gimp_enum_combo_box_set_stock_prefix:
189  * @combo_box:    a #GimpEnumComboBox
190  * @stock_prefix: a prefix to create icon stock ID from enum values
191  *
192  * Attempts to create stock icons for all items in the @combo_box. See
193  * gimp_enum_store_set_stock_prefix() to find out what to use as
194  * @stock_prefix.
195  *
196  * Since: 2.4
197  *
198  * Deprecated: GIMP 2.10
199  **/
200 void
gimp_enum_combo_box_set_stock_prefix(GimpEnumComboBox * combo_box,const gchar * stock_prefix)201 gimp_enum_combo_box_set_stock_prefix (GimpEnumComboBox *combo_box,
202                                       const gchar      *stock_prefix)
203 {
204   gimp_enum_combo_box_set_icon_prefix (combo_box, stock_prefix);
205 }
206 
207 /**
208  * gimp_enum_combo_box_set_icon_prefix:
209  * @combo_box:   a #GimpEnumComboBox
210  * @icon_prefix: a prefix to create icon names from enum values
211  *
212  * Attempts to create icons for all items in the @combo_box. See
213  * gimp_enum_store_set_icon_prefix() to find out what to use as
214  * @icon_prefix.
215  *
216  * Since: 2.10
217  **/
218 void
gimp_enum_combo_box_set_icon_prefix(GimpEnumComboBox * combo_box,const gchar * icon_prefix)219 gimp_enum_combo_box_set_icon_prefix (GimpEnumComboBox *combo_box,
220                                      const gchar      *icon_prefix)
221 {
222   GtkTreeModel *model;
223 
224   g_return_if_fail (GIMP_IS_ENUM_COMBO_BOX (combo_box));
225 
226   model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
227 
228   gimp_enum_store_set_icon_prefix (GIMP_ENUM_STORE (model), icon_prefix);
229 }
230