278 #ifndef INCLUDE_IMSTB_TEXTEDIT_H 279 #define INCLUDE_IMSTB_TEXTEDIT_H 290 #ifndef IMSTB_TEXTEDIT_UNDOSTATECOUNT 291 #define IMSTB_TEXTEDIT_UNDOSTATECOUNT 99 293 #ifndef IMSTB_TEXTEDIT_UNDOCHARCOUNT 294 #define IMSTB_TEXTEDIT_UNDOCHARCOUNT 999 296 #ifndef IMSTB_TEXTEDIT_CHARTYPE 297 #define IMSTB_TEXTEDIT_CHARTYPE int 299 #ifndef IMSTB_TEXTEDIT_POSITIONTYPE 300 #define IMSTB_TEXTEDIT_POSITIONTYPE int 306 IMSTB_TEXTEDIT_POSITIONTYPE where;
307 IMSTB_TEXTEDIT_POSITIONTYPE insert_length;
308 IMSTB_TEXTEDIT_POSITIONTYPE delete_length;
316 IMSTB_TEXTEDIT_CHARTYPE undo_char[IMSTB_TEXTEDIT_UNDOCHARCOUNT];
317 short undo_point, redo_point;
318 int undo_char_point, redo_char_point;
338 unsigned char insert_mode;
342 int row_count_per_page;
350 unsigned char cursor_at_end_of_line;
351 unsigned char initialized;
352 unsigned char has_preferred_x;
353 unsigned char single_line;
354 unsigned char padding1, padding2, padding3;
371 float baseline_y_delta;
375 #endif //INCLUDE_IMSTB_TEXTEDIT_H 388 #ifdef IMSTB_TEXTEDIT_IMPLEMENTATION 390 #ifndef IMSTB_TEXTEDIT_memmove 392 #define IMSTB_TEXTEDIT_memmove memmove 402 static int stb_text_locate_coord(IMSTB_TEXTEDIT_STRING *str,
float x,
float y)
405 int n = STB_TEXTEDIT_STRINGLEN(str);
406 float base_y = 0, prev_x;
415 STB_TEXTEDIT_LAYOUTROW(&r, str, i);
416 if (r.num_chars <= 0)
419 if (i==0 && y < base_y + r.ymin)
422 if (y < base_y + r.ymax)
426 base_y += r.baseline_y_delta;
441 for (k=0; k < r.num_chars; ++k) {
442 float w = STB_TEXTEDIT_GETWIDTH(str, i, k);
455 if (STB_TEXTEDIT_GETCHAR(str, i+r.num_chars-1) == STB_TEXTEDIT_NEWLINE)
456 return i+r.num_chars-1;
458 return i+r.num_chars;
462 static void stb_textedit_click(IMSTB_TEXTEDIT_STRING *str,
STB_TexteditState *state,
float x,
float y)
466 if( state->single_line )
469 STB_TEXTEDIT_LAYOUTROW(&r, str, 0);
473 state->cursor = stb_text_locate_coord(str, x, y);
474 state->select_start = state->cursor;
475 state->select_end = state->cursor;
476 state->has_preferred_x = 0;
480 static void stb_textedit_drag(IMSTB_TEXTEDIT_STRING *str,
STB_TexteditState *state,
float x,
float y)
486 if( state->single_line )
489 STB_TEXTEDIT_LAYOUTROW(&r, str, 0);
493 if (state->select_start == state->select_end)
494 state->select_start = state->cursor;
496 p = stb_text_locate_coord(str, x, y);
497 state->cursor = state->select_end = p;
508 static void stb_text_makeundo_delete(IMSTB_TEXTEDIT_STRING *str,
STB_TexteditState *state,
int where,
int length);
509 static void stb_text_makeundo_insert(
STB_TexteditState *state,
int where,
int length);
510 static void stb_text_makeundo_replace(IMSTB_TEXTEDIT_STRING *str,
STB_TexteditState *state,
int where,
int old_length,
int new_length);
516 int first_char, length;
522 static void stb_textedit_find_charpos(StbFindState *
find, IMSTB_TEXTEDIT_STRING *str,
int n,
int single_line)
526 int z = STB_TEXTEDIT_STRINGLEN(str);
529 if (n == z && single_line) {
531 STB_TEXTEDIT_LAYOUTROW(&r, str, 0);
533 find->first_char = 0;
535 find->height = r.ymax - r.ymin;
544 STB_TEXTEDIT_LAYOUTROW(&r, str, i);
545 if (n < i + r.num_chars)
547 if (i + r.num_chars == z && z > 0 && STB_TEXTEDIT_GETCHAR(str, z - 1) != STB_TEXTEDIT_NEWLINE)
551 find->y += r.baseline_y_delta;
559 find->first_char = first = i;
560 find->length = r.num_chars;
561 find->height = r.ymax - r.ymin;
562 find->prev_first = prev_start;
566 for (i=0; first+i < n; ++i)
567 find->x += STB_TEXTEDIT_GETWIDTH(str, first, i);
570 #define STB_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end) 573 static void stb_textedit_clamp(IMSTB_TEXTEDIT_STRING *str,
STB_TexteditState *state)
575 int n = STB_TEXTEDIT_STRINGLEN(str);
576 if (STB_TEXT_HAS_SELECTION(state)) {
577 if (state->select_start > n) state->select_start = n;
578 if (state->select_end > n) state->select_end = n;
580 if (state->select_start == state->select_end)
581 state->cursor = state->select_start;
583 if (state->cursor > n) state->cursor = n;
587 static void stb_textedit_delete(IMSTB_TEXTEDIT_STRING *str,
STB_TexteditState *state,
int where,
int len)
589 stb_text_makeundo_delete(str, state, where, len);
590 STB_TEXTEDIT_DELETECHARS(str, where, len);
591 state->has_preferred_x = 0;
595 static void stb_textedit_delete_selection(IMSTB_TEXTEDIT_STRING *str,
STB_TexteditState *state)
597 stb_textedit_clamp(str, state);
598 if (STB_TEXT_HAS_SELECTION(state)) {
599 if (state->select_start < state->select_end) {
600 stb_textedit_delete(str, state, state->select_start, state->select_end - state->select_start);
601 state->select_end = state->cursor = state->select_start;
603 stb_textedit_delete(str, state, state->select_end, state->select_start - state->select_end);
604 state->select_start = state->cursor = state->select_end;
606 state->has_preferred_x = 0;
613 if (state->select_end < state->select_start) {
614 int temp = state->select_end;
615 state->select_end = state->select_start;
616 state->select_start = temp;
623 if (STB_TEXT_HAS_SELECTION(state)) {
624 stb_textedit_sortselection(state);
625 state->cursor = state->select_start;
626 state->select_end = state->select_start;
627 state->has_preferred_x = 0;
632 static void stb_textedit_move_to_last(IMSTB_TEXTEDIT_STRING *str,
STB_TexteditState *state)
634 if (STB_TEXT_HAS_SELECTION(state)) {
635 stb_textedit_sortselection(state);
636 stb_textedit_clamp(str, state);
637 state->cursor = state->select_end;
638 state->select_start = state->select_end;
639 state->has_preferred_x = 0;
643 #ifdef STB_TEXTEDIT_IS_SPACE 644 static int is_word_boundary( IMSTB_TEXTEDIT_STRING *str,
int idx )
646 return idx > 0 ? (STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str,idx-1) ) && !STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str, idx) ) ) : 1;
649 #ifndef STB_TEXTEDIT_MOVEWORDLEFT 650 static int stb_textedit_move_to_word_previous( IMSTB_TEXTEDIT_STRING *str,
int c )
653 while( c >= 0 && !is_word_boundary( str, c ) )
661 #define STB_TEXTEDIT_MOVEWORDLEFT stb_textedit_move_to_word_previous 664 #ifndef STB_TEXTEDIT_MOVEWORDRIGHT 665 static int stb_textedit_move_to_word_next( IMSTB_TEXTEDIT_STRING *str,
int c )
667 const int len = STB_TEXTEDIT_STRINGLEN(str);
669 while( c < len && !is_word_boundary( str, c ) )
677 #define STB_TEXTEDIT_MOVEWORDRIGHT stb_textedit_move_to_word_next 685 if (!STB_TEXT_HAS_SELECTION(state))
686 state->select_start = state->select_end = state->cursor;
688 state->cursor = state->select_end;
692 static int stb_textedit_cut(IMSTB_TEXTEDIT_STRING *str,
STB_TexteditState *state)
694 if (STB_TEXT_HAS_SELECTION(state)) {
695 stb_textedit_delete_selection(str,state);
696 state->has_preferred_x = 0;
703 static int stb_textedit_paste_internal(IMSTB_TEXTEDIT_STRING *str,
STB_TexteditState *state, IMSTB_TEXTEDIT_CHARTYPE *text,
int len)
706 stb_textedit_clamp(str, state);
707 stb_textedit_delete_selection(str,state);
709 if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, len)) {
710 stb_text_makeundo_insert(state, state->cursor, len);
711 state->cursor += len;
712 state->has_preferred_x = 0;
719 #ifndef STB_TEXTEDIT_KEYTYPE 720 #define STB_TEXTEDIT_KEYTYPE int 724 static void stb_textedit_key(IMSTB_TEXTEDIT_STRING *str,
STB_TexteditState *state, STB_TEXTEDIT_KEYTYPE key)
729 int c = STB_TEXTEDIT_KEYTOTEXT(key);
731 IMSTB_TEXTEDIT_CHARTYPE ch = (IMSTB_TEXTEDIT_CHARTYPE) c;
734 if (c ==
'\n' && state->single_line)
737 if (state->insert_mode && !STB_TEXT_HAS_SELECTION(state) && state->cursor < STB_TEXTEDIT_STRINGLEN(str)) {
738 stb_text_makeundo_replace(str, state, state->cursor, 1, 1);
739 STB_TEXTEDIT_DELETECHARS(str, state->cursor, 1);
740 if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) {
742 state->has_preferred_x = 0;
745 stb_textedit_delete_selection(str,state);
746 if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) {
747 stb_text_makeundo_insert(state, state->cursor, 1);
749 state->has_preferred_x = 0;
756 #ifdef STB_TEXTEDIT_K_INSERT 757 case STB_TEXTEDIT_K_INSERT:
758 state->insert_mode = !state->insert_mode;
762 case STB_TEXTEDIT_K_UNDO:
763 stb_text_undo(str, state);
764 state->has_preferred_x = 0;
767 case STB_TEXTEDIT_K_REDO:
768 stb_text_redo(str, state);
769 state->has_preferred_x = 0;
772 case STB_TEXTEDIT_K_LEFT:
774 if (STB_TEXT_HAS_SELECTION(state))
775 stb_textedit_move_to_first(state);
777 if (state->cursor > 0)
779 state->has_preferred_x = 0;
782 case STB_TEXTEDIT_K_RIGHT:
784 if (STB_TEXT_HAS_SELECTION(state))
785 stb_textedit_move_to_last(str, state);
788 stb_textedit_clamp(str, state);
789 state->has_preferred_x = 0;
792 case STB_TEXTEDIT_K_LEFT | STB_TEXTEDIT_K_SHIFT:
793 stb_textedit_clamp(str, state);
794 stb_textedit_prep_selection_at_cursor(state);
796 if (state->select_end > 0)
798 state->cursor = state->select_end;
799 state->has_preferred_x = 0;
802 #ifdef STB_TEXTEDIT_MOVEWORDLEFT 803 case STB_TEXTEDIT_K_WORDLEFT:
804 if (STB_TEXT_HAS_SELECTION(state))
805 stb_textedit_move_to_first(state);
807 state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor);
808 stb_textedit_clamp( str, state );
812 case STB_TEXTEDIT_K_WORDLEFT | STB_TEXTEDIT_K_SHIFT:
813 if( !STB_TEXT_HAS_SELECTION( state ) )
814 stb_textedit_prep_selection_at_cursor(state);
816 state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor);
817 state->select_end = state->cursor;
819 stb_textedit_clamp( str, state );
823 #ifdef STB_TEXTEDIT_MOVEWORDRIGHT 824 case STB_TEXTEDIT_K_WORDRIGHT:
825 if (STB_TEXT_HAS_SELECTION(state))
826 stb_textedit_move_to_last(str, state);
828 state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor);
829 stb_textedit_clamp( str, state );
833 case STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT:
834 if( !STB_TEXT_HAS_SELECTION( state ) )
835 stb_textedit_prep_selection_at_cursor(state);
837 state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor);
838 state->select_end = state->cursor;
840 stb_textedit_clamp( str, state );
844 case STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT:
845 stb_textedit_prep_selection_at_cursor(state);
848 stb_textedit_clamp(str, state);
849 state->cursor = state->select_end;
850 state->has_preferred_x = 0;
853 case STB_TEXTEDIT_K_DOWN:
854 case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT:
855 case STB_TEXTEDIT_K_PGDOWN:
856 case STB_TEXTEDIT_K_PGDOWN | STB_TEXTEDIT_K_SHIFT: {
859 int i, j, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;
860 int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGDOWN;
861 int row_count = is_page ? state->row_count_per_page : 1;
863 if (!is_page && state->single_line) {
865 key = STB_TEXTEDIT_K_RIGHT | (key & STB_TEXTEDIT_K_SHIFT);
870 stb_textedit_prep_selection_at_cursor(state);
871 else if (STB_TEXT_HAS_SELECTION(state))
872 stb_textedit_move_to_last(str, state);
875 stb_textedit_clamp(str, state);
876 stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);
878 for (j = 0; j < row_count; ++j) {
879 float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x;
880 int start = find.first_char + find.length;
882 if (find.length == 0)
887 if (STB_TEXTEDIT_GETCHAR(str, find.first_char + find.length - 1) != STB_TEXTEDIT_NEWLINE)
891 state->cursor = start;
892 STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);
894 for (i=0; i < row.num_chars; ++i) {
895 float dx = STB_TEXTEDIT_GETWIDTH(str, start, i);
896 #ifdef IMSTB_TEXTEDIT_GETWIDTH_NEWLINE 897 if (dx == IMSTB_TEXTEDIT_GETWIDTH_NEWLINE)
905 stb_textedit_clamp(str, state);
907 state->has_preferred_x = 1;
908 state->preferred_x = goal_x;
911 state->select_end = state->cursor;
914 find.first_char = find.first_char + find.length;
915 find.length = row.num_chars;
920 case STB_TEXTEDIT_K_UP:
921 case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT:
922 case STB_TEXTEDIT_K_PGUP:
923 case STB_TEXTEDIT_K_PGUP | STB_TEXTEDIT_K_SHIFT: {
926 int i, j, prev_scan, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;
927 int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGUP;
928 int row_count = is_page ? state->row_count_per_page : 1;
930 if (!is_page && state->single_line) {
932 key = STB_TEXTEDIT_K_LEFT | (key & STB_TEXTEDIT_K_SHIFT);
937 stb_textedit_prep_selection_at_cursor(state);
938 else if (STB_TEXT_HAS_SELECTION(state))
939 stb_textedit_move_to_first(state);
942 stb_textedit_clamp(str, state);
943 stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);
945 for (j = 0; j < row_count; ++j) {
946 float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x;
949 if (find.prev_first == find.first_char)
953 state->cursor = find.prev_first;
954 STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);
956 for (i=0; i < row.num_chars; ++i) {
957 float dx = STB_TEXTEDIT_GETWIDTH(str, find.prev_first, i);
958 #ifdef IMSTB_TEXTEDIT_GETWIDTH_NEWLINE 959 if (dx == IMSTB_TEXTEDIT_GETWIDTH_NEWLINE)
967 stb_textedit_clamp(str, state);
969 state->has_preferred_x = 1;
970 state->preferred_x = goal_x;
973 state->select_end = state->cursor;
977 prev_scan = find.prev_first > 0 ? find.prev_first - 1 : 0;
978 while (prev_scan > 0 && STB_TEXTEDIT_GETCHAR(str, prev_scan - 1) != STB_TEXTEDIT_NEWLINE)
980 find.first_char = find.prev_first;
981 find.prev_first = prev_scan;
986 case STB_TEXTEDIT_K_DELETE:
987 case STB_TEXTEDIT_K_DELETE | STB_TEXTEDIT_K_SHIFT:
988 if (STB_TEXT_HAS_SELECTION(state))
989 stb_textedit_delete_selection(str, state);
991 int n = STB_TEXTEDIT_STRINGLEN(str);
992 if (state->cursor < n)
993 stb_textedit_delete(str, state, state->cursor, 1);
995 state->has_preferred_x = 0;
998 case STB_TEXTEDIT_K_BACKSPACE:
999 case STB_TEXTEDIT_K_BACKSPACE | STB_TEXTEDIT_K_SHIFT:
1000 if (STB_TEXT_HAS_SELECTION(state))
1001 stb_textedit_delete_selection(str, state);
1003 stb_textedit_clamp(str, state);
1004 if (state->cursor > 0) {
1005 stb_textedit_delete(str, state, state->cursor-1, 1);
1009 state->has_preferred_x = 0;
1012 #ifdef STB_TEXTEDIT_K_TEXTSTART2 1013 case STB_TEXTEDIT_K_TEXTSTART2:
1015 case STB_TEXTEDIT_K_TEXTSTART:
1016 state->cursor = state->select_start = state->select_end = 0;
1017 state->has_preferred_x = 0;
1020 #ifdef STB_TEXTEDIT_K_TEXTEND2 1021 case STB_TEXTEDIT_K_TEXTEND2:
1023 case STB_TEXTEDIT_K_TEXTEND:
1024 state->cursor = STB_TEXTEDIT_STRINGLEN(str);
1025 state->select_start = state->select_end = 0;
1026 state->has_preferred_x = 0;
1029 #ifdef STB_TEXTEDIT_K_TEXTSTART2 1030 case STB_TEXTEDIT_K_TEXTSTART2 | STB_TEXTEDIT_K_SHIFT:
1032 case STB_TEXTEDIT_K_TEXTSTART | STB_TEXTEDIT_K_SHIFT:
1033 stb_textedit_prep_selection_at_cursor(state);
1034 state->cursor = state->select_end = 0;
1035 state->has_preferred_x = 0;
1038 #ifdef STB_TEXTEDIT_K_TEXTEND2 1039 case STB_TEXTEDIT_K_TEXTEND2 | STB_TEXTEDIT_K_SHIFT:
1041 case STB_TEXTEDIT_K_TEXTEND | STB_TEXTEDIT_K_SHIFT:
1042 stb_textedit_prep_selection_at_cursor(state);
1043 state->cursor = state->select_end = STB_TEXTEDIT_STRINGLEN(str);
1044 state->has_preferred_x = 0;
1048 #ifdef STB_TEXTEDIT_K_LINESTART2 1049 case STB_TEXTEDIT_K_LINESTART2:
1051 case STB_TEXTEDIT_K_LINESTART:
1052 stb_textedit_clamp(str, state);
1053 stb_textedit_move_to_first(state);
1054 if (state->single_line)
1056 else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE)
1058 state->has_preferred_x = 0;
1061 #ifdef STB_TEXTEDIT_K_LINEEND2 1062 case STB_TEXTEDIT_K_LINEEND2:
1064 case STB_TEXTEDIT_K_LINEEND: {
1065 int n = STB_TEXTEDIT_STRINGLEN(str);
1066 stb_textedit_clamp(str, state);
1067 stb_textedit_move_to_first(state);
1068 if (state->single_line)
1070 else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE)
1072 state->has_preferred_x = 0;
1076 #ifdef STB_TEXTEDIT_K_LINESTART2 1077 case STB_TEXTEDIT_K_LINESTART2 | STB_TEXTEDIT_K_SHIFT:
1079 case STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT:
1080 stb_textedit_clamp(str, state);
1081 stb_textedit_prep_selection_at_cursor(state);
1082 if (state->single_line)
1084 else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE)
1086 state->select_end = state->cursor;
1087 state->has_preferred_x = 0;
1090 #ifdef STB_TEXTEDIT_K_LINEEND2 1091 case STB_TEXTEDIT_K_LINEEND2 | STB_TEXTEDIT_K_SHIFT:
1093 case STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT: {
1094 int n = STB_TEXTEDIT_STRINGLEN(str);
1095 stb_textedit_clamp(str, state);
1096 stb_textedit_prep_selection_at_cursor(state);
1097 if (state->single_line)
1099 else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE)
1101 state->select_end = state->cursor;
1102 state->has_preferred_x = 0;
1114 static void stb_textedit_flush_redo(
StbUndoState *state)
1116 state->redo_point = IMSTB_TEXTEDIT_UNDOSTATECOUNT;
1117 state->redo_char_point = IMSTB_TEXTEDIT_UNDOCHARCOUNT;
1121 static void stb_textedit_discard_undo(
StbUndoState *state)
1123 if (state->undo_point > 0) {
1125 if (state->undo_rec[0].char_storage >= 0) {
1126 int n = state->undo_rec[0].insert_length, i;
1128 state->undo_char_point -= n;
1129 IMSTB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (
size_t) (state->undo_char_point*
sizeof(IMSTB_TEXTEDIT_CHARTYPE)));
1130 for (i=0; i < state->undo_point; ++i)
1131 if (state->undo_rec[i].char_storage >= 0)
1132 state->undo_rec[i].char_storage -= n;
1134 --state->undo_point;
1135 IMSTB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (
size_t) (state->undo_point*
sizeof(state->undo_rec[0])));
1143 static void stb_textedit_discard_redo(
StbUndoState *state)
1145 int k = IMSTB_TEXTEDIT_UNDOSTATECOUNT-1;
1147 if (state->redo_point <= k) {
1149 if (state->undo_rec[k].char_storage >= 0) {
1150 int n = state->undo_rec[k].insert_length, i;
1152 state->redo_char_point += n;
1153 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)));
1155 for (i=state->redo_point; i < k; ++i)
1156 if (state->undo_rec[i].char_storage >= 0)
1157 state->undo_rec[i].char_storage += n;
1161 size_t move_size = (size_t)((IMSTB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point - 1) *
sizeof(state->undo_rec[0]));
1162 const char* buf_begin = (
char*)state->undo_rec; (
void)buf_begin;
1163 const char* buf_end = (
char*)state->undo_rec +
sizeof(state->undo_rec); (void)buf_end;
1164 IM_ASSERT(((
char*)(state->undo_rec + state->redo_point)) >= buf_begin);
1165 IM_ASSERT(((
char*)(state->undo_rec + state->redo_point + 1) + move_size) <= buf_end);
1166 IMSTB_TEXTEDIT_memmove(state->undo_rec + state->redo_point+1, state->undo_rec + state->redo_point, move_size);
1169 ++state->redo_point;
1176 stb_textedit_flush_redo(state);
1180 if (state->undo_point == IMSTB_TEXTEDIT_UNDOSTATECOUNT)
1181 stb_textedit_discard_undo(state);
1184 if (numchars > IMSTB_TEXTEDIT_UNDOCHARCOUNT) {
1185 state->undo_point = 0;
1186 state->undo_char_point = 0;
1191 while (state->undo_char_point + numchars > IMSTB_TEXTEDIT_UNDOCHARCOUNT)
1192 stb_textedit_discard_undo(state);
1194 return &state->undo_rec[state->undo_point++];
1197 static IMSTB_TEXTEDIT_CHARTYPE *stb_text_createundo(
StbUndoState *state,
int pos,
int insert_len,
int delete_len)
1199 StbUndoRecord *r = stb_text_create_undo_record(state, insert_len);
1204 r->insert_length = (IMSTB_TEXTEDIT_POSITIONTYPE) insert_len;
1205 r->delete_length = (IMSTB_TEXTEDIT_POSITIONTYPE) delete_len;
1207 if (insert_len == 0) {
1208 r->char_storage = -1;
1211 r->char_storage = state->undo_char_point;
1212 state->undo_char_point += insert_len;
1213 return &state->undo_char[r->char_storage];
1221 if (s->undo_point == 0)
1225 u = s->undo_rec[s->undo_point-1];
1226 r = &s->undo_rec[s->redo_point-1];
1227 r->char_storage = -1;
1229 r->insert_length = u.delete_length;
1230 r->delete_length = u.insert_length;
1233 if (u.delete_length) {
1244 if (s->undo_char_point + u.delete_length >= IMSTB_TEXTEDIT_UNDOCHARCOUNT) {
1246 r->insert_length = 0;
1251 while (s->undo_char_point + u.delete_length > s->redo_char_point) {
1253 if (s->redo_point == IMSTB_TEXTEDIT_UNDOSTATECOUNT)
1256 stb_textedit_discard_redo(s);
1258 r = &s->undo_rec[s->redo_point-1];
1260 r->char_storage = s->redo_char_point - u.delete_length;
1261 s->redo_char_point = s->redo_char_point - u.delete_length;
1264 for (i=0; i < u.delete_length; ++i)
1265 s->undo_char[r->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u.where + i);
1269 STB_TEXTEDIT_DELETECHARS(str, u.where, u.delete_length);
1273 if (u.insert_length) {
1275 STB_TEXTEDIT_INSERTCHARS(str, u.where, &s->undo_char[u.char_storage], u.insert_length);
1276 s->undo_char_point -= u.insert_length;
1279 state->cursor = u.where + u.insert_length;
1289 if (s->redo_point == IMSTB_TEXTEDIT_UNDOSTATECOUNT)
1293 u = &s->undo_rec[s->undo_point];
1294 r = s->undo_rec[s->redo_point];
1299 u->delete_length = r.insert_length;
1300 u->insert_length = r.delete_length;
1302 u->char_storage = -1;
1304 if (r.delete_length) {
1308 if (s->undo_char_point + u->insert_length > s->redo_char_point) {
1309 u->insert_length = 0;
1310 u->delete_length = 0;
1313 u->char_storage = s->undo_char_point;
1314 s->undo_char_point = s->undo_char_point + u->insert_length;
1317 for (i=0; i < u->insert_length; ++i)
1318 s->undo_char[u->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u->where + i);
1321 STB_TEXTEDIT_DELETECHARS(str, r.where, r.delete_length);
1324 if (r.insert_length) {
1326 STB_TEXTEDIT_INSERTCHARS(str, r.where, &s->undo_char[r.char_storage], r.insert_length);
1327 s->redo_char_point += r.insert_length;
1330 state->cursor = r.where + r.insert_length;
1336 static void stb_text_makeundo_insert(
STB_TexteditState *state,
int where,
int length)
1338 stb_text_createundo(&state->undostate, where, 0, length);
1341 static void stb_text_makeundo_delete(IMSTB_TEXTEDIT_STRING *str,
STB_TexteditState *state,
int where,
int length)
1344 IMSTB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, length, 0);
1346 for (i=0; i < length; ++i)
1347 p[i] = STB_TEXTEDIT_GETCHAR(str, where+i);
1351 static void stb_text_makeundo_replace(IMSTB_TEXTEDIT_STRING *str,
STB_TexteditState *state,
int where,
int old_length,
int new_length)
1354 IMSTB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, old_length, new_length);
1356 for (i=0; i < old_length; ++i)
1357 p[i] = STB_TEXTEDIT_GETCHAR(str, where+i);
1362 static void stb_textedit_clear_state(
STB_TexteditState *state,
int is_single_line)
1364 state->undostate.undo_point = 0;
1365 state->undostate.undo_char_point = 0;
1366 state->undostate.redo_point = IMSTB_TEXTEDIT_UNDOSTATECOUNT;
1367 state->undostate.redo_char_point = IMSTB_TEXTEDIT_UNDOCHARCOUNT;
1368 state->select_end = state->select_start = 0;
1370 state->has_preferred_x = 0;
1371 state->preferred_x = 0;
1372 state->cursor_at_end_of_line = 0;
1373 state->initialized = 1;
1374 state->single_line = (
unsigned char) is_single_line;
1375 state->insert_mode = 0;
1376 state->row_count_per_page = 0;
1380 static void stb_textedit_initialize_state(
STB_TexteditState *state,
int is_single_line)
1382 stb_textedit_clear_state(state, is_single_line);
1385 #if defined(__GNUC__) || defined(__clang__) 1386 #pragma GCC diagnostic push 1387 #pragma GCC diagnostic ignored "-Wcast-qual" 1390 static int stb_textedit_paste(IMSTB_TEXTEDIT_STRING *str,
STB_TexteditState *state, IMSTB_TEXTEDIT_CHARTYPE
const *ctext,
int len)
1392 return stb_textedit_paste_internal(str, state, (IMSTB_TEXTEDIT_CHARTYPE *) ctext, len);
1395 #if defined(__GNUC__) || defined(__clang__) 1396 #pragma GCC diagnostic pop 1399 #endif//IMSTB_TEXTEDIT_IMPLEMENTATION Definition: imstb_textedit.h:312
In find(In first, In last, const T &v)
Definition: algorithm.h:225
Definition: imstb_textedit.h:368
Definition: imstb_textedit.h:303
Definition: imstb_textedit.h:321