summaryrefslogtreecommitdiff
path: root/gtk/gtkshortcutaction.c
blob: e95c1d49443eafba1a1f0e58ab9b8a01ae013e33 (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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
/*
 * Copyright © 2018 Benjamin Otte
 *
 * 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 2.1 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
 * Lesser 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 <http://www.gnu.org/licenses/>.
 *
 * Authors: Benjamin Otte <otte@gnome.org>
 */

/**
 * SECTION:gtkshortcutaction
 * @Title: GtkShortcutAction
 * @Short_description: Tracking if shortcuts should be activated
 * @See_also: #GtkShortcut
 *
 * #GtkShortcutAction is the object used to track if a #GtkShortcut should be
 * activated. For this purpose, gtk_shortcut_action_action() can be called
 * on a #GdkEvent.
 *
 * #GtkShortcutActions contain functions that allow easy presentation to end
 * users as well as being printed for debugging.
 *
 * All #GtkShortcutActions are immutable, you can only specify their properties
 * during construction. If you want to change a action, you have to replace it
 * with a new one.
 */

#include "config.h"

#include "gtkshortcutaction.h"

#include "gtkwidgetprivate.h"

typedef struct _GtkShortcutActionClass GtkShortcutActionClass;

#define GTK_IS_SHORTCUT_ACTION_TYPE(action,type) (GTK_IS_SHORTCUT_ACTION (action) && (action)->action_class->action_type == (type))

struct _GtkShortcutAction
{
  const GtkShortcutActionClass *action_class;

  gatomicrefcount ref_count;
};

struct _GtkShortcutActionClass
{
  GtkShortcutActionType action_type;
  gsize struct_size;
  const char *type_name;

  void            (* finalize)    (GtkShortcutAction            *action);
  gboolean        (* activate)    (GtkShortcutAction            *action,
                                   GtkShortcutActionFlags        flags,
                                   GtkWidget                    *widget,
                                   GVariant                     *args);
};

G_DEFINE_BOXED_TYPE (GtkShortcutAction, gtk_shortcut_action,
                     gtk_shortcut_action_ref,
                     gtk_shortcut_action_unref)

static void
gtk_shortcut_action_finalize (GtkShortcutAction *self)
{
  self->action_class->finalize (self);

  g_free (self);
}

/*< private >
 * gtk_shortcut_action_new:
 * @action_class: class structure for this action
 *
 * Returns: (transfer full): the newly created #GtkShortcutAction
 */
static GtkShortcutAction *
gtk_shortcut_action_new (const GtkShortcutActionClass *action_class)
{
  GtkShortcutAction *self;

  g_return_val_if_fail (action_class != NULL, NULL);

  self = g_malloc0 (action_class->struct_size);
  g_atomic_ref_count_init (&self->ref_count);

  self->action_class = action_class;

  return self;
}

/**
 * gtk_shortcut_action_ref:
 * @self: a #GtkShortcutAction
 *
 * Acquires a reference on the given #GtkShortcutAction.
 *
 * Returns: (transfer full): the #GtkShortcutAction with an additional reference
 */
GtkShortcutAction *
gtk_shortcut_action_ref (GtkShortcutAction *self)
{
  g_return_val_if_fail (GTK_IS_SHORTCUT_ACTION (self), NULL);

  g_atomic_ref_count_inc (&self->ref_count);

  return self;
}

/**
 * gtk_shortcut_action_unref:
 * @self: (transfer full): a #GtkShortcutAction
 *
 * Releases a reference on the given #GtkShortcutAction.
 *
 * If the reference was the last, the resources associated to the @action are
 * freed.
 */
void
gtk_shortcut_action_unref (GtkShortcutAction *self)
{
  g_return_if_fail (GTK_IS_SHORTCUT_ACTION (self));

  if (g_atomic_ref_count_dec (&self->ref_count))
    gtk_shortcut_action_finalize (self);
}

/**
 * gtk_shortcut_action_get_action_type:
 * @self: a #GtkShortcutAction
 *
 * Returns the type of the @action.
 *
 * Returns: the type of the #GtkShortcutAction
 */
GtkShortcutActionType
gtk_shortcut_action_get_action_type (GtkShortcutAction *self)
{
  g_return_val_if_fail (GTK_IS_SHORTCUT_ACTION (self), GTK_SHORTCUT_ACTION_NOTHING);

  return self->action_class->action_type;
}

/**
 * gtk_shortcut_action_activate:
 * @self: a #GtkShortcutAction
 * @flags: flags to activate with
 * @widget: Target of the activation
 * @args: (allow-none): arguments to pass
 *
 * Activates the action on the @widget with the given @args. 
 *
 * Note that some actions do ignore the passed in @flags, @widget or
 * @args.
 *
 * Activation of an action can fail for various reasons. If the action
 * is not supported by the @widget, if the @args don't match the action
 * or if the activation otherwise had no effect, %FALSE will be returned.
 *
 * Returns: %TRUE if this action was activated successfully
 **/
gboolean
gtk_shortcut_action_activate (GtkShortcutAction      *self,
                              GtkShortcutActionFlags  flags,
                              GtkWidget              *widget,
                              GVariant               *args)
{
  g_return_val_if_fail (GTK_IS_SHORTCUT_ACTION (self), FALSE);
  g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);

  return self->action_class->activate (self, flags, widget, args);
}

/*** GTK_SHORTCUT_ACTION_NOTHING ***/

typedef struct _GtkNothingAction GtkNothingAction;

struct _GtkNothingAction
{
  GtkShortcutAction action;
};

static void
gtk_nothing_action_finalize (GtkShortcutAction *action)
{
  g_assert_not_reached ();
}

static gboolean
gtk_nothing_action_activate (GtkShortcutAction      *action,
                             GtkShortcutActionFlags  flags,
                             GtkWidget              *widget,
                             GVariant               *args)
{
  return FALSE;
}

static const GtkShortcutActionClass GTK_NOTHING_ACTION_CLASS = {
  GTK_SHORTCUT_ACTION_NOTHING,
  sizeof (GtkNothingAction),
  "GtkNothingAction",
  gtk_nothing_action_finalize,
  gtk_nothing_action_activate
};

static GtkNothingAction nothing = { { &GTK_NOTHING_ACTION_CLASS, 1 } };

/**
 * gtk_nothing_action_new:
 *
 * Gets the nothing action. This is an action that does nothing and where
 * activating it always fails.
 *
 * Returns: The nothing action
 */
GtkShortcutAction *
gtk_nothing_action_new (void)
{
  return gtk_shortcut_action_ref (&nothing.action);
}

/*** GTK_SHORTCUT_ACTION_CALLBACK ***/

typedef struct _GtkCallbackAction GtkCallbackAction;

struct _GtkCallbackAction
{
  GtkShortcutAction action;

  GtkShortcutFunc callback;
  gpointer user_data;
  GDestroyNotify destroy_notify;
};

static void
gtk_callback_action_finalize (GtkShortcutAction *action)
{
  GtkCallbackAction *self = (GtkCallbackAction *) action;

  if (self->destroy_notify)
    self->destroy_notify (self->user_data);
}

static gboolean
gtk_callback_action_activate (GtkShortcutAction      *action,
                              GtkShortcutActionFlags  flags,
                              GtkWidget              *widget,
                              GVariant               *args)
{
  GtkCallbackAction *self = (GtkCallbackAction *) action;

  return self->callback (widget, args, self->user_data);
}

static const GtkShortcutActionClass GTK_CALLBACK_ACTION_CLASS = {
  GTK_SHORTCUT_ACTION_CALLBACK,
  sizeof (GtkCallbackAction),
  "GtkCallbackAction",
  gtk_callback_action_finalize,
  gtk_callback_action_activate
};

/**
 * gtk_callback_action_new:
 * @callback: the callback to call
 * @data: 
 * @destroy: 
 *
 * Create a custom action that calls the given @callback when
 * activated.
 *
 * Returns: A new shortcut action
 **/
