📄 util.c
字号:
/* * util.c * * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk) * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include "dialog.h"struct dialog_info dlg;static void set_mono_theme(void){ dlg.screen.atr = A_NORMAL; dlg.shadow.atr = A_NORMAL; dlg.dialog.atr = A_NORMAL; dlg.title.atr = A_BOLD; dlg.border.atr = A_NORMAL; dlg.button_active.atr = A_REVERSE; dlg.button_inactive.atr = A_DIM; dlg.button_key_active.atr = A_REVERSE; dlg.button_key_inactive.atr = A_BOLD; dlg.button_label_active.atr = A_REVERSE; dlg.button_label_inactive.atr = A_NORMAL; dlg.inputbox.atr = A_NORMAL; dlg.inputbox_border.atr = A_NORMAL; dlg.searchbox.atr = A_NORMAL; dlg.searchbox_title.atr = A_BOLD; dlg.searchbox_border.atr = A_NORMAL; dlg.position_indicator.atr = A_BOLD; dlg.menubox.atr = A_NORMAL; dlg.menubox_border.atr = A_NORMAL; dlg.item.atr = A_NORMAL; dlg.item_selected.atr = A_REVERSE; dlg.tag.atr = A_BOLD; dlg.tag_selected.atr = A_REVERSE; dlg.tag_key.atr = A_BOLD; dlg.tag_key_selected.atr = A_REVERSE; dlg.check.atr = A_BOLD; dlg.check_selected.atr = A_REVERSE; dlg.uarrow.atr = A_BOLD; dlg.darrow.atr = A_BOLD;}#define DLG_COLOR(dialog, f, b, h) \do { \ dlg.dialog.fg = (f); \ dlg.dialog.bg = (b); \ dlg.dialog.hl = (h); \} while (0)static void set_classic_theme(void){ DLG_COLOR(screen, COLOR_CYAN, COLOR_BLUE, true); DLG_COLOR(shadow, COLOR_BLACK, COLOR_BLACK, true); DLG_COLOR(dialog, COLOR_BLACK, COLOR_WHITE, false); DLG_COLOR(title, COLOR_YELLOW, COLOR_WHITE, true); DLG_COLOR(border, COLOR_WHITE, COLOR_WHITE, true); DLG_COLOR(button_active, COLOR_WHITE, COLOR_BLUE, true); DLG_COLOR(button_inactive, COLOR_BLACK, COLOR_WHITE, false); DLG_COLOR(button_key_active, COLOR_WHITE, COLOR_BLUE, true); DLG_COLOR(button_key_inactive, COLOR_RED, COLOR_WHITE, false); DLG_COLOR(button_label_active, COLOR_YELLOW, COLOR_BLUE, true); DLG_COLOR(button_label_inactive, COLOR_BLACK, COLOR_WHITE, true); DLG_COLOR(inputbox, COLOR_BLACK, COLOR_WHITE, false); DLG_COLOR(inputbox_border, COLOR_BLACK, COLOR_WHITE, false); DLG_COLOR(searchbox, COLOR_BLACK, COLOR_WHITE, false); DLG_COLOR(searchbox_title, COLOR_YELLOW, COLOR_WHITE, true); DLG_COLOR(searchbox_border, COLOR_WHITE, COLOR_WHITE, true); DLG_COLOR(position_indicator, COLOR_YELLOW, COLOR_WHITE, true); DLG_COLOR(menubox, COLOR_BLACK, COLOR_WHITE, false); DLG_COLOR(menubox_border, COLOR_WHITE, COLOR_WHITE, true); DLG_COLOR(item, COLOR_BLACK, COLOR_WHITE, false); DLG_COLOR(item_selected, COLOR_WHITE, COLOR_BLUE, true); DLG_COLOR(tag, COLOR_YELLOW, COLOR_WHITE, true); DLG_COLOR(tag_selected, COLOR_YELLOW, COLOR_BLUE, true); DLG_COLOR(tag_key, COLOR_YELLOW, COLOR_WHITE, true); DLG_COLOR(tag_key_selected, COLOR_YELLOW, COLOR_BLUE, true); DLG_COLOR(check, COLOR_BLACK, COLOR_WHITE, false); DLG_COLOR(check_selected, COLOR_WHITE, COLOR_BLUE, true); DLG_COLOR(uarrow, COLOR_GREEN, COLOR_WHITE, true); DLG_COLOR(darrow, COLOR_GREEN, COLOR_WHITE, true);}static void set_blackbg_theme(void){ DLG_COLOR(screen, COLOR_RED, COLOR_BLACK, true); DLG_COLOR(shadow, COLOR_BLACK, COLOR_BLACK, false); DLG_COLOR(dialog, COLOR_WHITE, COLOR_BLACK, false); DLG_COLOR(title, COLOR_RED, COLOR_BLACK, false); DLG_COLOR(border, COLOR_BLACK, COLOR_BLACK, true); DLG_COLOR(button_active, COLOR_YELLOW, COLOR_RED, false); DLG_COLOR(button_inactive, COLOR_YELLOW, COLOR_BLACK, false); DLG_COLOR(button_key_active, COLOR_YELLOW, COLOR_RED, true); DLG_COLOR(button_key_inactive, COLOR_RED, COLOR_BLACK, false); DLG_COLOR(button_label_active, COLOR_WHITE, COLOR_RED, false); DLG_COLOR(button_label_inactive, COLOR_BLACK, COLOR_BLACK, true); DLG_COLOR(inputbox, COLOR_YELLOW, COLOR_BLACK, false); DLG_COLOR(inputbox_border, COLOR_YELLOW, COLOR_BLACK, false); DLG_COLOR(searchbox, COLOR_YELLOW, COLOR_BLACK, false); DLG_COLOR(searchbox_title, COLOR_YELLOW, COLOR_BLACK, true); DLG_COLOR(searchbox_border, COLOR_BLACK, COLOR_BLACK, true); DLG_COLOR(position_indicator, COLOR_RED, COLOR_BLACK, false); DLG_COLOR(menubox, COLOR_YELLOW, COLOR_BLACK, false); DLG_COLOR(menubox_border, COLOR_BLACK, COLOR_BLACK, true); DLG_COLOR(item, COLOR_WHITE, COLOR_BLACK, false); DLG_COLOR(item_selected, COLOR_WHITE, COLOR_RED, false); DLG_COLOR(tag, COLOR_RED, COLOR_BLACK, false); DLG_COLOR(tag_selected, COLOR_YELLOW, COLOR_RED, true); DLG_COLOR(tag_key, COLOR_RED, COLOR_BLACK, false); DLG_COLOR(tag_key_selected, COLOR_YELLOW, COLOR_RED, true); DLG_COLOR(check, COLOR_YELLOW, COLOR_BLACK, false); DLG_COLOR(check_selected, COLOR_YELLOW, COLOR_RED, true); DLG_COLOR(uarrow, COLOR_RED, COLOR_BLACK, false); DLG_COLOR(darrow, COLOR_RED, COLOR_BLACK, false);}static void set_bluetitle_theme(void){ set_classic_theme(); DLG_COLOR(title, COLOR_BLUE, COLOR_WHITE, true); DLG_COLOR(button_key_active, COLOR_YELLOW, COLOR_BLUE, true); DLG_COLOR(button_label_active, COLOR_WHITE, COLOR_BLUE, true); DLG_COLOR(searchbox_title, COLOR_BLUE, COLOR_WHITE, true); DLG_COLOR(position_indicator, COLOR_BLUE, COLOR_WHITE, true); DLG_COLOR(tag, COLOR_BLUE, COLOR_WHITE, true); DLG_COLOR(tag_key, COLOR_BLUE, COLOR_WHITE, true);}/* * Select color theme */static int set_theme(const char *theme){ int use_color = 1; if (!theme) set_bluetitle_theme(); else if (strcmp(theme, "classic") == 0) set_classic_theme(); else if (strcmp(theme, "bluetitle") == 0) set_bluetitle_theme(); else if (strcmp(theme, "blackbg") == 0) set_blackbg_theme(); else if (strcmp(theme, "mono") == 0) use_color = 0; return use_color;}static void init_one_color(struct dialog_color *color){ static int pair = 0; pair++; init_pair(pair, color->fg, color->bg); if (color->hl) color->atr = A_BOLD | COLOR_PAIR(pair); else color->atr = COLOR_PAIR(pair);}static void init_dialog_colors(void){ init_one_color(&dlg.screen); init_one_color(&dlg.shadow); init_one_color(&dlg.dialog); init_one_color(&dlg.title); init_one_color(&dlg.border); init_one_color(&dlg.button_active); init_one_color(&dlg.button_inactive); init_one_color(&dlg.button_key_active); init_one_color(&dlg.button_key_inactive); init_one_color(&dlg.button_label_active); init_one_color(&dlg.button_label_inactive); init_one_color(&dlg.inputbox); init_one_color(&dlg.inputbox_border); init_one_color(&dlg.searchbox); init_one_color(&dlg.searchbox_title); init_one_color(&dlg.searchbox_border); init_one_color(&dlg.position_indicator); init_one_color(&dlg.menubox); init_one_color(&dlg.menubox_border); init_one_color(&dlg.item); init_one_color(&dlg.item_selected); init_one_color(&dlg.tag); init_one_color(&dlg.tag_selected); init_one_color(&dlg.tag_key); init_one_color(&dlg.tag_key_selected); init_one_color(&dlg.check); init_one_color(&dlg.check_selected); init_one_color(&dlg.uarrow); init_one_color(&dlg.darrow);}/* * Setup for color display */static void color_setup(const char *theme){ int use_color; use_color = set_theme(theme); if (use_color && has_colors()) { start_color(); init_dialog_colors(); } else set_mono_theme();}/* * Set window to attribute 'attr' */void attr_clear(WINDOW * win, int height, int width, chtype attr){ int i, j; wattrset(win, attr); for (i = 0; i < height; i++) { wmove(win, i, 0); for (j = 0; j < width; j++) waddch(win, ' '); } touchwin(win);}void dialog_clear(void){ attr_clear(stdscr, LINES, COLS, dlg.screen.atr); /* Display background title if it exists ... - SLH */ if (dlg.backtitle != NULL) { int i; wattrset(stdscr, dlg.screen.atr); mvwaddstr(stdscr, 0, 1, (char *)dlg.backtitle); wmove(stdscr, 1, 1); for (i = 1; i < COLS - 1; i++) waddch(stdscr, ACS_HLINE); } wnoutrefresh(stdscr);}/* * Do some initialization for dialog */void init_dialog(const char *backtitle){ dlg.backtitle = backtitle; color_setup(getenv("MENUCONFIG_COLOR"));}void set_dialog_backtitle(const char *backtitle){ dlg.backtitle = backtitle;}void reset_dialog(void){ initscr(); /* Init curses */ keypad(stdscr, TRUE); cbreak(); noecho(); dialog_clear();}/* * End using dialog functions. */void end_dialog(void){ endwin();}/* Print the title of the dialog. Center the title and truncate * tile if wider than dialog (- 2 chars). **/void print_title(WINDOW *dialog, const char *title, int width){ if (title) { int tlen = MIN(width - 2, strlen(title)); wattrset(dialog, dlg.title.atr); mvwaddch(dialog, 0, (width - tlen) / 2 - 1, ' '); mvwaddnstr(dialog, 0, (width - tlen)/2, title, tlen); waddch(dialog, ' '); }}/* * Print a string of text in a window, automatically wrap around to the * next line if the string is too long to fit on one line. Newline * characters '\n' are replaced by spaces. We start on a new line * if there is no room for at least 4 nonblanks following a double-space. */void print_autowrap(WINDOW * win, const char *prompt, int width, int y, int x){ int newl, cur_x, cur_y; int i, prompt_len, room, wlen; char tempstr[MAX_LEN + 1], *word, *sp, *sp2; strcpy(tempstr, prompt);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -