2009-01-12 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-date-selector.c
index 7cc3bfd..1bbd993 100644 (file)
  * SECTION:hildon-date-selector
  * @short_description: A widget to select the current date.
  *
- * HildonDateSelector is a date widget, equivalent to hildon-calendar, but with a multi-column
- * approach
+ * #HildonDateSelector is a date widget with multiple columns. Users
+ * can choose a date by selecting values in the day, month and year
+ * columns.
  *
+ * The currently selected month and year can be altered with
+ * hildon_date_selector_select_month(). The day can be selected from
+ * the active month using hildon_date_selector_select_day().
  */
 
 #define _GNU_SOURCE     /* needed for GNU nl_langinfo_l */
@@ -87,6 +91,8 @@ struct _HildonDateSelectorPrivate
   gint creation_day;
   gint creation_month;
   gint creation_year;           /* date at creation time */
+
+  gint current_num_days;
 };
 
 static void hildon_date_selector_finalize (GObject * object);
@@ -214,6 +220,7 @@ hildon_date_selector_init (HildonDateSelector * selector)
 {
   GSList *iter = NULL;
   gint current_item = 0;
+  HildonTouchSelectorColumn *column = NULL;
 
   selector->priv = HILDON_DATE_SELECTOR_GET_PRIVATE (selector);
 
@@ -233,24 +240,28 @@ hildon_date_selector_init (HildonDateSelector * selector)
   selector->priv->year_model = _create_year_model (selector);
   selector->priv->month_model = _create_month_model (selector);
   selector->priv->day_model = _create_day_model (selector);
+  selector->priv->current_num_days = 31;
 
-  /* We add the columns: FIXME: check the locale order */
+  /* We add the columns, checking the locale order */
   iter = selector->priv->column_order;
   for (iter = selector->priv->column_order; iter; iter = g_slist_next (iter)) {
     current_item = GPOINTER_TO_INT (iter->data);
 
     switch (current_item) {
     case DAY:
-      hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
-                                                selector->priv->day_model, TRUE);
+      column = hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
+                                                         selector->priv->day_model, TRUE);
+      g_object_set (G_OBJECT (column), "text-column", 0, NULL);
       break;
     case MONTH:
-      hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
-                                                selector->priv->month_model, TRUE);
+      column = hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
+                                                         selector->priv->month_model, TRUE);
+      g_object_set (G_OBJECT (column), "text-column", 0, NULL);
       break;
     case YEAR:
-      hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
-                                                selector->priv->year_model, TRUE);
+      column = hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
+                                                         selector->priv->year_model, TRUE);
+      g_object_set (G_OBJECT (column), "text-column", 0, NULL);
       break;
     default:
       g_error ("Current column order incorrect");
@@ -275,6 +286,7 @@ hildon_date_selector_finalize (GObject * object)
   selector = HILDON_DATE_SELECTOR (object);
 
   g_slist_free (selector->priv->column_order);
+  g_free (selector->priv->format);
 
   (*G_OBJECT_CLASS (hildon_date_selector_parent_class)->finalize) (object);
 }
@@ -473,6 +485,7 @@ static GtkTreeModel *
 _update_day_model (HildonDateSelector * selector)
 {
   GtkListStore *store_days = NULL;
+  GtkTreePath *path = NULL;
   gint i = 0;
   GtkTreeIter iter;
   static gchar label[255];
@@ -482,32 +495,38 @@ _update_day_model (HildonDateSelector * selector)
   guint current_month = 0;
   guint num_days = 31;
 
-  hildon_date_selector_get_date (selector, NULL, NULL, &current_day);
-
-  hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector),
-                                      selector->priv->month_column, &iter);
-  gtk_tree_model_get (selector->priv->month_model,
-                      &iter, COLUMN_INT, &current_month, -1);
-
-  hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector),
-                                      selector->priv->year_column, &iter);
-  gtk_tree_model_get (selector->priv->year_model,
-                      &iter, COLUMN_INT, &current_year, -1);
+  hildon_date_selector_get_date (selector, &current_year, &current_month,
+                                 &current_day);
 
   num_days = _month_days (current_month, current_year);
