diff options
Diffstat (limited to 'src/ui/list_input.h')
-rw-r--r-- | src/ui/list_input.h | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/src/ui/list_input.h b/src/ui/list_input.h new file mode 100644 index 0000000..aad3ae4 --- /dev/null +++ b/src/ui/list_input.h | |||
@@ -0,0 +1,114 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2022 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /* | ||
21 | * @author Tobias Frisch | ||
22 | * @file ui/list_input.h | ||
23 | */ | ||
24 | |||
25 | #ifndef UI_LIST_INPUT_H_ | ||
26 | #define UI_LIST_INPUT_H_ | ||
27 | |||
28 | #include <stdbool.h> | ||
29 | #include <stdlib.h> | ||
30 | |||
31 | #define list_input_reset(list) { \ | ||
32 | (list)->line_index = 0; \ | ||
33 | (list)->selected = NULL; \ | ||
34 | } | ||
35 | |||
36 | #define list_input_select(list, line_width, item) { \ | ||
37 | const bool selected = ( \ | ||
38 | ((list)->line_selected >= (list)->line_index) && \ | ||
39 | ((list)->line_selected < (list)->line_index + line_width) \ | ||
40 | ); \ | ||
41 | \ | ||
42 | (list)->line_index += line_width; \ | ||
43 | \ | ||
44 | if (selected) \ | ||
45 | (list)->selected = item; \ | ||
46 | } | ||
47 | |||
48 | #define list_input_event(list, key) { \ | ||
49 | int count = (list)->line_index; \ | ||
50 | \ | ||
51 | switch (key) \ | ||
52 | { \ | ||
53 | case KEY_UP: \ | ||
54 | { \ | ||
55 | (list)->line_selected--; \ | ||
56 | break; \ | ||
57 | } \ | ||
58 | case KEY_DOWN: \ | ||
59 | { \ | ||
60 | (list)->line_selected++; \ | ||
61 | break; \ | ||
62 | } \ | ||
63 | default: \ | ||
64 | break; \ | ||
65 | } \ | ||
66 | \ | ||
67 | if ((list)->line_selected < 0) \ | ||
68 | (list)->line_selected = 0; \ | ||
69 | else if ((list)->line_selected >= count) \ | ||
70 | (list)->line_selected = count - 1; \ | ||
71 | \ | ||
72 | if ((list)->window) \ | ||
73 | { \ | ||
74 | const int height = getmaxy((list)->window); \ | ||
75 | const int y = (list)->line_selected - (list)->line_offset; \ | ||
76 | \ | ||
77 | if (y < 0) \ | ||
78 | (list)->line_offset += y; \ | ||
79 | else if (y + 1 >= height) \ | ||
80 | (list)->line_offset += y + 1 - height; \ | ||
81 | \ | ||
82 | if ((list)->line_offset < 0) \ | ||
83 | (list)->line_offset = 0; \ | ||
84 | else if ((list)->line_offset >= count) \ | ||
85 | (list)->line_offset = count - 1; \ | ||
86 | } \ | ||
87 | } | ||
88 | |||
89 | #define list_input_print_(list, line_width, yes_res, no_res) \ | ||
90 | const bool selected = ( \ | ||
91 | ((list)->line_selected >= (list)->line_index) && \ | ||
92 | ((list)->line_selected < (list)->line_index + line_width) \ | ||
93 | ); \ | ||
94 | \ | ||
95 | const int y = (list)->line_index - (list)->line_offset; { \ | ||
96 | \ | ||
97 | (list)->line_index += line_width; \ | ||
98 | \ | ||
99 | if (y + line_width < 1) \ | ||
100 | return yes_res; \ | ||
101 | \ | ||
102 | const int height = getmaxy((list)->window); \ | ||
103 | \ | ||
104 | if (y >= height) \ | ||
105 | return no_res; \ | ||
106 | } | ||
107 | |||
108 | #define list_input_print_gnunet(list, line_width) \ | ||
109 | list_input_print_(list, line_width, GNUNET_YES, GNUNET_NO) | ||
110 | |||
111 | #define list_input_print(list, line_width) \ | ||
112 | list_input_print_(list, line_width, , ) | ||
113 | |||
114 | #endif /* UI_LIST_INPUT_H_ */ | ||