2 * $Id: calendar.c,v 1.67 2013/03/17 15:03:41 tom Exp $
4 * calendar.c -- implements the calendar box
6 * Copyright 2001-2012,2013 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_box2(data
->parent
,
229 data
->y
- MARGIN
, data
->x
- MARGIN
,
230 data
->height
+ (2 * MARGIN
), data
->width
+ (2 * MARGIN
),
233 menubox_border2_attr
);
235 (void) wattrset(data
->window
, menubox_attr
); /* daynames headline */
236 for (x
= 0; x
< 7; x
++) {
237 mvwprintw(data
->window
,
238 0, (x
+ 1) * cell_wide
, "%*.*s ",
244 mday
= ((6 + current
->tm_mday
- current
->tm_wday
) % 7) - 7;
247 /* mday is now in the range -6 to 0. */
248 week
= (current
->tm_yday
+ 6 + mday
- current
->tm_mday
) / 7;
250 for (y
= 1; mday
< last
; y
++) {
251 (void) wattrset(data
->window
, menubox_attr
); /* weeknumbers headline */
252 mvwprintw(data
->window
,
257 for (x
= 0; x
< 7; x
++) {
258 this_x
= 1 + (x
+ 1) * cell_wide
;
260 if (wmove(data
->window
, y
, this_x
) == ERR
)
262 (void) wattrset(data
->window
, item_attr
); /* not selected days */
264 (void) wattrset(data
->window
, item_selected_attr
); /* selected day */
270 wprintw(data
->window
, "%*d", cell_wide
- 2, mday
);
271 } else if (mday
== day
) {
272 wprintw(data
->window
, "%*d", cell_wide
- 2, mday
- last
);
274 } else if (mday
== day
) {
275 wprintw(data
->window
, "%*d", cell_wide
- 2, mday
+ prev
);
278 wmove(data
->window
, save_y
, save_x
);
280 /* just draw arrows - scrollbar is unsuitable here */
281 dlg_draw_arrows(data
->parent
, TRUE
, TRUE
,
282 data
->x
+ ARROWS_COL
,
284 data
->y
+ data
->height
);
290 * Draw the month-of-year selection box
293 draw_month(BOX
* data
, struct tm
*current
)
297 month
= current
->tm_mon
+ 1;
299 (void) wattrset(data
->parent
, dialog_attr
); /* Headline "Month" */
300 (void) mvwprintw(data
->parent
, data
->y
- 2, data
->x
- 1, _("Month"));
301 dlg_draw_box2(data
->parent
,
302 data
->y
- 1, data
->x
- 1,
303 data
->height
+ 2, data
->width
+ 2,
306 menubox_border2_attr
);
307 (void) wattrset(data
->window
, item_attr
); /* color the month selection */
308 mvwprintw(data
->window
, 0, 0, "%s", nameOfMonth(month
- 1));
309 wmove(data
->window
, 0, 0);
314 * Draw the year selection box
317 draw_year(BOX
* data
, struct tm
*current
)
319 int year
= current
->tm_year
+ 1900;
321 (void) wattrset(data
->parent
, dialog_attr
); /* Headline "Year" */
322 (void) mvwprintw(data
->parent
, data
->y
- 2, data
->x
- 1, _("Year"));
323 dlg_draw_box2(data
->parent
,
324 data
->y
- 1, data
->x
- 1,
325 data
->height
+ 2, data
->width
+ 2,
328 menubox_border2_attr
);
329 (void) wattrset(data
->window
, item_attr
); /* color the year selection */
330 mvwprintw(data
->window
, 0, 0, "%4d", year
);
331 wmove(data
->window
, 0, 0);
336 init_object(BOX
* data
,
339 int width
, int height
,
343 data
->parent
= parent
;
347 data
->height
= height
;
348 data
->box_draw
= box_draw
;
350 data
->window
= derwin(data
->parent
,
351 data
->height
, data
->width
,
353 if (data
->window
== 0)
355 (void) keypad(data
->window
, TRUE
);
357 dlg_mouse_setbase(getbegx(parent
), getbegy(parent
));
359 dlg_mouse_mkbigregion(y
+ 1, x
+ MON_WIDE
, height
- 1, width
- MON_WIDE
,
360 KEY_MAX
, 1, MON_WIDE
, 3);
362 dlg_mouse_mkregion(y
, x
, height
, width
, code
);
369 CleanupResult(int code
, WINDOW
*dialog
, char *prompt
, DIALOG_VARS
* save_vars
)
372 dlg_del_window(dialog
);
373 dlg_mouse_free_regions();
376 dlg_restore_vars(save_vars
);
381 #define DrawObject(data) (data)->box_draw(data, ¤t)
384 * Display a dialog box for entering a date
387 dialog_calendar(const char *title
,
388 const char *subtitle
,
396 static DLG_KEYS_BINDING binding
[] = {
399 DLG_KEYS_DATA( DLGK_ENTER
, ' ' ),
400 DLG_KEYS_DATA( DLGK_FIELD_NEXT
, TAB
),
401 DLG_KEYS_DATA( DLGK_FIELD_PREV
, KEY_BTAB
),
402 DLG_KEYS_DATA( DLGK_GRID_DOWN
, 'j' ),
403 DLG_KEYS_DATA( DLGK_GRID_DOWN
, DLGK_MOUSE(KEY_NPAGE
) ),
404 DLG_KEYS_DATA( DLGK_GRID_DOWN
, KEY_DOWN
),
405 DLG_KEYS_DATA( DLGK_GRID_DOWN
, KEY_NPAGE
),
406 DLG_KEYS_DATA( DLGK_GRID_LEFT
, '-' ),
407 DLG_KEYS_DATA( DLGK_GRID_LEFT
, 'h' ),
408 DLG_KEYS_DATA( DLGK_GRID_LEFT
, CHR_BACKSPACE
),
409 DLG_KEYS_DATA( DLGK_GRID_LEFT
, CHR_PREVIOUS
),
410 DLG_KEYS_DATA( DLGK_GRID_LEFT
, KEY_LEFT
),
411 DLG_KEYS_DATA( DLGK_GRID_RIGHT
, '+' ),
412 DLG_KEYS_DATA( DLGK_GRID_RIGHT
, 'l' ),
413 DLG_KEYS_DATA( DLGK_GRID_RIGHT
, CHR_NEXT
),
414 DLG_KEYS_DATA( DLGK_GRID_RIGHT
, KEY_NEXT
),
415 DLG_KEYS_DATA( DLGK_GRID_RIGHT
, KEY_RIGHT
),
416 DLG_KEYS_DATA( DLGK_GRID_UP
, 'k' ),
417 DLG_KEYS_DATA( DLGK_GRID_UP
, KEY_PPAGE
),
418 DLG_KEYS_DATA( DLGK_GRID_UP
, KEY_PREVIOUS
),
419 DLG_KEYS_DATA( DLGK_GRID_UP
, KEY_UP
),
420 DLG_KEYS_DATA( DLGK_GRID_UP
, DLGK_MOUSE(KEY_PPAGE
) ),
426 int old_height
= height
;
427 int old_width
= width
;
429 BOX dy_box
, mn_box
, yr_box
;
435 int result
= DLG_EXIT_UNKNOWN
;
437 time_t now_time
= time((time_t *) 0);
439 int state
= dlg_default_button();
440 const char **buttons
= dlg_ok_labels();
441 char *prompt
= dlg_strclone(subtitle
);
442 int mincols
= MIN_WIDE
;
443 char buffer
[MAX_LEN
];
444 DIALOG_VARS save_vars
;
446 dlg_save_vars(&save_vars
);
447 dialog_vars
.separate_output
= TRUE
;
451 now_time
= time((time_t *) 0);
452 current
= *localtime(&now_time
);
454 day
= current
.tm_mday
;
456 month
= current
.tm_mon
+ 1;
458 year
= current
.tm_year
+ 1900;
460 /* compute a struct tm that matches the day/month/year parameters */
461 if (((year
-= 1900) > 0) && (year
< 200)) {
462 /* ugly, but I'd like to run this on older machines w/o mktime -TD */
464 if (year
> current
.tm_year
) {
465 now_time
+= ONE_DAY
* days_in_year(¤t
, 0);
466 } else if (year
< current
.tm_year
) {
467 now_time
-= ONE_DAY
* days_in_year(¤t
, -1);
468 } else if (month
> current
.tm_mon
+ 1) {
469 now_time
+= ONE_DAY
* days_in_month(¤t
, 0);
470 } else if (month
< current
.tm_mon
+ 1) {
471 now_time
-= ONE_DAY
* days_in_month(¤t
, -1);
472 } else if (day
> current
.tm_mday
) {
474 } else if (day
< current
.tm_mday
) {
479 current
= *localtime(&now_time
);
482 dlg_button_layout(buttons
, &mincols
);
488 dlg_auto_size(title
, prompt
, &height
, &width
, 0, mincols
);
489 height
+= MIN_HIGH
- 1;
490 dlg_print_size(height
, width
);
491 dlg_ctl_size(height
, width
);
493 dialog
= dlg_new_window(height
, width
,
494 dlg_box_y_ordinate(height
),
495 dlg_box_x_ordinate(width
));
496 dlg_register_window(dialog
, "calendar", binding
);
497 dlg_register_buttons(dialog
, "calendar", buttons
);
500 dlg_draw_box2(dialog
, 0, 0, height
, width
, dialog_attr
, border_attr
, border2_attr
);
501 dlg_draw_bottom_box2(dialog
, border_attr
, border2_attr
, dialog_attr
);
502 dlg_draw_title(dialog
, title
);
504 (void) wattrset(dialog
, dialog_attr
); /* text mainbox */
505 dlg_print_autowrap(dialog
, prompt
, height
, width
);
507 /* compute positions of day, month and year boxes */
508 memset(&dy_box
, 0, sizeof(dy_box
));
509 memset(&mn_box
, 0, sizeof(mn_box
));
510 memset(&yr_box
, 0, sizeof(yr_box
));
512 if (init_object(&dy_box
,
514 (width
- DAY_WIDE
) / 2,
515 1 + (height
- (DAY_HIGH
+ BTN_HIGH
+ (5 * MARGIN
))),
520 || DrawObject(&dy_box
) < 0) {
521 return CleanupResult(DLG_EXIT_ERROR
, dialog
, prompt
, &save_vars
);
524 if (init_object(&mn_box
,
527 dy_box
.y
- (HDR_HIGH
+ 2 * MARGIN
),
528 (DAY_WIDE
/ 2) - MARGIN
,
532 || DrawObject(&mn_box
) < 0) {
533 return CleanupResult(DLG_EXIT_ERROR
, dialog
, prompt
, &save_vars
);
536 if (init_object(&yr_box
,
538 dy_box
.x
+ mn_box
.width
+ 2,
544 || DrawObject(&yr_box
) < 0) {
545 return CleanupResult(DLG_EXIT_ERROR
, dialog
, prompt
, &save_vars
);
548 dlg_trace_win(dialog
);
549 while (result
== DLG_EXIT_UNKNOWN
) {
550 BOX
*obj
= (state
== sDAY
? &dy_box
551 : (state
== sMONTH
? &mn_box
:
552 (state
== sYEAR
? &yr_box
: 0)));
554 button
= (state
< 0) ? 0 : state
;
555 dlg_draw_buttons(dialog
, height
- 2, 0, buttons
, button
, FALSE
, width
);
557 dlg_set_focus(dialog
, obj
->window
);
559 key
= dlg_mouse_wgetch(dialog
, &fkey
);
560 if (dlg_result_key(key
, fkey
, &result
))
563 if (fkey
&& (key
>= DLGK_MOUSE(KEY_MIN
) && key
<= DLGK_MOUSE(KEY_MAX
))) {
564 key
= dlg_lookup_key(dialog
, key
- M_EVENT
, &fkey
);
567 if ((key2
= dlg_char_to_button(key
, buttons
)) >= 0) {
570 /* handle function-keys */
572 case DLGK_MOUSE('D'):
575 case DLGK_MOUSE('M'):
578 case DLGK_MOUSE('Y'):
582 result
= dlg_enter_buttoncode(button
);
584 case DLGK_FIELD_PREV
:
585 state
= dlg_prev_ok_buttonindex(state
, sMONTH
);
587 case DLGK_FIELD_NEXT
:
588 state
= dlg_next_ok_buttonindex(state
, sMONTH
);
597 dlg_del_window(dialog
);
599 dlg_mouse_free_regions();
605 if (is_DLGK_MOUSE(key
)) {
606 if ((key2
= dlg_ok_buttoncode(key
- M_EVENT
)) >= 0) {
609 } else if (key
>= DLGK_MOUSE(KEY_MAX
)) {
614 - DLGK_MOUSE(KEY_MAX
)
615 - day_cell_number(¤t
));
620 step
= next_or_previous(key
, (obj
== &dy_box
));
622 struct tm old
= current
;
624 /* see comment regarding mktime -TD */
625 if (obj
== &dy_box
) {
626 now_time
+= ONE_DAY
* step
;
627 } else if (obj
== &mn_box
) {
629 now_time
+= ONE_DAY
*
630 days_in_month(¤t
, 0);
632 now_time
-= ONE_DAY
*
633 days_in_month(¤t
, -1);
634 } else if (obj
== &yr_box
) {
637 * days_in_year(¤t
, 0));
640 * days_in_year(¤t
, -1));
643 current
= *localtime(&now_time
);
646 && (current
.tm_mday
!= old
.tm_mday
647 || current
.tm_mon
!= old
.tm_mon
648 || current
.tm_year
!= old
.tm_year
))
650 if (obj
!= &mn_box
&& current
.tm_mon
!= old
.tm_mon
)
652 if (obj
!= &yr_box
&& current
.tm_year
!= old
.tm_year
)
654 (void) DrawObject(obj
);
656 } else if (state
>= 0) {
657 if (next_or_previous(key
, FALSE
) < 0)
658 state
= dlg_prev_ok_buttonindex(state
, sMONTH
);
659 else if (next_or_previous(key
, FALSE
) > 0)
660 state
= dlg_next_ok_buttonindex(state
, sMONTH
);
667 #define DefaultFormat(dst, src) \
668 sprintf(dst, "%02d/%02d/%0d", \
669 src.tm_mday, src.tm_mon + 1, src.tm_year + 1900)
671 if (dialog_vars
.date_format
!= 0) {
672 size_t used
= strftime(buffer
,
674 dialog_vars
.date_format
,
676 if (used
== 0 || *buffer
== '\0')
677 DefaultFormat(buffer
, current
);
680 DefaultFormat(buffer
, current
);
682 dlg_add_result(buffer
);
684 dlg_add_last_key(-1);
686 return CleanupResult(result
, dialog
, prompt
, &save_vars
);