2009-03-10 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-app-menu.c
index f066a77..0813d8a 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
  * Besides that, the #HildonAppMenu can contain a group of filter buttons
  * (#GtkToggleButton or #GtkRadioButton).
  *
- * To use a #HildonAppMenu, add it to a #HildonStackableWindow using
- * hildon_stackable_window_set_main_menu(). The menu will appear when
- * the user presses the window title bar. Alternatively, you can show
- * it by hand using gtk_widget_show().
+ * 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
  * <example>
  * <title>Creating a HildonAppMenu</title>
  * <programlisting>
- * HildonStackableWindow *win;
+ * GtkWidget *win;
  * HildonAppMenu *menu;
  * GtkWidget *button;
  * GtkWidget *filter;
  * <!-- -->
- * win = HILDON_STACKABLE_WINDOW (hildon_stackable_window_new ());
+ * 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_after (button, "clicked", G_CALLBACK (button_one_clicked), userdata);
  * hildon_app_menu_append (menu, GTK_BUTTON (button));
- * gtk_widget_show (button);
  * <!-- -->
  * // Another button
  * button = gtk_button_new_with_label ("Menu command two");
  * g_signal_connect_after (button, "clicked", G_CALLBACK (button_two_clicked), userdata);
  * hildon_app_menu_append (menu, GTK_BUTTON (button));
- * gtk_widget_show (button);
  * <!-- -->
  * // Create a filter and add it to the menu
  * filter = gtk_radio_button_new_with_label (NULL, "Filter one");
  * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
  * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_one_clicked), userdata);
  * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
