imstb_textedit.h 52 KB

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