| 9120e874 Andy McCall |
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 |
#ifndef INCLUDE_IMSTB_TEXTEDIT_H |
| 287 |
#define INCLUDE_IMSTB_TEXTEDIT_H |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
#ifndef IMSTB_TEXTEDIT_UNDOSTATECOUNT |
| 299 |
#define IMSTB_TEXTEDIT_UNDOSTATECOUNT 99 |
| 300 |
#endif |
| 301 |
#ifndef IMSTB_TEXTEDIT_UNDOCHARCOUNT |
| 302 |
#define IMSTB_TEXTEDIT_UNDOCHARCOUNT 999 |
| 303 |
#endif |
| 304 |
#ifndef IMSTB_TEXTEDIT_CHARTYPE |
| 305 |
#define IMSTB_TEXTEDIT_CHARTYPE int |
| 306 |
#endif |
| 307 |
#ifndef IMSTB_TEXTEDIT_POSITIONTYPE |
| 308 |
#define IMSTB_TEXTEDIT_POSITIONTYPE int |
| 309 |
#endif |
| 310 |
|
| 311 |
typedef struct |
| 312 |
{ |
| 313 |
|
| 314 |
IMSTB_TEXTEDIT_POSITIONTYPE where; |
| 315 |
IMSTB_TEXTEDIT_POSITIONTYPE insert_length; |
| 316 |
IMSTB_TEXTEDIT_POSITIONTYPE delete_length; |
| 317 |
int char_storage; |
| 318 |
} StbUndoRecord; |
| 319 |
|
| 320 |
typedef struct |
| 321 |
{ |
| 322 |
|
| 323 |
StbUndoRecord undo_rec [IMSTB_TEXTEDIT_UNDOSTATECOUNT]; |
| 324 |
IMSTB_TEXTEDIT_CHARTYPE undo_char[IMSTB_TEXTEDIT_UNDOCHARCOUNT]; |
| 325 |
short undo_point, redo_point; |
| 326 |
int undo_char_point, redo_char_point; |
| 327 |
} StbUndoState; |
| 328 |
|
| 329 |
typedef struct STB_TexteditState |
| 330 |
{ |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
int cursor; |
| 337 |
|
| 338 |
|
| 339 |
int select_start; |
| 340 |
int select_end; |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
unsigned char insert_mode; |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
int row_count_per_page; |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
unsigned char cursor_at_end_of_line; |
| 359 |
unsigned char initialized; |
| 360 |
unsigned char has_preferred_x; |
| 361 |
unsigned char single_line; |
| 362 |
unsigned char padding1, padding2, padding3; |
| 363 |
float preferred_x; |
| 364 |
StbUndoState undostate; |
| 365 |
} STB_TexteditState; |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
typedef struct |
| 377 |
{ |
| 378 |
float x0,x1; |
| 379 |
float baseline_y_delta; |
| 380 |
float ymin,ymax; |
| 381 |
int num_chars; |
| 382 |
} StbTexteditRow; |
| 383 |
#endif |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
#ifdef IMSTB_TEXTEDIT_IMPLEMENTATION |
| 397 |
|
| 398 |
#ifndef IMSTB_TEXTEDIT_memmove |
| 399 |
#include <string.h> |
| 400 |
#define IMSTB_TEXTEDIT_memmove memmove |
| 401 |
#endif |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
static int stb_text_locate_coord(IMSTB_TEXTEDIT_STRING *str, float x, float y) |
| 411 |
{ |
| 412 |
StbTexteditRow r; |
| 413 |
int n = STB_TEXTEDIT_STRINGLEN(str); |
| 414 |
float base_y = 0, prev_x; |
| 415 |
int i=0, k; |
| 416 |
|
| 417 |
r.x0 = r.x1 = 0; |
| 418 |
r.ymin = r.ymax = 0; |
| 419 |
r.num_chars = 0; |
| 420 |
|
| 421 |
|
| 422 |
while (i < n) { |
| 423 |
STB_TEXTEDIT_LAYOUTROW(&r, str, i); |
| 424 |
if (r.num_chars <= 0) |
| 425 |
return n; |
| 426 |
|
| 427 |
if (i==0 && y < base_y + r.ymin) |
| 428 |
return 0; |
| 429 |
|
| 430 |
if (y < base_y + r.ymax) |
| 431 |
break; |
| 432 |
|
| 433 |
i += r.num_chars; |
| 434 |
base_y += r.baseline_y_delta; |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
if (i >= n) |
| 439 |
return n; |
| 440 |
|
| 441 |
|
| 442 |
if (x < r.x0) |
| 443 |
return i; |
| 444 |
|
| 445 |
|
| 446 |
if (x < r.x1) { |
| 447 |
|
| 448 |
prev_x = r.x0; |
| 449 |
for (k=0; k < r.num_chars; k = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, i + k) - i) { |
| 450 |
float w = STB_TEXTEDIT_GETWIDTH(str, i, k); |
| 451 |
if (x < prev_x+w) { |
| 452 |
if (x < prev_x+w/2) |
| 453 |
return k+i; |
| 454 |
else |
| 455 |
return IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, i + k); |
| 456 |
} |
| 457 |
prev_x += w; |
| 458 |
} |
| 459 |
|
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
if (STB_TEXTEDIT_GETCHAR(str, i+r.num_chars-1) == STB_TEXTEDIT_NEWLINE) |
| 464 |
return i+r.num_chars-1; |
| 465 |
else |
| 466 |
return i+r.num_chars; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
static void stb_textedit_click(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) |
| 471 |
{ |
| 472 |
|
| 473 |
|
| 474 |
if( state->single_line ) |
| 475 |
{ |
| 476 |
StbTexteditRow r; |
| 477 |
STB_TEXTEDIT_LAYOUTROW(&r, str, 0); |
| 478 |
y = r.ymin; |
| 479 |
} |
| 480 |
|
| 481 |
state->cursor = stb_text_locate_coord(str, x, y); |
| 482 |
state->select_start = state->cursor; |
| 483 |
state->select_end = state->cursor; |
| 484 |
state->has_preferred_x = 0; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
static void stb_textedit_drag(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) |
| 489 |
{ |
| 490 |
int p = 0; |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
if( state->single_line ) |
| 495 |
{ |
| 496 |
StbTexteditRow r; |
| 497 |
STB_TEXTEDIT_LAYOUTROW(&r, str, 0); |
| 498 |
y = r.ymin; |
| 499 |
} |
| 500 |
|
| 501 |
if (state->select_start == state->select_end) |
| 502 |
state->select_start = state->cursor; |
| 503 |
|
| 504 |
p = stb_text_locate_coord(str, x, y); |
| 505 |
state->cursor = state->select_end = p; |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
static void stb_text_undo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state); |
| 515 |
static void stb_text_redo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state); |
| 516 |
static void stb_text_makeundo_delete(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length); |
| 517 |
static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length); |
| 518 |
static void stb_text_makeundo_replace(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length); |
| 519 |
|
| 520 |
typedef struct |
| 521 |
{ |
| 522 |
float x,y; |
| 523 |
float height; |
| 524 |
int first_char, length; |
| 525 |
int prev_first; |
| 526 |
} StbFindState; |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
static void stb_textedit_find_charpos(StbFindState *find, IMSTB_TEXTEDIT_STRING *str, int n, int single_line) |
| 531 |
{ |
| 532 |
StbTexteditRow r; |
| 533 |
int prev_start = 0; |
| 534 |
int z = STB_TEXTEDIT_STRINGLEN(str); |
| 535 |
int i=0, first; |
| 536 |
|
| 537 |
if (n == z && single_line) { |
| 538 |
|
| 539 |
STB_TEXTEDIT_LAYOUTROW(&r, str, 0); |
| 540 |
find->y = 0; |
| 541 |
find->first_char = 0; |
| 542 |
find->length = z; |
| 543 |
find->height = r.ymax - r.ymin; |
| 544 |
find->x = r.x1; |
| 545 |
return; |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
find->y = 0; |
| 550 |
|
| 551 |
for(;;) { |
| 552 |
STB_TEXTEDIT_LAYOUTROW(&r, str, i); |
| 553 |
if (n < i + r.num_chars) |
| 554 |
break; |
| 555 |
if (i + r.num_chars == z && z > 0 && STB_TEXTEDIT_GETCHAR(str, z - 1) != STB_TEXTEDIT_NEWLINE) |
| 556 |
break; |
| 557 |
prev_start = i; |
| 558 |
i += r.num_chars; |
| 559 |
find->y += r.baseline_y_delta; |
| 560 |
if (i == z) |
| 561 |
{ |
| 562 |
r.num_chars = 0; |
| 563 |
break; |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
find->first_char = first = i; |
| 568 |
find->length = r.num_chars; |
| 569 |
find->height = r.ymax - r.ymin; |
| 570 |
find->prev_first = prev_start; |
| 571 |
|
| 572 |
|
| 573 |
find->x = r.x0; |
| 574 |
for (i=0; first+i < n; i = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, first + i) - first) |
| 575 |
find->x += STB_TEXTEDIT_GETWIDTH(str, first, i); |
| 576 |
} |
| 577 |
|
| 578 |
#define STB_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end) |
| 579 |
|
| 580 |
|
| 581 |
static void stb_textedit_clamp(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state) |
| 582 |
{ |
| 583 |
int n = STB_TEXTEDIT_STRINGLEN(str); |
| 584 |
if (STB_TEXT_HAS_SELECTION(state)) { |
| 585 |
if (state->select_start > n) state->select_start = n; |
| 586 |
if (state->select_end > n) state->select_end = n; |
| 587 |
|
| 588 |
if (state->select_start == state->select_end) |
| 589 |
state->cursor = state->select_start; |
| 590 |
} |
| 591 |
if (state->cursor > n) state->cursor = n; |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
static void stb_textedit_delete(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int len) |
| 596 |
{ |
| 597 |
stb_text_makeundo_delete(str, state, where, len); |
| 598 |
STB_TEXTEDIT_DELETECHARS(str, where, len); |
| 599 |
state->has_preferred_x = 0; |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
static void stb_textedit_delete_selection(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state) |
| 604 |
{ |
| 605 |
stb_textedit_clamp(str, state); |
| 606 |
if (STB_TEXT_HAS_SELECTION(state)) { |
| 607 |
if (state->select_start < state->select_end) { |
| 608 |
stb_textedit_delete(str, state, state->select_start, state->select_end - state->select_start); |
| 609 |
state->select_end = state->cursor = state->select_start; |
| 610 |
} else { |
| 611 |
stb_textedit_delete(str, state, state->select_end, state->select_start - state->select_end); |
| 612 |
state->select_start = state->cursor = state->select_end; |
| 613 |
} |
| 614 |
state->has_preferred_x = 0; |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
static void stb_textedit_sortselection(STB_TexteditState *state) |
| 620 |
{ |
| 621 |
if (state->select_end < state->select_start) { |
| 622 |
int temp = state->select_end; |
| 623 |
state->select_end = state->select_start; |
| 624 |
state->select_start = temp; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
static void stb_textedit_move_to_first(STB_TexteditState *state) |
| 630 |
{ |
| 631 |
if (STB_TEXT_HAS_SELECTION(state)) { |
| 632 |
stb_textedit_sortselection(state); |
| 633 |
state->cursor = state->select_start; |
| 634 |
state->select_end = state->select_start; |
| 635 |
state->has_preferred_x = 0; |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
static void stb_textedit_move_to_last(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state) |
| 641 |
{ |
| 642 |
if (STB_TEXT_HAS_SELECTION(state)) { |
| 643 |
stb_textedit_sortselection(state); |
| 644 |
stb_textedit_clamp(str, state); |
| 645 |
state->cursor = state->select_end; |
| 646 |
state->select_start = state->select_end; |
| 647 |
state->has_preferred_x = 0; |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
#ifndef IMSTB_TEXTEDIT_GETPREVCHARINDEX |
| 656 |
#define IMSTB_TEXTEDIT_GETPREVCHARINDEX(obj, idx) (idx - 1) |
| 657 |
#endif |
| 658 |
#ifndef IMSTB_TEXTEDIT_GETNEXTCHARINDEX |
| 659 |
#define IMSTB_TEXTEDIT_GETNEXTCHARINDEX(obj, idx) (idx + 1) |
| 660 |
#endif |
| 661 |
|
| 662 |
#ifdef STB_TEXTEDIT_IS_SPACE |
| 663 |
static int is_word_boundary( IMSTB_TEXTEDIT_STRING *str, int idx ) |
| 664 |
{ |
| 665 |
return idx > 0 ? (STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str,idx-1) ) && !STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str, idx) ) ) : 1; |
| 666 |
} |
| 667 |
|
| 668 |
#ifndef STB_TEXTEDIT_MOVEWORDLEFT |
| 669 |
static int stb_textedit_move_to_word_previous( IMSTB_TEXTEDIT_STRING *str, int c ) |
| 670 |
{ |
| 671 |
--c; |
| 672 |
while( c >= 0 && !is_word_boundary( str, c ) ) |
| 673 |
--c; |
| 674 |
|
| 675 |
if( c < 0 ) |
| 676 |
c = 0; |
| 677 |
|
| 678 |
return c; |
| 679 |
} |
| 680 |
#define STB_TEXTEDIT_MOVEWORDLEFT stb_textedit_move_to_word_previous |
| 681 |
#endif |
| 682 |
|
| 683 |
#ifndef STB_TEXTEDIT_MOVEWORDRIGHT |
| 684 |
static int stb_textedit_move_to_word_next( IMSTB_TEXTEDIT_STRING *str, int c ) |
| 685 |
{ |
| 686 |
const int len = STB_TEXTEDIT_STRINGLEN(str); |
| 687 |
++c; |
| 688 |
while( c < len && !is_word_boundary( str, c ) ) |
| 689 |
++c; |
| 690 |
|
| 691 |
if( c > len ) |
| 692 |
c = len; |
| 693 |
|
| 694 |
return c; |
| 695 |
} |
| 696 |
#define STB_TEXTEDIT_MOVEWORDRIGHT stb_textedit_move_to_word_next |
| 697 |
#endif |
| 698 |
|
| 699 |
#endif |
| 700 |
|
| 701 |
|
| 702 |
static void stb_textedit_prep_selection_at_cursor(STB_TexteditState *state) |
| 703 |
{ |
| 704 |
if (!STB_TEXT_HAS_SELECTION(state)) |
| 705 |
state->select_start = state->select_end = state->cursor; |
| 706 |
else |
| 707 |
state->cursor = state->select_end; |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
static int stb_textedit_cut(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state) |
| 712 |
{ |
| 713 |
if (STB_TEXT_HAS_SELECTION(state)) { |
| 714 |
stb_textedit_delete_selection(str,state); |
| 715 |
state->has_preferred_x = 0; |
| 716 |
return 1; |
| 717 |
} |
| 718 |
return 0; |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
static int stb_textedit_paste_internal(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, IMSTB_TEXTEDIT_CHARTYPE *text, int len) |
| 723 |
{ |
| 724 |
|
| 725 |
stb_textedit_clamp(str, state); |
| 726 |
stb_textedit_delete_selection(str,state); |
| 727 |
|
| 728 |
if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, len)) { |
| 729 |
stb_text_makeundo_insert(state, state->cursor, len); |
| 730 |
state->cursor += len; |
| 731 |
state->has_preferred_x = 0; |
| 732 |
return 1; |
| 733 |
} |
| 734 |
|
| 735 |
return 0; |
| 736 |
} |
| 737 |
|
| 738 |
#ifndef STB_TEXTEDIT_KEYTYPE |
| 739 |
#define STB_TEXTEDIT_KEYTYPE int |
| 740 |
#endif |
| 741 |
|
| 742 |
|
| 743 |
static void stb_textedit_text(IMSTB_TEXTEDIT_STRING* str, STB_TexteditState* state, const IMSTB_TEXTEDIT_CHARTYPE* text, int text_len) |
| 744 |
{ |
| 745 |
|
| 746 |
if (text[0] == '\n' && state->single_line) |
| 747 |
return; |
| 748 |
|
| 749 |
if (state->insert_mode && !STB_TEXT_HAS_SELECTION(state) && state->cursor < STB_TEXTEDIT_STRINGLEN(str)) { |
| 750 |
stb_text_makeundo_replace(str, state, state->cursor, 1, 1); |
| 751 |
STB_TEXTEDIT_DELETECHARS(str, state->cursor, 1); |
| 752 |
if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, text_len)) { |
| 753 |
state->cursor += text_len; |
| 754 |
state->has_preferred_x = 0; |
| 755 |
} |
| 756 |
} |
| 757 |
else { |
| 758 |
stb_textedit_delete_selection(str, state); |
| 759 |
if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, text_len)) { |
| 760 |
stb_text_makeundo_insert(state, state->cursor, text_len); |
| 761 |
state->cursor += text_len; |
| 762 |
state->has_preferred_x = 0; |
| 763 |
} |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
static void stb_textedit_key(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_KEYTYPE key) |
| 769 |
{ |
| 770 |
retry: |
| 771 |
switch (key) { |
| 772 |
default: { |
| 773 |
#ifdef STB_TEXTEDIT_KEYTOTEXT |
| 774 |
int c = STB_TEXTEDIT_KEYTOTEXT(key); |
| 775 |
if (c > 0) { |
| 776 |
IMSTB_TEXTEDIT_CHARTYPE ch = (IMSTB_TEXTEDIT_CHARTYPE)c; |
| 777 |
stb_textedit_text(str, state, &ch, 1); |
| 778 |
} |
| 779 |
#endif |
| 780 |
break; |
| 781 |
} |
| 782 |
|
| 783 |
#ifdef STB_TEXTEDIT_K_INSERT |
| 784 |
case STB_TEXTEDIT_K_INSERT: |
| 785 |
state->insert_mode = !state->insert_mode; |
| 786 |
break; |
| 787 |
#endif |
| 788 |
|
| 789 |
case STB_TEXTEDIT_K_UNDO: |
| 790 |
stb_text_undo(str, state); |
| 791 |
state->has_preferred_x = 0; |
| 792 |
break; |
| 793 |
|
| 794 |
case STB_TEXTEDIT_K_REDO: |
| 795 |
stb_text_redo(str, state); |
| 796 |
state->has_preferred_x = 0; |
| 797 |
break; |
| 798 |
|
| 799 |
case STB_TEXTEDIT_K_LEFT: |
| 800 |
|
| 801 |
if (STB_TEXT_HAS_SELECTION(state)) |
| 802 |
stb_textedit_move_to_first(state); |
| 803 |
else |
| 804 |
if (state->cursor > 0) |
| 805 |
state->cursor = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, state->cursor); |
| 806 |
state->has_preferred_x = 0; |
| 807 |
break; |
| 808 |
|
| 809 |
case STB_TEXTEDIT_K_RIGHT: |
| 810 |
|
| 811 |
if (STB_TEXT_HAS_SELECTION(state)) |
| 812 |
stb_textedit_move_to_last(str, state); |
| 813 |
else |
| 814 |
state->cursor = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor); |
| 815 |
stb_textedit_clamp(str, state); |
| 816 |
state->has_preferred_x = 0; |
| 817 |
break; |
| 818 |
|
| 819 |
case STB_TEXTEDIT_K_LEFT | STB_TEXTEDIT_K_SHIFT: |
| 820 |
stb_textedit_clamp(str, state); |
| 821 |
stb_textedit_prep_selection_at_cursor(state); |
| 822 |
|
| 823 |
if (state->select_end > 0) |
| 824 |
state->select_end = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, state->select_end); |
| 825 |
state->cursor = state->select_end; |
| 826 |
state->has_preferred_x = 0; |
| 827 |
break; |
| 828 |
|
| 829 |
#ifdef STB_TEXTEDIT_MOVEWORDLEFT |
| 830 |
case STB_TEXTEDIT_K_WORDLEFT: |
| 831 |
if (STB_TEXT_HAS_SELECTION(state)) |
| 832 |
stb_textedit_move_to_first(state); |
| 833 |
else { |
| 834 |
state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor); |
| 835 |
stb_textedit_clamp( str, state ); |
| 836 |
} |
| 837 |
break; |
| 838 |
|
| 839 |
case STB_TEXTEDIT_K_WORDLEFT | STB_TEXTEDIT_K_SHIFT: |
| 840 |
if( !STB_TEXT_HAS_SELECTION( state ) ) |
| 841 |
stb_textedit_prep_selection_at_cursor(state); |
| 842 |
|
| 843 |
state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor); |
| 844 |
state->select_end = state->cursor; |
| 845 |
|
| 846 |
stb_textedit_clamp( str, state ); |
| 847 |
break; |
| 848 |
#endif |
| 849 |
|
| 850 |
#ifdef STB_TEXTEDIT_MOVEWORDRIGHT |
| 851 |
case STB_TEXTEDIT_K_WORDRIGHT: |
| 852 |
if (STB_TEXT_HAS_SELECTION(state)) |
| 853 |
stb_textedit_move_to_last(str, state); |
| 854 |
else { |
| 855 |
state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor); |
| 856 |
stb_textedit_clamp( str, state ); |
| 857 |
} |
| 858 |
break; |
| 859 |
|
| 860 |
case STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT: |
| 861 |
if( !STB_TEXT_HAS_SELECTION( state ) ) |
| 862 |
stb_textedit_prep_selection_at_cursor(state); |
| 863 |
|
| 864 |
state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor); |
| 865 |
state->select_end = state->cursor; |
| 866 |
|
| 867 |
stb_textedit_clamp( str, state ); |
| 868 |
break; |
| 869 |
#endif |
| 870 |
|
| 871 |
case STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT: |
| 872 |
stb_textedit_prep_selection_at_cursor(state); |
| 873 |
|
| 874 |
state->select_end = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->select_end); |
| 875 |
stb_textedit_clamp(str, state); |
| 876 |
state->cursor = state->select_end; |
| 877 |
state->has_preferred_x = 0; |
| 878 |
break; |
| 879 |
|
| 880 |
case STB_TEXTEDIT_K_DOWN: |
| 881 |
case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT: |
| 882 |
case STB_TEXTEDIT_K_PGDOWN: |
| 883 |
case STB_TEXTEDIT_K_PGDOWN | STB_TEXTEDIT_K_SHIFT: { |
| 884 |
StbFindState find; |
| 885 |
StbTexteditRow row; |
| 886 |
int i, j, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0; |
| 887 |
int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGDOWN; |
| 888 |
int row_count = is_page ? state->row_count_per_page : 1; |
| 889 |
|
| 890 |
if (!is_page && state->single_line) { |
| 891 |
|
| 892 |
key = STB_TEXTEDIT_K_RIGHT | (key & STB_TEXTEDIT_K_SHIFT); |
| 893 |
goto retry; |
| 894 |
} |
| 895 |
|
| 896 |
if (sel) |
| 897 |
stb_textedit_prep_selection_at_cursor(state); |
| 898 |
else if (STB_TEXT_HAS_SELECTION(state)) |
| 899 |
stb_textedit_move_to_last(str, state); |
| 900 |
|
| 901 |
|
| 902 |
stb_textedit_clamp(str, state); |
| 903 |
stb_textedit_find_charpos(&find, str, state->cursor, state->single_line); |
| 904 |
|
| 905 |
for (j = 0; j < row_count; ++j) { |
| 906 |
float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x; |
| 907 |
int start = find.first_char + find.length; |
| 908 |
|
| 909 |
if (find.length == 0) |
| 910 |
break; |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
if (STB_TEXTEDIT_GETCHAR(str, find.first_char + find.length - 1) != STB_TEXTEDIT_NEWLINE) |
| 915 |
break; |
| 916 |
|
| 917 |
|
| 918 |
state->cursor = start; |
| 919 |
STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor); |
| 920 |
x = row.x0; |
| 921 |
for (i=0; i < row.num_chars; ++i) { |
| 922 |
float dx = STB_TEXTEDIT_GETWIDTH(str, start, i); |
| 923 |
#ifdef IMSTB_TEXTEDIT_GETWIDTH_NEWLINE |
| 924 |
if (dx == IMSTB_TEXTEDIT_GETWIDTH_NEWLINE) |
| 925 |
break; |
| 926 |
#endif |
| 927 |
x += dx; |
| 928 |
if (x > goal_x) |
| 929 |
break; |
| 930 |
state->cursor = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor); |
| 931 |
} |
| 932 |
stb_textedit_clamp(str, state); |
| 933 |
|
| 934 |
state->has_preferred_x = 1; |
| 935 |
state->preferred_x = goal_x; |
| 936 |
|
| 937 |
if (sel) |
| 938 |
state->select_end = state->cursor; |
| 939 |
|
| 940 |
|
| 941 |
find.first_char = find.first_char + find.length; |
| 942 |
find.length = row.num_chars; |
| 943 |
} |
| 944 |
break; |
| 945 |
} |
| 946 |
|
| 947 |
case STB_TEXTEDIT_K_UP: |
| 948 |
case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT: |
| 949 |
case STB_TEXTEDIT_K_PGUP: |
| 950 |
case STB_TEXTEDIT_K_PGUP | STB_TEXTEDIT_K_SHIFT: { |
| 951 |
StbFindState find; |
| 952 |
StbTexteditRow row; |
| 953 |
int i, j, prev_scan, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0; |
| 954 |
int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGUP; |
| 955 |
int row_count = is_page ? state->row_count_per_page : 1; |
| 956 |
|
| 957 |
if (!is_page && state->single_line) { |
| 958 |
|
| 959 |
key = STB_TEXTEDIT_K_LEFT | (key & STB_TEXTEDIT_K_SHIFT); |
| 960 |
goto retry; |
| 961 |
} |
| 962 |
|
| 963 |
if (sel) |
| 964 |
stb_textedit_prep_selection_at_cursor(state); |
| 965 |
else if (STB_TEXT_HAS_SELECTION(state)) |
| 966 |
stb_textedit_move_to_first(state); |
| 967 |
|
| 968 |
|
| 969 |
stb_textedit_clamp(str, state); |
| 970 |
stb_textedit_find_charpos(&find, str, state->cursor, state->single_line); |
| 971 |
|
| 972 |
for (j = 0; j < row_count; ++j) { |
| 973 |
float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x; |
| 974 |
|
| 975 |
|
| 976 |
if (find.prev_first == find.first_char) |
| 977 |
break; |
| 978 |
|
| 979 |
|
| 980 |
state->cursor = find.prev_first; |
| 981 |
STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor); |
| 982 |
x = row.x0; |
| 983 |
for (i=0; i < row.num_chars; ++i) { |
| 984 |
float dx = STB_TEXTEDIT_GETWIDTH(str, find.prev_first, i); |
| 985 |
#ifdef IMSTB_TEXTEDIT_GETWIDTH_NEWLINE |
| 986 |
if (dx == IMSTB_TEXTEDIT_GETWIDTH_NEWLINE) |
| 987 |
break; |
| 988 |
#endif |
| 989 |
x += dx; |
| 990 |
if (x > goal_x) |
| 991 |
break; |
| 992 |
state->cursor = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor); |
| 993 |
} |
| 994 |
stb_textedit_clamp(str, state); |
| 995 |
|
| 996 |
state->has_preferred_x = 1; |
| 997 |
state->preferred_x = goal_x; |
| 998 |
|
| 999 |
if (sel) |
| 1000 |
state->select_end = state->cursor; |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
prev_scan = find.prev_first > 0 ? find.prev_first - 1 : 0; |
| 1005 |
while (prev_scan > 0 && STB_TEXTEDIT_GETCHAR(str, prev_scan - 1) != STB_TEXTEDIT_NEWLINE) |
| 1006 |
--prev_scan; |
| 1007 |
find.first_char = find.prev_first; |
| 1008 |
find.prev_first = prev_scan; |
| 1009 |
} |
| 1010 |
break; |
| 1011 |
} |
| 1012 |
|
| 1013 |
case STB_TEXTEDIT_K_DELETE: |
| 1014 |
case STB_TEXTEDIT_K_DELETE | STB_TEXTEDIT_K_SHIFT: |
| 1015 |
if (STB_TEXT_HAS_SELECTION(state)) |
| 1016 |
stb_textedit_delete_selection(str, state); |
| 1017 |
else { |
| 1018 |
int n = STB_TEXTEDIT_STRINGLEN(str); |
| 1019 |
if (state->cursor < n) |
| 1020 |
stb_textedit_delete(str, state, state->cursor, IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor) - state->cursor); |
| 1021 |
} |
| 1022 |
state->has_preferred_x = 0; |
| 1023 |
break; |
| 1024 |
|
| 1025 |
case STB_TEXTEDIT_K_BACKSPACE: |
| 1026 |
case STB_TEXTEDIT_K_BACKSPACE | STB_TEXTEDIT_K_SHIFT: |
| 1027 |
if (STB_TEXT_HAS_SELECTION(state)) |
| 1028 |
stb_textedit_delete_selection(str, state); |
| 1029 |
else { |
| 1030 |
stb_textedit_clamp(str, state); |
| 1031 |
if (state->cursor > 0) { |
| 1032 |
int prev = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, state->cursor); |
| 1033 |
stb_textedit_delete(str, state, prev, state->cursor - prev); |
| 1034 |
state->cursor = prev; |
| 1035 |
} |
| 1036 |
} |
| 1037 |
state->has_preferred_x = 0; |
| 1038 |
break; |
| 1039 |
|
| 1040 |
#ifdef STB_TEXTEDIT_K_TEXTSTART2 |
| 1041 |
case STB_TEXTEDIT_K_TEXTSTART2: |
| 1042 |
#endif |
| 1043 |
case STB_TEXTEDIT_K_TEXTSTART: |
| 1044 |
state->cursor = state->select_start = state->select_end = 0; |
| 1045 |
state->has_preferred_x = 0; |
| 1046 |
break; |
| 1047 |
|
| 1048 |
#ifdef STB_TEXTEDIT_K_TEXTEND2 |
| 1049 |
case STB_TEXTEDIT_K_TEXTEND2: |
| 1050 |
#endif |
| 1051 |
case STB_TEXTEDIT_K_TEXTEND: |
| 1052 |
state->cursor = STB_TEXTEDIT_STRINGLEN(str); |
| 1053 |
state->select_start = state->select_end = 0; |
| 1054 |
state->has_preferred_x = 0; |
| 1055 |
break; |
| 1056 |
|
| 1057 |
#ifdef STB_TEXTEDIT_K_TEXTSTART2 |
| 1058 |
case STB_TEXTEDIT_K_TEXTSTART2 | STB_TEXTEDIT_K_SHIFT: |
| 1059 |
#endif |
| 1060 |
case STB_TEXTEDIT_K_TEXTSTART | STB_TEXTEDIT_K_SHIFT: |
| 1061 |
stb_textedit_prep_selection_at_cursor(state); |
| 1062 |
state->cursor = state->select_end = 0; |
| 1063 |
state->has_preferred_x = 0; |
| 1064 |
break; |
| 1065 |
|
| 1066 |
#ifdef STB_TEXTEDIT_K_TEXTEND2 |
| 1067 |
case STB_TEXTEDIT_K_TEXTEND2 | STB_TEXTEDIT_K_SHIFT: |
| 1068 |
#endif |
| 1069 |
case STB_TEXTEDIT_K_TEXTEND | STB_TEXTEDIT_K_SHIFT: |
| 1070 |
stb_textedit_prep_selection_at_cursor(state); |
| 1071 |
state->cursor = state->select_end = STB_TEXTEDIT_STRINGLEN(str); |
| 1072 |
state->has_preferred_x = 0; |
| 1073 |
break; |
| 1074 |
|
| 1075 |
|
| 1076 |
#ifdef STB_TEXTEDIT_K_LINESTART2 |
| 1077 |
case STB_TEXTEDIT_K_LINESTART2: |
| 1078 |
#endif |
| 1079 |
case STB_TEXTEDIT_K_LINESTART: |
| 1080 |
stb_textedit_clamp(str, state); |
| 1081 |
stb_textedit_move_to_first(state); |
| 1082 |
if (state->single_line) |
| 1083 |
state->cursor = 0; |
| 1084 |
else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE) |
| 1085 |
--state->cursor; |
| 1086 |
state->has_preferred_x = 0; |
| 1087 |
break; |
| 1088 |
|
| 1089 |
#ifdef STB_TEXTEDIT_K_LINEEND2 |
| 1090 |
case STB_TEXTEDIT_K_LINEEND2: |
| 1091 |
#endif |
| 1092 |
case STB_TEXTEDIT_K_LINEEND: { |
| 1093 |
int n = STB_TEXTEDIT_STRINGLEN(str); |
| 1094 |
stb_textedit_clamp(str, state); |
| 1095 |
stb_textedit_move_to_first(state); |
| 1096 |
if (state->single_line) |
| 1097 |
state->cursor = n; |
| 1098 |
else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE) |
| 1099 |
++state->cursor; |
| 1100 |
state->has_preferred_x = 0; |
| 1101 |
break; |
| 1102 |
} |
| 1103 |
|
| 1104 |
#ifdef STB_TEXTEDIT_K_LINESTART2 |
| 1105 |
case STB_TEXTEDIT_K_LINESTART2 | STB_TEXTEDIT_K_SHIFT: |
| 1106 |
#endif |
| 1107 |
case STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT: |
| 1108 |
stb_textedit_clamp(str, state); |
| 1109 |
stb_textedit_prep_selection_at_cursor(state); |
| 1110 |
if (state->single_line) |
| 1111 |
state->cursor = 0; |
| 1112 |
else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE) |
| 1113 |
--state->cursor; |
| 1114 |
state->select_end = state->cursor; |
| 1115 |
state->has_preferred_x = 0; |
| 1116 |
break; |
| 1117 |
|
| 1118 |
#ifdef STB_TEXTEDIT_K_LINEEND2 |
| 1119 |
case STB_TEXTEDIT_K_LINEEND2 | STB_TEXTEDIT_K_SHIFT: |
| 1120 |
#endif |
| 1121 |
case STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT: { |
| 1122 |
int n = STB_TEXTEDIT_STRINGLEN(str); |
| 1123 |
stb_textedit_clamp(str, state); |
| 1124 |
stb_textedit_prep_selection_at_cursor(state); |
| 1125 |
if (state->single_line) |
| 1126 |
state->cursor = n; |
| 1127 |
else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE) |
| 1128 |
++state->cursor; |
| 1129 |
state->select_end = state->cursor; |
| 1130 |
state->has_preferred_x = 0; |
| 1131 |
break; |
| 1132 |
} |
| 1133 |
} |
| 1134 |
} |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
static void stb_textedit_flush_redo(StbUndoState *state) |
| 1143 |
{ |
| 1144 |
state->redo_point = IMSTB_TEXTEDIT_UNDOSTATECOUNT; |
| 1145 |
state->redo_char_point = IMSTB_TEXTEDIT_UNDOCHARCOUNT; |
| 1146 |
} |
| 1147 |
|
| 1148 |
|
| 1149 |
static void stb_textedit_discard_undo(StbUndoState *state) |
| 1150 |
{ |
| 1151 |
if (state->undo_point > 0) { |
| 1152 |
|
| 1153 |
if (state->undo_rec[0].char_storage >= 0) { |
| 1154 |
int n = state->undo_rec[0].insert_length, i; |
| 1155 |
|
| 1156 |
state->undo_char_point -= n; |
| 1157 |
IMSTB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(IMSTB_TEXTEDIT_CHARTYPE))); |
| 1158 |
for (i=0; i < state->undo_point; ++i) |
| 1159 |
if (state->undo_rec[i].char_storage >= 0) |
| 1160 |
state->undo_rec[i].char_storage -= n; |
| 1161 |
} |
| 1162 |
--state->undo_point; |
| 1163 |
IMSTB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0]))); |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
static void stb_textedit_discard_redo(StbUndoState *state) |
| 1172 |
{ |
| 1173 |
int k = IMSTB_TEXTEDIT_UNDOSTATECOUNT-1; |
| 1174 |
|
| 1175 |
if (state->redo_point <= k) { |
| 1176 |
|
| 1177 |
if (state->undo_rec[k].char_storage >= 0) { |
| 1178 |
int n = state->undo_rec[k].insert_length, i; |
| 1179 |
|
| 1180 |
state->redo_char_point += n; |
| 1181 |
IMSTB_TEXTEDIT_memmove(state->undo_char + state->redo_char_point, state->undo_char + state->redo_char_point-n, (size_t) ((IMSTB_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point)*sizeof(IMSTB_TEXTEDIT_CHARTYPE))); |
| 1182 |
|
| 1183 |
for (i=state->redo_point; i < k; ++i) |
| 1184 |
if (state->undo_rec[i].char_storage >= 0) |
| 1185 |
state->undo_rec[i].char_storage += n; |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
size_t move_size = (size_t)((IMSTB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point - 1) * sizeof(state->undo_rec[0])); |
| 1190 |
const char* buf_begin = (char*)state->undo_rec; (void)buf_begin; |
| 1191 |
const char* buf_end = (char*)state->undo_rec + sizeof(state->undo_rec); (void)buf_end; |
| 1192 |
IM_ASSERT(((char*)(state->undo_rec + state->redo_point)) >= buf_begin); |
| 1193 |
IM_ASSERT(((char*)(state->undo_rec + state->redo_point + 1) + move_size) <= buf_end); |
| 1194 |
IMSTB_TEXTEDIT_memmove(state->undo_rec + state->redo_point+1, state->undo_rec + state->redo_point, move_size); |
| 1195 |
|
| 1196 |
|
| 1197 |
++state->redo_point; |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
static StbUndoRecord *stb_text_create_undo_record(StbUndoState *state, int numchars) |
| 1202 |
{ |
| 1203 |
|
| 1204 |
stb_textedit_flush_redo(state); |
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
if (state->undo_point == IMSTB_TEXTEDIT_UNDOSTATECOUNT) |
| 1209 |
stb_textedit_discard_undo(state); |
| 1210 |
|
| 1211 |
|
| 1212 |
if (numchars > IMSTB_TEXTEDIT_UNDOCHARCOUNT) { |
| 1213 |
state->undo_point = 0; |
| 1214 |
state->undo_char_point = 0; |
| 1215 |
return NULL; |
| 1216 |
} |
| 1217 |
|
| 1218 |
|
| 1219 |
while (state->undo_char_point + numchars > IMSTB_TEXTEDIT_UNDOCHARCOUNT) |
| 1220 |
stb_textedit_discard_undo(state); |
| 1221 |
|
| 1222 |
return &state->undo_rec[state->undo_point++]; |
| 1223 |
} |
| 1224 |
|
| 1225 |
static IMSTB_TEXTEDIT_CHARTYPE *stb_text_createundo(StbUndoState *state, int pos, int insert_len, int delete_len) |
| 1226 |
{ |
| 1227 |
StbUndoRecord *r = stb_text_create_undo_record(state, insert_len); |
| 1228 |
if (r == NULL) |
| 1229 |
return NULL; |
| 1230 |
|
| 1231 |
r->where = pos; |
| 1232 |
r->insert_length = (IMSTB_TEXTEDIT_POSITIONTYPE) insert_len; |
| 1233 |
r->delete_length = (IMSTB_TEXTEDIT_POSITIONTYPE) delete_len; |
| 1234 |
|
| 1235 |
if (insert_len == 0) { |
| 1236 |
r->char_storage = -1; |
| 1237 |
return NULL; |
| 1238 |
} else { |
| 1239 |
r->char_storage = state->undo_char_point; |
| 1240 |
state->undo_char_point += insert_len; |
| 1241 |
return &state->undo_char[r->char_storage]; |
| 1242 |
} |
| 1243 |
} |
| 1244 |
|
| 1245 |
static void stb_text_undo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state) |
| 1246 |
{ |
| 1247 |
StbUndoState *s = &state->undostate; |
| 1248 |
StbUndoRecord u, *r; |
| 1249 |
if (s->undo_point == 0) |
| 1250 |
return; |
| 1251 |
|
| 1252 |
|
| 1253 |
u = s->undo_rec[s->undo_point-1]; |
| 1254 |
r = &s->undo_rec[s->redo_point-1]; |
| 1255 |
r->char_storage = -1; |
| 1256 |
|
| 1257 |
r->insert_length = u.delete_length; |
| 1258 |
r->delete_length = u.insert_length; |
| 1259 |
r->where = u.where; |
| 1260 |
|
| 1261 |
if (u.delete_length) { |
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
if (s->undo_char_point + u.delete_length >= IMSTB_TEXTEDIT_UNDOCHARCOUNT) { |
| 1273 |
|
| 1274 |
r->insert_length = 0; |
| 1275 |
} else { |
| 1276 |
int i; |
| 1277 |
|
| 1278 |
|
| 1279 |
while (s->undo_char_point + u.delete_length > s->redo_char_point) { |
| 1280 |
|
| 1281 |
if (s->redo_point == IMSTB_TEXTEDIT_UNDOSTATECOUNT) |
| 1282 |
return; |
| 1283 |
|
| 1284 |
stb_textedit_discard_redo(s); |
| 1285 |
} |
| 1286 |
r = &s->undo_rec[s->redo_point-1]; |
| 1287 |
|
| 1288 |
r->char_storage = s->redo_char_point - u.delete_length; |
| 1289 |
s->redo_char_point = s->redo_char_point - u.delete_length; |
| 1290 |
|
| 1291 |
|
| 1292 |
for (i=0; i < u.delete_length; ++i) |
| 1293 |
s->undo_char[r->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u.where + i); |
| 1294 |
} |
| 1295 |
|
| 1296 |
|
| 1297 |
STB_TEXTEDIT_DELETECHARS(str, u.where, u.delete_length); |
| 1298 |
} |
| 1299 |
|
| 1300 |
|
| 1301 |
if (u.insert_length) { |
| 1302 |
|
| 1303 |
STB_TEXTEDIT_INSERTCHARS(str, u.where, &s->undo_char[u.char_storage], u.insert_length); |
| 1304 |
s->undo_char_point -= u.insert_length; |
| 1305 |
} |
| 1306 |
|
| 1307 |
state->cursor = u.where + u.insert_length; |
| 1308 |
|
| 1309 |
s->undo_point--; |
| 1310 |
s->redo_point--; |
| 1311 |
} |
| 1312 |
|
| 1313 |
static void stb_text_redo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state) |
| 1314 |
{ |
| 1315 |
StbUndoState *s = &state->undostate; |
| 1316 |
StbUndoRecord *u, r; |
| 1317 |
if (s->redo_point == IMSTB_TEXTEDIT_UNDOSTATECOUNT) |
| 1318 |
return; |
| 1319 |
|
| 1320 |
|
| 1321 |
u = &s->undo_rec[s->undo_point]; |
| 1322 |
r = s->undo_rec[s->redo_point]; |
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
u->delete_length = r.insert_length; |
| 1328 |
u->insert_length = r.delete_length; |
| 1329 |
u->where = r.where; |
| 1330 |
u->char_storage = -1; |
| 1331 |
|
| 1332 |
if (r.delete_length) { |
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
if (s->undo_char_point + u->insert_length > s->redo_char_point) { |
| 1337 |
u->insert_length = 0; |
| 1338 |
u->delete_length = 0; |
| 1339 |
} else { |
| 1340 |
int i; |
| 1341 |
u->char_storage = s->undo_char_point; |
| 1342 |
s->undo_char_point = s->undo_char_point + u->insert_length; |
| 1343 |
|
| 1344 |
|
| 1345 |
for (i=0; i < u->insert_length; ++i) |
| 1346 |
s->undo_char[u->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u->where + i); |
| 1347 |
} |
| 1348 |
|
| 1349 |
STB_TEXTEDIT_DELETECHARS(str, r.where, r.delete_length); |
| 1350 |
} |
| 1351 |
|
| 1352 |
if (r.insert_length) { |
| 1353 |
|
| 1354 |
STB_TEXTEDIT_INSERTCHARS(str, r.where, &s->undo_char[r.char_storage], r.insert_length); |
| 1355 |
s->redo_char_point += r.insert_length; |
| 1356 |
} |
| 1357 |
|
| 1358 |
state->cursor = r.where + r.insert_length; |
| 1359 |
|
| 1360 |
s->undo_point++; |
| 1361 |
s->redo_point++; |
| 1362 |
} |
| 1363 |
|
| 1364 |
static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length) |
| 1365 |
{ |
| 1366 |
stb_text_createundo(&state->undostate, where, 0, length); |
| 1367 |
} |
| 1368 |
|
| 1369 |
static void stb_text_makeundo_delete(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length) |
| 1370 |
{ |
| 1371 |
int i; |
| 1372 |
IMSTB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, length, 0); |
| 1373 |
if (p) { |
| 1374 |
for (i=0; i < length; ++i) |
| 1375 |
p[i] = STB_TEXTEDIT_GETCHAR(str, where+i); |
| 1376 |
} |
| 1377 |
} |
| 1378 |
|
| 1379 |
static void stb_text_makeundo_replace(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length) |
| 1380 |
{ |
| 1381 |
int i; |
| 1382 |
IMSTB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, old_length, new_length); |
| 1383 |
if (p) { |
| 1384 |
for (i=0; i < old_length; ++i) |
| 1385 |
p[i] = STB_TEXTEDIT_GETCHAR(str, where+i); |
| 1386 |
} |
| 1387 |
} |
| 1388 |
|
| 1389 |
|
| 1390 |
static void stb_textedit_clear_state(STB_TexteditState *state, int is_single_line) |
| 1391 |
{ |
| 1392 |
state->undostate.undo_point = 0; |
| 1393 |
state->undostate.undo_char_point = 0; |
| 1394 |
state->undostate.redo_point = IMSTB_TEXTEDIT_UNDOSTATECOUNT; |
| 1395 |
state->undostate.redo_char_point = IMSTB_TEXTEDIT_UNDOCHARCOUNT; |
| 1396 |
state->select_end = state->select_start = 0; |
| 1397 |
state->cursor = 0; |
| 1398 |
state->has_preferred_x = 0; |
| 1399 |
state->preferred_x = 0; |
| 1400 |
state->cursor_at_end_of_line = 0; |
| 1401 |
state->initialized = 1; |
| 1402 |
state->single_line = (unsigned char) is_single_line; |
| 1403 |
state->insert_mode = 0; |
| 1404 |
state->row_count_per_page = 0; |
| 1405 |
} |
| 1406 |
|
| 1407 |
|
| 1408 |
static void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line) |
| 1409 |
{ |
| 1410 |
stb_textedit_clear_state(state, is_single_line); |
| 1411 |
} |
| 1412 |
|
| 1413 |
#if defined(__GNUC__) || defined(__clang__) |
| 1414 |
#pragma GCC diagnostic push |
| 1415 |
#pragma GCC diagnostic ignored "-Wcast-qual" |
| 1416 |
#endif |
| 1417 |
|
| 1418 |
static int stb_textedit_paste(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, IMSTB_TEXTEDIT_CHARTYPE const *ctext, int len) |
| 1419 |
{ |
| 1420 |
return stb_textedit_paste_internal(str, state, (IMSTB_TEXTEDIT_CHARTYPE *) ctext, len); |
| 1421 |
} |
| 1422 |
|
| 1423 |
#if defined(__GNUC__) || defined(__clang__) |
| 1424 |
#pragma GCC diagnostic pop |
| 1425 |
#endif |
| 1426 |
|
| 1427 |
#endif |
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|