imstb_textedit.h 56 KB

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