File: gimpactionimpl.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 (420 lines) | stat: -rw-r--r-- 14,027 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
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
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpaction.c
 * Copyright (C) 2004-2019 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 "libgimpbase/gimpbase.h"

#include "widgets-types.h"

#include "gimpaction.h"
#include "gimpactionimpl.h"
#include "gimpaction-history.h"


enum
{
  PROP_0,
  PROP_ENABLED = GIMP_ACTION_PROP_LAST + 1,
  PROP_PARAMETER_TYPE,
  PROP_STATE_TYPE,
  PROP_STATE
};

enum
{
  CHANGE_STATE,
  LAST_SIGNAL
};

struct _GimpActionImplPrivate
{
  GVariantType *parameter_type;
  GVariant     *state;
  GVariant     *state_hint;
  gboolean      state_set_already;
};

static void   gimp_action_g_action_iface_init     (GActionInterface    *iface);

static void   gimp_action_impl_finalize           (GObject             *object);
static void   gimp_action_impl_set_property       (GObject             *object,
                                                   guint                prop_id,
                                                   const GValue        *value,
                                                   GParamSpec          *pspec);
static void   gimp_action_impl_get_property       (GObject             *object,
                                                   guint                prop_id,
                                                   GValue              *value,
                                                   GParamSpec          *pspec);

/* XXX Implementations for our GimpAction are widely inspired by GSimpleAction
 * implementations.
 */
static void   gimp_action_impl_activate           (GAction             *action,
                                                   GVariant            *parameter);
static void   gimp_action_impl_change_state       (GAction             *action,
                                                   GVariant            *value);
static gboolean gimp_action_impl_get_enabled      (GAction             *action);
static const GVariantType *
              gimp_action_impl_get_parameter_type (GAction             *action);
static const GVariantType *
              gimp_action_impl_get_state_type     (GAction             *action);
static GVariant *
              gimp_action_impl_get_state          (GAction             *action);
static GVariant *
              gimp_action_impl_get_state_hint     (GAction             *action);

static void   gimp_action_impl_set_state          (GimpAction          *gimp_action,
                                                   GVariant            *value);


G_DEFINE_TYPE_WITH_CODE (GimpActionImpl, gimp_action_impl, GIMP_TYPE_OBJECT,
                         G_ADD_PRIVATE (GimpActionImpl)
                         G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, gimp_action_g_action_iface_init)
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_ACTION, NULL))

#define parent_class gimp_action_impl_parent_class

static guint gimp_action_impl_signals[LAST_SIGNAL] = { 0 };

static void
gimp_action_impl_class_init (GimpActionImplClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  gimp_action_impl_signals[CHANGE_STATE] =
    g_signal_new ("change-state",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
                  G_STRUCT_OFFSET (GimpActionImplClass, change_state),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  G_TYPE_VARIANT);

  object_class->finalize      = gimp_action_impl_finalize;
  object_class->set_property  = gimp_action_impl_set_property;
  object_class->get_property  = gimp_action_impl_get_property;

  gimp_action_install_properties (object_class);

  /**
   * GimpAction:enabled:
   *
   * If @action is currently enabled.
   *
   * If the action is disabled then calls to g_action_activate() and
   * g_action_change_state() have no effect.
   **/
  g_object_class_install_property (object_class, PROP_ENABLED,
                                   g_param_spec_boolean ("enabled",
                                                         "Enabled",
                                                         "If the action can be activated",
                                                         TRUE,
                                                         GIMP_PARAM_READWRITE));
  /**
   * GimpAction:parameter-type:
   *
   * The type of the parameter that must be given when activating the
   * action.
   **/
  g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
                                   g_param_spec_boxed ("parameter-type",
                                                       "Parameter Type",
                                                       "The type of GVariant passed to activate()",
                                                       G_TYPE_VARIANT_TYPE,
                                                       GIMP_PARAM_READWRITE |
                                                       G_PARAM_CONSTRUCT_ONLY));
  /**
   * GimpAction:state-type:
   *
   * The #GVariantType of the state that the action has, or %NULL if the
   * action is stateless.
   **/
  g_object_class_install_property (object_class, PROP_STATE_TYPE,
                                   g_param_spec_boxed ("state-type",
                                                       "State Type",
                                                       "The type of the state kept by the action",
                                                       G_TYPE_VARIANT_TYPE,
                                                       GIMP_PARAM_READABLE));

  /**
   * GimpAction:state:
   *
   * The state of the action, or %NULL if the action is stateless.
   **/
  g_object_class_install_property (object_class, PROP_STATE,
                                   g_param_spec_variant ("state",
                                                         "State",
                                                         "The state the action is in",
                                                         G_VARIANT_TYPE_ANY,
                                                         NULL,
                                                         GIMP_PARAM_READWRITE | G_PARAM_CONSTRUCT));
}

static void
gimp_action_g_action_iface_init (GActionInterface *iface)
{
  iface->activate           = gimp_action_impl_activate;
  iface->change_state       = gimp_action_impl_change_state;
  iface->get_enabled        = gimp_action_impl_get_enabled;
  iface->get_name           = (const gchar* (*) (GAction*)) gimp_action_get_name;
  iface->get_parameter_type = gimp_action_impl_get_parameter_type;
  iface->get_state_type     = gimp_action_impl_get_state_type;
  iface->get_state          = gimp_action_impl_get_state;
  iface->get_state_hint     = gimp_action_impl_get_state_hint;
}

static void
gimp_action_impl_init (GimpActionImpl *impl)
{
  impl->priv                    = gimp_action_impl_get_instance_private (impl);
  impl->priv->state_set_already = FALSE;

  gimp_action_init (GIMP_ACTION (impl));
}

static void
gimp_action_impl_finalize (GObject *object)
{
  GimpActionImpl *impl = GIMP_ACTION_IMPL (object);

  if (impl->priv->parameter_type)
    g_variant_type_free (impl->priv->parameter_type);
  if (impl->priv->state)
    g_variant_unref (impl->priv->state);
  if (impl->priv->state_hint)
    g_variant_unref (impl->priv->state_hint);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gimp_action_impl_get_property (GObject    *object,
                               guint       prop_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  GimpActionImpl *impl = GIMP_ACTION_IMPL (object);

  switch (prop_id)
    {
    case PROP_ENABLED:
      g_value_set_boolean (value, gimp_action_impl_get_enabled (G_ACTION (impl)));
      break;
    case PROP_PARAMETER_TYPE:
      g_value_set_boxed (value, gimp_action_impl_get_parameter_type (G_ACTION (impl)));
      break;
    case PROP_STATE_TYPE:
      g_value_set_boxed (value, gimp_action_impl_get_state_type (G_ACTION (impl)));
      break;
    case PROP_STATE:
      g_value_take_variant (value, gimp_action_impl_get_state (G_ACTION (impl)));
      break;

    default:
      gimp_action_get_property (object, prop_id, value, pspec);
      break;
    }
}

static void
gimp_action_impl_set_property (GObject      *object,
                               guint         prop_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  GimpActionImpl *impl = GIMP_ACTION_IMPL (object);

  switch (prop_id)
    {
    case PROP_ENABLED:
      gimp_action_set_sensitive (GIMP_ACTION (impl),
                                 g_value_get_boolean (value), NULL);
      break;
    case PROP_PARAMETER_TYPE:
      impl->priv->parameter_type = g_value_dup_boxed (value);
      break;
    case PROP_STATE:
      /* The first time we see this (during construct) we should just
       * take the state as it was handed to us.
       *
       * After that, we should make sure we go through the same checks
       * as the C API.
       */
      if (!impl->priv->state_set_already)
        {
          impl->priv->state             = g_value_dup_variant (value);
          impl->priv->state_set_already = TRUE;
        }
      else
        {
          gimp_action_impl_set_state (GIMP_ACTION (impl), g_value_get_variant (value));
        }

      break;

    default:
      gimp_action_set_property (object, prop_id, value, pspec);
      break;
    }
}

static void
gimp_action_impl_activate (GAction  *action,
                           GVariant *parameter)
{
  g_object_add_weak_pointer (G_OBJECT (action), (gpointer) &action);

  gimp_action_emit_activate (GIMP_ACTION (action), parameter);

  if (action != NULL)
    {
      /* Some actions are self-destructive, such as the "windows-recent-*"
       * actions which don't exist after being run. Don't log these.
       */
      g_object_remove_weak_pointer (G_OBJECT (action), (gpointer) &action);

      gimp_action_history_action_activated (GIMP_ACTION (action));
    }
}

static void
gimp_action_impl_change_state (GAction  *action,
                               GVariant *value)
{
  /* If the user connected a signal handler then they are responsible
   * for handling state changes.
   */
  if (g_signal_has_handler_pending (action, gimp_action_impl_signals[CHANGE_STATE], 0, TRUE))
    g_signal_emit (action, gimp_action_impl_signals[CHANGE_STATE], 0, value);
  else
    /* If not, then the default behavior is to just set the state. */
    gimp_action_impl_set_state (GIMP_ACTION (action), value);
}

static gboolean
gimp_action_impl_get_enabled (GAction *action)
{
  return gimp_action_is_sensitive (GIMP_ACTION (action), NULL);
}

static const GVariantType *
gimp_action_impl_get_parameter_type (GAction *action)
{
  GimpActionImpl *impl = GIMP_ACTION_IMPL (action);

  return impl->priv->parameter_type;
}

static const GVariantType *
gimp_action_impl_get_state_type (GAction *action)
{
  GimpActionImpl *impl = GIMP_ACTION_IMPL (action);

  if (impl->priv->state != NULL)
    return g_variant_get_type (impl->priv->state);
  else
    return NULL;
}

static GVariant *
gimp_action_impl_get_state (GAction *action)
{
  GimpActionImpl *impl = GIMP_ACTION_IMPL (action);

  return impl->priv->state ? g_variant_ref (impl->priv->state) : NULL;
}

static GVariant *
gimp_action_impl_get_state_hint (GAction *action)
{
  GimpActionImpl *impl = GIMP_ACTION_IMPL (action);

  if (impl->priv->state_hint != NULL)
    return g_variant_ref (impl->priv->state_hint);
  else
    return NULL;
}


/*  public functions  */

GimpAction *
gimp_action_impl_new (const gchar *name,
                      const gchar *label,
                      const gchar *short_label,
                      const gchar *tooltip,
                      const gchar *icon_name,
                      const gchar *help_id,
                      GimpContext *context)
{
  GimpAction *action;

  action = g_object_new (GIMP_TYPE_ACTION_IMPL,
                         "name",        name,
                         "label",       label,
                         "short-label", short_label,
                         "tooltip",     tooltip,
                         "icon-name",   icon_name,
                         "context",     context,
                         NULL);

  gimp_action_set_help_id (action, help_id);

  return action;
}


/*  private functions  */

static void
gimp_action_impl_set_state (GimpAction *gimp_action,
                            GVariant   *value)
{
  GimpActionImpl     *impl;
  const GVariantType *state_type;

  g_return_if_fail (GIMP_IS_ACTION (gimp_action));
  g_return_if_fail (value != NULL);

  impl = GIMP_ACTION_IMPL (gimp_action);

  state_type = impl->priv->state ? g_variant_get_type (impl->priv->state) : NULL;

  g_return_if_fail (state_type != NULL);
  g_return_if_fail (g_variant_is_of_type (value, state_type));

  g_variant_ref_sink (value);

  if (! impl->priv->state || ! g_variant_equal (impl->priv->state, value))
    {
      if (impl->priv->state)
        g_variant_unref (impl->priv->state);

      impl->priv->state = g_variant_ref (value);

      g_object_notify (G_OBJECT (impl), "state");
    }

  g_variant_unref (value);
}