summaryrefslogtreecommitdiffstats
path: root/app/tools/gimpgradientoptions.c
blob: 86fdd2573097d8f19e4697b463f7385d1a3290b7 (plain)
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
402
403
404
405
406
407
408
409
410
411
412
413
414
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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 <gegl.h>
#include <gtk/gtk.h>

#include "libgimpconfig/gimpconfig.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "tools-types.h"

#include "core/gimpdata.h"
#include "core/gimpdatafactory.h"
#include "core/gimp-gradients.h"

#include "widgets/gimppropwidgets.h"
#include "widgets/gimpviewablebox.h"
#include "widgets/gimpwidgets-utils.h"

#include "gimpgradientoptions.h"
#include "gimppaintoptions-gui.h"

#include "gimp-intl.h"


enum
{
  PROP_0,
  PROP_OFFSET,
  PROP_GRADIENT_TYPE,
  PROP_DISTANCE_METRIC,
  PROP_SUPERSAMPLE,
  PROP_SUPERSAMPLE_DEPTH,
  PROP_SUPERSAMPLE_THRESHOLD,
  PROP_DITHER,
  PROP_INSTANT,
  PROP_MODIFY_ACTIVE
};


static void   gimp_gradient_options_set_property           (GObject             *object,
                                                            guint                property_id,
                                                            const GValue        *value,
                                                            GParamSpec          *pspec);
static void   gimp_gradient_options_get_property           (GObject             *object,
                                                            guint                property_id,
                                                            GValue              *value,
                                                            GParamSpec          *pspec);

static void   gradient_options_repeat_gradient_type_notify (GimpGradientOptions *options,
                                                            GParamSpec          *pspec,
                                                            GtkWidget           *repeat_combo);
static void   gradient_options_metric_gradient_type_notify (GimpGradientOptions *options,
                                                            GParamSpec          *pspec,
                                                            GtkWidget           *repeat_combo);


G_DEFINE_TYPE (GimpGradientOptions, gimp_gradient_options,
               GIMP_TYPE_PAINT_OPTIONS)


static void
gimp_gradient_options_class_init (GimpGradientOptionsClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = gimp_gradient_options_set_property;
  object_class->get_property = gimp_gradient_options_get_property;

  GIMP_CONFIG_PROP_DOUBLE (object_class, PROP_OFFSET,
                           "offset",
                           _("Offset"),
                           NULL,
                           0.0, 100.0, 0.0,
                           GIMP_PARAM_STATIC_STRINGS);
  GIMP_CONFIG_PROP_ENUM (object_class, PROP_GRADIENT_TYPE,
                         "gradient-type",
                         _("Shape"),
                         NULL,
                         GIMP_TYPE_GRADIENT_TYPE,
                         GIMP_GRADIENT_LINEAR,
                         GIMP_PARAM_STATIC_STRINGS);
  GIMP_CONFIG_PROP_ENUM (object_class, PROP_DISTANCE_METRIC,
                         "distance-metric",
                         _("Metric"),
                         _("Metric to use for the distance calculation"),
                         GEGL_TYPE_DISTANCE_METRIC,
                         GEGL_DISTANCE_METRIC_EUCLIDEAN,
                         GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_SUPERSAMPLE,
                            "supersample",
                            _("Adaptive Supersampling"),
                            NULL,
                            FALSE,
                            GIMP_PARAM_STATIC_STRINGS);
  GIMP_CONFIG_PROP_INT (object_class, PROP_SUPERSAMPLE_DEPTH,
                        "supersample-depth",
                        _("Max depth"),
                        NULL,
                        1, 9, 3,
                        GIMP_PARAM_STATIC_STRINGS);
  GIMP_CONFIG_PROP_DOUBLE (object_class, PROP_SUPERSAMPLE_THRESHOLD,
                           "supersample-threshold",
                           _("Threshold"),
                           NULL,
                           0.0, 4.0, 0.2,
                           GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_DITHER,
                            "dither",
                            _("Dithering"),
                            NULL,
                            TRUE,
                            GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_INSTANT,
                            "instant",
                            _("Instant mode"),
                            _("Commit gradient instantly"),
                            FALSE,
                            GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_MODIFY_ACTIVE,
                            "modify-active",
                            _("Modify active gradient"),
                            _("Modify the active gradient in-place"),
                            FALSE,
                            GIMP_PARAM_STATIC_STRINGS);
}

static void
gimp_gradient_options_init (GimpGradientOptions *options)
{
}

static void
gimp_gradient_options_set_property (GObject      *object,
                                    guint         property_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
{
  GimpGradientOptions *options = GIMP_GRADIENT_OPTIONS (object);

  switch (property_id)
    {
    case PROP_OFFSET:
      options->offset = g_value_get_double (value);
      break;
    case PROP_GRADIENT_TYPE:
      options->gradient_type = g_value_get_enum (value);
      break;
    case PROP_DISTANCE_METRIC:
      options->distance_metric = g_value_get_enum (value);
      break;

    case PROP_SUPERSAMPLE:
      options->supersample = g_value_get_boolean (value);
      break;
    case PROP_SUPERSAMPLE_DEPTH:
      options->supersample_depth = g_value_get_int (value);
      break;
    case PROP_SUPERSAMPLE_THRESHOLD:
      options->supersample_threshold = g_value_get_double (value);
      break;

    case PROP_DITHER:
      options->dither = g_value_get_boolean (value);
      break;

    case PROP_INSTANT:
      options->instant = g_value_get_boolean (value);
      break;
    case PROP_MODIFY_ACTIVE:
      options->modify_active = g_value_get_boolean (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_gradient_options_get_property (GObject    *object,
                                    guint       property_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
{
  GimpGradientOptions *options = GIMP_GRADIENT_OPTIONS (object);

  switch (property_id)
    {
    case PROP_OFFSET:
      g_value_set_double (value, options->offset);
      break;
    case PROP_GRADIENT_TYPE:
      g_value_set_enum (value, options->gradient_type);
      break;
    case PROP_DISTANCE_METRIC:
      g_value_set_enum (value, options->distance_metric);
      break;

    case PROP_SUPERSAMPLE:
      g_value_set_boolean (value, options->supersample);
      break;
    case PROP_SUPERSAMPLE_DEPTH:
      g_value_set_int (value, options->supersample_depth);
      break;
    case PROP_SUPERSAMPLE_THRESHOLD:
      g_value_set_double (value, options->supersample_threshold);
      break;

    case PROP_DITHER:
      g_value_set_boolean (value, options->dither);
      break;

    case PROP_INSTANT:
      g_value_set_boolean (value, options->instant);
      break;
    case PROP_MODIFY_ACTIVE:
      g_value_set_boolean (value, options->modify_active);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

GtkWidget *
gimp_gradient_options_gui (GimpToolOptions *tool_options)
{
  GObject             *config  = G_OBJECT (tool_options);
  GimpContext         *context = GIMP_CONTEXT (tool_options);
  GimpGradientOptions *options = GIMP_GRADIENT_OPTIONS (tool_options);
  GtkWidget           *vbox    = gimp_paint_options_gui (tool_options);
  GtkWidget           *vbox2;
  GtkWidget           *frame;
  GtkWidget           *scale;
  GtkWidget           *combo;
  GtkWidget           *button;
  GtkWidget           *label;
  gchar               *str;
  GdkModifierType      extend_mask;
  GimpGradient        *gradient;

  extend_mask = gimp_get_extend_selection_mask ();

  /*  the gradient  */
  button = gimp_prop_gradient_box_new (NULL, GIMP_CONTEXT (tool_options),
                                       _("Gradient"), 2,
                                       "gradient-view-type",
                                       "gradient-view-size",
                                       "gradient-reverse",
                                       "gradient-blend-color-space",
                                       "gimp-gradient-editor",
                                       _("Edit this gradient"));
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
  gtk_widget_show (button);

  /*  the blend color space  */
  combo = gimp_prop_enum_combo_box_new (config, "gradient-blend-color-space",
                                        0, 0);
  gimp_int_combo_box_set_label (GIMP_INT_COMBO_BOX (combo),
                                _("Blend Color Space"));
  g_object_set (combo, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
  gtk_box_pack_start (GTK_BOX (vbox), combo, TRUE, TRUE, 0);
  gtk_widget_show (combo);

  /*  the gradient type menu  */
  combo = gimp_prop_enum_combo_box_new (config, "gradient-type", 0, 0);
  gimp_int_combo_box_set_label (GIMP_INT_COMBO_BOX (combo), _("Shape"));
  g_object_set (combo, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
  gimp_enum_combo_box_set_icon_prefix (GIMP_ENUM_COMBO_BOX (combo),
                                       "gimp-gradient");
  gtk_box_pack_start (GTK_BOX (vbox), combo, FALSE, FALSE, 0);
  gtk_widget_show (combo);

  /*  the distance metric menu  */
  combo = gimp_prop_enum_combo_box_new (config, "distance-metric", 0, 0);
  gimp_int_combo_box_set_label (GIMP_INT_COMBO_BOX (combo), _("Metric"));
  g_object_set (combo, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
  gtk_box_pack_start (GTK_BOX (vbox), combo, FALSE, FALSE, 0);
  gtk_widget_show (combo);

  g_signal_connect (config, "notify::gradient-type",
                    G_CALLBACK (gradient_options_metric_gradient_type_notify),
                    combo);
  gradient_options_metric_gradient_type_notify (options, NULL, combo);

  /*  the repeat option  */
  combo = gimp_prop_enum_combo_box_new (config, "gradient-repeat", 0, 0);
  gimp_int_combo_box_set_label (GIMP_INT_COMBO_BOX (combo), _("Repeat"));
  g_object_set (combo, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
  gtk_box_pack_start (GTK_BOX (vbox), combo, FALSE, FALSE, 0);
  gtk_widget_show (combo);

  g_signal_connect (config, "notify::gradient-type",
                    G_CALLBACK (gradient_options_repeat_gradient_type_notify),
                    combo);
  gradient_options_repeat_gradient_type_notify (options, NULL, combo);

  /*  the offset scale  */
  scale = gimp_prop_spin_scale_new (config, "offset", NULL,
                                    1.0, 10.0, 1);
  gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, FALSE, 0);
  gtk_widget_show (scale);

  /*  the dither toggle  */
  button = gimp_prop_check_button_new (config, "dither", NULL);
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
  gtk_widget_show (button);

  /*  supersampling options  */
  vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
  frame = gimp_prop_expanding_frame_new (config, "supersample", NULL,
                                         vbox2, NULL);
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  /*  max depth scale  */
  scale = gimp_prop_spin_scale_new (config, "supersample-depth", NULL,
                                    1.0, 1.0, 0);
  gtk_box_pack_start (GTK_BOX (vbox2), scale, FALSE, FALSE, 0);
  gtk_widget_show (scale);

  /*  threshold scale  */
  scale = gimp_prop_spin_scale_new (config, "supersample-threshold", NULL,
                                    0.01, 0.1, 2);
  gtk_box_pack_start (GTK_BOX (vbox2), scale, FALSE, FALSE, 0);
  gtk_widget_show (scale);

  /* the instant toggle */
  str = g_strdup_printf (_("Instant mode  (%s)"),
                          gimp_get_mod_string (extend_mask));

  button = gimp_prop_check_button_new (config, "instant", str);
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
  gtk_widget_show (button);

  g_free (str);

  options->instant_toggle = button;

  /*  the modify active toggle  */
  vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
  frame = gimp_prop_expanding_frame_new (config, "modify-active", NULL,
                                         vbox2, NULL);
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  options->modify_active_frame = frame;

  label = gtk_label_new (_("The active gradient is non-writable "
                           "and cannot be edited directly. "
                           "Uncheck this option "
                           "to edit a copy of it."));
  gtk_box_pack_start (GTK_BOX (vbox2), label, TRUE, TRUE, 0);
  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
  gtk_label_set_width_chars (GTK_LABEL (label), 24);
  gtk_label_set_xalign (GTK_LABEL (label), 0.0);
  gimp_label_set_attributes (GTK_LABEL (label),
                             PANGO_ATTR_STYLE, PANGO_STYLE_ITALIC,
                             -1);

  options->modify_active_hint = label;

  gradient = gimp_context_get_gradient (GIMP_CONTEXT (options));

  gtk_widget_set_sensitive (options->modify_active_frame,
                            gradient !=
                            gimp_gradients_get_custom (context->gimp));
  gtk_widget_set_visible (options->modify_active_hint,
                          gradient &&
                          ! gimp_data_is_writable (GIMP_DATA (gradient)));

  return vbox;
}

static void
gradient_options_repeat_gradient_type_notify (GimpGradientOptions *options,
                                              GParamSpec       *pspec,
                                              GtkWidget        *repeat_combo)
{
  gtk_widget_set_sensitive (repeat_combo,
                            options->gradient_type < GIMP_GRADIENT_SHAPEBURST_ANGULAR);
}

static void
gradient_options_metric_gradient_type_notify (GimpGradientOptions *options,
                                              GParamSpec       *pspec,
                                              GtkWidget        *repeat_combo)
{
  gtk_widget_set_sensitive (repeat_combo,
                            options->gradient_type >= GIMP_GRADIENT_SHAPEBURST_ANGULAR &&
                            options->gradient_type <= GIMP_GRADIENT_SHAPEBURST_DIMPLED);
}