summaryrefslogtreecommitdiff
path: root/plugins/gtk+/glade-accels.c
blob: 994e846b48cb4b6abb95973269dce4fca3b437fa (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
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
/*
 * Copyright (C) 2008 Tristan Van Berkom
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Authors:
 *   Tristan Van Berkom <tvb@gnome.org>
 */

#include <config.h>

#include <gladeui/glade.h>
#include <gtk/gtk.h>
#include <glib/gi18n-lib.h>
#include <string.h>

#include "glade-accels.h"

#define GLADE_RESPONSE_CLEAR 42

GList *
glade_accel_list_copy (GList * accels)
{
  GList *ret = NULL, *list;
  GladeAccelInfo *info, *dup_info;

  for (list = accels; list; list = list->next)
    {
      info = list->data;

      dup_info = g_new0 (GladeAccelInfo, 1);
      dup_info->signal = g_strdup (info->signal);
      dup_info->key = info->key;
      dup_info->modifiers = info->modifiers;

      ret = g_list_prepend (ret, dup_info);
    }

  return g_list_reverse (ret);
}

void
glade_accel_list_free (GList * accels)
{
  GList *list;
  GladeAccelInfo *info;

  for (list = accels; list; list = list->next)
    {
      info = list->data;

      g_free (info->signal);
      g_free (info);
    }
  g_list_free (accels);
}

GType
glade_accel_glist_get_type (void)
{
  static GType type_id = 0;

  if (!type_id)
    type_id = g_boxed_type_register_static
        ("GladeAccelGList",
         (GBoxedCopyFunc) glade_accel_list_copy,
         (GBoxedFreeFunc) glade_accel_list_free);
  return type_id;
}

/* This is not used to save in the glade file... and its a one-way conversion.
 * its only usefull to show the values in the UI.
 */
gchar *
glade_accels_make_string (GList * accels)
{
  GladeAccelInfo *info;
  GString *string;
  GList *list;
  gchar *accel_text;

  string = g_string_new ("");

  for (list = accels; list; list = list->next)
    {
      info = list->data;

      accel_text = gtk_accelerator_name (info->key, info->modifiers);
      g_string_append (string, accel_text);
      g_free (accel_text);

      if (list->next)
        g_string_append (string, ", ");
    }

  return g_string_free (string, FALSE);
}


/**************************************************************
 *              GladeEditorProperty stuff here
 **************************************************************/

enum
{
  ACCEL_COLUMN_SIGNAL = 0,
  ACCEL_COLUMN_REAL_SIGNAL,
  ACCEL_COLUMN_TEXT,
  ACCEL_COLUMN_WEIGHT,
  ACCEL_COLUMN_STYLE,
  ACCEL_COLUMN_FOREGROUND,
  ACCEL_COLUMN_VISIBLE,
  ACCEL_COLUMN_KEY_ENTERED,
  ACCEL_COLUMN_KEYCODE,
  ACCEL_COLUMN_MODIFIERS,
  ACCEL_NUM_COLUMNS
};

struct _GladeEPropAccel
{
  GladeEditorProperty parent_instance;

  GtkWidget *entry;
  GList *parent_iters;
  GtkTreeModel *model;
} ;

typedef struct
{
  GtkTreeIter *iter;
  const gchar *name;                  /* <-- dont free */
} GladeEpropIterTab;

GLADE_MAKE_EPROP (GladeEPropAccel, glade_eprop_accel, GLADE, EPROP_ACCEL)

static void
glade_eprop_accel_finalize (GObject * object)
{
  /* Chain up */
  GObjectClass *parent_class =
      g_type_class_peek_parent (G_OBJECT_GET_CLASS (object));

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

static void
glade_eprop_accel_load (GladeEditorProperty * eprop, GladeProperty * property)
{
  GladeEditorPropertyClass *parent_class =
      g_type_class_peek_parent (GLADE_EDITOR_PROPERTY_GET_CLASS (eprop));
  GladeEPropAccel *eprop_accel = GLADE_EPROP_ACCEL (eprop);
  gchar *accels;

  /* Chain up first */
  parent_class->load (eprop, property);

  if (property == NULL)
    return;

  if ((accels =
       glade_accels_make_string (g_value_get_boxed (glade_property_inline_value (property)))) != NULL)
    {
      gtk_entry_set_text (GTK_ENTRY (eprop_accel->entry), accels);
      g_free (accels);
    }
  else
    gtk_entry_set_text (GTK_ENTRY (eprop_accel->entry), "");

}

static gint
eprop_find_iter (GladeEpropIterTab * iter_tab, gchar * name)
{
  return strcmp (iter_tab->name, name);
}

static void
iter_tab_free (GladeEpropIterTab * iter_tab)
{
  gtk_tree_iter_free (iter_tab->iter);
  g_free (iter_tab);
}

static void
glade_eprop_accel_populate_view (GladeEditorProperty * eprop,
                                 GtkTreeView * view)
{
  GladeEPropAccel *eprop_accel = GLADE_EPROP_ACCEL (eprop);
  GladeSignalDef *sdef;
  GladePropertyDef   *pdef = glade_editor_property_get_property_def (eprop);
  GladeProperty      *property = glade_editor_property_get_property (eprop);
  GladeWidgetAdaptor *adaptor = glade_property_def_get_adaptor (pdef);
  GtkTreeStore *model = (GtkTreeStore *) gtk_tree_view_get_model (view);
  GtkTreeIter iter;
  GladeEpropIterTab *parent_tab;
  GladeAccelInfo *info;
  GList *l, *found, *accelerators;
  gchar *name, *accel_text;
  const GList *list;
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  GType type_action = GTK_TYPE_ACTION;
G_GNUC_END_IGNORE_DEPRECATIONS

  accelerators = g_value_get_boxed (glade_property_inline_value (property));

  /* First make parent iters...
   */
  for (list = glade_widget_adaptor_get_signals (adaptor); list; list = list->next)
    {
      sdef = list->data;

      /* Special case for GtkAction accelerators  */
      if (glade_widget_adaptor_get_object_type (adaptor) == type_action ||
          g_type_is_a (glade_widget_adaptor_get_object_type (adaptor), type_action))
        {
          if (g_strcmp0 (glade_signal_def_get_object_type_name (sdef), "GtkAction") != 0 ||
              g_strcmp0 (glade_signal_def_get_name (sdef), "activate") != 0)
            continue;
        }
      /* Only action signals have accelerators. */
      else if ((glade_signal_def_get_flags (sdef) & G_SIGNAL_ACTION) == 0)
        continue;

      if (g_list_find_custom (eprop_accel->parent_iters,
                              glade_signal_def_get_object_type_name (sdef),
                              (GCompareFunc) eprop_find_iter) == NULL)
        {
          gtk_tree_store_append (model, &iter, NULL);
          gtk_tree_store_set (model, &iter,
                              ACCEL_COLUMN_SIGNAL, glade_signal_def_get_object_type_name (sdef),
                              ACCEL_COLUMN_WEIGHT, PANGO_WEIGHT_BOLD,
                              ACCEL_COLUMN_VISIBLE, FALSE, -1);

          parent_tab = g_new0 (GladeEpropIterTab, 1);
          parent_tab->name = glade_signal_def_get_object_type_name (sdef);
          parent_tab->iter = gtk_tree_iter_copy (&iter);

          eprop_accel->parent_iters =
              g_list_prepend (eprop_accel->parent_iters, parent_tab);
        }
    }

  /* Now we populate...
   */
  for (list = glade_widget_adaptor_get_signals (adaptor); list; list = list->next)
    {
      sdef = list->data;

      /* Special case for GtkAction accelerators  */
      if (glade_widget_adaptor_get_object_type (adaptor) == type_action ||
          g_type_is_a (glade_widget_adaptor_get_object_type (adaptor), type_action))
        {
          if (g_strcmp0 (glade_signal_def_get_object_type_name (sdef), "GtkAction") != 0 ||
              g_strcmp0 (glade_signal_def_get_name (sdef), "activate") != 0)
            continue;
        }
      /* Only action signals have accelerators. */
      else if ((glade_signal_def_get_flags (sdef) & G_SIGNAL_ACTION) == 0)
        continue;

      if ((found = g_list_find_custom (eprop_accel->parent_iters,
                                       glade_signal_def_get_object_type_name (sdef),
                                       (GCompareFunc) eprop_find_iter)) != NULL)
        {
          parent_tab = found->data;
          name = g_strdup_printf ("    %s", glade_signal_def_get_name (sdef));

          /* Populate from accelerator list
           */
          for (l = accelerators; l; l = l->next)
            {
              info = l->data;

              if (g_strcmp0 (info->signal, glade_signal_def_get_name (sdef)))
                continue;

              accel_text = gtk_accelerator_name (info->key, info->modifiers);

              gtk_tree_store_append (model, &iter, parent_tab->iter);
              gtk_tree_store_set
                  (model, &iter,
                   ACCEL_COLUMN_SIGNAL, name,
                   ACCEL_COLUMN_REAL_SIGNAL, glade_signal_def_get_name (sdef),
                   ACCEL_COLUMN_TEXT, accel_text,
                   ACCEL_COLUMN_WEIGHT, PANGO_WEIGHT_NORMAL,
                   ACCEL_COLUMN_STYLE, PANGO_STYLE_NORMAL,
                   ACCEL_COLUMN_FOREGROUND, "Black",
                   ACCEL_COLUMN_VISIBLE, TRUE,
                   ACCEL_COLUMN_KEYCODE, info->key,
                   ACCEL_COLUMN_MODIFIERS, info->modifiers,
                   ACCEL_COLUMN_KEY_ENTERED, TRUE, -1);

              g_free (accel_text);
            }

          /* Special case for GtkAction accelerators  */
          if ((glade_widget_adaptor_get_object_type (adaptor) == type_action ||
               g_type_is_a (glade_widget_adaptor_get_object_type (adaptor), type_action)) &&
              g_list_length (accelerators) > 0)
            continue;

          /* Append a new empty slot at the end */
          gtk_tree_store_append (model, &iter, parent_tab->iter);
          gtk_tree_store_set
              (model, &iter,
               ACCEL_COLUMN_SIGNAL, name,
               ACCEL_COLUMN_REAL_SIGNAL, glade_signal_def_get_name (sdef),
               ACCEL_COLUMN_TEXT, _("<choose a key>"),
               ACCEL_COLUMN_WEIGHT, PANGO_WEIGHT_NORMAL,
               ACCEL_COLUMN_STYLE, PANGO_STYLE_ITALIC,
               ACCEL_COLUMN_FOREGROUND, "Grey",
               ACCEL_COLUMN_VISIBLE, TRUE,
               ACCEL_COLUMN_KEYCODE, 0,
               ACCEL_COLUMN_MODIFIERS, 0, ACCEL_COLUMN_KEY_ENTERED, FALSE, -1);

          g_free (name);
        }
    }
}

void
accel_edited (GtkCellRendererAccel * accel,
              gchar * path_string,
              guint accel_key,
              GdkModifierType accel_mods,
              guint hardware_keycode, GladeEPropAccel * eprop_accel)
{
  gboolean key_was_set;
  GtkTreeIter iter, parent_iter, new_iter;
  gchar *accel_text;
  GladePropertyDef *pdef;
  GladeWidgetAdaptor *adaptor;
  gboolean is_action;
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  GType type_action = GTK_TYPE_ACTION;
G_GNUC_END_IGNORE_DEPRECATIONS

  pdef = glade_editor_property_get_property_def (GLADE_EDITOR_PROPERTY (eprop_accel));
  adaptor = glade_property_def_get_adaptor (pdef);

  if (!gtk_tree_model_get_iter_from_string (eprop_accel->model,
                                            &iter, path_string))
    return;

  is_action = (glade_widget_adaptor_get_object_type (adaptor) == type_action ||
               g_type_is_a (glade_widget_adaptor_get_object_type (adaptor), type_action));

  gtk_tree_model_get (eprop_accel->model, &iter,
                      ACCEL_COLUMN_KEY_ENTERED, &key_was_set, -1);

  accel_text = gtk_accelerator_name (accel_key, accel_mods);

  gtk_tree_store_set
      (GTK_TREE_STORE (eprop_accel->model), &iter,
       ACCEL_COLUMN_KEY_ENTERED, TRUE,
       ACCEL_COLUMN_STYLE, PANGO_STYLE_NORMAL,
       ACCEL_COLUMN_FOREGROUND, "Black",
       ACCEL_COLUMN_TEXT, accel_text,
       ACCEL_COLUMN_KEYCODE, accel_key, ACCEL_COLUMN_MODIFIERS, accel_mods, -1);

  g_free (accel_text);

  /* Append a new one if needed
   */
  if (is_action == FALSE && key_was_set == FALSE &&
      gtk_tree_model_iter_parent (eprop_accel->model, &parent_iter, &iter))
    {
      gchar *signal, *real_signal;

      gtk_tree_model_get (eprop_accel->model, &iter,
                          ACCEL_COLUMN_SIGNAL, &signal,
                          ACCEL_COLUMN_REAL_SIGNAL, &real_signal, -1);

      /* Append a new empty slot at the end */
      gtk_tree_store_insert_after (GTK_TREE_STORE (eprop_accel->model),
                                   &new_iter, &parent_iter, &iter);
      gtk_tree_store_set (GTK_TREE_STORE (eprop_accel->model), &new_iter,
                          ACCEL_COLUMN_SIGNAL, signal,
                          ACCEL_COLUMN_REAL_SIGNAL, real_signal,
                          ACCEL_COLUMN_TEXT, _("<choose a key>"),
                          ACCEL_COLUMN_WEIGHT, PANGO_WEIGHT_NORMAL,
                          ACCEL_COLUMN_STYLE, PANGO_STYLE_ITALIC,
                          ACCEL_COLUMN_FOREGROUND, "Grey",
                          ACCEL_COLUMN_VISIBLE, TRUE,
                          ACCEL_COLUMN_KEYCODE, 0,
                          ACCEL_COLUMN_MODIFIERS, 0,
                          ACCEL_COLUMN_KEY_ENTERED, FALSE, -1);
      g_free (signal);
      g_free (real_signal);
    }
}

void
accel_cleared (GtkCellRendererAccel * accel,
               gchar * path_string, GladeEPropAccel * eprop_accel)
{
  GtkTreeIter iter;

  if (!gtk_tree_model_get_iter_from_string (eprop_accel->model,
                                            &iter, path_string))
    return;

  gtk_tree_store_remove (GTK_TREE_STORE (eprop_accel->model), &iter);
}


static GtkWidget *
glade_eprop_accel_view (GladeEditorProperty * eprop)
{
  GladeEPropAccel *eprop_accel = GLADE_EPROP_ACCEL (eprop);
  GtkWidget *view_widget;
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;

  eprop_accel->model = (GtkTreeModel *) 
    gtk_tree_store_new (ACCEL_NUM_COLUMNS, G_TYPE_STRING,   /* The GSignal name formatted for display */
                        G_TYPE_STRING,      /* The GSignal name */
                        G_TYPE_STRING,      /* The text to show in the accelerator cell */
                        G_TYPE_INT, /* PangoWeight attribute for bold headers */
                        G_TYPE_INT, /* PangoStyle attribute for italic grey unset items */
                        G_TYPE_STRING,      /* Foreground colour for italic grey unset items */
                        G_TYPE_BOOLEAN,     /* Visible attribute to hide items for header entries */
                        G_TYPE_BOOLEAN,     /* Whether the key has been entered for this row */
                        G_TYPE_UINT,        /* Hardware keycode */
                        G_TYPE_INT);        /* GdkModifierType */
  
  view_widget = gtk_tree_view_new_with_model (eprop_accel->model);
  gtk_tree_view_set_show_expanders (GTK_TREE_VIEW (view_widget), FALSE);
  gtk_tree_view_set_enable_search (GTK_TREE_VIEW (view_widget), FALSE);

  /********************* signal name column *********************/
  renderer = gtk_cell_renderer_text_new ();
  g_object_set (G_OBJECT (renderer), "editable", FALSE, NULL);

  column = gtk_tree_view_column_new_with_attributes
      (_("Signal"), renderer,
       "text", ACCEL_COLUMN_SIGNAL, "weight", ACCEL_COLUMN_WEIGHT, NULL);


  gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
  gtk_tree_view_append_column (GTK_TREE_VIEW (view_widget), column);

  /********************* accel editor column *********************/
  renderer = gtk_cell_renderer_accel_new ();
  g_object_set (G_OBJECT (renderer), "editable", TRUE, NULL);

  g_signal_connect (renderer, "accel-edited", G_CALLBACK (accel_edited), eprop);
  g_signal_connect (renderer, "accel-cleared",
                    G_CALLBACK (accel_cleared), eprop);

  column = gtk_tree_view_column_new_with_attributes
      (_("Accelerator Key"), renderer,
       "text", ACCEL_COLUMN_TEXT,
       "foreground", ACCEL_COLUMN_FOREGROUND,
       "style", ACCEL_COLUMN_STYLE, "visible", ACCEL_COLUMN_VISIBLE, NULL);

  gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
  gtk_tree_view_append_column (GTK_TREE_VIEW (view_widget), column);

  return view_widget;
}

static gboolean
glade_eprop_accel_accum_accelerators (GtkTreeModel * model,
                                      GtkTreePath * path,
                                      GtkTreeIter * iter, GList ** ret)
{
  GladeAccelInfo *info;
  gchar *signal;
  GdkModifierType accel_mods;
  guint accel_key;
  gboolean entered = FALSE;

  gtk_tree_model_get (model, iter, ACCEL_COLUMN_KEY_ENTERED, &entered, -1);
  if (!entered)
    return FALSE;

  gtk_tree_model_get (model, iter,
                      ACCEL_COLUMN_REAL_SIGNAL, &signal,
                      ACCEL_COLUMN_KEYCODE, &accel_key,
                      ACCEL_COLUMN_MODIFIERS, &accel_mods, -1);

  info = g_new0 (GladeAccelInfo, 1);
  info->signal = signal;
  info->key = accel_key;
  info->modifiers = accel_mods;

  *ret = g_list_prepend (*ret, info);

  return FALSE;
}


static void
glade_eprop_accel_show_dialog (GladeEditorProperty *eprop)
{
  GladeEPropAccel *eprop_accel = GLADE_EPROP_ACCEL (eprop);
  GtkWidget *dialog, *parent, *vbox, *sw, *tree_view;
  GValue value = { 0, };
  GList *accelerators = NULL;
  gint res;

  parent = gtk_widget_get_toplevel (GTK_WIDGET (eprop));

  dialog = gtk_dialog_new_with_buttons (_("Choose accelerator keys..."),
                                        GTK_WINDOW (parent),
                                        GTK_DIALOG_MODAL |
                                        GTK_DIALOG_DESTROY_WITH_PARENT,
                                        _("C_lear"), GLADE_RESPONSE_CLEAR,
                                        _("_Cancel"), GTK_RESPONSE_CANCEL,
                                        _("_OK"), GTK_RESPONSE_OK, NULL);

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
  gtk_widget_show (vbox);

  gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);

  gtk_box_pack_start (GTK_BOX
                      (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), vbox,
                      TRUE, TRUE, 0);

  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_widget_show (sw);
  gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0);
  gtk_widget_set_size_request (sw, 400, 200);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);

  tree_view = glade_eprop_accel_view (eprop);
  glade_eprop_accel_populate_view (eprop, GTK_TREE_VIEW (tree_view));

  gtk_tree_view_expand_all (GTK_TREE_VIEW (tree_view));

  gtk_widget_show (tree_view);
  gtk_container_add (GTK_CONTAINER (sw), tree_view);

  /* Run the dialog */
  res = gtk_dialog_run (GTK_DIALOG (dialog));
  if (res == GTK_RESPONSE_OK)
    {
      gtk_tree_model_foreach
          (gtk_tree_view_get_model (GTK_TREE_VIEW (tree_view)),
           (GtkTreeModelForeachFunc)
           glade_eprop_accel_accum_accelerators, &accelerators);

      accelerators = g_list_reverse (accelerators);
      g_value_init (&value, GLADE_TYPE_ACCEL_GLIST);
      g_value_take_boxed (&value, accelerators);

      glade_editor_property_commit (eprop, &value);
      g_value_unset (&value);
    }
  else if (res == GLADE_RESPONSE_CLEAR)
    {
      g_value_init (&value, GLADE_TYPE_ACCEL_GLIST);
      g_value_set_boxed (&value, NULL);

      glade_editor_property_commit (eprop, &value);

      g_value_unset (&value);
    }

  /* Clean up ...
   */
  gtk_widget_destroy (dialog);

  g_object_unref (G_OBJECT (eprop_accel->model));
  eprop_accel->model = NULL;

  if (eprop_accel->parent_iters)
    {
      g_list_foreach (eprop_accel->parent_iters, (GFunc) iter_tab_free, NULL);
      g_list_free (eprop_accel->parent_iters);
      eprop_accel->parent_iters = NULL;
    }

}

