git repos / blockattack-game

blame: source/code/Libs/imgui/imstb_textedit.h

normal view · raw

1193b4be Poul Sander 2025-02-03 21:28 1
// [DEAR IMGUI]
1193b4be Poul Sander 2025-02-03 21:28 2
// This is a slightly modified version of stb_textedit.h 1.14.
1193b4be Poul Sander 2025-02-03 21:28 3
// Those changes would need to be pushed into nothings/stb:
1193b4be Poul Sander 2025-02-03 21:28 4
// - Fix in stb_textedit_discard_redo (see https://github.com/nothings/stb/issues/321)
1193b4be Poul Sander 2025-02-03 21:28 5
// - Fix in stb_textedit_find_charpos to handle last line (see https://github.com/ocornut/imgui/issues/6000 + #6783)
1193b4be Poul Sander 2025-02-03 21:28 6
// - Added name to struct or it may be forward declared in our code.
1193b4be Poul Sander 2025-02-03 21:28 7
// - Added UTF-8 support (see https://github.com/nothings/stb/issues/188 + https://github.com/ocornut/imgui/pull/7925)
1193b4be Poul Sander 2025-02-03 21:28 8
// Grep for [DEAR IMGUI] to find the changes.
1193b4be Poul Sander 2025-02-03 21:28 9
// - Also renamed macros used or defined outside of IMSTB_TEXTEDIT_IMPLEMENTATION block from STB_TEXTEDIT_* to IMSTB_TEXTEDIT_*
1193b4be Poul Sander 2025-02-03 21:28 10
1193b4be Poul Sander 2025-02-03 21:28 11
// stb_textedit.h - v1.14  - public domain - Sean Barrett
1193b4be Poul Sander 2025-02-03 21:28 12
// Development of this library was sponsored by RAD Game Tools
1193b4be Poul Sander 2025-02-03 21:28 13
//
1193b4be Poul Sander 2025-02-03 21:28 14
// This C header file implements the guts of a multi-line text-editing
1193b4be Poul Sander 2025-02-03 21:28 15
// widget; you implement display, word-wrapping, and low-level string
1193b4be Poul Sander 2025-02-03 21:28 16
// insertion/deletion, and stb_textedit will map user inputs into
1193b4be Poul Sander 2025-02-03 21:28 17
// insertions & deletions, plus updates to the cursor position,
1193b4be Poul Sander 2025-02-03 21:28 18
// selection state, and undo state.
1193b4be Poul Sander 2025-02-03 21:28 19
//
1193b4be Poul Sander 2025-02-03 21:28 20
// It is intended for use in games and other systems that need to build
1193b4be Poul Sander 2025-02-03 21:28 21
// their own custom widgets and which do not have heavy text-editing
1193b4be Poul Sander 2025-02-03 21:28 22
// requirements (this library is not recommended for use for editing large
1193b4be Poul Sander 2025-02-03 21:28 23
// texts, as its performance does not scale and it has limited undo).
1193b4be Poul Sander 2025-02-03 21:28 24
//
1193b4be Poul Sander 2025-02-03 21:28 25
// Non-trivial behaviors are modelled after Windows text controls.
1193b4be Poul Sander 2025-02-03 21:28 26
//
1193b4be Poul Sander 2025-02-03 21:28 27
//
1193b4be Poul Sander 2025-02-03 21:28 28
// LICENSE
1193b4be Poul Sander 2025-02-03 21:28 29
//
1193b4be Poul Sander 2025-02-03 21:28 30
// See end of file for license information.
1193b4be Poul Sander 2025-02-03 21:28 31
//
1193b4be Poul Sander 2025-02-03 21:28 32
//
1193b4be Poul Sander 2025-02-03 21:28 33
// DEPENDENCIES
1193b4be Poul Sander 2025-02-03 21:28 34
//
1193b4be Poul Sander 2025-02-03 21:28 35
// Uses the C runtime function 'memmove', which you can override
1193b4be Poul Sander 2025-02-03 21:28 36
// by defining IMSTB_TEXTEDIT_memmove before the implementation.
1193b4be Poul Sander 2025-02-03 21:28 37
// Uses no other functions. Performs no runtime allocations.
1193b4be Poul Sander 2025-02-03 21:28 38
//
1193b4be Poul Sander 2025-02-03 21:28 39
//
1193b4be Poul Sander 2025-02-03 21:28 40
// VERSION HISTORY
1193b4be Poul Sander 2025-02-03 21:28 41
//
1193b4be Poul Sander 2025-02-03 21:28 42
//   1.14 (2021-07-11) page up/down, various fixes
1193b4be Poul Sander 2025-02-03 21:28 43
//   1.13 (2019-02-07) fix bug in undo size management
1193b4be Poul Sander 2025-02-03 21:28 44
//   1.12 (2018-01-29) user can change STB_TEXTEDIT_KEYTYPE, fix redo to avoid crash
1193b4be Poul Sander 2025-02-03 21:28 45
//   1.11 (2017-03-03) fix HOME on last line, dragging off single-line textfield
1193b4be Poul Sander 2025-02-03 21:28 46
//   1.10 (2016-10-25) suppress warnings about casting away const with -Wcast-qual
1193b4be Poul Sander 2025-02-03 21:28 47
//   1.9  (2016-08-27) customizable move-by-word
1193b4be Poul Sander 2025-02-03 21:28 48
//   1.8  (2016-04-02) better keyboard handling when mouse button is down
1193b4be Poul Sander 2025-02-03 21:28 49
//   1.7  (2015-09-13) change y range handling in case baseline is non-0
1193b4be Poul Sander 2025-02-03 21:28 50
//   1.6  (2015-04-15) allow STB_TEXTEDIT_memmove
1193b4be Poul Sander 2025-02-03 21:28 51
//   1.5  (2014-09-10) add support for secondary keys for OS X
1193b4be Poul Sander 2025-02-03 21:28 52
//   1.4  (2014-08-17) fix signed/unsigned warnings
1193b4be Poul Sander 2025-02-03 21:28 53
//   1.3  (2014-06-19) fix mouse clicking to round to nearest char boundary
1193b4be Poul Sander 2025-02-03 21:28 54
//   1.2  (2014-05-27) fix some RAD types that had crept into the new code
1193b4be Poul Sander 2025-02-03 21:28 55
//   1.1  (2013-12-15) move-by-word (requires STB_TEXTEDIT_IS_SPACE )
1193b4be Poul Sander 2025-02-03 21:28 56
//   1.0  (2012-07-26) improve documentation, initial public release
1193b4be Poul Sander 2025-02-03 21:28 57
//   0.3  (2012-02-24) bugfixes, single-line mode; insert mode
1193b4be Poul Sander 2025-02-03 21:28 58
//   0.2  (2011-11-28) fixes to undo/redo
1193b4be Poul Sander 2025-02-03 21:28 59
//   0.1  (2010-07-08) initial version
1193b4be Poul Sander 2025-02-03 21:28 60
//
1193b4be Poul Sander 2025-02-03 21:28 61
// ADDITIONAL CONTRIBUTORS
1193b4be Poul Sander 2025-02-03 21:28 62
//
1193b4be Poul Sander 2025-02-03 21:28 63
//   Ulf Winklemann: move-by-word in 1.1
1193b4be Poul Sander 2025-02-03 21:28 64
//   Fabian Giesen: secondary key inputs in 1.5
1193b4be Poul Sander 2025-02-03 21:28 65
//   Martins Mozeiko: STB_TEXTEDIT_memmove in 1.6
1193b4be Poul Sander 2025-02-03 21:28 66
//   Louis Schnellbach: page up/down in 1.14
1193b4be Poul Sander 2025-02-03 21:28 67
//
1193b4be Poul Sander 2025-02-03 21:28 68
//   Bugfixes:
1193b4be Poul Sander 2025-02-03 21:28 69
//      Scott Graham
1193b4be Poul Sander 2025-02-03 21:28 70
//      Daniel Keller
1193b4be Poul Sander 2025-02-03 21:28 71
//      Omar Cornut
1193b4be Poul Sander 2025-02-03 21:28 72
//      Dan Thompson
1193b4be Poul Sander 2025-02-03 21:28 73
//
1193b4be Poul Sander 2025-02-03 21:28 74
// USAGE
1193b4be Poul Sander 2025-02-03 21:28 75
//
1193b4be Poul Sander 2025-02-03 21:28 76
// This file behaves differently depending on what symbols you define
1193b4be Poul Sander 2025-02-03 21:28 77
// before including it.
1193b4be Poul Sander 2025-02-03 21:28 78
//
1193b4be Poul Sander 2025-02-03 21:28 79
//
1193b4be Poul Sander 2025-02-03 21:28 80
// Header-file mode:
1193b4be Poul Sander 2025-02-03 21:28 81
//
1193b4be Poul Sander 2025-02-03 21:28 82
//   If you do not define STB_TEXTEDIT_IMPLEMENTATION before including this,
1193b4be Poul Sander 2025-02-03 21:28 83
//   it will operate in "header file" mode. In this mode, it declares a
1193b4be Poul Sander 2025-02-03 21:28 84
//   single public symbol, STB_TexteditState, which encapsulates the current
1193b4be Poul Sander 2025-02-03 21:28 85
//   state of a text widget (except for the string, which you will store
1193b4be Poul Sander 2025-02-03 21:28 86
//   separately).
1193b4be Poul Sander 2025-02-03 21:28 87
//
1193b4be Poul Sander 2025-02-03 21:28 88
//   To compile in this mode, you must define STB_TEXTEDIT_CHARTYPE to a
1193b4be Poul Sander 2025-02-03 21:28 89
//   primitive type that defines a single character (e.g. char, wchar_t, etc).
1193b4be Poul Sander 2025-02-03 21:28 90
//
1193b4be Poul Sander 2025-02-03 21:28 91
//   To save space or increase undo-ability, you can optionally define the
1193b4be Poul Sander 2025-02-03 21:28 92
//   following things that are used by the undo system:
1193b4be Poul Sander 2025-02-03 21:28 93
//
1193b4be Poul Sander 2025-02-03 21:28 94
//      STB_TEXTEDIT_POSITIONTYPE         small int type encoding a valid cursor position
1193b4be Poul Sander 2025-02-03 21:28 95
//      STB_TEXTEDIT_UNDOSTATECOUNT       the number of undo states to allow
1193b4be Poul Sander 2025-02-03 21:28 96
//      STB_TEXTEDIT_UNDOCHARCOUNT        the number of characters to store in the undo buffer
1193b4be Poul Sander 2025-02-03 21:28 97
//
1193b4be Poul Sander 2025-02-03 21:28 98
//   If you don't define these, they are set to permissive types and
1193b4be Poul Sander 2025-02-03 21:28 99
//   moderate sizes. The undo system does no memory allocations, so
1193b4be Poul Sander 2025-02-03 21:28 100
//   it grows STB_TexteditState by the worst-case storage which is (in bytes):
1193b4be Poul Sander 2025-02-03 21:28 101
//
1193b4be Poul Sander 2025-02-03 21:28 102
//        [4 + 3 * sizeof(STB_TEXTEDIT_POSITIONTYPE)] * STB_TEXTEDIT_UNDOSTATECOUNT
1193b4be Poul Sander 2025-02-03 21:28 103
//      +          sizeof(STB_TEXTEDIT_CHARTYPE)      * STB_TEXTEDIT_UNDOCHARCOUNT
1193b4be Poul Sander 2025-02-03 21:28 104
//
1193b4be Poul Sander 2025-02-03 21:28 105
//
1193b4be Poul Sander 2025-02-03 21:28 106
// Implementation mode:
1193b4be Poul Sander 2025-02-03 21:28 107
//
1193b4be Poul Sander 2025-02-03 21:28 108
//   If you define STB_TEXTEDIT_IMPLEMENTATION before including this, it
1193b4be Poul Sander 2025-02-03 21:28 109
//   will compile the implementation of the text edit widget, depending
1193b4be Poul Sander 2025-02-03 21:28 110
//   on a large number of symbols which must be defined before the include.
1193b4be Poul Sander 2025-02-03 21:28 111
//
1193b4be Poul Sander 2025-02-03 21:28 112
//   The implementation is defined only as static functions. You will then
1193b4be Poul Sander 2025-02-03 21:28 113
//   need to provide your own APIs in the same file which will access the
1193b4be Poul Sander 2025-02-03 21:28 114
//   static functions.
1193b4be Poul Sander 2025-02-03 21:28 115
//
1193b4be Poul Sander 2025-02-03 21:28 116
//   The basic concept is that you provide a "string" object which
1193b4be Poul Sander 2025-02-03 21:28 117
//   behaves like an array of characters. stb_textedit uses indices to
1193b4be Poul Sander 2025-02-03 21:28 118
//   refer to positions in the string, implicitly representing positions
1193b4be Poul Sander 2025-02-03 21:28 119
//   in the displayed textedit. This is true for both plain text and
1193b4be Poul Sander 2025-02-03 21:28 120
//   rich text; even with rich text stb_truetype interacts with your
1193b4be Poul Sander 2025-02-03 21:28 121
//   code as if there was an array of all the displayed characters.
1193b4be Poul Sander 2025-02-03 21:28 122
//
1193b4be Poul Sander 2025-02-03 21:28 123
// Symbols that must be the same in header-file and implementation mode:
1193b4be Poul Sander 2025-02-03 21:28 124
//
1193b4be Poul Sander 2025-02-03 21:28 125
//     STB_TEXTEDIT_CHARTYPE             the character type
1193b4be Poul Sander 2025-02-03 21:28 126
//     STB_TEXTEDIT_POSITIONTYPE         small type that is a valid cursor position
1193b4be Poul Sander 2025-02-03 21:28 127
//     STB_TEXTEDIT_UNDOSTATECOUNT       the number of undo states to allow
1193b4be Poul Sander 2025-02-03 21:28 128
//     STB_TEXTEDIT_UNDOCHARCOUNT        the number of characters to store in the undo buffer
1193b4be Poul Sander 2025-02-03 21:28 129
//
1193b4be Poul Sander 2025-02-03 21:28 130
// Symbols you must define for implementation mode:
1193b4be Poul Sander 2025-02-03 21:28 131
//
1193b4be Poul Sander 2025-02-03 21:28 132
//    STB_TEXTEDIT_STRING               the type of object representing a string being edited,
1193b4be Poul Sander 2025-02-03 21:28 133
//                                      typically this is a wrapper object with other data you need
1193b4be Poul Sander 2025-02-03 21:28 134
//
1193b4be Poul Sander 2025-02-03 21:28 135
//    STB_TEXTEDIT_STRINGLEN(obj)       the length of the string (ideally O(1))
1193b4be Poul Sander 2025-02-03 21:28 136
//    STB_TEXTEDIT_LAYOUTROW(&r,obj,n)  returns the results of laying out a line of characters
1193b4be Poul Sander 2025-02-03 21:28 137
//                                        starting from character #n (see discussion below)
1193b4be Poul Sander 2025-02-03 21:28 138
//    STB_TEXTEDIT_GETWIDTH(obj,n,i)    returns the pixel delta from the xpos of the i'th character
1193b4be Poul Sander 2025-02-03 21:28 139
//                                        to the xpos of the i+1'th char for a line of characters
1193b4be Poul Sander 2025-02-03 21:28 140
//                                        starting at character #n (i.e. accounts for kerning
1193b4be Poul Sander 2025-02-03 21:28 141
//                                        with previous char)
1193b4be Poul Sander 2025-02-03 21:28 142
//    STB_TEXTEDIT_KEYTOTEXT(k)         maps a keyboard input to an insertable character
1193b4be Poul Sander 2025-02-03 21:28 143
//                                        (return type is int, -1 means not valid to insert)
4e9e2555 Poul Sander 2025-10-05 10:15 144
//                                        (not supported if you want to use UTF-8, see below)
1193b4be Poul Sander 2025-02-03 21:28 145
//    STB_TEXTEDIT_GETCHAR(obj,i)       returns the i'th character of obj, 0-based
1193b4be Poul Sander 2025-02-03 21:28 146
//    STB_TEXTEDIT_NEWLINE              the character returned by _GETCHAR() we recognize
1193b4be Poul Sander 2025-02-03 21:28 147
//                                        as manually wordwrapping for end-of-line positioning
1193b4be Poul Sander 2025-02-03 21:28 148
//
1193b4be Poul Sander 2025-02-03 21:28 149
//    STB_TEXTEDIT_DELETECHARS(obj,i,n)      delete n characters starting at i
1193b4be Poul Sander 2025-02-03 21:28 150
//    STB_TEXTEDIT_INSERTCHARS(obj,i,c*,n)   insert n characters at i (pointed to by STB_TEXTEDIT_CHARTYPE*)
1193b4be Poul Sander 2025-02-03 21:28 151
//
1193b4be Poul Sander 2025-02-03 21:28 152
//    STB_TEXTEDIT_K_SHIFT       a power of two that is or'd in to a keyboard input to represent the shift key
1193b4be Poul Sander 2025-02-03 21:28 153
//
1193b4be Poul Sander 2025-02-03 21:28 154
//    STB_TEXTEDIT_K_LEFT        keyboard input to move cursor left
1193b4be Poul Sander 2025-02-03 21:28 155
//    STB_TEXTEDIT_K_RIGHT       keyboard input to move cursor right
1193b4be Poul Sander 2025-02-03 21:28 156
//    STB_TEXTEDIT_K_UP          keyboard input to move cursor up
1193b4be Poul Sander 2025-02-03 21:28 157
//    STB_TEXTEDIT_K_DOWN        keyboard input to move cursor down
1193b4be Poul Sander 2025-02-03 21:28 158
//    STB_TEXTEDIT_K_PGUP        keyboard input to move cursor up a page
1193b4be Poul Sander 2025-02-03 21:28 159
//    STB_TEXTEDIT_K_PGDOWN      keyboard input to move cursor down a page
1193b4be Poul Sander 2025-02-03 21:28 160
//    STB_TEXTEDIT_K_LINESTART   keyboard input to move cursor to start of line  // e.g. HOME
1193b4be Poul Sander 2025-02-03 21:28 161
//    STB_TEXTEDIT_K_LINEEND     keyboard input to move cursor to end of line    // e.g. END
1193b4be Poul Sander 2025-02-03 21:28 162
//    STB_TEXTEDIT_K_TEXTSTART   keyboard input to move cursor to start of text  // e.g. ctrl-HOME
1193b4be Poul Sander 2025-02-03 21:28 163
//    STB_TEXTEDIT_K_TEXTEND     keyboard input to move cursor to end of text    // e.g. ctrl-END
1193b4be Poul Sander 2025-02-03 21:28 164
//    STB_TEXTEDIT_K_DELETE      keyboard input to delete selection or character under cursor
1193b4be Poul Sander 2025-02-03 21:28 165
//    STB_TEXTEDIT_K_BACKSPACE   keyboard input to delete selection or character left of cursor
1193b4be Poul Sander 2025-02-03 21:28 166
//    STB_TEXTEDIT_K_UNDO        keyboard input to perform undo
1193b4be Poul Sander 2025-02-03 21:28 167
//    STB_TEXTEDIT_K_REDO        keyboard input to perform redo
1193b4be Poul Sander 2025-02-03 21:28 168
//
1193b4be Poul Sander 2025-02-03 21:28 169
// Optional:
1193b4be Poul Sander 2025-02-03 21:28 170
//    STB_TEXTEDIT_K_INSERT              keyboard input to toggle insert mode
1193b4be Poul Sander 2025-02-03 21:28 171
//    STB_TEXTEDIT_IS_SPACE(ch)          true if character is whitespace (e.g. 'isspace'),
1193b4be Poul Sander 2025-02-03 21:28 172
//                                          required for default WORDLEFT/WORDRIGHT handlers
1193b4be Poul Sander 2025-02-03 21:28 173
//    STB_TEXTEDIT_MOVEWORDLEFT(obj,i)   custom handler for WORDLEFT, returns index to move cursor to
1193b4be Poul Sander 2025-02-03 21:28 174
//    STB_TEXTEDIT_MOVEWORDRIGHT(obj,i)  custom handler for WORDRIGHT, returns index to move cursor to
1193b4be Poul Sander 2025-02-03 21:28 175
//    STB_TEXTEDIT_K_WORDLEFT            keyboard input to move cursor left one word // e.g. ctrl-LEFT
1193b4be Poul Sander 2025-02-03 21:28 176
//    STB_TEXTEDIT_K_WORDRIGHT           keyboard input to move cursor right one word // e.g. ctrl-RIGHT
1193b4be Poul Sander 2025-02-03 21:28 177
//    STB_TEXTEDIT_K_LINESTART2          secondary keyboard input to move cursor to start of line
1193b4be Poul Sander 2025-02-03 21:28 178
//    STB_TEXTEDIT_K_LINEEND2            secondary keyboard input to move cursor to end of line
1193b4be Poul Sander 2025-02-03 21:28 179
//    STB_TEXTEDIT_K_TEXTSTART2          secondary keyboard input to move cursor to start of text
1193b4be Poul Sander 2025-02-03 21:28 180
//    STB_TEXTEDIT_K_TEXTEND2            secondary keyboard input to move cursor to end of text
1193b4be Poul Sander 2025-02-03 21:28 181
//
4e9e2555 Poul Sander 2025-10-05 10:15 182
// To support UTF-8:
4e9e2555 Poul Sander 2025-10-05 10:15 183
//
4e9e2555 Poul Sander 2025-10-05 10:15 184
//    STB_TEXTEDIT_GETPREVCHARINDEX      returns index of previous character
4e9e2555 Poul Sander 2025-10-05 10:15 185
//    STB_TEXTEDIT_GETNEXTCHARINDEX      returns index of next character
4e9e2555 Poul Sander 2025-10-05 10:15 186
//    Do NOT define STB_TEXTEDIT_KEYTOTEXT.
4e9e2555 Poul Sander 2025-10-05 10:15 187
//    Instead, call stb_textedit_text() directly for text contents.
4e9e2555 Poul Sander 2025-10-05 10:15 188
//
1193b4be Poul Sander 2025-02-03 21:28 189
// Keyboard input must be encoded as a single integer value; e.g. a character code
1193b4be Poul Sander 2025-02-03 21:28 190
// and some bitflags that represent shift states. to simplify the interface, SHIFT must
1193b4be Poul Sander 2025-02-03 21:28 191
// be a bitflag, so we can test the shifted state of cursor movements to allow selection,
1193b4be Poul Sander 2025-02-03 21:28 192
// i.e. (STB_TEXTEDIT_K_RIGHT|STB_TEXTEDIT_K_SHIFT) should be shifted right-arrow.
1193b4be Poul Sander 2025-02-03 21:28 193
//
1193b4be Poul Sander 2025-02-03 21:28 194
// You can encode other things, such as CONTROL or ALT, in additional bits, and
1193b4be Poul Sander 2025-02-03 21:28 195
// then test for their presence in e.g. STB_TEXTEDIT_K_WORDLEFT. For example,
1193b4be Poul Sander 2025-02-03 21:28 196
// my Windows implementations add an additional CONTROL bit, and an additional KEYDOWN
1193b4be Poul Sander 2025-02-03 21:28 197
// bit. Then all of the STB_TEXTEDIT_K_ values bitwise-or in the KEYDOWN bit,
1193b4be Poul Sander 2025-02-03 21:28 198
// and I pass both WM_KEYDOWN and WM_CHAR events to the "key" function in the
1193b4be Poul Sander 2025-02-03 21:28 199
// API below. The control keys will only match WM_KEYDOWN events because of the
1193b4be Poul Sander 2025-02-03 21:28 200
// keydown bit I add, and STB_TEXTEDIT_KEYTOTEXT only tests for the KEYDOWN
1193b4be Poul Sander 2025-02-03 21:28 201
// bit so it only decodes WM_CHAR events.
1193b4be Poul Sander 2025-02-03 21:28 202
//
1193b4be Poul Sander 2025-02-03 21:28 203
// STB_TEXTEDIT_LAYOUTROW returns information about the shape of one displayed
1193b4be Poul Sander 2025-02-03 21:28 204
// row of characters assuming they start on the i'th character--the width and
1193b4be Poul Sander 2025-02-03 21:28 205
// the height and the number of characters consumed. This allows this library
1193b4be Poul Sander 2025-02-03 21:28 206
// to traverse the entire layout incrementally. You need to compute word-wrapping
1193b4be Poul Sander 2025-02-03 21:28 207
// here.
1193b4be Poul Sander 2025-02-03 21:28 208
//
1193b4be Poul Sander 2025-02-03 21:28 209
// Each textfield keeps its own insert mode state, which is not how normal
1193b4be Poul Sander 2025-02-03 21:28 210
// applications work. To keep an app-wide insert mode, update/copy the
1193b4be Poul Sander 2025-02-03 21:28 211
// "insert_mode" field of STB_TexteditState before/after calling API functions.
1193b4be Poul Sander 2025-02-03 21:28 212
//
1193b4be Poul Sander 2025-02-03 21:28 213
// API
1193b4be Poul Sander 2025-02-03 21:28 214
//
1193b4be Poul Sander 2025-02-03 21:28 215
//    void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line)
1193b4be Poul Sander 2025-02-03 21:28 216
//
1193b4be Poul Sander 2025-02-03 21:28 217
//    void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)
1193b4be Poul Sander 2025-02-03 21:28 218
//    void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)
1193b4be Poul Sander 2025-02-03 21:28 219
//    int  stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)
1193b4be Poul Sander 2025-02-03 21:28 220
//    int  stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len)
1193b4be Poul Sander 2025-02-03 21:28 221
//    void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXEDIT_KEYTYPE key)
1193b4be Poul Sander 2025-02-03 21:28 222
//    void stb_textedit_text(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int text_len)
1193b4be Poul Sander 2025-02-03 21:28 223
//
1193b4be Poul Sander 2025-02-03 21:28 224
//    Each of these functions potentially updates the string and updates the
1193b4be Poul Sander 2025-02-03 21:28 225
//    state.
1193b4be Poul Sander 2025-02-03 21:28 226
//
1193b4be Poul Sander 2025-02-03 21:28 227
//      initialize_state:
1193b4be Poul Sander 2025-02-03 21:28 228
//          set the textedit state to a known good default state when initially
1193b4be Poul Sander 2025-02-03 21:28 229
//          constructing the textedit.
1193b4be Poul Sander 2025-02-03 21:28 230
//
1193b4be Poul Sander 2025-02-03 21:28 231
//      click:
1193b4be Poul Sander 2025-02-03 21:28 232
//          call this with the mouse x,y on a mouse down; it will update the cursor
1193b4be Poul Sander 2025-02-03 21:28 233
//          and reset the selection start/end to the cursor point. the x,y must
1193b4be Poul Sander 2025-02-03 21:28 234
//          be relative to the text widget, with (0,0) being the top left.
1193b4be Poul Sander 2025-02-03 21:28 235
//
1193b4be Poul Sander 2025-02-03 21:28 236
//      drag:
1193b4be Poul Sander 2025-02-03 21:28 237
//          call this with the mouse x,y on a mouse drag/up; it will update the
1193b4be Poul Sander 2025-02-03 21:28 238
//          cursor and the selection end point
1193b4be Poul Sander 2025-02-03 21:28 239
//
1193b4be Poul Sander 2025-02-03 21:28 240
//      cut:
1193b4be Poul Sander 2025-02-03 21:28 241
//          call this to delete the current selection; returns true if there was
1193b4be Poul Sander 2025-02-03 21:28 242
//          one. you should FIRST copy the current selection to the system paste buffer.
1193b4be Poul Sander 2025-02-03 21:28 243
//          (To copy, just copy the current selection out of the string yourself.)
1193b4be Poul Sander 2025-02-03 21:28 244
//
1193b4be Poul Sander 2025-02-03 21:28 245
//      paste:
1193b4be Poul Sander 2025-02-03 21:28 246
//          call this to paste text at the current cursor point or over the current
1193b4be Poul Sander 2025-02-03 21:28 247
//          selection if there is one.
1193b4be Poul Sander 2025-02-03 21:28 248
//
1193b4be Poul Sander 2025-02-03 21:28 249
//      key:
1193b4be Poul Sander 2025-02-03 21:28 250
//          call this for keyboard inputs sent to the textfield. you can use it
1193b4be Poul Sander 2025-02-03 21:28 251
//          for "key down" events or for "translated" key events. if you need to
1193b4be Poul Sander 2025-02-03 21:28 252
//          do both (as in Win32), or distinguish Unicode characters from control
1193b4be Poul Sander 2025-02-03 21:28 253
//          inputs, set a high bit to distinguish the two; then you can define the
1193b4be Poul Sander 2025-02-03 21:28 254
//          various definitions like STB_TEXTEDIT_K_LEFT have the is-key-event bit
1193b4be Poul Sander 2025-02-03 21:28 255
//          set, and make STB_TEXTEDIT_KEYTOCHAR check that the is-key-event bit is
1193b4be Poul Sander 2025-02-03 21:28 256
//          clear. STB_TEXTEDIT_KEYTYPE defaults to int, but you can #define it to
1193b4be Poul Sander 2025-02-03 21:28 257
//          anything other type you want before including.
1193b4be Poul Sander 2025-02-03 21:28 258
//          if the STB_TEXTEDIT_KEYTOTEXT function is defined, selected keys are
1193b4be Poul Sander 2025-02-03 21:28 259
//          transformed into text and stb_textedit_text() is automatically called.
1193b4be Poul Sander 2025-02-03 21:28 260
//
4e9e2555 Poul Sander 2025-10-05 10:15 261
//      text: (added 2025)
4e9e2555 Poul Sander 2025-10-05 10:15 262
//          call this to directly send text input the textfield, which is required
4e9e2555 Poul Sander 2025-10-05 10:15 263
//          for UTF-8 support, because stb_textedit_key() + STB_TEXTEDIT_KEYTOTEXT()
4e9e2555 Poul Sander 2025-10-05 10:15 264
//          cannot infer text length.
1193b4be Poul Sander 2025-02-03 21:28 265
//
1193b4be Poul Sander 2025-02-03 21:28 266
//
1193b4be Poul Sander 2025-02-03 21:28 267
//   When rendering, you can read the cursor position and selection state from
1193b4be Poul Sander 2025-02-03 21:28 268
//   the STB_TexteditState.
1193b4be Poul Sander 2025-02-03 21:28 269
//
1193b4be Poul Sander 2025-02-03 21:28 270
//
1193b4be Poul Sander 2025-02-03 21:28 271
// Notes:
1193b4be Poul Sander 2025-02-03 21:28 272
//
1193b4be Poul Sander 2025-02-03 21:28 273
// This is designed to be usable in IMGUI, so it allows for the possibility of
1193b4be Poul Sander 2025-02-03 21:28 274
// running in an IMGUI that has NOT cached the multi-line layout. For this
1193b4be Poul Sander 2025-02-03 21:28 275
// reason, it provides an interface that is compatible with computing the
1193b4be Poul Sander 2025-02-03 21:28 276
// layout incrementally--we try to make sure we make as few passes through
1193b4be Poul Sander 2025-02-03 21:28 277
// as possible. (For example, to locate the mouse pointer in the text, we
1193b4be Poul Sander 2025-02-03 21:28 278
// could define functions that return the X and Y positions of characters
1193b4be Poul Sander 2025-02-03 21:28 279
// and binary search Y and then X, but if we're doing dynamic layout this
1193b4be Poul Sander 2025-02-03 21:28 280
// will run the layout algorithm many times, so instead we manually search
1193b4be Poul Sander 2025-02-03 21:28 281
// forward in one pass. Similar logic applies to e.g. up-arrow and
1193b4be Poul Sander 2025-02-03 21:28 282
// down-arrow movement.)
1193b4be Poul Sander 2025-02-03 21:28 283
//
1193b4be Poul Sander 2025-02-03 21:28 284
// If it's run in a widget that *has* cached the layout, then this is less
1193b4be Poul Sander 2025-02-03 21:28 285
// efficient, but it's not horrible on modern computers. But you wouldn't
1193b4be Poul Sander 2025-02-03 21:28 286
// want to edit million-line files with it.
1193b4be Poul Sander 2025-02-03 21:28 287
1193b4be Poul Sander 2025-02-03 21:28 288
1193b4be Poul Sander 2025-02-03 21:28 289
////////////////////////////////////////////////////////////////////////////
1193b4be Poul Sander 2025-02-03 21:28 290
////////////////////////////////////////////////////////////////////////////
1193b4be Poul Sander 2025-02-03 21:28 291
////
1193b4be Poul Sander 2025-02-03 21:28 292
////   Header-file mode
1193b4be Poul Sander 2025-02-03 21:28 293
////
1193b4be Poul Sander 2025-02-03 21:28 294
////
1193b4be Poul Sander 2025-02-03 21:28 295
1193b4be Poul Sander 2025-02-03 21:28 296
#ifndef INCLUDE_IMSTB_TEXTEDIT_H
1193b4be Poul Sander 2025-02-03 21:28 297
#define INCLUDE_IMSTB_TEXTEDIT_H
1193b4be Poul Sander 2025-02-03 21:28 298
1193b4be Poul Sander 2025-02-03 21:28 299
////////////////////////////////////////////////////////////////////////
1193b4be Poul Sander 2025-02-03 21:28 300
//
1193b4be Poul Sander 2025-02-03 21:28 301
//     STB_TexteditState
1193b4be Poul Sander 2025-02-03 21:28 302
//
1193b4be Poul Sander 2025-02-03 21:28 303
// Definition of STB_TexteditState which you should store
1193b4be Poul Sander 2025-02-03 21:28 304
// per-textfield; it includes cursor position, selection state,
1193b4be Poul Sander 2025-02-03 21:28 305
// and undo state.
1193b4be Poul Sander 2025-02-03 21:28 306
//
1193b4be Poul Sander 2025-02-03 21:28 307
1193b4be Poul Sander 2025-02-03 21:28 308
#ifndef IMSTB_TEXTEDIT_UNDOSTATECOUNT
1193b4be Poul Sander 2025-02-03 21:28 309
#define IMSTB_TEXTEDIT_UNDOSTATECOUNT   99
1193b4be Poul Sander 2025-02-03 21:28 310
#endif
1193b4be Poul Sander 2025-02-03 21:28 311
#ifndef IMSTB_TEXTEDIT_UNDOCHARCOUNT
1193b4be Poul Sander 2025-02-03 21:28 312
#define IMSTB_TEXTEDIT_UNDOCHARCOUNT   999
1193b4be Poul Sander 2025-02-03 21:28 313
#endif
1193b4be Poul Sander 2025-02-03 21:28 314
#ifndef IMSTB_TEXTEDIT_CHARTYPE
1193b4be Poul Sander 2025-02-03 21:28 315
#define IMSTB_TEXTEDIT_CHARTYPE        int
1193b4be Poul Sander 2025-02-03 21:28 316
#endif
1193b4be Poul Sander 2025-02-03 21:28 317
#ifndef IMSTB_TEXTEDIT_POSITIONTYPE
1193b4be Poul Sander 2025-02-03 21:28 318
#define IMSTB_TEXTEDIT_POSITIONTYPE    int
1193b4be Poul Sander 2025-02-03 21:28 319
#endif
1193b4be Poul Sander 2025-02-03 21:28 320
1193b4be Poul Sander 2025-02-03 21:28 321
typedef struct
1193b4be Poul Sander 2025-02-03 21:28 322
{
1193b4be Poul Sander 2025-02-03 21:28 323
   // private data
1193b4be Poul Sander 2025-02-03 21:28 324
   IMSTB_TEXTEDIT_POSITIONTYPE  where;
1193b4be Poul Sander 2025-02-03 21:28 325
   IMSTB_TEXTEDIT_POSITIONTYPE  insert_length;
1193b4be Poul Sander 2025-02-03 21:28 326
   IMSTB_TEXTEDIT_POSITIONTYPE  delete_length;
1193b4be Poul Sander 2025-02-03 21:28 327
   int                        char_storage;
1193b4be Poul Sander 2025-02-03 21:28 328
} StbUndoRecord;
1193b4be Poul Sander 2025-02-03 21:28 329
1193b4be Poul Sander 2025-02-03 21:28 330
typedef struct
1193b4be Poul Sander 2025-02-03 21:28 331
{
1193b4be Poul Sander 2025-02-03 21:28 332
   // private data
1193b4be Poul Sander 2025-02-03 21:28 333
   StbUndoRecord          undo_rec [IMSTB_TEXTEDIT_UNDOSTATECOUNT];
1193b4be Poul Sander 2025-02-03 21:28 334
   IMSTB_TEXTEDIT_CHARTYPE  undo_char[IMSTB_TEXTEDIT_UNDOCHARCOUNT];
1193b4be Poul Sander 2025-02-03 21:28 335
   short undo_point, redo_point;
1193b4be Poul Sander 2025-02-03 21:28 336
   int undo_char_point, redo_char_point;
1193b4be Poul Sander 2025-02-03 21:28 337
} StbUndoState;
1193b4be Poul Sander 2025-02-03 21:28 338
1193b4be Poul Sander 2025-02-03 21:28 339
typedef struct STB_TexteditState
1193b4be Poul Sander 2025-02-03 21:28 340
{
1193b4be Poul Sander 2025-02-03 21:28 341
   /////////////////////
1193b4be Poul Sander 2025-02-03 21:28 342
   //
1193b4be Poul Sander 2025-02-03 21:28 343
   // public data
1193b4be Poul Sander 2025-02-03 21:28 344
   //
1193b4be Poul Sander 2025-02-03 21:28 345
1193b4be Poul Sander 2025-02-03 21:28 346
   int cursor;
1193b4be Poul Sander 2025-02-03 21:28 347
   // position of the text cursor within the string
1193b4be Poul Sander 2025-02-03 21:28 348
1193b4be Poul Sander 2025-02-03 21:28 349
   int select_start;          // selection start point
1193b4be Poul Sander 2025-02-03 21:28 350
   int select_end;
1193b4be Poul Sander 2025-02-03 21:28 351
   // selection start and end point in characters; if equal, no selection.
1193b4be Poul Sander 2025-02-03 21:28 352
   // note that start may be less than or greater than end (e.g. when
1193b4be Poul Sander 2025-02-03 21:28 353
   // dragging the mouse, start is where the initial click was, and you
1193b4be Poul Sander 2025-02-03 21:28 354
   // can drag in either direction)
1193b4be Poul Sander 2025-02-03 21:28 355
1193b4be Poul Sander 2025-02-03 21:28 356
   unsigned char insert_mode;
1193b4be Poul Sander 2025-02-03 21:28 357
   // each textfield keeps its own insert mode state. to keep an app-wide
1193b4be Poul Sander 2025-02-03 21:28 358
   // insert mode, copy this value in/out of the app state
1193b4be Poul Sander 2025-02-03 21:28 359
1193b4be Poul Sander 2025-02-03 21:28 360
   int row_count_per_page;
1193b4be Poul Sander 2025-02-03 21:28 361
   // page size in number of row.
1193b4be Poul Sander 2025-02-03 21:28 362
   // this value MUST be set to >0 for pageup or pagedown in multilines documents.
1193b4be Poul Sander 2025-02-03 21:28 363
1193b4be Poul Sander 2025-02-03 21:28 364
   /////////////////////
1193b4be Poul Sander 2025-02-03 21:28 365
   //
1193b4be Poul Sander 2025-02-03 21:28 366
   // private data
1193b4be Poul Sander 2025-02-03 21:28 367
   //
1193b4be Poul Sander 2025-02-03 21:28 368
   unsigned char cursor_at_end_of_line; // not implemented yet
1193b4be Poul Sander 2025-02-03 21:28 369
   unsigned char initialized;
1193b4be Poul Sander 2025-02-03 21:28 370
   unsigned char has_preferred_x;
1193b4be Poul Sander 2025-02-03 21:28 371
   unsigned char single_line;
1193b4be Poul Sander 2025-02-03 21:28 372
   unsigned char padding1, padding2, padding3;
1193b4be Poul Sander 2025-02-03 21:28 373
   float preferred_x; // this determines where the cursor up/down tries to seek to along x
1193b4be Poul Sander 2025-02-03 21:28 374
   StbUndoState undostate;
1193b4be Poul Sander 2025-02-03 21:28 375
} STB_TexteditState;
1193b4be Poul Sander 2025-02-03 21:28 376
1193b4be Poul Sander 2025-02-03 21:28 377
1193b4be Poul Sander 2025-02-03 21:28 378
////////////////////////////////////////////////////////////////////////
1193b4be Poul Sander 2025-02-03 21:28 379
//
1193b4be Poul Sander 2025-02-03 21:28 380
//     StbTexteditRow
1193b4be Poul Sander 2025-02-03 21:28 381
//
1193b4be Poul Sander 2025-02-03 21:28 382
// Result of layout query, used by stb_textedit to determine where
1193b4be Poul Sander 2025-02-03 21:28 383
// the text in each row is.
1193b4be Poul Sander 2025-02-03 21:28 384
1193b4be Poul Sander 2025-02-03 21:28 385
// result of layout query
1193b4be Poul Sander 2025-02-03 21:28 386
typedef struct
1193b4be Poul Sander 2025-02-03 21:28 387
{
1193b4be Poul Sander 2025-02-03 21:28 388
   float x0,x1;             // starting x location, end x location (allows for align=right, etc)
1193b4be Poul Sander 2025-02-03 21:28 389
   float baseline_y_delta;  // position of baseline relative to previous row's baseline
1193b4be Poul Sander 2025-02-03 21:28 390
   float ymin,ymax;         // height of row above and below baseline
1193b4be Poul Sander 2025-02-03 21:28 391
   int num_chars;
1193b4be Poul Sander 2025-02-03 21:28 392
} StbTexteditRow;
1193b4be Poul Sander 2025-02-03 21:28 393
#endif //INCLUDE_IMSTB_TEXTEDIT_H
1193b4be Poul Sander 2025-02-03 21:28 394
1193b4be Poul Sander 2025-02-03 21:28 395
1193b4be Poul Sander 2025-02-03 21:28 396
////////////////////////////////////////////////////////////////////////////
1193b4be Poul Sander 2025-02-03 21:28 397
////////////////////////////////////////////////////////////////////////////
1193b4be Poul Sander 2025-02-03 21:28 398
////
1193b4be Poul Sander 2025-02-03 21:28 399
////   Implementation mode
1193b4be Poul Sander 2025-02-03 21:28 400
////
1193b4be Poul Sander 2025-02-03 21:28 401
////
1193b4be Poul Sander 2025-02-03 21:28 402
1193b4be Poul Sander 2025-02-03 21:28 403
1193b4be Poul Sander 2025-02-03 21:28 404
// implementation isn't include-guarded, since it might have indirectly
1193b4be Poul Sander 2025-02-03 21:28 405
// included just the "header" portion
1193b4be Poul Sander 2025-02-03 21:28 406
#ifdef IMSTB_TEXTEDIT_IMPLEMENTATION
1193b4be Poul Sander 2025-02-03 21:28 407
1193b4be Poul Sander 2025-02-03 21:28 408
#ifndef IMSTB_TEXTEDIT_memmove
1193b4be Poul Sander 2025-02-03 21:28 409
#include <string.h>
1193b4be Poul Sander 2025-02-03 21:28 410
#define IMSTB_TEXTEDIT_memmove memmove
1193b4be Poul Sander 2025-02-03 21:28 411
#endif
1193b4be Poul Sander 2025-02-03 21:28 412
4e9e2555 Poul Sander 2025-10-05 10:15 413
// [DEAR IMGUI]
4e9e2555 Poul Sander 2025-10-05 10:15 414
// Functions must be implemented for UTF8 support
4e9e2555 Poul Sander 2025-10-05 10:15 415
// Code in this file that uses those functions is modified for [DEAR IMGUI] and deviates from the original stb_textedit.
4e9e2555 Poul Sander 2025-10-05 10:15 416
// There is not necessarily a '[DEAR IMGUI]' at the usage sites.
4e9e2555 Poul Sander 2025-10-05 10:15 417
#ifndef IMSTB_TEXTEDIT_GETPREVCHARINDEX
4e9e2555 Poul Sander 2025-10-05 10:15 418
#define IMSTB_TEXTEDIT_GETPREVCHARINDEX(OBJ, IDX) ((IDX) - 1)
4e9e2555 Poul Sander 2025-10-05 10:15 419
#endif
4e9e2555 Poul Sander 2025-10-05 10:15 420
#ifndef IMSTB_TEXTEDIT_GETNEXTCHARINDEX
4e9e2555 Poul Sander 2025-10-05 10:15 421
#define IMSTB_TEXTEDIT_GETNEXTCHARINDEX(OBJ, IDX) ((IDX) + 1)
4e9e2555 Poul Sander 2025-10-05 10:15 422
#endif
1193b4be Poul Sander 2025-02-03 21:28 423
1193b4be Poul Sander 2025-02-03 21:28 424
/////////////////////////////////////////////////////////////////////////////
1193b4be Poul Sander 2025-02-03 21:28 425
//
1193b4be Poul Sander 2025-02-03 21:28 426
//      Mouse input handling
1193b4be Poul Sander 2025-02-03 21:28 427
//
1193b4be Poul Sander 2025-02-03 21:28 428
1193b4be Poul Sander 2025-02-03 21:28 429
// traverse the layout to locate the nearest character to a display position
4e9e2555 Poul Sander 2025-10-05 10:15 430
static int stb_text_locate_coord(IMSTB_TEXTEDIT_STRING *str, float x, float y, int* out_side_on_line)
1193b4be Poul Sander 2025-02-03 21:28 431
{
1193b4be Poul Sander 2025-02-03 21:28 432
   StbTexteditRow r;
1193b4be Poul Sander 2025-02-03 21:28 433
   int n = STB_TEXTEDIT_STRINGLEN(str);
1193b4be Poul Sander 2025-02-03 21:28 434
   float base_y = 0, prev_x;
1193b4be Poul Sander 2025-02-03 21:28 435
   int i=0, k;
1193b4be Poul Sander 2025-02-03 21:28 436
1193b4be Poul Sander 2025-02-03 21:28 437
   r.x0 = r.x1 = 0;
1193b4be Poul Sander 2025-02-03 21:28 438
   r.ymin = r.ymax = 0;
1193b4be Poul Sander 2025-02-03 21:28 439
   r.num_chars = 0;
4e9e2555 Poul Sander 2025-10-05 10:15 440
   *out_side_on_line = 0;
1193b4be Poul Sander 2025-02-03 21:28 441
1193b4be Poul Sander 2025-02-03 21:28 442
   // search rows to find one that straddles 'y'
1193b4be Poul Sander 2025-02-03 21:28 443
   while (i < n) {
1193b4be Poul Sander 2025-02-03 21:28 444
      STB_TEXTEDIT_LAYOUTROW(&r, str, i);
1193b4be Poul Sander 2025-02-03 21:28 445
      if (r.num_chars <= 0)
1193b4be Poul Sander 2025-02-03 21:28 446
         return n;
1193b4be Poul Sander 2025-02-03 21:28 447
1193b4be Poul Sander 2025-02-03 21:28 448
      if (i==0 && y < base_y + r.ymin)
1193b4be Poul Sander 2025-02-03 21:28 449
         return 0;
1193b4be Poul Sander 2025-02-03 21:28 450
1193b4be Poul Sander 2025-02-03 21:28 451
      if (y < base_y + r.ymax)
1193b4be Poul Sander 2025-02-03 21:28 452
         break;
1193b4be Poul Sander 2025-02-03 21:28 453
1193b4be Poul Sander 2025-02-03 21:28 454
      i += r.num_chars;
1193b4be Poul Sander 2025-02-03 21:28 455
      base_y += r.baseline_y_delta;
1193b4be Poul Sander 2025-02-03 21:28 456
   }
1193b4be Poul Sander 2025-02-03 21:28 457
1193b4be Poul Sander 2025-02-03 21:28 458
   // below all text, return 'after' last character
1193b4be Poul Sander 2025-02-03 21:28 459
   if (i >= n)
4e9e2555 Poul Sander 2025-10-05 10:15 460
   {
4e9e2555 Poul Sander 2025-10-05 10:15 461
      *out_side_on_line = 1;
1193b4be Poul Sander 2025-02-03 21:28 462
      return n;
4e9e2555 Poul Sander 2025-10-05 10:15 463
   }
1193b4be Poul Sander 2025-02-03 21:28 464
1193b4be Poul Sander 2025-02-03 21:28 465
   // check if it's before the beginning of the line
1193b4be Poul Sander 2025-02-03 21:28 466
   if (x < r.x0)
1193b4be Poul Sander 2025-02-03 21:28 467
      return i;
1193b4be Poul Sander 2025-02-03 21:28 468
1193b4be Poul Sander 2025-02-03 21:28 469
   // check if it's before the end of the line
1193b4be Poul Sander 2025-02-03 21:28 470
   if (x < r.x1) {
1193b4be Poul Sander 2025-02-03 21:28 471
      // search characters in row for one that straddles 'x'
1193b4be Poul Sander 2025-02-03 21:28 472
      prev_x = r.x0;
1193b4be Poul Sander 2025-02-03 21:28 473
      for (k=0; k < r.num_chars; k = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, i + k) - i) {
1193b4be Poul Sander 2025-02-03 21:28 474
         float w = STB_TEXTEDIT_GETWIDTH(str, i, k);
1193b4be Poul Sander 2025-02-03 21:28 475
         if (x < prev_x+w) {
4e9e2555 Poul Sander 2025-10-05 10:15 476
            *out_side_on_line = (k == 0) ? 0 : 1;
1193b4be Poul Sander 2025-02-03 21:28 477
            if (x < prev_x+w/2)
1193b4be Poul Sander 2025-02-03 21:28 478
               return k+i;
1193b4be Poul Sander 2025-02-03 21:28 479
            else
1193b4be Poul Sander 2025-02-03 21:28 480
               return IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, i + k);
1193b4be Poul Sander 2025-02-03 21:28 481
         }
1193b4be Poul Sander 2025-02-03 21:28 482
         prev_x += w;
1193b4be Poul Sander 2025-02-03 21:28 483
      }
1193b4be Poul Sander 2025-02-03 21:28 484
      // shouldn't happen, but if it does, fall through to end-of-line case
1193b4be Poul Sander 2025-02-03 21:28 485
   }
1193b4be Poul Sander 2025-02-03 21:28 486
1193b4be Poul Sander 2025-02-03 21:28 487
   // if the last character is a newline, return that. otherwise return 'after' the last character
4e9e2555 Poul Sander 2025-10-05 10:15 488
   *out_side_on_line = 1;
1193b4be Poul Sander 2025-02-03 21:28 489
   if (STB_TEXTEDIT_GETCHAR(str, i+r.num_chars-1) == STB_TEXTEDIT_NEWLINE)
1193b4be Poul Sander 2025-02-03 21:28 490
      return i+r.num_chars-1;
1193b4be Poul Sander 2025-02-03 21:28 491
   else
1193b4be Poul Sander 2025-02-03 21:28 492
      return i+r.num_chars;
1193b4be Poul Sander 2025-02-03 21:28 493
}
1193b4be Poul Sander 2025-02-03 21:28 494
1193b4be Poul Sander 2025-02-03 21:28 495
// API click: on mouse down, move the cursor to the clicked location, and reset the selection
1193b4be Poul Sander 2025-02-03 21:28 496
static void stb_textedit_click(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)
1193b4be Poul Sander 2025-02-03 21:28 497
{
1193b4be Poul Sander 2025-02-03 21:28 498
   // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse
1193b4be Poul Sander 2025-02-03 21:28 499
   // goes off the top or bottom of the text
4e9e2555 Poul Sander 2025-10-05 10:15 500
   int side_on_line;
1193b4be Poul Sander 2025-02-03 21:28 501
   if( state->single_line )
1193b4be Poul Sander 2025-02-03 21:28 502
   {
1193b4be Poul Sander 2025-02-03 21:28 503
      StbTexteditRow r;
1193b4be Poul Sander 2025-02-03 21:28 504
      STB_TEXTEDIT_LAYOUTROW(&r, str, 0);
1193b4be Poul Sander 2025-02-03 21:28 505
      y = r.ymin;
1193b4be Poul Sander 2025-02-03 21:28 506
   }
1193b4be Poul Sander 2025-02-03 21:28 507
4e9e2555 Poul Sander 2025-10-05 10:15 508
   state->cursor = stb_text_locate_coord(str, x, y, &side_on_line);
1193b4be Poul Sander 2025-02-03 21:28 509
   state->select_start = state->cursor;
1193b4be Poul Sander 2025-02-03 21:28 510
   state->select_end = state->cursor;
1193b4be Poul Sander 2025-02-03 21:28 511
   state->has_preferred_x = 0;
4e9e2555 Poul Sander 2025-10-05 10:15 512
   str->LastMoveDirectionLR = (ImS8)(side_on_line ? ImGuiDir_Right : ImGuiDir_Left);
1193b4be Poul Sander 2025-02-03 21:28 513
}
1193b4be Poul Sander 2025-02-03 21:28 514
1193b4be Poul Sander 2025-02-03 21:28 515
// API drag: on mouse drag, move the cursor and selection endpoint to the clicked location
1193b4be Poul Sander 2025-02-03 21:28 516
static void stb_textedit_drag(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)
1193b4be Poul Sander 2025-02-03 21:28 517
{
1193b4be Poul Sander 2025-02-03 21:28 518
   int p = 0;
4e9e2555 Poul Sander 2025-10-05 10:15 519
   int side_on_line;
1193b4be Poul Sander 2025-02-03 21:28 520
1193b4be Poul Sander 2025-02-03 21:28 521
   // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse
1193b4be Poul Sander 2025-02-03 21:28 522
   // goes off the top or bottom of the text
1193b4be Poul Sander 2025-02-03 21:28 523
   if( state->single_line )
1193b4be Poul Sander 2025-02-03 21:28 524
   {
1193b4be Poul Sander 2025-02-03 21:28 525
      StbTexteditRow r;
1193b4be Poul Sander 2025-02-03 21:28 526
      STB_TEXTEDIT_LAYOUTROW(&r, str, 0);
1193b4be Poul Sander 2025-02-03 21:28 527
      y = r.ymin;
1193b4be Poul Sander 2025-02-03 21:28 528
   }
1193b4be Poul Sander 2025-02-03 21:28 529
1193b4be Poul Sander 2025-02-03 21:28 530
   if (state->select_start == state->select_end)
1193b4be Poul Sander 2025-02-03 21:28 531
      state->select_start = state->cursor;
1193b4be Poul Sander 2025-02-03 21:28 532
4e9e2555 Poul Sander 2025-10-05 10:15 533
   p = stb_text_locate_coord(str, x, y, &side_on_line);
1193b4be Poul Sander 2025-02-03 21:28 534
   state->cursor = state->select_end = p;
4e9e2555 Poul Sander 2025-10-05 10:15 535
   str->LastMoveDirectionLR = (ImS8)(side_on_line ? ImGuiDir_Right : ImGuiDir_Left);
1193b4be Poul Sander 2025-02-03 21:28 536
}
1193b4be Poul Sander 2025-02-03 21:28 537
1193b4be Poul Sander 2025-02-03 21:28 538
/////////////////////////////////////////////////////////////////////////////
1193b4be Poul Sander 2025-02-03 21:28 539
//
1193b4be Poul Sander 2025-02-03 21:28 540
//      Keyboard input handling
1193b4be Poul Sander 2025-02-03 21:28 541
//
1193b4be Poul Sander 2025-02-03 21:28 542
1193b4be Poul Sander 2025-02-03 21:28 543
// forward declarations
1193b4be Poul Sander 2025-02-03 21:28 544
static void stb_text_undo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state);
1193b4be Poul Sander 2025-02-03 21:28 545
static void stb_text_redo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state);
1193b4be Poul Sander 2025-02-03 21:28 546
static void stb_text_makeundo_delete(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length);
1193b4be Poul Sander 2025-02-03 21:28 547
static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length);
1193b4be Poul Sander 2025-02-03 21:28 548
static void stb_text_makeundo_replace(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length);
1193b4be Poul Sander 2025-02-03 21:28 549
1193b4be Poul Sander 2025-02-03 21:28 550
typedef struct
1193b4be Poul Sander 2025-02-03 21:28 551
{
1193b4be Poul Sander 2025-02-03 21:28 552
   float x,y;    // position of n'th character
1193b4be Poul Sander 2025-02-03 21:28 553
   float height; // height of line
1193b4be Poul Sander 2025-02-03 21:28 554
   int first_char, length; // first char of row, and length
1193b4be Poul Sander 2025-02-03 21:28 555
   int prev_first;  // first char of previous row
1193b4be Poul Sander 2025-02-03 21:28 556
} StbFindState;
1193b4be Poul Sander 2025-02-03 21:28 557
1193b4be Poul Sander 2025-02-03 21:28 558
// find the x/y location of a character, and remember info about the previous row in
1193b4be Poul Sander 2025-02-03 21:28 559
// case we get a move-up event (for page up, we'll have to rescan)
1193b4be Poul Sander 2025-02-03 21:28 560
static void stb_textedit_find_charpos(StbFindState *find, IMSTB_TEXTEDIT_STRING *str, int n, int single_line)
1193b4be Poul Sander 2025-02-03 21:28 561
{
1193b4be Poul Sander 2025-02-03 21:28 562
   StbTexteditRow r;
1193b4be Poul Sander 2025-02-03 21:28 563
   int prev_start = 0;
1193b4be Poul Sander 2025-02-03 21:28 564
   int z = STB_TEXTEDIT_STRINGLEN(str);
1193b4be Poul Sander 2025-02-03 21:28 565
   int i=0, first;
1193b4be Poul Sander 2025-02-03 21:28 566
1193b4be Poul Sander 2025-02-03 21:28 567
   if (n == z && single_line) {
1193b4be Poul Sander 2025-02-03 21:28 568
      // special case if it's at the end (may not be needed?)
1193b4be Poul Sander 2025-02-03 21:28 569
      STB_TEXTEDIT_LAYOUTROW(&r, str, 0);
1193b4be Poul Sander 2025-02-03 21:28 570
      find->y = 0;
1193b4be Poul Sander 2025-02-03 21:28 571
      find->first_char = 0;
1193b4be Poul Sander 2025-02-03 21:28 572
      find->length = z;
1193b4be Poul Sander 2025-02-03 21:28 573
      find->height = r.ymax - r.ymin;
1193b4be Poul Sander 2025-02-03 21:28 574
      find->x = r.x1;
1193b4be Poul Sander 2025-02-03 21:28 575
      return;
1193b4be Poul Sander 2025-02-03 21:28 576
   }
1193b4be Poul Sander 2025-02-03 21:28 577
1193b4be Poul Sander 2025-02-03 21:28 578
   // search rows to find the one that straddles character n
1193b4be Poul Sander 2025-02-03 21:28 579
   find->y = 0;
1193b4be Poul Sander 2025-02-03 21:28 580
1193b4be Poul Sander 2025-02-03 21:28 581
   for(;;) {
1193b4be Poul Sander 2025-02-03 21:28 582
      STB_TEXTEDIT_LAYOUTROW(&r, str, i);
1193b4be Poul Sander 2025-02-03 21:28 583
      if (n < i + r.num_chars)
1193b4be Poul Sander 2025-02-03 21:28 584
         break;
4e9e2555 Poul Sander 2025-10-05 10:15 585
      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) // [DEAR IMGUI] Wrapping point handling
4e9e2555 Poul Sander 2025-10-05 10:15 586
         break;
1193b4be Poul Sander 2025-02-03 21:28 587
      if (i + r.num_chars == z && z > 0 && STB_TEXTEDIT_GETCHAR(str, z - 1) != STB_TEXTEDIT_NEWLINE)  // [DEAR IMGUI] special handling for last line
1193b4be Poul Sander 2025-02-03 21:28 588
         break;   // [DEAR IMGUI]
1193b4be Poul Sander 2025-02-03 21:28 589
      prev_start = i;
1193b4be Poul Sander 2025-02-03 21:28 590
      i += r.num_chars;
1193b4be Poul Sander 2025-02-03 21:28 591
      find->y += r.baseline_y_delta;
1193b4be Poul Sander 2025-02-03 21:28 592
      if (i == z) // [DEAR IMGUI]
1193b4be Poul Sander 2025-02-03 21:28 593
      {
1193b4be Poul Sander 2025-02-03 21:28 594
         r.num_chars = 0; // [DEAR IMGUI]
1193b4be Poul Sander 2025-02-03 21:28 595
         break;   // [DEAR IMGUI]
1193b4be Poul Sander 2025-02-03 21:28 596
      }
1193b4be Poul Sander 2025-02-03 21:28 597
   }
1193b4be Poul Sander 2025-02-03 21:28 598
1193b4be Poul Sander 2025-02-03 21:28 599
   find->first_char = first = i;
1193b4be Poul Sander 2025-02-03 21:28 600
   find->length = r.num_chars;
1193b4be Poul Sander 2025-02-03 21:28 601
   find->height = r.ymax - r.ymin;
1193b4be Poul Sander 2025-02-03 21:28 602
   find->prev_first = prev_start;
1193b4be Poul Sander 2025-02-03 21:28 603
1193b4be Poul Sander 2025-02-03 21:28 604
   // now scan to find xpos
1193b4be Poul Sander 2025-02-03 21:28 605
   find->x = r.x0;
1193b4be Poul Sander 2025-02-03 21:28 606
   for (i=0; first+i < n; i = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, first + i) - first)
1193b4be Poul Sander 2025-02-03 21:28 607
      find->x += STB_TEXTEDIT_GETWIDTH(str, first, i);
1193b4be Poul Sander 2025-02-03 21:28 608
}
1193b4be Poul Sander 2025-02-03 21:28 609
1193b4be Poul Sander 2025-02-03 21:28 610
#define STB_TEXT_HAS_SELECTION(s)   ((s)->select_start != (s)->select_end)
1193b4be Poul Sander 2025-02-03 21:28 611
1193b4be Poul Sander 2025-02-03 21:28 612
// make the selection/cursor state valid if client altered the string
1193b4be Poul Sander 2025-02-03 21:28 613
static void stb_textedit_clamp(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state)
1193b4be Poul Sander 2025-02-03 21:28 614
{
1193b4be Poul Sander 2025-02-03 21:28 615
   int n = STB_TEXTEDIT_STRINGLEN(str);
1193b4be Poul Sander 2025-02-03 21:28 616
   if (STB_TEXT_HAS_SELECTION(state)) {
1193b4be Poul Sander 2025-02-03 21:28 617
      if (state->select_start > n) state->select_start = n;
1193b4be Poul Sander 2025-02-03 21:28 618
      if (state->select_end   > n) state->select_end = n;
1193b4be Poul Sander 2025-02-03 21:28 619
      // if clamping forced them to be equal, move the cursor to match
1193b4be Poul Sander 2025-02-03 21:28 620
      if (state->select_start == state->select_end)
1193b4be Poul Sander 2025-02-03 21:28 621
         state->cursor = state->select_start;
1193b4be Poul Sander 2025-02-03 21:28 622
   }
1193b4be Poul Sander 2025-02-03 21:28 623
   if (state->cursor > n) state->cursor = n;
1193b4be Poul Sander 2025-02-03 21:28 624
}
1193b4be Poul Sander 2025-02-03 21:28 625
1193b4be Poul Sander 2025-02-03 21:28 626
// delete characters while updating undo
1193b4be Poul Sander 2025-02-03 21:28 627
static void stb_textedit_delete(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int len)
1193b4be Poul Sander 2025-02-03 21:28 628
{
1193b4be Poul Sander 2025-02-03 21:28 629
   stb_text_makeundo_delete(str, state, where, len);
1193b4be Poul Sander 2025-02-03 21:28 630
   STB_TEXTEDIT_DELETECHARS(str, where, len);
1193b4be Poul Sander 2025-02-03 21:28 631
   state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 632
}
1193b4be Poul Sander 2025-02-03 21:28 633
1193b4be Poul Sander 2025-02-03 21:28 634
// delete the section
1193b4be Poul Sander 2025-02-03 21:28 635
static void stb_textedit_delete_selection(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state)
1193b4be Poul Sander 2025-02-03 21:28 636
{
1193b4be Poul Sander 2025-02-03 21:28 637
   stb_textedit_clamp(str, state);
1193b4be Poul Sander 2025-02-03 21:28 638
   if (STB_TEXT_HAS_SELECTION(state)) {
1193b4be Poul Sander 2025-02-03 21:28 639
      if (state->select_start < state->select_end) {
1193b4be Poul Sander 2025-02-03 21:28 640
         stb_textedit_delete(str, state, state->select_start, state->select_end - state->select_start);
1193b4be Poul Sander 2025-02-03 21:28 641
         state->select_end = state->cursor = state->select_start;
1193b4be Poul Sander 2025-02-03 21:28 642
      } else {
1193b4be Poul Sander 2025-02-03 21:28 643
         stb_textedit_delete(str, state, state->select_end, state->select_start - state->select_end);
1193b4be Poul Sander 2025-02-03 21:28 644
         state->select_start = state->cursor = state->select_end;
1193b4be Poul Sander 2025-02-03 21:28 645
      }
1193b4be Poul Sander 2025-02-03 21:28 646
      state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 647
   }
1193b4be Poul Sander 2025-02-03 21:28 648
}
1193b4be Poul Sander 2025-02-03 21:28 649
1193b4be Poul Sander 2025-02-03 21:28 650
// canoncialize the selection so start <= end
1193b4be Poul Sander 2025-02-03 21:28 651
static void stb_textedit_sortselection(STB_TexteditState *state)
1193b4be Poul Sander 2025-02-03 21:28 652
{
1193b4be Poul Sander 2025-02-03 21:28 653
   if (state->select_end < state->select_start) {
1193b4be Poul Sander 2025-02-03 21:28 654
      int temp = state->select_end;
1193b4be Poul Sander 2025-02-03 21:28 655
      state->select_end = state->select_start;
1193b4be Poul Sander 2025-02-03 21:28 656
      state->select_start = temp;
1193b4be Poul Sander 2025-02-03 21:28 657
   }
1193b4be Poul Sander 2025-02-03 21:28 658
}
1193b4be Poul Sander 2025-02-03 21:28 659
1193b4be Poul Sander 2025-02-03 21:28 660
// move cursor to first character of selection
1193b4be Poul Sander 2025-02-03 21:28 661
static void stb_textedit_move_to_first(STB_TexteditState *state)
1193b4be Poul Sander 2025-02-03 21:28 662
{
1193b4be Poul Sander 2025-02-03 21:28 663
   if (STB_TEXT_HAS_SELECTION(state)) {
1193b4be Poul Sander 2025-02-03 21:28 664
      stb_textedit_sortselection(state);
1193b4be Poul Sander 2025-02-03 21:28 665
      state->cursor = state->select_start;
1193b4be Poul Sander 2025-02-03 21:28 666
      state->select_end = state->select_start;
1193b4be Poul Sander 2025-02-03 21:28 667
      state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 668
   }
1193b4be Poul Sander 2025-02-03 21:28 669
}
1193b4be Poul Sander 2025-02-03 21:28 670
1193b4be Poul Sander 2025-02-03 21:28 671
// move cursor to last character of selection
1193b4be Poul Sander 2025-02-03 21:28 672
static void stb_textedit_move_to_last(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state)
1193b4be Poul Sander 2025-02-03 21:28 673
{
1193b4be Poul Sander 2025-02-03 21:28 674
   if (STB_TEXT_HAS_SELECTION(state)) {
1193b4be Poul Sander 2025-02-03 21:28 675
      stb_textedit_sortselection(state);
1193b4be Poul Sander 2025-02-03 21:28 676
      stb_textedit_clamp(str, state);
1193b4be Poul Sander 2025-02-03 21:28 677
      state->cursor = state->select_end;
1193b4be Poul Sander 2025-02-03 21:28 678
      state->select_start = state->select_end;
1193b4be Poul Sander 2025-02-03 21:28 679
      state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 680
   }
1193b4be Poul Sander 2025-02-03 21:28 681
}
1193b4be Poul Sander 2025-02-03 21:28 682
4e9e2555 Poul Sander 2025-10-05 10:15 683
// [DEAR IMGUI] Extracted this function so we can more easily add support for word-wrapping.
4e9e2555 Poul Sander 2025-10-05 10:15 684
#ifndef STB_TEXTEDIT_MOVELINESTART
4e9e2555 Poul Sander 2025-10-05 10:15 685
static int stb_textedit_move_line_start(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int cursor)
4e9e2555 Poul Sander 2025-10-05 10:15 686
{
4e9e2555 Poul Sander 2025-10-05 10:15 687
   if (state->single_line)
4e9e2555 Poul Sander 2025-10-05 10:15 688
      return 0;
4e9e2555 Poul Sander 2025-10-05 10:15 689
   while (cursor > 0) {
4e9e2555 Poul Sander 2025-10-05 10:15 690
      int prev = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, cursor);
4e9e2555 Poul Sander 2025-10-05 10:15 691
      if (STB_TEXTEDIT_GETCHAR(str, prev) == STB_TEXTEDIT_NEWLINE)
4e9e2555 Poul Sander 2025-10-05 10:15 692
         break;
4e9e2555 Poul Sander 2025-10-05 10:15 693
      cursor = prev;
4e9e2555 Poul Sander 2025-10-05 10:15 694
   }
4e9e2555 Poul Sander 2025-10-05 10:15 695
   return cursor;
4e9e2555 Poul Sander 2025-10-05 10:15 696
}
4e9e2555 Poul Sander 2025-10-05 10:15 697
#define STB_TEXTEDIT_MOVELINESTART stb_textedit_move_line_start
1193b4be Poul Sander 2025-02-03 21:28 698
#endif
4e9e2555 Poul Sander 2025-10-05 10:15 699
#ifndef STB_TEXTEDIT_MOVELINEEND
4e9e2555 Poul Sander 2025-10-05 10:15 700
static int stb_textedit_move_line_end(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int cursor)
4e9e2555 Poul Sander 2025-10-05 10:15 701
{
4e9e2555 Poul Sander 2025-10-05 10:15 702
   int n = STB_TEXTEDIT_STRINGLEN(str);
4e9e2555 Poul Sander 2025-10-05 10:15 703
   if (state->single_line)
4e9e2555 Poul Sander 2025-10-05 10:15 704
      return n;
4e9e2555 Poul Sander 2025-10-05 10:15 705
   while (cursor < n && STB_TEXTEDIT_GETCHAR(str, cursor) != STB_TEXTEDIT_NEWLINE)
4e9e2555 Poul Sander 2025-10-05 10:15 706
      cursor = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, cursor);
4e9e2555 Poul Sander 2025-10-05 10:15 707
   return cursor;
4e9e2555 Poul Sander 2025-10-05 10:15 708
}
4e9e2555 Poul Sander 2025-10-05 10:15 709
#define STB_TEXTEDIT_MOVELINEEND stb_textedit_move_line_end
1193b4be Poul Sander 2025-02-03 21:28 710
#endif
1193b4be Poul Sander 2025-02-03 21:28 711
1193b4be Poul Sander 2025-02-03 21:28 712
#ifdef STB_TEXTEDIT_IS_SPACE
1193b4be Poul Sander 2025-02-03 21:28 713
static int is_word_boundary( IMSTB_TEXTEDIT_STRING *str, int idx )
1193b4be Poul Sander 2025-02-03 21:28 714
{
1193b4be Poul Sander 2025-02-03 21:28 715
   return idx > 0 ? (STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str,idx-1) ) && !STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str, idx) ) ) : 1;
1193b4be Poul Sander 2025-02-03 21:28 716
}
1193b4be Poul Sander 2025-02-03 21:28 717
1193b4be Poul Sander 2025-02-03 21:28 718
#ifndef STB_TEXTEDIT_MOVEWORDLEFT
1193b4be Poul Sander 2025-02-03 21:28 719
static int stb_textedit_move_to_word_previous( IMSTB_TEXTEDIT_STRING *str, int c )
1193b4be Poul Sander 2025-02-03 21:28 720
{
4e9e2555 Poul Sander 2025-10-05 10:15 721
   c = IMSTB_TEXTEDIT_GETPREVCHARINDEX( str, c ); // always move at least one character
4e9e2555 Poul Sander 2025-10-05 10:15 722
   while (c >= 0 && !is_word_boundary(str, c))
4e9e2555 Poul Sander 2025-10-05 10:15 723
      c = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, c);
1193b4be Poul Sander 2025-02-03 21:28 724
1193b4be Poul Sander 2025-02-03 21:28 725
   if( c < 0 )
1193b4be Poul Sander 2025-02-03 21:28 726
      c = 0;
1193b4be Poul Sander 2025-02-03 21:28 727
1193b4be Poul Sander 2025-02-03 21:28 728
   return c;
1193b4be Poul Sander 2025-02-03 21:28 729
}
1193b4be Poul Sander 2025-02-03 21:28 730
#define STB_TEXTEDIT_MOVEWORDLEFT stb_textedit_move_to_word_previous
1193b4be Poul Sander 2025-02-03 21:28 731
#endif
1193b4be Poul Sander 2025-02-03 21:28 732
1193b4be Poul Sander 2025-02-03 21:28 733
#ifndef STB_TEXTEDIT_MOVEWORDRIGHT
1193b4be Poul Sander 2025-02-03 21:28 734
static int stb_textedit_move_to_word_next( IMSTB_TEXTEDIT_STRING *str, int c )
1193b4be Poul Sander 2025-02-03 21:28 735
{
1193b4be Poul Sander 2025-02-03 21:28 736
   const int len = STB_TEXTEDIT_STRINGLEN(str);
4e9e2555 Poul Sander 2025-10-05 10:15 737
   c = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, c); // always move at least one character
1193b4be Poul Sander 2025-02-03 21:28 738
   while( c < len && !is_word_boundary( str, c ) )
4e9e2555 Poul Sander 2025-10-05 10:15 739
      c = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, c);
1193b4be Poul Sander 2025-02-03 21:28 740
1193b4be Poul Sander 2025-02-03 21:28 741
   if( c > len )
1193b4be Poul Sander 2025-02-03 21:28 742
      c = len;
1193b4be Poul Sander 2025-02-03 21:28 743
1193b4be Poul Sander 2025-02-03 21:28 744
   return c;
1193b4be Poul Sander 2025-02-03 21:28 745
}
1193b4be Poul Sander 2025-02-03 21:28 746
#define STB_TEXTEDIT_MOVEWORDRIGHT stb_textedit_move_to_word_next
1193b4be Poul Sander 2025-02-03 21:28 747
#endif
1193b4be Poul Sander 2025-02-03 21:28 748
1193b4be Poul Sander 2025-02-03 21:28 749
#endif
1193b4be Poul Sander 2025-02-03 21:28 750
1193b4be Poul Sander 2025-02-03 21:28 751
// update selection and cursor to match each other
1193b4be Poul Sander 2025-02-03 21:28 752
static void stb_textedit_prep_selection_at_cursor(STB_TexteditState *state)
1193b4be Poul Sander 2025-02-03 21:28 753
{
1193b4be Poul Sander 2025-02-03 21:28 754
   if (!STB_TEXT_HAS_SELECTION(state))
1193b4be Poul Sander 2025-02-03 21:28 755
      state->select_start = state->select_end = state->cursor;
1193b4be Poul Sander 2025-02-03 21:28 756
   else
1193b4be Poul Sander 2025-02-03 21:28 757
      state->cursor = state->select_end;
1193b4be Poul Sander 2025-02-03 21:28 758
}
1193b4be Poul Sander 2025-02-03 21:28 759
1193b4be Poul Sander 2025-02-03 21:28 760
// API cut: delete selection
1193b4be Poul Sander 2025-02-03 21:28 761
static int stb_textedit_cut(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state)
1193b4be Poul Sander 2025-02-03 21:28 762
{
1193b4be Poul Sander 2025-02-03 21:28 763
   if (STB_TEXT_HAS_SELECTION(state)) {
1193b4be Poul Sander 2025-02-03 21:28 764
      stb_textedit_delete_selection(str,state); // implicitly clamps
1193b4be Poul Sander 2025-02-03 21:28 765
      state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 766
      return 1;
1193b4be Poul Sander 2025-02-03 21:28 767
   }
1193b4be Poul Sander 2025-02-03 21:28 768
   return 0;
1193b4be Poul Sander 2025-02-03 21:28 769
}
1193b4be Poul Sander 2025-02-03 21:28 770
1193b4be Poul Sander 2025-02-03 21:28 771
// API paste: replace existing selection with passed-in text
1193b4be Poul Sander 2025-02-03 21:28 772
static int stb_textedit_paste_internal(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, IMSTB_TEXTEDIT_CHARTYPE *text, int len)
1193b4be Poul Sander 2025-02-03 21:28 773
{
1193b4be Poul Sander 2025-02-03 21:28 774
   // if there's a selection, the paste should delete it
1193b4be Poul Sander 2025-02-03 21:28 775
   stb_textedit_clamp(str, state);
1193b4be Poul Sander 2025-02-03 21:28 776
   stb_textedit_delete_selection(str,state);
1193b4be Poul Sander 2025-02-03 21:28 777
   // try to insert the characters
1193b4be Poul Sander 2025-02-03 21:28 778
   if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, len)) {
1193b4be Poul Sander 2025-02-03 21:28 779
      stb_text_makeundo_insert(state, state->cursor, len);
1193b4be Poul Sander 2025-02-03 21:28 780
      state->cursor += len;
1193b4be Poul Sander 2025-02-03 21:28 781
      state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 782
      return 1;
1193b4be Poul Sander 2025-02-03 21:28 783
   }
1193b4be Poul Sander 2025-02-03 21:28 784
   // note: paste failure will leave deleted selection, may be restored with an undo (see https://github.com/nothings/stb/issues/734 for details)
1193b4be Poul Sander 2025-02-03 21:28 785
   return 0;
1193b4be Poul Sander 2025-02-03 21:28 786
}
1193b4be Poul Sander 2025-02-03 21:28 787
1193b4be Poul Sander 2025-02-03 21:28 788
#ifndef STB_TEXTEDIT_KEYTYPE
1193b4be Poul Sander 2025-02-03 21:28 789
#define STB_TEXTEDIT_KEYTYPE int
1193b4be Poul Sander 2025-02-03 21:28 790
#endif
1193b4be Poul Sander 2025-02-03 21:28 791
4e9e2555 Poul Sander 2025-10-05 10:15 792
// API key: process text input
1193b4be Poul Sander 2025-02-03 21:28 793
// [DEAR IMGUI] Added stb_textedit_text(), extracted out and called by stb_textedit_key() for backward compatibility.
1193b4be Poul Sander 2025-02-03 21:28 794
static void stb_textedit_text(IMSTB_TEXTEDIT_STRING* str, STB_TexteditState* state, const IMSTB_TEXTEDIT_CHARTYPE* text, int text_len)
1193b4be Poul Sander 2025-02-03 21:28 795
{
1193b4be Poul Sander 2025-02-03 21:28 796
   // can't add newline in single-line mode
1193b4be Poul Sander 2025-02-03 21:28 797
   if (text[0] == '\n' && state->single_line)
1193b4be Poul Sander 2025-02-03 21:28 798
      return;
1193b4be Poul Sander 2025-02-03 21:28 799
1193b4be Poul Sander 2025-02-03 21:28 800
   if (state->insert_mode && !STB_TEXT_HAS_SELECTION(state) && state->cursor < STB_TEXTEDIT_STRINGLEN(str)) {
1193b4be Poul Sander 2025-02-03 21:28 801
      stb_text_makeundo_replace(str, state, state->cursor, 1, 1);
1193b4be Poul Sander 2025-02-03 21:28 802
      STB_TEXTEDIT_DELETECHARS(str, state->cursor, 1);
1193b4be Poul Sander 2025-02-03 21:28 803
      if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, text_len)) {
1193b4be Poul Sander 2025-02-03 21:28 804
         state->cursor += text_len;
1193b4be Poul Sander 2025-02-03 21:28 805
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 806
      }
4e9e2555 Poul Sander 2025-10-05 10:15 807
   } else {
1193b4be Poul Sander 2025-02-03 21:28 808
      stb_textedit_delete_selection(str, state); // implicitly clamps
1193b4be Poul Sander 2025-02-03 21:28 809
      if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, text_len)) {
1193b4be Poul Sander 2025-02-03 21:28 810
         stb_text_makeundo_insert(state, state->cursor, text_len);
1193b4be Poul Sander 2025-02-03 21:28 811
         state->cursor += text_len;
1193b4be Poul Sander 2025-02-03 21:28 812
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 813
      }
1193b4be Poul Sander 2025-02-03 21:28 814
   }
1193b4be Poul Sander 2025-02-03 21:28 815
}
1193b4be Poul Sander 2025-02-03 21:28 816
1193b4be Poul Sander 2025-02-03 21:28 817
// API key: process a keyboard input
1193b4be Poul Sander 2025-02-03 21:28 818
static void stb_textedit_key(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_KEYTYPE key)
1193b4be Poul Sander 2025-02-03 21:28 819
{
1193b4be Poul Sander 2025-02-03 21:28 820
retry:
1193b4be Poul Sander 2025-02-03 21:28 821
   switch (key) {
1193b4be Poul Sander 2025-02-03 21:28 822
      default: {
1193b4be Poul Sander 2025-02-03 21:28 823
#ifdef STB_TEXTEDIT_KEYTOTEXT
4e9e2555 Poul Sander 2025-10-05 10:15 824
         // This is not suitable for UTF-8 support.
1193b4be Poul Sander 2025-02-03 21:28 825
         int c = STB_TEXTEDIT_KEYTOTEXT(key);
1193b4be Poul Sander 2025-02-03 21:28 826
         if (c > 0) {
1193b4be Poul Sander 2025-02-03 21:28 827
            IMSTB_TEXTEDIT_CHARTYPE ch = (IMSTB_TEXTEDIT_CHARTYPE)c;
1193b4be Poul Sander 2025-02-03 21:28 828
            stb_textedit_text(str, state, &ch, 1);
1193b4be Poul Sander 2025-02-03 21:28 829
         }
1193b4be Poul Sander 2025-02-03 21:28 830
#endif
1193b4be Poul Sander 2025-02-03 21:28 831
         break;
1193b4be Poul Sander 2025-02-03 21:28 832
      }
1193b4be Poul Sander 2025-02-03 21:28 833
1193b4be Poul Sander 2025-02-03 21:28 834
#ifdef STB_TEXTEDIT_K_INSERT
1193b4be Poul Sander 2025-02-03 21:28 835
      case STB_TEXTEDIT_K_INSERT:
1193b4be Poul Sander 2025-02-03 21:28 836
         state->insert_mode = !state->insert_mode;
1193b4be Poul Sander 2025-02-03 21:28 837
         break;
1193b4be Poul Sander 2025-02-03 21:28 838
#endif
1193b4be Poul Sander 2025-02-03 21:28 839
1193b4be Poul Sander 2025-02-03 21:28 840
      case STB_TEXTEDIT_K_UNDO:
1193b4be Poul Sander 2025-02-03 21:28 841
         stb_text_undo(str, state);
1193b4be Poul Sander 2025-02-03 21:28 842
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 843
         break;
1193b4be Poul Sander 2025-02-03 21:28 844
1193b4be Poul Sander 2025-02-03 21:28 845
      case STB_TEXTEDIT_K_REDO:
1193b4be Poul Sander 2025-02-03 21:28 846
         stb_text_redo(str, state);
1193b4be Poul Sander 2025-02-03 21:28 847
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 848
         break;
1193b4be Poul Sander 2025-02-03 21:28 849
1193b4be Poul Sander 2025-02-03 21:28 850
      case STB_TEXTEDIT_K_LEFT:
1193b4be Poul Sander 2025-02-03 21:28 851
         // if currently there's a selection, move cursor to start of selection
1193b4be Poul Sander 2025-02-03 21:28 852
         if (STB_TEXT_HAS_SELECTION(state))
1193b4be Poul Sander 2025-02-03 21:28 853
            stb_textedit_move_to_first(state);
1193b4be Poul Sander 2025-02-03 21:28 854
         else
1193b4be Poul Sander 2025-02-03 21:28 855
            if (state->cursor > 0)
1193b4be Poul Sander 2025-02-03 21:28 856
               state->cursor = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 857
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 858
         break;
1193b4be Poul Sander 2025-02-03 21:28 859
1193b4be Poul Sander 2025-02-03 21:28 860
      case STB_TEXTEDIT_K_RIGHT:
1193b4be Poul Sander 2025-02-03 21:28 861
         // if currently there's a selection, move cursor to end of selection
1193b4be Poul Sander 2025-02-03 21:28 862
         if (STB_TEXT_HAS_SELECTION(state))
1193b4be Poul Sander 2025-02-03 21:28 863
            stb_textedit_move_to_last(str, state);
1193b4be Poul Sander 2025-02-03 21:28 864
         else
1193b4be Poul Sander 2025-02-03 21:28 865
            state->cursor = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 866
         stb_textedit_clamp(str, state);
1193b4be Poul Sander 2025-02-03 21:28 867
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 868
         break;
1193b4be Poul Sander 2025-02-03 21:28 869
1193b4be Poul Sander 2025-02-03 21:28 870
      case STB_TEXTEDIT_K_LEFT | STB_TEXTEDIT_K_SHIFT:
1193b4be Poul Sander 2025-02-03 21:28 871
         stb_textedit_clamp(str, state);
1193b4be Poul Sander 2025-02-03 21:28 872
         stb_textedit_prep_selection_at_cursor(state);
1193b4be Poul Sander 2025-02-03 21:28 873
         // move selection left
1193b4be Poul Sander 2025-02-03 21:28 874
         if (state->select_end > 0)
1193b4be Poul Sander 2025-02-03 21:28 875
            state->select_end = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, state->select_end);
1193b4be Poul Sander 2025-02-03 21:28 876
         state->cursor = state->select_end;
1193b4be Poul Sander 2025-02-03 21:28 877
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 878
         break;
1193b4be Poul Sander 2025-02-03 21:28 879
1193b4be Poul Sander 2025-02-03 21:28 880
#ifdef STB_TEXTEDIT_MOVEWORDLEFT
1193b4be Poul Sander 2025-02-03 21:28 881
      case STB_TEXTEDIT_K_WORDLEFT:
1193b4be Poul Sander 2025-02-03 21:28 882
         if (STB_TEXT_HAS_SELECTION(state))
1193b4be Poul Sander 2025-02-03 21:28 883
            stb_textedit_move_to_first(state);
1193b4be Poul Sander 2025-02-03 21:28 884
         else {
1193b4be Poul Sander 2025-02-03 21:28 885
            state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 886
            stb_textedit_clamp( str, state );
1193b4be Poul Sander 2025-02-03 21:28 887
         }
1193b4be Poul Sander 2025-02-03 21:28 888
         break;
1193b4be Poul Sander 2025-02-03 21:28 889
1193b4be Poul Sander 2025-02-03 21:28 890
      case STB_TEXTEDIT_K_WORDLEFT | STB_TEXTEDIT_K_SHIFT:
1193b4be Poul Sander 2025-02-03 21:28 891
         if( !STB_TEXT_HAS_SELECTION( state ) )
1193b4be Poul Sander 2025-02-03 21:28 892
            stb_textedit_prep_selection_at_cursor(state);
1193b4be Poul Sander 2025-02-03 21:28 893
1193b4be Poul Sander 2025-02-03 21:28 894
         state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 895
         state->select_end = state->cursor;
1193b4be Poul Sander 2025-02-03 21:28 896
1193b4be Poul Sander 2025-02-03 21:28 897
         stb_textedit_clamp( str, state );
1193b4be Poul Sander 2025-02-03 21:28 898
         break;
1193b4be Poul Sander 2025-02-03 21:28 899
#endif
1193b4be Poul Sander 2025-02-03 21:28 900
1193b4be Poul Sander 2025-02-03 21:28 901
#ifdef STB_TEXTEDIT_MOVEWORDRIGHT
1193b4be Poul Sander 2025-02-03 21:28 902
      case STB_TEXTEDIT_K_WORDRIGHT:
1193b4be Poul Sander 2025-02-03 21:28 903
         if (STB_TEXT_HAS_SELECTION(state))
1193b4be Poul Sander 2025-02-03 21:28 904
            stb_textedit_move_to_last(str, state);
1193b4be Poul Sander 2025-02-03 21:28 905
         else {
1193b4be Poul Sander 2025-02-03 21:28 906
            state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 907
            stb_textedit_clamp( str, state );
1193b4be Poul Sander 2025-02-03 21:28 908
         }
1193b4be Poul Sander 2025-02-03 21:28 909
         break;
1193b4be Poul Sander 2025-02-03 21:28 910
1193b4be Poul Sander 2025-02-03 21:28 911
      case STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT:
1193b4be Poul Sander 2025-02-03 21:28 912
         if( !STB_TEXT_HAS_SELECTION( state ) )
1193b4be Poul Sander 2025-02-03 21:28 913
            stb_textedit_prep_selection_at_cursor(state);
1193b4be Poul Sander 2025-02-03 21:28 914
1193b4be Poul Sander 2025-02-03 21:28 915
         state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 916
         state->select_end = state->cursor;
1193b4be Poul Sander 2025-02-03 21:28 917
1193b4be Poul Sander 2025-02-03 21:28 918
         stb_textedit_clamp( str, state );
1193b4be Poul Sander 2025-02-03 21:28 919
         break;
1193b4be Poul Sander 2025-02-03 21:28 920
#endif
1193b4be Poul Sander 2025-02-03 21:28 921
1193b4be Poul Sander 2025-02-03 21:28 922
      case STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT:
1193b4be Poul Sander 2025-02-03 21:28 923
         stb_textedit_prep_selection_at_cursor(state);
1193b4be Poul Sander 2025-02-03 21:28 924
         // move selection right
1193b4be Poul Sander 2025-02-03 21:28 925
         state->select_end = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->select_end);
1193b4be Poul Sander 2025-02-03 21:28 926
         stb_textedit_clamp(str, state);
1193b4be Poul Sander 2025-02-03 21:28 927
         state->cursor = state->select_end;
1193b4be Poul Sander 2025-02-03 21:28 928
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 929
         break;
1193b4be Poul Sander 2025-02-03 21:28 930
1193b4be Poul Sander 2025-02-03 21:28 931
      case STB_TEXTEDIT_K_DOWN:
1193b4be Poul Sander 2025-02-03 21:28 932
      case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT:
1193b4be Poul Sander 2025-02-03 21:28 933
      case STB_TEXTEDIT_K_PGDOWN:
1193b4be Poul Sander 2025-02-03 21:28 934
      case STB_TEXTEDIT_K_PGDOWN | STB_TEXTEDIT_K_SHIFT: {
1193b4be Poul Sander 2025-02-03 21:28 935
         StbFindState find;
1193b4be Poul Sander 2025-02-03 21:28 936
         StbTexteditRow row;
1193b4be Poul Sander 2025-02-03 21:28 937
         int i, j, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;
1193b4be Poul Sander 2025-02-03 21:28 938
         int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGDOWN;
1193b4be Poul Sander 2025-02-03 21:28 939
         int row_count = is_page ? state->row_count_per_page : 1;
1193b4be Poul Sander 2025-02-03 21:28 940
1193b4be Poul Sander 2025-02-03 21:28 941
         if (!is_page && state->single_line) {
1193b4be Poul Sander 2025-02-03 21:28 942
            // on windows, up&down in single-line behave like left&right
1193b4be Poul Sander 2025-02-03 21:28 943
            key = STB_TEXTEDIT_K_RIGHT | (key & STB_TEXTEDIT_K_SHIFT);
1193b4be Poul Sander 2025-02-03 21:28 944
            goto retry;
1193b4be Poul Sander 2025-02-03 21:28 945
         }
1193b4be Poul Sander 2025-02-03 21:28 946
1193b4be Poul Sander 2025-02-03 21:28 947
         if (sel)
1193b4be Poul Sander 2025-02-03 21:28 948
            stb_textedit_prep_selection_at_cursor(state);
1193b4be Poul Sander 2025-02-03 21:28 949
         else if (STB_TEXT_HAS_SELECTION(state))
1193b4be Poul Sander 2025-02-03 21:28 950
            stb_textedit_move_to_last(str, state);
1193b4be Poul Sander 2025-02-03 21:28 951
1193b4be Poul Sander 2025-02-03 21:28 952
         // compute current position of cursor point
1193b4be Poul Sander 2025-02-03 21:28 953
         stb_textedit_clamp(str, state);
1193b4be Poul Sander 2025-02-03 21:28 954
         stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);
1193b4be Poul Sander 2025-02-03 21:28 955
1193b4be Poul Sander 2025-02-03 21:28 956
         for (j = 0; j < row_count; ++j) {
1193b4be Poul Sander 2025-02-03 21:28 957
            float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x;
1193b4be Poul Sander 2025-02-03 21:28 958
            int start = find.first_char + find.length;
1193b4be Poul Sander 2025-02-03 21:28 959
1193b4be Poul Sander 2025-02-03 21:28 960
            if (find.length == 0)
1193b4be Poul Sander 2025-02-03 21:28 961
               break;
1193b4be Poul Sander 2025-02-03 21:28 962
1193b4be Poul Sander 2025-02-03 21:28 963
            // [DEAR IMGUI]
1193b4be Poul Sander 2025-02-03 21:28 964
            // going down while being on the last line shouldn't bring us to that line end
4e9e2555 Poul Sander 2025-10-05 10:15 965
            //if (STB_TEXTEDIT_GETCHAR(str, find.first_char + find.length - 1) != STB_TEXTEDIT_NEWLINE)
4e9e2555 Poul Sander 2025-10-05 10:15 966
            //   break;
1193b4be Poul Sander 2025-02-03 21:28 967
1193b4be Poul Sander 2025-02-03 21:28 968
            // now find character position down a row
1193b4be Poul Sander 2025-02-03 21:28 969
            state->cursor = start;
1193b4be Poul Sander 2025-02-03 21:28 970
            STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 971
            x = row.x0;
4e9e2555 Poul Sander 2025-10-05 10:15 972
            for (i=0; i < row.num_chars; ) {
1193b4be Poul Sander 2025-02-03 21:28 973
               float dx = STB_TEXTEDIT_GETWIDTH(str, start, i);
4e9e2555 Poul Sander 2025-10-05 10:15 974
               int next = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 975
               #ifdef IMSTB_TEXTEDIT_GETWIDTH_NEWLINE
1193b4be Poul Sander 2025-02-03 21:28 976
               if (dx == IMSTB_TEXTEDIT_GETWIDTH_NEWLINE)
1193b4be Poul Sander 2025-02-03 21:28 977
                  break;
1193b4be Poul Sander 2025-02-03 21:28 978
               #endif
1193b4be Poul Sander 2025-02-03 21:28 979
               x += dx;
1193b4be Poul Sander 2025-02-03 21:28 980
               if (x > goal_x)
1193b4be Poul Sander 2025-02-03 21:28 981
                  break;
4e9e2555 Poul Sander 2025-10-05 10:15 982
               i += next - state->cursor;
4e9e2555 Poul Sander 2025-10-05 10:15 983
               state->cursor = next;
1193b4be Poul Sander 2025-02-03 21:28 984
            }
1193b4be Poul Sander 2025-02-03 21:28 985
            stb_textedit_clamp(str, state);
1193b4be Poul Sander 2025-02-03 21:28 986
4e9e2555 Poul Sander 2025-10-05 10:15 987
            if (state->cursor == find.first_char + find.length)
4e9e2555 Poul Sander 2025-10-05 10:15 988
               str->LastMoveDirectionLR = ImGuiDir_Left;
1193b4be Poul Sander 2025-02-03 21:28 989
            state->has_preferred_x = 1;
1193b4be Poul Sander 2025-02-03 21:28 990
            state->preferred_x = goal_x;
1193b4be Poul Sander 2025-02-03 21:28 991
1193b4be Poul Sander 2025-02-03 21:28 992
            if (sel)
1193b4be Poul Sander 2025-02-03 21:28 993
               state->select_end = state->cursor;
1193b4be Poul Sander 2025-02-03 21:28 994
1193b4be Poul Sander 2025-02-03 21:28 995
            // go to next line
1193b4be Poul Sander 2025-02-03 21:28 996
            find.first_char = find.first_char + find.length;
1193b4be Poul Sander 2025-02-03 21:28 997
            find.length = row.num_chars;
1193b4be Poul Sander 2025-02-03 21:28 998
         }
1193b4be Poul Sander 2025-02-03 21:28 999
         break;
1193b4be Poul Sander 2025-02-03 21:28 1000
      }
1193b4be Poul Sander 2025-02-03 21:28 1001
1193b4be Poul Sander 2025-02-03 21:28 1002
      case STB_TEXTEDIT_K_UP:
1193b4be Poul Sander 2025-02-03 21:28 1003
      case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT:
1193b4be Poul Sander 2025-02-03 21:28 1004
      case STB_TEXTEDIT_K_PGUP:
1193b4be Poul Sander 2025-02-03 21:28 1005
      case STB_TEXTEDIT_K_PGUP | STB_TEXTEDIT_K_SHIFT: {
1193b4be Poul Sander 2025-02-03 21:28 1006
         StbFindState find;
1193b4be Poul Sander 2025-02-03 21:28 1007
         StbTexteditRow row;
1193b4be Poul Sander 2025-02-03 21:28 1008
         int i, j, prev_scan, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;
1193b4be Poul Sander 2025-02-03 21:28 1009
         int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGUP;
1193b4be Poul Sander 2025-02-03 21:28 1010
         int row_count = is_page ? state->row_count_per_page : 1;
1193b4be Poul Sander 2025-02-03 21:28 1011
1193b4be Poul Sander 2025-02-03 21:28 1012
         if (!is_page && state->single_line) {
1193b4be Poul Sander 2025-02-03 21:28 1013
            // on windows, up&down become left&right
1193b4be Poul Sander 2025-02-03 21:28 1014
            key = STB_TEXTEDIT_K_LEFT | (key & STB_TEXTEDIT_K_SHIFT);
1193b4be Poul Sander 2025-02-03 21:28 1015
            goto retry;
1193b4be Poul Sander 2025-02-03 21:28 1016
         }
1193b4be Poul Sander 2025-02-03 21:28 1017
1193b4be Poul Sander 2025-02-03 21:28 1018
         if (sel)
1193b4be Poul Sander 2025-02-03 21:28 1019
            stb_textedit_prep_selection_at_cursor(state);
1193b4be Poul Sander 2025-02-03 21:28 1020
         else if (STB_TEXT_HAS_SELECTION(state))
1193b4be Poul Sander 2025-02-03 21:28 1021
            stb_textedit_move_to_first(state);
1193b4be Poul Sander 2025-02-03 21:28 1022
1193b4be Poul Sander 2025-02-03 21:28 1023
         // compute current position of cursor point
1193b4be Poul Sander 2025-02-03 21:28 1024
         stb_textedit_clamp(str, state);
1193b4be Poul Sander 2025-02-03 21:28 1025
         stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);
1193b4be Poul Sander 2025-02-03 21:28 1026
1193b4be Poul Sander 2025-02-03 21:28 1027
         for (j = 0; j < row_count; ++j) {
1193b4be Poul Sander 2025-02-03 21:28 1028
            float  x, goal_x = state->has_preferred_x ? state->preferred_x : find.x;
1193b4be Poul Sander 2025-02-03 21:28 1029
1193b4be Poul Sander 2025-02-03 21:28 1030
            // can only go up if there's a previous row
1193b4be Poul Sander 2025-02-03 21:28 1031
            if (find.prev_first == find.first_char)
1193b4be Poul Sander 2025-02-03 21:28 1032
               break;
1193b4be Poul Sander 2025-02-03 21:28 1033
1193b4be Poul Sander 2025-02-03 21:28 1034
            // now find character position up a row
1193b4be Poul Sander 2025-02-03 21:28 1035
            state->cursor = find.prev_first;
1193b4be Poul Sander 2025-02-03 21:28 1036
            STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 1037
            x = row.x0;
4e9e2555 Poul Sander 2025-10-05 10:15 1038
            for (i=0; i < row.num_chars; ) {
1193b4be Poul Sander 2025-02-03 21:28 1039
               float dx = STB_TEXTEDIT_GETWIDTH(str, find.prev_first, i);
4e9e2555 Poul Sander 2025-10-05 10:15 1040
               int next = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 1041
               #ifdef IMSTB_TEXTEDIT_GETWIDTH_NEWLINE
1193b4be Poul Sander 2025-02-03 21:28 1042
               if (dx == IMSTB_TEXTEDIT_GETWIDTH_NEWLINE)
1193b4be Poul Sander 2025-02-03 21:28 1043
                  break;
1193b4be Poul Sander 2025-02-03 21:28 1044
               #endif
1193b4be Poul Sander 2025-02-03 21:28 1045
               x += dx;
1193b4be Poul Sander 2025-02-03 21:28 1046
               if (x > goal_x)
1193b4be Poul Sander 2025-02-03 21:28 1047
                  break;
4e9e2555 Poul Sander 2025-10-05 10:15 1048
               i += next - state->cursor;
4e9e2555 Poul Sander 2025-10-05 10:15 1049
               state->cursor = next;
1193b4be Poul Sander 2025-02-03 21:28 1050
            }
1193b4be Poul Sander 2025-02-03 21:28 1051
            stb_textedit_clamp(str, state);
1193b4be Poul Sander 2025-02-03 21:28 1052
4e9e2555 Poul Sander 2025-10-05 10:15 1053
            if (state->cursor == find.first_char)
4e9e2555 Poul Sander 2025-10-05 10:15 1054
               str->LastMoveDirectionLR = ImGuiDir_Right;
4e9e2555 Poul Sander 2025-10-05 10:15 1055
            else if (state->cursor == find.prev_first)
4e9e2555 Poul Sander 2025-10-05 10:15 1056
               str->LastMoveDirectionLR = ImGuiDir_Left;
1193b4be Poul Sander 2025-02-03 21:28 1057
            state->has_preferred_x = 1;
1193b4be Poul Sander 2025-02-03 21:28 1058
            state->preferred_x = goal_x;
1193b4be Poul Sander 2025-02-03 21:28 1059
1193b4be Poul Sander 2025-02-03 21:28 1060
            if (sel)
1193b4be Poul Sander 2025-02-03 21:28 1061
               state->select_end = state->cursor;
1193b4be Poul Sander 2025-02-03 21:28 1062
1193b4be Poul Sander 2025-02-03 21:28 1063
            // go to previous line
1193b4be Poul Sander 2025-02-03 21:28 1064
            // (we need to scan previous line the hard way. maybe we could expose this as a new API function?)
1193b4be Poul Sander 2025-02-03 21:28 1065
            prev_scan = find.prev_first > 0 ? find.prev_first - 1 : 0;
4e9e2555 Poul Sander 2025-10-05 10:15 1066
            while (prev_scan > 0)
4e9e2555 Poul Sander 2025-10-05 10:15 1067
            {
4e9e2555 Poul Sander 2025-10-05 10:15 1068
               int prev = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, prev_scan);
4e9e2555 Poul Sander 2025-10-05 10:15 1069
               if (STB_TEXTEDIT_GETCHAR(str, prev) == STB_TEXTEDIT_NEWLINE)
4e9e2555 Poul Sander 2025-10-05 10:15 1070
                  break;
4e9e2555 Poul Sander 2025-10-05 10:15 1071
               prev_scan = prev;
4e9e2555 Poul Sander 2025-10-05 10:15 1072
            }
1193b4be Poul Sander 2025-02-03 21:28 1073
            find.first_char = find.prev_first;
4e9e2555 Poul Sander 2025-10-05 10:15 1074
            find.prev_first = STB_TEXTEDIT_MOVELINESTART(str, state, prev_scan);
1193b4be Poul Sander 2025-02-03 21:28 1075
         }
1193b4be Poul Sander 2025-02-03 21:28 1076
         break;
1193b4be Poul Sander 2025-02-03 21:28 1077
      }
1193b4be Poul Sander 2025-02-03 21:28 1078
1193b4be Poul Sander 2025-02-03 21:28 1079
      case STB_TEXTEDIT_K_DELETE:
1193b4be Poul Sander 2025-02-03 21:28 1080
      case STB_TEXTEDIT_K_DELETE | STB_TEXTEDIT_K_SHIFT:
1193b4be Poul Sander 2025-02-03 21:28 1081
         if (STB_TEXT_HAS_SELECTION(state))
1193b4be Poul Sander 2025-02-03 21:28 1082
            stb_textedit_delete_selection(str, state);
1193b4be Poul Sander 2025-02-03 21:28 1083
         else {
1193b4be Poul Sander 2025-02-03 21:28 1084
            int n = STB_TEXTEDIT_STRINGLEN(str);
1193b4be Poul Sander 2025-02-03 21:28 1085
            if (state->cursor < n)
1193b4be Poul Sander 2025-02-03 21:28 1086
               stb_textedit_delete(str, state, state->cursor, IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor) - state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 1087
         }
1193b4be Poul Sander 2025-02-03 21:28 1088
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 1089
         break;
1193b4be Poul Sander 2025-02-03 21:28 1090
1193b4be Poul Sander 2025-02-03 21:28 1091
      case STB_TEXTEDIT_K_BACKSPACE:
1193b4be Poul Sander 2025-02-03 21:28 1092
      case STB_TEXTEDIT_K_BACKSPACE | STB_TEXTEDIT_K_SHIFT:
1193b4be Poul Sander 2025-02-03 21:28 1093
         if (STB_TEXT_HAS_SELECTION(state))
1193b4be Poul Sander 2025-02-03 21:28 1094
            stb_textedit_delete_selection(str, state);
1193b4be Poul Sander 2025-02-03 21:28 1095
         else {
1193b4be Poul Sander 2025-02-03 21:28 1096
            stb_textedit_clamp(str, state);
1193b4be Poul Sander 2025-02-03 21:28 1097
            if (state->cursor > 0) {
1193b4be Poul Sander 2025-02-03 21:28 1098
               int prev = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 1099
               stb_textedit_delete(str, state, prev, state->cursor - prev);
1193b4be Poul Sander 2025-02-03 21:28 1100
               state->cursor = prev;
1193b4be Poul Sander 2025-02-03 21:28 1101
            }
1193b4be Poul Sander 2025-02-03 21:28 1102
         }
1193b4be Poul Sander 2025-02-03 21:28 1103
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 1104
         break;
1193b4be Poul Sander 2025-02-03 21:28 1105
1193b4be Poul Sander 2025-02-03 21:28 1106
#ifdef STB_TEXTEDIT_K_TEXTSTART2
1193b4be Poul Sander 2025-02-03 21:28 1107
      case STB_TEXTEDIT_K_TEXTSTART2:
1193b4be Poul Sander 2025-02-03 21:28 1108
#endif
1193b4be Poul Sander 2025-02-03 21:28 1109
      case STB_TEXTEDIT_K_TEXTSTART:
1193b4be Poul Sander 2025-02-03 21:28 1110
         state->cursor = state->select_start = state->select_end = 0;
1193b4be Poul Sander 2025-02-03 21:28 1111
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 1112
         break;
1193b4be Poul Sander 2025-02-03 21:28 1113
1193b4be Poul Sander 2025-02-03 21:28 1114
#ifdef STB_TEXTEDIT_K_TEXTEND2
1193b4be Poul Sander 2025-02-03 21:28 1115
      case STB_TEXTEDIT_K_TEXTEND2:
1193b4be Poul Sander 2025-02-03 21:28 1116
#endif
1193b4be Poul Sander 2025-02-03 21:28 1117
      case STB_TEXTEDIT_K_TEXTEND:
1193b4be Poul Sander 2025-02-03 21:28 1118
         state->cursor = STB_TEXTEDIT_STRINGLEN(str);
1193b4be Poul Sander 2025-02-03 21:28 1119
         state->select_start = state->select_end = 0;
1193b4be Poul Sander 2025-02-03 21:28 1120
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 1121
         break;
1193b4be Poul Sander 2025-02-03 21:28 1122
1193b4be Poul Sander 2025-02-03 21:28 1123
#ifdef STB_TEXTEDIT_K_TEXTSTART2
1193b4be Poul Sander 2025-02-03 21:28 1124
      case STB_TEXTEDIT_K_TEXTSTART2 | STB_TEXTEDIT_K_SHIFT:
1193b4be Poul Sander 2025-02-03 21:28 1125
#endif
1193b4be Poul Sander 2025-02-03 21:28 1126
      case STB_TEXTEDIT_K_TEXTSTART | STB_TEXTEDIT_K_SHIFT:
1193b4be Poul Sander 2025-02-03 21:28 1127
         stb_textedit_prep_selection_at_cursor(state);
1193b4be Poul Sander 2025-02-03 21:28 1128
         state->cursor = state->select_end = 0;
1193b4be Poul Sander 2025-02-03 21:28 1129
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 1130
         break;
1193b4be Poul Sander 2025-02-03 21:28 1131
1193b4be Poul Sander 2025-02-03 21:28 1132
#ifdef STB_TEXTEDIT_K_TEXTEND2
1193b4be Poul Sander 2025-02-03 21:28 1133
      case STB_TEXTEDIT_K_TEXTEND2 | STB_TEXTEDIT_K_SHIFT:
1193b4be Poul Sander 2025-02-03 21:28 1134
#endif
1193b4be Poul Sander 2025-02-03 21:28 1135
      case STB_TEXTEDIT_K_TEXTEND | STB_TEXTEDIT_K_SHIFT:
1193b4be Poul Sander 2025-02-03 21:28 1136
         stb_textedit_prep_selection_at_cursor(state);
1193b4be Poul Sander 2025-02-03 21:28 1137
         state->cursor = state->select_end = STB_TEXTEDIT_STRINGLEN(str);
1193b4be Poul Sander 2025-02-03 21:28 1138
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 1139
         break;
1193b4be Poul Sander 2025-02-03 21:28 1140
1193b4be Poul Sander 2025-02-03 21:28 1141
1193b4be Poul Sander 2025-02-03 21:28 1142
#ifdef STB_TEXTEDIT_K_LINESTART2
1193b4be Poul Sander 2025-02-03 21:28 1143
      case STB_TEXTEDIT_K_LINESTART2:
1193b4be Poul Sander 2025-02-03 21:28 1144
#endif
1193b4be Poul Sander 2025-02-03 21:28 1145
      case STB_TEXTEDIT_K_LINESTART:
1193b4be Poul Sander 2025-02-03 21:28 1146
         stb_textedit_clamp(str, state);
1193b4be Poul Sander 2025-02-03 21:28 1147
         stb_textedit_move_to_first(state);
4e9e2555 Poul Sander 2025-10-05 10:15 1148
         state->cursor = STB_TEXTEDIT_MOVELINESTART(str, state, state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 1149
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 1150
         break;
1193b4be Poul Sander 2025-02-03 21:28 1151
1193b4be Poul Sander 2025-02-03 21:28 1152
#ifdef STB_TEXTEDIT_K_LINEEND2
1193b4be Poul Sander 2025-02-03 21:28 1153
      case STB_TEXTEDIT_K_LINEEND2:
1193b4be Poul Sander 2025-02-03 21:28 1154
#endif
1193b4be Poul Sander 2025-02-03 21:28 1155
      case STB_TEXTEDIT_K_LINEEND: {
1193b4be Poul Sander 2025-02-03 21:28 1156
         stb_textedit_clamp(str, state);
4e9e2555 Poul Sander 2025-10-05 10:15 1157
         stb_textedit_move_to_last(str, state);
4e9e2555 Poul Sander 2025-10-05 10:15 1158
         state->cursor = STB_TEXTEDIT_MOVELINEEND(str, state, state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 1159
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 1160
         break;
1193b4be Poul Sander 2025-02-03 21:28 1161
      }
1193b4be Poul Sander 2025-02-03 21:28 1162
1193b4be Poul Sander 2025-02-03 21:28 1163
#ifdef STB_TEXTEDIT_K_LINESTART2
1193b4be Poul Sander 2025-02-03 21:28 1164
      case STB_TEXTEDIT_K_LINESTART2 | STB_TEXTEDIT_K_SHIFT:
1193b4be Poul Sander 2025-02-03 21:28 1165
#endif
1193b4be Poul Sander 2025-02-03 21:28 1166
      case STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT:
1193b4be Poul Sander 2025-02-03 21:28 1167
         stb_textedit_clamp(str, state);
1193b4be Poul Sander 2025-02-03 21:28 1168
         stb_textedit_prep_selection_at_cursor(state);
4e9e2555 Poul Sander 2025-10-05 10:15 1169
         state->cursor = STB_TEXTEDIT_MOVELINESTART(str, state, state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 1170
         state->select_end = state->cursor;
1193b4be Poul Sander 2025-02-03 21:28 1171
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 1172
         break;
1193b4be Poul Sander 2025-02-03 21:28 1173
1193b4be Poul Sander 2025-02-03 21:28 1174
#ifdef STB_TEXTEDIT_K_LINEEND2
1193b4be Poul Sander 2025-02-03 21:28 1175
      case STB_TEXTEDIT_K_LINEEND2 | STB_TEXTEDIT_K_SHIFT:
1193b4be Poul Sander 2025-02-03 21:28 1176
#endif
1193b4be Poul Sander 2025-02-03 21:28 1177
      case STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT: {
1193b4be Poul Sander 2025-02-03 21:28 1178
         stb_textedit_clamp(str, state);
1193b4be Poul Sander 2025-02-03 21:28 1179
         stb_textedit_prep_selection_at_cursor(state);
4e9e2555 Poul Sander 2025-10-05 10:15 1180
         state->cursor = STB_TEXTEDIT_MOVELINEEND(str, state, state->cursor);
1193b4be Poul Sander 2025-02-03 21:28 1181
         state->select_end = state->cursor;
1193b4be Poul Sander 2025-02-03 21:28 1182
         state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 1183
         break;
1193b4be Poul Sander 2025-02-03 21:28 1184
      }
1193b4be Poul Sander 2025-02-03 21:28 1185
   }
1193b4be Poul Sander 2025-02-03 21:28 1186
}
1193b4be Poul Sander 2025-02-03 21:28 1187
1193b4be Poul Sander 2025-02-03 21:28 1188
/////////////////////////////////////////////////////////////////////////////
1193b4be Poul Sander 2025-02-03 21:28 1189
//
1193b4be Poul Sander 2025-02-03 21:28 1190
//      Undo processing
1193b4be Poul Sander 2025-02-03 21:28 1191
//
1193b4be Poul Sander 2025-02-03 21:28 1192
// @OPTIMIZE: the undo/redo buffer should be circular
1193b4be Poul Sander 2025-02-03 21:28 1193
1193b4be Poul Sander 2025-02-03 21:28 1194
static void stb_textedit_flush_redo(StbUndoState *state)
1193b4be Poul Sander 2025-02-03 21:28 1195
{
1193b4be Poul Sander 2025-02-03 21:28 1196
   state->redo_point = IMSTB_TEXTEDIT_UNDOSTATECOUNT;
1193b4be Poul Sander 2025-02-03 21:28 1197
   state->redo_char_point = IMSTB_TEXTEDIT_UNDOCHARCOUNT;
1193b4be Poul Sander 2025-02-03 21:28 1198
}
1193b4be Poul Sander 2025-02-03 21:28 1199
1193b4be Poul Sander 2025-02-03 21:28 1200
// discard the oldest entry in the undo list
1193b4be Poul Sander 2025-02-03 21:28 1201
static void stb_textedit_discard_undo(StbUndoState *state)
1193b4be Poul Sander 2025-02-03 21:28 1202
{
1193b4be Poul Sander 2025-02-03 21:28 1203
   if (state->undo_point > 0) {
1193b4be Poul Sander 2025-02-03 21:28 1204
      // if the 0th undo state has characters, clean those up
1193b4be Poul Sander 2025-02-03 21:28 1205
      if (state->undo_rec[0].char_storage >= 0) {
1193b4be Poul Sander 2025-02-03 21:28 1206
         int n = state->undo_rec[0].insert_length, i;
1193b4be Poul Sander 2025-02-03 21:28 1207
         // delete n characters from all other records
1193b4be Poul Sander 2025-02-03 21:28 1208
         state->undo_char_point -= n;
1193b4be Poul Sander 2025-02-03 21:28 1209
         IMSTB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(IMSTB_TEXTEDIT_CHARTYPE)));
1193b4be Poul Sander 2025-02-03 21:28 1210
         for (i=0; i < state->undo_point; ++i)
1193b4be Poul Sander 2025-02-03 21:28 1211
            if (state->undo_rec[i].char_storage >= 0)
1193b4be Poul Sander 2025-02-03 21:28 1212
               state->undo_rec[i].char_storage -= n; // @OPTIMIZE: get rid of char_storage and infer it
1193b4be Poul Sander 2025-02-03 21:28 1213
      }
1193b4be Poul Sander 2025-02-03 21:28 1214
      --state->undo_point;
1193b4be Poul Sander 2025-02-03 21:28 1215
      IMSTB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0])));
1193b4be Poul Sander 2025-02-03 21:28 1216
   }
1193b4be Poul Sander 2025-02-03 21:28 1217
}
1193b4be Poul Sander 2025-02-03 21:28 1218
1193b4be Poul Sander 2025-02-03 21:28 1219
// discard the oldest entry in the redo list--it's bad if this
1193b4be Poul Sander 2025-02-03 21:28 1220
// ever happens, but because undo & redo have to store the actual
1193b4be Poul Sander 2025-02-03 21:28 1221
// characters in different cases, the redo character buffer can
1193b4be Poul Sander 2025-02-03 21:28 1222
// fill up even though the undo buffer didn't
1193b4be Poul Sander 2025-02-03 21:28 1223
static void stb_textedit_discard_redo(StbUndoState *state)
1193b4be Poul Sander 2025-02-03 21:28 1224
{
1193b4be Poul Sander 2025-02-03 21:28 1225
   int k = IMSTB_TEXTEDIT_UNDOSTATECOUNT-1;
1193b4be Poul Sander 2025-02-03 21:28 1226
1193b4be Poul Sander 2025-02-03 21:28 1227
   if (state->redo_point <= k) {
1193b4be Poul Sander 2025-02-03 21:28 1228
      // if the k'th undo state has characters, clean those up
1193b4be Poul Sander 2025-02-03 21:28 1229
      if (state->undo_rec[k].char_storage >= 0) {
1193b4be Poul Sander 2025-02-03 21:28 1230
         int n = state->undo_rec[k].insert_length, i;
1193b4be Poul Sander 2025-02-03 21:28 1231
         // move the remaining redo character data to the end of the buffer
1193b4be Poul Sander 2025-02-03 21:28 1232
         state->redo_char_point += n;
1193b4be Poul Sander 2025-02-03 21:28 1233
         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)));
1193b4be Poul Sander 2025-02-03 21:28 1234
         // adjust the position of all the other records to account for above memmove
1193b4be Poul Sander 2025-02-03 21:28 1235
         for (i=state->redo_point; i < k; ++i)
1193b4be Poul Sander 2025-02-03 21:28 1236
            if (state->undo_rec[i].char_storage >= 0)
1193b4be Poul Sander 2025-02-03 21:28 1237
               state->undo_rec[i].char_storage += n;
1193b4be Poul Sander 2025-02-03 21:28 1238
      }
1193b4be Poul Sander 2025-02-03 21:28 1239
      // now move all the redo records towards the end of the buffer; the first one is at 'redo_point'
1193b4be Poul Sander 2025-02-03 21:28 1240
      // [DEAR IMGUI]
1193b4be Poul Sander 2025-02-03 21:28 1241
      size_t move_size = (size_t)((IMSTB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point - 1) * sizeof(state->undo_rec[0]));
1193b4be Poul Sander 2025-02-03 21:28 1242
      const char* buf_begin = (char*)state->undo_rec; (void)buf_begin;
1193b4be Poul Sander 2025-02-03 21:28 1243
      const char* buf_end   = (char*)state->undo_rec + sizeof(state->undo_rec); (void)buf_end;
1193b4be Poul Sander 2025-02-03 21:28 1244
      IM_ASSERT(((char*)(state->undo_rec + state->redo_point)) >= buf_begin);
1193b4be Poul Sander 2025-02-03 21:28 1245
      IM_ASSERT(((char*)(state->undo_rec + state->redo_point + 1) + move_size) <= buf_end);
1193b4be Poul Sander 2025-02-03 21:28 1246
      IMSTB_TEXTEDIT_memmove(state->undo_rec + state->redo_point+1, state->undo_rec + state->redo_point, move_size);
1193b4be Poul Sander 2025-02-03 21:28 1247
1193b4be Poul Sander 2025-02-03 21:28 1248
      // now move redo_point to point to the new one
1193b4be Poul Sander 2025-02-03 21:28 1249
      ++state->redo_point;
1193b4be Poul Sander 2025-02-03 21:28 1250
   }
1193b4be Poul Sander 2025-02-03 21:28 1251
}
1193b4be Poul Sander 2025-02-03 21:28 1252
1193b4be Poul Sander 2025-02-03 21:28 1253
static StbUndoRecord *stb_text_create_undo_record(StbUndoState *state, int numchars)
1193b4be Poul Sander 2025-02-03 21:28 1254
{
1193b4be Poul Sander 2025-02-03 21:28 1255
   // any time we create a new undo record, we discard redo
1193b4be Poul Sander 2025-02-03 21:28 1256
   stb_textedit_flush_redo(state);
1193b4be Poul Sander 2025-02-03 21:28 1257
1193b4be Poul Sander 2025-02-03 21:28 1258
   // if we have no free records, we have to make room, by sliding the
1193b4be Poul Sander 2025-02-03 21:28 1259
   // existing records down
1193b4be Poul Sander 2025-02-03 21:28 1260
   if (state->undo_point == IMSTB_TEXTEDIT_UNDOSTATECOUNT)
1193b4be Poul Sander 2025-02-03 21:28 1261
      stb_textedit_discard_undo(state);
1193b4be Poul Sander 2025-02-03 21:28 1262
1193b4be Poul Sander 2025-02-03 21:28 1263
   // if the characters to store won't possibly fit in the buffer, we can't undo
1193b4be Poul Sander 2025-02-03 21:28 1264
   if (numchars > IMSTB_TEXTEDIT_UNDOCHARCOUNT) {
1193b4be Poul Sander 2025-02-03 21:28 1265
      state->undo_point = 0;
1193b4be Poul Sander 2025-02-03 21:28 1266
      state->undo_char_point = 0;
1193b4be Poul Sander 2025-02-03 21:28 1267
      return NULL;
1193b4be Poul Sander 2025-02-03 21:28 1268
   }
1193b4be Poul Sander 2025-02-03 21:28 1269
1193b4be Poul Sander 2025-02-03 21:28 1270
   // if we don't have enough free characters in the buffer, we have to make room
1193b4be Poul Sander 2025-02-03 21:28 1271
   while (state->undo_char_point + numchars > IMSTB_TEXTEDIT_UNDOCHARCOUNT)
1193b4be Poul Sander 2025-02-03 21:28 1272
      stb_textedit_discard_undo(state);
1193b4be Poul Sander 2025-02-03 21:28 1273
1193b4be Poul Sander 2025-02-03 21:28 1274
   return &state->undo_rec[state->undo_point++];
1193b4be Poul Sander 2025-02-03 21:28 1275
}
1193b4be Poul Sander 2025-02-03 21:28 1276
1193b4be Poul Sander 2025-02-03 21:28 1277
static IMSTB_TEXTEDIT_CHARTYPE *stb_text_createundo(StbUndoState *state, int pos, int insert_len, int delete_len)
1193b4be Poul Sander 2025-02-03 21:28 1278
{
1193b4be Poul Sander 2025-02-03 21:28 1279
   StbUndoRecord *r = stb_text_create_undo_record(state, insert_len);
1193b4be Poul Sander 2025-02-03 21:28 1280
   if (r == NULL)
1193b4be Poul Sander 2025-02-03 21:28 1281
      return NULL;
1193b4be Poul Sander 2025-02-03 21:28 1282
1193b4be Poul Sander 2025-02-03 21:28 1283
   r->where = pos;
1193b4be Poul Sander 2025-02-03 21:28 1284
   r->insert_length = (IMSTB_TEXTEDIT_POSITIONTYPE) insert_len;
1193b4be Poul Sander 2025-02-03 21:28 1285
   r->delete_length = (IMSTB_TEXTEDIT_POSITIONTYPE) delete_len;
1193b4be Poul Sander 2025-02-03 21:28 1286
1193b4be Poul Sander 2025-02-03 21:28 1287
   if (insert_len == 0) {
1193b4be Poul Sander 2025-02-03 21:28 1288
      r->char_storage = -1;
1193b4be Poul Sander 2025-02-03 21:28 1289
      return NULL;
1193b4be Poul Sander 2025-02-03 21:28 1290
   } else {
1193b4be Poul Sander 2025-02-03 21:28 1291
      r->char_storage = state->undo_char_point;
1193b4be Poul Sander 2025-02-03 21:28 1292
      state->undo_char_point += insert_len;
1193b4be Poul Sander 2025-02-03 21:28 1293
      return &state->undo_char[r->char_storage];
1193b4be Poul Sander 2025-02-03 21:28 1294
   }
1193b4be Poul Sander 2025-02-03 21:28 1295
}
1193b4be Poul Sander 2025-02-03 21:28 1296
1193b4be Poul Sander 2025-02-03 21:28 1297
static void stb_text_undo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state)
1193b4be Poul Sander 2025-02-03 21:28 1298
{
1193b4be Poul Sander 2025-02-03 21:28 1299
   StbUndoState *s = &state->undostate;
1193b4be Poul Sander 2025-02-03 21:28 1300
   StbUndoRecord u, *r;
1193b4be Poul Sander 2025-02-03 21:28 1301
   if (s->undo_point == 0)
1193b4be Poul Sander 2025-02-03 21:28 1302
      return;
1193b4be Poul Sander 2025-02-03 21:28 1303
1193b4be Poul Sander 2025-02-03 21:28 1304
   // we need to do two things: apply the undo record, and create a redo record
1193b4be Poul Sander 2025-02-03 21:28 1305
   u = s->undo_rec[s->undo_point-1];
1193b4be Poul Sander 2025-02-03 21:28 1306
   r = &s->undo_rec[s->redo_point-1];
1193b4be Poul Sander 2025-02-03 21:28 1307
   r->char_storage = -1;
1193b4be Poul Sander 2025-02-03 21:28 1308
1193b4be Poul Sander 2025-02-03 21:28 1309
   r->insert_length = u.delete_length;
1193b4be Poul Sander 2025-02-03 21:28 1310
   r->delete_length = u.insert_length;
1193b4be Poul Sander 2025-02-03 21:28 1311
   r->where = u.where;
1193b4be Poul Sander 2025-02-03 21:28 1312
1193b4be Poul Sander 2025-02-03 21:28 1313
   if (u.delete_length) {
1193b4be Poul Sander 2025-02-03 21:28 1314
      // if the undo record says to delete characters, then the redo record will
1193b4be Poul Sander 2025-02-03 21:28 1315
      // need to re-insert the characters that get deleted, so we need to store
1193b4be Poul Sander 2025-02-03 21:28 1316
      // them.
1193b4be Poul Sander 2025-02-03 21:28 1317
1193b4be Poul Sander 2025-02-03 21:28 1318
      // there are three cases:
1193b4be Poul Sander 2025-02-03 21:28 1319
      //    there's enough room to store the characters
1193b4be Poul Sander 2025-02-03 21:28 1320
      //    characters stored for *redoing* don't leave room for redo
1193b4be Poul Sander 2025-02-03 21:28 1321
      //    characters stored for *undoing* don't leave room for redo
1193b4be Poul Sander 2025-02-03 21:28 1322
      // if the last is true, we have to bail
1193b4be Poul Sander 2025-02-03 21:28 1323
1193b4be Poul Sander 2025-02-03 21:28 1324
      if (s->undo_char_point + u.delete_length >= IMSTB_TEXTEDIT_UNDOCHARCOUNT) {
1193b4be Poul Sander 2025-02-03 21:28 1325
         // the undo records take up too much character space; there's no space to store the redo characters
1193b4be Poul Sander 2025-02-03 21:28 1326
         r->insert_length = 0;
1193b4be Poul Sander 2025-02-03 21:28 1327
      } else {
1193b4be Poul Sander 2025-02-03 21:28 1328
         int i;
1193b4be Poul Sander 2025-02-03 21:28 1329
1193b4be Poul Sander 2025-02-03 21:28 1330
         // there's definitely room to store the characters eventually
1193b4be Poul Sander 2025-02-03 21:28 1331
         while (s->undo_char_point + u.delete_length > s->redo_char_point) {
1193b4be Poul Sander 2025-02-03 21:28 1332
            // should never happen:
1193b4be Poul Sander 2025-02-03 21:28 1333
            if (s->redo_point == IMSTB_TEXTEDIT_UNDOSTATECOUNT)
1193b4be Poul Sander 2025-02-03 21:28 1334
               return;
1193b4be Poul Sander 2025-02-03 21:28 1335
            // there's currently not enough room, so discard a redo record
1193b4be Poul Sander 2025-02-03 21:28 1336
            stb_textedit_discard_redo(s);
1193b4be Poul Sander 2025-02-03 21:28 1337
         }
1193b4be Poul Sander 2025-02-03 21:28 1338
         r = &s->undo_rec[s->redo_point-1];
1193b4be Poul Sander 2025-02-03 21:28 1339
1193b4be Poul Sander 2025-02-03 21:28 1340
         r->char_storage = s->redo_char_point - u.delete_length;
1193b4be Poul Sander 2025-02-03 21:28 1341
         s->redo_char_point = s->redo_char_point - u.delete_length;
1193b4be Poul Sander 2025-02-03 21:28 1342
1193b4be Poul Sander 2025-02-03 21:28 1343
         // now save the characters
1193b4be Poul Sander 2025-02-03 21:28 1344
         for (i=0; i < u.delete_length; ++i)
1193b4be Poul Sander 2025-02-03 21:28 1345
            s->undo_char[r->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u.where + i);
1193b4be Poul Sander 2025-02-03 21:28 1346
      }
1193b4be Poul Sander 2025-02-03 21:28 1347
1193b4be Poul Sander 2025-02-03 21:28 1348
      // now we can carry out the deletion
1193b4be Poul Sander 2025-02-03 21:28 1349
      STB_TEXTEDIT_DELETECHARS(str, u.where, u.delete_length);
1193b4be Poul Sander 2025-02-03 21:28 1350
   }
1193b4be Poul Sander 2025-02-03 21:28 1351
1193b4be Poul Sander 2025-02-03 21:28 1352
   // check type of recorded action:
1193b4be Poul Sander 2025-02-03 21:28 1353
   if (u.insert_length) {
1193b4be Poul Sander 2025-02-03 21:28 1354
      // easy case: was a deletion, so we need to insert n characters
1193b4be Poul Sander 2025-02-03 21:28 1355
      STB_TEXTEDIT_INSERTCHARS(str, u.where, &s->undo_char[u.char_storage], u.insert_length);
1193b4be Poul Sander 2025-02-03 21:28 1356
      s->undo_char_point -= u.insert_length;
1193b4be Poul Sander 2025-02-03 21:28 1357
   }
1193b4be Poul Sander 2025-02-03 21:28 1358
1193b4be Poul Sander 2025-02-03 21:28 1359
   state->cursor = u.where + u.insert_length;
1193b4be Poul Sander 2025-02-03 21:28 1360
1193b4be Poul Sander 2025-02-03 21:28 1361
   s->undo_point--;
1193b4be Poul Sander 2025-02-03 21:28 1362
   s->redo_point--;
1193b4be Poul Sander 2025-02-03 21:28 1363
}
1193b4be Poul Sander 2025-02-03 21:28 1364
1193b4be Poul Sander 2025-02-03 21:28 1365
static void stb_text_redo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state)
1193b4be Poul Sander 2025-02-03 21:28 1366
{
1193b4be Poul Sander 2025-02-03 21:28 1367
   StbUndoState *s = &state->undostate;
1193b4be Poul Sander 2025-02-03 21:28 1368
   StbUndoRecord *u, r;
1193b4be Poul Sander 2025-02-03 21:28 1369
   if (s->redo_point == IMSTB_TEXTEDIT_UNDOSTATECOUNT)
1193b4be Poul Sander 2025-02-03 21:28 1370
      return;
1193b4be Poul Sander 2025-02-03 21:28 1371
1193b4be Poul Sander 2025-02-03 21:28 1372
   // we need to do two things: apply the redo record, and create an undo record
1193b4be Poul Sander 2025-02-03 21:28 1373
   u = &s->undo_rec[s->undo_point];
1193b4be Poul Sander 2025-02-03 21:28 1374
   r = s->undo_rec[s->redo_point];
1193b4be Poul Sander 2025-02-03 21:28 1375
1193b4be Poul Sander 2025-02-03 21:28 1376
   // we KNOW there must be room for the undo record, because the redo record
1193b4be Poul Sander 2025-02-03 21:28 1377
   // was derived from an undo record
1193b4be Poul Sander 2025-02-03 21:28 1378
1193b4be Poul Sander 2025-02-03 21:28 1379
   u->delete_length = r.insert_length;
1193b4be Poul Sander 2025-02-03 21:28 1380
   u->insert_length = r.delete_length;
1193b4be Poul Sander 2025-02-03 21:28 1381
   u->where = r.where;
1193b4be Poul Sander 2025-02-03 21:28 1382
   u->char_storage = -1;
1193b4be Poul Sander 2025-02-03 21:28 1383
1193b4be Poul Sander 2025-02-03 21:28 1384
   if (r.delete_length) {
1193b4be Poul Sander 2025-02-03 21:28 1385
      // the redo record requires us to delete characters, so the undo record
1193b4be Poul Sander 2025-02-03 21:28 1386
      // needs to store the characters
1193b4be Poul Sander 2025-02-03 21:28 1387
1193b4be Poul Sander 2025-02-03 21:28 1388
      if (s->undo_char_point + u->insert_length > s->redo_char_point) {
1193b4be Poul Sander 2025-02-03 21:28 1389
         u->insert_length = 0;
1193b4be Poul Sander 2025-02-03 21:28 1390
         u->delete_length = 0;
1193b4be Poul Sander 2025-02-03 21:28 1391
      } else {
1193b4be Poul Sander 2025-02-03 21:28 1392
         int i;
1193b4be Poul Sander 2025-02-03 21:28 1393
         u->char_storage = s->undo_char_point;
1193b4be Poul Sander 2025-02-03 21:28 1394
         s->undo_char_point = s->undo_char_point + u->insert_length;
1193b4be Poul Sander 2025-02-03 21:28 1395
1193b4be Poul Sander 2025-02-03 21:28 1396
         // now save the characters
1193b4be Poul Sander 2025-02-03 21:28 1397
         for (i=0; i < u->insert_length; ++i)
1193b4be Poul Sander 2025-02-03 21:28 1398
            s->undo_char[u->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u->where + i);
1193b4be Poul Sander 2025-02-03 21:28 1399
      }
1193b4be Poul Sander 2025-02-03 21:28 1400
1193b4be Poul Sander 2025-02-03 21:28 1401
      STB_TEXTEDIT_DELETECHARS(str, r.where, r.delete_length);
1193b4be Poul Sander 2025-02-03 21:28 1402
   }
1193b4be Poul Sander 2025-02-03 21:28 1403
1193b4be Poul Sander 2025-02-03 21:28 1404
   if (r.insert_length) {
1193b4be Poul Sander 2025-02-03 21:28 1405
      // easy case: need to insert n characters
1193b4be Poul Sander 2025-02-03 21:28 1406
      STB_TEXTEDIT_INSERTCHARS(str, r.where, &s->undo_char[r.char_storage], r.insert_length);
1193b4be Poul Sander 2025-02-03 21:28 1407
      s->redo_char_point += r.insert_length;
1193b4be Poul Sander 2025-02-03 21:28 1408
   }
1193b4be Poul Sander 2025-02-03 21:28 1409
1193b4be Poul Sander 2025-02-03 21:28 1410
   state->cursor = r.where + r.insert_length;
1193b4be Poul Sander 2025-02-03 21:28 1411
1193b4be Poul Sander 2025-02-03 21:28 1412
   s->undo_point++;
1193b4be Poul Sander 2025-02-03 21:28 1413
   s->redo_point++;
1193b4be Poul Sander 2025-02-03 21:28 1414
}
1193b4be Poul Sander 2025-02-03 21:28 1415
1193b4be Poul Sander 2025-02-03 21:28 1416
static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length)
1193b4be Poul Sander 2025-02-03 21:28 1417
{
1193b4be Poul Sander 2025-02-03 21:28 1418
   stb_text_createundo(&state->undostate, where, 0, length);
1193b4be Poul Sander 2025-02-03 21:28 1419
}
1193b4be Poul Sander 2025-02-03 21:28 1420
1193b4be Poul Sander 2025-02-03 21:28 1421
static void stb_text_makeundo_delete(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length)
1193b4be Poul Sander 2025-02-03 21:28 1422
{
1193b4be Poul Sander 2025-02-03 21:28 1423
   int i;
1193b4be Poul Sander 2025-02-03 21:28 1424
   IMSTB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, length, 0);
1193b4be Poul Sander 2025-02-03 21:28 1425
   if (p) {
1193b4be Poul Sander 2025-02-03 21:28 1426
      for (i=0; i < length; ++i)
1193b4be Poul Sander 2025-02-03 21:28 1427
         p[i] = STB_TEXTEDIT_GETCHAR(str, where+i);
1193b4be Poul Sander 2025-02-03 21:28 1428
   }
1193b4be Poul Sander 2025-02-03 21:28 1429
}
1193b4be Poul Sander 2025-02-03 21:28 1430
1193b4be Poul Sander 2025-02-03 21:28 1431
static void stb_text_makeundo_replace(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length)
1193b4be Poul Sander 2025-02-03 21:28 1432
{
1193b4be Poul Sander 2025-02-03 21:28 1433
   int i;
1193b4be Poul Sander 2025-02-03 21:28 1434
   IMSTB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, old_length, new_length);
1193b4be Poul Sander 2025-02-03 21:28 1435
   if (p) {
1193b4be Poul Sander 2025-02-03 21:28 1436
      for (i=0; i < old_length; ++i)
1193b4be Poul Sander 2025-02-03 21:28 1437
         p[i] = STB_TEXTEDIT_GETCHAR(str, where+i);
1193b4be Poul Sander 2025-02-03 21:28 1438
   }
1193b4be Poul Sander 2025-02-03 21:28 1439
}
1193b4be Poul Sander 2025-02-03 21:28 1440
1193b4be Poul Sander 2025-02-03 21:28 1441
// reset the state to default
1193b4be Poul Sander 2025-02-03 21:28 1442
static void stb_textedit_clear_state(STB_TexteditState *state, int is_single_line)
1193b4be Poul Sander 2025-02-03 21:28 1443
{
1193b4be Poul Sander 2025-02-03 21:28 1444
   state->undostate.undo_point = 0;
1193b4be Poul Sander 2025-02-03 21:28 1445
   state->undostate.undo_char_point = 0;
1193b4be Poul Sander 2025-02-03 21:28 1446
   state->undostate.redo_point = IMSTB_TEXTEDIT_UNDOSTATECOUNT;
1193b4be Poul Sander 2025-02-03 21:28 1447
   state->undostate.redo_char_point = IMSTB_TEXTEDIT_UNDOCHARCOUNT;
1193b4be Poul Sander 2025-02-03 21:28 1448
   state->select_end = state->select_start = 0;
1193b4be Poul Sander 2025-02-03 21:28 1449
   state->cursor = 0;
1193b4be Poul Sander 2025-02-03 21:28 1450
   state->has_preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 1451
   state->preferred_x = 0;
1193b4be Poul Sander 2025-02-03 21:28 1452
   state->cursor_at_end_of_line = 0;
1193b4be Poul Sander 2025-02-03 21:28 1453
   state->initialized = 1;
1193b4be Poul Sander 2025-02-03 21:28 1454
   state->single_line = (unsigned char) is_single_line;
1193b4be Poul Sander 2025-02-03 21:28 1455
   state->insert_mode = 0;
1193b4be Poul Sander 2025-02-03 21:28 1456
   state->row_count_per_page = 0;
1193b4be Poul Sander 2025-02-03 21:28 1457
}
1193b4be Poul Sander 2025-02-03 21:28 1458
1193b4be Poul Sander 2025-02-03 21:28 1459
// API initialize
1193b4be Poul Sander 2025-02-03 21:28 1460
static void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line)
1193b4be Poul Sander 2025-02-03 21:28 1461
{
1193b4be Poul Sander 2025-02-03 21:28 1462
   stb_textedit_clear_state(state, is_single_line);
1193b4be Poul Sander 2025-02-03 21:28 1463
}
1193b4be Poul Sander 2025-02-03 21:28 1464
1193b4be Poul Sander 2025-02-03 21:28 1465
#if defined(__GNUC__) || defined(__clang__)
1193b4be Poul Sander 2025-02-03 21:28 1466
#pragma GCC diagnostic push
1193b4be Poul Sander 2025-02-03 21:28 1467
#pragma GCC diagnostic ignored "-Wcast-qual"
1193b4be Poul Sander 2025-02-03 21:28 1468
#endif
1193b4be Poul Sander 2025-02-03 21:28 1469
1193b4be Poul Sander 2025-02-03 21:28 1470
static int stb_textedit_paste(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, IMSTB_TEXTEDIT_CHARTYPE const *ctext, int len)
1193b4be Poul Sander 2025-02-03 21:28 1471
{
1193b4be Poul Sander 2025-02-03 21:28 1472
   return stb_textedit_paste_internal(str, state, (IMSTB_TEXTEDIT_CHARTYPE *) ctext, len);
1193b4be Poul Sander 2025-02-03 21:28 1473
}
1193b4be Poul Sander 2025-02-03 21:28 1474
1193b4be Poul Sander 2025-02-03 21:28 1475
#if defined(__GNUC__) || defined(__clang__)
1193b4be Poul Sander 2025-02-03 21:28 1476
#pragma GCC diagnostic pop
1193b4be Poul Sander 2025-02-03 21:28 1477
#endif
1193b4be Poul Sander 2025-02-03 21:28 1478
1193b4be Poul Sander 2025-02-03 21:28 1479
#endif//IMSTB_TEXTEDIT_IMPLEMENTATION
1193b4be Poul Sander 2025-02-03 21:28 1480
1193b4be Poul Sander 2025-02-03 21:28 1481
/*
1193b4be Poul Sander 2025-02-03 21:28 1482
------------------------------------------------------------------------------
1193b4be Poul Sander 2025-02-03 21:28 1483
This software is available under 2 licenses -- choose whichever you prefer.
1193b4be Poul Sander 2025-02-03 21:28 1484
------------------------------------------------------------------------------
1193b4be Poul Sander 2025-02-03 21:28 1485
ALTERNATIVE A - MIT License
1193b4be Poul Sander 2025-02-03 21:28 1486
Copyright (c) 2017 Sean Barrett
1193b4be Poul Sander 2025-02-03 21:28 1487
Permission is hereby granted, free of charge, to any person obtaining a copy of
1193b4be Poul Sander 2025-02-03 21:28 1488
this software and associated documentation files (the "Software"), to deal in
1193b4be Poul Sander 2025-02-03 21:28 1489
the Software without restriction, including without limitation the rights to
1193b4be Poul Sander 2025-02-03 21:28 1490
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
1193b4be Poul Sander 2025-02-03 21:28 1491
of the Software, and to permit persons to whom the Software is furnished to do
1193b4be Poul Sander 2025-02-03 21:28 1492
so, subject to the following conditions:
1193b4be Poul Sander 2025-02-03 21:28 1493
The above copyright notice and this permission notice shall be included in all
1193b4be Poul Sander 2025-02-03 21:28 1494
copies or substantial portions of the Software.
1193b4be Poul Sander 2025-02-03 21:28 1495
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1193b4be Poul Sander 2025-02-03 21:28 1496
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1193b4be Poul Sander 2025-02-03 21:28 1497
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1193b4be Poul Sander 2025-02-03 21:28 1498
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1193b4be Poul Sander 2025-02-03 21:28 1499
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1193b4be Poul Sander 2025-02-03 21:28 1500
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1193b4be Poul Sander 2025-02-03 21:28 1501
SOFTWARE.
1193b4be Poul Sander 2025-02-03 21:28 1502
------------------------------------------------------------------------------
1193b4be Poul Sander 2025-02-03 21:28 1503
ALTERNATIVE B - Public Domain (www.unlicense.org)
1193b4be Poul Sander 2025-02-03 21:28 1504
This is free and unencumbered software released into the public domain.
1193b4be Poul Sander 2025-02-03 21:28 1505
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
1193b4be Poul Sander 2025-02-03 21:28 1506
software, either in source code form or as a compiled binary, for any purpose,
1193b4be Poul Sander 2025-02-03 21:28 1507
commercial or non-commercial, and by any means.
1193b4be Poul Sander 2025-02-03 21:28 1508
In jurisdictions that recognize copyright laws, the author or authors of this
1193b4be Poul Sander 2025-02-03 21:28 1509
software dedicate any and all copyright interest in the software to the public
1193b4be Poul Sander 2025-02-03 21:28 1510
domain. We make this dedication for the benefit of the public at large and to
1193b4be Poul Sander 2025-02-03 21:28 1511
the detriment of our heirs and successors. We intend this dedication to be an
1193b4be Poul Sander 2025-02-03 21:28 1512
overt act of relinquishment in perpetuity of all present and future rights to
1193b4be Poul Sander 2025-02-03 21:28 1513
this software under copyright law.
1193b4be Poul Sander 2025-02-03 21:28 1514
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1193b4be Poul Sander 2025-02-03 21:28 1515
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1193b4be Poul Sander 2025-02-03 21:28 1516
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1193b4be Poul Sander 2025-02-03 21:28 1517
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
1193b4be Poul Sander 2025-02-03 21:28 1518
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1193b4be Poul Sander 2025-02-03 21:28 1519
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1193b4be Poul Sander 2025-02-03 21:28 1520
------------------------------------------------------------------------------
1193b4be Poul Sander 2025-02-03 21:28 1521
*/
1970-01-01 00:00 1522