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