diff options
Diffstat (limited to 'src/ui/text_input.h')
-rw-r--r-- | src/ui/text_input.h | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/ui/text_input.h b/src/ui/text_input.h new file mode 100644 index 0000000..8dc416f --- /dev/null +++ b/src/ui/text_input.h | |||
@@ -0,0 +1,85 @@ | |||
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/text_input.h | ||
23 | */ | ||
24 | |||
25 | #ifndef UI_TEXT_INPUT_H_ | ||
26 | #define UI_TEXT_INPUT_H_ | ||
27 | |||
28 | #include <ctype.h> | ||
29 | |||
30 | #define text_input_event(text, key) { \ | ||
31 | switch (key) \ | ||
32 | { \ | ||
33 | case KEY_LEFT: \ | ||
34 | { \ | ||
35 | text##_pos--; \ | ||
36 | break; \ | ||
37 | } \ | ||
38 | case KEY_RIGHT: \ | ||
39 | { \ | ||
40 | text##_pos++; \ | ||
41 | break; \ | ||
42 | } \ | ||
43 | case KEY_BACKSPACE: \ | ||
44 | { \ | ||
45 | if ((text##_pos < text##_len) && (text##_pos > 0)) \ | ||
46 | for (int i = text##_pos; i < text##_len; i++) \ | ||
47 | text[i - 1] = text[i]; \ | ||
48 | \ | ||
49 | if ((text##_pos > 0) && (text##_len > 0)) \ | ||
50 | { \ | ||
51 | text##_pos--; \ | ||
52 | text##_len--; \ | ||
53 | } \ | ||
54 | \ | ||
55 | break; \ | ||
56 | } \ | ||
57 | default: \ | ||
58 | { \ | ||
59 | if (!isprint(key)) \ | ||
60 | break; \ | ||
61 | \ | ||
62 | for (int i = text##_len - 1; i >= text##_pos; i--) \ | ||
63 | text[i + 1] = text[i]; \ | ||
64 | \ | ||
65 | text[text##_pos++] = (char) key; \ | ||
66 | text##_len++; \ | ||
67 | break; \ | ||
68 | } \ | ||
69 | } \ | ||
70 | \ | ||
71 | int max_len = (int) sizeof(text) - 1; \ | ||
72 | \ | ||
73 | if (text##_len > max_len) \ | ||
74 | text##_len = max_len; \ | ||
75 | \ | ||
76 | text[text##_len] = '\0'; \ | ||
77 | \ | ||
78 | if (text##_pos < 0) \ | ||
79 | text##_pos = 0; \ | ||
80 | \ | ||
81 | if (text##_pos > text##_len) \ | ||
82 | text##_pos = text##_len; \ | ||
83 | } | ||
84 | |||
85 | #endif /* UI_TEXT_INPUT_H_ */ | ||