Advertisement
Brandford

Ejemplo Ncurses

Jul 30th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.48 KB | None | 0 0
  1. #include <curses.h>
  2. #include <stdlib.h>
  3.  
  4. #define ENTER 10
  5. #define ESCAPE 27
  6. void init_curses()
  7. {
  8.         initscr();
  9.         start_color();
  10.         init_pair(1,COLOR_WHITE,COLOR_BLUE);
  11.         init_pair(2,COLOR_BLUE,COLOR_WHITE);
  12.         init_pair(3,COLOR_RED,COLOR_WHITE);
  13.         curs_set(0);
  14.         noecho();
  15.         keypad(stdscr,TRUE);
  16. }
  17. void draw_menubar(WINDOW *menubar)
  18. {
  19.         wbkgd(menubar,COLOR_PAIR(2));
  20.         waddstr(menubar,"Menu1");
  21.         wattron(menubar,COLOR_PAIR(3));
  22.         waddstr(menubar,"(F1)");
  23.         wattroff(menubar,COLOR_PAIR(3));
  24.         wmove(menubar,0,20);
  25.         waddstr(menubar,"Menu2");
  26.         wattron(menubar,COLOR_PAIR(3));
  27.         waddstr(menubar,"(F2)");
  28.         wattroff(menubar,COLOR_PAIR(3));
  29. }
  30. WINDOW **draw_menu(int start_col)
  31. {
  32.         int i;
  33.         WINDOW **items;
  34.         items=(WINDOW **)malloc(9*sizeof(WINDOW *));
  35.  
  36.         items[0]=newwin(10,19,1,start_col);
  37.         wbkgd(items[0],COLOR_PAIR(2));
  38.         box(items[0],ACS_VLINE,ACS_HLINE);
  39.         items[1]=subwin(items[0],1,17,2,start_col+1);
  40.         items[2]=subwin(items[0],1,17,3,start_col+1);
  41.         items[3]=subwin(items[0],1,17,4,start_col+1);
  42.         items[4]=subwin(items[0],1,17,5,start_col+1);
  43.         items[5]=subwin(items[0],1,17,6,start_col+1);
  44.         items[6]=subwin(items[0],1,17,7,start_col+1);
  45.         items[7]=subwin(items[0],1,17,8,start_col+1);
  46.         items[8]=subwin(items[0],1,17,9,start_col+1);
  47.         for (i=1;i<9;i++)
  48.                 wprintw(items[i],"Item%d",i);
  49.         wbkgd(items[1],COLOR_PAIR(1));
  50.         wrefresh(items[0]);
  51.         return items;
  52. }
  53. void delete_menu(WINDOW **items,int count)
  54. {
  55.         int i;
  56.         for (i=0;i<count;i++)
  57.                 delwin(items[i]);
  58.         free(items);
  59. }
  60. int scroll_menu(WINDOW **items,int count,int menu_start_col)
  61. {
  62.         int key;
  63.         int selected=0;
  64.         while (1) {
  65.                 key=getch();
  66.                 if (key==KEY_DOWN || key==KEY_UP) {
  67.                         wbkgd(items[selected+1],COLOR_PAIR(2));
  68.                         wnoutrefresh(items[selected+1]);
  69.                         if (key==KEY_DOWN) {
  70.                                 selected=(selected+1) % count;
  71.                         } else {
  72.                                 selected=(selected+count-1) % count;
  73.                         }
  74.                         wbkgd(items[selected+1],COLOR_PAIR(1));
  75.                         wnoutrefresh(items[selected+1]);
  76.                         doupdate();
  77.                 } else if (key==KEY_LEFT || key==KEY_RIGHT) {
  78.                         delete_menu(items,count+1);
  79.                         touchwin(stdscr);
  80.                         refresh();
  81.                         items=draw_menu(20-menu_start_col);
  82.                         return scroll_menu(items,8,20-menu_start_col);
  83.                 } else if (key==ESCAPE) {
  84.                         return -1;
  85.                 } else if (key==ENTER) {
  86.                         return selected;
  87.                 }
  88.         }
  89. }
  90. int main()
  91. {
  92.     int key;
  93.     WINDOW *menubar,*messagebar;
  94.    
  95.     init_curses();
  96.    
  97.     bkgd(COLOR_PAIR(1));
  98.     menubar=subwin(stdscr,1,80,0,0);
  99.     messagebar=subwin(stdscr,1,79,23,1);
  100.     draw_menubar(menubar);
  101.     move(2,1);
  102.     printw("Press F1 or F2 to open the menus. ");
  103.     printw("ESC quits.");
  104.     refresh();
  105.  
  106.     do {
  107.         int selected_item;
  108.         WINDOW **menu_items;
  109.         key=getch();
  110.         werase(messagebar);
  111.         wrefresh(messagebar);
  112.         if (key==KEY_F(1)) {
  113.             menu_items=draw_menu(0);
  114.             selected_item=scroll_menu(menu_items,8,0);
  115.             delete_menu(menu_items,9);
  116.             if (selected_item<0)
  117.                 wprintw(messagebar,"You haven't selected any item.");
  118.             else
  119.                 wprintw(messagebar,
  120.                   "You have selected menu item %d.",selected_item+1);
  121.             touchwin(stdscr);
  122.             refresh();
  123.         } else if (key==KEY_F(2)) {
  124.             menu_items=draw_menu(20);
  125.             selected_item=scroll_menu(menu_items,8,20);
  126.             delete_menu(menu_items,9);
  127.             if (selected_item<0)
  128.                 wprintw(messagebar,"You haven't selected any item.");
  129.             else
  130.                 wprintw(messagebar,
  131.                   "You have selected menu item %d.",selected_item+1);
  132.             touchwin(stdscr);
  133.             refresh();
  134.         }
  135.     } while (key!=ESCAPE);
  136.    
  137.     delwin(menubar);
  138.     delwin(messagebar);
  139.     endwin();
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement