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