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