2009-04-01 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-app-menu.c
index 6107add..6a3f95a 100644 (file)
  * 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
@@ -85,7 +85,7 @@
  * 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>
  *
@@ -434,13 +434,6 @@ hildon_app_menu_show                            (GtkWidget *widget)
     for (i = priv->filters; i && !show_menu; i = i->next)
         show_menu = GTK_WIDGET_VISIBLE (i->data);
 
-    /* Don't show menu if its parent window is not the topmost one */
-    if (show_menu && priv->parent_window) {
-        show_menu =
-            GTK_WIDGET_VISIBLE (priv->parent_window) &&
-            hildon_window_get_is_topmost (HILDON_WINDOW (priv->parent_window));
-    }
-
     if (show_menu) {
         GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->show (widget);
     }
@@ -467,6 +460,45 @@ hildon_app_menu_hide_all                        (GtkWidget *widget)
     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);
+
+    priv->find_intruder_idle_id = 0;
+
+    /* 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)
@@ -502,6 +534,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);
+
+    priv->find_intruder_idle_id = gdk_threads_add_idle (hildon_app_menu_find_intruder, widget);
 }
 
 static void
@@ -520,6 +558,8 @@ 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
@@ -536,7 +576,9 @@ hildon_app_menu_grab_notify                     (GtkWidget *widget,
 static gboolean
 hildon_app_menu_hide_idle                       (gpointer widget)
 {
+    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
     gtk_widget_hide (GTK_WIDGET (widget));
+    priv->hide_idle_id = 0;
     return FALSE;
 }
 
@@ -589,7 +631,7 @@ hildon_app_menu_key_press                       (GtkWidget   *widget,
 
             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);
+                priv->hide_idle_id = gdk_threads_add_idle (hildon_app_menu_hide_idle, widget);
                 break;
             }
         }
@@ -648,6 +690,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)
@@ -677,6 +728,17 @@ grab_transfer_window_get                        (GtkWidget *widget)
 }
 
 static void
+hildon_app_menu_size_request                    (GtkWidget      *widget,
+                                                 GtkRequisition *requisition)
+{
+    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
+
+    GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->size_request (widget, requisition);
+
+    requisition->width = priv->width_request;
+}
+
+static void
 hildon_app_menu_realize                         (GtkWidget *widget)
 {
     Atom property, window_type;
@@ -718,8 +780,8 @@ 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, external_border;
     HildonAppMenuPrivate *priv;
 
     priv = HILDON_APP_MENU_GET_PRIVATE (widget);
@@ -727,6 +789,7 @@ 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);
@@ -734,15 +797,19 @@ hildon_app_menu_apply_style                     (GtkWidget *widget)
     /* 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 */
+    /* Compute width request */
     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);
+    if (gdk_screen_get_width (screen) < gdk_screen_get_height (screen)) {
+        external_border = 0;
+    }
+    priv->width_request = gdk_screen_get_width (screen) - external_border * 2;
+    gtk_window_move (GTK_WINDOW (widget), external_border, 0);
+    gtk_widget_queue_resize (widget);
 }
 
 static void
@@ -837,10 +904,72 @@ hildon_app_menu_repack_items                    (HildonAppMenu *menu,
     } 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
@@ -856,6 +985,9 @@ hildon_app_menu_init                            (HildonAppMenu *menu)
     priv->buttons = NULL;
     priv->filters = NULL;
     priv->columns = 2;
+    priv->width_request = -1;
+    priv->find_intruder_idle_id = 0;
+    priv->hide_idle_id = 0;
 
     /* Create boxes and tables */
     priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
@@ -886,6 +1018,16 @@ hildon_app_menu_finalize                        (GObject *object)
 {
     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
 
+    if (priv->find_intruder_idle_id) {
+        g_source_remove (priv->find_intruder_idle_id);
+        priv->find_intruder_idle_id = 0;
+    }
+
+    if (priv->hide_idle_id) {
+        g_source_remove (priv->hide_idle_id);
+        priv->hide_idle_id = 0;
+    }
+
     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);
@@ -923,6 +1065,8 @@ hildon_app_menu_class_init                      (HildonAppMenuClass *klass)
     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;
+    widget_class->size_request = hildon_app_menu_size_request;
 
     g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
 
@@ -947,6 +1091,15 @@ hildon_app_menu_class_init                      (HildonAppMenuClass *klass)
     gtk_widget_class_install_style_property (
         widget_class,
         g_param_spec_uint (
+            "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 (
             "inner-border",
             "Border between menu edges and buttons",
             "Border between menu edges and buttons",
@@ -957,8 +1110,9 @@ hildon_app_menu_class_init                      (HildonAppMenuClass *klass)
         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,
+            "Border between menu and screen edges (in horizontal mode)",
+            "Border between the right and left edges of the menu and "
+            "the screen edges (in horizontal mode)",
+            0, G_MAXUINT, 50,
             G_PARAM_READABLE));
 }