Replaced the name of the clicked attribute, now it is button_pressed
[hildon] / hildon / hildon-app-menu.c
index 1c9b3c4..8b6974d 100644 (file)
@@ -403,7 +403,8 @@ item_visibility_changed                         (GtkWidget     *item,
 {
     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
 
-    hildon_app_menu_repack_items (menu, g_list_index (priv->buttons, item));
+    if (! priv->inhibit_repack)
+        hildon_app_menu_repack_items (menu, g_list_index (priv->buttons, item));
 }
 
 static void
@@ -411,7 +412,10 @@ filter_visibility_changed                       (GtkWidget     *item,
                                                  GParamSpec    *arg1,
                                                  HildonAppMenu *menu)
 {
-    hildon_app_menu_repack_filters (menu);
+    HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
+
+    if (! priv->inhibit_repack)
+        hildon_app_menu_repack_filters (menu);
 }
 
 static void
@@ -424,22 +428,38 @@ remove_item_from_list                           (GList    **list,
 static void
 hildon_app_menu_show_all                        (GtkWidget *widget)
 {
+    HildonAppMenu *menu = HILDON_APP_MENU (widget);
     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
 
+    priv->inhibit_repack = TRUE;
+
     /* 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);
+
+    priv->inhibit_repack = FALSE;
+
+    hildon_app_menu_repack_items (menu, 0);
+    hildon_app_menu_repack_filters (menu);
 }
 
 
 static void
 hildon_app_menu_hide_all                        (GtkWidget *widget)
 {
+    HildonAppMenu *menu = HILDON_APP_MENU (widget);
     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
 
+    priv->inhibit_repack = TRUE;
+
     /* 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);
+
+    priv->inhibit_repack = FALSE;
+
+    hildon_app_menu_repack_items (menu, 0);
+    hildon_app_menu_repack_filters (menu);
 }
 
 /*
@@ -848,14 +868,18 @@ hildon_app_menu_repack_items                    (HildonAppMenu *menu,
                                                  gint           start_from)
 {
     HildonAppMenuPrivate *priv;
-    gint row, col;
+    gint row, col, nvisible, i;
     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) {
+    i = nvisible = 0;
+    for (iter = priv->buttons; iter != NULL; iter = iter->next) {
+        /* Count number of visible items */
+        if (GTK_WIDGET_VISIBLE (iter->data))
+            nvisible++;
+        /* Remove buttons from their parent */
+        if (start_from != -1 && i >= start_from) {
             GtkWidget *item = GTK_WIDGET (iter->data);
             GtkWidget *parent = gtk_widget_get_parent (item);
             if (parent) {
@@ -863,10 +887,16 @@ hildon_app_menu_repack_items                    (HildonAppMenu *menu,
                 gtk_container_remove (GTK_CONTAINER (parent), item);
             }
         }
+        i++;
+    }
 
-        /* If items have been removed, recalculate the size of the menu */
+    /* If items have been removed, recalculate the size of the menu */
+    if (start_from != -1)
         gtk_window_resize (GTK_WINDOW (menu), 1, 1);
-    }
+
+    /* Set the final size now to avoid unnecessary resizes later */
+    if (nvisible > 0)
+        gtk_table_resize (priv->table, ((nvisible - 1) / priv->columns) + 1, priv->columns);
 
     /* Add buttons */
     row = col = 0;
@@ -886,14 +916,6 @@ 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, MAX (row, 1), priv->columns);
-    } else {
-        gtk_table_resize (priv->table, row + 1, priv->columns);
-    }
 }
 
 /**
@@ -988,6 +1010,7 @@ hildon_app_menu_init                            (HildonAppMenu *menu)
     priv->parent_window = NULL;
     priv->transfer_window = NULL;
     priv->pressed_outside = FALSE;
+    priv->inhibit_repack = FALSE;
     priv->buttons = NULL;
     priv->filters = NULL;
     priv->columns = 2;