Minor style fix for the previous commit
[hildon] / src / hildon-app-menu.c
index 0c84211..d35f7cf 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
  *
- * Contact: Karl Lattimer <karl.lattimer@nokia.com>
+ * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU Lesser Public License as published by
  * menu in the Hildon framework.
  *
  * This menu opens from the top of the screen and contains a number of
- * entries (#GtkButton) organized in two columns. Entries are added
- * left to right and top to bottom.
+ * entries (#GtkButton) organized in one or two columns, depending on
+ * the size of the screen (the number of columns changes automatically
+ * if the screen is resized). Entries are added left to right and top
+ * to bottom.
  *
- * Besides that, the #HildonAppMenu can contain filter buttons
- * (#GtkToggleButton or #GtkRadioButton), which can be grouped.
+ * Besides that, the #HildonAppMenu can contain a group of filter buttons
+ * (#GtkToggleButton or #GtkRadioButton).
+ *
+ * To use a #HildonAppMenu, add it to a #HildonWindow using
+ * hildon_window_set_app_menu(). The menu will appear when the user
+ * presses the window title bar. Alternatively, you can show it by
+ * hand using hildon_app_menu_popup().
+ *
+ * The menu will be automatically hidden when one of its buttons is
+ * clicked. Use g_signal_connect_after() when connecting callbacks to
+ * buttons to make sure that they're called after the menu
+ * disappears. Alternatively, you can add the button to the menu
+ * before connecting any callback.
+ *
+ * Although implemented with a #GtkWindow, #HildonAppMenu behaves like
+ * a normal ref-counted widget, so g_object_ref(), g_object_unref(),
+ * g_object_ref_sink() and friends will behave just like with any
+ * other non-toplevel widget.
  *
  * <example>
  * <title>Creating a HildonAppMenu</title>
  * <programlisting>
+ * GtkWidget *win;
  * HildonAppMenu *menu;
  * GtkWidget *button;
  * GtkWidget *filter;
- * GtkWidget *filtergroup;
  * <!-- -->
+ * win = hildon_stackable_window_new ();
  * menu = HILDON_APP_MENU (hildon_app_menu_new ());
  * <!-- -->
  * // Create a button and add it to the menu
  * button = gtk_button_new_with_label ("Menu command one");
- * g_signal_connect (button, "clicked", G_CALLBACK (button_one_clicked), userdata);
+ * g_signal_connect_after (button, "clicked", G_CALLBACK (button_one_clicked), userdata);
  * hildon_app_menu_append (menu, GTK_BUTTON (button));
+ * <!-- -->
  * // Another button
  * button = gtk_button_new_with_label ("Menu command two");
- * g_signal_connect (button, "clicked", G_CALLBACK (button_two_clicked), userdata);
+ * g_signal_connect_after (button, "clicked", G_CALLBACK (button_two_clicked), userdata);
  * hildon_app_menu_append (menu, GTK_BUTTON (button));
  * <!-- -->
  * // Create a filter and add it to the menu
- * filter = gtk_toggle_button_new_with_label ("Filter one");
- * g_signal_connect (filter, "clicked", G_CALLBACK (filter_one_clicked), userdata);
- * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter), NULL);
- * <!-- -->
- * // Create another filter and add it to a new filter group
- * filter = gtk_radio_button_new_with_label (NULL, "Filter two");
+ * filter = gtk_radio_button_new_with_label (NULL, "Filter one");
  * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
- * g_signal_connect (filter, "clicked", G_CALLBACK (filter_two_clicked), userdata);
- * filtergroup = hildon_app_menu_add_filter (menu, GTK_BUTTON (filter), NULL);
- * // Add a new filter to the same filter group
- * filter = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (filter), "Filter three");
+ * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_one_clicked), userdata);
+ * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
+ * <!-- -->
+ * // Add a new filter
+ * filter = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (filter), "Filter two");
  * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
- * g_signal_connect (filter, "clicked", G_CALLBACK (filter_three_clicked), userdata);
- * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter), filtergroup);
+ * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_two_clicked), userdata);
+ * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
  * <!-- -->
- * // Pop the menu up
- * hildon_app_menu_popup (menu);
+ * // Show all menu items
+ * gtk_widget_show_all (GTK_WIDGET (menu));
+ * <!-- -->
+ * // Add the menu to the window
+ * hildon_window_set_app_menu (HILDON_WINDOW (win), menu);
  * </programlisting>
  * </example>
  *
  */
 
-#include "hildon-app-menu.h"
-#include "hildon-app-menu-private.h"
+#include                                        <string.h>
+#include                                        <X11/Xatom.h>
+#include                                        <gdk/gdkx.h>
+
+#include                                        "hildon-gtk.h"
+#include                                        "hildon-app-menu.h"
+#include                                        "hildon-app-menu-private.h"
+#include                                        "hildon-window.h"
 
 static GdkWindow *
 grab_transfer_window_get                        (GtkWidget *widget);
 
