Ejemplo n.º 1
0
/*
 * Display text from a file in a dialog box, like in a "tail -f".
 */
int
dialog_tailbox(const char *title, const char *file, int height, int width, int bg_task)
{
    /* *INDENT-OFF* */
    static DLG_KEYS_BINDING binding[] = {
	HELPKEY_BINDINGS,
	ENTERKEY_BINDINGS,
	DLG_KEYS_DATA( DLGK_BEGIN,      '0' ),
	DLG_KEYS_DATA( DLGK_BEGIN,      KEY_BEG ),
	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'H' ),
	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'h' ),
	DLG_KEYS_DATA( DLGK_GRID_LEFT,  KEY_LEFT ),
	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'L' ),
	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
	DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
	END_KEYS_BINDING
    };
    /* *INDENT-ON* */

#ifdef KEY_RESIZE
    int old_height = height;
    int old_width = width;
#endif
    int fkey;
    int x, y, result, thigh;
    WINDOW *dialog, *text;
    const char **buttons = 0;
    MY_OBJ *obj;
    FILE *fd;
    int min_width = 12;

    /* Open input file for reading */
    if ((fd = fopen(file, "rb")) == NULL)
	dlg_exiterr("Can't open input file in dialog_tailbox().");

#ifdef KEY_RESIZE
  retry:
#endif
    dlg_auto_sizefile(title, file, &height, &width, 2, min_width);
    dlg_print_size(height, width);
    dlg_ctl_size(height, width);

    x = dlg_box_x_ordinate(width);
    y = dlg_box_y_ordinate(height);
    thigh = height - ((2 * MARGIN) + (bg_task ? 0 : 2));

    dialog = dlg_new_window(height, width, y, x);

    dlg_mouse_setbase(x, y);

    /* Create window for text region, used for scrolling text */
    text = dlg_sub_window(dialog,
			  thigh,
			  width - (2 * MARGIN),
			  y + MARGIN,
			  x + MARGIN);

    dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
    dlg_draw_bottom_box(dialog);
    dlg_draw_title(dialog, title);
    dlg_draw_helpline(dialog, FALSE);

    if (!bg_task) {
	buttons = dlg_exit_label();
	dlg_button_layout(buttons, &min_width);
	dlg_draw_buttons(dialog, height - (2 * MARGIN), 0, buttons, FALSE,
			 FALSE, width);
    }

    (void) wmove(dialog, thigh, (MARGIN + 1));
    (void) wnoutrefresh(dialog);

    obj = dlg_calloc(MY_OBJ, 1);
    assert_ptr(obj, "dialog_tailbox");

    obj->obj.input = fd;
    obj->obj.win = dialog;
    obj->obj.handle_getc = handle_my_getc;
    obj->obj.handle_input = bg_task ? handle_input : 0;
    obj->obj.keep_bg = bg_task && dialog_vars.cant_kill;
    obj->obj.bg_task = bg_task;
    obj->text = text;
    obj->buttons = buttons;
    dlg_add_callback(&(obj->obj));

    dlg_register_window(dialog, "tailbox", binding);
    dlg_register_buttons(dialog, "tailbox", buttons);

    /* Print last page of text */
    dlg_attr_clear(text, thigh, getmaxx(text), dialog_attr);
    repaint_text(obj);

    if (bg_task) {
	result = DLG_EXIT_OK;
    } else {
	int ch;
	do {
	    ch = dlg_getc(dialog, &fkey);
#ifdef KEY_RESIZE
	    if (fkey && ch == KEY_RESIZE) {
		/* reset data */
		height = old_height;
		width = old_width;
		/* repaint */
		dlg_clear();
		dlg_del_window(dialog);
		refresh();
		dlg_mouse_free_regions();
		dlg_button_layout(buttons, &min_width);
		goto retry;
	    }
#endif
	}
	while (handle_my_getc(&(obj->obj), ch, fkey, &result));
    }
    dlg_mouse_free_regions();
    return result;
}
Ejemplo n.º 2
0
/*
 * Display a dialog box for entering a date
 */
int
dialog_calendar(const char *title,
		const char *subtitle,
		int height,
		int width,
		int day,
		int month,
		int year)
{
    /* *INDENT-OFF* */
    static DLG_KEYS_BINDING binding[] = {
	HELPKEY_BINDINGS,
	ENTERKEY_BINDINGS,
	DLG_KEYS_DATA( DLGK_ENTER,	' ' ),
	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
	DLG_KEYS_DATA( DLGK_GRID_DOWN,	'j' ),
	DLG_KEYS_DATA( DLGK_GRID_DOWN,	DLGK_MOUSE(KEY_NPAGE) ),
	DLG_KEYS_DATA( DLGK_GRID_DOWN,	KEY_DOWN ),
	DLG_KEYS_DATA( DLGK_GRID_DOWN,	KEY_NPAGE ),
	DLG_KEYS_DATA( DLGK_GRID_LEFT,	'-' ),
	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'h' ),
	DLG_KEYS_DATA( DLGK_GRID_LEFT,  CHR_BACKSPACE ),
	DLG_KEYS_DATA( DLGK_GRID_LEFT,  CHR_PREVIOUS ),
	DLG_KEYS_DATA( DLGK_GRID_LEFT,  KEY_LEFT ),
	DLG_KEYS_DATA( DLGK_GRID_RIGHT,	'+' ),
	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
	DLG_KEYS_DATA( DLGK_GRID_RIGHT, CHR_NEXT ),
	DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_NEXT ),
	DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
	DLG_KEYS_DATA( DLGK_GRID_UP,	'k' ),
	DLG_KEYS_DATA( DLGK_GRID_UP,	KEY_PPAGE ),
	DLG_KEYS_DATA( DLGK_GRID_UP,	KEY_PREVIOUS ),
	DLG_KEYS_DATA( DLGK_GRID_UP,	KEY_UP ),
	DLG_KEYS_DATA( DLGK_GRID_UP,  	DLGK_MOUSE(KEY_PPAGE) ),
	END_KEYS_BINDING
    };
    /* *INDENT-ON* */

#ifdef KEY_RESIZE
    int old_height = height;
    int old_width = width;
