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
|
#include <stdlib.h>
#include <string.h>
#include "ui.h"
#include "ui_input.h"
struct input *
init_input(unsigned int x, unsigned int y, unsigned int width, char *prompt, size_t input_len, chtype attrs)
{
struct input *a = calloc(1, sizeof(struct input));
a->x = x;
a->y = y;
a->width = width;
a->cur_pos = 0;
a->input = calloc(input_len+1, sizeof(char));
a->input_max = input_len;
a->input_len = 0;
a->input_pos = 0;
a->prompt = strdup(prompt);
a->attrs = attrs;
return (a);
}
void
free_input(struct input *a)
{
if (a->input != NULL) {
free(a->input);
}
free(a->prompt);
free(a);
}
static void
print_input_text(WINDOW *win, struct input *a)
{
size_t start = 0;
size_t p_len = strlen(a->prompt);
char tmp[a->width + 1];
memset(tmp, '\0', a->width + 1);
if (a->input_pos >= a->width) {
start = a->input_pos - a->width;
}
strncpy(tmp, (char *)(a->input + start), a->width);
if (win == NULL) {
mvprintw(a->y, a->x + p_len, "%s", tmp);
} else {
mvwprintw(win, a->y, a->x + p_len, "%s", tmp);
}
}
static void
print_input(WINDOW *win, struct input *a)
{
char *tmp;
int i;
size_t p_len = strlen(a->prompt);
attron(a->attrs);
if (win == NULL) {
mvprintw(a->y, a->x, a->prompt);
tmp = calloc(a->width+1, sizeof(char));
for (i = 0; i < a->width; i++) {
*(tmp + i) = '_';
}
mvprintw(a->y, a->x + p_len, tmp);
free(tmp);
} else {
}
print_input_text(win, a);
attroff(a->attrs);
}
int
activate_input(WINDOW *win, struct input *a)
{
if (a == NULL) return (UICB_ERR_UNDEF);
size_t p_len = strlen(a->prompt);
if (win == NULL) {
move(a->y, a->x + p_len + a->cur_pos);
} else {
wmove(win, a->y, a->x + p_len + a->cur_pos);
}
return (UICB_OK);
}
int
add_input(WINDOW *win, struct input *a, int key)
{
if (a == NULL) return (UICB_ERR_UNDEF);
if (a->input_len >= a->input_max) return (UICB_ERR_BUF);
*(a->input + a->input_pos) = (char) key;
++a->input_pos;
++a->input_len;
a->cur_pos = (a->cur_pos+1 < a->width ? a->cur_pos+1 : a->cur_pos);
print_input(win, a);
//mvwprintw(win, 10, 1, "w:%d,cp:%d,im:%lu,il:%lu,ip:%lu,s:%s", a->width, a->cur_pos, a->input_max, a->input_len, a->input_pos, a->input);
return (UICB_OK);
}
int
del_input(WINDOW *win, struct input *a)
{
if (a == NULL) return (UICB_ERR_UNDEF);
memmove((a->input + a->input_pos - 1), (a->input + a->input_pos), a->input_max - a->input_pos);
--a->input_len;
if (a->input_pos == a->input_len) {
--a->input_pos;
}
print_input(win, a);
mvwprintw(win, 10, 1, "w:%d,cp:%d,im:%lu,il:%lu,ip:%lu,s:%s", a->width, a->cur_pos, a->input_max, a->input_len, a->input_pos, a->input);
return (UICB_OK);
}
int
input_cb(WINDOW *win, void *data, bool timed_out)
{
struct input *a = (struct input *) data;
if (a == NULL) return (UICB_ERR_UNDEF);
if (timed_out == true) {
print_input(win, a);
}
return (UICB_OK);
}
void
register_input(WINDOW *win, struct input *a)
{
register_ui_elt(input_cb, (void *) a, win);
}
|