summaryrefslogtreecommitdiffstats
path: root/app/tools/gimp-tool-options-manager.c
blob: a466d65a1ae637ca02f15b50e7e53fbfb6c7308e (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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimp-tool-options-manager.c
 * Copyright (C) 2018 Michael Natterer <mitch@gimp.org>
 *
 * 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 "tools-types.h"

#include "config/gimpcoreconfig.h"

#include "core/gimp.h"
#include "core/gimpcontext.h"
#include "core/gimptoolinfo.h"

#include "paint/gimppaintoptions.h"

#include "widgets/gimpwidgets-utils.h"

#include "gimp-tool-options-manager.h"


typedef struct _GimpToolOptionsManager GimpToolOptionsManager;

struct _GimpToolOptionsManager
{
  Gimp                *gimp;
  GimpPaintOptions    *global_paint_options;
  GimpContextPropMask  global_props;

  GimpToolInfo        *active_tool;
};


static GQuark manager_quark = 0;


/*  local function prototypes  */

static GimpContextPropMask
              tool_options_manager_get_global_props
                                                 (GimpCoreConfig         *config);

static void   tool_options_manager_global_notify (GimpCoreConfig         *config,
                                                  const GParamSpec       *pspec,
                                                  GimpToolOptionsManager *manager);
static void   tool_options_manager_paint_options_notify
                                                 (GimpPaintOptions       *src,
                                                  const GParamSpec       *pspec,
                                                  GimpPaintOptions       *dest);

static void   tool_options_manager_copy_paint_props
                                                 (GimpPaintOptions       *src,
                                                  GimpPaintOptions       *dest,
                                                  GimpContextPropMask     prop_mask);

static void   tool_options_manager_tool_changed  (GimpContext            *user_context,
                                                  GimpToolInfo           *tool_info,
                                                  GimpToolOptionsManager *manager);


/*  public functions  */

void
gimp_tool_options_manager_init (Gimp *gimp)
{
  GimpToolOptionsManager *manager;
  GimpContext            *user_context;
  GimpCoreConfig         *config;
  GList                  *list;

  g_return_if_fail (GIMP_IS_GIMP (gimp));
  g_return_if_fail (manager_quark == 0);

  manager_quark = g_quark_from_static_string ("gimp-tool-options-manager");

  config = gimp->config;

  manager = g_slice_new0 (GimpToolOptionsManager);

  manager->gimp = gimp;

  manager->global_paint_options =
    g_object_new (GIMP_TYPE_PAINT_OPTIONS,
                  "gimp", gimp,
                  "name", "tool-options-manager-global-paint-options",
                  NULL);

  manager->global_props = tool_options_manager_get_global_props (config);

  g_object_set_qdata (G_OBJECT (gimp), manager_quark, manager);

  user_context = gimp_get_user_context (gimp);

  for (list = gimp_get_tool_info_iter (gimp);
       list;
       list = g_list_next (list))
    {
      GimpToolInfo *tool_info = list->data;

      /*  the global props that are actually used by the tool are
       *  always shared with the user context by undefining them...
       */
      gimp_context_define_properties (GIMP_CONTEXT (tool_info->tool_options),
                                      manager->global_props &
                                      tool_info->context_props,
                                      FALSE);

      /*  ...and setting the user context as parent
       */
      gimp_context_set_parent (GIMP_CONTEXT (tool_info->tool_options),
                               user_context);

      /*  make sure paint tools also share their brush, dynamics,
       *  gradient properties if the resp. context properties are
       *  global
       */
      if (GIMP_IS_PAINT_OPTIONS (tool_info->tool_options))
        {
          g_signal_connect (tool_info->tool_options, "notify",
                            G_CALLBACK (tool_options_manager_paint_options_notify),
                            manager->global_paint_options);

          g_signal_connect (manager->global_paint_options, "notify",
                            G_CALLBACK (tool_options_manager_paint_options_notify),
                            tool_info->tool_options);

          tool_options_manager_copy_paint_props (manager->global_paint_options,
                                                 GIMP_PAINT_OPTIONS (tool_info->tool_options),
                                                 tool_info->context_props &
                                                 manager->global_props);
        }
    }

  g_signal_connect (gimp->config, "notify::global-brush",
                    G_CALLBACK (tool_options_manager_global_notify),
                    manager);
  g_signal_connect (gimp->config, "notify::global-dynamics",
                    G_CALLBACK (tool_options_manager_global_notify),
                    manager);
  g_signal_connect (gimp->config, "notify::global-pattern",
                    G_CALLBACK (tool_options_manager_global_notify),
                    manager);
  g_signal_connect (gimp->config, "notify::global-palette",
                    G_CALLBACK (tool_options_manager_global_notify),
                    manager);
  g_signal_connect (gimp->config, "notify::global-gradient",
                    G_CALLBACK (tool_options_manager_global_notify),
                    manager);
  g_signal_connect (gimp->config, "notify::global-font",
                    G_CALLBACK (tool_options_manager_global_notify),
                    manager);

  g_signal_connect (user_context, "tool-changed",
                    G_CALLBACK (tool_options_manager_tool_changed),
                    manager);

  tool_options_manager_tool_changed (user_context,
                                     gimp_context_get_tool (user_context),
                                     manager);
}

void
gimp_tool_options_manager_exit (Gimp *gimp)
{
  GimpToolOptionsManager *manager;
  GimpContext            *user_context;
  GList                  *list;

  g_return_if_fail (GIMP_IS_GIMP (gimp));

  manager = g_object_get_qdata (G_OBJECT (gimp), manager_quark);

  g_return_if_fail (manager != NULL);

  user_context = gimp_get_user_context (gimp);

  g_signal_handlers_disconnect_by_func (user_context,
                                        tool_options_manager_tool_changed,
                                        manager);

  g_signal_handlers_disconnect_by_func (gimp->config,
                                        tool_options_manager_global_notify,
                                        manager);

  for (list = gimp_get_tool_info_iter (gimp);
       list;
       list = g_list_next (list))
    {
      GimpToolInfo *tool_info = list->data;

      gimp_context_set_parent (GIMP_CONTEXT (tool_info->tool_options), NULL);

      if (GIMP_IS_PAINT_OPTIONS (tool_info->tool_options))
        {
          g_signal_handlers_disconnect_by_func (tool_info->tool_options,
                                                tool_options_manager_paint_options_notify,
                                                manager->global_paint_options);

          g_signal_handlers_disconnect_by_func (manager->global_paint_options,
                                                tool_options_manager_paint_options_notify,
                                                tool_info->tool_options);
        }
    }

  g_clear_object (&manager->global_paint_options);

  g_slice_free (GimpToolOptionsManager, manager);

  g_object_set_qdata (G_OBJECT (gimp), manager_quark, NULL);
}


/*  private functions  */

static GimpContextPropMask
tool_options_manager_get_global_props (GimpCoreConfig *config)
{
  GimpContextPropMask global_props = 0;

  /*  FG and BG are always shared between all tools  */
  global_props |= GIMP_CONTEXT_PROP_MASK_FOREGROUND;
  global_props |= GIMP_CONTEXT_PROP_MASK_BACKGROUND;

  if (config->global_brush)
    global_props |= GIMP_CONTEXT_PROP_MASK_BRUSH;
  if (config->global_dynamics)
    global_props |= GIMP_CONTEXT_PROP_MASK_DYNAMICS;
  if (config->global_pattern)
    global_props |= GIMP_CONTEXT_PROP_MASK_PATTERN;
  if (config->global_palette)
    global_props |= GIMP_CONTEXT_PROP_MASK_PALETTE;
  if (config->global_gradient)
    global_props |= GIMP_CONTEXT_PROP_MASK_GRADIENT;
  if (config->global_font)
    global_props |= GIMP_CONTEXT_PROP_MASK_FONT;

  return global_props;
}

static void
tool_options_manager_global_notify (GimpCoreConfig         *config,
                                    const GParamSpec       *pspec,
                                    GimpToolOptionsManager *manager)
{
  GimpContextPropMask  global_props;
  GimpContextPropMask  enabled_global_props;
  GimpContextPropMask  disabled_global_props;
  GList               *list;

  global_props = tool_options_manager_get_global_props (config);

  enabled_global_props  = global_props & ~manager->global_props;
  disabled_global_props = manager->global_props & ~global_props;

  /*  copy the newly enabled global props to all tool options, and
   *  disconnect the newly disabled ones from the user context
   */
  for (list = gimp_get_tool_info_iter (manager->gimp);
       list;
       list = g_list_next (list))
    {
      GimpToolInfo *tool_info = list->data;

      /*  don't change the active tool, it is always fully connected
       *  to the user_context anyway because we set its
       *  defined/undefined context props in tool_changed()
       */
      if (tool_info == manager->active_tool)
        continue;

      /*  defining the newly disabled ones disconnects them from the
       *  parent user context
       */
      gimp_context_define_properties (GIMP_CONTEXT (tool_info->tool_options),
                                      tool_info->context_props &
                                      disabled_global_props,
                                      TRUE);

      /*  undefining the newly enabled ones copies the value from the
       *  parent user context
       */
      gimp_context_define_properties (GIMP_CONTEXT (tool_info->tool_options),
                                      tool_info->context_props &
                                      enabled_global_props,
                                      FALSE);

      if (GIMP_IS_PAINT_OPTIONS (tool_info->tool_options))
        tool_options_manager_copy_paint_props (manager->global_paint_options,
                                               GIMP_PAINT_OPTIONS (tool_info->tool_options),
                                               tool_info->context_props &
                                               enabled_global_props);
    }

  manager->global_props = global_props;
}

static void
tool_options_manager_paint_options_notify (GimpPaintOptions *src,
                                           const GParamSpec *pspec,
                                           GimpPaintOptions *dest)
{
  Gimp                   *gimp   = GIMP_CONTEXT (src)->gimp;
  GimpCoreConfig         *config = gimp->config;
  GimpToolOptionsManager *manager;
  GimpToolInfo           *tool_info;
  GimpContextPropMask     prop_mask = 0;
  gboolean                active    = FALSE;

  manager = g_object_get_qdata (G_OBJECT (gimp), manager_quark);

  /*  one of the options is the global one, the other is the tool's,
   *  get the tool_info from the tool's options
   */
  if (manager->global_paint_options == src)
    tool_info = gimp_context_get_tool (GIMP_CONTEXT (dest));
  else
    tool_info = gimp_context_get_tool (GIMP_CONTEXT (src));

  if (tool_info == manager->active_tool)
    active = TRUE;

  if ((active || config->global_brush) &&
      tool_info->context_props & GIMP_CONTEXT_PROP_MASK_BRUSH)
    {
      prop_mask |= GIMP_CONTEXT_PROP_MASK_BRUSH;
    }

  if ((active || config->global_dynamics) &&
      tool_info->context_props & GIMP_CONTEXT_PROP_MASK_DYNAMICS)
    {
      prop_mask |= GIMP_CONTEXT_PROP_MASK_DYNAMICS;
    }

  if ((active || config->global_gradient) &&
      tool_info->context_props & GIMP_CONTEXT_PROP_MASK_GRADIENT)
    {
      prop_mask |= GIMP_CONTEXT_PROP_MASK_GRADIENT;
    }

  if (gimp_paint_options_is_prop (pspec->name, prop_mask))
    {
      GValue value = G_VALUE_INIT;

      g_value_init (&value, pspec->value_type);

      g_object_get_property (G_OBJECT (src), pspec->name, &value);

      g_signal_handlers_block_by_func (dest,
                                       tool_options_manager_paint_options_notify,
                                       src);

      g_object_set_property (G_OBJECT (dest), pspec->name, &value);

      g_signal_handlers_unblock_by_func (dest,
                                         tool_options_manager_paint_options_notify,
                                         src);

      g_value_unset (&value);
    }
}

static void
tool_options_manager_copy_paint_props (GimpPaintOptions    *src,
                                       GimpPaintOptions    *dest,
                                       GimpContextPropMask  prop_mask)
{
  g_signal_handlers_block_by_func (dest,
                                   tool_options_manager_paint_options_notify,
                                   src);

  gimp_paint_options_copy_props (src, dest, prop_mask);

  g_signal_handlers_unblock_by_func (dest,
                                     tool_options_manager_paint_options_notify,
                                     src);
}

static void
tool_options_manager_tool_changed (GimpContext            *user_context,
                                   GimpToolInfo           *tool_info,
                                   GimpToolOptionsManager *manager)
{
  if (tool_info == manager->active_tool)
    return;

  /*  FIXME: gimp_busy HACK
   *  the tool manager will stop the emission, so simply return
   */
  if (user_context->gimp->busy)
    return;

  if (manager->active_tool)
    {
      GimpToolInfo *active = manager->active_tool;

      /*  disconnect the old active tool from all context properties
       *  it uses, but are not currently global
       */
      gimp_context_define_properties (GIMP_CONTEXT (active->tool_options),
                                      active->context_props &
                                      ~manager->global_props,
                                      TRUE);
    }

  manager->active_tool = tool_info;

  if (manager->active_tool)
    {
      GimpToolInfo *active = manager->active_tool;

      /*  make sure the tool options GUI always exists, this call
       *  creates it if needed, so tools always have their option GUI
       *  available even if the tool options dockable is not open, see
       *  for example issue #3435
       */
      gimp_tools_get_tool_options_gui (active->tool_options);

      /*  copy the new tool's context properties that are not
       *  currently global to the user context, so they get used by
       *  everything
       */
      gimp_context_copy_properties (GIMP_CONTEXT (active->tool_options),
                                    gimp_get_user_context (manager->gimp),
                                    active->context_props &
                                    ~manager->global_props);

      if (GIMP_IS_PAINT_OPTIONS (active->tool_options))
        tool_options_manager_copy_paint_props (GIMP_PAINT_OPTIONS (active->tool_options),
                                               manager->global_paint_options,
                                               active->context_props &
                                               ~manager->global_props);

      /*  then, undefine these properties so the tool syncs with the
       *  user context automatically
       */
      gimp_context_define_properties (GIMP_CONTEXT (active->tool_options),
                                      active->context_props &
                                      ~manager->global_props,
                                      FALSE);
    }
}