2 * Copyright © 2011 Canonical Limited
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
24 #include "gtkapplicationwindow.h"
26 #include "gtkseparatormenuitem.h"
27 #include "gtkcheckmenuitem.h"
28 #include "gtkmenubar.h"
29 #include "gactionmuxer.h"
31 struct _GtkApplicationWindowPrivate
33 GSimpleActionGroup *actions;
36 gboolean show_app_menu;
40 gtk_application_window_list_actions (GActionGroup *group)
42 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
44 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
48 gtk_application_window_query_action (GActionGroup *group,
49 const gchar *action_name,
51 const GVariantType **parameter_type,
52 const GVariantType **state_type,
53 GVariant **state_hint,
56 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
58 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
59 action_name, enabled, parameter_type, state_type, state_hint, state);
63 gtk_application_window_activate_action (GActionGroup *group,
64 const gchar *action_name,
67 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
69 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
73 gtk_application_window_change_action_state (GActionGroup *group,
74 const gchar *action_name,
77 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
79 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
83 gtk_application_window_lookup_action (GActionMap *action_map,
84 const gchar *action_name)
86 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
88 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
92 gtk_application_window_add_action (GActionMap *action_map,
95 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
97 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
101 gtk_application_window_remove_action (GActionMap *action_map,
102 const gchar *action_name)
104 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
106 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
110 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
112 iface->list_actions = gtk_application_window_list_actions;
113 iface->query_action = gtk_application_window_query_action;
114 iface->activate_action = gtk_application_window_activate_action;
115 iface->change_action_state = gtk_application_window_change_action_state;
119 gtk_application_window_map_iface_init (GActionMapInterface *iface)
121 iface->lookup_action = gtk_application_window_lookup_action;
122 iface->add_action = gtk_application_window_add_action;
123 iface->remove_action = gtk_application_window_remove_action;
126 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
127 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
128 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
135 static GParamSpec *gtk_application_window_properties[N_PROPS];
138 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
139 gint *minimum_height,
140 gint *natural_height)
142 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
144 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
145 ->get_preferred_height (widget, minimum_height, natural_height);
147 if (window->priv->menubar != NULL)
149 gint menubar_min_height, menubar_nat_height;
151 gtk_widget_get_preferred_height (GTK_WIDGET (window->priv->menubar), &menubar_min_height, &menubar_nat_height);
152 *minimum_height += menubar_min_height;
153 *natural_height += menubar_nat_height;
158 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
160 gint *minimum_height,
161 gint *natural_height)
163 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
165 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
166 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
168 if (window->priv->menubar != NULL)
170 gint menubar_min_height, menubar_nat_height;
172 gtk_widget_get_preferred_height_for_width (GTK_WIDGET (window->priv->menubar), width, &menubar_min_height, &menubar_nat_height);
173 *minimum_height += menubar_min_height;
174 *natural_height += menubar_nat_height;
179 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
183 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
185 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
186 ->get_preferred_width (widget, minimum_width, natural_width);
188 if (window->priv->menubar != NULL)
190 gint menubar_min_width, menubar_nat_width;
192 gtk_widget_get_preferred_width (GTK_WIDGET (window->priv->menubar), &menubar_min_width, &menubar_nat_width);
193 *minimum_width = MAX (*minimum_width, menubar_min_width);
194 *natural_width = MAX (*natural_width, menubar_nat_width);
199 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
204 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
206 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
207 ->get_preferred_width_for_height (widget, height, minimum_width, natural_width);
209 if (window->priv->menubar != NULL)
211 gint menubar_min_width, menubar_nat_width;
213 gtk_widget_get_preferred_width_for_height (GTK_WIDGET (window->priv->menubar), height, &menubar_min_width, &menubar_nat_width);
214 *minimum_width = MAX (*minimum_width, menubar_min_width);
215 *natural_width = MAX (*natural_width, menubar_nat_width);
220 gtk_application_window_real_size_allocate (GtkWidget *widget,
221 GtkAllocation *allocation)
223 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
225 if (window->priv->menubar != NULL)
227 GtkAllocation menubar_allocation = *allocation;
228 gint menubar_min_height, menubar_nat_height;
231 gtk_widget_get_preferred_height_for_width (GTK_WIDGET (window->priv->menubar), allocation->width, &menubar_min_height, &menubar_nat_height);
233 menubar_allocation.height = menubar_min_height;
234 gtk_widget_size_allocate (GTK_WIDGET (window->priv->menubar), &menubar_allocation);
236 child = gtk_bin_get_child (GTK_BIN (window));
237 if (child != NULL && gtk_widget_get_visible (child))
239 GtkAllocation child_allocation = *allocation;
242 child_allocation.height = MAX (1, child_allocation.height - menubar_min_height);
244 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
245 child_allocation.x += border_width;
246 child_allocation.y += border_width + menubar_min_height;
247 child_allocation.width -= border_width * 2;
248 child_allocation.height -= border_width * 2 - menubar_min_height;
249 gtk_widget_size_allocate (child, &child_allocation);
252 gtk_widget_set_allocation (widget, allocation);
255 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
256 ->size_allocate (widget, allocation);
260 gtk_application_window_real_map (GtkWidget *widget)
262 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
264 /* XXX could elimate this by tweaking gtk_window_map */
265 if (window->priv->menubar)
266 gtk_widget_map (GTK_WIDGET (window->priv->menubar));
268 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
273 gtk_application_window_real_forall_internal (GtkContainer *container,
274 gboolean include_internal,
275 GtkCallback callback,
278 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
280 if (window->priv->menubar)
281 callback (GTK_WIDGET (window->priv->menubar), user_data);
283 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
284 ->forall (container, include_internal, callback, user_data);
289 gtk_application_window_get_property (GObject *object, guint prop_id,
290 GValue *value, GParamSpec *pspec)
292 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
296 case PROP_SHOW_APP_MENU:
297 g_value_set_boolean (value, window->priv->show_app_menu);
301 g_assert_not_reached ();
306 gtk_application_window_set_property (GObject *object, guint prop_id,
307 const GValue *value, GParamSpec *pspec)
309 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
313 case PROP_SHOW_APP_MENU:
314 gtk_application_window_set_show_app_menu (window, g_value_get_boolean (value));
318 g_assert_not_reached ();
323 gtk_application_window_finalize (GObject *object)
325 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
327 if (window->priv->menubar)
328 g_object_unref (window->priv->menubar);
330 g_object_unref (window->priv->actions);
332 G_OBJECT_CLASS (gtk_application_window_parent_class)
337 gtk_application_window_init (GtkApplicationWindow *window)
339 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
341 window->priv->actions = g_simple_action_group_new ();
343 /* window->priv->actions is the one and only ref on the group, so when
344 * we finalize, the action group will die, disconnecting all signals.
346 g_signal_connect_swapped (window->priv->actions, "action-added",
347 G_CALLBACK (g_action_group_action_added), window);
348 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
349 G_CALLBACK (g_action_group_action_enabled_changed), window);
350 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
351 G_CALLBACK (g_action_group_action_state_changed), window);
352 g_signal_connect_swapped (window->priv->actions, "action-removed",
353 G_CALLBACK (g_action_group_action_removed), window);
357 gtk_application_window_class_init (GtkApplicationWindowClass *class)
359 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
360 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
361 GObjectClass *object_class = G_OBJECT_CLASS (class);
363 container_class->forall = gtk_application_window_real_forall_internal;
364 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
365 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
366 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
367 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
368 widget_class->size_allocate = gtk_application_window_real_size_allocate;
369 widget_class->map = gtk_application_window_real_map;
370 object_class->get_property = gtk_application_window_get_property;
371 object_class->set_property = gtk_application_window_set_property;
372 object_class->finalize = gtk_application_window_finalize;
374 gtk_application_window_properties[PROP_SHOW_APP_MENU] =
375 g_param_spec_boolean ("show-app-menu", "show application menu",
376 "TRUE if the application menu should be included in the menubar at the top of the window",
377 FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
378 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
379 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
383 gtk_application_window_new (GtkApplication *application)
385 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
387 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
388 "application", application,
393 gtk_application_window_get_show_app_menu (GtkApplicationWindow *window)
395 return window->priv->show_app_menu;
399 gtk_application_window_set_show_app_menu (GtkApplicationWindow *window,
400 gboolean show_app_menu)
402 if (window->priv->show_app_menu != show_app_menu)
404 window->priv->show_app_menu = show_app_menu;
405 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_APP_MENU]);
412 item = gtk_menu_item_new_with_label ("Application");
413 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), gtk_application_window_get_app_menu (window));
415 menubar = gtk_menu_bar_new ();
416 window->priv->menubar = g_object_ref_sink (menubar);
417 gtk_menu_shell_append (GTK_MENU_SHELL (menubar), item);
418 gtk_widget_set_parent (menubar, GTK_WIDGET (window));
419 gtk_widget_show_all (menubar);
423 gtk_widget_unparent (GTK_WIDGET (window->priv->menubar));
424 g_object_unref (window->priv->menubar);
429 /* GtkMenu construction {{{1 */
435 gulong enabled_changed_id;
436 gulong state_changed_id;
437 gulong activate_handler;
441 action_data_free (gpointer data)
443 ActionData *a = data;
445 if (a->enabled_changed_id)
446 g_signal_handler_disconnect (a->group, a->enabled_changed_id);
448 if (a->state_changed_id)
449 g_signal_handler_disconnect (a->group, a->state_changed_id);
451 g_object_unref (a->group);
459 enabled_changed (GActionGroup *group,
460 const gchar *action_name,
464 gtk_widget_set_sensitive (widget, enabled);
468 item_activated (GtkWidget *w,
474 a = g_object_get_data (G_OBJECT (w), "action");
476 parameter = g_variant_ref_sink (g_variant_new_string (a->target));
479 g_action_group_activate_action (a->group, a->name, parameter);
481 g_variant_unref (parameter);
485 toggle_state_changed (GActionGroup *group,
492 a = g_object_get_data (G_OBJECT (w), "action");
493 g_signal_handler_block (w, a->activate_handler);
494 gtk_check_menu_item_set_active (w, g_variant_get_boolean (state));
495 g_signal_handler_unblock (w, a->activate_handler);
499 radio_state_changed (GActionGroup *group,
507 a = g_object_get_data (G_OBJECT (w), "action");
508 g_signal_handler_block (w, a->activate_handler);
509 b = g_strcmp0 (a->target, g_variant_get_string (state, NULL)) == 0;
510 gtk_check_menu_item_set_active (w, b);
511 g_signal_handler_unblock (w, a->activate_handler);
515 create_menuitem_from_model (GMenuModel *model,
525 const GVariantType *type;
529 g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_LABEL, "s", &label);
532 g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_ACTION, "s", &action);
535 type = g_action_group_get_action_state_type (group, action);
540 w = gtk_menu_item_new_with_mnemonic (label);
541 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
542 w = gtk_check_menu_item_new_with_label (label);
543 else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
545 w = gtk_check_menu_item_new_with_label (label);
546 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (w), TRUE);
549 g_assert_not_reached ();
553 a = g_new0 (ActionData, 1);
554 a->group = g_object_ref (group);
555 a->name = g_strdup (action);
556 g_object_set_data_full (G_OBJECT (w), "action", a, action_data_free);
558 if (!g_action_group_get_action_enabled (group, action))
559 gtk_widget_set_sensitive (w, FALSE);
561 s = g_strconcat ("action-enabled-changed::", action, NULL);
562 a->enabled_changed_id = g_signal_connect (group, s,
563 G_CALLBACK (enabled_changed), w);
565 a->activate_handler = g_signal_connect (w, "activate",
566 G_CALLBACK (item_activated), NULL);
572 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
574 s = g_strconcat ("action-state-changed::", action, NULL);
575 a->state_changed_id = g_signal_connect (group, s,
576 G_CALLBACK (toggle_state_changed), w);
578 v = g_action_group_get_action_state (group, action);
579 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
580 g_variant_get_boolean (v));
583 else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
585 s = g_strconcat ("action-state-changed::", action, NULL);
586 a->state_changed_id = g_signal_connect (group, s,
587 G_CALLBACK (radio_state_changed), w);
589 g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_TARGET, "s", &target);
590 a->target = g_strdup (target);
591 v = g_action_group_get_action_state (group, action);
592 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
593 g_strcmp0 (g_variant_get_string (v, NULL), target) == 0);
598 g_assert_not_reached ();
607 static void populate_menu_from_model (GtkMenuShell *menu,
609 GActionGroup *group);
612 append_items_from_model (GtkMenuShell *menu,
615 gboolean *need_separator,
616 const gchar *heading)
626 n = g_menu_model_get_n_items (model);
628 if (*need_separator && n > 0)
630 w = gtk_separator_menu_item_new ();
632 gtk_menu_shell_append (menu, w);
633 *need_separator = FALSE;
638 w = gtk_menu_item_new_with_label (heading);
640 gtk_widget_set_sensitive (w, FALSE);
641 gtk_menu_shell_append (GTK_MENU_SHELL (menu), w);
644 for (i = 0; i < n; i++)
646 if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SECTION)))
649 g_menu_model_get_item_attribute (model, i, G_MENU_ATTRIBUTE_LABEL, "s", &label);
650 append_items_from_model (menu, m, group, need_separator, label);
658 w = gtk_separator_menu_item_new ();
660 gtk_menu_shell_append (menu, w);
661 *need_separator = FALSE;
664 menuitem = create_menuitem_from_model (model, i, group);
666 if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SUBMENU)))
668 submenu = gtk_menu_new ();
669 populate_menu_from_model (GTK_MENU_SHELL (submenu), m, group);
670 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
674 gtk_widget_show (menuitem);
675 gtk_menu_shell_append (menu, menuitem);
677 *need_separator = TRUE;
682 populate_menu_from_model (GtkMenuShell *menu,
686 gboolean need_separator;
688 need_separator = FALSE;
689 append_items_from_model (menu, model, group, &need_separator, NULL);
693 GtkApplication *application;
696 GHashTable *connected;
700 free_items_changed_data (gpointer data)
702 ItemsChangedData *d = data;
704 g_object_unref (d->application);
706 if (d->update_idle != 0)
707 g_source_remove (d->update_idle);
709 g_hash_table_unref (d->connected);
715 repopulate_menu (gpointer data)
717 ItemsChangedData *d = data;
722 /* remove current children */
723 children = gtk_container_get_children (GTK_CONTAINER (d->menu));
724 for (l = children; l; l = l->next)
727 gtk_container_remove (GTK_CONTAINER (d->menu), child);
729 g_list_free (children);
732 model = g_application_get_menu (G_APPLICATION (d->application));
733 populate_menu_from_model (d->menu, model, G_ACTION_GROUP (d->application));
741 connect_to_items_changed (GMenuModel *model,
745 ItemsChangedData *d = data;
750 if (!g_hash_table_lookup (d->connected, model))
752 g_signal_connect (model, "items-changed", callback, data);
753 g_hash_table_insert (d->connected, model, model);
756 for (i = 0; i < g_menu_model_get_n_items (model); i++)
758 iter = g_menu_model_iterate_item_links (model, i);
759 while (g_menu_link_iter_next (iter))
761 m = g_menu_link_iter_get_value (iter);
762 connect_to_items_changed (m, callback, data);
765 g_object_unref (iter);
770 items_changed (GMenuModel *model,
776 ItemsChangedData *d = data;
778 if (d->update_idle == 0)
779 d->update_idle = gdk_threads_add_idle (repopulate_menu, data);
780 connect_to_items_changed (model, G_CALLBACK (items_changed), data);
784 * gtk_application_window_get_app_menu:
785 * @application: a #GtkApplication
787 * Populates a menu widget from a menu model that is
788 * associated with @application. See g_application_set_menu().
789 * The menu items will be connected to action of @application,
790 * as indicated by the menu model. The menus contents will be
791 * updated automatically in response to menu model changes.
793 * It is the callers responsibility to add the menu at a
794 * suitable place in the widget hierarchy.
796 * This function returns %NULL if @application has no associated
797 * menu model. It also returns %NULL if the menu model is
798 * represented outside the application, e.g. by an application
799 * menu in the desktop shell.
801 * @menu may be a #GtkMenu or a #GtkMenuBar.
803 * Returns: A #GtkMenu that has been populated from the
804 * #GMenuModel that is associated with @application,
808 gtk_application_window_get_app_menu (GtkApplicationWindow *window)
810 GtkApplication *application;
813 ItemsChangedData *data;
816 application = gtk_window_get_application (GTK_WINDOW (window));
818 model = g_application_get_menu (G_APPLICATION (application));
823 menu = gtk_menu_new ();
825 muxer = g_action_muxer_new ();
826 g_action_muxer_insert (muxer, "app", G_ACTION_GROUP (application));
827 g_action_muxer_insert (muxer, "win", G_ACTION_GROUP (window));
828 populate_menu_from_model (GTK_MENU_SHELL (menu), model, G_ACTION_GROUP (muxer));
829 g_object_unref (muxer);
831 data = g_new (ItemsChangedData, 1);
832 data->application = g_object_ref (application);
833 data->menu = GTK_MENU_SHELL (menu);
834 data->update_idle = 0;
835 data->connected = g_hash_table_new (NULL, NULL);
837 g_object_set_data_full (G_OBJECT (menu), "gtk-application-menu-data",
838 data, free_items_changed_data);
840 connect_to_items_changed (model, G_CALLBACK (items_changed), data);