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


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21