capt_get_packet(): check for key press only every 20ms
[iptraf-ng.git] / src / tui / listbox.c
blob5eb60c704fe4163bd3a5899c4331bd4fdb9c773d
1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
4 /*
5 * listbox.c - scrollable listbox management module
6 */
8 #include "iptraf-ng-compat.h"
10 #include "winops.h"
11 #include "labels.h"
12 #include "listbox.h"
13 #include "msgboxes.h"
15 #define SCROLLUP 0
16 #define SCROLLDOWN 1
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;
36 list->width = width;
37 list->keyattr = keyattr;
38 list->row = 0;
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);
56 ptmp->nodeptr = node;
58 if (list->textlist == NULL) {
59 list->textlist = ptmp;
60 ptmp->prev_entry = NULL;
61 } else {
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)
72 int i = 0;
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;
78 i++;
81 update_panels();
82 doupdate();
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,
89 list->textptr->text);
93 static void tx_scroll_listbox(struct scroll_list *list, int direction,
94 int lines)
96 if (lines < 1)
97 return;
99 if (direction == SCROLLUP) {
100 for (int i = 0; i < lines; i++) {
101 if (list->textptr->next_entry == NULL)
102 break;
104 tx_print_row(list, list->mainattr);
105 if (list->row == list->height - 3) {
106 scrollok(list->win, 1);
107 wscrl(list->win, 1);
108 scrollok(list->win, 0);
109 } else
110 list->row++;
111 list->textptr = list->textptr->next_entry;
112 tx_print_row(list, list->selectattr);
114 } else {
115 for (int i = 0; i < lines; i++) {
116 if (list->textptr->prev_entry == NULL)
117 break;
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);
124 } else
125 list->row--;
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)
134 int endloop = 0;
136 if (list->textlist == NULL) {
137 tui_error(ANYKEY_MSG, "No list entries");
138 *aborted = 1;
139 return;
142 list->textptr = list->textlist;
144 tx_listkeyhelp(list->mainattr, list->keyattr);
145 update_panels();
146 doupdate();
148 tx_print_row(list, list->selectattr);
149 while (!endloop) {
150 switch (wgetch(list->win)) {
151 case KEY_UP:
152 tx_scroll_listbox(list, SCROLLDOWN, 1);
153 break;
154 case KEY_DOWN:
155 tx_scroll_listbox(list, SCROLLUP, 1);
156 break;
157 case KEY_PPAGE:
158 tx_scroll_listbox(list, SCROLLDOWN, list->height - 2);
159 break;
160 case KEY_NPAGE:
161 tx_scroll_listbox(list, SCROLLUP, list->height - 2);
162 break;
163 case KEY_HOME:
164 tx_scroll_listbox(list, SCROLLDOWN, INT_MAX);
165 break;
166 case KEY_END:
167 tx_scroll_listbox(list, SCROLLUP, INT_MAX);
168 break;
169 case 13:
170 *aborted = 0;
171 endloop = 1;
172 break;
173 case 27:
174 case 'x':
175 case 'X':
176 case 24:
177 *aborted = 1;
178 endloop = 1;
179 /* fall through */
180 case 12:
181 case 'l':
182 case 'L':
183 tx_refresh_screen();
184 break;
189 void tx_hide_listbox(struct scroll_list *list)
191 hide_panel(list->panel);
192 hide_panel(list->borderpanel);
193 update_panels();
194 doupdate();
197 void tx_unhide_listbox(struct scroll_list *list)
199 show_panel(list->panel);
200 show_panel(list->panel);
201 update_panels();
202 doupdate();
205 void tx_close_listbox(struct scroll_list *list)
207 del_panel(list->panel);
208 del_panel(list->borderpanel);
209 delwin(list->win);
210 delwin(list->borderwin);
212 update_panels();
213 doupdate();
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;
223 free(ttmp);
224 ttmp = ctmp;