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 "gtkmodelmenu.h"
27 #include "gactionmuxer.h"
31 * SECTION:gtkapplicationwindow
32 * @title: GtkApplicationWindow
33 * @short_description: GtkWindow subclass with GtkApplication support
35 * GtkApplicationWindow is a #GtkWindow subclass that offers some
36 * extra functionality for better integration with #GtkApplication
37 * features. Notably, it can handle both the application menu as well
38 * as the menubar. See g_application_set_app_menu() and
39 * g_application_set_menubar().
41 * This class implements the #GActionGroup and #GActionMap interfaces,
42 * to let you add window-specific actions that will be exported by the
43 * associated #GtkApplication, together with its application-wide
44 * actions. Window-specific actions are prefixed with the "win."
45 * prefix and application-wide actions are prefixed with the "app."
46 * prefix. Actions must be addressed with the prefixed name when
47 * referring to them from a #GMenuModel.
49 * If the desktop environment does not display the application menu
50 * as part of the desktop shell, then #GApplicationWindow will
51 * automatically show the menu as part of a menubar. This behaviour
52 * can be overridden with the #GtkApplicationWindow:show-menubar
55 struct _GtkApplicationWindowPrivate
57 GSimpleActionGroup *actions;
60 GMenu *app_menu_section;
61 GMenu *menubar_section;
62 gboolean show_menubar;
66 gtk_application_window_update_menubar (GtkApplicationWindow *window)
68 gboolean should_have_menubar;
69 gboolean have_menubar;
71 have_menubar = window->priv->menubar != NULL;
73 should_have_menubar = window->priv->show_menubar &&
74 (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
75 g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
77 if (have_menubar && !should_have_menubar)
79 gtk_widget_unparent (window->priv->menubar);
80 window->priv->menubar = NULL;
82 gtk_widget_queue_resize (GTK_WIDGET (window));
85 if (!have_menubar && should_have_menubar)
90 muxer = g_action_muxer_new ();
91 g_action_muxer_insert (muxer, "app", G_ACTION_GROUP (gtk_window_get_application (GTK_WINDOW (window))));
92 g_action_muxer_insert (muxer, "win", G_ACTION_GROUP (window));
94 combined = g_menu_new ();
95 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
96 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
98 window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), G_ACTION_OBSERVABLE (muxer));
99 gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
100 gtk_widget_show_all (window->priv->menubar);
101 g_object_unref (combined);
102 g_object_unref (muxer);
104 gtk_widget_queue_resize (GTK_WIDGET (window));
109 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
110 GtkSettings *settings)
112 gboolean shown_by_shell;
114 g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
118 /* the shell shows it, so don't show it locally */
119 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
120 g_menu_remove (window->priv->app_menu_section, 0);
124 /* the shell does not show it, so make sure we show it */
125 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
127 GMenuModel *app_menu;
129 app_menu = g_application_get_app_menu (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))));
131 if (app_menu != NULL)
132 g_menu_append_submenu (window->priv->app_menu_section, _("Application"), app_menu);
138 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
139 GtkSettings *settings)
141 gboolean shown_by_shell;
143 g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
147 /* the shell shows it, so don't show it locally */
148 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
149 g_menu_remove (window->priv->menubar_section, 0);
153 /* the shell does not show it, so make sure we show it */
154 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
158 menubar = g_application_get_menubar (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))));
161 g_menu_append_section (window->priv->menubar_section, NULL, menubar);
167 gtk_application_window_shell_shows_app_menu_changed (GObject *object,
171 GtkApplicationWindow *window = user_data;
173 gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
174 gtk_application_window_update_menubar (window);
178 gtk_application_window_shell_shows_menubar_changed (GObject *object,
182 GtkApplicationWindow *window = user_data;
184 gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
185 gtk_application_window_update_menubar (window);
189 gtk_application_window_list_actions (GActionGroup *group)
191 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
193 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
197 gtk_application_window_query_action (GActionGroup *group,
198 const gchar *action_name,
200 const GVariantType **parameter_type,
201 const GVariantType **state_type,
202 GVariant **state_hint,
205 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
207 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
208 action_name, enabled, parameter_type, state_type, state_hint, state);
212 gtk_application_window_activate_action (GActionGroup *group,
213 const gchar *action_name,
216 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
218 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
222 gtk_application_window_change_action_state (GActionGroup *group,
223 const gchar *action_name,
226 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
228 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
232 gtk_application_window_lookup_action (GActionMap *action_map,
233 const gchar *action_name)
235 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
237 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
241 gtk_application_window_add_action (GActionMap *action_map,
244 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
246 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
250 gtk_application_window_remove_action (GActionMap *action_map,
251 const gchar *action_name)
253 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
255 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
259 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
261 iface->list_actions = gtk_application_window_list_actions;
262 iface->query_action = gtk_application_window_query_action;
263 iface->activate_action = gtk_application_window_activate_action;
264 iface->change_action_state = gtk_application_window_change_action_state;
268 gtk_application_window_map_iface_init (GActionMapInterface *iface)
270 iface->lookup_action = gtk_application_window_lookup_action;
271 iface->add_action = gtk_application_window_add_action;
272 iface->remove_action = gtk_application_window_remove_action;
275 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
276 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
277 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
284 static GParamSpec *gtk_application_window_properties[N_PROPS];
287 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
288 gint *minimum_height,
289 gint *natural_height)
291 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
293 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
294 ->get_preferred_height (widget, minimum_height, natural_height);
296 if (window->priv->menubar != NULL)
298 gint menubar_min_height, menubar_nat_height;
300 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
301 *minimum_height += menubar_min_height;
302 *natural_height += menubar_nat_height;
307 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
309 gint *minimum_height,
310 gint *natural_height)
312 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
314 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
315 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
317 if (window->priv->menubar != NULL)
319 gint menubar_min_height, menubar_nat_height;
321 gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
322 *minimum_height += menubar_min_height;
323 *natural_height += menubar_nat_height;
328 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
332 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
334 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
335 ->get_preferred_width (widget, minimum_width, natural_width);
337 if (window->priv->menubar != NULL)
339 gint menubar_min_width, menubar_nat_width;
341 gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
342 *minimum_width = MAX (*minimum_width, menubar_min_width);
343 *natural_width = MAX (*natural_width, menubar_nat_width);
348 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
353 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
355 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
356 ->get_preferred_width_for_height (widget, height, minimum_width, natural_width);
358 if (window->priv->menubar != NULL)
360 gint menubar_min_width, menubar_nat_width;
362 gtk_widget_get_preferred_width_for_height (window->priv->menubar, height, &menubar_min_width, &menubar_nat_width);
363 *minimum_width = MAX (*minimum_width, menubar_min_width);
364 *natural_width = MAX (*natural_width, menubar_nat_width);
369 gtk_application_window_real_size_allocate (GtkWidget *widget,
370 GtkAllocation *allocation)
372 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
374 if (window->priv->menubar != NULL)
376 GtkAllocation menubar_allocation = *allocation;
377 gint menubar_min_height, menubar_nat_height;
380 gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_min_height, &menubar_nat_height);
382 menubar_allocation.height = menubar_min_height;
383 gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
385 child = gtk_bin_get_child (GTK_BIN (window));
386 if (child != NULL && gtk_widget_get_visible (child))
388 GtkAllocation child_allocation = *allocation;
391 child_allocation.height = MAX (1, child_allocation.height - menubar_min_height);
393 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
394 child_allocation.x += border_width;
395 child_allocation.y += border_width + menubar_min_height;
396 child_allocation.width -= border_width * 2;
397 child_allocation.height -= border_width * 2 - menubar_min_height;
398 gtk_widget_size_allocate (child, &child_allocation);
401 gtk_widget_set_allocation (widget, allocation);
404 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
405 ->size_allocate (widget, allocation);
409 gtk_application_window_real_realize (GtkWidget *widget)
411 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
412 GtkSettings *settings;
414 settings = gtk_widget_get_settings (widget);
416 g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
417 G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
418 g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
419 G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
421 gtk_application_window_update_shell_shows_app_menu (window, settings);
422 gtk_application_window_update_shell_shows_menubar (window, settings);
423 gtk_application_window_update_menubar (window);
425 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
430 gtk_application_window_real_unrealize (GtkWidget *widget)
432 GtkSettings *settings;
434 settings = gtk_widget_get_settings (widget);
436 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
437 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
439 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
440 ->unrealize (widget);
444 gtk_application_window_real_map (GtkWidget *widget)
446 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
448 /* XXX could eliminate this by tweaking gtk_window_map */
449 if (window->priv->menubar)
450 gtk_widget_map (window->priv->menubar);
452 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
457 gtk_application_window_real_forall_internal (GtkContainer *container,
458 gboolean include_internal,
459 GtkCallback callback,
462 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
464 if (window->priv->menubar)
465 callback (window->priv->menubar, user_data);
467 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
468 ->forall (container, include_internal, callback, user_data);
473 gtk_application_window_get_property (GObject *object,
478 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
482 case PROP_SHOW_MENUBAR:
483 g_value_set_boolean (value, window->priv->show_menubar);
487 g_assert_not_reached ();
492 gtk_application_window_set_property (GObject *object,
497 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
501 case PROP_SHOW_MENUBAR:
502 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
506 g_assert_not_reached ();
511 gtk_application_window_dispose (GObject *object)
513 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
515 if (window->priv->menubar)
517 gtk_widget_unparent (window->priv->menubar);
518 window->priv->menubar = NULL;
521 g_clear_object (&window->priv->app_menu_section);
522 g_clear_object (&window->priv->menubar_section);
523 g_clear_object (&window->priv->actions);
525 G_OBJECT_CLASS (gtk_application_window_parent_class)
530 gtk_application_window_init (GtkApplicationWindow *window)
532 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
534 window->priv->actions = g_simple_action_group_new ();
535 window->priv->app_menu_section = g_menu_new ();
536 window->priv->menubar_section = g_menu_new ();
538 /* window->priv->actions is the one and only ref on the group, so when
539 * we dispose, the action group will die, disconnecting all signals.
541 g_signal_connect_swapped (window->priv->actions, "action-added",
542 G_CALLBACK (g_action_group_action_added), window);
543 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
544 G_CALLBACK (g_action_group_action_enabled_changed), window);
545 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
546 G_CALLBACK (g_action_group_action_state_changed), window);
547 g_signal_connect_swapped (window->priv->actions, "action-removed",
548 G_CALLBACK (g_action_group_action_removed), window);
552 gtk_application_window_class_init (GtkApplicationWindowClass *class)
554 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
555 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
556 GObjectClass *object_class = G_OBJECT_CLASS (class);
558 container_class->forall = gtk_application_window_real_forall_internal;
559 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
560 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
561 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
562 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
563 widget_class->size_allocate = gtk_application_window_real_size_allocate;
564 widget_class->realize = gtk_application_window_real_realize;
565 widget_class->unrealize = gtk_application_window_real_unrealize;
566 widget_class->map = gtk_application_window_real_map;
567 object_class->get_property = gtk_application_window_get_property;
568 object_class->set_property = gtk_application_window_set_property;
569 object_class->dispose = gtk_application_window_dispose;
572 * GtkApplicationWindow:show-menubar:
574 * If this property is %TRUE, the window will display a menubar
575 * that includes the app menu and menubar, unless these are
576 * shown by the desktop shell. See g_application_set_app_menu()
577 * and g_application_set_menubar().
579 * If %FALSE, the window will not display a menubar, regardless
580 * of whether the desktop shell is showing the menus or not.
582 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
583 g_param_spec_boolean ("show-menubar",
584 P_("Show a menubar"),
585 P_("TRUE if the window should show a "
586 "menubar at the top of the window"),
587 TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
588 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
589 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
593 * gtk_application_window_new:
594 * @application: a #GtkApplication
596 * Creates a new #GtkApplicationWindow.
598 * Returns: a newly created #GtkApplicationWindow
603 gtk_application_window_new (GtkApplication *application)
605 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
607 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
608 "application", application,
613 * gtk_application_window_get_show_menubar:
614 * @window: a #GtkApplicationWindow
616 * Returns whether the window will display a menubar for the app menu
617 * and menubar as needed.
619 * Returns: %TRUE if @window will display a menubar when needed
624 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
626 return window->priv->show_menubar;
630 * gtk_application_window_set_show_menubar:
631 * @window: a #GtkApplicationWindow
632 * @show_menubar: whether to show a menubar when needed
634 * Sets whether the window will display a menubar for the app menu
635 * and menubar as needed.
640 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
641 gboolean show_menubar)
643 g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
645 show_menubar = !!show_menubar;
647 if (window->priv->show_menubar != show_menubar)
649 window->priv->show_menubar = show_menubar;
651 gtk_application_window_update_menubar (window);
653 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);