Skip to content
Myr

stb_textedit.h

This file

Path

stb_textedit.h

1,429 lines · 52.8 KiB · C

Last changed by

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

History of this file

Take it

The walk stopped after 200 commits: lines older than aeec66c point at it.

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

line 1why, one step back
1// stb_textedit.h - v1.14 - public domain - Sean Barrett

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 2–16why, one step back
2// Development of this library was sponsored by RAD Game Tools
3//4// This C header file implements the guts of a multi-line text-editing5// widget; you implement display, word-wrapping, and low-level string6// insertion/deletion, and stb_textedit will map user inputs into7// insertions & deletions, plus updates to the cursor position,8// selection state, and undo state.9//10// It is intended for use in games and other systems that need to build11// their own custom widgets and which do not have heavy text-editing12// requirements (this library is not recommended for use for editing large13// texts, as its performance does not scale and it has limited undo).14//15// Non-trivial behaviors are modelled after Windows text controls.16//

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

line 17why, one step back
17//

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 18–31why, one step back
18// LICENSE
19//20// See end of file for license information.21//22//23// DEPENDENCIES24//25// Uses the C runtime function 'memmove', which you can override26// by defining STB_TEXTEDIT_memmove before the implementation.27// Uses no other functions. Performs no runtime allocations.28//29//30// VERSION HISTORY31//

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

line 32why, one step back
32// 1.14 (2021-07-11) page up/down, various fixes

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 33–55why, one step back
33// 1.13 (2019-02-07) fix bug in undo size management
34// 1.12 (2018-01-29) user can change STB_TEXTEDIT_KEYTYPE, fix redo to avoid crash35// 1.11 (2017-03-03) fix HOME on last line, dragging off single-line textfield36// 1.10 (2016-10-25) supress warnings about casting away const with -Wcast-qual37// 1.9 (2016-08-27) customizable move-by-word38// 1.8 (2016-04-02) better keyboard handling when mouse button is down39// 1.7 (2015-09-13) change y range handling in case baseline is non-040// 1.6 (2015-04-15) allow STB_TEXTEDIT_memmove41// 1.5 (2014-09-10) add support for secondary keys for OS X42// 1.4 (2014-08-17) fix signed/unsigned warnings43// 1.3 (2014-06-19) fix mouse clicking to round to nearest char boundary44// 1.2 (2014-05-27) fix some RAD types that had crept into the new code45// 1.1 (2013-12-15) move-by-word (requires STB_TEXTEDIT_IS_SPACE )46// 1.0 (2012-07-26) improve documentation, initial public release47// 0.3 (2012-02-24) bugfixes, single-line mode; insert mode48// 0.2 (2011-11-28) fixes to undo/redo49// 0.1 (2010-07-08) initial version50//51// ADDITIONAL CONTRIBUTORS52//53// Ulf Winklemann: move-by-word in 1.154// Fabian Giesen: secondary key inputs in 1.555// Martins Mozeiko: STB_TEXTEDIT_memmove in 1.6

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

line 56why, one step back
56// Louis Schnellbach: page up/down in 1.14

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 57–91why, one step back
57//
58// Bugfixes:59// Scott Graham60// Daniel Keller61// Omar Cornut62// Dan Thompson63//64// USAGE65//66// This file behaves differently depending on what symbols you define67// before including it.68//69//70// Header-file mode:71//72// If you do not define STB_TEXTEDIT_IMPLEMENTATION before including this,73// it will operate in "header file" mode. In this mode, it declares a74// single public symbol, STB_TexteditState, which encapsulates the current75// state of a text widget (except for the string, which you will store76// separately).77//78// To compile in this mode, you must define STB_TEXTEDIT_CHARTYPE to a79// primitive type that defines a single character (e.g. char, wchar_t, etc).80//81// To save space or increase undo-ability, you can optionally define the82// following things that are used by the undo system:83//84// STB_TEXTEDIT_POSITIONTYPE small int type encoding a valid cursor position85// STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow86// STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer87//88// If you don't define these, they are set to permissive types and89// moderate sizes. The undo system does no memory allocations, so90// it grows STB_TexteditState by the worst-case storage which is (in bytes):91//

Merge branch 'dev' into workingSean Barrettbefore adoptionc497f72

lines 92–93why, one step back
92// [4 + 3 * sizeof(STB_TEXTEDIT_POSITIONTYPE)] * STB_TEXTEDIT_UNDOSTATECOUNT
93// + sizeof(STB_TEXTEDIT_CHARTYPE) * STB_TEXTEDIT_UNDOCHARCOUNT

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 94–146why, one step back
94//
95//96// Implementation mode:97//98// If you define STB_TEXTEDIT_IMPLEMENTATION before including this, it99// will compile the implementation of the text edit widget, depending100// on a large number of symbols which must be defined before the include.101//102// The implementation is defined only as static functions. You will then103// need to provide your own APIs in the same file which will access the104// static functions.105//106// The basic concept is that you provide a "string" object which107// behaves like an array of characters. stb_textedit uses indices to108// refer to positions in the string, implicitly representing positions109// in the displayed textedit. This is true for both plain text and110// rich text; even with rich text stb_truetype interacts with your111// code as if there was an array of all the displayed characters.112//113// Symbols that must be the same in header-file and implementation mode:114//115// STB_TEXTEDIT_CHARTYPE the character type116// STB_TEXTEDIT_POSITIONTYPE small type that is a valid cursor position117// STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow118// STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer119//120// Symbols you must define for implementation mode:121//122// STB_TEXTEDIT_STRING the type of object representing a string being edited,123// typically this is a wrapper object with other data you need124//125// STB_TEXTEDIT_STRINGLEN(obj) the length of the string (ideally O(1))126// STB_TEXTEDIT_LAYOUTROW(&r,obj,n) returns the results of laying out a line of characters127// starting from character #n (see discussion below)128// STB_TEXTEDIT_GETWIDTH(obj,n,i) returns the pixel delta from the xpos of the i'th character129// to the xpos of the i+1'th char for a line of characters130// starting at character #n (i.e. accounts for kerning131// with previous char)132// STB_TEXTEDIT_KEYTOTEXT(k) maps a keyboard input to an insertable character133// (return type is int, -1 means not valid to insert)134// STB_TEXTEDIT_GETCHAR(obj,i) returns the i'th character of obj, 0-based135// STB_TEXTEDIT_NEWLINE the character returned by _GETCHAR() we recognize136// as manually wordwrapping for end-of-line positioning137//138// STB_TEXTEDIT_DELETECHARS(obj,i,n) delete n characters starting at i139// STB_TEXTEDIT_INSERTCHARS(obj,i,c*,n) insert n characters at i (pointed to by STB_TEXTEDIT_CHARTYPE*)140//141// STB_TEXTEDIT_K_SHIFT a power of two that is or'd in to a keyboard input to represent the shift key142//143// STB_TEXTEDIT_K_LEFT keyboard input to move cursor left144// STB_TEXTEDIT_K_RIGHT keyboard input to move cursor right145// STB_TEXTEDIT_K_UP keyboard input to move cursor up146// STB_TEXTEDIT_K_DOWN keyboard input to move cursor down

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

