Path: blob/21.2-virgl/src/imgui/imstb_textedit.h
4558 views
// [DEAR IMGUI]1// This is a slightly modified version of stb_textedit.h 1.13.2// Those changes would need to be pushed into nothings/stb:3// - Fix in stb_textedit_discard_redo (see https://github.com/nothings/stb/issues/321)4// Grep for [DEAR IMGUI] to find the changes.56// stb_textedit.h - v1.13 - public domain - Sean Barrett7// Development of this library was sponsored by RAD Game Tools8//9// This C header file implements the guts of a multi-line text-editing10// widget; you implement display, word-wrapping, and low-level string11// insertion/deletion, and stb_textedit will map user inputs into12// insertions & deletions, plus updates to the cursor position,13// selection state, and undo state.14//15// It is intended for use in games and other systems that need to build16// their own custom widgets and which do not have heavy text-editing17// requirements (this library is not recommended for use for editing large18// texts, as its performance does not scale and it has limited undo).19//20// Non-trivial behaviors are modelled after Windows text controls.21//22//23// LICENSE24//25// See end of file for license information.26//27//28// DEPENDENCIES29//30// Uses the C runtime function 'memmove', which you can override31// by defining STB_TEXTEDIT_memmove before the implementation.32// Uses no other functions. Performs no runtime allocations.33//34//35// VERSION HISTORY36//37// 1.13 (2019-02-07) fix bug in undo size management38// 1.12 (2018-01-29) user can change STB_TEXTEDIT_KEYTYPE, fix redo to avoid crash39// 1.11 (2017-03-03) fix HOME on last line, dragging off single-line textfield40// 1.10 (2016-10-25) supress warnings about casting away const with -Wcast-qual41// 1.9 (2016-08-27) customizable move-by-word42// 1.8 (2016-04-02) better keyboard handling when mouse button is down43// 1.7 (2015-09-13) change y range handling in case baseline is non-044// 1.6 (2015-04-15) allow STB_TEXTEDIT_memmove45// 1.5 (2014-09-10) add support for secondary keys for OS X46// 1.4 (2014-08-17) fix signed/unsigned warnings47// 1.3 (2014-06-19) fix mouse clicking to round to nearest char boundary48// 1.2 (2014-05-27) fix some RAD types that had crept into the new code49// 1.1 (2013-12-15) move-by-word (requires STB_TEXTEDIT_IS_SPACE )50// 1.0 (2012-07-26) improve documentation, initial public release51// 0.3 (2012-02-24) bugfixes, single-line mode; insert mode52// 0.2 (2011-11-28) fixes to undo/redo53// 0.1 (2010-07-08) initial version54//55// ADDITIONAL CONTRIBUTORS56//57// Ulf Winklemann: move-by-word in 1.158// Fabian Giesen: secondary key inputs in 1.559// Martins Mozeiko: STB_TEXTEDIT_memmove in 1.660//61// Bugfixes:62// Scott Graham63// Daniel Keller64// Omar Cornut65// Dan Thompson66//67// USAGE68//69// This file behaves differently depending on what symbols you define70// before including it.71//72//73// Header-file mode:74//75// If you do not define STB_TEXTEDIT_IMPLEMENTATION before including this,76// it will operate in "header file" mode. In this mode, it declares a77// single public symbol, STB_TexteditState, which encapsulates the current78// state of a text widget (except for the string, which you will store79// separately).80//81// To compile in this mode, you must define STB_TEXTEDIT_CHARTYPE to a82// primitive type that defines a single character (e.g. char, wchar_t, etc).83//84// To save space or increase undo-ability, you can optionally define the85// following things that are used by the undo system:86//87// STB_TEXTEDIT_POSITIONTYPE small int type encoding a valid cursor position88// STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow89// STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer90//91// If you don't define these, they are set to permissive types and92// moderate sizes. The undo system does no memory allocations, so93// it grows STB_TexteditState by the worst-case storage which is (in bytes):94//95// [4 + 3 * sizeof(STB_TEXTEDIT_POSITIONTYPE)] * STB_TEXTEDIT_UNDOSTATE_COUNT96// + sizeof(STB_TEXTEDIT_CHARTYPE) * STB_TEXTEDIT_UNDOCHAR_COUNT97//98//99// Implementation mode:100//101// If you define STB_TEXTEDIT_IMPLEMENTATION before including this, it102// will compile the implementation of the text edit widget, depending103// on a large number of symbols which must be defined before the include.104//105// The implementation is defined only as static functions. You will then106// need to provide your own APIs in the same file which will access the107// static functions.108//109// The basic concept is that you provide a "string" object which110// behaves like an array of characters. stb_textedit uses indices to111// refer to positions in the string, implicitly representing positions112// in the displayed textedit. This is true for both plain text and113// rich text; even with rich text stb_truetype interacts with your114// code as if there was an array of all the displayed characters.115//116// Symbols that must be the same in header-file and implementation mode:117//118// STB_TEXTEDIT_CHARTYPE the character type119// STB_TEXTEDIT_POSITIONTYPE small type that is a valid cursor position120// STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow121// STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer122//123// Symbols you must define for implementation mode:124//125// STB_TEXTEDIT_STRING the type of object representing a string being edited,126// typically this is a wrapper object with other data you need127//128// STB_TEXTEDIT_STRINGLEN(obj) the length of the string (ideally O(1))129// STB_TEXTEDIT_LAYOUTROW(&r,obj,n) returns the results of laying out a line of characters130// starting from character #n (see discussion below)131// STB_TEXTEDIT_GETWIDTH(obj,n,i) returns the pixel delta from the xpos of the i'th character132// to the xpos of the i+1'th char for a line of characters133// starting at character #n (i.e. accounts for kerning134// with previous char)135// STB_TEXTEDIT_KEYTOTEXT(k) maps a keyboard input to an insertable character136// (return type is int, -1 means not valid to insert)137// STB_TEXTEDIT_GETCHAR(obj,i) returns the i'th character of obj, 0-based138// STB_TEXTEDIT_NEWLINE the character returned by _GETCHAR() we recognize139// as manually wordwrapping for end-of-line positioning140//141// STB_TEXTEDIT_DELETECHARS(obj,i,n) delete n characters starting at i142// STB_TEXTEDIT_INSERTCHARS(obj,i,c*,n) insert n characters at i (pointed to by STB_TEXTEDIT_CHARTYPE*)143//144// STB_TEXTEDIT_K_SHIFT a power of two that is or'd in to a keyboard input to represent the shift key145//146// STB_TEXTEDIT_K_LEFT keyboard input to move cursor left147// STB_TEXTEDIT_K_RIGHT keyboard input to move cursor right148// STB_TEXTEDIT_K_UP keyboard input to move cursor up149// STB_TEXTEDIT_K_DOWN keyboard input to move cursor down150// STB_TEXTEDIT_K_LINESTART keyboard input to move cursor to start of line // e.g. HOME151// STB_TEXTEDIT_K_LINEEND keyboard input to move cursor to end of line // e.g. END152// STB_TEXTEDIT_K_TEXTSTART keyboard input to move cursor to start of text // e.g. ctrl-HOME153// STB_TEXTEDIT_K_TEXTEND keyboard input to move cursor to end of text // e.g. ctrl-END154// STB_TEXTEDIT_K_DELETE keyboard input to delete selection or character under cursor155// STB_TEXTEDIT_K_BACKSPACE keyboard input to delete selection or character left of cursor156// STB_TEXTEDIT_K_UNDO keyboard input to perform undo157// STB_TEXTEDIT_K_REDO keyboard input to perform redo158//159// Optional:160// STB_TEXTEDIT_K_INSERT keyboard input to toggle insert mode161// STB_TEXTEDIT_IS_SPACE(ch) true if character is whitespace (e.g. 'isspace'),162// required for default WORDLEFT/WORDRIGHT handlers163// STB_TEXTEDIT_MOVEWORDLEFT(obj,i) custom handler for WORDLEFT, returns index to move cursor to164// STB_TEXTEDIT_MOVEWORDRIGHT(obj,i) custom handler for WORDRIGHT, returns index to move cursor to165// STB_TEXTEDIT_K_WORDLEFT keyboard input to move cursor left one word // e.g. ctrl-LEFT166// STB_TEXTEDIT_K_WORDRIGHT keyboard input to move cursor right one word // e.g. ctrl-RIGHT167// STB_TEXTEDIT_K_LINESTART2 secondary keyboard input to move cursor to start of line168// STB_TEXTEDIT_K_LINEEND2 secondary keyboard input to move cursor to end of line169// STB_TEXTEDIT_K_TEXTSTART2 secondary keyboard input to move cursor to start of text170// STB_TEXTEDIT_K_TEXTEND2 secondary keyboard input to move cursor to end of text171//172// Todo:173// STB_TEXTEDIT_K_PGUP keyboard input to move cursor up a page174// STB_TEXTEDIT_K_PGDOWN keyboard input to move cursor down a page175//176// Keyboard input must be encoded as a single integer value; e.g. a character code177// and some bitflags that represent shift states. to simplify the interface, SHIFT must178// be a bitflag, so we can test the shifted state of cursor movements to allow selection,179// i.e. (STB_TEXTED_K_RIGHT|STB_TEXTEDIT_K_SHIFT) should be shifted right-arrow.180//181// You can encode other things, such as CONTROL or ALT, in additional bits, and182// then test for their presence in e.g. STB_TEXTEDIT_K_WORDLEFT. For example,183// my Windows implementations add an additional CONTROL bit, and an additional KEYDOWN184// bit. Then all of the STB_TEXTEDIT_K_ values bitwise-or in the KEYDOWN bit,185// and I pass both WM_KEYDOWN and WM_CHAR events to the "key" function in the186// API below. The control keys will only match WM_KEYDOWN events because of the187// keydown bit I add, and STB_TEXTEDIT_KEYTOTEXT only tests for the KEYDOWN188// bit so it only decodes WM_CHAR events.189//190// STB_TEXTEDIT_LAYOUTROW returns information about the shape of one displayed191// row of characters assuming they start on the i'th character--the width and192// the height and the number of characters consumed. This allows this library193// to traverse the entire layout incrementally. You need to compute word-wrapping194// here.195//196// Each textfield keeps its own insert mode state, which is not how normal197// applications work. To keep an app-wide insert mode, update/copy the198// "insert_mode" field of STB_TexteditState before/after calling API functions.199//200// API201//202// void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line)203//204// void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)205// void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)206// int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)207// int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len)208// void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXEDIT_KEYTYPE key)209//210// Each of these functions potentially updates the string and updates the211// state.212//213// initialize_state:214// set the textedit state to a known good default state when initially215// constructing the textedit.216//217// click:218// call this with the mouse x,y on a mouse down; it will update the cursor219// and reset the selection start/end to the cursor point. the x,y must220// be relative to the text widget, with (0,0) being the top left.221//222// drag:223// call this with the mouse x,y on a mouse drag/up; it will update the224// cursor and the selection end point225//226// cut:227// call this to delete the current selection; returns true if there was228// one. you should FIRST copy the current selection to the system paste buffer.229// (To copy, just copy the current selection out of the string yourself.)230//231// paste:232// call this to paste text at the current cursor point or over the current233// selection if there is one.234//235// key:236// call this for keyboard inputs sent to the textfield. you can use it237// for "key down" events or for "translated" key events. if you need to238// do both (as in Win32), or distinguish Unicode characters from control239// inputs, set a high bit to distinguish the two; then you can define the240// various definitions like STB_TEXTEDIT_K_LEFT have the is-key-event bit241// set, and make STB_TEXTEDIT_KEYTOCHAR check that the is-key-event bit is242// clear. STB_TEXTEDIT_KEYTYPE defaults to int, but you can #define it to243// anything other type you wante before including.244//245//246// When rendering, you can read the cursor position and selection state from247// the STB_TexteditState.248//249//250// Notes:251//252// This is designed to be usable in IMGUI, so it allows for the possibility of253// running in an IMGUI that has NOT cached the multi-line layout. For this254// reason, it provides an interface that is compatible with computing the255// layout incrementally--we try to make sure we make as few passes through256// as possible. (For example, to locate the mouse pointer in the text, we257// could define functions that return the X and Y positions of characters258// and binary search Y and then X, but if we're doing dynamic layout this259// will run the layout algorithm many times, so instead we manually search260// forward in one pass. Similar logic applies to e.g. up-arrow and261// down-arrow movement.)262//263// If it's run in a widget that *has* cached the layout, then this is less264// efficient, but it's not horrible on modern computers. But you wouldn't265// want to edit million-line files with it.266267268////////////////////////////////////////////////////////////////////////////269////////////////////////////////////////////////////////////////////////////270////271//// Header-file mode272////273////274275#ifndef INCLUDE_STB_TEXTEDIT_H276#define INCLUDE_STB_TEXTEDIT_H277278////////////////////////////////////////////////////////////////////////279//280// STB_TexteditState281//282// Definition of STB_TexteditState which you should store283// per-textfield; it includes cursor position, selection state,284// and undo state.285//286287#ifndef STB_TEXTEDIT_UNDOSTATECOUNT288#define STB_TEXTEDIT_UNDOSTATECOUNT 99289#endif290#ifndef STB_TEXTEDIT_UNDOCHARCOUNT291#define STB_TEXTEDIT_UNDOCHARCOUNT 999292#endif293#ifndef STB_TEXTEDIT_CHARTYPE294#define STB_TEXTEDIT_CHARTYPE int295#endif296#ifndef STB_TEXTEDIT_POSITIONTYPE297#define STB_TEXTEDIT_POSITIONTYPE int298#endif299300typedef struct301{302// private data303STB_TEXTEDIT_POSITIONTYPE where;304STB_TEXTEDIT_POSITIONTYPE insert_length;305STB_TEXTEDIT_POSITIONTYPE delete_length;306int char_storage;307} StbUndoRecord;308309typedef struct310{311// private data312StbUndoRecord undo_rec [STB_TEXTEDIT_UNDOSTATECOUNT];313STB_TEXTEDIT_CHARTYPE undo_char[STB_TEXTEDIT_UNDOCHARCOUNT];314short undo_point, redo_point;315int undo_char_point, redo_char_point;316} StbUndoState;317318typedef struct319{320/////////////////////321//322// public data323//324325int cursor;326// position of the text cursor within the string327328int select_start; // selection start point329int 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. when332// dragging the mouse, start is where the initial click was, and you333// can drag in either direction)334335unsigned char insert_mode;336// each textfield keeps its own insert mode state. to keep an app-wide337// insert mode, copy this value in/out of the app state338339/////////////////////340//341// private data342//343unsigned char cursor_at_end_of_line; // not implemented yet344unsigned char initialized;345unsigned char has_preferred_x;346unsigned char single_line;347unsigned char padding1, padding2, padding3;348float preferred_x; // this determines where the cursor up/down tries to seek to along x349StbUndoState undostate;350} STB_TexteditState;351352353////////////////////////////////////////////////////////////////////////354//355// StbTexteditRow356//357// Result of layout query, used by stb_textedit to determine where358// the text in each row is.359360// result of layout query361typedef struct362{363float x0,x1; // starting x location, end x location (allows for align=right, etc)364float baseline_y_delta; // position of baseline relative to previous row's baseline365float ymin,ymax; // height of row above and below baseline366int num_chars;367} StbTexteditRow;368#endif //INCLUDE_STB_TEXTEDIT_H369370371////////////////////////////////////////////////////////////////////////////372////////////////////////////////////////////////////////////////////////////373////374//// Implementation mode375////376////377378379// implementation isn't include-guarded, since it might have indirectly380// included just the "header" portion381#ifdef STB_TEXTEDIT_IMPLEMENTATION382383#ifndef STB_TEXTEDIT_memmove384#include <string.h>385#define STB_TEXTEDIT_memmove memmove386#endif387388389/////////////////////////////////////////////////////////////////////////////390//391// Mouse input handling392//393394// traverse the layout to locate the nearest character to a display position395static int stb_text_locate_coord(STB_TEXTEDIT_STRING *str, float x, float y)396{397StbTexteditRow r;398int n = STB_TEXTEDIT_STRINGLEN(str);399float base_y = 0, prev_x;400int i=0, k;401402r.x0 = r.x1 = 0;403r.ymin = r.ymax = 0;404r.num_chars = 0;405406// search rows to find one that straddles 'y'407while (i < n) {408STB_TEXTEDIT_LAYOUTROW(&r, str, i);409if (r.num_chars <= 0)410return n;411412if (i==0 && y < base_y + r.ymin)413return 0;414415if (y < base_y + r.ymax)416break;417418i += r.num_chars;419base_y += r.baseline_y_delta;420}421422// below all text, return 'after' last character423if (i >= n)424return n;425426// check if it's before the beginning of the line427if (x < r.x0)428return i;429430// check if it's before the end of the line431if (x < r.x1) {432// search characters in row for one that straddles 'x'433prev_x = r.x0;434for (k=0; k < r.num_chars; ++k) {435float w = STB_TEXTEDIT_GETWIDTH(str, i, k);436if (x < prev_x+w) {437if (x < prev_x+w/2)438return k+i;439else440return k+i+1;441}442prev_x += w;443}444// shouldn't happen, but if it does, fall through to end-of-line case445}446447// if the last character is a newline, return that. otherwise return 'after' the last character448if (STB_TEXTEDIT_GETCHAR(str, i+r.num_chars-1) == STB_TEXTEDIT_NEWLINE)449return i+r.num_chars-1;450else451return i+r.num_chars;452}453454// API click: on mouse down, move the cursor to the clicked location, and reset the selection455static void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)456{457// In single-line mode, just always make y = 0. This lets the drag keep working if the mouse458// goes off the top or bottom of the text459if( state->single_line )460{461StbTexteditRow r;462STB_TEXTEDIT_LAYOUTROW(&r, str, 0);463y = r.ymin;464}465466state->cursor = stb_text_locate_coord(str, x, y);467state->select_start = state->cursor;468state->select_end = state->cursor;469state->has_preferred_x = 0;470}471472// API drag: on mouse drag, move the cursor and selection endpoint to the clicked location473static void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)474{475int p = 0;476477// In single-line mode, just always make y = 0. This lets the drag keep working if the mouse478// goes off the top or bottom of the text479if( state->single_line )480{481StbTexteditRow r;482STB_TEXTEDIT_LAYOUTROW(&r, str, 0);483y = r.ymin;484}485486if (state->select_start == state->select_end)487state->select_start = state->cursor;488489p = stb_text_locate_coord(str, x, y);490state->cursor = state->select_end = p;491}492493/////////////////////////////////////////////////////////////////////////////494//495// Keyboard input handling496//497498// forward declarations499static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state);500static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state);501static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length);502static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length);503static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length);504505typedef struct506{507float x,y; // position of n'th character508float height; // height of line509int first_char, length; // first char of row, and length510int prev_first; // first char of previous row511} StbFindState;512513// find the x/y location of a character, and remember info about the previous row in514// case we get a move-up event (for page up, we'll have to rescan)515static void stb_textedit_find_charpos(StbFindState *find, STB_TEXTEDIT_STRING *str, int n, int single_line)516{517StbTexteditRow r;518int prev_start = 0;519int z = STB_TEXTEDIT_STRINGLEN(str);520int i=0, first;521522if (n == z) {523// if it's at the end, then find the last line -- simpler than trying to524// explicitly handle this case in the regular code525if (single_line) {526STB_TEXTEDIT_LAYOUTROW(&r, str, 0);527find->y = 0;528find->first_char = 0;529find->length = z;530find->height = r.ymax - r.ymin;531find->x = r.x1;532} else {533find->y = 0;534find->x = 0;535find->height = 1;536while (i < z) {537STB_TEXTEDIT_LAYOUTROW(&r, str, i);538prev_start = i;539i += r.num_chars;540}541find->first_char = i;542find->length = 0;543find->prev_first = prev_start;544}545return;546}547548// search rows to find the one that straddles character n549find->y = 0;550551for(;;) {552STB_TEXTEDIT_LAYOUTROW(&r, str, i);553if (n < i + r.num_chars)554break;555prev_start = i;556i += r.num_chars;557find->y += r.baseline_y_delta;558}559560find->first_char = first = i;561find->length = r.num_chars;562find->height = r.ymax - r.ymin;563find->prev_first = prev_start;564565// now scan to find xpos566find->x = r.x0;567for (i=0; first+i < n; ++i)568find->x += STB_TEXTEDIT_GETWIDTH(str, first, i);569}570571#define STB_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end)572573// make the selection/cursor state valid if client altered the string574static void stb_textedit_clamp(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)575{576int n = STB_TEXTEDIT_STRINGLEN(str);577if (STB_TEXT_HAS_SELECTION(state)) {578if (state->select_start > n) state->select_start = n;579if (state->select_end > n) state->select_end = n;580// if clamping forced them to be equal, move the cursor to match581if (state->select_start == state->select_end)582state->cursor = state->select_start;583}584if (state->cursor > n) state->cursor = n;585}586587// delete characters while updating undo588static void stb_textedit_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int len)589{590stb_text_makeundo_delete(str, state, where, len);591STB_TEXTEDIT_DELETECHARS(str, where, len);592state->has_preferred_x = 0;593}594595// delete the section596static void stb_textedit_delete_selection(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)597{598stb_textedit_clamp(str, state);599if (STB_TEXT_HAS_SELECTION(state)) {600if (state->select_start < state->select_end) {601stb_textedit_delete(str, state, state->select_start, state->select_end - state->select_start);602state->select_end = state->cursor = state->select_start;603} else {604stb_textedit_delete(str, state, state->select_end, state->select_start - state->select_end);605state->select_start = state->cursor = state->select_end;606}607state->has_preferred_x = 0;608}609}610611// canoncialize the selection so start <= end612static void stb_textedit_sortselection(STB_TexteditState *state)613{614if (state->select_end < state->select_start) {615int temp = state->select_end;616state->select_end = state->select_start;617state->select_start = temp;618}619}620621// move cursor to first character of selection622static void stb_textedit_move_to_first(STB_TexteditState *state)623{624if (STB_TEXT_HAS_SELECTION(state)) {625stb_textedit_sortselection(state);626state->cursor = state->select_start;627state->select_end = state->select_start;628state->has_preferred_x = 0;629}630}631632// move cursor to last character of selection633static void stb_textedit_move_to_last(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)634{635if (STB_TEXT_HAS_SELECTION(state)) {636stb_textedit_sortselection(state);637stb_textedit_clamp(str, state);638state->cursor = state->select_end;639state->select_start = state->select_end;640state->has_preferred_x = 0;641}642}643644#ifdef STB_TEXTEDIT_IS_SPACE645static int is_word_boundary( STB_TEXTEDIT_STRING *str, int idx )646{647return idx > 0 ? (STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str,idx-1) ) && !STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str, idx) ) ) : 1;648}649650#ifndef STB_TEXTEDIT_MOVEWORDLEFT651static int stb_textedit_move_to_word_previous( STB_TEXTEDIT_STRING *str, int c )652{653--c; // always move at least one character654while( c >= 0 && !is_word_boundary( str, c ) )655--c;656657if( c < 0 )658c = 0;659660return c;661}662#define STB_TEXTEDIT_MOVEWORDLEFT stb_textedit_move_to_word_previous663#endif664665#ifndef STB_TEXTEDIT_MOVEWORDRIGHT666static int stb_textedit_move_to_word_next( STB_TEXTEDIT_STRING *str, int c )667{668const int len = STB_TEXTEDIT_STRINGLEN(str);669++c; // always move at least one character670while( c < len && !is_word_boundary( str, c ) )671++c;672673if( c > len )674c = len;675676return c;677}678#define STB_TEXTEDIT_MOVEWORDRIGHT stb_textedit_move_to_word_next679#endif680681#endif682683// update selection and cursor to match each other684static void stb_textedit_prep_selection_at_cursor(STB_TexteditState *state)685{686if (!STB_TEXT_HAS_SELECTION(state))687state->select_start = state->select_end = state->cursor;688else689state->cursor = state->select_end;690}691692// API cut: delete selection693static int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)694{695if (STB_TEXT_HAS_SELECTION(state)) {696stb_textedit_delete_selection(str,state); // implicitly clamps697state->has_preferred_x = 0;698return 1;699}700return 0;701}702703// API paste: replace existing selection with passed-in text704static int stb_textedit_paste_internal(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len)705{706// if there's a selection, the paste should delete it707stb_textedit_clamp(str, state);708stb_textedit_delete_selection(str,state);709// try to insert the characters710if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, len)) {711stb_text_makeundo_insert(state, state->cursor, len);712state->cursor += len;713state->has_preferred_x = 0;714return 1;715}716// remove the undo since we didn't actually insert the characters717if (state->undostate.undo_point)718--state->undostate.undo_point;719return 0;720}721722#ifndef STB_TEXTEDIT_KEYTYPE723#define STB_TEXTEDIT_KEYTYPE int724#endif725726// API key: process a keyboard input727static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_KEYTYPE key)728{729retry:730switch (key) {731default: {732int c = STB_TEXTEDIT_KEYTOTEXT(key);733if (c > 0) {734STB_TEXTEDIT_CHARTYPE ch = (STB_TEXTEDIT_CHARTYPE) c;735736// can't add newline in single-line mode737if (c == '\n' && state->single_line)738break;739740if (state->insert_mode && !STB_TEXT_HAS_SELECTION(state) && state->cursor < STB_TEXTEDIT_STRINGLEN(str)) {741stb_text_makeundo_replace(str, state, state->cursor, 1, 1);742STB_TEXTEDIT_DELETECHARS(str, state->cursor, 1);743if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) {744++state->cursor;745state->has_preferred_x = 0;746}747} else {748stb_textedit_delete_selection(str,state); // implicitly clamps749if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) {750stb_text_makeundo_insert(state, state->cursor, 1);751++state->cursor;752state->has_preferred_x = 0;753}754}755}756break;757}758759#ifdef STB_TEXTEDIT_K_INSERT760case STB_TEXTEDIT_K_INSERT:761state->insert_mode = !state->insert_mode;762break;763#endif764765case STB_TEXTEDIT_K_UNDO:766stb_text_undo(str, state);767state->has_preferred_x = 0;768break;769770case STB_TEXTEDIT_K_REDO:771stb_text_redo(str, state);772state->has_preferred_x = 0;773break;774775case STB_TEXTEDIT_K_LEFT:776// if currently there's a selection, move cursor to start of selection777if (STB_TEXT_HAS_SELECTION(state))778stb_textedit_move_to_first(state);779else780if (state->cursor > 0)781--state->cursor;782state->has_preferred_x = 0;783break;784785case STB_TEXTEDIT_K_RIGHT:786// if currently there's a selection, move cursor to end of selection787if (STB_TEXT_HAS_SELECTION(state))788stb_textedit_move_to_last(str, state);789else790++state->cursor;791stb_textedit_clamp(str, state);792state->has_preferred_x = 0;793break;794795case STB_TEXTEDIT_K_LEFT | STB_TEXTEDIT_K_SHIFT:796stb_textedit_clamp(str, state);797stb_textedit_prep_selection_at_cursor(state);798// move selection left799if (state->select_end > 0)800--state->select_end;801state->cursor = state->select_end;802state->has_preferred_x = 0;803break;804805#ifdef STB_TEXTEDIT_MOVEWORDLEFT806case STB_TEXTEDIT_K_WORDLEFT:807if (STB_TEXT_HAS_SELECTION(state))808stb_textedit_move_to_first(state);809else {810state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor);811stb_textedit_clamp( str, state );812}813break;814815case STB_TEXTEDIT_K_WORDLEFT | STB_TEXTEDIT_K_SHIFT:816if( !STB_TEXT_HAS_SELECTION( state ) )817stb_textedit_prep_selection_at_cursor(state);818819state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor);820state->select_end = state->cursor;821822stb_textedit_clamp( str, state );823break;824#endif825826#ifdef STB_TEXTEDIT_MOVEWORDRIGHT827case STB_TEXTEDIT_K_WORDRIGHT:828if (STB_TEXT_HAS_SELECTION(state))829stb_textedit_move_to_last(str, state);830else {831state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor);832stb_textedit_clamp( str, state );833}834break;835836case STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT:837if( !STB_TEXT_HAS_SELECTION( state ) )838stb_textedit_prep_selection_at_cursor(state);839840state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor);841state->select_end = state->cursor;842843stb_textedit_clamp( str, state );844break;845#endif846847case STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT:848stb_textedit_prep_selection_at_cursor(state);849// move selection right850++state->select_end;851stb_textedit_clamp(str, state);852state->cursor = state->select_end;853state->has_preferred_x = 0;854break;855856case STB_TEXTEDIT_K_DOWN:857case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT: {858StbFindState find;859StbTexteditRow row;860int i, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;861862if (state->single_line) {863// on windows, up&down in single-line behave like left&right864key = STB_TEXTEDIT_K_RIGHT | (key & STB_TEXTEDIT_K_SHIFT);865goto retry;866}867868if (sel)869stb_textedit_prep_selection_at_cursor(state);870else if (STB_TEXT_HAS_SELECTION(state))871stb_textedit_move_to_last(str,state);872873// compute current position of cursor point874stb_textedit_clamp(str, state);875stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);876877// now find character position down a row878if (find.length) {879float goal_x = state->has_preferred_x ? state->preferred_x : find.x;880float x;881int start = find.first_char + find.length;882state->cursor = start;883STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);884x = row.x0;885for (i=0; i < row.num_chars; ++i) {886float dx = STB_TEXTEDIT_GETWIDTH(str, start, i);887#ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE888if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE)889break;890#endif891x += dx;892if (x > goal_x)893break;894++state->cursor;895}896stb_textedit_clamp(str, state);897898state->has_preferred_x = 1;899state->preferred_x = goal_x;900901if (sel)902state->select_end = state->cursor;903}904break;905}906907case STB_TEXTEDIT_K_UP:908case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT: {909StbFindState find;910StbTexteditRow row;911int i, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;912913if (state->single_line) {914// on windows, up&down become left&right915key = STB_TEXTEDIT_K_LEFT | (key & STB_TEXTEDIT_K_SHIFT);916goto retry;917}918919if (sel)920stb_textedit_prep_selection_at_cursor(state);921else if (STB_TEXT_HAS_SELECTION(state))922stb_textedit_move_to_first(state);923924// compute current position of cursor point925stb_textedit_clamp(str, state);926stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);927928// can only go up if there's a previous row929if (find.prev_first != find.first_char) {930// now find character position up a row931float goal_x = state->has_preferred_x ? state->preferred_x : find.x;932float x;933state->cursor = find.prev_first;934STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);935x = row.x0;936for (i=0; i < row.num_chars; ++i) {937float dx = STB_TEXTEDIT_GETWIDTH(str, find.prev_first, i);938#ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE939if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE)940break;941#endif942x += dx;943if (x > goal_x)944break;945++state->cursor;946}947stb_textedit_clamp(str, state);948949state->has_preferred_x = 1;950state->preferred_x = goal_x;951952if (sel)953state->select_end = state->cursor;954}955break;956}957958case STB_TEXTEDIT_K_DELETE:959case STB_TEXTEDIT_K_DELETE | STB_TEXTEDIT_K_SHIFT:960if (STB_TEXT_HAS_SELECTION(state))961stb_textedit_delete_selection(str, state);962else {963int n = STB_TEXTEDIT_STRINGLEN(str);964if (state->cursor < n)965stb_textedit_delete(str, state, state->cursor, 1);966}967state->has_preferred_x = 0;968break;969970case STB_TEXTEDIT_K_BACKSPACE:971case STB_TEXTEDIT_K_BACKSPACE | STB_TEXTEDIT_K_SHIFT:972if (STB_TEXT_HAS_SELECTION(state))973stb_textedit_delete_selection(str, state);974else {975stb_textedit_clamp(str, state);976if (state->cursor > 0) {977stb_textedit_delete(str, state, state->cursor-1, 1);978--state->cursor;979}980}981state->has_preferred_x = 0;982break;983984#ifdef STB_TEXTEDIT_K_TEXTSTART2985case STB_TEXTEDIT_K_TEXTSTART2:986#endif987case STB_TEXTEDIT_K_TEXTSTART:988state->cursor = state->select_start = state->select_end = 0;989state->has_preferred_x = 0;990break;991992#ifdef STB_TEXTEDIT_K_TEXTEND2993case STB_TEXTEDIT_K_TEXTEND2:994#endif995case STB_TEXTEDIT_K_TEXTEND:996state->cursor = STB_TEXTEDIT_STRINGLEN(str);997state->select_start = state->select_end = 0;998state->has_preferred_x = 0;999break;10001001#ifdef STB_TEXTEDIT_K_TEXTSTART21002case STB_TEXTEDIT_K_TEXTSTART2 | STB_TEXTEDIT_K_SHIFT:1003#endif1004case STB_TEXTEDIT_K_TEXTSTART | STB_TEXTEDIT_K_SHIFT:1005stb_textedit_prep_selection_at_cursor(state);1006state->cursor = state->select_end = 0;1007state->has_preferred_x = 0;1008break;10091010#ifdef STB_TEXTEDIT_K_TEXTEND21011case STB_TEXTEDIT_K_TEXTEND2 | STB_TEXTEDIT_K_SHIFT:1012#endif1013case STB_TEXTEDIT_K_TEXTEND | STB_TEXTEDIT_K_SHIFT:1014stb_textedit_prep_selection_at_cursor(state);1015state->cursor = state->select_end = STB_TEXTEDIT_STRINGLEN(str);1016state->has_preferred_x = 0;1017break;101810191020#ifdef STB_TEXTEDIT_K_LINESTART21021case STB_TEXTEDIT_K_LINESTART2:1022#endif1023case STB_TEXTEDIT_K_LINESTART:1024stb_textedit_clamp(str, state);1025stb_textedit_move_to_first(state);1026if (state->single_line)1027state->cursor = 0;1028else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE)1029--state->cursor;1030state->has_preferred_x = 0;1031break;10321033#ifdef STB_TEXTEDIT_K_LINEEND21034case STB_TEXTEDIT_K_LINEEND2:1035#endif1036case STB_TEXTEDIT_K_LINEEND: {1037int n = STB_TEXTEDIT_STRINGLEN(str);1038stb_textedit_clamp(str, state);1039stb_textedit_move_to_first(state);1040if (state->single_line)1041state->cursor = n;1042else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE)1043++state->cursor;1044state->has_preferred_x = 0;1045break;1046}10471048#ifdef STB_TEXTEDIT_K_LINESTART21049case STB_TEXTEDIT_K_LINESTART2 | STB_TEXTEDIT_K_SHIFT:1050#endif1051case STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT:1052stb_textedit_clamp(str, state);1053stb_textedit_prep_selection_at_cursor(state);1054if (state->single_line)1055state->cursor = 0;1056else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE)1057--state->cursor;1058state->select_end = state->cursor;1059state->has_preferred_x = 0;1060break;10611062#ifdef STB_TEXTEDIT_K_LINEEND21063case STB_TEXTEDIT_K_LINEEND2 | STB_TEXTEDIT_K_SHIFT:1064#endif1065case STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT: {1066int n = STB_TEXTEDIT_STRINGLEN(str);1067stb_textedit_clamp(str, state);1068stb_textedit_prep_selection_at_cursor(state);1069if (state->single_line)1070state->cursor = n;1071else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE)1072++state->cursor;1073state->select_end = state->cursor;1074state->has_preferred_x = 0;1075break;1076}10771078// @TODO:1079// STB_TEXTEDIT_K_PGUP - move cursor up a page1080// STB_TEXTEDIT_K_PGDOWN - move cursor down a page1081}1082}10831084/////////////////////////////////////////////////////////////////////////////1085//1086// Undo processing1087//1088// @OPTIMIZE: the undo/redo buffer should be circular10891090static void stb_textedit_flush_redo(StbUndoState *state)1091{1092state->redo_point = STB_TEXTEDIT_UNDOSTATECOUNT;1093state->redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT;1094}10951096// discard the oldest entry in the undo list1097static void stb_textedit_discard_undo(StbUndoState *state)1098{1099if (state->undo_point > 0) {1100// if the 0th undo state has characters, clean those up1101if (state->undo_rec[0].char_storage >= 0) {1102int n = state->undo_rec[0].insert_length, i;1103// delete n characters from all other records1104state->undo_char_point -= n;1105STB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(STB_TEXTEDIT_CHARTYPE)));1106for (i=0; i < state->undo_point; ++i)1107if (state->undo_rec[i].char_storage >= 0)1108state->undo_rec[i].char_storage -= n; // @OPTIMIZE: get rid of char_storage and infer it1109}1110--state->undo_point;1111STB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0])));1112}1113}11141115// discard the oldest entry in the redo list--it's bad if this1116// ever happens, but because undo & redo have to store the actual1117// characters in different cases, the redo character buffer can1118// fill up even though the undo buffer didn't1119static void stb_textedit_discard_redo(StbUndoState *state)1120{1121int k = STB_TEXTEDIT_UNDOSTATECOUNT-1;11221123if (state->redo_point <= k) {1124// if the k'th undo state has characters, clean those up1125if (state->undo_rec[k].char_storage >= 0) {1126int n = state->undo_rec[k].insert_length, i;1127// move the remaining redo character data to the end of the buffer1128state->redo_char_point += n;1129STB_TEXTEDIT_memmove(state->undo_char + state->redo_char_point, state->undo_char + state->redo_char_point-n, (size_t) ((STB_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point)*sizeof(STB_TEXTEDIT_CHARTYPE)));1130// adjust the position of all the other records to account for above memmove1131for (i=state->redo_point; i < k; ++i)1132if (state->undo_rec[i].char_storage >= 0)1133state->undo_rec[i].char_storage += n;1134}1135// now move all the redo records towards the end of the buffer; the first one is at 'redo_point'1136// {DEAR IMGUI]1137size_t move_size = (size_t)((STB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point - 1) * sizeof(state->undo_rec[0]));1138const char* buf_begin = (char*)state->undo_rec; (void)buf_begin;1139const char* buf_end = (char*)state->undo_rec + sizeof(state->undo_rec); (void)buf_end;1140IM_ASSERT(((char*)(state->undo_rec + state->redo_point)) >= buf_begin);1141IM_ASSERT(((char*)(state->undo_rec + state->redo_point + 1) + move_size) <= buf_end);1142STB_TEXTEDIT_memmove(state->undo_rec + state->redo_point+1, state->undo_rec + state->redo_point, move_size);11431144// now move redo_point to point to the new one1145++state->redo_point;1146}1147}11481149static StbUndoRecord *stb_text_create_undo_record(StbUndoState *state, int numchars)1150{1151// any time we create a new undo record, we discard redo1152stb_textedit_flush_redo(state);11531154// if we have no free records, we have to make room, by sliding the1155// existing records down1156if (state->undo_point == STB_TEXTEDIT_UNDOSTATECOUNT)1157stb_textedit_discard_undo(state);11581159// if the characters to store won't possibly fit in the buffer, we can't undo1160if (numchars > STB_TEXTEDIT_UNDOCHARCOUNT) {1161state->undo_point = 0;1162state->undo_char_point = 0;1163return NULL;1164}11651166// if we don't have enough free characters in the buffer, we have to make room1167while (state->undo_char_point + numchars > STB_TEXTEDIT_UNDOCHARCOUNT)1168stb_textedit_discard_undo(state);11691170return &state->undo_rec[state->undo_point++];1171}11721173static STB_TEXTEDIT_CHARTYPE *stb_text_createundo(StbUndoState *state, int pos, int insert_len, int delete_len)1174{1175StbUndoRecord *r = stb_text_create_undo_record(state, insert_len);1176if (r == NULL)1177return NULL;11781179r->where = pos;1180r->insert_length = (STB_TEXTEDIT_POSITIONTYPE) insert_len;1181r->delete_length = (STB_TEXTEDIT_POSITIONTYPE) delete_len;11821183if (insert_len == 0) {1184r->char_storage = -1;1185return NULL;1186} else {1187r->char_storage = state->undo_char_point;1188state->undo_char_point += insert_len;1189return &state->undo_char[r->char_storage];1190}1191}11921193static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)1194{1195StbUndoState *s = &state->undostate;1196StbUndoRecord u, *r;1197if (s->undo_point == 0)1198return;11991200// we need to do two things: apply the undo record, and create a redo record1201u = s->undo_rec[s->undo_point-1];1202r = &s->undo_rec[s->redo_point-1];1203r->char_storage = -1;12041205r->insert_length = u.delete_length;1206r->delete_length = u.insert_length;1207r->where = u.where;12081209if (u.delete_length) {1210// if the undo record says to delete characters, then the redo record will1211// need to re-insert the characters that get deleted, so we need to store1212// them.12131214// there are three cases:1215// there's enough room to store the characters1216// characters stored for *redoing* don't leave room for redo1217// characters stored for *undoing* don't leave room for redo1218// if the last is true, we have to bail12191220if (s->undo_char_point + u.delete_length >= STB_TEXTEDIT_UNDOCHARCOUNT) {1221// the undo records take up too much character space; there's no space to store the redo characters1222r->insert_length = 0;1223} else {1224int i;12251226// there's definitely room to store the characters eventually1227while (s->undo_char_point + u.delete_length > s->redo_char_point) {1228// should never happen:1229if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT)1230return;1231// there's currently not enough room, so discard a redo record1232stb_textedit_discard_redo(s);1233}1234r = &s->undo_rec[s->redo_point-1];12351236r->char_storage = s->redo_char_point - u.delete_length;1237s->redo_char_point = s->redo_char_point - u.delete_length;12381239// now save the characters1240for (i=0; i < u.delete_length; ++i)1241s->undo_char[r->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u.where + i);1242}12431244// now we can carry out the deletion1245STB_TEXTEDIT_DELETECHARS(str, u.where, u.delete_length);1246}12471248// check type of recorded action:1249if (u.insert_length) {1250// easy case: was a deletion, so we need to insert n characters1251STB_TEXTEDIT_INSERTCHARS(str, u.where, &s->undo_char[u.char_storage], u.insert_length);1252s->undo_char_point -= u.insert_length;1253}12541255state->cursor = u.where + u.insert_length;12561257s->undo_point--;1258s->redo_point--;1259}12601261static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)1262{1263StbUndoState *s = &state->undostate;1264StbUndoRecord *u, r;1265if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT)1266return;12671268// we need to do two things: apply the redo record, and create an undo record1269u = &s->undo_rec[s->undo_point];1270r = s->undo_rec[s->redo_point];12711272// we KNOW there must be room for the undo record, because the redo record1273// was derived from an undo record12741275u->delete_length = r.insert_length;1276u->insert_length = r.delete_length;1277u->where = r.where;1278u->char_storage = -1;12791280if (r.delete_length) {1281// the redo record requires us to delete characters, so the undo record1282// needs to store the characters12831284if (s->undo_char_point + u->insert_length > s->redo_char_point) {1285u->insert_length = 0;1286u->delete_length = 0;1287} else {1288int i;1289u->char_storage = s->undo_char_point;1290s->undo_char_point = s->undo_char_point + u->insert_length;12911292// now save the characters1293for (i=0; i < u->insert_length; ++i)1294s->undo_char[u->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u->where + i);1295}12961297STB_TEXTEDIT_DELETECHARS(str, r.where, r.delete_length);1298}12991300if (r.insert_length) {1301// easy case: need to insert n characters1302STB_TEXTEDIT_INSERTCHARS(str, r.where, &s->undo_char[r.char_storage], r.insert_length);1303s->redo_char_point += r.insert_length;1304}13051306state->cursor = r.where + r.insert_length;13071308s->undo_point++;1309s->redo_point++;1310}13111312static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length)1313{1314stb_text_createundo(&state->undostate, where, 0, length);1315}13161317static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length)1318{1319int i;1320STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, length, 0);1321if (p) {1322for (i=0; i < length; ++i)1323p[i] = STB_TEXTEDIT_GETCHAR(str, where+i);1324}1325}13261327static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length)1328{1329int i;1330STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, old_length, new_length);1331if (p) {1332for (i=0; i < old_length; ++i)1333p[i] = STB_TEXTEDIT_GETCHAR(str, where+i);1334}1335}13361337// reset the state to default1338static void stb_textedit_clear_state(STB_TexteditState *state, int is_single_line)1339{1340state->undostate.undo_point = 0;1341state->undostate.undo_char_point = 0;1342state->undostate.redo_point = STB_TEXTEDIT_UNDOSTATECOUNT;1343state->undostate.redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT;1344state->select_end = state->select_start = 0;1345state->cursor = 0;1346state->has_preferred_x = 0;1347state->preferred_x = 0;1348state->cursor_at_end_of_line = 0;1349state->initialized = 1;1350state->single_line = (unsigned char) is_single_line;1351state->insert_mode = 0;1352}13531354// API initialize1355static void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line)1356{1357stb_textedit_clear_state(state, is_single_line);1358}13591360#if defined(__GNUC__) || defined(__clang__)1361#pragma GCC diagnostic push1362#pragma GCC diagnostic ignored "-Wcast-qual"1363#endif13641365static int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE const *ctext, int len)1366{1367return stb_textedit_paste_internal(str, state, (STB_TEXTEDIT_CHARTYPE *) ctext, len);1368}13691370#if defined(__GNUC__) || defined(__clang__)1371#pragma GCC diagnostic pop1372#endif13731374#endif//STB_TEXTEDIT_IMPLEMENTATION13751376/*1377------------------------------------------------------------------------------1378This software is available under 2 licenses -- choose whichever you prefer.1379------------------------------------------------------------------------------1380ALTERNATIVE A - MIT License1381Copyright (c) 2017 Sean Barrett1382Permission is hereby granted, free of charge, to any person obtaining a copy of1383this software and associated documentation files (the "Software"), to deal in1384the Software without restriction, including without limitation the rights to1385use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies1386of the Software, and to permit persons to whom the Software is furnished to do1387so, subject to the following conditions:1388The above copyright notice and this permission notice shall be included in all1389copies or substantial portions of the Software.1390THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR1391IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,1392FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE1393AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER1394LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,1395OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE1396SOFTWARE.1397------------------------------------------------------------------------------1398ALTERNATIVE B - Public Domain (www.unlicense.org)1399This is free and unencumbered software released into the public domain.1400Anyone is free to copy, modify, publish, use, compile, sell, or distribute this1401software, either in source code form or as a compiled binary, for any purpose,1402commercial or non-commercial, and by any means.1403In jurisdictions that recognize copyright laws, the author or authors of this1404software dedicate any and all copyright interest in the software to the public1405domain. We make this dedication for the benefit of the public at large and to1406the detriment of our heirs and successors. We intend this dedication to be an1407overt act of relinquishment in perpetuity of all present and future rights to1408this software under copyright law.1409THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR1410IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,1411FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE1412AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN1413ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION1414WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.1415------------------------------------------------------------------------------1416*/141714181419