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
|
/*
* $Id: formbox.c,v 1.66 2008/06/21 12:12:05 tom Exp $
*
* formbox.c -- implements the form (i.e, some pairs label/editbox)
*
* Copyright 2003-2007,2008 Thomas E. Dickey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License, version 2.1
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to
* Free Software Foundation, Inc.
* 51 Franklin St., Fifth Floor
* Boston, MA 02110, USA.
*
* This is adapted from source contributed by
* Valery Reznic (valery_reznic@users.sourceforge.net)
*/
#include <dialog.h>
#include <dlg_keys.h>
#define LLEN(n) ((n) * FORMBOX_TAGS)
#define ItemName(i) items[LLEN(i) + 0]
#define ItemNameY(i) items[LLEN(i) + 1]
#define ItemNameX(i) items[LLEN(i) + 2]
#define ItemText(i) items[LLEN(i) + 3]
#define ItemTextY(i) items[LLEN(i) + 4]
#define ItemTextX(i) items[LLEN(i) + 5]
#define ItemTextFLen(i) items[LLEN(i) + 6]
#define ItemTextILen(i) items[LLEN(i) + 7]
#define ItemHelp(i) (dialog_vars.item_help ? items[LLEN(i) + 8] : dlg_strempty())
static bool
is_readonly(DIALOG_FORMITEM * item)
{
return ((item->type & 2) != 0) || (item->text_flen <= 0);
}
static bool
is_hidden(DIALOG_FORMITEM * item)
{
return ((item->type & 1) != 0);
}
static bool
in_window(WINDOW *win, int scrollamt, int y)
{
return (y >= scrollamt && y - scrollamt < getmaxy(win));
}
static bool
ok_move(WINDOW *win, int scrollamt, int y, int x)
{
return in_window(win, scrollamt, y)
&& (wmove(win, y - scrollamt, x) != ERR);
}
static void
move_past(WINDOW *win, int y, int x)
{
if (wmove(win, y, x) == ERR)
wmove(win, y, getmaxx(win) - 1);
}
/*
* Print form item
*/
static int
print_item(WINDOW *win, DIALOG_FORMITEM * item, int scrollamt, bool choice)
{
int count = 0;
int len;
if (ok_move(win, scrollamt, item->name_y, item->name_x)) {
len = item->name_len;
len = MIN(len, getmaxx(win) - item->name_x);
if (len > 0) {
dlg_show_string(win,
item->name,
0,
menubox_attr,
item->name_y - scrollamt,
item->name_x,
len,
FALSE,
FALSE);
move_past(win, item->name_y - scrollamt, item->name_x + len);
count = 1;
}
}
if (item->text_len && ok_move(win, scrollamt, item->text_y, item->text_x)) {
chtype this_item_attribute;
len = item->text_len;
len = MIN(len, getmaxx(win) - item->text_x);
if (!is_readonly(item)) {
this_item_attribute = choice
? form_active_text_attr
: form_text_attr;
} else {
this_item_attribute = form_item_readonly_attr;
}
if (len > 0) {
dlg_show_string(win,
item->text,
0,
this_item_attribute,
item->text_y - scrollamt,
item->text_x,
len,
is_hidden(item),
FALSE);
move_past(win, item->text_y - scrollamt, item->text_x + len);
count = 1;
}
}
return count;
}
/*
* Print the entire form.
*/
static void
print_form(WINDOW *win, DIALOG_FORMITEM * item, int total, int scrollamt, int choice)
{
int n;
int count = 0;
for (n = 0; n < total; ++n) {
count += print_item(win, item + n, scrollamt, n == choice);
}
if (count) {
wbkgdset(win, menubox_attr | ' ');
wclrtobot(win);
(void) wnoutrefresh(win);
}
}
static int
set_choice(DIALOG_FORMITEM item[], int choice, int item_no, bool * noneditable)
{
int result = -1;
int i;
*noneditable = FALSE;
if (!is_readonly(&item[choice])) {
result = choice;
} else {
for (i = 0; i < item_no; i++) {
if (!is_readonly(&(item[i]))) {
result = i;
break;
}
}
if (result < 0) {
*noneditable = TRUE;
result = 0;
}
}
return result;
}
/*
* Find the last y-value in the form.
*/
static int
form_limit(DIALOG_FORMITEM item[])
{
int n;
int limit = 0;
for (n = 0; item[n].name != 0; ++n) {
if (limit < item[n].name_y)
limit = item[n].name_y;
if (limit < item[n].text_y)
limit = item[n].text_y;
}
return limit;
}
/*
* Tab to the next field.
*/
static bool
tab_next(WINDOW *win,
DIALOG_FORMITEM item[],
int item_no,
int stepsize,
int *choice,
int *scrollamt)
{
int old_choice = *choice;
int old_scroll = *scrollamt;
bool wrapped = FALSE;
do {
do {
*choice += stepsize;
if (*choice < 0) {
*choice = item_no - 1;
wrapped = TRUE;
} else if (*choice >= item_no) {
*choice = 0;
wrapped = TRUE;
}
} while ((*choice != old_choice) && is_readonly(&(item[*choice])));
if (item[*choice].text_flen > 0) {
int lo = MIN(item[*choice].name_y, item[*choice].text_y);
int hi = MAX(item[*choice].name_y, item[*choice].text_y);
if (old_choice == *choice)
break;
print_item(win, item + old_choice, *scrollamt, FALSE);
if (*scrollamt < lo + 1 - getmaxy(win))
*scrollamt = lo + 1 - getmaxy(win);
if (*scrollamt > hi)
*scrollamt = hi;
/*
* If we have to scroll to show a wrap-around, it does get
* confusing. Just give up rather than scroll. Tab'ing to the
* next field in a multi-column form is a different matter. Scroll
* for that.
*/
if (*scrollamt != old_scroll) {
if (wrapped) {
beep();
*scrollamt = old_scroll;
*choice = old_choice;
} else {
scrollok(win, TRUE);
wscrl(win, *scrollamt - old_scroll);
scrollok(win, FALSE);
}
}
break;
}
} while (*choice != old_choice);
return (old_choice != *choice) || (old_scroll != *scrollamt);
}
/*
* Scroll to the next page, putting the choice at the first editable field
* in that page. Note that fields are not necessarily in top-to-bottom order,
* nor is there necessarily a field on each row of the window.
*/
static bool
scroll_next(WINDOW *win, DIALOG_FORMITEM item[], int stepsize, int *choice, int *scrollamt)
{
int old_choice = *choice;
int old_scroll = *scrollamt;
int old_row = MIN(item[old_choice].text_y, item[old_choice].name_y);
int target = old_scroll + stepsize;
int n;
if (stepsize < 0) {
if (old_row != old_scroll)
target = old_scroll;
else
target = old_scroll + stepsize;
if (target < 0)
target = 0;
} else {
int limit = form_limit(item);
if (target > limit)
target = limit;
}
for (n = 0; item[n].name != 0; ++n) {
if (item[n].text_flen > 0) {
int new_row = MIN(item[n].text_y, item[n].name_y);
if (abs(new_row - target) < abs(old_row - target)) {
old_row = new_row;
*choice = n;
}
}
}
if (old_choice != *choice)
print_item(win, item + old_choice, *scrollamt, FALSE);
*scrollamt = *choice;
if (*scrollamt != old_scroll) {
scrollok(win, TRUE);
wscrl(win, *scrollamt - old_scroll);
scrollok(win, FALSE);
}
return (old_choice != *choice) || (old_scroll != *scrollamt);
}
/*
* Do a sanity check on the field length, and return the "right" value.
*/
static int
real_length(DIALOG_FORMITEM * item)
{
return (item->text_flen > 0
? item->text_flen
: (item->text_flen < 0
? -item->text_flen
: item->text_len));
}
/*
* Compute the form size, setup field buffers.
*/
static void
make_FORM_ELTs(DIALOG_FORMITEM * item,
int item_no,
int *min_height,
int *min_width)
{
int i;
int min_w = 0;
int min_h = 0;
for (i = 0; i < item_no; ++i) {
int real_len = real_length(item + i);
/*
* Special value '0' for text_flen: no input allowed
* Special value '0' for text_ilen: 'be the same as text_flen'
*/
if (item[i].text_ilen == 0)
item[i].text_ilen = real_len;
min_h = MAX(min_h, item[i].name_y + 1);
min_h = MAX(min_h, item[i].text_y + 1);
min_w = MAX(min_w, item[i].name_x + 1 + item[i].name_len);
min_w = MAX(min_w, item[i].text_x + 1 + real_len);
item[i].text_len = real_length(item + i);
/*
* We do not know the actual length of .text, so we allocate it here
* to ensure it is big enough.
*/
if (item[i].text_flen > 0) {
int max_len = dlg_max_input(MAX(item[i].text_ilen + 1, MAX_LEN));
char *old_text = item[i].text;
item[i].text = dlg_malloc(char, max_len + 1);
assert_ptr(item[i].text, "make_FORM_ELTs");
sprintf(item[i].text, "%.*s", item[i].text_ilen, old_text);
if (item[i].text_free) {
item[i].text_free = FALSE;
free(old_text);
}
item[i].text_free = TRUE;
}
}
*min_height = min_h;
*min_width = min_w;
}
int
dlg_default_formitem(DIALOG_FORMITEM * items)
{
int result = 0;
if (dialog_vars.default_item != 0) {
int count = 0;
while (items->name != 0) {
if (!strcmp(dialog_vars.default_item, items->name)) {
result = count;
break;
}
++items;
count++;
}
}
return result;
}
#define sTEXT -1
static int
next_valid_buttonindex(int state, int extra, bool non_editable)
{
state = dlg_next_ok_buttonindex(state, extra);
while (non_editable && state == sTEXT)
state = dlg_next_ok_buttonindex(state, sTEXT);
return state;
}
static int
prev_valid_buttonindex(int state, int extra, bool non_editable)
{
state = dlg_prev_ok_buttonindex(state, extra);
while (non_editable && state == sTEXT)
state = dlg_prev_ok_buttonindex(state, sTEXT);
return state;
}
/*
* Display a form for fulfill a number of fields
*/
int
dlg_form(const char *title,
const char *cprompt,
int height,
int width,
int form_height,
int item_no,
DIALOG_FORMITEM * items,
int *current_item)
{
/* *INDENT-OFF* */
static DLG_KEYS_BINDING binding[] = {
INPUTSTR_BINDINGS,
ENTERKEY_BINDINGS,
DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ),
DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ),
DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NEXT ),
DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ),
DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_PREVIOUS ),
DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ),
DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ),
DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE ),
END_KEYS_BINDING
};
/* *INDENT-ON* */
#ifdef KEY_RESIZE
int old_height = height;
int old_width = width;
#endif
int form_width;
int first = TRUE;
int chr_offset = 0;
int state = dialog_vars.defaultno ? dlg_defaultno_button() : sTEXT;
int x, y, cur_x, cur_y, box_x, box_y;
int code;
int key = 0;
int fkey;
int choice = dlg_default_formitem(items);
int new_choice, new_scroll;
int scrollamt = 0;
int result = DLG_EXIT_UNKNOWN;
int min_width = 0, min_height = 0;
bool was_autosize = (height == 0 || width == 0);
bool show_buttons = FALSE;
bool scroll_changed = FALSE;
bool field_changed = FALSE;
bool non_editable = FALSE;
WINDOW *dialog, *form;
char *prompt = dlg_strclone(cprompt);
const char **buttons = dlg_ok_labels();
DIALOG_FORMITEM *current;
make_FORM_ELTs(items, item_no, &min_height, &min_width);
dlg_does_output();
dlg_tab_correct_str(prompt);
#ifdef KEY_RESIZE
retry:
#endif
dlg_auto_size(title, prompt, &height, &width,
1 + 3 * MARGIN,
MAX(26, 2 + min_width));
if (form_height == 0)
form_height = min_height;
if (was_autosize) {
form_height = MIN(SLINES - height, form_height);
height += form_height;
} else {
int thigh = 0;
int twide = 0;
dlg_auto_size(title, prompt, &thigh, &twide, 0, width);
thigh = SLINES - (height - (thigh + 1 + 3 * MARGIN));
form_height = MIN(thigh, form_height);
}
dlg_print_size(height, width);
dlg_ctl_size(height, width);
x = dlg_box_x_ordinate(width);
y = dlg_box_y_ordinate(height);
dialog = dlg_new_window(height, width, y, x);
dlg_register_window(dialog, "formbox", binding);
dlg_register_buttons(dialog, "formbox", buttons);
dlg_mouse_setbase(x, y);
dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
dlg_draw_bottom_box(dialog);
dlg_draw_title(dialog, title);
wattrset(dialog, dialog_attr);
dlg_print_autowrap(dialog, prompt, height, width);
form_width = width - 6;
getyx(dialog, cur_y, cur_x);
box_y = cur_y + 1;
box_x = (width - form_width) / 2 - 1;
/* create new window for the form */
form = dlg_sub_window(dialog, form_height, form_width, y + box_y + 1,
x + box_x + 1);
/* draw a box around the form items */
dlg_draw_box(dialog, box_y, box_x, form_height + 2, form_width + 2,
menubox_border_attr, menubox_attr);
/* register the new window, along with its borders */
dlg_mouse_mkbigregion(getbegy(form) - getbegy(dialog),
getbegx(form) - getbegx(dialog),
getmaxy(form),
getmaxx(form),
KEY_MAX, 1, 1, 3 /* by cells */ );
show_buttons = TRUE;
scroll_changed = TRUE;
choice = set_choice(items, choice, item_no, &non_editable);
current = &items[choice];
if (non_editable)
state = next_valid_buttonindex(state, sTEXT, non_editable);
while (result == DLG_EXIT_UNKNOWN) {
int edit = FALSE;
if (scroll_changed) {
print_form(form, items, item_no, scrollamt, choice);
dlg_draw_arrows(dialog, scrollamt,
scrollamt + form_height < item_no,
box_x + 1,
box_y,
box_y + form_height + 1);
scroll_changed = FALSE;
}
if (show_buttons) {
dlg_item_help("");
dlg_draw_buttons(dialog, height - 2, 0, buttons,
((state < 0)
? 1000 /* no such button, not highlighted */
: state),
FALSE, width);
show_buttons = FALSE;
}
if (field_changed || state == sTEXT) {
if (field_changed)
chr_offset = 0;
current = &items[choice];
dialog_vars.max_input = current->text_ilen;
dlg_item_help(current->help);
dlg_show_string(form, current->text, chr_offset,
form_active_text_attr,
current->text_y - scrollamt,
current->text_x,
current->text_len,
is_hidden(current), first);
field_changed = FALSE;
}
key = dlg_mouse_wgetch(dialog, &fkey);
if (dlg_result_key(key, fkey, &result))
break;
/* handle non-functionkeys */
if (!fkey) {
if (state != sTEXT) {
code = dlg_char_to_button(key, buttons);
if (code >= 0) {
dlg_del_window(dialog);
result = dlg_ok_buttoncode(code);
continue;
}
if (key == ' ') {
fkey = TRUE;
key = DLGK_ENTER;
}
}
}
/* handle functionkeys */
if (fkey) {
bool do_scroll = FALSE;
bool do_tab = FALSE;
int move_by = 0;
switch (key) {
case DLGK_MOUSE(KEY_PPAGE):
case DLGK_PAGE_PREV:
do_scroll = TRUE;
move_by = -form_height;
break;
case DLGK_MOUSE(KEY_NPAGE):
case DLGK_PAGE_NEXT:
do_scroll = TRUE;
move_by = form_height;
break;
case DLGK_ENTER:
dlg_del_window(dialog);
result = (state >= 0) ? dlg_ok_buttoncode(state) : DLG_EXIT_OK;
continue;
case DLGK_GRID_LEFT:
if (state == sTEXT)
break;
/* FALLTHRU */
case DLGK_ITEM_PREV:
if (state == sTEXT) {
do_tab = TRUE;
move_by = -1;
break;
} else {
state = prev_valid_buttonindex(state, 0, non_editable);
show_buttons = TRUE;
continue;
}
case DLGK_FIELD_PREV:
state = prev_valid_buttonindex(state, sTEXT, non_editable);
show_buttons = TRUE;
continue;
case DLGK_FIELD_NEXT:
state = next_valid_buttonindex(state, sTEXT, non_editable);
show_buttons = TRUE;
continue;
case DLGK_GRID_RIGHT:
if (state == sTEXT)
break;
/* FALLTHRU */
case DLGK_ITEM_NEXT:
if (state == sTEXT) {
do_tab = TRUE;
move_by = 1;
break;
} else {
state = next_valid_buttonindex(state, 0, non_editable);
show_buttons = TRUE;
continue;
}
#ifdef KEY_RESIZE
case KEY_RESIZE:
/* reset data */
height = old_height;
width = old_width;
/* repaint */
dlg_clear();
dlg_del_window(dialog);
refresh();
dlg_mouse_free_regions();
goto retry;
#endif
default:
#if USE_MOUSE
if (is_DLGK_MOUSE(key)) {
if (key >= DLGK_MOUSE(KEY_MAX)) {
int cell = key - DLGK_MOUSE(KEY_MAX);
int row = (cell / getmaxx(form)) + scrollamt;
int col = (cell % getmaxx(form));
int n;
for (n = 0; n < item_no; ++n) {
if (items[n].name_y == row
&& items[n].name_x <= col
&& (items[n].name_x + items[n].name_len > col
|| (items[n].name_y == items[n].text_y
&& items[n].text_x > col))) {
if (!is_readonly(&(items[n]))) {
field_changed = TRUE;
break;
}
}
if (items[n].text_y == row
&& items[n].text_x <= col
&& items[n].text_x + items[n].text_ilen > col) {
if (!is_readonly(&(items[n]))) {
field_changed = TRUE;
break;
}
}
}
if (field_changed) {
print_item(form, items + choice, scrollamt, FALSE);
choice = n;
continue;
}
beep();
} else if ((code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
result = code;
}
continue;
}
#endif
break;
}
new_scroll = scrollamt;
new_choice = choice;
if (do_scroll) {
if (scroll_next(form, items, move_by, &new_choice, &new_scroll)) {
if (choice != new_choice) {
choice = new_choice;
field_changed = TRUE;
}
if (scrollamt != new_scroll) {
scrollamt = new_scroll;
scroll_changed = TRUE;
}
}
continue;
}
if (do_tab) {
if (tab_next(form, items, item_no, move_by, &new_choice, &new_scroll)) {
if (choice != new_choice) {
choice = new_choice;
field_changed = TRUE;
}
if (scrollamt != new_scroll) {
scrollamt = new_scroll;
scroll_changed = TRUE;
}
}
continue;
}
}
if (state == sTEXT) { /* Input box selected */
if (!is_readonly(current))
edit = dlg_edit_string(current->text, &chr_offset, key,
fkey, first);
if (edit) {
dlg_show_string(form, current->text, chr_offset,
form_active_text_attr,
current->text_y - scrollamt,
current->text_x,
current->text_len,
is_hidden(current), first);
continue;
}
}
}
dlg_mouse_free_regions();
dlg_del_window(dialog);
free(prompt);
*current_item = choice;
return result;
}
/*
* Free memory owned by a list of DIALOG_FORMITEM's.
*/
void
dlg_free_formitems(DIALOG_FORMITEM * items)
{
int n;
for (n = 0; items[n].name != 0; ++n) {
if (items[n].name_free)
free(items[n].name);
if (items[n].text_free)
free(items[n].text);
if (items[n].help_free && items[n].help != dlg_strempty())
free(items[n].help);
}
free(items);
}
/*
* The script accepts values beginning at 1, while curses starts at 0.
*/
int
dlg_ordinate(const char *s)
{
int result = atoi(s);
if (result > 0)
--result;
else
result = 0;
return result;
}
int
dialog_form(const char *title,
const char *cprompt,
int height,
int width,
int form_height,
int item_no,
char **items)
{
int result;
int choice;
int i;
DIALOG_FORMITEM *listitems;
DIALOG_VARS save_vars;
bool show_status = FALSE;
dlg_save_vars(&save_vars);
dialog_vars.separate_output = TRUE;
listitems = dlg_calloc(DIALOG_FORMITEM, item_no + 1);
assert_ptr(listitems, "dialog_form");
for (i = 0; i < item_no; ++i) {
listitems[i].type = dialog_vars.formitem_type;
listitems[i].name = ItemName(i);
listitems[i].name_len = strlen(ItemName(i));
listitems[i].name_y = dlg_ordinate(ItemNameY(i));
listitems[i].name_x = dlg_ordinate(ItemNameX(i));
listitems[i].text = ItemText(i);
listitems[i].text_len = strlen(ItemText(i));
listitems[i].text_y = dlg_ordinate(ItemTextY(i));
listitems[i].text_x = dlg_ordinate(ItemTextX(i));
listitems[i].text_flen = atoi(ItemTextFLen(i));
listitems[i].text_ilen = atoi(ItemTextILen(i));
listitems[i].help = (dialog_vars.item_help) ? ItemHelp(i) :
dlg_strempty();
}
result = dlg_form(title,
cprompt,
height,
width,
form_height,
item_no,
listitems,
&choice);
switch (result) {
case DLG_EXIT_OK: /* FALLTHRU */
case DLG_EXIT_EXTRA:
show_status = TRUE;
break;
case DLG_EXIT_HELP:
dlg_add_result("HELP ");
show_status = dialog_vars.help_status;
if (USE_ITEM_HELP(listitems[choice].help)) {
dlg_add_string(listitems[choice].help);
result = DLG_EXIT_ITEM_HELP;
} else {
dlg_add_string(listitems[choice].name);
}
if (show_status)
dlg_add_separator();
break;
}
if (show_status) {
for (i = 0; i < item_no; i++) {
if (listitems[i].text_flen > 0) {
dlg_add_string(listitems[i].text);
dlg_add_separator();
}
}
}
dlg_free_formitems(listitems);
dlg_restore_vars(&save_vars);
return result;
}
|