2 * Copyright © 2011 Red Hat, Inc.
3 * Copyright © 2011 Canonical Limited
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the licence, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Matthias Clasen <mclasen@redhat.com>
21 * Ryan Lortie <desrt@desrt.ca>
26 #include "gtkmodelmenu.h"
29 #include "gtkmenubar.h"
30 #include "gtkseparatormenuitem.h"
31 #include "gtkmodelmenuitem.h"
34 GActionObservable *actions;
36 GtkAccelGroup *accels;
40 gboolean with_separators;
42 } GtkModelMenuBinding;
45 gtk_model_menu_binding_items_changed (GMenuModel *model,
50 static void gtk_model_menu_binding_append_model (GtkModelMenuBinding *binding,
52 gboolean with_separators);
55 gtk_model_menu_binding_free (gpointer data)
57 GtkModelMenuBinding *binding = data;
59 /* disconnect all existing signal handlers */
60 while (binding->connected)
62 g_signal_handlers_disconnect_by_func (binding->connected->data, gtk_model_menu_binding_items_changed, binding);
63 g_object_unref (binding->connected->data);
65 binding->connected = g_slist_delete_link (binding->connected, binding->connected);
68 g_object_unref (binding->actions);
69 g_object_unref (binding->model);
73 gtk_model_menu_binding_append_item (GtkModelMenuBinding *binding,
80 if ((section = g_menu_model_get_item_link (model, item_index, "section")))
82 g_menu_model_get_item_attribute (model, item_index, "label", "s", &heading);
83 gtk_model_menu_binding_append_model (binding, section, FALSE);
89 item = gtk_model_menu_item_new (model, item_index, binding->actions, binding->accels);
90 gtk_menu_shell_append (binding->shell, GTK_WIDGET (item));
91 gtk_widget_show (GTK_WIDGET (item));
97 gtk_model_menu_binding_append_model (GtkModelMenuBinding *binding,
99 gboolean with_separators)
103 g_signal_connect (model, "items-changed", G_CALLBACK (gtk_model_menu_binding_items_changed), binding);
104 binding->connected = g_slist_prepend (binding->connected, g_object_ref (model));
106 /* Deciding if we should show a separator is a bit difficult. There
107 * are two types of separators:
109 * - section headings (when sections have 'label' property)
111 * - normal separators automatically put between sections
113 * The easiest way to think about it is that a section usually has a
114 * separator (or heading) immediately before it.
116 * There are three exceptions to this general rule:
118 * - empty sections don't get separators or headings
120 * - sections only get separators and headings at the toplevel of a
121 * menu (ie: no separators on nested sections or in menubars)
123 * - the first section in the menu doesn't get a normal separator,
124 * but it can get a header (if it's not empty)
126 * Unfortunately, we cannot simply check the size of the section in
127 * order to determine if we should place a header: the section may
128 * contain other sections that are themselves empty. Instead, we need
129 * to append the section, and check if we ended up with any actual
130 * content. If we did, then we need to insert before that content.
131 * We use 'our_position' to keep track of this.
134 n = g_menu_model_get_n_items (model);
136 for (i = 0; i < n; i++)
138 gint our_position = binding->n_items;
139 gchar *heading = NULL;
141 gtk_model_menu_binding_append_item (binding, model, i, &heading);
143 if (with_separators && our_position < binding->n_items)
145 GtkWidget *separator = NULL;
149 separator = gtk_menu_item_new_with_label (heading);
150 gtk_widget_set_sensitive (separator, FALSE);
152 else if (our_position > 0)
153 separator = gtk_separator_menu_item_new ();
157 gtk_menu_shell_insert (binding->shell, separator, our_position);
158 gtk_widget_show (separator);
168 gtk_model_menu_binding_populate (GtkModelMenuBinding *binding)
172 /* remove current children */
173 children = gtk_container_get_children (GTK_CONTAINER (binding->shell));
176 gtk_container_remove (GTK_CONTAINER (binding->shell), children->data);
177 children = g_list_delete_link (children, children);
180 binding->n_items = 0;
182 /* add new items from the model */
183 gtk_model_menu_binding_append_model (binding, binding->model, binding->with_separators);
187 gtk_model_menu_binding_handle_changes (gpointer user_data)
189 GtkModelMenuBinding *binding = user_data;
191 /* disconnect all existing signal handlers */
192 while (binding->connected)
194 g_signal_handlers_disconnect_by_func (binding->connected->data, gtk_model_menu_binding_items_changed, binding);
195 g_object_unref (binding->connected->data);
197 binding->connected = g_slist_delete_link (binding->connected, binding->connected);
200 gtk_model_menu_binding_populate (binding);
202 binding->update_idle = 0;
204 g_object_unref (binding->shell);
206 return G_SOURCE_REMOVE;
210 gtk_model_menu_binding_items_changed (GMenuModel *model,
216 GtkModelMenuBinding *binding = user_data;
218 if (binding->update_idle == 0)
220 binding->update_idle = gdk_threads_add_idle (gtk_model_menu_binding_handle_changes, user_data);
221 g_object_ref (binding->shell);
226 gtk_model_menu_bind (GtkMenuShell *shell,
228 GActionObservable *actions,
229 GtkAccelGroup *accels,
230 gboolean with_separators)
232 GtkModelMenuBinding *binding;
234 binding = g_slice_new (GtkModelMenuBinding);
235 binding->model = g_object_ref (model);
236 binding->actions = g_object_ref (actions);
237 binding->accels = accels;
238 binding->shell = shell;
239 binding->update_idle = 0;
240 binding->connected = NULL;
241 binding->with_separators = with_separators;
243 g_object_set_data_full (G_OBJECT (shell), "gtk-model-menu-binding", binding, gtk_model_menu_binding_free);
244 gtk_model_menu_binding_populate (binding);
248 gtk_model_menu_create_menu (GMenuModel *model,
249 GActionObservable *actions,
250 GtkAccelGroup *accels)
254 menu = gtk_menu_new ();
255 gtk_menu_set_accel_group (GTK_MENU (menu), accels);
256 gtk_model_menu_bind (GTK_MENU_SHELL (menu), model, actions, accels, TRUE);
262 gtk_model_menu_create_menu_bar (GMenuModel *model,
263 GActionObservable *actions,
264 GtkAccelGroup *accels)
268 menubar = gtk_menu_bar_new ();
269 gtk_model_menu_bind (GTK_MENU_SHELL (menubar), model, actions, accels, FALSE);