imstb_textedit.h 52 KB

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