Make HildonEntry:size a G_PARAM_CONSTRUCT property
[hildon] / hildon / hildon-app-menu.c
index 178586a..f1df859 100644 (file)
@@ -20,8 +20,8 @@
  * SECTION:hildon-app-menu
  * @short_description: Widget representing the application menu in the Hildon framework.
  *
- * The #HildonAppMenu is a GTK widget which represents an application
- * menu in the Hildon framework.
+ * #HildonAppMenu is a GTK widget which represents an application menu
+ * in the Hildon framework.
  *
  * This menu opens from the top of the screen and contains a number of
  * entries (#GtkButton) organized in one or two columns, depending on
  * if the screen is resized). Entries are added left to right and top
  * to bottom.
  *
- * Besides that, the #HildonAppMenu can contain a group of filter buttons
- * (#GtkToggleButton or #GtkRadioButton).
+ * Besides that, #HildonAppMenu can contain a group of filter buttons
+ * (#GtkToggleButton or #GtkRadioButton). Filters are meant to change
+ * the way data is presented in the application, rather than change
+ * the layout of the menu itself. For example, a file manager can have
+ * filters to decide the order used to display a list of files (name,
+ * date, size, etc.).
  *
  * To use a #HildonAppMenu, add it to a #HildonWindow using
  * hildon_window_set_app_menu(). The menu will appear when the user
 #include                                        "hildon-app-menu-private.h"
 #include                                        "hildon-window.h"
 #include                                        "hildon-banner.h"
+#include                                        "hildon-animation-actor.h"
 
 static GdkWindow *
 grab_transfer_window_get                        (GtkWidget *widget);
@@ -178,10 +183,10 @@ hildon_app_menu_insert                          (HildonAppMenu *menu,
                                       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);
+    if (GTK_WIDGET_VISIBLE (item))
+        hildon_app_menu_repack_items (menu, position);
 
     /* Enable accelerators */
     g_signal_connect (item, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
@@ -285,10 +290,10 @@ hildon_app_menu_add_filter                      (HildonAppMenu *menu,
                                       HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
 
     /* 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);
+    if (GTK_WIDGET_VISIBLE (filter))
+        hildon_app_menu_repack_filters (menu);
 
     /* Enable accelerators */
     g_signal_connect (filter, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
@@ -403,7 +408,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 +417,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 +433,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);
 }
 
 /*
@@ -472,7 +497,7 @@ hildon_app_menu_find_intruder                   (gpointer data)
                      * Yes, this is a hack. See NB#111027 */
                     if (HILDON_IS_BANNER (i->data)) {
                         gtk_widget_hide (i->data);
-                    } else {
+                    } else if (!HILDON_IS_ANIMATION_ACTOR (i->data)) {
                         intruder_found = TRUE;
                     }
                 }
@@ -495,14 +520,15 @@ hildon_app_menu_map                             (GtkWidget *widget)
 {
     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
 
+    if (priv->transfer_window == NULL)
+        priv->transfer_window = grab_transfer_window_get (widget);
+
     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
 
     /* Grab pointer and keyboard */
-    if (priv->transfer_window == NULL) {
+    if (priv->transfer_window != NULL) {
         gboolean has_grab = FALSE;
 
-        priv->transfer_window = grab_transfer_window_get (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 |
@@ -848,14 +874,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 +893,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 +922,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 +1016,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;