-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextbox.c
203 lines (168 loc) · 3.56 KB
/
textbox.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "mace.h"
struct textbox *
textboxnew(struct mace *m,
struct colour *bg,
struct sequence *seq)
{
struct textbox *t;
t = calloc(1, sizeof(struct textbox));
if (t == NULL) {
return NULL;
}
t->mace = m;
t->sequence = seq;
t->curcs = NULL;
t->start = 0;
t->linewidth = 0;
t->nglyphs = 0;
t->nglyphsmax = 1024;
t->glyphs = malloc(sizeof(cairo_glyph_t) * t->nglyphsmax);
if (t->glyphs == NULL) {
free(t);
return NULL;
}
memmove(&t->bg, bg, sizeof(struct colour));
return t;
}
void
textboxfree(struct textbox *t)
{
struct cursel *c, *cn;
for (c = t->mace->cursels; c != NULL; c = cn) {
cn = c->next;
if (c->tb == t) {
curselremove(t->mace, c);
}
}
sequencefree(t->sequence);
free(t->glyphs);
free(t);
}
bool
textboxresize(struct textbox *t, int lw, int h)
{
t->linewidth = lw;
t->maxheight = h;
t->nglyphs = 0;
textboxplaceglyphs(t);
return true;
}
bool
textboxbuttonpress(struct textbox *t, int x, int y,
unsigned int button)
{
struct cursel *c;
size_t pos, start, len;
pos = textboxfindpos(t, x, y);
switch (button) {
case 1:
curselremoveall(t->mace);
t->curcs = curseladd(t->mace, t, CURSEL_nrm, pos);
return true;
case 2:
/* Temparary */
t->curcs = curseladd(t->mace, t, CURSEL_nrm, pos);
return true;
case 3:
c = curselat(t->mace, t, pos);
if (c != NULL && c->start != c->end) {
c->type |= CURSEL_cmd;
return true;
} else if (sequencefindword(t->sequence, pos, &start,
&len)) {
c = curseladd(t->mace, t, CURSEL_cmd, start);
curselupdate(c, start + len);
return true;
} else {
return false;
}
}
return false;
}
bool
textboxbuttonrelease(struct textbox *t, int x, int y,
unsigned int button)
{
struct cursel *c, *cn;
size_t pos, start, len;
uint8_t *buf;
t->curcs = NULL;
switch (button) {
case 1:
case 2:
break;
case 3:
pos = textboxfindpos(t, x, y);
c = curselat(t->mace, t, pos);
if (c != NULL && (c->type & CURSEL_cmd) != 0) {
start = c->start;
len = c->end - start;
} else {
len = 0;
}
for (c = t->mace->cursels; c != NULL; c = cn) {
cn = c->next;
if ((c->type & CURSEL_cmd) != 0) {
if ((c->type & CURSEL_nrm) != 0) {
c->type &= ~CURSEL_cmd;
} else {
curselremove(t->mace, c);
}
}
}
if (len > 0) {
buf = malloc(len + 1);
if (buf == NULL) {
return false;
}
if (sequenceget(t->sequence, start, buf, len) == 0) {
free(buf);
return false;
}
buf[len] = 0;
/* TODO: Show an error somehow if this returns false. */
command(t->mace, buf);
free(buf);
}
return true;
}
return false;
}
bool
textboxmotion(struct textbox *t, int x, int y)
{
size_t pos;
if (t->curcs != NULL) {
pos = textboxfindpos(t, x, y);
return curselupdate(t->curcs, pos);
} else {
return false;
}
}
static size_t
scrollnewindex(struct textbox *t, size_t i, int lines)
{
if (lines > 0) {
while (lines-- > 0) {
i = textboxindexbelow(t, i);
}
} else if (lines < 0) {
while (lines++ < 0) {
i = textboxindexabove(t, i);
}
}
return i;
}
bool
textboxscroll(struct textbox *t, int lines)
{
size_t newstart;
newstart = scrollnewindex(t, t->start, lines);
if (newstart != t->start) {
t->start = newstart;
textboxplaceglyphs(t);
return true;
} else {
return false;
}
}