Debian Codegraph @

source/code/Libs/imgui/imstb_textedit.h

raw symbols SCIP
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, int* out_side_on_line)
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 *out_side_on_line = 0;
441
442 // search rows to find one that straddles 'y'
443 while (i < n) {
444 STB_TEXTEDIT_LAYOUTROW(&r, str, i);
445 if (r.num_chars <= 0)
446 return n;
447
448 if (i==0 && y < base_y + r.ymin)
449 return 0;
450
451 if (y < base_y + r.ymax)
452 break;
453
454 i += r.num_chars;
455 base_y += r.baseline_y_delta;
456 }
457
458 // below all text, return 'after' last character
459 if (i >= n)
460 {
461 *out_side_on_line = 1;
462 return n;
463 }
464
465 // check if it's before the beginning of the line
466 if (x < r.x0)
467 return i;
468
469 // check if it's before the end of the line
470 if (x < r.x1) {
471 // search characters in row for one that straddles 'x'
472 prev_x = r.x0;
473 for (k=0; k < r.num_chars; k = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, i + k) - i) {
474 float w = STB_TEXTEDIT_GETWIDTH(str, i, k);
475 if (x < prev_x+w) {
476 *out_side_on_line = (k == 0) ? 0 : 1;
477 if (x < prev_x+w/2)
478 return k+i;
479 else
480 return IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, i + k);
481 }
482 prev_x += w;
483 }
484 // shouldn't happen, but if it does, fall through to end-of-line case
485 }
486
487 // if the last character is a newline, return that. otherwise return 'after' the last character
488 *out_side_on_line = 1;
489 if (STB_TEXTEDIT_GETCHAR(str, i+r.num_chars-1) == STB_TEXTEDIT_NEWLINE)
490 return i+r.num_chars-1;
491 else
492 return i+r.num_chars;
493 }
494
495 // API click: on mouse down, move the cursor to the clicked location, and reset the selection
496 static void stb_textedit_click(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)
497 {
498 // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse
499 // goes off the top or bottom of the text
500 int side_on_line;
501 if( state->single_line )
502 {
503 StbTexteditRow r;
504 STB_TEXTEDIT_LAYOUTROW(&r, str, 0);
505 y = r.ymin;
506 }
507
508 state->cursor = stb_text_locate_coord(str, x, y, &side_on_line);
509 state->select_start = state->cursor;
510 state->select_end = state->cursor;
511 state->has_preferred_x = 0;
512 str->LastMoveDirectionLR = (ImS8)(side_on_line ? ImGuiDir_Right : ImGuiDir_Left);
513 }
514
515 // API drag: on mouse drag, move the cursor and selection endpoint to the clicked location
516 static void stb_textedit_drag(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)
517 {
518 int p = 0;
519 int side_on_line;
520
521 // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse
522 // goes off the top or bottom of the text
523 if( state->single_line )
524 {
525 StbTexteditRow r;
526 STB_TEXTEDIT_LAYOUTROW(&r, str, 0);
527 y = r.ymin;
528 }
529
530 if (state->select_start == state->select_end)
531 state->select_start = state->cursor;
532
533 p = stb_text_locate_coord(str, x, y, &side_on_line);
534 state->cursor = state->select_end = p;
535 str->LastMoveDirectionLR = (ImS8)(side_on_line ? ImGuiDir_Right : ImGuiDir_Left);
536 }
537
538 /////////////////////////////////////////////////////////////////////////////
539 //
540 // Keyboard input handling
541 //
542
543 // forward declarations
544 static void stb_text_undo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state);
545 static void stb_text_redo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state);
546 static void stb_text_makeundo_delete(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length);
547 static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length);
548 static void stb_text_makeundo_replace(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length);
549
550 typedef struct
551 {
552 float x,y; // position of n'th character
553 float height; // height of line
554 int first_char, length; // first char of row, and length
555 int prev_first; // first char of previous row
556 } StbFindState;
557
558 // find the x/y location of a character, and remember info about the previous row in
559 // case we get a move-up event (for page up, we'll have to rescan)
560 static void stb_textedit_find_charpos(StbFindState *find, IMSTB_TEXTEDIT_STRING *str, int n, int single_line)
561 {
562 StbTexteditRow r;
563 int prev_start = 0;
564 int z = STB_TEXTEDIT_STRINGLEN(str);
565 int i=0, first;
566
567 if (n == z && single_line) {
568 // special case if it's at the end (may not be needed?)
569 STB_TEXTEDIT_LAYOUTROW(&r, str, 0);
570 find->y = 0;
571 find->first_char = 0;
572 find->length = z;
573 find->height = r.ymax - r.ymin;
574 find->x = r.x1;
575 return;
576 }
577
578 // search rows to find the one that straddles character n
579 find->y = 0;
580
581 for(;;) {
582 STB_TEXTEDIT_LAYOUTROW(&r, str, i);
583 if (n < i + r.num_chars)
584 break;
585 if (str->LastMoveDirectionLR == ImGuiDir_Right && str->Stb->cursor > 0 && str->Stb->cursor == i + r.num_chars && STB_TEXTEDIT_GETCHAR(str, i + r.num_chars - 1) != STB_TEXTEDIT_NEWLINE) // [DEAR IMGUI] Wrapping point handling
586 break;
587 if (i + r.num_chars == z && z > 0 && STB_TEXTEDIT_GETCHAR(str, z - 1) != STB_TEXTEDIT_NEWLINE) // [DEAR IMGUI] special handling for last line
588 break; // [DEAR IMGUI]
589 prev_start = i;
590 i += r.num_chars;
591 find->y += r.baseline_y_delta;
592 if (i == z) // [DEAR IMGUI]
593 {
594 r.num_chars = 0; // [DEAR IMGUI]
595 break; // [DEAR IMGUI]
596 }
597 }
598
599 find->first_char = first = i;
600 find->length = r.num_chars;
601 find->height = r.ymax - r.ymin;
602 find->prev_first = prev_start;
603
604 // now scan to find xpos
605 find->x = r.x0;
606 for (i=0; first+i < n; i = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, first + i) - first)
607 find->x += STB_TEXTEDIT_GETWIDTH(str, first, i);
608 }
609
610 #define STB_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end)
611
612 // make the selection/cursor state valid if client altered the string
613 static void stb_textedit_clamp(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state)
614 {
615 int n = STB_TEXTEDIT_STRINGLEN(str);
616 if (STB_TEXT_HAS_SELECTION(state)) {
617 if (state->select_start > n) state->select_start = n;
618 if (state->select_end > n) state->select_end = n;
619 // if clamping forced them to be equal, move the cursor to match
620 if (state->select_start == state->select_end)
621 state->cursor = state->select_start;
622 }
623 if (state->cursor > n) state->cursor = n;
624 }
625
626 // delete characters while updating undo
627 static void stb_textedit_delete(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int len)
628 {
629 stb_text_makeundo_delete(str, state, where, len);
630 STB_TEXTEDIT_DELETECHARS(str, where, len);
631 state->has_preferred_x = 0;
632 }
633
634 // delete the section
635 static void stb_textedit_delete_selection(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state)
636 {
637 stb_textedit_clamp(str, state);
638 if (STB_TEXT_HAS_SELECTION(state)) {
639 if (state->select_start < state->select_end) {
640 stb_textedit_delete(str, state, state->select_start, state->select_end - state->select_start);
641 state->select_end = state->cursor = state->select_start;
642 } else {
643 stb_textedit_delete(str, state, state->select_end, state->select_start - state->select_end);
644 state->select_start = state->cursor = state->select_end;
645 }
646 state->has_preferred_x = 0;
647 }
648 }
649
650 // canoncialize the selection so start <= end
651 static void stb_textedit_sortselection(STB_TexteditState *state)
652 {
653 if (state->select_end < state->select_start) {
654 int temp = state->select_end;
655 state->select_end = state->select_start;
656 state->select_start = temp;
657 }
658 }
659
660 // move cursor to first character of selection
661 static void stb_textedit_move_to_first(STB_TexteditState *state)
662 {
663 if (STB_TEXT_HAS_SELECTION(state)) {
664 stb_textedit_sortselection(state);
665 state->cursor = state->select_start;
666 state->select_end = state->select_start;
667 state->has_preferred_x = 0;
668 }
669 }
670
671 // move cursor to last character of selection
672 static void stb_textedit_move_to_last(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state)
673 {
674 if (STB_TEXT_HAS_SELECTION(state)) {
675 stb_textedit_sortselection(state);
676 stb_textedit_clamp(str, state);
677 state->cursor = state->select_end;
678 state->select_start = state->select_end;
679 state->has_preferred_x = 0;
680 }
681 }
682
683 // [DEAR IMGUI] Extracted this function so we can more easily add support for word-wrapping.
684 #ifndef STB_TEXTEDIT_MOVELINESTART
685 static int stb_textedit_move_line_start(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int cursor)
686 {
687 if (state->single_line)
688 return 0;
689 while (cursor > 0) {
690 int prev = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, cursor);
691 if (STB_TEXTEDIT_GETCHAR(str, prev) == STB_TEXTEDIT_NEWLINE)
692 break;
693 cursor = prev;
694 }
695 return cursor;
696 }
697 #define STB_TEXTEDIT_MOVELINESTART stb_textedit_move_line_start
698 #endif
699 #ifndef STB_TEXTEDIT_MOVELINEEND
700 static int stb_textedit_move_line_end(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int cursor)
701 {
702 int n = STB_TEXTEDIT_STRINGLEN(str);
703 if (state->single_line)
704 return n;
705 while (cursor < n && STB_TEXTEDIT_GETCHAR(str, cursor) != STB_TEXTEDIT_NEWLINE)
706 cursor = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, cursor);
707 return cursor;
708 }
709 #define STB_TEXTEDIT_MOVELINEEND stb_textedit_move_line_end
710 #endif
711
712 #ifdef STB_TEXTEDIT_IS_SPACE
713 static int is_word_boundary( IMSTB_TEXTEDIT_STRING *str, int idx )
714 {
715 return idx > 0 ? (STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str,idx-1) ) && !STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str, idx) ) ) : 1;
716 }
717
718 #ifndef STB_TEXTEDIT_MOVEWORDLEFT
719 static int stb_textedit_move_to_word_previous( IMSTB_TEXTEDIT_STRING *str, int c )
720 {
721 c = IMSTB_TEXTEDIT_GETPREVCHARINDEX( str, c ); // always move at least one character
722 while (c >= 0 && !is_word_boundary(str, c))
723 c = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, c);
724
725 if( c < 0 )
726 c = 0;
727
728 return c;
729 }
730 #define STB_TEXTEDIT_MOVEWORDLEFT stb_textedit_move_to_word_previous
731 #endif
732
733 #ifndef STB_TEXTEDIT_MOVEWORDRIGHT
734 static int stb_textedit_move_to_word_next( IMSTB_TEXTEDIT_STRING *str, int c )
735 {
736 const int len = STB_TEXTEDIT_STRINGLEN(str);
737 c = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, c); // always move at least one character
738 while( c < len && !is_word_boundary( str, c ) )
739 c = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, c);
740
741 if( c > len )
742 c = len;
743
744 return c;
745 }
746 #define STB_TEXTEDIT_MOVEWORDRIGHT stb_textedit_move_to_word_next
747 #endif
748
749 #endif
750
751 // update selection and cursor to match each other
752 static void stb_textedit_prep_selection_at_cursor(STB_TexteditState *state)
753 {
754 if (!STB_TEXT_HAS_SELECTION(state))
755 state->select_start = state->select_end = state->cursor;
756 else
757 state->cursor = state->select_end;
758 }
759
760 // API cut: delete selection
761 static int stb_textedit_cut(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state)
762 {
763 if (STB_TEXT_HAS_SELECTION(state)) {
764 stb_textedit_delete_selection(str,state); // implicitly clamps
765 state->has_preferred_x = 0;
766 return 1;
767 }
768 return 0;
769 }
770
771 // API paste: replace existing selection with passed-in text
772 static int stb_textedit_paste_internal(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, IMSTB_TEXTEDIT_CHARTYPE *text, int len)
773 {
774 // if there's a selection, the paste should delete it
775 stb_textedit_clamp(str, state);
776 stb_textedit_delete_selection(str,state);
777 // try to insert the characters
778 if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, len)) {
779 stb_text_makeundo_insert(state, state->cursor, len);
780 state->cursor += len;
781 state->has_preferred_x = 0;
782 return 1;
783 }
784 // note: paste failure will leave deleted selection, may be restored with an undo (see https://github.com/nothings/stb/issues/734 for details)
785 return 0;
786 }
787
788 #ifndef STB_TEXTEDIT_KEYTYPE
789 #define STB_TEXTEDIT_KEYTYPE int
790 #endif
791
792 // API key: process text input
793 // [DEAR IMGUI] Added stb_textedit_text(), extracted out and called by stb_textedit_key() for backward compatibility.
794 static void stb_textedit_text(IMSTB_TEXTEDIT_STRING* str, STB_TexteditState* state, const IMSTB_TEXTEDIT_CHARTYPE* text, int text_len)
795 {
796 // can't add newline in single-line mode
797 if (text[0] == '\n' && state->single_line)
798 return;
799
800 if (state->insert_mode && !STB_TEXT_HAS_SELECTION(state) && state->cursor < STB_TEXTEDIT_STRINGLEN(str)) {
801 stb_text_makeundo_replace(str, state, state->cursor, 1, 1);
802 STB_TEXTEDIT_DELETECHARS(str, state->cursor, 1);
803 if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, text_len)) {
804 state->cursor += text_len;
805 state->has_preferred_x = 0;
806 }
807 } else {
808 stb_textedit_delete_selection(str, state); // implicitly clamps
809 if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, text_len)) {
810 stb_text_makeundo_insert(state, state->cursor, text_len);
811 state->cursor += text_len;
812 state->has_preferred_x = 0;
813 }
814 }
815 }
816
817 // API key: process a keyboard input
818 static void stb_textedit_key(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_KEYTYPE key)
819 {
820 retry:
821 switch (key) {
822 default: {
823 #ifdef STB_TEXTEDIT_KEYTOTEXT
824 // This is not suitable for UTF-8 support.
825 int c = STB_TEXTEDIT_KEYTOTEXT(key);
826 if (c > 0) {
827 IMSTB_TEXTEDIT_CHARTYPE ch = (IMSTB_TEXTEDIT_CHARTYPE)c;
828 stb_textedit_text(str, state, &ch, 1);
829 }
830 #endif
831 break;
832 }
833
834 #ifdef STB_TEXTEDIT_K_INSERT
835 case STB_TEXTEDIT_K_INSERT:
836 state->insert_mode = !state->insert_mode;
837 break;
838 #endif
839
840 case STB_TEXTEDIT_K_UNDO:
841 stb_text_undo(str, state);
842 state->has_preferred_x = 0;
843 break;
844
845 case STB_TEXTEDIT_K_REDO:
846 stb_text_redo(str, state);
847 state->has_preferred_x = 0;
848 break;
849
850 case STB_TEXTEDIT_K_LEFT:
851 // if currently there's a selection, move cursor to start of selection
852 if (STB_TEXT_HAS_SELECTION(state))
853 stb_textedit_move_to_first(state);
854 else
855 if (state->cursor > 0)
856 state->cursor = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, state->cursor);
857 state->has_preferred_x = 0;
858 break;
859
860 case STB_TEXTEDIT_K_RIGHT:
861 // if currently there's a selection, move cursor to end of selection
862 if (STB_TEXT_HAS_SELECTION(state))
863 stb_textedit_move_to_last(str, state);
864 else
865 state->cursor = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor);
866 stb_textedit_clamp(str, state);
867 state->has_preferred_x = 0;
868 break;
869
870 case STB_TEXTEDIT_K_LEFT | STB_TEXTEDIT_K_SHIFT:
871 stb_textedit_clamp(str, state);
872 stb_textedit_prep_selection_at_cursor(state);
873 // move selection left
874 if (state->select_end > 0)
875 state->select_end = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, state->select_end);
876 state->cursor = state->select_end;
877 state->has_preferred_x = 0;
878 break;
879
880 #ifdef STB_TEXTEDIT_MOVEWORDLEFT
881 case STB_TEXTEDIT_K_WORDLEFT:
882 if (STB_TEXT_HAS_SELECTION(state))
883 stb_textedit_move_to_first(state);
884 else {
885 state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor);
886 stb_textedit_clamp( str, state );
887 }
888 break;
889
890 case STB_TEXTEDIT_K_WORDLEFT | STB_TEXTEDIT_K_SHIFT:
891 if( !STB_TEXT_HAS_SELECTION( state ) )
892 stb_textedit_prep_selection_at_cursor(state);
893
894 state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor);
895 state->select_end = state->cursor;
896
897 stb_textedit_clamp( str, state );
898 break;
899 #endif
900
901 #ifdef STB_TEXTEDIT_MOVEWORDRIGHT
902 case STB_TEXTEDIT_K_WORDRIGHT:
903 if (STB_TEXT_HAS_SELECTION(state))
904 stb_textedit_move_to_last(str, state);
905 else {
906 state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor);
907 stb_textedit_clamp( str, state );
908 }
909 break;
910
911 case STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT:
912 if( !STB_TEXT_HAS_SELECTION( state ) )
913 stb_textedit_prep_selection_at_cursor(state);
914
915 state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor);
916 state->select_end = state->cursor;
917
918 stb_textedit_clamp( str, state );
919 break;
920 #endif
921
922 case STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT:
923 stb_textedit_prep_selection_at_cursor(state);
924 // move selection right
925 state->select_end = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->select_end);
926 stb_textedit_clamp(str, state);
927 state->cursor = state->select_end;
928 state->has_preferred_x = 0;
929 break;
930
931 case STB_TEXTEDIT_K_DOWN:
932 case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT:
933 case STB_TEXTEDIT_K_PGDOWN:
934 case STB_TEXTEDIT_K_PGDOWN | STB_TEXTEDIT_K_SHIFT: {
935 StbFindState find;
936 StbTexteditRow row;
937 int i, j, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;
938 int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGDOWN;
939 int row_count = is_page ? state->row_count_per_page : 1;
940
941 if (!is_page && state->single_line) {
942 // on windows, up&down in single-line behave like left&right
943 key = STB_TEXTEDIT_K_RIGHT | (key & STB_TEXTEDIT_K_SHIFT);
944 goto retry;
945 }
946
947 if (sel)
948 stb_textedit_prep_selection_at_cursor(state);
949 else if (STB_TEXT_HAS_SELECTION(state))
950 stb_textedit_move_to_last(str, state);
951
952 // compute current position of cursor point
953 stb_textedit_clamp(str, state);
954 stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);
955
956 for (j = 0; j < row_count; ++j) {
957 float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x;
958 int start = find.first_char + find.length;
959
960 if (find.length == 0)
961 break;
962
963 // [DEAR IMGUI]
964 // going down while being on the last line shouldn't bring us to that line end
965 //if (STB_TEXTEDIT_GETCHAR(str, find.first_char + find.length - 1) != STB_TEXTEDIT_NEWLINE)
966 // break;
967
968 // now find character position down a row
969 state->cursor = start;
970 STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);
971 x = row.x0;
972 for (i=0; i < row.num_chars; ) {
973 float dx = STB_TEXTEDIT_GETWIDTH(str, start, i);
974 int next = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor);
975 #ifdef IMSTB_TEXTEDIT_GETWIDTH_NEWLINE
976 if (dx == IMSTB_TEXTEDIT_GETWIDTH_NEWLINE)
977 break;
978 #endif
979 x += dx;
980 if (x > goal_x)
981 break;
982 i += next - state->cursor;
983 state->cursor = next;
984 }
985 stb_textedit_clamp(str, state);
986
987 if (state->cursor == find.first_char + find.length)
988 str->LastMoveDirectionLR = ImGuiDir_Left;
989 state->has_preferred_x = 1;
990 state->preferred_x = goal_x;
991
992 if (sel)
993 state->select_end = state->cursor;
994
995 // go to next line
996 find.first_char = find.first_char + find.length;
997 find.length = row.num_chars;
998 }
999 break;
1000 }
1001
1002 case STB_TEXTEDIT_K_UP:
1003 case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT:
1004 case STB_TEXTEDIT_K_PGUP:
1005 case STB_TEXTEDIT_K_PGUP | STB_TEXTEDIT_K_SHIFT: {
1006 StbFindState find;
1007 StbTexteditRow row;
1008 int i, j, prev_scan, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;
1009 int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGUP;
1010 int row_count = is_page ? state->row_count_per_page : 1;
1011
1012 if (!is_page && state->single_line) {
1013 // on windows, up&down become left&right
1014 key = STB_TEXTEDIT_K_LEFT | (key & STB_TEXTEDIT_K_SHIFT);
1015 goto retry;
1016 }
1017
1018 if (sel)
1019 stb_textedit_prep_selection_at_cursor(state);
1020 else if (STB_TEXT_HAS_SELECTION(state))
1021 stb_textedit_move_to_first(state);
1022
1023 // compute current position of cursor point
1024 stb_textedit_clamp(str, state);
1025 stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);
1026
1027 for (j = 0; j < row_count; ++j) {
1028 float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x;
1029
1030 // can only go up if there's a previous row
1031 if (find.prev_first == find.first_char)
1032 break;
1033
1034 // now find character position up a row
1035 state->cursor = find.prev_first;
1036 STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);
1037 x = row.x0;
1038 for (i=0; i < row.num_chars; ) {
1039 float dx = STB_TEXTEDIT_GETWIDTH(str, find.prev_first, i);
1040 int next = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor);
1041 #ifdef IMSTB_TEXTEDIT_GETWIDTH_NEWLINE
1042 if (dx == IMSTB_TEXTEDIT_GETWIDTH_NEWLINE)
1043 break;
1044 #endif
1045 x += dx;
1046 if (x > goal_x)
1047 break;
1048 i += next - state->cursor;
1049 state->cursor = next;
1050 }
1051 stb_textedit_clamp(str, state);
1052
1053 if (state->cursor == find.first_char)
1054 str->LastMoveDirectionLR = ImGuiDir_Right;
1055 else if (state->cursor == find.prev_first)
1056 str->LastMoveDirectionLR = ImGuiDir_Left;
1057 state->has_preferred_x = 1;
1058 state->preferred_x = goal_x;
1059
1060 if (sel)
1061 state->select_end = state->cursor;
1062
1063 // go to previous line
1064 // (we need to scan previous line the hard way. maybe we could expose this as a new API function?)
1065 prev_scan = find.prev_first > 0 ? find.prev_first - 1 : 0;
1066 while (prev_scan > 0)
1067 {
1068 int prev = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, prev_scan);
1069 if (STB_TEXTEDIT_GETCHAR(str, prev) == STB_TEXTEDIT_NEWLINE)
1070 break;
1071 prev_scan = prev;
1072 }
1073 find.first_char = find.prev_first;
1074 find.prev_first = STB_TEXTEDIT_MOVELINESTART(str, state, prev_scan);
1075 }
1076 break;
1077 }
1078
1079 case STB_TEXTEDIT_K_DELETE:
1080 case STB_TEXTEDIT_K_DELETE | STB_TEXTEDIT_K_SHIFT:
1081 if (STB_TEXT_HAS_SELECTION(state))
1082 stb_textedit_delete_selection(str, state);
1083 else {
1084 int n = STB_TEXTEDIT_STRINGLEN(str);
1085 if (state->cursor < n)
1086 stb_textedit_delete(str, state, state->cursor, IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor) - state->cursor);
1087 }
1088 state->has_preferred_x = 0;
1089 break;
1090
1091 case STB_TEXTEDIT_K_BACKSPACE:
1092 case STB_TEXTEDIT_K_BACKSPACE | STB_TEXTEDIT_K_SHIFT:
1093 if (STB_TEXT_HAS_SELECTION(state))
1094 stb_textedit_delete_selection(str, state);
1095 else {
1096 stb_textedit_clamp(str, state);
1097 if (state->cursor > 0) {
1098 int prev = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, state->cursor);
1099 stb_textedit_delete(str, state, prev, state->cursor - prev);
1100 state->cursor = prev;
1101 }
1102 }
1103 state->has_preferred_x = 0;
1104 break;
1105
1106 #ifdef STB_TEXTEDIT_K_TEXTSTART2
1107 case STB_TEXTEDIT_K_TEXTSTART2:
1108 #endif
1109 case STB_TEXTEDIT_K_TEXTSTART:
1110 state->cursor = state->select_start = state->select_end = 0;
1111 state->has_preferred_x = 0;
1112 break;
1113
1114 #ifdef STB_TEXTEDIT_K_TEXTEND2
1115 case STB_TEXTEDIT_K_TEXTEND2:
1116 #endif
1117 case STB_TEXTEDIT_K_TEXTEND:
1118 state->cursor = STB_TEXTEDIT_STRINGLEN(str);
1119 state->select_start = state->select_end = 0;
1120 state->has_preferred_x = 0;
1121 break;
1122
1123 #ifdef STB_TEXTEDIT_K_TEXTSTART2
1124 case STB_TEXTEDIT_K_TEXTSTART2 | STB_TEXTEDIT_K_SHIFT:
1125 #endif
1126 case STB_TEXTEDIT_K_TEXTSTART | STB_TEXTEDIT_K_SHIFT:
1127 stb_textedit_prep_selection_at_cursor(state);
1128 state->cursor = state->select_end = 0;
1129 state->has_preferred_x = 0;
1130 break;
1131
1132 #ifdef STB_TEXTEDIT_K_TEXTEND2
1133 case STB_TEXTEDIT_K_TEXTEND2 | STB_TEXTEDIT_K_SHIFT:
1134 #endif
1135 case STB_TEXTEDIT_K_TEXTEND | STB_TEXTEDIT_K_SHIFT:
1136 stb_textedit_prep_selection_at_cursor(state);
1137 state->cursor = state->select_end = STB_TEXTEDIT_STRINGLEN(str);
1138 state->has_preferred_x = 0;
1139 break;
1140
1141
1142 #ifdef STB_TEXTEDIT_K_LINESTART2
1143 case STB_TEXTEDIT_K_LINESTART2:
1144 #endif
1145 case STB_TEXTEDIT_K_LINESTART:
1146 stb_textedit_clamp(str, state);
1147 stb_textedit_move_to_first(state);
1148 state->cursor = STB_TEXTEDIT_MOVELINESTART(str, state, state->cursor);
1149 state->has_preferred_x = 0;
1150 break;
1151
1152 #ifdef STB_TEXTEDIT_K_LINEEND2
1153 case STB_TEXTEDIT_K_LINEEND2:
1154 #endif
1155 case STB_TEXTEDIT_K_LINEEND: {
1156 stb_textedit_clamp(str, state);
1157 stb_textedit_move_to_last(str, state);
1158 state->cursor = STB_TEXTEDIT_MOVELINEEND(str, state, state->cursor);
1159 state->has_preferred_x = 0;
1160 break;
1161 }
1162
1163 #ifdef STB_TEXTEDIT_K_LINESTART2
1164 case STB_TEXTEDIT_K_LINESTART2 | STB_TEXTEDIT_K_SHIFT:
1165 #endif
1166 case STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT:
1167 stb_textedit_clamp(str, state);
1168 stb_textedit_prep_selection_at_cursor(state);
1169 state->cursor = STB_TEXTEDIT_MOVELINESTART(str, state, state->cursor);
1170 state->select_end = state->cursor;
1171 state->has_preferred_x = 0;
1172 break;
1173
1174 #ifdef STB_TEXTEDIT_K_LINEEND2
1175 case STB_TEXTEDIT_K_LINEEND2 | STB_TEXTEDIT_K_SHIFT:
1176 #endif
1177 case STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT: {
1178 stb_textedit_clamp(str, state);
1179 stb_textedit_prep_selection_at_cursor(state);
1180 state->cursor = STB_TEXTEDIT_MOVELINEEND(str, state, state->cursor);
1181 state->select_end = state->cursor;
1182 state->has_preferred_x = 0;
1183 break;
1184 }
1185 }
1186 }
1187
1188 /////////////////////////////////////////////////////////////////////////////
1189 //
1190 // Undo processing
1191 //
1192 // @OPTIMIZE: the undo/redo buffer should be circular
1193
1194 static void stb_textedit_flush_redo(StbUndoState *state)
1195 {
1196 state->redo_point = IMSTB_TEXTEDIT_UNDOSTATECOUNT;
1197 state->redo_char_point = IMSTB_TEXTEDIT_UNDOCHARCOUNT;
1198 }
1199
1200 // discard the oldest entry in the undo list
1201 static void stb_textedit_discard_undo(StbUndoState *state)
1202 {
1203 if (state->undo_point > 0) {
1204 // if the 0th undo state has characters, clean those up
1205 if (state->undo_rec[0].char_storage >= 0) {
1206 int n = state->undo_rec[0].insert_length, i;
1207 // delete n characters from all other records
1208 state->undo_char_point -= n;
1209 IMSTB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(IMSTB_TEXTEDIT_CHARTYPE)));
1210 for (i=0; i < state->undo_point; ++i)
1211 if (state->undo_rec[i].char_storage >= 0)
1212 state->undo_rec[i].char_storage -= n; // @OPTIMIZE: get rid of char_storage and infer it
1213 }
1214 --state->undo_point;
1215 IMSTB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0])));
1216 }
1217 }
1218
1219 // discard the oldest entry in the redo list--it's bad if this
1220 // ever happens, but because undo & redo have to store the actual
1221 // characters in different cases, the redo character buffer can
1222 // fill up even though the undo buffer didn't
1223 static void stb_textedit_discard_redo(StbUndoState *state)
1224 {
1225 int k = IMSTB_TEXTEDIT_UNDOSTATECOUNT-1;
1226
1227 if (state->redo_point <= k) {
1228 // if the k'th undo state has characters, clean those up
1229 if (state->undo_rec[k].char_storage >= 0) {
1230 int n = state->undo_rec[k].insert_length, i;
1231 // move the remaining redo character data to the end of the buffer
1232 state->redo_char_point += n;
1233 IMSTB_TEXTEDIT_memmove(state->undo_char + state->redo_char_point, state->undo_char + state->redo_char_point-n, (size_t) ((IMSTB_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point)*sizeof(IMSTB_TEXTEDIT_CHARTYPE)));
1234 // adjust the position of all the other records to account for above memmove
1235 for (i=state->redo_point; i < k; ++i)
1236 if (state->undo_rec[i].char_storage >= 0)
1237 state->undo_rec[i].char_storage += n;
1238 }
1239 // now move all the redo records towards the end of the buffer; the first one is at 'redo_point'
1240 // [DEAR IMGUI]
1241 size_t move_size = (size_t)((IMSTB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point - 1) * sizeof(state->undo_rec[0]));
1242 const char* buf_begin = (char*)state->undo_rec; (void)buf_begin;
1243 const char* buf_end = (char*)state->undo_rec + sizeof(state->undo_rec); (void)buf_end;
1244 IM_ASSERT(((char*)(state->undo_rec + state->redo_point)) >= buf_begin);
1245 IM_ASSERT(((char*)(state->undo_rec + state->redo_point + 1) + move_size) <= buf_end);
1246 IMSTB_TEXTEDIT_memmove(state->undo_rec + state->redo_point+1, state->undo_rec + state->redo_point, move_size);
1247
1248 // now move redo_point to point to the new one
1249 ++state->redo_point;
1250 }
1251 }
1252
1253 static StbUndoRecord *stb_text_create_undo_record(StbUndoState *state, int numchars)
1254 {
1255 // any time we create a new undo record, we discard redo
1256 stb_textedit_flush_redo(state);
1257
1258 // if we have no free records, we have to make room, by sliding the
1259 // existing records down
1260 if (state->undo_point == IMSTB_TEXTEDIT_UNDOSTATECOUNT)
1261 stb_textedit_discard_undo(state);
1262
1263 // if the characters to store won't possibly fit in the buffer, we can't undo
1264 if (numchars > IMSTB_TEXTEDIT_UNDOCHARCOUNT) {
1265 state->undo_point = 0;
1266 state->undo_char_point = 0;
1267 return NULL;
1268 }
1269
1270 // if we don't have enough free characters in the buffer, we have to make room
1271 while (state->undo_char_point + numchars > IMSTB_TEXTEDIT_UNDOCHARCOUNT)
1272 stb_textedit_discard_undo(state);
1273
1274 return &state->undo_rec[state->undo_point++];
1275 }
1276
1277 static IMSTB_TEXTEDIT_CHARTYPE *stb_text_createundo(StbUndoState *state, int pos, int insert_len, int delete_len)
1278 {
1279 StbUndoRecord *r = stb_text_create_undo_record(state, insert_len);
1280 if (r == NULL)
1281 return NULL;
1282
1283 r->where = pos;
1284 r->insert_length = (IMSTB_TEXTEDIT_POSITIONTYPE) insert_len;
1285 r->delete_length = (IMSTB_TEXTEDIT_POSITIONTYPE) delete_len;
1286
1287 if (insert_len == 0) {
1288 r->char_storage = -1;
1289 return NULL;
1290 } else {
1291 r->char_storage = state->undo_char_point;
1292 state->undo_char_point += insert_len;
1293 return &state->undo_char[r->char_storage];
1294 }
1295 }
1296
1297 static void stb_text_undo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state)
1298 {
1299 StbUndoState *s = &state->undostate;
1300 StbUndoRecord u, *r;
1301 if (s->undo_point == 0)
1302 return;
1303
1304 // we need to do two things: apply the undo record, and create a redo record
1305 u = s->undo_rec[s->undo_point-1];
1306 r = &s->undo_rec[s->redo_point-1];
1307 r->char_storage = -1;
1308
1309 r->insert_length = u.delete_length;
1310 r->delete_length = u.insert_length;
1311 r->where = u.where;
1312
1313 if (u.delete_length) {
1314 // if the undo record says to delete characters, then the redo record will
1315 // need to re-insert the characters that get deleted, so we need to store
1316 // them.
1317
1318 // there are three cases:
1319 // there's enough room to store the characters
1320 // characters stored for *redoing* don't leave room for redo
1321 // characters stored for *undoing* don't leave room for redo
1322 // if the last is true, we have to bail
1323
1324 if (s->undo_char_point + u.delete_length >= IMSTB_TEXTEDIT_UNDOCHARCOUNT) {
1325 // the undo records take up too much character space; there's no space to store the redo characters
1326 r->insert_length = 0;
1327 } else {
1328 int i;
1329
1330 // there's definitely room to store the characters eventually
1331 while (s->undo_char_point + u.delete_length > s->redo_char_point) {
1332 // should never happen:
1333 if (s->redo_point == IMSTB_TEXTEDIT_UNDOSTATECOUNT)
1334 return;
1335 // there's currently not enough room, so discard a redo record
1336 stb_textedit_discard_redo(s);
1337 }
1338 r = &s->undo_rec[s->redo_point-1];
1339
1340 r->char_storage = s->redo_char_point - u.delete_length;
1341 s->redo_char_point = s->redo_char_point - u.delete_length;
1342
1343 // now save the characters
1344 for (i=0; i < u.delete_length; ++i)
1345 s->undo_char[r->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u.where + i);
1346 }
1347
1348 // now we can carry out the deletion
1349 STB_TEXTEDIT_DELETECHARS(str, u.where, u.delete_length);
1350 }
1351
1352 // check type of recorded action:
1353 if (u.insert_length) {
1354 // easy case: was a deletion, so we need to insert n characters
1355 STB_TEXTEDIT_INSERTCHARS(str, u.where, &s->undo_char[u.char_storage], u.insert_length);
1356 s->undo_char_point -= u.insert_length;
1357 }
1358
1359 state->cursor = u.where + u.insert_length;
1360
1361 s->undo_point--;
1362 s->redo_point--;
1363 }
1364
1365 static void stb_text_redo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state)
1366 {
1367 StbUndoState *s = &state->undostate;
1368 StbUndoRecord *u, r;
1369 if (s->redo_point == IMSTB_TEXTEDIT_UNDOSTATECOUNT)
1370 return;
1371
1372 // we need to do two things: apply the redo record, and create an undo record
1373 u = &s->undo_rec[s->undo_point];
1374 r = s->undo_rec[s->redo_point];
1375
1376 // we KNOW there must be room for the undo record, because the redo record
1377 // was derived from an undo record
1378
1379 u->delete_length = r.insert_length;
1380 u->insert_length = r.delete_length;
1381 u->where = r.where;
1382 u->char_storage = -1;
1383
1384 if (r.delete_length) {
1385 // the redo record requires us to delete characters, so the undo record
1386 // needs to store the characters
1387
1388 if (s->undo_char_point + u->insert_length > s->redo_char_point) {
1389 u->insert_length = 0;
1390 u->delete_length = 0;
1391 } else {
1392 int i;
1393 u->char_storage = s->undo_char_point;
1394 s->undo_char_point = s->undo_char_point + u->insert_length;
1395
1396 // now save the characters
1397 for (i=0; i < u->insert_length; ++i)
1398 s->undo_char[u->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u->where + i);
1399 }
1400
1401 STB_TEXTEDIT_DELETECHARS(str, r.where, r.delete_length);
1402 }
1403
1404 if (r.insert_length) {
1405 // easy case: need to insert n characters
1406 STB_TEXTEDIT_INSERTCHARS(str, r.where, &s->undo_char[r.char_storage], r.insert_length);
1407 s->redo_char_point += r.insert_length;
1408 }
1409
1410 state->cursor = r.where + r.insert_length;
1411
1412 s->undo_point++;
1413 s->redo_point++;
1414 }
1415
1416 static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length)
1417 {
1418 stb_text_createundo(&state->undostate, where, 0, length);
1419 }
1420
1421 static void stb_text_makeundo_delete(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length)
1422 {
1423 int i;
1424 IMSTB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, length, 0);
1425 if (p) {
1426 for (i=0; i < length; ++i)
1427 p[i] = STB_TEXTEDIT_GETCHAR(str, where+i);
1428 }
1429 }
1430
1431 static void stb_text_makeundo_replace(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length)
1432 {
1433 int i;
1434 IMSTB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, old_length, new_length);
1435 if (p) {
1436 for (i=0; i < old_length; ++i)
1437 p[i] = STB_TEXTEDIT_GETCHAR(str, where+i);
1438 }
1439 }
1440
1441 // reset the state to default
1442 static void stb_textedit_clear_state(STB_TexteditState *state, int is_single_line)
1443 {
1444 state->undostate.undo_point = 0;
1445 state->undostate.undo_char_point = 0;
1446 state->undostate.redo_point = IMSTB_TEXTEDIT_UNDOSTATECOUNT;
1447 state->undostate.redo_char_point = IMSTB_TEXTEDIT_UNDOCHARCOUNT;
1448 state->select_end = state->select_start = 0;
1449 state->cursor = 0;
1450 state->has_preferred_x = 0;
1451 state->preferred_x = 0;
1452 state->cursor_at_end_of_line = 0;
1453 state->initialized = 1;
1454 state->single_line = (unsigned char) is_single_line;
1455 state->insert_mode = 0;
1456 state->row_count_per_page = 0;
1457 }
1458
1459 // API initialize
1460 static void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line)
1461 {
1462 stb_textedit_clear_state(state, is_single_line);
1463 }
1464
1465 #if defined(__GNUC__) || defined(__clang__)
1466 #pragma GCC diagnostic push
1467 #pragma GCC diagnostic ignored "-Wcast-qual"
1468 #endif
1469
1470 static int stb_textedit_paste(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, IMSTB_TEXTEDIT_CHARTYPE const *ctext, int len)
1471 {
1472 return stb_textedit_paste_internal(str, state, (IMSTB_TEXTEDIT_CHARTYPE *) ctext, len);
1473 }
1474
1475 #if defined(__GNUC__) || defined(__clang__)
1476 #pragma GCC diagnostic pop
1477 #endif
1478
1479 #endif//IMSTB_TEXTEDIT_IMPLEMENTATION
1480
1481 /*
1482 ------------------------------------------------------------------------------
1483 This software is available under 2 licenses -- choose whichever you prefer.
1484 ------------------------------------------------------------------------------
1485 ALTERNATIVE A - MIT License
1486 Copyright (c) 2017 Sean Barrett
1487 Permission is hereby granted, free of charge, to any person obtaining a copy of
1488 this software and associated documentation files (the "Software"), to deal in
1489 the Software without restriction, including without limitation the rights to
1490 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
1491 of the Software, and to permit persons to whom the Software is furnished to do
1492 so, subject to the following conditions:
1493 The above copyright notice and this permission notice shall be included in all
1494 copies or substantial portions of the Software.
1495 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1496 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1497 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1498 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1499 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1500 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1501 SOFTWARE.
1502 ------------------------------------------------------------------------------
1503 ALTERNATIVE B - Public Domain (www.unlicense.org)
1504 This is free and unencumbered software released into the public domain.
1505 Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
1506 software, either in source code form or as a compiled binary, for any purpose,
1507 commercial or non-commercial, and by any means.
1508 In jurisdictions that recognize copyright laws, the author or authors of this
1509 software dedicate any and all copyright interest in the software to the public
1510 domain. We make this dedication for the benefit of the public at large and to
1511 the detriment of our heirs and successors. We intend this dedication to be an
1512 overt act of relinquishment in perpetuity of all present and future rights to
1513 this software under copyright law.
1514 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1515 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1516 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1517 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
1518 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1519 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1520 ------------------------------------------------------------------------------
1521 */