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