File: gimpresourceselect.c

package info (click to toggle)
gimp 3.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 210,368 kB
  • sloc: ansic: 845,152; lisp: 10,855; python: 10,330; cpp: 7,238; perl: 4,382; sh: 1,412; xml: 963; yacc: 609; lex: 348; javascript: 150; makefile: 43
file content (401 lines) | stat: -rw-r--r-- 15,000 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * 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
 * Library 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 "gimp.h"
#include "gimpresourceselect-private.h"

typedef struct
{
  GType                        resource_type;
  GimpResourceChoosedCallback  callback;
  gpointer                     owner_data;
  GDestroyNotify               data_destroy;
} GimpResourceAdaption;


/*  local */

static void             gimp_resource_data_free               (GimpResourceAdaption *adaption);

static GimpValueArray * gimp_temp_resource_run                (GimpProcedure        *procedure,
                                                               GimpProcedureConfig  *config,
                                                               gpointer              run_data);
static gboolean         gimp_resource_select_remove_after_run (const gchar         *procedure_name);


/*  public */

/* Annotation, which appears in the libgimp API doc.
 * Not a class, only functions.
 * The functions appear as class methods of GimpResource class.
 * Formerly, API had separate functions for each resource subclass.
 */

/**
 * SECTION: gimpresourceselect
 * @title: GimpResourceSelect
 * @short_description: A resource selection dialog.
 *
 * A resource selection dialog.
 *
 * An adapter and proxy between libgimp and core.
 * (see Adapter and Proxy patterns in programming literature.)
 *
 * Proxy: to a remote dialog in core.
 * Is a dialog, but the dialog is remote (another process.)
 * Remote dialog is a chooser dialog of subclass of GimpResource,
 * e.g. GimpBrush, GimpFont, etc.
 *
 * Adapter: gets a callback via PDB procedure from remote dialog
 * and shuffles parameters to call a owner's callback on libgimp side.
 *
 * Generic on type of GimpResource subclass.
 * That is, the type of GimpResource subclass is passed.
 *
 * Responsibilities:
 *
 *    - implement a proxy  to a chooser widget in core
 *
 * Collaborations:
 *
 *     - called by GimpResourceSelectButton to popup as a sibling widget
 *     - PDB procedures to/from core, which implements the remote dialog
 *       (from via PDB temp callback, to via PDB procs such as gimp_fonts_popup)
 *     - plugins implementing their own GUI
 **/

/* This was extracted from gimpbrushselect.c, gimpfontselect.c, etc.
 * and those were deleted.
 */

/* Functions that dispatch on resource type.
 *
 * For now, the design is that core callbacks pass
 * attributes of the resource (not just the resource.)
 * FUTURE: core will use the same signature for all remote dialogs,
 * regardless of resource type.
 * Then all this dispatching code can be deleted.
 */


/* Create args for temp PDB callback.
 * Must match signature that core hardcodes in a subclass of gimp_pdb_dialog.
 *
 * For example, see app/widgets/paletteselect.c
 *
 * When testing, the error "Unable to run callback... temp_proc... wrong type"
 * means a signature mismatch.
 *
 * Note the signature from core might be from an old design where
 * the core sent all data needed to draw the resource.
 * In the new design, libgimp gets the attributes of the resource
 * from core in a separate PDB proc call back to core.
 * While core uses the old design and libgimp uses the new design,
 * libgimp simply ignores the extra args (adapts the signature.)
 */
static void
create_callback_PDB_procedure_params (GimpProcedure *procedure,
                                      GType          resource_type)
{
  gimp_procedure_add_resource_argument (procedure, "resource",
                                        "Resource", "The resource",
                                        TRUE,
                                        NULL, /* no default*/
                                        G_PARAM_READWRITE);

  /* Create args for the extra, superfluous args that core is passing.*/
  if (g_type_is_a (resource_type, GIMP_TYPE_FONT))
    {
      /* No other args. */
    }
  else if (g_type_is_a (resource_type, GIMP_TYPE_GRADIENT))
    {
      gimp_procedure_add_double_array_argument (procedure, "gradient-data",
                                                "Gradient data",
                                                "The gradient data",
                                                G_PARAM_READWRITE);
    }
  else if (g_type_is_a (resource_type, GIMP_TYPE_BRUSH))
    {
      gimp_procedure_add_int_argument (procedure, "mask-width",
                                       "Brush width",
                                       NULL,
                                       0, 10000, 0,
                                       G_PARAM_READWRITE);

      gimp_procedure_add_int_argument (procedure, "mask-height",
                                       "Brush height",
                                       NULL,
                                       0, 10000, 0,
                                       G_PARAM_READWRITE);

      gimp_procedure_add_bytes_argument (procedure, "mask-data",
                                         "Mask data",
                                         "The brush mask data",
                                         G_PARAM_READWRITE);
    }
  else if (g_type_is_a (resource_type, GIMP_TYPE_PALETTE))
    {
      gimp_procedure_add_int_argument (procedure, "num-colors",
                                       "Num colors",
                                       "Number of colors",
                                       0, G_MAXINT, 0,
                                       G_PARAM_READWRITE);
    }
  else if (g_type_is_a (resource_type, GIMP_TYPE_PATTERN))
    {
      gimp_procedure_add_int_argument (procedure, "mask-width",
                                       "Mask width",
                                       "Pattern width",
                                       0, 10000, 0,
                                       G_PARAM_READWRITE);

      gimp_procedure_add_int_argument (procedure, "mask-height",
                                       "Mask height",
                                       "Pattern height",
                                       0, 10000, 0,
                                       G_PARAM_READWRITE);

      gimp_procedure_add_int_argument (procedure, "mask-bpp",
                                       "Mask bpp",
                                       "Pattern bytes per pixel",
                                       0, 10000, 0,
                                       G_PARAM_READWRITE);

      gimp_procedure_add_bytes_argument (procedure, "mask-data",
                                         "Mask data",
                                         "The pattern mask data",
                                         G_PARAM_READWRITE);
    }
  else
    {
      g_warning ("%s: unhandled resource type", G_STRFUNC);
    }

  gimp_procedure_add_boolean_argument (procedure, "closing",
                                       "Closing",
                                       "If the dialog was closing",
                                       FALSE,
                                       G_PARAM_READWRITE);
}

/* Open (create) a remote chooser dialog of resource.
 *
 * Dispatch on subclass of GimpResource.
 * Call a PDB procedure that communicates with core to create remote dialog.
 */
static gboolean
popup_remote_chooser (const gchar   *title,
                      GBytes        *parent_handle,
                      GimpResource  *resource,
                      const gchar   *temp_PDB_callback_name,
                      GType          resource_type)
{
  gboolean result = FALSE;

  if (g_type_is_a (resource_type, GIMP_TYPE_BRUSH))
    {
      result = gimp_brushes_popup (temp_PDB_callback_name, title, GIMP_BRUSH (resource), parent_handle);
    }
  else if (g_type_is_a (resource_type, GIMP_TYPE_FONT))
    {
      result = gimp_fonts_popup (temp_PDB_callback_name, title, GIMP_FONT (resource), parent_handle);
    }
  else if (g_type_is_a (resource_type, GIMP_TYPE_GRADIENT))
    {
      result = gimp_gradients_popup (temp_PDB_callback_name, title, GIMP_GRADIENT (resource), parent_handle);
    }
  else if (g_type_is_a (resource_type, GIMP_TYPE_PALETTE))
    {
      result = gimp_palettes_popup (temp_PDB_callback_name, title, GIMP_PALETTE (resource), parent_handle);
    }
  else if (g_type_is_a (resource_type, GIMP_TYPE_PATTERN))
    {
      result = gimp_patterns_popup (temp_PDB_callback_name, title, GIMP_PATTERN (resource), parent_handle);
    }
  else
    {
      g_warning ("%s: unhandled resource type", G_STRFUNC);
    }

  return result;
}

/**
 * gimp_resource_select_new:
 * @title:                          Title of the resource selection dialog.
 * @resource:                       The resource to set as the initial choice.
 * @resource_type:                  The type of the subclass of [class@Resource].
 * @callback: (scope notified):     The callback function to call when the user chooses a resource.
 * @owner_data: (closure callback): The run_data given to @callback.
 * @data_destroy: (destroy owner_data): The destroy function for @owner_data.
 *
 * Invoke a resource chooser dialog which may call @callback with the chosen
 * @resource and @owner_data.
 *
 * A proxy to a remote dialog in core, which knows the installed resources.
 *
 * Note: this function is private and must not be used in plug-ins or
 * considered part of the API.
 *
 * Returns: (transfer none): the name of a temporary PDB procedure. The
 *          string belongs to the resource selection dialog and will be
 *          freed automatically when the dialog is closed.
 **/
const gchar *
gimp_resource_select_new (const gchar                 *title,
                          GBytes                      *parent_handle,
                          GimpResource                *resource,
                          GType                        resource_type,
                          GimpResourceChoosedCallback  callback,
                          gpointer                     owner_data,
                          GDestroyNotify               data_destroy)
{
  GimpPlugIn    *plug_in = gimp_get_plug_in ();
  GimpProcedure *procedure;
  gchar         *temp_PDB_callback_name;
  GimpResourceAdaption  *adaption;

  g_return_val_if_fail (resource != NULL, NULL);
  g_return_val_if_fail (callback != NULL, NULL);
  g_return_val_if_fail (g_type_is_a (resource_type, GIMP_TYPE_RESOURCE), NULL);

  temp_PDB_callback_name = gimp_pdb_temp_procedure_name (gimp_get_pdb ());

  adaption = g_slice_new0 (GimpResourceAdaption);

  adaption->callback      = callback;
  adaption->owner_data    = owner_data;
  adaption->data_destroy  = data_destroy;
  adaption->resource_type = resource_type;

  procedure = gimp_procedure_new (plug_in,
                                  temp_PDB_callback_name,
                                  GIMP_PDB_PROC_TYPE_TEMPORARY,
                                  gimp_temp_resource_run,
                                  adaption,
                                  (GDestroyNotify) gimp_resource_data_free);

  create_callback_PDB_procedure_params (procedure, resource_type);

  gimp_plug_in_add_temp_procedure (plug_in, procedure);
  g_object_unref (procedure);
  g_free (temp_PDB_callback_name);

  if (popup_remote_chooser (title, parent_handle, resource, gimp_procedure_get_name (procedure), resource_type))
    {
      /* Allow callbacks to be watched */
      gimp_plug_in_persistent_enable (plug_in);

      return gimp_procedure_get_name (procedure);
    }
  else
    {
      g_warning ("Failed to open remote resource select dialog.");
      gimp_plug_in_remove_temp_procedure (plug_in, temp_PDB_callback_name);
      return NULL;
    }
}


/* gimp_resource_select_set:
 * @callback_name: a callback name as returned by [func@resource_select_new].
 * @resource:      a [class@Resource] of type @resource_type.
 *
 * Set currently selected resource in remote chooser.
 *
 * Note: this function is private and must not be used in plug-ins or
 * considered part of the API.
 */
void
gimp_resource_select_set (const gchar  *callback_name,
                          GimpResource *resource)
{
  GType resource_type;

  g_return_if_fail (resource != NULL);

  resource_type = G_TYPE_FROM_INSTANCE (resource);
  g_return_if_fail (g_type_is_a (resource_type, GIMP_TYPE_RESOURCE));

  if (g_type_is_a (resource_type, GIMP_TYPE_FONT))
    gimp_fonts_set_popup (callback_name, GIMP_FONT (resource));
  else if (g_type_is_a (resource_type, GIMP_TYPE_GRADIENT))
    gimp_gradients_set_popup (callback_name, GIMP_GRADIENT (resource));
  else if (g_type_is_a (resource_type, GIMP_TYPE_BRUSH))
    gimp_brushes_set_popup (callback_name, GIMP_BRUSH (resource));
  else if (g_type_is_a (resource_type, GIMP_TYPE_PALETTE))
    gimp_palettes_set_popup (callback_name, GIMP_PALETTE (resource));
  else if (g_type_is_a (resource_type, GIMP_TYPE_PATTERN))
    gimp_patterns_set_popup (callback_name, GIMP_PATTERN (resource));
  else
    g_return_if_reached ();
}

/*  private functions  */

static void
gimp_resource_data_free (GimpResourceAdaption *adaption)
{
  if (adaption->data_destroy)
    adaption->data_destroy (adaption->owner_data);

  g_slice_free (GimpResourceAdaption, adaption);
}

/* Run func for the temporary PDB procedure.
 * Called when user chooses a resource in remote dialog.
 */
static GimpValueArray *
gimp_temp_resource_run (GimpProcedure       *procedure,
                        GimpProcedureConfig *config,
                        gpointer             run_data)
{
  GimpResourceAdaption *adaption = run_data;
  GimpResource         *resource;
  gboolean              closing;

  g_object_get (config,
                "resource", &resource,
                "closing",  &closing,
                NULL);

  if (adaption->callback)
    adaption->callback (resource, closing,
                        adaption->owner_data);

  if (closing)
    g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
                     (GSourceFunc) gimp_resource_select_remove_after_run,
                     g_strdup (gimp_procedure_get_name (procedure)),
                     g_free);

  g_object_unref (resource);

  return gimp_procedure_new_return_values (procedure, GIMP_PDB_SUCCESS, NULL);
}

static gboolean
gimp_resource_select_remove_after_run (const gchar *procedure_name)
{
  gimp_plug_in_remove_temp_procedure (gimp_get_plug_in (), procedure_name);

  return G_SOURCE_REMOVE;
}