You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1393 lines
51 KiB

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