lines 147–148why, one step back
147// STB_TEXTEDIT_K_PGUP keyboard input to move cursor up a page
148// STB_TEXTEDIT_K_PGDOWN keyboard input to move cursor down a page

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 149–173why, one step back
149// STB_TEXTEDIT_K_LINESTART keyboard input to move cursor to start of line // e.g. HOME
150// STB_TEXTEDIT_K_LINEEND keyboard input to move cursor to end of line // e.g. END151// STB_TEXTEDIT_K_TEXTSTART keyboard input to move cursor to start of text // e.g. ctrl-HOME152// STB_TEXTEDIT_K_TEXTEND keyboard input to move cursor to end of text // e.g. ctrl-END153// STB_TEXTEDIT_K_DELETE keyboard input to delete selection or character under cursor154// STB_TEXTEDIT_K_BACKSPACE keyboard input to delete selection or character left of cursor155// STB_TEXTEDIT_K_UNDO keyboard input to perform undo156// STB_TEXTEDIT_K_REDO keyboard input to perform redo157//158// Optional:159// STB_TEXTEDIT_K_INSERT keyboard input to toggle insert mode160// STB_TEXTEDIT_IS_SPACE(ch) true if character is whitespace (e.g. 'isspace'),161// required for default WORDLEFT/WORDRIGHT handlers162// STB_TEXTEDIT_MOVEWORDLEFT(obj,i) custom handler for WORDLEFT, returns index to move cursor to163// STB_TEXTEDIT_MOVEWORDRIGHT(obj,i) custom handler for WORDRIGHT, returns index to move cursor to164// STB_TEXTEDIT_K_WORDLEFT keyboard input to move cursor left one word // e.g. ctrl-LEFT165// STB_TEXTEDIT_K_WORDRIGHT keyboard input to move cursor right one word // e.g. ctrl-RIGHT166// STB_TEXTEDIT_K_LINESTART2 secondary keyboard input to move cursor to start of line167// STB_TEXTEDIT_K_LINEEND2 secondary keyboard input to move cursor to end of line168// STB_TEXTEDIT_K_TEXTSTART2 secondary keyboard input to move cursor to start of text169// STB_TEXTEDIT_K_TEXTEND2 secondary keyboard input to move cursor to end of text170//171// Keyboard input must be encoded as a single integer value; e.g. a character code172// and some bitflags that represent shift states. to simplify the interface, SHIFT must173// be a bitflag, so we can test the shifted state of cursor movements to allow selection,

Merge branch 'fix-textedit-typo' of https://github.com/mastensg/stb into workingSean Barrettbefore adoptiona5cbacd

line 174why, one step back
174// i.e. (STB_TEXTEDIT_K_RIGHT|STB_TEXTEDIT_K_SHIFT) should be shifted right-arrow.

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 175–215why, one step back
175//
176// You can encode other things, such as CONTROL or ALT, in additional bits, and177// then test for their presence in e.g. STB_TEXTEDIT_K_WORDLEFT. For example,178// my Windows implementations add an additional CONTROL bit, and an additional KEYDOWN179// bit. Then all of the STB_TEXTEDIT_K_ values bitwise-or in the KEYDOWN bit,180// and I pass both WM_KEYDOWN and WM_CHAR events to the "key" function in the181// API below. The control keys will only match WM_KEYDOWN events because of the182// keydown bit I add, and STB_TEXTEDIT_KEYTOTEXT only tests for the KEYDOWN183// bit so it only decodes WM_CHAR events.184//185// STB_TEXTEDIT_LAYOUTROW returns information about the shape of one displayed186// row of characters assuming they start on the i'th character--the width and187// the height and the number of characters consumed. This allows this library188// to traverse the entire layout incrementally. You need to compute word-wrapping189// here.190//191// Each textfield keeps its own insert mode state, which is not how normal192// applications work. To keep an app-wide insert mode, update/copy the193// "insert_mode" field of STB_TexteditState before/after calling API functions.194//195// API196//197// void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line)198//199// void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)200// void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)201// int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)202// int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len)203// void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXEDIT_KEYTYPE key)204//205// Each of these functions potentially updates the string and updates the206// state.207//208// initialize_state:209// set the textedit state to a known good default state when initially210// constructing the textedit.211//212// click:213// call this with the mouse x,y on a mouse down; it will update the cursor214// and reset the selection start/end to the cursor point. the x,y must215// be relative to the text widget, with (0,0) being the top left.

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

line 216why, one step back
216//

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 217–219why, one step back
217// drag:
218// call this with the mouse x,y on a mouse drag/up; it will update the219// cursor and the selection end point

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

line 220why, one step back
220//

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 221–224why, one step back
221// cut:
222// call this to delete the current selection; returns true if there was223// one. you should FIRST copy the current selection to the system paste buffer.224// (To copy, just copy the current selection out of the string yourself.)

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

line 225why, one step back
225//

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 226–228why, one step back
226// paste:
227// call this to paste text at the current cursor point or over the current228// selection if there is one.

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

line 229why, one step back
229//

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 230–239why, one step back
230// key:
231// call this for keyboard inputs sent to the textfield. you can use it232// for "key down" events or for "translated" key events. if you need to233// do both (as in Win32), or distinguish Unicode characters from control234// inputs, set a high bit to distinguish the two; then you can define the235// various definitions like STB_TEXTEDIT_K_LEFT have the is-key-event bit236// set, and make STB_TEXTEDIT_KEYTOCHAR check that the is-key-event bit is237// clear. STB_TEXTEDIT_KEYTYPE defaults to int, but you can #define it to238// anything other type you wante before including.239//

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

line 240why, one step back
240//

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 241–333why, one step back
241// When rendering, you can read the cursor position and selection state from
242// the STB_TexteditState.243//244//245// Notes:246//247// This is designed to be usable in IMGUI, so it allows for the possibility of248// running in an IMGUI that has NOT cached the multi-line layout. For this249// reason, it provides an interface that is compatible with computing the250// layout incrementally--we try to make sure we make as few passes through251// as possible. (For example, to locate the mouse pointer in the text, we252// could define functions that return the X and Y positions of characters253// and binary search Y and then X, but if we're doing dynamic layout this254// will run the layout algorithm many times, so instead we manually search255// forward in one pass. Similar logic applies to e.g. up-arrow and256// down-arrow movement.)257//258// If it's run in a widget that *has* cached the layout, then this is less259// efficient, but it's not horrible on modern computers. But you wouldn't260// want to edit million-line files with it.261262263////////////////////////////////////////////////////////////////////////////264////////////////////////////////////////////////////////////////////////////265////266//// Header-file mode267////268////269270#ifndef INCLUDE_STB_TEXTEDIT_H271#define INCLUDE_STB_TEXTEDIT_H272273////////////////////////////////////////////////////////////////////////274//275// STB_TexteditState276//277// Definition of STB_TexteditState which you should store278// per-textfield; it includes cursor position, selection state,279// and undo state.280//281282#ifndef STB_TEXTEDIT_UNDOSTATECOUNT283#define STB_TEXTEDIT_UNDOSTATECOUNT 99284#endif285#ifndef STB_TEXTEDIT_UNDOCHARCOUNT286#define STB_TEXTEDIT_UNDOCHARCOUNT 999287#endif288#ifndef STB_TEXTEDIT_CHARTYPE289#define STB_TEXTEDIT_CHARTYPE int290#endif291#ifndef STB_TEXTEDIT_POSITIONTYPE292#define STB_TEXTEDIT_POSITIONTYPE int293#endif294295typedef struct296{297 // private data298 STB_TEXTEDIT_POSITIONTYPE where;299 STB_TEXTEDIT_POSITIONTYPE insert_length;300 STB_TEXTEDIT_POSITIONTYPE delete_length;301 int char_storage;302} StbUndoRecord;303304typedef struct305{306 // private data307 StbUndoRecord undo_rec [STB_TEXTEDIT_UNDOSTATECOUNT];308 STB_TEXTEDIT_CHARTYPE undo_char[STB_TEXTEDIT_UNDOCHARCOUNT];309 short undo_point, redo_point;310 int undo_char_point, redo_char_point;311} StbUndoState;312313typedef struct314{315 /////////////////////316 //317 // public data318 //319320 int cursor;321 // position of the text cursor within the string322323 int select_start; // selection start point324 int select_end;325 // selection start and end point in characters; if equal, no selection.326 // note that start may be less than or greater than end (e.g. when327 // dragging the mouse, start is where the initial click was, and you328 // can drag in either direction)329330 unsigned char insert_mode;331 // each textfield keeps its own insert mode state. to keep an app-wide332 // insert mode, copy this value in/out of the app state333

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

