stb_textedit.h 53 KB

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