~attente/unity-gtk-module/eclipse-accelerators

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
/*
 * Copyright 2012 Canonical Ltd.
 *
 * This program 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, version 3 of the License.
 *
 * 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Ryan Lortie <desrt@desrt.ca>
 *          William Hua <william.hua@canonical.com>
 */

#include "unity-gtk-menu-section-private.h"

#ifndef G_MENU_ATTRIBUTE_ACCEL
#define G_MENU_ATTRIBUTE_ACCEL "accel"
#endif

G_DEFINE_TYPE (UnityGtkMenuSection,
               unity_gtk_menu_section,
               G_TYPE_MENU_MODEL);

static gint
g_uintcmp (gconstpointer a,
           gconstpointer b,
           gpointer      user_data)
{
  return GPOINTER_TO_INT (a) - GPOINTER_TO_INT (b);
}

static gboolean
g_closure_equal (GtkAccelKey *key,
                 GClosure    *closure,
                 gpointer     data)
{
  return closure == data;
}

static void
unity_gtk_menu_section_set_parent_shell (UnityGtkMenuSection *section,
                                         UnityGtkMenuShell   *parent_shell)
{
  g_return_if_fail (UNITY_GTK_IS_MENU_SECTION (section));

  section->parent_shell = parent_shell;
}

static void
unity_gtk_menu_section_dispose (GObject *object)
{
  UnityGtkMenuSection *section;

  g_return_if_fail (UNITY_GTK_IS_MENU_SECTION (object));

  section = UNITY_GTK_MENU_SECTION (object);

  unity_gtk_menu_section_set_parent_shell (section, NULL);

  G_OBJECT_CLASS (unity_gtk_menu_section_parent_class)->dispose (object);
}

static gboolean
unity_gtk_menu_section_is_mutable (GMenuModel *model)
{
  g_return_val_if_fail (UNITY_GTK_IS_MENU_SECTION (model), TRUE);

  return TRUE;
}

static gint
unity_gtk_menu_section_get_n_items (GMenuModel *model)
{
  UnityGtkMenuSection *section;
  GSequenceIter *begin;
  GSequenceIter *end;

  g_return_val_if_fail (UNITY_GTK_IS_MENU_SECTION (model), 0);

  section = UNITY_GTK_MENU_SECTION (model);
  begin = unity_gtk_menu_section_get_begin_iter (section);
  end = unity_gtk_menu_section_get_end_iter (section);

  g_return_val_if_fail (begin != NULL && end != NULL, 0);

  return g_sequence_iter_get_position (end) - g_sequence_iter_get_position (begin);
}

