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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/*
Copyright (C) 2003-2006 Andrey Nazarov
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
//
// field.c
//
#include "shared/shared.h"
#include "common/common.h"
#include "common/field.h"
#include "client/client.h"
#include "client/keys.h"
#include "client/video.h"
#include "refresh/refresh.h"
/*
================
IF_Init
================
*/
void IF_Init(inputField_t *field, size_t visibleChars, size_t maxChars)
{
memset(field, 0, sizeof(*field));
if (maxChars >= sizeof(field->text)) {
maxChars = sizeof(field->text) - 1;
}
if (visibleChars > maxChars) {
visibleChars = maxChars;
}
field->maxChars = maxChars;
field->visibleChars = visibleChars;
}
/*
================
IF_Clear
================
*/
void IF_Clear(inputField_t *field)
{
memset(field->text, 0, sizeof(field->text));
field->cursorPos = 0;
}
/*
================
IF_Replace
================
*/
void IF_Replace(inputField_t *field, const char *text)
{
if (field->maxChars && text) {
size_t len = Q_strlcpy(field->text, text, field->maxChars + 1);
field->cursorPos = len >= field->maxChars ? field->maxChars - 1 : len;
} else {
field->text[0] = 0;
field->cursorPos = 0;
}
}
#if USE_CLIENT
/*
================
IF_KeyEvent
================
*/
qboolean IF_KeyEvent(inputField_t *field, int key)
{
if (!field->maxChars) {
return qfalse;
}
if (field->cursorPos >= field->maxChars) {
Com_Error(ERR_FATAL, "%s: bad cursorPos", __func__);
}
if (key == K_DEL) {
if (field->text[field->cursorPos]) {
memmove(field->text + field->cursorPos,
field->text + field->cursorPos + 1,
sizeof(field->text) - field->cursorPos);
}
return qtrue;
}
if (key == K_BACKSPACE || (key == 'h' && Key_IsDown(K_CTRL))) {
if (field->cursorPos > 0) {
memmove(field->text + field->cursorPos - 1,
field->text + field->cursorPos,
sizeof(field->text) - field->cursorPos);
field->cursorPos--;
}
return qtrue;
}
if (key == 'w' && Key_IsDown(K_CTRL)) {
size_t oldpos = field->cursorPos;
// kill trailing whitespace
while (field->cursorPos > 0 && field->text[field->cursorPos] <= 32) {
field->cursorPos--;
}
// kill this word
while (field->cursorPos > 0 && field->text[field->cursorPos - 1] > 32) {
field->cursorPos--;
}
memmove(field->text + field->cursorPos, field->text + oldpos,
sizeof(field->text) - oldpos);
return qtrue;
}
if (key == 'u' && Key_IsDown(K_CTRL)) {
memmove(field->text, field->text + field->cursorPos,
sizeof(field->text) - field->cursorPos);
field->cursorPos = 0;
return qtrue;
}
if (key == 'k' && Key_IsDown(K_CTRL)) {
field->text[field->cursorPos] = 0;
return qtrue;
}
if (key == 'c' && Key_IsDown(K_CTRL)) {
VID_SetClipboardData(field->text);
return qtrue;
}
if (key == K_LEFTARROW || (key == 'b' && Key_IsDown(K_CTRL))) {
if (field->cursorPos > 0) {
field->cursorPos--;
}
return qtrue;
}
if (key == K_RIGHTARROW || (key == 'f' && Key_IsDown(K_CTRL))) {
if (field->text[field->cursorPos]) {
field->cursorPos++;
}
goto check;
}
if (key == 'b' && Key_IsDown(K_ALT)) {
if (field->cursorPos > 0 && field->text[field->cursorPos - 1] <= 32) {
field->cursorPos--;
}
while (field->cursorPos > 0 && field->text[field->cursorPos] <= 32) {
field->cursorPos--;
}
while (field->cursorPos > 0 && field->text[field->cursorPos - 1] > 32) {
field->cursorPos--;
}
return qtrue;
}
if (key == 'f' && Key_IsDown(K_ALT)) {
while (field->text[field->cursorPos] && field->text[field->cursorPos] <= 32) {
field->cursorPos++;
}
while (field->text[field->cursorPos] > 32) {
field->cursorPos++;
}
goto check;
}
if (key == K_HOME || (key == 'a' && Key_IsDown(K_CTRL))) {
field->cursorPos = 0;
return qtrue;
}
if (key == K_END || (key == 'e' && Key_IsDown(K_CTRL))) {
field->cursorPos = strlen(field->text);
goto check;
}
if (key == K_INS) {
Key_SetOverstrikeMode(Key_GetOverstrikeMode() ^ 1);
return qtrue;
}
return qfalse;
check:
if (field->cursorPos >= field->maxChars) {
field->cursorPos = field->maxChars - 1;
}
return qtrue;
}
/*
================
IF_CharEvent
================
*/
qboolean IF_CharEvent(inputField_t *field, int key)
{
if (!field->maxChars) {
return qfalse;
}
if (field->cursorPos >= field->maxChars) {
Com_Error(ERR_FATAL, "%s: bad cursorPos", __func__);
}
if (key < 32 || key > 127) {
return qfalse; // non printable
}
if (field->cursorPos == field->maxChars - 1) {
// buffer limit was reached, just replace the last character
field->text[field->cursorPos] = key;
return qtrue;
}
if (Key_GetOverstrikeMode()) {
// replace the character at cursor and advance
field->text[field->cursorPos++] = key;
return qtrue;
}
// insert new character at cursor position
memmove(field->text + field->cursorPos + 1,
field->text + field->cursorPos,
sizeof(field->text) - field->cursorPos - 1);
field->text[field->cursorPos++] = key;
return qtrue;
}
/*
================
IF_Draw
The input line scrolls horizontally if typing goes beyond the right edge.
Returns x offset of the rightmost character drawn.
================
*/
int IF_Draw(inputField_t *field, int x, int y, int flags, qhandle_t font)
{
char *text = field->text;
size_t cursorPos = field->cursorPos;
size_t offset = 0;
int ret;
if (!field->maxChars || !field->visibleChars) {
return 0;
}
if (cursorPos >= field->maxChars) {
Com_Error(ERR_FATAL, "%s: bad cursorPos", __func__);
}
// scroll horizontally
if (cursorPos >= field->visibleChars) {
cursorPos = field->visibleChars - 1;
offset = field->cursorPos - cursorPos;
}
// draw text
ret = R_DrawString(x, y, flags, field->visibleChars, text + offset, font);
if (flags & UI_DRAWCURSOR) {
// draw blinking cursor
if ((com_localTime >> 8) & 1) {
int c = Key_GetOverstrikeMode() ? 11 : '_';
R_DrawChar(x + cursorPos * CHAR_WIDTH, y, flags, c, font);
}
}
return ret;
}
#endif
|