+static void
+hildon_app_menu_repack_items                    (HildonAppMenu *menu,
+                                                 gint           start_from);
+
+static void
+hildon_app_menu_repack_filters                  (HildonAppMenu *menu);
+
+static gboolean
+can_activate_accel                              (GtkWidget *widget,
+                                                 guint      signal_id,
+                                                 gpointer   user_data);
+
+static void
+item_visibility_changed                         (GtkWidget     *item,
+                                                 GParamSpec    *arg1,
+                                                 HildonAppMenu *menu);
+
+static void
+filter_visibility_changed                       (GtkWidget     *item,
+                                                 GParamSpec    *arg1,
+                                                 HildonAppMenu *menu);
+
+static void
+remove_item_from_list                           (GList    **list,
+                                                 gpointer   item);
+
+static void
+hildon_app_menu_apply_style                     (GtkWidget *widget);
+
 G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
 
 /**
  * hildon_app_menu_new:
  *
- * Creates a new HildonAppMenu.
+ * Creates a new #HildonAppMenu.
+ *
+ * Return value: A #HildonAppMenu.
  *
- * Return value: A @HildonAppMenu.
+ * Since: 2.2
  **/
 GtkWidget *
 hildon_app_menu_new                             (void)
@@ -95,148 +151,351 @@ hildon_app_menu_new                             (void)
 }
 
 /**
- * hildon_app_menu_append
- * @menu : A @HildonAppMenu
- * @item : A @GtkButton to add to the HildonAppMenu
+ * hildon_app_menu_insert:
+ * @menu : A #HildonAppMenu
+ * @item : A #GtkButton to add to the #HildonAppMenu
+ * @position : The position in the item list where @item is added (from 0 to n-1).
+ *
+ * Adds @item to @menu at the position indicated by @position.
  *
- * Adds the @item to the @HildonAppMenu
+ * Since: 2.2
  */
 void