static void
unity_gtk_menu_section_get_item_attributes (GMenuModel  *model,
                                            gint         item_index,
                                            GHashTable **attributes)
{
  UnityGtkMenuSection *section;
  UnityGtkMenuShell *parent_shell;
  UnityGtkMenuItem *item;
  GSequenceIter *iter;
  guint index;
  const gchar *label;
  GIcon *icon;
  UnityGtkAction *action;

  g_return_if_fail (UNITY_GTK_IS_MENU_SECTION (model));
  g_return_if_fail (attributes != NULL);

  section = UNITY_GTK_MENU_SECTION (model);
  parent_shell = section->parent_shell;

  g_return_if_fail (parent_shell != NULL);

  iter = unity_gtk_menu_section_get_iter (section, item_index);
  index = GPOINTER_TO_UINT (g_sequence_get (iter));
  item = unity_gtk_menu_shell_get_item (parent_shell, index);
  label = unity_gtk_menu_item_get_label (item);
  icon = unity_gtk_menu_item_get_icon (item);
  action = item->action;

  *attributes = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) g_variant_unref);

  if (label != NULL)
    g_hash_table_insert (*attributes, G_MENU_ATTRIBUTE_LABEL, g_variant_ref_sink (g_variant_new_string (label)));

  if (icon != NULL)
    {
      g_hash_table_insert (*attributes, G_MENU_ATTRIBUTE_ICON, g_icon_serialize (icon));
      g_object_unref (icon);
    }

  if (action != NULL && action->name != NULL)
    {
      gchar *name = g_strdup_printf ("unity.%s", action->name);
      GVariant *variant = g_variant_ref_sink (g_variant_new_string (name));

      g_hash_table_insert (*attributes, G_MENU_ATTRIBUTE_ACTION, variant);

      if (action->items_by_name != NULL)
        {
          GHashTableIter iter;
          gpointer key;
          gpointer value;
          const gchar *target = NULL;

          g_hash_table_iter_init (&iter, action->items_by_name);
          while (target == NULL && g_hash_table_iter_next (&iter, &key, &value))
            if (value == item)
              target = key;

          if (target != NULL)
            g_hash_table_insert (*attributes, G_MENU_ATTRIBUTE_TARGET, g_variant_ref_sink (g_variant_new_string (target)));
        }
      else if (unity_gtk_menu_item_get_draw_as_radio (item))
        g_hash_table_insert (*attributes, G_MENU_ATTRIBUTE_TARGET, g_variant_ref_sink (g_variant_new_string (action->name)));

      g_free (name);
    }

  if (item->menu_item != NULL)
    {
      gchar *accel_name = NULL;
      const gchar *accel_path = gtk_menu_item_get_accel_path (item->menu_item);

      if (accel_path != NULL)
        {
          GtkAccelKey accel_key;

          if (gtk_accel_map_lookup_entry (accel_path, &accel_key))
            accel_name = gtk_accelerator_name (accel_key.accel_key, accel_key.accel_mods);
        }

      if (accel_name == NULL)
        {
          GList *closures = gtk_widget_list_accel_closures (GTK_WIDGET (item->menu_item));
          GList *iter;

          for (iter = closures; iter != NULL && accel_name == NULL; iter = g_list_next (iter))
            {
              GClosure *closure = iter->data;
              GtkAccelGroup *accel_group = gtk_accel_group_from_accel_closure (closure);

              if (accel_group != NULL)
                {
                  GtkAccelKey *accel_key = gtk_accel_group_find (accel_group, g_closure_equal, closure);

                  if (accel_key != NULL)
                    accel_name = gtk_accelerator_name (accel_key->accel_key, accel_key->accel_mods);
                }
            }

          g_list_free (closures);
        }

      if (accel_name == NULL)
        accel_name = g_strdup (gtk_menu_item_get_nth_label_label (item->menu_item, 1));

      if (accel_name != NULL)
        g_hash_table_insert (*attributes, G_MENU_ATTRIBUTE_ACCEL, g_variant_ref_sink (g_variant_new_string (accel_name)));

      g_free (accel_name);
    }
}

static void
unity_gtk_menu_section_get_item_links (GMenuModel  *model,
                                       gint         item_index,
                                       GHashTable **links)
{
  UnityGtkMenuSection *section;
  UnityGtkMenuShell *parent_shell;
  UnityGtkMenuItem *item;
  GSequenceIter *iter;
  guint index;
  UnityGtkMenuShell *child_shell;

  g_return_if_fail (UNITY_GTK_IS_MENU_SECTION (model));
  g_return_if_fail (links != NULL);

  section = UNITY_GTK_MENU_SECTION (model);
  parent_shell = section->parent_shell;

  g_return_if_fail (parent_shell != NULL);

  iter = unity_gtk_menu_section_get_iter (section, item_index);
  index = GPOINTER_TO_UINT (g_sequence_get (iter));
  item = unity_gtk_menu_shell_get_item (parent_shell, index);
  child_shell = unity_gtk_menu_item_get_child_shell (item);

  *links = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);

  if (child_shell != NULL)
    g_hash_table_insert (*links, G_MENU_LINK_SUBMENU, g_object_ref (child_shell));
}

static void
unity_gtk_menu_section_class_init (UnityGtkMenuSectionClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GMenuModelClass *menu_model_class = G_MENU_MODEL_CLASS (klass);

  object_class->dispose = unity_gtk_menu_section_dispose;
  menu_model_class->is_mutable = unity_gtk_menu_section_is_mutable;
  menu_model_class->get_n_items = unity_gtk_menu_section_get_n_items;
  menu_model_class->get_item_attributes = unity_gtk_menu_section_get_item_attributes;
  menu_model_class->get_item_links = unity_gtk_menu_section_get_item_links;
}

