t2menu.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. {$MODE OBJFPC}
  2. program Menu_Example;
  3. uses
  4. ncurses, menu, panel, sysutils;
  5. function st_middle(scrlen, itemlen: Smallint): Smallint; inline;
  6. begin
  7. st_middle := (scrlen - itemlen) div 2;
  8. end;
  9. procedure print_in_middle(win: PWINDOW; starty, startx: Smallint;
  10. width: Longint; pair: Smallint;
  11. const fmt: AnsiString; args: array of const);
  12. var
  13. tstr: AnsiString;
  14. my, mx: Smallint;
  15. begin
  16. FmtStr(tstr, fmt, args);
  17. getmaxyx(win, my, mx);
  18. mx -= startx;
  19. if (width > length(tstr)) OR (width < 2) then
  20. width := length(tstr);
  21. if width > mx then
  22. width := mx;
  23. wattron(win,COLOR_PAIR(pair));
  24. mvwaddnstr(win,starty,startx + st_middle(mx,width),PChar(tstr),width);
  25. wattroff(win,COLOR_PAIR(pair));
  26. end;
  27. type
  28. PMinfo = ^TMinfo;
  29. TMinfo = record
  30. n, d: PChar;
  31. end;
  32. type
  33. TSubmenu = class
  34. private
  35. _win: PWINDOW;
  36. _pan: PPANEL;
  37. _items: ppITEM;
  38. _exit, _sitem: pITEM;
  39. _menu: pMENU;
  40. public
  41. function doevent: chtype;
  42. constructor create(szy,szx,nch: Smallint; choices: PMinfo;
  43. pair: Smallint;const name: AnsiString);
  44. destructor destroy; override;
  45. property menu: pMENU read _menu;
  46. property items: ppITEM read _items;
  47. property sitem: pITEM read _sitem write _sitem ;
  48. property win: PWINDOW read _win;
  49. property pan: PPANEL read _pan;
  50. end;
  51. function TSubmenu.doevent: chtype;
  52. function doenter(var ch: chtype): Boolean;
  53. begin
  54. if current_item(_menu) = _exit then
  55. begin
  56. doenter := false;
  57. ch := -1
  58. end
  59. else
  60. if current_item(_menu) = sitem then
  61. begin
  62. doenter := false;
  63. ch := 10
  64. end
  65. else
  66. doenter := true;
  67. end;
  68. var
  69. ch: chtype = 0;
  70. doiter: Boolean = true;
  71. begin
  72. while doiter do
  73. begin
  74. ch := wgetch(_win);
  75. case ch of
  76. KEY_DOWN: menu_driver(_menu, REQ_DOWN_ITEM);
  77. KEY_UP: menu_driver(_menu, REQ_UP_ITEM);
  78. KEY_LEFT: menu_driver(_menu, REQ_LEFT_ITEM);
  79. KEY_RIGHT: menu_driver(_menu, REQ_RIGHT_ITEM);
  80. KEY_NPAGE: menu_driver(_menu, REQ_SCR_DPAGE);
  81. KEY_PPAGE: menu_driver(_menu, REQ_SCR_UPAGE);
  82. chtype(' '): menu_driver(_menu, REQ_TOGGLE_ITEM);
  83. 10: doiter := doenter(ch); (* Enter *)
  84. else
  85. if menu_driver(_menu, ch) <> E_OK then
  86. begin
  87. doiter := false;
  88. if (ch <> chtype('q')) AND (ch <> KEY_F(10)) then
  89. ch := -1; (* Close menu *)
  90. end
  91. else
  92. if (ch = KEY_MOUSE) then
  93. doiter := doenter(ch);
  94. end;
  95. end;
  96. update_panels();
  97. doupdate();
  98. doevent := ch;
  99. end;
  100. constructor TSubmenu.create(szy,szx,nch: Smallint; choices: PMinfo;
  101. pair: Smallint;const name: AnsiString);
  102. var
  103. i: Longint = 0;
  104. begin
  105. GetMem(_items, (nch+1)*sizeof(pITEM));
  106. for i := 0 to nch - 1 do
  107. _items[i] := new_item(choices[i].n, choices[i].d);
  108. _items[nch] := nil;
  109. _exit := _items[i];
  110. sitem := nil;
  111. _menu := new_menu(_items);
  112. //scale_menu(_menu, @mrows, @mcols);
  113. _win := newwin(szy,szx,st_middle(LINES,szy),st_middle(COLS,szx));
  114. //_win := newwin(mrows + 2, mcols + 2, st_middle(LINES,mrows+2),st_middle(COLS,mcols+2));
  115. _pan := new_panel(_win);
  116. keypad(_win, TRUE);
  117. box(_win, ACS_VLINE, ACS_HLINE);
  118. wbkgd(_win, COLOR_PAIR(pair));
  119. set_menu_back(_menu, COLOR_PAIR(pair));
  120. print_in_middle(_win,0,0,szx-2,pair,name,[]);
  121. set_menu_win(_menu, _win);
  122. set_menu_sub(_menu, derwin(_win, szy-2, szx-2, 1, 1));
  123. //set_menu_sub(_menu, derwin(_win, mrows, mcols, 1, 1));
  124. set_menu_mark(_menu, '-');
  125. end;
  126. destructor TSubmenu.destroy;
  127. var
  128. i: Longint = 0;
  129. begin
  130. unpost_menu(_menu);
  131. free_menu(_menu);
  132. while _items[i] <> nil do
  133. begin
  134. free_item(_items[i]); Inc(i);
  135. end;
  136. FreeMem(_items, (i+1)*sizeof(pITEM));
  137. del_panel(_pan);
  138. delwin(_win);
  139. update_panels();
  140. doupdate();
  141. end;
  142. type
  143. Tmainptr = function: chtype;
  144. const
  145. EXIT_PROGRAM = KEY_MAX + 100;
  146. function confirm_menu: chtype;
  147. const
  148. choices: array[0..2] of TMinfo =
  149. (
  150. (n:' Yes ';d:nil),
  151. (n:'I dont know';d:nil),
  152. (n:' No ';d:nil)
  153. );
  154. var
  155. smenu: TSubmenu;
  156. begin
  157. smenu := TSubmenu.create(3, 41,3,choices,5,'Do you really want to quit?');
  158. menu_opts_off(smenu.menu, O_SHOWDESC);
  159. set_menu_format(smenu.menu, 1, 3);
  160. post_menu(smenu.menu);
  161. smenu.sitem := smenu.items[0];
  162. confirm_menu := smenu.doevent;
  163. if (confirm_menu = 10) OR (confirm_menu = chtype('q')) OR (confirm_menu = KEY_F(10)) then
  164. confirm_menu := EXIT_PROGRAM
  165. else
  166. confirm_menu := -1;
  167. smenu.destroy;
  168. end;
  169. (* Scrolling Menus example *)
  170. function scroll_menu: chtype;
  171. const
  172. choices: array[0..9] of TMinfo =
  173. (
  174. (n: '1_'; d: 'Choice'),
  175. (n: '2_'; d: 'Choice'),
  176. (n: '3_'; d: 'Choice'),
  177. (n: '4_'; d: 'Choice'),
  178. (n: '5_'; d: 'Choice'),
  179. (n: '6_'; d: 'Choice'),
  180. (n: '7_'; d: 'Choice'),
  181. (n: '8_'; d: 'Choice'),
  182. (n: '9_'; d: 'Choice'),
  183. (n: '..'; d: 'Close')
  184. );
  185. var
  186. smenu: TSubmenu;
  187. begin
  188. mvaddstr(LINES - 3, COLS - 30, '"PAGEUP" "PAGEDOWN" - scroll');
  189. refresh();
  190. smenu := TSubmenu.create(8, 13,10,choices,6,'Scrolling');
  191. set_menu_format(smenu.menu, 6, 1);
  192. post_menu(smenu.menu);
  193. scroll_menu := smenu.doevent;
  194. smenu.destroy;
  195. mvaddstr(LINES - 3, COLS - 30, ' ');
  196. refresh();
  197. end;
  198. (* Milt Columnar Menus Example *)
  199. function multicol_menu: chtype;
  200. const
  201. choices: array[0..24] of TMinfo =
  202. (
  203. (n:'1_';d:nil),(n:'2_';d:nil),(n:'3_';d:nil),(n:'4_';d:nil),(n:'5_';d:nil),
  204. (n:'6_';d:nil),(n:'7_';d:nil),(n:'8_';d:nil),(n:'9_';d:nil),(n:'10';d:nil),
  205. (n:'11';d:nil),(n:'12';d:nil),(n:'13';d:nil),(n:'14';d:nil),(n:'15';d:nil),
  206. (n:'16';d:nil),(n:'17';d:nil),(n:'18';d:nil),(n:'19';d:nil),(n:'20';d:nil),
  207. (n:'21';d:nil),(n:'22';d:nil),(n:'23';d:nil),(n:'24';d:nil),(n:'..';d:nil)
  208. );
  209. var
  210. smenu: TSubmenu;
  211. i: Longint;
  212. begin
  213. smenu := TSubmenu.create(7, 22,25,choices,5,'Multicol');
  214. (* Set menu option not to show the description *)
  215. menu_opts_off(smenu.menu, O_SHOWDESC);
  216. set_menu_format(smenu.menu, 5, 5);
  217. post_menu(smenu.menu);
  218. multicol_menu := smenu.doevent;
  219. smenu.destroy;
  220. end;
  221. (* Multi Valued Menus example *)
  222. function multival_menu: chtype;
  223. const
  224. choices: array[0..5] of TMinfo =
  225. (
  226. (n: '1_'; d: 'Choice'),
  227. (n: '2_'; d: 'Choice'),
  228. (n: '3_'; d: 'Choice'),
  229. (n: '4_'; d: 'Choice'),
  230. (n: '5_'; d: 'Choice'),
  231. (n: '..'; d: 'Close')
  232. );
  233. var
  234. smenu: TSubmenu;
  235. begin
  236. mvaddstr(LINES - 3, COLS - 30, '"SPACE" - toggle choice');
  237. refresh();
  238. smenu := TSubmenu.create(8, 13,6,choices,7,'Multival');
  239. menu_opts_off(smenu.menu, O_ONEVALUE);
  240. post_menu(smenu.menu);
  241. multival_menu := smenu.doevent;
  242. smenu.destroy;
  243. mvaddstr(LINES - 3, COLS - 30, ' ');
  244. refresh();
  245. end;
  246. const
  247. n_choices = 4;
  248. choices: array[0..3] of TMinfo =
  249. (
  250. (n: '1_'; d: 'Scrolling Menus'),
  251. (n: '2_'; d: 'Multi Columnar Menus'),
  252. (n: '3_'; d: 'Multi Valued Menus'),
  253. (n: '..'; d: 'Exit')
  254. );
  255. var
  256. main_menu_win: PWINDOW;
  257. main_menu_panel: PPANEL;
  258. function mgetch: chtype;
  259. begin
  260. mgetch := wgetch(main_menu_win);
  261. end;
  262. var
  263. my_bg: Smallint = COLOR_BLACK;
  264. main_items: ppITEM;
  265. cur_item: pITEM;
  266. main_menu: pMENU;
  267. ptr: Tmainptr = @mgetch;
  268. ch: chtype = -1;
  269. i: Longint;
  270. begin
  271. try
  272. (* Initialize curses *)
  273. initscr();
  274. noecho();
  275. cbreak();
  276. keypad(stdscr, TRUE);
  277. curs_set(0);
  278. clear();
  279. mousemask(ALL_MOUSE_EVENTS, nil);
  280. if has_colors() then
  281. begin
  282. start_color();
  283. if (use_default_colors() = OK) then
  284. my_bg := -1
  285. else
  286. my_bg := COLOR_BLACK;
  287. init_pair(1, COLOR_YELLOW, my_bg);
  288. init_pair(2, COLOR_RED, my_bg);
  289. init_pair(3, COLOR_MAGENTA, my_bg);
  290. init_pair(4, COLOR_CYAN, my_bg);
  291. init_pair(5, COLOR_WHITE, COLOR_RED);
  292. init_pair(6, COLOR_WHITE, COLOR_BLUE);
  293. init_pair(7, COLOR_WHITE, COLOR_GREEN);
  294. end;
  295. main_menu_win := newwin(8, 40, st_middle(LINES, 8) - 2, st_middle(COLS, 40) - 10);
  296. main_menu_panel := new_panel(main_menu_win);
  297. keypad(main_menu_win, TRUE);
  298. (* Create items *)
  299. GetMem(main_items, (n_choices+1)*sizeof(pITEM));
  300. for i := 0 to n_choices-1 do
  301. main_items[i] := new_item(choices[i].n, choices[i].d);
  302. main_items[n_choices] := nil;
  303. (* Set the user pointers *)
  304. set_item_userptr(main_items[0], @scroll_menu);
  305. set_item_userptr(main_items[1], @multicol_menu);
  306. set_item_userptr(main_items[2], @multival_menu);
  307. set_item_userptr(main_items[3], @confirm_menu);
  308. (* Crate menu *)
  309. main_menu := new_menu(main_items);
  310. (* Set main window and sub window *)
  311. set_menu_win(main_menu, main_menu_win);
  312. set_menu_sub(main_menu, derwin(main_menu_win, 4, 38, 3, 1));
  313. (* Set menu mark to the string "=>" *)
  314. set_menu_mark(main_menu, '=>');
  315. (* Print a border around the main window and print a title *)
  316. box(main_menu_win, 0, 0);
  317. wbkgd(main_menu_win, COLOR_PAIR(6));
  318. set_menu_back(main_menu, COLOR_PAIR(6));
  319. print_in_middle(main_menu_win, 1, 0, 40, COLOR_PAIR(6), 'Main Menu', []);
  320. mvwaddch(main_menu_win, 2, 0, ACS_LTEE);
  321. mvwhline(main_menu_win, 2, 1, ACS_HLINE, 38);
  322. mvwaddch(main_menu_win, 2, 39, ACS_RTEE);
  323. attron(COLOR_PAIR(4));
  324. mvaddstr(LINES - 1, COLS - 30, 'Press "F10" or "q" to exit ');
  325. attroff(COLOR_PAIR(4));
  326. refresh();
  327. (* Post the menu *)
  328. post_menu(main_menu);
  329. wrefresh(main_menu_win);
  330. while ch <> EXIT_PROGRAM do
  331. begin
  332. case ch of
  333. KEY_DOWN: menu_driver(main_menu, REQ_DOWN_ITEM);
  334. KEY_UP: menu_driver(main_menu, REQ_UP_ITEM);
  335. -1: ptr := @mgetch; (* Restore ptr *)
  336. 10: (* Enter *)
  337. begin
  338. cur_item := current_item(main_menu); (* get current item *)
  339. ptr := Tmainptr(item_userptr(cur_item)); (* set ptr to current item *)
  340. end;
  341. else
  342. (* Process mouse and others events *)
  343. if (menu_driver(main_menu, ch) = E_OK) AND (ch = KEY_MOUSE) then
  344. begin
  345. cur_item := current_item(main_menu);
  346. ptr := Tmainptr(item_userptr(cur_item));
  347. wrefresh(main_menu_win);
  348. end;
  349. end;
  350. ch := ptr(); (* Call ptr function *)
  351. if (ch = chtype('q')) OR (ch = KEY_F(10)) then
  352. ch := confirm_menu();
  353. end;
  354. finally
  355. unpost_menu(main_menu);
  356. free_menu(main_menu);
  357. for i := 0 to n_choices - 1 do
  358. free_item(main_items[i]);
  359. FreeMem(main_items, (n_choices+1)*sizeof(pITEM));
  360. del_panel(main_menu_panel);
  361. delwin(main_menu_win);
  362. curs_set(1);
  363. endwin();
  364. end;
  365. end.