#endif
    BOX dy_box, mn_box, yr_box;
    int fkey;
    int key = 0;
    int key2;
    int step;
    int button;
    int result = DLG_EXIT_UNKNOWN;
    WINDOW *dialog;
    time_t now_time = time((time_t *) 0);
    struct tm current;
    int state = dlg_defaultno_button();
    const char **buttons = dlg_ok_labels();
    char *prompt = dlg_strclone(subtitle);
    int mincols = MIN_WIDE;
    char buffer[MAX_LEN];
    DIALOG_VARS save_vars;

    dlg_save_vars(&save_vars);
    dialog_vars.separate_output = TRUE;

    dlg_does_output();

    now_time = time((time_t *) 0);
    current = *localtime(&now_time);
    if (day < 0)
	day = current.tm_mday;
    if (month < 0)
	month = current.tm_mon + 1;
    if (year < 0)
	year = current.tm_year + 1900;

    /* compute a struct tm that matches the day/month/year parameters */
    if (((year -= 1900) > 0) && (year < 200)) {
	/* ugly, but I'd like to run this on older machines w/o mktime -TD */
	for (;;) {
	    if (year > current.tm_year) {
		now_time += ONE_DAY * days_in_year(&current, 0);
	    } else if (year < current.tm_year) {
		now_time -= ONE_DAY * days_in_year(&current, -1);
	    } else if (month > current.tm_mon + 1) {
		now_time += ONE_DAY * days_in_month(&current, 0);
	    } else if (month < current.tm_mon + 1) {
		now_time -= ONE_DAY * days_in_month(&current, -1);
	    } else if (day > current.tm_mday) {
		now_time += ONE_DAY;
	    } else if (day < current.tm_mday) {
		now_time -= ONE_DAY;
	    } else {
		break;
	    }
	    current = *localtime(&now_time);
	}
    }
    dlg_button_layout(buttons, &mincols);

#ifdef KEY_RESIZE
  retry:
#endif

    dlg_auto_size(title, prompt, &height, &width, 0, mincols);
    height += MIN_HIGH - 1;
    dlg_print_size(height, width);
    dlg_ctl_size(height, width);

    dialog = dlg_new_window(height, width,
			    dlg_box_y_ordinate(height),
			    dlg_box_x_ordinate(width));
    dlg_register_window(dialog, "calendar", binding);
    dlg_register_buttons(dialog, "calendar", buttons);

    /* mainbox */
    dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
    dlg_draw_bottom_box(dialog);
    dlg_draw_title(dialog, title);

    wattrset(dialog, dialog_attr);	/* text mainbox */
    dlg_print_autowrap(dialog, prompt, height, width);

    /* compute positions of day, month and year boxes */
    memset(&dy_box, 0, sizeof(dy_box));
    memset(&mn_box, 0, sizeof(mn_box));
    memset(&yr_box, 0, sizeof(yr_box));

    if (init_object(&dy_box,
		    dialog,
		    (width - DAY_WIDE) / 2,
		    1 + (height - (DAY_HIGH + BTN_HIGH + (5 * MARGIN))),
		    DAY_WIDE,
		    DAY_HIGH + 1,
		    draw_day,
		    'D') < 0
	|| DrawObject(&dy_box) < 0) {
	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
    }

    if (init_object(&mn_box,
		    dialog,
		    dy_box.x,
		    dy_box.y - (HDR_HIGH + 2 * MARGIN),
		    (DAY_WIDE / 2) - MARGIN,
		    HDR_HIGH,
		    draw_month,
		    'M') < 0
	|| DrawObject(&mn_box) < 0) {
	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
    }

    if (init_object(&yr_box,
		    dialog,
		    dy_box.x + mn_box.width + 2,
		    mn_box.y,
		    mn_box.width,
		    mn_box.height,
		    draw_year,
		    'Y') < 0
	|| DrawObject(&yr_box) < 0) {
	return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
    }

    while (result == DLG_EXIT_UNKNOWN) {
	BOX *obj = (state == sDAY ? &dy_box
		    : (state == sMONTH ? &mn_box :
		       (state == sYEAR ? &yr_box : 0)));

	button = (state < 0) ? 0 : state;
	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
	if (obj != 0)
	    dlg_set_focus(dialog, obj->window);

	key = dlg_mouse_wgetch(dialog, &fkey);
	if (dlg_result_key(key, fkey, &result))
	    break;

	if (fkey && (key >= DLGK_MOUSE(KEY_MIN) && key <= DLGK_MOUSE(KEY_MAX))) {
	    key = dlg_lookup_key(dialog, key - M_EVENT, &fkey);
	}

	if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
	    result = key2;
	} else if (fkey) {
	    /* handle function-keys */
	    switch (key) {
	    case DLGK_MOUSE('D'):
		state = sDAY;
		break;
	    case DLGK_MOUSE('M'):
		state = sMONTH;
		break;
	    case DLGK_MOUSE('Y'):
		state = sYEAR;
		break;
	    case DLGK_ENTER:
		result = dlg_enter_buttoncode(button);
		break;
	    case DLGK_FIELD_PREV:
		state = dlg_prev_ok_buttonindex(state, sMONTH);
		break;
	    case DLGK_FIELD_NEXT:
		state = dlg_next_ok_buttonindex(state, sMONTH);
		break;
#ifdef KEY_RESIZE
	    case KEY_RESIZE:
		/* reset data */
		height = old_height;
		width = old_width;
		/* repaint */
		dlg_clear();
		dlg_del_window(dialog);
		refresh();
		dlg_mouse_free_regions();
		goto retry;
#endif
	    default:
		step = 0;
		key2 = -1;
		if (is_DLGK_MOUSE(key)) {
		    if ((key2 = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
			result = key2;
			break;
		    } else if (key >= DLGK_MOUSE(KEY_MAX)) {
			state = sDAY;
			obj = &dy_box;
			key2 = 1;
			step = (key
				- DLGK_MOUSE(KEY_MAX)
				- day_cell_number(&current));
		    }
		}
		if (obj != 0) {
		    if (key2 < 0)
			step = next_or_previous(key, (obj == &dy_box));
		    if (step != 0) {
			struct tm old = current;

			/* see comment regarding mktime -TD */
			if (obj == &dy_box) {
			    now_time += ONE_DAY * step;
			} else if (obj == &mn_box) {
			    if (step > 0)
				now_time += ONE_DAY *
				    days_in_month(&current, 0);
			    else
				now_time -= ONE_DAY *
				    days_in_month(&current, -1);
			} else if (obj == &yr_box) {
			    if (step > 0)
				now_time += (ONE_DAY
					     * days_in_year(&current, 0));
			    else
				now_time -= (ONE_DAY
					     * days_in_year(&current, -1));
			}

			current = *localtime(&now_time);

			if (obj != &dy_box
			    && (current.tm_mday != old.tm_mday
				|| current.tm_mon != old.tm_mon
				|| current.tm_year != old.tm_year))
			    DrawObject(&dy_box);
			if (obj != &mn_box && current.tm_mon != old.tm_mon)
			    DrawObject(&mn_box);
			if (obj != &yr_box && current.tm_year != old.tm_year)
			    DrawObject(&yr_box);
			(void) DrawObject(obj);
		    }
		} else if (state >= 0) {
		    if (next_or_previous(key, FALSE) < 0)
			state = dlg_prev_ok_buttonindex(state, sMONTH);
		    else if (next_or_previous(key, FALSE) > 0)
			state = dlg_next_ok_buttonindex(state, sMONTH);
		}
		break;
	    }
	}
    }