-
   store_days = GTK_LIST_STORE (selector->priv->day_model);
-  gtk_list_store_clear (store_days);
 
-  for (i = 1; i <= num_days; i++) {
-    tm.tm_mday = i;
-    strftime (label, 255, _("wdgt_va_day_numeric"), &tm);
+  if (num_days == selector->priv->current_num_days) {
+    return GTK_TREE_MODEL (store_days);
+  }
 
-    gtk_list_store_append (store_days, &iter);
-    gtk_list_store_set (store_days, &iter,
-                        COLUMN_STRING, label, COLUMN_INT, i, -1);
+  if (num_days > selector->priv->current_num_days) {
+    for (i = selector->priv->current_num_days + 1; i <= num_days; i++) {
+      tm.tm_mday = i;
+      strftime (label, 255, _("wdgt_va_day_numeric"), &tm);
+
+      gtk_list_store_append (store_days, &iter);
+      gtk_list_store_set (store_days, &iter,
+                          COLUMN_STRING, label, COLUMN_INT, i, -1);
+    }
+  } else {
+    path = gtk_tree_path_new_from_indices (num_days,
+                                           -1);
+    gtk_tree_model_get_iter (GTK_TREE_MODEL (store_days), &iter, path);
+    do {
+    }while (gtk_list_store_remove (store_days, &iter));
+
+    gtk_tree_path_free (path);
   }
 
+
+  selector->priv->current_num_days = num_days;
+
   /* now we select a day */
   if (current_day >= num_days) {
     current_day = num_days;
@@ -554,6 +573,8 @@ _manage_selector_change_cb (HildonTouchSelector * touch_selector,
   if ((num_column == selector->priv->month_column) ||
       (num_column == selector->priv->year_column)) /* it is required to check that with
                                                     * the years too,remember: leap years
+                                                    * update_day_model will check if
+                                                    * the number of days is different
                                                     */
   {
     _update_day_model (selector);
@@ -582,6 +603,8 @@ _month_days (gint month, gint year)
  * Creates a new #HildonDateSelector
  *
  * Returns: a new #HildonDateSelector
+ *
+ * Since: 2.2
  **/
 GtkWidget *
 hildon_date_selector_new ()
@@ -591,7 +614,7 @@ hildon_date_selector_new ()
 
 
 /**
- * hildon_date_selector_select_date:
+ * hildon_date_selector_select_current_date:
  * @selector: the #HildonDateSelector
  * @year:  the current year
  * @month: the current month (0-11)
@@ -599,6 +622,9 @@ hildon_date_selector_new ()
  *
  * Sets the current active date on the #HildonDateSelector widget
  *
+ * Since: 2.2
+ *
+ * Returns: %TRUE on success, %FALSE otherwise
  **/
 gboolean
 hildon_date_selector_select_current_date (HildonDateSelector * selector,
@@ -651,7 +677,7 @@ hildon_date_selector_select_current_date (HildonDateSelector * selector,
  *
  * Gets the current active date on the #HildonDateSelector widget
  *
- *
+ * Since: 2.2
  **/
 void
 hildon_date_selector_get_date (HildonDateSelector * selector,
@@ -695,10 +721,12 @@ hildon_date_selector_get_date (HildonDateSelector * selector,
  *
  * Modify the current month and year on the current active date
  *
- * Utility function, too keep this API more similar to the previously existing
- * hildon-calendar widget.
+ * Utility function to keep this API similar to the previously
+ * existing #HildonCalendar widget.
  *
+ * Since: 2.2
  *
+ * Returns: %TRUE on success, %FALSE otherwise
  **/
 gboolean hildon_date_selector_select_month (HildonDateSelector *selector,
                                             guint month, guint year)
@@ -717,10 +745,10 @@ gboolean hildon_date_selector_select_month (HildonDateSelector *selector,
  *
  * Modify the current day on the current active date
  *
- * Utility function, too keep this API more similar to the previously existing
- * hildon-calendar widget.
- *
+ * Utility function to keep this API similar to the previously
+ * existing #HildonCalendar widget.
  *
+ * Since: 2.2
  **/
 void
 hildon_date_selector_select_day (HildonDateSelector *selector, guint day)