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