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, equivalent to hildon-calendar, but with a multi-column
30 #define _GNU_SOURCE /* needed for GNU nl_langinfo_l */
31 #define __USE_GNU /* needed for locale */
35 #ifdef HAVE_SYS_TIME_H
46 #include "hildon-date-selector.h"
48 #define HILDON_DATE_SELECTOR_GET_PRIVATE(obj) \
49 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), HILDON_TYPE_DATE_SELECTOR, HildonDateSelectorPrivate))
51 G_DEFINE_TYPE (HildonDateSelector, hildon_date_selector, HILDON_TYPE_TOUCH_SELECTOR)
54 #define LAST_YEAR 50 /* since current year */
56 #define _(String) dgettext("hildon-libs", String)
58 /* #define _(String) "%A %e. %B %Y" debug purposes */
74 struct _HildonDateSelectorPrivate
76 GtkTreeModel *year_model;
77 GtkTreeModel *month_model;
78 GtkTreeModel *day_model;
83 gint year_column; /* it depends on the locale */
85 gchar *format; /* day/month/year format, depends on locale */
89 gint creation_year; /* date at creation time */
92 static void hildon_date_selector_finalize (GObject * object);
94 /* private functions */
95 static GtkTreeModel *_create_day_model (HildonDateSelector * selector);
96 static GtkTreeModel *_create_year_model (HildonDateSelector * selector);
97 static GtkTreeModel *_create_month_model (HildonDateSelector * selector);
99 static void _get_real_date (gint * year, gint * month, gint * day);
100 static void _locales_init (HildonDateSelectorPrivate * priv);
102 static void _manage_selector_change_cb (HildonTouchSelector * selector,
103 gint num_column, gpointer data);
105 static GtkTreeModel *_update_day_model (HildonDateSelector * selector);
107 static gint _month_days (gint month, gint year);
108 static void _init_column_order (HildonDateSelector * selector);
110 static gchar *_custom_print_func (HildonTouchSelector * selector);
112 /***************************************************************************/
113 /* The following date routines are taken from the lib_date package. Keep
114 * them separate in case we want to update them if a newer lib_date comes
117 typedef unsigned int N_int;
119 typedef unsigned long N_long;
121 typedef signed long Z_long;
124 { false = FALSE, true = TRUE } boolean;
126 #define and && /* logical (boolean) operators: lower case */
130 static const N_int month_length[2][13] = {
131 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
132 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
135 static const N_int days_in_months[2][14] = {
136 {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
137 {0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
140 static Z_long _calc_days (N_int year, N_int mm, N_int dd);
142 static N_int _day_of_week (N_int year, N_int mm, N_int dd);
144 static boolean _leap (N_int year);
150 return ((((year % 4) == 0) and ((year % 100) != 0)) or ((year % 400) == 0));
154 _day_of_week (N_int year, N_int mm, N_int dd)
158 days = _calc_days (year, mm, dd);
164 return ((N_int) days);
168 _year_to_days (N_int year)
170 return (year * 365L + (year / 4) - (year / 100) + (year / 400));
174 _calc_days (N_int year, N_int mm, N_int dd)
180 if ((mm < 1) or (mm > 12))
182 if ((dd < 1) or (dd > month_length[(lp = _leap (year))][mm]))
184 return (_year_to_days (--year) + days_in_months[lp][mm] + dd);
188 hildon_date_selector_class_init (HildonDateSelectorClass * class)
190 GObjectClass *gobject_class;
191 GtkObjectClass *object_class;
192 GtkWidgetClass *widget_class;
193 GtkContainerClass *container_class;
195 gobject_class = (GObjectClass *) class;
196 object_class = (GtkObjectClass *) class;
197 widget_class = (GtkWidgetClass *) class;
198 container_class = (GtkContainerClass *) class;
201 gobject_class->finalize = hildon_date_selector_finalize;
209 g_type_class_add_private (object_class, sizeof (HildonDateSelectorPrivate));
213 hildon_date_selector_init (HildonDateSelector * selector)
216 gint current_item = 0;
218 selector->priv = HILDON_DATE_SELECTOR_GET_PRIVATE (selector);
220 GTK_WIDGET_SET_FLAGS (GTK_WIDGET (selector), GTK_NO_WINDOW);
221 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (selector), FALSE);
223 hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (selector),
226 _locales_init (selector->priv);
228 _init_column_order (selector);
230 _get_real_date (&selector->priv->creation_year,
231 &selector->priv->creation_month, &selector->priv->creation_day);
233 selector->priv->year_model = _create_year_model (selector);
234 selector->priv->month_model = _create_month_model (selector);
235 selector->priv->day_model = _create_day_model (selector);
237 /* We add the columns: FIXME: check the locale order */
238 iter = selector->priv->column_order;
239 for (iter = selector->priv->column_order; iter; iter = g_slist_next (iter)) {
240 current_item = GPOINTER_TO_INT (iter->data);
242 switch (current_item) {
244 hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
245 selector->priv->day_model, TRUE);
248 hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
249 selector->priv->month_model, TRUE);
252 hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
253 selector->priv->year_model, TRUE);
256 g_error ("Current column order incorrect");
261 g_signal_connect (G_OBJECT (selector),
262 "changed", G_CALLBACK (_manage_selector_change_cb), NULL);
264 /* By default we should select the current day */
265 hildon_date_selector_select_current_date (selector, selector->priv->creation_year,
266 selector->priv->creation_month,
267 selector->priv->creation_day);
271 hildon_date_selector_finalize (GObject * object)
273 HildonDateSelector *selector = NULL;
275 selector = HILDON_DATE_SELECTOR (object);
277 g_slist_free (selector->priv->column_order);
279 (*G_OBJECT_CLASS (hildon_date_selector_parent_class)->finalize) (object);
282 /* ------------------------------ PRIVATE METHODS ---------------------------- */
284 _custom_print_func (HildonTouchSelector * touch_selector)
286 HildonDateSelector *selector = NULL;
287 gchar *result = NULL;
288 guint year, month, day;
289 gint day_of_week = 0;
290 static gchar string[255];
291 struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
293 selector = HILDON_DATE_SELECTOR (touch_selector);
295 hildon_date_selector_get_date (selector, &year, &month, &day);
296 day_of_week = _day_of_week (year, month + 1, day) % 7;
300 tm.tm_year = year - 1900;
301 tm.tm_wday = day_of_week;
303 strftime (string, 255, _("wdgt_va_date_long"), &tm);
305 result = g_strdup (string);
310 /* This was copied from hildon-calendar */
312 _locales_init (HildonDateSelectorPrivate * priv)
314 /* Hildon: This is not exactly portable, see
315 * http://bugzilla.gnome.org/show_bug.cgi?id=343415
316 * The labels need to be instance variables as the startup wizard changes
321 l = newlocale (LC_TIME_MASK, setlocale (LC_MESSAGES, NULL), NULL);
323 priv->format = g_locale_to_utf8 (nl_langinfo_l (D_FMT, l),
324 -1, NULL, NULL, NULL);
330 _init_column_order (HildonDateSelector * selector)
332 gchar *current_order[3] = { NULL, NULL, NULL };
333 gchar *day_pos = NULL;
334 gchar *month_pos = NULL;
335 gchar *year_pos = NULL;
339 g_debug ("Current format: %s", selector->priv->format);
341 /* search each token on the format */
342 day_pos = g_strrstr (selector->priv->format, "%d");
344 month_pos = g_strrstr (selector->priv->format, "%m");
345 year_pos = g_strrstr (selector->priv->format, "%y");
346 if (year_pos == NULL) {
347 year_pos = g_strrstr (selector->priv->format, "%Y");
351 if ((day_pos == NULL) || (month_pos == NULL) || (year_pos == NULL)) {
352 g_error ("Wrong date format"); /* so default values */
354 selector->priv->day_column = 0;
355 selector->priv->month_column = 1;
356 selector->priv->year_column = 2;
357 selector->priv->column_order = g_slist_append (NULL, GINT_TO_POINTER (DAY));
358 selector->priv->column_order =
359 g_slist_append (selector->priv->column_order, GINT_TO_POINTER (MONTH));
360 selector->priv->column_order =
361 g_slist_append (selector->priv->column_order, GINT_TO_POINTER (YEAR));
364 /* sort current_order with this values (bubble sort) */
365 current_order[0] = day_pos;
366 current_order[1] = month_pos;
367 current_order[2] = year_pos;
369 for (c = 1; c <= 2; c++) {
370 for (i = 0; i < 3 - c; i++) {
371 if (current_order[i] > current_order[i + 1]) {
372 aux = current_order[i];
373 current_order[i] = current_order[i + 1];
374 current_order[i + 1] = aux;
379 /* fill the column positions */
380 selector->priv->column_order = NULL;
382 for (i = 0; i < 3; i++) {
383 if (current_order[i] == day_pos) {
384 selector->priv->column_order =
385 g_slist_append (selector->priv->column_order, GINT_TO_POINTER (DAY));
386 selector->priv->day_column = c++;
388 if (current_order[i] == month_pos) {
389 selector->priv->column_order =
390 g_slist_append (selector->priv->column_order, GINT_TO_POINTER (MONTH));
391 selector->priv->month_column = c++;
393 if (current_order[i] == year_pos) {
394 selector->priv->column_order =
395 g_slist_append (selector->priv->column_order, GINT_TO_POINTER (YEAR));
396 selector->priv->year_column = c++;
402 static GtkTreeModel *
403 _create_day_model (HildonDateSelector * selector)
405 GtkListStore *store_days = NULL;
408 struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
411 store_days = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
412 for (i = 1; i < 32; i++) {
414 strftime (label, 255, _("wdgt_va_day_numeric"), &tm);
416 gtk_list_store_append (store_days, &iter);
417 gtk_list_store_set (store_days, &iter,
418 COLUMN_STRING, label, COLUMN_INT, i, -1);
421 return GTK_TREE_MODEL (store_days);
424 static GtkTreeModel *
425 _create_year_model (HildonDateSelector * selector)
427 GtkListStore *store_years = NULL;
430 static gchar label[255];
431 struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
434 real_year = selector->priv->creation_year;
436 store_years = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
437 for (i = real_year - INIT_YEAR; i < real_year + LAST_YEAR; i++) {
438 tm.tm_year = i - 1900;
439 strftime (label, 255, _("wdgt_va_year"), &tm);
441 gtk_list_store_append (store_years, &iter);
442 gtk_list_store_set (store_years, &iter,
443 COLUMN_STRING, label, COLUMN_INT, i, -1);
446 return GTK_TREE_MODEL (store_years);
449 static GtkTreeModel *
450 _create_month_model (HildonDateSelector * selector)
454 GtkListStore *store_months = NULL;
455 static gchar label[255];
456 struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
458 store_months = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
459 for (i = 0; i < 12; i++) {
461 strftime (label, 255, _("wdgt_va_month"), &tm);
463 gtk_list_store_append (store_months, &iter);
464 gtk_list_store_set (store_months, &iter, COLUMN_STRING, label,
469 return GTK_TREE_MODEL (store_months);
472 static GtkTreeModel *
473 _update_day_model (HildonDateSelector * selector)
475 GtkListStore *store_days = NULL;
478 static gchar label[255];
479 struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
480 guint current_day = 0;
481 guint current_year = 0;
482 guint current_month = 0;
485 hildon_date_selector_get_date (selector, NULL, NULL, ¤t_day);
487 hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector),
488 selector->priv->month_column, &iter);
489 gtk_tree_model_get (selector->priv->month_model,
490 &iter, COLUMN_INT, ¤t_month, -1);
492 hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector),
493 selector->priv->year_column, &iter);
494 gtk_tree_model_get (selector->priv->year_model,
495 &iter, COLUMN_INT, ¤t_year, -1);
497 num_days = _month_days (current_month, current_year);
499 store_days = GTK_LIST_STORE (selector->priv->day_model);
500 gtk_list_store_clear (store_days);
502 for (i = 1; i <= num_days; i++) {
504 strftime (label, 255, _("wdgt_va_day_numeric"), &tm);
506 gtk_list_store_append (store_days, &iter);
507 gtk_list_store_set (store_days, &iter,
508 COLUMN_STRING, label, COLUMN_INT, i, -1);
511 /* now we select a day */
512 if (current_day >= num_days) {
513 current_day = num_days;
516 hildon_date_selector_select_day (selector, current_day);
518 return GTK_TREE_MODEL (store_days);
523 _get_real_date (gint * year, gint * month, gint * day)
526 struct tm *tm = NULL;
529 tm = localtime (&secs);
532 *year = 1900 + tm->tm_year;
546 _manage_selector_change_cb (HildonTouchSelector * touch_selector,
547 gint num_column, gpointer data)
549 HildonDateSelector *selector = NULL;
551 g_return_if_fail (HILDON_IS_DATE_SELECTOR (touch_selector));
552 selector = HILDON_DATE_SELECTOR (touch_selector);
554 if ((num_column == selector->priv->month_column) ||
555 (num_column == selector->priv->year_column)) /* it is required to check that with
556 * the years too,remember: leap years
559 _update_day_model (selector);
564 _month_days (gint month, gint year)
566 gint month_days[2][12] = {
567 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
568 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
571 g_return_val_if_fail (month >= 0 && month <= 12, -1);
573 return month_days[_leap (year)][month];
577 /* ------------------------------ PUBLIC METHODS ---------------------------- */
580 * hildon_date_selector_new:
582 * Creates a new #HildonDateSelector
584 * Returns: a new #HildonDateSelector
587 hildon_date_selector_new ()
589 return g_object_new (HILDON_TYPE_DATE_SELECTOR, NULL);
594 * hildon_date_selector_select_date:
595 * @selector: the #HildonDateSelector
596 * @year: the current year
597 * @month: the current month (0-11)
598 * @day: the current day (1-31, 1-30, 1-29, 1-28) depends on the month
600 * Sets the current active date on the #HildonDateSelector widget
604 hildon_date_selector_select_current_date (HildonDateSelector * selector,
605 guint year, guint month, guint day)
612 min_year = selector->priv->creation_year - INIT_YEAR;
613 max_year = selector->priv->creation_year + LAST_YEAR;
615 g_return_val_if_fail (year > min_year && year < max_year, FALSE);
616 g_return_val_if_fail (month >= 0 && month < 12, FALSE);
618 num_days = _month_days (month, year);
619 g_return_val_if_fail (day > 0 && day <= num_days, FALSE);
622 gtk_tree_model_iter_nth_child (selector->priv->year_model, &iter, NULL,
623 year - selector->priv->creation_year +
625 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector),
626 selector->priv->year_column, &iter,
629 gtk_tree_model_iter_nth_child (selector->priv->month_model, &iter, NULL,
631 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector),
632 selector->priv->month_column, &iter,
635 gtk_tree_model_iter_nth_child (selector->priv->day_model, &iter, NULL,
637 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector),
638 selector->priv->day_column, &iter,
646 * hildon_date_selector_get_date:
647 * @selector: the #HildonDateSelector
648 * @year: to set the current year
649 * @month: to set the current month (0-11)
650 * @day: to the current day (1-31, 1-30, 1-29, 1-28) depends on the month
652 * Gets the current active date on the #HildonDateSelector widget
657 hildon_date_selector_get_date (HildonDateSelector * selector,
658 guint * year, guint * month, guint * day)
663 hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector),
664 selector->priv->year_column, &iter);
665 gtk_tree_model_get (selector->priv->year_model,
666 &iter, COLUMN_INT, year, -1);
670 hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector),
671 selector->priv->month_column, &iter);
672 gtk_tree_model_get (selector->priv->month_model,
673 &iter, COLUMN_INT, month, -1);
678 if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector),
679 selector->priv->day_column, &iter))
681 gtk_tree_model_get (selector->priv->day_model,
682 &iter, COLUMN_INT, day, -1);
684 /* *day = *day - 1; */
691 * hildon_date_selector_select_month:
692 * @selector: the #HildonDateSelector
693 * @month: the current month (0-11)
694 * @year: the current year
696 * Modify the current month and year on the current active date
698 * Utility function, too keep this API more similar to the previously existing
699 * hildon-calendar widget.
703 gboolean hildon_date_selector_select_month (HildonDateSelector *selector,
704 guint month, guint year)
708 hildon_date_selector_get_date (selector, NULL, NULL, &day);
710 return hildon_date_selector_select_current_date (selector, year, month, day);
714 * hildon_date_selector_select_day:
715 * @selector: the #HildonDateSelector
716 * @day: the current day (1-31, 1-30, 1-29, 1-28) depends on the month
718 * Modify the current day on the current active date
720 * Utility function, too keep this API more similar to the previously existing
721 * hildon-calendar widget.
726 hildon_date_selector_select_day (HildonDateSelector *selector, guint day)
731 hildon_date_selector_get_date (selector, &year, &month, NULL);
733 hildon_date_selector_select_current_date (selector, year, month, day);