-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcursdial.c
1583 lines (1365 loc) · 51.1 KB
/
cursdial.c
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vim:set cin ft=c sw=4 sts=4 ts=8 et ai cino=Ls\:0t0(0 : -*- mode:c;fill-column:80;tab-width:8;c-basic-offset:4;indent-tabs-mode:nil;c-file-style:"k&r" -*-*/
/* NetHack 3.6 cursdial.c */
/* Copyright (c) Karl Garrison, 2010. */
/* NetHack may be freely redistributed. See license for details. */
#include "curses.h"
#include "hack.h"
#include "wincurs.h"
#include "cursdial.h"
#include "func_tab.h"
#include <ctype.h>
#if defined(FILENAME_CMP) && !defined(strcasecmp)
#define strcasecmp FILENAME_CMP
#endif
#if defined(STRNCMPI) && !defined(strncasecmp)
#define strncasecmp strncmpi
#endif
/* defined in sys/<foo>/<foo>tty.c or cursmain.c as last resort;
set up by curses_init_nhwindows() */
extern char erase_char, kill_char;
/*
* Note:
*
* Prototypes need to use the widened/unwidened type macros (CHAR_P, &c)
* in order to match fields of the window_procs struct (see winprocs.h).
* But for a standard-conforming compiler, we'll end up with the widened
* types necessary to match the mixed prototype/old-style function
* definition environment as used by nethack's core. Prototype
int func(CHAR_P);
* becomes
int func(int);
* after expansion, which matches the definition
int func(arg) char arg; { ... }
* according to the rules of the C standard. But the use of new-style
* definitions
int func(char arg) { ... }
* by the curses interface turns that into a conflict. No widening takes
* place so it ought to be 'int func(char);' instead. Unfortunately that
* would be incompatible for functions assigned to window_procs.
*
* So, the code here (also cursmain.c and cursinvt.c) is mis-using the
* widening macros for variable types
int func(CHAR_P arg) { ... }
* (no doubt modelling it after the C++ code in win/Qt where the option
* to switch the applicable definitions to old-style isn't available).
* Other alternatives aren't significantly better so just live with it.
* [Redoing the windowing interface to avoid narrow arguments would be
* better since that would fix Qt's usage too.]
*/
/* Dialog windows for curses interface */
/* Private declarations */
typedef struct nhmi {
winid wid; /* NetHack window id */
int glyph; /* Menu glyphs */
anything identifier; /* Value returned if item selected */
CHAR_P accelerator; /* Character used to select item from menu */
CHAR_P group_accel; /* Group accelerator for menu item, if any */
int attr; /* Text attributes for item */
const char *str; /* Text of menu item */
BOOLEAN_P presel; /* Whether menu item should be preselected */
boolean selected; /* Whether item is currently selected */
int page_num; /* Display page number for entry */
int line_num; /* Line number on page where entry begins */
int num_lines; /* Number of lines entry uses on page */
int count; /* Count for selected item */
struct nhmi *prev_item; /* Pointer to previous entry */
struct nhmi *next_item; /* Pointer to next entry */
} nhmenu_item;
typedef struct nhm {
winid wid; /* NetHack window id */
const char *prompt; /* Menu prompt text */
nhmenu_item *entries; /* Menu entries */
int num_entries; /* Number of menu entries */
int num_pages; /* Number of display pages for entry */
int height; /* Window height of menu */
int width; /* Window width of menu */
boolean reuse_accels; /* Non-unique accelerators per page */
boolean bottom_heavy; /* display multi-page menu starting at end */
struct nhm *prev_menu; /* Pointer to previous entry */
struct nhm *next_menu; /* Pointer to next entry */
} nhmenu;
typedef enum menu_op_type {
SELECT,
DESELECT,
INVERT
} menu_op;
static nhmenu_item *curs_new_menu_item(winid, const char *);
static void curs_pad_menu(nhmenu *, boolean);
static nhmenu *get_menu(winid wid);
static char menu_get_accel(boolean first);
static void menu_determine_pages(nhmenu *menu);
static boolean menu_is_multipage(nhmenu *menu, int width, int height);
static void menu_win_size(nhmenu *menu);
static void menu_display_page(nhmenu *menu, WINDOW * win, int page_num,
char *);
static int menu_get_selections(WINDOW * win, nhmenu *menu, int how);
static void menu_select_deselect(WINDOW * win, nhmenu_item *item,
menu_op operation, int);
static int menu_operation(WINDOW * win, nhmenu *menu, menu_op operation,
int page_num);
static void menu_clear_selections(nhmenu *menu);
static int menu_max_height(void);
static nhmenu *nhmenus = NULL; /* NetHack menu array */
/* maximum number of selector entries per page; if '$' is present
it isn't counted toward maximum, so actual max per page is 53 */
#define MAX_ACCEL_PER_PAGE 52 /* 'a'..'z' + 'A'..'Z' */
/* TODO? limit per page should be ignored for perm_invent, which might
have some '#' overflow entries and isn't used to select items */
/* Get a line of text from the player, such as asking for a character name
or a wish. Note: EDIT_GETLIN not supported for popup prompting. */
void
curses_line_input_dialog(const char *prompt, char *answer, int buffer)
{
int map_height, map_width, maxwidth, remaining_buf, winx, winy, count;
WINDOW *askwin, *bwin;
char *tmpstr;
int prompt_width, prompt_height = 1, height = prompt_height;
char input[BUFSZ];
/* if messages were being suppressed for the remainder of the turn,
re-activate them now that input is being requested */
curses_got_input();
if (buffer > (int) sizeof input)
buffer = (int) sizeof input;
/* +1: space between prompt and answer; buffer already accounts for \0 */
prompt_width = (int) strlen(prompt) + 1 + buffer;
maxwidth = term_cols - 2;
if (iflags.window_inited) {
if (!iflags.wc_popup_dialog) {
curses_message_win_getline(prompt, answer, buffer);
return;
}
curses_get_window_size(MAP_WIN, &map_height, &map_width);
if ((prompt_width + 2) > map_width)
maxwidth = map_width - 2;
}
if (prompt_width > maxwidth) {
prompt_height = curses_num_lines(prompt, maxwidth);
height = prompt_height;
prompt_width = maxwidth;
tmpstr = curses_break_str(prompt, maxwidth, prompt_height);
remaining_buf = buffer - ((int) strlen(tmpstr) - 1);
if (remaining_buf > 0) {
height += (remaining_buf / prompt_width);
if ((remaining_buf % prompt_width) > 0) {
height++;
}
}
free(tmpstr);
}
bwin = curses_create_window(prompt_width, height,
iflags.window_inited ? UP : CENTER);
wrefresh(bwin);
getbegyx(bwin, winy, winx);
askwin = newwin(height, prompt_width, winy + 1, winx + 1);
for (count = 0; count < prompt_height; count++) {
tmpstr = curses_break_str(prompt, maxwidth, count + 1);
mvwaddstr(askwin, count, 0, tmpstr);
if (count == prompt_height - 1) { /* Last line */
if ((int) strlen(tmpstr) < maxwidth)
waddch(askwin, ' ');
else
wmove(askwin, count + 1, 0);
}
free(tmpstr);
}
echo();
curs_set(1);
wgetnstr(askwin, input, buffer - 1);
curs_set(0);
Strcpy(answer, input);
werase(bwin);
delwin(bwin);
curses_destroy_win(askwin);
noecho();
}
/* Get a single character response from the player, such as a y/n prompt */
int
curses_character_input_dialog(const char *prompt, const char *choices,
CHAR_P def)
{
WINDOW *askwin = NULL;
#ifdef PDCURSES
WINDOW *message_window;
#endif
int answer, count, maxwidth, map_height, map_width;
char *linestr;
char askstr[BUFSZ + QBUFSZ];
char choicestr[QBUFSZ];
int prompt_width;
int prompt_height = 1;
boolean any_choice = FALSE;
boolean accept_count = FALSE;
/* if messages were being suppressed for the remainder of the turn,
re-activate them now that input is being requested */
curses_got_input();
if (invent || (moves > 1)) {
curses_get_window_size(MAP_WIN, &map_height, &map_width);
} else {
map_height = term_rows;
map_width = term_cols;
}
#ifdef PDCURSES
message_window = curses_get_nhwin(MESSAGE_WIN);
#endif
maxwidth = map_width - 2;
if (choices != NULL) {
for (count = 0; choices[count] != '\0'; count++) {
if (choices[count] == '#') { /* Accept a count */
accept_count = TRUE;
}
}
choicestr[0] = ' ';
choicestr[1] = '[';
for (count = 0; choices[count] != '\0'; count++) {
if (choices[count] == '\033') { /* Escape */
break;
}
choicestr[count + 2] = choices[count];
}
choicestr[count + 2] = ']';
if (((def >= 'A') && (def <= 'Z')) || ((def >= 'a') && (def <= 'z'))) {
choicestr[count + 3] = ' ';
choicestr[count + 4] = '(';
choicestr[count + 5] = def;
choicestr[count + 6] = ')';
choicestr[count + 7] = '\0';
} else { /* No usable default choice */
choicestr[count + 3] = '\0';
def = '\0'; /* Mark as no default */
}
strcpy(askstr, prompt);
strcat(askstr, choicestr);
} else {
strcpy(askstr, prompt);
any_choice = TRUE;
}
/* +1: room for a trailing space where the cursor will rest */
prompt_width = (int) strlen(askstr) + 1;
if ((prompt_width + 2) > maxwidth) {
prompt_height = curses_num_lines(askstr, maxwidth);
prompt_width = map_width - 2;
}
if (iflags.wc_popup_dialog /*|| curses_stupid_hack*/) {
askwin = curses_create_window(prompt_width, prompt_height, UP);
for (count = 0; count < prompt_height; count++) {
linestr = curses_break_str(askstr, maxwidth, count + 1);
mvwaddstr(askwin, count + 1, 1, linestr);
free(linestr);
}
wrefresh(askwin);
} else {
/* TODO: add SUPPRESS_HISTORY flag, then after getting a response,
append it and use put_msghistory() on combined prompt+answer */
custompline(OVERRIDE_MSGTYPE, "%s", askstr);
}
/*curses_stupid_hack = 0; */
curs_set(1);
while (1) {
#ifdef PDCURSES
answer = wgetch(message_window);
#else
answer = getch();
#endif
if (answer == ERR) {
answer = def;
break;
}
answer = curses_convert_keys(answer);
if (answer == KEY_ESC) {
if (choices == NULL) {
break;
}
answer = def;
for (count = 0; choices[count] != '\0'; count++) {
if (choices[count] == 'q') { /* q is preferred over n */
answer = 'q';
} else if ((choices[count] == 'n') && answer != 'q') {
answer = 'n';
}
}
break;
} else if ((answer == '\n') || (answer == '\r') || (answer == ' ')) {
if ((choices != NULL)
&& ((def != '\0') || !strchr(choices, answer))) {
answer = def;
}
break;
}
if (digit(answer)) {
if (accept_count) {
if (answer != '0') {
yn_number = curses_get_count(answer - '0');
touchwin(askwin);
refresh();
}
answer = '#';
break;
}
}
if (any_choice) {
break;
}
if (choices != NULL && answer != '\0' && index(choices, answer))
break;
}
curs_set(0);
if (iflags.wc_popup_dialog) {
/* Kludge to make prompt visible after window is dismissed
when inputting a number */
if (digit(answer)) {
custompline(OVERRIDE_MSGTYPE, "%s", askstr);
curs_set(1);
}
curses_destroy_win(askwin);
} else {
curses_clear_unhighlight_message_window();
}
return answer;
}
/* Return an extended command from the user */
int
curses_ext_cmd()
{
int count, letter, prompt_width, startx, starty, winx, winy;
int messageh, messagew, maxlen = BUFSZ - 1;
int ret = -1;
char cur_choice[BUFSZ];
int matches = 0;
WINDOW *extwin = NULL, *extwin2 = NULL;
if (iflags.extmenu) {
return extcmd_via_menu();
}
startx = 0;
starty = 0;
if (iflags.wc_popup_dialog) { /* Prompt in popup window */
int x0, y0, w, h; /* bounding coords of popup */
extwin2 = curses_create_window(25, 1, UP);
wrefresh(extwin2);
/* create window inside window to prevent overwriting of border */
getbegyx(extwin2, y0, x0);
getmaxyx(extwin2, h, w);
extwin = newwin(1, w - 2, y0 + 1, x0 + 1);
if (w - 4 < maxlen)
maxlen = w - 4;
nhUse(h); /* needed only to give getmaxyx three arguments */
} else {
curses_get_window_xy(MESSAGE_WIN, &winx, &winy);
curses_get_window_size(MESSAGE_WIN, &messageh, &messagew);
if (curses_window_has_border(MESSAGE_WIN)) {
winx++;
winy++;
}
winy += messageh - 1;
extwin = newwin(1, messagew - 2, winy, winx);
if (messagew - 4 < maxlen)
maxlen = messagew - 4;
custompline(OVERRIDE_MSGTYPE, "#");
}
cur_choice[0] = '\0';
while (1) {
wmove(extwin, starty, startx);
waddstr(extwin, "# ");
wmove(extwin, starty, startx + 2);
waddstr(extwin, cur_choice);
wmove(extwin, starty, (int) strlen(cur_choice) + startx + 2);
wclrtoeol(extwin);
/* if we have an autocomplete command, AND it matches uniquely */
if (matches == 1) {
curses_toggle_color_attr(extwin, NONE, A_UNDERLINE, ON);
wmove(extwin, starty, (int) strlen(cur_choice) + startx + 2);
wprintw(extwin, "%s",
extcmdlist[ret].ef_txt + (int) strlen(cur_choice));
curses_toggle_color_attr(extwin, NONE, A_UNDERLINE, OFF);
}
curs_set(1);
wrefresh(extwin);
letter = getch();
curs_set(0);
prompt_width = (int) strlen(cur_choice);
matches = 0;
if (letter == '\033' || letter == ERR) {
ret = -1;
break;
}
if (letter == '\r' || letter == '\n') {
if (ret == -1) {
for (count = 0; extcmdlist[count].ef_txt; count++) {
if (!strcasecmp(cur_choice, extcmdlist[count].ef_txt)) {
ret = count;
break;
}
}
}
break;
}
if (letter == '\177') /* DEL/Rubout */
letter = '\b';
if (letter == '\b' || letter == KEY_BACKSPACE
|| (erase_char && letter == (int) (uchar) erase_char)) {
if (prompt_width == 0) {
ret = -1;
break;
} else {
cur_choice[prompt_width - 1] = '\0';
letter = '*';
prompt_width--;
}
/* honor kill_char if it's ^U or similar, but not if it's '@' */
} else if (kill_char && letter == (int) (uchar) kill_char
&& (letter < ' ' || letter >= '\177')) { /*ASCII*/
cur_choice[0] = '\0';
letter = '*';
prompt_width = 0;
}
if (letter != '*' && prompt_width < maxlen) {
cur_choice[prompt_width] = letter;
cur_choice[prompt_width + 1] = '\0';
ret = -1;
}
for (count = 0; extcmdlist[count].ef_txt; count++) {
if (!wizard && (extcmdlist[count].flags & WIZMODECMD))
continue;
if (!(extcmdlist[count].flags & AUTOCOMPLETE))
continue;
if ((int) strlen(extcmdlist[count].ef_txt) > prompt_width) {
if (strncasecmp(cur_choice, extcmdlist[count].ef_txt,
prompt_width) == 0) {
if ((extcmdlist[count].ef_txt[prompt_width] ==
lowc(letter)) || letter == '*') {
if (matches == 0) {
ret = count;
}
matches++;
}
}
}
}
}
curses_destroy_win(extwin);
if (extwin2)
curses_destroy_win(extwin2);
return ret;
}
/* Initialize a menu from given NetHack winid */
void
curses_create_nhmenu(winid wid)
{
nhmenu *new_menu = NULL;
nhmenu *menuptr = nhmenus;
nhmenu_item *menu_item_ptr = NULL;
nhmenu_item *tmp_menu_item = NULL;
new_menu = get_menu(wid);
if (new_menu != NULL) {
/* Reuse existing menu, clearing out current entries */
menu_item_ptr = new_menu->entries;
if (menu_item_ptr != NULL) {
while (menu_item_ptr->next_item != NULL) {
tmp_menu_item = menu_item_ptr->next_item;
free((genericptr_t) menu_item_ptr->str);
free((genericptr_t) menu_item_ptr);
menu_item_ptr = tmp_menu_item;
}
free((genericptr_t) menu_item_ptr->str);
free((genericptr_t) menu_item_ptr); /* Last entry */
new_menu->entries = NULL;
}
if (new_menu->prompt != NULL) { /* Reusing existing menu */
free((genericptr_t) new_menu->prompt);
new_menu->prompt = NULL;
}
new_menu->num_pages = 0;
new_menu->height = 0;
new_menu->width = 0;
new_menu->reuse_accels = FALSE;
new_menu->bottom_heavy = FALSE;
return;
}
new_menu = (nhmenu *) alloc((signed) sizeof (nhmenu));
new_menu->wid = wid;
new_menu->prompt = NULL;
new_menu->entries = NULL;
new_menu->num_pages = 0;
new_menu->height = 0;
new_menu->width = 0;
new_menu->reuse_accels = FALSE;
new_menu->bottom_heavy = FALSE;
new_menu->next_menu = NULL;
if (nhmenus == NULL) { /* no menus in memory yet */
new_menu->prev_menu = NULL;
nhmenus = new_menu;
} else {
while (menuptr->next_menu != NULL) {
menuptr = menuptr->next_menu;
}
new_menu->prev_menu = menuptr;
menuptr->next_menu = new_menu;
}
}
static nhmenu_item *
curs_new_menu_item(winid wid, const char *str)
{
char *new_str;
nhmenu_item *new_item;
new_str = curses_copy_of(str);
curses_rtrim(new_str);
new_item = (nhmenu_item *) alloc((unsigned) sizeof (nhmenu_item));
new_item->wid = wid;
new_item->glyph = NO_GLYPH;
new_item->identifier = zeroany;
new_item->accelerator = '\0';
new_item->group_accel = '\0';
new_item->attr = 0;
new_item->str = new_str;
new_item->presel = FALSE;
new_item->selected = FALSE;
new_item->page_num = 0;
new_item->line_num = 0;
new_item->num_lines = 0;
new_item->count = -1;
new_item->next_item = NULL;
return new_item;
}
/* Add a menu item to the given menu window */
void
curses_add_nhmenu_item(winid wid, int glyph, const ANY_P *identifier,
CHAR_P accelerator, CHAR_P group_accel, int attr,
const char *str, BOOLEAN_P presel)
{
nhmenu_item *new_item, *current_items, *menu_item_ptr;
nhmenu *current_menu = get_menu(wid);
if (current_menu == NULL) {
impossible(
"curses_add_nhmenu_item: attempt to add item to nonexistent menu");
return;
}
if (str == NULL) {
return;
}
new_item = curs_new_menu_item(wid, str);
new_item->glyph = glyph;
new_item->identifier = *identifier;
new_item->accelerator = accelerator;
new_item->group_accel = group_accel;
new_item->attr = attr;
new_item->presel = presel;
current_items = current_menu->entries;
menu_item_ptr = current_items;
if (current_items == NULL) {
new_item->prev_item = NULL;
current_menu->entries = new_item;
} else {
while (menu_item_ptr->next_item != NULL) {
menu_item_ptr = menu_item_ptr->next_item;
}
new_item->prev_item = menu_item_ptr;
menu_item_ptr->next_item = new_item;
}
}
/* for menu->bottom_heavy -- insert enough blank lines at top of
first page to make the last page become a full one */
static void
curs_pad_menu(nhmenu *current_menu, boolean do_pad UNUSED)
{
nhmenu_item *menu_item_ptr;
int numpages = current_menu->num_pages;
/* caller has already called menu_win_size() */
menu_determine_pages(current_menu); /* sets 'menu->num_pages' */
numpages = current_menu->num_pages;
/* pad beginning of menu so that partial last page becomes full;
might be slightly less than full if any entries take multiple
lines and the padding would force those to span page boundary
and that gets prevented; so we re-count the number of pages
with every insertion instead of trying to calculate the number
of them to add */
do {
menu_item_ptr = curs_new_menu_item(current_menu->wid, "");
menu_item_ptr->next_item = current_menu->entries;
current_menu->entries->prev_item = menu_item_ptr;
current_menu->entries = menu_item_ptr;
current_menu->num_entries += 1;
menu_determine_pages(current_menu);
} while (current_menu->num_pages == numpages);
/* we inserted blank lines at beginning until final entry spilled
over to another page; take the most recent blank one back out */
current_menu->num_entries -= 1;
current_menu->entries = menu_item_ptr->next_item;
current_menu->entries->prev_item = (nhmenu_item *) 0;
free((genericptr_t) menu_item_ptr->str);
free((genericptr_t) menu_item_ptr);
/* reset page count; shouldn't need to re-count */
current_menu->num_pages = numpages;
return;
}
/* mark ^P message recall menu, for msg_window:full (FIFO), where we'll
start viewing on the last page so be able to see most recent immediately */
void
curs_menu_set_bottom_heavy(winid wid)
{
nhmenu *menu = get_menu(wid);
/*
* Called after end_menu + finalize_nhmenu,
* before select_menu + display_nhmenu.
*/
menu_win_size(menu); /* (normally not done until display_nhmenu) */
if (menu_is_multipage(menu, menu->width, menu->height)) {
menu->bottom_heavy = TRUE;
/* insert enough blank lines at top of first page to make the
last page become a full one */
curs_pad_menu(menu, TRUE);
}
return;
}
/* No more entries are to be added to menu, so details of the menu can be
finalized in memory */
void
curses_finalize_nhmenu(winid wid, const char *prompt)
{
int count = 0;
nhmenu_item *menu_item_ptr;
nhmenu *current_menu = get_menu(wid);
if (current_menu == NULL) {
impossible(
"curses_finalize_nhmenu: attempt to finalize nonexistent menu");
return;
}
for (menu_item_ptr = current_menu->entries;
menu_item_ptr != NULL; menu_item_ptr = menu_item_ptr->next_item)
count++;
current_menu->num_entries = count;
current_menu->prompt = curses_copy_of(prompt);
}
/* Display a nethack menu, and return a selection, if applicable */
int
curses_display_nhmenu(winid wid, int how, MENU_ITEM_P ** _selected)
{
nhmenu *current_menu = get_menu(wid);
nhmenu_item *menu_item_ptr;
int num_chosen, count;
WINDOW *win;
MENU_ITEM_P *selected = NULL;
*_selected = NULL;
if (current_menu == NULL) {
impossible(
"curses_display_nhmenu: attempt to display nonexistent menu");
return '\033';
}
menu_item_ptr = current_menu->entries;
if (menu_item_ptr == NULL) {
impossible("curses_display_nhmenu: attempt to display empty menu");
return '\033';
}
/* Reset items to unselected to clear out selections from previous
invocations of this menu, and preselect appropriate items */
while (menu_item_ptr != NULL) {
menu_item_ptr->selected = menu_item_ptr->presel;
menu_item_ptr = menu_item_ptr->next_item;
}
menu_win_size(current_menu);
menu_determine_pages(current_menu);
/* Display pre and post-game menus centered */
if ((moves <= 1 && !invent) || program_state.gameover) {
win = curses_create_window(current_menu->width,
current_menu->height, CENTER);
} else { /* Display during-game menus on the right out of the way */
win = curses_create_window(current_menu->width,
current_menu->height, RIGHT);
}
num_chosen = menu_get_selections(win, current_menu, how);
curses_destroy_win(win);
if (num_chosen > 0) {
selected = (MENU_ITEM_P *) alloc((unsigned)
(num_chosen * sizeof (MENU_ITEM_P)));
count = 0;
menu_item_ptr = current_menu->entries;
while (menu_item_ptr != NULL) {
if (menu_item_ptr->selected) {
if (count == num_chosen) {
impossible("curses_display_nhmenu: Selected items "
"exceeds expected number");
break;
}
selected[count].item = menu_item_ptr->identifier;
selected[count].count = menu_item_ptr->count;
count++;
}
menu_item_ptr = menu_item_ptr->next_item;
}
if (count != num_chosen) {
impossible(
"curses_display_nhmenu: Selected items less than expected number");
}
}
*_selected = selected;
return num_chosen;
}
boolean
curses_menu_exists(winid wid)
{
if (get_menu(wid) != NULL) {
return TRUE;
} else {
return FALSE;
}
}
/* Delete the menu associated with the given NetHack winid from memory */
void
curses_del_menu(winid wid, boolean del_wid_too)
{
nhmenu_item *tmp_menu_item;
nhmenu_item *menu_item_ptr;
nhmenu *tmpmenu;
nhmenu *current_menu = get_menu(wid);
if (current_menu == NULL) {
return;
}
menu_item_ptr = current_menu->entries;
/* First free entries associated with this menu from memory */
if (menu_item_ptr != NULL) {
while (menu_item_ptr->next_item != NULL) {
tmp_menu_item = menu_item_ptr->next_item;
free((genericptr_t) menu_item_ptr->str);
free(menu_item_ptr);
menu_item_ptr = tmp_menu_item;
}
free((genericptr_t) menu_item_ptr->str);
free(menu_item_ptr); /* Last entry */
current_menu->entries = NULL;
}
/* Now unlink the menu from the list and free it as well */
if ((tmpmenu = current_menu->prev_menu) != NULL) {
tmpmenu->next_menu = current_menu->next_menu;
} else {
nhmenus = current_menu->next_menu; /* New head node or NULL */
}
if ((tmpmenu = current_menu->next_menu) != NULL) {
tmpmenu->prev_menu = current_menu->prev_menu;
}
if (current_menu->prompt)
free((genericptr_t) current_menu->prompt);
free((genericptr_t) current_menu);
if (del_wid_too)
curses_del_wid(wid);
}
/* return a pointer to the menu associated with the given NetHack winid */
static nhmenu *
get_menu(winid wid)
{
nhmenu *menuptr = nhmenus;
while (menuptr != NULL) {
if (menuptr->wid == wid) {
return menuptr;
}
menuptr = menuptr->next_menu;
}
return NULL; /* Not found */
}
static char
menu_get_accel(boolean first)
{
static char next_letter;
char ret;
if (first) {
next_letter = 'a';
}
ret = next_letter;
if ((next_letter < 'z' && next_letter >= 'a')
|| (next_letter < 'Z' && next_letter >= 'A')) {
next_letter++;
} else if (next_letter == 'z') {
next_letter = 'A';
} else if (next_letter == 'Z') {
next_letter = 'a'; /* wrap to beginning */
}
return ret;
}
/* Determine if menu will require multiple pages to display */
static boolean
menu_is_multipage(nhmenu *menu, int width, int height)
{
int num_lines;
int curline = 0, accel_per_page = 0;
nhmenu_item *menu_item_ptr = menu->entries;
if (*menu->prompt) {
curline += curses_num_lines(menu->prompt, width) + 1;
}
while (menu_item_ptr != NULL) {
menu_item_ptr->line_num = curline;
if (menu_item_ptr->identifier.a_void == NULL) {
num_lines = curses_num_lines(menu_item_ptr->str, width);
} else {
if (menu_item_ptr->accelerator != GOLD_SYM) {
if (++accel_per_page > MAX_ACCEL_PER_PAGE)
break;
}
/* Add space for accelerator */
num_lines = curses_num_lines(menu_item_ptr->str, width - 4);
}
menu_item_ptr->num_lines = num_lines;
curline += num_lines;
menu_item_ptr = menu_item_ptr->next_item;
if (curline > height
|| (curline > height - 2 && height == menu_max_height())) {
break;
}
}
return (menu_item_ptr != NULL) ? TRUE : FALSE;
}
/* Determine which entries go on which page, and total number of pages */
static void
menu_determine_pages(nhmenu *menu)
{
int tmpline, num_lines, accel_per_page;
int curline = 0;
int page_num = 1;
nhmenu_item *menu_item_ptr = menu->entries;
int width = menu->width;
int height = menu->height;
int page_end = height;
if (*menu->prompt) {
curline += curses_num_lines(menu->prompt, width) + 1;
}
tmpline = curline;
if (menu_is_multipage(menu, width, height)) {
page_end -= 2; /* Room to display current page number */
}
accel_per_page = 0;
/* Determine what entries belong on which page */
for (menu_item_ptr = menu->entries; menu_item_ptr != NULL;
menu_item_ptr = menu_item_ptr->next_item) {
menu_item_ptr->page_num = page_num;
menu_item_ptr->line_num = curline;
if (menu_item_ptr->identifier.a_void == NULL) {
num_lines = curses_num_lines(menu_item_ptr->str, width);
} else {
if (menu_item_ptr->accelerator != GOLD_SYM)
++accel_per_page;
/* Add space for accelerator */
num_lines = curses_num_lines(menu_item_ptr->str, width - 4);
}
menu_item_ptr->num_lines = num_lines;
curline += num_lines;
if (curline > page_end || accel_per_page > MAX_ACCEL_PER_PAGE) {
++page_num;
accel_per_page = 0; /* reset */
curline = tmpline;
/* Move ptr back so entry will be reprocessed on new page */
menu_item_ptr = menu_item_ptr->prev_item;
}
}
menu->num_pages = page_num;
}
/* Determine dimensions of menu window based on term size and entries */
static void
menu_win_size(nhmenu *menu)