GtkShortcutAction *
gtk_callback_action_new (GtkShortcutFunc         callback,
                         gpointer                data,
                         GDestroyNotify          destroy)
{
  GtkCallbackAction *self;

  g_return_val_if_fail (callback != NULL, NULL);

  self = (GtkCallbackAction *) gtk_shortcut_action_new (&GTK_CALLBACK_ACTION_CLASS);

  self->callback = callback;
  self->user_data = data;
  self->destroy_notify = destroy;

  return &self->action;
}

/*** GTK_SHORTCUT_ACTION_ACTIVATE ***/

typedef struct _GtkActivateAction GtkActivateAction;

struct _GtkActivateAction
{
  GtkShortcutAction action;
};

static void
gtk_activate_action_finalize (GtkShortcutAction *action)
{
  g_assert_not_reached ();
}

static gboolean
gtk_activate_action_activate (GtkShortcutAction      *action,
                             GtkShortcutActionFlags  flags,
                             GtkWidget              *widget,
                             GVariant               *args)
{
  return gtk_widget_activate (widget);
}

static const GtkShortcutActionClass GTK_ACTIVATE_ACTION_CLASS = {
  GTK_SHORTCUT_ACTION_ACTIVATE,
  sizeof (GtkActivateAction),
  "GtkActivateAction",
  gtk_activate_action_finalize,
  gtk_activate_action_activate
};

static GtkActivateAction activate = { { &GTK_ACTIVATE_ACTION_CLASS, 1 } };

/**
 * gtk_activate_action_new:
 *
 * Gets the activate action. This is an action that calls gtk_widget_activate()
 * on the given widget upon activation.
 *
 * Returns: The activate action
 */
GtkShortcutAction *
gtk_activate_action_new (void)
{
  return gtk_shortcut_action_ref (&activate.action);
}

/*** GTK_SHORTCUT_ACTION_MNEMONIC ***/

typedef struct _GtkMnemonicAction GtkMnemonicAction;

struct _GtkMnemonicAction
{
  GtkShortcutAction action;
};

static void
gtk_mnemonic_action_finalize (GtkShortcutAction *action)
{
  g_assert_not_reached ();
}

static gboolean
gtk_mnemonic_action_activate (GtkShortcutAction      *action,
                              GtkShortcutActionFlags  flags,
                              GtkWidget              *widget,
                              GVariant               *args)
{
  return gtk_widget_mnemonic_activate (widget, flags & GTK_SHORTCUT_ACTION_EXCLUSIVE ? FALSE : TRUE);
}

static const GtkShortcutActionClass GTK_MNEMONIC_ACTION_CLASS = {
  GTK_SHORTCUT_ACTION_MNEMONIC,
  sizeof (GtkMnemonicAction),
  "GtkMnemonicAction",
  gtk_mnemonic_action_finalize,
  gtk_mnemonic_action_activate
};

static GtkMnemonicAction mnemonic = { { &GTK_MNEMONIC_ACTION_CLASS, 1 } };

/**
 * gtk_mnemonic_action_new:
 *
 * Gets the mnemonic action. This is an action that calls
 * gtk_widget_mnemonic_activate() on the given widget upon activation.
 *
 * Returns: The mnemonic action
 */
GtkShortcutAction *
gtk_mnemonic_action_new (void)
{
  return gtk_shortcut_action_ref (&mnemonic.action);
}

/*** GTK_SHORTCUT_ACTION_SIGNAL ***/

typedef struct _GtkSignalAction GtkSignalAction;

struct _GtkSignalAction
{
  GtkShortcutAction action;

  char *name;
};

static void
gtk_signal_action_finalize (GtkShortcutAction *action)
{
  GtkSignalAction *self = (GtkSignalAction *) action;

  g_free (self->name);
}

static gboolean
binding_compose_params (GtkWidget     *widget,
                        GVariantIter  *args,
                        GSignalQuery  *query,
                        GValue       **params_p)
{
  GValue *params;
  const GType *types;
  guint i;
  gboolean valid;

  params = g_new0 (GValue, query->n_params + 1);
  *params_p = params;

  /* The instance we emit on is the first object in the array
   */
  g_value_init (params, G_TYPE_OBJECT);
  g_value_set_object (params, G_OBJECT (widget));
  params++;

  types = query->param_types;
  valid = TRUE;
  for (i = 1; i < query->n_params + 1 && valid; i++)
    {
      GValue tmp_value = G_VALUE_INIT;
      GVariant *tmp_variant;

      g_value_init (params, *types);
      tmp_variant = g_variant_iter_next_value (args);

      switch ((guint) g_variant_classify (tmp_variant))
        {
        case G_VARIANT_CLASS_BOOLEAN:
          g_value_init (&tmp_value, G_TYPE_BOOLEAN);
          g_value_set_boolean (&tmp_value, g_variant_get_boolean (tmp_variant));
          break;
        case G_VARIANT_CLASS_DOUBLE:
          g_value_init (&tmp_value, G_TYPE_DOUBLE);
          g_value_set_double (&tmp_value, g_variant_get_double (tmp_variant));
          break;
        case G_VARIANT_CLASS_INT32:
          g_value_init (&tmp_value, G_TYPE_LONG);
          g_value_set_long (&tmp_value, g_variant_get_int32 (tmp_variant));
          break;
        case G_VARIANT_CLASS_UINT32:
          g_value_init (&tmp_value, G_TYPE_LONG);
          g_value_set_long (&tmp_value, g_variant_get_uint32 (tmp_variant));
          break;
        case G_VARIANT_CLASS_INT64:
          g_value_init (&tmp_value, G_TYPE_LONG);
          g_value_set_long (&tmp_value, g_variant_get_int64 (tmp_variant));
          break;
        case G_VARIANT_CLASS_STRING:
          /* gtk_rc_parse_flags/enum() has fancier parsing for this; we can't call
           * that since we don't have a GParamSpec, so just do something simple
           */
          if (G_TYPE_FUNDAMENTAL (*types) == G_TYPE_ENUM)
            {
              GEnumClass *class = G_ENUM_CLASS (g_type_class_ref (*types));
              GEnumValue *enum_value;
              const char *s = g_variant_get_string (tmp_variant, NULL);

              valid = FALSE;

              enum_value = g_enum_get_value_by_name (class, s);
              if (!enum_value)
                enum_value = g_enum_get_value_by_nick (class, s);

              if (enum_value)
                {
                  g_value_init (&tmp_value, *types);
                  g_value_set_enum (&tmp_value, enum_value->value);
                  valid = TRUE;
                }

              g_type_class_unref (class);
            }
          /* This is just a hack for compatibility with GTK+-1.2 where a string
           * could be used for a single flag value / without the support for multiple
           * values in gtk_rc_parse_flags(), this isn't very useful.
           */
          else if (G_TYPE_FUNDAMENTAL (*types) == G_TYPE_FLAGS)
            {
              GFlagsClass *class = G_FLAGS_CLASS (g_type_class_ref (*types));
              GFlagsValue *flags_value;
              const char *s = g_variant_get_string (tmp_variant, NULL);

              valid = FALSE;

              flags_value = g_flags_get_value_by_name (class, s);
              if (!flags_value)
                flags_value = g_flags_get_value_by_nick (class, s);
              if (flags_value)
                {
                  g_value_init (&tmp_value, *types);
                  g_value_set_flags (&tmp_value, flags_value->value);
                  valid = TRUE;
                }

              g_type_class_unref (class);
            }
          else
            {
              g_value_init (&tmp_value, G_TYPE_STRING);
              g_value_set_static_string (&tmp_value, g_variant_get_string (tmp_variant, NULL));
            }
          break;
        default:
          valid = FALSE;
          break;
        }

      if (valid)
        {
          if (!g_value_transform (&tmp_value, params))
            valid = FALSE;

          g_value_unset (&tmp_value);
        }

      g_variant_unref (tmp_variant);
      types++;
      params++;
    }

  if (!valid)
    {
      guint j;

      for (j = 0; j < i; j++)
        g_value_unset (&(*params_p)[j]);

      g_free (*params_p);
      *params_p = NULL;
    }

  return valid;
}

