xref: /freebsd/contrib/dialog/rangebox.c (revision f3c5273d315a64826d2149ac453ff8c4583ddbe8)
1 /*
2  *  $Id: rangebox.c,v 1.24 2018/06/19 22:57:01 tom Exp $
3  *
4  *  rangebox.c -- implements the rangebox dialog
5  *
6  *  Copyright 2012-2017,2018	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 #define ONE_HIGH 1
28 
29 #define MIN_HIGH (ONE_HIGH + 1 + (4 * MARGIN))
30 #define MIN_WIDE (10 + 2 + (2 * MARGIN))
31 
32 struct _box;
33 
34 typedef struct _box {
35     WINDOW *parent;
36     WINDOW *window;
37     int x;
38     int y;
39     int width;
40     int height;
41     int period;
42     int value;
43 } BOX;
44 
45 typedef struct {
46     /* window in which the value and slider are drawn */
47     WINDOW *window;
48     int min_value;
49     int max_value;
50     /* position and width of the numeric field */
51     int value_x;
52     int value_len;
53     int value_col;
54     /* position and width of the slider field */
55     int slide_x;
56     int slide_y;
57     int slide_len;
58     /* current value drawn */
59     int current;
60     /* value to add to make slider move by one cell */
61     int slide_inc;
62 } VALUE;
63 
64 static int
65 digits_of(int value)
66 {
67     char temp[80];
68     sprintf(temp, "%d", value);
69     return (int) strlen(temp);
70 }
71 
72 static int
73 digit_of(VALUE * data)
74 {
75     int col = data->value_col;
76     int result = 1;
77 
78     while (++col < data->value_len) {
79 	result *= 10;
80     }
81     return result;
82 }
83 
84 static bool
85 set_digit(VALUE * data, int chr)
86 {
87     bool result = FALSE;
88     char buffer[80];
89     long check;
90     char *next = 0;
91 
92     sprintf(buffer, "%*d", data->value_len, data->current);
93     buffer[data->value_col] = (char) chr;
94     check = strtol(buffer, &next, 10);
95     if (next == 0 || *next == '\0') {
96 	if ((check <= (long) data->max_value) &&
97 	    (check >= (long) data->min_value)) {
98 	    result = TRUE;
99 	    data->current = (int) check;
100 	}
101     }
102 
103     return result;
104 }
105 
106 /*
107  * This is similar to the gauge code, but differs in the way the number
108  * is displayed, etc.
109  */
110 static void
111 draw_value(VALUE * data, int value)
112 {
113     if (value != data->current) {
114 	WINDOW *win = data->window;
115 	int y, x;
116 	int n;
117 	int ranges = (data->max_value + 1 - data->min_value);
118 	int offset = (value - data->min_value);
119 	int scaled;
120 
121 	getyx(win, y, x);
122 
123 	if (ranges > data->slide_len) {
124 	    scaled = (offset + data->slide_inc) / data->slide_inc;
125 	} else if (ranges < data->slide_len) {
126 	    scaled = (offset + 1) * data->slide_inc;
127 	} else {
128 	    scaled = offset;
129 	}
130 
131 	dlg_attrset(win, gauge_attr);
132 	wmove(win, data->slide_y, data->slide_x);
133 	for (n = 0; n < data->slide_len; ++n) {
134 	    (void) waddch(win, ' ');
135 	}
136 	wmove(win, data->slide_y, data->value_x);
137 	wprintw(win, "%*d", data->value_len, value);
138 	if ((gauge_attr & A_REVERSE) != 0) {
139 	    dlg_attroff(win, A_REVERSE);
140 	} else {
141 	    dlg_attrset(win, A_REVERSE);
142 	}
143 	wmove(win, data->slide_y, data->slide_x);
144 	for (n = 0; n < scaled; ++n) {
145 	    chtype ch2 = winch(win);
146 	    if (gauge_attr & A_REVERSE) {
147 		ch2 &= ~A_REVERSE;
148 	    }
149 	    (void) waddch(win, ch2);
150 	}
151 	dlg_attrset(win, dialog_attr);
152 
153 	wmove(win, y, x);
154 	data->current = value;
155 
156 	DLG_TRACE(("# drew %d offset %d scaled %d limit %d inc %d\n",
157 		   value,
158 		   offset,
159 		   scaled,
160 		   data->slide_len,
161 		   data->slide_inc));
162 
163 	dlg_trace_win(win);
164     }
165 }
166 
167 /*
168  * Allow the user to select from a range of values, e.g., using a slider.
169  */
170 int
171 dialog_rangebox(const char *title,
172 		const char *cprompt,
173 		int height,
174 		int width,
175 		int min_value,
176 		int max_value,
177 		int default_value)
178 {
179     /* *INDENT-OFF* */
180     static DLG_KEYS_BINDING binding[] = {
181 	DLG_KEYS_DATA( DLGK_DELETE_RIGHT,KEY_DC ),
182 	HELPKEY_BINDINGS,
183 	ENTERKEY_BINDINGS,
184 	TOGGLEKEY_BINDINGS,
185 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, CHR_NEXT ),
186 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
187 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
188 	DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_BACKSPACE ),
189 	DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_PREVIOUS ),
190 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
191 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
192 	DLG_KEYS_DATA( DLGK_ITEM_FIRST, KEY_HOME),
193 	DLG_KEYS_DATA( DLGK_ITEM_LAST,  KEY_END),
194 	DLG_KEYS_DATA( DLGK_ITEM_LAST,  KEY_LL ),
195 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  '+'),
196 	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_DOWN),
197 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  '-' ),
198 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_UP ),
199 	DLG_KEYS_DATA( DLGK_PAGE_NEXT,  KEY_NEXT),
200 	DLG_KEYS_DATA( DLGK_PAGE_NEXT,  KEY_NPAGE),
201 	DLG_KEYS_DATA( DLGK_PAGE_PREV,  KEY_PPAGE ),
202 	DLG_KEYS_DATA( DLGK_PAGE_PREV,  KEY_PREVIOUS ),
203 	END_KEYS_BINDING
204     };
205     /* *INDENT-ON* */
206 
207 #ifdef KEY_RESIZE
208     int old_height = height;
209     int old_width = width;
210 #endif
211     VALUE data;
212     int key = 0, key2, fkey;
213     int button;
214     int result = DLG_EXIT_UNKNOWN;
215     WINDOW *dialog;
216     int state = dlg_default_button();
217     const char **buttons = dlg_ok_labels();
218     char *prompt;
219     char buffer[MAX_LEN];
220     int cur_value = default_value;
221     int usable;
222     int ranges;
223     int yorg, xorg;
224 
225     DLG_TRACE(("# tailbox args:\n"));
226     DLG_TRACE2S("title", title);
227     DLG_TRACE2S("message", cprompt);
228     DLG_TRACE2N("height", height);
229     DLG_TRACE2N("width", width);
230     DLG_TRACE2N("minval", min_value);
231     DLG_TRACE2N("maxval", max_value);
232     DLG_TRACE2N("default", default_value);
233 
234     if (max_value < min_value)
235 	max_value = min_value;
236     if (cur_value > max_value)
237 	cur_value = max_value;
238     if (cur_value < min_value)
239 	cur_value = min_value;
240 
241     dlg_does_output();
242 
243 #ifdef KEY_RESIZE
244   retry:
245 #endif
246 
247     prompt = dlg_strclone(cprompt);
248     dlg_auto_size(title, prompt, &height, &width, 0, 0);
249 
250     height += MIN_HIGH;
251     if (width < MIN_WIDE)
252 	width = MIN_WIDE;
253     dlg_button_layout(buttons, &width);
254     dlg_print_size(height, width);
255     dlg_ctl_size(height, width);
256 
257     dialog = dlg_new_window(height, width,
258 			    yorg = dlg_box_y_ordinate(height),
259 			    xorg = dlg_box_x_ordinate(width));
260 
261     data.window = dialog;
262 
263     data.min_value = min_value;
264     data.max_value = max_value;
265 
266     usable = (width - 2 - 4 * MARGIN);
267     ranges = max_value - min_value + 1;
268 
269     /*
270      * Center the number after allowing for its maximum number of digits.
271      */
272     data.value_len = digits_of(max_value);
273     if (digits_of(min_value) > data.value_len)
274 	data.value_len = digits_of(min_value);
275     data.value_x = (usable - data.value_len) / 2 + MARGIN;
276     data.value_col = data.value_len - 1;
277 
278     /*
279      * The slider is scaled, to try to use the width of the dialog.
280      */
281     if (ranges > usable) {
282 	data.slide_inc = (ranges + usable - 1) / usable;
283 	data.slide_len = 1 + ranges / data.slide_inc;
284     } else if (ranges < usable) {
285 	data.slide_inc = usable / ranges;
286 	data.slide_len = ranges * data.slide_inc;
287     } else {
288 	data.slide_inc = 1;
289 	data.slide_len = usable;
290     }
291     data.slide_x = (usable - data.slide_len) / 2 + MARGIN + 2;
292     data.slide_y = height - 5;
293 
294     data.current = cur_value - 1;
295 
296     dlg_register_window(dialog, "rangebox", binding);
297     dlg_register_buttons(dialog, "rangebox", buttons);
298 
299     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
300     dlg_mouse_setbase(xorg, yorg);
301     dlg_mouse_mkregion(data.slide_y - 1, data.slide_x - 1, 3, usable + 2, 'i');
302     dlg_draw_box2(dialog,
303 		  height - 6, data.slide_x - MARGIN,
304 		  2 + MARGIN, data.slide_len + 2 * MARGIN,
305 		  dialog_attr,
306 		  border_attr,
307 		  border2_attr);
308     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
309     dlg_draw_title(dialog, title);
310     dlg_draw_helpline(dialog, FALSE);
311 
312     dlg_attrset(dialog, dialog_attr);
313     dlg_print_autowrap(dialog, prompt, height, width);
314 
315     dlg_trace_win(dialog);
316     while (result == DLG_EXIT_UNKNOWN) {
317 	draw_value(&data, cur_value);
318 	button = (state < 0) ? 0 : state;
319 	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
320 	if (state < 0) {
321 	    data.value_col = data.value_len + state;
322 	    wmove(dialog, data.slide_y, data.value_x + data.value_col);
323 	}
324 
325 	key = dlg_mouse_wgetch(dialog, &fkey);
326 	if (dlg_result_key(key, fkey, &result))
327 	    break;
328 
329 	if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
330 	    result = key2;
331 	} else {
332 	    /* handle function-keys */
333 	    if (fkey) {
334 		switch (key) {
335 		case DLGK_TOGGLE:
336 		case DLGK_ENTER:
337 		    result = dlg_ok_buttoncode(button);
338 		    break;
339 		case DLGK_FIELD_PREV:
340 		    if (state < 0 && state > -data.value_len) {
341 			--state;
342 		    } else {
343 			state = dlg_prev_ok_buttonindex(state, -data.value_len);
344 		    }
345 		    break;
346 		case DLGK_FIELD_NEXT:
347 		    if (state < 0) {
348 			++state;
349 		    } else {
350 			state = dlg_next_ok_buttonindex(state, -data.value_len);
351 		    }
352 		    break;
353 		case DLGK_ITEM_FIRST:
354 		    cur_value = min_value;
355 		    break;
356 		case DLGK_ITEM_LAST:
357 		    cur_value = max_value;
358 		    break;
359 		case DLGK_ITEM_PREV:
360 		    if (state < 0) {
361 			cur_value -= digit_of(&data);
362 		    } else {
363 			cur_value -= 1;
364 		    }
365 		    if (cur_value < min_value)
366 			cur_value = min_value;
367 		    break;
368 		case DLGK_ITEM_NEXT:
369 		    if (state < 0) {
370 			cur_value += digit_of(&data);
371 		    } else {
372 			cur_value += 1;
373 		    }
374 		    if (cur_value > max_value)
375 			cur_value = max_value;
376 		    break;
377 		case DLGK_PAGE_PREV:
378 		    cur_value -= data.slide_inc;
379 		    if (cur_value < min_value)
380 			cur_value = min_value;
381 		    break;
382 		case DLGK_PAGE_NEXT:
383 		    cur_value += data.slide_inc;
384 		    if (cur_value > max_value)
385 			cur_value = max_value;
386 		    break;
387 #ifdef KEY_RESIZE
388 		case KEY_RESIZE:
389 		    dlg_will_resize(dialog);
390 		    /* reset data */
391 		    height = old_height;
392 		    width = old_width;
393 		    /* repaint */
394 		    free(prompt);
395 		    dlg_clear();
396 		    dlg_del_window(dialog);
397 		    dlg_mouse_free_regions();
398 		    goto retry;
399 #endif
400 		case DLGK_MOUSE('i'):
401 		    state = -data.value_len;
402 		    break;
403 		default:
404 		    if (is_DLGK_MOUSE(key)) {
405 			result = dlg_ok_buttoncode(key - M_EVENT);
406 			if (result < 0)
407 			    result = DLG_EXIT_OK;
408 		    }
409 		    break;
410 		}
411 	    } else if (isdigit(key) && state < 0) {
412 		if (set_digit(&data, key)) {
413 		    cur_value = data.current;
414 		    data.current--;
415 		}
416 	    } else {
417 		beep();
418 	    }
419 	}
420     }
421 
422     sprintf(buffer, "%d", cur_value);
423     dlg_add_result(buffer);
424     dlg_add_separator();
425     dlg_add_last_key(-1);
426 
427     dlg_del_window(dialog);
428     dlg_mouse_free_regions();
429     free(prompt);
430 
431     return result;
432 }
433