xref: /freebsd/contrib/dialog/timebox.c (revision a96ef4501919d7ac08e94e98dc34b0bdd744802b)
14c8945a0SNathan Whitehorn /*
2*a96ef450SBaptiste Daroussin  * $Id: timebox.c,v 1.69 2020/11/23 09:04:00 tom Exp $
34c8945a0SNathan Whitehorn  *
44c8945a0SNathan Whitehorn  *  timebox.c -- implements the timebox dialog
54c8945a0SNathan Whitehorn  *
6*a96ef450SBaptiste Daroussin  *  Copyright 2001-2019,2020   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 
24*a96ef450SBaptiste Daroussin #include <dlg_internals.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
next_or_previous(int key)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
draw_cell(BOX * data)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 
85f4f33ea0SBaptiste 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
init_object(BOX * data,WINDOW * parent,int x,int y,int width,int height,int period,int value,int code)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 
108*a96ef450SBaptiste Daroussin     data->window = dlg_der_window(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 
1144c8945a0SNathan Whitehorn     dlg_mouse_setbase(getbegx(parent), getbegy(parent));
1154c8945a0SNathan Whitehorn     dlg_mouse_mkregion(y, x, height, width, code);
1164c8945a0SNathan Whitehorn 
1174c8945a0SNathan Whitehorn     return 0;
1184c8945a0SNathan Whitehorn }
1194c8945a0SNathan Whitehorn 
1204c8945a0SNathan Whitehorn static int
CleanupResult(int code,WINDOW * dialog,char * prompt,DIALOG_VARS * save_vars)1214c8945a0SNathan Whitehorn CleanupResult(int code, WINDOW *dialog, char *prompt, DIALOG_VARS * save_vars)
1224c8945a0SNathan Whitehorn {
1234c8945a0SNathan Whitehorn     dlg_del_window(dialog);
1244c8945a0SNathan Whitehorn     dlg_mouse_free_regions();
1254c8945a0SNathan Whitehorn     free(prompt);
1264c8945a0SNathan Whitehorn     dlg_restore_vars(save_vars);
1274c8945a0SNathan Whitehorn 
1284c8945a0SNathan Whitehorn     return code;
1294c8945a0SNathan Whitehorn }
1304c8945a0SNathan Whitehorn 
1314c8945a0SNathan Whitehorn #define DrawObject(data) draw_cell(data)
1324c8945a0SNathan Whitehorn 
1334c8945a0SNathan Whitehorn /*
1344c8945a0SNathan Whitehorn  * Display a dialog box for entering a date
1354c8945a0SNathan Whitehorn  */
1364c8945a0SNathan Whitehorn int
dialog_timebox(const char * title,const char * subtitle,int height,int width,int hour,int minute,int second)1374c8945a0SNathan Whitehorn dialog_timebox(const char *title,
1384c8945a0SNathan Whitehorn 	       const char *subtitle,
1394c8945a0SNathan Whitehorn 	       int height,
1404c8945a0SNathan Whitehorn 	       int width,
1414c8945a0SNathan Whitehorn 	       int hour,
1424c8945a0SNathan Whitehorn 	       int minute,
1434c8945a0SNathan Whitehorn 	       int second)
1444c8945a0SNathan Whitehorn {
1454c8945a0SNathan Whitehorn     /* *INDENT-OFF* */
1464c8945a0SNathan Whitehorn     static DLG_KEYS_BINDING binding[] = {
1474c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_DELETE_RIGHT,KEY_DC ),
148682c9e0fSNathan Whitehorn 	HELPKEY_BINDINGS,
1494c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
150f4f33ea0SBaptiste Daroussin 	TOGGLEKEY_BINDINGS,
1514c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_FIRST,KEY_HOME ),
1524c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_END ),
1534c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_LL ),
1544c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, CHR_NEXT ),
1554c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
1564c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
1574c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_BACKSPACE ),
1584c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_PREVIOUS ),
1594c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
1604c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
1614c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  '+'),
1624c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_DOWN),
1634c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_NEXT),
1644c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_NPAGE),
1654c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  '-' ),
1664c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_PPAGE ),
1674c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_PREVIOUS ),
1684c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_UP ),
1694c8945a0SNathan Whitehorn 	END_KEYS_BINDING
1704c8945a0SNathan Whitehorn     };
1714c8945a0SNathan Whitehorn     /* *INDENT-ON* */
1724c8945a0SNathan Whitehorn 
1734c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
1744c8945a0SNathan Whitehorn     int old_height = height;
1754c8945a0SNathan Whitehorn     int old_width = width;
1764c8945a0SNathan Whitehorn #endif
1774c8945a0SNathan Whitehorn     BOX hr_box, mn_box, sc_box;
178*a96ef450SBaptiste Daroussin     int key, fkey;
1794c8945a0SNathan Whitehorn     int button;
1804c8945a0SNathan Whitehorn     int result = DLG_EXIT_UNKNOWN;
1814c8945a0SNathan Whitehorn     WINDOW *dialog;
182*a96ef450SBaptiste Daroussin     time_t now_time;
1834c8945a0SNathan Whitehorn     struct tm current;
1842a3e3873SBaptiste Daroussin     int state = dlg_default_button();
1854c8945a0SNathan Whitehorn     const char **buttons = dlg_ok_labels();
186f4f33ea0SBaptiste Daroussin     char *prompt;
1874c8945a0SNathan Whitehorn     char buffer[MAX_LEN];
1884c8945a0SNathan Whitehorn     DIALOG_VARS save_vars;
1894c8945a0SNathan Whitehorn 
190f4f33ea0SBaptiste Daroussin     DLG_TRACE(("# timebox args:\n"));
191f4f33ea0SBaptiste Daroussin     DLG_TRACE2S("title", title);
192f4f33ea0SBaptiste Daroussin     DLG_TRACE2S("message", subtitle);
193f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("height", height);
194f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("width", width);
195f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("hour", hour);
196f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("minute", minute);
197f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("second", second);
198f4f33ea0SBaptiste Daroussin 
1994c8945a0SNathan Whitehorn     now_time = time((time_t *) 0);
2004c8945a0SNathan Whitehorn     current = *localtime(&now_time);
2014c8945a0SNathan Whitehorn 
2024c8945a0SNathan Whitehorn     dlg_save_vars(&save_vars);
2034c8945a0SNathan Whitehorn     dialog_vars.separate_output = TRUE;
2044c8945a0SNathan Whitehorn 
2054c8945a0SNathan Whitehorn     dlg_does_output();
2064c8945a0SNathan Whitehorn 
2074c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
2084c8945a0SNathan Whitehorn   retry:
2094c8945a0SNathan Whitehorn #endif
2104c8945a0SNathan Whitehorn 
211f4f33ea0SBaptiste Daroussin     prompt = dlg_strclone(subtitle);
212*a96ef450SBaptiste Daroussin     dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE);
213f4f33ea0SBaptiste Daroussin 
2144c8945a0SNathan Whitehorn     dlg_button_layout(buttons, &width);
2154c8945a0SNathan Whitehorn     dlg_print_size(height, width);
2164c8945a0SNathan Whitehorn     dlg_ctl_size(height, width);
2174c8945a0SNathan Whitehorn 
2184c8945a0SNathan Whitehorn     dialog = dlg_new_window(height, width,
2194c8945a0SNathan Whitehorn 			    dlg_box_y_ordinate(height),
2204c8945a0SNathan Whitehorn 			    dlg_box_x_ordinate(width));
2217a1c0d96SNathan Whitehorn 
2227a1c0d96SNathan Whitehorn     if (hour >= 24 || minute >= 60 || second >= 60) {
2237a1c0d96SNathan Whitehorn 	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
2247a1c0d96SNathan Whitehorn     }
2257a1c0d96SNathan Whitehorn 
2264c8945a0SNathan Whitehorn     dlg_register_window(dialog, "timebox", binding);
2274c8945a0SNathan Whitehorn     dlg_register_buttons(dialog, "timebox", buttons);
2284c8945a0SNathan Whitehorn 
2292a3e3873SBaptiste Daroussin     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
2302a3e3873SBaptiste Daroussin     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
2314c8945a0SNathan Whitehorn     dlg_draw_title(dialog, title);
232682c9e0fSNathan Whitehorn     dlg_draw_helpline(dialog, FALSE);
2334c8945a0SNathan Whitehorn 
234f4f33ea0SBaptiste Daroussin     dlg_attrset(dialog, dialog_attr);
2354c8945a0SNathan Whitehorn     dlg_print_autowrap(dialog, prompt, height, width);
2364c8945a0SNathan Whitehorn 
2374c8945a0SNathan Whitehorn     /* compute positions of hour, month and year boxes */
2384c8945a0SNathan Whitehorn     memset(&hr_box, 0, sizeof(hr_box));
2394c8945a0SNathan Whitehorn     memset(&mn_box, 0, sizeof(mn_box));
2404c8945a0SNathan Whitehorn     memset(&sc_box, 0, sizeof(sc_box));
2414c8945a0SNathan Whitehorn 
2424c8945a0SNathan Whitehorn     if (init_object(&hr_box,
2434c8945a0SNathan Whitehorn 		    dialog,
2444c8945a0SNathan Whitehorn 		    (width - MIN_WIDE + 1) / 2 + MARGIN,
2454c8945a0SNathan Whitehorn 		    (height - MIN_HIGH + MARGIN),
2464c8945a0SNathan Whitehorn 		    ONE_WIDE,
2474c8945a0SNathan Whitehorn 		    ONE_HIGH,
2484c8945a0SNathan Whitehorn 		    24,
2494c8945a0SNathan Whitehorn 		    hour >= 0 ? hour : current.tm_hour,
2504c8945a0SNathan Whitehorn 		    'H') < 0
2514c8945a0SNathan Whitehorn 	|| DrawObject(&hr_box) < 0) {
2524c8945a0SNathan Whitehorn 	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
2534c8945a0SNathan Whitehorn     }
2544c8945a0SNathan Whitehorn 
2554c8945a0SNathan Whitehorn     mvwprintw(dialog, hr_box.y, hr_box.x + ONE_WIDE + MARGIN, ":");
2564c8945a0SNathan Whitehorn     if (init_object(&mn_box,
2574c8945a0SNathan Whitehorn 		    dialog,
2584c8945a0SNathan Whitehorn 		    hr_box.x + (ONE_WIDE + 2 * MARGIN + 1),
2594c8945a0SNathan Whitehorn 		    hr_box.y,
2604c8945a0SNathan Whitehorn 		    hr_box.width,
2614c8945a0SNathan Whitehorn 		    hr_box.height,
2624c8945a0SNathan Whitehorn 		    60,
2634c8945a0SNathan Whitehorn 		    minute >= 0 ? minute : current.tm_min,
2644c8945a0SNathan Whitehorn 		    'M') < 0
2654c8945a0SNathan Whitehorn 	|| DrawObject(&mn_box) < 0) {
2664c8945a0SNathan Whitehorn 	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
2674c8945a0SNathan Whitehorn     }
2684c8945a0SNathan Whitehorn 
2694c8945a0SNathan Whitehorn     mvwprintw(dialog, mn_box.y, mn_box.x + ONE_WIDE + MARGIN, ":");
2704c8945a0SNathan Whitehorn     if (init_object(&sc_box,
2714c8945a0SNathan Whitehorn 		    dialog,
2724c8945a0SNathan Whitehorn 		    mn_box.x + (ONE_WIDE + 2 * MARGIN + 1),
2734c8945a0SNathan Whitehorn 		    mn_box.y,
2744c8945a0SNathan Whitehorn 		    mn_box.width,
2754c8945a0SNathan Whitehorn 		    mn_box.height,
2764c8945a0SNathan Whitehorn 		    60,
2774c8945a0SNathan Whitehorn 		    second >= 0 ? second : current.tm_sec,
2784c8945a0SNathan Whitehorn 		    'S') < 0
2794c8945a0SNathan Whitehorn 	|| DrawObject(&sc_box) < 0) {
2804c8945a0SNathan Whitehorn 	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
2814c8945a0SNathan Whitehorn     }
2824c8945a0SNathan Whitehorn 
2832a3e3873SBaptiste Daroussin     dlg_trace_win(dialog);
2844c8945a0SNathan Whitehorn     while (result == DLG_EXIT_UNKNOWN) {
2854c8945a0SNathan Whitehorn 	BOX *obj = (state == sHR ? &hr_box
2864c8945a0SNathan Whitehorn 		    : (state == sMN ? &mn_box :
2874c8945a0SNathan Whitehorn 		       (state == sSC ? &sc_box : 0)));
288*a96ef450SBaptiste Daroussin 	int key2;
2894c8945a0SNathan Whitehorn 
2904c8945a0SNathan Whitehorn 	button = (state < 0) ? 0 : state;
2914c8945a0SNathan Whitehorn 	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
2924c8945a0SNathan Whitehorn 	if (obj != 0)
2934c8945a0SNathan Whitehorn 	    dlg_set_focus(dialog, obj->window);
2944c8945a0SNathan Whitehorn 
2954c8945a0SNathan Whitehorn 	key = dlg_mouse_wgetch(dialog, &fkey);
296*a96ef450SBaptiste Daroussin 	if (dlg_result_key(key, fkey, &result)) {
297*a96ef450SBaptiste Daroussin 	    if (!dlg_button_key(result, &button, &key, &fkey))
2984c8945a0SNathan Whitehorn 		break;
299*a96ef450SBaptiste Daroussin 	}
3004c8945a0SNathan Whitehorn 
3014c8945a0SNathan Whitehorn 	if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
3024c8945a0SNathan Whitehorn 	    result = key2;
3034c8945a0SNathan Whitehorn 	} else {
3044c8945a0SNathan Whitehorn 	    /* handle function-keys */
3054c8945a0SNathan Whitehorn 	    if (fkey) {
3064c8945a0SNathan Whitehorn 		switch (key) {
3074c8945a0SNathan Whitehorn 		case DLGK_MOUSE('H'):
3084c8945a0SNathan Whitehorn 		    state = sHR;
3094c8945a0SNathan Whitehorn 		    break;
3104c8945a0SNathan Whitehorn 		case DLGK_MOUSE('M'):
3114c8945a0SNathan Whitehorn 		    state = sMN;
3124c8945a0SNathan Whitehorn 		    break;
3134c8945a0SNathan Whitehorn 		case DLGK_MOUSE('S'):
3144c8945a0SNathan Whitehorn 		    state = sSC;
3154c8945a0SNathan Whitehorn 		    break;
316f4f33ea0SBaptiste Daroussin 		case DLGK_TOGGLE:
3174c8945a0SNathan Whitehorn 		case DLGK_ENTER:
318*a96ef450SBaptiste Daroussin 		    result = dlg_enter_buttoncode(button);
319*a96ef450SBaptiste Daroussin 		    break;
320*a96ef450SBaptiste Daroussin 		case DLGK_LEAVE:
3212a3e3873SBaptiste Daroussin 		    result = dlg_ok_buttoncode(button);
3224c8945a0SNathan Whitehorn 		    break;
3234c8945a0SNathan Whitehorn 		case DLGK_FIELD_PREV:
3244c8945a0SNathan Whitehorn 		    state = dlg_prev_ok_buttonindex(state, sHR);
3254c8945a0SNathan Whitehorn 		    break;
3264c8945a0SNathan Whitehorn 		case DLGK_FIELD_NEXT:
3274c8945a0SNathan Whitehorn 		    state = dlg_next_ok_buttonindex(state, sHR);
3284c8945a0SNathan Whitehorn 		    break;
3294c8945a0SNathan Whitehorn 		case DLGK_FIELD_FIRST:
3304c8945a0SNathan Whitehorn 		    if (obj != 0) {
3314c8945a0SNathan Whitehorn 			obj->value = 0;
3324c8945a0SNathan Whitehorn 			(void) DrawObject(obj);
3334c8945a0SNathan Whitehorn 		    }
3344c8945a0SNathan Whitehorn 		    break;
3354c8945a0SNathan Whitehorn 		case DLGK_FIELD_LAST:
3364c8945a0SNathan Whitehorn 		    if (obj != 0) {
3374c8945a0SNathan Whitehorn 			switch (state) {
3384c8945a0SNathan Whitehorn 			case sHR:
3394c8945a0SNathan Whitehorn 			    obj->value = 23;
3404c8945a0SNathan Whitehorn 			    break;
3414c8945a0SNathan Whitehorn 			case sMN:
3424c8945a0SNathan Whitehorn 			case sSC:
3434c8945a0SNathan Whitehorn 			    obj->value = 59;
3444c8945a0SNathan Whitehorn 			    break;
3454c8945a0SNathan Whitehorn 			}
3464c8945a0SNathan Whitehorn 			(void) DrawObject(obj);
3474c8945a0SNathan Whitehorn 		    }
3484c8945a0SNathan Whitehorn 		    break;
3494c8945a0SNathan Whitehorn 		case DLGK_DELETE_RIGHT:
3504c8945a0SNathan Whitehorn 		    if (obj != 0) {
3514c8945a0SNathan Whitehorn 			obj->value /= 10;
3524c8945a0SNathan Whitehorn 			(void) DrawObject(obj);
3534c8945a0SNathan Whitehorn 		    }
3544c8945a0SNathan Whitehorn 		    break;
3554c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
3564c8945a0SNathan Whitehorn 		case KEY_RESIZE:
357f4f33ea0SBaptiste Daroussin 		    dlg_will_resize(dialog);
3584c8945a0SNathan Whitehorn 		    /* reset data */
3594c8945a0SNathan Whitehorn 		    height = old_height;
3604c8945a0SNathan Whitehorn 		    width = old_width;
3614c8945a0SNathan Whitehorn 		    hour = hr_box.value;
3624c8945a0SNathan Whitehorn 		    minute = mn_box.value;
3634c8945a0SNathan Whitehorn 		    second = sc_box.value;
3644c8945a0SNathan Whitehorn 		    /* repaint */
365f4f33ea0SBaptiste Daroussin 		    free(prompt);
366*a96ef450SBaptiste Daroussin 		    _dlg_resize_cleanup(dialog);
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 		}
397*a96ef450SBaptiste Daroussin 	    } else if (key > 0) {
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);
427*a96ef450SBaptiste Daroussin     AddLastKey();
4284c8945a0SNathan Whitehorn 
4294c8945a0SNathan Whitehorn     return CleanupResult(result, dialog, prompt, &save_vars);
4304c8945a0SNathan Whitehorn }
431