2 * This file is a part of hildon
4 * Copyright (C) 2005, 2008 Nokia Corporation.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version. or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * SECTION:hildon-date-selector
23 * @short_description: A widget to select the current date.
25 * #HildonDateSelector is a date widget with multiple columns. Users
26 * can choose a date by selecting values in the day, month and year
29 * The currently selected month and year can be altered with
30 * hildon_date_selector_select_month(). The day can be selected from
31 * the active month using hildon_date_selector_select_day().
34 #define _GNU_SOURCE /* needed for GNU nl_langinfo_l */
35 #define __USE_GNU /* needed for locale */
39 #ifdef HAVE_SYS_TIME_H
50 #include "hildon-date-selector.h"
52 #define HILDON_DATE_SELECTOR_GET_PRIVATE(obj) \
53 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), HILDON_TYPE_DATE_SELECTOR, HildonDateSelectorPrivate))
55 G_DEFINE_TYPE (HildonDateSelector, hildon_date_selector, HILDON_TYPE_TOUCH_SELECTOR)
58 #define LAST_YEAR 50 /* since current year */
60 #define _(String) dgettext("hildon-libs", String)
62 /* #define _(String) "%A %e. %B %Y" debug purposes */
78 struct _HildonDateSelectorPrivate
80 GtkTreeModel *year_model;
81 GtkTreeModel *month_model;
82 GtkTreeModel *day_model;
87 gint year_column; /* it depends on the locale */
89 gchar *format; /* day/month/year format, depends on locale */
93 gint creation_year; /* date at creation time */
95 gint current_num_days;
106 static GObject * hildon_date_selector_constructor (GType type,
107 guint n_construct_properties,
108 GObjectConstructParam *construct_properties);
109 static void hildon_date_selector_finalize (GObject * object);
111 /* private functions */
112 static GtkTreeModel *_create_day_model (HildonDateSelector * selector);
113 static GtkTreeModel *_create_year_model (HildonDateSelector * selector);
114 static GtkTreeModel *_create_month_model (HildonDateSelector * selector);
116 static void _get_real_date (gint * year, gint * month, gint * day);
117 static void _locales_init (HildonDateSelectorPrivate * priv);
119 static void _manage_selector_change_cb (HildonTouchSelector * selector,
120 gint num_column, gpointer data);
122 static GtkTreeModel *_update_day_model (HildonDateSelector * selector);
124 static gint _month_days (gint month, gint year);
125 static void _init_column_order (HildonDateSelector * selector);
127 static gchar *_custom_print_func (HildonTouchSelector * selector,
130 /***************************************************************************/
131 /* The following date routines are taken from the lib_date package. Keep
132 * them separate in case we want to update them if a newer lib_date comes
135 typedef unsigned int N_int;
137 typedef unsigned long N_long;
139 typedef signed long Z_long;
142 { false = FALSE, true = TRUE } boolean;
144 #define and && /* logical (boolean) operators: lower case */
148 static const N_int month_length[2][13] = {
149 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
150 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
153 static const N_int days_in_months[2][14] = {
154 {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
155 {0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
158 static Z_long _calc_days (N_int year, N_int mm, N_int dd);
160 static N_int _day_of_week (N_int year, N_int mm, N_int dd);
162 static boolean _leap (N_int year);
168 return ((((year % 4) == 0) and ((year % 100) != 0)) or ((year % 400) == 0));
172 _day_of_week (N_int year, N_int mm, N_int dd)
176 days = _calc_days (year, mm, dd);
182 return ((N_int) days);
186 _year_to_days (N_int year)
188 return (year * 365L + (year / 4) - (year / 100) + (year / 400));
192 _calc_days (N_int year, N_int mm, N_int dd)
198 if ((mm < 1) or (mm > 12))
200 if ((dd < 1) or (dd > month_length[(lp = _leap (year))][mm]))
202 return (_year_to_days (--year) + days_in_months[lp][mm] + dd);
206 hildon_date_selector_set_property (GObject *object,
211 HildonDateSelectorPrivate *priv = HILDON_DATE_SELECTOR (object)->priv;
216 priv->min_year = g_value_get_int (value);
219 priv->max_year = g_value_get_int (value);
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227 hildon_date_selector_get_property (GObject *object,
232 HildonDateSelectorPrivate *priv = HILDON_DATE_SELECTOR (object)->priv;
237 g_value_set_int (value, priv->min_year);
240 g_value_set_int (value, priv->max_year);
243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
248 hildon_date_selector_class_init (HildonDateSelectorClass * class)
250 GObjectClass *gobject_class;
251 GtkObjectClass *object_class;
252 GtkWidgetClass *widget_class;
253 GtkContainerClass *container_class;
255 gobject_class = (GObjectClass *) class;
256 object_class = (GtkObjectClass *) class;
257 widget_class = (GtkWidgetClass *) class;
258 container_class = (GtkContainerClass *) class;
261 gobject_class->finalize = hildon_date_selector_finalize;
262 gobject_class->get_property = hildon_date_selector_get_property;
263 gobject_class->set_property = hildon_date_selector_set_property;
264 gobject_class->constructor = hildon_date_selector_constructor;
272 g_object_class_install_property (
278 "The minimum available year in the selector",
282 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
284 g_object_class_install_property (
290 "The maximum available year in the selector",
294 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
298 g_type_class_add_private (object_class, sizeof (HildonDateSelectorPrivate));
302 hildon_date_selector_construct_ui (HildonDateSelector *selector)
305 gint current_item = 0;
306 HildonTouchSelectorColumn *column = NULL;
308 selector->priv->year_model = _create_year_model (selector);
309 selector->priv->month_model = _create_month_model (selector);
310 selector->priv->day_model = _create_day_model (selector);
312 /* We add the columns, checking the locale order */
313 iter = selector->priv->column_order;
314 for (iter = selector->priv->column_order; iter; iter = g_slist_next (iter)) {
315 current_item = GPOINTER_TO_INT (iter->data);
317 switch (current_item) {
319 column = hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
320 selector->priv->day_model, TRUE);
321 hildon_touch_selector_column_set_text_column (column, 0);
324 column = hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
325 selector->priv->month_model, TRUE);
326 hildon_touch_selector_column_set_text_column (column, 0);
329 column = hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
330 selector->priv->year_model, TRUE);
331 hildon_touch_selector_column_set_text_column (column, 0);
334 g_error ("Current column order incorrect");
341 hildon_date_selector_constructor (GType type,
342 guint n_construct_properties,
343 GObjectConstructParam *construct_properties)
346 HildonDateSelector *selector;
348 object = G_OBJECT_CLASS (hildon_date_selector_parent_class)->constructor
349 (type, n_construct_properties, construct_properties);
351 selector = HILDON_DATE_SELECTOR (object);
353 hildon_date_selector_construct_ui (selector);
355 g_signal_connect (object, "changed", G_CALLBACK (_manage_selector_change_cb), NULL);
357 /* By default we should select the current day */
358 hildon_date_selector_select_current_date (selector, selector->priv->creation_year,
359 selector->priv->creation_month,
360 selector->priv->creation_day);
366 hildon_date_selector_init (HildonDateSelector * selector)
368 selector->priv = HILDON_DATE_SELECTOR_GET_PRIVATE (selector);
370 GTK_WIDGET_SET_FLAGS (GTK_WIDGET (selector), GTK_NO_WINDOW);
371 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (selector), FALSE);
373 hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (selector),
376 _locales_init (selector->priv);
378 _init_column_order (selector);
380 _get_real_date (&selector->priv->creation_year,
381 &selector->priv->creation_month, &selector->priv->creation_day);
382 selector->priv->current_num_days = 31;
386 hildon_date_selector_finalize (GObject * object)
388 HildonDateSelector *selector = NULL;
390 selector = HILDON_DATE_SELECTOR (object);
392 g_slist_free (selector->priv->column_order);
393 g_free (selector->priv->format);
395 (*G_OBJECT_CLASS (hildon_date_selector_parent_class)->finalize) (object);
398 /* ------------------------------ PRIVATE METHODS ---------------------------- */
400 _custom_print_func (HildonTouchSelector * touch_selector, gpointer user_data)
402 HildonDateSelector *selector = NULL;
403 gchar *result = NULL;
404 guint year, month, day;
405 gint day_of_week = 0;
406 static gchar string[255];
407 struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
409 selector = HILDON_DATE_SELECTOR (touch_selector);
411 hildon_date_selector_get_date (selector, &year, &month, &day);
412 day_of_week = _day_of_week (year, month + 1, day) % 7;
416 tm.tm_year = year - 1900;
417 tm.tm_wday = day_of_week;
419 strftime (string, 255, _("wdgt_va_date_long"), &tm);
421 result = g_strdup (string);
426 /* This was copied from hildon-calendar */
428 _locales_init (HildonDateSelectorPrivate * priv)
430 /* Hildon: This is not exactly portable, see
431 * http://bugzilla.gnome.org/show_bug.cgi?id=343415
432 * The labels need to be instance variables as the startup wizard changes
437 l = newlocale (LC_TIME_MASK, setlocale (LC_MESSAGES, NULL), NULL);
439 priv->format = g_locale_to_utf8 (nl_langinfo_l (D_FMT, l),
440 -1, NULL, NULL, NULL);
446 _init_column_order (HildonDateSelector * selector)
448 gchar *current_order[3] = { NULL, NULL, NULL };
449 gchar *day_pos = NULL;
450 gchar *month_pos = NULL;
451 gchar *year_pos = NULL;
455 g_debug ("Current format: %s", selector->priv->format);
457 /* search each token on the format */
458 day_pos = g_strrstr (selector->priv->format, "%d");
460 month_pos = g_strrstr (selector->priv->format, "%m");
461 year_pos = g_strrstr (selector->priv->format, "%y");
462 if (year_pos == NULL) {
463 year_pos = g_strrstr (selector->priv->format, "%Y");
467 if ((day_pos == NULL) || (month_pos == NULL) || (year_pos == NULL)) {
468 g_error ("Wrong date format"); /* so default values */
470 selector->priv->day_column = 0;
471 selector->priv->month_column = 1;
472 selector->priv->year_column = 2;
473 selector->priv->column_order = g_slist_append (NULL, GINT_TO_POINTER (DAY));
474 selector->priv->column_order =
475 g_slist_append (selector->priv->column_order, GINT_TO_POINTER (MONTH));
476 selector->priv->column_order =
477 g_slist_append (selector->priv->column_order, GINT_TO_POINTER (YEAR));
480 /* sort current_order with this values (bubble sort) */
481 current_order[0] = day_pos;
482 current_order[1] = month_pos;
483 current_order[2] = year_pos;
485 for (c = 1; c <= 2; c++) {
486 for (i = 0; i < 3 - c; i++) {
487 if (current_order[i] > current_order[i + 1]) {
488 aux = current_order[i];
489 current_order[i] = current_order[i + 1];
490 current_order[i + 1] = aux;
495 /* fill the column positions */
496 selector->priv->column_order = NULL;
498 for (i = 0; i < 3; i++) {
499 if (current_order[i] == day_pos) {
500 selector->priv->column_order =
501 g_slist_append (selector->priv->column_order, GINT_TO_POINTER (DAY));
502 selector->priv->day_column = c++;
504 if (current_order[i] == month_pos) {
505 selector->priv->column_order =
506 g_slist_append (selector->priv->column_order, GINT_TO_POINTER (MONTH));
507 selector->priv->month_column = c++;
509 if (current_order[i] == year_pos) {
510 selector->priv->column_order =
511 g_slist_append (selector->priv->column_order, GINT_TO_POINTER (YEAR));
512 selector->priv->year_column = c++;
518 static GtkTreeModel *
519 _create_day_model (HildonDateSelector * selector)
521 GtkListStore *store_days = NULL;
524 struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
527 store_days = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
528 for (i = 1; i < 32; i++) {
530 strftime (label, 255, _("wdgt_va_day_numeric"), &tm);
532 gtk_list_store_append (store_days, &iter);
533 gtk_list_store_set (store_days, &iter,
534 COLUMN_STRING, label, COLUMN_INT, i, -1);
537 return GTK_TREE_MODEL (store_days);
540 static GtkTreeModel *
541 _create_year_model (HildonDateSelector * selector)
543 GtkListStore *store_years = NULL;
546 static gchar label[255];
547 struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
550 real_year = selector->priv->creation_year;
552 store_years = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
553 for (i = selector->priv->min_year; i < selector->priv->max_year + 1; i++) {
554 tm.tm_year = i - 1900;
555 strftime (label, 255, _("wdgt_va_year"), &tm);
557 gtk_list_store_append (store_years, &iter);
558 gtk_list_store_set (store_years, &iter,
559 COLUMN_STRING, label, COLUMN_INT, i, -1);
562 return GTK_TREE_MODEL (store_years);
565 static GtkTreeModel *
566 _create_month_model (HildonDateSelector * selector)
570 GtkListStore *store_months = NULL;
571 static gchar label[255];
572 struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
574 store_months = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
575 for (i = 0; i < 12; i++) {
577 strftime (label, 255, _("wdgt_va_month"), &tm);
579 gtk_list_store_append (store_months, &iter);
580 gtk_list_store_set (store_months, &iter, COLUMN_STRING, label,
585 return GTK_TREE_MODEL (store_months);
588 static GtkTreeModel *
589 _update_day_model (HildonDateSelector * selector)
591 GtkListStore *store_days = NULL;
592 GtkTreePath *path = NULL;
595 static gchar label[255];
596 struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
597 guint current_day = 0;
598 guint current_year = 0;
599 guint current_month = 0;
602 hildon_date_selector_get_date (selector, ¤t_year, ¤t_month,
605 num_days = _month_days (current_month, current_year);
606 store_days = GTK_LIST_STORE (selector->priv->day_model);
608 if (num_days == selector->priv->current_num_days) {
609 return GTK_TREE_MODEL (store_days);
612 if (num_days > selector->priv->current_num_days) {
613 for (i = selector->priv->current_num_days + 1; i <= num_days; i++) {
615 strftime (label, 255, _("wdgt_va_day_numeric"), &tm);
617 gtk_list_store_append (store_days, &iter);
618 gtk_list_store_set (store_days, &iter,
619 COLUMN_STRING, label, COLUMN_INT, i, -1);
622 path = gtk_tree_path_new_from_indices (num_days,
624 gtk_tree_model_get_iter (GTK_TREE_MODEL (store_days), &iter, path);
626 }while (gtk_list_store_remove (store_days, &iter));
628 gtk_tree_path_free (path);
632 selector->priv->current_num_days = num_days;
634 /* now we select a day */
635 if (current_day >= num_days) {
636 current_day = num_days;
639 hildon_date_selector_select_day (selector, current_day);
641 return GTK_TREE_MODEL (store_days);
646 _get_real_date (gint * year, gint * month, gint * day)
649 struct tm *tm = NULL;
652 tm = localtime (&secs);
655 *year = 1900 + tm->tm_year;
669 _manage_selector_change_cb (HildonTouchSelector * touch_selector,
670 gint num_column, gpointer data)
672 HildonDateSelector *selector = NULL;
674 g_return_if_fail (HILDON_IS_DATE_SELECTOR (touch_selector));
675 selector = HILDON_DATE_SELECTOR (touch_selector);
677 if ((num_column == selector->priv->month_column) ||
678 (num_column == selector->priv->year_column)) /* it is required to check that with
679 * the years too,remember: leap years
680 * update_day_model will check if
681 * the number of days is different
684 _update_day_model (selector);
689 _month_days (gint month, gint year)
691 gint month_days[2][12] = {
692 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
693 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
696 g_return_val_if_fail (month >= 0 && month < 12, -1);
698 return month_days[_leap (year)][month];
702 /* ------------------------------ PUBLIC METHODS ---------------------------- */
705 * hildon_date_selector_new:
707 * Creates a new #HildonDateSelector
709 * Returns: a new #HildonDateSelector
714 hildon_date_selector_new ()
716 return g_object_new (HILDON_TYPE_DATE_SELECTOR, NULL);
720 * hildon_date_selector_new_with_year_range:
721 * @min_year: the minimum available year or -1 to ignore
722 * @max_year: the maximum available year or -1 to ignore
724 * Creates a new #HildonDateSelector with a specific year range.
725 * If @min_year or @max_year are set to -1, then the default
726 * upper or lower bound will be used, respectively.
728 * Returns: a new #HildonDateSelector
733 hildon_date_selector_new_with_year_range (gint min_year,
738 g_return_val_if_fail (min_year <= max_year, NULL);
740 if (min_year == -1 && min_year == -1) {
741 selector = g_object_new (HILDON_TYPE_DATE_SELECTOR,
743 } else if (min_year == -1) {
744 selector = g_object_new (HILDON_TYPE_DATE_SELECTOR,
745 "max-year", max_year,
747 } else if (max_year == -1) {
748 selector = g_object_new (HILDON_TYPE_DATE_SELECTOR,
749 "min-year", min_year,
752 selector = g_object_new (HILDON_TYPE_DATE_SELECTOR,
753 "min-year", min_year,
754 "max-year", max_year,
761 * hildon_date_selector_select_current_date:
762 * @selector: the #HildonDateSelector
763 * @year: the current year
764 * @month: the current month (between 0 and 11)
765 * @day: the current day (between 1 and 31)
767 * Sets the current active date on the #HildonDateSelector widget
771 * Returns: %TRUE on success, %FALSE otherwise
774 hildon_date_selector_select_current_date (HildonDateSelector * selector,
775 guint year, guint month, guint day)
782 min_year = selector->priv->min_year;
783 max_year = selector->priv->max_year;
785 g_return_val_if_fail (min_year <= year && year <= max_year, FALSE);
786 g_return_val_if_fail (month < 12, FALSE);
788 num_days = _month_days (month, year);
789 g_return_val_if_fail (day > 0 && day <= num_days, FALSE);
792 gtk_tree_model_iter_nth_child (selector->priv->year_model, &iter, NULL,
794 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector),
795 selector->priv->year_column, &iter,
798 gtk_tree_model_iter_nth_child (selector->priv->month_model, &iter, NULL,
800 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector),
801 selector->priv->month_column, &iter,
804 gtk_tree_model_iter_nth_child (selector->priv->day_model, &iter, NULL,
806 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector),
807 selector->priv->day_column, &iter,
815 * hildon_date_selector_get_date:
816 * @selector: the #HildonDateSelector
817 * @year: location of the current year, or NULL
818 * @month: location of the current month (between 0 and 11), or NULL
819 * @day: location of the current day (between 1 and 31), or NULL
821 * Gets the current active date on the #HildonDateSelector widget
826 hildon_date_selector_get_date (HildonDateSelector * selector,
827 guint * year, guint * month, guint * day)
832 if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector),
833 selector->priv->year_column, &iter))
834 gtk_tree_model_get (selector->priv->year_model,
835 &iter, COLUMN_INT, year, -1);
839 if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector),
840 selector->priv->month_column, &iter))
841 gtk_tree_model_get (selector->priv->month_model,
842 &iter, COLUMN_INT, month, -1);
846 if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector),
847 selector->priv->day_column, &iter))
849 gtk_tree_model_get (selector->priv->day_model,
850 &iter, COLUMN_INT, day, -1);
852 /* *day = *day - 1; */
859 * hildon_date_selector_select_month:
860 * @selector: the #HildonDateSelector
861 * @month: the current month (0-11)
862 * @year: the current year
864 * Modify the current month and year on the current active date
866 * Utility function to keep this API similar to the previously
867 * existing #HildonCalendar widget.
871 * Returns: %TRUE on success, %FALSE otherwise
873 gboolean hildon_date_selector_select_month (HildonDateSelector *selector,
874 guint month, guint year)
878 hildon_date_selector_get_date (selector, NULL, NULL, &day);
880 return hildon_date_selector_select_current_date (selector, year, month, day);
884 * hildon_date_selector_select_day:
885 * @selector: the #HildonDateSelector
886 * @day: the current day (between 1 and 31)
888 * Modify the current day on the current active date
890 * Utility function to keep this API similar to the previously
891 * existing #HildonCalendar widget.
896 hildon_date_selector_select_day (HildonDateSelector *selector, guint day)
901 hildon_date_selector_get_date (selector, &year, &month, NULL);
903 hildon_date_selector_select_current_date (selector, year, month, day);