xref: /freebsd/contrib/dialog/timebox.c (revision 4c8945a06b01a5c8122cdeb402af36bb46a06acc)
1*4c8945a0SNathan Whitehorn /*
2*4c8945a0SNathan Whitehorn  * $Id: timebox.c,v 1.41 2010/01/18 10:33:42 tom Exp $
3*4c8945a0SNathan Whitehorn  *
4*4c8945a0SNathan Whitehorn  *  timebox.c -- implements the timebox dialog
5*4c8945a0SNathan Whitehorn  *
6*4c8945a0SNathan Whitehorn  *  Copyright 2001-2009,2010   Thomas E. Dickey
7*4c8945a0SNathan Whitehorn  *
8*4c8945a0SNathan Whitehorn  *  This program is free software; you can redistribute it and/or modify
9*4c8945a0SNathan Whitehorn  *  it under the terms of the GNU Lesser General Public License, version 2.1
10*4c8945a0SNathan Whitehorn  *  as published by the Free Software Foundation.
11*4c8945a0SNathan Whitehorn  *
12*4c8945a0SNathan Whitehorn  *  This program is distributed in the hope that it will be useful, but
13*4c8945a0SNathan Whitehorn  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14*4c8945a0SNathan Whitehorn  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15*4c8945a0SNathan Whitehorn  *  Lesser General Public License for more details.
16*4c8945a0SNathan Whitehorn  *
17*4c8945a0SNathan Whitehorn  *  You should have received a copy of the GNU Lesser General Public
18*4c8945a0SNathan Whitehorn  *  License along with this program; if not, write to
19*4c8945a0SNathan Whitehorn  *	Free Software Foundation, Inc.
20*4c8945a0SNathan Whitehorn  *	51 Franklin St., Fifth Floor
21*4c8945a0SNathan Whitehorn  *	Boston, MA 02110, USA.
22*4c8945a0SNathan Whitehorn  */
23*4c8945a0SNathan Whitehorn 
24*4c8945a0SNathan Whitehorn #include <dialog.h>
25*4c8945a0SNathan Whitehorn #include <dlg_keys.h>
26*4c8945a0SNathan Whitehorn 
27*4c8945a0SNathan Whitehorn #include <time.h>
28*4c8945a0SNathan Whitehorn 
29*4c8945a0SNathan Whitehorn #define ONE_HIGH 1
30*4c8945a0SNathan Whitehorn #define ONE_WIDE 2
31*4c8945a0SNathan Whitehorn #define BTN_HIGH 2
32*4c8945a0SNathan Whitehorn 
33*4c8945a0SNathan Whitehorn #define MIN_HIGH (ONE_HIGH + BTN_HIGH + (4 * MARGIN))
34*4c8945a0SNathan Whitehorn #define MIN_WIDE ((3 * (ONE_WIDE + 2 * MARGIN)) + 2 + (2 * MARGIN))
35*4c8945a0SNathan Whitehorn 
36*4c8945a0SNathan Whitehorn typedef enum {
37*4c8945a0SNathan Whitehorn     sHR = -3
38*4c8945a0SNathan Whitehorn     ,sMN = -2
39*4c8945a0SNathan Whitehorn     ,sSC = -1
40*4c8945a0SNathan Whitehorn } STATES;
41*4c8945a0SNathan Whitehorn 
42*4c8945a0SNathan Whitehorn struct _box;
43*4c8945a0SNathan Whitehorn 
44*4c8945a0SNathan Whitehorn typedef int (*BOX_DRAW) (struct _box *, struct tm *);
45*4c8945a0SNathan Whitehorn 
46*4c8945a0SNathan Whitehorn typedef struct _box {
47*4c8945a0SNathan Whitehorn     WINDOW *parent;
48*4c8945a0SNathan Whitehorn     WINDOW *window;
49*4c8945a0SNathan Whitehorn     int x;
50*4c8945a0SNathan Whitehorn     int y;
51*4c8945a0SNathan Whitehorn     int width;
52*4c8945a0SNathan Whitehorn     int height;
53*4c8945a0SNathan Whitehorn     int period;
54*4c8945a0SNathan Whitehorn     int value;
55*4c8945a0SNathan Whitehorn } BOX;
56*4c8945a0SNathan Whitehorn 
57*4c8945a0SNathan Whitehorn static int
58*4c8945a0SNathan Whitehorn next_or_previous(int key)
59*4c8945a0SNathan Whitehorn {
60*4c8945a0SNathan Whitehorn     int result = 0;
61*4c8945a0SNathan Whitehorn 
62*4c8945a0SNathan Whitehorn     switch (key) {
63*4c8945a0SNathan Whitehorn     case DLGK_ITEM_PREV:
64*4c8945a0SNathan Whitehorn 	result = -1;
65*4c8945a0SNathan Whitehorn 	break;
66*4c8945a0SNathan Whitehorn     case DLGK_ITEM_NEXT:
67*4c8945a0SNathan Whitehorn 	result = 1;
68*4c8945a0SNathan Whitehorn 	break;
69*4c8945a0SNathan Whitehorn     default:
70*4c8945a0SNathan Whitehorn 	beep();
71*4c8945a0SNathan Whitehorn 	break;
72*4c8945a0SNathan Whitehorn     }
73*4c8945a0SNathan Whitehorn     return result;
74*4c8945a0SNathan Whitehorn }
75*4c8945a0SNathan Whitehorn /*
76*4c8945a0SNathan Whitehorn  * Draw the hour-of-month selection box
77*4c8945a0SNathan Whitehorn  */
78*4c8945a0SNathan Whitehorn static int
79*4c8945a0SNathan Whitehorn draw_cell(BOX * data)
80*4c8945a0SNathan Whitehorn {
81*4c8945a0SNathan Whitehorn     werase(data->window);
82*4c8945a0SNathan Whitehorn     dlg_draw_box(data->parent,
83*4c8945a0SNathan Whitehorn 		 data->y - MARGIN, data->x - MARGIN,
84*4c8945a0SNathan Whitehorn 		 data->height + (2 * MARGIN), data->width + (2 * MARGIN),
85*4c8945a0SNathan Whitehorn 		 menubox_border_attr, menubox_attr);
86*4c8945a0SNathan Whitehorn 
87*4c8945a0SNathan Whitehorn     wattrset(data->window, item_attr);
88*4c8945a0SNathan Whitehorn     wprintw(data->window, "%02d", data->value);
89*4c8945a0SNathan Whitehorn     return 0;
90*4c8945a0SNathan Whitehorn }
91*4c8945a0SNathan Whitehorn 
92*4c8945a0SNathan Whitehorn static int
93*4c8945a0SNathan Whitehorn init_object(BOX * data,
94*4c8945a0SNathan Whitehorn 	    WINDOW *parent,
95*4c8945a0SNathan Whitehorn 	    int x, int y,
96*4c8945a0SNathan Whitehorn 	    int width, int height,
97*4c8945a0SNathan Whitehorn 	    int period, int value,
98*4c8945a0SNathan Whitehorn 	    int code)
99*4c8945a0SNathan Whitehorn {
100*4c8945a0SNathan Whitehorn     data->parent = parent;
101*4c8945a0SNathan Whitehorn     data->x = x;
102*4c8945a0SNathan Whitehorn     data->y = y;
103*4c8945a0SNathan Whitehorn     data->width = width;
104*4c8945a0SNathan Whitehorn     data->height = height;
105*4c8945a0SNathan Whitehorn     data->period = period;
106*4c8945a0SNathan Whitehorn     data->value = value % period;
107*4c8945a0SNathan Whitehorn 
108*4c8945a0SNathan Whitehorn     data->window = derwin(data->parent,
109*4c8945a0SNathan Whitehorn 			  data->height, data->width,
110*4c8945a0SNathan Whitehorn 			  data->y, data->x);
111*4c8945a0SNathan Whitehorn     if (data->window == 0)
112*4c8945a0SNathan Whitehorn 	return -1;
113*4c8945a0SNathan Whitehorn     (void) keypad(data->window, TRUE);
114*4c8945a0SNathan Whitehorn 
115*4c8945a0SNathan Whitehorn     dlg_mouse_setbase(getbegx(parent), getbegy(parent));
116*4c8945a0SNathan Whitehorn     dlg_mouse_mkregion(y, x, height, width, code);
117*4c8945a0SNathan Whitehorn 
118*4c8945a0SNathan Whitehorn     return 0;
119*4c8945a0SNathan Whitehorn }
120*4c8945a0SNathan Whitehorn 
121*4c8945a0SNathan Whitehorn static int
122*4c8945a0SNathan Whitehorn CleanupResult(int code, WINDOW *dialog, char *prompt, DIALOG_VARS * save_vars)
123*4c8945a0SNathan Whitehorn {
124*4c8945a0SNathan Whitehorn     dlg_del_window(dialog);
125*4c8945a0SNathan Whitehorn     dlg_mouse_free_regions();
126*4c8945a0SNathan Whitehorn     free(prompt);
127*4c8945a0SNathan Whitehorn     dlg_restore_vars(save_vars);
128*4c8945a0SNathan Whitehorn 
129*4c8945a0SNathan Whitehorn     return code;
130*4c8945a0SNathan Whitehorn }
131*4c8945a0SNathan Whitehorn 
132*4c8945a0SNathan Whitehorn #define DrawObject(data) draw_cell(data)
133*4c8945a0SNathan Whitehorn 
134*4c8945a0SNathan Whitehorn /*
135*4c8945a0SNathan Whitehorn  * Display a dialog box for entering a date
136*4c8945a0SNathan Whitehorn  */
137*4c8945a0SNathan Whitehorn int
138*4c8945a0SNathan Whitehorn dialog_timebox(const char *title,
139*4c8945a0SNathan Whitehorn 	       const char *subtitle,
140*4c8945a0SNathan Whitehorn 	       int height,
141*4c8945a0SNathan Whitehorn 	       int width,
142*4c8945a0SNathan Whitehorn 	       int hour,
143*4c8945a0SNathan Whitehorn 	       int minute,
144*4c8945a0SNathan Whitehorn 	       int second)
145*4c8945a0SNathan Whitehorn {
146*4c8945a0SNathan Whitehorn     /* *INDENT-OFF* */
147*4c8945a0SNathan Whitehorn     static DLG_KEYS_BINDING binding[] = {
148*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_DELETE_RIGHT,KEY_DC ),
149*4c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
150*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ENTER,	' ' ),
151*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_FIRST,KEY_HOME ),
152*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_END ),
153*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_LL ),
154*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, CHR_NEXT ),
155*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
156*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
157*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_BACKSPACE ),
158*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_PREVIOUS ),
159*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
160*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
161*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  '+'),
162*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_DOWN),
163*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_NEXT),
164*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_NPAGE),
165*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  '-' ),
166*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_PPAGE ),
167*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_PREVIOUS ),
168*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_UP ),
169*4c8945a0SNathan Whitehorn 	END_KEYS_BINDING
170*4c8945a0SNathan Whitehorn     };
171*4c8945a0SNathan Whitehorn     /* *INDENT-ON* */
172*4c8945a0SNathan Whitehorn 
173*4c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
174*4c8945a0SNathan Whitehorn     int old_height = height;
175*4c8945a0SNathan Whitehorn     int old_width = width;
176*4c8945a0SNathan Whitehorn #endif
177*4c8945a0SNathan Whitehorn     BOX hr_box, mn_box, sc_box;
178*4c8945a0SNathan Whitehorn     int key = 0, key2, fkey;
179*4c8945a0SNathan Whitehorn     int button;
180*4c8945a0SNathan Whitehorn     int result = DLG_EXIT_UNKNOWN;
181*4c8945a0SNathan Whitehorn     WINDOW *dialog;
182*4c8945a0SNathan Whitehorn     time_t now_time = time((time_t *) 0);
183*4c8945a0SNathan Whitehorn     struct tm current;
184*4c8945a0SNathan Whitehorn     int state = dlg_defaultno_button();
185*4c8945a0SNathan Whitehorn     const char **buttons = dlg_ok_labels();
186*4c8945a0SNathan Whitehorn     char *prompt = dlg_strclone(subtitle);
187*4c8945a0SNathan Whitehorn     char buffer[MAX_LEN];
188*4c8945a0SNathan Whitehorn     DIALOG_VARS save_vars;
189*4c8945a0SNathan Whitehorn 
190*4c8945a0SNathan Whitehorn     now_time = time((time_t *) 0);
191*4c8945a0SNathan Whitehorn     current = *localtime(&now_time);
192*4c8945a0SNathan Whitehorn 
193*4c8945a0SNathan Whitehorn     dlg_save_vars(&save_vars);
194*4c8945a0SNathan Whitehorn     dialog_vars.separate_output = TRUE;
195*4c8945a0SNathan Whitehorn 
196*4c8945a0SNathan Whitehorn     dlg_does_output();
197*4c8945a0SNathan Whitehorn 
198*4c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
199*4c8945a0SNathan Whitehorn   retry:
200*4c8945a0SNathan Whitehorn #endif
201*4c8945a0SNathan Whitehorn 
202*4c8945a0SNathan Whitehorn     dlg_auto_size(title, prompt, &height, &width, 0, 0);
203*4c8945a0SNathan Whitehorn     height += MIN_HIGH;
204*4c8945a0SNathan Whitehorn     if (width < MIN_WIDE)
205*4c8945a0SNathan Whitehorn 	width = MIN_WIDE;
206*4c8945a0SNathan Whitehorn     dlg_button_layout(buttons, &width);
207*4c8945a0SNathan Whitehorn     dlg_print_size(height, width);
208*4c8945a0SNathan Whitehorn     dlg_ctl_size(height, width);
209*4c8945a0SNathan Whitehorn 
210*4c8945a0SNathan Whitehorn     dialog = dlg_new_window(height, width,
211*4c8945a0SNathan Whitehorn 			    dlg_box_y_ordinate(height),
212*4c8945a0SNathan Whitehorn 			    dlg_box_x_ordinate(width));
213*4c8945a0SNathan Whitehorn     dlg_register_window(dialog, "timebox", binding);
214*4c8945a0SNathan Whitehorn     dlg_register_buttons(dialog, "timebox", buttons);
215*4c8945a0SNathan Whitehorn 
216*4c8945a0SNathan Whitehorn     dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
217*4c8945a0SNathan Whitehorn     dlg_draw_bottom_box(dialog);
218*4c8945a0SNathan Whitehorn     dlg_draw_title(dialog, title);
219*4c8945a0SNathan Whitehorn 
220*4c8945a0SNathan Whitehorn     wattrset(dialog, dialog_attr);
221*4c8945a0SNathan Whitehorn     dlg_print_autowrap(dialog, prompt, height, width);
222*4c8945a0SNathan Whitehorn 
223*4c8945a0SNathan Whitehorn     /* compute positions of hour, month and year boxes */
224*4c8945a0SNathan Whitehorn     memset(&hr_box, 0, sizeof(hr_box));
225*4c8945a0SNathan Whitehorn     memset(&mn_box, 0, sizeof(mn_box));
226*4c8945a0SNathan Whitehorn     memset(&sc_box, 0, sizeof(sc_box));
227*4c8945a0SNathan Whitehorn 
228*4c8945a0SNathan Whitehorn     if (init_object(&hr_box,
229*4c8945a0SNathan Whitehorn 		    dialog,
230*4c8945a0SNathan Whitehorn 		    (width - MIN_WIDE + 1) / 2 + MARGIN,
231*4c8945a0SNathan Whitehorn 		    (height - MIN_HIGH + MARGIN),
232*4c8945a0SNathan Whitehorn 		    ONE_WIDE,
233*4c8945a0SNathan Whitehorn 		    ONE_HIGH,
234*4c8945a0SNathan Whitehorn 		    24,
235*4c8945a0SNathan Whitehorn 		    hour >= 0 ? hour : current.tm_hour,
236*4c8945a0SNathan Whitehorn 		    'H') < 0
237*4c8945a0SNathan Whitehorn 	|| DrawObject(&hr_box) < 0) {
238*4c8945a0SNathan Whitehorn 	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
239*4c8945a0SNathan Whitehorn     }
240*4c8945a0SNathan Whitehorn 
241*4c8945a0SNathan Whitehorn     mvwprintw(dialog, hr_box.y, hr_box.x + ONE_WIDE + MARGIN, ":");
242*4c8945a0SNathan Whitehorn     if (init_object(&mn_box,
243*4c8945a0SNathan Whitehorn 		    dialog,
244*4c8945a0SNathan Whitehorn 		    hr_box.x + (ONE_WIDE + 2 * MARGIN + 1),
245*4c8945a0SNathan Whitehorn 		    hr_box.y,
246*4c8945a0SNathan Whitehorn 		    hr_box.width,
247*4c8945a0SNathan Whitehorn 		    hr_box.height,
248*4c8945a0SNathan Whitehorn 		    60,
249*4c8945a0SNathan Whitehorn 		    minute >= 0 ? minute : current.tm_min,
250*4c8945a0SNathan Whitehorn 		    'M') < 0
251*4c8945a0SNathan Whitehorn 	|| DrawObject(&mn_box) < 0) {
252*4c8945a0SNathan Whitehorn 	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
253*4c8945a0SNathan Whitehorn     }
254*4c8945a0SNathan Whitehorn 
255*4c8945a0SNathan Whitehorn     mvwprintw(dialog, mn_box.y, mn_box.x + ONE_WIDE + MARGIN, ":");
256*4c8945a0SNathan Whitehorn     if (init_object(&sc_box,
257*4c8945a0SNathan Whitehorn 		    dialog,
258*4c8945a0SNathan Whitehorn 		    mn_box.x + (ONE_WIDE + 2 * MARGIN + 1),
259*4c8945a0SNathan Whitehorn 		    mn_box.y,
260*4c8945a0SNathan Whitehorn 		    mn_box.width,
261*4c8945a0SNathan Whitehorn 		    mn_box.height,
262*4c8945a0SNathan Whitehorn 		    60,
263*4c8945a0SNathan Whitehorn 		    second >= 0 ? second : current.tm_sec,
264*4c8945a0SNathan Whitehorn 		    'S') < 0
265*4c8945a0SNathan Whitehorn 	|| DrawObject(&sc_box) < 0) {
266*4c8945a0SNathan Whitehorn 	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
267*4c8945a0SNathan Whitehorn     }
268*4c8945a0SNathan Whitehorn 
269*4c8945a0SNathan Whitehorn     while (result == DLG_EXIT_UNKNOWN) {
270*4c8945a0SNathan Whitehorn 	BOX *obj = (state == sHR ? &hr_box
271*4c8945a0SNathan Whitehorn 		    : (state == sMN ? &mn_box :
272*4c8945a0SNathan Whitehorn 		       (state == sSC ? &sc_box : 0)));
273*4c8945a0SNathan Whitehorn 
274*4c8945a0SNathan Whitehorn 	button = (state < 0) ? 0 : state;
275*4c8945a0SNathan Whitehorn 	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
276*4c8945a0SNathan Whitehorn 	if (obj != 0)
277*4c8945a0SNathan Whitehorn 	    dlg_set_focus(dialog, obj->window);
278*4c8945a0SNathan Whitehorn 
279*4c8945a0SNathan Whitehorn 	key = dlg_mouse_wgetch(dialog, &fkey);
280*4c8945a0SNathan Whitehorn 	if (dlg_result_key(key, fkey, &result))
281*4c8945a0SNathan Whitehorn 	    break;
282*4c8945a0SNathan Whitehorn 
283*4c8945a0SNathan Whitehorn 	if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
284*4c8945a0SNathan Whitehorn 	    result = key2;
285*4c8945a0SNathan Whitehorn 	} else {
286*4c8945a0SNathan Whitehorn 	    /* handle function-keys */
287*4c8945a0SNathan Whitehorn 	    if (fkey) {
288*4c8945a0SNathan Whitehorn 		switch (key) {
289*4c8945a0SNathan Whitehorn 		case DLGK_MOUSE(0):
290*4c8945a0SNathan Whitehorn 		    result = DLG_EXIT_OK;
291*4c8945a0SNathan Whitehorn 		    break;
292*4c8945a0SNathan Whitehorn 		case DLGK_MOUSE(1):
293*4c8945a0SNathan Whitehorn 		    result = DLG_EXIT_CANCEL;
294*4c8945a0SNathan Whitehorn 		    break;
295*4c8945a0SNathan Whitehorn 		case DLGK_MOUSE('H'):
296*4c8945a0SNathan Whitehorn 		    state = sHR;
297*4c8945a0SNathan Whitehorn 		    break;
298*4c8945a0SNathan Whitehorn 		case DLGK_MOUSE('M'):
299*4c8945a0SNathan Whitehorn 		    state = sMN;
300*4c8945a0SNathan Whitehorn 		    break;
301*4c8945a0SNathan Whitehorn 		case DLGK_MOUSE('S'):
302*4c8945a0SNathan Whitehorn 		    state = sSC;
303*4c8945a0SNathan Whitehorn 		    break;
304*4c8945a0SNathan Whitehorn 		case DLGK_ENTER:
305*4c8945a0SNathan Whitehorn 		    result = button;
306*4c8945a0SNathan Whitehorn 		    break;
307*4c8945a0SNathan Whitehorn 		case DLGK_FIELD_PREV:
308*4c8945a0SNathan Whitehorn 		    state = dlg_prev_ok_buttonindex(state, sHR);
309*4c8945a0SNathan Whitehorn 		    break;
310*4c8945a0SNathan Whitehorn 		case DLGK_FIELD_NEXT:
311*4c8945a0SNathan Whitehorn 		    state = dlg_next_ok_buttonindex(state, sHR);
312*4c8945a0SNathan Whitehorn 		    break;
313*4c8945a0SNathan Whitehorn 		case DLGK_FIELD_FIRST:
314*4c8945a0SNathan Whitehorn 		    if (obj != 0) {
315*4c8945a0SNathan Whitehorn 			obj->value = 0;
316*4c8945a0SNathan Whitehorn 			(void) DrawObject(obj);
317*4c8945a0SNathan Whitehorn 		    }
318*4c8945a0SNathan Whitehorn 		    break;
319*4c8945a0SNathan Whitehorn 		case DLGK_FIELD_LAST:
320*4c8945a0SNathan Whitehorn 		    if (obj != 0) {
321*4c8945a0SNathan Whitehorn 			switch (state) {
322*4c8945a0SNathan Whitehorn 			case sHR:
323*4c8945a0SNathan Whitehorn 			    obj->value = 23;
324*4c8945a0SNathan Whitehorn 			    break;
325*4c8945a0SNathan Whitehorn 			case sMN:
326*4c8945a0SNathan Whitehorn 			case sSC:
327*4c8945a0SNathan Whitehorn 			    obj->value = 59;
328*4c8945a0SNathan Whitehorn 			    break;
329*4c8945a0SNathan Whitehorn 			}
330*4c8945a0SNathan Whitehorn 			(void) DrawObject(obj);
331*4c8945a0SNathan Whitehorn 		    }
332*4c8945a0SNathan Whitehorn 		    break;
333*4c8945a0SNathan Whitehorn 		case DLGK_DELETE_RIGHT:
334*4c8945a0SNathan Whitehorn 		    if (obj != 0) {
335*4c8945a0SNathan Whitehorn 			obj->value /= 10;
336*4c8945a0SNathan Whitehorn 			(void) DrawObject(obj);
337*4c8945a0SNathan Whitehorn 		    }
338*4c8945a0SNathan Whitehorn 		    break;
339*4c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
340*4c8945a0SNathan Whitehorn 		case KEY_RESIZE:
341*4c8945a0SNathan Whitehorn 		    /* reset data */
342*4c8945a0SNathan Whitehorn 		    height = old_height;
343*4c8945a0SNathan Whitehorn 		    width = old_width;
344*4c8945a0SNathan Whitehorn 		    hour = hr_box.value;
345*4c8945a0SNathan Whitehorn 		    minute = mn_box.value;
346*4c8945a0SNathan Whitehorn 		    second = sc_box.value;
347*4c8945a0SNathan Whitehorn 		    /* repaint */
348*4c8945a0SNathan Whitehorn 		    dlg_clear();
349*4c8945a0SNathan Whitehorn 		    dlg_del_window(dialog);
350*4c8945a0SNathan Whitehorn 		    refresh();
351*4c8945a0SNathan Whitehorn 		    dlg_mouse_free_regions();
352*4c8945a0SNathan Whitehorn 		    goto retry;
353*4c8945a0SNathan Whitehorn #endif
354*4c8945a0SNathan Whitehorn 		default:
355*4c8945a0SNathan Whitehorn 		    if (obj != 0) {
356*4c8945a0SNathan Whitehorn 			int step = next_or_previous(key);
357*4c8945a0SNathan Whitehorn 			if (step != 0) {
358*4c8945a0SNathan Whitehorn 			    obj->value += step;
359*4c8945a0SNathan Whitehorn 			    while (obj->value < 0)
360*4c8945a0SNathan Whitehorn 				obj->value += obj->period;
361*4c8945a0SNathan Whitehorn 			    obj->value %= obj->period;
362*4c8945a0SNathan Whitehorn 			    (void) DrawObject(obj);
363*4c8945a0SNathan Whitehorn 			}
364*4c8945a0SNathan Whitehorn 		    }
365*4c8945a0SNathan Whitehorn 		    break;
366*4c8945a0SNathan Whitehorn 		}
367*4c8945a0SNathan Whitehorn 	    } else if (isdigit(key)) {
368*4c8945a0SNathan Whitehorn 		if (obj != 0) {
369*4c8945a0SNathan Whitehorn 		    int digit = (key - '0');
370*4c8945a0SNathan Whitehorn 		    int value = (obj->value * 10) + digit;
371*4c8945a0SNathan Whitehorn 		    if (value < obj->period) {
372*4c8945a0SNathan Whitehorn 			obj->value = value;
373*4c8945a0SNathan Whitehorn 			(void) DrawObject(obj);
374*4c8945a0SNathan Whitehorn 		    } else {
375*4c8945a0SNathan Whitehorn 			beep();
376*4c8945a0SNathan Whitehorn 		    }
377*4c8945a0SNathan Whitehorn 		}
378*4c8945a0SNathan Whitehorn 	    } else {
379*4c8945a0SNathan Whitehorn 		beep();
380*4c8945a0SNathan Whitehorn 	    }
381*4c8945a0SNathan Whitehorn 	}
382*4c8945a0SNathan Whitehorn     }
383*4c8945a0SNathan Whitehorn 
384*4c8945a0SNathan Whitehorn #define DefaultFormat(dst, src) \
385*4c8945a0SNathan Whitehorn 	sprintf(dst, "%02d:%02d:%02d", \
386*4c8945a0SNathan Whitehorn 		hr_box.value, mn_box.value, sc_box.value)
387*4c8945a0SNathan Whitehorn 
388*4c8945a0SNathan Whitehorn #if defined(HAVE_STRFTIME)
389*4c8945a0SNathan Whitehorn     if (dialog_vars.time_format != 0) {
390*4c8945a0SNathan Whitehorn 	size_t used;
391*4c8945a0SNathan Whitehorn 	time_t now = time((time_t *) 0);
392*4c8945a0SNathan Whitehorn 	struct tm *parts = localtime(&now);
393*4c8945a0SNathan Whitehorn 
394*4c8945a0SNathan Whitehorn 	parts->tm_sec = sc_box.value;
395*4c8945a0SNathan Whitehorn 	parts->tm_min = mn_box.value;
396*4c8945a0SNathan Whitehorn 	parts->tm_hour = hr_box.value;
397*4c8945a0SNathan Whitehorn 	used = strftime(buffer,
398*4c8945a0SNathan Whitehorn 			sizeof(buffer) - 1,
399*4c8945a0SNathan Whitehorn 			dialog_vars.time_format,
400*4c8945a0SNathan Whitehorn 			parts);
401*4c8945a0SNathan Whitehorn 	if (used == 0 || *buffer == '\0')
402*4c8945a0SNathan Whitehorn 	    DefaultFormat(buffer, hr_box);
403*4c8945a0SNathan Whitehorn     } else
404*4c8945a0SNathan Whitehorn #endif
405*4c8945a0SNathan Whitehorn 	DefaultFormat(buffer, hr_box);
406*4c8945a0SNathan Whitehorn 
407*4c8945a0SNathan Whitehorn     dlg_add_result(buffer);
408*4c8945a0SNathan Whitehorn     dlg_add_separator();
409*4c8945a0SNathan Whitehorn 
410*4c8945a0SNathan Whitehorn     return CleanupResult(result, dialog, prompt, &save_vars);
411*4c8945a0SNathan Whitehorn }
412