-hildon_app_menu_append                          (HildonAppMenu *menu,
-                                                 GtkButton *item)
+hildon_app_menu_insert                          (HildonAppMenu *menu,
+                                                 GtkButton     *item,
+                                                 gint           position)
 {
     HildonAppMenuPrivate *priv;
-    int row, col;
 
     g_return_if_fail (HILDON_IS_APP_MENU (menu));
     g_return_if_fail (GTK_IS_BUTTON (item));
 
     priv = HILDON_APP_MENU_GET_PRIVATE(menu);
 
-    /* Calculate the row and column number */
-    col = priv->nitems % 2;
-    row = (priv->nitems - col) / 2;
-    priv->nitems++;
+    /* Force widget size */
+    hildon_gtk_widget_set_theme_size (GTK_WIDGET (item),
+                                      HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
+
+    /* Add the item to the menu */
+    gtk_widget_show (GTK_WIDGET (item));
+    g_object_ref_sink (item);
+    priv->buttons = g_list_insert (priv->buttons, item, position);
+    hildon_app_menu_repack_items (menu, position);
 
-    /* GtkTable already calls gtk_table_resize() if necessary */
-    gtk_table_attach_defaults (priv->table, GTK_WIDGET (item), col, col + 1, row, row + 1);
+    /* Enable accelerators */
+    g_signal_connect (item, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
 
     /* Close the menu when the button is clicked */
     g_signal_connect_swapped (item, "clicked", G_CALLBACK (gtk_widget_hide), menu);
+    g_signal_connect (item, "notify::visible", G_CALLBACK (item_visibility_changed), menu);
 
-    gtk_widget_show (GTK_WIDGET (item));
+    /* Remove item from list when it is destroyed */
+    g_object_weak_ref (G_OBJECT (item), (GWeakNotify) remove_item_from_list, &(priv->buttons));
 }
 
 /**
- * hildon_app_menu_add_filter
- * @menu : A @HildonAppMenu
- * @filter : A @GtkButton to add to the HildonAppMenu
- * @group : An existing filter group, or %NULL to create a new one
+ * hildon_app_menu_append:
+ * @menu : A #HildonAppMenu
+ * @item : A #GtkButton to add to the #HildonAppMenu
  *
- * Adds the @filter to the @HildonAppMenu, to the group specified by @group
+ * Adds @item to the end of the menu's item list.
  *
- * Return value: The filter group where the filter has been added
+ * Since: 2.2
  */
-GtkWidget *
-hildon_app_menu_add_filter                      (HildonAppMenu *menu,
-                                                 GtkButton *filter,
-                                                 GtkWidget *group)
+void
+hildon_app_menu_append                          (HildonAppMenu *menu,
+                                                 GtkButton     *item)
 {
-    HildonAppMenuPrivate *priv;
+    hildon_app_menu_insert (menu, item, -1);
+}
 
-    g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
-    g_return_val_if_fail (GTK_IS_BUTTON (filter), NULL);
-    g_return_val_if_fail (!group || GTK_IS_BOX (group), NULL);
+/**
+ * hildon_app_menu_prepend:
+ * @menu : A #HildonAppMenu
+ * @item : A #GtkButton to add to the #HildonAppMenu
+ *
+ * Adds @item to the beginning of the menu's item list.
+ *
+ * Since: 2.2
+ */
+void
+hildon_app_menu_prepend                         (HildonAppMenu *menu,
+                                                 GtkButton     *item)
+{
+    hildon_app_menu_insert (menu, item, 0);
+}
 
-    priv = HILDON_APP_MENU_GET_PRIVATE(menu);
+/**
+ * hildon_app_menu_reorder_child:
+ * @menu : A #HildonAppMenu
+ * @item : A #GtkButton to move
+ * @position : The new position to place @item (from 0 to n-1).
+ *
+ * Moves a #GtkButton to a new position within #HildonAppMenu.
+ *
+ * Since: 2.2
+ */
+void
+hildon_app_menu_reorder_child                   (HildonAppMenu *menu,
+                                                 GtkButton     *item,
+                                                 gint           position)
+{
+    HildonAppMenuPrivate *priv;
+    gint old_position;
 
-    /* Create a new group if needed */
-    if (!group) {
-        group = gtk_hbox_new (TRUE, 0);
-        gtk_box_pack_start (priv->filters_hbox, group, TRUE, TRUE, 0);
-        gtk_widget_show (group);
-    }
+    g_return_if_fail (HILDON_IS_APP_MENU (menu));
+    g_return_if_fail (GTK_IS_BUTTON (item));
+    g_return_if_fail (position >= 0);
 
-    /* Pack the filter in the group and set its size */
-    gtk_box_pack_start (GTK_BOX (group), GTK_WIDGET (filter), TRUE, TRUE, 0);
-    gtk_size_group_add_widget (priv->sizegroup, GTK_WIDGET (filter));
+    priv = HILDON_APP_MENU_GET_PRIVATE (menu);
+    old_position = g_list_index (priv->buttons, item);
 
-    /* Close the menu when the button is clicked */
-    g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
+    g_return_if_fail (old_position >= 0);
 
-    gtk_widget_show (GTK_WIDGET (filter));
+    /* Move the item */
+    priv->buttons = g_list_remove (priv->buttons, item);
+    priv->buttons = g_list_insert (priv->buttons, item, position);
 
-    return group;
+    hildon_app_menu_repack_items (menu, MIN (old_position, position));
 }
 
 /**
- * hildon_app_menu_get_group_from_filter
- * @menu : A @HildonAppMenu
- * @filter : A @GtkButton previously added to the menu
+ * hildon_app_menu_add_filter:
+ * @menu : A #HildonAppMenu
+ * @filter : A #GtkButton to add to the #HildonAppMenu.
  *
- * Gets the filter group from a @filter previously added to a @HildonAppMenu
+ * Adds the @filter to @menu.
  *
- * Return value: The group where the @filter is in, or %NULL
+ * Since: 2.2
  */
-GtkWidget *
-hildon_app_menu_get_group_from_filter           (HildonAppMenu *menu,
+void
+hildon_app_menu_add_filter                      (HildonAppMenu *menu,
                                                  GtkButton *filter)
 {
     HildonAppMenuPrivate *priv;
-    GList *grouplist;
-    GtkWidget *result = NULL;
 
-    g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
-    g_return_val_if_fail (GTK_IS_BUTTON (filter), NULL);
+    g_return_if_fail (HILDON_IS_APP_MENU (menu));
+    g_return_if_fail (GTK_IS_BUTTON (filter));
 
     priv = HILDON_APP_MENU_GET_PRIVATE(menu);
 
-    /* Get the list of filter groups */
-    grouplist = gtk_container_get_children (GTK_CONTAINER (priv->filters_hbox));
+    /* Force widget size */
+    hildon_gtk_widget_set_theme_size (GTK_WIDGET (filter),
+                                      HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
 
-    for (; grouplist != NULL && !result; grouplist = grouplist->next) {
+    /* Add the filter to the menu */
+    gtk_widget_show (GTK_WIDGET (filter));
+    g_object_ref_sink (filter);
+    priv->filters = g_list_append (priv->filters, filter);
+    hildon_app_menu_repack_filters (menu);
 
-        GtkBox *group = GTK_BOX (grouplist->data);
-        GList *items = gtk_container_get_children (GTK_CONTAINER (group));
+    /* Enable accelerators */
+    g_signal_connect (filter, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
 
-        /* Look for the filter inside each filter group */
-        for (; items != NULL && !result; items = items->next) {
-            if (filter == items->data) {
-                result = GTK_WIDGET (group);
-            }
-        }
-        g_list_free (items);
+    /* Close the menu when the button is clicked */
+    g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
+    g_signal_connect (filter, "notify::visible", G_CALLBACK (filter_visibility_changed), menu);
+
+    /* Remove filter from list when it is destroyed */
+    g_object_weak_ref (G_OBJECT (filter), (GWeakNotify) remove_item_from_list, &(priv->filters));
+}
+
+static void
+hildon_app_menu_set_columns                     (HildonAppMenu *menu,
+                                                 guint          columns)
+{
+    HildonAppMenuPrivate *priv;
+
+    g_return_if_fail (HILDON_IS_APP_MENU (menu));
+    g_return_if_fail (columns > 0);
 
+    priv = HILDON_APP_MENU_GET_PRIVATE (menu);
+
+    if (columns != priv->columns) {
+        priv->columns = columns;
+        hildon_app_menu_repack_items (menu, 0);
     }
-    g_list_free (grouplist);
+}
 
-    if (!result)
-        g_critical("Filter not found in hildon app menu!");
+static void
+parent_window_topmost_notify                   (HildonWindow *parent_win,
+                                                GParamSpec   *arg1,
+                                                GtkWidget    *menu)
+{
+    if (!hildon_window_get_is_topmost (parent_win))
+        gtk_widget_hide (menu);
+}
 
-    return result;
+static void
+parent_window_unmapped                         (HildonWindow *parent_win,
+                                                GtkWidget    *menu)
+{
+    gtk_widget_hide (menu);
 }
 
-/**
- * hildon_app_menu_popup
- * @menu : A @HildonAppMenu
- *
- * Displays the @HildonAppMenu on top of the screen
+void G_GNUC_INTERNAL
+hildon_app_menu_set_parent_window              (HildonAppMenu *self,
+                                                GtkWindow     *parent_window)
+{
+    HildonAppMenuPrivate *priv;
+
+    g_return_if_fail (HILDON_IS_APP_MENU (self));
+    g_return_if_fail (parent_window == NULL || GTK_IS_WINDOW (parent_window));
+
+    priv = HILDON_APP_MENU_GET_PRIVATE(self);
+
+    /* Disconnect old handlers, if any */
+    if (priv->parent_window) {
+        g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, self);
+        g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, self);
+    }
+
+    /* Connect a new handler */
+    if (parent_window) {
+        g_signal_connect (parent_window, "notify::is-topmost", G_CALLBACK (parent_window_topmost_notify), self);
+        g_signal_connect (parent_window, "unmap", G_CALLBACK (parent_window_unmapped), self);
+    }
+
+    priv->parent_window = parent_window;
+
+    if (parent_window == NULL && GTK_WIDGET_VISIBLE (self))
+        gtk_widget_hide (GTK_WIDGET (self));
+}
+
+gpointer G_GNUC_INTERNAL
+hildon_app_menu_get_parent_window              (HildonAppMenu *self)
+{
+    HildonAppMenuPrivate *priv;
+
+    g_return_val_if_fail (HILDON_IS_APP_MENU (self), NULL);
+
+    priv = HILDON_APP_MENU_GET_PRIVATE (self);
+
+    return priv->parent_window;
+}
+
+static void
+screen_size_changed                            (GdkScreen     *screen,
+                                                HildonAppMenu *menu)
+{
+    hildon_app_menu_apply_style (GTK_WIDGET (menu));
+
+    if (gdk_screen_get_width (screen) > gdk_screen_get_height (screen)) {
+        hildon_app_menu_set_columns (menu, 2);
+    } else {
+        hildon_app_menu_set_columns (menu, 1);
+    }
+}
+
+static gboolean
+can_activate_accel                              (GtkWidget *widget,
+                                                 guint      signal_id,
+                                                 gpointer   user_data)
+{
+    return GTK_WIDGET_VISIBLE (widget);
+}
+
+static void
+item_visibility_changed                         (GtkWidget     *item,
+                                                 GParamSpec    *arg1,
+                                                 HildonAppMenu *menu)
+{
+    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
+
+    hildon_app_menu_repack_items (menu, g_list_index (priv->buttons, item));
+}
+
+static void
+filter_visibility_changed                       (GtkWidget     *item,
+                                                 GParamSpec    *arg1,
+                                                 HildonAppMenu *menu)
+{
+    hildon_app_menu_repack_filters (menu);
+}
+
+static void
+remove_item_from_list                           (GList    **list,
+                                                 gpointer   item)
+{
+    *list = g_list_remove (*list, item);
+}
+
+static void
+hildon_app_menu_show                            (GtkWidget *widget)
+{
+    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
+    gboolean show_menu = FALSE;
+    GList *i;
+
+    /* Don't show menu if it doesn't contain visible items */
+    for (i = priv->buttons; i && !show_menu; i = i->next)
+        show_menu = GTK_WIDGET_VISIBLE (i->data);
+
+    for (i = priv->filters; i && !show_menu; i = i->next)
+        show_menu = GTK_WIDGET_VISIBLE (i->data);
+
+    if (show_menu) {
+        GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->show (widget);
+    }
+}
+
+static void
+hildon_app_menu_show_all                        (GtkWidget *widget)
+{
+    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
+
+    /* Show children, but not self. */
+    g_list_foreach (priv->buttons, (GFunc) gtk_widget_show_all, NULL);
+    g_list_foreach (priv->filters, (GFunc) gtk_widget_show_all, NULL);
+}
+
+
+static void
+hildon_app_menu_hide_all                        (GtkWidget *widget)
+{
+    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
+
+    /* Hide children, but not self. */
+    g_list_foreach (priv->buttons, (GFunc) gtk_widget_hide_all, NULL);
+    g_list_foreach (priv->filters, (GFunc) gtk_widget_hide_all, NULL);
+}
+
+/*
+ * There's a race condition that can freeze the UI if a dialog appears
+ * between a HildonAppMenu and its parent window, see NB#100468
  */
-void
-hildon_app_menu_popup                           (HildonAppMenu *menu)
+static gboolean
+hildon_app_menu_find_intruder                   (gpointer data)
 {
-    g_return_if_fail (HILDON_IS_APP_MENU (menu));
-    int x, xpos;
-    GtkRequisition req;
-    GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (menu));
+    GtkWidget *widget = GTK_WIDGET (data);
+    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
+
+    /* If there's a window between the menu and its parent window, hide the menu */
+    if (priv->parent_window) {
+        gboolean intruder_found = FALSE;
+        GdkScreen *screen = gtk_widget_get_screen (widget);
+        GList *stack = gdk_screen_get_window_stack (screen);
+        GList *parent_pos = g_list_find (stack, GTK_WIDGET (priv->parent_window)->window);
+        GList *toplevels = gtk_window_list_toplevels ();
+        GList *i;
+
+        for (i = toplevels; i != NULL && !intruder_found; i = i->next) {
+            if (i->data != widget && i->data != priv->parent_window) {
+                if (g_list_find (parent_pos, GTK_WIDGET (i->data)->window)) {
+                    intruder_found = TRUE;
+                }
+            }
+        }
 
-    /* Position the menu in the top center of the screen */
-    gtk_window_get_default_size (GTK_WINDOW (menu), &x, NULL);
-    gtk_widget_size_request (GTK_WIDGET (menu), &req);
-    xpos = (gdk_screen_get_width (screen) - MAX(x, req.width)) / 2;
-    gtk_window_move (GTK_WINDOW (menu), xpos, 0);
+        g_list_foreach (stack, (GFunc) g_object_unref, NULL);
+        g_list_free (stack);
+        g_list_free (toplevels);
 
-    gtk_widget_show (GTK_WIDGET (menu));
+        if (intruder_found)
+            gtk_widget_hide (widget);
+    }
+
+    return FALSE;
 }
 
 static void
@@ -248,15 +507,37 @@ hildon_app_menu_map                             (GtkWidget *widget)
 
     /* Grab pointer and keyboard */
     if (priv->transfer_window == NULL) {
+        gboolean has_grab = FALSE;
+
         priv->transfer_window = grab_transfer_window_get (widget);
-        gdk_pointer_grab (
-            priv->transfer_window, TRUE,
-            GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
-            GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
-            GDK_POINTER_MOTION_MASK, NULL, NULL, GDK_CURRENT_TIME);
-        gdk_keyboard_grab (priv->transfer_window, TRUE, GDK_CURRENT_TIME);
-        gtk_grab_add (widget);
+
+        if (gdk_pointer_grab (priv->transfer_window, TRUE,
+                              GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
+                              GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
+                              GDK_POINTER_MOTION_MASK, NULL, NULL,
+                              GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
+            if (gdk_keyboard_grab (priv->transfer_window, TRUE,
+                                   GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
+                has_grab = TRUE;
+            } else {
+                gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
+                                            GDK_CURRENT_TIME);
+            }
+        }
+
+        if (has_grab) {
+            gtk_grab_add (widget);
+        } else {
+            gdk_window_destroy (priv->transfer_window);
+            priv->transfer_window = NULL;
+        }
     }
+
+    /* Make the menu temporary when it's mapped, so it's closed if a
+     * new window appears */
+    gtk_window_set_is_temporary (GTK_WINDOW (widget), TRUE);
+
+    gdk_threads_add_idle (hildon_app_menu_find_intruder, widget);
 }
 
 static void
@@ -264,37 +545,138 @@ hildon_app_menu_unmap                           (GtkWidget *widget)
 {
     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
 
+    /* Remove the grab */
     if (priv->transfer_window != NULL) {
-        /* Remove the grab */
         gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
                                     GDK_CURRENT_TIME);
         gtk_grab_remove (widget);
 
-        /* Destroy the transfer window */
         gdk_window_destroy (priv->transfer_window);
         priv->transfer_window = NULL;
     }
 
     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unmap (widget);
+
+    gtk_window_set_is_temporary (GTK_WINDOW (widget), FALSE);
+}
+
+static void
+hildon_app_menu_grab_notify                     (GtkWidget *widget,
+                                                 gboolean   was_grabbed)
+{
+    if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify)
+        GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify (widget, was_grabbed);
+
+    if (!was_grabbed && GTK_WIDGET_VISIBLE (widget))
+        gtk_widget_hide (widget);
 }
 
 static gboolean
-hildon_app_menu_button_release                  (GtkWidget *widget,
+hildon_app_menu_hide_idle                       (gpointer widget)
+{
+    gtk_widget_hide (GTK_WIDGET (widget));
+    return FALSE;
+}
+
+/* Send keyboard accelerators to the parent window, if necessary.
+ * This code is heavily based on gtk_menu_key_press ()
+ */
+static gboolean
+hildon_app_menu_key_press                       (GtkWidget   *widget,
+                                                 GdkEventKey *event)
+{
+    GtkWindow *parent_window;
+    HildonAppMenuPrivate *priv;
+
+    g_return_val_if_fail (HILDON_IS_APP_MENU (widget), FALSE);
+    g_return_val_if_fail (event != NULL, FALSE);
+
+    if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->key_press_event (widget, event))
+        return TRUE;
+
+    priv = HILDON_APP_MENU_GET_PRIVATE (widget);
+    parent_window = priv->parent_window;
+
+    if (parent_window) {
+        guint accel_key, accel_mods;
+        GdkModifierType consumed_modifiers;
+        GdkDisplay *display;
+        GSList *accel_groups;
+        GSList *list;
+
+        display = gtk_widget_get_display (widget);
+
+        /* Figure out what modifiers went into determining the key symbol */
+        gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
+                                             event->hardware_keycode, event->state, event->group,
+                                             NULL, NULL, NULL, &consumed_modifiers);
+
+        accel_key = gdk_keyval_to_lower (event->keyval);
+        accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
+
+        /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
+         * We re-upper case when we match against the keyval, but display and save in caseless form.
+         */
+        if (accel_key != event->keyval)
+            accel_mods |= GDK_SHIFT_MASK;
+
+        accel_groups = gtk_accel_groups_from_object (G_OBJECT (parent_window));
+
+        for (list = accel_groups; list; list = list->next) {
+            GtkAccelGroup *accel_group = list->data;
+
+            if (gtk_accel_group_query (accel_group, accel_key, accel_mods, NULL)) {
+                gtk_window_activate_key (parent_window, event);
+                gdk_threads_add_idle (hildon_app_menu_hide_idle, widget);
+                break;
+            }
+        }
+    }
+
+    return TRUE;
+}
+
+static gboolean
+hildon_app_menu_button_press                    (GtkWidget *widget,
                                                  GdkEventButton *event)
 {
     int x, y;
-    gboolean released_outside;
     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
 
     gdk_window_get_position (widget->window, &x, &y);
 
-    /* Whether the button has been released outside the widget */
-    released_outside = (event->window != priv->transfer_window ||
-                        event->x < x || event->x > x + widget->allocation.width ||
-                        event->y < y || event->y > y + widget->allocation.height);
+    /* Whether the button has been pressed outside the widget */
+    priv->pressed_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
+                             event->y_root < y || event->y_root > y + widget->allocation.height);
 