static GtkWidget *
glade_eprop_accel_create_input (GladeEditorProperty * eprop)
{
  GladeEPropAccel *eprop_accel = GLADE_EPROP_ACCEL (eprop);

  eprop_accel->entry = gtk_entry_new ();
  gtk_widget_set_valign (eprop_accel->entry, GTK_ALIGN_CENTER);
  gtk_editable_set_editable (GTK_EDITABLE (eprop_accel->entry), FALSE);
  gtk_entry_set_icon_from_icon_name (GTK_ENTRY (eprop_accel->entry),
                                     GTK_ENTRY_ICON_SECONDARY,
                                     "document-edit-symbolic");
  g_signal_connect_swapped (eprop_accel->entry, "icon-release",
                            G_CALLBACK (glade_eprop_accel_show_dialog), eprop);

  return eprop_accel->entry;
}


static GdkModifierType
glade_gtk_parse_modifiers (const gchar * string)
{
  const gchar *pos = string;
  GdkModifierType modifiers = 0;

  while (pos && pos[0])
    {
      if (!strncmp (pos, "GDK_", 4))
        {
          pos += 4;
          if (!strncmp (pos, "SHIFT_MASK", 10))
            {
              modifiers |= GDK_SHIFT_MASK;
              pos += 10;
            }
          else if (!strncmp (pos, "SUPER_MASK", 10))
            {
              modifiers |= GDK_SUPER_MASK;
              pos += 10;
            }            
          else if (!strncmp (pos, "LOCK_MASK", 9))
            {
              modifiers |= GDK_LOCK_MASK;
              pos += 9;
            }
          else if (!strncmp (pos, "CONTROL_MASK", 12))
            {
              modifiers |= GDK_CONTROL_MASK;
              pos += 12;
            }
          else if (!strncmp (pos, "MOD", 3) && !strncmp (pos + 4, "_MASK", 5))
            {
              switch (pos[3])
                {
                  case '1':
                    modifiers |= GDK_MOD1_MASK;
                    break;
                  case '2':
                    modifiers |= GDK_MOD2_MASK;
                    break;
                  case '3':
                    modifiers |= GDK_MOD3_MASK;
                    break;
                  case '4':
                    modifiers |= GDK_MOD4_MASK;
                    break;
                  case '5':
                    modifiers |= GDK_MOD5_MASK;
                    break;
                }
              pos += 9;
            }
          else if (!strncmp (pos, "BUTTON", 6) &&
                   !strncmp (pos + 7, "_MASK", 5))
            {
              switch (pos[6])
                {
                  case '1':
                    modifiers |= GDK_BUTTON1_MASK;
                    break;
                  case '2':
                    modifiers |= GDK_BUTTON2_MASK;
                    break;
                  case '3':
                    modifiers |= GDK_BUTTON3_MASK;
                    break;
                  case '4':
                    modifiers |= GDK_BUTTON4_MASK;
                    break;
                  case '5':
                    modifiers |= GDK_BUTTON5_MASK;
                    break;
                }
              pos += 12;
            }
          else if (!strncmp (pos, "RELEASE_MASK", 12))
            {
              modifiers |= GDK_RELEASE_MASK;
              pos += 12;
            }
          else
            pos++;
        }
      else
        pos++;
    }
  return modifiers;
}


static gchar *
glade_gtk_modifier_string_from_bits (GdkModifierType modifiers)
{
  GString *string = g_string_new ("");

  if (modifiers & GDK_SHIFT_MASK)
    {
      if (string->len > 0)
        g_string_append (string, " | ");
      g_string_append (string, "GDK_SHIFT_MASK");
    }

  if (modifiers & GDK_SUPER_MASK)
    {
      if (string->len > 0)
        g_string_append (string, " | ");
      g_string_append (string, "GDK_SUPER_MASK");
    }

  if (modifiers & GDK_LOCK_MASK)
    {
      if (string->len > 0)
        g_string_append (string, " | ");
      g_string_append (string, "GDK_LOCK_MASK");
    }

  if (modifiers & GDK_CONTROL_MASK)
    {
      if (string->len > 0)
        g_string_append (string, " | ");
      g_string_append (string, "GDK_CONTROL_MASK");
    }

  if (modifiers & GDK_MOD1_MASK)
    {
      if (string->len > 0)
        g_string_append (string, " | ");
      g_string_append (string, "GDK_MOD1_MASK");
    }

  if (modifiers & GDK_MOD2_MASK)
    {
      if (string->len > 0)
        g_string_append (string, " | ");
      g_string_append (string, "GDK_MOD2_MASK");
    }

  if (modifiers & GDK_MOD3_MASK)
    {
      if (string->len > 0)
        g_string_append (string, " | ");
      g_string_append (string, "GDK_MOD3_MASK");
    }

  if (modifiers & GDK_MOD4_MASK)
    {
      if (string->len > 0)
        g_string_append (string, " | ");
      g_string_append (string, "GDK_MOD4_MASK");
    }

  if (modifiers & GDK_MOD5_MASK)
    {
      if (string->len > 0)
        g_string_append (string, " | ");
      g_string_append (string, "GDK_MOD5_MASK");
    }

  if (modifiers & GDK_BUTTON1_MASK)
    {
      if (string->len > 0)
        g_string_append (string, " | ");
      g_string_append (string, "GDK_BUTTON1_MASK");
    }

  if (modifiers & GDK_BUTTON2_MASK)
    {
      if (string->len > 0)
        g_string_append (string, " | ");
      g_string_append (string, "GDK_BUTTON2_MASK");
    }

  if (modifiers & GDK_BUTTON3_MASK)
    {
      if (string->len > 0)
        g_string_append (string, " | ");
      g_string_append (string, "GDK_BUTTON3_MASK");
    }

  if (modifiers & GDK_BUTTON4_MASK)
    {
      if (string->len > 0)
        g_string_append (string, " | ");
      g_string_append (string, "GDK_BUTTON4_MASK");
    }

  if (modifiers & GDK_BUTTON5_MASK)
    {
      if (string->len > 0)
        g_string_append (string, " | ");
      g_string_append (string, "GDK_BUTTON5_MASK");
    }

  if (modifiers & GDK_RELEASE_MASK)
    {
      if (string->len > 0)
        g_string_append (string, " | ");
      g_string_append (string, "GDK_RELEASE_MASK");
    }

  if (string->len > 0)
    return g_string_free (string, FALSE);

  g_string_free (string, TRUE);
  return NULL;
}

GladeAccelInfo *
glade_accel_read (GladeXmlNode * node, gboolean require_signal)
{
  GladeAccelInfo *ainfo;
  gchar *key, *modifiers, *signal;

  g_return_val_if_fail (node != NULL, NULL);

  if (!glade_xml_node_verify (node, GLADE_TAG_ACCEL))
    return NULL;

  /* Get from xml... */
  key = glade_xml_get_property_string_required
      (node, GLADE_TAG_ACCEL_KEY, NULL);
  if (require_signal)
    signal =
        glade_xml_get_property_string_required (node, GLADE_TAG_ACCEL_SIGNAL,
                                                NULL);
  else
    signal = glade_xml_get_property_string (node, GLADE_TAG_ACCEL_SIGNAL);

  modifiers = glade_xml_get_property_string (node, GLADE_TAG_ACCEL_MODIFIERS);

  /* translate to GladeAccelInfo... */
  ainfo = g_new0 (GladeAccelInfo, 1);
  ainfo->key = gdk_keyval_from_name (key);
  ainfo->signal = signal;       /* take string ownership... */
  ainfo->modifiers = glade_gtk_parse_modifiers (modifiers);

  g_free (modifiers);

  return ainfo;
}

GladeXmlNode *
glade_accel_write (GladeAccelInfo * accel,
                   GladeXmlContext * context, gboolean write_signal)
{
  GladeXmlNode *accel_node;
  gchar *modifiers;

  g_return_val_if_fail (accel != NULL, NULL);
  g_return_val_if_fail (context != NULL, NULL);

  accel_node = glade_xml_node_new (context, GLADE_TAG_ACCEL);
  modifiers = glade_gtk_modifier_string_from_bits (accel->modifiers);

  glade_xml_node_set_property_string (accel_node, GLADE_TAG_ACCEL_KEY,
                                      gdk_keyval_name (accel->key));

  if (write_signal)
    glade_xml_node_set_property_string (accel_node, GLADE_TAG_ACCEL_SIGNAL,
                                        accel->signal);
  glade_xml_node_set_property_string (accel_node, GLADE_TAG_ACCEL_MODIFIERS,
                                      modifiers);

  g_free (modifiers);

  return accel_node;
}


void
glade_gtk_read_accels (GladeWidget * widget,
                       GladeXmlNode * node, gboolean require_signal)
{
  GladeProperty *property;
  GladeXmlNode *prop;
  GladeAccelInfo *ainfo;
  GValue *value = NULL;
  GList *accels = NULL;

  for (prop = glade_xml_node_get_children (node);
       prop; prop = glade_xml_node_next (prop))
    {
      if (!glade_xml_node_verify_silent (prop, GLADE_TAG_ACCEL))
        continue;

      if ((ainfo = glade_accel_read (prop, require_signal)) != NULL)
        accels = g_list_prepend (accels, ainfo);
    }

  if (accels)
    {
      value = g_new0 (GValue, 1);
      g_value_init (value, GLADE_TYPE_ACCEL_GLIST);
      g_value_take_boxed (value, accels);

      property = glade_widget_get_property (widget, "accelerator");
      glade_property_set_value (property, value);

      g_value_unset (value);
      g_free (value);
    }
}

void
glade_gtk_write_accels (GladeWidget * widget,
                        GladeXmlContext * context,
                        GladeXmlNode * node, gboolean write_signal)
{
  GladeXmlNode *accel_node;
  GladeProperty *property;
  GList *list;

  /* Some child widgets may have disabled the property */
  if (!(property = glade_widget_get_property (widget, "accelerator")))
    return;

  for (list = g_value_get_boxed (glade_property_inline_value (property)); list; list = list->next)
    {
      GladeAccelInfo *accel = list->data;

      accel_node = glade_accel_write (accel, context, write_signal);
      glade_xml_node_append_child (node, accel_node);
    }
}