lines 334–337why, one step back
334 int row_count_per_page;
335 // page size in number of row.336 // this value MUST be set to >0 for pageup or pagedown in multilines documents.337

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 338–714why, one step back
338 /////////////////////
339 //340 // private data341 //342 unsigned char cursor_at_end_of_line; // not implemented yet343 unsigned char initialized;344 unsigned char has_preferred_x;345 unsigned char single_line;346 unsigned char padding1, padding2, padding3;347 float preferred_x; // this determines where the cursor up/down tries to seek to along x348 StbUndoState undostate;349} STB_TexteditState;350351352////////////////////////////////////////////////////////////////////////353//354// StbTexteditRow355//356// Result of layout query, used by stb_textedit to determine where357// the text in each row is.358359// result of layout query360typedef struct361{362 float x0,x1; // starting x location, end x location (allows for align=right, etc)363 float baseline_y_delta; // position of baseline relative to previous row's baseline364 float ymin,ymax; // height of row above and below baseline365 int num_chars;366} StbTexteditRow;367#endif //INCLUDE_STB_TEXTEDIT_H368369370////////////////////////////////////////////////////////////////////////////371////////////////////////////////////////////////////////////////////////////372////373//// Implementation mode374////375////376377378// implementation isn't include-guarded, since it might have indirectly379// included just the "header" portion380#ifdef STB_TEXTEDIT_IMPLEMENTATION381382#ifndef STB_TEXTEDIT_memmove383#include <string.h>384#define STB_TEXTEDIT_memmove memmove385#endif386387388/////////////////////////////////////////////////////////////////////////////389//390// Mouse input handling391//392393// traverse the layout to locate the nearest character to a display position394static int stb_text_locate_coord(STB_TEXTEDIT_STRING *str, float x, float y)395{396 StbTexteditRow r;397 int n = STB_TEXTEDIT_STRINGLEN(str);398 float base_y = 0, prev_x;399 int i=0, k;400401 r.x0 = r.x1 = 0;402 r.ymin = r.ymax = 0;403 r.num_chars = 0;404405 // search rows to find one that straddles 'y'406 while (i < n) {407 STB_TEXTEDIT_LAYOUTROW(&r, str, i);408 if (r.num_chars <= 0)409 return n;410411 if (i==0 && y < base_y + r.ymin)412 return 0;413414 if (y < base_y + r.ymax)415 break;416417 i += r.num_chars;418 base_y += r.baseline_y_delta;419 }420421 // below all text, return 'after' last character422 if (i >= n)423 return n;424425 // check if it's before the beginning of the line426 if (x < r.x0)427 return i;428429 // check if it's before the end of the line430 if (x < r.x1) {431 // search characters in row for one that straddles 'x'432 prev_x = r.x0;433 for (k=0; k < r.num_chars; ++k) {434 float w = STB_TEXTEDIT_GETWIDTH(str, i, k);435 if (x < prev_x+w) {436 if (x < prev_x+w/2)437 return k+i;438 else439 return k+i+1;440 }441 prev_x += w;442 }443 // shouldn't happen, but if it does, fall through to end-of-line case444 }445446 // if the last character is a newline, return that. otherwise return 'after' the last character447 if (STB_TEXTEDIT_GETCHAR(str, i+r.num_chars-1) == STB_TEXTEDIT_NEWLINE)448 return i+r.num_chars-1;449 else450 return i+r.num_chars;451}452453// API click: on mouse down, move the cursor to the clicked location, and reset the selection454static void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)455{456 // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse457 // goes off the top or bottom of the text458 if( state->single_line )459 {460 StbTexteditRow r;461 STB_TEXTEDIT_LAYOUTROW(&r, str, 0);462 y = r.ymin;463 }464465 state->cursor = stb_text_locate_coord(str, x, y);466 state->select_start = state->cursor;467 state->select_end = state->cursor;468 state->has_preferred_x = 0;469}470471// API drag: on mouse drag, move the cursor and selection endpoint to the clicked location472static void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)473{474 int p = 0;475476 // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse477 // goes off the top or bottom of the text478 if( state->single_line )479 {480 StbTexteditRow r;481 STB_TEXTEDIT_LAYOUTROW(&r, str, 0);482 y = r.ymin;483 }484485 if (state->select_start == state->select_end)486 state->select_start = state->cursor;487488 p = stb_text_locate_coord(str, x, y);489 state->cursor = state->select_end = p;490}491492/////////////////////////////////////////////////////////////////////////////493//494// Keyboard input handling495//496497// forward declarations498static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state);499static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state);500static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length);501static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length);502static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length);503504typedef struct505{506 float x,y; // position of n'th character507 float height; // height of line508 int first_char, length; // first char of row, and length509 int prev_first; // first char of previous row510} StbFindState;511512// find the x/y location of a character, and remember info about the previous row in513// case we get a move-up event (for page up, we'll have to rescan)514static void stb_textedit_find_charpos(StbFindState *find, STB_TEXTEDIT_STRING *str, int n, int single_line)515{516 StbTexteditRow r;517 int prev_start = 0;518 int z = STB_TEXTEDIT_STRINGLEN(str);519 int i=0, first;520521 if (n == z) {522 // if it's at the end, then find the last line -- simpler than trying to523 // explicitly handle this case in the regular code524 if (single_line) {525 STB_TEXTEDIT_LAYOUTROW(&r, str, 0);526 find->y = 0;527 find->first_char = 0;528 find->length = z;529 find->height = r.ymax - r.ymin;530 find->x = r.x1;531 } else {532 find->y = 0;533 find->x = 0;534 find->height = 1;535 while (i < z) {536 STB_TEXTEDIT_LAYOUTROW(&r, str, i);537 prev_start = i;538 i += r.num_chars;539 }540 find->first_char = i;541 find->length = 0;542 find->prev_first = prev_start;543 }544 return;545 }546547 // search rows to find the one that straddles character n548 find->y = 0;549550 for(;;) {551 STB_TEXTEDIT_LAYOUTROW(&r, str, i);552 if (n < i + r.num_chars)553 break;554 prev_start = i;555 i += r.num_chars;556 find->y += r.baseline_y_delta;557 }558559 find->first_char = first = i;560 find->length = r.num_chars;561 find->height = r.ymax - r.ymin;562 find->prev_first = prev_start;563564 // now scan to find xpos565 find->x = r.x0;566 for (i=0; first+i < n; ++i)567 find->x += STB_TEXTEDIT_GETWIDTH(str, first, i);568}569570#define STB_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end)571572// make the selection/cursor state valid if client altered the string573static void stb_textedit_clamp(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)574{575 int n = STB_TEXTEDIT_STRINGLEN(str);576 if (STB_TEXT_HAS_SELECTION(state)) {577 if (state->select_start > n) state->select_start = n;578 if (state->select_end > n) state->select_end = n;579 // if clamping forced them to be equal, move the cursor to match580 if (state->select_start == state->select_end)581 state->cursor = state->select_start;582 }583 if (state->cursor > n) state->cursor = n;584}585586// delete characters while updating undo587static void stb_textedit_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int len)588{589 stb_text_makeundo_delete(str, state, where, len);590 STB_TEXTEDIT_DELETECHARS(str, where, len);591 state->has_preferred_x = 0;592}593594// delete the section595static void stb_textedit_delete_selection(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)596{597 stb_textedit_clamp(str, state);598 if (STB_TEXT_HAS_SELECTION(state)) {599 if (state->select_start < state->select_end) {600 stb_textedit_delete(str, state, state->select_start, state->select_end - state->select_start);601 state->select_end = state->cursor = state->select_start;602 } else {603 stb_textedit_delete(str, state, state->select_end, state->select_start - state->select_end);604 state->select_start = state->cursor = state->select_end;605 }606 state->has_preferred_x = 0;607 }608}609610// canoncialize the selection so start <= end611static void stb_textedit_sortselection(STB_TexteditState *state)612{613 if (state->select_end < state->select_start) {614 int temp = state->select_end;615 state->select_end = state->select_start;616 state->select_start = temp;617 }618}619620// move cursor to first character of selection621static void stb_textedit_move_to_first(STB_TexteditState *state)622{623 if (STB_TEXT_HAS_SELECTION(state)) {624 stb_textedit_sortselection(state);625 state->cursor = state->select_start;626 state->select_end = state->select_start;627 state->has_preferred_x = 0;628 }629}630631// move cursor to last character of selection632static void stb_textedit_move_to_last(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)633{634 if (STB_TEXT_HAS_SELECTION(state)) {635 stb_textedit_sortselection(state);636 stb_textedit_clamp(str, state);637 state->cursor = state->select_end;638 state->select_start = state->select_end;639 state->has_preferred_x = 0;640 }641}642643#ifdef STB_TEXTEDIT_IS_SPACE644static int is_word_boundary( STB_TEXTEDIT_STRING *str, int idx )645{646 return idx > 0 ? (STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str,idx-1) ) && !STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str, idx) ) ) : 1;647}648649#ifndef STB_TEXTEDIT_MOVEWORDLEFT650static int stb_textedit_move_to_word_previous( STB_TEXTEDIT_STRING *str, int c )651{652 --c; // always move at least one character653 while( c >= 0 && !is_word_boundary( str, c ) )654 --c;655656 if( c < 0 )657 c = 0;658659 return c;660}661#define STB_TEXTEDIT_MOVEWORDLEFT stb_textedit_move_to_word_previous662#endif663664#ifndef STB_TEXTEDIT_MOVEWORDRIGHT665static int stb_textedit_move_to_word_next( STB_TEXTEDIT_STRING *str, int c )666{667 const int len = STB_TEXTEDIT_STRINGLEN(str);668 ++c; // always move at least one character669 while( c < len && !is_word_boundary( str, c ) )670 ++c;671672 if( c > len )673 c = len;674675 return c;676}677#define STB_TEXTEDIT_MOVEWORDRIGHT stb_textedit_move_to_word_next678#endif679680#endif681682// update selection and cursor to match each other683static void stb_textedit_prep_selection_at_cursor(STB_TexteditState *state)684{685 if (!STB_TEXT_HAS_SELECTION(state))686 state->select_start = state->select_end = state->cursor;687 else688 state->cursor = state->select_end;689}690691// API cut: delete selection692static int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)693{694 if (STB_TEXT_HAS_SELECTION(state)) {695 stb_textedit_delete_selection(str,state); // implicitly clamps696 state->has_preferred_x = 0;697 return 1;698 }699 return 0;700}701702// API paste: replace existing selection with passed-in text703static int stb_textedit_paste_internal(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len)704{705 // if there's a selection, the paste should delete it706 stb_textedit_clamp(str, state);707 stb_textedit_delete_selection(str,state);708 // try to insert the characters709 if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, len)) {710 stb_text_makeundo_insert(state, state->cursor, len);711 state->cursor += len;712 state->has_preferred_x = 0;713 return 1;714 }

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

