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 "gtkmodelmenuitem.h"
28 #include "gtkcheckmenuitem.h"
29 #include "gtkmenubar.h"
30 #include "gactionmuxer.h"
34 * SECTION:gtkapplicationwindow
35 * @title: GtkApplicationWindow
36 * @short_description: GtkWindow subclass with GtkApplication support
38 * GtkApplicationWindow is a #GtkWindow subclass that offers some
39 * extra functionality for better integration with #GtkApplication
40 * features. Notably, it can handle both the application menu as well
41 * as the menubar. See g_application_set_app_menu() and
42 * g_application_set_menubar().
44 * This class implements the #GActionGroup and #GActionMap interfaces,
45 * to let you add window-specific actions that will be exported by the
46 * associated #GtkApplication, together with its application-wide
47 * actions. Window-specific actions are prefixed with the "win."
48 * prefix and application-wide actions are prefixed with the "app."
49 * prefix. Actions must be addressed with the prefixed name when
50 * referring to them from a #GMenuModel.
52 * If the desktop environment does not display the application menu
53 * as part of the desktop shell, then #GApplicationWindow will
54 * automatically show the menu as part of a menubar. This behaviour
55 * can be overridden with the #GtkApplicationWindow:show-app-menu
58 struct _GtkApplicationWindowPrivate
60 GSimpleActionGroup *actions;
63 GMenu *app_menu_section;
64 GMenu *menubar_section;
65 gboolean show_menubar;
69 gtk_application_window_create_menubar (GMenuModel *model,
70 GActionObservable *actions);
74 gtk_application_window_update_menubar (GtkApplicationWindow *window)
76 gboolean should_have_menubar;
77 gboolean have_menubar;
79 have_menubar = window->priv->menubar != NULL;
81 should_have_menubar = window->priv->show_menubar &&
82 (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
83 g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
85 g_print ("h %d s %d\n", have_menubar, should_have_menubar);
87 if (have_menubar && !should_have_menubar)
89 gtk_widget_unparent (window->priv->menubar);
90 g_object_unref (window->priv->menubar);
91 window->priv->menubar = NULL;
93 gtk_widget_queue_resize (GTK_WIDGET (window));
96 if (!have_menubar && should_have_menubar)
101 muxer = g_action_muxer_new ();
102 g_action_muxer_insert (muxer, "app", G_ACTION_GROUP (gtk_window_get_application (GTK_WINDOW (window))));
103 g_action_muxer_insert (muxer, "win", G_ACTION_GROUP (window));
105 combined = g_menu_new ();
106 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
107 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
109 window->priv->menubar = gtk_application_window_create_menubar (G_MENU_MODEL (combined), G_ACTION_OBSERVABLE (muxer));
110 gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
111 gtk_widget_show_all (window->priv->menubar);
112 g_object_unref (combined);
113 g_object_unref (muxer);
115 gtk_widget_queue_resize (GTK_WIDGET (window));
120 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window)
122 gboolean shown_by_shell;
124 g_object_get (gtk_widget_get_settings (GTK_WIDGET (window)), "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
126 g_print ("am sbs %d\n", shown_by_shell);
130 /* the shell shows it, so don't show it locally */
131 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
132 g_menu_remove (window->priv->app_menu_section, 0);
136 /* the shell does not show it, so make sure we show it */
137 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
139 GMenuModel *app_menu;
141 app_menu = g_application_get_app_menu (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))));
143 if (app_menu != NULL)
144 g_menu_append_submenu (window->priv->app_menu_section, _("Application"), app_menu);
150 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window)
152 gboolean shown_by_shell;
154 g_object_get (gtk_widget_get_settings (GTK_WIDGET (window)), "gtk-shell-shows-menubar", &shown_by_shell, NULL);
156 g_print ("mb sbs %d\n", shown_by_shell);
160 /* the shell shows it, so don't show it locally */
161 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
162 g_menu_remove (window->priv->menubar_section, 0);
166 /* the shell does not show it, so make sure we show it */
167 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
171 menubar = g_application_get_menubar (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))));
174 g_menu_append_section (window->priv->menubar_section, NULL, menubar);
180 gtk_application_window_shell_shows_app_menu_changed (GObject *object,
184 GtkApplicationWindow *window = user_data;
186 gtk_application_window_update_shell_shows_app_menu (window);
187 gtk_application_window_update_menubar (window);
191 gtk_application_window_shell_shows_menubar_changed (GObject *object,
195 GtkApplicationWindow *window = user_data;
197 gtk_application_window_update_shell_shows_menubar (window);
198 gtk_application_window_update_menubar (window);
202 gtk_application_window_list_actions (GActionGroup *group)
204 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
206 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
210 gtk_application_window_query_action (GActionGroup *group,
211 const gchar *action_name,
213 const GVariantType **parameter_type,
214 const GVariantType **state_type,
215 GVariant **state_hint,
218 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
220 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
221 action_name, enabled, parameter_type, state_type, state_hint, state);
225 gtk_application_window_activate_action (GActionGroup *group,
226 const gchar *action_name,
229 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
231 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
235 gtk_application_window_change_action_state (GActionGroup *group,
236 const gchar *action_name,
239 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
241 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
245 gtk_application_window_lookup_action (GActionMap *action_map,
246 const gchar *action_name)
248 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
250 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
254 gtk_application_window_add_action (GActionMap *action_map,
257 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
259 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
263 gtk_application_window_remove_action (GActionMap *action_map,
264 const gchar *action_name)
266 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
268 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
272 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
274 iface->list_actions = gtk_application_window_list_actions;
275 iface->query_action = gtk_application_window_query_action;
276 iface->activate_action = gtk_application_window_activate_action;
277 iface->change_action_state = gtk_application_window_change_action_state;
281 gtk_application_window_map_iface_init (GActionMapInterface *iface)
283 iface->lookup_action = gtk_application_window_lookup_action;
284 iface->add_action = gtk_application_window_add_action;
285 iface->remove_action = gtk_application_window_remove_action;
288 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
289 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
290 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
297 static GParamSpec *gtk_application_window_properties[N_PROPS];
300 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
301 gint *minimum_height,
302 gint *natural_height)
304 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
306 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
307 ->get_preferred_height (widget, minimum_height, natural_height);
309 if (window->priv->menubar != NULL)
311 gint menubar_min_height, menubar_nat_height;
313 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
314 *minimum_height += menubar_min_height;
315 *natural_height += menubar_nat_height;
320 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
322 gint *minimum_height,
323 gint *natural_height)
325 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
327 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
328 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
330 if (window->priv->menubar != NULL)
332 gint menubar_min_height, menubar_nat_height;
334 gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
335 *minimum_height += menubar_min_height;
336 *natural_height += menubar_nat_height;
341 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
345 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
347 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
348 ->get_preferred_width (widget, minimum_width, natural_width);
350 if (window->priv->menubar != NULL)
352 gint menubar_min_width, menubar_nat_width;
354 gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
355 *minimum_width = MAX (*minimum_width, menubar_min_width);
356 *natural_width = MAX (*natural_width, menubar_nat_width);
361 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
366 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
368 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
369 ->get_preferred_width_for_height (widget, height, minimum_width, natural_width);
371 if (window->priv->menubar != NULL)
373 gint menubar_min_width, menubar_nat_width;
375 gtk_widget_get_preferred_width_for_height (window->priv->menubar, height, &menubar_min_width, &menubar_nat_width);
376 *minimum_width = MAX (*minimum_width, menubar_min_width);
377 *natural_width = MAX (*natural_width, menubar_nat_width);
382 gtk_application_window_real_size_allocate (GtkWidget *widget,
383 GtkAllocation *allocation)
385 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
387 if (window->priv->menubar != NULL)
389 GtkAllocation menubar_allocation = *allocation;
390 gint menubar_min_height, menubar_nat_height;
393 gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_min_height, &menubar_nat_height);
395 menubar_allocation.height = menubar_min_height;
396 gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
398 child = gtk_bin_get_child (GTK_BIN (window));
399 if (child != NULL && gtk_widget_get_visible (child))
401 GtkAllocation child_allocation = *allocation;
404 child_allocation.height = MAX (1, child_allocation.height - menubar_min_height);
406 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
407 child_allocation.x += border_width;
408 child_allocation.y += border_width + menubar_min_height;
409 child_allocation.width -= border_width * 2;
410 child_allocation.height -= border_width * 2 - menubar_min_height;
411 gtk_widget_size_allocate (child, &child_allocation);
414 gtk_widget_set_allocation (widget, allocation);
417 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
418 ->size_allocate (widget, allocation);
422 gtk_application_window_real_realize (GtkWidget *widget)
424 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
426 gtk_application_window_update_shell_shows_app_menu (window);
427 gtk_application_window_update_shell_shows_menubar (window);
428 gtk_application_window_update_menubar (window);
430 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
435 gtk_application_window_real_map (GtkWidget *widget)
437 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
439 /* XXX could eliminate this by tweaking gtk_window_map */
440 if (window->priv->menubar)
441 gtk_widget_map (window->priv->menubar);
443 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
448 gtk_application_window_real_forall_internal (GtkContainer *container,
449 gboolean include_internal,
450 GtkCallback callback,
453 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
455 if (window->priv->menubar)
456 callback (window->priv->menubar, user_data);
458 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
459 ->forall (container, include_internal, callback, user_data);
464 gtk_application_window_get_property (GObject *object,
469 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
473 case PROP_SHOW_MENUBAR:
474 g_value_set_boolean (value, window->priv->show_menubar);
478 g_assert_not_reached ();
483 gtk_application_window_set_property (GObject *object,
488 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
492 case PROP_SHOW_MENUBAR:
493 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
497 g_assert_not_reached ();
502 gtk_application_window_dispose (GObject *object)
504 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
506 g_clear_object (&window->priv->menubar);
507 g_clear_object (&window->priv->actions);
509 G_OBJECT_CLASS (gtk_application_window_parent_class)
514 gtk_application_window_init (GtkApplicationWindow *window)
516 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
518 window->priv->actions = g_simple_action_group_new ();
519 window->priv->app_menu_section = g_menu_new ();
520 window->priv->menubar_section = g_menu_new ();
522 /* window->priv->actions is the one and only ref on the group, so when
523 * we dispose, the action group will die, disconnecting all signals.
525 g_signal_connect_swapped (window->priv->actions, "action-added",
526 G_CALLBACK (g_action_group_action_added), window);
527 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
528 G_CALLBACK (g_action_group_action_enabled_changed), window);
529 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
530 G_CALLBACK (g_action_group_action_state_changed), window);
531 g_signal_connect_swapped (window->priv->actions, "action-removed",
532 G_CALLBACK (g_action_group_action_removed), window);
536 gtk_application_window_class_init (GtkApplicationWindowClass *class)
538 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
539 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
540 GObjectClass *object_class = G_OBJECT_CLASS (class);
542 container_class->forall = gtk_application_window_real_forall_internal;
543 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
544 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
545 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
546 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
547 widget_class->size_allocate = gtk_application_window_real_size_allocate;
548 widget_class->realize = gtk_application_window_real_realize;
549 widget_class->map = gtk_application_window_real_map;
550 object_class->get_property = gtk_application_window_get_property;
551 object_class->set_property = gtk_application_window_set_property;
552 object_class->dispose = gtk_application_window_dispose;
554 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
555 g_param_spec_boolean ("show-menubar",
556 P_("Show a menubar"),
557 P_("TRUE if the application's menus should be included "
558 "in the menubar at the top of the window"),
559 TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
560 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
561 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
565 * gtk_application_window_new:
566 * @application: a #GtkApplication
568 * Creates a new #GtkApplicationWindow.
570 * Returns: a newly created #GtkApplicationWindow
575 gtk_application_window_new (GtkApplication *application)
577 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
579 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
580 "application", application,
585 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
587 return window->priv->show_menubar;
591 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
592 gboolean show_menubar)
594 g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
596 show_menubar = !!show_menubar;
598 if (window->priv->show_menubar != show_menubar)
600 window->priv->show_menubar = show_menubar;
602 gtk_application_window_update_menubar (window);
604 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
608 /* GtkMenu construction {{{1 */
610 static void populate_menu_from_model (GtkMenuShell *menu,
612 GActionObservable *actions);
615 append_items_from_model (GtkMenuShell *menu,
617 GActionObservable *actions,
618 gboolean *need_separator,
619 const gchar *heading)
624 GtkMenuItem *menuitem;
629 n = g_menu_model_get_n_items (model);
631 if (*need_separator && n > 0)
633 w = gtk_separator_menu_item_new ();
635 gtk_menu_shell_append (menu, w);
636 *need_separator = FALSE;
641 w = gtk_menu_item_new_with_label (heading);
643 gtk_widget_set_sensitive (w, FALSE);
644 gtk_menu_shell_append (GTK_MENU_SHELL (menu), w);
647 for (i = 0; i < n; i++)
649 if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SECTION)))
652 g_menu_model_get_item_attribute (model, i, G_MENU_ATTRIBUTE_LABEL, "s", &label);
653 append_items_from_model (menu, m, actions, need_separator, label);
659 w = gtk_separator_menu_item_new ();
661 gtk_menu_shell_append (menu, w);
662 *need_separator = FALSE;
668 menuitem = gtk_model_menu_item_new (model, i, actions);
670 if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SUBMENU)))
672 submenu = gtk_menu_new ();
673 populate_menu_from_model (GTK_MENU_SHELL (submenu), m, actions);
674 gtk_menu_item_set_submenu (menuitem, submenu);
678 gtk_widget_show (GTK_WIDGET (menuitem));
679 gtk_menu_shell_append (menu, GTK_WIDGET (menuitem));
681 *need_separator = TRUE;
686 populate_menu_from_model (GtkMenuShell *menu,
688 GActionObservable *actions)
690 gboolean need_separator;
692 need_separator = FALSE;
693 append_items_from_model (menu, model, actions, &need_separator, NULL);
697 GActionObservable *actions;
701 GHashTable *connected;
705 free_items_changed_data (gpointer data)
707 ItemsChangedData *d = data;
709 g_object_unref (d->actions);
710 g_object_unref (d->model);
712 if (d->update_idle != 0)
713 g_source_remove (d->update_idle);
715 g_hash_table_unref (d->connected);
721 repopulate_menu (gpointer data)
723 ItemsChangedData *d = data;
727 /* remove current children */
728 children = gtk_container_get_children (GTK_CONTAINER (d->menu));
729 for (l = children; l; l = l->next)
732 gtk_container_remove (GTK_CONTAINER (d->menu), child);
734 g_list_free (children);
736 populate_menu_from_model (d->menu, d->model, d->actions);
743 connect_to_items_changed (GMenuModel *model,
747 ItemsChangedData *d = data;
752 if (!g_hash_table_lookup (d->connected, model))
754 g_signal_connect (model, "items-changed", callback, data);
755 g_hash_table_insert (d->connected, model, model);
758 for (i = 0; i < g_menu_model_get_n_items (model); i++)
760 iter = g_menu_model_iterate_item_links (model, i);
761 while (g_menu_link_iter_next (iter))
763 m = g_menu_link_iter_get_value (iter);
764 connect_to_items_changed (m, callback, data);
767 g_object_unref (iter);
772 items_changed (GMenuModel *model,
778 ItemsChangedData *d = data;
780 if (d->update_idle == 0)
781 d->update_idle = gdk_threads_add_idle (repopulate_menu, data);
782 connect_to_items_changed (model, G_CALLBACK (items_changed), data);
786 gtk_application_window_create_menubar (GMenuModel *model,
787 GActionObservable *actions)
789 ItemsChangedData *data;
792 menubar = gtk_menu_bar_new ();
794 data = g_new (ItemsChangedData, 1);
795 data->model = g_object_ref (model);
796 data->actions = g_object_ref (actions);
797 data->menu = GTK_MENU_SHELL (menubar);
798 data->update_idle = 0;
799 data->connected = g_hash_table_new (NULL, NULL);
801 g_object_set_data_full (G_OBJECT (menubar), "gtk-application-menu-data",
802 data, free_items_changed_data);
804 connect_to_items_changed (model, G_CALLBACK (items_changed), data);
805 repopulate_menu (data);