#define DefaultFormat(dst, src) \
	sprintf(dst, "%02d/%02d/%0d", \
		src.tm_mday, src.tm_mon + 1, src.tm_year + 1900)
#ifdef HAVE_STRFTIME
    if (dialog_vars.date_format != 0) {
	size_t used = strftime(buffer,
			       sizeof(buffer) - 1,
			       dialog_vars.date_format,
			       &current);
	if (used == 0 || *buffer == '\0')
	    DefaultFormat(buffer, current);
    } else
#endif
	DefaultFormat(buffer, current);

    dlg_add_result(buffer);
    dlg_add_separator();

    return CleanupResult(result, dialog, prompt, &save_vars);
}
Ejemplo n.º 3
0
int
diskeditor_show(const char *title, const char *cprompt,
    struct partedit_item *items, int nitems, int *selected, int *nscroll)
{
	WINDOW *dialog, *partitions;
	char *prompt;
	const char *buttons[] =
	    { "Create", "Delete", "Modify", "Revert", "Auto", "Finish", NULL };
	const char *help_text[] = {
	    "Add a new partition", "Delete selected partition or partitions",
	    "Change partition type or mountpoint",
	    "Revert changes to disk setup", "Use guided partitioning tool",
	    "Exit partitioner (will ask whether to save changes)", NULL };
	int x, y;
	int i;
	int height, width, min_width;
	int partlist_height, partlist_width;
	int cur_scroll = 0;
	int key, fkey;
	int cur_button = 5, cur_part = 0;
	int result = DLG_EXIT_UNKNOWN;

	static DLG_KEYS_BINDING binding[] = {
		ENTERKEY_BINDINGS,
		DLG_KEYS_DATA( DLGK_ENTER,      ' ' ),
		DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ),
		DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ),
		DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
		DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
		DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
		DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),

		SCROLLKEY_BINDINGS,
		END_KEYS_BINDING
	};

	/*
	 * Set up editor window.
	 */
	prompt = dlg_strclone(cprompt);

	min_width = 50;
	height = width = 0;
	partlist_height = 10;
	dlg_tab_correct_str(prompt);
	dlg_button_layout(buttons, &min_width);
	dlg_auto_size(title, prompt, &height, &width, 2, min_width);
	height += partlist_height;
	partlist_width = width - 2*MARGIN;
	dlg_print_size(height, width);
	dlg_ctl_size(height, width);

	x = dlg_box_x_ordinate(width);
	y = dlg_box_y_ordinate(height);

	dialog = dlg_new_window(height, width, y, x);
	dlg_register_window(dialog, "diskeditorbox", binding);
	dlg_register_buttons(dialog, "diskeditorbox", buttons);

	dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
	dlg_draw_bottom_box(dialog);
	dlg_draw_title(dialog, title);
	wattrset(dialog, dialog_attr);

	/* Partition list sub-window */
	partitions = dlg_sub_window(dialog, partlist_height, partlist_width,
	    y + 3, x + 1);
	dlg_register_window(partitions, "partlist", binding);
	dlg_register_buttons(partitions, "partlist", buttons);
	wattrset(partitions, menubox_attr);

	dlg_item_help(help_text[cur_button]);
	dlg_draw_buttons(dialog, height - 2*MARGIN, 0, buttons,
	    cur_button, FALSE, width);
	dlg_print_autowrap(dialog, prompt, height, width);

	if (selected != NULL)
		cur_part = *selected;
	if (nscroll != NULL)
		cur_scroll = *nscroll;
	if (cur_part - cur_scroll >= partlist_height - 2 ||
	    cur_part - cur_scroll < 0)
		cur_scroll = cur_part;

repaint:
	dlg_draw_box(dialog, 3, 1,  partlist_height, partlist_width,
	    menubox_border_attr, menubox_attr);
	for (i = cur_scroll; i < MIN(cur_scroll + partlist_height - 2, nitems);
	    i++)
		print_partedit_item(partitions, items, i, cur_scroll,
		    i == cur_part);
	if (nitems > partlist_height - 2)
		dlg_draw_arrows(partitions, cur_scroll > 0,
		    nitems > cur_scroll + partlist_height - 2,
		    partlist_width - 5, 0, partlist_height - 1);
	wrefresh(partitions);

	while (result == DLG_EXIT_UNKNOWN) {
		key = dlg_mouse_wgetch(dialog, &fkey);
		if ((i = dlg_char_to_button(key, buttons)) >= 0) {
			cur_button = i;
			dlg_item_help(help_text[cur_button]);
			dlg_draw_buttons(dialog, height - 2*MARGIN, 0, buttons,
			    cur_button, FALSE, width);
			break;
		}

		if (!fkey)
			continue;

		switch (key) {
		case DLGK_FIELD_NEXT:
			cur_button = dlg_next_button(buttons, cur_button);
			if (cur_button < 0)
				cur_button = 0;
			dlg_item_help(help_text[cur_button]);
			dlg_draw_buttons(dialog, height - 2*MARGIN, 0, buttons,
			    cur_button, FALSE, width);
			break;
		case DLGK_FIELD_PREV:
			cur_button = dlg_prev_button(buttons, cur_button);
			if (cur_button < 0)
				cur_button = 0;
			dlg_item_help(help_text[cur_button]);
			dlg_draw_buttons(dialog, height - 2*MARGIN, 0, buttons,
			    cur_button, FALSE, width);
			break;
		case DLGK_ITEM_NEXT:
			if (cur_part == nitems - 1)
				break; /* End of list */

			/* Deselect old item */
			print_partedit_item(partitions, items, cur_part,
			    cur_scroll, 0);
			/* Select new item */
			cur_part++;
			if (cur_part - cur_scroll >= partlist_height - 2) {
				cur_scroll = cur_part;
				goto repaint;
			}
			print_partedit_item(partitions, items, cur_part,
			    cur_scroll, 1);
			wrefresh(partitions);
			break;
		case DLGK_ITEM_PREV:
			if (cur_part == 0)
				break; /* Start of list */

			/* Deselect old item */
			print_partedit_item(partitions, items, cur_part,
			    cur_scroll, 0);
			/* Select new item */
			cur_part--;
			if (cur_part - cur_scroll < 0) {
				cur_scroll = cur_part;
				goto repaint;
			}
			print_partedit_item(partitions, items, cur_part,
			    cur_scroll, 1);
			wrefresh(partitions);
			break;
		case DLGK_PAGE_NEXT:
			cur_scroll += (partlist_height - 2);
			if (cur_scroll + partlist_height - 2 >= nitems)
				cur_scroll = nitems - (partlist_height - 2);
			if (cur_scroll < 0)
				cur_scroll = 0;
			if (cur_part < cur_scroll)
				cur_part = cur_scroll;
			goto repaint;
		case DLGK_PAGE_PREV:
			cur_scroll -= (partlist_height - 2);
			if (cur_scroll < 0)
				cur_scroll = 0;
			if (cur_part >= cur_scroll + partlist_height - 2)
				cur_part = cur_scroll;
			goto repaint;
		case DLGK_PAGE_FIRST:
			cur_scroll = 0;
			cur_part = cur_scroll;
			goto repaint;
		case DLGK_PAGE_LAST:
			cur_scroll = nitems - (partlist_height - 2);
			if (cur_scroll < 0)
				cur_scroll = 0;
			cur_part = cur_scroll;
			goto repaint;
		case DLGK_ENTER:
			goto done;
		default:
			if (is_DLGK_MOUSE(key)) {
				cur_button = key - M_EVENT;
				dlg_item_help(help_text[cur_button]);
				dlg_draw_buttons(dialog, height - 2*MARGIN, 0,
				    buttons, cur_button, FALSE, width);
				goto done;
			}
			break;
		}
	}