static gboolean
gtk_signal_action_emit_signal (GtkWidget *widget,
                               const char *signal,
                               GVariant   *args,
                               gboolean   *handled,
                               GError    **error)
{
  GSignalQuery query;
  guint signal_id;
  GValue *params = NULL;
  GValue return_val = G_VALUE_INIT;
  GVariantIter args_iter;
  gsize n_args;
  guint i;

  *handled = FALSE;

  signal_id = g_signal_lookup (signal, G_OBJECT_TYPE (widget));
  if (!signal_id)
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "Could not find signal \"%s\" in the '%s' class ancestry",
                   signal,
                   g_type_name (G_OBJECT_TYPE (widget)));
      return FALSE;
    }

  g_signal_query (signal_id, &query);
  if (args == NULL)
    n_args = 0;
  else if (g_variant_is_of_type (args, G_VARIANT_TYPE_TUPLE))
    n_args = g_variant_iter_init (&args_iter, args);
  else
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "argument GVariant is not a tuple");
      return FALSE;
    }
  if (query.n_params != n_args ||
      (query.return_type != G_TYPE_NONE && query.return_type != G_TYPE_BOOLEAN) ||
      !binding_compose_params (widget, &args_iter, &query, &params))
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "signature mismatch for signal \"%s\" in the '%s' class ancestry",
                   signal,
                   g_type_name (G_OBJECT_TYPE (widget)));
      return FALSE;
    }
  else if (!(query.signal_flags & G_SIGNAL_ACTION))
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "signal \"%s\" in the '%s' class ancestry cannot be used for action emissions",
                   signal,
                   g_type_name (G_OBJECT_TYPE (widget)));
      return FALSE;
    }

  if (query.return_type == G_TYPE_BOOLEAN)
    g_value_init (&return_val, G_TYPE_BOOLEAN);

  g_signal_emitv (params, signal_id, 0, &return_val);

  if (query.return_type == G_TYPE_BOOLEAN)
    {
      if (g_value_get_boolean (&return_val))
        *handled = TRUE;
      g_value_unset (&return_val);
    }
  else
    *handled = TRUE;

  if (params != NULL)
    {
      for (i = 0; i < query.n_params + 1; i++)
        g_value_unset (&params[i]);

      g_free (params);
    }

  return TRUE;
}

static gboolean
gtk_signal_action_activate (GtkShortcutAction      *action,
                            GtkShortcutActionFlags  flags,
                            GtkWidget              *widget,
                            GVariant               *args)
{
  GtkSignalAction *self = (GtkSignalAction *) action;
  GError *error = NULL;
  gboolean handled;

  if (!gtk_signal_action_emit_signal (widget,
                                      self->name,
                                      args,
                                      &handled,
                                      &error))
    {
      g_warning ("gtk_signal_action_activate(): %s",
                 error->message);
      g_clear_error (&error);
      return FALSE;
    }

  return handled;
}

static const GtkShortcutActionClass GTK_SIGNAL_ACTION_CLASS = {
  GTK_SHORTCUT_ACTION_SIGNAL,
  sizeof (GtkSignalAction),
  "GtkSignalAction",
  gtk_signal_action_finalize,
  gtk_signal_action_activate
};

/**
 * gtk_signal_action_new:
 * @signal_name: name of the signal to emit
 *
 * Creates an action that when activated, emits the given action signal
 * on the provided widget unpacking the given args into arguments passed
 * to the signal.
 *
 * Returns: a new #GtkShortcutAction
 **/
GtkShortcutAction *
gtk_signal_action_new (const char *signal_name)
{
  GtkSignalAction *self;

  g_return_val_if_fail (signal_name != NULL, NULL);

  self = (GtkSignalAction *) gtk_shortcut_action_new (&GTK_SIGNAL_ACTION_CLASS);

  self->name = g_strdup (signal_name);

  return &self->action;
}

/**
 * gtk_signal_action_get_signal_name:
 * @action: a signal action
 *
 * Returns the name of the signal that will be emitted.
 *
 * Returns: the name of the signal to emit
 **/
const char *
gtk_signal_action_get_signal_name (GtkShortcutAction *action)
{
  GtkSignalAction *self = (GtkSignalAction *) action;

  g_return_val_if_fail (GTK_IS_SHORTCUT_ACTION_TYPE (action, GTK_SHORTCUT_ACTION_SIGNAL), NULL);

  return self->name;
}

