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