2 * $Id: rangebox.c,v 1.17 2013/03/17 16:02:00 tom Exp $
4 * rangebox.c -- implements the rangebox dialog
6 * Copyright 2012,2013 Thomas E. Dickey
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.
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.
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.
29 #define MIN_HIGH (ONE_HIGH + 1 + (4 * MARGIN))
30 #define MIN_WIDE (10 + 2 + (2 * MARGIN))
46 /* window in which the value and slider are drawn */
50 /* position and width of the numeric field */
54 /* position and width of the slider field */
58 /* current value drawn */
60 /* value to add to make slider move by one cell */
68 sprintf(temp, "%d", value);
69 return (int) strlen(temp);
73 digit_of(VALUE * data)
75 int col = data->value_col;
78 while (++col < data->value_len) {
85 set_digit(VALUE * data, int chr)
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)) {
99 data->current = (int) check;
107 * This is similar to the gauge code, but differs in the way the number
111 draw_value(VALUE * data, int value)
113 if (value != data->current) {
114 WINDOW *win = data->window;
117 int ranges = (data->max_value + 1 - data->min_value);
118 int offset = (value - data->min_value);
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;
131 (void) wattrset(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, ' ');
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 wattroff(win, A_REVERSE);
141 (void) wattrset(win, A_REVERSE);
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) {
149 (void) waddch(win, ch2);
151 (void) wattrset(win, dialog_attr);
154 data->current = value;
156 dlg_trace_msg("drew %d offset %d scaled %d limit %d inc %d\n",
168 * Allow the user to select from a range of values, e.g., using a slider.
171 dialog_rangebox(const char *title,
180 static DLG_KEYS_BINDING binding[] = {
181 DLG_KEYS_DATA( DLGK_DELETE_RIGHT,KEY_DC ),
184 DLG_KEYS_DATA( DLGK_ENTER, ' ' ),
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 ),
208 int old_height = height;
209 int old_width = width;
212 int key = 0, key2, fkey;
214 int result = DLG_EXIT_UNKNOWN;
216 int state = dlg_default_button();
217 const char **buttons = dlg_ok_labels();
218 char *prompt = dlg_strclone(cprompt);
219 char buffer[MAX_LEN];
220 int cur_value = default_value;
225 if (max_value < min_value)
226 max_value = min_value;
227 if (cur_value > max_value)
228 cur_value = max_value;
229 if (cur_value < min_value)
230 cur_value = min_value;
238 dlg_auto_size(title, prompt, &height, &width, 0, 0);
240 if (width < MIN_WIDE)
242 dlg_button_layout(buttons, &width);
243 dlg_print_size(height, width);
244 dlg_ctl_size(height, width);
246 dialog = dlg_new_window(height, width,
247 yorg = dlg_box_y_ordinate(height),
248 xorg = dlg_box_x_ordinate(width));
250 data.window = dialog;
252 data.min_value = min_value;
253 data.max_value = max_value;
255 usable = (width - 2 - 4 * MARGIN);
256 ranges = max_value - min_value + 1;
259 * Center the number after allowing for its maximum number of digits.
261 data.value_len = digits_of(max_value);
262 if (digits_of(min_value) > data.value_len)
263 data.value_len = digits_of(min_value);
264 data.value_x = (usable - data.value_len) / 2 + MARGIN;
265 data.value_col = data.value_len - 1;
268 * The slider is scaled, to try to use the width of the dialog.
270 if (ranges > usable) {
271 data.slide_inc = (ranges + usable - 1) / usable;
272 data.slide_len = 1 + ranges / data.slide_inc;
273 } else if (ranges < usable) {
274 data.slide_inc = usable / ranges;
275 data.slide_len = ranges * data.slide_inc;
278 data.slide_len = usable;
280 data.slide_x = (usable - data.slide_len) / 2 + MARGIN + 2;
281 data.slide_y = height - 5;
283 data.current = cur_value - 1;
285 dlg_register_window(dialog, "rangebox", binding);
286 dlg_register_buttons(dialog, "rangebox", buttons);
288 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
289 dlg_mouse_setbase(xorg, yorg);
290 dlg_mouse_mkregion(data.slide_y - 1, data.slide_x - 1, 3, usable + 2, 'i');
291 dlg_draw_box2(dialog,
292 height - 6, data.slide_x - MARGIN,
293 2 + MARGIN, data.slide_len + 2 * MARGIN,
297 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
298 dlg_draw_title(dialog, title);
299 dlg_draw_helpline(dialog, FALSE);
301 (void) wattrset(dialog, dialog_attr);
302 dlg_print_autowrap(dialog, prompt, height, width);
304 dlg_trace_win(dialog);
305 while (result == DLG_EXIT_UNKNOWN) {
306 draw_value(&data, cur_value);
307 button = (state < 0) ? 0 : state;
308 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
310 data.value_col = data.value_len + state;
311 wmove(dialog, data.slide_y, data.value_x + data.value_col);
314 key = dlg_mouse_wgetch(dialog, &fkey);
315 if (dlg_result_key(key, fkey, &result))
318 if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
321 /* handle function-keys */
325 result = dlg_ok_buttoncode(button);
327 case DLGK_FIELD_PREV:
328 if (state < 0 && state > -data.value_len) {
331 state = dlg_prev_ok_buttonindex(state, -data.value_len);
334 case DLGK_FIELD_NEXT:
338 state = dlg_next_ok_buttonindex(state, -data.value_len);
341 case DLGK_ITEM_FIRST:
342 cur_value = min_value;
345 cur_value = max_value;
349 cur_value -= digit_of(&data);
353 if (cur_value < min_value)
354 cur_value = min_value;
358 cur_value += digit_of(&data);
362 if (cur_value > max_value)
363 cur_value = max_value;
366 cur_value -= data.slide_inc;
367 if (cur_value < min_value)
368 cur_value = min_value;
371 cur_value += data.slide_inc;
372 if (cur_value > max_value)
373 cur_value = max_value;
382 dlg_del_window(dialog);
384 dlg_mouse_free_regions();
387 case DLGK_MOUSE('i'):
388 state = -data.value_len;
391 if (is_DLGK_MOUSE(key)) {
392 result = dlg_ok_buttoncode(key - M_EVENT);
394 result = DLG_EXIT_OK;
398 } else if (isdigit(key) && state < 0) {
399 if (set_digit(&data, key)) {
400 cur_value = data.current;
409 sprintf(buffer, "%d", cur_value);
410 dlg_add_result(buffer);
412 dlg_add_last_key(-1);
414 dlg_del_window(dialog);
415 dlg_mouse_free_regions();