1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
5 * listbox.c - scrollable listbox management module
8 #include "iptraf-ng-compat.h"
18 void tx_init_listbox(struct scroll_list
*list
, int width
, int height
,
19 int startx
, int starty
, int mainattr
, int borderattr
,
20 int selectattr
, int keyattr
)
22 memset(list
, 0, sizeof(struct scroll_list
));
23 list
->borderwin
= newwin(height
, width
, starty
, startx
);
24 list
->borderpanel
= new_panel(list
->borderwin
);
25 wattrset(list
->borderwin
, borderattr
);
26 tx_box(list
->borderwin
, ACS_VLINE
, ACS_HLINE
);
28 list
->win
= newwin(height
- 2, width
- 2, starty
+ 1, startx
+ 1);
29 list
->panel
= new_panel(list
->win
);
30 wattrset(list
->win
, mainattr
);
31 tx_colorwin(list
->win
);
33 list
->mainattr
= mainattr
;
34 list
->selectattr
= selectattr
;
35 list
->height
= height
;
37 list
->keyattr
= keyattr
;
40 tx_stdwinset(list
->win
);
41 scrollok(list
->win
, 0);
44 void tx_set_listbox_title(struct scroll_list
*list
, char *text
, int x
)
46 mvwprintw(list
->borderwin
, 0, x
, " %s ", text
);
49 void tx_add_list_entry(struct scroll_list
*list
, char *node
, char *text
)
51 struct textlisttype
*ptmp
;
53 ptmp
= xmallocz(sizeof(struct textlisttype
));
55 strncpy(ptmp
->text
, text
, MAX_TEXT_LENGTH
- 1);
58 if (list
->textlist
== NULL
) {
59 list
->textlist
= ptmp
;
60 ptmp
->prev_entry
= NULL
;
62 list
->texttail
->next_entry
= ptmp
;
63 ptmp
->prev_entry
= list
->texttail
;
66 list
->texttail
= ptmp
;
67 ptmp
->next_entry
= NULL
;
70 void tx_show_listbox(struct scroll_list
*list
)
73 struct textlisttype
*tptr
= list
->textlist
;
75 while ((i
<= list
->height
- 3) && (tptr
!= NULL
)) {
76 mvwprintw(list
->win
, i
, 1, tptr
->text
);
77 tptr
= tptr
->next_entry
;
85 static void tx_print_row(struct scroll_list
*list
, int attr
)
87 wattrset(list
->win
, attr
);
88 mvwprintw(list
->win
, list
->row
, 0, " %-*s", list
->width
- 3,
93 static void tx_scroll_listbox(struct scroll_list
*list
, int direction
,
99 if (direction
== SCROLLUP
) {
100 for (int i
= 0; i
< lines
; i
++) {
101 if (list
->textptr
->next_entry
== NULL
)
104 tx_print_row(list
, list
->mainattr
);
105 if (list
->row
== list
->height
- 3) {
106 scrollok(list
->win
, 1);
108 scrollok(list
->win
, 0);
111 list
->textptr
= list
->textptr
->next_entry
;
112 tx_print_row(list
, list
->selectattr
);
115 for (int i
= 0; i
< lines
; i
++) {
116 if (list
->textptr
->prev_entry
== NULL
)
119 tx_print_row(list
, list
->mainattr
);
120 if (list
->row
== 0) {
121 scrollok(list
->win
, 1);
122 wscrl(list
->win
, -1);
123 scrollok(list
->win
, 0);
126 list
->textptr
= list
->textptr
->prev_entry
;
127 tx_print_row(list
, list
->selectattr
);
132 void tx_operate_listbox(struct scroll_list
*list
, int *aborted
)
136 if (list
->textlist
== NULL
) {
137 tui_error(ANYKEY_MSG
, "No list entries");
142 list
->textptr
= list
->textlist
;
144 tx_listkeyhelp(list
->mainattr
, list
->keyattr
);
148 tx_print_row(list
, list
->selectattr
);
150 switch (wgetch(list
->win
)) {
152 tx_scroll_listbox(list
, SCROLLDOWN
, 1);
155 tx_scroll_listbox(list
, SCROLLUP
, 1);
158 tx_scroll_listbox(list
, SCROLLDOWN
, list
->height
- 2);
161 tx_scroll_listbox(list
, SCROLLUP
, list
->height
- 2);
164 tx_scroll_listbox(list
, SCROLLDOWN
, INT_MAX
);
167 tx_scroll_listbox(list
, SCROLLUP
, INT_MAX
);
189 void tx_hide_listbox(struct scroll_list
*list
)
191 hide_panel(list
->panel
);
192 hide_panel(list
->borderpanel
);
197 void tx_unhide_listbox(struct scroll_list
*list
)
199 show_panel(list
->panel
);
200 show_panel(list
->panel
);
205 void tx_close_listbox(struct scroll_list
*list
)
207 del_panel(list
->panel
);
208 del_panel(list
->borderpanel
);
210 delwin(list
->borderwin
);
216 void tx_destroy_list(struct scroll_list
*list
)
218 struct textlisttype
*ttmp
= list
->textlist
;
219 struct textlisttype
*ctmp
;
221 while (ttmp
!= NULL
) {
222 ctmp
= ttmp
->next_entry
;