1 // [ImGui] this is a slightly modified version of stb_textedit.h 1.12. Those changes would need to be pushed into nothings/stb
2 // [ImGui] - 2018-06: fixed undo/redo after pasting large amount of text (over 32 kb). Redo will still fail when undo buffers are exhausted, but text won't be corrupted (see nothings/stb issue #620)
3 // [ImGui] - 2018-06: fix in stb_textedit_discard_redo (see https://github.com/nothings/stb/issues/321)
4 // [ImGui] - fixed some minor warnings
6 // stb_textedit.h - v1.12 - public domain - Sean Barrett
7 // Development of this library was sponsored by RAD Game Tools
9 // This C header file implements the guts of a multi-line text-editing
10 // widget; you implement display, word-wrapping, and low-level string
11 // insertion/deletion, and stb_textedit will map user inputs into
12 // insertions & deletions, plus updates to the cursor position,
13 // selection state, and undo state.
15 // It is intended for use in games and other systems that need to build
16 // their own custom widgets and which do not have heavy text-editing
17 // requirements (this library is not recommended for use for editing large
18 // texts, as its performance does not scale and it has limited undo).
20 // Non-trivial behaviors are modelled after Windows text controls.
25 // See end of file for license information.
30 // Uses the C runtime function 'memmove', which you can override
31 // by defining STB_TEXTEDIT_memmove before the implementation.
32 // Uses no other functions. Performs no runtime allocations.
37 // 1.12 (2018-01-29) user can change STB_TEXTEDIT_KEYTYPE, fix redo to avoid crash
38 // 1.11 (2017-03-03) fix HOME on last line, dragging off single-line textfield
39 // 1.10 (2016-10-25) supress warnings about casting away const with -Wcast-qual
40 // 1.9 (2016-08-27) customizable move-by-word
41 // 1.8 (2016-04-02) better keyboard handling when mouse button is down
42 // 1.7 (2015-09-13) change y range handling in case baseline is non-0
43 // 1.6 (2015-04-15) allow STB_TEXTEDIT_memmove
44 // 1.5 (2014-09-10) add support for secondary keys for OS X
45 // 1.4 (2014-08-17) fix signed/unsigned warnings
46 // 1.3 (2014-06-19) fix mouse clicking to round to nearest char boundary
47 // 1.2 (2014-05-27) fix some RAD types that had crept into the new code
48 // 1.1 (2013-12-15) move-by-word (requires STB_TEXTEDIT_IS_SPACE )
49 // 1.0 (2012-07-26) improve documentation, initial public release
50 // 0.3 (2012-02-24) bugfixes, single-line mode; insert mode
51 // 0.2 (2011-11-28) fixes to undo/redo
52 // 0.1 (2010-07-08) initial version
54 // ADDITIONAL CONTRIBUTORS
56 // Ulf Winklemann: move-by-word in 1.1
57 // Fabian Giesen: secondary key inputs in 1.5
58 // Martins Mozeiko: STB_TEXTEDIT_memmove in 1.6
68 // This file behaves differently depending on what symbols you define
69 // before including it.
74 // If you do not define STB_TEXTEDIT_IMPLEMENTATION before including this,
75 // it will operate in "header file" mode. In this mode, it declares a
76 // single public symbol, STB_TexteditState, which encapsulates the current
77 // state of a text widget (except for the string, which you will store
80 // To compile in this mode, you must define STB_TEXTEDIT_CHARTYPE to a
81 // primitive type that defines a single character (e.g. char, wchar_t, etc).
83 // To save space or increase undo-ability, you can optionally define the
84 // following things that are used by the undo system:
86 // STB_TEXTEDIT_POSITIONTYPE small int type encoding a valid cursor position
87 // STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow
88 // STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer
90 // If you don't define these, they are set to permissive types and
91 // moderate sizes. The undo system does no memory allocations, so
92 // it grows STB_TexteditState by the worst-case storage which is (in bytes):
94 // [4 + 3 * sizeof(STB_TEXTEDIT_POSITIONTYPE)] * STB_TEXTEDIT_UNDOSTATE_COUNT
95 // + sizeof(STB_TEXTEDIT_CHARTYPE) * STB_TEXTEDIT_UNDOCHAR_COUNT
98 // Implementation mode:
100 // If you define STB_TEXTEDIT_IMPLEMENTATION before including this, it
101 // will compile the implementation of the text edit widget, depending
102 // on a large number of symbols which must be defined before the include.
104 // The implementation is defined only as static functions. You will then
105 // need to provide your own APIs in the same file which will access the
108 // The basic concept is that you provide a "string" object which
109 // behaves like an array of characters. stb_textedit uses indices to
110 // refer to positions in the string, implicitly representing positions
111 // in the displayed textedit. This is true for both plain text and
112 // rich text; even with rich text stb_truetype interacts with your
113 // code as if there was an array of all the displayed characters.
115 // Symbols that must be the same in header-file and implementation mode:
117 // STB_TEXTEDIT_CHARTYPE the character type
118 // STB_TEXTEDIT_POSITIONTYPE small type that is a valid cursor position
119 // STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow
120 // STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer
122 // Symbols you must define for implementation mode:
124 // STB_TEXTEDIT_STRING the type of object representing a string being edited,
125 // typically this is a wrapper object with other data you need
127 // STB_TEXTEDIT_STRINGLEN(obj) the length of the string (ideally O(1))
128 // STB_TEXTEDIT_LAYOUTROW(&r,obj,n) returns the results of laying out a line of characters
129 // starting from character #n (see discussion below)
130 // STB_TEXTEDIT_GETWIDTH(obj,n,i) returns the pixel delta from the xpos of the i'th character
131 // to the xpos of the i+1'th char for a line of characters
132 // starting at character #n (i.e. accounts for kerning
133 // with previous char)
134 // STB_TEXTEDIT_KEYTOTEXT(k) maps a keyboard input to an insertable character
135 // (return type is int, -1 means not valid to insert)
136 // STB_TEXTEDIT_GETCHAR(obj,i) returns the i'th character of obj, 0-based
137 // STB_TEXTEDIT_NEWLINE the character returned by _GETCHAR() we recognize
138 // as manually wordwrapping for end-of-line positioning
140 // STB_TEXTEDIT_DELETECHARS(obj,i,n) delete n characters starting at i
141 // STB_TEXTEDIT_INSERTCHARS(obj,i,c*,n) insert n characters at i (pointed to by STB_TEXTEDIT_CHARTYPE*)
143 // STB_TEXTEDIT_K_SHIFT a power of two that is or'd in to a keyboard input to represent the shift key
145 // STB_TEXTEDIT_K_LEFT keyboard input to move cursor left
146 // STB_TEXTEDIT_K_RIGHT keyboard input to move cursor right
147 // STB_TEXTEDIT_K_UP keyboard input to move cursor up
148 // STB_TEXTEDIT_K_DOWN keyboard input to move cursor down
149 // STB_TEXTEDIT_K_LINESTART keyboard input to move cursor to start of line // e.g. HOME
150 // STB_TEXTEDIT_K_LINEEND keyboard input to move cursor to end of line // e.g. END
151 // STB_TEXTEDIT_K_TEXTSTART keyboard input to move cursor to start of text // e.g. ctrl-HOME
152 // STB_TEXTEDIT_K_TEXTEND keyboard input to move cursor to end of text // e.g. ctrl-END
153 // STB_TEXTEDIT_K_DELETE keyboard input to delete selection or character under cursor
154 // STB_TEXTEDIT_K_BACKSPACE keyboard input to delete selection or character left of cursor
155 // STB_TEXTEDIT_K_UNDO keyboard input to perform undo
156 // STB_TEXTEDIT_K_REDO keyboard input to perform redo
159 // STB_TEXTEDIT_K_INSERT keyboard input to toggle insert mode
160 // STB_TEXTEDIT_IS_SPACE(ch) true if character is whitespace (e.g. 'isspace'),
161 // required for default WORDLEFT/WORDRIGHT handlers
162 // STB_TEXTEDIT_MOVEWORDLEFT(obj,i) custom handler for WORDLEFT, returns index to move cursor to
163 // STB_TEXTEDIT_MOVEWORDRIGHT(obj,i) custom handler for WORDRIGHT, returns index to move cursor to
164 // STB_TEXTEDIT_K_WORDLEFT keyboard input to move cursor left one word // e.g. ctrl-LEFT
165 // STB_TEXTEDIT_K_WORDRIGHT keyboard input to move cursor right one word // e.g. ctrl-RIGHT
166 // STB_TEXTEDIT_K_LINESTART2 secondary keyboard input to move cursor to start of line
167 // STB_TEXTEDIT_K_LINEEND2 secondary keyboard input to move cursor to end of line
168 // STB_TEXTEDIT_K_TEXTSTART2 secondary keyboard input to move cursor to start of text
169 // STB_TEXTEDIT_K_TEXTEND2 secondary keyboard input to move cursor to end of text
172 // STB_TEXTEDIT_K_PGUP keyboard input to move cursor up a page
173 // STB_TEXTEDIT_K_PGDOWN keyboard input to move cursor down a page
175 // Keyboard input must be encoded as a single integer value; e.g. a character code
176 // and some bitflags that represent shift states. to simplify the interface, SHIFT must
177 // be a bitflag, so we can test the shifted state of cursor movements to allow selection,
178 // i.e. (STB_TEXTED_K_RIGHT|STB_TEXTEDIT_K_SHIFT) should be shifted right-arrow.
180 // You can encode other things, such as CONTROL or ALT, in additional bits, and
181 // then test for their presence in e.g. STB_TEXTEDIT_K_WORDLEFT. For example,
182 // my Windows implementations add an additional CONTROL bit, and an additional KEYDOWN
183 // bit. Then all of the STB_TEXTEDIT_K_ values bitwise-or in the KEYDOWN bit,
184 // and I pass both WM_KEYDOWN and WM_CHAR events to the "key" function in the
185 // API below. The control keys will only match WM_KEYDOWN events because of the
186 // keydown bit I add, and STB_TEXTEDIT_KEYTOTEXT only tests for the KEYDOWN
187 // bit so it only decodes WM_CHAR events.
189 // STB_TEXTEDIT_LAYOUTROW returns information about the shape of one displayed
190 // row of characters assuming they start on the i'th character--the width and
191 // the height and the number of characters consumed. This allows this library
192 // to traverse the entire layout incrementally. You need to compute word-wrapping
195 // Each textfield keeps its own insert mode state, which is not how normal
196 // applications work. To keep an app-wide insert mode, update/copy the
197 // "insert_mode" field of STB_TexteditState before/after calling API functions.
201 // void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line)
203 // void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)
204 // void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)
205 // int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)
206 // int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len)
207 // void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXEDIT_KEYTYPE key)
209 // Each of these functions potentially updates the string and updates the
213 // set the textedit state to a known good default state when initially
214 // constructing the textedit.
217 // call this with the mouse x,y on a mouse down; it will update the cursor
218 // and reset the selection start/end to the cursor point. the x,y must
219 // be relative to the text widget, with (0,0) being the top left.
222 // call this with the mouse x,y on a mouse drag/up; it will update the
223 // cursor and the selection end point
226 // call this to delete the current selection; returns true if there was
227 // one. you should FIRST copy the current selection to the system paste buffer.
228 // (To copy, just copy the current selection out of the string yourself.)
231 // call this to paste text at the current cursor point or over the current
232 // selection if there is one.
235 // call this for keyboard inputs sent to the textfield. you can use it
236 // for "key down" events or for "translated" key events. if you need to
237 // do both (as in Win32), or distinguish Unicode characters from control
238 // inputs, set a high bit to distinguish the two; then you can define the
239 // various definitions like STB_TEXTEDIT_K_LEFT have the is-key-event bit
240 // set, and make STB_TEXTEDIT_KEYTOCHAR check that the is-key-event bit is
241 // clear. STB_TEXTEDIT_KEYTYPE defaults to int, but you can #define it to
242 // anything other type you wante before including.
245 // When rendering, you can read the cursor position and selection state from
246 // the STB_TexteditState.
251 // This is designed to be usable in IMGUI, so it allows for the possibility of
252 // running in an IMGUI that has NOT cached the multi-line layout. For this
253 // reason, it provides an interface that is compatible with computing the
254 // layout incrementally--we try to make sure we make as few passes through
255 // as possible. (For example, to locate the mouse pointer in the text, we
256 // could define functions that return the X and Y positions of characters
257 // and binary search Y and then X, but if we're doing dynamic layout this
258 // will run the layout algorithm many times, so instead we manually search
259 // forward in one pass. Similar logic applies to e.g. up-arrow and
260 // down-arrow movement.)
262 // If it's run in a widget that *has* cached the layout, then this is less
263 // efficient, but it's not horrible on modern computers. But you wouldn't
264 // want to edit million-line files with it.
267 ////////////////////////////////////////////////////////////////////////////
268 ////////////////////////////////////////////////////////////////////////////
270 //// Header-file mode
274 #ifndef INCLUDE_STB_TEXTEDIT_H
275 #define INCLUDE_STB_TEXTEDIT_H
277 ////////////////////////////////////////////////////////////////////////
281 // Definition of STB_TexteditState which you should store
282 // per-textfield; it includes cursor position, selection state,
286 #ifndef STB_TEXTEDIT_UNDOSTATECOUNT
287 #define STB_TEXTEDIT_UNDOSTATECOUNT 99
289 #ifndef STB_TEXTEDIT_UNDOCHARCOUNT
290 #define STB_TEXTEDIT_UNDOCHARCOUNT 999
292 #ifndef STB_TEXTEDIT_CHARTYPE
293 #define STB_TEXTEDIT_CHARTYPE int
295 #ifndef STB_TEXTEDIT_POSITIONTYPE
296 #define STB_TEXTEDIT_POSITIONTYPE int
302 STB_TEXTEDIT_POSITIONTYPE where
;
303 STB_TEXTEDIT_POSITIONTYPE insert_length
;
304 STB_TEXTEDIT_POSITIONTYPE delete_length
;
311 StbUndoRecord undo_rec
[STB_TEXTEDIT_UNDOSTATECOUNT
];
312 STB_TEXTEDIT_CHARTYPE undo_char
[STB_TEXTEDIT_UNDOCHARCOUNT
];
313 short undo_point
, redo_point
;
314 int undo_char_point
, redo_char_point
;
319 /////////////////////
325 // position of the text cursor within the string
327 int select_start
; // selection start point
329 // selection start and end point in characters; if equal, no selection.
330 // note that start may be less than or greater than end (e.g. when
331 // dragging the mouse, start is where the initial click was, and you
332 // can drag in either direction)
334 unsigned char insert_mode
;
335 // each textfield keeps its own insert mode state. to keep an app-wide
336 // insert mode, copy this value in/out of the app state
338 /////////////////////
342 unsigned char cursor_at_end_of_line
; // not implemented yet
343 unsigned char initialized
;
344 unsigned char has_preferred_x
;
345 unsigned char single_line
;
346 unsigned char padding1
, padding2
, padding3
;
347 float preferred_x
; // this determines where the cursor up/down tries to seek to along x
348 StbUndoState undostate
;
352 ////////////////////////////////////////////////////////////////////////
356 // Result of layout query, used by stb_textedit to determine where
357 // the text in each row is.
359 // result of layout query
362 float x0
,x1
; // starting x location, end x location (allows for align=right, etc)
363 float baseline_y_delta
; // position of baseline relative to previous row's baseline
364 float ymin
,ymax
; // height of row above and below baseline
367 #endif //INCLUDE_STB_TEXTEDIT_H
370 ////////////////////////////////////////////////////////////////////////////
371 ////////////////////////////////////////////////////////////////////////////
373 //// Implementation mode
378 // implementation isn't include-guarded, since it might have indirectly
379 // included just the "header" portion
380 #ifdef STB_TEXTEDIT_IMPLEMENTATION
382 #ifndef STB_TEXTEDIT_memmove
384 #define STB_TEXTEDIT_memmove memmove
388 /////////////////////////////////////////////////////////////////////////////
390 // Mouse input handling
393 // traverse the layout to locate the nearest character to a display position
394 static int stb_text_locate_coord(STB_TEXTEDIT_STRING
*str
, float x
, float y
)
397 int n
= STB_TEXTEDIT_STRINGLEN(str
);
398 float base_y
= 0, prev_x
;
405 // search rows to find one that straddles 'y'
407 STB_TEXTEDIT_LAYOUTROW(&r
, str
, i
);
408 if (r
.num_chars
<= 0)
411 if (i
==0 && y
< base_y
+ r
.ymin
)
414 if (y
< base_y
+ r
.ymax
)
418 base_y
+= r
.baseline_y_delta
;
421 // below all text, return 'after' last character
425 // check if it's before the beginning of the line
429 // check if it's before the end of the line
431 // search characters in row for one that straddles 'x'
433 for (k
=0; k
< r
.num_chars
; ++k
) {
434 float w
= STB_TEXTEDIT_GETWIDTH(str
, i
, k
);
443 // shouldn't happen, but if it does, fall through to end-of-line case
446 // if the last character is a newline, return that. otherwise return 'after' the last character
447 if (STB_TEXTEDIT_GETCHAR(str
, i
+r
.num_chars
-1) == STB_TEXTEDIT_NEWLINE
)
448 return i
+r
.num_chars
-1;
450 return i
+r
.num_chars
;
453 // API click: on mouse down, move the cursor to the clicked location, and reset the selection
454 static void stb_textedit_click(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
, float x
, float y
)
456 // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse
457 // goes off the top or bottom of the text
458 if( state
->single_line
)
461 STB_TEXTEDIT_LAYOUTROW(&r
, str
, 0);
465 state
->cursor
= stb_text_locate_coord(str
, x
, y
);
466 state
->select_start
= state
->cursor
;
467 state
->select_end
= state
->cursor
;
468 state
->has_preferred_x
= 0;
471 // API drag: on mouse drag, move the cursor and selection endpoint to the clicked location
472 static void stb_textedit_drag(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
, float x
, float y
)
476 // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse
477 // goes off the top or bottom of the text
478 if( state
->single_line
)
481 STB_TEXTEDIT_LAYOUTROW(&r
, str
, 0);
485 if (state
->select_start
== state
->select_end
)
486 state
->select_start
= state
->cursor
;
488 p
= stb_text_locate_coord(str
, x
, y
);
489 state
->cursor
= state
->select_end
= p
;
492 /////////////////////////////////////////////////////////////////////////////
494 // Keyboard input handling
497 // forward declarations
498 static void stb_text_undo(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
);
499 static void stb_text_redo(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
);
500 static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
, int where
, int length
);
501 static void stb_text_makeundo_insert(STB_TexteditState
*state
, int where
, int length
);
502 static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
, int where
, int old_length
, int new_length
);
506 float x
,y
; // position of n'th character
507 float height
; // height of line
508 int first_char
, length
; // first char of row, and length
509 int prev_first
; // first char of previous row
512 // find the x/y location of a character, and remember info about the previous row in
513 // case we get a move-up event (for page up, we'll have to rescan)
514 static void stb_textedit_find_charpos(StbFindState
*find
, STB_TEXTEDIT_STRING
*str
, int n
, int single_line
)
518 int z
= STB_TEXTEDIT_STRINGLEN(str
);
522 // if it's at the end, then find the last line -- simpler than trying to
523 // explicitly handle this case in the regular code
525 STB_TEXTEDIT_LAYOUTROW(&r
, str
, 0);
527 find
->first_char
= 0;
529 find
->height
= r
.ymax
- r
.ymin
;
536 STB_TEXTEDIT_LAYOUTROW(&r
, str
, i
);
540 find
->first_char
= i
;
542 find
->prev_first
= prev_start
;
547 // search rows to find the one that straddles character n
551 STB_TEXTEDIT_LAYOUTROW(&r
, str
, i
);
552 if (n
< i
+ r
.num_chars
)
556 find
->y
+= r
.baseline_y_delta
;
559 find
->first_char
= first
= i
;
560 find
->length
= r
.num_chars
;
561 find
->height
= r
.ymax
- r
.ymin
;
562 find
->prev_first
= prev_start
;
564 // now scan to find xpos
567 for (i
=0; first
+i
< n
; ++i
)
568 find
->x
+= STB_TEXTEDIT_GETWIDTH(str
, first
, i
);
571 #define STB_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end)
573 // make the selection/cursor state valid if client altered the string
574 static void stb_textedit_clamp(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
)
576 int n
= STB_TEXTEDIT_STRINGLEN(str
);
577 if (STB_TEXT_HAS_SELECTION(state
)) {
578 if (state
->select_start
> n
) state
->select_start
= n
;
579 if (state
->select_end
> n
) state
->select_end
= n
;
580 // if clamping forced them to be equal, move the cursor to match
581 if (state
->select_start
== state
->select_end
)
582 state
->cursor
= state
->select_start
;
584 if (state
->cursor
> n
) state
->cursor
= n
;
587 // delete characters while updating undo
588 static void stb_textedit_delete(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
, int where
, int len
)
590 stb_text_makeundo_delete(str
, state
, where
, len
);
591 STB_TEXTEDIT_DELETECHARS(str
, where
, len
);
592 state
->has_preferred_x
= 0;
595 // delete the section
596 static void stb_textedit_delete_selection(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
)
598 stb_textedit_clamp(str
, state
);
599 if (STB_TEXT_HAS_SELECTION(state
)) {
600 if (state
->select_start
< state
->select_end
) {
601 stb_textedit_delete(str
, state
, state
->select_start
, state
->select_end
- state
->select_start
);
602 state
->select_end
= state
->cursor
= state
->select_start
;
604 stb_textedit_delete(str
, state
, state
->select_end
, state
->select_start
- state
->select_end
);
605 state
->select_start
= state
->cursor
= state
->select_end
;
607 state
->has_preferred_x
= 0;
611 // canoncialize the selection so start <= end
612 static void stb_textedit_sortselection(STB_TexteditState
*state
)
614 if (state
->select_end
< state
->select_start
) {
615 int temp
= state
->select_end
;
616 state
->select_end
= state
->select_start
;
617 state
->select_start
= temp
;
621 // move cursor to first character of selection
622 static void stb_textedit_move_to_first(STB_TexteditState
*state
)
624 if (STB_TEXT_HAS_SELECTION(state
)) {
625 stb_textedit_sortselection(state
);
626 state
->cursor
= state
->select_start
;
627 state
->select_end
= state
->select_start
;
628 state
->has_preferred_x
= 0;
632 // move cursor to last character of selection
633 static void stb_textedit_move_to_last(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
)
635 if (STB_TEXT_HAS_SELECTION(state
)) {
636 stb_textedit_sortselection(state
);
637 stb_textedit_clamp(str
, state
);
638 state
->cursor
= state
->select_end
;
639 state
->select_start
= state
->select_end
;
640 state
->has_preferred_x
= 0;
644 #ifdef STB_TEXTEDIT_IS_SPACE
645 static int is_word_boundary( STB_TEXTEDIT_STRING
*str
, int idx
)
647 return idx
> 0 ? (STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str
,idx
-1) ) && !STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str
, idx
) ) ) : 1;
650 #ifndef STB_TEXTEDIT_MOVEWORDLEFT
651 static int stb_textedit_move_to_word_previous( STB_TEXTEDIT_STRING
*str
, int c
)
653 --c
; // always move at least one character
654 while( c
>= 0 && !is_word_boundary( str
, c
) )
662 #define STB_TEXTEDIT_MOVEWORDLEFT stb_textedit_move_to_word_previous
665 #ifndef STB_TEXTEDIT_MOVEWORDRIGHT
666 static int stb_textedit_move_to_word_next( STB_TEXTEDIT_STRING
*str
, int c
)
668 const int len
= STB_TEXTEDIT_STRINGLEN(str
);
669 ++c
; // always move at least one character
670 while( c
< len
&& !is_word_boundary( str
, c
) )
678 #define STB_TEXTEDIT_MOVEWORDRIGHT stb_textedit_move_to_word_next
683 // update selection and cursor to match each other
684 static void stb_textedit_prep_selection_at_cursor(STB_TexteditState
*state
)
686 if (!STB_TEXT_HAS_SELECTION(state
))
687 state
->select_start
= state
->select_end
= state
->cursor
;
689 state
->cursor
= state
->select_end
;
692 // API cut: delete selection
693 static int stb_textedit_cut(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
)
695 if (STB_TEXT_HAS_SELECTION(state
)) {
696 stb_textedit_delete_selection(str
,state
); // implicity clamps
697 state
->has_preferred_x
= 0;
703 // API paste: replace existing selection with passed-in text
704 static int stb_textedit_paste_internal(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
, STB_TEXTEDIT_CHARTYPE
*text
, int len
)
706 // if there's a selection, the paste should delete it
707 stb_textedit_clamp(str
, state
);
708 stb_textedit_delete_selection(str
,state
);
709 // try to insert the characters
710 if (STB_TEXTEDIT_INSERTCHARS(str
, state
->cursor
, text
, len
)) {
711 stb_text_makeundo_insert(state
, state
->cursor
, len
);
712 state
->cursor
+= len
;
713 state
->has_preferred_x
= 0;
716 // remove the undo since we didn't actually insert the characters
717 if (state
->undostate
.undo_point
)
718 --state
->undostate
.undo_point
;
722 #ifndef STB_TEXTEDIT_KEYTYPE
723 #define STB_TEXTEDIT_KEYTYPE int
726 // API key: process a keyboard input
727 static void stb_textedit_key(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
, STB_TEXTEDIT_KEYTYPE key
)
732 int c
= STB_TEXTEDIT_KEYTOTEXT(key
);
734 STB_TEXTEDIT_CHARTYPE ch
= (STB_TEXTEDIT_CHARTYPE
) c
;
736 // can't add newline in single-line mode
737 if (c
== '\n' && state
->single_line
)
740 if (state
->insert_mode
&& !STB_TEXT_HAS_SELECTION(state
) && state
->cursor
< STB_TEXTEDIT_STRINGLEN(str
)) {
741 stb_text_makeundo_replace(str
, state
, state
->cursor
, 1, 1);
742 STB_TEXTEDIT_DELETECHARS(str
, state
->cursor
, 1);
743 if (STB_TEXTEDIT_INSERTCHARS(str
, state
->cursor
, &ch
, 1)) {
745 state
->has_preferred_x
= 0;
748 stb_textedit_delete_selection(str
,state
); // implicity clamps
749 if (STB_TEXTEDIT_INSERTCHARS(str
, state
->cursor
, &ch
, 1)) {
750 stb_text_makeundo_insert(state
, state
->cursor
, 1);
752 state
->has_preferred_x
= 0;
759 #ifdef STB_TEXTEDIT_K_INSERT
760 case STB_TEXTEDIT_K_INSERT
:
761 state
->insert_mode
= !state
->insert_mode
;
765 case STB_TEXTEDIT_K_UNDO
:
766 stb_text_undo(str
, state
);
767 state
->has_preferred_x
= 0;
770 case STB_TEXTEDIT_K_REDO
:
771 stb_text_redo(str
, state
);
772 state
->has_preferred_x
= 0;
775 case STB_TEXTEDIT_K_LEFT
:
776 // if currently there's a selection, move cursor to start of selection
777 if (STB_TEXT_HAS_SELECTION(state
))
778 stb_textedit_move_to_first(state
);
780 if (state
->cursor
> 0)
782 state
->has_preferred_x
= 0;
785 case STB_TEXTEDIT_K_RIGHT
:
786 // if currently there's a selection, move cursor to end of selection
787 if (STB_TEXT_HAS_SELECTION(state
))
788 stb_textedit_move_to_last(str
, state
);
791 stb_textedit_clamp(str
, state
);
792 state
->has_preferred_x
= 0;
795 case STB_TEXTEDIT_K_LEFT
| STB_TEXTEDIT_K_SHIFT
:
796 stb_textedit_clamp(str
, state
);
797 stb_textedit_prep_selection_at_cursor(state
);
798 // move selection left
799 if (state
->select_end
> 0)
801 state
->cursor
= state
->select_end
;
802 state
->has_preferred_x
= 0;
805 #ifdef STB_TEXTEDIT_MOVEWORDLEFT
806 case STB_TEXTEDIT_K_WORDLEFT
:
807 if (STB_TEXT_HAS_SELECTION(state
))
808 stb_textedit_move_to_first(state
);
810 state
->cursor
= STB_TEXTEDIT_MOVEWORDLEFT(str
, state
->cursor
);
811 stb_textedit_clamp( str
, state
);
815 case STB_TEXTEDIT_K_WORDLEFT
| STB_TEXTEDIT_K_SHIFT
:
816 if( !STB_TEXT_HAS_SELECTION( state
) )
817 stb_textedit_prep_selection_at_cursor(state
);
819 state
->cursor
= STB_TEXTEDIT_MOVEWORDLEFT(str
, state
->cursor
);
820 state
->select_end
= state
->cursor
;
822 stb_textedit_clamp( str
, state
);
826 #ifdef STB_TEXTEDIT_MOVEWORDRIGHT
827 case STB_TEXTEDIT_K_WORDRIGHT
:
828 if (STB_TEXT_HAS_SELECTION(state
))
829 stb_textedit_move_to_last(str
, state
);
831 state
->cursor
= STB_TEXTEDIT_MOVEWORDRIGHT(str
, state
->cursor
);
832 stb_textedit_clamp( str
, state
);
836 case STB_TEXTEDIT_K_WORDRIGHT
| STB_TEXTEDIT_K_SHIFT
:
837 if( !STB_TEXT_HAS_SELECTION( state
) )
838 stb_textedit_prep_selection_at_cursor(state
);
840 state
->cursor
= STB_TEXTEDIT_MOVEWORDRIGHT(str
, state
->cursor
);
841 state
->select_end
= state
->cursor
;
843 stb_textedit_clamp( str
, state
);
847 case STB_TEXTEDIT_K_RIGHT
| STB_TEXTEDIT_K_SHIFT
:
848 stb_textedit_prep_selection_at_cursor(state
);
849 // move selection right
851 stb_textedit_clamp(str
, state
);
852 state
->cursor
= state
->select_end
;
853 state
->has_preferred_x
= 0;
856 case STB_TEXTEDIT_K_DOWN
:
857 case STB_TEXTEDIT_K_DOWN
| STB_TEXTEDIT_K_SHIFT
: {
860 int i
, sel
= (key
& STB_TEXTEDIT_K_SHIFT
) != 0;
862 if (state
->single_line
) {
863 // on windows, up&down in single-line behave like left&right
864 key
= STB_TEXTEDIT_K_RIGHT
| (key
& STB_TEXTEDIT_K_SHIFT
);
869 stb_textedit_prep_selection_at_cursor(state
);
870 else if (STB_TEXT_HAS_SELECTION(state
))
871 stb_textedit_move_to_last(str
,state
);
873 // compute current position of cursor point
874 stb_textedit_clamp(str
, state
);
875 stb_textedit_find_charpos(&find
, str
, state
->cursor
, state
->single_line
);
877 // now find character position down a row
879 float goal_x
= state
->has_preferred_x
? state
->preferred_x
: find
.x
;
881 int start
= find
.first_char
+ find
.length
;
882 state
->cursor
= start
;
883 STB_TEXTEDIT_LAYOUTROW(&row
, str
, state
->cursor
);
885 for (i
=0; i
< row
.num_chars
; ++i
) {
886 float dx
= STB_TEXTEDIT_GETWIDTH(str
, start
, i
);
887 #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE
888 if (dx
== STB_TEXTEDIT_GETWIDTH_NEWLINE
)
896 stb_textedit_clamp(str
, state
);
898 state
->has_preferred_x
= 1;
899 state
->preferred_x
= goal_x
;
902 state
->select_end
= state
->cursor
;
907 case STB_TEXTEDIT_K_UP
:
908 case STB_TEXTEDIT_K_UP
| STB_TEXTEDIT_K_SHIFT
: {
911 int i
, sel
= (key
& STB_TEXTEDIT_K_SHIFT
) != 0;
913 if (state
->single_line
) {
914 // on windows, up&down become left&right
915 key
= STB_TEXTEDIT_K_LEFT
| (key
& STB_TEXTEDIT_K_SHIFT
);
920 stb_textedit_prep_selection_at_cursor(state
);
921 else if (STB_TEXT_HAS_SELECTION(state
))
922 stb_textedit_move_to_first(state
);
924 // compute current position of cursor point
925 stb_textedit_clamp(str
, state
);
926 stb_textedit_find_charpos(&find
, str
, state
->cursor
, state
->single_line
);
928 // can only go up if there's a previous row
929 if (find
.prev_first
!= find
.first_char
) {
930 // now find character position up a row
931 float goal_x
= state
->has_preferred_x
? state
->preferred_x
: find
.x
;
933 state
->cursor
= find
.prev_first
;
934 STB_TEXTEDIT_LAYOUTROW(&row
, str
, state
->cursor
);
936 for (i
=0; i
< row
.num_chars
; ++i
) {
937 float dx
= STB_TEXTEDIT_GETWIDTH(str
, find
.prev_first
, i
);
938 #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE
939 if (dx
== STB_TEXTEDIT_GETWIDTH_NEWLINE
)
947 stb_textedit_clamp(str
, state
);
949 state
->has_preferred_x
= 1;
950 state
->preferred_x
= goal_x
;
953 state
->select_end
= state
->cursor
;
958 case STB_TEXTEDIT_K_DELETE
:
959 case STB_TEXTEDIT_K_DELETE
| STB_TEXTEDIT_K_SHIFT
:
960 if (STB_TEXT_HAS_SELECTION(state
))
961 stb_textedit_delete_selection(str
, state
);
963 int n
= STB_TEXTEDIT_STRINGLEN(str
);
964 if (state
->cursor
< n
)
965 stb_textedit_delete(str
, state
, state
->cursor
, 1);
967 state
->has_preferred_x
= 0;
970 case STB_TEXTEDIT_K_BACKSPACE
:
971 case STB_TEXTEDIT_K_BACKSPACE
| STB_TEXTEDIT_K_SHIFT
:
972 if (STB_TEXT_HAS_SELECTION(state
))
973 stb_textedit_delete_selection(str
, state
);
975 stb_textedit_clamp(str
, state
);
976 if (state
->cursor
> 0) {
977 stb_textedit_delete(str
, state
, state
->cursor
-1, 1);
981 state
->has_preferred_x
= 0;
984 #ifdef STB_TEXTEDIT_K_TEXTSTART2
985 case STB_TEXTEDIT_K_TEXTSTART2
:
987 case STB_TEXTEDIT_K_TEXTSTART
:
988 state
->cursor
= state
->select_start
= state
->select_end
= 0;
989 state
->has_preferred_x
= 0;
992 #ifdef STB_TEXTEDIT_K_TEXTEND2
993 case STB_TEXTEDIT_K_TEXTEND2
:
995 case STB_TEXTEDIT_K_TEXTEND
:
996 state
->cursor
= STB_TEXTEDIT_STRINGLEN(str
);
997 state
->select_start
= state
->select_end
= 0;
998 state
->has_preferred_x
= 0;
1001 #ifdef STB_TEXTEDIT_K_TEXTSTART2
1002 case STB_TEXTEDIT_K_TEXTSTART2
| STB_TEXTEDIT_K_SHIFT
:
1004 case STB_TEXTEDIT_K_TEXTSTART
| STB_TEXTEDIT_K_SHIFT
:
1005 stb_textedit_prep_selection_at_cursor(state
);
1006 state
->cursor
= state
->select_end
= 0;
1007 state
->has_preferred_x
= 0;
1010 #ifdef STB_TEXTEDIT_K_TEXTEND2
1011 case STB_TEXTEDIT_K_TEXTEND2
| STB_TEXTEDIT_K_SHIFT
:
1013 case STB_TEXTEDIT_K_TEXTEND
| STB_TEXTEDIT_K_SHIFT
:
1014 stb_textedit_prep_selection_at_cursor(state
);
1015 state
->cursor
= state
->select_end
= STB_TEXTEDIT_STRINGLEN(str
);
1016 state
->has_preferred_x
= 0;
1020 #ifdef STB_TEXTEDIT_K_LINESTART2
1021 case STB_TEXTEDIT_K_LINESTART2
:
1023 case STB_TEXTEDIT_K_LINESTART
:
1024 stb_textedit_clamp(str
, state
);
1025 stb_textedit_move_to_first(state
);
1026 if (state
->single_line
)
1028 else while (state
->cursor
> 0 && STB_TEXTEDIT_GETCHAR(str
, state
->cursor
-1) != STB_TEXTEDIT_NEWLINE
)
1030 state
->has_preferred_x
= 0;
1033 #ifdef STB_TEXTEDIT_K_LINEEND2
1034 case STB_TEXTEDIT_K_LINEEND2
:
1036 case STB_TEXTEDIT_K_LINEEND
: {
1037 int n
= STB_TEXTEDIT_STRINGLEN(str
);
1038 stb_textedit_clamp(str
, state
);
1039 stb_textedit_move_to_first(state
);
1040 if (state
->single_line
)
1042 else while (state
->cursor
< n
&& STB_TEXTEDIT_GETCHAR(str
, state
->cursor
) != STB_TEXTEDIT_NEWLINE
)
1044 state
->has_preferred_x
= 0;
1048 #ifdef STB_TEXTEDIT_K_LINESTART2
1049 case STB_TEXTEDIT_K_LINESTART2
| STB_TEXTEDIT_K_SHIFT
:
1051 case STB_TEXTEDIT_K_LINESTART
| STB_TEXTEDIT_K_SHIFT
:
1052 stb_textedit_clamp(str
, state
);
1053 stb_textedit_prep_selection_at_cursor(state
);
1054 if (state
->single_line
)
1056 else while (state
->cursor
> 0 && STB_TEXTEDIT_GETCHAR(str
, state
->cursor
-1) != STB_TEXTEDIT_NEWLINE
)
1058 state
->select_end
= state
->cursor
;
1059 state
->has_preferred_x
= 0;
1062 #ifdef STB_TEXTEDIT_K_LINEEND2
1063 case STB_TEXTEDIT_K_LINEEND2
| STB_TEXTEDIT_K_SHIFT
:
1065 case STB_TEXTEDIT_K_LINEEND
| STB_TEXTEDIT_K_SHIFT
: {
1066 int n
= STB_TEXTEDIT_STRINGLEN(str
);
1067 stb_textedit_clamp(str
, state
);
1068 stb_textedit_prep_selection_at_cursor(state
);
1069 if (state
->single_line
)
1071 else while (state
->cursor
< n
&& STB_TEXTEDIT_GETCHAR(str
, state
->cursor
) != STB_TEXTEDIT_NEWLINE
)
1073 state
->select_end
= state
->cursor
;
1074 state
->has_preferred_x
= 0;
1079 // STB_TEXTEDIT_K_PGUP - move cursor up a page
1080 // STB_TEXTEDIT_K_PGDOWN - move cursor down a page
1084 /////////////////////////////////////////////////////////////////////////////
1088 // @OPTIMIZE: the undo/redo buffer should be circular
1090 static void stb_textedit_flush_redo(StbUndoState
*state
)
1092 state
->redo_point
= STB_TEXTEDIT_UNDOSTATECOUNT
;
1093 state
->redo_char_point
= STB_TEXTEDIT_UNDOCHARCOUNT
;
1096 // discard the oldest entry in the undo list
1097 static void stb_textedit_discard_undo(StbUndoState
*state
)
1099 if (state
->undo_point
> 0) {
1100 // if the 0th undo state has characters, clean those up
1101 if (state
->undo_rec
[0].char_storage
>= 0) {
1102 int n
= state
->undo_rec
[0].insert_length
, i
;
1103 // delete n characters from all other records
1104 state
->undo_char_point
-= n
;
1105 STB_TEXTEDIT_memmove(state
->undo_char
, state
->undo_char
+ n
, (size_t) (state
->undo_char_point
*sizeof(STB_TEXTEDIT_CHARTYPE
)));
1106 for (i
=0; i
< state
->undo_point
; ++i
)
1107 if (state
->undo_rec
[i
].char_storage
>= 0)
1108 state
->undo_rec
[i
].char_storage
-= n
; // @OPTIMIZE: get rid of char_storage and infer it
1110 --state
->undo_point
;
1111 STB_TEXTEDIT_memmove(state
->undo_rec
, state
->undo_rec
+1, (size_t) (state
->undo_point
*sizeof(state
->undo_rec
[0])));
1115 // discard the oldest entry in the redo list--it's bad if this
1116 // ever happens, but because undo & redo have to store the actual
1117 // characters in different cases, the redo character buffer can
1118 // fill up even though the undo buffer didn't
1119 static void stb_textedit_discard_redo(StbUndoState
*state
)
1121 int k
= STB_TEXTEDIT_UNDOSTATECOUNT
-1;
1123 if (state
->redo_point
<= k
) {
1124 // if the k'th undo state has characters, clean those up
1125 if (state
->undo_rec
[k
].char_storage
>= 0) {
1126 int n
= state
->undo_rec
[k
].insert_length
, i
;
1127 // move the remaining redo character data to the end of the buffer
1128 state
->redo_char_point
+= n
;
1129 STB_TEXTEDIT_memmove(state
->undo_char
+ state
->redo_char_point
, state
->undo_char
+ state
->redo_char_point
-n
, (size_t) ((STB_TEXTEDIT_UNDOCHARCOUNT
- state
->redo_char_point
)*sizeof(STB_TEXTEDIT_CHARTYPE
)));
1130 // adjust the position of all the other records to account for above memmove
1131 for (i
=state
->redo_point
; i
< k
; ++i
)
1132 if (state
->undo_rec
[i
].char_storage
>= 0)
1133 state
->undo_rec
[i
].char_storage
+= n
;
1135 // now move all the redo records towards the end of the buffer; the first one is at 'redo_point'
1136 STB_TEXTEDIT_memmove(state
->undo_rec
+ state
->redo_point
+1, state
->undo_rec
+ state
->redo_point
, (size_t) ((STB_TEXTEDIT_UNDOSTATECOUNT
- state
->redo_point
)*sizeof(state
->undo_rec
[0])));
1137 // now move redo_point to point to the new one
1138 ++state
->redo_point
;
1142 static StbUndoRecord
*stb_text_create_undo_record(StbUndoState
*state
, int numchars
)
1144 // any time we create a new undo record, we discard redo
1145 stb_textedit_flush_redo(state
);
1147 // if we have no free records, we have to make room, by sliding the
1148 // existing records down
1149 if (state
->undo_point
== STB_TEXTEDIT_UNDOSTATECOUNT
)
1150 stb_textedit_discard_undo(state
);
1152 // if the characters to store won't possibly fit in the buffer, we can't undo
1153 if (numchars
> STB_TEXTEDIT_UNDOCHARCOUNT
) {
1154 state
->undo_point
= 0;
1155 state
->undo_char_point
= 0;
1159 // if we don't have enough free characters in the buffer, we have to make room
1160 while (state
->undo_char_point
+ numchars
> STB_TEXTEDIT_UNDOCHARCOUNT
)
1161 stb_textedit_discard_undo(state
);
1163 return &state
->undo_rec
[state
->undo_point
++];
1166 static STB_TEXTEDIT_CHARTYPE
*stb_text_createundo(StbUndoState
*state
, int pos
, int insert_len
, int delete_len
)
1168 StbUndoRecord
*r
= stb_text_create_undo_record(state
, insert_len
);
1173 r
->insert_length
= (STB_TEXTEDIT_POSITIONTYPE
) insert_len
;
1174 r
->delete_length
= (STB_TEXTEDIT_POSITIONTYPE
) delete_len
;
1176 if (insert_len
== 0) {
1177 r
->char_storage
= -1;
1180 r
->char_storage
= state
->undo_char_point
;
1181 state
->undo_char_point
+= insert_len
;
1182 return &state
->undo_char
[r
->char_storage
];
1186 static void stb_text_undo(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
)
1188 StbUndoState
*s
= &state
->undostate
;
1189 StbUndoRecord u
, *r
;
1190 if (s
->undo_point
== 0)
1193 // we need to do two things: apply the undo record, and create a redo record
1194 u
= s
->undo_rec
[s
->undo_point
-1];
1195 r
= &s
->undo_rec
[s
->redo_point
-1];
1196 r
->char_storage
= -1;
1198 r
->insert_length
= u
.delete_length
;
1199 r
->delete_length
= u
.insert_length
;
1202 if (u
.delete_length
) {
1203 // if the undo record says to delete characters, then the redo record will
1204 // need to re-insert the characters that get deleted, so we need to store
1207 // there are three cases:
1208 // there's enough room to store the characters
1209 // characters stored for *redoing* don't leave room for redo
1210 // characters stored for *undoing* don't leave room for redo
1211 // if the last is true, we have to bail
1213 if (s
->undo_char_point
+ u
.delete_length
>= STB_TEXTEDIT_UNDOCHARCOUNT
) {
1214 // the undo records take up too much character space; there's no space to store the redo characters
1215 r
->insert_length
= 0;
1219 // there's definitely room to store the characters eventually
1220 while (s
->undo_char_point
+ u
.delete_length
> s
->redo_char_point
) {
1221 // should never happen:
1222 if (s
->redo_point
== STB_TEXTEDIT_UNDOSTATECOUNT
)
1224 // there's currently not enough room, so discard a redo record
1225 stb_textedit_discard_redo(s
);
1227 r
= &s
->undo_rec
[s
->redo_point
-1];
1229 r
->char_storage
= s
->redo_char_point
- u
.delete_length
;
1230 s
->redo_char_point
= s
->redo_char_point
- u
.delete_length
;
1232 // now save the characters
1233 for (i
=0; i
< u
.delete_length
; ++i
)
1234 s
->undo_char
[r
->char_storage
+ i
] = STB_TEXTEDIT_GETCHAR(str
, u
.where
+ i
);
1237 // now we can carry out the deletion
1238 STB_TEXTEDIT_DELETECHARS(str
, u
.where
, u
.delete_length
);
1241 // check type of recorded action:
1242 if (u
.insert_length
) {
1243 // easy case: was a deletion, so we need to insert n characters
1244 STB_TEXTEDIT_INSERTCHARS(str
, u
.where
, &s
->undo_char
[u
.char_storage
], u
.insert_length
);
1245 s
->undo_char_point
-= u
.insert_length
;
1248 state
->cursor
= u
.where
+ u
.insert_length
;
1254 static void stb_text_redo(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
)
1256 StbUndoState
*s
= &state
->undostate
;
1257 StbUndoRecord
*u
, r
;
1258 if (s
->redo_point
== STB_TEXTEDIT_UNDOSTATECOUNT
)
1261 // we need to do two things: apply the redo record, and create an undo record
1262 u
= &s
->undo_rec
[s
->undo_point
];
1263 r
= s
->undo_rec
[s
->redo_point
];
1265 // we KNOW there must be room for the undo record, because the redo record
1266 // was derived from an undo record
1268 u
->delete_length
= r
.insert_length
;
1269 u
->insert_length
= r
.delete_length
;
1271 u
->char_storage
= -1;
1273 if (r
.delete_length
) {
1274 // the redo record requires us to delete characters, so the undo record
1275 // needs to store the characters
1277 if (s
->undo_char_point
+ u
->insert_length
> s
->redo_char_point
) {
1278 u
->insert_length
= 0;
1279 u
->delete_length
= 0;
1282 u
->char_storage
= s
->undo_char_point
;
1283 s
->undo_char_point
= s
->undo_char_point
+ u
->insert_length
;
1285 // now save the characters
1286 for (i
=0; i
< u
->insert_length
; ++i
)
1287 s
->undo_char
[u
->char_storage
+ i
] = STB_TEXTEDIT_GETCHAR(str
, u
->where
+ i
);
1290 STB_TEXTEDIT_DELETECHARS(str
, r
.where
, r
.delete_length
);
1293 if (r
.insert_length
) {
1294 // easy case: need to insert n characters
1295 STB_TEXTEDIT_INSERTCHARS(str
, r
.where
, &s
->undo_char
[r
.char_storage
], r
.insert_length
);
1296 s
->redo_char_point
+= r
.insert_length
;
1299 state
->cursor
= r
.where
+ r
.insert_length
;
1305 static void stb_text_makeundo_insert(STB_TexteditState
*state
, int where
, int length
)
1307 stb_text_createundo(&state
->undostate
, where
, 0, length
);
1310 static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
, int where
, int length
)
1313 STB_TEXTEDIT_CHARTYPE
*p
= stb_text_createundo(&state
->undostate
, where
, length
, 0);
1315 for (i
=0; i
< length
; ++i
)
1316 p
[i
] = STB_TEXTEDIT_GETCHAR(str
, where
+i
);
1320 static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
, int where
, int old_length
, int new_length
)
1323 STB_TEXTEDIT_CHARTYPE
*p
= stb_text_createundo(&state
->undostate
, where
, old_length
, new_length
);
1325 for (i
=0; i
< old_length
; ++i
)
1326 p
[i
] = STB_TEXTEDIT_GETCHAR(str
, where
+i
);
1330 // reset the state to default
1331 static void stb_textedit_clear_state(STB_TexteditState
*state
, int is_single_line
)
1333 state
->undostate
.undo_point
= 0;
1334 state
->undostate
.undo_char_point
= 0;
1335 state
->undostate
.redo_point
= STB_TEXTEDIT_UNDOSTATECOUNT
;
1336 state
->undostate
.redo_char_point
= STB_TEXTEDIT_UNDOCHARCOUNT
;
1337 state
->select_end
= state
->select_start
= 0;
1339 state
->has_preferred_x
= 0;
1340 state
->preferred_x
= 0;
1341 state
->cursor_at_end_of_line
= 0;
1342 state
->initialized
= 1;
1343 state
->single_line
= (unsigned char) is_single_line
;
1344 state
->insert_mode
= 0;
1348 static void stb_textedit_initialize_state(STB_TexteditState
*state
, int is_single_line
)
1350 stb_textedit_clear_state(state
, is_single_line
);
1353 #if defined(__GNUC__) || defined(__clang__)
1354 #pragma GCC diagnostic push
1355 #pragma GCC diagnostic ignored "-Wcast-qual"
1358 static int stb_textedit_paste(STB_TEXTEDIT_STRING
*str
, STB_TexteditState
*state
, STB_TEXTEDIT_CHARTYPE
const *ctext
, int len
)
1360 return stb_textedit_paste_internal(str
, state
, (STB_TEXTEDIT_CHARTYPE
*) ctext
, len
);
1363 #if defined(__GNUC__) || defined(__clang__)
1364 #pragma GCC diagnostic pop
1367 #endif//STB_TEXTEDIT_IMPLEMENTATION
1370 ------------------------------------------------------------------------------
1371 This software is available under 2 licenses -- choose whichever you prefer.
1372 ------------------------------------------------------------------------------
1373 ALTERNATIVE A - MIT License
1374 Copyright (c) 2017 Sean Barrett
1375 Permission is hereby granted, free of charge, to any person obtaining a copy of
1376 this software and associated documentation files (the "Software"), to deal in
1377 the Software without restriction, including without limitation the rights to
1378 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
1379 of the Software, and to permit persons to whom the Software is furnished to do
1380 so, subject to the following conditions:
1381 The above copyright notice and this permission notice shall be included in all
1382 copies or substantial portions of the Software.
1383 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1384 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1385 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1386 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1387 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1388 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1390 ------------------------------------------------------------------------------
1391 ALTERNATIVE B - Public Domain (www.unlicense.org)
1392 This is free and unencumbered software released into the public domain.
1393 Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
1394 software, either in source code form or as a compiled binary, for any purpose,
1395 commercial or non-commercial, and by any means.
1396 In jurisdictions that recognize copyright laws, the author or authors of this
1397 software dedicate any and all copyright interest in the software to the public
1398 domain. We make this dedication for the benefit of the public at large and to
1399 the detriment of our heirs and successors. We intend this dedication to be an
1400 overt act of relinquishment in perpetuity of all present and future rights to
1401 this software under copyright law.
1402 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1403 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1404 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1405 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
1406 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1407 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1408 ------------------------------------------------------------------------------