menu_demo.pp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. uses
  2. ncurses,menu;
  3. const
  4. choices : array[0..5] of pchar = (
  5. 'Choice 1',
  6. 'Choice 2',
  7. 'Choice 3',
  8. 'Choice 4',
  9. 'Exit',
  10. nil
  11. );
  12. procedure print_in_middle(win : PWINDOW;starty,startx,width : longint;_string : pchar;color : chtype);
  13. var
  14. length,x,y : longint;
  15. temp : single;
  16. begin
  17. if win=nil then
  18. win:=stdscr;
  19. getyx(win, y, x);
  20. if startx <> 0 then
  21. x := startx;
  22. if starty <> 0 then
  23. y := starty;
  24. if width=0 then
  25. width := 80;
  26. length := strlen(_string);
  27. temp := (width - length)/ 2;
  28. x := startx + round(temp);
  29. wattron(win, color);
  30. mvwprintw(win, y, x, '%s', [_string]);
  31. wattroff(win, color);
  32. refresh;
  33. end;
  34. var
  35. my_items : ppitem;
  36. c : longint;
  37. my_menu : pmenu;
  38. my_menu_win : pwindow;
  39. i,n_choices : longint;
  40. begin
  41. { Initialize curses }
  42. initscr;
  43. start_color;
  44. cbreak;
  45. noecho;
  46. keypad(stdscr, 1);
  47. init_pair(1, COLOR_RED, COLOR_BLACK);
  48. { Create items }
  49. n_choices := high(choices);
  50. getmem(my_items,n_choices*sizeof(pitem));
  51. for i:=0 to n_choices-1 do
  52. my_items[i] := new_item(choices[i], choices[i]);
  53. { Create menu }
  54. my_menu := new_menu(ppitem(my_items));
  55. { Create the window to be associated with the menu }
  56. my_menu_win := newwin(10, 40, 4, 4);
  57. keypad(my_menu_win, 1);
  58. { Set main window and sub window }
  59. set_menu_win(my_menu, my_menu_win);
  60. set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
  61. { Set menu mark to the string ' * ' }
  62. set_menu_mark(my_menu, ' * ');
  63. { Print a border around the main window and print a title }
  64. box(my_menu_win, 0, 0);
  65. print_in_middle(my_menu_win, 1, 0, 40, 'My Menu', COLOR_PAIR(1));
  66. mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
  67. mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
  68. mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
  69. mvprintw(LINES - 2, 0, 'F1 to exit',[]);
  70. refresh();
  71. { Post the menu }
  72. post_menu(my_menu);
  73. wrefresh(my_menu_win);
  74. c:=wgetch(my_menu_win);
  75. while(c<> KEY_F(1)) do
  76. begin
  77. case c of
  78. KEY_DOWN:
  79. menu_driver(my_menu, REQ_DOWN_ITEM);
  80. KEY_UP:
  81. menu_driver(my_menu, REQ_UP_ITEM);
  82. end;
  83. wrefresh(my_menu_win);
  84. c:=wgetch(my_menu_win);
  85. end;
  86. { Unpost and free all the memory taken up }
  87. unpost_menu(my_menu);
  88. free_menu(my_menu);
  89. for i:=0 to n_choices-1 do
  90. free_item(my_items[i]);
  91. endwin();
  92. end.