Linux-Visual Menu interface design

Source: Internet
Author: User

Optimize screen Refresh

Problem
Refreshing multiple forms requires a bit of complexity, and on a slow link, the screen is drawn quite slowly
The goal is to minimize the number of characters to be drawn on the screen

基本函数int wnoutrefresh(WINDOW *window_ptr);决定哪些字符需要发送到屏幕,但是并不实际的发送int doupdate(void);向终端发送实际的改变

Description
If you simply call Wnoutrefresh and then call DoUpdate immediately thereafter, the effect is just like calling Wrefresh
If you want to redraw a form stack, you can call the Wnoutrefresh function on each form (in the correct order), but call the DoUpdate function after the last Wnoutrefresh function
This allows curses to perform screen update calculations sequentially on each form, and only output the updated screen. This will allow curses to minimize the number of characters that need to be sent

*subwin*parentintintintyintx);subwin函数具有与newwin几乎相同的参数列表子窗体的删除方式也与其他的窗体使用一个delwin调用方式相同与新窗体类似,可以使用一系列的 mvw函数将数据写入子窗体中,但有一点重要区别子窗体本身并不会存储一个单独的屏幕字符集,他们与子窗体创建时所指定的父窗体共享存储空间这就意味着子窗体中的任何改动也同时会发生在底层的父窗体中,所以当一个子窗体被删除时,屏幕并不会发生变化int*window_to_delete);

After you point sub_window_ptr to the call result of Subwin, the subform becomes scrollable
Even after the subform is deleted and the base form (STRDCR) has been refreshed, the text on the screen remains intact
This is because the subform is actually updated with the STDSCR character data

Keypad mode

Handling of function keys
In most terminals, a string starting with an escape character is sent
These programs have not only the problem of clicking the Escape key and the difference between the strings caused by pressing a function key, but must also use different sequences of the same logic keys to handle different terminals
Curses provides an elegant and practical function to manage these function keys
For each terminal, a sequence of characters sent by each function key is stored, usually stored in a terminfo structure
The included header file curses.h has a defined section with a KEY_ prefix that defines the logic keys
When curses is started, the conversion between the sequence and the logic key is forbidden, and the keypad function must be used to open
int keypad (WINDOW *window_ptr, bool keypad_on);
Returns OK if the function call succeeds, otherwise it returns err

Three limitations of the keypad mode

The recognition of escape sequences is time-dependent
Many network protocols assemble characters into several packages (which can cause an escape sequence not to be recognized correctly)
or split them (which will cause the function key sequence to be recognized as escape with a single character)
The only way out
Programming, using signals to handle every function key you want to use, sending a single, unique character to it
For curses to differentiate between pressing escape and the keyboard sequence that begins with escape, you must wait for a short period of time
Sometimes, once the keypad mode is turned on, a very small delay in the escape key processing is also noted
Curses cannot handle non-unique escape sequences
If the terminal has two different keys that can send the same sequence, curses simply does not process the sequence because he cannot determine which logic key to return

Color display

Most early versions of curses do not support color
Color is supported by ncurses and most of the current curses implementations
Color support in curses is somewhat different because the color of each character is not defined independently of its background
So you have to define both the foreground and background colors, the so-called color pairs

bool has_colors(void);如果支持颜色,has_colors就会返回真int start_color(void01234567 白色
Initialize color pairs
int init_pair(shortshortshort background);用于更改一个彩色对的定义彩色对是Curses的一个概念,它用一个整型数值去标志一对前景/背景彩色pair_number:彩色对数值,其范围从1到 COLOR_PAIRS-1f:指定前景彩色b:指定背景彩色int COLOR_PAIR(int pair_number);int pair_content(shortshortshort *background);
Initializing the color process
定义颜色对1,使其背景色为绿色而前景色为红色init_pair(1, COLOR_RED, COLOR_GREEN);使用COLOR_PAIR将这个颜色作为一个属性来进行访问wattron(window_ptr, COLOR_PAIR(1));将屏幕设置为绿色的背景色以及红色的前景色说明因为COLOR_PAIR是一个属性,所以可以将其与其他的属性进行组合在PC上,经常可以通过使用位或操作符组合COLOR_PAIR属性与A_BOLD属性来获得屏幕的亮度wattron(window_ptr, COLOR_PAIR(1) | A_BOLD);
Redefining colors
在初始化颜色的时候改变某个颜色的RGB70000); 参数1 : 颜色名称参数234 : 分别为R(red),G(green),B(blue)的数值最小值:0最大值:1000如果显示终端无法改变颜色设置,函数将返回ERRcan_change_color()监测终端是否可以支持颜色改变
Pad

Background
Expect to get a larger logical screen than the actual physical screen, and display only part of the logical screen at a time
Window defects
All forms must be no larger than the physical screen
Pad function
Operation is not suitable for logical screen information of normal forms
Pad structure similar to window structure
All functions that can be used to output to a form can also be used for pad
But the pad has its own special create and refresh routines

*newpad(intint columns);返回一个指向WINDOW的指针,与newwin函数相同删除pad使用delwin函数,与窗体相同pad并没有限定一个特定的屏幕位置,必须指定希望pad出现在屏幕上的区域int*pad_ptrintintintintintint screen_col_max);创建一个pad区域,由(pad_row, pad_column)开始所定义的区域为(screen_row_min, screen_col_min)到(screen_row_max, screen_col_max)
Visual Menu Interface Design
init_ Curses   ()  function INITSCR  initialize pointer then start_color  to display color curs_set   (0 )  shield off physical pointer noecho   ()  terminates the input on the keyboard that appears on the screen   
init_cures(){    initscr();    start_color();    init_pair(1,COLOR_WHITE,COLOR_BLUE);    init_pair(2,COLOR_BLUE,COLOR_WHITE);    init_pair(3,COLOR_RED,COLOR_WHITE);    curs_set(0);    noecho();    keypad(stdscr,TRUE);}
voidDraw_menubar (WINDOW *menubar){wbkgd (menubar,Color_pair (2));waddstr (menubar,"Menu1");Wattron (menubar,Color_pair (3));waddstr (MenuBar,"(F1)");Wattroff (menubar,Color_pair (3));wmove (menubar,0,);waddstr (menubar,"Menu2");Wattron (menubar,Color_pair (3));waddstr (MenuBar,"(F2)");Wattroff (menubar,Color_pair (3));}

Draw_menubar () function description
Define a menu bar that appears at the top of the screen
is actually a subform of the Stdscr form, with only one row of the subform
The program will point to the subform's pointer as its argument
First change its background color, then define the menu text
Use WADDSTR to define the text of a menu
Wattron call another different color pair (ordinal 3) to replace the default color pair (ordinal 2)
Color pair 2nd is set to the default color pair at the very beginning by wbkgd
The Wattroff function allows us to switch to the default color pair state

WINDOW **draw_menu (intStart_col) {intI    WINDOW **items; items= (WINDOW *)malloc(9*sizeof(WINDOW *)); items[0]=newwin (Ten, +,1, Start_col); WBKGD (items[0],color_pair (2)); Box (items[0],acs_vline,acs_hline); items[1]=subwin (items[0],1, -,2, start_col+1); items[2]=subwin (items[0],1, -,3, start_col+1); items[3]=subwin (items[0],1, -,4, start_col+1); items[4]=subwin (items[0],1, -,5, start_col+1); items[5]=subwin (items[0],1, -,6, start_col+1); items[6]=subwin (items[0],1, -,7, start_col+1); items[7]=subwin (items[0],1, -,8, start_col+1); items[8]=subwin (items[0],1, -,9, start_col+1);

Draw_menu () function description
Displays the menu displayed when the F1 or F2 key is pressed
Defines a white background form with the same color as the menu bar on a blue background
The new window should not be overwritten by a word that appears on the background color, it should stay there until the menu is closed
Therefore, the menu form cannot be defined as a STDSCR subform
Form Items[0] is defined with the Newwin function, and the other 8 forms are subforms that are defined as items[0] forms
Items[0] is used to draw a border around the menu, and other forms are used to display the selected cells in the menu
Again, they won't overwrite the border on the menu.
The third sentence in the function makes the first cell background color different from the rest of the menu, because the first cell is selected (the selected and unchecked States) after the menu is bounced.

void delete_menu(WINDOW **items,intcount){    int i;    for (i=0;i<count;i++)        delwin(items[i]);    free(items);}
intScroll_menu (WINDOW **items,int Count,intMenu_start_col) {intKeyintSelected=0; while(1) {Key=getch ();if(Key==key_down | | key==key_up) {WBKGD (items[selected+1],color_pair (2)); Wnoutrefresh (items[selected+1]);if(Key==key_down) {selected= (selected+1) %Count; }Else{selected= (selected+Count-1) %Count; }
WBKGD (items[selected+1],color_pair (1)); Wnoutrefresh (items[selected+1]);        DoUpdate (); }Else if(Key==key_left | | key==key_right) {Delete_menu (items,Count+1);  Touchwin (STDSCR);  Refresh (); Items=draw_menu ( --MENU_START_COL);returnScroll_menu (Items,8, --MENU_START_COL); }Else if(Key==escape) {return-1; }Else if(Key==enter) {returnSelected }    }}

Scroll_menu () function Description
allows you to move up or down on a menu selection
Read key values on the keyboard by Getch
If you press the UP or DOWN ARROW keys, the previous item of the menu selection or the next item is selected
If the left or right arrow keys, The current menu closes and the other menu opens
If you press ENTER, the selected cell value is returned
If the ESC key is pressed, the menu will be closed and no selection
Getch can read the key value from the keyboard
This is because the program is starting to use keypad ( Stdscr,true)
assigns the return value to an int variable instead of a char variable, because the int variable can represent a value larger than the char type

int main(){    int key;    *menubar,*messagebar;    init_curses();    bkgd(COLOR_PAIR(1));    menubar=subwin(stdscr,1,80,0,0);    messagebar=subwin(stdscr,1,79,23,1);    draw_menubar(menubar);    move(2,1);    printw("Press F1 or F2 to open the menus. ");    printw("ESC quits.");    refresh();
  Do{intSelected_item; WINDOW**Menu_items;        Key=getch ();        Werase (Messagebar); Wrefresh (Messagebar);if(Key==key_f (1) {Menu_items=draw_menu (0); Selected_item=scroll_menu (Menu_items,8,0); Delete_menu (Menu_items,9);if(selected_item<0) WPRINTW (Messagebar,"You haven ' t selected any item.");ElseWPRINTW (Messagebar,"You have selected menu item %d.", selected_item+1);
touchwin (STDSCR); refresh  (); } else  if  (Key==key_f ( 2 ) {menu_items=draw_menu (20 ); Selected_item=scroll_menu (Menu_items,8 , 20 ); Delete_menu (Menu_items,9 ); if  (Selected_item<0 ) wprintw (Messagebar, "you haven ' t Selected any item. "); else  wprintw (Messagebar,  "You have selected menu Item%d. ", Selected_item+1 ); Touchwin (STDSCR); refresh  (); }} while  (key!=escape); 

Linux-Visual Menu interface design

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.