File: gimppropwidgets.c

package info (click to toggle)
gimp 3.0.4-6.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 210,548 kB
  • sloc: ansic: 842,405; lisp: 10,761; python: 10,318; cpp: 7,238; perl: 4,355; sh: 1,043; xml: 963; yacc: 609; lex: 348; javascript: 150; makefile: 43
file content (273 lines) | stat: -rw-r--r-- 9,746 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimppropwidgets.c
 * Copyright (C) 2023 Jehan
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "libgimp/gimpui.h"

#include "libgimp-intl.h"

/*
 * This is a complement of libgimpwidgets/gimppropwidgets.c
 * These are property functions for types from libgimp, such as
 * [class@Gimp.Resource] or [class@Gimp.Item] subclasses.
 */


typedef GtkWidget* (*GimpResourceWidgetCreator) (const gchar  *title,
                                                 const gchar  *label,
                                                 GimpResource *initial_resource);


/*  utility function prototypes  */

static GtkWidget  * gimp_prop_resource_chooser_factory   (GimpResourceWidgetCreator  widget_creator_func,
                                                          GObject                   *config,
                                                          const gchar               *property_name,
                                                          const gchar               *chooser_title);
static gchar      * gimp_utils_make_canonical_menu_label (const gchar               *path);


/**
 * gimp_prop_brush_chooser_new:
 * @config:        Object to which property is attached.
 * @property_name: Name of a [class@Gimp.Brush] property.
 * @chooser_title: (nullable): title for the poppable dialog.
 *
 * Creates a [class@GimpUi.BrushChooser] controlled by the specified property.
 *
 * Returns: (transfer full): A new [class@GimpUi.BrushChooser].
 *
 * Since: 3.0
 */
GtkWidget *
gimp_prop_brush_chooser_new (GObject     *config,
                             const gchar *property_name,
                             const gchar *chooser_title)
{
  return gimp_prop_resource_chooser_factory ((GimpResourceWidgetCreator) gimp_brush_chooser_new,
                                             config, property_name, chooser_title);
}

/**
 * gimp_prop_font_chooser_new:
 * @config:        Object to which property is attached.
 * @property_name: Name of a [class@Gimp.Font] property.
 * @chooser_title: (nullable): title for the poppable dialog.
 *
 * Creates a [class@GimpUi.FontChooser] controlled by the specified property.
 *
 * Returns: (transfer full): A new [class@GimpUi.FontChooser].
 *
 * Since: 3.0
 */
GtkWidget *
gimp_prop_font_chooser_new (GObject     *config,
                            const gchar *property_name,
                            const gchar *chooser_title)
{
  return gimp_prop_resource_chooser_factory ((GimpResourceWidgetCreator) gimp_font_chooser_new,
                                             config, property_name, chooser_title);
}

/**
 * gimp_prop_gradient_chooser_new:
 * @config:        Object to which property is attached.
 * @property_name: Name of a [class@Gimp.Gradient] property.
 * @chooser_title: (nullable): title for the poppable dialog.
 *
 * Creates a [class@GimpUi.GradientChooser] controlled by the specified property.
 *
 * Returns: (transfer full): A new [class@GimpUi.GradientChooser].
 *
 * Since: 3.0
 */
GtkWidget *
gimp_prop_gradient_chooser_new (GObject     *config,
                                const gchar *property_name,
                                const gchar *chooser_title)
{
  return gimp_prop_resource_chooser_factory ((GimpResourceWidgetCreator) gimp_gradient_chooser_new,
                                             config, property_name, chooser_title);
}

/**
 * gimp_prop_palette_chooser_new:
 * @config:        Object to which property is attached.
 * @property_name: Name of a [class@Gimp.Palette] property.
 * @chooser_title: (nullable): title for the poppable dialog.
 *
 * Creates a [class@GimpUi.PaletteChooser] controlled by the specified property.
 *
 * Returns: (transfer full): A new [class@GimpUi.PaletteChooser].
 *
 * Since: 3.0
 */
GtkWidget *
gimp_prop_palette_chooser_new (GObject     *config,
                               const gchar *property_name,
                               const gchar *chooser_title)
{
  return gimp_prop_resource_chooser_factory ((GimpResourceWidgetCreator) gimp_palette_chooser_new,
                                             config, property_name, chooser_title);
}

/**
 * gimp_prop_pattern_chooser_new:
 * @config:        Object to which property is attached.
 * @property_name: Name of a [class@Gimp.Pattern] property.
 * @chooser_title: (nullable): title for the poppable dialog.
 *
 * Creates a [class@GimpUi.PatternChooser] controlled by the specified property.
 *
 * Returns: (transfer full): A new [class@GimpUi.PatternChooser].
 *
 * Since: 3.0
 */
GtkWidget *
gimp_prop_pattern_chooser_new (GObject     *config,
                               const gchar *property_name,
                               const gchar *chooser_title)
{
  return gimp_prop_resource_chooser_factory ((GimpResourceWidgetCreator) gimp_pattern_chooser_new,
                                             config, property_name, chooser_title);
}

/**
 * gimp_prop_drawable_chooser_new:
 * @config:        Object to which property is attached.
 * @property_name: Name of a [class@Gimp.Drawable] property.
 * @chooser_title: (nullable): title for the poppable dialog.
 *
 * Creates a [class@GimpUi.DrawableChooser] controlled by the specified property.
 *
 * Returns: (transfer full): A new [class@GimpUi.DrawableChooser].
 *
 * Since: 3.0
 */
GtkWidget *
gimp_prop_drawable_chooser_new (GObject     *config,
                                const gchar *property_name,
                                const gchar *chooser_title)
{
  GParamSpec   *param_spec;
  GtkWidget    *prop_chooser;
  GimpDrawable *initial_drawable = NULL;
  gchar        *title            = NULL;
  const gchar  *label;

  param_spec = g_object_class_find_property (G_OBJECT_GET_CLASS (config),
                                             property_name);

  g_return_val_if_fail (param_spec != NULL, NULL);
  g_return_val_if_fail (g_type_is_a (G_TYPE_FROM_INSTANCE (param_spec), G_TYPE_PARAM_OBJECT) &&
                        g_type_is_a (param_spec->value_type, GIMP_TYPE_DRAWABLE), NULL);

  g_object_get (config,
                property_name, &initial_drawable,
                NULL);

  label = g_param_spec_get_nick (param_spec);

  if (chooser_title == NULL)
    {
      gchar *canonical;

      canonical = gimp_utils_make_canonical_menu_label (label);
      if (g_type_is_a (param_spec->value_type, GIMP_TYPE_LAYER))
        title = g_strdup_printf (_("Choose layer: %s"), canonical);
      if (g_type_is_a (param_spec->value_type, GIMP_TYPE_CHANNEL))
        title = g_strdup_printf (_("Choose channel: %s"), canonical);
      else
        title = g_strdup_printf (_("Choose drawable: %s"), canonical);
      g_free (canonical);
    }
  else
    {
      title = g_strdup (chooser_title);
    }

  prop_chooser = gimp_drawable_chooser_new (title, label, param_spec->value_type, initial_drawable);
  g_clear_object (&initial_drawable);
  g_free (title);

  g_object_bind_property (prop_chooser, "drawable",
                          config,       property_name,
                          G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);

  return prop_chooser;
}

/*******************************/
/*  private utility functions  */
/*******************************/

static GtkWidget *
gimp_prop_resource_chooser_factory (GimpResourceWidgetCreator  widget_creator_func,
                                    GObject                   *config,
                                    const gchar               *property_name,
                                    const gchar               *chooser_title)
{
  GParamSpec   *param_spec;
  GtkWidget    *prop_chooser;
  GimpResource *initial_resource;
  const gchar  *label;

  param_spec = g_object_class_find_property (G_OBJECT_GET_CLASS (config),
                                             property_name);

  g_return_val_if_fail (param_spec != NULL, NULL);
  g_return_val_if_fail (g_type_is_a (G_TYPE_FROM_INSTANCE (param_spec), G_TYPE_PARAM_OBJECT) &&
                        g_type_is_a (param_spec->value_type, GIMP_TYPE_RESOURCE), NULL);

  g_object_get (config,
                property_name, &initial_resource,
                NULL);

  label = g_param_spec_get_nick (param_spec);

  /* Create the wrapped widget. For example, call gimp_font_chooser_new.
   * When initial_resource is NULL, the widget creator will set it's resource
   * property from context.
   */
  prop_chooser = widget_creator_func (chooser_title, label, initial_resource);
  g_clear_object (&initial_resource);

  g_object_bind_property (prop_chooser, "resource",
                          config,       property_name,
                          G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);

  return prop_chooser;
}

/* This is a copy of the similarly-named function in app/widgets/gimpwidgets-utils.c
 * I hesitated to put this maybe in libgimpwidgets/gimpwidgetsutils.h but for
 * now, let's not. If it's useful to more people, it's always easier to move the
 * function in rather than deprecating it.
 */
static gchar *
gimp_utils_make_canonical_menu_label (const gchar *path)
{
  gchar **split_path;
  gchar  *canon_path;

  /* The first underscore of each path item is a mnemonic. */
  split_path = g_strsplit (path, "_", 2);
  canon_path = g_strjoinv ("", split_path);
  g_strfreev (split_path);

  return canon_path;
}