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