static void
unity_gtk_menu_section_init (UnityGtkMenuSection *self)
{
}

UnityGtkMenuSection *
unity_gtk_menu_section_new (UnityGtkMenuShell *parent_shell,
                            guint              section_index)
{
  UnityGtkMenuSection *section = g_object_new (UNITY_GTK_TYPE_MENU_SECTION, NULL);

  unity_gtk_menu_section_set_parent_shell (section, parent_shell);
  section->section_index = section_index;

  return section;
}

GSequenceIter *
unity_gtk_menu_section_get_begin_iter (UnityGtkMenuSection *section)
{
  UnityGtkMenuShell *parent_shell;
  GSequence *separator_indices;
  GSequence *visible_indices;
  GSequenceIter *separator_iter;
  GSequenceIter *visible_iter;
  guint section_index;

  g_return_val_if_fail (UNITY_GTK_IS_MENU_SECTION (section), NULL);

  parent_shell = section->parent_shell;

  g_return_val_if_fail (parent_shell != NULL, NULL);

  separator_indices = unity_gtk_menu_shell_get_separator_indices (parent_shell);
  visible_indices = unity_gtk_menu_shell_get_visible_indices (parent_shell);
  section_index = section->section_index;

  if (section_index > 0)
    separator_iter = g_sequence_get_iter_at_pos (separator_indices, section_index - 1);
  else
    separator_iter = NULL;

  if (separator_iter != NULL)
    {
      gpointer separator_index = g_sequence_get (separator_iter);
      visible_iter = g_sequence_lookup (visible_indices, separator_index, g_uintcmp, NULL);
      visible_iter = g_sequence_iter_next (visible_iter);
    }
  else
    visible_iter = g_sequence_get_begin_iter (visible_indices);

  return visible_iter;
}

GSequenceIter *
unity_gtk_menu_section_get_end_iter (UnityGtkMenuSection *section)
{
  UnityGtkMenuShell *parent_shell;
  GSequence *separator_indices;
  GSequence *visible_indices;
  GSequenceIter *separator_iter;
  GSequenceIter *visible_iter;

  g_return_val_if_fail (UNITY_GTK_IS_MENU_SECTION (section), NULL);

  parent_shell = section->parent_shell;

  g_return_val_if_fail (parent_shell != NULL, NULL);

  separator_indices = unity_gtk_menu_shell_get_separator_indices (parent_shell);
  visible_indices = unity_gtk_menu_shell_get_visible_indices (parent_shell);
  separator_iter = g_sequence_get_iter_at_pos (separator_indices, section->section_index);

  if (g_sequence_iter_is_end (separator_iter))
    separator_iter = NULL;

  if (separator_iter != NULL)
    visible_iter = g_sequence_lookup (visible_indices, g_sequence_get (separator_iter), g_uintcmp, NULL);
  else
    visible_iter = g_sequence_get_end_iter (visible_indices);

  return visible_iter;
}

GSequenceIter *
unity_gtk_menu_section_get_iter (UnityGtkMenuSection *section,
                                 guint                index)
{
  g_return_val_if_fail (UNITY_GTK_IS_MENU_SECTION (section), NULL);

  return g_sequence_iter_move (unity_gtk_menu_section_get_begin_iter (section), index);
}

void
unity_gtk_menu_section_print (UnityGtkMenuSection *section,
                              guint                indent)
{
  gchar *space;

  g_return_if_fail (section == NULL || UNITY_GTK_IS_MENU_SECTION (section));

  space = g_strnfill (indent, ' ');

  if (section != NULL)
    {
      g_print ("%s%u (%s *) %p\n", space, section->section_index, G_OBJECT_CLASS_NAME (G_OBJECT_GET_CLASS (section)), section);

      if (section->parent_shell != NULL)
        g_print ("%s  (%s *) %p\n", space, G_OBJECT_CLASS_NAME (G_OBJECT_GET_CLASS (section->parent_shell)), section->parent_shell);
    }
  else
    g_print ("%sNULL\n", space);

  g_free (space);
}