line 715why, one step back
715 // note: paste failure will leave deleted selection, may be restored with an undo (see https://github.com/nothings/stb/issues/734 for details)

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 716–760why, one step back
716 return 0;
717}718719#ifndef STB_TEXTEDIT_KEYTYPE720#define STB_TEXTEDIT_KEYTYPE int721#endif722723// API key: process a keyboard input724static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_KEYTYPE key)725{726retry:727 switch (key) {728 default: {729 int c = STB_TEXTEDIT_KEYTOTEXT(key);730 if (c > 0) {731 STB_TEXTEDIT_CHARTYPE ch = (STB_TEXTEDIT_CHARTYPE) c;732733 // can't add newline in single-line mode734 if (c == '\n' && state->single_line)735 break;736737 if (state->insert_mode && !STB_TEXT_HAS_SELECTION(state) && state->cursor < STB_TEXTEDIT_STRINGLEN(str)) {738 stb_text_makeundo_replace(str, state, state->cursor, 1, 1);739 STB_TEXTEDIT_DELETECHARS(str, state->cursor, 1);740 if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) {741 ++state->cursor;742 state->has_preferred_x = 0;743 }744 } else {745 stb_textedit_delete_selection(str,state); // implicitly clamps746 if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) {747 stb_text_makeundo_insert(state, state->cursor, 1);748 ++state->cursor;749 state->has_preferred_x = 0;750 }751 }752 }753 break;754 }755756#ifdef STB_TEXTEDIT_K_INSERT757 case STB_TEXTEDIT_K_INSERT:758 state->insert_mode = !state->insert_mode;759 break;760#endif

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

line 761why, one step back
761

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 762–775why, one step back
762 case STB_TEXTEDIT_K_UNDO:
763 stb_text_undo(str, state);764 state->has_preferred_x = 0;765 break;766767 case STB_TEXTEDIT_K_REDO:768 stb_text_redo(str, state);769 state->has_preferred_x = 0;770 break;771772 case STB_TEXTEDIT_K_LEFT:773 // if currently there's a selection, move cursor to start of selection774 if (STB_TEXT_HAS_SELECTION(state))775 stb_textedit_move_to_first(state);

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

line 776why, one step back
776 else

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 777–824why, one step back
777 if (state->cursor > 0)
778 --state->cursor;779 state->has_preferred_x = 0;780 break;781782 case STB_TEXTEDIT_K_RIGHT:783 // if currently there's a selection, move cursor to end of selection784 if (STB_TEXT_HAS_SELECTION(state))785 stb_textedit_move_to_last(str, state);786 else787 ++state->cursor;788 stb_textedit_clamp(str, state);789 state->has_preferred_x = 0;790 break;791792 case STB_TEXTEDIT_K_LEFT | STB_TEXTEDIT_K_SHIFT:793 stb_textedit_clamp(str, state);794 stb_textedit_prep_selection_at_cursor(state);795 // move selection left796 if (state->select_end > 0)797 --state->select_end;798 state->cursor = state->select_end;799 state->has_preferred_x = 0;800 break;801802#ifdef STB_TEXTEDIT_MOVEWORDLEFT803 case STB_TEXTEDIT_K_WORDLEFT:804 if (STB_TEXT_HAS_SELECTION(state))805 stb_textedit_move_to_first(state);806 else {807 state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor);808 stb_textedit_clamp( str, state );809 }810 break;811812 case STB_TEXTEDIT_K_WORDLEFT | STB_TEXTEDIT_K_SHIFT:813 if( !STB_TEXT_HAS_SELECTION( state ) )814 stb_textedit_prep_selection_at_cursor(state);815816 state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor);817 state->select_end = state->cursor;818819 stb_textedit_clamp( str, state );820 break;821#endif822823#ifdef STB_TEXTEDIT_MOVEWORDRIGHT824 case STB_TEXTEDIT_K_WORDRIGHT:

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

line 825why, one step back
825 if (STB_TEXT_HAS_SELECTION(state))

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 826–853why, one step back
826 stb_textedit_move_to_last(str, state);
827 else {828 state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor);829 stb_textedit_clamp( str, state );830 }831 break;832833 case STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT:834 if( !STB_TEXT_HAS_SELECTION( state ) )835 stb_textedit_prep_selection_at_cursor(state);836837 state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor);838 state->select_end = state->cursor;839840 stb_textedit_clamp( str, state );841 break;842#endif843844 case STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT:845 stb_textedit_prep_selection_at_cursor(state);846 // move selection right847 ++state->select_end;848 stb_textedit_clamp(str, state);849 state->cursor = state->select_end;850 state->has_preferred_x = 0;851 break;852853 case STB_TEXTEDIT_K_DOWN:

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

lines 854–856why, one step back
854 case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT:
855 case STB_TEXTEDIT_K_PGDOWN:856 case STB_TEXTEDIT_K_PGDOWN | STB_TEXTEDIT_K_SHIFT: {

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 857–858why, one step back
857 StbFindState find;
858 StbTexteditRow row;

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

lines 859–861why, one step back
859 int i, j, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;
860 int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGDOWN;861 int row_count = is_page ? state->row_count_per_page : 1;

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

line 862why, one step back
862

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

line 863why, one step back
863 if (!is_page && state->single_line) {

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 864–871why, one step back
864 // on windows, up&down in single-line behave like left&right
865 key = STB_TEXTEDIT_K_RIGHT | (key & STB_TEXTEDIT_K_SHIFT);866 goto retry;867 }868869 if (sel)870 stb_textedit_prep_selection_at_cursor(state);871 else if (STB_TEXT_HAS_SELECTION(state))

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

line 872why, one step back
872 stb_textedit_move_to_last(str, state);

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 873–877why, one step back
873
874 // compute current position of cursor point875 stb_textedit_clamp(str, state);876 stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);877

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

lines 878–879why, one step back
878 for (j = 0; j < row_count; ++j) {
879 float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x;

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

line 880why, one step back
880 int start = find.first_char + find.length;

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

lines 881–885why, one step back
881
882 if (find.length == 0)883 break;884885 // now find character position down a row

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 886–906why, one step back
886 state->cursor = start;
887 STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);888 x = row.x0;889 for (i=0; i < row.num_chars; ++i) {890 float dx = STB_TEXTEDIT_GETWIDTH(str, start, i);891 #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE892 if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE)893 break;894 #endif895 x += dx;896 if (x > goal_x)897 break;898 ++state->cursor;899 }900 stb_textedit_clamp(str, state);901902 state->has_preferred_x = 1;903 state->preferred_x = goal_x;904905 if (sel)906 state->select_end = state->cursor;

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

lines 907–910why, one step back
907
908 // go to next line909 find.first_char = find.first_char + find.length;910 find.length = row.num_chars;

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 911–913why, one step back
911 }
912 break;913 }

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

line 914why, one step back
914

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

line 915why, one step back
915 case STB_TEXTEDIT_K_UP:

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

lines 916–918why, one step back
916 case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT:
917 case STB_TEXTEDIT_K_PGUP:918 case STB_TEXTEDIT_K_PGUP | STB_TEXTEDIT_K_SHIFT: {

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 919–920why, one step back
919 StbFindState find;
920 StbTexteditRow row;

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

lines 921–923why, one step back
921 int i, j, prev_scan, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;
922 int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGUP;923 int row_count = is_page ? state->row_count_per_page : 1;

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

line 924why, one step back
924

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

line 925why, one step back
925 if (!is_page && state->single_line) {

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 926–939why, one step back
926 // on windows, up&down become left&right
927 key = STB_TEXTEDIT_K_LEFT | (key & STB_TEXTEDIT_K_SHIFT);928 goto retry;929 }930931 if (sel)932 stb_textedit_prep_selection_at_cursor(state);933 else if (STB_TEXT_HAS_SELECTION(state))934 stb_textedit_move_to_first(state);935936 // compute current position of cursor point937 stb_textedit_clamp(str, state);938 stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);939

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

lines 940–946why, one step back
940 for (j = 0; j < row_count; ++j) {
941 float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x;942943 // can only go up if there's a previous row944 if (find.prev_first == find.first_char)945 break;946

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 947–968why, one step back
947 // now find character position up a row
948 state->cursor = find.prev_first;949 STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);950 x = row.x0;951 for (i=0; i < row.num_chars; ++i) {952 float dx = STB_TEXTEDIT_GETWIDTH(str, find.prev_first, i);953 #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE954 if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE)955 break;956 #endif957 x += dx;958 if (x > goal_x)959 break;960 ++state->cursor;961 }962 stb_textedit_clamp(str, state);963964 state->has_preferred_x = 1;965 state->preferred_x = goal_x;966967 if (sel)968 state->select_end = state->cursor;

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

lines 969–976why, one step back
969
970 // go to previous line971 // (we need to scan previous line the hard way. maybe we could expose this as a new API function?)972 prev_scan = find.prev_first > 0 ? find.prev_first - 1 : 0;973 while (prev_scan > 0 && STB_TEXTEDIT_GETCHAR(str, prev_scan - 1) != STB_TEXTEDIT_NEWLINE)974 --prev_scan;975 find.first_char = find.prev_first;976 find.prev_first = prev_scan;

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 977–1005why, one step back
977 }
978 break;979 }980981 case STB_TEXTEDIT_K_DELETE:982 case STB_TEXTEDIT_K_DELETE | STB_TEXTEDIT_K_SHIFT:983 if (STB_TEXT_HAS_SELECTION(state))984 stb_textedit_delete_selection(str, state);985 else {986 int n = STB_TEXTEDIT_STRINGLEN(str);987 if (state->cursor < n)988 stb_textedit_delete(str, state, state->cursor, 1);989 }990 state->has_preferred_x = 0;991 break;992993 case STB_TEXTEDIT_K_BACKSPACE:994 case STB_TEXTEDIT_K_BACKSPACE | STB_TEXTEDIT_K_SHIFT:995 if (STB_TEXT_HAS_SELECTION(state))996 stb_textedit_delete_selection(str, state);997 else {998 stb_textedit_clamp(str, state);999 if (state->cursor > 0) {1000 stb_textedit_delete(str, state, state->cursor-1, 1);1001 --state->cursor;1002 }1003 }1004 state->has_preferred_x = 0;1005 break;

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

line 1006why, one step back
1006

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 1007–1022why, one step back
1007#ifdef STB_TEXTEDIT_K_TEXTSTART2
1008 case STB_TEXTEDIT_K_TEXTSTART2:1009#endif1010 case STB_TEXTEDIT_K_TEXTSTART:1011 state->cursor = state->select_start = state->select_end = 0;1012 state->has_preferred_x = 0;1013 break;10141015#ifdef STB_TEXTEDIT_K_TEXTEND21016 case STB_TEXTEDIT_K_TEXTEND2:1017#endif1018 case STB_TEXTEDIT_K_TEXTEND:1019 state->cursor = STB_TEXTEDIT_STRINGLEN(str);1020 state->select_start = state->select_end = 0;1021 state->has_preferred_x = 0;1022 break;

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

