~indicator-applet-developers/indicator-appmenu/trunk.13.04

« back to all changes in this revision

Viewing changes to src/gtkmodelmenuitem.c

  • Committer: Ted Gould
  • Date: 2012-03-21 14:46:04 UTC
  • mfrom: (166.3.59 gmenumodel-menus)
  • Revision ID: ted@gould.cx-20120321144604-r4limdyast215291
Adding GMenuModel support for menus

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2011 Canonical Limited
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the licence, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 
16
 *
 
17
 * Author: Ryan Lortie <desrt@desrt.ca>
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
 
 
22
#include "gtkmodelmenuitem.h"
 
23
 
 
24
#include "gtkmodelmenu.h"
 
25
 
 
26
struct _GtkModelMenuItem
 
27
{
 
28
  GtkCheckMenuItem parent_instance;
 
29
 
 
30
  GActionGroup *actions;
 
31
  const gchar *action_name;
 
32
  gboolean has_indicator;
 
33
  gboolean can_activate;
 
34
  GVariant *target;
 
35
};
 
36
 
 
37
typedef GtkCheckMenuItemClass GtkModelMenuItemClass;
 
38
 
 
39
static void gtk_model_menu_item_observer_iface_init (GActionObserverInterface *iface);
 
40
G_DEFINE_TYPE_WITH_CODE (GtkModelMenuItem, gtk_model_menu_item, GTK_TYPE_CHECK_MENU_ITEM,
 
41
                         G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_OBSERVER, gtk_model_menu_item_observer_iface_init))
 
42
 
 
43
static void
 
44
gtk_model_menu_item_activate (GtkMenuItem *menu_item)
 
45
{
 
46
  GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (menu_item);
 
47
 
 
48
  if (item->can_activate)
 
49
    g_action_group_activate_action (item->actions, item->action_name, item->target);
 
50
}
 
51
 
 
52
static void
 
53
gtk_model_menu_item_toggle_size_request (GtkMenuItem *menu_item,
 
54
                                         gint        *requisition)
 
55
{
 
56
  GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (menu_item);
 
57
 
 
58
  if (item->has_indicator)
 
59
    GTK_MENU_ITEM_CLASS (gtk_model_menu_item_parent_class)
 
60
      ->toggle_size_request (menu_item, requisition);
 
61
 
 
62
  else
 
63
    *requisition = 0;
 
64
}
 
65
 
 
66
static void
 
67
gtk_model_menu_item_draw_indicator (GtkCheckMenuItem *check_item,
 
68
                                    cairo_t          *cr)
 
69
{
 
70
  GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (check_item);
 
71
 
 
72
  if (item->has_indicator)
 
73
    GTK_CHECK_MENU_ITEM_CLASS (gtk_model_menu_item_parent_class)
 
74
      ->draw_indicator (check_item, cr);
 
75
}
 
76
 
 
77
static void
 
78
gtk_model_menu_item_set_active (GtkModelMenuItem *item,
 
79
                                gboolean          active)
 
80
{
 
81
  GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (item);
 
82
 
 
83
  if (gtk_check_menu_item_get_active (checkitem) != active)
 
84
    {
 
85
      // TODO _gtk_check_menu_item_set_active (checkitem, active);
 
86
      g_object_notify (G_OBJECT (checkitem), "active");
 
87
      gtk_check_menu_item_toggled (checkitem);
 
88
      gtk_widget_queue_draw (GTK_WIDGET (item));
 
89
    }
 
90
}
 
91
 
 
92
static void
 
93
gtk_model_menu_item_action_added (GActionObserver    *observer,
 
94
                                  GActionObservable  *observable,
 
95
                                  const gchar        *action_name,
 
96
                                  const GVariantType *parameter_type,
 
97
                                  gboolean            enabled,
 
98
                                  GVariant           *state)
 
99
{
 
100
  GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (observer);
 
101
 
 
102
  /* we can only activate the item if we have the correct type of parameter */
 
103
  item->can_activate = (item->target == NULL && parameter_type == NULL) ||
 
104
                       (item->target != NULL && parameter_type != NULL &&
 
105
                        g_variant_is_of_type (item->target, parameter_type));
 
106
 
 
107
  if (item->can_activate)
 
108
    {
 
109
      if (item->target != NULL && state != NULL)
 
110
        {
 
111
          /* actions with states and targets are radios */
 
112
          gboolean selected;
 
113
 
 
114
          selected = g_variant_equal (state, item->target);
 
115
          gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (item), TRUE);
 
116
          gtk_model_menu_item_set_active (item, selected);
 
117
          item->has_indicator = TRUE;
 
118
        }
 
119
 
 
120
      else if (state != NULL && g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN))
 
121
        {
 
122
          /* boolean state actions without target are checks */
 
123
          gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (item), FALSE);
 
124
          gtk_model_menu_item_set_active (item, g_variant_get_boolean (state));
 
125
          item->has_indicator = TRUE;
 
126
        }
 
127
 
 
128
      else
 
129
        {
 
130
          /* stateless items are just plain actions */
 
131
          gtk_model_menu_item_set_active (item, FALSE);
 
132
          item->has_indicator = FALSE;
 
133
        }
 
134
 
 
135
      gtk_widget_set_sensitive (GTK_WIDGET (item), enabled);
 
136
      gtk_widget_queue_resize (GTK_WIDGET (item));
 
137
    }
 
138
}
 
139
 
 
140
static void
 
141
gtk_model_menu_item_action_enabled_changed (GActionObserver   *observer,
 
142
                                            GActionObservable *observable,
 
143
                                            const gchar       *action_name,
 
144
                                            gboolean           enabled)
 
145
{
 
146
  GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (observer);
 
147
 
 
148
  if (!item->can_activate)
 
149
    return;
 
150
 
 
151
  gtk_widget_set_sensitive (GTK_WIDGET (item), item->can_activate && enabled);
 
152
}
 
153
 
 
154
static void
 
155
gtk_model_menu_item_action_state_changed (GActionObserver   *observer,
 
156
                                          GActionObservable *observable,
 
157
                                          const gchar       *action_name,
 
158
                                          GVariant          *state)
 
159
{
 
160
  GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (observer);
 
161
 
 
162
  if (!item->can_activate)
 
163
    return;
 
164
 
 
165
  if (item->target)
 
166
    gtk_model_menu_item_set_active (item, g_variant_equal (state, item->target));
 
167
 
 
168
  else if (g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN))
 
169
    gtk_model_menu_item_set_active (item, g_variant_get_boolean (state));
 
170
}
 
171
 
 
172
static void
 
173
gtk_model_menu_item_action_removed (GActionObserver   *observer,
 
174
                                    GActionObservable *observable,
 
175
                                    const gchar       *action_name)
 
176
{
 
177
  GtkModelMenuItem *item = GTK_MODEL_MENU_ITEM (observer);
 
178
 
 
179
  if (!item->can_activate)
 
180
    return;
 
181
 
 
182
  gtk_widget_set_sensitive (GTK_WIDGET (item), FALSE);
 
183
  gtk_model_menu_item_set_active (item, FALSE);
 
184
  item->has_indicator = FALSE;
 
185
 
 
186
  gtk_widget_queue_resize (GTK_WIDGET (item));
 
187
}
 
188
 
 
189
gchar *
 
190
_gtk_accel_path_for_action (const gchar *action_name,
 
191
                            GVariant    *parameter)
 
192
{
 
193
  GString *s;
 
194
 
 
195
  s = g_string_new ("<GAction>/");
 
196
  g_string_append (s, action_name);
 
197
  if (parameter)
 
198
    {    
 
199
      g_string_append_c (s, '/');
 
200
      g_variant_print_string (parameter, s, FALSE);
 
201
    }    
 
202
  return g_string_free (s, FALSE);
 
203
}
 
204
 
 
205
static void
 
206
gtk_model_menu_item_setup (GtkModelMenuItem  *item,
 
207
                           GMenuModel        *model,
 
208
                           gint               item_index,
 
209
                           GActionObservable *actions,
 
210
                           GtkAccelGroup     *accels)
 
211
{
 
212
  GMenuAttributeIter *iter;
 
213
  GMenuModel *submenu;
 
214
  const gchar *key;
 
215
  GVariant *value;
 
216
 
 
217
  if ((submenu = g_menu_model_get_item_link (model, item_index, "submenu")))
 
218
    {
 
219
      gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), gtk_model_menu_create_menu (submenu, actions, accels));
 
220
      g_object_unref (submenu);
 
221
    }
 
222
 
 
223
  iter = g_menu_model_iterate_item_attributes (model, item_index);
 
224
  while (g_menu_attribute_iter_get_next (iter, &key, &value))
 
