calendar.c revision 217309
1/*
2 * $Id: calendar.c,v 1.59 2010/01/18 09:50:44 tom Exp $
3 *
4 *  calendar.c -- implements the calendar box
5 *
6 *  Copyright 2001-2009,2010	Thomas E. Dickey
7 *
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.
11 *
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.
16 *
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.
22 */
23
24#include <dialog.h>
25#include <dlg_keys.h>
26
27#include <time.h>
28
29#define ONE_DAY  (60 * 60 * 24)
30
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 */
36
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))
40
41typedef enum {
42    sMONTH = -3
43    ,sYEAR = -2
44    ,sDAY = -1
45} STATES;
46
47struct _box;
48
49typedef int (*BOX_DRAW) (struct _box *, struct tm *);
50
51typedef struct _box {
52    WINDOW *parent;
53    WINDOW *window;
54    int x;
55    int y;
56    int width;
57    int height;
58    BOX_DRAW box_draw;
59} BOX;
60
61static const char *
62nameOfDayOfWeek(int n)
63{
64    static const char *table[7]
65#ifndef ENABLE_NLS
66    =
67    {
68	"Sunday",
69	"Monday",
70	"Tuesday",
71	"Wednesday",
72	"Thursday",
73	"Friday",
74	"Saturday"
75    }
76#endif
77     ;
78    const char *result = 0;
79
80    if (n >= 0 && n < 7) {
81#ifdef ENABLE_NLS
82	if (table[n] == 0) {
83	    nl_item items[7] =
84	    {
85		ABDAY_1, ABDAY_2, ABDAY_3, ABDAY_4, ABDAY_5, ABDAY_6, ABDAY_7
86	    };
87	    table[n] = nl_langinfo(items[n]);
88	}
89#endif
90	result = table[n];
91    }
92    if (result == 0) {
93	result = "?";
94    }
95    return result;
96}
97
98static const char *
99nameOfMonth(int n)
100{
101    static const char *table[12]
102#ifndef ENABLE_NLS
103    =
104    {
105	"January",
106	"February",
107	"March",
108	"April",
109	"May",
110	"June",
111	"July",
112	"August",
113	"September",
114	"October",
115	"November",
116	"December"
117    }
118#endif
119     ;
120    const char *result = 0;
121
122    if (n >= 0 && n < 12) {
123#ifdef ENABLE_NLS
124	if (table[n] == 0) {
125	    nl_item items[12] =
126	    {
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
129	    };
130	    table[n] = nl_langinfo(items[n]);
131	}
132#endif
133	result = table[n];
134    }
135    if (result == 0) {
136	result = "?";
137    }
138    return result;
139}
140
141static int
142days_in_month(struct tm *current, int offset /* -1, 0, 1 */ )
143{
144    static const int nominal[] =
145    {
146	31, 28, 31, 30, 31, 30,
147	31, 31, 30, 31, 30, 31
148    };
149    int year = current->tm_year;
150    int month = current->tm_mon + offset;
151    int result;
152
153    while (month < 0) {
154	month += 12;
155	year -= 1;
156    }
157    while (month >= 12) {
158	month -= 12;
159	year += 1;
160    }
161    result = nominal[month];
162    if (month == 1)
163	result += ((year % 4) == 0);
164    return result;
165}
166
167static int
168days_in_year(struct tm *current, int offset /* -1, 0, 1 */ )
169{
170    int year = current->tm_year + 1900 + offset;
171
172    return ((year % 4) == 0) ? 366 : 365;
173}
174
175static int
176day_cell_number(struct tm *current)
177{
178    int cell;
179    cell = current->tm_mday - ((6 + current->tm_mday - current->tm_wday) % 7);
180    if ((current->tm_mday - 1) % 7 != current->tm_wday)
181	cell += 6;
182    else
183	cell--;
184    return cell;
185}
186
187static int
188next_or_previous(int key, int two_d)
189{
190    int result = 0;
191
192    switch (key) {
193    case DLGK_GRID_UP:
194	result = two_d ? -7 : -1;
195	break;
196    case DLGK_GRID_LEFT:
197	result = -1;
198	break;
199    case DLGK_GRID_DOWN:
200	result = two_d ? 7 : 1;
201	break;
202    case DLGK_GRID_RIGHT:
203	result = 1;
204	break;
205    default:
206	beep();
207	break;
208    }
209    return result;
210}
211
212/*
213 * Draw the day-of-month selection box
214 */
215static int
216draw_day(BOX * data, struct tm *current)
217{
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;
222    int mday;
223    int week;
224    int last = days_in_month(current, 0);
225    int prev = days_in_month(current, -1);
226
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 */
232
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 ",
237		  cell_wide - 1,
238		  cell_wide - 1,
239		  nameOfDayOfWeek(x));
240    }
241
242    mday = ((6 + current->tm_mday - current->tm_wday) % 7) - 7;
243    if (mday <= -7)
244	mday += 7;
245    /* mday is now in the range -6 to 0. */
246    week = (current->tm_yday + 6 + mday - current->tm_mday) / 7;
247
248    for (y = 1; mday < last; y++) {
249	wattrset(data->window, menubox_attr);	/* weeknumbers headline */
250	mvwprintw(data->window,
251		  y, 0,
252		  "%*d ",
253		  cell_wide - 1,
254		  ++week);
255	for (x = 0; x < 7; x++) {
256	    this_x = 1 + (x + 1) * cell_wide;
257	    ++mday;
258	    if (wmove(data->window, y, this_x) == ERR)
259		continue;
260	    wattrset(data->window, item_attr);	/* not selected days */
261	    if (mday == day) {
262		wattrset(data->window, item_selected_attr);	/* selected day */
263		save_y = y;
264		save_x = this_x;
265	    }
266	    if (mday > 0) {
267		if (mday <= last) {
268		    wprintw(data->window, "%*d", cell_wide - 2, mday);
269		} else if (mday == day) {
270		    wprintw(data->window, "%*d", cell_wide - 2, mday - last);
271		}
272	    } else if (mday == day) {
273		wprintw(data->window, "%*d", cell_wide - 2, mday + prev);
274	    }
275	}
276	wmove(data->window, save_y, save_x);
277    }
278    /* just draw arrows - scrollbar is unsuitable here */
279    dlg_draw_arrows(data->parent, TRUE, TRUE,
280		    data->x + ARROWS_COL,
281		    data->y - 1,
282		    data->y + data->height);
283
284    return 0;
285}
286
287/*
288 * Draw the month-of-year selection box
289 */
290static int
291draw_month(BOX * data, struct tm *current)
292{
293    int month;
294
295    month = current->tm_mon + 1;
296
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);
306    return 0;
307}
308
309/*
310 * Draw the year selection box
311 */
312static int
313draw_year(BOX * data, struct tm *current)
314{
315    int year = current->tm_year + 1900;
316
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);
326    return 0;
327}
328
329static int
330init_object(BOX * data,
331	    WINDOW *parent,
332	    int x, int y,
333	    int width, int height,
334	    BOX_DRAW box_draw,
335	    int code)
336{
337    data->parent = parent;
338    data->x = x;
339    data->y = y;
340    data->width = width;
341    data->height = height;
342    data->box_draw = box_draw;
343
344    data->window = derwin(data->parent,
345			  data->height, data->width,
346			  data->y, data->x);
347    if (data->window == 0)
348	return -1;
349    (void) keypad(data->window, TRUE);
350
351    dlg_mouse_setbase(getbegx(parent), getbegy(parent));
352    if (code == 'D') {
353	dlg_mouse_mkbigregion(y + 1, x + MON_WIDE, height - 1, width - MON_WIDE,
354			      KEY_MAX, 1, MON_WIDE, 3);
355    } else {
356	dlg_mouse_mkregion(y, x, height, width, code);
357    }
358
359    return 0;
360}
361
362static int
363CleanupResult(int code, WINDOW *dialog, char *prompt, DIALOG_VARS * save_vars)
364{
365    if (dialog != 0)
366	dlg_del_window(dialog);
367    dlg_mouse_free_regions();
368    if (prompt != 0)
369	free(prompt);
370    dlg_restore_vars(save_vars);
371
372    return code;
373}
374
375#define DrawObject(data) (data)->box_draw(data, &current)
376
377/*
378 * Display a dialog box for entering a date
379 */
380int
381dialog_calendar(const char *title,
382		const char *subtitle,
383		int height,
384		int width,
385		int day,
386		int month,
387		int year)
388{
389    /* *INDENT-OFF* */
390    static DLG_KEYS_BINDING binding[] = {
391	ENTERKEY_BINDINGS,
392	DLG_KEYS_DATA( DLGK_ENTER,	' ' ),
393	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
394	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
395	DLG_KEYS_DATA( DLGK_GRID_DOWN,	'j' ),
396	DLG_KEYS_DATA( DLGK_GRID_DOWN,	DLGK_MOUSE(KEY_NPAGE) ),
397	DLG_KEYS_DATA( DLGK_GRID_DOWN,	KEY_DOWN ),
398	DLG_KEYS_DATA( DLGK_GRID_DOWN,	KEY_NPAGE ),
399	DLG_KEYS_DATA( DLGK_GRID_LEFT,	'-' ),
400	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'h' ),
401	DLG_KEYS_DATA( DLGK_GRID_LEFT,  CHR_BACKSPACE ),
402	DLG_KEYS_DATA( DLGK_GRID_LEFT,  CHR_PREVIOUS ),
403	DLG_KEYS_DATA( DLGK_GRID_LEFT,  KEY_LEFT ),
404	DLG_KEYS_DATA( DLGK_GRID_RIGHT,	'+' ),
405	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
406	DLG_KEYS_DATA( DLGK_GRID_RIGHT, CHR_NEXT ),
407	DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_NEXT ),
408	DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
409	DLG_KEYS_DATA( DLGK_GRID_UP,	'k' ),
410	DLG_KEYS_DATA( DLGK_GRID_UP,	KEY_PPAGE ),
411	DLG_KEYS_DATA( DLGK_GRID_UP,	KEY_PREVIOUS ),
412	DLG_KEYS_DATA( DLGK_GRID_UP,	KEY_UP ),
413	DLG_KEYS_DATA( DLGK_GRID_UP,  	DLGK_MOUSE(KEY_PPAGE) ),
414	END_KEYS_BINDING
415    };
416    /* *INDENT-ON* */
417
418#ifdef KEY_RESIZE
419    int old_height = height;
420    int old_width = width;
421#endif
422    BOX dy_box, mn_box, yr_box;
423    int fkey;
424    int key = 0;
425    int key2;
426    int step;
427    int button;
428    int result = DLG_EXIT_UNKNOWN;
429    WINDOW *dialog;
430    time_t now_time = time((time_t *) 0);
431    struct tm current;
432    int state = dlg_defaultno_button();
433    const char **buttons = dlg_ok_labels();
434    char *prompt = dlg_strclone(subtitle);
435    int mincols = MIN_WIDE;
436    char buffer[MAX_LEN];
437    DIALOG_VARS save_vars;
438
439    dlg_save_vars(&save_vars);
440    dialog_vars.separate_output = TRUE;
441
442    dlg_does_output();
443
444    now_time = time((time_t *) 0);
445    current = *localtime(&now_time);
446    if (day < 0)
447	day = current.tm_mday;
448    if (month < 0)
449	month = current.tm_mon + 1;
450    if (year < 0)
451	year = current.tm_year + 1900;
452
453    /* compute a struct tm that matches the day/month/year parameters */
454    if (((year -= 1900) > 0) && (year < 200)) {
455	/* ugly, but I'd like to run this on older machines w/o mktime -TD */
456	for (;;) {
457	    if (year > current.tm_year) {
458		now_time += ONE_DAY * days_in_year(&current, 0);
459	    } else if (year < current.tm_year) {
460		now_time -= ONE_DAY * days_in_year(&current, -1);
461	    } else if (month > current.tm_mon + 1) {
462		now_time += ONE_DAY * days_in_month(&current, 0);
463	    } else if (month < current.tm_mon + 1) {
464		now_time -= ONE_DAY * days_in_month(&current, -1);
465	    } else if (day > current.tm_mday) {
466		now_time += ONE_DAY;
467	    } else if (day < current.tm_mday) {
468		now_time -= ONE_DAY;
469	    } else {
470		break;
471	    }
472	    current = *localtime(&now_time);
473	}
474    }
475    dlg_button_layout(buttons, &mincols);
476
477#ifdef KEY_RESIZE
478  retry:
479#endif
480
481    dlg_auto_size(title, prompt, &height, &width, 0, mincols);
482    height += MIN_HIGH - 1;
483    dlg_print_size(height, width);
484    dlg_ctl_size(height, width);
485
486    dialog = dlg_new_window(height, width,
487			    dlg_box_y_ordinate(height),
488			    dlg_box_x_ordinate(width));
489    dlg_register_window(dialog, "calendar", binding);
490    dlg_register_buttons(dialog, "calendar", buttons);
491
492    /* mainbox */
493    dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
494    dlg_draw_bottom_box(dialog);
495    dlg_draw_title(dialog, title);
496
497    wattrset(dialog, dialog_attr);	/* text mainbox */
498    dlg_print_autowrap(dialog, prompt, height, width);
499
500    /* compute positions of day, month and year boxes */
501    memset(&dy_box, 0, sizeof(dy_box));
502    memset(&mn_box, 0, sizeof(mn_box));
503    memset(&yr_box, 0, sizeof(yr_box));
504
505    if (init_object(&dy_box,
506		    dialog,
507		    (width - DAY_WIDE) / 2,
508		    1 + (height - (DAY_HIGH + BTN_HIGH + (5 * MARGIN))),
509		    DAY_WIDE,
510		    DAY_HIGH + 1,
511		    draw_day,
512		    'D') < 0
513	|| DrawObject(&dy_box) < 0) {
514	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
515    }
516
517    if (init_object(&mn_box,
518		    dialog,
519		    dy_box.x,
520		    dy_box.y - (HDR_HIGH + 2 * MARGIN),
521		    (DAY_WIDE / 2) - MARGIN,
522		    HDR_HIGH,
523		    draw_month,
524		    'M') < 0
525	|| DrawObject(&mn_box) < 0) {
526	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
527    }
528
529    if (init_object(&yr_box,
530		    dialog,
531		    dy_box.x + mn_box.width + 2,
532		    mn_box.y,
533		    mn_box.width,
534		    mn_box.height,
535		    draw_year,
536		    'Y') < 0
537	|| DrawObject(&yr_box) < 0) {
538	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
539    }
540
541    while (result == DLG_EXIT_UNKNOWN) {
542	BOX *obj = (state == sDAY ? &dy_box
543		    : (state == sMONTH ? &mn_box :
544		       (state == sYEAR ? &yr_box : 0)));
545
546	button = (state < 0) ? 0 : state;
547	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
548	if (obj != 0)
549	    dlg_set_focus(dialog, obj->window);
550
551	key = dlg_mouse_wgetch(dialog, &fkey);
552	if (dlg_result_key(key, fkey, &result))
553	    break;
554
555	if (fkey && (key >= DLGK_MOUSE(KEY_MIN) && key <= DLGK_MOUSE(KEY_MAX))) {
556	    key = dlg_lookup_key(dialog, key - M_EVENT, &fkey);
557	}
558
559	if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
560	    result = key2;
561	} else if (fkey) {
562	    /* handle function-keys */
563	    switch (key) {
564	    case DLGK_MOUSE('D'):
565		state = sDAY;
566		break;
567	    case DLGK_MOUSE('M'):
568		state = sMONTH;
569		break;
570	    case DLGK_MOUSE('Y'):
571		state = sYEAR;
572		break;
573	    case DLGK_ENTER:
574		result = dlg_ok_buttoncode(button);
575		break;
576	    case DLGK_FIELD_PREV:
577		state = dlg_prev_ok_buttonindex(state, sMONTH);
578		break;
579	    case DLGK_FIELD_NEXT:
580		state = dlg_next_ok_buttonindex(state, sMONTH);
581		break;
582#ifdef KEY_RESIZE
583	    case KEY_RESIZE:
584		/* reset data */
585		height = old_height;
586		width = old_width;
587		/* repaint */
588		dlg_clear();
589		dlg_del_window(dialog);
590		refresh();
591		dlg_mouse_free_regions();
592		goto retry;
593#endif
594	    default:
595		step = 0;
596		key2 = -1;
597		if (is_DLGK_MOUSE(key)) {
598		    if ((key2 = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
599			result = key2;
600			break;
601		    } else if (key >= DLGK_MOUSE(KEY_MAX)) {
602			state = sDAY;
603			obj = &dy_box;
604			key2 = 1;
605			step = (key
606				- DLGK_MOUSE(KEY_MAX)
607				- day_cell_number(&current));
608		    }
609		}
610		if (obj != 0) {
611		    if (key2 < 0)
612			step = next_or_previous(key, (obj == &dy_box));
613		    if (step != 0) {
614			struct tm old = current;
615
616			/* see comment regarding mktime -TD */
617			if (obj == &dy_box) {
618			    now_time += ONE_DAY * step;
619			} else if (obj == &mn_box) {
620			    if (step > 0)
621				now_time += ONE_DAY *
622				    days_in_month(&current, 0);
623			    else
624				now_time -= ONE_DAY *
625				    days_in_month(&current, -1);
626			} else if (obj == &yr_box) {
627			    if (step > 0)
628				now_time += (ONE_DAY
629					     * days_in_year(&current, 0));
630			    else
631				now_time -= (ONE_DAY
632					     * days_in_year(&current, -1));
633			}
634
635			current = *localtime(&now_time);
636
637			if (obj != &dy_box
638			    && (current.tm_mday != old.tm_mday
639				|| current.tm_mon != old.tm_mon
640				|| current.tm_year != old.tm_year))
641			    DrawObject(&dy_box);
642			if (obj != &mn_box && current.tm_mon != old.tm_mon)
643			    DrawObject(&mn_box);
644			if (obj != &yr_box && current.tm_year != old.tm_year)
645			    DrawObject(&yr_box);
646			(void) DrawObject(obj);
647		    }
648		} else if (state >= 0) {
649		    if (next_or_previous(key, FALSE) < 0)
650			state = dlg_prev_ok_buttonindex(state, sMONTH);
651		    else if (next_or_previous(key, FALSE) > 0)
652			state = dlg_next_ok_buttonindex(state, sMONTH);
653		}
654		break;
655	    }
656	}
657    }
658
659#define DefaultFormat(dst, src) \
660	sprintf(dst, "%02d/%02d/%0d", \
661		src.tm_mday, src.tm_mon + 1, src.tm_year + 1900)
662#ifdef HAVE_STRFTIME
663    if (dialog_vars.date_format != 0) {
664       size_t used = strftime(buffer,
665			      sizeof(buffer) - 1,
666			      dialog_vars.date_format,
667			      &current);
668	if (used == 0 || *buffer == '\0')
669	    DefaultFormat(buffer, current);
670    } else
671#endif
672	DefaultFormat(buffer, current);
673
674    dlg_add_result(buffer);
675    dlg_add_separator();
676
677    return CleanupResult(result, dialog, prompt, &save_vars);
678}
679