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, see <http://www.gnu.org/licenses/>.
18 * Author: Matthias Clasen <mclasen@redhat.com>
19 * Ryan Lortie <desrt@desrt.ca>
24 #include "gtkmodelmenu.h"
27 #include "gtkmenubar.h"
28 #include "gtkseparatormenuitem.h"
29 #include "gtkmodelmenuitem.h"
30 #include "gtkapplicationprivate.h"
33 GActionObservable *actions;
35 GtkAccelGroup *accels;
39 gboolean with_separators;
41 } GtkModelMenuBinding;
44 gtk_model_menu_binding_items_changed (GMenuModel *model,
49 static void gtk_model_menu_binding_append_model (GtkModelMenuBinding *binding,
51 gboolean with_separators);
54 gtk_model_menu_binding_free (gpointer data)
56 GtkModelMenuBinding *binding = data;
58 /* disconnect all existing signal handlers */
59 while (binding->connected)
61 g_signal_handlers_disconnect_by_func (binding->connected->data, gtk_model_menu_binding_items_changed, binding);
62 g_object_unref (binding->connected->data);
64 binding->connected = g_slist_delete_link (binding->connected, binding->connected);
68 g_object_unref (binding->actions);
69 g_object_unref (binding->model);
71 g_slice_free (GtkModelMenuBinding, binding);
75 gtk_model_menu_binding_append_item (GtkModelMenuBinding *binding,
82 if ((section = g_menu_model_get_item_link (model, item_index, "section")))
84 g_menu_model_get_item_attribute (model, item_index, "label", "s", heading);
85 gtk_model_menu_binding_append_model (binding, section, FALSE);
91 item = gtk_model_menu_item_new (model, item_index, binding->actions, binding->accels);
92 gtk_menu_shell_append (binding->shell, GTK_WIDGET (item));
93 gtk_widget_show (GTK_WIDGET (item));
99 gtk_model_menu_binding_append_model (GtkModelMenuBinding *binding,
101 gboolean with_separators)
105 g_signal_connect (model, "items-changed", G_CALLBACK (gtk_model_menu_binding_items_changed), binding);
106 binding->connected = g_slist_prepend (binding->connected, g_object_ref (model));
108 /* Deciding if we should show a separator is a bit difficult. There
109 * are two types of separators:
111 * - section headings (when sections have 'label' property)
113 * - normal separators automatically put between sections
115 * The easiest way to think about it is that a section usually has a
116 * separator (or heading) immediately before it.
118 * There are three exceptions to this general rule:
120 * - empty sections don't get separators or headings
122 * - sections only get separators and headings at the toplevel of a
123 * menu (ie: no separators on nested sections or in menubars)
125 * - the first section in the menu doesn't get a normal separator,
126 * but it can get a header (if it's not empty)
128 * Unfortunately, we cannot simply check the size of the section in
129 * order to determine if we should place a header: the section may
130 * contain other sections that are themselves empty. Instead, we need
131 * to append the section, and check if we ended up with any actual
132 * content. If we did, then we need to insert before that content.
133 * We use 'our_position' to keep track of this.
136 n = g_menu_model_get_n_items (model);
138 for (i = 0; i < n; i++)
140 gint our_position = binding->n_items;
141 gchar *heading = NULL;
143 gtk_model_menu_binding_append_item (binding, model, i, &heading);
145 if (with_separators && our_position < binding->n_items)
147 GtkWidget *separator = NULL;
151 separator = gtk_menu_item_new_with_label (heading);
152 gtk_widget_set_sensitive (separator, FALSE);
154 else if (our_position > 0)
155 separator = gtk_separator_menu_item_new ();
159 gtk_menu_shell_insert (binding->shell, separator, our_position);
160 gtk_widget_show (separator);
170 gtk_model_menu_binding_populate (GtkModelMenuBinding *binding)
174 /* remove current children */
175 children = gtk_container_get_children (GTK_CONTAINER (binding->shell));
178 gtk_container_remove (GTK_CONTAINER (binding->shell), children->data);
179 children = g_list_delete_link (children, children);
182 binding->n_items = 0;
184 /* add new items from the model */
185 gtk_model_menu_binding_append_model (binding, binding->model, binding->with_separators);
189 gtk_model_menu_binding_handle_changes (gpointer user_data)
191 GtkModelMenuBinding *binding = user_data;
193 /* disconnect all existing signal handlers */
194 while (binding->connected)
196 g_signal_handlers_disconnect_by_func (binding->connected->data, gtk_model_menu_binding_items_changed, binding);
197 g_object_unref (binding->connected->data);
199 binding->connected = g_slist_delete_link (binding->connected, binding->connected);
202 gtk_model_menu_binding_populate (binding);
204 binding->update_idle = 0;
206 g_object_unref (binding->shell);
208 return G_SOURCE_REMOVE;
212 gtk_model_menu_binding_items_changed (GMenuModel *model,
218 GtkModelMenuBinding *binding = user_data;
220 if (binding->update_idle == 0)
222 binding->update_idle = gdk_threads_add_idle (gtk_model_menu_binding_handle_changes, user_data);
223 g_object_ref (binding->shell);
228 gtk_model_menu_bind (GtkMenuShell *shell,
230 gboolean with_separators)
232 GtkModelMenuBinding *binding;
234 binding = g_slice_new (GtkModelMenuBinding);
235 binding->model = g_object_ref (model);
236 binding->actions = NULL;
237 binding->accels = NULL;
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);
248 gtk_model_menu_populate (GtkMenuShell *shell,
249 GActionObservable *actions,
250 GtkAccelGroup *accels)
252 GtkModelMenuBinding *binding;
254 binding = (GtkModelMenuBinding*) g_object_get_data (G_OBJECT (shell), "gtk-model-menu-binding");
256 binding->actions = g_object_ref (actions);
257 binding->accels = accels;
259 gtk_model_menu_binding_populate (binding);
263 gtk_model_menu_create_menu (GMenuModel *model,
264 GActionObservable *actions,
265 GtkAccelGroup *accels)
269 menu = gtk_menu_new ();
270 gtk_menu_set_accel_group (GTK_MENU (menu), accels);
272 gtk_model_menu_bind (GTK_MENU_SHELL (menu), model, TRUE);
273 gtk_model_menu_populate (GTK_MENU_SHELL (menu), actions, accels);
279 notify_attach (GtkMenu *menu,
285 GActionObservable *actions;
286 GtkAccelGroup *accels;
288 widget = gtk_menu_get_attach_widget (menu);
289 toplevel = gtk_widget_get_toplevel (widget);
290 if (GTK_IS_APPLICATION_WINDOW (toplevel))
292 actions = gtk_application_window_get_observable (GTK_APPLICATION_WINDOW (toplevel));
293 accels = gtk_application_window_get_accel_group (GTK_APPLICATION_WINDOW (toplevel));
295 gtk_menu_set_accel_group (menu, accels);
296 gtk_model_menu_populate (GTK_MENU_SHELL (menu), actions, accels);
301 * gtk_menu_new_from_model:
302 * @model: a #GMenuModel
304 * Creates a #GtkMenu and populates it with menu items and
305 * submenus according to @model.
307 * The created menu items are connected to actions found in the
308 * #GtkApplicationWindow to which the menu belongs - typically
309 * by means of being attached to a widget (see gtk_menu_attach_to_widget())
310 * that is contained within the #GtkApplicationWindows widget hierarchy.
312 * Returns: a new #GtkMenu
317 gtk_menu_new_from_model (GMenuModel *model)
321 menu = gtk_menu_new ();
322 gtk_model_menu_bind (GTK_MENU_SHELL (menu), model, TRUE);
323 g_signal_connect (menu, "notify::attach-widget",
324 G_CALLBACK (notify_attach), NULL);
330 gtk_model_menu_create_menu_bar (GMenuModel *model,
331 GActionObservable *actions,
332 GtkAccelGroup *accels)
336 menubar = gtk_menu_bar_new ();
338 gtk_model_menu_bind (GTK_MENU_SHELL (menubar), model, FALSE);
339 gtk_model_menu_populate (GTK_MENU_SHELL (menubar), actions, accels);
345 hierarchy_changed (GtkMenuShell *shell,
346 GObject *previous_toplevel,
350 GActionObservable *actions;
351 GtkAccelGroup *accels;
353 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (shell));
354 if (GTK_IS_APPLICATION_WINDOW (toplevel))
356 actions = gtk_application_window_get_observable (GTK_APPLICATION_WINDOW (toplevel));
357 accels = gtk_application_window_get_accel_group (GTK_APPLICATION_WINDOW (toplevel));
359 gtk_model_menu_populate (shell, actions, accels);
364 * gtk_menu_bar_new_from_model:
365 * @model: a #GMenuModel
367 * Creates a new #GtkMenuBar and populates it with menu items
368 * and submenus according to @model.
370 * The created menu items are connected to actions found in the
371 * #GtkApplicationWindow to which the menu bar belongs - typically
372 * by means of being contained within the #GtkApplicationWindows
375 * Returns: a new #GtkMenuBar
380 gtk_menu_bar_new_from_model (GMenuModel *model)
384 menubar = gtk_menu_bar_new ();
386 gtk_model_menu_bind (GTK_MENU_SHELL (menubar), model, FALSE);
388 g_signal_connect (menubar, "hierarchy-changed",
389 G_CALLBACK (hierarchy_changed), NULL);