-    if (released_outside) {
-        gtk_widget_hide (widget);
+    if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event) {
+        return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event (widget, event);
+    } else {
+        return FALSE;
+    }
+}
+
+static gboolean
+hildon_app_menu_button_release                  (GtkWidget *widget,
+                                                 GdkEventButton *event)
+{
+    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
+
+    if (priv->pressed_outside) {
+        int x, y;
+        gboolean released_outside;
+
+        gdk_window_get_position (widget->window, &x, &y);
+
+        /* Whether the button has been released outside the widget */
+        released_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
+                            event->y_root < y || event->y_root > y + widget->allocation.height);
+
+        if (released_outside) {
+            gtk_widget_hide (widget);
+        }
+
+        priv->pressed_outside = FALSE; /* Always reset pressed_outside to FALSE */
     }
 
     if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event) {
@@ -304,6 +686,15 @@ hildon_app_menu_button_release                  (GtkWidget *widget,
     }
 }
 
+static gboolean
+hildon_app_menu_delete_event_handler            (GtkWidget   *widget,
+                                                 GdkEventAny *event)
+{
+    /* Hide the menu if it receives a delete-event, but don't destroy it */
+    gtk_widget_hide (widget);
+    return TRUE;
+}
+
 /* Grab transfer window (based on the one from GtkMenu) */
 static GdkWindow *
 grab_transfer_window_get                        (GtkWidget *widget)
