1 /*
2  *  Based on checklist.c from dialog
3  *
4  *  Copyright 2012-2013,2015-2016	Ilya A. Arkhipov
5  *  Copyright 2000-2011			Thomas E. Dickey
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser General Public License, version 2.1
9  *  as published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful, but
12  *  WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this program; if not, write to
18  *  Free Software Foundation, Inc.
19  *  51 Franklin St., Fifth Floor
20  *  Boston, MA 02110, USA.
21  *
22  *  An earlier version of this program lists as authors:
23  *  Savio Lam (lam836@cs.cuhk.hk)
24  *  Stuart Herbert - S.Herbert@sheffield.ac.uk: radiolist extension
25  *  Alessandro Rubini - rubini@ipvvis.unipv.it: merged the two
26  */
27 
28 #include <dialog.h>
29 #include <dlg_keys.h>
30 #include <stdbool.h>
31 
32 #include "mixedlist.h"
33 
34 static int list_width, check_x, item_x;
35 
36 #define MIN_HIGH			(1 + (5 * MARGIN))
37 
38 static int
d4p_default_mixlistitem(dialog_mixedlist * items)39 d4p_default_mixlistitem(dialog_mixedlist *items)
40 {
41 	int result = 0;
42 
43 	if (dialog_vars.default_item != 0) {
44 		int count = 0;
45 		while (items->name != 0) {
46 			if (!strcmp(dialog_vars.default_item, items->name)) {
47 				result = count;
48 				break;
49 			}
50 			items++;
51 			count++;
52 		}
53 	}
54 	return result;
55 }
56 
57 static int
d4p_calc_mixlist_width(int item_no,dialog_mixedlist * items)58 d4p_calc_mixlist_width(int item_no, dialog_mixedlist *items)
59 {
60 	int n, i, len1 = 0, len2 = 0;
61 	for (i = 0; i < item_no; ++i) {
62 		if (items[i].type != ITEM_SEPARATOR) {
63 			if ((n = dlg_count_columns(items[i].name)) > len1)
64 				len1 = n;
65 			if ((n = dlg_count_columns(items[i].text)) > len2)
66 				len2 = n;
67 		}
68 	}
69 	return len1 + len2;
70 }
71 
72 static void
d4p_calc_mixlisth(int * height,int * list_height,int item_no)73 d4p_calc_mixlisth(int *height, int *list_height, int item_no)
74 {
75 	/* calculate new height and list_height */
76 	int rows = SLINES - (dialog_vars.begin_set ? dialog_vars.begin_y : 0);
77 	if (rows - (*height) > 0) {
78 		if (rows - (*height) > item_no)
79 			*list_height = item_no;
80 		else
81 			*list_height = rows - (*height);
82 	}
83 	(*height) = (*list_height) + MAGIC_BORDER; // XXX: add checks for prompt
84 }
85 
86 static void
d4p_print_arrows(WINDOW * win,int box_x,int box_y,int scrollamt,int choice,int item_no,int list_height)87 d4p_print_arrows(WINDOW *win,
88 		int box_x,
89 		int box_y,
90 		int scrollamt,
91 		int choice,
92 		int item_no,
93 		int list_height)
94 {
95 	dlg_draw_scrollbar(win,
96 			(long) (scrollamt),
97 			(long) (scrollamt),
98 			(long) (scrollamt + choice),
99 			(long) (item_no),
100 			box_x + check_x,
101 			box_x + list_width,
102 			box_y,
103 			box_y + list_height + 1,
104 			menubox_attr,
105 			menubox_border_attr);
106 }
107 
108  /* Display a help-file as a textbox widget. */
109 static int
d4p_helpwindow(const char * title,const char * file,int height,int width)110 d4p_helpwindow(const char *title,
111 			const char *file,
112 			int height,
113 			int width)
114 {
115 	int result = DLG_EXIT_ERROR;
116 
117 	if (!dialog_vars.in_helpfile && file != 0 && *file != '\0') {
118 		dialog_vars.in_helpfile = true;
119 		result = dialog_textbox(title, file, height, width);
120 		dialog_vars.in_helpfile = false;
121 	}
122 	return (result);
123 }
124 
125 static void
d4p_clean_window(WINDOW * dialog)126 d4p_clean_window(WINDOW *dialog)
127 {
128 
129 	dlg_clear();
130 	if (dialog != NULL)
131 		dlg_del_window(dialog);
132 	dlg_mouse_free_regions();
133 	refresh();
134 }
135 
136 
137 /*
138  * Print list item.  The 'selected' parameter is true if 'choice' is the
139  * current item.  That one is colored differently from the other items.
140  */
141 static void
print_item(WINDOW * win,dialog_mixedlist * items,int choice,bool selected)142 print_item(WINDOW *win,
143 		dialog_mixedlist *items,
144 		int choice,
145 		bool selected)
146 {
147 	chtype save = dlg_get_attrs(win);
148 	int i, namelen, slsize;
149 	chtype attr = A_NORMAL;
150 	const int *cols;
151 	const int *indx;
152 	int limit, itype;
153 	char const *states;
154 
155 	itype = items->type;
156 	/* x for [ ], * for ( ) */
157 	if (itype == ITEM_CHECK)
158 		states = " x";
159 	else if (itype == ITEM_RADIO)
160 		states = " *";
161 	else
162 		states = "  ";
163 
164 	/* Clear 'residue' of last item */
165 	wattrset(win, menubox_attr);
166 	wmove(win, choice, 0);
167 	for (i = 0; i < list_width; i++)
168 		waddch(win, ' ');
169 
170 	/* Is separator? */
171 	if (itype == ITEM_SEPARATOR) {
172 		wmove(win, choice, 0);
173 		if (strlen(items->name) == 0) {
174 			for (i = 0; i < getmaxx(win); i++)
175 				waddch(win, ACS_HLINE);
176 		} else {
177 			namelen = strlen(items->name) / 2 ;
178 			slsize = (getmaxx(win) / 2) - namelen - 1;
179 			if (strlen(items->name) % 2 == 0)
180 				slsize++;
181 			for (i = 0; i < slsize ; i++)
182 				waddch(win, ACS_HLINE);
183 			waddch(win, ' ');
184 			wattrset(win, tag_key_attr);
185 			waddstr(win, items->name );
186 			wattrset(win, item_attr);
187 			waddch(win, ' ');
188 			if (strlen(items->name) % 2 == 0)
189 				slsize--;
190 			for (i = 0; i < slsize - 1; i++)
191 				waddch(win, ACS_HLINE);
192 		}
193 	} /* No ;) */
194 	else {
195 		wmove(win, choice, check_x - 1);
196 		wattrset(win, tag_key_attr);
197 		waddch(win, items->new ? '+' : ' ');
198 		wattrset(win, selected ? check_selected_attr : check_attr);
199 		wprintw(win,
200 				itype == ITEM_CHECK ? "[%c]" : "(%c)",
201 				states[items->state]);
202 		wattrset(win, menubox_attr);
203 		waddch(win, ' ');
204 
205 		if (strlen(items->name) != 0) {
206 
207 			indx = dlg_index_wchars(items->name);
208 
209 			wattrset(win, selected ? tag_key_selected_attr : tag_key_attr);
210 			waddnstr(win, items->name, indx[1]);
211 
212 			if ((int) strlen(items->name) > indx[1]) {
213 				limit = dlg_limit_columns(items->name,
214 						(item_x - check_x - MAGIC_BORDER), 1);
215 				if (limit > 1) {
216 					wattrset(win, selected ? tag_selected_attr : tag_attr);
217 					waddnstr(win,
218 							items->name + indx[1],
219 							indx[limit] - indx[1]);
220 				}
221 			}
222 		}
223 
224 		if (strlen(items->text) != 0) {
225 			cols = dlg_index_columns(items->text);
226 			limit = dlg_limit_columns(items->text,
227 					(getmaxx(win) - item_x + 1), 0);
228 
229 			if (limit > 0) {
230 				wmove(win, choice, item_x);
231 				wattrset(win, selected ? item_selected_attr : item_attr);
232 				dlg_print_text(win, items->text, cols[limit], &attr);
233 			}
234 		}
235 
236 		wattrset(win, save);
237 	}
238 }
239 
240 /*
241  * This is an alternate interface to 'checklist' which allows the application
242  * to read the list item states back directly without putting them in the
243  * output buffer.  It also provides for more than two states over which the
244  * check/radio box can display.
245  */
246 int
dlg_mixedlist(const char * title,const char * cprompt,int height,int min_height,int width,int item_no,dialog_mixedlist * items,bool align_center,bool fullscreen)247 dlg_mixedlist(const char *title,
248 		const char *cprompt,
249 		int height,
250 		int min_height,
251 		int width,
252 		int item_no,
253 		dialog_mixedlist *items,
254 		bool align_center,
255 		bool fullscreen)
256 {
257 	/* *INDENT-OFF* */
258 	static DLG_KEYS_BINDING binding[] = {
259 		ENTERKEY_BINDINGS,
260 		DLG_KEYS_DATA( DLGK_FIELD_NEXT,	KEY_RIGHT ),
261 		DLG_KEYS_DATA( DLGK_FIELD_NEXT,	TAB ),
262 		DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_BTAB ),
263 		DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_LEFT ),
264 		DLG_KEYS_DATA( DLGK_ITEM_FIRST,	KEY_HOME ),
265 		DLG_KEYS_DATA( DLGK_ITEM_LAST,	KEY_END ),
266 		DLG_KEYS_DATA( DLGK_ITEM_LAST,	KEY_LL ),
267 		DLG_KEYS_DATA( DLGK_ITEM_NEXT,	'+' ),
268 		DLG_KEYS_DATA( DLGK_ITEM_NEXT,	KEY_DOWN ),
269 		DLG_KEYS_DATA( DLGK_ITEM_NEXT,	CHR_NEXT ),
270 		DLG_KEYS_DATA( DLGK_ITEM_PREV,	'-' ),
271 		DLG_KEYS_DATA( DLGK_ITEM_PREV,	KEY_UP ),
272 		DLG_KEYS_DATA( DLGK_ITEM_PREV,	CHR_PREVIOUS ),
273 		DLG_KEYS_DATA( DLGK_PAGE_NEXT,	KEY_NPAGE ),
274 		DLG_KEYS_DATA( DLGK_PAGE_NEXT,	DLGK_MOUSE(KEY_NPAGE) ),
275 		DLG_KEYS_DATA( DLGK_PAGE_PREV,	KEY_PPAGE ),
276 		DLG_KEYS_DATA( DLGK_PAGE_PREV,	DLGK_MOUSE(KEY_PPAGE) ),
277 
278 		END_KEYS_BINDING
279 	};
280 	/* *INDENT-ON* */
281 
282 #ifdef KEY_RESIZE
283 	int old_height = height;
284 	int old_width = width;
285 #endif
286 	int i, j, key2, x, y, cur_x, cur_y, box_x, box_y;
287 	int key = 0, fkey;
288 	int button = dialog_state.visit_items ? -1 : dlg_defaultno_button();
289 	int choice = d4p_default_mixlistitem(items); // current item
290 	int scrollamt = 0; // scroll screen
291 	int max_choice;
292 	int was_mouse;
293 	int use_height;
294 	int use_width, name_width, text_width;
295 	int result = DLG_EXIT_UNKNOWN;
296 	WINDOW *dialog = NULL;
297 	WINDOW *list;
298 	char *prompt = dlg_strclone(cprompt);
299 	const char **buttons = dlg_ok_labels();
300 	bool found;
301 
302 	dlg_does_output();
303 	dlg_tab_correct_str(prompt);
304 
305 	/*
306 	 * If this is a radiobutton list, ensure that no more than one item is
307 	 * selected initially.  Allow none to be selected, since some users may
308 	 * wish to provide this flavor.
309 	 */
310 	int group = 0;
311 
312 	for (i = 0; i < item_no; i++) {
313 		if (items[i].type == ITEM_RADIO) {
314 			/* We don't know id radio group */
315 			if (group == 0 || group < items[i].group)
316 				group = items[i].group;
317 			if (items[i].state) {
318 				if (items[i].group == group) {
319 					group++;
320 				} else {
321 					items[i].state = 0;
322 				}
323 			}
324 		}
325 	}
326 
327 	if (min_height != 0 && item_no < min_height)
328 		height = min_height;
329 
330 
331 retry:
332 	d4p_clean_window(dialog);
333 
334 	if (fullscreen) {
335 		height = SLINES - 4;
336 		width = SCOLS - 4;
337 	}
338 
339 	use_height = height;
340 	if (use_height == 0) {
341 		use_width = d4p_calc_mixlist_width(item_no, items) + 10;
342 		/* calculate height without items (4) */
343 		dlg_auto_size(	title, cprompt, &height, &width, MIN_HIGH,
344 						MAX(26, use_width));
345 		d4p_calc_mixlisth(&height, &use_height, item_no);
346 		/* anyway we should have marging */
347 		if (width >= SCOLS)
348 			width = SCOLS - 4;
349 		if (height >= SLINES)
350 			height = SLINES - 4;
351 	} else {
352 		dlg_auto_size(	title, prompt, &height, &width,
353 						MIN_HIGH + use_height, 26);
354 	}
355 	dlg_button_layout(buttons, &width);
356 	dlg_print_size(height, width);
357 	dlg_ctl_size(height, width);
358 
359 	x = dlg_box_x_ordinate(width);
360 	y = dlg_box_y_ordinate(height);
361 
362 	dialog = dlg_new_window(height, width, y, x);
363 	dlg_register_window(dialog, "mixedlist", binding);
364 	dlg_register_buttons(dialog, "mixedlist", buttons);
365 
366 	dlg_mouse_setbase(x, y);
367 
368 	dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
369 	dlg_draw_bottom_box(dialog);
370 
371 	dlg_draw_title(dialog, title);
372 
373 	wattrset(dialog, dialog_attr);
374 	dlg_print_autowrap(dialog, prompt, height, width);
375 
376 	list_width = width - MAGIC_BORDER;
377 	getyx(dialog, cur_y, cur_x);
378 	box_y = cur_y + 1;
379 	box_x = (width - list_width) / 2 - 1;
380 
381 	if (dialog_vars.help_file != NULL) {
382 		dialog_vars.item_help = true;
383 		dlg_item_help("Detailed help is available. Press F1 or ^E to view it.");
384 	}
385 
386 	/*
387 	 * After displaying the prompt, we know how much space we really have.
388 	 * Limit the list to avoid overwriting the ok-button.
389 	 */
390 	if (use_height + MIN_HIGH > height - cur_y)
391 		use_height = height - MIN_HIGH - cur_y;
392 	if (use_height <= 0)
393 		use_height = 1;
394 
395 	max_choice = MIN(use_height, item_no);
396 
397 	/* create new window for the list */
398 	list = dlg_sub_window(dialog, use_height, list_width,
399 			y + box_y + 1, x + box_x + 1);
400 
401 	/* draw a box around the list items */
402 	dlg_draw_box(dialog, box_y, box_x,
403 			use_height + 2 * MARGIN,
404 			list_width + 2 * MARGIN,
405 			menubox_border_attr, menubox_attr);
406 
407 	text_width = 0;
408 	name_width = 0;
409 	/* Find length of longest item to center checklist */
410 	for (i = 0; i < item_no; i++) {
411 		if (items[i].type != ITEM_SEPARATOR) {
412 			text_width = MAX(text_width, dlg_count_columns(items[i].text));
413 			name_width = MAX(name_width, dlg_count_columns(items[i].name));
414 		}
415 	}
416 
417 	/* If the name+text is wider than the list is allowed, then truncate
418 	 * one or both of them.  If the name is no wider than 1/4 of the list,
419 	 * leave it intact.
420 	 */
421 	use_width = (list_width - MAGIC_BORDER);
422 	if (text_width + name_width > use_width) {
423 		int need = (int) (0.25 * use_width);
424 		if (name_width > need) {
425 			int want = (int) (use_width * ((double) name_width) /
426 					(text_width + name_width));
427 			name_width = (want > need) ? want : need;
428 		}
429 		text_width = use_width - name_width;
430 	}
431 
432 	if (align_center) {
433 		check_x = (use_width - (text_width + name_width)) / 2;
434 	} else {
435 		check_x = 1;
436 	}
437 	item_x = name_width + check_x + MAGIC_BORDER;
438 
439 	/* ensure we are scrolled to show the current choice */
440 	if (choice >= (max_choice + scrollamt)) {
441 		scrollamt = choice - max_choice + 1;
442 		choice = max_choice - 1;
443 	}
444 
445 	/* scrollamt+max_choice should not be more than item number */
446 	if (scrollamt + max_choice >= item_no)
447 		scrollamt = item_no - max_choice;
448 
449 	/* Print the list */
450 	for (i = 0; i < max_choice; i++) {
451 		/* Check for first item, and jump to second if 1 is separator */
452 		if (items[choice + scrollamt].type == ITEM_SEPARATOR)
453 			choice = 1;
454 		print_item(list,
455 				&items[i + scrollamt],
456 				i, i == choice);
457 	}
458 	(void) wnoutrefresh(list);
459 
460 	/* register the new window, along with its borders */
461 	dlg_mouse_mkbigregion(box_y + 1, box_x, use_height, list_width + 2,
462 			KEY_MAX, 1, 1, 1 /* by lines */ );
463 
464 	d4p_print_arrows(dialog,
465 			box_x, box_y,
466 			scrollamt, max_choice, item_no, use_height);
467 
468 	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, false, width);
469 
470 	while (result == DLG_EXIT_UNKNOWN) {
471 		if (button < 0)		/* --visit-items */
472 			wmove(dialog, box_y + choice + 1, box_x + check_x + 2);
473 
474 		key = dlg_mouse_wgetch(dialog, &fkey);
475 		if (dlg_result_key(key, fkey, &result))
476 			break;
477 
478 		was_mouse = (fkey && is_DLGK_MOUSE(key));
479 		if (was_mouse)
480 			key -= M_EVENT;
481 		if (was_mouse && (key >= KEY_MAX)) {
482 			getyx(dialog, cur_y, cur_x);
483 			i = (key - KEY_MAX);
484 			if (i < max_choice && items[i + scrollamt].type != ITEM_SEPARATOR ) {
485 				/* De-highlight current item */
486 				print_item(list,
487 						&items[scrollamt + choice],
488 						choice, false);
489 				/* Highlight new item */
490 				choice = (key - KEY_MAX);
491 				print_item(list,
492 						&items[scrollamt + choice],
493 						choice, true);
494 				(void) wnoutrefresh(list);
495 				(void) wmove(dialog, cur_y, cur_x);
496 
497 				key = ' ';	/* force the selected item to toggle */
498 			} else {
499 				beep();
500 				continue;
501 			}
502 			fkey = 0;
503 		} else if (was_mouse && key >= KEY_MIN) {
504 			key = dlg_lookup_key(dialog, key, &fkey);
505 		}
506 
507 		switch (key) {
508 			case CTRL_R:
509 				goto retry;
510 				break;
511 			case CTRL_E:
512 			case F1:
513 				dialog_state.use_shadow = false;
514 				d4p_helpwindow(" HELP ", dialog_vars.help_file, 0, 0);
515 				dialog_state.use_shadow = true;
516 				goto retry;
517 				break;
518 		}
519 
520 		/*
521 		 * A space toggles the item status.  We handle either a checklist
522 		 * (any number of items can be selected) or radio list (zero or one
523 		 * items can be selected).
524 		 */
525 		if (key == ' ') {
526 			int current = scrollamt + choice;
527 			int next = items[current].state + 1;
528 
529 			if (next >= 2)
530 				next = 0;
531 
532 			getyx(dialog, cur_y, cur_x);
533 			/* XXX need a case? */
534 			if (items[current].type == ITEM_CHECK) {	/* checklist? */
535 				items[current].state = next;
536 				print_item(list,
537 						&items[scrollamt + choice],
538 						choice, true);
539 			} else if (items[current].type == ITEM_RADIO) {		/* radiolist */
540 				for (i = 0; i < item_no; i++) {
541 					if (i != current && items[i].group == items[current].group)
542 							items[i].state = 0;
543 				}
544 				if (items[current].state) {
545 					items[current].state = 0;
546 					print_item(list,
547 							&items[current],
548 							choice, true);
549 				} else {
550 					items[current].state = 1;
551 					for (i = 0; i < max_choice; i++) {
552 						print_item(list,
553 								&items[scrollamt + i],
554 								i, i == choice);
555 					}
556 				}
557 			} else {
558 				// Something wrong ;(
559 				dlg_exiterr("ERROR: Trying push incorrect item!");
560 			}
561 			(void) wnoutrefresh(list);
562 			(void) wmove(dialog, cur_y, cur_x);
563 			wrefresh(dialog);
564 			continue;		/* wait for another key press */
565 		}
566 
567 		/*
568 		 * Check if key pressed matches first character of any item tag in
569 		 * list.  If there is more than one match, we will cycle through
570 		 * each one as the same key is pressed repeatedly.
571 		 */
572 		found = false;
573 		if (!fkey) {
574 			if (button < 0 || !dialog_state.visit_items) {
575 				for (j = scrollamt + choice + 1; j < item_no; j++) {
576 					if (dlg_match_char(dlg_last_getc(), items[j].name) &&
577 						items[j].type != ITEM_SEPARATOR ) {
578 							found = true;
579 							i = j - scrollamt;
580 							break;
581 					}
582 				}
583 				if (!found) {
584 					for (j = 0; j <= scrollamt + choice; j++) {
585 						if (dlg_match_char(dlg_last_getc(), items[j].name) &&
586 							items[j].type != ITEM_SEPARATOR) {
587 								found = true;
588 								i = j - scrollamt;
589 								break;
590 						}
591 					}
592 				}
593 				if (found)
594 					dlg_flush_getc();
595 			} else if ((j = dlg_char_to_button(key, buttons)) >= 0) {
596 				button = j;
597 				ungetch('\n');
598 				continue;
599 			}
600 		}
601 
602 		/*
603 		 * A single digit (1-9) positions the selection to that line in the
604 		 * current screen.
605 		 */
606 		if (!found
607 				&& (key <= '9')
608 				&& (key > '0')
609 				&& (key - '1' < max_choice)) {
610 			found = true;
611 			i = key - '1';
612 		}
613 
614 		if (!found) {
615 			if (fkey) {
616 				found = true;
617 				switch (key) {
618 					case DLGK_ITEM_FIRST:
619 						i = -scrollamt;
620 						if (items[0].type == ITEM_SEPARATOR)
621 							i++;
622 						break;
623 					case DLGK_ITEM_LAST:
624 						i = item_no - 1 - scrollamt;
625 						break;
626 					case DLGK_PAGE_PREV:
627 						if (choice) {
628 							i = 0;
629 							if (items[scrollamt].type == ITEM_SEPARATOR && scrollamt == 0)
630 								i++;
631 							else if (items[scrollamt].type == ITEM_SEPARATOR)
632 								i--;
633 						}
634 						else if (scrollamt != 0) {
635 							i = -MIN(scrollamt, max_choice);
636 							if (items[scrollamt + i].type == ITEM_SEPARATOR && (scrollamt + i) == 0)
637 								i++;
638 							else if (items[scrollamt + i].type == ITEM_SEPARATOR)
639 								i--;
640 							}
641 						else
642 							continue;
643 						break;
644 					case DLGK_PAGE_NEXT:
645 						i = MIN(choice + max_choice, item_no - scrollamt - 1);
646 						if (items[scrollamt + i].type == ITEM_SEPARATOR) {
647 							if (scrollamt + i + 1 < item_no)
648 								i++;
649 							else
650 								i--;
651 						}
652 						break;
653 					case DLGK_ITEM_PREV:
654 						i = choice - 1;
655 						if (choice == 0 && scrollamt == 0)
656 							continue;
657 						if (items[scrollamt + i].type == ITEM_SEPARATOR && (scrollamt + i) == 0)
658 							i++;
659 						else if (items[scrollamt + i].type == ITEM_SEPARATOR)
660 							i--;
661 						break;
662 					case DLGK_ITEM_NEXT:
663 						i = choice + 1;
664 						if (items[scrollamt + i].type == ITEM_SEPARATOR) {
665 							if (scrollamt + i + 1 < item_no)
666 								i++;
667 							else
668 								i--;
669 						}
670 						if (scrollamt + choice >= item_no - 1)
671 							continue;
672 						break;
673 					default:
674 						found = false;
675 						break;
676 				}
677 			}
678 		}
679 
680 		if (found) {
681 			if (i != choice) {
682 				getyx(dialog, cur_y, cur_x);
683 				if (i <= 0 || i >= max_choice) {
684 					{
685 						if (i <= 0) {
686 							if (items[0].type == ITEM_SEPARATOR && (scrollamt + i) == 1) {
687 								scrollamt += (i - 1);
688 								choice = 1;
689 							}
690 							else {
691 								scrollamt += i;
692 								choice = 0;
693 							}
694 						} else {
695 							choice = max_choice - 1;
696 							scrollamt += (i - max_choice + 1);
697 						}
698 						for (i = 0; i < max_choice; i++) {
699 							print_item(list,
700 									&items[scrollamt + i],
701 									i, i == choice);
702 						}
703 					}
704 					(void) wnoutrefresh(list);
705 					d4p_print_arrows(dialog,
706 							box_x, box_y,
707 							scrollamt, max_choice, item_no, use_height);
708 				} else {
709 					/* De-highlight current item */
710 					print_item(list,
711 							&items[scrollamt + choice],
712 							choice, false);
713 					/* Highlight new item */
714 					choice = i;
715 					print_item(list,
716 							&items[scrollamt + choice],
717 							choice, true);
718 					(void) wnoutrefresh(list);
719 					d4p_print_arrows(dialog,
720 							box_x, box_y,
721 							scrollamt, max_choice, item_no, use_height);
722 					(void) wmove(dialog, cur_y, cur_x);
723 					wrefresh(dialog);
724 				}
725 			}
726 			continue;		/* wait for another key press */
727 		}
728 
729 		if (fkey) {
730 			switch (key) {
731 				case DLGK_ENTER:
732 					result = dlg_enter_buttoncode(button);
733 					break;
734 				case DLGK_FIELD_PREV:
735 					button = dlg_prev_button(buttons, button);
736 					dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
737 							0, width);
738 					break;
739 				case DLGK_FIELD_NEXT:
740 					button = dlg_next_button(buttons, button);
741 					dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
742 							0, width);
743 					break;
744 #ifdef KEY_RESIZE
745 				case KEY_RESIZE:
746 					/* reset data */
747 					height = old_height;
748 					width = old_width;
749 					/* repaint */
750 					goto retry;
751 #endif
752 				default:
753 					if (was_mouse) {
754 						if ((key2 = dlg_ok_buttoncode(key)) >= 0) {
755 							result = key2;
756 							break;
757 						}
758 						beep();
759 					}
760 			}
761 		} else {
762 			beep();
763 		}
764 	}
765 
766 	dlg_clear();
767 	dlg_del_window(dialog);
768 	dlg_mouse_free_regions();
769 	free(prompt);
770 	return result;
771 }
772