xref: /freebsd/contrib/dialog/timebox.c (revision f4f33ea0c752ff0f9bfad34991d5bbb54e71133d)
14c8945a0SNathan Whitehorn /*
2*f4f33ea0SBaptiste Daroussin  * $Id: timebox.c,v 1.59 2018/06/19 22:57:01 tom Exp $
34c8945a0SNathan Whitehorn  *
44c8945a0SNathan Whitehorn  *  timebox.c -- implements the timebox dialog
54c8945a0SNathan Whitehorn  *
6*f4f33ea0SBaptiste Daroussin  *  Copyright 2001-2016,2018   Thomas E. Dickey
74c8945a0SNathan Whitehorn  *
84c8945a0SNathan Whitehorn  *  This program is free software; you can redistribute it and/or modify
94c8945a0SNathan Whitehorn  *  it under the terms of the GNU Lesser General Public License, version 2.1
104c8945a0SNathan Whitehorn  *  as published by the Free Software Foundation.
114c8945a0SNathan Whitehorn  *
124c8945a0SNathan Whitehorn  *  This program is distributed in the hope that it will be useful, but
134c8945a0SNathan Whitehorn  *  WITHOUT ANY WARRANTY; without even the implied warranty of
144c8945a0SNathan Whitehorn  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
154c8945a0SNathan Whitehorn  *  Lesser General Public License for more details.
164c8945a0SNathan Whitehorn  *
174c8945a0SNathan Whitehorn  *  You should have received a copy of the GNU Lesser General Public
184c8945a0SNathan Whitehorn  *  License along with this program; if not, write to
194c8945a0SNathan Whitehorn  *	Free Software Foundation, Inc.
204c8945a0SNathan Whitehorn  *	51 Franklin St., Fifth Floor
214c8945a0SNathan Whitehorn  *	Boston, MA 02110, USA.
224c8945a0SNathan Whitehorn  */
234c8945a0SNathan Whitehorn 
244c8945a0SNathan Whitehorn #include <dialog.h>
254c8945a0SNathan Whitehorn #include <dlg_keys.h>
264c8945a0SNathan Whitehorn 
274c8945a0SNathan Whitehorn #include <time.h>
284c8945a0SNathan Whitehorn 
294c8945a0SNathan Whitehorn #define ONE_HIGH 1
304c8945a0SNathan Whitehorn #define ONE_WIDE 2
314c8945a0SNathan Whitehorn #define BTN_HIGH 2
324c8945a0SNathan Whitehorn 
334c8945a0SNathan Whitehorn #define MIN_HIGH (ONE_HIGH + BTN_HIGH + (4 * MARGIN))
344c8945a0SNathan Whitehorn #define MIN_WIDE ((3 * (ONE_WIDE + 2 * MARGIN)) + 2 + (2 * MARGIN))
354c8945a0SNathan Whitehorn 
364c8945a0SNathan Whitehorn typedef enum {
374c8945a0SNathan Whitehorn     sHR = -3
384c8945a0SNathan Whitehorn     ,sMN = -2
394c8945a0SNathan Whitehorn     ,sSC = -1
404c8945a0SNathan Whitehorn } STATES;
414c8945a0SNathan Whitehorn 
424c8945a0SNathan Whitehorn struct _box;
434c8945a0SNathan Whitehorn 
444c8945a0SNathan Whitehorn typedef struct _box {
454c8945a0SNathan Whitehorn     WINDOW *parent;
464c8945a0SNathan Whitehorn     WINDOW *window;
474c8945a0SNathan Whitehorn     int x;
484c8945a0SNathan Whitehorn     int y;
494c8945a0SNathan Whitehorn     int width;
504c8945a0SNathan Whitehorn     int height;
514c8945a0SNathan Whitehorn     int period;
524c8945a0SNathan Whitehorn     int value;
534c8945a0SNathan Whitehorn } BOX;
544c8945a0SNathan Whitehorn 
554c8945a0SNathan Whitehorn static int
564c8945a0SNathan Whitehorn next_or_previous(int key)
574c8945a0SNathan Whitehorn {
584c8945a0SNathan Whitehorn     int result = 0;
594c8945a0SNathan Whitehorn 
604c8945a0SNathan Whitehorn     switch (key) {
614c8945a0SNathan Whitehorn     case DLGK_ITEM_PREV:
624c8945a0SNathan Whitehorn 	result = -1;
634c8945a0SNathan Whitehorn 	break;
644c8945a0SNathan Whitehorn     case DLGK_ITEM_NEXT:
654c8945a0SNathan Whitehorn 	result = 1;
664c8945a0SNathan Whitehorn 	break;
674c8945a0SNathan Whitehorn     default:
684c8945a0SNathan Whitehorn 	beep();
694c8945a0SNathan Whitehorn 	break;
704c8945a0SNathan Whitehorn     }
714c8945a0SNathan Whitehorn     return result;
724c8945a0SNathan Whitehorn }
734c8945a0SNathan Whitehorn /*
744c8945a0SNathan Whitehorn  * Draw the hour-of-month selection box
754c8945a0SNathan Whitehorn  */
764c8945a0SNathan Whitehorn static int
774c8945a0SNathan Whitehorn draw_cell(BOX * data)
784c8945a0SNathan Whitehorn {
794c8945a0SNathan Whitehorn     werase(data->window);
804c8945a0SNathan Whitehorn     dlg_draw_box(data->parent,
814c8945a0SNathan Whitehorn 		 data->y - MARGIN, data->x - MARGIN,
824c8945a0SNathan Whitehorn 		 data->height + (2 * MARGIN), data->width + (2 * MARGIN),
832a3e3873SBaptiste Daroussin 		 menubox_border_attr, menubox_border2_attr);
844c8945a0SNathan Whitehorn 
85*f4f33ea0SBaptiste Daroussin     dlg_attrset(data->window, item_attr);
864c8945a0SNathan Whitehorn     wprintw(data->window, "%02d", data->value);
874c8945a0SNathan Whitehorn     return 0;
884c8945a0SNathan Whitehorn }
894c8945a0SNathan Whitehorn 
904c8945a0SNathan Whitehorn static int
914c8945a0SNathan Whitehorn init_object(BOX * data,
924c8945a0SNathan Whitehorn 	    WINDOW *parent,
934c8945a0SNathan Whitehorn 	    int x, int y,
944c8945a0SNathan Whitehorn 	    int width, int height,
954c8945a0SNathan Whitehorn 	    int period, int value,
964c8945a0SNathan Whitehorn 	    int code)
974c8945a0SNathan Whitehorn {
987a1c0d96SNathan Whitehorn     (void) code;
997a1c0d96SNathan Whitehorn 
1004c8945a0SNathan Whitehorn     data->parent = parent;
1014c8945a0SNathan Whitehorn     data->x = x;
1024c8945a0SNathan Whitehorn     data->y = y;
1034c8945a0SNathan Whitehorn     data->width = width;
1044c8945a0SNathan Whitehorn     data->height = height;
1054c8945a0SNathan Whitehorn     data->period = period;
1064c8945a0SNathan Whitehorn     data->value = value % period;
1074c8945a0SNathan Whitehorn 
1084c8945a0SNathan Whitehorn     data->window = derwin(data->parent,
1094c8945a0SNathan Whitehorn 			  data->height, data->width,
1104c8945a0SNathan Whitehorn 			  data->y, data->x);
1114c8945a0SNathan Whitehorn     if (data->window == 0)
1124c8945a0SNathan Whitehorn 	return -1;
1134c8945a0SNathan Whitehorn     (void) keypad(data->window, TRUE);
1144c8945a0SNathan Whitehorn 
1154c8945a0SNathan Whitehorn     dlg_mouse_setbase(getbegx(parent), getbegy(parent));
1164c8945a0SNathan Whitehorn     dlg_mouse_mkregion(y, x, height, width, code);
1174c8945a0SNathan Whitehorn 
1184c8945a0SNathan Whitehorn     return 0;
1194c8945a0SNathan Whitehorn }
1204c8945a0SNathan Whitehorn 
1214c8945a0SNathan Whitehorn static int
1224c8945a0SNathan Whitehorn CleanupResult(int code, WINDOW *dialog, char *prompt, DIALOG_VARS * save_vars)
1234c8945a0SNathan Whitehorn {
1244c8945a0SNathan Whitehorn     dlg_del_window(dialog);
1254c8945a0SNathan Whitehorn     dlg_mouse_free_regions();
1264c8945a0SNathan Whitehorn     free(prompt);
1274c8945a0SNathan Whitehorn     dlg_restore_vars(save_vars);
1284c8945a0SNathan Whitehorn 
1294c8945a0SNathan Whitehorn     return code;
1304c8945a0SNathan Whitehorn }
1314c8945a0SNathan Whitehorn 
1324c8945a0SNathan Whitehorn #define DrawObject(data) draw_cell(data)
1334c8945a0SNathan Whitehorn 
1344c8945a0SNathan Whitehorn /*
1354c8945a0SNathan Whitehorn  * Display a dialog box for entering a date
1364c8945a0SNathan Whitehorn  */
1374c8945a0SNathan Whitehorn int
1384c8945a0SNathan Whitehorn dialog_timebox(const char *title,
1394c8945a0SNathan Whitehorn 	       const char *subtitle,
1404c8945a0SNathan Whitehorn 	       int height,
1414c8945a0SNathan Whitehorn 	       int width,
1424c8945a0SNathan Whitehorn 	       int hour,
1434c8945a0SNathan Whitehorn 	       int minute,
1444c8945a0SNathan Whitehorn 	       int second)
1454c8945a0SNathan Whitehorn {
1464c8945a0SNathan Whitehorn     /* *INDENT-OFF* */
1474c8945a0SNathan Whitehorn     static DLG_KEYS_BINDING binding[] = {
1484c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_DELETE_RIGHT,KEY_DC ),
149682c9e0fSNathan Whitehorn 	HELPKEY_BINDINGS,
1504c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
151*f4f33ea0SBaptiste Daroussin 	TOGGLEKEY_BINDINGS,
1524c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_FIRST,KEY_HOME ),
1534c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_END ),
1544c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_LL ),
1554c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, CHR_NEXT ),
1564c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
1574c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
1584c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_BACKSPACE ),
1594c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_PREVIOUS ),
1604c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
1614c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
1624c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  '+'),
1634c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_DOWN),
1644c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_NEXT),
1654c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_NPAGE),
1664c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  '-' ),
1674c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_PPAGE ),
1684c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_PREVIOUS ),
1694c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_UP ),
1704c8945a0SNathan Whitehorn 	END_KEYS_BINDING
1714c8945a0SNathan Whitehorn     };
1724c8945a0SNathan Whitehorn     /* *INDENT-ON* */
1734c8945a0SNathan Whitehorn 
1744c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
1754c8945a0SNathan Whitehorn     int old_height = height;
1764c8945a0SNathan Whitehorn     int old_width = width;
1774c8945a0SNathan Whitehorn #endif
1784c8945a0SNathan Whitehorn     BOX hr_box, mn_box, sc_box;
1794c8945a0SNathan Whitehorn     int key = 0, key2, fkey;
1804c8945a0SNathan Whitehorn     int button;
1814c8945a0SNathan Whitehorn     int result = DLG_EXIT_UNKNOWN;
1824c8945a0SNathan Whitehorn     WINDOW *dialog;
1834c8945a0SNathan Whitehorn     time_t now_time = time((time_t *) 0);
1844c8945a0SNathan Whitehorn     struct tm current;
1852a3e3873SBaptiste Daroussin     int state = dlg_default_button();
1864c8945a0SNathan Whitehorn     const char **buttons = dlg_ok_labels();
187*f4f33ea0SBaptiste Daroussin     char *prompt;
1884c8945a0SNathan Whitehorn     char buffer[MAX_LEN];
1894c8945a0SNathan Whitehorn     DIALOG_VARS save_vars;
1904c8945a0SNathan Whitehorn 
191*f4f33ea0SBaptiste Daroussin     DLG_TRACE(("# timebox args:\n"));
192*f4f33ea0SBaptiste Daroussin     DLG_TRACE2S("title", title);
193*f4f33ea0SBaptiste Daroussin     DLG_TRACE2S("message", subtitle);
194*f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("height", height);
195*f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("width", width);
196*f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("hour", hour);
197*f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("minute", minute);
198*f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("second", second);
199*f4f33ea0SBaptiste Daroussin 
2004c8945a0SNathan Whitehorn     now_time = time((time_t *) 0);
2014c8945a0SNathan Whitehorn     current = *localtime(&now_time);
2024c8945a0SNathan Whitehorn 
2034c8945a0SNathan Whitehorn     dlg_save_vars(&save_vars);
2044c8945a0SNathan Whitehorn     dialog_vars.separate_output = TRUE;
2054c8945a0SNathan Whitehorn 
2064c8945a0SNathan Whitehorn     dlg_does_output();
2074c8945a0SNathan Whitehorn 
2084c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
2094c8945a0SNathan Whitehorn   retry:
2104c8945a0SNathan Whitehorn #endif
2114c8945a0SNathan Whitehorn 
212*f4f33ea0SBaptiste Daroussin     prompt = dlg_strclone(subtitle);
2134c8945a0SNathan Whitehorn     dlg_auto_size(title, prompt, &height, &width, 0, 0);
214*f4f33ea0SBaptiste Daroussin 
2154c8945a0SNathan Whitehorn     height += MIN_HIGH;
2164c8945a0SNathan Whitehorn     if (width < MIN_WIDE)
2174c8945a0SNathan Whitehorn 	width = MIN_WIDE;
2184c8945a0SNathan Whitehorn     dlg_button_layout(buttons, &width);
2194c8945a0SNathan Whitehorn     dlg_print_size(height, width);
2204c8945a0SNathan Whitehorn     dlg_ctl_size(height, width);
2214c8945a0SNathan Whitehorn 
2224c8945a0SNathan Whitehorn     dialog = dlg_new_window(height, width,
2234c8945a0SNathan Whitehorn 			    dlg_box_y_ordinate(height),
2244c8945a0SNathan Whitehorn 			    dlg_box_x_ordinate(width));
2257a1c0d96SNathan Whitehorn 
2267a1c0d96SNathan Whitehorn     if (hour >= 24 || minute >= 60 || second >= 60) {
2277a1c0d96SNathan Whitehorn 	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
2287a1c0d96SNathan Whitehorn     }
2297a1c0d96SNathan Whitehorn 
2304c8945a0SNathan Whitehorn     dlg_register_window(dialog, "timebox", binding);
2314c8945a0SNathan Whitehorn     dlg_register_buttons(dialog, "timebox", buttons);
2324c8945a0SNathan Whitehorn 
2332a3e3873SBaptiste Daroussin     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
2342a3e3873SBaptiste Daroussin     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
2354c8945a0SNathan Whitehorn     dlg_draw_title(dialog, title);
236682c9e0fSNathan Whitehorn     dlg_draw_helpline(dialog, FALSE);
2374c8945a0SNathan Whitehorn 
238*f4f33ea0SBaptiste Daroussin     dlg_attrset(dialog, dialog_attr);
2394c8945a0SNathan Whitehorn     dlg_print_autowrap(dialog, prompt, height, width);
2404c8945a0SNathan Whitehorn 
2414c8945a0SNathan Whitehorn     /* compute positions of hour, month and year boxes */
2424c8945a0SNathan Whitehorn     memset(&hr_box, 0, sizeof(hr_box));
2434c8945a0SNathan Whitehorn     memset(&mn_box, 0, sizeof(mn_box));
2444c8945a0SNathan Whitehorn     memset(&sc_box, 0, sizeof(sc_box));
2454c8945a0SNathan Whitehorn 
2464c8945a0SNathan Whitehorn     if (init_object(&hr_box,
2474c8945a0SNathan Whitehorn 		    dialog,
2484c8945a0SNathan Whitehorn 		    (width - MIN_WIDE + 1) / 2 + MARGIN,
2494c8945a0SNathan Whitehorn 		    (height - MIN_HIGH + MARGIN),
2504c8945a0SNathan Whitehorn 		    ONE_WIDE,
2514c8945a0SNathan Whitehorn 		    ONE_HIGH,
2524c8945a0SNathan Whitehorn 		    24,
2534c8945a0SNathan Whitehorn 		    hour >= 0 ? hour : current.tm_hour,
2544c8945a0SNathan Whitehorn 		    'H') < 0
2554c8945a0SNathan Whitehorn 	|| DrawObject(&hr_box) < 0) {
2564c8945a0SNathan Whitehorn 	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
2574c8945a0SNathan Whitehorn     }
2584c8945a0SNathan Whitehorn 
2594c8945a0SNathan Whitehorn     mvwprintw(dialog, hr_box.y, hr_box.x + ONE_WIDE + MARGIN, ":");
2604c8945a0SNathan Whitehorn     if (init_object(&mn_box,
2614c8945a0SNathan Whitehorn 		    dialog,
2624c8945a0SNathan Whitehorn 		    hr_box.x + (ONE_WIDE + 2 * MARGIN + 1),
2634c8945a0SNathan Whitehorn 		    hr_box.y,
2644c8945a0SNathan Whitehorn 		    hr_box.width,
2654c8945a0SNathan Whitehorn 		    hr_box.height,
2664c8945a0SNathan Whitehorn 		    60,
2674c8945a0SNathan Whitehorn 		    minute >= 0 ? minute : current.tm_min,
2684c8945a0SNathan Whitehorn 		    'M') < 0
2694c8945a0SNathan Whitehorn 	|| DrawObject(&mn_box) < 0) {
2704c8945a0SNathan Whitehorn 	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
2714c8945a0SNathan Whitehorn     }
2724c8945a0SNathan Whitehorn 
2734c8945a0SNathan Whitehorn     mvwprintw(dialog, mn_box.y, mn_box.x + ONE_WIDE + MARGIN, ":");
2744c8945a0SNathan Whitehorn     if (init_object(&sc_box,
2754c8945a0SNathan Whitehorn 		    dialog,
2764c8945a0SNathan Whitehorn 		    mn_box.x + (ONE_WIDE + 2 * MARGIN + 1),
2774c8945a0SNathan Whitehorn 		    mn_box.y,
2784c8945a0SNathan Whitehorn 		    mn_box.width,
2794c8945a0SNathan Whitehorn 		    mn_box.height,
2804c8945a0SNathan Whitehorn 		    60,
2814c8945a0SNathan Whitehorn 		    second >= 0 ? second : current.tm_sec,
2824c8945a0SNathan Whitehorn 		    'S') < 0
2834c8945a0SNathan Whitehorn 	|| DrawObject(&sc_box) < 0) {
2844c8945a0SNathan Whitehorn 	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
2854c8945a0SNathan Whitehorn     }
2864c8945a0SNathan Whitehorn 
2872a3e3873SBaptiste Daroussin     dlg_trace_win(dialog);
2884c8945a0SNathan Whitehorn     while (result == DLG_EXIT_UNKNOWN) {
2894c8945a0SNathan Whitehorn 	BOX *obj = (state == sHR ? &hr_box
2904c8945a0SNathan Whitehorn 		    : (state == sMN ? &mn_box :
2914c8945a0SNathan Whitehorn 		       (state == sSC ? &sc_box : 0)));
2924c8945a0SNathan Whitehorn 
2934c8945a0SNathan Whitehorn 	button = (state < 0) ? 0 : state;
2944c8945a0SNathan Whitehorn 	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
2954c8945a0SNathan Whitehorn 	if (obj != 0)
2964c8945a0SNathan Whitehorn 	    dlg_set_focus(dialog, obj->window);
2974c8945a0SNathan Whitehorn 
2984c8945a0SNathan Whitehorn 	key = dlg_mouse_wgetch(dialog, &fkey);
2994c8945a0SNathan Whitehorn 	if (dlg_result_key(key, fkey, &result))
3004c8945a0SNathan Whitehorn 	    break;
3014c8945a0SNathan Whitehorn 
3024c8945a0SNathan Whitehorn 	if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
3034c8945a0SNathan Whitehorn 	    result = key2;
3044c8945a0SNathan Whitehorn 	} else {
3054c8945a0SNathan Whitehorn 	    /* handle function-keys */
3064c8945a0SNathan Whitehorn 	    if (fkey) {
3074c8945a0SNathan Whitehorn 		switch (key) {
3084c8945a0SNathan Whitehorn 		case DLGK_MOUSE('H'):
3094c8945a0SNathan Whitehorn 		    state = sHR;
3104c8945a0SNathan Whitehorn 		    break;
3114c8945a0SNathan Whitehorn 		case DLGK_MOUSE('M'):
3124c8945a0SNathan Whitehorn 		    state = sMN;
3134c8945a0SNathan Whitehorn 		    break;
3144c8945a0SNathan Whitehorn 		case DLGK_MOUSE('S'):
3154c8945a0SNathan Whitehorn 		    state = sSC;
3164c8945a0SNathan Whitehorn 		    break;
317*f4f33ea0SBaptiste Daroussin 		case DLGK_TOGGLE:
3184c8945a0SNathan Whitehorn 		case DLGK_ENTER:
3192a3e3873SBaptiste Daroussin 		    result = dlg_ok_buttoncode(button);
3204c8945a0SNathan Whitehorn 		    break;
3214c8945a0SNathan Whitehorn 		case DLGK_FIELD_PREV:
3224c8945a0SNathan Whitehorn 		    state = dlg_prev_ok_buttonindex(state, sHR);
3234c8945a0SNathan Whitehorn 		    break;
3244c8945a0SNathan Whitehorn 		case DLGK_FIELD_NEXT:
3254c8945a0SNathan Whitehorn 		    state = dlg_next_ok_buttonindex(state, sHR);
3264c8945a0SNathan Whitehorn 		    break;
3274c8945a0SNathan Whitehorn 		case DLGK_FIELD_FIRST:
3284c8945a0SNathan Whitehorn 		    if (obj != 0) {
3294c8945a0SNathan Whitehorn 			obj->value = 0;
3304c8945a0SNathan Whitehorn 			(void) DrawObject(obj);
3314c8945a0SNathan Whitehorn 		    }
3324c8945a0SNathan Whitehorn 		    break;
3334c8945a0SNathan Whitehorn 		case DLGK_FIELD_LAST:
3344c8945a0SNathan Whitehorn 		    if (obj != 0) {
3354c8945a0SNathan Whitehorn 			switch (state) {
3364c8945a0SNathan Whitehorn 			case sHR:
3374c8945a0SNathan Whitehorn 			    obj->value = 23;
3384c8945a0SNathan Whitehorn 			    break;
3394c8945a0SNathan Whitehorn 			case sMN:
3404c8945a0SNathan Whitehorn 			case sSC:
3414c8945a0SNathan Whitehorn 			    obj->value = 59;
3424c8945a0SNathan Whitehorn 			    break;
3434c8945a0SNathan Whitehorn 			}
3444c8945a0SNathan Whitehorn 			(void) DrawObject(obj);
3454c8945a0SNathan Whitehorn 		    }
3464c8945a0SNathan Whitehorn 		    break;
3474c8945a0SNathan Whitehorn 		case DLGK_DELETE_RIGHT:
3484c8945a0SNathan Whitehorn 		    if (obj != 0) {
3494c8945a0SNathan Whitehorn 			obj->value /= 10;
3504c8945a0SNathan Whitehorn 			(void) DrawObject(obj);
3514c8945a0SNathan Whitehorn 		    }
3524c8945a0SNathan Whitehorn 		    break;
3534c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
3544c8945a0SNathan Whitehorn 		case KEY_RESIZE:
355*f4f33ea0SBaptiste Daroussin 		    dlg_will_resize(dialog);
3564c8945a0SNathan Whitehorn 		    /* reset data */
3574c8945a0SNathan Whitehorn 		    height = old_height;
3584c8945a0SNathan Whitehorn 		    width = old_width;
3594c8945a0SNathan Whitehorn 		    hour = hr_box.value;
3604c8945a0SNathan Whitehorn 		    minute = mn_box.value;
3614c8945a0SNathan Whitehorn 		    second = sc_box.value;
3624c8945a0SNathan Whitehorn 		    /* repaint */
363*f4f33ea0SBaptiste Daroussin 		    free(prompt);
3644c8945a0SNathan Whitehorn 		    dlg_clear();
3654c8945a0SNathan Whitehorn 		    dlg_del_window(dialog);
3664c8945a0SNathan Whitehorn 		    dlg_mouse_free_regions();
3674c8945a0SNathan Whitehorn 		    goto retry;
3684c8945a0SNathan Whitehorn #endif
3694c8945a0SNathan Whitehorn 		default:
3702a3e3873SBaptiste Daroussin 		    if (is_DLGK_MOUSE(key)) {
3712a3e3873SBaptiste Daroussin 			result = dlg_ok_buttoncode(key - M_EVENT);
3722a3e3873SBaptiste Daroussin 			if (result < 0)
3732a3e3873SBaptiste Daroussin 			    result = DLG_EXIT_OK;
3742a3e3873SBaptiste Daroussin 		    } else if (obj != 0) {
3754c8945a0SNathan Whitehorn 			int step = next_or_previous(key);
3764c8945a0SNathan Whitehorn 			if (step != 0) {
3774c8945a0SNathan Whitehorn 			    obj->value += step;
3784c8945a0SNathan Whitehorn 			    while (obj->value < 0)
3794c8945a0SNathan Whitehorn 				obj->value += obj->period;
3804c8945a0SNathan Whitehorn 			    obj->value %= obj->period;
3814c8945a0SNathan Whitehorn 			    (void) DrawObject(obj);
3824c8945a0SNathan Whitehorn 			}
3834c8945a0SNathan Whitehorn 		    }
3844c8945a0SNathan Whitehorn 		    break;
3854c8945a0SNathan Whitehorn 		}
3864c8945a0SNathan Whitehorn 	    } else if (isdigit(key)) {
3874c8945a0SNathan Whitehorn 		if (obj != 0) {
3884c8945a0SNathan Whitehorn 		    int digit = (key - '0');
3894c8945a0SNathan Whitehorn 		    int value = (obj->value * 10) + digit;
3904c8945a0SNathan Whitehorn 		    if (value < obj->period) {
3914c8945a0SNathan Whitehorn 			obj->value = value;
3924c8945a0SNathan Whitehorn 			(void) DrawObject(obj);
3934c8945a0SNathan Whitehorn 		    } else {
3944c8945a0SNathan Whitehorn 			beep();
3954c8945a0SNathan Whitehorn 		    }
3964c8945a0SNathan Whitehorn 		}
3974c8945a0SNathan Whitehorn 	    } else {
3984c8945a0SNathan Whitehorn 		beep();
3994c8945a0SNathan Whitehorn 	    }
4004c8945a0SNathan Whitehorn 	}
4014c8945a0SNathan Whitehorn     }
4024c8945a0SNathan Whitehorn 
4034c8945a0SNathan Whitehorn #define DefaultFormat(dst, src) \
4044c8945a0SNathan Whitehorn 	sprintf(dst, "%02d:%02d:%02d", \
4054c8945a0SNathan Whitehorn 		hr_box.value, mn_box.value, sc_box.value)
4064c8945a0SNathan Whitehorn 
4074c8945a0SNathan Whitehorn #if defined(HAVE_STRFTIME)
4084c8945a0SNathan Whitehorn     if (dialog_vars.time_format != 0) {
4094c8945a0SNathan Whitehorn 	size_t used;
4104c8945a0SNathan Whitehorn 	time_t now = time((time_t *) 0);
4114c8945a0SNathan Whitehorn 	struct tm *parts = localtime(&now);
4124c8945a0SNathan Whitehorn 
4134c8945a0SNathan Whitehorn 	parts->tm_sec = sc_box.value;
4144c8945a0SNathan Whitehorn 	parts->tm_min = mn_box.value;
4154c8945a0SNathan Whitehorn 	parts->tm_hour = hr_box.value;
4164c8945a0SNathan Whitehorn 	used = strftime(buffer,
4174c8945a0SNathan Whitehorn 			sizeof(buffer) - 1,
4184c8945a0SNathan Whitehorn 			dialog_vars.time_format,
4194c8945a0SNathan Whitehorn 			parts);
4204c8945a0SNathan Whitehorn 	if (used == 0 || *buffer == '\0')
4214c8945a0SNathan Whitehorn 	    DefaultFormat(buffer, hr_box);
4224c8945a0SNathan Whitehorn     } else
4234c8945a0SNathan Whitehorn #endif
4244c8945a0SNathan Whitehorn 	DefaultFormat(buffer, hr_box);
4254c8945a0SNathan Whitehorn 
4264c8945a0SNathan Whitehorn     dlg_add_result(buffer);
4274c8945a0SNathan Whitehorn     dlg_add_separator();
4282a3e3873SBaptiste Daroussin     dlg_add_last_key(-1);
4294c8945a0SNathan Whitehorn 
4304c8945a0SNathan Whitehorn     return CleanupResult(result, dialog, prompt, &save_vars);
4314c8945a0SNathan Whitehorn }
432