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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
|
#include "config.h"
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#include <ctype.h>
#include <slang.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "newt.h"
#include "newt_pr.h"
struct entry {
int flags;
char * buf;
const char ** resultPtr;
int bufAlloced;
int bufUsed; /* amount of the buffer that's been used */
int cursorPosition; /* cursor *in the string* on on screen */
int firstChar; /* first character position being shown */
newtEntryFilter filter;
void * filterData;
int cs;
int csDisabled;
};
static int previous_char(const char *buf, int pos);
static int next_char(const char *buf, int pos);
static void entryDraw(newtComponent co);
static void entryDestroy(newtComponent co);
static struct eventResult entryEvent(newtComponent co,
struct event ev);
static struct eventResult entryHandleKey(newtComponent co, int key);
static struct componentOps entryOps = {
entryDraw,
entryEvent,
entryDestroy,
newtDefaultPlaceHandler,
newtDefaultMappedHandler,
} ;
void newtEntrySet(newtComponent co, const char * value, int cursorAtEnd) {
struct entry * en = co->data;
if ((strlen(value) + 1) > (unsigned int)en->bufAlloced) {
free(en->buf);
en->bufAlloced = strlen(value) + 1;
en->buf = malloc(en->bufAlloced);
if (en->resultPtr) *en->resultPtr = en->buf;
}
memset(en->buf, 0, en->bufAlloced); /* clear the buffer */
strcpy(en->buf, value);
en->bufUsed = strlen(value);
en->firstChar = 0;
if (cursorAtEnd)
en->cursorPosition = en->bufUsed;
else
en->cursorPosition = 0;
entryDraw(co);
} ;
newtComponent newtEntry(int left, int top, const char * initialValue, int width,
const char ** resultPtr, int flags) {
newtComponent co;
struct entry * en;
co = malloc(sizeof(*co));
en = malloc(sizeof(struct entry));
co->data = en;
co->top = top;
co->left = left;
co->height = 1;
co->width = width;
co->isMapped = 0;
co->callback = NULL;
co->destroyCallback = NULL;
co->ops = &entryOps;
en->flags = flags;
en->cursorPosition = 0;
en->firstChar = 0;
en->bufUsed = 0;
en->bufAlloced = width + 1;
en->filter = NULL;
if (!(en->flags & NEWT_FLAG_DISABLED))
co->takesFocus = 1;
else
co->takesFocus = 0;
if (initialValue && strlen(initialValue) > (unsigned int)width) {
en->bufAlloced = strlen(initialValue) + 1;
}
en->buf = malloc(en->bufAlloced);
en->resultPtr = resultPtr;
if (en->resultPtr) *en->resultPtr = en->buf;
memset(en->buf, 0, en->bufAlloced);
if (initialValue) {
strcpy(en->buf, initialValue);
en->bufUsed = strlen(initialValue);
en->cursorPosition = en->bufUsed;
/* move cursor back if entry is full */
if (en->cursorPosition && !(en->flags & NEWT_FLAG_SCROLL ||
wstrlen(en->buf, -1) < co->width))
en->cursorPosition = previous_char(en->buf, en->cursorPosition);
} else {
*en->buf = '\0';
en->bufUsed = 0;
en->cursorPosition = 0;
}
en->cs = NEWT_COLORSET_ENTRY;
en->csDisabled = NEWT_COLORSET_DISENTRY;
return co;
}
static void scroll(struct entry *en, int width)
{
int r, lv, rv, cntx, cw, cn, nc, pc, ncw, pcw;
if (width <= 1) {
en->firstChar = en->cursorPosition;
return;
}
cntx = width / 4;
if (cntx > 5)
cntx = 5;
if (en->cursorPosition < en->firstChar)
en->firstChar = en->cursorPosition;
cn = next_char(en->buf, en->cursorPosition);
cw = en->cursorPosition >= en->bufUsed ? 1 :
wstrlen(en->buf + en->cursorPosition, cn - en->cursorPosition);
r = wstrlen(en->buf + cn, -1);
lv = wstrlen(en->buf + en->firstChar, en->cursorPosition - en->firstChar);
rv = width - lv - cw;
#define RC (ncw > 0 && (r > rv && lv - ncw >= cntx && rv < cntx))
#define LC (pcw > 0 && (r + pcw <= rv || (lv < cntx && rv - pcw >= cntx)))
nc = next_char(en->buf, en->firstChar);
ncw = wstrlen(en->buf + en->firstChar, nc - en->firstChar);
if (RC) {
do {
lv -= ncw;
rv += ncw;
en->firstChar = nc;
nc = next_char(en->buf, en->firstChar);
ncw = wstrlen(en->buf + en->firstChar, nc - en->firstChar);
} while (RC);
return;
}
pc = previous_char(en->buf, en->firstChar);
pcw = wstrlen(en->buf + pc, en->firstChar - pc);
if (LC) {
do {
lv += pcw;
rv -= pcw;
en->firstChar = pc;
pc = previous_char(en->buf, en->firstChar);
pcw = wstrlen(en->buf + pc, en->firstChar - pc);
} while (LC);
}
}
static void entryDraw(newtComponent co) {
struct entry * en = co->data;
int i;
char * chptr;
int len;
char *tmpptr = NULL;
if (!co->isMapped) return;
if (en->flags & NEWT_FLAG_DISABLED)
SLsmg_set_color(en->csDisabled);
else
SLsmg_set_color(en->cs);
if (en->flags & NEWT_FLAG_HIDDEN) {
newtGotorc(co->top, co->left);
for (i = 0; i < co->width; i++)
SLsmg_write_char('_');
newtGotorc(co->top, co->left);
return;
}
newtTrashScreen();
/* scroll if necessary */
scroll(en, co->width);
chptr = en->buf + en->firstChar;
if (en->flags & NEWT_FLAG_PASSWORD) {
len = wstrlen(chptr, -1);
tmpptr = alloca(len + 1);
for (i = 0; i < len; i++)
memset(tmpptr, '*', len);
tmpptr[len] = '\0';
chptr = tmpptr;
}
len = wstrlen(chptr, -1);
/* workaround for double width characters */
if (co->width > 1) {
i = len < co->width ? len : co->width;
i = i > 2 ? i - 2 : 0;
newtGotorc(co->top, co->left + i);
SLsmg_write_char('_');
SLsmg_write_char('_');
}
newtGotorc(co->top, co->left);
if (len <= co->width) {
i = len;
/* BIDI: do not replaced, because it will not work.
* More work needed */
SLsmg_write_nstring(chptr, len);
while (i < co->width) {
SLsmg_write_char('_');
i++;
}
} else
/* BIDI: will not work for RTL text */
SLsmg_write_nstring(chptr, co->width);
newtGotorc(co->top, co->left + wstrlen(en->buf+en->firstChar, en->cursorPosition - en->firstChar));
}
void newtEntrySetFlags(newtComponent co, int flags, enum newtFlagsSense sense) {
struct entry * en = co->data;
int row, col;
en->flags = newtSetFlags(en->flags, flags, sense);
if (!(en->flags & NEWT_FLAG_DISABLED))
co->takesFocus = 1;
else
co->takesFocus = 0;
newtGetrc(&row, &col);
entryDraw(co);
newtGotorc(row, col);
}
void newtEntrySetColors(newtComponent co, int normal, int disabled) {
struct entry * en = co->data;
en->cs = normal;
en->csDisabled = disabled;
entryDraw(co);
}
static void entryDestroy(newtComponent co) {
struct entry * en = co->data;
free(en->buf);
free(en);
free(co);
}
static struct eventResult entryEvent(newtComponent co,
struct event ev) {
struct entry * en = co->data;
struct eventResult er;
int ch;
er.result = ER_IGNORED;
if (ev.when == EV_NORMAL) {
switch (ev.event) {
case EV_FOCUS:
newtCursorOn();
if (en->flags & NEWT_FLAG_HIDDEN)
newtGotorc(co->top, co->left);
else
newtGotorc(co->top, co->left +
wstrlen(en->buf + en->firstChar, en->cursorPosition - en->firstChar));
er.result = ER_SWALLOWED;
break;
case EV_UNFOCUS:
newtCursorOff();
newtGotorc(0, 0);
er.result = ER_SWALLOWED;
if (co->callback)
co->callback(co, co->callbackData);
break;
case EV_KEYPRESS:
ch = ev.u.key;
if (en->filter)
ch = en->filter(co, en->filterData, ch, en->cursorPosition);
if (ch) er = entryHandleKey(co, ch);
break;
case EV_MOUSE:
if ((ev.u.mouse.type == MOUSE_BUTTON_DOWN) &&
(en->flags ^ NEWT_FLAG_HIDDEN)) {
if (strlen(en->buf) >= ev.u.mouse.x - co->left) {
en->cursorPosition = ev.u.mouse.x - co->left;
newtGotorc(co->top,
co->left +(en->cursorPosition - en->firstChar));
} else {
en->cursorPosition = strlen(en->buf);
newtGotorc(co->top,
co->left +(en->cursorPosition - en->firstChar));
}
}
break;
}
}
return er;
}
static int previous_char(const char *buf, int pos)
{
int len = 0;
int off = 0;
while (off < pos) {
len = mblen(buf+off, MB_CUR_MAX);
if (len <= 0)
return pos;
off+=len;
}
return off-len;
}
static int next_char(const char *buf, int pos)
{
int len = mblen(buf + pos, MB_CUR_MAX);
if (len <= 0)
return pos;
return pos+len;
}
static struct eventResult entryHandleKey(newtComponent co, int key) {
struct entry * en = co->data;
struct eventResult er;
char * chptr;
er.result = ER_SWALLOWED;
switch (key) {
case '\r': /* Return */
if (en->flags & NEWT_FLAG_RETURNEXIT) {
newtCursorOff();
er.result = ER_EXITFORM;
} else {
er.result = ER_NEXTCOMP;
}
break;
case '\001': /* ^A */
case NEWT_KEY_HOME:
en->cursorPosition = 0;
break;
case '\005': /* ^E */
case NEWT_KEY_END:
en->cursorPosition = en->bufUsed;
break;
case '\013': /* ^K */
en->bufUsed = en->cursorPosition;
memset(en->buf + en->bufUsed, 0, en->bufAlloced - en->bufUsed);
break;
case '\025': /* ^U */
en->bufUsed -= en->cursorPosition;
memmove(en->buf, en->buf + en->cursorPosition, en->bufUsed);
en->cursorPosition = 0;
memset(en->buf + en->bufUsed, 0, en->bufAlloced - en->bufUsed);
break;
case '\002': /* ^B */
case NEWT_KEY_LEFT:
if (en->cursorPosition)
en->cursorPosition = previous_char(en->buf, en->cursorPosition);
break;
case '\004':
case NEWT_KEY_DELETE:
chptr = en->buf + en->cursorPosition;
if (*chptr) {
int delta = next_char(en->buf, en->cursorPosition)-en->cursorPosition;
if (delta) {
chptr+=delta;
while (*chptr) {
*(chptr - delta) = *chptr;
chptr++;
}
memset(chptr - delta, 0, delta);
en->bufUsed-=delta;
}
}
break;
case NEWT_KEY_BKSPC: {
int prev = previous_char(en->buf, en->cursorPosition);
if (en->cursorPosition != prev) {
/* if this isn't true, there's nothing to erase */
int delta = en->cursorPosition - prev;
chptr = en->buf + en->cursorPosition;
en->bufUsed-=delta;
en->cursorPosition-=delta;
while (*chptr) {
*(chptr - delta) = *chptr;
chptr++;
}
memset(chptr - delta, 0, delta);
}
}
break;
case '\006': /* ^B */
case NEWT_KEY_RIGHT:
if (en->cursorPosition < en->bufUsed)
en->cursorPosition = next_char(en->buf, en->cursorPosition);
break;
default:
if ((key >= 0x20 && key <= 0x7e) || (key >= 0x80 && key <= 0xff)) {
char s[MB_CUR_MAX];
mbstate_t ps;
int i, l;
for (i = 1, s[0] = key; ; i++) {
memset(&ps, 0, sizeof (ps));
l = mbrtowc(NULL, s, i, &ps);
if (l == -1) /* invalid sequence */
i = 0;
if (l != -2) /* not incomplete sequence */
break;
/* read next byte */
if (i == MB_CUR_MAX || !SLang_input_pending(1)) {
i = 0;
break;
}
s[i] = SLang_getkey();
}
if (!i || (!(en->flags & NEWT_FLAG_SCROLL) && wstrlen(en->buf, -1) + wstrlen(s, i) > co->width)) {
/* FIXME this is broken */
SLtt_beep();
break;
}
if ((en->bufUsed + i) >= en->bufAlloced) {
en->bufAlloced += 20;
en->buf = realloc(en->buf, en->bufAlloced);
if (en->resultPtr) *en->resultPtr = en->buf;
memset(en->buf + en->bufAlloced - 20, 0, 20);
}
if (en->cursorPosition != en->bufUsed) {
/* insert the new character */
memmove(en->buf + en->cursorPosition + i, en->buf + en->cursorPosition, en->bufUsed - en->cursorPosition);
}
en->bufUsed += i;
for (l = 0; l < i; l++)
en->buf[en->cursorPosition++] = s[l];
} else {
er.result = ER_IGNORED;
}
}
if (en->cursorPosition == en->bufUsed && en->cursorPosition &&
!(en->flags & NEWT_FLAG_SCROLL || wstrlen(en->buf, -1) < co->width))
en->cursorPosition = previous_char(en->buf, en->cursorPosition);
entryDraw(co);
return er;
}
char * newtEntryGetValue(newtComponent co) {
struct entry * en = co->data;
return en->buf;
}
void newtEntrySetFilter(newtComponent co, newtEntryFilter filter, void * data) {
struct entry * en = co->data;
en->filter = filter;
en->filterData = data;
}
int newtEntryGetCursorPosition (newtComponent co) {
struct entry * en = co->data;
return en->cursorPosition;
}
void newtEntrySetCursorPosition (newtComponent co, int position) {
struct entry * en = co->data;
en->cursorPosition = position;
}
|