/*** GTK_SHORTCUT_ACTION_ACTION ***/

typedef struct _GtkActionAction GtkActionAction;

struct _GtkActionAction
{
  GtkShortcutAction action;

  char *name;
};

static void
gtk_action_action_finalize (GtkShortcutAction *action)
{
  GtkSignalAction *self = (GtkSignalAction *) action;

  g_free (self->name);
}

static gboolean
gtk_shortcut_trigger_check_parameter_type (GVariant           *args,
                                           const GVariantType *parameter_type)
{
  if (args)
    {
      if (parameter_type == NULL)
        {
          g_warning ("Trying to invoke action with arguments, but action has no parameter");
          return FALSE;
        }

      if (!g_variant_is_of_type (args, parameter_type))
        {
          gchar *typestr = g_variant_type_dup_string (parameter_type);
          gchar *targetstr = g_variant_print (args, TRUE);
          g_warning ("Trying to invoke action with target '%s',"
                     " but action expects parameter with type '%s'", targetstr, typestr);
          g_free (targetstr);
          g_free (typestr);
          return FALSE;
        }
    }
  else
    {
      if (parameter_type != NULL)
        {
          gchar *typestr = g_variant_type_dup_string (parameter_type);
          g_warning ("Trying to invoke action without arguments,"
                     " but action expects parameter with type '%s'", typestr);
          g_free (typestr);
          return FALSE;
        }
    }

  return TRUE;
}

static gboolean
gtk_action_action_activate (GtkShortcutAction      *action,
                            GtkShortcutActionFlags  flags,
                            GtkWidget              *widget,
                            GVariant               *args)
{
  GtkSignalAction *self = (GtkSignalAction *) action;
  GActionGroup *action_group;
  const GVariantType *parameter_type;
  gboolean enabled;

  action_group = G_ACTION_GROUP (_gtk_widget_get_action_muxer (widget, FALSE));
  if (action_group == NULL)
    return FALSE;

  if (!g_action_group_query_action (action_group, self->name, &enabled, &parameter_type, NULL, NULL, NULL))
    return FALSE;

  if (!enabled)
    return FALSE;

  /* We found an action with the correct name and it's enabled.
   * This is the action that we are going to try to invoke.
   *
   * There is still the possibility that the args don't
   * match the expected parameter type.  In that case, we will print
   * a warning.
   */
  if (!gtk_shortcut_trigger_check_parameter_type (args, parameter_type))
    return FALSE;

  g_action_group_activate_action (action_group, self->name, args);

  return TRUE;
}

static const GtkShortcutActionClass GTK_ACTION_ACTION_CLASS = {
  GTK_SHORTCUT_ACTION_ACTION,
  sizeof (GtkActionAction),
  "GtkActionAction",
  gtk_action_action_finalize,
  gtk_action_action_activate
};

/**
 * gtk_action_action_new:
 * @name: the detailed name of the action
 *
 * Creates an action that when activated, activates the action given by
 * the detailed @name on the widget passing the given arguments to it.
 *
 * See gtk_widget_insert_action_group() for how to add actions to widgets.
 *
 * Returns: a new #GtkShortcutAction
 **/
GtkShortcutAction *
gtk_action_action_new (const char *name)
{
  GtkActionAction *self;

  g_return_val_if_fail (name != NULL, NULL);

  self = (GtkActionAction *) gtk_shortcut_action_new (&GTK_ACTION_ACTION_CLASS);

  self->name = g_strdup (name);

  return &self->action;
}

/**
 * gtk_action_action_get_name:
 * @action: an action action
 *
 * Returns the name of the action that will be activated.
 *
 * Returns: the name of the action to activate
 **/
const char *
gtk_action_action_get_name (GtkShortcutAction *action)
{
  GtkActionAction *self = (GtkActionAction *) action;

  g_return_val_if_fail (GTK_IS_SHORTCUT_ACTION_TYPE (action, GTK_SHORTCUT_ACTION_ACTION), NULL);

  return self->name;
}