225
    {
 
226
      if (g_str_equal (key, "label") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
 
227
        gtk_menu_item_set_label (GTK_MENU_ITEM (item), g_variant_get_string (value, NULL));
 
228
 
 
229
      else if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
 
230
        item->action_name = g_variant_get_string (value, NULL);
 
231
 
 
232
      else if (g_str_equal (key, "target"))
 
233
        item->target = g_variant_ref (value);
 
234
 
 
235
      g_variant_unref (value);
 
236
    }
 
237
  g_object_unref (iter);
 
238
 
 
239
  gtk_menu_item_set_use_underline (GTK_MENU_ITEM (item), TRUE);
 
240
 
 
241
  if (item->action_name)
 
242
    {
 
243
      const GVariantType *type;
 
244
      gboolean enabled;
 
245
      GVariant *state;
 
246
      gchar *path;
 
247
 
 
248
      /* observer already causes us to hold a hard ref on the group */
 
249
      item->actions = G_ACTION_GROUP (actions);
 
250
 
 
251
      if (actions)
 
252
        {
 
253
          g_action_observable_register_observer (actions, item->action_name, G_ACTION_OBSERVER (item));
 
254
 
 
255
          if (g_action_group_query_action (G_ACTION_GROUP (actions), item->action_name, &enabled, &type, NULL, NULL, &state))
 
256
            {
 
257
              gtk_model_menu_item_action_added (G_ACTION_OBSERVER (item), actions, item->action_name, type, enabled, state);
 
258
              if (state != NULL)
 
259
                g_variant_unref (state);
 
260
            }
 
261
          else
 
262
            gtk_widget_set_sensitive (GTK_WIDGET (item), FALSE);
 
263
        }
 
264
      else
 
265
        gtk_widget_set_sensitive (GTK_WIDGET (item), FALSE);
 
266
 
 
267
      path = _gtk_accel_path_for_action (item->action_name, item->target);
 
268
      gtk_menu_item_set_accel_path (GTK_MENU_ITEM (item), path);
 
269
      g_free (path);
 
270
    }
 
271
}
 
272
 
 
273
static void
 
274
gtk_model_menu_item_finalize (GObject *object)
 
275
{
 
276
  G_OBJECT_CLASS (gtk_model_menu_item_parent_class)
 
277
    ->finalize (object);
 
278
}
 
279
 
 
280
static void
 
281
gtk_model_menu_item_init (GtkModelMenuItem *item)
 
282
{
 
283
}
 
284
 
 
285
static void
 
286
gtk_model_menu_item_observer_iface_init (GActionObserverInterface *iface)
 
287
{
 
288
  iface->action_added = gtk_model_menu_item_action_added;
 
289
  iface->action_enabled_changed = gtk_model_menu_item_action_enabled_changed;
 
290
  iface->action_state_changed = gtk_model_menu_item_action_state_changed;
 
291
  iface->action_removed = gtk_model_menu_item_action_removed;
 
292
}
 
293
 
 
294
static void
 
295
gtk_model_menu_item_class_init (GtkModelMenuItemClass *class)
 
296
{
 
297
  GtkCheckMenuItemClass *check_class = GTK_CHECK_MENU_ITEM_CLASS (class);
 
298
  GtkMenuItemClass *item_class = GTK_MENU_ITEM_CLASS (class);
 
299
  GObjectClass *object_class = G_OBJECT_CLASS (class);
 
300
 
 
301
  check_class->draw_indicator = gtk_model_menu_item_draw_indicator;
 
302
 
 
303
  item_class->activate = gtk_model_menu_item_activate;
 
304
  item_class->toggle_size_request = gtk_model_menu_item_toggle_size_request;
 
305
 
 
306
  object_class->finalize = gtk_model_menu_item_finalize;
 
307
}
 
308
 
 
309
GtkMenuItem *
 
310
gtk_model_menu_item_new (GMenuModel        *model,
 
311
                         gint               item_index,
 
312
                         GActionObservable *actions,
 
313
                         GtkAccelGroup     *accels)
 
314
{
 
315
  GtkModelMenuItem *item;
 
316
 
 
317
  item = g_object_new (GTK_TYPE_MODEL_MENU_ITEM, NULL);
 
318
 
 
319
  gtk_model_menu_item_setup (item, model, item_index, actions, accels);
 
320
 
 
321
  return GTK_MENU_ITEM (item);
 
322
}