line 1023why, one step back
1023

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 1024–1363why, one step back
1024#ifdef STB_TEXTEDIT_K_TEXTSTART2
1025 case STB_TEXTEDIT_K_TEXTSTART2 | STB_TEXTEDIT_K_SHIFT:1026#endif1027 case STB_TEXTEDIT_K_TEXTSTART | STB_TEXTEDIT_K_SHIFT:1028 stb_textedit_prep_selection_at_cursor(state);1029 state->cursor = state->select_end = 0;1030 state->has_preferred_x = 0;1031 break;10321033#ifdef STB_TEXTEDIT_K_TEXTEND21034 case STB_TEXTEDIT_K_TEXTEND2 | STB_TEXTEDIT_K_SHIFT:1035#endif1036 case STB_TEXTEDIT_K_TEXTEND | STB_TEXTEDIT_K_SHIFT:1037 stb_textedit_prep_selection_at_cursor(state);1038 state->cursor = state->select_end = STB_TEXTEDIT_STRINGLEN(str);1039 state->has_preferred_x = 0;1040 break;104110421043#ifdef STB_TEXTEDIT_K_LINESTART21044 case STB_TEXTEDIT_K_LINESTART2:1045#endif1046 case STB_TEXTEDIT_K_LINESTART:1047 stb_textedit_clamp(str, state);1048 stb_textedit_move_to_first(state);1049 if (state->single_line)1050 state->cursor = 0;1051 else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE)1052 --state->cursor;1053 state->has_preferred_x = 0;1054 break;10551056#ifdef STB_TEXTEDIT_K_LINEEND21057 case STB_TEXTEDIT_K_LINEEND2:1058#endif1059 case STB_TEXTEDIT_K_LINEEND: {1060 int n = STB_TEXTEDIT_STRINGLEN(str);1061 stb_textedit_clamp(str, state);1062 stb_textedit_move_to_first(state);1063 if (state->single_line)1064 state->cursor = n;1065 else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE)1066 ++state->cursor;1067 state->has_preferred_x = 0;1068 break;1069 }10701071#ifdef STB_TEXTEDIT_K_LINESTART21072 case STB_TEXTEDIT_K_LINESTART2 | STB_TEXTEDIT_K_SHIFT:1073#endif1074 case STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT:1075 stb_textedit_clamp(str, state);1076 stb_textedit_prep_selection_at_cursor(state);1077 if (state->single_line)1078 state->cursor = 0;1079 else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE)1080 --state->cursor;1081 state->select_end = state->cursor;1082 state->has_preferred_x = 0;1083 break;10841085#ifdef STB_TEXTEDIT_K_LINEEND21086 case STB_TEXTEDIT_K_LINEEND2 | STB_TEXTEDIT_K_SHIFT:1087#endif1088 case STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT: {1089 int n = STB_TEXTEDIT_STRINGLEN(str);1090 stb_textedit_clamp(str, state);1091 stb_textedit_prep_selection_at_cursor(state);1092 if (state->single_line)1093 state->cursor = n;1094 else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE)1095 ++state->cursor;1096 state->select_end = state->cursor;1097 state->has_preferred_x = 0;1098 break;1099 }1100 }1101}11021103/////////////////////////////////////////////////////////////////////////////1104//1105// Undo processing1106//1107// @OPTIMIZE: the undo/redo buffer should be circular11081109static void stb_textedit_flush_redo(StbUndoState *state)1110{1111 state->redo_point = STB_TEXTEDIT_UNDOSTATECOUNT;1112 state->redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT;1113}11141115// discard the oldest entry in the undo list1116static void stb_textedit_discard_undo(StbUndoState *state)1117{1118 if (state->undo_point > 0) {1119 // if the 0th undo state has characters, clean those up1120 if (state->undo_rec[0].char_storage >= 0) {1121 int n = state->undo_rec[0].insert_length, i;1122 // delete n characters from all other records1123 state->undo_char_point -= n;1124 STB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(STB_TEXTEDIT_CHARTYPE)));1125 for (i=0; i < state->undo_point; ++i)1126 if (state->undo_rec[i].char_storage >= 0)1127 state->undo_rec[i].char_storage -= n; // @OPTIMIZE: get rid of char_storage and infer it1128 }1129 --state->undo_point;1130 STB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0])));1131 }1132}11331134// discard the oldest entry in the redo list--it's bad if this1135// ever happens, but because undo & redo have to store the actual1136// characters in different cases, the redo character buffer can1137// fill up even though the undo buffer didn't1138static void stb_textedit_discard_redo(StbUndoState *state)1139{1140 int k = STB_TEXTEDIT_UNDOSTATECOUNT-1;11411142 if (state->redo_point <= k) {1143 // if the k'th undo state has characters, clean those up1144 if (state->undo_rec[k].char_storage >= 0) {1145 int n = state->undo_rec[k].insert_length, i;1146 // move the remaining redo character data to the end of the buffer1147 state->redo_char_point += n;1148 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)));1149 // adjust the position of all the other records to account for above memmove1150 for (i=state->redo_point; i < k; ++i)1151 if (state->undo_rec[i].char_storage >= 0)1152 state->undo_rec[i].char_storage += n;1153 }1154 // now move all the redo records towards the end of the buffer; the first one is at 'redo_point'1155 STB_TEXTEDIT_memmove(state->undo_rec + state->redo_point+1, state->undo_rec + state->redo_point, (size_t) ((STB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point)*sizeof(state->undo_rec[0])));1156 // now move redo_point to point to the new one1157 ++state->redo_point;1158 }1159}11601161static StbUndoRecord *stb_text_create_undo_record(StbUndoState *state, int numchars)1162{1163 // any time we create a new undo record, we discard redo1164 stb_textedit_flush_redo(state);11651166 // if we have no free records, we have to make room, by sliding the1167 // existing records down1168 if (state->undo_point == STB_TEXTEDIT_UNDOSTATECOUNT)1169 stb_textedit_discard_undo(state);11701171 // if the characters to store won't possibly fit in the buffer, we can't undo1172 if (numchars > STB_TEXTEDIT_UNDOCHARCOUNT) {1173 state->undo_point = 0;1174 state->undo_char_point = 0;1175 return NULL;1176 }11771178 // if we don't have enough free characters in the buffer, we have to make room1179 while (state->undo_char_point + numchars > STB_TEXTEDIT_UNDOCHARCOUNT)1180 stb_textedit_discard_undo(state);11811182 return &state->undo_rec[state->undo_point++];1183}11841185static STB_TEXTEDIT_CHARTYPE *stb_text_createundo(StbUndoState *state, int pos, int insert_len, int delete_len)1186{1187 StbUndoRecord *r = stb_text_create_undo_record(state, insert_len);1188 if (r == NULL)1189 return NULL;11901191 r->where = pos;1192 r->insert_length = (STB_TEXTEDIT_POSITIONTYPE) insert_len;1193 r->delete_length = (STB_TEXTEDIT_POSITIONTYPE) delete_len;11941195 if (insert_len == 0) {1196 r->char_storage = -1;1197 return NULL;1198 } else {1199 r->char_storage = state->undo_char_point;1200 state->undo_char_point += insert_len;1201 return &state->undo_char[r->char_storage];1202 }1203}12041205static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)1206{1207 StbUndoState *s = &state->undostate;1208 StbUndoRecord u, *r;1209 if (s->undo_point == 0)1210 return;12111212 // we need to do two things: apply the undo record, and create a redo record1213 u = s->undo_rec[s->undo_point-1];1214 r = &s->undo_rec[s->redo_point-1];1215 r->char_storage = -1;12161217 r->insert_length = u.delete_length;1218 r->delete_length = u.insert_length;1219 r->where = u.where;12201221 if (u.delete_length) {1222 // if the undo record says to delete characters, then the redo record will1223 // need to re-insert the characters that get deleted, so we need to store1224 // them.12251226 // there are three cases:1227 // there's enough room to store the characters1228 // characters stored for *redoing* don't leave room for redo1229 // characters stored for *undoing* don't leave room for redo1230 // if the last is true, we have to bail12311232 if (s->undo_char_point + u.delete_length >= STB_TEXTEDIT_UNDOCHARCOUNT) {1233 // the undo records take up too much character space; there's no space to store the redo characters1234 r->insert_length = 0;1235 } else {1236 int i;12371238 // there's definitely room to store the characters eventually1239 while (s->undo_char_point + u.delete_length > s->redo_char_point) {1240 // should never happen:1241 if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT)1242 return;1243 // there's currently not enough room, so discard a redo record1244 stb_textedit_discard_redo(s);1245 }1246 r = &s->undo_rec[s->redo_point-1];12471248 r->char_storage = s->redo_char_point - u.delete_length;1249 s->redo_char_point = s->redo_char_point - u.delete_length;12501251 // now save the characters1252 for (i=0; i < u.delete_length; ++i)1253 s->undo_char[r->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u.where + i);1254 }12551256 // now we can carry out the deletion1257 STB_TEXTEDIT_DELETECHARS(str, u.where, u.delete_length);1258 }12591260 // check type of recorded action:1261 if (u.insert_length) {1262 // easy case: was a deletion, so we need to insert n characters1263 STB_TEXTEDIT_INSERTCHARS(str, u.where, &s->undo_char[u.char_storage], u.insert_length);1264 s->undo_char_point -= u.insert_length;1265 }12661267 state->cursor = u.where + u.insert_length;12681269 s->undo_point--;1270 s->redo_point--;1271}12721273static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)1274{1275 StbUndoState *s = &state->undostate;1276 StbUndoRecord *u, r;1277 if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT)1278 return;12791280 // we need to do two things: apply the redo record, and create an undo record1281 u = &s->undo_rec[s->undo_point];1282 r = s->undo_rec[s->redo_point];12831284 // we KNOW there must be room for the undo record, because the redo record1285 // was derived from an undo record12861287 u->delete_length = r.insert_length;1288 u->insert_length = r.delete_length;1289 u->where = r.where;1290 u->char_storage = -1;12911292 if (r.delete_length) {1293 // the redo record requires us to delete characters, so the undo record1294 // needs to store the characters12951296 if (s->undo_char_point + u->insert_length > s->redo_char_point) {1297 u->insert_length = 0;1298 u->delete_length = 0;1299 } else {1300 int i;1301 u->char_storage = s->undo_char_point;1302 s->undo_char_point = s->undo_char_point + u->insert_length;13031304 // now save the characters1305 for (i=0; i < u->insert_length; ++i)1306 s->undo_char[u->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u->where + i);1307 }13081309 STB_TEXTEDIT_DELETECHARS(str, r.where, r.delete_length);1310 }13111312 if (r.insert_length) {1313 // easy case: need to insert n characters1314 STB_TEXTEDIT_INSERTCHARS(str, r.where, &s->undo_char[r.char_storage], r.insert_length);1315 s->redo_char_point += r.insert_length;1316 }13171318 state->cursor = r.where + r.insert_length;13191320 s->undo_point++;1321 s->redo_point++;1322}13231324static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length)1325{1326 stb_text_createundo(&state->undostate, where, 0, length);1327}13281329static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length)1330{1331 int i;1332 STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, length, 0);1333 if (p) {1334 for (i=0; i < length; ++i)1335 p[i] = STB_TEXTEDIT_GETCHAR(str, where+i);1336 }1337}13381339static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length)1340{1341 int i;1342 STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, old_length, new_length);1343 if (p) {1344 for (i=0; i < old_length; ++i)1345 p[i] = STB_TEXTEDIT_GETCHAR(str, where+i);1346 }1347}13481349// reset the state to default1350static void stb_textedit_clear_state(STB_TexteditState *state, int is_single_line)1351{1352 state->undostate.undo_point = 0;1353 state->undostate.undo_char_point = 0;1354 state->undostate.redo_point = STB_TEXTEDIT_UNDOSTATECOUNT;1355 state->undostate.redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT;1356 state->select_end = state->select_start = 0;1357 state->cursor = 0;1358 state->has_preferred_x = 0;1359 state->preferred_x = 0;1360 state->cursor_at_end_of_line = 0;1361 state->initialized = 1;1362 state->single_line = (unsigned char) is_single_line;1363 state->insert_mode = 0;

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

line 1364why, one step back
1364 state->row_count_per_page = 0;

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 1365–1394why, one step back
1365}
13661367// API initialize1368static void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line)1369{1370 stb_textedit_clear_state(state, is_single_line);1371}13721373#if defined(__GNUC__) || defined(__clang__)1374#pragma GCC diagnostic push1375#pragma GCC diagnostic ignored "-Wcast-qual"1376#endif13771378static int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE const *ctext, int len)1379{1380 return stb_textedit_paste_internal(str, state, (STB_TEXTEDIT_CHARTYPE *) ctext, len);1381}13821383#if defined(__GNUC__) || defined(__clang__)1384#pragma GCC diagnostic pop1385#endif13861387#endif//STB_TEXTEDIT_IMPLEMENTATION13881389/*1390------------------------------------------------------------------------------1391This software is available under 2 licenses -- choose whichever you prefer.1392------------------------------------------------------------------------------1393ALTERNATIVE A - MIT License1394Copyright (c) 2017 Sean Barrett

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

lines 1395–1399why, one step back
1395Permission is hereby granted, free of charge, to any person obtaining a copy of
1396this software and associated documentation files (the "Software"), to deal in1397the Software without restriction, including without limitation the rights to1398use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies1399of the Software, and to permit persons to whom the Software is furnished to do

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

line 1400why, one step back
1400so, subject to the following conditions:

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

line 1401why, one step back
1401The above copyright notice and this permission notice shall be included in all

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

line 1402why, one step back
1402copies or substantial portions of the Software.

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

lines 1403–1408why, one step back
1403THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1404IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,1405FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE1406AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER1407LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,1408OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 1409–1412why, one step back
1409SOFTWARE.
1410------------------------------------------------------------------------------1411ALTERNATIVE B - Public Domain (www.unlicense.org)1412This is free and unencumbered software released into the public domain.

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

lines 1413–1414why, one step back
1413Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
1414software, either in source code form or as a compiled binary, for any purpose,

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

line 1415why, one step back
1415commercial or non-commercial, and by any means.

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

lines 1416–1420why, one step back
1416In jurisdictions that recognize copyright laws, the author or authors of this
1417software dedicate any and all copyright interest in the software to the public1418domain. We make this dedication for the benefit of the public at large and to1419the detriment of our heirs and successors. We intend this dedication to be an1420overt act of relinquishment in perpetuity of all present and future rights to

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

line 1421why, one step back
1421this software under copyright law.

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

lines 1422–1426why, one step back
1422THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1423IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,1424FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE1425AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN1426ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 1427–1429why, one step back
1427WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1428------------------------------------------------------------------------------1429*/