247 #ifndef INCLUDE_STB_TEXTEDIT_H
248 #define INCLUDE_STB_TEXTEDIT_H
259 #ifndef STB_TEXTEDIT_UNDOSTATECOUNT
260 #define STB_TEXTEDIT_UNDOSTATECOUNT 99
262 #ifndef STB_TEXTEDIT_UNDOCHARCOUNT
263 #define STB_TEXTEDIT_UNDOCHARCOUNT 999
265 #ifndef STB_TEXTEDIT_CHARTYPE
266 #define STB_TEXTEDIT_CHARTYPE int
268 #ifndef STB_TEXTEDIT_POSITIONTYPE
269 #define STB_TEXTEDIT_POSITIONTYPE int
340 #endif //INCLUDE_STB_TEXTEDIT_H
353 #ifdef STB_TEXTEDIT_IMPLEMENTATION
368 float base_y = 0, prev_x;
384 if (y < base_y + r.
ymax)
427 state->
cursor = stb_text_locate_coord(str, x, y);
436 int p = stb_text_locate_coord(str, x, y);
449 static void stb_text_makeundo_insert(
STB_TexteditState *state,
int where,
int length);
456 int first_char, length;
462 static void stb_textedit_find_charpos(StbFindState *find,
STB_TEXTEDIT_STRING *str,
int n,
int single_line)
475 find->first_char = 0;
488 find->first_char = i;
490 find->prev_first = prev_start;
507 find->first_char = first = i;
510 find->prev_first = prev_start;
515 for (i=0; first+i < n; ++i)
519 #define STB_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end)
525 if (STB_TEXT_HAS_SELECTION(state)) {
538 stb_text_makeundo_delete(str, state, where, len);
546 stb_textedit_clamp(str, state);
547 if (STB_TEXT_HAS_SELECTION(state)) {
572 if (STB_TEXT_HAS_SELECTION(state)) {
573 stb_textedit_sortselection(state);
583 if (STB_TEXT_HAS_SELECTION(state)) {
584 stb_textedit_sortselection(state);
585 stb_textedit_clamp(str, state);
592 #ifdef STB_TEXTEDIT_IS_SPACE
600 int c = _state->
cursor - 1;
601 while( c >= 0 && !is_word_boundary( _str, c ) )
614 while( c < len && !is_word_boundary( _str, c ) )
627 if (!STB_TEXT_HAS_SELECTION(state))
636 if (STB_TEXT_HAS_SELECTION(state)) {
637 stb_textedit_delete_selection(str,state);
649 stb_textedit_clamp(str, state);
650 stb_textedit_delete_selection(str,state);
653 stb_text_makeundo_insert(state, state->
cursor, len);
679 stb_text_makeundo_replace(str, state, state->
cursor, 1, 1);
686 stb_textedit_delete_selection(str,state);
688 stb_text_makeundo_insert(state, state->
cursor, 1);
697 #ifdef STB_TEXTEDIT_K_INSERT
698 case STB_TEXTEDIT_K_INSERT:
704 stb_text_undo(str, state);
709 stb_text_redo(str, state);
715 if (STB_TEXT_HAS_SELECTION(state))
716 stb_textedit_move_to_first(state);
725 if (STB_TEXT_HAS_SELECTION(state))
726 stb_textedit_move_to_last(str, state);
729 stb_textedit_clamp(str, state);
734 stb_textedit_clamp(str, state);
735 stb_textedit_prep_selection_at_cursor(state);
743 #ifdef STB_TEXTEDIT_IS_SPACE
745 if (STB_TEXT_HAS_SELECTION(state))
746 stb_textedit_move_to_first(state);
748 state->
cursor = stb_textedit_move_to_word_previous(str, state);
749 stb_textedit_clamp( str, state );
754 if (STB_TEXT_HAS_SELECTION(state))
755 stb_textedit_move_to_last(str, state);
757 state->
cursor = stb_textedit_move_to_word_next(str, state);
758 stb_textedit_clamp( str, state );
763 if( !STB_TEXT_HAS_SELECTION( state ) )
764 stb_textedit_prep_selection_at_cursor(state);
766 state->
cursor = stb_textedit_move_to_word_previous(str, state);
769 stb_textedit_clamp( str, state );
773 if( !STB_TEXT_HAS_SELECTION( state ) )
774 stb_textedit_prep_selection_at_cursor(state);
776 state->
cursor = stb_textedit_move_to_word_next(str, state);
779 stb_textedit_clamp( str, state );
784 stb_textedit_prep_selection_at_cursor(state);
787 stb_textedit_clamp(str, state);
805 stb_textedit_prep_selection_at_cursor(state);
806 else if (STB_TEXT_HAS_SELECTION(state))
807 stb_textedit_move_to_last(str,state);
810 stb_textedit_clamp(str, state);
817 int start = find.first_char + find.length;
823 #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE
824 if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE)
832 stb_textedit_clamp(str, state);
856 stb_textedit_prep_selection_at_cursor(state);
857 else if (STB_TEXT_HAS_SELECTION(state))
858 stb_textedit_move_to_first(state);
861 stb_textedit_clamp(str, state);
865 if (find.prev_first != find.first_char) {
869 state->
cursor = find.prev_first;
874 #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE
875 if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE)
883 stb_textedit_clamp(str, state);
896 if (STB_TEXT_HAS_SELECTION(state))
897 stb_textedit_delete_selection(str, state);
901 stb_textedit_delete(str, state, state->
cursor, 1);
908 if (STB_TEXT_HAS_SELECTION(state))
909 stb_textedit_delete_selection(str, state);
911 stb_textedit_clamp(str, state);
913 stb_textedit_delete(str, state, state->
cursor-1, 1);
932 stb_textedit_prep_selection_at_cursor(state);
938 stb_textedit_prep_selection_at_cursor(state);
946 stb_textedit_clamp(str, state);
947 stb_textedit_move_to_first(state);
949 state->
cursor = find.first_char;
956 stb_textedit_clamp(str, state);
957 stb_textedit_move_to_first(state);
959 state->
cursor = find.first_char + find.length;
966 stb_textedit_clamp(str, state);
967 stb_textedit_prep_selection_at_cursor(state);
976 stb_textedit_clamp(str, state);
977 stb_textedit_prep_selection_at_cursor(state);
996 static void stb_textedit_flush_redo(
StbUndoState *state)
1003 static void stb_textedit_discard_undo(
StbUndoState *state)
1025 static void stb_textedit_discard_redo(
StbUndoState *state)
1048 stb_textedit_flush_redo(state);
1053 stb_textedit_discard_undo(state);
1064 stb_textedit_discard_undo(state);
1071 StbUndoRecord *r = stb_text_create_undo_record(state, insert_len);
1079 if (insert_len == 0) {
1125 stb_textedit_discard_redo(s);
1207 static void stb_text_makeundo_insert(
STB_TexteditState *state,
int where,
int length)
1209 stb_text_createundo(&state->
undostate, where, 0, length);
1217 for (i=0; i < length; ++i)
1227 for (i=0; i < old_length; ++i)
1233 static void stb_textedit_clear_state(
STB_TexteditState *state,
int is_single_line)
1245 state->
single_line = (
unsigned char) is_single_line;
1250 static void stb_textedit_initialize_state(
STB_TexteditState *state,
int is_single_line)
1252 stb_textedit_clear_state(state, is_single_line);
1254 #endif//STB_TEXTEDIT_IMPLEMENTATION
#define STB_TEXTEDIT_IS_SPACE(CH)
unsigned char cursor_at_end_of_line
#define STB_TEXTEDIT_STRING
static ImWchar STB_TEXTEDIT_GETCHAR(const STB_TEXTEDIT_STRING *obj, int idx)
static float STB_TEXTEDIT_GETWIDTH(STB_TEXTEDIT_STRING *obj, int line_start_idx, int char_idx)
static int STB_TEXTEDIT_KEYTOTEXT(int key)
static int STB_TEXTEDIT_STRINGLEN(const STB_TEXTEDIT_STRING *obj)
static ImWchar STB_TEXTEDIT_NEWLINE
#define STB_TEXTEDIT_UNDOSTATECOUNT
#define STB_TEXTEDIT_CHARTYPE
StbUndoRecord undo_rec[STB_TEXTEDIT_UNDOSTATECOUNT]
#define STB_TEXTEDIT_UNDOCHARCOUNT
static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow *r, STB_TEXTEDIT_STRING *obj, int line_start_idx)
#define STB_TEXTEDIT_POSITIONTYPE
unsigned char initialized
STB_TEXTEDIT_CHARTYPE undo_char[STB_TEXTEDIT_UNDOCHARCOUNT]
static void STB_TEXTEDIT_DELETECHARS(STB_TEXTEDIT_STRING *obj, int pos, int n)
STB_TEXTEDIT_POSITIONTYPE where
unsigned char insert_mode
static bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING *obj, int pos, const ImWchar *new_text, int new_text_len)
unsigned char single_line
unsigned char has_preferred_x