示例#1
0
/*
 * Call this function after dlg_register_window(), for the list of button
 * labels associated with the widget.
 *
 * Ensure that dlg_lookup_key() will not accidentally translate a key that
 * we would like to use for a button abbreviation to some other key, e.g.,
 * h/j/k/l for navigation into a cursor key.  Do this by binding the key
 * to itself.
 *
 * See dlg_char_to_button().
 */
void
dlg_register_buttons(WINDOW *win, const char *name, const char **buttons)
{
    int n;
    LIST_BINDINGS *p;
    DLG_KEYS_BINDING *q;

    if (buttons == 0)
	return;

    for (n = 0; buttons[n] != 0; ++n) {
	int curses_key = dlg_button_to_char(buttons[n]);

	/* ignore multibyte characters */
	if (curses_key >= KEY_MIN)
	    continue;

	/* if it is not bound in the widget, skip it (no conflicts) */
	if (!key_is_bound(win, name, curses_key, FALSE))
	    continue;

#ifdef HAVE_RC_FILE
	/* if it is bound in the rc-file, skip it */
	if (key_is_bound(0, name, curses_key, FALSE))
	    continue;
#endif

	if ((p = dlg_calloc(LIST_BINDINGS, 1)) != 0) {
	    if ((q = dlg_calloc(DLG_KEYS_BINDING, 2)) != 0) {
		q[0].is_function_key = 0;
		q[0].curses_key = curses_key;
		q[0].dialog_key = curses_key;
		q[1] = end_keys_binding;

		p->win = win;
		p->name = name;
		p->buttons = TRUE;
		p->binding = q;

		/* put these at the beginning, to override the widget's table */
		p->link = all_bindings;
		all_bindings = p;
	    } else {
		free(p);
	    }
	}
    }
}
示例#2
0
/*
 * For a given named widget's window, associate a binding table.
 */
void
dlg_register_window(WINDOW *win, const char *name, DLG_KEYS_BINDING * binding)
{
    LIST_BINDINGS *p, *q;

    for (p = all_bindings, q = 0; p != 0; q = p, p = p->link) {
	if (p->win == win && !strcmp(p->name, name)) {
	    p->binding = binding;
	    return;
	}
    }
    /* add built-in bindings at the end of the list (see compare_bindings). */
    if ((p = dlg_calloc(LIST_BINDINGS, 1)) != 0) {
	p->win = win;
	p->name = name;
	p->binding = binding;
	if (q != 0)
	    q->link = p;
	else
	    all_bindings = p;
    }
#if defined(HAVE_DLG_TRACE) && defined(HAVE_RC_FILE)
    /*
     * Trace the binding information assigned to this window.  For most widgets
     * there is only one binding table.  forms have two, so the trace will be
     * longer.  Since compiled-in bindings are only visible when the widget is
     * registered, there is no other way to see what bindings are available,
     * than by running dialog and tracing it.
     */
    dlg_trace_msg("# dlg_register_window %s\n", name);
    dlg_dump_window_keys(dialog_state.trace_output, win);
#endif
}
示例#3
0
/*
 * Find a user-defined binding, given the curses key code.  If it does not
 * exist, create a new one, inserting it into the linked list, keeping it
 * sorted to simplify lookups for user-defined bindings that can override
 * the built-in bindings.
 */
static DLG_KEYS_BINDING *
make_binding(char *widget, int curses_key, int is_function, int dialog_key)
{
    LIST_BINDINGS *entry = 0;
    DLG_KEYS_BINDING *data = 0;
    char *name;
    LIST_BINDINGS *p, *q;
    DLG_KEYS_BINDING *result = find_binding(widget, curses_key);

    if (result == 0
	&& (entry = dlg_calloc(LIST_BINDINGS, 1)) != 0
	&& (data = dlg_calloc(DLG_KEYS_BINDING, 2)) != 0
	&& (name = dlg_strclone(widget)) != 0) {

	entry->name = name;
	entry->binding = data;

	data[0].is_function_key = is_function;
	data[0].curses_key = curses_key;
	data[0].dialog_key = dialog_key;

	data[1] = end_keys_binding;

	for (p = all_bindings, q = 0; p != 0; q = p, p = p->link) {
	    if (compare_bindings(entry, p) < 0) {
		break;
	    }
	}
	if (q != 0) {
	    q->link = entry;
	} else {
	    all_bindings = entry;
	}
	if (p != 0) {
	    entry->link = p;
	}
	result = data;
    } else if (entry != 0) {
	free(entry);
	if (data)
	    free(data);
    }

    return result;
}
示例#4
0
文件: guage.c 项目: Distrotech/dialog
/*
 * Allocates a new object and fills it as per the arguments
 */
void *
dlg_allocate_gauge(const char *title,
		   const char *cprompt,
		   int height,
		   int width,
		   int percent)
{
    int x, y;
    char *prompt = dlg_strclone(cprompt);
    WINDOW *dialog;
    MY_OBJ *obj = 0;

    dlg_tab_correct_str(prompt);

    dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE);
    dlg_print_size(height, width);
    dlg_ctl_size(height, width);

    /* center dialog box on screen */
    x = dlg_box_x_ordinate(width);
    y = dlg_box_y_ordinate(height);

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

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

    obj->obj.input = dialog_state.pipe_input;
    obj->obj.win = dialog;
    obj->obj.keep_win = TRUE;
    obj->obj.bg_task = TRUE;
    obj->obj.handle_getc = handle_my_getc;
    obj->obj.handle_input = handle_input;

    obj->title = title;
    obj->prompt = prompt;
    obj->percent = percent;
    obj->height = height;
    obj->width = width;

    obj->next = all_objects;
    all_objects = obj;

    return (void *) obj;
}
示例#5
0
static void
make_cache(CACHE * cache, const char *string)
{
    CACHE *p;

    p = dlg_calloc(CACHE, 1);
    assert_ptr(p, "load_cache");
    p->next = cache_list;
    cache_list = p;

    p->cache_at = cache;
    p->string_at = string;

    *cache = *p;
#ifdef HAVE_TSEARCH
    (void) tsearch(p, &sorted_cache, compare_cache);
#endif
}
示例#6
0
static CACHE *
make_cache(int cache_num, const char *string)
{
    CACHE *p;

    p = dlg_calloc(CACHE, 1);
    assert_ptr(p, "load_cache");
    p->next = cache_list;
    cache_list = p;

    p->cache_num = cache_num;
    p->string_at = string;

#ifdef HAVE_TSEARCH
    (void) tsearch(p, &sorted_cache, compare_cache);
#endif
    return p;
}
示例#7
0
/*
 * For a given named widget's window, associate a binding table.
 */
void
dlg_register_window(WINDOW *win, const char *name, DLG_KEYS_BINDING * binding)
{
    LIST_BINDINGS *p, *q;

    for (p = all_bindings, q = 0; p != 0; q = p, p = p->link) {
	if (p->win == win && !strcmp(p->name, name)) {
	    p->binding = binding;
	    return;
	}
    }
    /* add built-in bindings at the end of the list (see compare_bindings). */
    if ((p = dlg_calloc(LIST_BINDINGS, 1)) != 0) {
	p->win = win;
	p->name = name;
	p->binding = binding;
	if (q != 0)
	    q->link = p;
	else
	    all_bindings = p;
    }
}
示例#8
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;
}
示例#9
0
文件: checklist.c 项目: 0mp/freebsd
/*
 * Display a dialog box with a list of options that can be turned on or off
 * The `flag' parameter is used to select between radiolist and checklist.
 */
int
dialog_checklist(const char *title,
		 const char *cprompt,
		 int height,
		 int width,
		 int list_height,
		 int item_no,
		 char **items,
		 int flag)
{
    int result;
    int i, j;
    DIALOG_LISTITEM *listitems;
    bool separate_output = ((flag == FLAG_CHECK)
			    && (dialog_vars.separate_output));
    bool show_status = FALSE;
    int current = 0;
    char *help_result;

    listitems = dlg_calloc(DIALOG_LISTITEM, (size_t) item_no + 1);
    assert_ptr(listitems, "dialog_checklist");

    for (i = j = 0; i < item_no; ++i) {
	listitems[i].name = items[j++];
	listitems[i].text = (dialog_vars.no_items
			     ? dlg_strempty()
			     : items[j++]);
	listitems[i].state = !dlg_strcmp(items[j++], "on");
	listitems[i].help = ((dialog_vars.item_help)
			     ? items[j++]
			     : dlg_strempty());
    }
    dlg_align_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no);

    result = dlg_checklist(title,
			   cprompt,
			   height,
			   width,
			   list_height,
			   item_no,
			   listitems,
			   NULL,
			   flag,
			   &current);

    switch (result) {
    case DLG_EXIT_OK:		/* FALLTHRU */
    case DLG_EXIT_EXTRA:
	show_status = TRUE;
	break;
    case DLG_EXIT_HELP:
	dlg_add_help_listitem(&result, &help_result, &listitems[current]);
	if ((show_status = dialog_vars.help_status)) {
	    if (separate_output) {
		dlg_add_string(help_result);
		dlg_add_separator();
	    } else {
		dlg_add_quoted(help_result);
	    }
	} else {
	    dlg_add_string(help_result);
	}
	break;
    }

    if (show_status) {
	for (i = 0; i < item_no; i++) {
	    if (listitems[i].state) {
		if (separate_output) {
		    dlg_add_string(listitems[i].name);
		    dlg_add_separator();
		} else {
		    if (dlg_need_separator())
			dlg_add_separator();
		    if (flag == FLAG_CHECK)
			dlg_add_quoted(listitems[i].name);
		    else
			dlg_add_string(listitems[i].name);
		}
	    }
	}
	dlg_add_last_key(separate_output);
    }

    dlg_free_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no);
    free(listitems);
    return result;
}
示例#10
0
/*
 * Display a dialog box with a list of options that can be turned on or off
 * The `flag' parameter is used to select between radiolist and checklist.
 */
int
dialog_checklist(const char *title,
		 const char *cprompt,
		 int height,
		 int width,
		 int list_height,
		 int item_no,
		 char **items,
		 int flag)
{
    int result;
    int i;
    DIALOG_LISTITEM *listitems;
    bool separate_output = ((flag == FLAG_CHECK)
			    && (dialog_vars.separate_output));
    bool show_status = FALSE;
    int current = 0;

    listitems = dlg_calloc(DIALOG_LISTITEM, (size_t) item_no + 1);
    assert_ptr(listitems, "dialog_checklist");

    for (i = 0; i < item_no; ++i) {
	listitems[i].name = ItemName(i);
	listitems[i].text = ItemText(i);
	listitems[i].help = ((dialog_vars.item_help)
			     ? ItemHelp(i)
			     : dlg_strempty());
	listitems[i].state = !dlg_strcmp(ItemStatus(i), "on");
    }
    dlg_align_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no);

    result = dlg_checklist(title,
			   cprompt,
			   height,
			   width,
			   list_height,
			   item_no,
			   listitems,
			   NULL,
			   flag,
			   &current);

    switch (result) {
    case DLG_EXIT_OK:		/* FALLTHRU */
    case DLG_EXIT_EXTRA:
	show_status = TRUE;
	break;
    case DLG_EXIT_HELP:
	dlg_add_result("HELP ");
	show_status = dialog_vars.help_status;
	if (USE_ITEM_HELP(listitems[current].help)) {
	    if (show_status) {
		if (separate_output) {
		    dlg_add_string(listitems[current].help);
		    dlg_add_separator();
		} else {
		    dlg_add_quoted(listitems[current].help);
		}
	    } else {
		dlg_add_string(listitems[current].help);
	    }
	    result = DLG_EXIT_ITEM_HELP;
	} else {
	    if (show_status) {
		if (separate_output) {
		    dlg_add_string(listitems[current].name);
		    dlg_add_separator();
		} else {
		    dlg_add_quoted(listitems[current].name);
		}
	    } else {
		dlg_add_string(listitems[current].name);
	    }
	}
	break;
    }

    if (show_status) {
	for (i = 0; i < item_no; i++) {
	    if (listitems[i].state) {
		if (separate_output) {
		    dlg_add_string(listitems[i].name);
		    dlg_add_separator();
		} else {
		    if (dlg_need_separator())
			dlg_add_separator();
		    dlg_add_string(listitems[i].name);
		}
	    }
	}
    }

    dlg_free_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no);
    free(listitems);
    return result;
}
示例#11
0
int
dialog_mixedform(const char *title,
                 const char *cprompt,
                 int height,
                 int width,
                 int form_height,
                 int item_no,
                 char **items)
{
    int result;
    int choice;
    int i;
    DIALOG_FORMITEM *listitems;
    DIALOG_VARS save_vars;
    bool show_status = FALSE;

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

    listitems = dlg_calloc(DIALOG_FORMITEM, (size_t) item_no + 1);
    assert_ptr(listitems, "dialog_mixedform");

    for (i = 0; i < item_no; ++i) {
        listitems[i].type = dialog_vars.formitem_type;
        listitems[i].name = ItemName(i);
        listitems[i].name_len = (int) strlen(ItemName(i));
        listitems[i].name_y = dlg_ordinate(ItemNameY(i));
        listitems[i].name_x = dlg_ordinate(ItemNameX(i));
        listitems[i].text = ItemText(i);
        listitems[i].text_len = (int) strlen(ItemText(i));
        listitems[i].text_y = dlg_ordinate(ItemTextY(i));
        listitems[i].text_x = dlg_ordinate(ItemTextX(i));
        listitems[i].text_flen = atoi(ItemTextFLen(i));
        listitems[i].text_ilen = atoi(ItemTextILen(i));
        listitems[i].help = (dialog_vars.item_help ? ItemHelp(i) :
                             dlg_strempty());
        listitems[i].type = (unsigned) atoi(ItemTypep(i));
    }

    result = dlg_form(title,
                      cprompt,
                      height,
                      width,
                      form_height,
                      item_no,
                      listitems,
                      &choice);

    switch (result) {
    case DLG_EXIT_OK:		/* FALLTHRU */
    case DLG_EXIT_EXTRA:
        show_status = TRUE;
        break;
    case DLG_EXIT_HELP:
        dlg_add_result("HELP ");
        show_status = dialog_vars.help_status;
        if (USE_ITEM_HELP(listitems[choice].help)) {
            dlg_add_string(listitems[choice].help);
            result = DLG_EXIT_ITEM_HELP;
        } else {
            dlg_add_string(listitems[choice].name);
        }
        if (show_status)
            dlg_add_separator();
        break;
    }
    if (show_status) {
        for (i = 0; i < item_no; i++) {
            if (listitems[i].text_flen > 0) {
                dlg_add_string(listitems[i].text);
                dlg_add_separator();
            }
        }
        dlg_add_last_key(-1);
    }

    dlg_free_formitems(listitems);
    dlg_restore_vars(&save_vars);

    return result;
}
示例#12
0
文件: progressbox.c 项目: 0mp/freebsd
int
dlg_progressbox(const char *title,
		const char *cprompt,
		int height,
		int width,
		int pauseopt,
		FILE *fp)
{
    int i;
    int x, y, thigh;
    WINDOW *dialog, *text;
    MY_OBJ *obj;
    char *prompt = dlg_strclone(cprompt);
    int result;

    dlg_tab_correct_str(prompt);
    dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE);
    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);

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

    dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
    dlg_draw_title(dialog, title);
    dlg_draw_helpline(dialog, FALSE);

    if (*prompt != '\0') {
	int y2, x2;

	(void) wattrset(dialog, dialog_attr);
	dlg_print_autowrap(dialog, prompt, height, width);
	getyx(dialog, y2, x2);
	(void) x2;
	++y2;
	wmove(dialog, y2, MARGIN);
	for (i = 0; i < getmaxx(dialog) - 2 * MARGIN; i++)
	    (void) waddch(dialog, dlg_boxchar(ACS_HLINE));
	y += y2;
	thigh -= y2;
    }

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

    (void) wrefresh(dialog);

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

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

    obj->obj.input = fp;
    obj->obj.win = dialog;
    obj->text = text;

    dlg_attr_clear(text, thigh, getmaxx(text), dialog_attr);
    for (i = 0; get_line(obj); i++) {
	if (i < thigh) {
	    print_line(obj, text, i, width - (2 * MARGIN));
	} else {
	    scrollok(text, TRUE);
	    scroll(text);
	    scrollok(text, FALSE);
	    print_line(obj, text, thigh - 1, width - (2 * MARGIN));
	}
	(void) wrefresh(text);
	dlg_trace_win(dialog);
	if (obj->is_eof)
	    break;
    }

    if (pauseopt) {
	scrollok(text, TRUE);
	wscrl(text, 1 + MARGIN);
	(void) wrefresh(text);
	result = pause_for_ok(dialog, height, width);
    } else {
	wrefresh(dialog);
	result = DLG_EXIT_OK;
    }

    dlg_del_window(dialog);
    free(prompt);
    free(obj);

    return result;
}