Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1404 lines
52 KiB

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