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
36 gboolean show_app_menu;
39 G_DEFINE_TYPE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW)
46 static GParamSpec *gtk_application_window_properties[N_PROPS];
49 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
53 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
55 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
56 ->get_preferred_height (widget, minimum_height, natural_height);
58 if (window->priv->menubar != NULL)
60 gint menubar_min_height, menubar_nat_height;
62 gtk_widget_get_preferred_height (GTK_WIDGET (window->priv->menubar), &menubar_min_height, &menubar_nat_height);
63 *minimum_height += menubar_min_height;
64 *natural_height += menubar_nat_height;
69 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
74 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
76 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
77 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
79 if (window->priv->menubar != NULL)
81 gint menubar_min_height, menubar_nat_height;
83 gtk_widget_get_preferred_height_for_width (GTK_WIDGET (window->priv->menubar), width, &menubar_min_height, &menubar_nat_height);
84 *minimum_height += menubar_min_height;
85 *natural_height += menubar_nat_height;
90 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
94 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
96 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
97 ->get_preferred_width (widget, minimum_width, natural_width);
99 if (window->priv->menubar != NULL)
101 gint menubar_min_width, menubar_nat_width;
103 gtk_widget_get_preferred_width (GTK_WIDGET (window->priv->menubar), &menubar_min_width, &menubar_nat_width);
104 *minimum_width = MAX (*minimum_width, menubar_min_width);
105 *natural_width = MAX (*natural_width, menubar_nat_width);
110 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
115 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
117 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
118 ->get_preferred_width_for_height (widget, height, minimum_width, natural_width);
120 if (window->priv->menubar != NULL)
122 gint menubar_min_width, menubar_nat_width;
124 gtk_widget_get_preferred_width_for_height (GTK_WIDGET (window->priv->menubar), height, &menubar_min_width, &menubar_nat_width);
125 *minimum_width = MAX (*minimum_width, menubar_min_width);
126 *natural_width = MAX (*natural_width, menubar_nat_width);
131 gtk_application_window_real_size_allocate (GtkWidget *widget,
132 GtkAllocation *allocation)
134 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
136 if (window->priv->menubar != NULL)
138 GtkAllocation menubar_allocation = *allocation;
139 gint menubar_min_height, menubar_nat_height;
142 gtk_widget_get_preferred_height_for_width (GTK_WIDGET (window->priv->menubar), allocation->width, &menubar_min_height, &menubar_nat_height);
144 menubar_allocation.height = menubar_min_height;
145 gtk_widget_size_allocate (GTK_WIDGET (window->priv->menubar), &menubar_allocation);
147 child = gtk_bin_get_child (GTK_BIN (window));
148 if (child != NULL && gtk_widget_get_visible (child))
150 GtkAllocation child_allocation = *allocation;
153 child_allocation.height = MAX (1, child_allocation.height - menubar_min_height);
155 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
156 child_allocation.x += border_width;
157 child_allocation.y += border_width + menubar_min_height;
158 child_allocation.width -= border_width * 2;
159 child_allocation.height -= border_width * 2 - menubar_min_height;
160 gtk_widget_size_allocate (child, &child_allocation);
163 gtk_widget_set_allocation (widget, allocation);
166 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
167 ->size_allocate (widget, allocation);
171 gtk_application_window_real_map (GtkWidget *widget)
173 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
175 /* XXX could elimate this by tweaking gtk_window_map */
176 if (window->priv->menubar)
177 gtk_widget_map (GTK_WIDGET (window->priv->menubar));
179 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
184 gtk_application_window_real_forall_internal (GtkContainer *container,
185 gboolean include_internal,
186 GtkCallback callback,
189 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
191 if (window->priv->menubar)
192 callback (GTK_WIDGET (window->priv->menubar), user_data);
194 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
195 ->forall (container, include_internal, callback, user_data);
200 gtk_application_window_get_property (GObject *object, guint prop_id,
201 GValue *value, GParamSpec *pspec)
203 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
207 case PROP_SHOW_APP_MENU:
208 g_value_set_boolean (value, window->priv->show_app_menu);
212 g_assert_not_reached ();
217 gtk_application_window_set_property (GObject *object, guint prop_id,
218 const GValue *value, GParamSpec *pspec)
220 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
224 case PROP_SHOW_APP_MENU:
225 gtk_application_window_set_show_app_menu (window, g_value_get_boolean (value));
229 g_assert_not_reached ();
234 gtk_application_window_finalize (GObject *object)
236 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
238 if (window->priv->menubar)
239 g_object_unref (window->priv->menubar);
241 if (window->priv->menu)
242 g_object_unref (window->priv->menu);
244 G_OBJECT_CLASS (gtk_application_window_parent_class)
249 gtk_application_window_init (GtkApplicationWindow *window)
251 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
255 gtk_application_window_class_init (GtkApplicationWindowClass *class)
257 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
258 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
259 GObjectClass *object_class = G_OBJECT_CLASS (class);
261 container_class->forall = gtk_application_window_real_forall_internal;
262 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
263 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
264 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
265 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
266 widget_class->size_allocate = gtk_application_window_real_size_allocate;
267 widget_class->map = gtk_application_window_real_map;
268 object_class->get_property = gtk_application_window_get_property;
269 object_class->set_property = gtk_application_window_set_property;
270 object_class->finalize = gtk_application_window_finalize;
272 gtk_application_window_properties[PROP_SHOW_APP_MENU] =
273 g_param_spec_boolean ("show-app-menu", "show application menu",
274 "TRUE if the application menu should be included in the menubar at the top of the window",
275 FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
276 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
277 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
281 gtk_application_window_new (GtkApplication *application)
283 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
285 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
286 "application", application,
291 gtk_application_window_get_show_app_menu (GtkApplicationWindow *window)
293 return window->priv->show_app_menu;
297 gtk_application_window_set_show_app_menu (GtkApplicationWindow *window,
298 gboolean show_app_menu)
300 if (window->priv->show_app_menu != show_app_menu)
302 window->priv->show_app_menu = show_app_menu;
303 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_APP_MENU]);
310 item = gtk_menu_item_new_with_label ("Application");
311 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), gtk_application_window_get_app_menu (window));
313 menubar = gtk_menu_bar_new ();
314 window->priv->menubar = g_object_ref_sink (menubar);
315 gtk_menu_shell_append (GTK_MENU_SHELL (menubar), item);
316 gtk_widget_set_parent (menubar, GTK_WIDGET (window));
317 gtk_widget_show_all (menubar);
321 gtk_widget_unparent (GTK_WIDGET (window->priv->menubar));
322 g_object_unref (window->priv->menubar);
327 /* GtkMenu construction {{{1 */
333 gulong enabled_changed_id;
334 gulong state_changed_id;
335 gulong activate_handler;
339 action_data_free (gpointer data)
341 ActionData *a = data;
343 if (a->enabled_changed_id)
344 g_signal_handler_disconnect (a->group, a->enabled_changed_id);
346 if (a->state_changed_id)
347 g_signal_handler_disconnect (a->group, a->state_changed_id);
349 g_object_unref (a->group);
357 enabled_changed (GActionGroup *group,
358 const gchar *action_name,
362 gtk_widget_set_sensitive (widget, enabled);
366 item_activated (GtkWidget *w,
372 a = g_object_get_data (G_OBJECT (w), "action");
374 parameter = g_variant_ref_sink (g_variant_new_string (a->target));
377 g_action_group_activate_action (a->group, a->name, parameter);
379 g_variant_unref (parameter);
383 toggle_state_changed (GActionGroup *group,
390 a = g_object_get_data (G_OBJECT (w), "action");
391 g_signal_handler_block (w, a->activate_handler);
392 gtk_check_menu_item_set_active (w, g_variant_get_boolean (state));
393 g_signal_handler_unblock (w, a->activate_handler);
397 radio_state_changed (GActionGroup *group,
405 a = g_object_get_data (G_OBJECT (w), "action");
406 g_signal_handler_block (w, a->activate_handler);
407 b = g_strcmp0 (a->target, g_variant_get_string (state, NULL)) == 0;
408 gtk_check_menu_item_set_active (w, b);
409 g_signal_handler_unblock (w, a->activate_handler);
413 create_menuitem_from_model (GMenuModel *model,
423 const GVariantType *type;
427 g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_LABEL, "s", &label);
430 g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_ACTION, "s", &action);
433 type = g_action_group_get_action_state_type (group, action);
438 w = gtk_menu_item_new_with_mnemonic (label);
439 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
440 w = gtk_check_menu_item_new_with_label (label);
441 else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
443 w = gtk_check_menu_item_new_with_label (label);
444 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (w), TRUE);
447 g_assert_not_reached ();
451 a = g_new0 (ActionData, 1);
452 a->group = g_object_ref (group);
453 a->name = g_strdup (action);
454 g_object_set_data_full (G_OBJECT (w), "action", a, action_data_free);
456 if (!g_action_group_get_action_enabled (group, action))
457 gtk_widget_set_sensitive (w, FALSE);
459 s = g_strconcat ("action-enabled-changed::", action, NULL);
460 a->enabled_changed_id = g_signal_connect (group, s,
461 G_CALLBACK (enabled_changed), w);
463 a->activate_handler = g_signal_connect (w, "activate",
464 G_CALLBACK (item_activated), NULL);
470 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
472 s = g_strconcat ("action-state-changed::", action, NULL);
473 a->state_changed_id = g_signal_connect (group, s,
474 G_CALLBACK (toggle_state_changed), w);
476 v = g_action_group_get_action_state (group, action);
477 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
478 g_variant_get_boolean (v));
481 else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
483 s = g_strconcat ("action-state-changed::", action, NULL);
484 a->state_changed_id = g_signal_connect (group, s,
485 G_CALLBACK (radio_state_changed), w);
487 g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_TARGET, "s", &target);
488 a->target = g_strdup (target);
489 v = g_action_group_get_action_state (group, action);
490 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
491 g_strcmp0 (g_variant_get_string (v, NULL), target) == 0);
496 g_assert_not_reached ();
505 static void populate_menu_from_model (GtkMenuShell *menu,
507 GActionGroup *group);
510 append_items_from_model (GtkMenuShell *menu,
513 gboolean *need_separator,
514 const gchar *heading)
524 n = g_menu_model_get_n_items (model);
526 if (*need_separator && n > 0)
528 w = gtk_separator_menu_item_new ();
530 gtk_menu_shell_append (menu, w);
531 *need_separator = FALSE;
536 w = gtk_menu_item_new_with_label (heading);
538 gtk_widget_set_sensitive (w, FALSE);
539 gtk_menu_shell_append (GTK_MENU_SHELL (menu), w);
542 for (i = 0; i < n; i++)
544 if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SECTION)))
547 g_menu_model_get_item_attribute (model, i, G_MENU_ATTRIBUTE_LABEL, "s", &label);
548 append_items_from_model (menu, m, group, need_separator, label);
556 w = gtk_separator_menu_item_new ();
558 gtk_menu_shell_append (menu, w);
559 *need_separator = FALSE;
562 menuitem = create_menuitem_from_model (model, i, group);
564 if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SUBMENU)))
566 submenu = gtk_menu_new ();
567 populate_menu_from_model (GTK_MENU_SHELL (submenu), m, group);
568 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
572 gtk_widget_show (menuitem);
573 gtk_menu_shell_append (menu, menuitem);
575 *need_separator = TRUE;
580 populate_menu_from_model (GtkMenuShell *menu,
584 gboolean need_separator;
586 need_separator = FALSE;
587 append_items_from_model (menu, model, group, &need_separator, NULL);
591 GtkApplication *application;
594 GHashTable *connected;
598 free_items_changed_data (gpointer data)
600 ItemsChangedData *d = data;
602 g_object_unref (d->application);
604 if (d->update_idle != 0)
605 g_source_remove (d->update_idle);
607 g_hash_table_unref (d->connected);
613 repopulate_menu (gpointer data)
615 ItemsChangedData *d = data;
620 /* remove current children */
621 children = gtk_container_get_children (GTK_CONTAINER (d->menu));
622 for (l = children; l; l = l->next)
625 gtk_container_remove (GTK_CONTAINER (d->menu), child);
627 g_list_free (children);
630 model = g_application_get_menu (G_APPLICATION (d->application));
631 populate_menu_from_model (d->menu, model, G_ACTION_GROUP (d->application));
639 connect_to_items_changed (GMenuModel *model,
643 ItemsChangedData *d = data;
648 if (!g_hash_table_lookup (d->connected, model))
650 g_signal_connect (model, "items-changed", callback, data);
651 g_hash_table_insert (d->connected, model, model);
654 for (i = 0; i < g_menu_model_get_n_items (model); i++)
656 iter = g_menu_model_iterate_item_links (model, i);
657 while (g_menu_link_iter_next (iter))
659 m = g_menu_link_iter_get_value (iter);
660 connect_to_items_changed (m, callback, data);
663 g_object_unref (iter);
668 items_changed (GMenuModel *model,
674 ItemsChangedData *d = data;
676 if (d->update_idle == 0)
677 d->update_idle = gdk_threads_add_idle (repopulate_menu, data);
678 connect_to_items_changed (model, G_CALLBACK (items_changed), data);
682 * gtk_application_window_get_app_menu:
683 * @application: a #GtkApplication
685 * Populates a menu widget from a menu model that is
686 * associated with @application. See g_application_set_menu().
687 * The menu items will be connected to action of @application,
688 * as indicated by the menu model. The menus contents will be
689 * updated automatically in response to menu model changes.
691 * It is the callers responsibility to add the menu at a
692 * suitable place in the widget hierarchy.
694 * This function returns %NULL if @application has no associated
695 * menu model. It also returns %NULL if the menu model is
696 * represented outside the application, e.g. by an application
697 * menu in the desktop shell.
699 * @menu may be a #GtkMenu or a #GtkMenuBar.
701 * Returns: A #GtkMenu that has been populated from the
702 * #GMenuModel that is associated with @application,
706 gtk_application_window_get_app_menu (GtkApplicationWindow *window)
708 GtkApplication *application;
711 ItemsChangedData *data;
714 application = gtk_window_get_application (GTK_WINDOW (window));
716 model = g_application_get_menu (G_APPLICATION (application));
721 menu = gtk_menu_new ();
723 muxer = g_action_muxer_new ();
724 g_action_muxer_insert (muxer, "app", G_ACTION_GROUP (application));
725 populate_menu_from_model (GTK_MENU_SHELL (menu), model, G_ACTION_GROUP (muxer));
727 data = g_new (ItemsChangedData, 1);
728 data->application = g_object_ref (application);
729 data->menu = GTK_MENU_SHELL (menu);
730 data->update_idle = 0;
731 data->connected = g_hash_table_new (NULL, NULL);
733 g_object_set_data_full (G_OBJECT (menu), "gtk-application-menu-data",
734 data, free_items_changed_data);
736 connect_to_items_changed (model, G_CALLBACK (items_changed), data);