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_lookup_action (GActionMap *action_map,
64 const gchar *action_name)
66 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
68 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
72 gtk_application_window_add_action (GActionMap *action_map,
75 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
77 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
81 gtk_application_window_remove_action (GActionMap *action_map,
82 const gchar *action_name)
84 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
86 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
90 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
92 iface->list_actions = gtk_application_window_list_actions;
93 iface->query_action = gtk_application_window_query_action;
97 gtk_application_window_map_iface_init (GActionMapInterface *iface)
99 iface->lookup_action = gtk_application_window_lookup_action;
100 iface->add_action = gtk_application_window_add_action;
101 iface->remove_action = gtk_application_window_remove_action;
104 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
105 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
106 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
113 static GParamSpec *gtk_application_window_properties[N_PROPS];
116 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
117 gint *minimum_height,
118 gint *natural_height)
120 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
122 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
123 ->get_preferred_height (widget, minimum_height, natural_height);
125 if (window->priv->menubar != NULL)
127 gint menubar_min_height, menubar_nat_height;
129 gtk_widget_get_preferred_height (GTK_WIDGET (window->priv->menubar), &menubar_min_height, &menubar_nat_height);
130 *minimum_height += menubar_min_height;
131 *natural_height += menubar_nat_height;
136 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
138 gint *minimum_height,
139 gint *natural_height)
141 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
143 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
144 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
146 if (window->priv->menubar != NULL)
148 gint menubar_min_height, menubar_nat_height;
150 gtk_widget_get_preferred_height_for_width (GTK_WIDGET (window->priv->menubar), width, &menubar_min_height, &menubar_nat_height);
151 *minimum_height += menubar_min_height;
152 *natural_height += menubar_nat_height;
157 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
161 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
163 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
164 ->get_preferred_width (widget, minimum_width, natural_width);
166 if (window->priv->menubar != NULL)
168 gint menubar_min_width, menubar_nat_width;
170 gtk_widget_get_preferred_width (GTK_WIDGET (window->priv->menubar), &menubar_min_width, &menubar_nat_width);
171 *minimum_width = MAX (*minimum_width, menubar_min_width);
172 *natural_width = MAX (*natural_width, menubar_nat_width);
177 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
182 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
184 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
185 ->get_preferred_width_for_height (widget, height, minimum_width, natural_width);
187 if (window->priv->menubar != NULL)
189 gint menubar_min_width, menubar_nat_width;
191 gtk_widget_get_preferred_width_for_height (GTK_WIDGET (window->priv->menubar), height, &menubar_min_width, &menubar_nat_width);
192 *minimum_width = MAX (*minimum_width, menubar_min_width);
193 *natural_width = MAX (*natural_width, menubar_nat_width);
198 gtk_application_window_real_size_allocate (GtkWidget *widget,
199 GtkAllocation *allocation)
201 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
203 if (window->priv->menubar != NULL)
205 GtkAllocation menubar_allocation = *allocation;
206 gint menubar_min_height, menubar_nat_height;
209 gtk_widget_get_preferred_height_for_width (GTK_WIDGET (window->priv->menubar), allocation->width, &menubar_min_height, &menubar_nat_height);
211 menubar_allocation.height = menubar_min_height;
212 gtk_widget_size_allocate (GTK_WIDGET (window->priv->menubar), &menubar_allocation);
214 child = gtk_bin_get_child (GTK_BIN (window));
215 if (child != NULL && gtk_widget_get_visible (child))
217 GtkAllocation child_allocation = *allocation;
220 child_allocation.height = MAX (1, child_allocation.height - menubar_min_height);
222 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
223 child_allocation.x += border_width;
224 child_allocation.y += border_width + menubar_min_height;
225 child_allocation.width -= border_width * 2;
226 child_allocation.height -= border_width * 2 - menubar_min_height;
227 gtk_widget_size_allocate (child, &child_allocation);
230 gtk_widget_set_allocation (widget, allocation);
233 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
234 ->size_allocate (widget, allocation);
238 gtk_application_window_real_map (GtkWidget *widget)
240 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
242 /* XXX could elimate this by tweaking gtk_window_map */
243 if (window->priv->menubar)
244 gtk_widget_map (GTK_WIDGET (window->priv->menubar));
246 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
251 gtk_application_window_real_forall_internal (GtkContainer *container,
252 gboolean include_internal,
253 GtkCallback callback,
256 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
258 if (window->priv->menubar)
259 callback (GTK_WIDGET (window->priv->menubar), user_data);
261 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
262 ->forall (container, include_internal, callback, user_data);
267 gtk_application_window_get_property (GObject *object, guint prop_id,
268 GValue *value, GParamSpec *pspec)
270 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
274 case PROP_SHOW_APP_MENU:
275 g_value_set_boolean (value, window->priv->show_app_menu);
279 g_assert_not_reached ();
284 gtk_application_window_set_property (GObject *object, guint prop_id,
285 const GValue *value, GParamSpec *pspec)
287 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
291 case PROP_SHOW_APP_MENU:
292 gtk_application_window_set_show_app_menu (window, g_value_get_boolean (value));
296 g_assert_not_reached ();
301 gtk_application_window_finalize (GObject *object)
303 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
305 if (window->priv->menubar)
306 g_object_unref (window->priv->menubar);
308 g_object_unref (window->priv->actions);
310 G_OBJECT_CLASS (gtk_application_window_parent_class)
315 gtk_application_window_init (GtkApplicationWindow *window)
317 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
319 window->priv->actions = g_simple_action_group_new ();
321 /* window->priv->actions is the one and only ref on the group, so when
322 * we finalize, the action group will die, disconnecting all signals.
324 g_signal_connect_swapped (window->priv->actions, "action-added",
325 G_CALLBACK (g_action_group_action_added), window);
326 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
327 G_CALLBACK (g_action_group_action_enabled_changed), window);
328 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
329 G_CALLBACK (g_action_group_action_state_changed), window);
330 g_signal_connect_swapped (window->priv->actions, "action-removed",
331 G_CALLBACK (g_action_group_action_removed), window);
335 gtk_application_window_class_init (GtkApplicationWindowClass *class)
337 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
338 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
339 GObjectClass *object_class = G_OBJECT_CLASS (class);
341 container_class->forall = gtk_application_window_real_forall_internal;
342 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
343 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
344 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
345 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
346 widget_class->size_allocate = gtk_application_window_real_size_allocate;
347 widget_class->map = gtk_application_window_real_map;
348 object_class->get_property = gtk_application_window_get_property;
349 object_class->set_property = gtk_application_window_set_property;
350 object_class->finalize = gtk_application_window_finalize;
352 gtk_application_window_properties[PROP_SHOW_APP_MENU] =
353 g_param_spec_boolean ("show-app-menu", "show application menu",
354 "TRUE if the application menu should be included in the menubar at the top of the window",
355 FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
356 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
357 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
361 gtk_application_window_new (GtkApplication *application)
363 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
365 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
366 "application", application,
371 gtk_application_window_get_show_app_menu (GtkApplicationWindow *window)
373 return window->priv->show_app_menu;
377 gtk_application_window_set_show_app_menu (GtkApplicationWindow *window,
378 gboolean show_app_menu)
380 if (window->priv->show_app_menu != show_app_menu)
382 window->priv->show_app_menu = show_app_menu;
383 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_APP_MENU]);
390 item = gtk_menu_item_new_with_label ("Application");
391 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), gtk_application_window_get_app_menu (window));
393 menubar = gtk_menu_bar_new ();
394 window->priv->menubar = g_object_ref_sink (menubar);
395 gtk_menu_shell_append (GTK_MENU_SHELL (menubar), item);
396 gtk_widget_set_parent (menubar, GTK_WIDGET (window));
397 gtk_widget_show_all (menubar);
401 gtk_widget_unparent (GTK_WIDGET (window->priv->menubar));
402 g_object_unref (window->priv->menubar);
407 /* GtkMenu construction {{{1 */
413 gulong enabled_changed_id;
414 gulong state_changed_id;
415 gulong activate_handler;
419 action_data_free (gpointer data)
421 ActionData *a = data;
423 if (a->enabled_changed_id)
424 g_signal_handler_disconnect (a->group, a->enabled_changed_id);
426 if (a->state_changed_id)
427 g_signal_handler_disconnect (a->group, a->state_changed_id);
429 g_object_unref (a->group);
437 enabled_changed (GActionGroup *group,
438 const gchar *action_name,
442 gtk_widget_set_sensitive (widget, enabled);
446 item_activated (GtkWidget *w,
452 a = g_object_get_data (G_OBJECT (w), "action");
454 parameter = g_variant_ref_sink (g_variant_new_string (a->target));
457 g_action_group_activate_action (a->group, a->name, parameter);
459 g_variant_unref (parameter);
463 toggle_state_changed (GActionGroup *group,
470 a = g_object_get_data (G_OBJECT (w), "action");
471 g_signal_handler_block (w, a->activate_handler);
472 gtk_check_menu_item_set_active (w, g_variant_get_boolean (state));
473 g_signal_handler_unblock (w, a->activate_handler);
477 radio_state_changed (GActionGroup *group,
485 a = g_object_get_data (G_OBJECT (w), "action");
486 g_signal_handler_block (w, a->activate_handler);
487 b = g_strcmp0 (a->target, g_variant_get_string (state, NULL)) == 0;
488 gtk_check_menu_item_set_active (w, b);
489 g_signal_handler_unblock (w, a->activate_handler);
493 create_menuitem_from_model (GMenuModel *model,
503 const GVariantType *type;
507 g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_LABEL, "s", &label);
510 g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_ACTION, "s", &action);
513 type = g_action_group_get_action_state_type (group, action);
518 w = gtk_menu_item_new_with_mnemonic (label);
519 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
520 w = gtk_check_menu_item_new_with_label (label);
521 else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
523 w = gtk_check_menu_item_new_with_label (label);
524 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (w), TRUE);
527 g_assert_not_reached ();
531 a = g_new0 (ActionData, 1);
532 a->group = g_object_ref (group);
533 a->name = g_strdup (action);
534 g_object_set_data_full (G_OBJECT (w), "action", a, action_data_free);
536 if (!g_action_group_get_action_enabled (group, action))
537 gtk_widget_set_sensitive (w, FALSE);
539 s = g_strconcat ("action-enabled-changed::", action, NULL);
540 a->enabled_changed_id = g_signal_connect (group, s,
541 G_CALLBACK (enabled_changed), w);
543 a->activate_handler = g_signal_connect (w, "activate",
544 G_CALLBACK (item_activated), NULL);
550 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
552 s = g_strconcat ("action-state-changed::", action, NULL);
553 a->state_changed_id = g_signal_connect (group, s,
554 G_CALLBACK (toggle_state_changed), w);
556 v = g_action_group_get_action_state (group, action);
557 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
558 g_variant_get_boolean (v));
561 else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
563 s = g_strconcat ("action-state-changed::", action, NULL);
564 a->state_changed_id = g_signal_connect (group, s,
565 G_CALLBACK (radio_state_changed), w);
567 g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_TARGET, "s", &target);
568 a->target = g_strdup (target);
569 v = g_action_group_get_action_state (group, action);
570 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
571 g_strcmp0 (g_variant_get_string (v, NULL), target) == 0);
576 g_assert_not_reached ();
585 static void populate_menu_from_model (GtkMenuShell *menu,
587 GActionGroup *group);
590 append_items_from_model (GtkMenuShell *menu,
593 gboolean *need_separator,
594 const gchar *heading)
604 n = g_menu_model_get_n_items (model);
606 if (*need_separator && n > 0)
608 w = gtk_separator_menu_item_new ();
610 gtk_menu_shell_append (menu, w);
611 *need_separator = FALSE;
616 w = gtk_menu_item_new_with_label (heading);
618 gtk_widget_set_sensitive (w, FALSE);
619 gtk_menu_shell_append (GTK_MENU_SHELL (menu), w);
622 for (i = 0; i < n; i++)
624 if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SECTION)))
627 g_menu_model_get_item_attribute (model, i, G_MENU_ATTRIBUTE_LABEL, "s", &label);
628 append_items_from_model (menu, m, group, need_separator, label);
636 w = gtk_separator_menu_item_new ();
638 gtk_menu_shell_append (menu, w);
639 *need_separator = FALSE;
642 menuitem = create_menuitem_from_model (model, i, group);
644 if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SUBMENU)))
646 submenu = gtk_menu_new ();
647 populate_menu_from_model (GTK_MENU_SHELL (submenu), m, group);
648 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
652 gtk_widget_show (menuitem);
653 gtk_menu_shell_append (menu, menuitem);
655 *need_separator = TRUE;
660 populate_menu_from_model (GtkMenuShell *menu,
664 gboolean need_separator;
666 need_separator = FALSE;
667 append_items_from_model (menu, model, group, &need_separator, NULL);
671 GtkApplication *application;
674 GHashTable *connected;
678 free_items_changed_data (gpointer data)
680 ItemsChangedData *d = data;
682 g_object_unref (d->application);
684 if (d->update_idle != 0)
685 g_source_remove (d->update_idle);
687 g_hash_table_unref (d->connected);
693 repopulate_menu (gpointer data)
695 ItemsChangedData *d = data;
700 /* remove current children */
701 children = gtk_container_get_children (GTK_CONTAINER (d->menu));
702 for (l = children; l; l = l->next)
705 gtk_container_remove (GTK_CONTAINER (d->menu), child);
707 g_list_free (children);
710 model = g_application_get_menu (G_APPLICATION (d->application));
711 populate_menu_from_model (d->menu, model, G_ACTION_GROUP (d->application));
719 connect_to_items_changed (GMenuModel *model,
723 ItemsChangedData *d = data;
728 if (!g_hash_table_lookup (d->connected, model))
730 g_signal_connect (model, "items-changed", callback, data);
731 g_hash_table_insert (d->connected, model, model);
734 for (i = 0; i < g_menu_model_get_n_items (model); i++)
736 iter = g_menu_model_iterate_item_links (model, i);
737 while (g_menu_link_iter_next (iter))
739 m = g_menu_link_iter_get_value (iter);
740 connect_to_items_changed (m, callback, data);
743 g_object_unref (iter);
748 items_changed (GMenuModel *model,
754 ItemsChangedData *d = data;
756 if (d->update_idle == 0)
757 d->update_idle = gdk_threads_add_idle (repopulate_menu, data);
758 connect_to_items_changed (model, G_CALLBACK (items_changed), data);
762 * gtk_application_window_get_app_menu:
763 * @application: a #GtkApplication
765 * Populates a menu widget from a menu model that is
766 * associated with @application. See g_application_set_menu().
767 * The menu items will be connected to action of @application,
768 * as indicated by the menu model. The menus contents will be
769 * updated automatically in response to menu model changes.
771 * It is the callers responsibility to add the menu at a
772 * suitable place in the widget hierarchy.
774 * This function returns %NULL if @application has no associated
775 * menu model. It also returns %NULL if the menu model is
776 * represented outside the application, e.g. by an application
777 * menu in the desktop shell.
779 * @menu may be a #GtkMenu or a #GtkMenuBar.
781 * Returns: A #GtkMenu that has been populated from the
782 * #GMenuModel that is associated with @application,
786 gtk_application_window_get_app_menu (GtkApplicationWindow *window)
788 GtkApplication *application;
791 ItemsChangedData *data;
794 application = gtk_window_get_application (GTK_WINDOW (window));
796 model = g_application_get_menu (G_APPLICATION (application));
801 menu = gtk_menu_new ();
803 muxer = g_action_muxer_new ();
804 g_action_muxer_insert (muxer, "app", G_ACTION_GROUP (application));
805 g_action_muxer_insert (muxer, "win", G_ACTION_GROUP (window));
806 populate_menu_from_model (GTK_MENU_SHELL (menu), model, G_ACTION_GROUP (muxer));
807 g_object_unref (muxer);
809 data = g_new (ItemsChangedData, 1);
810 data->application = g_object_ref (application);
811 data->menu = GTK_MENU_SHELL (menu);
812 data->update_idle = 0;
813 data->connected = g_hash_table_new (NULL, NULL);
815 g_object_set_data_full (G_OBJECT (menu), "gtk-application-menu-data",
816 data, free_items_changed_data);
818 connect_to_items_changed (model, G_CALLBACK (items_changed), data);