imstb_textedit.h 59 KB

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