Debian Codegraph @

extern/imgui_patched/imstb_textedit.h

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