done:
	if (selected != NULL)
		*selected = cur_part;
	if (nscroll != NULL)
		*nscroll = cur_scroll;

	dlg_del_window(partitions);
	dlg_del_window(dialog);
	dlg_mouse_free_regions();

	return (cur_button);
}
Ejemplo n.º 4
0
/*
 * This is an alternate interface to 'checklist' which allows the application
 * to read the list item states back directly without putting them in the
 * output buffer.  It also provides for more than two states over which the
 * check/radio box can display.
 */
int
dlg_checklist(const char *title,
	      const char *cprompt,
	      int height,
	      int width,
	      int list_height,
	      int item_no,
	      DIALOG_LISTITEM * items,
	      const char *states,
	      int flag,
	      int *current_item)
{
    /* *INDENT-OFF* */
    static DLG_KEYS_BINDING binding[] = {
	HELPKEY_BINDINGS,
	ENTERKEY_BINDINGS,
	DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
	DLG_KEYS_DATA( DLGK_ITEM_FIRST, KEY_HOME ),
	DLG_KEYS_DATA( DLGK_ITEM_LAST,	KEY_END ),
	DLG_KEYS_DATA( DLGK_ITEM_LAST,	KEY_LL ),
	DLG_KEYS_DATA( DLGK_ITEM_NEXT,	'+' ),
	DLG_KEYS_DATA( DLGK_ITEM_NEXT,	KEY_DOWN ),
	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  CHR_NEXT ),
	DLG_KEYS_DATA( DLGK_ITEM_PREV,	'-' ),
	DLG_KEYS_DATA( DLGK_ITEM_PREV,	KEY_UP ),
	DLG_KEYS_DATA( DLGK_ITEM_PREV,  CHR_PREVIOUS ),
	DLG_KEYS_DATA( DLGK_PAGE_NEXT,	KEY_NPAGE ),
	DLG_KEYS_DATA( DLGK_PAGE_NEXT,	DLGK_MOUSE(KEY_NPAGE) ),
	DLG_KEYS_DATA( DLGK_PAGE_PREV,	KEY_PPAGE ),
	DLG_KEYS_DATA( DLGK_PAGE_PREV,	DLGK_MOUSE(KEY_PPAGE) ),
	END_KEYS_BINDING
    };
    /* *INDENT-ON* */

#ifdef KEY_RESIZE
    int old_height = height;
    int old_width = width;
#endif
    int i, j, key2, found, x, y, cur_x, cur_y, box_x, box_y;
    int key = 0, fkey;
    int button = dialog_state.visit_items ? -1 : dlg_defaultno_button();
    int choice = dlg_default_listitem(items);
    int scrollamt = 0;
    int max_choice;
    int was_mouse;
    int use_height;
    int use_width, name_width, text_width;
    int result = DLG_EXIT_UNKNOWN;
    int num_states;
    WINDOW *dialog, *list;
    char *prompt = dlg_strclone(cprompt);
    const char **buttons = dlg_ok_labels();

    dlg_does_output();
    dlg_tab_correct_str(prompt);

    /*
     * If this is a radiobutton list, ensure that no more than one item is
     * selected initially.  Allow none to be selected, since some users may
     * wish to provide this flavor.
     */
    if (flag == FLAG_RADIO) {
	bool first = TRUE;

	for (i = 0; i < item_no; i++) {
	    if (items[i].state) {
		if (first) {
		    first = FALSE;
		} else {
		    items[i].state = 0;
		}
	    }
	}
    }
#ifdef KEY_RESIZE
  retry:
#endif

    use_height = list_height;
    if (use_height == 0) {
	use_width = dlg_calc_list_width(item_no, items) + 10;
	/* calculate height without items (4) */
	dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MAX(26, use_width));
	dlg_calc_listh(&height, &use_height, item_no);
    } else {
	dlg_auto_size(title, prompt, &height, &width, MIN_HIGH + use_height, 26);
    }
    dlg_button_layout(buttons, &width);
    dlg_print_size(height, width);
    dlg_ctl_size(height, width);

    /* we need at least two states */
    if (states == 0 || strlen(states) < 2)
	states = " *";
    num_states = (int) strlen(states);

    checkflag = flag;

    x = dlg_box_x_ordinate(width);
    y = dlg_box_y_ordinate(height);

    dialog = dlg_new_window(height, width, y, x);
    dlg_register_window(dialog, "checklist", binding);
    dlg_register_buttons(dialog, "checklist", buttons);

    dlg_mouse_setbase(x, y);

    dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
    dlg_draw_bottom_box(dialog);
    dlg_draw_title(dialog, title);

    wattrset(dialog, dialog_attr);
    dlg_print_autowrap(dialog, prompt, height, width);

    list_width = width - 6;
    getyx(dialog, cur_y, cur_x);
    box_y = cur_y + 1;
    box_x = (width - list_width) / 2 - 1;

    /*
     * After displaying the prompt, we know how much space we really have.
     * Limit the list to avoid overwriting the ok-button.
     */
    if (use_height + MIN_HIGH > height - cur_y)
	use_height = height - MIN_HIGH - cur_y;
    if (use_height <= 0)
	use_height = 1;

    max_choice = MIN(use_height, item_no);

    /* create new window for the list */
    list = dlg_sub_window(dialog, use_height, list_width,
			  y + box_y + 1, x + box_x + 1);

    /* draw a box around the list items */
    dlg_draw_box(dialog, box_y, box_x,
		 use_height + 2 * MARGIN,
		 list_width + 2 * MARGIN,
		 menubox_border_attr, menubox_attr);

    text_width = 0;
    name_width = 0;
    /* Find length of longest item to center checklist */
    for (i = 0; i < item_no; i++) {
	text_width = MAX(text_width, dlg_count_columns(items[i].text));
	name_width = MAX(name_width, dlg_count_columns(items[i].name));
    }

    /* If the name+text is wider than the list is allowed, then truncate
     * one or both of them.  If the name is no wider than 1/4 of the list,
     * leave it intact.
     */
    use_width = (list_width - 6);
    if (text_width + name_width > use_width) {
	int need = (int) (0.25 * use_width);
	if (name_width > need) {
	    int want = (int) (use_width * ((double) name_width) /
			      (text_width + name_width));
	    name_width = (want > need) ? want : need;
	}
	text_width = use_width - name_width;
    }

    check_x = (use_width - (text_width + name_width)) / 2;
    item_x = name_width + check_x + 6;

    /* ensure we are scrolled to show the current choice */
    if (choice >= (max_choice + scrollamt)) {
	scrollamt = choice - max_choice + 1;
	choice = max_choice - 1;
    }
    /* Print the list */
    for (i = 0; i < max_choice; i++) {
	print_item(list,
		   &items[i + scrollamt],
		   states,
		   i, i == choice);
    }
    (void) wnoutrefresh(list);

    /* register the new window, along with its borders */
    dlg_mouse_mkbigregion(box_y + 1, box_x, use_height, list_width + 2,
			  KEY_MAX, 1, 1, 1 /* by lines */ );

    print_arrows(dialog,
		 box_x, box_y,
		 scrollamt, max_choice, item_no, use_height);

    dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);

    while (result == DLG_EXIT_UNKNOWN) {
	if (button < 0)		/* --visit-items */
	    wmove(dialog, box_y + choice + 1, box_x + check_x + 2);

	key = dlg_mouse_wgetch(dialog, &fkey);
	if (dlg_result_key(key, fkey, &result))
	    break;

	was_mouse = (fkey && is_DLGK_MOUSE(key));
	if (was_mouse)
	    key -= M_EVENT;

	if (was_mouse && (key >= KEY_MAX)) {
	    getyx(dialog, cur_y, cur_x);
	    i = (key - KEY_MAX);
	    if (i < max_choice) {
		/* De-highlight current item */
		print_item(list,
			   &items[scrollamt + choice],
			   states,
			   choice, FALSE);
		/* Highlight new item */
		choice = (key - KEY_MAX);
		print_item(list,
			   &items[scrollamt + choice],
			   states,
			   choice, TRUE);
		(void) wnoutrefresh(list);
		(void) wmove(dialog, cur_y, cur_x);

		key = ' ';	/* force the selected item to toggle */
	    } else {
		beep();
		continue;
	    }
	    fkey = FALSE;
	} else if (was_mouse && key >= KEY_MIN) {
	    key = dlg_lookup_key(dialog, key, &fkey);
	}

	/*
	 * A space toggles the item status.  We handle either a checklist
	 * (any number of items can be selected) or radio list (zero or one
	 * items can be selected).
	 */
	if (key == ' ') {
	    int current = scrollamt + choice;
	    int next = items[current].state + 1;

	    if (next >= num_states)
		next = 0;

	    getyx(dialog, cur_y, cur_x);
	    if (flag == FLAG_CHECK) {	/* checklist? */
		items[current].state = next;
		print_item(list,
			   &items[scrollamt + choice],
			   states,
			   choice, TRUE);
	    } else {		/* radiolist */
		for (i = 0; i < item_no; i++) {
		    if (i != current) {
			items[i].state = 0;
		    }
		}
		if (items[current].state) {
		    items[current].state = next ? next : 1;
		    print_item(list,
			       &items[current],
			       states,
			       choice, TRUE);
		} else {
		    items[current].state = 1;
		    for (i = 0; i < max_choice; i++)
			print_item(list,
				   &items[scrollamt + i],
				   states,
				   i, i == choice);
		}
	    }
	    (void) wnoutrefresh(list);
	    (void) wmove(dialog, cur_y, cur_x);
	    wrefresh(dialog);
	    continue;		/* wait for another key press */
	}

	/*
	 * Check if key pressed matches first character of any item tag in
	 * list.  If there is more than one match, we will cycle through
	 * each one as the same key is pressed repeatedly.
	 */
	found = FALSE;
	if (!fkey) {
	    if (button < 0 || !dialog_state.visit_items) {
		for (j = scrollamt + choice + 1; j < item_no; j++) {
		    if (dlg_match_char(dlg_last_getc(), items[j].name)) {
			found = TRUE;
			i = j - scrollamt;
			break;
		    }
		}
		if (!found) {
		    for (j = 0; j <= scrollamt + choice; j++) {
			if (dlg_match_char(dlg_last_getc(), items[j].name)) {
			    found = TRUE;
			    i = j - scrollamt;
			    break;
			}
		    }
		}
		if (found)
		    dlg_flush_getc();
	    } else if ((j = dlg_char_to_button(key, buttons)) >= 0) {
		button = j;
		ungetch('\n');
		continue;
	    }
	}

	/*
	 * A single digit (1-9) positions the selection to that line in the
	 * current screen.
	 */
	if (!found
	    && (key <= '9')
	    && (key > '0')
	    && (key - '1' < max_choice)) {
	    found = TRUE;
	    i = key - '1';
	}

	if (!found) {
	    if (fkey) {
		found = TRUE;
		switch (key) {
		case DLGK_ITEM_FIRST:
		    i = -scrollamt;
		    break;
		case DLGK_ITEM_LAST:
		    i = item_no - 1 - scrollamt;
		    break;
		case DLGK_PAGE_PREV:
		    if (choice)
			i = 0;
		    else if (scrollamt != 0)
			i = -MIN(scrollamt, max_choice);
		    else
			continue;
		    break;
		case DLGK_PAGE_NEXT:
		    i = MIN(choice + max_choice, item_no - scrollamt - 1);
		    break;
		case DLGK_ITEM_PREV:
		    i = choice - 1;
		    if (choice == 0 && scrollamt == 0)
			continue;
		    break;
		case DLGK_ITEM_NEXT:
		    i = choice + 1;
		    if (scrollamt + choice >= item_no - 1)
			continue;
		    break;
		default:
		    found = FALSE;
		    break;
		}
	    }
	}

	if (found) {
	    if (i != choice) {
		getyx(dialog, cur_y, cur_x);
		if (i < 0 || i >= max_choice) {
#if defined(NCURSES_VERSION_MAJOR) && NCURSES_VERSION_MAJOR < 5
		    /*
		     * Using wscrl to assist ncurses scrolling is not needed
		     * in version 5.x
		     */
		    if (i == -1) {
			if (use_height > 1) {
			    /* De-highlight current first item */
			    print_item(list,
				       &items[scrollamt],
				       states,
				       0, FALSE);
			    scrollok(list, TRUE);
			    wscrl(list, -1);
			    scrollok(list, FALSE);
			}
			scrollamt--;
			print_item(list,
				   &items[scrollamt],
				   states,
				   0, TRUE);
		    } else if (i == max_choice) {
			if (use_height > 1) {
			    /* De-highlight current last item before scrolling up */
			    print_item(list,
				       &items[scrollamt + max_choice - 1],
				       states,
				       max_choice - 1, FALSE);
			    scrollok(list, TRUE);
			    wscrl(list, 1);
			    scrollok(list, FALSE);
			}
			scrollamt++;
			print_item(list,
				   &items[scrollamt + max_choice - 1],
				   states,
				   max_choice - 1, TRUE);
		    } else
#endif
		    {
			if (i < 0) {
			    scrollamt += i;
			    choice = 0;
			} else {
			    choice = max_choice - 1;
			    scrollamt += (i - max_choice + 1);
			}
			for (i = 0; i < max_choice; i++) {
			    print_item(list,
				       &items[scrollamt + i],
				       states,
				       i, i == choice);
			}
		    }
		    (void) wnoutrefresh(list);
		    print_arrows(dialog,
				 box_x, box_y,
				 scrollamt, max_choice, item_no, use_height);
		} else {
		    /* De-highlight current item */
		    print_item(list,
			       &items[scrollamt + choice],
			       states,
			       choice, FALSE);
		    /* Highlight new item */
		    choice = i;
		    print_item(list,
			       &items[scrollamt + choice],
			       states,
			       choice, TRUE);
		    (void) wnoutrefresh(list);
		    print_arrows(dialog,
				 box_x, box_y,
				 scrollamt, max_choice, item_no, use_height);
		    (void) wmove(dialog, cur_y, cur_x);
		    wrefresh(dialog);
		}
	    }
	    continue;		/* wait for another key press */
	}

	if (fkey) {
	    switch (key) {
	    case DLGK_ENTER:
		result = dlg_enter_buttoncode(button);
		break;
	    case DLGK_FIELD_PREV:
		button = dlg_prev_button(buttons, button);
		dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
				 FALSE, width);
		break;
	    case DLGK_FIELD_NEXT:
		button = dlg_next_button(buttons, button);
		dlg_draw_buttons(dialog, height - 2, 0, buttons, button,
				 FALSE, width);
		break;
#ifdef KEY_RESIZE
	    case KEY_RESIZE:
		/* reset data */
		height = old_height;
		width = old_width;
		/* repaint */
		dlg_clear();
		dlg_del_window(dialog);
		refresh();
		dlg_mouse_free_regions();
		goto retry;
#endif
	    default:
		if (was_mouse) {
		    if ((key2 = dlg_ok_buttoncode(key)) >= 0) {
			result = key2;
			break;
		    }
		    beep();
		}
	    }
	} else {
	    beep();
	}
    }

    dlg_del_window(dialog);
    dlg_mouse_free_regions();
    free(prompt);
    *current_item = (scrollamt + choice);
    return result;
}
Ejemplo n.º 5
0
/*
 * Display text from a file in a dialog box.
 */
int
dialog_textbox(const char *title, const char *file, int height, int width)
{
    /* *INDENT-OFF* */
    static DLG_KEYS_BINDING binding[] = {
	HELPKEY_BINDINGS,
	ENTERKEY_BINDINGS,
	DLG_KEYS_DATA( DLGK_GRID_DOWN,  'J' ),
	DLG_KEYS_DATA( DLGK_GRID_DOWN,  'j' ),
	DLG_KEYS_DATA( DLGK_GRID_DOWN,  KEY_DOWN ),
	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'H' ),
	DLG_KEYS_DATA( DLGK_GRID_LEFT,  'h' ),
	DLG_KEYS_DATA( DLGK_GRID_LEFT,  KEY_LEFT ),
	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'L' ),
	DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
	DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
	DLG_KEYS_DATA( DLGK_GRID_UP,    'K' ),
	DLG_KEYS_DATA( DLGK_GRID_UP,    'k' ),
	DLG_KEYS_DATA( DLGK_GRID_UP,    KEY_UP ),
	DLG_KEYS_DATA( DLGK_PAGE_FIRST, 'g' ),
	DLG_KEYS_DATA( DLGK_PAGE_FIRST, KEY_HOME ),
	DLG_KEYS_DATA( DLGK_PAGE_LAST,  'G' ),
	DLG_KEYS_DATA( DLGK_PAGE_LAST,  KEY_END ),
	DLG_KEYS_DATA( DLGK_PAGE_LAST,  KEY_LL ),
	DLG_KEYS_DATA( DLGK_PAGE_NEXT,  ' ' ),
	DLG_KEYS_DATA( DLGK_PAGE_NEXT,  KEY_NPAGE ),
	DLG_KEYS_DATA( DLGK_PAGE_PREV,  'B' ),
	DLG_KEYS_DATA( DLGK_PAGE_PREV,  'b' ),
	DLG_KEYS_DATA( DLGK_PAGE_PREV,  KEY_PPAGE ),
	DLG_KEYS_DATA( DLGK_BEGIN,	'0' ),
	DLG_KEYS_DATA( DLGK_BEGIN,	KEY_BEG ),
	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
	END_KEYS_BINDING
    };
    /* *INDENT-ON* */

#ifdef KEY_RESIZE
    int old_height = height;
    int old_width = width;