- * gtk_widget_show (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_after (filter, "clicked", G_CALLBACK (filter_two_clicked), userdata);
  * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
- * gtk_widget_show (filter);
+ * <!-- -->
+ * // Show all menu items
+ * gtk_widget_show_all (GTK_WIDGET (menu));
  * <!-- -->
  * // Add the menu to the window
- * hildon_stackable_window_set_main_menu (win, menu);
+ * hildon_window_set_app_menu (HILDON_WINDOW (win), menu);
  * </programlisting>
  * </example>
  *
@@ -99,6 +98,7 @@
 #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);
@@ -140,6 +140,8 @@ G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
  * Creates a new #HildonAppMenu.
  *
  * Return value: A #HildonAppMenu.
+ *
+ * Since: 2.2
  **/
 GtkWidget *
 hildon_app_menu_new                             (void)
@@ -155,6 +157,8 @@ hildon_app_menu_new                             (void)
  * @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.
+ *
+ * Since: 2.2
  */
 void
 hildon_app_menu_insert                          (HildonAppMenu *menu,
@@ -179,7 +183,6 @@ hildon_app_menu_insert                          (HildonAppMenu *menu,
     hildon_app_menu_repack_items (menu, position);
 
     /* Enable accelerators */
-    gtk_widget_realize (GTK_WIDGET (item));
     g_signal_connect (item, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
 
     /* Close the menu when the button is clicked */
@@ -196,6 +199,8 @@ hildon_app_menu_insert                          (HildonAppMenu *menu,
  * @item : A #GtkButton to add to the #HildonAppMenu
  *
  * Adds @item to the end of the menu's item list.
+ *
+ * Since: 2.2
  */
 void
 hildon_app_menu_append                          (HildonAppMenu *menu,
@@ -210,6 +215,8 @@ hildon_app_menu_append                          (HildonAppMenu *menu,
  * @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,
@@ -225,6 +232,8 @@ hildon_app_menu_prepend                         (HildonAppMenu *menu,
  * @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,
@@ -256,6 +265,8 @@ hildon_app_menu_reorder_child                   (HildonAppMenu *menu,
  * @filter : A #GtkButton to add to the #HildonAppMenu.
  *
  * Adds the @filter to @menu.
+ *
+ * Since: 2.2
  */
 void
 hildon_app_menu_add_filter                      (HildonAppMenu *menu,
@@ -279,7 +290,6 @@ hildon_app_menu_add_filter                      (HildonAppMenu *menu,
     hildon_app_menu_repack_filters (menu);
 
     /* Enable accelerators */
-    gtk_widget_realize (GTK_WIDGET (filter));
     g_signal_connect (filter, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
 
     /* Close the menu when the button is clicked */
@@ -307,6 +317,22 @@ hildon_app_menu_set_columns                     (HildonAppMenu *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);
+}
+
+static void
+parent_window_unmapped                         (HildonWindow *parent_win,
+                                                GtkWidget    *menu)
+{
+    gtk_widget_hide (menu);
+}
+
 void G_GNUC_INTERNAL
 hildon_app_menu_set_parent_window              (HildonAppMenu *self,
                                                 GtkWindow     *parent_window)
@@ -318,7 +344,34 @@ hildon_app_menu_set_parent_window              (HildonAppMenu *self,
 
     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
@@ -370,15 +423,82 @@ remove_item_from_list                           (GList    **list,
 static void
 hildon_app_menu_show                            (GtkWidget *widget)
 {
-    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
+    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
+    gboolean show_menu = FALSE;
+    GList *i;
 
-    /* Show the menu only if it's not empty */
-    if (priv->buttons || priv->filters) {
+    /* 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
+ */
+static gboolean
+hildon_app_menu_find_intruder                   (gpointer data)
+{
+    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;
+                }
+            }
+        }
+
+        g_list_foreach (stack, (GFunc) g_object_unref, NULL);
+        g_list_free (stack);
+        g_list_free (toplevels);
+
+        if (intruder_found)
+            gtk_widget_hide (widget);
+    }
+
+    return FALSE;
+}
+
+static void
 hildon_app_menu_map                             (GtkWidget *widget)
 {
     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
@@ -412,6 +532,12 @@ hildon_app_menu_map                             (GtkWidget *widget)
             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
@@ -430,6 +556,19 @@ hildon_app_menu_unmap                           (GtkWidget *widget)
     }
 
     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
@@ -616,9 +755,8 @@ hildon_app_menu_unrealize                       (GtkWidget *widget)
 static void
 hildon_app_menu_apply_style                     (GtkWidget *widget)
 {
-    GdkScreen *screen;
-    gint width;
-    guint horizontal_spacing, vertical_spacing, inner_border, external_border;
+    guint horizontal_spacing, vertical_spacing, filter_vertical_spacing;
+    guint inner_border;
     HildonAppMenuPrivate *priv;
 
     priv = HILDON_APP_MENU_GET_PRIVATE (widget);
@@ -626,22 +764,17 @@ hildon_app_menu_apply_style                     (GtkWidget *widget)
     gtk_widget_style_get (widget,
                           "horizontal-spacing", &horizontal_spacing,
                           "vertical-spacing", &vertical_spacing,
+                          "filter-vertical-spacing", &filter_vertical_spacing,
                           "inner-border", &inner_border,
-                          "external-border", &external_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, vertical_spacing);
+    gtk_box_set_spacing (priv->vbox, filter_vertical_spacing);
 
     /* Set inner border */
     gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
-
-    /* Set default size */
-    screen = gtk_widget_get_screen (widget);
-    width = gdk_screen_get_width (screen) - external_border * 2;
-    gtk_window_set_default_size (GTK_WINDOW (widget), width, -1);
 }
 
 static void
@@ -674,6 +807,8 @@ hildon_app_menu_repack_filters                  (HildonAppMenu *menu)
         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);
         }
     }
 }
@@ -717,6 +852,8 @@ hildon_app_menu_repack_items                    (HildonAppMenu *menu,
             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;
@@ -728,14 +865,76 @@ hildon_app_menu_repack_items                    (HildonAppMenu *menu,
     /* The number of rows/columns might have changed, so we have to
      * resize the table */
     if (col == 0) {
-        gtk_table_resize (priv->table, row, priv->columns);
+        gtk_table_resize (priv->table, MAX (row, 1), priv->columns);
     } else {
         gtk_table_resize (priv->table, row + 1, priv->columns);
     }
+}
 
-    if (GTK_WIDGET_VISIBLE (GTK_WIDGET (menu))) {
-        gtk_window_reshow_with_initial_size (GTK_WINDOW (menu));
-    }
+/**
+ * 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
@@ -781,6 +980,11 @@ hildon_app_menu_finalize                        (GObject *object)
 {
     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
 
+    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);
 
@@ -802,10 +1006,13 @@ hildon_app_menu_class_init                      (HildonAppMenuClass *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;
@@ -834,18 +1041,18 @@ hildon_app_menu_class_init                      (HildonAppMenuClass *klass)
     gtk_widget_class_install_style_property (
         widget_class,
         g_param_spec_uint (
-            "inner-border",
-            "Border between menu edges and buttons",
-            "Border between menu edges and buttons",
-            0, G_MAXUINT, 16,
+            "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));
 }