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 guint initialized_gsetting_monitoring : 1;
64 guint shell_shows_app_menu : 1;
65 guint default_show_menubar : 1;
66 guint did_override_show_menubar : 1;
67 guint override_show_menubar : 1;
71 recreate_menubar (GtkApplicationWindow *window);
73 gtk_application_window_create_menubar (GtkApplicationWindow *window);
76 on_shell_shows_app_menu_changed (GtkSettings *settings,
80 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (user_data);
83 g_object_get (settings, "gtk-shell-shows-app-menu", &val, NULL);
85 if (window->priv->shell_shows_app_menu != val)
87 window->priv->shell_shows_app_menu = val;
88 recreate_menubar (window);
93 gtk_application_window_list_actions (GActionGroup *group)
95 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
97 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
101 gtk_application_window_query_action (GActionGroup *group,
102 const gchar *action_name,
104 const GVariantType **parameter_type,
105 const GVariantType **state_type,
106 GVariant **state_hint,
109 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
111 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
112 action_name, enabled, parameter_type, state_type, state_hint, state);
116 gtk_application_window_activate_action (GActionGroup *group,
117 const gchar *action_name,
120 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
122 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
126 gtk_application_window_change_action_state (GActionGroup *group,
127 const gchar *action_name,
130 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
132 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
136 gtk_application_window_lookup_action (GActionMap *action_map,
137 const gchar *action_name)
139 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
141 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
145 gtk_application_window_add_action (GActionMap *action_map,
148 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
150 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
154 gtk_application_window_remove_action (GActionMap *action_map,
155 const gchar *action_name)
157 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
159 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
163 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
165 iface->list_actions = gtk_application_window_list_actions;
166 iface->query_action = gtk_application_window_query_action;
167 iface->activate_action = gtk_application_window_activate_action;
168 iface->change_action_state = gtk_application_window_change_action_state;
172 gtk_application_window_map_iface_init (GActionMapInterface *iface)
174 iface->lookup_action = gtk_application_window_lookup_action;
175 iface->add_action = gtk_application_window_add_action;
176 iface->remove_action = gtk_application_window_remove_action;
179 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
180 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
181 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
188 static GParamSpec *gtk_application_window_properties[N_PROPS];
191 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
192 gint *minimum_height,
193 gint *natural_height)
195 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
197 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
198 ->get_preferred_height (widget, minimum_height, natural_height);
200 if (window->priv->menubar != NULL)
202 gint menubar_min_height, menubar_nat_height;
204 gtk_widget_get_preferred_height (GTK_WIDGET (window->priv->menubar), &menubar_min_height, &menubar_nat_height);
205 *minimum_height += menubar_min_height;
206 *natural_height += menubar_nat_height;
211 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
213 gint *minimum_height,
214 gint *natural_height)
216 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
218 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
219 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
221 if (window->priv->menubar != NULL)
223 gint menubar_min_height, menubar_nat_height;
225 gtk_widget_get_preferred_height_for_width (GTK_WIDGET (window->priv->menubar), width, &menubar_min_height, &menubar_nat_height);
226 *minimum_height += menubar_min_height;
227 *natural_height += menubar_nat_height;
232 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
236 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
238 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
239 ->get_preferred_width (widget, minimum_width, natural_width);
241 if (window->priv->menubar != NULL)
243 gint menubar_min_width, menubar_nat_width;
245 gtk_widget_get_preferred_width (GTK_WIDGET (window->priv->menubar), &menubar_min_width, &menubar_nat_width);
246 *minimum_width = MAX (*minimum_width, menubar_min_width);
247 *natural_width = MAX (*natural_width, menubar_nat_width);
252 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
257 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
259 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
260 ->get_preferred_width_for_height (widget, height, minimum_width, natural_width);
262 if (window->priv->menubar != NULL)
264 gint menubar_min_width, menubar_nat_width;
266 gtk_widget_get_preferred_width_for_height (GTK_WIDGET (window->priv->menubar), height, &menubar_min_width, &menubar_nat_width);
267 *minimum_width = MAX (*minimum_width, menubar_min_width);
268 *natural_width = MAX (*natural_width, menubar_nat_width);
273 gtk_application_window_real_size_allocate (GtkWidget *widget,
274 GtkAllocation *allocation)
276 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
278 if (window->priv->menubar != NULL)
280 GtkAllocation menubar_allocation = *allocation;
281 gint menubar_min_height, menubar_nat_height;
284 gtk_widget_get_preferred_height_for_width (GTK_WIDGET (window->priv->menubar), allocation->width, &menubar_min_height, &menubar_nat_height);
286 menubar_allocation.height = menubar_min_height;
287 gtk_widget_size_allocate (GTK_WIDGET (window->priv->menubar), &menubar_allocation);
289 child = gtk_bin_get_child (GTK_BIN (window));
290 if (child != NULL && gtk_widget_get_visible (child))
292 GtkAllocation child_allocation = *allocation;
295 child_allocation.height = MAX (1, child_allocation.height - menubar_min_height);
297 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
298 child_allocation.x += border_width;
299 child_allocation.y += border_width + menubar_min_height;
300 child_allocation.width -= border_width * 2;
301 child_allocation.height -= border_width * 2 - menubar_min_height;
302 gtk_widget_size_allocate (child, &child_allocation);
305 gtk_widget_set_allocation (widget, allocation);
308 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
309 ->size_allocate (widget, allocation);
313 gtk_application_window_real_realize (GtkWidget *widget)
315 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
317 if (!window->priv->initialized_gsetting_monitoring)
319 window->priv->initialized_gsetting_monitoring = TRUE;
320 g_signal_connect (gtk_widget_get_settings ((GtkWidget*)window),
321 "notify::gtk-shell-shows-app-menu",
322 G_CALLBACK (on_shell_shows_app_menu_changed),
324 on_shell_shows_app_menu_changed (gtk_widget_get_settings ((GtkWidget*)window), NULL, window);
327 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
332 gtk_application_window_real_map (GtkWidget *widget)
334 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
336 /* XXX could eliminate this by tweaking gtk_window_map */
337 if (window->priv->menubar)
338 gtk_widget_map (GTK_WIDGET (window->priv->menubar));
340 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
345 gtk_application_window_real_forall_internal (GtkContainer *container,
346 gboolean include_internal,
347 GtkCallback callback,
350 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
352 if (window->priv->menubar)
353 callback (GTK_WIDGET (window->priv->menubar), user_data);
355 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
356 ->forall (container, include_internal, callback, user_data);
361 gtk_application_window_get_property (GObject *object,
366 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
370 case PROP_SHOW_MENUBAR:
371 g_value_set_boolean (value, window->priv->override_show_menubar);
375 g_assert_not_reached ();
380 gtk_application_window_set_property (GObject *object,
385 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
389 case PROP_SHOW_MENUBAR:
390 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
394 g_assert_not_reached ();
399 gtk_application_window_dispose (GObject *object)
401 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
403 g_clear_object (&window->priv->menubar);
404 g_clear_object (&window->priv->actions);
406 G_OBJECT_CLASS (gtk_application_window_parent_class)
411 gtk_application_window_init (GtkApplicationWindow *window)
413 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
415 window->priv->actions = g_simple_action_group_new ();
417 /* window->priv->actions is the one and only ref on the group, so when
418 * we dispose, the action group will die, disconnecting all signals.
420 g_signal_connect_swapped (window->priv->actions, "action-added",
421 G_CALLBACK (g_action_group_action_added), window);
422 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
423 G_CALLBACK (g_action_group_action_enabled_changed), window);
424 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
425 G_CALLBACK (g_action_group_action_state_changed), window);
426 g_signal_connect_swapped (window->priv->actions, "action-removed",
427 G_CALLBACK (g_action_group_action_removed), window);
431 gtk_application_window_class_init (GtkApplicationWindowClass *class)
433 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
434 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
435 GObjectClass *object_class = G_OBJECT_CLASS (class);
437 container_class->forall = gtk_application_window_real_forall_internal;
438 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
439 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
440 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
441 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
442 widget_class->size_allocate = gtk_application_window_real_size_allocate;
443 widget_class->realize = gtk_application_window_real_realize;
444 widget_class->map = gtk_application_window_real_map;
445 object_class->get_property = gtk_application_window_get_property;
446 object_class->set_property = gtk_application_window_set_property;
447 object_class->dispose = gtk_application_window_dispose;
449 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
450 g_param_spec_boolean ("show-menubar",
451 P_("Show a menubar"),
452 P_("TRUE if the application's menus should be included "
453 "in the menubar at the top of the window"),
454 FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
455 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
456 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
460 * gtk_application_window_new:
461 * @application: a #GtkApplication
463 * Creates a new #GtkApplicationWindow.
465 * Returns: a newly created #GtkApplicationWindow
470 gtk_application_window_new (GtkApplication *application)
472 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
474 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
475 "application", application,
480 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
482 return window->priv->override_show_menubar;
486 recreate_menubar (GtkApplicationWindow *window)
488 GtkWidget *new_menubar;
490 if (window->priv->menubar)
491 gtk_widget_unparent (GTK_WIDGET (window->priv->menubar));
492 g_clear_object (&window->priv->menubar);
494 new_menubar = gtk_application_window_create_menubar (window);
498 window->priv->menubar = g_object_ref_sink (new_menubar);
499 gtk_widget_set_parent (new_menubar, GTK_WIDGET (window));
500 gtk_widget_show_all (new_menubar);
505 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
509 window->priv->did_override_show_menubar = TRUE;
510 if (window->priv->override_show_menubar != show)
512 window->priv->override_show_menubar = show;
513 recreate_menubar (window);
514 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
518 /* GtkMenu construction {{{1 */
520 static void populate_menu_from_model (GtkMenuShell *menu,
522 GActionGroup *group);
525 append_items_from_model (GtkMenuShell *menu,
528 gboolean *need_separator,
529 const gchar *heading)
534 GtkMenuItem *menuitem;
539 n = g_menu_model_get_n_items (model);
541 if (*need_separator && n > 0)
543 w = gtk_separator_menu_item_new ();
545 gtk_menu_shell_append (menu, w);
546 *need_separator = FALSE;
551 w = gtk_menu_item_new_with_label (heading);
553 gtk_widget_set_sensitive (w, FALSE);
554 gtk_menu_shell_append (GTK_MENU_SHELL (menu), w);
557 for (i = 0; i < n; i++)
559 if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SECTION)))
562 g_menu_model_get_item_attribute (model, i, G_MENU_ATTRIBUTE_LABEL, "s", &label);
563 append_items_from_model (menu, m, group, need_separator, label);
569 w = gtk_separator_menu_item_new ();
571 gtk_menu_shell_append (menu, w);
572 *need_separator = FALSE;
578 menuitem = gtk_model_menu_item_new (model, i, G_ACTION_OBSERVABLE (group));
580 if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SUBMENU)))
582 submenu = gtk_menu_new ();
583 populate_menu_from_model (GTK_MENU_SHELL (submenu), m, group);
584 gtk_menu_item_set_submenu (menuitem, submenu);
588 gtk_widget_show (GTK_WIDGET (menuitem));
589 gtk_menu_shell_append (menu, GTK_WIDGET (menuitem));
591 *need_separator = TRUE;
596 populate_menu_from_model (GtkMenuShell *menu,
600 gboolean need_separator;
602 need_separator = FALSE;
603 append_items_from_model (menu, model, group, &need_separator, NULL);
608 GtkApplication *application;
609 GtkApplicationWindow *window;
612 GHashTable *connected;
616 free_items_changed_data (gpointer data)
618 ItemsChangedData *d = data;
620 g_object_unref (d->muxer);
621 g_object_unref (d->window);
622 g_object_unref (d->application);
624 if (d->update_idle != 0)
625 g_source_remove (d->update_idle);
627 g_hash_table_unref (d->connected);
633 repopulate_menu (gpointer data)
635 ItemsChangedData *d = data;
638 GtkMenuShell *app_shell;
639 GMenuModel *app_model;
640 GMenuModel *window_model;
642 /* remove current children */
643 children = gtk_container_get_children (GTK_CONTAINER (d->menu));
644 for (l = children; l; l = l->next)
647 gtk_container_remove (GTK_CONTAINER (d->menu), child);
649 g_list_free (children);
651 app_model = g_application_get_app_menu (G_APPLICATION (d->application));
653 if (app_model && !d->window->priv->shell_shows_app_menu)
655 child = gtk_menu_item_new_with_label (_("Application"));
656 app_shell = (GtkMenuShell*)gtk_menu_new ();
657 gtk_menu_item_set_submenu ((GtkMenuItem*)child, (GtkWidget*)app_shell);
659 populate_menu_from_model (app_shell, app_model, (GActionGroup*)d->muxer);
660 gtk_menu_shell_append ((GtkMenuShell*)d->menu, child);
663 window_model = g_application_get_menubar (G_APPLICATION (d->application));
666 populate_menu_from_model (d->menu, window_model, (GActionGroup*)d->muxer);
675 connect_to_items_changed (GMenuModel *model,
679 ItemsChangedData *d = data;
684 if (!g_hash_table_lookup (d->connected, model))
686 g_signal_connect (model, "items-changed", callback, data);
687 g_hash_table_insert (d->connected, model, model);
690 for (i = 0; i < g_menu_model_get_n_items (model); i++)
692 iter = g_menu_model_iterate_item_links (model, i);
693 while (g_menu_link_iter_next (iter))
695 m = g_menu_link_iter_get_value (iter);
696 connect_to_items_changed (m, callback, data);
699 g_object_unref (iter);
704 items_changed (GMenuModel *model,
710 ItemsChangedData *d = data;
712 if (d->update_idle == 0)
713 d->update_idle = gdk_threads_add_idle (repopulate_menu, data);
714 connect_to_items_changed (model, G_CALLBACK (items_changed), data);
718 gtk_application_window_create_menubar (GtkApplicationWindow *window)
720 GtkApplication *application;
721 GMenuModel *app_model;
722 GMenuModel *win_model;
723 ItemsChangedData *data;
726 application = gtk_window_get_application (GTK_WINDOW (window));
727 app_model = g_application_get_app_menu (G_APPLICATION (application));
728 win_model = g_application_get_menubar (G_APPLICATION (application));
730 if (!(app_model || win_model)
731 || (window->priv->did_override_show_menubar
732 && window->priv->override_show_menubar == FALSE))
735 menubar = gtk_menu_bar_new ();
737 data = g_new (ItemsChangedData, 1);
738 data->muxer = g_action_muxer_new ();
739 g_action_muxer_insert (data->muxer, "app", G_ACTION_GROUP (application));
740 g_action_muxer_insert (data->muxer, "win", G_ACTION_GROUP (window));
741 data->window = g_object_ref (window);
742 data->application = g_object_ref (application);
743 data->menu = GTK_MENU_SHELL (menubar);
744 data->update_idle = 0;
745 data->connected = g_hash_table_new (NULL, NULL);
747 g_object_set_data_full (G_OBJECT (menubar), "gtk-application-menu-data",
748 data, free_items_changed_data);
751 connect_to_items_changed (app_model, G_CALLBACK (items_changed), data);
753 connect_to_items_changed (win_model, G_CALLBACK (items_changed), data);
755 repopulate_menu (data);