bfu.c
来自「this is the file used to browse web」· C语言 代码 · 共 1,481 行 · 第 1/4 页
C
1,481 行
checkboxes_width(term, dlg->dlg->udata, &max, max_text_width); checkboxes_width(term, dlg->dlg->udata, &min, min_text_width); max_buttons_width(term, dlg->items + dlg->n - 2, 2, &max); min_buttons_width(term, dlg->items + dlg->n - 2, 2, &min); w = term->x * 9 / 10 - 2 * DIALOG_LB; if (w > max) w = max; if (w < min) w = min; if (w > term->x - 2 * DIALOG_LB) w = term->x - 2 * DIALOG_LB; if (w < 5) w = 5; rw = 0; dlg_format_checkboxes(NULL, term, dlg->items, dlg->n - 2, 0, &y, w, &rw, dlg->dlg->udata); y++; dlg_format_buttons(NULL, term, dlg->items + dlg->n - 2, 2, 0, &y, w, &rw, AL_CENTER); w = rw; dlg->xw = rw + 2 * DIALOG_LB; dlg->yw = y + 2 * DIALOG_TB; center_dlg(dlg); draw_dlg(dlg); y = dlg->y + DIALOG_TB + 1; dlg_format_checkboxes(term, term, dlg->items, dlg->n - 2, dlg->x + DIALOG_LB, &y, w, NULL, dlg->dlg->udata); y++; dlg_format_buttons(term, term, dlg->items + dlg->n - 2, 2, dlg->x + DIALOG_LB, &y, w, &rw, AL_CENTER);}void group_fn(struct dialog_data *dlg){ struct terminal *term = dlg->win->term; int max = 0, min = 0; int w, rw; int y = 0; max_group_width(term, dlg->dlg->udata, dlg->items, dlg->n - 2, &max); min_group_width(term, dlg->dlg->udata, dlg->items, dlg->n - 2, &min); max_buttons_width(term, dlg->items + dlg->n - 2, 2, &max); min_buttons_width(term, dlg->items + dlg->n - 2, 2, &min); w = term->x * 9 / 10 - 2 * DIALOG_LB; if (w > max) w = max; if (w < min) w = min; if (w > term->x - 2 * DIALOG_LB) w = term->x - 2 * DIALOG_LB; if (w < 1) w = 1; rw = 0; dlg_format_group(NULL, term, dlg->dlg->udata, dlg->items, dlg->n - 2, 0, &y, w, &rw); y++; dlg_format_buttons(NULL, term, dlg->items + dlg->n - 2, 2, 0, &y, w, &rw, AL_CENTER); w = rw; dlg->xw = rw + 2 * DIALOG_LB; dlg->yw = y + 2 * DIALOG_TB; center_dlg(dlg); draw_dlg(dlg); y = dlg->y + DIALOG_TB + 1; dlg_format_group(term, term, dlg->dlg->udata, dlg->items, dlg->n - 2, dlg->x + DIALOG_LB, &y, w, NULL); y++; dlg_format_buttons(term, term, dlg->items + dlg->n - 2, 2, dlg->x + DIALOG_LB, &y, w, &rw, AL_CENTER);}void msg_box_fn(struct dialog_data *dlg){ struct terminal *term = dlg->win->term; int max = 0, min = 0; int w, rw; int y = 0; unsigned char **ptr; unsigned char *text = init_str(); int textl = 0; for (ptr = dlg->dlg->udata; *ptr; ptr++) add_to_str(&text, &textl, _(*ptr, term)); max_text_width(term, text, &max); min_text_width(term, text, &min); max_buttons_width(term, dlg->items, dlg->n, &max); min_buttons_width(term, dlg->items, dlg->n, &min); w = term->x * 9 / 10 - 2 * DIALOG_LB; if (w > max) w = max; if (w < min) w = min; if (w > term->x - 2 * DIALOG_LB) w = term->x - 2 * DIALOG_LB; if (w < 1) w = 1; rw = 0; dlg_format_text(NULL, term, text, 0, &y, w, &rw, COLOR_DIALOG_TEXT, dlg->dlg->align); y++; dlg_format_buttons(NULL, term, dlg->items, dlg->n, 0, &y, w, &rw, AL_CENTER); w = rw; dlg->xw = rw + 2 * DIALOG_LB; dlg->yw = y + 2 * DIALOG_TB; center_dlg(dlg); draw_dlg(dlg); y = dlg->y + DIALOG_TB + 1; dlg_format_text(term, term, text, dlg->x + DIALOG_LB, &y, w, NULL, COLOR_DIALOG_TEXT, dlg->dlg->align); y++; dlg_format_buttons(term, term, dlg->items, dlg->n, dlg->x + DIALOG_LB, &y, w, NULL, AL_CENTER); mem_free(text);}int msg_box_button(struct dialog_data *dlg, struct dialog_item_data *di){ void (*fn)(void *) = (void (*)(void *))di->item->udata; void *data = dlg->dlg->udata2; /*struct dialog *dl = dlg->dlg;*/ if (fn) fn(data); cancel_dialog(dlg, di); return 0;}void msg_box(struct terminal *term, struct memory_list *ml, unsigned char *title, int align, /*unsigned char *text, void *data, int n,*/ ...){ struct dialog *dlg; int i; int n; unsigned char *text; unsigned char **udata; void *udata2; int udatan; va_list ap; va_start(ap, align); udata = DUMMY; udatan = 0; do { text = va_arg(ap, unsigned char *); na_kovarne__to_je_narez: udatan++; if ((unsigned)udatan > MAXINT / sizeof(unsigned char *)) overalloc(); udata = mem_realloc(udata, udatan * sizeof(unsigned char *)); udata[udatan - 1] = text; if (text && !(align & AL_EXTD_TEXT)) { text = NULL; goto na_kovarne__to_je_narez; } } while (text); udata2 = va_arg(ap, void *); n = va_arg(ap, int); if ((unsigned)n > (MAXINT - sizeof(struct dialog)) / sizeof(struct dialog_item) - 1) overalloc(); dlg = mem_alloc(sizeof(struct dialog) + (n + 1) * sizeof(struct dialog_item)); memset(dlg, 0, sizeof(struct dialog) + (n + 1) * sizeof(struct dialog_item)); dlg->title = title; dlg->fn = msg_box_fn; dlg->udata = udata; dlg->udata2 = udata2; dlg->align = align; for (i = 0; i < n; i++) { unsigned char *m; void (*fn)(void *); int flags; m = va_arg(ap, unsigned char *); fn = va_arg(ap, void *); flags = va_arg(ap, int); if (!m) { i--, n--; continue; } dlg->items[i].type = D_BUTTON; dlg->items[i].gid = flags; dlg->items[i].fn = msg_box_button; dlg->items[i].dlen = 0; dlg->items[i].text = m; dlg->items[i].udata = fn; } va_end(ap); dlg->items[i].type = D_END; add_to_ml(&ml, dlg, udata, NULL); do_dialog(term, dlg, ml);}void add_to_history(struct history *h, unsigned char *t){ struct history_item *hi, *hs; size_t l; if (!h || !t || !*t) return; l = strlen(t) + 1; if (l > MAXINT - sizeof(struct history_item)) overalloc(); hi = mem_alloc(sizeof(struct history_item) + l); memcpy(hi->d, t, l); foreach(hs, h->items) if (!strcmp(hs->d, t)) { struct history_item *hd = hs; hs = hs->prev; del_from_list(hd); mem_free(hd); h->n--; } add_to_list(h->items, hi); h->n++; while (h->n > MAX_HISTORY_ITEMS) { struct history_item *hd = h->items.prev; if ((void *)hd == &h->items) { internal("history is empty"); h->n = 0; return; } del_from_list(hd); mem_free(hd); h->n--; }}int input_field_cancel(struct dialog_data *dlg, struct dialog_item_data *di){ void (*fn)(void *) = di->item->udata; void *data = dlg->dlg->udata2; if (fn) fn(data); cancel_dialog(dlg, di); return 0;}int input_field_ok(struct dialog_data *dlg, struct dialog_item_data *di){ void (*fn)(void *, unsigned char *) = di->item->udata; void *data = dlg->dlg->udata2; unsigned char *text = dlg->items->cdata; if (check_dialog(dlg)) return 1; add_to_history(dlg->dlg->items->history, text); if (fn) fn(data, text); ok_dialog(dlg, di); return 0;}void input_field_fn(struct dialog_data *dlg){ struct terminal *term = dlg->win->term; int max = 0, min = 0; int w, rw; int y = -1; max_text_width(term, dlg->dlg->udata, &max); min_text_width(term, dlg->dlg->udata, &min); max_buttons_width(term, dlg->items + 1, 2, &max); min_buttons_width(term, dlg->items + 1, 2, &min); if (max < dlg->dlg->items->dlen) max = dlg->dlg->items->dlen; w = term->x * 9 / 10 - 2 * DIALOG_LB; if (w > max) w = max; if (w < min) w = min; rw = 0; /* !!! FIXME: input field */ dlg_format_text(NULL, term, dlg->dlg->udata, 0, &y, w, &rw, COLOR_DIALOG_TEXT, AL_LEFT); dlg_format_field(NULL, term, dlg->items, 0, &y, w, &rw, AL_LEFT); y++; dlg_format_buttons(NULL, term, dlg->items + 1, 2, 0, &y, w, &rw, AL_CENTER); w = rw; dlg->xw = rw + 2 * DIALOG_LB; dlg->yw = y + 2 * DIALOG_TB; center_dlg(dlg); draw_dlg(dlg); y = dlg->y + DIALOG_TB; dlg_format_text(term, term, dlg->dlg->udata, dlg->x + DIALOG_LB, &y, w, NULL, COLOR_DIALOG_TEXT, AL_LEFT); dlg_format_field(term, term, dlg->items, dlg->x + DIALOG_LB, &y, w, NULL, AL_LEFT); y++; dlg_format_buttons(term, term, dlg->items + 1, 2, dlg->x + DIALOG_LB, &y, w, NULL, AL_CENTER);}void input_field(struct terminal *term, struct memory_list *ml, unsigned char *title, unsigned char *text, unsigned char *okbutton, unsigned char *cancelbutton, void *data, struct history *history, int l, unsigned char *def, int min, int max, int (*check)(struct dialog_data *, struct dialog_item_data *), void (*fn)(void *, unsigned char *), void (*cancelfn)(void *)){ struct dialog *dlg; unsigned char *field; if ((unsigned)l > MAXINT - sizeof(struct dialog) + 4 * sizeof(struct dialog_item)) overalloc(); dlg = mem_alloc(sizeof(struct dialog) + 4 * sizeof(struct dialog_item) + l); memset(dlg, 0, sizeof(struct dialog) + 4 * sizeof(struct dialog_item) + l); *(field = (unsigned char *)dlg + sizeof(struct dialog) + 4 * sizeof(struct dialog_item)) = 0; if (def) { if (strlen(def) + 1 > (size_t)l) memcpy(field, def, l - 1); else strcpy(field, def); } dlg->title = title; dlg->fn = input_field_fn; dlg->udata = text; dlg->udata2 = data; dlg->items[0].type = D_FIELD; dlg->items[0].gid = min; dlg->items[0].gnum = max; dlg->items[0].fn = check; dlg->items[0].history = history; dlg->items[0].dlen = l; dlg->items[0].data = field; dlg->items[1].type = D_BUTTON; dlg->items[1].gid = B_ENTER; dlg->items[1].fn = input_field_ok; dlg->items[1].dlen = 0; dlg->items[1].text = okbutton; dlg->items[1].udata = fn; dlg->items[2].type = D_BUTTON; dlg->items[2].gid = B_ESC; dlg->items[2].fn = input_field_cancel; dlg->items[2].dlen = 0; dlg->items[2].text = cancelbutton; dlg->items[2].udata = cancelfn; dlg->items[3].type = D_END; add_to_ml(&ml, dlg, NULL); do_dialog(term, dlg, ml);}/* Sets the selected item to one that is visible.*/void box_sel_set_visible(struct dialog_item_data *box_item_data, int offset) { struct dlg_data_item_data_box *box; int sel; box = (struct dlg_data_item_data_box *)(box_item_data->item->data); if (offset > box_item_data->item->gid || offset < 0) { return; } /*debug("offset: %d", offset);*/ sel = box->box_top + offset; if (sel > box->list_len) { box->sel = box->list_len - 1; } else { box->sel = sel; }}/* Moves the selected item [dist] thingies. If [dist] is out of the current range, the selected item is moved to the extreme (ie, the top or bottom) */void box_sel_move(struct dialog_item_data *box_item_data, int dist) { struct dlg_data_item_data_box *box; int new_sel; int new_top; box = (struct dlg_data_item_data_box *)(box_item_data->item->data); new_sel = box->sel + dist; new_top = box->box_top; /* Ensure that the selection is in range */ if (new_sel < 0) new_sel = 0; else if (new_sel >= box->list_len) new_sel = box->list_len - 1; /* Ensure that the display box is over the item */ if ( new_sel >= (new_top + box_item_data->item->gid) ) { /* Move it down */ new_top = new_sel - box_item_data->item->gid + 1;#ifdef DEBUG if (new_top < 0) debug("Newly calculated box_top is an extremely wrong value (%d). It should not be below zero.", new_top);#endif } else if ( new_sel < new_top ) { /* Move the display up (if necessary) */ new_top = new_sel; } box->sel = new_sel; box->box_top = new_top;}/* Displays a dialog box */void show_dlg_item_box(struct dialog_data *dlg, struct dialog_item_data *box_item_data) { struct terminal *term = dlg->win->term; struct dlg_data_item_data_box *box; struct box_item *citem; /* Item currently being shown */ int n; /* Index of item currently being displayed */ box = (struct dlg_data_item_data_box *)(box_item_data->item->data); /* FIXME: Counting here SHOULD be unnecessary */ n = 0; fill_area(term, box_item_data->x, box_item_data->y, box_item_data->l, box_item_data->item->gid, COLOR_DIALOG_FIELD); foreach (citem, box->items) { int len; /* Length of the current text field. */ len = strlen(citem->text); if (len > box_item_data->l) { len = box_item_data->l; } /* Is the current item in the region to be displayed? */ if ( (n >= box->box_top) && (n < (box->box_top + box_item_data->item->gid)) ) { print_text(term, box_item_data->x, box_item_data->y + n - box->box_top, len, citem->text, n == box->sel ? COLOR_DIALOG_BUTTON_SELECTED : COLOR_DIALOG_FIELD_TEXT); } n++; } box->list_len = n;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?