2 * $Id: calendar.c,v 1.97 2018/06/19 22:57:01 tom Exp $
4 * calendar.c -- implements the calendar box
6 * Copyright 2001-2017,2018 Thomas E. Dickey
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
35 #define ONE_DAY (60 * 60 * 24)
37 #define MON_WIDE 4 /* width of a month-name */
38 #define DAY_HIGH 6 /* maximum lines in day-grid */
39 #define DAY_WIDE (8 * MON_WIDE) /* width of the day-grid */
40 #define HDR_HIGH 1 /* height of cells with month/year */
41 #define BTN_HIGH 1 /* height of button-row excluding margin */
43 /* two more lines: titles for day-of-week and month/year boxes */
44 #define MIN_HIGH (DAY_HIGH + 2 + HDR_HIGH + BTN_HIGH + (MAX_DAYS * MARGIN))
45 #define MIN_WIDE (DAY_WIDE + (4 * MARGIN))
55 typedef int (*BOX_DRAW) (struct _box *, struct tm *);
71 static char *cached_days[MAX_DAYS];
72 static char *cached_months[MAX_MONTHS];
75 nameOfDayOfWeek(int n)
77 static bool shown[MAX_DAYS];
78 static const char *posix_days[MAX_DAYS] =
94 if (cached_days[n] == 0) {
95 const nl_item items[MAX_DAYS] =
97 ABDAY_1, ABDAY_2, ABDAY_3, ABDAY_4, ABDAY_5, ABDAY_6, ABDAY_7
99 cached_days[n] = dlg_strclone(nl_langinfo(items[n]));
100 memset(shown, 0, sizeof(shown));
103 if (cached_days[n] == 0) {
104 size_t len, limit = MON_WIDE - 1;
105 char *value = dlg_strclone(posix_days[n]);
108 * POSIX does not actually say what the length of an abbreviated name
109 * is. Typically it is 2, which will fit into our layout. That also
110 * happens to work with CJK entries as seen in glibc, which are a
111 * double-width cell. For now (2016/01/26), handle too-long names only
114 if ((len = strlen(value)) > limit)
116 cached_days[n] = value;
119 DLG_TRACE(("# DAY(%d) = '%s'\n", n, cached_days[n]));
122 return cached_days[n];
128 static bool shown[MAX_MONTHS];
129 static const char *posix_mons[MAX_MONTHS] =
150 if (cached_months[n] == 0) {
151 const nl_item items[MAX_MONTHS] =
153 MON_1, MON_2, MON_3, MON_4, MON_5, MON_6,
154 MON_7, MON_8, MON_9, MON_10, MON_11, MON_12
156 cached_months[n] = dlg_strclone(nl_langinfo(items[n]));
157 memset(shown, 0, sizeof(shown));
160 if (cached_months[n] == 0) {
161 cached_months[n] = dlg_strclone(posix_mons[n]);
164 DLG_TRACE(("# MON(%d) = '%s'\n", n, cached_months[n]));
167 return cached_months[n];
171 * Algorithm for Gregorian calendar.
176 return ((y % 4 == 0) &&
178 (y % 400 == 0))) ? 1 : 0;
182 adjust_year_month(int *year, int *month)
185 *month += MAX_MONTHS;
188 while (*month >= MAX_MONTHS) {
189 *month -= MAX_MONTHS;
195 days_per_month(int year, int month)
197 static const int nominal[] =
199 31, 28, 31, 30, 31, 30,
200 31, 31, 30, 31, 30, 31
204 adjust_year_month(&year, &month);
205 result = nominal[month];
207 result += isleap(year);
212 days_in_month(struct tm *current, int offset /* -1, 0, 1 */ )
214 int year = current->tm_year + 1900;
215 int month = current->tm_mon + offset;
217 adjust_year_month(&year, &month);
218 return days_per_month(year, month);
222 days_per_year(int year)
224 return (isleap(year) ? 366 : 365);
228 days_in_year(struct tm *current, int offset /* -1, 0, 1 */ )
230 return days_per_year(current->tm_year + 1900 + offset);
235 * "17.28: How can I find the day of the week given the date?"
236 * implementation by Tomohiko Sakamoto.
238 * d = day (0 to whatever)
239 * m = month (1 through 12)
240 * y = year (1752 and later, for Gregorian calendar)
243 day_of_week(int y, int m, int d)
247 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4
250 return (6 + (y + (y / 4) - (y / 100) + (y / 400) + t[m - 1] + d)) % MAX_DAYS;
254 day_in_year(int year, int month, int day)
258 result += days_per_month(year, month);
263 iso_week(int year, int month, int day)
269 int new_years_eve_dow;
270 static const int thursday = 3;
272 /* add the number weeks *between* date and newyear */
273 diy = day_in_year(year, month, day);
274 week += (diy - 1) / MAX_DAYS;
277 dow = day_of_week(year, month, day);
278 new_year_dow = day_of_week(year, 1, 1);
281 * If New Year falls on Friday, Saturday or Sunday, then New Years's week
282 * is the last week of the preceding year. In that case subtract one week.
284 if (new_year_dow > thursday)
287 /* Add one week if there is a Sunday to Monday transition. */
288 if (dow - new_year_dow < 0)
291 /* Check if we are in the last week of the preceding year. */
293 week = iso_week(--year, 12, 31);
297 * If we are in the same week as New Year's eve, check if New Year's eve is
298 * in the first week of the next year.
300 new_years_eve_dow = (new_year_dow + 364 + isleap(year)) % MAX_DAYS;
301 if (365 + isleap(year) - diy < MAX_DAYS
302 && new_years_eve_dow >= dow
303 && new_years_eve_dow < thursday) {
311 getisoweeks(int year, int month)
313 static int result[10];
316 int dpm = days_per_month(year, month);
318 for (day = 1; day <= dpm; day += MAX_DAYS)
319 result[windx++] = iso_week(year, month, day);
321 * Ensure that there is a week number associated with the last day of the
322 * month, e.g., in case the last day of the month falls before Thursday,
323 * so that we have to show the week-number for the beginning of the
326 result[windx] = iso_week(year, month, dpm);
331 day_cell_number(struct tm *current)
334 cell = current->tm_mday - ((6 + current->tm_mday - current->tm_wday) % MAX_DAYS);
335 if ((current->tm_mday - 1) % MAX_DAYS != current->tm_wday)
343 next_or_previous(int key, int two_d)
349 result = two_d ? -MAX_DAYS : -1;
355 result = two_d ? MAX_DAYS : 1;
357 case DLGK_GRID_RIGHT:
368 * Draw the day-of-month selection box
371 draw_day(BOX * data, struct tm *current)
373 int cell_wide = MON_WIDE;
374 int y, x, this_x = 0;
375 int save_y = 0, save_x = 0;
376 int day = current->tm_mday;
381 int last = days_in_month(current, 0);
382 int prev = days_in_month(current, -1);
384 werase(data->window);
385 dlg_draw_box2(data->parent,
386 data->y - MARGIN, data->x - MARGIN,
387 data->height + (2 * MARGIN), data->width + (2 * MARGIN),
390 menubox_border2_attr);
392 dlg_attrset(data->window, menubox_attr); /* daynames headline */
393 for (x = 0; x < MAX_DAYS; x++) {
394 mvwprintw(data->window,
395 0, (x + 1) * cell_wide, "%*.*s ",
398 nameOfDayOfWeek(x + data->week_start));
401 mday = ((6 + current->tm_mday -
403 data->week_start) % MAX_DAYS) - MAX_DAYS;
404 if (mday <= -MAX_DAYS)
407 if (dialog_vars.iso_week) {
408 weeks = getisoweeks(current->tm_year + 1900, current->tm_mon + 1);
410 /* mday is now in the range -6 to 0. */
411 week = (current->tm_yday + 6 + mday - current->tm_mday) / MAX_DAYS;
414 for (y = 1; mday < last; y++) {
415 dlg_attrset(data->window, menubox_attr); /* weeknumbers headline */
416 mvwprintw(data->window,
420 weeks ? weeks[windx++] : ++week);
421 for (x = 0; x < MAX_DAYS; x++) {
422 this_x = 1 + (x + 1) * cell_wide;
424 if (wmove(data->window, y, this_x) == ERR)
426 dlg_attrset(data->window, item_attr); /* not selected days */
428 dlg_attrset(data->window, item_selected_attr); /* selected day */
434 wprintw(data->window, "%*d", cell_wide - 2, mday);
435 } else if (mday == day) {
436 wprintw(data->window, "%*d", cell_wide - 2, mday - last);
438 } else if (mday == day) {
439 wprintw(data->window, "%*d", cell_wide - 2, mday + prev);
442 wmove(data->window, save_y, save_x);
444 /* just draw arrows - scrollbar is unsuitable here */
445 dlg_draw_arrows(data->parent, TRUE, TRUE,
446 data->x + ARROWS_COL,
448 data->y + data->height);
454 * Draw the month-of-year selection box
457 draw_month(BOX * data, struct tm *current)
461 month = current->tm_mon + 1;
463 dlg_attrset(data->parent, dialog_attr); /* Headline "Month" */
464 (void) mvwprintw(data->parent, data->y - 2, data->x - 1, _("Month"));
465 dlg_draw_box2(data->parent,
466 data->y - 1, data->x - 1,
467 data->height + 2, data->width + 2,
470 menubox_border2_attr);
471 dlg_attrset(data->window, item_attr); /* color the month selection */
472 mvwprintw(data->window, 0, 0, "%s", nameOfMonth(month - 1));
473 wmove(data->window, 0, 0);
478 * Draw the year selection box
481 draw_year(BOX * data, struct tm *current)
483 int year = current->tm_year + 1900;
485 dlg_attrset(data->parent, dialog_attr); /* Headline "Year" */
486 (void) mvwprintw(data->parent, data->y - 2, data->x - 1, _("Year"));
487 dlg_draw_box2(data->parent,
488 data->y - 1, data->x - 1,
489 data->height + 2, data->width + 2,
492 menubox_border2_attr);
493 dlg_attrset(data->window, item_attr); /* color the year selection */
494 mvwprintw(data->window, 0, 0, "%4d", year);
495 wmove(data->window, 0, 0);
500 init_object(BOX * data,
503 int width, int height,
508 data->parent = parent;
512 data->height = height;
513 data->box_draw = box_draw;
514 data->week_start = key_offset;
516 data->window = derwin(data->parent,
517 data->height, data->width,
519 if (data->window == 0)
521 (void) keypad(data->window, TRUE);
523 dlg_mouse_setbase(getbegx(parent), getbegy(parent));
525 dlg_mouse_mkbigregion(y + 1, x + MON_WIDE, height - 1, width - MON_WIDE,
526 KEY_MAX + key_offset, 1, MON_WIDE, 3);
528 dlg_mouse_mkregion(y, x, height, width, code);
534 #if defined(ENABLE_NLS) && defined(HAVE_NL_LANGINFO_1STDAY)
535 #elif defined(HAVE_DLG_GAUGE)
537 read_locale_setting(const char *name, int which)
543 sprintf(command, "locale %s", name);
544 if ((fp = dlg_popen(command, "r")) != 0) {
548 while (fgets(buf, (int) sizeof(buf) - 1, fp) != 0) {
549 if (++count > which) {
551 long check = strtol(buf, &next, 0);
555 result = (int) check;
570 char *option = dialog_vars.week_start;
574 long check = strtol(option, &next, 0);
578 if (!strcmp(option, "locale")) {
579 #if defined(ENABLE_NLS) && defined(HAVE_NL_LANGINFO_1STDAY)
583 int first_day = nl_langinfo(_NL_TIME_FIRST_WEEKDAY)[0];
584 char *basis_ptr = nl_langinfo(_NL_TIME_WEEK_1STDAY);
585 int basis_day = (int) (intptr_t) basis_ptr;
586 #elif defined(HAVE_DLG_GAUGE)
588 * probably Linux-specific, but harmless otherwise. When
589 * available, the locale program will return a single
590 * integer on one line.
592 int first_day = read_locale_setting("first_weekday", 0);
593 int basis_day = read_locale_setting("week-1stday", 0);
595 #if (defined(ENABLE_NLS) && defined(HAVE_NL_LANGINFO_1STDAY)) || defined(HAVE_DLG_GAUGE)
596 int week_1stday = -1;
597 if (basis_day == 19971130)
598 week_1stday = 0; /* Sun */
599 else if (basis_day == 19971201)
600 week_1stday = 1; /* Mon */
601 if (week_1stday >= 0) {
602 result = first_day - week_1stday - 1;
605 result = 0; /* Sun */
609 size_t eql = strlen(option);
610 for (day = 0; day < MAX_DAYS; ++day) {
611 if (!strncmp(nameOfDayOfWeek(day), option, eql)) {
617 } else if (check < 0) {
620 result = (int) (check % MAX_DAYS);
628 CleanupResult(int code, WINDOW *dialog, char *prompt, DIALOG_VARS * save_vars)
633 dlg_del_window(dialog);
634 dlg_mouse_free_regions();
637 dlg_restore_vars(save_vars);
639 for (n = 0; n < MAX_DAYS; ++n) {
640 free(cached_days[n]);
643 for (n = 0; n < MAX_MONTHS; ++n) {
644 free(cached_months[n]);
645 cached_months[n] = 0;
652 trace_date(struct tm *current, struct tm *old)
654 bool changed = (old == 0 ||
655 current->tm_mday != old->tm_mday ||
656 current->tm_mon != old->tm_mon ||
657 current->tm_year != old->tm_year);
659 DLG_TRACE(("# current %04d/%02d/%02d\n",
660 current->tm_year + 1900,
664 DLG_TRACE(("# current (unchanged)\n"));
668 #define DrawObject(data) (data)->box_draw(data, ¤t)
671 * Display a dialog box for entering a date
674 dialog_calendar(const char *title,
675 const char *subtitle,
683 static DLG_KEYS_BINDING binding[] = {
687 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
688 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
689 DLG_KEYS_DATA( DLGK_GRID_DOWN, 'j' ),
690 DLG_KEYS_DATA( DLGK_GRID_DOWN, DLGK_MOUSE(KEY_NPAGE) ),
691 DLG_KEYS_DATA( DLGK_GRID_DOWN, KEY_DOWN ),
692 DLG_KEYS_DATA( DLGK_GRID_DOWN, KEY_NPAGE ),
693 DLG_KEYS_DATA( DLGK_GRID_LEFT, '-' ),
694 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'h' ),
695 DLG_KEYS_DATA( DLGK_GRID_LEFT, CHR_BACKSPACE ),
696 DLG_KEYS_DATA( DLGK_GRID_LEFT, CHR_PREVIOUS ),
697 DLG_KEYS_DATA( DLGK_GRID_LEFT, KEY_LEFT ),
698 DLG_KEYS_DATA( DLGK_GRID_RIGHT, '+' ),
699 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
700 DLG_KEYS_DATA( DLGK_GRID_RIGHT, CHR_NEXT ),
701 DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_NEXT ),
702 DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
703 DLG_KEYS_DATA( DLGK_GRID_UP, 'k' ),
704 DLG_KEYS_DATA( DLGK_GRID_UP, KEY_PPAGE ),
705 DLG_KEYS_DATA( DLGK_GRID_UP, KEY_PREVIOUS ),
706 DLG_KEYS_DATA( DLGK_GRID_UP, KEY_UP ),
707 DLG_KEYS_DATA( DLGK_GRID_UP, DLGK_MOUSE(KEY_PPAGE) ),
713 int old_height = height;
714 int old_width = width;
716 BOX dy_box, mn_box, yr_box;
722 int result = DLG_EXIT_UNKNOWN;
725 time_t now_time = time((time_t *) 0);
727 int state = dlg_default_button();
728 const char **buttons = dlg_ok_labels();
730 int mincols = MIN_WIDE;
731 char buffer[MAX_LEN];
732 DIALOG_VARS save_vars;
734 DLG_TRACE(("# calendar args:\n"));
735 DLG_TRACE2S("title", title);
736 DLG_TRACE2S("message", subtitle);
737 DLG_TRACE2N("height", height);
738 DLG_TRACE2N("width", width);
739 DLG_TRACE2N("day", day);
740 DLG_TRACE2N("month", month);
741 DLG_TRACE2N("year", year);
743 dlg_save_vars(&save_vars);
744 dialog_vars.separate_output = TRUE;
749 * Unless overrridden, the current time/date is our starting point.
751 now_time = time((time_t *) 0);
752 current = *localtime(&now_time);
755 current.tm_isdst = -1;
757 current.tm_year = year - 1900;
760 current.tm_mon = month - 1;
762 if (day > 0 && day <= days_per_month(current.tm_year + 1900,
763 current.tm_mon + 1)) {
764 current.tm_mday = day;
766 now_time = mktime(¤t);
769 day = current.tm_mday;
771 month = current.tm_mon + 1;
773 year = current.tm_year + 1900;
775 /* compute a struct tm that matches the day/month/year parameters */
776 if (((year -= 1900) > 0) && (year < 200)) {
777 /* ugly, but I'd like to run this on older machines w/o mktime -TD */
779 if (year > current.tm_year) {
780 now_time += ONE_DAY * days_in_year(¤t, 0);
781 } else if (year < current.tm_year) {
782 now_time -= ONE_DAY * days_in_year(¤t, -1);
783 } else if (month > current.tm_mon + 1) {
784 now_time += ONE_DAY * days_in_month(¤t, 0);
785 } else if (month < current.tm_mon + 1) {
786 now_time -= ONE_DAY * days_in_month(¤t, -1);
787 } else if (day > current.tm_mday) {
789 } else if (day < current.tm_mday) {
794 current = *localtime(&now_time);
799 dlg_button_layout(buttons, &mincols);
805 prompt = dlg_strclone(subtitle);
806 dlg_auto_size(title, prompt, &height, &width, 0, mincols);
808 height += MIN_HIGH - 1;
809 dlg_print_size(height, width);
810 dlg_ctl_size(height, width);
812 dialog = dlg_new_window(height, width,
813 dlg_box_y_ordinate(height),
814 dlg_box_x_ordinate(width));
815 dlg_register_window(dialog, "calendar", binding);
816 dlg_register_buttons(dialog, "calendar", buttons);
819 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
820 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
821 dlg_draw_title(dialog, title);
823 dlg_attrset(dialog, dialog_attr); /* text mainbox */
824 dlg_print_autowrap(dialog, prompt, height, width);
826 /* compute positions of day, month and year boxes */
827 memset(&dy_box, 0, sizeof(dy_box));
828 memset(&mn_box, 0, sizeof(mn_box));
829 memset(&yr_box, 0, sizeof(yr_box));
831 if ((week_start = WeekStart()) < 0 ||
834 (width - DAY_WIDE) / 2,
835 1 + (height - (DAY_HIGH + BTN_HIGH + (5 * MARGIN))),
841 ((dy_box.week_start = WeekStart()) < 0) ||
842 DrawObject(&dy_box) < 0) {
843 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
846 if (init_object(&mn_box,
849 dy_box.y - (HDR_HIGH + 2 * MARGIN),
850 (DAY_WIDE / 2) - MARGIN,
855 || DrawObject(&mn_box) < 0) {
856 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
859 if (init_object(&yr_box,
861 dy_box.x + mn_box.width + 2,
868 || DrawObject(&yr_box) < 0) {
869 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
872 dlg_trace_win(dialog);
873 while (result == DLG_EXIT_UNKNOWN) {
874 BOX *obj = (state == sDAY ? &dy_box
875 : (state == sMONTH ? &mn_box :
876 (state == sYEAR ? &yr_box : 0)));
878 button = (state < 0) ? 0 : state;
879 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
881 dlg_set_focus(dialog, obj->window);
883 key = dlg_mouse_wgetch(dialog, &fkey);
884 if (dlg_result_key(key, fkey, &result))
887 #define Mouse2Key(key) (key - M_EVENT)
888 if (fkey && (key >= DLGK_MOUSE(KEY_MIN) && key <= DLGK_MOUSE(KEY_MAX))) {
889 key = dlg_lookup_key(dialog, Mouse2Key(key), &fkey);
892 if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
895 /* handle function-keys */
897 case DLGK_MOUSE('D'):
900 case DLGK_MOUSE('M'):
903 case DLGK_MOUSE('Y'):
908 result = dlg_enter_buttoncode(button);
910 case DLGK_FIELD_PREV:
911 state = dlg_prev_ok_buttonindex(state, sMONTH);
913 case DLGK_FIELD_NEXT:
914 state = dlg_next_ok_buttonindex(state, sMONTH);
918 dlg_will_resize(dialog);
924 dlg_del_window(dialog);
925 dlg_mouse_free_regions();
932 if (is_DLGK_MOUSE(key)) {
933 if ((key2 = dlg_ok_buttoncode(Mouse2Key(key))) >= 0) {
936 } else if (key >= DLGK_MOUSE(KEY_MAX)) {
941 - DLGK_MOUSE(KEY_MAX)
942 - day_cell_number(¤t));
943 DLG_TRACE(("# mouseclick decoded %d\n", step));
948 step = next_or_previous(key, (obj == &dy_box));
951 struct tm old = current;
953 /* see comment regarding mktime -TD */
954 if (obj == &dy_box) {
955 now_time += ONE_DAY * step;
956 } else if (obj == &mn_box) {
958 now_time += ONE_DAY *
959 days_in_month(¤t, 0);
961 now_time -= ONE_DAY *
962 days_in_month(¤t, -1);
963 } else if (obj == &yr_box) {
966 * days_in_year(¤t, 0));
969 * days_in_year(¤t, -1));
972 current = *localtime(&now_time);
974 trace_date(¤t, &old);
976 && (current.tm_mday != old.tm_mday
977 || current.tm_mon != old.tm_mon
978 || current.tm_year != old.tm_year))
980 if (obj != &mn_box && current.tm_mon != old.tm_mon)
982 if (obj != &yr_box && current.tm_year != old.tm_year)
984 (void) DrawObject(obj);
986 } else if (state >= 0) {
987 if (next_or_previous(key, FALSE) < 0)
988 state = dlg_prev_ok_buttonindex(state, sMONTH);
989 else if (next_or_previous(key, FALSE) > 0)
990 state = dlg_next_ok_buttonindex(state, sMONTH);
997 #define DefaultFormat(dst, src) \
998 sprintf(dst, "%02d/%02d/%0d", \
999 src.tm_mday, src.tm_mon + 1, src.tm_year + 1900)
1000 #ifdef HAVE_STRFTIME
1001 if (dialog_vars.date_format != 0) {
1002 size_t used = strftime(buffer,
1004 dialog_vars.date_format,
1006 if (used == 0 || *buffer == '\0')
1007 DefaultFormat(buffer, current);
1010 DefaultFormat(buffer, current);
1012 dlg_add_result(buffer);
1013 dlg_add_separator();
1014 dlg_add_last_key(-1);
1016 return CleanupResult(result, dialog, prompt, &save_vars);