📄 util.c
字号:
fprintf(dialog_state.output, "Size: %d, %d\n", height, width);}voiddlg_ctl_size(int height, int width){ if (dialog_vars.size_err) { if ((width > COLS) || (height > LINES)) { dlg_exiterr("Window too big. (height, width) = (%d, %d). Max allowed (%d, %d).", height, width, LINES, COLS); }#ifdef HAVE_COLOR else if ((dialog_state.use_shadow) && ((width > SCOLS || height > SLINES))) { dlg_exiterr("Window+Shadow too big. (height, width) = (%d, %d). Max allowed (%d, %d).", height, width, SLINES, SCOLS); }#endif }}voiddlg_tab_correct_str(char *prompt){ char *ptr; if (!dialog_vars.tab_correct) return; while ((ptr = strchr(prompt, TAB)) != NULL) { *ptr = ' '; prompt = ptr; }}voiddlg_calc_listh(int *height, int *list_height, int item_no){ /* calculate new height and list_height */ int rows = SLINES - (dialog_vars.begin_set ? dialog_vars.begin_y : 0); if (rows - (*height) > 0) { if (rows - (*height) > item_no) *list_height = item_no; else *list_height = rows - (*height); } (*height) += (*list_height);}intdlg_calc_listw(int item_no, char **items, int group){ int n, i, len1 = 0, len2 = 0; for (i = 0; i < (item_no * group); i += group) { if ((n = strlen(items[i])) > len1) len1 = n; if ((n = strlen(items[i + 1])) > len2) len2 = n; } return len1 + len2;}char *dlg_strclone(const char *cprompt){ char *prompt = (char *) malloc(strlen(cprompt) + 1); assert_ptr(prompt, "dlg_strclone"); strcpy(prompt, cprompt); return prompt;}intdlg_box_x_ordinate(int width){ int x; if (dialog_vars.begin_set == 1) { x = dialog_vars.begin_x; } else { /* center dialog box on screen unless --begin-set */ x = (SCOLS - width) / 2; } return x;}intdlg_box_y_ordinate(int height){ int y; if (dialog_vars.begin_set == 1) { y = dialog_vars.begin_y; } else { /* center dialog box on screen unless --begin-set */ y = (SLINES - height) / 2; } return y;}voiddlg_draw_title(WINDOW *win, const char *title){ if (title != NULL) { chtype attr = A_NORMAL; chtype save = getattrs(win); int x = centered(getmaxx(win), title); wattrset(win, title_attr); wmove(win, 0, x); dlg_print_text(win, title, getmaxx(win) - x, &attr); wattrset(win, save); }}voiddlg_draw_bottom_box(WINDOW *win){ int width = getmaxx(win); int height = getmaxy(win); int i; wattrset(win, border_attr); (void) wmove(win, height - 3, 0); (void) waddch(win, ACS_LTEE); for (i = 0; i < width - 2; i++) (void) waddch(win, ACS_HLINE); wattrset(win, dialog_attr); (void) waddch(win, ACS_RTEE); (void) wmove(win, height - 2, 1); for (i = 0; i < width - 2; i++) (void) waddch(win, ' ');}/* * Remove a window, repainting everything else. This would be simpler if we * used the panel library, but that is not _always_ available. */voiddlg_del_window(WINDOW *win){ DIALOG_WINDOWS *p, *q, *r; /* * If --keep-window was set, do not delete/repaint the windows. */ if (dialog_vars.keep_window) return; /* Leave the main window untouched if there are no background windows. * We do this so the current window will not be cleared on exit, allowing * things like the infobox demo to run without flicker. */ if (dialog_state.getc_callbacks != 0) { touchwin(stdscr); wnoutrefresh(stdscr); } for (p = dialog_state.all_windows, r = 0; p != 0; r = p, p = q) { q = p->next; if (p->normal == win) { if (p->shadow != 0) delwin(p->shadow); delwin(p->normal); if (r == 0) { dialog_state.all_windows = q; } else { r->next = q; } free(p); } else { if (p->shadow != 0) { touchwin(p->shadow); wnoutrefresh(p->shadow); } touchwin(p->normal); wnoutrefresh(p->normal); } } doupdate();}/* * Create a window, optionally with a shadow. */WINDOW *dlg_new_window(int height, int width, int y, int x){ WINDOW *win; DIALOG_WINDOWS *p = (DIALOG_WINDOWS *) calloc(1, sizeof(DIALOG_WINDOWS));#ifdef HAVE_COLOR if (dialog_state.use_shadow) { if ((win = newwin(height, width, y + 1, x + 2)) != 0) { dlg_draw_shadow(win, 0, 0, height, width); } p->shadow = win; }#endif if ((win = newwin(height, width, y, x)) == 0) { dlg_exiterr("Can't make new window at (%d,%d), size (%d,%d).\n", y, x, height, width); } p->next = dialog_state.all_windows; p->normal = win; dialog_state.all_windows = p; (void) keypad(win, TRUE); return win;}WINDOW *dlg_sub_window(WINDOW *parent, int height, int width, int y, int x){ WINDOW *win; if ((win = subwin(parent, height, width, y, x)) == 0) { dlg_exiterr("Can't make sub-window at (%d,%d), size (%d,%d).\n", y, x, height, width); } (void) keypad(win, TRUE); return win;}intdlg_default_item(char **items, int llen){ if (dialog_vars.default_item != 0) { int count = 0; while (*items != 0) { if (!strcmp(dialog_vars.default_item, *items)) return count; items += llen; count++; } } return 0;}/* * Draw the string for item_help */voiddlg_item_help(char *txt){ if (USE_ITEM_HELP(txt)) { chtype attr = A_NORMAL; wattrset(stdscr, itemhelp_attr); (void) wmove(stdscr, LINES - 1, 0); (void) wclrtoeol(stdscr); (void) addch(' '); dlg_print_text(stdscr, txt, COLS - 2, &attr); (void) wnoutrefresh(stdscr); }}#ifndef HAVE_STRCASECMPintdlg_strcmp(const char *a, const char *b){ int ac, bc, cmp; for (;;) { ac = UCH(*a++); bc = UCH(*b++); if (isalpha(ac) && islower(ac)) ac = _toupper(ac); if (isalpha(bc) && islower(bc)) bc = _toupper(bc); cmp = ac - bc; if (ac == 0 || bc == 0 || cmp != 0) break; } return cmp;}#endif/* * Returns true if 'dst' points to a blank which follows another blank which * is not a leading blank on a line. */static booltrim_blank(char *base, char *dst){ int count = 0; while (dst-- != base) { if (*dst == '\n') { return FALSE; } else if (*dst != ' ') { return (count > 1); } else { count++; } } return FALSE;}/* * Change embedded "\n" substrings to '\n' characters and tabs to single * spaces. If there are no "\n"s, it will strip all extra spaces, for * justification. If it has "\n"'s, it will preserve extra spaces. If cr_wrap * is set, it will preserve '\n's. */voiddlg_trim_string(char *s){ char *base = s; char *p1; char *p = s; int has_newlines = (strstr(s, "\\n") != 0); while (*p != '\0') { if (*p == '\t' && !dialog_vars.nocollapse) *p = ' '; if (has_newlines) { /* If prompt contains "\n" strings */ if (*p == '\\' && *(p + 1) == 'n') { *s++ = '\n'; p += 2; p1 = p; /* * Handle end of lines intelligently. If '\n' follows "\n" * then ignore the '\n'. This eliminates the need to escape * the '\n' character (no need to use "\n\"). */ while (*p1 == ' ') p1++; if (*p1 == '\n') p = p1 + 1; } else if (*p == '\n') { if (dialog_vars.cr_wrap) *s++ = *p++; else { /* Replace the '\n' with a space if cr_wrap is not set */ if (!trim_blank(base, s)) *s++ = ' '; p++; } } else /* If *p != '\n' */ *s++ = *p++; } else if (dialog_vars.trim_whitespace) { if (*p == ' ') { if (*(s - 1) != ' ') { *s++ = ' '; p++; } else p++; } else if (*p == '\n') { if (dialog_vars.cr_wrap) *s++ = *p++; else if (*(s - 1) != ' ') { /* Strip '\n's if cr_wrap is not set. */ *s++ = ' '; p++; } else p++; } else *s++ = *p++; } else { /* If there are no "\n" strings */ if (*p == ' ' && !dialog_vars.nocollapse) { if (!trim_blank(base, s)) *s++ = *p; p++; } else *s++ = *p++; } } *s = '\0';}voiddlg_set_focus(WINDOW *parent, WINDOW *win){ if (win != 0) { (void) wmove(parent, getpary(win) + getcury(win), getparx(win) + getcurx(win)); (void) wnoutrefresh(win); (void) doupdate(); }}/* * Most of the time we can copy into a fixed buffer. This handles the other * cases, e.g., checklist.c */voiddlg_add_result(char *string){ unsigned have = strlen(dialog_vars.input_result); unsigned want = strlen(string) + have; if (want >= MAX_LEN) { if (dialog_vars.input_length == 0) { char *save = dialog_vars.input_result; dialog_vars.input_length = want * 2; dialog_vars.input_result = malloc(dialog_vars.input_length); assert_ptr(dialog_vars.input_result, "dlg_add_result malloc"); dialog_vars.input_result[0] = 0; if (save != 0) strcpy(dialog_vars.input_result, save); } else if (want >= dialog_vars.input_length) { dialog_vars.input_length = want * 2; dialog_vars.input_result = realloc(dialog_vars.input_result, dialog_vars.input_length); assert_ptr(dialog_vars.input_result, "dlg_add_result realloc"); } } strcat(dialog_vars.input_result, string);}/* * Add a quoted string to the result buffer. */voiddlg_add_quoted(char *string){ char temp[2]; if (strcspn(string, " \n\t\\\"[]{}?*;`~#$^&()|<>") == strlen(string)) { dlg_add_result(string); } else { temp[1] = '\0'; dlg_add_result("\""); while (*string != '\0') { temp[0] = *string++; if (*temp == '"') dlg_add_result("\\"); dlg_add_result(temp); } dlg_add_result("\""); }}/* * Called each time a widget is invoked which may do output, increment a count. */voiddlg_does_output(void){ dialog_state.output_count += 1;}/* * Compatibility for different versions of curses. */#if !(defined(HAVE_GETBEGX) && defined(HAVE_GETBEGY))intgetbegx(WINDOW *win){ int y, x; getbegyx(win, y, x); return x;}intgetbegy(WINDOW *win){ int y, x; getbegyx(win, y, x); return y;}#endif#if !(defined(HAVE_GETCURX) && defined(HAVE_GETCURY))intgetcurx(WINDOW *win){ int y, x; getyx(win, y, x); return x;}intgetcury(WINDOW *win){ int y, x; getyx(win, y, x); return y;}#endif#if !(defined(HAVE_GETMAXX) && defined(HAVE_GETMAXY))intgetmaxx(WINDOW *win){ int y, x; getmaxyx(win, y, x); return x;}intgetmaxy(WINDOW *win){ int y, x; getmaxyx(win, y, x); return y;}#endif#if !(defined(HAVE_GETPARX) && defined(HAVE_GETPARY))intgetparx(WINDOW *win){ int y, x; getparyx(win, y, x); return x;}intgetpary(WINDOW *win){ int y, x; getparyx(win, y, x); return y;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -