您的位置:首页 > 大数据 > 人工智能

October 25th Thursday (十月 二十五日 木曜日)

2007-11-30 20:58 302 查看
 The raw() and noraw() routines place the terminal into or out of the raw mode.  Raw mode is similar to
cbreak mode, in that characters typed are immediatedly passed through to the user program.  The differences
are that in raw mode, the interrupt, quit, suspend, and flow control characters are all passed through
uninterpreted, instead of generating a signal.  The behavior of the BREAK key depends on other bits in
the tty driver that are not set by "curses".

  The def_prog_mode() saves the information of a tty.  The reset_prog_mode() restores that information.  They
are usually used to leave temporary Curses mode.

  The getyx() is a macro not a function.  The getparyx() is used to get the coordination of a sub-window
related to a main window.  The getbegyx() and getmaxyx() respectively is used to get the beginning
coordination and the end coordination.

  The scr_dump() save the stdscr into a file, to restore stdscr you can use scr_restore().  The getwin() can
save a window into a file, to extract the window from the saved file, invoking putwine().

  There a segment of codes to resize a panel.

old_win = panel_window(stack_top);
temp_win=newwin(newh, neww, newy, newx);
replace_panel(stack_top, temp_win)
win_show(temp, top->label, top->label_color);
delwin(old_win);

  How to create a menu?  There are two ways.  One, 1) creating it for yourself, another is to 2) use menu
extension library.

1)
#include <curses.h>
#include <stdio.h>

#define WIDTH 30
#define HEIGHT 10

int startx = 0;
int starty = 0;

char *choices[] = { "Choice1", "Choice2", "Choice3", "Choice4", "Exit"};

int n_choices = sizeof(choices) / sizeof(char*);

void print_menu(WINDOW *menu_win, int highlight);
void report_choice(int mouse_x, int mouse_y, int *p_choice);

int main(){
  int c, choice = 0;
  WINDOW *menu_win;
  MEVENT event;
  int highlight = 1;

  initscr();
  clear();
  noecho();
  cbreak();

  startx = (80 - WIDTH)/2;
  starty = (24 - HEIGHT)/2;

  attron(A_REVERSE);
  mvprintw(23, 1, "Click on Exit to quit");
  refresh();
  attroff(A_REVERSE);

  menu_win = newwin(HEIGHT, WIDTH, starty, startx);
  keypad(menu_win, TRUE);
  mvprintw(0,0,"User arrow keys to go up and down, Press enter to select a choice");
  refresh();
  print_menu(menu_win, 1);

  mousemask(ALL_MOUSE_EVENTS, NULL);

  while(1){
    c = wgetch(menu_win);
    switch(c){
      case KEY_UP:
      if (highlight == 1)
        highlight = n_choices;
          else
          highlight--;
          break;
      case KEY_DOWN:
      if (highlight == n_choices)
        highlight = 1;
          else
          highlight++;
          break;
      case 10:
      choice = highlight;
          break;
      case KEY_MOUSE:
       if (getmouse(&event) == OK){
          if (event.bstate & BUTTON1_PRESSED){
           report_choice(event.x + 1, event.y + 1, &choice);
            if (choice == -1)
goto end;
            mvprintw(22, 1, "Choice made is: %d String Chosen is [ %10s ]",
            choice, choices[choice-1]);
          refresh();
        }
      }
      print_menu(menu_win, choice);
      break;
    }
  }
end:
  mvprintw(23, 0, "%s", choices[choice - 1]);
  clrtoeol();
  refresh();
  endwin();
  return 0;

}

void print_menu(WINDOW *menu_win, int highlight){
int x, y, i;
x = 2, y = 2;
box(menu_win, 0,0);
for(i=0; i<n_choices; i++){
  if (highlight == i + 1){
wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices[i]);
wattroff(menu_win, A_REVERSE);
  }
  else
mvwprintw(menu_win, y, x, "%s", choices[i]);
  ++y;
}
wrefresh(menu_win);
}

void report_choice(int mouse_x, int mouse_y, int *p_choice){
int i, j, choice;
i = startx + 2;
j = starty + 3;

for (choice = 0; choice < n_choices; choice++)
if(mouse_y == j + choice && mouse_x >= i && mouse_x <=i + strlen(choices[choice])){
if (choice == n_choices - 1)
*p_choice = -1;
else
*p_choice = choice + 1;
break;
}
}

2)
#include <curses.h>
#include <menu.h>
#include <string.h>

#define WIDTH 30
#define HEIGHT 10

#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
#define CTRLD 4

char *choices[] = { "Choice1", "Choice2", "Choice3", "Choice4", "Exit"};

void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color){
int length, x, y;
float temp;

if (win == NULL)
win = stdscr;
getyx(win, y, x);
if (startx != 0)
x = startx;
if (starty != 0)
y = starty;
if (width == 0)
width = 80;

length = strlen(string);
temp = (width - length)/2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}

int main(){
  ITEM **my_items;
  int c;
  MENU *my_menu;
  int n_choices, i;
  ITEM *cur_item;
  WINDOW *my_menu_win;

  initscr();
  clear();
  noecho();
  cbreak();
  keypad(stdscr, TRUE);
  init_pair(1, COLOR_RED, COLOR_BLACK);

  n_choices = ARRAY_SIZE(choices);
  my_items = (ITEM**)calloc(n_choices + 1, sizeof(ITEM*));

  for (i=0; i<n_choices; i++){
    my_items[i] = new_item(choices[i], choices[i]);
  }
  my_items[n_choices]=(ITEM*)NULL;

  my_menu=new_menu((ITEM**)my_items);
  menu_opts_off(my_menu, O_SHOWDESC);

  my_menu_win=newwin(10, 40, 4, 4);
  keypad(my_menu_win, TRUE);
  init_pair(1, COLOR_RED, COLOR_BLACK);
 
  set_menu_win(my_menu, my_menu_win);
  set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
  set_menu_format(my_menu, 5, 1);
  set_menu_mark(my_menu, "*");
 
  box(my_menu_win, 0, 0);
  print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
  mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
  mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
  mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
   
  mvprintw(LINES - 2, 0, "F12 to Exit");
  refresh();

  post_menu(my_menu);
  wrefresh(my_menu_win);

  while((c=wgetch(my_menu_win))!=KEY_F(12)){
    switch(c){
      case KEY_UP:
      menu_driver(my_menu, REQ_UP_ITEM);
          break;
      case KEY_DOWN:
      menu_driver(my_menu, REQ_DOWN_ITEM);
          break;
            case KEY_NPAGE:
      menu_driver(my_menu, REQ_SCR_DPAGE);
          break;
            case KEY_PPAGE:
      menu_driver(my_menu, REQ_SCR_UPAGE);
          break;
    }
    wrefresh(my_menu_win);
  }
  unpost_menu(my_menu);
  free_menu(my_menu);
  for(i=0;i<n_choices; i++)
    free_item(my_items[i]);
 
  endwin();
  return 0;

}

  The menu_opts() can set options to a menu, and can get the options from a menu.  The menu_opts_on()
and menu_opts_off() can switch on or off a menu's options.  Yes, set_menu_opts() can also set a menu
options.

  The menu_items() can get the menu items about a menu.  To check if a item is selected, you can use
item_value().  It return TRUE and the item is selected.  The item_count() tells you the count of menu
items.  The item_name() and item_description() tell you the name and the description of a menu item.

  The set_menu_fore() and set_menu_back() can change item's foreground and background when selected
or unselected.  To disable an item, we can use item_opts_off() with O_SELECTABLE option parameter. 
The set_menu_grey() just set items in a menu the disable attribute.

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息