📄 ip_dialog.hpp
字号:
/* Copyright(c) Ben Bear 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 __ip_dialog_hpp#define __ip_dialog_hpp#include <curses.h>#include <string.h>#include <ctype.h>class ip_dialog{private: enum { BUTTON_OK = -1, BUTTON_CANCEL = -2 };private: const char* info; WINDOW *dialog; int dlg_h, dlg_w; int part[4]; int part_x; int cur_p; int cur_c; WINDOW *win_btn; int btn_w;public: ip_dialog (const char* str) : info(str) { for (int i = 0; i < 4; ++i) part[i] = 0; } ip_dialog (const char* str, int p1, int p2, int p3, int p4) : info(str) { part[0] = p1 & 0xFF; part[1] = p2 & 0xFF; part[2] = p3 & 0xFF; part[3] = p4 & 0xFF; } bool show (); void get_ip (int& p1, int& p2, int& p3, int& p4);private: void new_dialog (); void del_dialog (); void init_part (); void show_part (int p); bool edit_part (int key); void brower (int key); void show_button (bool on); void show_item (bool on); int len_part (int p) { if (part[p] < 10) return 1; else if (part[p] < 100) return 2; else return 3; } void next_part () { cur_c = len_part(cur_p)-1; }};boolip_dialog::show (){ new_dialog (); cur_p = 0; cur_c = 0; int ch; show_item (true); do { ch = wgetch (dialog); if ((isdigit(ch) || (ch == KEY_BACKSPACE) || (ch == KEY_DC)) && (cur_p >= 0)) { if (!edit_part (ch)) beep (); else show_part (cur_p); continue; } switch (ch) { case KEY_LEFT: case KEY_RIGHT: case '\t': case ' ': case '.': show_item (false); brower (ch); show_item (true); break; } if ((ch == '\n') && (cur_p < 0)) break; } while (true); del_dialog (); if (cur_p == BUTTON_OK) return true; else return false;}voidip_dialog::get_ip (int& p1, int& p2, int& p3, int& p4){ p1 = part[0]; p2 = part[1]; p3 = part[2]; p4 = part[3];}voidip_dialog::new_dialog (){ int len = strlen(info); int y, x; getmaxyx (stdscr, y, x); dlg_h = 7; dlg_w = 30; if (dlg_w < len+2) dlg_w = len+2; dialog = newwin (dlg_h, dlg_w, (y-dlg_h)/2, (x-dlg_w)/2); keypad (dialog, TRUE); box (dialog, 0, 0); mvwprintw (dialog, 1, (dlg_w-len)/2, "%s", info); part_x = (dlg_w-15)/2; mvwprintw (dialog, 3, part_x-3, "IP: . . ."); for (int i = 0; i < 4; ++i) show_part (i); btn_w = dlg_w/3+8; win_btn = derwin (dialog, 1, btn_w, 5, dlg_w/3-4); keypad (win_btn, TRUE); mvwprintw (win_btn, 0, 0, "< OK >"); mvwprintw (win_btn, 0, btn_w - 10, "< Cancel >"); touchwin (dialog); wrefresh (dialog);}voidip_dialog::del_dialog (){ delwin (win_btn); wclear (dialog); wrefresh (dialog); delwin (dialog); dialog = 0;}voidip_dialog::show_part (int p){ wattron (dialog, A_REVERSE); mvwprintw (dialog, 3, part_x+p*4, "%3d", part[p]); wattroff (dialog, A_REVERSE); wmove (dialog, 3, part_x+p*4+2-cur_c); wrefresh (dialog);}voidip_dialog::show_button (bool on){ int type = on ? A_REVERSE : A_NORMAL; int x; int len; if (cur_p == BUTTON_OK) { x = 0; len = 6; } else { x = btn_w - 10; len = 10; } wmove (win_btn, 0, x); wchgat (win_btn, len, type, 0, NULL); wmove (dialog, 5, dlg_w/3-4+x+2); touchwin (dialog); wrefresh (dialog);}boolip_dialog::edit_part (int key){ int len = len_part (cur_p); int t = part[cur_p]; if (key == KEY_DC) { if (cur_c == 0) part[cur_p] /= 10; else if (cur_c == 1) { part[cur_p] = t / 100 * 10 + t % 10; if (part[cur_p] < 10) cur_c = 0; } else { part[cur_p] = t % 100; --cur_c; } } else if (key == KEY_BACKSPACE) { if (((len == 1) && (t == 0)) || (len == cur_c)) { brower (KEY_LEFT); brower (KEY_LEFT); return true; } else if (len == 1) t = 0; else if (len-1 == cur_c) return false; else if (cur_c == 0) t = t / 100 * 10 + t % 10; else if (cur_c == 1) t = t % 100; part[cur_p] = t; } else // is digit { key -= '0'; if ((t == 0) && (key == 0)) { brower (KEY_RIGHT); return true; } else if (cur_c == 0) t = t * 10 + key; else if (cur_c == 1) t = t / 10 * 100 + t % 10 + key * 10; else if (t >= 100) return false; else t += key * 100; if (t > 255) { if (cur_c == 0) { brower (KEY_RIGHT); return true; } else return false; } part[cur_p] = t; if ((cur_c == 0) && (part[cur_p] > 25)) { show_part (cur_p); brower (KEY_RIGHT); } } return true;}voidip_dialog::brower (int key){ switch (key) { case KEY_LEFT: if (cur_p < 0) { cur_p = ((cur_p == BUTTON_OK) ? BUTTON_CANCEL : BUTTON_OK); } else { int max = len_part (cur_p); if ((cur_c == 2) || (cur_c == max)) { if (cur_p > 0) { --cur_p; cur_c = 0; } } else ++cur_c; } break; case KEY_RIGHT: if (cur_p < 0) { cur_p = ((cur_p == BUTTON_OK) ? BUTTON_CANCEL : BUTTON_OK); } else { if (cur_c > 0) --cur_c; else if (cur_p < 3) { ++cur_p; next_part (); } } break; case '\t': if (cur_p == 3) cur_p = BUTTON_OK; else if (cur_p >= 0) { ++cur_p; next_part (); } else if (cur_p == BUTTON_OK) cur_p = BUTTON_CANCEL; else { cur_p = 0; next_part (); } break; case ' ': case '.': if (cur_p < 0) break; if (cur_p == 3) cur_p = 0; else ++cur_p; next_part (); break; }}voidip_dialog::show_item (bool on){ if (cur_p >= 0) { if (on) show_part (cur_p); } else show_button (on);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -