File: script-fu-widgets-custom.c

package info (click to toggle)
gimp 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 222,880 kB
  • sloc: ansic: 870,914; python: 10,965; lisp: 10,857; cpp: 7,355; perl: 4,536; sh: 1,753; xml: 972; yacc: 609; lex: 348; javascript: 150; makefile: 42
file content (206 lines) | stat: -rw-r--r-- 6,860 bytes parent folder | download | duplicates (3)
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
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * Copyright (C) 2024 Lloyd Konneker
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <libgimp/gimpui.h>

#include "script-fu-types.h"
#include "script-fu-widgets-custom.h"

/* Widgets in GimpProcedureDialog *custom* to ScriptFu.
 *
 * GimpProcedureDialog provides stock widgets
 * based on the type of a procedure argument.
 * A custom widget is the same kind of widget as stock, but specialized.
 *
 * Both stock and custom widgets are keyed to a property name
 * of the GimpProcedure for the script.
 * You add custom widgets, then call gimp_procedure_dialog_fill_list()
 * which adds a stock widget for any property name not having a custom widget.
 *
 * FUTURE: implement these in GimpProcedureDialog.
 * That can't be done now because GParamSpecs are limited
 * in the types they can describe.
 * E.G. SF-OPTION describes an enumerated type
 * that can't be described completely in a GParamSpec.
 * E.G. property type GFile is a general type
 * whose subtypes: existing-file, existing-dir, or file-to-save
 * can't be described in a GParamSpec.
 *
 * Most SFArg types are supported by stock widgets.
 *
 * SF-FILENAME is stock.
 * For property of type G_TYPE_FILE.
 * A GtkFileChooser in the mode for user to open a existing file.
 *
 * SF-DIRNAME is custom.
 * Also a GtkFileChooser but in mode for user to choose a directory.
 *
 * FUTURE:
 * SF-SAVE-FILENAME is custom.
 * Also a GtkFileChooser but in mode for user to save a file
 * (choose a file to replace, or enter filename and choose a directory.)
 *
 * SF-OPTION is custom.
 * Is an int combo box widget, for an enumeration defined by the script
 * (versus SF-ENUM for enums defined by GIMP.)
 * Note SF-OPTION does not create symbols in the interpreter.
 * The script itself, and all other plugins, must use integer literals
 * to denote values.
 *
 * SF-ADJUSTMENT:SF-SLIDER is custom.
 * Default widget for propety of type DOUBLE is an entry w/ spinner.
 * Override with a slider w/ spinner.
 */

/* Does SFArg type need custom widget? */
static gboolean
sf_arg_type_is_custom (SFScript *script,
                       guint     arg_index)
{
  gboolean result=FALSE;

  switch (script->args[arg_index].type)
    {
    case SF_OPTION:
      result = TRUE;
      break;
    case SF_ADJUSTMENT:
      if (script->args[arg_index].default_value.sfa_adjustment.type == SF_SLIDER)
        result = TRUE;
      break;
    default:
      result = FALSE;
    }
  return result;
}

/* Returns new GtkListStore from SFArg declaration.
 * Transfers ownership of the allocated, returned store.
 * Ownership transfers to a dialog, which frees it on dialog destroy.
 *
 * Returned value is never NULL.
 * Returned store can be empty if the declaration of the arg is non-sensical.
 */
static GtkListStore *
sf_widget_custom_new_int_store (SFArg *arg)
{
  GtkListStore *result;
  GSList       *list;
  guint         counter = 0; /* SF enumerations start at 0. */

  /* Create empty store. */
  result = g_object_new (GIMP_TYPE_INT_STORE, NULL);

  /* Iterate over list of names of enumerated values,
   * appending each to store having value equal to the iteration index.
   *
   * ScriptFu does NOT define constant Scheme symbols in the interpreter;
   * the names of enumerated values are not available to the script,
   * which must use integer literals or (define other symbols for the values.)
   */
  for (list = arg->default_value.sfa_option.list;
       list;
       list = g_slist_next (list))
    {
      GtkTreeIter iter;

      gtk_list_store_append (result, &iter);
      gtk_list_store_set (result, &iter,
                          GIMP_INT_STORE_VALUE, counter,
                          GIMP_INT_STORE_LABEL, list->data,
                          -1);
      counter++;
    }

  return result;
}

/* Adds widget for arg of type SF-OPTION to the dialog.
 * Widget is a combobox widget.
 * Specializes the widget by a custom store i.e. model
 * derived from the arg's declaration.
 */
static void
sf_widget_custom_option (GimpProcedureDialog *dialog,
                         SFArg               *arg)
{
  GtkListStore *store;

  store = sf_widget_custom_new_int_store (arg);
  gimp_procedure_dialog_get_int_combo (dialog,
                                       arg->property_name,
                                       GIMP_INT_STORE (store));
}

/* Adds widget for arg of type SF-ADJUSTMENT:SF_SLIDER to the dialog.
 * Specializes the widget: slider instead of entry, and a spinner
 */
static void
sf_widget_custom_slider (GimpProcedureDialog *dialog,
                         SFArg               *arg)
{
  /* Widget belongs to dialog, discard the returned ref. */
  (void) gimp_procedure_dialog_get_widget (dialog,
                                           arg->property_name,
                                           GIMP_TYPE_SCALE_ENTRY);
}

/* Add a custom widget for a script's arg to the script's dialog.
 * Does nothing when the type of the arg does not need a custom widget.
 *
 * Widget is associated with the arg's property, by name of the property.
 * The arg's property is a property of the Procedure.
 * The widget will be positioned in the dialog by order of property.
 */
static void
sf_widget_custom_add_to_dialog (GimpProcedureDialog *dialog,
                                SFArg               *arg)
{
  /* Handles same cases as sf_arg_type_is_custom() */
  switch (arg->type)
    {
    case SF_OPTION:
      sf_widget_custom_option (dialog, arg);
      break;
    case SF_ADJUSTMENT:
      /* We already know sfa_adjustment.type == SF_SLIDER. */
      sf_widget_custom_slider (dialog, arg);
      break;
    default:
      g_warning ("%s Unhandled custom widget type.", G_STRFUNC);
      break;
    }
}


/* Add custom widgets to dialog, for certain type of args of script. */
void
script_fu_widgets_custom_add (GimpProcedureDialog *dialog,
                              SFScript            *script)
{
  for (int arg_index=0; arg_index < script->n_args; arg_index++)
    {
      if (sf_arg_type_is_custom (script, arg_index))
        {
          sf_widget_custom_add_to_dialog (dialog, &script->args[arg_index]);
        }
    }
}