-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimstb_textedit.go
1293 lines (1146 loc) · 38 KB
/
imstb_textedit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package imgui
// stb_textedit internally allows for a single undo record to do addition and deletion, but somehow, calling
// the stb_textedit_paste() function creates two separate records, so we perform it manually. (FIXME: Report to nothings/stb?)
func stb_textedit_replace(str *ImGuiInputTextState, state *STB_TexteditState, text []STB_TEXTEDIT_CHARTYPE, text_len int) {
stb_text_makeundo_replace(str, state, 0, str.CurLenW, text_len)
STB_TEXTEDIT_DELETECHARS(str, 0, str.CurLenW)
if text_len <= 0 {
return
}
if STB_TEXTEDIT_INSERTCHARS(str, 0, text, text_len) {
state.cursor = text_len
state.has_preferred_x = 0
return
}
IM_ASSERT(false) // Failed to insert character, normally shouldn't happen because of how we currently use stb_textedit_replace()
}
type short = int16
type (
STB_TEXTEDIT_STRING = ImGuiInputTextState
STB_TEXTEDIT_CHARTYPE = ImWchar
)
const (
STB_TEXTEDIT_NEWLINE = '\n'
STB_TEXTEDIT_GETWIDTH_NEWLINE = -1
STB_TEXTEDIT_UNDOSTATECOUNT = 99
STB_TEXTEDIT_UNDOCHARCOUNT = 999
)
func STB_TEXTEDIT_STRINGLEN(obj *STB_TEXTEDIT_STRING) int {
return obj.CurLenW
}
func STB_TEXTEDIT_LAYOUTROW(r *StbTexteditRow, obj *STB_TEXTEDIT_STRING, line_start_idx int) {
var text = obj.TextW
var text_remaining []ImWchar
var size = InputTextCalcTextSizeW(text[line_start_idx:obj.CurLenW], &text_remaining, nil, true)
r.x0 = 0.0
r.x1 = size.x
r.baseline_y_delta = size.y
r.ymin = 0.0
r.ymax = size.y
r.num_chars = int(len(text) - len(text[line_start_idx:]))
}
func STB_TEXTEDIT_GETWIDTH(obj *STB_TEXTEDIT_STRING, line_start_idx, char_idx int) float {
var c = obj.TextW[line_start_idx+char_idx]
if c == '\n' {
return STB_TEXTEDIT_GETWIDTH_NEWLINE
}
var g = GImGui
return g.Font.GetCharAdvance(c) * (g.FontSize / g.Font.FontSize)
}
func STB_TEXTEDIT_GETCHAR(obj *STB_TEXTEDIT_STRING, idx int) STB_TEXTEDIT_CHARTYPE {
return obj.TextW[idx]
}
func STB_TEXTEDIT_DELETECHARS(obj *STB_TEXTEDIT_STRING, pos, n int) {
var dst = obj.TextW[pos:]
// We maintain our buffer length in both UTF-8 and wchar formats
obj.Edited = true
obj.CurLenA -= ImTextCountUtf8BytesFromStr(dst, dst[n:])
obj.CurLenW -= n
// Offset remaining text (FIXME-OPT: Use memmove)
src := obj.TextW[pos+n:]
copy(dst, src)
}
func STB_TEXTEDIT_INSERTCHARS(obj *STB_TEXTEDIT_STRING, pos int, new_text []STB_TEXTEDIT_CHARTYPE, new_text_len int) bool {
var is_resizable = (obj.Flags & ImGuiInputTextFlags_CallbackResize) != 0
var text_len = obj.CurLenW
IM_ASSERT(pos <= text_len)
var new_text_len_utf8 = ImTextCountUtf8BytesFromStr(new_text, new_text[new_text_len:])
if !is_resizable && (new_text_len_utf8+obj.CurLenA+1 > obj.BufCapacityA) {
return false
}
// Grow internal buffer if needed
if new_text_len+text_len+1 > int(len(obj.TextW)) {
if !is_resizable {
return false
}
IM_ASSERT(text_len < int(len(obj.TextW)))
obj.TextW = append(obj.TextW, make([]rune, text_len+ImClampInt(new_text_len*4, 32, ImMaxInt(256, new_text_len))+1)...)
}
var text = obj.TextW
if pos != text_len {
copy(text[pos+new_text_len:], text[pos:text_len])
}
copy(text[pos:], new_text[new_text_len:])
obj.Edited = true
obj.CurLenW += new_text_len
obj.CurLenA += new_text_len_utf8
return true
}
func STB_TEXTEDIT_KEYTOTEXT(key STB_TEXTEDIT_KEYTYPE) STB_TEXTEDIT_CHARTYPE {
// if i understand everything correctly, it just converts it into a rune
return STB_TEXTEDIT_CHARTYPE(key)
}
func is_separator(c rune) bool {
return ImCharIsBlankW(c) || c == ',' || c == ';' || c == '(' || c == ')' || c == '{' || c == '}' || c == '[' || c == ']' || c == '|'
}
func is_word_boundary_from_right(obj *ImGuiInputTextState, idx int) bool {
if (obj.Flags & ImGuiInputTextFlags_Password) != 0 {
return false
}
if idx > 0 {
return is_separator(obj.TextW[idx-1]) && !is_separator(obj.TextW[idx])
}
return true
}
func STB_TEXTEDIT_MOVEWORDLEFT(obj *STB_TEXTEDIT_STRING, idx int) int {
idx--
for idx >= 0 && !is_word_boundary_from_right(obj, idx) {
idx--
}
if idx < 0 {
return 0
}
return idx
}
func STB_TEXTEDIT_MOVEWORDRIGHT(obj *STB_TEXTEDIT_STRING, idx int) int {
idx++
var len = obj.CurLenW
for idx < len && !is_word_boundary_from_right(obj, idx) {
idx++
}
if idx > len {
return len
}
return idx
}
func STB_TEXTEDIT_IS_SPACE(char STB_TEXTEDIT_CHARTYPE) bool {
return char == ' ' || char == '\t'
}
type (
STB_TEXTEDIT_POSITIONTYPE = int
)
type StbUndoRecord struct {
// private data
where STB_TEXTEDIT_POSITIONTYPE
insert_length STB_TEXTEDIT_POSITIONTYPE
delete_length STB_TEXTEDIT_POSITIONTYPE
char_storage int
}
type StbUndoState struct {
// private data
undo_rec [STB_TEXTEDIT_UNDOSTATECOUNT]StbUndoRecord
undo_char [STB_TEXTEDIT_UNDOCHARCOUNT]STB_TEXTEDIT_CHARTYPE
undo_point, redo_point short
undo_char_point, redo_char_point int
}
const STB_TEXTEDIT_K_SHIFT = 0x400000
const (
STB_TEXTEDIT_K_LEFT = 0x200000 + iota
STB_TEXTEDIT_K_RIGHT
STB_TEXTEDIT_K_UP
STB_TEXTEDIT_K_DOWN
STB_TEXTEDIT_K_PGUP
STB_TEXTEDIT_K_PGDOWN
STB_TEXTEDIT_K_LINESTART
STB_TEXTEDIT_K_LINEEND
STB_TEXTEDIT_K_TEXTSTART
STB_TEXTEDIT_K_TEXTEND
STB_TEXTEDIT_K_DELETE
STB_TEXTEDIT_K_BACKSPACE
STB_TEXTEDIT_K_UNDO
STB_TEXTEDIT_K_REDO
STB_TEXTEDIT_K_INSERT
STB_TEXTEDIT_K_WORDLEFT
STB_TEXTEDIT_K_WORDRIGHT
STB_TEXTEDIT_K_LINESTART2
STB_TEXTEDIT_K_LINEEND2
STB_TEXTEDIT_K_TEXTSTART2
STB_TEXTEDIT_K_TEXTEND2
)
type STB_TexteditState struct {
/////////////////////
//
// public data
//
cursor int
// position of the text cursor within the string
select_start int // selection start point
select_end int
// selection start and end point in characters; if equal, no selection.
// note that start may be less than or greater than end (e.g. when
// dragging the mouse, start is where the initial click was, and you
// can drag in either direction)
insert_mode byte
// each textfield keeps its own insert mode state. to keep an app-wide
// insert mode, copy this value in/out of the app state
row_count_per_page int
// page size in number of row.
// this value MUST be set to >0 for pageup or pagedown in multilines documents.
/////////////////////
//
// private data
//
cursor_at_end_of_line byte // not implemented yet
initialized byte
has_preferred_x byte
single_line byte
padding1, padding2, padding3 byte
preferred_x float // this determines where the cursor up/down tries to seek to along x
undostate StbUndoState
}
// StbTexteditRow //////////////////////////////////////////////////////////////////////
// Result of layout query, used by stb_textedit to determine where
// the text in each row is.
// result of layout query
type StbTexteditRow struct {
x0, x1 float // starting x location, end x location (allows for align=right, etc)
baseline_y_delta float // position of baseline relative to previous row's baseline
ymin, ymax float // height of row above and below baseline
num_chars int
}
// traverse the layout to locate the nearest character to a display position
func stb_text_locate_coord(str *STB_TEXTEDIT_STRING, x, y float) int {
var r StbTexteditRow
var n = STB_TEXTEDIT_STRINGLEN(str)
var base_y, prev_x float
var i, k int
r.x0 = 0
r.x1 = 0
r.ymin = 0
r.ymax = 0
r.num_chars = 0
// search rows to find one that straddles 'y'
for i < n {
STB_TEXTEDIT_LAYOUTROW(&r, str, i)
if r.num_chars <= 0 {
return n
}
if i == 0 && y < base_y+r.ymin {
return 0
}
if y < base_y+r.ymax {
break
}
i += r.num_chars
base_y += r.baseline_y_delta
}
// below all text, return 'after' last character
if i >= n {
return n
}
// check if it's before the beginning of the line
if x < r.x0 {
return i
}
// check if it's before the end of the line
if x < r.x1 {
// search characters in row for one that straddles 'x'
prev_x = r.x0
for k = 0; k < r.num_chars; k++ {
var w = STB_TEXTEDIT_GETWIDTH(str, i, k)
if x < prev_x+w {
if x < prev_x+w/2 {
return k + i
} else {
return k + i + 1
}
}
prev_x += w
}
// shouldn't happen, but if it does, fall through to end-of-line case
}
// if the last character is a newline, return that. otherwise return 'after' the last character
if STB_TEXTEDIT_GETCHAR(str, i+r.num_chars-1) == STB_TEXTEDIT_NEWLINE {
return i + r.num_chars - 1
} else {
return i + r.num_chars
}
}
// API click: on mouse down, move the cursor to the clicked location, and reset the selection
func stb_textedit_click(str *STB_TEXTEDIT_STRING, state *STB_TexteditState, x, y float) {
// In single-line mode, just always make y = 0. This lets the drag keep working if the mouse
// goes off the top or bottom of the text
if state.single_line != 0 {
var r StbTexteditRow
STB_TEXTEDIT_LAYOUTROW(&r, str, 0)
y = r.ymin
}
state.cursor = stb_text_locate_coord(str, x, y)
state.select_start = state.cursor
state.select_end = state.cursor
state.has_preferred_x = 0
}
// API drag: on mouse drag, move the cursor and selection endpoint to the clicked location
func stb_textedit_drag(str *STB_TEXTEDIT_STRING, state *STB_TexteditState, x, y float) {
var p int = 0
// In single-line mode, just always make y = 0. This lets the drag keep working if the mouse
// goes off the top or bottom of the text
if state.single_line != 0 {
var r StbTexteditRow
STB_TEXTEDIT_LAYOUTROW(&r, str, 0)
y = r.ymin
}
if state.select_start == state.select_end {
state.select_start = state.cursor
}
p = stb_text_locate_coord(str, x, y)
state.cursor = p
state.select_end = p
}
func stb_text_undo(str *STB_TEXTEDIT_STRING, state *STB_TexteditState) {
var s = &state.undostate
var u StbUndoRecord
var r *StbUndoRecord
if s.undo_point == 0 {
return
}
// we need to do two things: apply the undo record, and create a redo record
u = s.undo_rec[s.undo_point-1]
r = &s.undo_rec[s.redo_point-1]
r.char_storage = -1
r.insert_length = u.delete_length
r.delete_length = u.insert_length
r.where = u.where
if u.delete_length != 0 {
// if the undo record says to delete characters, then the redo record will
// need to re-insert the characters that get deleted, so we need to store
// them.
// there are three cases:
// there's enough room to store the characters
// characters stored for *redoing* don't leave room for redo
// characters stored for *undoing* don't leave room for redo
// if the last is true, we have to bail
if s.undo_char_point+u.delete_length >= STB_TEXTEDIT_UNDOCHARCOUNT {
// the undo records take up too much character space; there's no space to store the redo characters
r.insert_length = 0
} else {
var i int
// there's definitely room to store the characters eventually
for s.undo_char_point+u.delete_length > s.redo_char_point {
// should never happen:
if s.redo_point == STB_TEXTEDIT_UNDOSTATECOUNT {
return
}
// there's currently not enough room, so discard a redo record
stb_textedit_discard_redo(s)
}
r = &s.undo_rec[s.redo_point-1]
r.char_storage = s.redo_char_point - u.delete_length
s.redo_char_point = s.redo_char_point - u.delete_length
// now save the characters
for i = 0; i < u.delete_length; i++ {
s.undo_char[r.char_storage+i] = STB_TEXTEDIT_GETCHAR(str, u.where+i)
}
}
// now we can carry out the deletion
STB_TEXTEDIT_DELETECHARS(str, u.where, u.delete_length)
}
// check type of recorded action:
if u.insert_length != 0 {
// easy case: was a deletion, so we need to insert n characters
STB_TEXTEDIT_INSERTCHARS(str, u.where, s.undo_char[u.char_storage:], u.insert_length)
s.undo_char_point -= u.insert_length
}
state.cursor = u.where + u.insert_length
s.undo_point--
s.redo_point--
}
func stb_text_redo(str *STB_TEXTEDIT_STRING, state *STB_TexteditState) {
var s = &state.undostate
var u *StbUndoRecord
var r StbUndoRecord
if s.redo_point == STB_TEXTEDIT_UNDOSTATECOUNT {
return
}
// we need to do two things: apply the redo record, and create an undo record
u = &s.undo_rec[s.undo_point]
r = s.undo_rec[s.redo_point]
// we KNOW there must be room for the undo record, because the redo record
// was derived from an undo record
u.delete_length = r.insert_length
u.insert_length = r.delete_length
u.where = r.where
u.char_storage = -1
if r.delete_length != 0 {
// the redo record requires us to delete characters, so the undo record
// needs to store the characters
if s.undo_char_point+u.insert_length > s.redo_char_point {
u.insert_length = 0
u.delete_length = 0
} else {
var i int
u.char_storage = s.undo_char_point
s.undo_char_point = s.undo_char_point + u.insert_length
// now save the characters
for i = 0; i < u.insert_length; i++ {
s.undo_char[u.char_storage+i] = STB_TEXTEDIT_GETCHAR(str, u.where+i)
}
}
STB_TEXTEDIT_DELETECHARS(str, r.where, r.delete_length)
}
if r.insert_length != 0 {
// easy case: need to insert n characters
STB_TEXTEDIT_INSERTCHARS(str, r.where, s.undo_char[r.char_storage:], r.insert_length)
s.redo_char_point += r.insert_length
}
state.cursor = r.where + r.insert_length
s.undo_point++
s.redo_point++
}
func stb_text_makeundo_delete(str *STB_TEXTEDIT_STRING, state *STB_TexteditState, where, length int) {
var i int
var p = stb_text_createundo(&state.undostate, where, length, 0)
if p != nil {
for i = 0; i < length; i++ {
p[i] = STB_TEXTEDIT_GETCHAR(str, where+i)
}
}
}
func stb_text_makeundo_insert(state *STB_TexteditState, where, length int) {
stb_text_createundo(&state.undostate, where, 0, length)
}
func stb_text_makeundo_replace(str *STB_TEXTEDIT_STRING, state *STB_TexteditState, where, old_length, new_length int) {
var i int
var p = stb_text_createundo(&state.undostate, where, old_length, new_length)
if p != nil {
for i = 0; i < old_length; i++ {
p[i] = STB_TEXTEDIT_GETCHAR(str, where+i)
}
}
}
type StbFindState struct {
x, y float // position of n'th character
height float // height of line
first_char, length int // first char of row, and length
prev_first int // first char of previous row
}
// find the x/y location of a character, and remember info about the previous row in
// case we get a move-up event (for page up, we'll have to rescan)
func stb_textedit_find_charpos(find *StbFindState, str *STB_TEXTEDIT_STRING, n, single_line int) {
var r StbTexteditRow
var prev_start int = 0
var z = STB_TEXTEDIT_STRINGLEN(str)
var i, first int
if n == z {
// if it's at the end, then find the last line -- simpler than trying to
// explicitly handle this case in the regular code
if single_line != 0 {
STB_TEXTEDIT_LAYOUTROW(&r, str, 0)
find.y = 0
find.first_char = 0
find.length = z
find.height = r.ymax - r.ymin
find.x = r.x1
} else {
find.y = 0
find.x = 0
find.height = 1
for i < z {
STB_TEXTEDIT_LAYOUTROW(&r, str, i)
prev_start = i
i += r.num_chars
}
find.first_char = i
find.length = 0
find.prev_first = prev_start
}
return
}
// search rows to find the one that straddles character n
find.y = 0
for {
STB_TEXTEDIT_LAYOUTROW(&r, str, i)
if n < i+r.num_chars {
break
}
prev_start = i
i += r.num_chars
find.y += r.baseline_y_delta
}
find.first_char = i
first = i
find.length = r.num_chars
find.height = r.ymax - r.ymin
find.prev_first = prev_start
// now scan to find xpos
find.x = r.x0
for i = 0; first+i < n; i++ {
find.x += STB_TEXTEDIT_GETWIDTH(str, first, i)
}
}
func STB_TEXT_HAS_SELECTION(s *STB_TexteditState) bool {
return ((s).select_start != (s).select_end)
}
// make the selection/cursor state valid if client altered the string
func stb_textedit_clamp(str *STB_TEXTEDIT_STRING, state *STB_TexteditState) {
var n = STB_TEXTEDIT_STRINGLEN(str)
if STB_TEXT_HAS_SELECTION(state) {
if state.select_start > n {
state.select_start = n
}
if state.select_end > n {
state.select_end = n
}
// if clamping forced them to be equal, move the cursor to match
if state.select_start == state.select_end {
state.cursor = state.select_start
}
}
if state.cursor > n {
state.cursor = n
}
}
// delete characters while updating undo
func stb_textedit_delete(str *STB_TEXTEDIT_STRING, state *STB_TexteditState, where, len int) {
stb_text_makeundo_delete(str, state, where, len)
STB_TEXTEDIT_DELETECHARS(str, where, len)
state.has_preferred_x = 0
}
// delete the section
func stb_textedit_delete_selection(str *STB_TEXTEDIT_STRING, state *STB_TexteditState) {
stb_textedit_clamp(str, state)
if STB_TEXT_HAS_SELECTION(state) {
if state.select_start < state.select_end {
stb_textedit_delete(str, state, state.select_start, state.select_end-state.select_start)
state.select_end = state.select_start
state.cursor = state.select_start
} else {
stb_textedit_delete(str, state, state.select_end, state.select_start-state.select_end)
state.select_start = state.select_end
state.cursor = state.select_end
}
state.has_preferred_x = 0
}
}
// canoncialize the selection so start <= end
func stb_textedit_sortselection(state *STB_TexteditState) {
if state.select_end < state.select_start {
var temp = state.select_end
state.select_end = state.select_start
state.select_start = temp
}
}
// move cursor to first character of selection
func stb_textedit_move_to_first(state *STB_TexteditState) {
if STB_TEXT_HAS_SELECTION(state) {
stb_textedit_sortselection(state)
state.cursor = state.select_start
state.select_end = state.select_start
state.has_preferred_x = 0
}
}
// move cursor to last character of selection
func stb_textedit_move_to_last(str *STB_TEXTEDIT_STRING, state *STB_TexteditState) {
if STB_TEXT_HAS_SELECTION(state) {
stb_textedit_sortselection(state)
stb_textedit_clamp(str, state)
state.cursor = state.select_end
state.select_start = state.select_end
state.has_preferred_x = 0
}
}
func is_word_boundary(str *STB_TEXTEDIT_STRING, idx int) int {
if idx > 0 {
return bool2int(STB_TEXTEDIT_IS_SPACE(STB_TEXTEDIT_GETCHAR(str, idx-1)) && !STB_TEXTEDIT_IS_SPACE(STB_TEXTEDIT_GETCHAR(str, idx)))
}
return 1
}
func stb_textedit_move_to_word_previous(str *STB_TEXTEDIT_STRING, c int) int {
c-- // always move at least one character
for c >= 0 && is_word_boundary(str, c) == 0 {
c--
}
if c < 0 {
c = 0
}
return c
}
func stb_textedit_move_to_word_next(str *STB_TEXTEDIT_STRING, c int) int {
var len = STB_TEXTEDIT_STRINGLEN(str)
c++ // always move at least one character
for c < len && is_word_boundary(str, c) == 0 {
c++
}
if c > len {
c = len
}
return c
}
// update selection and cursor to match each other
func stb_textedit_prep_selection_at_cursor(state *STB_TexteditState) {
if !STB_TEXT_HAS_SELECTION(state) {
state.select_start = state.cursor
state.select_end = state.cursor
} else {
state.cursor = state.select_end
}
}
// API cut: delete selection
func stb_textedit_cut(str *STB_TEXTEDIT_STRING, state *STB_TexteditState) int {
if STB_TEXT_HAS_SELECTION(state) {
stb_textedit_delete_selection(str, state) // implicitly clamps
state.has_preferred_x = 0
return 1
}
return 0
}
// API paste: replace existing selection with passed-in text
func stb_textedit_paste_internal(str *STB_TEXTEDIT_STRING, state *STB_TexteditState, text []STB_TEXTEDIT_CHARTYPE, len int) int {
// if there's a selection, the paste should delete it
stb_textedit_clamp(str, state)
stb_textedit_delete_selection(str, state)
// try to insert the characters
if STB_TEXTEDIT_INSERTCHARS(str, state.cursor, text, len) {
stb_text_makeundo_insert(state, state.cursor, len)
state.cursor += len
state.has_preferred_x = 0
return 1
}
// [DEAR IMGUI]
//// remove the undo since we didn't actually insert the characters
//if (state.undostate.undo_point)
// --state.undostate.undo_point;
// note: paste failure will leave deleted selection, may be restored with an undo (see https://github.com/nothings/stb/issues/734 for details)
return 0
}
type STB_TEXTEDIT_KEYTYPE int
// API key: process a keyboard input
func stb_textedit_key(str *STB_TEXTEDIT_STRING, state *STB_TexteditState, key STB_TEXTEDIT_KEYTYPE) {
retry:
switch key {
default:
{
var c = int(STB_TEXTEDIT_KEYTOTEXT(key))
if c > 0 {
var ch = [1]STB_TEXTEDIT_CHARTYPE{(STB_TEXTEDIT_CHARTYPE)(c)}
// can't add newline in single-line mode
if c == '\n' && state.single_line != 0 {
break
}
if state.insert_mode != 0 && !STB_TEXT_HAS_SELECTION(state) && state.cursor < STB_TEXTEDIT_STRINGLEN(str) {
stb_text_makeundo_replace(str, state, state.cursor, 1, 1)
STB_TEXTEDIT_DELETECHARS(str, state.cursor, 1)
if STB_TEXTEDIT_INSERTCHARS(str, state.cursor, ch[:], 1) {
state.cursor++
state.has_preferred_x = 0
}
} else {
stb_textedit_delete_selection(str, state) // implicitly clamps
if STB_TEXTEDIT_INSERTCHARS(str, state.cursor, ch[:], 1) {
stb_text_makeundo_insert(state, state.cursor, 1)
state.cursor++
state.has_preferred_x = 0
}
}
}
break
}
case STB_TEXTEDIT_K_INSERT:
state.insert_mode = byte(bool2int(state.insert_mode == 0))
case STB_TEXTEDIT_K_UNDO:
stb_text_undo(str, state)
state.has_preferred_x = 0
case STB_TEXTEDIT_K_REDO:
stb_text_redo(str, state)
state.has_preferred_x = 0
case STB_TEXTEDIT_K_LEFT:
// if currently there's a selection, move cursor to start of selection
if STB_TEXT_HAS_SELECTION(state) {
stb_textedit_move_to_first(state)
} else {
if state.cursor > 0 {
state.cursor--
}
}
state.has_preferred_x = 0
case STB_TEXTEDIT_K_RIGHT:
// if currently there's a selection, move cursor to end of selection
if STB_TEXT_HAS_SELECTION(state) {
stb_textedit_move_to_last(str, state)
} else {
state.cursor++
}
stb_textedit_clamp(str, state)
state.has_preferred_x = 0
case STB_TEXTEDIT_K_LEFT | STB_TEXTEDIT_K_SHIFT:
stb_textedit_clamp(str, state)
stb_textedit_prep_selection_at_cursor(state)
// move selection left
if state.select_end > 0 {
state.select_end--
}
state.cursor = state.select_end
state.has_preferred_x = 0
case STB_TEXTEDIT_K_WORDLEFT:
if STB_TEXT_HAS_SELECTION(state) {
stb_textedit_move_to_first(state)
} else {
state.cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state.cursor)
stb_textedit_clamp(str, state)
}
case STB_TEXTEDIT_K_WORDLEFT | STB_TEXTEDIT_K_SHIFT:
if !STB_TEXT_HAS_SELECTION(state) {
stb_textedit_prep_selection_at_cursor(state)
}
state.cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state.cursor)
state.select_end = state.cursor
stb_textedit_clamp(str, state)
case STB_TEXTEDIT_K_WORDRIGHT:
if STB_TEXT_HAS_SELECTION(state) {
stb_textedit_move_to_last(str, state)
} else {
state.cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state.cursor)
stb_textedit_clamp(str, state)
}
case STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT:
if !STB_TEXT_HAS_SELECTION(state) {
stb_textedit_prep_selection_at_cursor(state)
}
state.cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state.cursor)
state.select_end = state.cursor
stb_textedit_clamp(str, state)
case STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT:
stb_textedit_prep_selection_at_cursor(state)
// move selection right
state.select_end++
stb_textedit_clamp(str, state)
state.cursor = state.select_end
state.has_preferred_x = 0
case STB_TEXTEDIT_K_DOWN:
fallthrough
case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT:
fallthrough
case STB_TEXTEDIT_K_PGDOWN:
fallthrough
case STB_TEXTEDIT_K_PGDOWN | STB_TEXTEDIT_K_SHIFT:
{
var find StbFindState
var row StbTexteditRow
var i, j, sel int = 0, 0, bool2int(key&STB_TEXTEDIT_K_SHIFT != 0)
var is_page = bool2int((key & ^STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGDOWN)
var row_count int
if is_page != 0 {
row_count = state.row_count_per_page
} else {
row_count = 1
}
if is_page == 0 && state.single_line != 0 {
// on windows, up&down in single-line behave like left&right
key = STB_TEXTEDIT_K_RIGHT | (key & STB_TEXTEDIT_K_SHIFT)
goto retry
}
if sel != 0 {
stb_textedit_prep_selection_at_cursor(state)
} else if STB_TEXT_HAS_SELECTION(state) {
stb_textedit_move_to_last(str, state)
}
// compute current position of cursor point
stb_textedit_clamp(str, state)
stb_textedit_find_charpos(&find, str, state.cursor, int(state.single_line))
for j = 0; j < row_count; j++ {
var x, goal_x float
if state.has_preferred_x != 0 {
goal_x = state.preferred_x
} else {
goal_x = find.x
}
var start = find.first_char + find.length
if find.length == 0 {
break
}
// [DEAR IMGUI]
// going down while being on the last line shouldn't bring us to that line end
if STB_TEXTEDIT_GETCHAR(str, find.first_char+find.length-1) != STB_TEXTEDIT_NEWLINE {
break
}
// now find character position down a row
state.cursor = start
STB_TEXTEDIT_LAYOUTROW(&row, str, state.cursor)
x = row.x0
for i = 0; i < row.num_chars; i++ {
var dx = STB_TEXTEDIT_GETWIDTH(str, start, i)
if dx == STB_TEXTEDIT_GETWIDTH_NEWLINE {
break
}
x += dx
if x > goal_x {
break
}
state.cursor++
}
stb_textedit_clamp(str, state)
state.has_preferred_x = 1
state.preferred_x = goal_x
if sel != 0x80 {
state.select_end = state.cursor
}
// go to next line
find.first_char = find.first_char + find.length
find.length = row.num_chars
}
break
}
case STB_TEXTEDIT_K_UP:
fallthrough
case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT:
fallthrough
case STB_TEXTEDIT_K_PGUP:
fallthrough
case STB_TEXTEDIT_K_PGUP | STB_TEXTEDIT_K_SHIFT:
{
var find StbFindState
var row StbTexteditRow
var i, j, prev_scan, sel int = 0, 0, 0, bool2int(key&STB_TEXTEDIT_K_SHIFT != 0)
var is_page = bool2int((key & ^STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGUP)
var row_count int
if is_page != 0 {
row_count = state.row_count_per_page
} else {
row_count = 1
}
if is_page == 0 && state.single_line != 0 {
// on windows, up&down become left&right
key = STB_TEXTEDIT_K_LEFT | (key & STB_TEXTEDIT_K_SHIFT)
goto retry
}
if sel != 0 {
stb_textedit_prep_selection_at_cursor(state)
} else if STB_TEXT_HAS_SELECTION(state) {
stb_textedit_move_to_first(state)
}
// compute current position of cursor point
stb_textedit_clamp(str, state)
stb_textedit_find_charpos(&find, str, state.cursor, int(state.single_line))
for j = 0; j < row_count; j++ {
var x, goal_x float
if state.has_preferred_x != 0 {
goal_x = state.preferred_x
} else {
goal_x = find.x
}
// can only go up if there's a previous row
if find.prev_first == find.first_char {
break
}
// now find character position up a row
state.cursor = find.prev_first
STB_TEXTEDIT_LAYOUTROW(&row, str, state.cursor)
x = row.x0
for i = 0; i < row.num_chars; i++ {
var dx = STB_TEXTEDIT_GETWIDTH(str, find.prev_first, i)
if dx == STB_TEXTEDIT_GETWIDTH_NEWLINE {
break
}
x += dx
if x > goal_x {
break
}
state.cursor++
}
stb_textedit_clamp(str, state)
state.has_preferred_x = 1
state.preferred_x = goal_x
if sel != 0 {
state.select_end = state.cursor
}
// go to previous line
// (we need to scan previous line the hard way. maybe we could expose this as a new API function?)
if find.prev_first > 0 {
prev_scan = find.prev_first - 1
} else {
prev_scan = 0
}
for prev_scan > 0 && STB_TEXTEDIT_GETCHAR(str, prev_scan-1) != STB_TEXTEDIT_NEWLINE {
prev_scan--
}
find.first_char = find.prev_first
find.prev_first = prev_scan
}
break
}
case STB_TEXTEDIT_K_DELETE: