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