#endif
    long fpos;
    int x, y, cur_x, cur_y;
    int key = 0, fkey;
    int next = 0;
    int i, code, passed_end;
    char search_term[MAX_LEN + 1];
    MY_OBJ obj;
    WINDOW *dialog;
    bool moved;
    int result = DLG_EXIT_UNKNOWN;
    int button = dialog_vars.extra_button ? dlg_defaultno_button() : 0;
    int min_width = 12;

    search_term[0] = '\0';	/* no search term entered yet */

    memset(&obj, 0, sizeof(obj));

    obj.begin_reached = TRUE;
    obj.buffer_first = TRUE;
    obj.end_reached = FALSE;
    obj.buttons = dlg_exit_label();

    /* Open input file for reading */
    if ((obj.fd = open(file, O_RDONLY)) == -1)
        dlg_exiterr("Can't open input file %s", file);

    /* Get file size. Actually, 'file_size' is the real file size - 1,
       since it's only the last byte offset from the beginning */
    obj.file_size = lseek_obj(&obj, 0L, SEEK_END);

    /* Restore file pointer to beginning of file after getting file size */
    lseek_obj(&obj, 0L, SEEK_SET);

    read_high(&obj, BUF_SIZE);

    dlg_button_layout(obj.buttons, &min_width);

#ifdef KEY_RESIZE
retry:
#endif
    moved = TRUE;

    dlg_auto_sizefile(title, file, &height, &width, 2, min_width);
    dlg_print_size(height, width);
    dlg_ctl_size(height, width);

    x = dlg_box_x_ordinate(width);
    y = dlg_box_y_ordinate(height);

    dialog = dlg_new_window(height, width, y, x);
    dlg_register_window(dialog, "textbox", binding);
    dlg_register_buttons(dialog, "textbox", obj.buttons);

    dlg_mouse_setbase(x, y);

    /* Create window for text region, used for scrolling text */
    obj.text = dlg_sub_window(dialog, PAGE_LENGTH, PAGE_WIDTH, y + 1, x + 1);

    /* register the new window, along with its borders */
    dlg_mouse_mkbigregion(0, 0, PAGE_LENGTH + 2, width, KEY_MAX, 1, 1, 1 /* lines */ );
    dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
    dlg_draw_bottom_box(dialog);
    dlg_draw_title(dialog, title);

    dlg_draw_buttons(dialog, PAGE_LENGTH + 2, 0, obj.buttons, button, FALSE, width);
    (void) wnoutrefresh(dialog);
    getyx(dialog, cur_y, cur_x);	/* Save cursor position */

    dlg_attr_clear(obj.text, PAGE_LENGTH, PAGE_WIDTH, dialog_attr);

    while (result == DLG_EXIT_UNKNOWN) {

        /*
         * Update the screen according to whether we shifted up/down by a line
         * or not.
         */
        if (moved) {
            if (next < 0) {
                (void) scrollok(obj.text, TRUE);
                (void) scroll(obj.text);	/* Scroll text region up one line */
                (void) scrollok(obj.text, FALSE);
                print_line(&obj, PAGE_LENGTH - 1, PAGE_WIDTH);
                (void) wnoutrefresh(obj.text);
            } else if (next > 0) {
                /*
                 * We don't call print_page() here but use scrolling to ensure
                 * faster screen update.  However, 'end_reached' and
                 * 'page_length' should still be updated, and 'in_buf' should
                 * point to start of next page.  This is done by calling
                 * get_line() in the following 'for' loop.
                 */
                (void) scrollok(obj.text, TRUE);
                (void) wscrl(obj.text, -1);	/* Scroll text region down one line */
                (void) scrollok(obj.text, FALSE);
                obj.page_length = 0;
                passed_end = 0;
                for (i = 0; i < PAGE_LENGTH; i++) {
                    if (!i) {
                        print_line(&obj, 0, PAGE_WIDTH);	/* print first line of page */
                        (void) wnoutrefresh(obj.text);
                    } else
                        (void) get_line(&obj);	/* Called to update 'end_reached' and 'in_buf' */
                    if (!passed_end)
                        obj.page_length++;
                    if (obj.end_reached && !passed_end)
                        passed_end = 1;
                }
            } else {
                print_page(&obj, PAGE_LENGTH, PAGE_WIDTH);
            }
            print_position(&obj, dialog, height, width);
            (void) wmove(dialog, cur_y, cur_x);		/* Restore cursor position */
            wrefresh(dialog);
        }
        moved = FALSE;		/* assume we'll not move */
        next = 0;		/* ...but not scroll by a line */

        key = dlg_mouse_wgetch(dialog, &fkey);
        if (dlg_result_key(key, fkey, &result))
            break;

        if (!fkey && (code = dlg_char_to_button(key, obj.buttons)) >= 0) {
            result = dlg_ok_buttoncode(code);
            break;
        }

        if (fkey) {
            switch (key) {
            default:
                if (is_DLGK_MOUSE(key)) {
                    result = dlg_exit_buttoncode(key - M_EVENT);
                    if (result < 0)
                        result = DLG_EXIT_OK;
                } else {
                    beep();
                }
                break;
            case DLGK_FIELD_NEXT:
                button = dlg_next_button(obj.buttons, button);
                if (button < 0)
                    button = 0;
                dlg_draw_buttons(dialog,
                                 height - 2, 0,
                                 obj.buttons, button,
                                 FALSE, width);
                break;
            case DLGK_FIELD_PREV:
                button = dlg_prev_button(obj.buttons, button);
                if (button < 0)
                    button = 0;
                dlg_draw_buttons(dialog,
                                 height - 2, 0,
                                 obj.buttons, button,
                                 FALSE, width);
                break;
            case DLGK_ENTER:
                if (dialog_vars.nook)
                    result = DLG_EXIT_OK;
                else
                    result = dlg_exit_buttoncode(button);
                break;
            case DLGK_PAGE_FIRST:
                if (!obj.begin_reached) {
                    obj.begin_reached = 1;
                    /* First page not in buffer? */
                    fpos = ftell_obj(&obj);

                    if (fpos > obj.fd_bytes_read) {
                        /* Yes, we have to read it in */
                        lseek_obj(&obj, 0L, SEEK_SET);

                        read_high(&obj, BUF_SIZE);
                    }
                    obj.in_buf = 0;
                    moved = TRUE;
                }
                break;
            case DLGK_PAGE_LAST:
                obj.end_reached = TRUE;
                /* Last page not in buffer? */
                fpos = ftell_obj(&obj);

                if (fpos < obj.file_size) {
                    /* Yes, we have to read it in */
                    lseek_obj(&obj, -BUF_SIZE, SEEK_END);

                    read_high(&obj, BUF_SIZE);
                }
                obj.in_buf = obj.bytes_read;
                back_lines(&obj, (long) PAGE_LENGTH);
                moved = TRUE;
                break;
            case DLGK_GRID_UP:	/* Previous line */
                if (!obj.begin_reached) {
                    back_lines(&obj, obj.page_length + 1);
                    next = 1;
                    moved = TRUE;
                }
                break;
            case DLGK_PAGE_PREV:	/* Previous page */
            case DLGK_MOUSE(KEY_PPAGE):
                if (!obj.begin_reached) {
                    back_lines(&obj, obj.page_length + PAGE_LENGTH);
                    moved = TRUE;
                }
                break;
            case DLGK_GRID_DOWN:	/* Next line */
                if (!obj.end_reached) {
                    obj.begin_reached = 0;
                    next = -1;
                    moved = TRUE;
                }
                break;
            case DLGK_PAGE_NEXT:	/* Next page */
            case DLGK_MOUSE(KEY_NPAGE):
                if (!obj.end_reached) {
                    obj.begin_reached = 0;
                    moved = TRUE;
                }
                break;
            case DLGK_BEGIN:	/* Beginning of line */
                if (obj.hscroll > 0) {
                    obj.hscroll = 0;
                    /* Reprint current page to scroll horizontally */
                    back_lines(&obj, obj.page_length);
                    moved = TRUE;
                }
                break;
            case DLGK_GRID_LEFT:	/* Scroll left */
                if (obj.hscroll > 0) {
                    obj.hscroll--;
                    /* Reprint current page to scroll horizontally */
                    back_lines(&obj, obj.page_length);
                    moved = TRUE;
                }
                break;
            case DLGK_GRID_RIGHT:	/* Scroll right */
                if (obj.hscroll < MAX_LEN) {
                    obj.hscroll++;
                    /* Reprint current page to scroll horizontally */
                    back_lines(&obj, obj.page_length);
                    moved = TRUE;
                }
                break;
#ifdef KEY_RESIZE
            case KEY_RESIZE:
                /* reset data */
                height = old_height;
                width = old_width;
                back_lines(&obj, obj.page_length);
                moved = TRUE;
                /* repaint */
                dlg_clear();
                dlg_del_window(dialog);
                refresh();
                dlg_mouse_free_regions();
                goto retry;
#endif
            }
        } else {
            switch (key) {
            case '/':		/* Forward search */
            case 'n':		/* Repeat forward search */
            case '?':		/* Backward search */
            case 'N':		/* Repeat backward search */
                moved = perform_search(&obj, height, width, key, search_term);
                fkey = FALSE;
                break;
            default:
                beep();
                break;
            }
        }
    }

    dlg_del_window(dialog);
    free(obj.buf);
    (void) close(obj.fd);
    dlg_mouse_free_regions();
    return result;
}
Ejemplo n.º 6
0
/*
 * Display a dialog box with two buttons - Yes and No.
 */
int
dialog_yesno(const char *title, const char *cprompt, int height, int width)
{
    /* *INDENT-OFF* */
    static DLG_KEYS_BINDING binding[] = {
	HELPKEY_BINDINGS,
	ENTERKEY_BINDINGS,
	DLG_KEYS_DATA( DLGK_ENTER,	' ' ),
	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	KEY_DOWN ),
	DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
	DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_UP ),
	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
	SCROLLKEY_BINDINGS,
	END_KEYS_BINDING
    };
    /* *INDENT-ON* */

    int x, y;
    int key = 0, fkey;
    int code;
    int button = dlg_defaultno_button();
    WINDOW *dialog = 0;
    int result = DLG_EXIT_UNKNOWN;
    char *prompt = dlg_strclone(cprompt);
    const char **buttons = dlg_yes_labels();
    int min_width = 25;
    bool show = TRUE;
    int page, last = 0, offset = 0;

#ifdef KEY_RESIZE
    int req_high = height;
    int req_wide = width;
  restart:
#endif

    dlg_tab_correct_str(prompt);
    dlg_button_layout(buttons, &min_width);
    dlg_auto_size(title, prompt, &height, &width, 2, min_width);
    dlg_print_size(height, width);
    dlg_ctl_size(height, width);

    x = dlg_box_x_ordinate(width);
    y = dlg_box_y_ordinate(height);

#ifdef KEY_RESIZE
    if (dialog != 0)
	dlg_move_window(dialog, height, width, y, x);
    else
#endif
    {
	dialog = dlg_new_window(height, width, y, x);
	dlg_register_window(dialog, "yesno", binding);
	dlg_register_buttons(dialog, "yesno", buttons);
    }

    dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
    dlg_draw_bottom_box(dialog);
    dlg_draw_title(dialog, title);
    dlg_draw_helpline(dialog, FALSE);

    wattrset(dialog, dialog_attr);

    page = height - (1 + 3 * MARGIN);
    dlg_draw_buttons(dialog,
		     height - 2 * MARGIN, 0,
		     buttons, button, FALSE, width);

    while (result == DLG_EXIT_UNKNOWN) {
	if (show) {
	    last = dlg_print_scrolled(dialog, prompt, offset,
				      page, width, TRUE);
	    show = FALSE;
	}
	key = dlg_mouse_wgetch(dialog, &fkey);
	if (dlg_result_key(key, fkey, &result))
	    break;
	if ((code = dlg_char_to_button(key, buttons)) >= 0) {
	    result = dlg_ok_buttoncode(code);
	    break;
	}
	/* handle function keys */
	if (fkey) {
	    switch (key) {
	    case DLGK_FIELD_NEXT:
		button = dlg_next_button(buttons, button);
		if (button < 0)
		    button = 0;
		dlg_draw_buttons(dialog,
				 height - 2, 0,
				 buttons, button,
				 FALSE, width);
		break;
	    case DLGK_FIELD_PREV:
		button = dlg_prev_button(buttons, button);
		if (button < 0)
		    button = 0;
		dlg_draw_buttons(dialog,
				 height - 2, 0,
				 buttons, button,
				 FALSE, width);
		break;
	    case DLGK_ENTER:
		result = dlg_yes_buttoncode(button);
		break;
#ifdef KEY_RESIZE
	    case KEY_RESIZE:
		dlg_clear();
		height = req_high;
		width = req_wide;
		goto restart;
#endif
	    default:
		if (is_DLGK_MOUSE(key)) {
		    result = dlg_yes_buttoncode(key - M_EVENT);
		    if (result < 0)
			result = DLG_EXIT_OK;
		} else if (dlg_check_scrolled(key, last, page,
					      &show, &offset) != 0) {
		    beep();
		}
		break;
	    }
	} else {
	    beep();
	}
    }

    dlg_del_window(dialog);
    dlg_mouse_free_regions();
    free(prompt);
    return result;
}