📄 string_dialog.hpp
字号:
/* Copyright(c) Ben Bear 2003-2004 */// 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., 59 Temple Place - Suite 330, Boston, MA// 02111-1307, USA.#ifndef __string_dialog_hpp#define __string_dialog_hpp#include <curses.h>#include <string.h>#include <ctype.h>class string_dialog{private: WINDOW *dialog; int dlg_h, dlg_w; const char* info; int info_len; char* buf; char* str_end; int buf_len; int box_len; int cur_head; int cur_pos; short color_f, color_b;private: bool new_dialog (); void del_dialog (); void move_cur (); void insert (int ch); void erase (char *pos); void update (); void brower (int key);public: string_dialog (const char* text, char* str, int len) : info(text), buf(str), buf_len(len) { if ((buf == 0) || (buf_len <= 0)) throw -1; info_len = strlen(info); str_end = buf + strlen(buf); } bool show ();};boolstring_dialog::new_dialog (){ dlg_h = 5; dlg_w = 30; if (dlg_w < info_len+2) dlg_w = info_len+2; int y, x; getmaxyx (stdscr, y, x); dialog = newwin (dlg_h, dlg_w, (y-dlg_h)/2, (x-dlg_w)/2); keypad (dialog, TRUE); wattron (dialog, A_REVERSE); //wattrset (dialog, A_REVERSE); werase (dialog); box (dialog, 0, 0); box_len = dlg_w-4; char tmp[12]; sprintf (tmp, "%%-%ds", dlg_w-2); mvwprintw (dialog, 1, 1, tmp, info); mvwprintw (dialog, 2, 1, tmp, ""); mvwaddch (dialog, 3, 1, ' '); mvwaddch (dialog, 3, dlg_w-2, ' '); if (has_colors()) { pair_content (1, &color_f, &color_b); init_pair (1, COLOR_GREEN, COLOR_BLACK); wattron (dialog, COLOR_PAIR(1)); } wattroff (dialog, A_REVERSE); sprintf (tmp, "%%%ds", box_len); mvwprintw (dialog, 3, 2, tmp, ""); wrefresh (dialog); cur_head = 0; cur_pos = 0;}voidstring_dialog::del_dialog (){ if (has_colors()) init_pair (1, color_f, color_b); wclear (dialog); wrefresh (dialog); delwin (dialog);}boolstring_dialog::show (){ new_dialog (); int ch; do { wmove (dialog, 3, 2+cur_pos); ch = wgetch (dialog); if (isprint(ch))// && ch != '\t') { insert (ch); update (); } else brower (ch); } while (ch != '\n'); del_dialog ();}voidstring_dialog::insert (int ch){ if (str_end == buf+buf_len) return; char* pos = buf + (cur_head + cur_pos); for (char *p = str_end; p >= pos; --p) *(p+1) = *p; *pos = ch; ++str_end; brower (KEY_RIGHT);}voidstring_dialog::erase (char* pos){ for (char *p = pos+1; p <= str_end; ++p) *(p-1) = *p; --str_end;}voidstring_dialog::update (){ char tmp[12]; sprintf (tmp, "%%%ds", box_len); mvwprintw (dialog, 3, 2, tmp, ""); mvwaddnstr (dialog, 3, 2, buf+cur_head, box_len);}voidstring_dialog::brower (int key){ bool chg = false; switch (key) { case KEY_LEFT: if (cur_head + cur_pos == 0) return; if (cur_pos > 0) --cur_pos; else { if (cur_head >= box_len) { cur_head -= box_len-1; cur_pos = box_len-2; } else { cur_pos = cur_head-1; cur_head = 0; } chg = true; } break; case KEY_RIGHT: if (buf[cur_head+cur_pos] == '\0') return; if (cur_pos < box_len-1) ++cur_pos; else { cur_head += box_len-1; cur_pos = 1; chg = true; } break; case KEY_HOME: if (cur_head + cur_pos >= box_len) chg = true; cur_head = 0; cur_pos = 0; break; case KEY_END: cur_head = str_end - buf; if (cur_head >= box_len) { cur_head -= box_len-1; cur_pos = box_len-1; } else { cur_pos = cur_head; cur_head = 0; } chg = true; break; case KEY_BACKSPACE: if (cur_head + cur_pos == 0) return; brower (KEY_LEFT); case KEY_DC: { char *pos = buf + cur_head + cur_pos; if (*pos == '\0') return; erase (pos); if ((pos == str_end) && (cur_pos == 0)) brower (KEY_END); chg = true; break; } } if (chg) update ();}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -