-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextbox.go
More file actions
1616 lines (1430 loc) · 65.9 KB
/
textbox.go
File metadata and controls
1616 lines (1430 loc) · 65.9 KB
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 consolizer
import (
"fmt"
"github.com/atotto/clipboard"
"github.com/supercom32/consolizer/memory"
"github.com/supercom32/consolizer/stringformat"
"math"
"strings"
"unicode"
"github.com/supercom32/consolizer/constants"
"github.com/supercom32/consolizer/types"
)
type TextboxInstanceType struct {
BaseControlInstanceType
}
type textboxType struct{}
var textbox textboxType
var Textboxes = memory.NewControlMemoryManager[types.TextboxEntryType]()
func AddTextbox(layerAlias string, textboxAlias string, styleEntry types.TuiStyleEntryType, xLocation int, yLocation int, width int, height int, isBorderDrawn bool) {
textboxEntry := types.NewTexboxEntry()
textboxEntry.Alias = textboxAlias
textboxEntry.StyleEntry = styleEntry
textboxEntry.XLocation = xLocation
textboxEntry.YLocation = yLocation
textboxEntry.Width = width
textboxEntry.Height = height
textboxEntry.IsBorderDrawn = isBorderDrawn
textboxEntry.TooltipAlias = stringformat.GetLastSortedUUID()
// Create associated tooltip (always created but disabled by default)
Tooltip.Add(layerAlias, textboxEntry.TooltipAlias, "", styleEntry,
textboxEntry.XLocation, textboxEntry.YLocation,
textboxEntry.Width, textboxEntry.Height,
textboxEntry.XLocation, textboxEntry.YLocation+textboxEntry.Height+1,
textboxEntry.Width, 3,
false, true, constants.DefaultTooltipHoverTime)
// Use the generic memory manager to add the textbox entry
Textboxes.Add(layerAlias, textboxAlias, &textboxEntry)
}
func GetTextbox(layerAlias string, textboxAlias string) *types.TextboxEntryType {
// Use the generic memory manager to retrieve the textbox entry
textboxEntry := Textboxes.Get(layerAlias, textboxAlias)
if textboxEntry == nil {
panic(fmt.Sprintf("The requested text with alias '%s' on layer '%s' could not be returned since it does not exist.", textboxAlias, layerAlias))
}
return textboxEntry
}
func IsTextboxExists(layerAlias string, textboxAlias string) bool {
// Use the generic memory manager to check existence
return Textboxes.Get(layerAlias, textboxAlias) != nil
}
func DeleteTextbox(layerAlias string, textboxAlias string) {
// Use the generic memory manager to remove the textbox entry
Textboxes.Remove(layerAlias, textboxAlias)
}
/*
DeleteAllTextboxesFromLayer allows you to delete all textboxes from a given layer. In addition, the following
information should be noted:
- All textboxes on the specified layer will be removed.
- All memory associated with the textboxes will be freed.
- The textboxes will be removed from the tab index if they were added.
*/
func DeleteAllTextboxesFromLayer(layerAlias string) {
// Retrieve all textboxes in the specified layer
textboxes := Textboxes.GetAllEntries(layerAlias)
// Loop through all entries and delete them
for _, textbox := range textboxes {
Textboxes.Remove(layerAlias, textbox.Alias) // Assuming textbox.Alias contains the alias
}
}
/*
GetTooltip retrieves the tooltip associated with this textbox and returns the textbox instance
for method chaining. In addition, the following information should be noted:
- The tooltip is automatically created when the textbox is added.
- Use the returned instance to continue chaining method calls.
- Follow the same pattern as other controls for consistency.
*/
func (shared *TextboxInstanceType) GetTooltip() *TextboxInstanceType {
// No need to retrieve the tooltip, just return self for chaining
return shared
}
// Add a helper method to set tooltip text
func (shared *TextboxInstanceType) SetTooltipText(text string) *TextboxInstanceType {
if Textboxes.IsExists(shared.layerAlias, shared.controlAlias) {
textboxEntry := Textboxes.Get(shared.layerAlias, shared.controlAlias)
var tooltipInstance TooltipInstanceType
tooltipInstance.layerAlias = shared.layerAlias
tooltipInstance.controlAlias = textboxEntry.TooltipAlias
tooltipInstance.SetTooltipValue(text)
}
return shared
}
// Add a helper method to enable/disable the tooltip
func (shared *TextboxInstanceType) EnableTooltip(enabled bool) *TextboxInstanceType {
if Textboxes.IsExists(shared.layerAlias, shared.controlAlias) {
textboxEntry := Textboxes.Get(shared.layerAlias, shared.controlAlias)
var tooltipInstance TooltipInstanceType
tooltipInstance.layerAlias = shared.layerAlias
tooltipInstance.controlAlias = textboxEntry.TooltipAlias
tooltipInstance.SetEnabled(enabled)
}
return shared
}
/*
SetText allows you to set the text for a textbox. If the textbox instance
no longer exists, then no operation takes place. In addition, the following
information should be noted:
- Text can be broke up into multiple lines by using the '\n' escape sequence.
*/
func (shared *TextboxInstanceType) SetText(text string) *TextboxInstanceType {
if Textboxes.IsExists(shared.layerAlias, shared.controlAlias) {
textData := strings.Split(text, "\n")
textboxEntry := Textboxes.Get(shared.layerAlias, shared.controlAlias)
for _, text := range textData {
textboxEntry.TextData = append(textboxEntry.TextData, stringformat.GetRunesFromString(text))
}
textbox.setTextboxMaxScrollBarValues(shared.layerAlias, shared.controlAlias)
}
return shared
}
/*
SetViewport allows you to set the current viewport for a textbox. If the textbox instance
no longer exists, then no operation takes place.
*/
func (shared *TextboxInstanceType) SetViewport(xLocation int, yLocation int) *TextboxInstanceType {
if Textboxes.IsExists(shared.layerAlias, shared.controlAlias) {
textboxEntry := Textboxes.Get(shared.layerAlias, shared.controlAlias)
textboxEntry.ViewportXLocation = xLocation
textboxEntry.ViewportYLocation = yLocation
}
return shared
}
/*
SetWordWrap allows you to enable or disable word wrapping for a textbox. If the textbox instance
no longer exists, then no operation takes place. In addition, the following information should be noted:
- When word wrapping is enabled, text will automatically wrap at word boundaries.
- The horizontal scrollbar will be hidden when word wrapping is enabled.
- Returns the textbox instance for method chaining.
*/
func (shared *TextboxInstanceType) SetWordWrap(enabled bool) *TextboxInstanceType {
if Textboxes.IsExists(shared.layerAlias, shared.controlAlias) {
textboxEntry := Textboxes.Get(shared.layerAlias, shared.controlAlias)
textboxEntry.IsWordWrapEnabled = enabled
// Update scrollbar visibility
if enabled {
// Hide horizontal scrollbar when word wrap is enabled
hScrollBarEntry := ScrollBars.Get(shared.layerAlias, textboxEntry.HorizontalScrollbarAlias)
if hScrollBarEntry != nil {
hScrollBarEntry.IsVisible = false
hScrollBarEntry.IsEnabled = false
}
} else {
// Show horizontal scrollbar when word wrap is disabled (if needed)
textbox.setTextboxMaxScrollBarValues(shared.layerAlias, shared.controlAlias)
}
}
return shared
}
/*
SetAutoIndent allows you to enable or disable auto-indentation for a textbox. If the textbox instance
no longer exists, then no operation takes place. In addition, the following information should be noted:
- When auto-indent is enabled, new lines will inherit the indentation of the previous line.
- Returns the textbox instance for method chaining.
*/
func (shared *TextboxInstanceType) SetAutoIndent(enabled bool) *TextboxInstanceType {
if Textboxes.IsExists(shared.layerAlias, shared.controlAlias) {
textboxEntry := Textboxes.Get(shared.layerAlias, shared.controlAlias)
textboxEntry.IsAutoIndentEnabled = enabled
}
return shared
}
/*
getTextboxClickCoordinates converts a cell ID to x and y coordinates within a textbox.
The conversion is based on the table width, where:
- x coordinate is calculated as cellId modulo tableWidth
- y coordinate is calculated as cellId divided by tableWidth (rounded down)
Parameters:
- cellId: The ID of the cell to convert to coordinates
- tableWidth: The width of the textbox table in cells
Returns:
- xLocation: The x coordinate of the cell
- yLocation: The y coordinate of the cell
This function is primarily used to determine cursor position from mouse clicks within a textbox.
*/
func (shared *textboxType) getTextboxClickCoordinates(cellId int, tableWidth int) (int, int) {
xLocation := cellId % tableWidth
yLocation := math.Floor(float64(cellId) / float64(tableWidth))
return xLocation, int(yLocation)
}
/*
insertCharacterUsingAbsoluteCoordinates allows you to insert a character at a specific position in a textbox. In addition,
the following information should be noted:
- The character is inserted at the specified x and y coordinates.
- The text after the insertion point is shifted right.
- The cursor position is updated to after the inserted character.
*/
func (shared *textboxType) insertCharacterUsingAbsoluteCoordinates(textboxEntry *types.TextboxEntryType, xLocation int, yLocation int, characterToInsert rune) {
stringDataSuffixAfterInsert := textboxEntry.TextData[yLocation][xLocation:len(textboxEntry.TextData[yLocation])]
textboxEntry.TextData[yLocation] = append([]rune{}, textboxEntry.TextData[yLocation][:xLocation]...)
textboxEntry.TextData[yLocation] = append(textboxEntry.TextData[yLocation][:xLocation], characterToInsert)
textboxEntry.TextData[yLocation] = append(textboxEntry.TextData[yLocation], stringDataSuffixAfterInsert...)
textboxEntry.CursorXLocation++
}
/*
backspaceCharacterUsingRelativeCoordinates allows you to delete the character before the cursor. In addition,
the following information should be noted:
- If at the beginning of a line, moves the cursor to the end of the previous line.
- If at the beginning of the first line, no action is taken.
- The cursor position is updated after the deletion.
*/
func (shared *textboxType) backspaceCharacterUsingRelativeCoordinates(textboxEntry *types.TextboxEntryType) {
// If nothing left to backspace, do nothing.
if textboxEntry.CursorXLocation == 0 && textboxEntry.CursorYLocation == 0 {
return
} else if textboxEntry.CursorXLocation == 0 {
// If at the beginning of a line, move cursor the previous line ending.
textboxEntry.CursorXLocation = len(textboxEntry.TextData[textboxEntry.CursorYLocation-1]) - 1
shared.moveRemainingTextToPreviousLine(textboxEntry, textboxEntry.CursorYLocation)
textboxEntry.CursorYLocation--
return
}
// Otherwise, just backspace a single character.
textboxEntry.CursorXLocation--
shared.deleteCharacterUsingAbsoluteCoordinates(textboxEntry, textboxEntry.CursorXLocation, textboxEntry.CursorYLocation)
}
/*
deleteCharacterUsingAbsoluteCoordinates allows you to delete a character at a specific position. In addition,
the following information should be noted:
- The character at the specified x and y coordinates is deleted.
- If at the end of a line, moves the next line up if possible.
- The cursor position is updated after the deletion.
*/
func (shared *textboxType) deleteCharacterUsingAbsoluteCoordinates(textboxEntry *types.TextboxEntryType, xLocation int, yLocation int) {
// If cursor yLocation is out of bounds, do nothing.
if yLocation >= len(textboxEntry.TextData) {
return
}
// If cursor xLocation is at/out of bounds, move previous line to current line and position.
if xLocation >= len(textboxEntry.TextData[yLocation])-1 {
if len(textboxEntry.TextData)-1 == yLocation && (len(textboxEntry.TextData[yLocation]) <= 1 || xLocation >= len(textboxEntry.TextData[yLocation])-1) {
return
}
// This is a complex operation that joins lines
textboxEntry.TextData[yLocation] = textboxEntry.TextData[yLocation][:len(textboxEntry.TextData[yLocation])-1]
textboxEntry.TextData[yLocation] = append(textboxEntry.TextData[yLocation], textboxEntry.TextData[yLocation+1]...)
copy(textboxEntry.TextData[yLocation+1:], textboxEntry.TextData[yLocation+2:])
textboxEntry.TextData = textboxEntry.TextData[:len(textboxEntry.TextData)-1]
return
}
// Record the character being deleted for undo
// Remove the current character.
stringDataSuffixAfterInsert := textboxEntry.TextData[yLocation][xLocation+1 : len(textboxEntry.TextData[yLocation])]
textboxEntry.TextData[yLocation] = append([]rune{}, textboxEntry.TextData[yLocation][:xLocation]...)
textboxEntry.TextData[yLocation] = append(textboxEntry.TextData[yLocation], stringDataSuffixAfterInsert...)
}
/*
moveRemainingTextToPreviousLine allows you to move text from the current line to the previous line. In addition,
the following information should be noted:
- If the cursor is at the beginning of a line, the text after the cursor is moved to the previous line.
- The cursor position is updated to the end of the previous line.
- The current line is removed if it becomes empty after the move.
*/
func (shared *textboxType) moveRemainingTextToPreviousLine(textboxEntry *types.TextboxEntryType, yLocation int) {
// If the only row of text or the cursor yLocation is out of bounds, then exit.
if len(textboxEntry.TextData) == 1 || yLocation >= len(textboxEntry.TextData) {
return
}
textboxEntry.TextData[yLocation-1] = textboxEntry.TextData[yLocation-1][:len(textboxEntry.TextData[yLocation-1])-1]
textboxEntry.TextData[yLocation-1] = append(textboxEntry.TextData[yLocation-1], textboxEntry.TextData[yLocation]...)
textboxEntry.TextData = shared.removeLine(textboxEntry.TextData, yLocation)
}
/*
removeLine allows you to remove a line from a textbox. In addition, the following
information should be noted:
- The line at the specified index is removed.
- The remaining lines are shifted up to fill the gap.
- Returns the modified text data array.
*/
func (shared *textboxType) removeLine(textData [][]rune, index int) [][]rune {
return append(textData[:index], textData[index+1:]...)
}
/*
insertLine allows you to insert a new line into a textbox. In addition, the following
information should be noted:
- A new line is inserted at the specified index.
- If the index is out of bounds, the line is appended to the end.
- Returns the modified text data array.
*/
func (shared *textboxType) insertLine(textData [][]rune, lineData []rune, index int) [][]rune {
// If the index provided is inbounds, insert the line data accordingly.
if index < len(textData) {
textData = append(textData[:index+1], textData[index:]...)
textData[index] = lineData
} else {
// Otherwise, append the line data to the end of the array.
textData = append(textData, []rune{' '})
}
return textData
}
/*
moveTextAfterCursorToNextLine allows you to move text after your cursor to a new line underneath it. In addition,
the following information should be noted:
- Creates a new line with a default space character.
- Copies all text after the cursor position to the new line.
- Truncates the current line at the cursor position.
- Updates the cursor position to the start of the new line.
- Maintains proper text formatting and cursor visibility.
*/
func (shared *textboxType) moveTextAfterCursorToNextLine(textboxEntry *types.TextboxEntryType, yLocation int) {
// Record operation for undo - this is a complex operation that splits a line
lineText := make([]rune, len(textboxEntry.TextData[yLocation]))
copy(lineText, textboxEntry.TextData[yLocation])
// Create a new line with our default ' ' rune.
textboxEntry.TextData = shared.insertLine(textboxEntry.TextData, []rune{' '}, yLocation+1)
// Copy everything past our cursor on the current line.
charactersToCopy := textboxEntry.TextData[textboxEntry.CursorYLocation][textboxEntry.CursorXLocation:]
copyOfCharacters := make([]rune, len(textboxEntry.TextData[textboxEntry.CursorYLocation][textboxEntry.CursorXLocation:]))
copy(copyOfCharacters, charactersToCopy)
// Make our current line = everything up to our cursor + ' ' ending.
textboxEntry.TextData[textboxEntry.CursorYLocation] = append(textboxEntry.TextData[textboxEntry.CursorYLocation][:textboxEntry.CursorXLocation], ' ')
// Move to the next line
textboxEntry.CursorYLocation++
textboxEntry.CursorXLocation = 0
// Apply auto-indentation if enabled
if textboxEntry.IsAutoIndentEnabled {
// Get the indentation from the previous line
indentation := []rune{}
for _, char := range textboxEntry.TextData[textboxEntry.CursorYLocation-1] {
if unicode.IsSpace(char) {
indentation = append(indentation, char)
} else {
break
}
}
// Apply the indentation to the new line
if len(indentation) > 0 {
// Create a new line with indentation + copied text
newLine := make([]rune, len(indentation)+len(copyOfCharacters))
copy(newLine, indentation)
copy(newLine[len(indentation):], copyOfCharacters)
textboxEntry.TextData[textboxEntry.CursorYLocation] = newLine
textboxEntry.CursorXLocation = len(indentation)
} else {
// No indentation, just use the copied text
textboxEntry.TextData[textboxEntry.CursorYLocation] = make([]rune, len(copyOfCharacters))
copy(textboxEntry.TextData[textboxEntry.CursorYLocation], copyOfCharacters)
}
} else {
// No auto-indent, just paste the copied text
textboxEntry.TextData[textboxEntry.CursorYLocation] = make([]rune, len(copyOfCharacters))
copy(textboxEntry.TextData[textboxEntry.CursorYLocation], copyOfCharacters)
}
}
/*
updateScrollbarBasedOnTextboxViewport allows you to update the scrollbar positions based on the current
viewport position of a textbox. In addition, the following information should be noted:
- Updates both horizontal and vertical scrollbars if they exist.
- Adjusts the scrollbar handle positions to match the viewport position.
- Ensures the scrollbars accurately reflect the current view of the textbox.
*/
func (shared *textboxType) updateScrollbarBasedOnTextboxViewport(layerAlias string, textboxAlias string) {
textboxEntry := Textboxes.Get(layerAlias, textboxAlias)
horizontalScrollbarEntry := ScrollBars.Get(layerAlias, textboxEntry.HorizontalScrollbarAlias)
horizontalScrollbarEntry.ScrollValue = textboxEntry.ViewportXLocation
scrollbar.computeScrollbarHandlePositionByScrollValue(layerAlias, textboxEntry.HorizontalScrollbarAlias)
verticalScrollbarEntry := ScrollBars.Get(layerAlias, textboxEntry.VerticalScrollbarAlias)
verticalScrollbarEntry.ScrollValue = textboxEntry.ViewportYLocation
scrollbar.computeScrollbarHandlePositionByScrollValue(layerAlias, textboxEntry.VerticalScrollbarAlias)
}
/*
getMaxHorizontalTextValue returns the maximum line length in a textbox. In addition,
the following information should be noted:
- Calculates the maximum width of any line in the textbox.
- Takes into account wide characters that take up multiple spaces.
- Used to determine horizontal scrollbar limits.
*/
func (shared *textboxType) getMaxHorizontalTextValue(layerAlias string, textboxAlias string) int {
textboxEntry := Textboxes.Get(layerAlias, textboxAlias)
maxHorizontalValue := 0
for _, currentLine := range textboxEntry.TextData {
lengthOfLine := stringformat.GetWidthOfRunesWhenPrinted(currentLine)
over := lengthOfLine - len(currentLine)
if lengthOfLine > maxHorizontalValue {
maxHorizontalValue = lengthOfLine - (over / 2)
}
}
return maxHorizontalValue
}
/*
setTextboxMaxScrollBarValues allows you to update the scrollbar limits based on text content. In addition,
the following information should be noted:
- Updates both horizontal and vertical scrollbar maximum values.
- Disables scrollbars if the content fits within the viewport.
- Ensures scrollbars accurately reflect the text dimensions.
*/
func (shared *textboxType) setTextboxMaxScrollBarValues(layerAlias string, textboxAlias string) {
textboxEntry := Textboxes.Get(layerAlias, textboxAlias)
maxVerticalValue := len(textboxEntry.TextData)
maxHorizontalValue := shared.getMaxHorizontalTextValue(layerAlias, textboxAlias)
hScrollBarEntry := ScrollBars.Get(layerAlias, textboxEntry.HorizontalScrollbarAlias)
vScrollBarEntry := ScrollBars.Get(layerAlias, textboxEntry.VerticalScrollbarAlias)
maxHorizontalValue = maxHorizontalValue - textboxEntry.Width
// If the max horizontal width is smaller than the textbox width, disable scrolling.
if maxHorizontalValue <= 0 {
maxHorizontalValue = 0
hScrollBarEntry.IsEnabled = false
hScrollBarEntry.IsVisible = false
} else {
hScrollBarEntry.IsEnabled = true
hScrollBarEntry.IsVisible = true
}
maxVerticalValue = maxVerticalValue - textboxEntry.Height
// If the max vertical height is smaller than the textbox height, disable scrolling.
if maxVerticalValue <= 0 {
maxVerticalValue = 0
vScrollBarEntry.IsEnabled = false
vScrollBarEntry.IsVisible = false
} else {
vScrollBarEntry.IsEnabled = true
vScrollBarEntry.IsVisible = true
}
hScrollBarEntry.MaxScrollValue = maxHorizontalValue
vScrollBarEntry.MaxScrollValue = maxVerticalValue
}
/*
AddTextbox allows you to add a text box to a text layer. Once called, an instance of your control is
returned which will allow you to read or manipulate the properties for it. The Style of the text box
will be determined by the style entry passed in. If you wish to remove a text box from a text
layer, simply call 'DeleteTextBox'. In addition, the following information should be noted:
- Text boxes are not drawn physically to the text layer provided. Instead
they are rendered to the terminal at the same time when the text layer is
rendered. This allows you to create text boxes without actually overwriting
the text layer data under it.
- If the text box to be drawn falls outside the range of the provided layer,
then only the visible portion of the text box will be drawn.
*/
func (shared *textboxType) AddTextbox(layerAlias string, textboxAlias string, styleEntry types.TuiStyleEntryType, xLocation int, yLocation int, width int, height int, isBorderDrawn bool) TextboxInstanceType {
newTextboxEntry := types.NewTexboxEntry()
newTextboxEntry.Alias = textboxAlias
newTextboxEntry.StyleEntry = styleEntry
newTextboxEntry.XLocation = xLocation
newTextboxEntry.YLocation = yLocation
newTextboxEntry.Width = width
newTextboxEntry.Height = height
newTextboxEntry.IsBorderDrawn = isBorderDrawn
newTextboxEntry.TooltipAlias = stringformat.GetLastSortedUUID()
tooltipInstance := Tooltip.Add(layerAlias, newTextboxEntry.TooltipAlias, "", styleEntry,
newTextboxEntry.XLocation, newTextboxEntry.YLocation,
newTextboxEntry.Width, newTextboxEntry.Height,
newTextboxEntry.XLocation, newTextboxEntry.YLocation+1,
newTextboxEntry.Width, newTextboxEntry.Height,
false, true, constants.DefaultTooltipHoverTime)
tooltipInstance.SetEnabled(false)
tooltipInstance.setParentControlAlias(textboxAlias)
// Use the generic memory manager to add the textbox entry
Textboxes.Add(layerAlias, textboxAlias, &newTextboxEntry)
textboxEntry := Textboxes.Get(layerAlias, textboxAlias)
textboxEntry.TextData = append(textboxEntry.TextData, stringformat.GetRunesFromString(" "))
textboxEntry.HorizontalScrollbarAlias = stringformat.GetLastSortedUUID()
textboxEntry.VerticalScrollbarAlias = stringformat.GetLastSortedUUID()
hScrollbarWidth := width
hScrollbarXLocation := xLocation
hScrollbarYLocation := yLocation + height
vScrollbarHeight := height
vScrollbarXLocation := xLocation + width
vScrollbarYLocation := yLocation
if isBorderDrawn == true {
hScrollbarYLocation++
hScrollbarXLocation--
vScrollbarXLocation++
vScrollbarYLocation--
hScrollbarWidth = hScrollbarWidth + 2
vScrollbarHeight = vScrollbarHeight + 2
}
scrollbar.Add(layerAlias, textboxEntry.HorizontalScrollbarAlias, styleEntry, hScrollbarXLocation, hScrollbarYLocation, hScrollbarWidth, 0, 0, 1, true)
scrollbar.Add(layerAlias, textboxEntry.VerticalScrollbarAlias, styleEntry, vScrollbarXLocation, vScrollbarYLocation, vScrollbarHeight, 0, 0, 1, false)
shared.setTextboxMaxScrollBarValues(layerAlias, textboxAlias)
var textboxInstance TextboxInstanceType
textboxInstance.layerAlias = layerAlias
textboxInstance.controlAlias = textboxAlias
textboxInstance.controlType = "textbox"
return textboxInstance
}
/*
DeleteTextbox allows you to remove a text box from a text layer. In addition,
the following information should be noted:
- If you attempt to delete a text box which does not exist, then the request
will simply be ignored.
*/
func (shared *textboxType) DeleteTextbox(layerAlias string, textboxAlias string) {
Textboxes.Remove(layerAlias, textboxAlias)
}
/*
DeleteAllTextboxes allows you to delete all textboxes on a given layer. In addition, the following
information should be noted:
- All textboxes on the specified layer will be removed.
- All memory associated with the textboxes will be freed.
- The textboxes will be removed from the tab index if they were added.
*/
func (shared *textboxType) DeleteAllTextboxes(layerAlias string) {
Textboxes.RemoveAll(layerAlias)
}
/*
drawTextboxesOnLayer allows you to draw all textboxes on a layer. In addition, the following
information should be noted:
- Iterates through all textboxes on the specified layer.
- Draws each textbox with its current content and style.
- Handles cursor and highlight rendering.
*/
func (shared *textboxType) drawTextboxesOnLayer(layerEntry types.LayerEntryType) {
layerAlias := layerEntry.LayerAlias
for _, currentTextBoxEntry := range Textboxes.GetAllEntries(layerAlias) {
shared.drawTextbox(&layerEntry, currentTextBoxEntry.Alias)
}
}
/*
drawTextbox allows you to draw a textbox on a given text layer. In addition, the following
information should be noted:
- Draws the textbox with its current content and style.
- Handles border drawing if enabled.
- Manages cursor and highlight rendering.
- Adjusts the viewport to show the correct portion of text.
*/
/*
wrapTextToWidth wraps text to fit within a specified width.
It breaks lines at word boundaries when possible.
*/
func (shared *textboxType) wrapTextToWidth(text [][]rune, width int) [][]rune {
if width <= 0 {
return text
}
result := make([][]rune, 0)
for _, line := range text {
if len(line) <= width {
// Line fits, no wrapping needed
result = append(result, line)
continue
}
// Line needs wrapping
currentPos := 0
for currentPos < len(line) {
// Determine end position for this segment
endPos := currentPos + width
if endPos > len(line) {
endPos = len(line)
} else {
// Try to break at word boundary
for endPos > currentPos && !unicode.IsSpace(line[endPos-1]) {
// Look for a space to break at
foundSpace := false
for i := endPos - 1; i > currentPos && i > endPos-width/4; i-- {
if unicode.IsSpace(line[i]) {
endPos = i + 1 // Break after the space
foundSpace = true
break
}
}
if foundSpace {
break
} else {
// If no good break point, just use the full width
endPos = currentPos + width
if endPos > len(line) {
endPos = len(line)
}
break
}
}
}
// Add this segment as a new line
segment := make([]rune, endPos-currentPos)
copy(segment, line[currentPos:endPos])
result = append(result, segment)
// Move to next segment, skipping spaces at the beginning
currentPos = endPos
for currentPos < len(line) && unicode.IsSpace(line[currentPos]) {
currentPos++
}
}
}
return result
}
func (shared *textboxType) drawTextbox(layerEntry *types.LayerEntryType, textboxAlias string) {
t := Textboxes.Get(layerEntry.LayerAlias, textboxAlias)
attributeEntry := types.NewAttributeEntry()
attributeEntry.ForegroundColor = t.StyleEntry.Textbox.ForegroundColor
attributeEntry.BackgroundColor = t.StyleEntry.Textbox.BackgroundColor
attributeEntry.CellControlAlias = textboxAlias
if t.IsBorderDrawn {
drawBorder(layerEntry, t.StyleEntry, attributeEntry, t.XLocation-1, t.YLocation-1, t.Width+2, t.Height+2, false)
}
attributeEntry.CellType = constants.CellTypeTextbox
fillArea(layerEntry, attributeEntry, " ", t.XLocation, t.YLocation, t.Width, t.Height, t.ViewportYLocation)
attributeEntry.CellControlAlias = textboxAlias
// Apply word wrapping if enabled
var displayText [][]rune
if t.IsWordWrapEnabled {
displayText = shared.wrapTextToWidth(t.TextData, t.Width)
} else {
displayText = t.TextData
}
for currentLine := 0; currentLine < t.Height; currentLine++ {
var arrayOfRunes []rune
if t.ViewportYLocation+currentLine < len(displayText) && t.ViewportYLocation+currentLine >= 0 {
arrayOfRunes = displayText[t.ViewportYLocation+currentLine]
if !t.IsWordWrapEnabled {
// Only apply horizontal scrolling if word wrap is disabled
if t.ViewportXLocation < len(arrayOfRunes) && t.ViewportXLocation >= 0 {
if t.ViewportXLocation+t.Width < len(arrayOfRunes) {
arrayOfRunes = arrayOfRunes[t.ViewportXLocation : t.ViewportXLocation+t.Width]
} else {
arrayOfRunes = arrayOfRunes[t.ViewportXLocation:]
}
} else {
// If scrolled too far right and there are no column text to print, just show blanks.
// If scrolled too far left (negative value) then show blanks. Note: This case should never happen really.
arrayOfRunes = []rune{}
}
}
arrayOfRunes = stringformat.GetMaxCharactersThatFitInStringSize(arrayOfRunes, t.Width)
shared.printControlText(layerEntry, textboxAlias, t.StyleEntry, attributeEntry, t.XLocation, t.YLocation+currentLine, arrayOfRunes, t.ViewportYLocation+currentLine, t.ViewportXLocation, t.CursorXLocation, t.CursorYLocation)
} else {
// If scrolled too far down and there are no more rows to print, just show blanks.
// If scrolled too far up and there are no rows to print, just print blanks. Note: This case should never happen really.
shared.printControlText(layerEntry, textboxAlias, t.StyleEntry, attributeEntry, t.XLocation, t.YLocation+currentLine, arrayOfRunes, t.ViewportYLocation+currentLine, t.ViewportXLocation, t.CursorXLocation, t.CursorYLocation)
}
}
}
/*
drawBorder allows you to draw a border around a textbox. In addition, the following
information should be noted:
- Draws a border using the specified style and attributes.
- Handles both single and double line borders.
- Adjusts the border position based on the textbox dimensions.
*/
func (shared *textboxType) drawBorder(layerEntry *types.LayerEntryType, styleEntry types.TuiStyleEntryType, attributeEntry types.AttributeEntryType, xLocation int, yLocation int, width int, height int, isDoubleLine bool) {
// Implementation of drawBorder method
}
/*
drawTextboxContent allows you to draw the content of a textbox. In addition, the following
information should be noted:
- Draws the text content with proper formatting and alignment.
- Handles wide characters and line wrapping.
- Manages cursor and highlight rendering.
*/
func (shared *textboxType) drawTextboxContent(layerEntry *types.LayerEntryType, textboxAlias string, styleEntry types.TuiStyleEntryType, attributeEntry types.AttributeEntryType, xLocation int, yLocation int, width int, height int) {
// Implementation of drawTextboxContent method
}
/*
drawTextboxCursor allows you to draw the cursor in a textbox. In addition, the following
information should be noted:
- Draws the cursor at the current position.
- Uses the specified cursor style and attributes.
- Handles cursor visibility and blinking.
*/
func (shared *textboxType) drawTextboxCursor(layerEntry *types.LayerEntryType, textboxAlias string, styleEntry types.TuiStyleEntryType, attributeEntry types.AttributeEntryType, xLocation int, yLocation int) {
// Implementation of drawTextboxCursor method
}
/*
drawTextboxHighlight allows you to draw highlighted text in a textbox. In addition, the following
information should be noted:
- Draws the highlighted text with inverted colors.
- Handles both single-line and multi-line highlights.
- Manages highlight start and end positions.
*/
func (shared *textboxType) drawTextboxHighlight(layerEntry *types.LayerEntryType, textboxAlias string, styleEntry types.TuiStyleEntryType, attributeEntry types.AttributeEntryType, xLocation int, yLocation int, width int, height int) {
// Implementation of drawTextboxHighlight method
}
/*
drawTextboxScrollbars allows you to draw the scrollbars for a textbox. In addition, the following
information should be noted:
- Draws both horizontal and vertical scrollbars if enabled.
- Updates scrollbar positions based on viewport position.
- Handles scrollbar visibility and interaction.
*/
func (shared *textboxType) drawTextboxScrollbars(layerEntry *types.LayerEntryType, textboxAlias string, styleEntry types.TuiStyleEntryType, attributeEntry types.AttributeEntryType, xLocation int, yLocation int, width int, height int) {
// Implementation of drawTextboxScrollbars method
}
/*
drawTextboxScrollbar allows you to draw a single scrollbar for a textbox. In addition, the following
information should be noted:
- Draws either a horizontal or vertical scrollbar.
- Updates the scrollbar handle position based on the current scroll value.
- Handles scrollbar interaction and updates.
*/
func (shared *textboxType) drawTextboxScrollbar(layerEntry *types.LayerEntryType, textboxAlias string, styleEntry types.TuiStyleEntryType, attributeEntry types.AttributeEntryType, xLocation int, yLocation int, length int, isHorizontal bool) {
// Implementation of drawTextboxScrollbar method
}
/*
drawTextboxScrollbarHandle allows you to draw the handle of a scrollbar. In addition, the following
information should be noted:
- Draws the scrollbar handle at the current position.
- Updates the handle position based on the scroll value.
- Handles handle interaction and updates.
*/
func (shared *textboxType) drawTextboxScrollbarHandle(layerEntry *types.LayerEntryType, textboxAlias string, styleEntry types.TuiStyleEntryType, attributeEntry types.AttributeEntryType, xLocation int, yLocation int, length int, isHorizontal bool) {
// Implementation of drawTextboxScrollbarHandle method
}
/*
drawTextboxScrollbarTrack allows you to draw the track of a scrollbar. In addition, the following
information should be noted:
- Draws the scrollbar track with the specified style.
- Handles both horizontal and vertical tracks.
- Updates the track appearance based on scrollbar state.
*/
func (shared *textboxType) drawTextboxScrollbarTrack(layerEntry *types.LayerEntryType, textboxAlias string, styleEntry types.TuiStyleEntryType, attributeEntry types.AttributeEntryType, xLocation int, yLocation int, length int, isHorizontal bool) {
// Implementation of drawTextboxScrollbarTrack method
}
/*
drawTextboxScrollbarArrows allows you to draw the arrows of a scrollbar. In addition, the following
information should be noted:
- Draws both up/down or left/right arrows for the scrollbar.
- Handles arrow interaction and updates.
- Updates arrow appearance based on scrollbar state.
*/
func (shared *textboxType) drawTextboxScrollbarArrows(layerEntry *types.LayerEntryType, textboxAlias string, styleEntry types.TuiStyleEntryType, attributeEntry types.AttributeEntryType, xLocation int, yLocation int, length int, isHorizontal bool) {
// Implementation of drawTextboxScrollbarArrows method
}
/*
printControlText allows you to print text with control IDs. In addition, the following
information should be noted:
- Prints each character with its associated control ID.
- Handles wide characters that take up multiple spaces.
- Manages cursor and highlight rendering.
*/
func (shared *textboxType) printControlText(layerEntry *types.LayerEntryType, textboxAlias string, styleEntry types.TuiStyleEntryType, attributeEntry types.AttributeEntryType, xLocation int, yLocation int, arrayOfRunes []rune, controlYLocation int, startingControlId int, cursorXLocation int, cursorYLocation int) {
currentControlId := startingControlId
currentXOffset := 0
for _, currentCharacter := range arrayOfRunes {
attributeEntry.CellControlId = currentControlId
attributeEntry.CellControlLocation = controlYLocation
// If the textbox being drawn is focused, render the cursor as well.
if isControlCurrentlyFocused(layerEntry.LayerAlias, textboxAlias, constants.CellTypeTextbox) {
textboxEntry := Textboxes.Get(layerEntry.LayerAlias, textboxAlias)
if textboxEntry.IsHighlightActive {
// Check if current position is within highlight range
isHighlighted := false
// Determine the correct start and end positions for highlighting
var highlightStartX, highlightEndX, highlightStartY, highlightEndY int
// If cursor is to the left of the start position
if textboxEntry.CursorYLocation < textboxEntry.HighlightStartY ||
(textboxEntry.CursorYLocation == textboxEntry.HighlightStartY &&
textboxEntry.CursorXLocation < textboxEntry.HighlightStartX) {
// Cursor is before the highlight start, so swap the positions
highlightStartX = textboxEntry.CursorXLocation
highlightStartY = textboxEntry.CursorYLocation
highlightEndX = textboxEntry.HighlightStartX
highlightEndY = textboxEntry.HighlightStartY
} else {
// Cursor is after or at the highlight start
highlightStartX = textboxEntry.HighlightStartX
highlightStartY = textboxEntry.HighlightStartY
highlightEndX = textboxEntry.CursorXLocation
highlightEndY = textboxEntry.CursorYLocation
}
// Check if the current position is within the highlight range
if controlYLocation >= highlightStartY && controlYLocation <= highlightEndY {
if controlYLocation == highlightStartY && controlYLocation == highlightEndY {
// Same line highlight
isHighlighted = currentControlId >= highlightStartX && currentControlId <= highlightEndX
} else if controlYLocation == highlightStartY {
// First line of multi-line highlight
isHighlighted = currentControlId >= highlightStartX
} else if controlYLocation == highlightEndY {
// Last line of multi-line highlight
isHighlighted = currentControlId <= highlightEndX
} else {
// Middle line of multi-line highlight
isHighlighted = true
}
}
if isHighlighted {
attributeEntry.ForegroundColor = styleEntry.Textbox.HighlightForegroundColor
attributeEntry.BackgroundColor = styleEntry.Textbox.HighlightBackgroundColor
} else if cursorXLocation == currentControlId && cursorYLocation == controlYLocation {
attributeEntry.ForegroundColor = styleEntry.Textbox.CursorForegroundColor
attributeEntry.BackgroundColor = styleEntry.Textbox.CursorBackgroundColor
} else {
attributeEntry.ForegroundColor = styleEntry.Textbox.ForegroundColor
attributeEntry.BackgroundColor = styleEntry.Textbox.BackgroundColor
}
} else if cursorXLocation == currentControlId && cursorYLocation == controlYLocation {
attributeEntry.ForegroundColor = styleEntry.Textbox.CursorForegroundColor
attributeEntry.BackgroundColor = styleEntry.Textbox.CursorBackgroundColor
} else {
attributeEntry.ForegroundColor = styleEntry.Textbox.ForegroundColor
attributeEntry.BackgroundColor = styleEntry.Textbox.BackgroundColor
}
}
printLayer(layerEntry, attributeEntry, xLocation+currentXOffset, yLocation, []rune{currentCharacter})
if stringformat.IsRuneCharacterWide(currentCharacter) {
// If we find a wide character, we add a blank space with the same ID as the previous
// character so the next printed character doesn't get covered by the wide one.
currentXOffset++
printLayer(layerEntry, attributeEntry, xLocation+currentXOffset, yLocation, []rune{' '})
currentXOffset++
} else {
currentXOffset++
}
currentControlId++
}
}
/*
updateCursor allows you to update the cursor position in a textbox. In addition, the following
information should be noted:
- Ensures the cursor stays within valid bounds.
- Handles cases where the cursor moves outside the text.
- Updates the cursor position in the textbox entry.
*/
func (shared *textboxType) updateCursor(textboxEntry *types.TextboxEntryType, xLocation int, yLocation int) {
textboxEntry.CursorXLocation = xLocation
textboxEntry.CursorYLocation = yLocation
// If yLocation is less than text data bounds.
if textboxEntry.CursorYLocation < 0 {
textboxEntry.CursorYLocation = 0
}
// If yLocation is greater than column data bounds.
if textboxEntry.CursorYLocation > len(textboxEntry.TextData)-1 {
textboxEntry.CursorYLocation = len(textboxEntry.TextData) - 1
}
// If our cursor xLocation was jumped (due to NullCellControlId) or placed in an invalid xLocation spot greater than the length of our text line.
// Move it to the end of the line.
if textboxEntry.CursorXLocation == constants.NullCellControlId || textboxEntry.CursorXLocation > len(textboxEntry.TextData[textboxEntry.CursorYLocation])-1 {
textboxEntry.CursorXLocation = len(textboxEntry.TextData[textboxEntry.CursorYLocation]) - 1
} else if textboxEntry.CursorXLocation < 0 {
// If the cursor xLocation was scrolled to be out of lower bounds, just set the location to 0.
textboxEntry.CursorXLocation = 0
}
}
/*
updateViewport allows you to update the visible portion of a textbox. In addition, the following
information should be noted:
- Adjusts the viewport to keep the cursor visible.
- Handles cases where the cursor moves outside the viewport.
- Updates the viewport position in the textbox entry.
*/
func (shared *textboxType) updateViewport(textboxEntry *types.TextboxEntryType) {
// If cursor yLocation is higher than the viewport window, move the window to make the cursor appear at the end.
if textboxEntry.CursorYLocation >= textboxEntry.ViewportYLocation+textboxEntry.Height {
textboxEntry.ViewportYLocation = textboxEntry.CursorYLocation - textboxEntry.Height + 1
}
// If cursor yLocation is lower than viewport window, make the viewport window start at yLocation.
if textboxEntry.CursorYLocation < textboxEntry.ViewportYLocation {
textboxEntry.ViewportYLocation = textboxEntry.CursorYLocation
}
// If cursor yLocation is less than 0 (Out of range), just set viewport window to 0.
if textboxEntry.CursorYLocation <= 0 {
textboxEntry.ViewportYLocation = 0
}
// If cursor xLocation is lower than the viewport window
if textboxEntry.CursorXLocation < textboxEntry.ViewportXLocation {
// LogInfo("YES1 " + fmt.Sprintf("%d", time.Now().Unix()))
isCursorJumped := false
// Detect if the cursor xLocation was jumped or if it was scrolled.
if textboxEntry.ViewportXLocation-textboxEntry.CursorXLocation > 2 || textboxEntry.CursorXLocation-textboxEntry.ViewportXLocation > 2 {
isCursorJumped = true
}
// If the cursor xLocation is less than the size of our viewport and was jumped, just set the viewport to 0.
if isCursorJumped && textboxEntry.CursorXLocation-textboxEntry.Width < 0 {
textboxEntry.ViewportXLocation = 0
} else {
// Otherwise, this is a normal backwards scroll so make viewport equal to our cursor location.
textboxEntry.ViewportXLocation = textboxEntry.CursorXLocation
}
}
// Figure out how much displayable space is in our current viewport window.
arrayOfRunesAvailableToPrint := textboxEntry.TextData[textboxEntry.CursorYLocation][textboxEntry.ViewportXLocation:]
arrayOfRunesThatFitStringSize := stringformat.GetMaxCharactersThatFitInStringSize(arrayOfRunesAvailableToPrint, textboxEntry.Width)
// If the cursor xLocation is equal or greater than the visible viewport window width.
if textboxEntry.CursorXLocation >= textboxEntry.ViewportXLocation+len(arrayOfRunesThatFitStringSize) {
// Then make the viewport xLocation equal to the visible viewport width behind it.
maxViewportWidthAvaliable := textboxEntry.Width
if textboxEntry.CursorXLocation-textboxEntry.Width < 0 {
maxViewportWidthAvaliable = textboxEntry.CursorXLocation
}
arrayOfRunesAvailableToPrint = textboxEntry.TextData[textboxEntry.CursorYLocation][textboxEntry.CursorXLocation-maxViewportWidthAvaliable : textboxEntry.CursorXLocation]
numberOfRunesThatFitStringSize := stringformat.GetMaxCharactersThatFitInStringSizeReverse(arrayOfRunesAvailableToPrint, textboxEntry.Width)
// LogInfo(fmt.Sprintf("v: %d x: %d off: %d fit: %d, aval: %s", textboxEntry.ViewportXLocation, textboxEntry.CursorXLocation, maxViewportWidthAvaliable, numberOfRunesThatFitStringSize, string(arrayOfRunesAvailableToPrint)))
textboxEntry.ViewportXLocation = textboxEntry.CursorXLocation - numberOfRunesThatFitStringSize + 1
}
}
/*
UpdateKeyboardEventTextboxWithString allows you to process a string of characters as keyboard input. In addition,
the following information should be noted: