API key: process a keyboard input
| 714 | |
| 715 | // API key: process a keyboard input |
| 716 | static void stb_textedit_key(STB_TEXTEDIT_STRING* str, STB_TexteditState* state, STB_TEXTEDIT_KEYTYPE key) |
| 717 | { |
| 718 | retry: |
| 719 | switch (key) { |
| 720 | default: { |
| 721 | int c = STB_TEXTEDIT_KEYTOTEXT(key); |
| 722 | if (c > 0) { |
| 723 | STB_TEXTEDIT_CHARTYPE ch = (STB_TEXTEDIT_CHARTYPE)c; |
| 724 | |
| 725 | // can't add newline in single-line mode |
| 726 | if (c == '\n' && state->single_line) |
| 727 | break; |
| 728 | |
| 729 | if (state->insert_mode && !STB_TEXT_HAS_SELECTION(state) && state->cursor < STB_TEXTEDIT_STRINGLEN(str)) { |
| 730 | stb_text_makeundo_replace(str, state, state->cursor, 1, 1); |
| 731 | STB_TEXTEDIT_DELETECHARS(str, state->cursor, 1); |
| 732 | if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) { |
| 733 | ++state->cursor; |
| 734 | state->has_preferred_x = 0; |
| 735 | } |
| 736 | } |
| 737 | else { |
| 738 | stb_textedit_delete_selection(str, state); // implicitly clamps |
| 739 | if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) { |
| 740 | stb_text_makeundo_insert(state, state->cursor, 1); |
| 741 | ++state->cursor; |
| 742 | state->has_preferred_x = 0; |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | break; |
| 747 | } |
| 748 | |
| 749 | #ifdef STB_TEXTEDIT_K_INSERT |
| 750 | case STB_TEXTEDIT_K_INSERT: |
| 751 | state->insert_mode = !state->insert_mode; |
| 752 | break; |
| 753 | #endif |
| 754 | |
| 755 | case STB_TEXTEDIT_K_UNDO: |
| 756 | stb_text_undo(str, state); |
| 757 | state->has_preferred_x = 0; |
| 758 | break; |
| 759 | |
| 760 | case STB_TEXTEDIT_K_REDO: |
| 761 | stb_text_redo(str, state); |
| 762 | state->has_preferred_x = 0; |
| 763 | break; |
| 764 | |
| 765 | case STB_TEXTEDIT_K_LEFT: |
| 766 | // if currently there's a selection, move cursor to start of selection |
| 767 | if (STB_TEXT_HAS_SELECTION(state)) |
| 768 | stb_textedit_move_to_first(state); |
| 769 | else |
| 770 | if (state->cursor > 0) |
| 771 | --state->cursor; |
| 772 | state->has_preferred_x = 0; |
| 773 | break; |
no test coverage detected