@@ -335,38 +726,244 @@ grab_transfer_window_get                        (GtkWidget *widget)
 static void
 hildon_app_menu_realize                         (GtkWidget *widget)
 {
+    Atom property, window_type;
+    Display *xdisplay;
+    GdkDisplay *gdkdisplay;
+    GdkScreen *screen;
+
     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
-    gdk_window_set_override_redirect(widget->window, TRUE);
+
+    gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
+
+    gdkdisplay = gdk_drawable_get_display (widget->window);
+    xdisplay = GDK_WINDOW_XDISPLAY (widget->window);
+
+    property = gdk_x11_get_xatom_by_name_for_display (gdkdisplay, "_NET_WM_WINDOW_TYPE");
+    window_type = XInternAtom (xdisplay, "_HILDON_WM_WINDOW_TYPE_APP_MENU", False);
+    XChangeProperty (xdisplay, GDK_WINDOW_XID (widget->window), property,
+                     XA_ATOM, 32, PropModeReplace, (guchar *) &window_type, 1);
+
+    /* Detect any screen changes */
+    screen = gtk_widget_get_screen (widget);
+    g_signal_connect (screen, "size-changed", G_CALLBACK (screen_size_changed), widget);
+
+    /* Force menu to set the initial layout */
+    screen_size_changed (screen, HILDON_APP_MENU (widget));
 }
 
 static void
-hildon_app_menu_init                            (HildonAppMenu *menu)
+hildon_app_menu_unrealize                       (GtkWidget *widget)
 {
-    GtkWidget *alignment;
-    GdkScreen *screen;
-    int width;
-    guint filter_group_spacing, horizontal_spacing, vertical_spacing,
-            external_border;
-    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
+    GdkScreen *screen = gtk_widget_get_screen (widget);
+    /* Disconnect "size-changed" signal handler */
+    g_signal_handlers_disconnect_by_func (screen, G_CALLBACK (screen_size_changed), widget);
+
+    GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unrealize (widget);
+}
+
+static void
+hildon_app_menu_apply_style                     (GtkWidget *widget)
+{
+    guint horizontal_spacing, vertical_spacing, filter_vertical_spacing;
+    guint inner_border;
+    HildonAppMenuPrivate *priv;
+
+    priv = HILDON_APP_MENU_GET_PRIVATE (widget);
 
-    gtk_widget_style_get (GTK_WIDGET (menu),
-                          "filter-group-spacing", &filter_group_spacing,
+    gtk_widget_style_get (widget,
                           "horizontal-spacing", &horizontal_spacing,
                           "vertical-spacing", &vertical_spacing,
-                          "external-border", &external_border,
+                          "filter-vertical-spacing", &filter_vertical_spacing,
+                          "inner-border", &inner_border,
                           NULL);
 
+    /* Set spacings */
+    gtk_table_set_row_spacings (priv->table, vertical_spacing);
+    gtk_table_set_col_spacings (priv->table, horizontal_spacing);
+    gtk_box_set_spacing (priv->vbox, filter_vertical_spacing);
+
+    /* Set inner border */
+    gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
+}
+
+static void
+hildon_app_menu_style_set                       (GtkWidget *widget,
+                                                 GtkStyle  *previous_style)
+{
+    if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
+        GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
+
+    hildon_app_menu_apply_style (widget);
+}
+
+static void
+hildon_app_menu_repack_filters                  (HildonAppMenu *menu)
+{
+    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
+    GList *iter;
+
+    for (iter = priv->filters; iter != NULL; iter = iter->next) {
+        GtkWidget *filter = GTK_WIDGET (iter->data);
+        GtkWidget *parent = gtk_widget_get_parent (filter);
+        if (parent) {
+            g_object_ref (filter);
+            gtk_container_remove (GTK_CONTAINER (parent), filter);
+        }
+    }
+
+    for (iter = priv->filters; iter != NULL; iter = iter->next) {
+        GtkWidget *filter = GTK_WIDGET (iter->data);
+        if (GTK_WIDGET_VISIBLE (filter)) {
+            gtk_box_pack_start (GTK_BOX (priv->filters_hbox), filter, TRUE, TRUE, 0);
+            g_object_unref (filter);
+            /* GtkButton must be realized for accelerators to work */
+            gtk_widget_realize (filter);
+        }
+    }
+}
+
+/*
+ * When items displayed in the menu change (e.g, a new item is added,
+ * an item is hidden or the list is reordered), the layout must be
+ * updated. To do this we repack all items starting from a given one.
+ */
+static void
+hildon_app_menu_repack_items                    (HildonAppMenu *menu,
+                                                 gint           start_from)
+{
+    HildonAppMenuPrivate *priv;
+    gint row, col;
+    GList *iter;
+
+    priv = HILDON_APP_MENU_GET_PRIVATE(menu);
+
+    /* Remove buttons from their parent */
+    if (start_from != -1) {
+        for (iter = g_list_nth (priv->buttons, start_from); iter != NULL; iter = iter->next) {
+            GtkWidget *item = GTK_WIDGET (iter->data);
+            GtkWidget *parent = gtk_widget_get_parent (item);
+            if (parent) {
+                g_object_ref (item);
+                gtk_container_remove (GTK_CONTAINER (parent), item);
+            }
+        }
+
+        /* If items have been removed, recalculate the size of the menu */
+        gtk_window_resize (GTK_WINDOW (menu), 1, 1);
+    }
+
+    /* Add buttons */
+    row = col = 0;
+    for (iter = priv->buttons; iter != NULL; iter = iter->next) {
+        GtkWidget *item = GTK_WIDGET (iter->data);
+        if (GTK_WIDGET_VISIBLE (item)) {
+            /* Don't add an item to the table if it's already there */
+            if (gtk_widget_get_parent (item) == NULL) {
+                gtk_table_attach_defaults (priv->table, item, col, col + 1, row, row + 1);
+                g_object_unref (item);
+                /* GtkButton must be realized for accelerators to work */
+                gtk_widget_realize (item);
+            }
+            if (++col == priv->columns) {
+                col = 0;
+                row++;
+            }
+        }
+    }
+
+    /* The number of rows/columns might have changed, so we have to
+     * resize the table */
+    if (col == 0) {
+        gtk_table_resize (priv->table, MAX (row, 1), priv->columns);
+    } else {
+        gtk_table_resize (priv->table, row + 1, priv->columns);
+    }
+}
+
+/**
+ * hildon_app_menu_popup:
+ * @menu: a #HildonAppMenu
+ * @parent_window: a #GtkWindow
+ *
+ * Displays a menu on top of a window and makes it available for
+ * selection.
+ *
+ * Since: 2.2
+ **/
+void
+hildon_app_menu_popup                           (HildonAppMenu *menu,
+                                                 GtkWindow     *parent_window)
+{
+    g_return_if_fail (HILDON_IS_APP_MENU (menu));
+    g_return_if_fail (GTK_IS_WINDOW (parent_window));
+
+    hildon_app_menu_set_parent_window (menu, parent_window);
+    gtk_widget_show (GTK_WIDGET (menu));
+}
+
+/**
+ * hildon_app_menu_get_items:
+ * @menu: a #HildonAppMenu
+ *
+ * Returns a list of all items (regular items, not filters) contained
+ * in @menu.
+ *
+ * Returns: a newly-allocated list containing the items in @menu
+ *
+ * Since: 2.2
+ **/
+GList *
+hildon_app_menu_get_items                       (HildonAppMenu *menu)
+{
+    HildonAppMenuPrivate *priv;
+
+    g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
+
+    priv = HILDON_APP_MENU_GET_PRIVATE (menu);
+
+    return g_list_copy (priv->buttons);
+}
+
+/**
+ * hildon_app_menu_get_filters:
+ * @menu: a #HildonAppMenu
+ *
+ * Returns a list of all filters contained in @menu.
+ *
+ * Returns: a newly-allocated list containing the filters in @menu
+ *
+ * Since: 2.2
+ **/
+GList *
+hildon_app_menu_get_filters                     (HildonAppMenu *menu)
+{
+    HildonAppMenuPrivate *priv;
+
+    g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
+
+    priv = HILDON_APP_MENU_GET_PRIVATE (menu);
+
+    return g_list_copy (priv->filters);
+}
+
+static void
+hildon_app_menu_init                            (HildonAppMenu *menu)
+{
+    GtkWidget *alignment;
+    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
+
     /* Initialize private variables */
-    priv->filters_hbox = GTK_BOX (gtk_hbox_new (FALSE, filter_group_spacing));
-    priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 10));
-    priv->table = GTK_TABLE (gtk_table_new (1, 2, TRUE));
-    priv->sizegroup = GTK_SIZE_GROUP (gtk_size_group_new (GTK_SIZE_GROUP_BOTH));
-    priv->nitems = 0;
+    priv->parent_window = NULL;
     priv->transfer_window = NULL;
+    priv->pressed_outside = FALSE;
+    priv->buttons = NULL;
+    priv->filters = NULL;
+    priv->columns = 2;
 
-    /* Set spacing between table elements */
-    gtk_table_set_row_spacings (priv->table, vertical_spacing);
-    gtk_table_set_col_spacings (priv->table, horizontal_spacing);
+    /* Create boxes and tables */
+    priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
+    priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
+    priv->table = GTK_TABLE (gtk_table_new (1, priv->columns, TRUE));
 
     /* Align the filters to the center */
     alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
@@ -377,14 +974,14 @@ hildon_app_menu_init                            (HildonAppMenu *menu)
     gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
     gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
 
-    /* Set default size */
-    screen = gtk_widget_get_screen (GTK_WIDGET (menu));
-    width = gdk_screen_get_width (screen) - external_border * 2;
-    gtk_window_set_default_size (GTK_WINDOW (menu), width, -1);
+    /* Make menu a modal window */
+    gtk_window_set_modal (GTK_WINDOW (menu), TRUE);
 
-    gtk_widget_show_all (GTK_WIDGET (priv->vbox));
+    /* This should be treated like a normal, ref-counted widget */
+    g_object_force_floating (G_OBJECT (menu));
+    GTK_WINDOW (menu)->has_user_ref_count = FALSE;
 
-    gtk_window_set_type_hint (GTK_WINDOW (menu), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
+    gtk_widget_show_all (GTK_WIDGET (priv->vbox));
 }
 
 static void
@@ -392,10 +989,20 @@ hildon_app_menu_finalize                        (GObject *object)
 {
     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
 
-    g_object_unref (priv->sizegroup);
+    if (priv->parent_window) {
+        g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, object);
+        g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, object);
+    }
+
     if (priv->transfer_window)
         gdk_window_destroy (priv->transfer_window);
 
+    g_list_foreach (priv->buttons, (GFunc) g_object_unref, NULL);
+    g_list_foreach (priv->filters, (GFunc) g_object_unref, NULL);
+
+    g_list_free (priv->buttons);
+    g_list_free (priv->filters);
+
     g_signal_handlers_destroy (object);
     G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
 }
@@ -407,46 +1014,55 @@ hildon_app_menu_class_init                      (HildonAppMenuClass *klass)
     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
 
     gobject_class->finalize = hildon_app_menu_finalize;
+    widget_class->show = hildon_app_menu_show;
+    widget_class->show_all = hildon_app_menu_show_all;
+    widget_class->hide_all = hildon_app_menu_hide_all;
     widget_class->map = hildon_app_menu_map;
     widget_class->unmap = hildon_app_menu_unmap;
     widget_class->realize = hildon_app_menu_realize;
+    widget_class->unrealize = hildon_app_menu_unrealize;
+    widget_class->grab_notify = hildon_app_menu_grab_notify;
+    widget_class->key_press_event = hildon_app_menu_key_press;
+    widget_class->button_press_event = hildon_app_menu_button_press;
     widget_class->button_release_event = hildon_app_menu_button_release;
+    widget_class->style_set = hildon_app_menu_style_set;
+    widget_class->delete_event = hildon_app_menu_delete_event_handler;
 
     g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
 
     gtk_widget_class_install_style_property (
         widget_class,
         g_param_spec_uint (
-            "filter-group-spacing",
-            "Space between filter groups",
-            "Space in pixels between the filter groups",
-            0, G_MAXUINT, 10,
+            "horizontal-spacing",
+            "Horizontal spacing on menu items",
+            "Horizontal spacing between each menu item. Does not apply to filter buttons.",
+            0, G_MAXUINT, 16,
             G_PARAM_READABLE));
 
     gtk_widget_class_install_style_property (
         widget_class,
         g_param_spec_uint (
-            "horizontal-spacing",
-            "Horizontal spacing on menu items",
-            "Horizontal spacing between each menu item (but not filters)",
-            0, G_MAXUINT, 10,
+            "vertical-spacing",
+            "Vertical spacing on menu items",
+            "Vertical spacing between each menu item. Does not apply to filter buttons.",
+            0, G_MAXUINT, 16,
             G_PARAM_READABLE));
 
     gtk_widget_class_install_style_property (
         widget_class,
         g_param_spec_uint (
-            "vertical-spacing",
-            "Vertical spacing on menu items",
-            "Vertical spacing between each menu item (but not filters)",
-            0, G_MAXUINT, 10,
+            "filter-vertical-spacing",
+            "Vertical spacing between filters and menu items",
+            "Vertical spacing between filters and menu items",
+            0, G_MAXUINT, 8,
             G_PARAM_READABLE));
 
     gtk_widget_class_install_style_property (
         widget_class,
         g_param_spec_uint (
-            "external-border",
-            "Border between menu and screen edges",
-            "Border between the right and left edges of the menu and the screen edges",
-            0, G_MAXUINT, 40,
+            "inner-border",
+            "Border between menu edges and buttons",
+            "Border between menu edges and buttons",
+            0, G_MAXUINT, 16,
             G_PARAM_READABLE));
 }