2 * This file is a part of hildon
4 * Copyright (C) 2008 Nokia Corporation, all rights reserved.
6 * Contact: Karl Lattimer <karl.lattimer@nokia.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser Public License as published by
10 * the Free Software Foundation; version 2 of the license.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser Public License for more details.
20 * SECTION:hildon-app-menu
21 * @short_description: Widget representing the application menu in the Hildon framework.
23 * The #HildonAppMenu is a GTK widget which represents an application
24 * menu in the Hildon framework.
26 * This menu opens from the top of the screen and contains a number of
27 * entries (#GtkButton) organized in one or two columns, depending on
28 * the size of the screen (the number of columns changes automatically
29 * if the screen is resized). Entries are added left to right and top
32 * Besides that, the #HildonAppMenu can contain a group of filter buttons
33 * (#GtkToggleButton or #GtkRadioButton).
35 * To use a #HildonAppMenu, add it to a #HildonStackableWindow using
36 * hildon_stackable_window_set_main_menu(). The menu will appear when
37 * the user presses the window title bar. Alternatively, you can show
38 * it by hand using gtk_widget_show().
40 * The menu will be automatically hidden when one of its buttons is
41 * clicked. Use g_signal_connect_after() when connecting callbacks to
42 * buttons to make sure that they're called after the menu
43 * disappears. Alternatively, you can add the button to the menu
44 * before connecting any callback.
46 * Although implemented with a #GtkWindow, #HildonAppMenu behaves like
47 * a normal ref-counted widget, so g_object_ref(), g_object_unref(),
48 * g_object_ref_sink() and friends will behave just like with any
49 * other non-toplevel widget.
52 * <title>Creating a HildonAppMenu</title>
54 * HildonStackableWindow *win;
55 * HildonAppMenu *menu;
59 * win = HILDON_STACKABLE_WINDOW (hildon_stackable_window_new ());
60 * menu = HILDON_APP_MENU (hildon_app_menu_new ());
62 * // Create a button and add it to the menu
63 * button = gtk_button_new_with_label ("Menu command one");
64 * g_signal_connect_after (button, "clicked", G_CALLBACK (button_one_clicked), userdata);
65 * hildon_app_menu_append (menu, GTK_BUTTON (button));
68 * button = gtk_button_new_with_label ("Menu command two");
69 * g_signal_connect_after (button, "clicked", G_CALLBACK (button_two_clicked), userdata);
70 * hildon_app_menu_append (menu, GTK_BUTTON (button));
72 * // Create a filter and add it to the menu
73 * filter = gtk_radio_button_new_with_label (NULL, "Filter one");
74 * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
75 * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_one_clicked), userdata);
76 * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
79 * filter = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (filter), "Filter two");
80 * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
81 * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_two_clicked), userdata);
82 * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
84 * // Add the menu to the window
85 * hildon_stackable_window_set_main_menu (win, menu);
92 #include <X11/Xatom.h>
95 #include "hildon-app-menu.h"
96 #include "hildon-app-menu-private.h"
99 grab_transfer_window_get (GtkWidget *widget);
102 hildon_app_menu_repack_items (HildonAppMenu *menu,
106 hildon_app_menu_repack_filters (HildonAppMenu *menu);
109 item_visibility_changed (GtkWidget *item,
111 HildonAppMenu *menu);
114 filter_visibility_changed (GtkWidget *item,
116 HildonAppMenu *menu);
119 remove_item_from_list (GList **list,
123 hildon_app_menu_apply_style (GtkWidget *widget);
125 G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
128 * hildon_app_menu_new:
130 * Creates a new #HildonAppMenu.
132 * Return value: A #HildonAppMenu.
135 hildon_app_menu_new (void)
137 GtkWidget *menu = g_object_new (HILDON_TYPE_APP_MENU, NULL);
142 * hildon_app_menu_insert:
143 * @menu : A #HildonAppMenu
144 * @item : A #GtkButton to add to the #HildonAppMenu
145 * @position : The position in the item list where @item is added (from 0 to n-1).
147 * Adds @item to @menu at the position indicated by @position.
150 hildon_app_menu_insert (HildonAppMenu *menu,
154 HildonAppMenuPrivate *priv;
156 g_return_if_fail (HILDON_IS_APP_MENU (menu));
157 g_return_if_fail (GTK_IS_BUTTON (item));
161 /* Add the item to the menu */
162 gtk_widget_show (GTK_WIDGET (item));
163 g_object_ref_sink (item);
164 priv->buttons = g_list_insert (priv->buttons, item, position);
165 hildon_app_menu_repack_items (menu, position);
167 /* Close the menu when the button is clicked */
168 g_signal_connect_swapped (item, "clicked", G_CALLBACK (gtk_widget_hide), menu);
169 g_signal_connect (item, "notify::visible", G_CALLBACK (item_visibility_changed), menu);
171 /* Remove item from list when it is destroyed */
172 g_object_weak_ref (G_OBJECT (item), (GWeakNotify) remove_item_from_list, &(priv->buttons));
176 * hildon_app_menu_append:
177 * @menu : A #HildonAppMenu
178 * @item : A #GtkButton to add to the #HildonAppMenu
180 * Adds @item to the end of the menu's item list.
183 hildon_app_menu_append (HildonAppMenu *menu,
186 hildon_app_menu_insert (menu, item, -1);
190 * hildon_app_menu_prepend:
191 * @menu : A #HildonAppMenu
192 * @item : A #GtkButton to add to the #HildonAppMenu
194 * Adds @item to the beginning of the menu's item list.
197 hildon_app_menu_prepend (HildonAppMenu *menu,
200 hildon_app_menu_insert (menu, item, 0);
204 * hildon_app_menu_reorder_child:
205 * @menu : A #HildonAppMenu
206 * @item : A #GtkButton to move
207 * @position : The new position to place @item (from 0 to n-1).
209 * Moves a #GtkButton to a new position within #HildonAppMenu.
212 hildon_app_menu_reorder_child (HildonAppMenu *menu,
216 HildonAppMenuPrivate *priv;
219 g_return_if_fail (HILDON_IS_APP_MENU (menu));
220 g_return_if_fail (GTK_IS_BUTTON (item));
221 g_return_if_fail (position >= 0);
224 old_position = g_list_index (priv->buttons, item);
226 g_return_if_fail (old_position >= 0);
229 priv->buttons = g_list_remove (priv->buttons, item);
230 priv->buttons = g_list_insert (priv->buttons, item, position);
232 hildon_app_menu_repack_items (menu, MIN (old_position, position));
236 * hildon_app_menu_add_filter:
237 * @menu : A #HildonAppMenu
238 * @filter : A #GtkButton to add to the #HildonAppMenu.
240 * Adds the @filter to @menu.
243 hildon_app_menu_add_filter (HildonAppMenu *menu,
246 HildonAppMenuPrivate *priv;
248 g_return_if_fail (HILDON_IS_APP_MENU (menu));
249 g_return_if_fail (GTK_IS_BUTTON (filter));
253 /* Add the filter to the menu */
254 gtk_widget_show (GTK_WIDGET (filter));
255 g_object_ref_sink (filter);
256 priv->filters = g_list_append (priv->filters, filter);
257 hildon_app_menu_repack_filters (menu);
259 /* Close the menu when the button is clicked */
260 g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
261 g_signal_connect (filter, "notify::visible", G_CALLBACK (filter_visibility_changed), menu);
263 /* Remove filter from list when it is destroyed */
264 g_object_weak_ref (G_OBJECT (filter), (GWeakNotify) remove_item_from_list, &(priv->filters));
268 hildon_app_menu_set_columns (HildonAppMenu *menu,
271 HildonAppMenuPrivate *priv;
273 g_return_if_fail (HILDON_IS_APP_MENU (menu));
274 g_return_if_fail (columns > 0);
278 if (columns != priv->columns) {
279 priv->columns = columns;
280 hildon_app_menu_repack_items (menu, 0);
285 screen_size_changed (GdkScreen *screen,
288 hildon_app_menu_apply_style (GTK_WIDGET (menu));
290 if (gdk_screen_get_width (screen) > gdk_screen_get_height (screen)) {
291 hildon_app_menu_set_columns (menu, 2);
293 hildon_app_menu_set_columns (menu, 1);
298 item_visibility_changed (GtkWidget *item,
302 hildon_app_menu_repack_items (menu, g_list_index (menu->priv->buttons, item));
306 filter_visibility_changed (GtkWidget *item,
310 hildon_app_menu_repack_filters (menu);
314 remove_item_from_list (GList **list,
317 *list = g_list_remove (*list, item);
321 hildon_app_menu_map (GtkWidget *widget)
323 HildonAppMenuPrivate *priv = HILDON_APP_MENU (widget)->priv;
325 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
327 /* Grab pointer and keyboard */
328 if (priv->transfer_window == NULL) {
329 gboolean has_grab = FALSE;
331 priv->transfer_window = grab_transfer_window_get (widget);
333 if (gdk_pointer_grab (priv->transfer_window, TRUE,
334 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
335 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
336 GDK_POINTER_MOTION_MASK, NULL, NULL,
337 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
338 if (gdk_keyboard_grab (priv->transfer_window, TRUE,
339 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
342 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
348 gtk_grab_add (widget);
350 gdk_window_destroy (priv->transfer_window);
351 priv->transfer_window = NULL;
357 hildon_app_menu_unmap (GtkWidget *widget)
359 HildonAppMenuPrivate *priv = HILDON_APP_MENU (widget)->priv;
361 /* Remove the grab */
362 if (priv->transfer_window != NULL) {
363 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
365 gtk_grab_remove (widget);
367 gdk_window_destroy (priv->transfer_window);
368 priv->transfer_window = NULL;
371 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unmap (widget);
375 hildon_app_menu_button_press (GtkWidget *widget,
376 GdkEventButton *event)
379 HildonAppMenuPrivate *priv = HILDON_APP_MENU (widget)->priv;
381 gdk_window_get_position (widget->window, &x, &y);
383 /* Whether the button has been pressed outside the widget */
384 priv->pressed_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
385 event->y_root < y || event->y_root > y + widget->allocation.height);
387 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event) {
388 return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event (widget, event);
395 hildon_app_menu_button_release (GtkWidget *widget,
396 GdkEventButton *event)
398 HildonAppMenuPrivate *priv = HILDON_APP_MENU (widget)->priv;
400 if (priv->pressed_outside) {
402 gboolean released_outside;
404 gdk_window_get_position (widget->window, &x, &y);
406 /* Whether the button has been released outside the widget */
407 released_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
408 event->y_root < y || event->y_root > y + widget->allocation.height);
410 if (released_outside) {
411 gtk_widget_hide (widget);
414 priv->pressed_outside = FALSE; /* Always reset pressed_outside to FALSE */
417 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event) {
418 return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event (widget, event);
424 /* Grab transfer window (based on the one from GtkMenu) */
426 grab_transfer_window_get (GtkWidget *widget)
429 GdkWindowAttr attributes;
430 gint attributes_mask;
434 attributes.width = 10;
435 attributes.height = 10;
436 attributes.window_type = GDK_WINDOW_TEMP;
437 attributes.wclass = GDK_INPUT_ONLY;
438 attributes.override_redirect = TRUE;
439 attributes.event_mask = 0;
441 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
443 window = gdk_window_new (gtk_widget_get_root_window (widget),
444 &attributes, attributes_mask);
445 gdk_window_set_user_data (window, widget);
447 gdk_window_show (window);
453 hildon_app_menu_realize (GtkWidget *widget)
458 const gchar *notification_type = "_HILDON_WM_WINDOW_TYPE_APP_MENU";
460 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
462 gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
464 display = gdk_drawable_get_display (widget->window);
465 atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE");
466 XChangeProperty (GDK_WINDOW_XDISPLAY (widget->window), GDK_WINDOW_XID (widget->window),
467 atom, XA_STRING, 8, PropModeReplace, (guchar *) notification_type,
468 strlen (notification_type));
470 /* Detect any screen changes */
471 screen = gtk_widget_get_screen (widget);
472 g_signal_connect (screen, "size-changed", G_CALLBACK (screen_size_changed), widget);
474 /* Force menu to set the initial layout */
475 screen_size_changed (screen, HILDON_APP_MENU (widget));
479 hildon_app_menu_unrealize (GtkWidget *widget)
481 GdkScreen *screen = gtk_widget_get_screen (widget);
482 /* Disconnect "size-changed" signal handler */
483 g_signal_handlers_disconnect_by_func (screen, G_CALLBACK (screen_size_changed), widget);
485 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unrealize (widget);
489 hildon_app_menu_apply_style (GtkWidget *widget)
493 guint horizontal_spacing, vertical_spacing, inner_border, external_border;
494 HildonAppMenuPrivate *priv = HILDON_APP_MENU (widget)->priv;
496 gtk_widget_style_get (widget,
497 "horizontal-spacing", &horizontal_spacing,
498 "vertical-spacing", &vertical_spacing,
499 "inner-border", &inner_border,
500 "external-border", &external_border,
504 gtk_table_set_row_spacings (priv->table, vertical_spacing);
505 gtk_table_set_col_spacings (priv->table, horizontal_spacing);
506 gtk_box_set_spacing (priv->vbox, vertical_spacing);
508 /* Set inner border */
509 gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
511 /* Set default size */
512 screen = gtk_widget_get_screen (widget);
513 width = gdk_screen_get_width (screen) - external_border * 2;
514 gtk_window_set_default_size (GTK_WINDOW (widget), width, -1);
518 hildon_app_menu_style_set (GtkWidget *widget,
519 GtkStyle *previous_style)
521 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
522 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
524 hildon_app_menu_apply_style (widget);
528 hildon_app_menu_repack_filters (HildonAppMenu *menu)
530 HildonAppMenuPrivate *priv = menu->priv;
533 for (iter = priv->filters; iter != NULL; iter = iter->next) {
534 GtkWidget *filter = GTK_WIDGET (iter->data);
535 GtkWidget *parent = gtk_widget_get_parent (filter);
537 g_object_ref (filter);
538 gtk_container_remove (GTK_CONTAINER (parent), filter);
542 for (iter = priv->filters; iter != NULL; iter = iter->next) {
543 GtkWidget *filter = GTK_WIDGET (iter->data);
544 if (GTK_WIDGET_VISIBLE (filter)) {
545 gtk_box_pack_start (GTK_BOX (priv->filters_hbox), filter, TRUE, TRUE, 0);
546 g_object_unref (filter);
552 * When items displayed in the menu change (e.g, a new item is added,
553 * an item is hidden or the list is reordered), the layout must be
554 * updated. To do this we repack all items starting from a given one.
557 hildon_app_menu_repack_items (HildonAppMenu *menu,
560 HildonAppMenuPrivate *priv = menu->priv;
564 /* Remove buttons from their parent */
565 if (start_from != -1) {
566 for (iter = g_list_nth (priv->buttons, start_from); iter != NULL; iter = iter->next) {
567 GtkWidget *item = GTK_WIDGET (iter->data);
568 GtkWidget *parent = gtk_widget_get_parent (item);
571 gtk_container_remove (GTK_CONTAINER (parent), item);
575 /* If items have been removed, recalculate the size of the menu */
576 gtk_window_resize (GTK_WINDOW (menu), 1, 1);
581 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
582 GtkWidget *item = GTK_WIDGET (iter->data);
583 if (GTK_WIDGET_VISIBLE (item)) {
584 /* Don't add an item to the table if it's already there */
585 if (gtk_widget_get_parent (item) == NULL) {
586 gtk_table_attach_defaults (priv->table, item, col, col + 1, row, row + 1);
587 g_object_unref (item);
589 if (++col == priv->columns) {
596 /* The number of rows/columns might have changed, so we have to
597 * resize the table */
599 gtk_table_resize (priv->table, row, priv->columns);
601 gtk_table_resize (priv->table, row + 1, priv->columns);
604 if (GTK_WIDGET_VISIBLE (GTK_WIDGET (menu))) {
605 gtk_window_reshow_with_initial_size (GTK_WINDOW (menu));
610 hildon_app_menu_init (HildonAppMenu *menu)
612 GtkWidget *alignment;
613 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
617 /* Initialize private variables */
618 priv->transfer_window = NULL;
619 priv->pressed_outside = FALSE;
620 priv->buttons = NULL;
621 priv->filters = NULL;
624 /* Create boxes and tables */
625 priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
626 priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
627 priv->table = GTK_TABLE (gtk_table_new (1, priv->columns, TRUE));
629 /* Align the filters to the center */
630 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
631 gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
633 /* Pack everything */
634 gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
635 gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
636 gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
638 /* Make menu a modal window */
639 gtk_window_set_modal (GTK_WINDOW (menu), TRUE);
641 /* This should be treated like a normal, ref-counted widget */
642 g_object_force_floating (G_OBJECT (menu));
643 GTK_WINDOW (menu)->has_user_ref_count = FALSE;
645 gtk_widget_show_all (GTK_WIDGET (priv->vbox));
649 hildon_app_menu_finalize (GObject *object)
651 HildonAppMenuPrivate *priv = HILDON_APP_MENU (object)->priv;
653 if (priv->transfer_window)
654 gdk_window_destroy (priv->transfer_window);
656 g_list_foreach (priv->buttons, (GFunc) g_object_unref, NULL);
657 g_list_foreach (priv->filters, (GFunc) g_object_unref, NULL);
659 g_list_free (priv->buttons);
660 g_list_free (priv->filters);
662 g_signal_handlers_destroy (object);
663 G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
667 hildon_app_menu_class_init (HildonAppMenuClass *klass)
669 GObjectClass *gobject_class = (GObjectClass *)klass;
670 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
672 gobject_class->finalize = hildon_app_menu_finalize;
673 widget_class->map = hildon_app_menu_map;
674 widget_class->unmap = hildon_app_menu_unmap;
675 widget_class->realize = hildon_app_menu_realize;
676 widget_class->unrealize = hildon_app_menu_unrealize;
677 widget_class->button_press_event = hildon_app_menu_button_press;
678 widget_class->button_release_event = hildon_app_menu_button_release;
679 widget_class->style_set = hildon_app_menu_style_set;
681 g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
683 gtk_widget_class_install_style_property (
686 "horizontal-spacing",
687 "Horizontal spacing on menu items",
688 "Horizontal spacing between each menu item. Does not apply to filter buttons.",
692 gtk_widget_class_install_style_property (
696 "Vertical spacing on menu items",
697 "Vertical spacing between each menu item. Does not apply to filter buttons.",
701 gtk_widget_class_install_style_property (
705 "Border between menu edges and buttons",
706 "Border between menu edges and buttons",
710 gtk_widget_class_install_style_property (
714 "Border between menu and screen edges",
715 "Border between the right and left edges of the menu and the screen edges",