menu_demo.pp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. uses
  2. ncurses,menu;
  3. const
  4. choices : array[0..5] of PAnsiChar = (
  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 : PAnsiChar;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, true);
  47. init_pair(1, COLOR_RED, COLOR_BLACK);
  48. { Create items }
  49. n_choices := high(choices);
  50. getmem(my_items,(n_choices+1)*sizeof(pitem));
  51. my_items[n_choices]:=nil;
  52. for i:=0 to n_choices-1 do
  53. my_items[i] := new_item(choices[i], choices[i]);
  54. { Create menu }
  55. my_menu := new_menu(ppitem(my_items));
  56. { Create the window to be associated with the menu }
  57. my_menu_win := newwin(10, 40, 4, 4);
  58. keypad(my_menu_win, true);
  59. { Set main window and sub window }
  60. set_menu_win(my_menu, my_menu_win);
  61. set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
  62. { Set menu mark to the string ' * ' }
  63. set_menu_mark(my_menu, ' * ');
  64. { Print a border around the main window and print a title }
  65. box(my_menu_win, 0, 0);
  66. print_in_middle(my_menu_win, 1, 0, 40, 'My Menu', COLOR_PAIR(1));
  67. mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
  68. mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
  69. mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
  70. mvprintw(LINES - 2, 0, 'F1 to exit');
  71. refresh();
  72. { Post the menu }
  73. post_menu(my_menu);
  74. wrefresh(my_menu_win);
  75. c:=wgetch(my_menu_win);
  76. while(c<> KEY_F(1)) do
  77. begin
  78. case c of
  79. KEY_DOWN:
  80. menu_driver(my_menu, REQ_DOWN_ITEM);
  81. KEY_UP:
  82. menu_driver(my_menu, REQ_UP_ITEM);
  83. end;
  84. wrefresh(my_menu_win);
  85. c:=wgetch(my_menu_win);
  86. end;
  87. { Unpost and free all the memory taken up }
  88. unpost_menu(my_menu);
  89. free_menu(my_menu);
  90. for i:=0 to n_choices-1 do
  91. free_item(my_items[i]);
  92. endwin();
  93. end.