📄 winstr.c
字号:
#include "../def/window.h"#include <ctype.h>/***** 函数: 行式菜单**** 返回: 菜单项编号, 首项为0.最后一项为退出*/int linemenu(win, y, msgwin,y1,x1,menu, count, dqxz)WINDOW *win; /*窗口指针*/short y; /*菜单显示行*/WINDOW *msgwin; /*帮助窗口指针*/short y1, x1; /*帮助栏起始坐标*/int count; /*菜单项数*/ int dqxz; /*当前选项*/char *menu[]; /*菜单字符串数组指针*/ { int sy[30],i,len,k,lenstart,choice = 0; short f, b, c; char key[30]; len = 0; for (i = 0; i < count; i++) { sy[i] = len; len = len + strlen(menu[i]) + 2; key[i]=menu[i][0]; if(!isalnum(key[i])) { disperror(msgwin,y1,x1,"菜单选择项应是数字或字母!"); return(0); } } choice=dqxz; if((sy[choice]+strlen(menu[choice]+2)>win->_maxx)){ lenstart=sy[choice]; k=choice;} else{lenstart=0;k=0;} clearmsg(msgwin,y1,x1, 60); while(1){ for(i=2;i<win->_maxx-2;i++){ mvwaddch(win,y,i,' '); mvwaddch(win,y+1,i,'_');} for(i=k;(i<count)&&((sy[i]+strlen(menu[i])+2-lenstart)<win->_maxx);i++){ wmove(win,y,sy[i]-lenstart+2); waddstr(win,menu[i]); } wrefresh(win); wstandout(win); mvwaddstr(win, y, sy[choice]-lenstart+2, menu[choice]); wrefresh(win); wstandend(win); c = wacckey(win) ; clearmsg(msgwin,y1,x1, 60); for(i=0;i<=count;i++) if(key[i]==c) break; if(i<count) { choice = i;k=i;lenstart=sy[i];} else { switch (c) { case RET: case LF : wstandout(win); wmove(win, y, sy[choice]-lenstart+2); waddstr(win, menu[choice]); wrefresh(win); wstandend(win); return(choice); break; case KEY_LEFT: if(k==choice){ choice--; if (choice < 0) choice = count - 1; lenstart=sy[choice];k=choice; } else choice--; break; case KEY_RIGHT: choice++; if (choice == count){ choice = 0; lenstart=sy[0];k=0;} else if(sy[choice]+strlen(menu[choice])+4 > win->_maxx+lenstart){ for(i=0;i<choice;i++) if(sy[choice]+strlen(menu[choice])+4-sy[i]<win->_maxx)break; lenstart=sy[i];k=i; } break; default: printf("\007"); break; } } }}/***** 函数: 弹出式菜单**** 返回: 菜单项编号, 首项为0.*/pop_menu(win, y, x, msgwin, y1, x1, menu, count, dqxz)WINDOW *win; /*窗口指针*/short y, x; /*菜单起始坐标*/WINDOW *msgwin; /*帮助窗口指针*/short y1, x1; /*帮助栏起始坐标*/char *menu[]; /*菜单字符串数组指针*/int count; /*菜单项数*/int dqxz; /*当前选项*/{ int i, choice, choices; short f, b, c; char key[25]; if (count > win->_maxy - y - 2) choices = win->_maxy - y - 2; else choices = count; for (i = 0; i < choices; i++) { key[i]=menu[i][0]; if(!isalnum(key[i])) { disperror(msgwin,y1,x1,"菜单选择项应是数字或字母!"); return(0); } mvwprintw(win, i + y, x, "%s", menu[i]); } wrefresh(win); choice = dqxz; getcolor(win, f, b); for (; ; ) { wsetcolor(win, b, f); mvwaddstr(win, choice + y, x, menu[choice]); wrefresh(win); wsetcolor(win, f, b); wrefresh(win); c = wacckey(win); clearmsg(msgwin,y1,x1,60); if (isalnum(c)) { mvwaddstr(win, choice + y, x, menu[choice]); wrefresh(win); } for(i=0;i<=count;i++) if(key[i]==c) break; if(i<count) choice = i; else { switch (c) { case RET: case LF: return(choice); break; case KEY_DOWN: case KEY_RIGHT: mvwaddstr(win, choice + y, x, menu[choice]); wrefresh(win); choice++; if (choice > (count - 1)) choice = 0; break; case KEY_UP: case KEY_LEFT: mvwaddstr(win, choice + y, x, menu[choice]); wrefresh(win); choice--; if (choice < 0) choice = count - 1; break; default: printf("\007"); break; } } }}/***** 函数: 在win上接收用户键值(标准屏win用stdscr)**** 返回: 键值.*/wacckey(win)WINDOW *win;{ short c; raw(); c = wgetch(win); if (c == CTRL_F) { c = wgetch(win); switch (c) { case '1': return(KEY_F(1)); case '2': return(KEY_F(2)); case '3': return(KEY_F(3)); case '4': return(KEY_F(4)); default : return(c); } } else if (c == ESC) { c = wgetch(win); if (c == SQUARE) { c = wgetch(win); switch (c) { case 'A': return(KEY_UP); case 'B': return(KEY_DOWN); case 'C': return(KEY_RIGHT); case 'D': return(KEY_LEFT); case 'I': return(PAGE_UP); case 'G': return(PAGE_DOWN); case 'F': return(End); default : return(ERR); } } else return(c); } else return(c);}/***** 函数: 划中文制表符边框***/pbox(swin)WINDOW *swin; /*窗口指针*/{ int i; short y, x; y = swin->_maxy; x = swin->_maxx; for (i = 0; i <= (y - 1); i++) { if (i == 0) { mvwaddstr(swin, i , 0, "┌"); mvwaddstr(swin, i , x - 2, "┐"); } else if (i == (y - 1)) { mvwaddstr(swin, i , 0, "└"); mvwaddstr(swin, i , x - 2, "┘"); } else { mvwaddstr(swin, i , 0, "│"); mvwaddstr(swin, i , x - 2, "│"); } } for (i = 2; i < x - 2; i += 2) { mvwaddstr(swin, 0 , i, "─"); mvwaddstr(swin, y - 1 , i, "─"); } wrefresh(swin); return(0);}pbox_f(swin,f,b)WINDOW *swin; /*窗口指针*/int f, b; /*色值*/{ int i,j; short y, x; y = swin->_maxy; x = swin->_maxx; wsetcolor(swin, f, b); for (i = 0; i <= (y - 1); i++) { for (j = 0; j <= x - 2; j += 2) { if ( i == 0 ) { if (j==0) mvwaddstr(swin, i , j, "┌"); else if (j == ( x-2 ) ) mvwaddstr(swin, i , j, "┐"); else mvwaddstr(swin, i , j,"─"); } else if (i == (y - 1)) { if (j==0) mvwaddstr(swin, i , j, "└"); else if ( j == ( x-2 ) ) mvwaddstr(swin, i , j, "┘"); else mvwaddstr(swin, i , j,"─"); } else if ( j==0 ) mvwaddstr(swin, i , j, "│"); else if ( j==(x-2) ) mvwaddstr(swin, i , j, "│"); else mvwaddstr(swin, i , j, " "); } } wrefresh(swin); return(0);}/***** 函数: 给窗口填色.***/fillcolor(swin, fc, bc)WINDOW *swin; /*窗口指针*/int fc, bc;/*色值*/{ int i, j; short y, x; y = swin->_maxy; x = swin->_maxx; wsetcolor(swin, fc, bc); for (i = 0; i <= y; i++) for (j = 0; j <= x; j++) mvwaddch(swin, i, j, ' '); wrefresh(swin); return(0);}/***** 函数: 弹出栈顶窗口.***/pop(){ int i, j, k; /*short *p;*/ cchar_t *p; top--; /* 弹出栈顶窗口 */ curwin = stack[top]; /* 当前窗口指针指向栈顶窗口 */ if (top == 0) { /* 恢复弹出后的栈顶窗口 */ p = (cchar_t *)calloc(LINES * COLS, sizeof(cchar_t)); k = 0; for (i = 0; i < LINES; i++) for (j = 0; j < COLS; j++) p[k++] = stdscr->_y[i][j]; k = 0; clear(); refresh(); for (i = 0; i < LINES; i++) { for (j = 0; j < COLS; j++) stdscr->_y[i][j] = p[k++]; stdscr->_firstch[i] = 0; stdscr->_lastch[i] = COLS - 1; } refresh(); free(p); /* 栈顶窗口是 stdscr */ } else { touchwin(curwin); wrefresh(curwin); }}/***** 函数: 把窗口压入堆栈. ***/push(win)WINDOW *win; /*窗口指针*/{ int i; wclear(win); wrefresh(win); /* 清除窗口 */ top++; stack[top] = win; /* 窗口压入堆栈 */ curwin = win; /* 当前窗口指针指向栈顶窗口 */}/***** 函数: 恢复指定窗口后面的屏幕.***/restore_win(){ WINDOW * ptr; /* 窗口指示指针 */ int i, j, k; cchar_t *p; /* 暂存 stdscr 内容的变量 */ p = (cchar_t *)calloc(LINES * COLS, sizeof(cchar_t));/*给暂存变量分配空间 */ k = 0; for (i = 0; i < LINES; i++) for (j = 0; j < COLS; j++) p[k++] = stdscr->_y[i][j]; /* 读取 stdscr 的内容 */ k = 0; clear(); refresh(); for (i = 0; i < LINES; i++) { for (j = 0; j < COLS; j++) stdscr->_y[i][j] = p[k++]; stdscr->_firstch[i] = 0; stdscr->_lastch[i] = COLS - 1; } /* 恢复 stdscr 的内容 */ refresh(); free(p); i = 1; while (i <= top) { /* 恢复窗口堆栈中的其他窗口 */ ptr = stack[i]; touchwin(ptr); wrefresh(ptr); i++; }}/***** 函数: 恢复stdscr部分屏幕.***/restore_bfscr(y, x, y1, x1)short y, x; /*行列大小*/short y1, x1; /*起始坐标*/{ int i, j, k; cchar_t *p; /* 暂存 stdscr 内容的变量 */ p = (cchar_t *)calloc(y * x, sizeof(cchar_t));/*暂存变量分配空间 */ k = 0; for (i = y1; i < (y1 + y); i++) for (j = x1; j < (x1 + x); j++) p[k++] = stdscr->_y[i][j]; /* 读取 stdscr 的内容 */ k = 0; for (i = y1; i < (y1 + y); i++) { for (j = x1; j < (x1 + x); j++) stdscr->_y[i][j] = p[k++]; stdscr->_firstch[i] = 0; stdscr->_lastch[i] = COLS - 1; } /* 恢复 stdscr 的内容 */ refresh(); free(p);}/***** 函数: 接收密码键值***/acc_pwd(win, y, x, str, l)WINDOW *win; /*窗口*/short y, x; /*行,列*/short l; /*串长*/char *str; /*接收串*/{ short c, m, i; char *p; mvwaddstr(win, y - 1, x, " "); wrefresh(win); p = str; m = i = 0; do { mvwaddstr(win, y - 1, x, " "); mvwaddstr(win, y - 1, x + m, "
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -