📄 menulib.c
字号:
} /* Display message text */ wcls(txtwin,screen.text); mvwaddstr(txtwin,0,0,msg); touchwin(txtwin); wrefresh(txtwin); wrefresh(stdscr); free(msg);}/*----------------------------------------------------------------------------* * Function: 清除版权信息. * Argument: * None. * Return : * None. *----------------------------------------------------------------------------*/void clear_copyright(){ if( menuhead.copyright == NULL ) return; cls(copyright_sy,copyright_sx,copyright_row,copyright_col,screen.client);}/*----------------------------------------------------------------------------* * Function: 菜单主循环. * Argument: * None. * Return : * > 0 被选择的菜单id. * = 0 按了ESC键或'0'. *----------------------------------------------------------------------------*/int menu_select(){ menu_t *ptr,*saveptr; int ret,i; char proc[128]; uchar ch; cursor_control(CUR_DISABLE); cls(menuhead.sy,0,screen_maxrow,screen_maxcol,screen.client); if( not_color && screen.mstyle & MENU_PULLDOWN && !(screen.mstyle&MENU_BOX) ) { mvaddstr(menuhead.sy+1,0,"────────────────────────────────────────"); } disp_title(); if( menuhead.disp_copyright_flag != 0 ) { disp_copyright(); } if( disp_menu(menuhead.menu) == FALSE ) { cursor_control(CUR_ENABLE); return -1; } ptr = menuhead.menu; while(1) { disp_prompts(ptr->citem->htext); disp_menuitem(ptr,REV); ch=get_char(); if( menuhead.disp_copyright_flag == 1 ) /* Clear title */ { clear_copyright(); } switch(ch) { case '0': if( ptr->parent == NULL ) { cursor_control(CUR_ENABLE); return(-1); } else { hide_window(ptr->win); ptr=ptr->parent; if( ptr->parent == NULL ) /* Top menu */ { menuhead.status = 0; } } break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': if( !(screen.mstyle & MENU_ACCEPT_NUMBER) && !(screen.mstyle & MENU_DISP_NUMBER) ) break; disp_menuitem(ptr,NOR); get_menuhandle(ptr,(uchar)(ch-'0')); if( !(screen.mstyle & MENU_DIRECT_ACCEPT) ) break; disp_menuitem(ptr,REV); case KeyEnter: /* Enter */ if( ptr->citem->child == NULL ) { if( ptr->citem->flag & MF_GRAYED ) break; if( screen.mstyle & MENU_RETURN_ID ) { ret = ptr->citem->id; /* Return top menu */ while( ptr->parent != NULL ) { hide_window(ptr->win); ptr = ptr->parent; } menuhead.status = 0; cursor_control(CUR_ENABLE); return(ret); } else if( screen.mstyle & MENU_EXEC_PROC ) { strcpy(proc,ptr->citem->proc); saveptr=ptr; /* Return top menu */ while( ptr->parent != NULL ) { hide_window(ptr->win); ptr = ptr->parent; } clear_copyright(); save_vs(0,0,screen_maxrow,screen_maxcol); endwin(); system(proc); while( ptr != saveptr ) { ptr=ptr->citem->child; disp_menu(ptr); } } break; } ptr=ptr->citem->child; disp_menu(ptr); menuhead.status = 1; break; case KeyEsc: /* ESC */ if( !get_key() ) /* Press ESC key */ { if( ptr->parent == NULL ) { cursor_control(CUR_ENABLE); return(-1); } else { hide_window(ptr->win); ptr=ptr->parent; if( ptr->parent == NULL ) /* Top menu */ { menuhead.status = 0; } } break; } /* Press arrow key */ switch(get_key()) { case KeyUp: /* Up */ disp_menuitem(ptr,NOR); if( !((ptr->parent == NULL) && (screen.mstyle & MENU_PULLDOWN)) ) { ptr->citem = ptr->citem->prev; if( ptr->citem->index == 0 ) /* Separator */ ptr->citem = ptr->citem->prev; } break; case KeyDown: /* Down */ if( (ptr->parent == NULL) && (screen.mstyle & MENU_PULLDOWN) ) { if( ptr->citem->child != NULL ) { ptr=ptr->citem->child; disp_menu(ptr); menuhead.status = 1; } } else { disp_menuitem(ptr,NOR); ptr->citem = ptr->citem->next; if( ptr->citem->index == 0 ) /* Separator */ ptr->citem = ptr->citem->next; } break; case KeyRight: /* Right */ /* Pulldown's top menu */ if( ptr->parent == NULL && screen.mstyle & MENU_PULLDOWN ) { disp_menuitem(ptr,NOR); ptr->citem = ptr->citem->next; if( (menuhead.status == 1) && ptr->citem->child ) { disp_menuitem(ptr,REV); ptr=ptr->citem->child; disp_menu(ptr); } break; } /* 接受RIGHT,并且有子菜单 */ if( (screen.mstyle & MENU_ACCEPT_RIGHT) && (ptr->citem->child != NULL) ) { ptr=ptr->citem->child; disp_menu(ptr); break; } /* PULLDOWN菜单,并且接受RIGHT或是第二层子菜单 */ if( (screen.mstyle & MENU_PULLDOWN) && ((ptr->parent->parent == NULL) || (screen.mstyle & MENU_ACCEPT_RIGHT)) ) { /* Return top menu */ while( ptr->parent != NULL ) { hide_window(ptr->win); ptr = ptr->parent; } touchwin(stdscr); disp_menuitem(ptr,NOR); ptr->citem = ptr->citem->next; if( (menuhead.status == 1) && ptr->citem->child ) { disp_menuitem(ptr,REV); ptr=ptr->citem->child; disp_menu(ptr); } break; } else { disp_menuitem(ptr,NOR); ptr->citem = ptr->citem->next; if( ptr->citem->index == 0 ) /* Separator */ ptr->citem = ptr->citem->next; } break; case KeyLeft: /* Left */ /* Top menu */ if( ptr->parent == NULL ) { disp_menuitem(ptr,NOR); ptr->citem = ptr->citem->prev; if( (menuhead.status == 1) && ptr->citem->child ) { disp_menuitem(ptr,REV); ptr=ptr->citem->child; disp_menu(ptr); } break; } if( screen.mstyle & MENU_PULLDOWN && ptr->parent->parent == NULL ) { /* Return top menu */ while( ptr->parent != NULL ) { hide_window(ptr->win); ptr = ptr->parent; } touchwin(stdscr); disp_menuitem(ptr,NOR); ptr->citem = ptr->citem->prev; if( (menuhead.status == 1) && ptr->citem->child ) { disp_menuitem(ptr,REV); ptr=ptr->citem->child; disp_menu(ptr); } break; } if( screen.mstyle & MENU_ACCEPT_RIGHT ) { /* Return upper menu */ hide_window(ptr->win); ptr = ptr->parent; break; } else { disp_menuitem(ptr,NOR); ptr->citem = ptr->citem->prev; if( ptr->citem->index == 0 ) /* Separator */ ptr->citem = ptr->citem->prev; } break; } break; default: if( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ) { disp_menuitem(ptr,NOR); if( get_menuhandle(ptr,ch) == FALSE ) break; disp_menuitem(ptr,REV); if( ptr->citem->child == NULL ) { if( ptr->citem->flag & MF_GRAYED ) break; if( screen.mstyle & MENU_RETURN_ID ) { ret = ptr->citem->id; /* Return top menu */ while( ptr->parent != NULL ) { hide_window(ptr->win); ptr = ptr->parent; } touchwin(stdscr); wrefresh(stdscr); cursor_control(CUR_ENABLE); return(ret); } else if( screen.mstyle & MENU_EXEC_PROC ) { strcpy(proc,ptr->citem->proc); saveptr=ptr; /* Return top menu */ while( ptr->parent != NULL ) { hide_window(ptr->win); ptr = ptr->parent; } clear_copyright(); save_vs(0,0,screen_maxrow,screen_maxcol); endwin(); system(proc); while( ptr != saveptr ) { ptr=ptr->citem->child; disp_menu(ptr); } break; } } ptr=ptr->citem->child; disp_menu(ptr); break; } beep(); break; } }}/****************************************************************************** * <第三章> 菜单操作函数. * ******************************************************************************//*----------------------------------------------------------------------------* * Function: 取菜单显示串. * Argument: * char *str - 串. * char *menutext - 显示串. * Return : * 串长. *----------------------------------------------------------------------------*/int get_menuitem(char *str,char *menutext){ int i=0; char *ptr,buf[128]; ptr = str; while( *ptr ) { if( *ptr == '"' ) break; ptr++; } ptr++; while( *ptr ) { if( *ptr == '"' ) break; buf[i++]=*ptr++; } buf[i] = 0; strcpy(menutext,buf); return strlen(buf);}/*----------------------------------------------------------------------------* * Function: 取符号的值. * Argument: * char *str - 串. * Return : * >= 0 符号的值. * = -1 没有匹配项. *----------------------------------------------------------------------------*/int in_symbol_table(char *str){ register int i=0; while( menu_symbol[i].value != -1 ) { if( strcmp(str,menu_symbol[i].name) == 0 ) return menu_symbol[i].value; i++; } return -1;}/*----------------------------------------------------------------------------* * Function: 获得菜单id. * Argument: * char *str - 串. * Return : * >= 0 菜单id. *----------------------------------------------------------------------------*/int get_id(char *str){ char tmp[128]; int value; memset(tmp,'\0',sizeof(tmp)); strcpy(tmp,get_item(str,',',1)); if( strlen(tmp) == 0 ) return -1; ltrim(tmp); rtrim(tmp); if( (value=atoi(tmp)) > 0 ) { return(value); } else { return 1; }}/*----------------------------------------------------------------------------* * Function: 获得指定的菜单项的状态. * Argument: * menu_t *hmenu - 菜单句柄(即菜单的入口指针). * int position - 指定菜的单项的位置(1...n). * uchar *flag - 控制标志:MF_ENABLED,MF_GRAYED,MF_CHECKED,MF_UNCHECKED * Return : * >= 0 菜单项的先前状态. * < 0 菜单项不存在或是一分隔符. *----------------------------------------------------------------------------*/get_menuitem_status(menu_t *hmenu,int position,uchar *flag){ register int i; menuitem_t *ptr; ptr = hmenu->hitem; for(i=1;i<position;i++) { ptr = ptr->next; if( ptr == hmenu->hitem ) { *flag = 0; return -1; } } if( ptr->index == 0 ) /* This is a separator */ { *flag = 0; return -1; } else { *flag = ptr->flag; return *flag; }}/*----------------------------------------------------------------------------* * Function: 允许,禁止或变灰指定的菜单项. * Argument: * menu_t *hmenu - 菜单句柄(即菜单的入口指针). * int position - 指定菜的单项的位置(1...n). * uchar flag - 控制标志:MF_ENABLED,MF_GRAYED,MF_CHECKED,MF_UNCHECKED * Return : * >= 0 菜单项的先前状态. * < 0 菜单项不存在或是一分隔符. *----------------------------------------------------------------------------*/set_menuitem_status(menu_t *hmenu,int position,uchar flag){ uchar oldflag; register int i; menuitem_t *ptr; ptr = hmenu->hitem; for(i=1;i<position;i++) { ptr = ptr->next; if( ptr->index == 0 ) /* This is a separator */ ptr = ptr->next; if( ptr == hmenu->hitem ) { return -1; } } oldflag = ptr->flag; ptr->flag = flag; return oldflag;}/*----------------------------------------------------------------------------* * Function: 获得主菜单的句柄. * Argument: * None. * Return : * menu_t * - 菜单句柄(即菜单的入口指针). * NULL 如果该菜单项没有子菜单或没有找到该菜单项. *----------------------------------------------------------------------------*/menu_t *get_menu(){ return menuhead.menu;}/*----------------------------------------------------------------------------* * Function: 获得指定的菜单项的激活的子菜单的句柄. * Argument: * menu_t *hmenu - 菜单句柄(即菜单的入口指针). * int position - 指定菜的单项的位置(1...n). * Return : * menu_t * - 子菜单句柄(即子菜单的入口指针). * NULL 如果该菜单项没有子菜单或没有找到该菜单项. *----------------------------------------------------------------------------*/menu_t *get_submenu(menu_t *hmenu,int position){ register int i; menuitem_t *ptr; ptr = hmenu->hitem; for(i=1;i<position;i++) { ptr = ptr->next; if( ptr == hmenu->hitem ) { return NULL; } } return ptr->child;}/*----------------------------------------------------------------------------* * Function: Get system keyword. * Argument: None. * Return : * >= 0 成功. * < 0 失败. *----------------------------------------------------------------------------*/get_keyword(char *buf){ int len,i,j; char tmp[100]; len = strlen(buf); for(i=0;i<len;i++) { if( buf[i] != ' ' && buf[i] != '\t' ) break; } if( i == len ) return -1; j = 0; while(buf[i] != 0 ) { if( buf[i] != ' ' && buf[i] != '\t' ) tmp[j++] = buf[i++]; else break; } tmp[j] = 0; return(in_symbol_table(tmp));}/*----------------------------------------------------------------------------* * Function: 获得错误代码的信息. * Argument: * int errcode - 错误代码. * Return : * char * - 中文或英文的信息(取决 screen.wstyle). *----------------------------------------------------------------------------*/const char *get_errmsg(int errcode){ register unsigned short i; i = 0; while(TRUE) { if( errcode == error_code[i++].code ) { if( screen.wstyle & WINDOW_CHINESE ) return(error_code[i].chn_msg); else return(error_code[i].eng_msg); } if( error_code[i].code == 0 ) { if( screen.wstyle & WINDOW_CHINESE ) return(error_code[i].chn_msg); else return(error_code[i].eng_msg); } }}/*----------------------------------------------------------------------------* * Function: 显示提示信息. * Argument: * char *msg - 提示信息. * Return : * None. *----------------------------------------------------------------------------*/void disp_prompts(char *msg){ char buf[DATA_LENGTH]; if( msg == NULL ) { if( DefaultMsg == NULL ) return; else strcpy(buf,DefaultMsg); } else strcpy(buf,msg); cls_line(screen_endy,0,80,screen.prompt); if( screen.wstyle & WINDOW_CHINESE ) { aprint(screen_endy,0,screen.prompt,"提示│"); if( strlen(buf) > 74 ) buf[74] = 0; aprint(screen_endy,6,screen.prompt,buf); } else { aprint(screen_endy,0,screen.prompt,"Prompts\xb3"); if( strlen(buf) > 72 ) buf[72] = 0; aprint(screen_endy,8,screen.prompt,buf); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -