2 * $Id: calendar.c,v 1.62 2011/06/29 09:47:06 tom Exp $
4 * calendar.c -- implements the calendar box
6 * Copyright 2001-2010,2011 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.
29 #define ONE_DAY (60 * 60 * 24)
31 #define MON_WIDE 4 /* width of a month-name */
32 #define DAY_HIGH 6 /* maximum lines in day-grid */
33 #define DAY_WIDE (8 * MON_WIDE) /* width of the day-grid */
34 #define HDR_HIGH 1 /* height of cells with month/year */
35 #define BTN_HIGH 1 /* height of button-row excluding margin */
37 /* two more lines: titles for day-of-week and month/year boxes */
38 #define MIN_HIGH (DAY_HIGH + 2 + HDR_HIGH + BTN_HIGH + (7 * MARGIN))
39 #define MIN_WIDE (DAY_WIDE + (4 * MARGIN))
49 typedef int (*BOX_DRAW) (struct _box *, struct tm *);
62 nameOfDayOfWeek(int n)
64 static const char *table[7]
78 const char *result = 0;
80 if (n >= 0 && n < 7) {
85 ABDAY_1, ABDAY_2, ABDAY_3, ABDAY_4, ABDAY_5, ABDAY_6, ABDAY_7
87 table[n] = nl_langinfo(items[n]);
101 static const char *table[12]
120 const char *result = 0;
122 if (n >= 0 && n < 12) {
127 MON_1, MON_2, MON_3, MON_4, MON_5, MON_6,
128 MON_7, MON_8, MON_9, MON_10, MON_11, MON_12
130 table[n] = nl_langinfo(items[n]);
142 days_in_month(struct tm *current, int offset /* -1, 0, 1 */ )
144 static const int nominal[] =
146 31, 28, 31, 30, 31, 30,
147 31, 31, 30, 31, 30, 31
149 int year = current->tm_year;
150 int month = current->tm_mon + offset;
157 while (month >= 12) {
161 result = nominal[month];
163 result += ((year % 4) == 0);
168 days_in_year(struct tm *current, int offset /* -1, 0, 1 */ )
170 int year = current->tm_year + 1900 + offset;
172 return ((year % 4) == 0) ? 366 : 365;
176 day_cell_number(struct tm *current)
179 cell = current->tm_mday - ((6 + current->tm_mday - current->tm_wday) % 7);
180 if ((current->tm_mday - 1) % 7 != current->tm_wday)
188 next_or_previous(int key, int two_d)
194 result = two_d ? -7 : -1;
200 result = two_d ? 7 : 1;
202 case DLGK_GRID_RIGHT:
213 * Draw the day-of-month selection box
216 draw_day(BOX * data, struct tm *current)
218 int cell_wide = MON_WIDE;
219 int y, x, this_x = 0;
220 int save_y = 0, save_x = 0;
221 int day = current->tm_mday;
224 int last = days_in_month(current, 0);
225 int prev = days_in_month(current, -1);
227 werase(data->window);
228 dlg_draw_box(data->parent,
229 data->y - MARGIN, data->x - MARGIN,
230 data->height + (2 * MARGIN), data->width + (2 * MARGIN),
231 menubox_border_attr, menubox_attr); /* border of daybox */
233 wattrset(data->window, menubox_attr); /* daynames headline */
234 for (x = 0; x < 7; x++) {
235 mvwprintw(data->window,
236 0, (x + 1) * cell_wide, "%*.*s ",
242 mday = ((6 + current->tm_mday - current->tm_wday) % 7) - 7;
245 /* mday is now in the range -6 to 0. */
246 week = (current->tm_yday + 6 + mday - current->tm_mday) / 7;
248 for (y = 1; mday < last; y++) {
249 wattrset(data->window, menubox_attr); /* weeknumbers headline */
250 mvwprintw(data->window,
255 for (x = 0; x < 7; x++) {
256 this_x = 1 + (x + 1) * cell_wide;
258 if (wmove(data->window, y, this_x) == ERR)
260 wattrset(data->window, item_attr); /* not selected days */
262 wattrset(data->window, item_selected_attr); /* selected day */
268 wprintw(data->window, "%*d", cell_wide - 2, mday);
269 } else if (mday == day) {
270 wprintw(data->window, "%*d", cell_wide - 2, mday - last);
272 } else if (mday == day) {
273 wprintw(data->window, "%*d", cell_wide - 2, mday + prev);
276 wmove(data->window, save_y, save_x);
278 /* just draw arrows - scrollbar is unsuitable here */
279 dlg_draw_arrows(data->parent, TRUE, TRUE,
280 data->x + ARROWS_COL,
282 data->y + data->height);
288 * Draw the month-of-year selection box
291 draw_month(BOX * data, struct tm *current)
295 month = current->tm_mon + 1;
297 wattrset(data->parent, dialog_attr); /* Headline "Month" */
298 (void) mvwprintw(data->parent, data->y - 2, data->x - 1, _("Month"));
299 dlg_draw_box(data->parent,
300 data->y - 1, data->x - 1,
301 data->height + 2, data->width + 2,
302 menubox_border_attr, menubox_attr); /* borders of monthbox */
303 wattrset(data->window, item_attr); /* color the month selection */
304 mvwprintw(data->window, 0, 0, "%s", nameOfMonth(month - 1));
305 wmove(data->window, 0, 0);
310 * Draw the year selection box
313 draw_year(BOX * data, struct tm *current)
315 int year = current->tm_year + 1900;
317 wattrset(data->parent, dialog_attr); /* Headline "Year" */
318 (void) mvwprintw(data->parent, data->y - 2, data->x - 1, _("Year"));
319 dlg_draw_box(data->parent,
320 data->y - 1, data->x - 1,
321 data->height + 2, data->width + 2,
322 menubox_border_attr, menubox_attr); /* borders of yearbox */
323 wattrset(data->window, item_attr); /* color the year selection */
324 mvwprintw(data->window, 0, 0, "%4d", year);
325 wmove(data->window, 0, 0);
330 init_object(BOX * data,
333 int width, int height,
337 data->parent = parent;
341 data->height = height;
342 data->box_draw = box_draw;
344 data->window = derwin(data->parent,
345 data->height, data->width,
347 if (data->window == 0)
349 (void) keypad(data->window, TRUE);
351 dlg_mouse_setbase(getbegx(parent), getbegy(parent));
353 dlg_mouse_mkbigregion(y + 1, x + MON_WIDE, height - 1, width - MON_WIDE,
354 KEY_MAX, 1, MON_WIDE, 3);
356 dlg_mouse_mkregion(y, x, height, width, code);
363 CleanupResult(int code, WINDOW *dialog, char *prompt, DIALOG_VARS * save_vars)
366 dlg_del_window(dialog);
367 dlg_mouse_free_regions();
370 dlg_restore_vars(save_vars);
375 #define DrawObject(data) (data)->box_draw(data, ¤t)
378 * Display a dialog box for entering a date
381 dialog_calendar(const char *title,
382 const char *subtitle,
390 static DLG_KEYS_BINDING binding[] = {
393 DLG_KEYS_DATA( DLGK_ENTER, ' ' ),
394 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
395 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
396 DLG_KEYS_DATA( DLGK_GRID_DOWN, 'j' ),
397 DLG_KEYS_DATA( DLGK_GRID_DOWN, DLGK_MOUSE(KEY_NPAGE) ),
398 DLG_KEYS_DATA( DLGK_GRID_DOWN, KEY_DOWN ),
399 DLG_KEYS_DATA( DLGK_GRID_DOWN, KEY_NPAGE ),
400 DLG_KEYS_DATA( DLGK_GRID_LEFT, '-' ),
401 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'h' ),
402 DLG_KEYS_DATA( DLGK_GRID_LEFT, CHR_BACKSPACE ),
403 DLG_KEYS_DATA( DLGK_GRID_LEFT, CHR_PREVIOUS ),
404 DLG_KEYS_DATA( DLGK_GRID_LEFT, KEY_LEFT ),
405 DLG_KEYS_DATA( DLGK_GRID_RIGHT, '+' ),
406 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
407 DLG_KEYS_DATA( DLGK_GRID_RIGHT, CHR_NEXT ),
408 DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_NEXT ),
409 DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
410 DLG_KEYS_DATA( DLGK_GRID_UP, 'k' ),
411 DLG_KEYS_DATA( DLGK_GRID_UP, KEY_PPAGE ),
412 DLG_KEYS_DATA( DLGK_GRID_UP, KEY_PREVIOUS ),
413 DLG_KEYS_DATA( DLGK_GRID_UP, KEY_UP ),
414 DLG_KEYS_DATA( DLGK_GRID_UP, DLGK_MOUSE(KEY_PPAGE) ),
420 int old_height = height;
421 int old_width = width;
423 BOX dy_box, mn_box, yr_box;
429 int result = DLG_EXIT_UNKNOWN;
431 time_t now_time = time((time_t *) 0);
433 int state = dlg_defaultno_button();
434 const char **buttons = dlg_ok_labels();
435 char *prompt = dlg_strclone(subtitle);
436 int mincols = MIN_WIDE;
437 char buffer[MAX_LEN];
438 DIALOG_VARS save_vars;
440 dlg_save_vars(&save_vars);
441 dialog_vars.separate_output = TRUE;
445 now_time = time((time_t *) 0);
446 current = *localtime(&now_time);
448 day = current.tm_mday;
450 month = current.tm_mon + 1;
452 year = current.tm_year + 1900;
454 /* compute a struct tm that matches the day/month/year parameters */
455 if (((year -= 1900) > 0) && (year < 200)) {
456 /* ugly, but I'd like to run this on older machines w/o mktime -TD */
458 if (year > current.tm_year) {
459 now_time += ONE_DAY * days_in_year(¤t, 0);
460 } else if (year < current.tm_year) {
461 now_time -= ONE_DAY * days_in_year(¤t, -1);
462 } else if (month > current.tm_mon + 1) {
463 now_time += ONE_DAY * days_in_month(¤t, 0);
464 } else if (month < current.tm_mon + 1) {
465 now_time -= ONE_DAY * days_in_month(¤t, -1);
466 } else if (day > current.tm_mday) {
468 } else if (day < current.tm_mday) {
473 current = *localtime(&now_time);
476 dlg_button_layout(buttons, &mincols);
482 dlg_auto_size(title, prompt, &height, &width, 0, mincols);
483 height += MIN_HIGH - 1;
484 dlg_print_size(height, width);
485 dlg_ctl_size(height, width);
487 dialog = dlg_new_window(height, width,
488 dlg_box_y_ordinate(height),
489 dlg_box_x_ordinate(width));
490 dlg_register_window(dialog, "calendar", binding);
491 dlg_register_buttons(dialog, "calendar", buttons);
494 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
495 dlg_draw_bottom_box(dialog);
496 dlg_draw_title(dialog, title);
498 wattrset(dialog, dialog_attr); /* text mainbox */
499 dlg_print_autowrap(dialog, prompt, height, width);
501 /* compute positions of day, month and year boxes */
502 memset(&dy_box, 0, sizeof(dy_box));
503 memset(&mn_box, 0, sizeof(mn_box));
504 memset(&yr_box, 0, sizeof(yr_box));
506 if (init_object(&dy_box,
508 (width - DAY_WIDE) / 2,
509 1 + (height - (DAY_HIGH + BTN_HIGH + (5 * MARGIN))),
514 || DrawObject(&dy_box) < 0) {
515 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
518 if (init_object(&mn_box,
521 dy_box.y - (HDR_HIGH + 2 * MARGIN),
522 (DAY_WIDE / 2) - MARGIN,
526 || DrawObject(&mn_box) < 0) {
527 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
530 if (init_object(&yr_box,
532 dy_box.x + mn_box.width + 2,
538 || DrawObject(&yr_box) < 0) {
539 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
542 while (result == DLG_EXIT_UNKNOWN) {
543 BOX *obj = (state == sDAY ? &dy_box
544 : (state == sMONTH ? &mn_box :
545 (state == sYEAR ? &yr_box : 0)));
547 button = (state < 0) ? 0 : state;
548 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
550 dlg_set_focus(dialog, obj->window);
552 key = dlg_mouse_wgetch(dialog, &fkey);
553 if (dlg_result_key(key, fkey, &result))
556 if (fkey && (key >= DLGK_MOUSE(KEY_MIN) && key <= DLGK_MOUSE(KEY_MAX))) {
557 key = dlg_lookup_key(dialog, key - M_EVENT, &fkey);
560 if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
563 /* handle function-keys */
565 case DLGK_MOUSE('D'):
568 case DLGK_MOUSE('M'):
571 case DLGK_MOUSE('Y'):
575 result = dlg_enter_buttoncode(button);
577 case DLGK_FIELD_PREV:
578 state = dlg_prev_ok_buttonindex(state, sMONTH);
580 case DLGK_FIELD_NEXT:
581 state = dlg_next_ok_buttonindex(state, sMONTH);
590 dlg_del_window(dialog);
592 dlg_mouse_free_regions();
598 if (is_DLGK_MOUSE(key)) {
599 if ((key2 = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
602 } else if (key >= DLGK_MOUSE(KEY_MAX)) {
607 - DLGK_MOUSE(KEY_MAX)
608 - day_cell_number(¤t));
613 step = next_or_previous(key, (obj == &dy_box));
615 struct tm old = current;
617 /* see comment regarding mktime -TD */
618 if (obj == &dy_box) {
619 now_time += ONE_DAY * step;
620 } else if (obj == &mn_box) {
622 now_time += ONE_DAY *
623 days_in_month(¤t, 0);
625 now_time -= ONE_DAY *
626 days_in_month(¤t, -1);
627 } else if (obj == &yr_box) {
630 * days_in_year(¤t, 0));
633 * days_in_year(¤t, -1));
636 current = *localtime(&now_time);
639 && (current.tm_mday != old.tm_mday
640 || current.tm_mon != old.tm_mon
641 || current.tm_year != old.tm_year))
643 if (obj != &mn_box && current.tm_mon != old.tm_mon)
645 if (obj != &yr_box && current.tm_year != old.tm_year)
647 (void) DrawObject(obj);
649 } else if (state >= 0) {
650 if (next_or_previous(key, FALSE) < 0)
651 state = dlg_prev_ok_buttonindex(state, sMONTH);
652 else if (next_or_previous(key, FALSE) > 0)
653 state = dlg_next_ok_buttonindex(state, sMONTH);
660 #define DefaultFormat(dst, src) \
661 sprintf(dst, "%02d/%02d/%0d", \
662 src.tm_mday, src.tm_mon + 1, src.tm_year + 1900)
664 if (dialog_vars.date_format != 0) {
665 size_t used = strftime(buffer,
667 dialog_vars.date_format,
669 if (used == 0 || *buffer == '\0')
670 DefaultFormat(buffer, current);
673 DefaultFormat(buffer, current);
675 dlg_add_result(buffer);
678 return CleanupResult(result, dialog, prompt, &save_vars);