-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.go
1508 lines (1350 loc) · 39.3 KB
/
entry.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
// Copyright (c) 2023 The Go-Curses Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ctk
import (
"fmt"
"strings"
"github.com/gofrs/uuid"
"github.com/go-curses/cdk"
cenums "github.com/go-curses/cdk/lib/enums"
cmath "github.com/go-curses/cdk/lib/math"
"github.com/go-curses/cdk/lib/paint"
"github.com/go-curses/cdk/lib/ptypes"
cstrings "github.com/go-curses/cdk/lib/strings"
"github.com/go-curses/cdk/memphis"
"github.com/go-curses/ctk/lib/enums"
)
const (
TypeEntry cdk.CTypeTag = "ctk-entry"
EntryMonoTheme paint.ThemeName = "entry-mono"
EntryColorTheme paint.ThemeName = "entry-color"
)
func init() {
_ = cdk.TypesManager.AddType(TypeEntry, func() interface{} { return MakeEntry() })
ctkBuilderTranslators[TypeEntry] = func(builder Builder, widget Widget, name, value string) error {
switch strings.ToLower(name) {
case "wrap":
isTrue := cstrings.IsTrue(value)
if err := widget.SetBoolProperty(PropertyWrap, isTrue); err != nil {
return err
}
if isTrue {
if wmi, err := widget.GetStructProperty(PropertyWrapMode); err == nil {
if wm, ok := wmi.(cenums.WrapMode); ok {
if wm == cenums.WRAP_NONE {
if err := widget.SetStructProperty(PropertyWrapMode, cenums.WRAP_WORD); err != nil {
widget.LogErr(err)
}
}
}
}
}
return nil
}
return ErrFallthrough
}
borders, _ := paint.GetDefaultBorderRunes(paint.StockBorder)
arrows, _ := paint.GetArrows(paint.WideArrow)
style := paint.GetDefaultColorStyle()
styleLight := style.Foreground(paint.ColorWhite).Background(paint.ColorDarkSlateGray)
styleDark := style.Foreground(paint.ColorSilver).Background(paint.ColorGray)
paint.RegisterTheme(EntryColorTheme, paint.Theme{
Content: paint.ThemeAspect{
Normal: styleDark.Dim(true).Bold(false),
Selected: styleLight.Dim(false).Bold(true),
Active: styleLight.Dim(false).Bold(true).Reverse(true),
Prelight: styleLight.Dim(false).Bold(false),
Insensitive: styleDark.Dim(false).Bold(false),
FillRune: paint.DefaultFillRune,
BorderRunes: borders,
ArrowRunes: arrows,
Overlay: false,
},
Border: paint.ThemeAspect{
Normal: styleDark.Dim(false).Bold(false),
Selected: styleLight.Dim(false).Bold(true),
Active: styleLight.Dim(false).Bold(true).Reverse(true),
Prelight: styleLight.Dim(false),
Insensitive: styleDark.Dim(false),
FillRune: paint.DefaultFillRune,
BorderRunes: borders,
ArrowRunes: arrows,
Overlay: false,
},
})
style = paint.GetDefaultMonoStyle()
styleLight = style.Foreground(paint.ColorWhite).Background(paint.ColorDarkSlateGray)
styleDark = style.Foreground(paint.ColorSilver).Background(paint.ColorGray)
paint.RegisterTheme(EntryMonoTheme, paint.Theme{
Content: paint.ThemeAspect{
Normal: styleDark.Dim(true).Bold(false),
Selected: styleLight.Dim(false).Bold(true),
Active: styleLight.Dim(false).Bold(true).Reverse(true),
Prelight: styleLight.Dim(false).Bold(false),
Insensitive: styleDark.Dim(false).Bold(false),
FillRune: paint.DefaultFillRune,
BorderRunes: borders,
ArrowRunes: arrows,
Overlay: false,
},
Border: paint.ThemeAspect{
Normal: styleDark.Dim(false).Bold(false),
Selected: styleLight.Dim(false).Bold(true),
Active: styleLight.Dim(false).Bold(true).Reverse(true),
Prelight: styleLight.Dim(false),
Insensitive: styleDark.Dim(false),
FillRune: paint.DefaultFillRune,
BorderRunes: borders,
ArrowRunes: arrows,
Overlay: false,
},
})
}
// Entry Hierarchy:
// Object
// +- Widget
// +- Misc
// +- Entry
// +- AccelLabel
// +- TipsQuery
//
// The Entry Widget presents text to the end user.
type Entry interface {
Misc
Alignable
Buildable
Editable
Sensitive
SetText(text string)
SetAttributes(attrs paint.Style)
SetJustify(justify cenums.Justification)
SetWidthChars(nChars int)
SetMaxWidthChars(nChars int)
SetLineWrap(wrap bool)
SetLineWrapMode(wrapMode cenums.WrapMode)
GetSelectable() (value bool)
GetText() (value string)
SelectRegion(startOffset int, endOffset int)
SetSelectable(setting bool)
GetAttributes() (value paint.Style)
GetJustify() (value cenums.Justification)
GetWidthChars() (value int)
GetMaxWidthChars() (value int)
GetLineWrap() (value bool)
GetLineWrapMode() (value cenums.WrapMode)
GetSingleLineMode() (value bool)
SetSingleLineMode(singleLineMode bool)
Settings() (singleLineMode bool, lineWrapMode cenums.WrapMode, justify cenums.Justification, maxWidthChars int)
}
var _ Entry = (*CEntry)(nil)
type cEntryChange struct {
name string
argv []interface{}
}
// The CTextField structure implements the Entry interface and is exported
// to facilitate type embedding with custom implementations. No member variables
// are exported as the interface methods are the only intended means of
// interacting with Entry objects.
type CEntry struct {
CMisc
tid uuid.UUID
tRegion ptypes.Region
offset *ptypes.Region
cursor *ptypes.Point2I
selection *ptypes.Range
position int
selectionMovingStart bool
tProfile *memphis.TextProfile
tBuffer memphis.TextBuffer
tbStyle paint.Style
}
// MakeEntry is used by the Buildable system to construct a new Entry.
func MakeEntry() Entry {
return NewEntry("")
}
// NewEntry is the constructor for new Entry instances.
func NewEntry(plain string) Entry {
l := new(CEntry)
l.Init()
l.SetText(plain)
return l
}
// Init initializes a Entry object. This must be called at least once to
// set up the necessary defaults and allocate any memory structures. Calling
// this more than once is safe though unnecessary. Only the first call will
// result in any effect upon the Entry instance. Init is used in the
// NewEntry constructor and only necessary when implementing a derivative
// Entry type.
func (l *CEntry) Init() (already bool) {
if l.InitTypeItem(TypeEntry, l) {
return true
}
l.CMisc.Init()
l.flags = enums.NULL_WIDGET_FLAG
l.SetFlags(enums.SENSITIVE | enums.PARENT_SENSITIVE | enums.CAN_DEFAULT | enums.APP_PAINTABLE | enums.CAN_FOCUS)
_ = l.InstallProperty(PropertyAttributes, cdk.StructProperty, true, nil)
_ = l.InstallProperty(PropertyJustify, cdk.StructProperty, true, cenums.JUSTIFY_NONE)
_ = l.InstallProperty(PropertyText, cdk.StringProperty, true, "")
_ = l.InstallProperty(PropertyMaxWidthChars, cdk.IntProperty, true, -1)
_ = l.InstallProperty(PropertySelectable, cdk.BoolProperty, true, false)
_ = l.InstallProperty(PropertySingleLineMode, cdk.BoolProperty, true, false)
_ = l.InstallProperty(PropertyWidthChars, cdk.IntProperty, true, -1)
_ = l.InstallProperty(PropertyWrap, cdk.BoolProperty, true, false)
_ = l.InstallProperty(PropertyWrapMode, cdk.StructProperty, true, cenums.WRAP_NONE)
_ = l.InstallProperty(PropertyEditable, cdk.BoolProperty, true, true)
l.selection = nil
l.position = 0
l.offset = ptypes.NewRegion(0, 0, 0, 0)
l.cursor = ptypes.NewPoint2I(0, 0)
l.tProfile = memphis.NewTextProfile("")
l.tBuffer = nil
l.tid, _ = uuid.NewV4()
l.tRegion = ptypes.MakeRegion(0, 0, 0, 0)
theme, _ := paint.GetTheme(EntryColorTheme)
if err := memphis.MakeSurface(l.tid, l.tRegion.Origin(), l.tRegion.Size(), theme.Content.Normal); err != nil {
l.LogErr(err)
}
l.SetTheme(theme)
l.Connect(SignalCdkEvent, TextFieldEventHandle, l.event)
l.Connect(SignalLostFocus, TextFieldLostFocusHandle, l.lostFocus)
l.Connect(SignalGainedFocus, TextFieldGainedFocusHandle, l.gainedFocus)
l.Connect(SignalResize, TextFieldResizeHandle, l.resize)
l.Connect(SignalDraw, TextFieldDrawHandle, l.draw)
// _ = l.SetBoolProperty(PropertyDebug, true)
return false
}
// Build provides customizations to the Buildable system for Entry Widgets.
func (l *CEntry) Build(builder Builder, element *CBuilderElement) error {
l.Freeze()
defer l.Thaw()
if name, ok := element.Attributes["id"]; ok {
l.SetName(name)
}
for k, v := range element.Properties {
switch cdk.Property(k) {
case PropertyText:
l.SetText(v)
default:
element.ApplyProperty(k, v)
}
}
element.ApplySignals()
return nil
}
// SetText updates the text within the Entry widget. It overwrites any text that
// was there before. This will also clear any previously set mnemonic
// accelerators.
//
// Parameters:
// text the text you want to set
//
// Locking: write
func (l *CEntry) SetText(text string) {
l.setText(text)
}
func (l *CEntry) setText(text string) {
l.Lock()
l.tProfile.Set(text)
l.Unlock()
if err := l.SetStringProperty(PropertyText, l.tProfile.Get()); err != nil {
l.LogErr(err)
} else {
l.refresh()
}
}
// SetAttributes updates the attributes property to be the given paint.Style.
//
// Parameters:
// attrs a paint.Style
//
// Locking: write
func (l *CEntry) SetAttributes(attrs paint.Style) {
if err := l.SetStructProperty(PropertyAttributes, attrs); err != nil {
l.LogErr(err)
}
}
// SetJustify updates the alignment of the lines in the text of the label
// relative to each other. JUSTIFY_LEFT is the default value when the widget is
// first created with New. If you instead want to set the alignment of the label
// as a whole, use SetAlignment instead.
//
// SetJustify has no effect on labels containing only a single line.
//
// Parameters:
// jtype a Justification
//
// Locking: write
func (l *CEntry) SetJustify(justify cenums.Justification) {
if err := l.SetStructProperty(PropertyJustify, justify); err != nil {
l.LogErr(err)
} else {
l.refresh()
}
}
// SetWidthChars updates the desired width in characters of label to nChars.
//
// Parameters:
// nChars the new desired width, in characters.
//
// Locking: write
func (l *CEntry) SetWidthChars(nChars int) {
if err := l.SetIntProperty(PropertyWidthChars, nChars); err != nil {
l.LogErr(err)
} else {
l.refresh()
}
}
// SetMaxWidthChars updates the desired maximum width in characters of label to
// nChars.
//
// Parameters:
// nChars the new desired maximum width, in characters.
//
// Locking: write
func (l *CEntry) SetMaxWidthChars(nChars int) {
if err := l.SetIntProperty(PropertyMaxWidthChars, nChars); err != nil {
l.LogErr(err)
} else {
l.refresh()
}
}
// SetLineWrap updates the line wrapping within the Entry widget. TRUE makes it
// break lines if text exceeds the widget's size. FALSE lets the text get cut
// off by the edge of the widget if it exceeds the widget size. Note that
// setting line wrapping to TRUE does not make the label wrap at its parent
// container's width, because CTK widgets conceptually can't make their
// requisition depend on the parent container's size. For a label that wraps
// at a specific position, set the label's width using SetSizeRequest.
//
// Parameters:
// wrap the setting
//
// Locking: write
func (l *CEntry) SetLineWrap(wrap bool) {
if err := l.SetBoolProperty(PropertyWrap, wrap); err != nil {
l.LogErr(err)
} else {
l.refresh()
}
}
// SetLineWrapMode updates the line wrapping if line-wrap is on (see
// SetLineWrap) this controls how the line wrapping is done. The default is
// WRAP_WORD which means wrap on word boundaries.
//
// Parameters:
// wrapMode the line wrapping mode
//
// Locking: write
func (l *CEntry) SetLineWrapMode(wrapMode cenums.WrapMode) {
if err := l.SetStructProperty(PropertyWrapMode, wrapMode); err != nil {
l.LogErr(err)
} else {
l.refresh()
}
}
// GetSelectable returns the value set by SetSelectable.
//
// Locking: read
func (l *CEntry) GetSelectable() (value bool) {
var err error
if value, err = l.GetBoolProperty(PropertySelectable); err != nil {
l.LogErr(err)
}
return
}
// GetText returns the text from a label widget, as displayed on the screen.
// This does not include any embedded underlines indicating mnemonics or Tango
// markup.
// See: GetLabel
//
// Locking: read
func (l *CEntry) GetText() (value string) {
return l.tProfile.Get()
}
// SelectRegion selects a range of characters in the label, if the label is
// selectable. If the label is not selectable, this function has no effect. If
// start_offset or end_offset are -1, then the end of the label will be
// substituted.
// See: SetSelectable()
//
// Parameters:
// startOffset start offset (in characters not bytes)
// endOffset end offset (in characters not bytes)
//
func (l *CEntry) SelectRegion(startOffset int, endOffset int) {
if l.GetSelectable() {
l.Lock()
l.selection = ptypes.NewRange(startOffset, endOffset)
l.Unlock()
}
}
// SetSelectable updates the selectable property for the Entry. TextFields allow the
// user to select text from the label, for copy-and-paste.
//
// Parameters:
// setting TRUE to allow selecting text in the label
//
// Note that usage of this within CTK is unimplemented at this time
//
// Locking: write
func (l *CEntry) SetSelectable(setting bool) {
if err := l.SetBoolProperty(PropertySelectable, setting); err != nil {
l.LogErr(err)
} else {
l.refresh()
}
}
// GetAttributes returns the attribute list that was set on the label using
// SetAttributes, if any. This function does not reflect attributes that come
// from the Entry markup (see SetMarkup).
//
// Locking: read
func (l *CEntry) GetAttributes() (value paint.Style) {
var ok bool
if v, err := l.GetStructProperty(PropertyAttributes); err != nil {
l.LogErr(err)
} else if value, ok = v.(paint.Style); !ok {
l.LogError("value stored in PropertyAttributes is not of paint.Style type: %v (%T)", v, v)
}
return
}
// GetJustify returns the justification of the label.
// See: SetJustify()
//
// Locking: read
func (l *CEntry) GetJustify() (value cenums.Justification) {
var ok bool
if v, err := l.GetStructProperty(PropertyJustify); err != nil {
l.LogErr(err)
} else if value, ok = v.(cenums.Justification); !ok {
l.LogError("value stored in PropertyJustify is not of cenums.Justification type: %v (%T)", v, v)
}
return
}
// GetWidthChars retrieves the desired width of label, in characters.
// See: SetWidthChars()
//
// Locking: read
func (l *CEntry) GetWidthChars() (value int) {
var err error
if value, err = l.GetIntProperty(PropertyWidthChars); err != nil {
l.LogErr(err)
}
return
}
// GetMaxWidthChars retrieves the desired maximum width of label, in characters.
// See: SetWidthChars()
//
// Locking: read
func (l *CEntry) GetMaxWidthChars() (value int) {
var err error
if value, err = l.GetIntProperty(PropertyMaxWidthChars); err != nil {
l.LogErr(err)
}
return
}
// GetLineWrap returns whether lines in the label are automatically wrapped.
// See: SetLineWrap()
//
// Locking: read
func (l *CEntry) GetLineWrap() (value bool) {
var err error
if value, err = l.GetBoolProperty(PropertyWrap); err != nil {
l.LogErr(err)
}
return
}
// GetLineWrapMode returns line wrap mode used by the label.
// See: SetLineWrapMode()
//
// Locking: read
func (l *CEntry) GetLineWrapMode() (value cenums.WrapMode) {
var ok bool
if v, err := l.GetStructProperty(PropertyWrapMode); err != nil {
l.LogErr(err)
} else if value, ok = v.(cenums.WrapMode); !ok {
l.LogError("value stored in PropertyWrap is not of cenums.WrapMode type: %v (%T)", v, v)
}
return
}
// GetSingleLineMode returns whether the label is in single line mode.
//
// Locking: read
func (l *CEntry) GetSingleLineMode() (value bool) {
var err error
if value, err = l.GetBoolProperty(PropertySingleLineMode); err != nil {
l.LogErr(err)
}
return
}
// SetSingleLineMode updates whether the label is in single line mode.
//
// Parameters:
// singleLineMode TRUE if the label should be in single line mode
//
// Locking: write
func (l *CEntry) SetSingleLineMode(singleLineMode bool) {
if err := l.SetBoolProperty(PropertySingleLineMode, singleLineMode); err != nil {
l.LogErr(err)
} else {
l.refresh()
}
}
// Settings is a convenience method to return the interesting settings currently
// configured on the Entry instance.
//
// Locking: read
func (l *CEntry) Settings() (singleLineMode bool, lineWrapMode cenums.WrapMode, justify cenums.Justification, maxWidthChars int) {
singleLineMode = l.GetSingleLineMode()
lineWrapMode = l.GetLineWrapMode()
justify = l.GetJustify()
maxWidthChars = l.GetMaxWidthChars()
return
}
func (l *CEntry) GetSelectionBounds() (startPos, endPos int, ok bool) {
l.RLock()
defer l.RUnlock()
if l.selection != nil {
startPos = l.selection.Start
endPos = l.selection.End
ok = true
}
return
}
func (l *CEntry) InsertTextAndSetPosition(newText string, index, position int) {
l.insertTextAndSetPosition(newText, index, position)
}
func (l *CEntry) insertTextAndSetPosition(newText string, index, position int) {
l.insertText(newText, index)
l.setPosition(position)
}
func (l *CEntry) InsertText(newText string, position int) {
l.insertText(newText, position)
}
func (l *CEntry) insertText(newText string, position int) {
if modified, ok := l.tProfile.Insert(newText, position); ok {
if err := l.SetStringProperty(PropertyText, modified); err != nil {
l.LogErr(err)
} else {
l.refresh()
l.Emit(SignalChangedText, l, modified)
}
}
}
func (l *CEntry) DeleteTextAndSetPosition(start, end, position int) {
l.deleteTextAndSetPosition(start, end, position)
}
func (l *CEntry) deleteTextAndSetPosition(start, end, position int) {
l.deleteText(start, end)
l.setPosition(position)
}
func (l *CEntry) DeleteText(startPos int, endPos int) {
l.deleteText(startPos, endPos)
}
func (l *CEntry) deleteText(startPos int, endPos int) {
if modified, ok := l.tProfile.Delete(startPos, endPos); ok {
if err := l.SetStringProperty(PropertyText, modified); err != nil {
l.LogErr(err)
} else {
l.refresh()
l.Emit(SignalChangedText, l, modified)
}
}
}
func (l *CEntry) GetChars(startPos int, endPos int) (value string) {
content := l.GetText()
contentLength := len(content)
if startPos >= contentLength {
return
}
if contentLength <= endPos {
value = content[startPos:]
} else {
value = content[startPos:endPos]
}
return
}
func (l *CEntry) CutClipboard() {
value := ""
l.RLock()
if l.selection != nil && l.tProfile != nil {
if l.tProfile.Len() > 0 {
value = l.tProfile.Select(l.selection.Start, l.selection.End)
}
l.RUnlock()
l.deleteTextAndSetPosition(l.selection.Start, l.selection.End, l.selection.Start)
} else {
l.RUnlock()
}
if d := l.GetDisplay(); d != nil {
clipboard := d.GetClipboard()
clipboard.Copy(value)
}
l.LogDebug("cut to clipboard: \"%v\"", value)
l.clearSelection()
}
func (l *CEntry) CopyClipboard() {
value := ""
l.RLock()
if l.selection != nil && l.tProfile != nil {
if l.tProfile.Len() > 0 {
value = l.tProfile.Select(l.selection.Start, l.selection.End)
}
}
l.RUnlock()
if d := l.GetDisplay(); d != nil {
clipboard := d.GetClipboard()
clipboard.Copy(value)
}
l.LogDebug("copied to clipboard: \"%v\"", value)
l.clearSelection()
}
func (l *CEntry) PasteClipboard() {
var value string
if d := l.GetDisplay(); d != nil {
clipboard := d.GetClipboard()
value = clipboard.GetText()
}
pos := l.GetPosition()
l.RLock()
var selection *ptypes.Range
if l.selection != nil {
selection = l.selection.NewClone()
}
l.RUnlock()
if selection != nil {
l.deleteTextAndSetPosition(selection.Start, selection.End, selection.Start)
pos = selection.Start
}
pos = cmath.FloorI(pos, 0)
l.insertTextAndSetPosition(value, pos, pos+len(value))
l.LogDebug("pasted from clipboard: \"%v\"", value)
l.clearSelection()
}
func (l *CEntry) DeleteSelection() {
l.RLock()
selection := l.selection
l.RUnlock()
if selection != nil {
l.deleteTextAndSetPosition(l.selection.Start, l.selection.End, l.selection.Start)
l.LogDebug("selection deleted")
}
l.clearSelection()
}
func (l *CEntry) SetPosition(position int) {
l.setPosition(position)
}
func (l *CEntry) setPosition(position int) {
l.Lock()
max := l.tProfile.Len()
if position > max {
position = max
}
l.position = position
l.Unlock()
l.refresh()
}
func (l *CEntry) GetPosition() (value int) {
l.RLock()
defer l.RUnlock()
return l.position
}
func (l *CEntry) SetEditable(isEditable bool) {
if err := l.SetBoolProperty(PropertyEditable, isEditable); err != nil {
l.LogErr(err)
} else {
l.refresh()
}
}
func (l *CEntry) GetEditable() (value bool) {
var err error
if value, err = l.GetBoolProperty(PropertyEditable); err != nil {
l.LogErr(err)
}
return
}
// GetSizeRequest returns the requested size of the Entry taking into account
// the label's content and any padding set.
//
// Locking: read
func (l *CEntry) GetSizeRequest() (width, height int) {
alloc := l.GetAllocation()
size := l.CWidget.SizeRequest()
if alloc.W > 0 && size.W > alloc.W {
size.W = alloc.W
}
if alloc.H > 0 && size.H > alloc.H {
size.H = alloc.H
}
return size.W, size.H
}
// CancelEvent emits a cancel-event signal and if the signal handlers all return
// cenums.EVENT_PASS, then set the button as not pressed and release any event
// focus.
func (l *CEntry) CancelEvent() {
l.LogDebug("hit cancel event")
}
// Activate emits a SignalActivate, returning TRUE if the event was handled
func (l *CEntry) Activate() (value bool) {
return l.Emit(SignalActivate, l) == cenums.EVENT_STOP
}
func (l *CEntry) getMaxCharsRequest() (maxWidth int) {
alloc := l.GetAllocation()
maxWidth = l.GetMaxWidthChars()
if maxWidth <= -1 {
w, _ := l.GetSizeRequest()
if w > -1 {
maxWidth = w
} else {
maxWidth = alloc.W
}
}
return
}
func (l *CEntry) refreshTextBuffer() (err error) {
style := l.GetThemeRequest().Content.Normal
alloc := l.GetAllocation()
pos := l.GetPosition()
l.Lock()
posPoint := l.tProfile.GetPointFromPosition(pos)
// keep pos within alloc
if posPoint.X > alloc.W {
l.offset.X = posPoint.X - alloc.W
l.cursor.X = posPoint.X - l.offset.X
} else {
l.offset.X = 0
l.cursor.X = posPoint.X
}
if posPoint.Y > alloc.H {
l.offset.Y = posPoint.Y - alloc.H
l.cursor.Y = posPoint.Y - l.offset.Y
} else {
l.offset.Y = 0
l.cursor.Y = posPoint.Y
}
l.offset.W = alloc.W
l.offset.H = alloc.H
if l.cursor.X >= alloc.W {
l.offset.X += 1
l.cursor.X = alloc.W - 1
}
if l.cursor.Y >= alloc.H {
l.offset.Y += 1
l.cursor.Y = alloc.H - 1
}
// crop text to alloc using offset
text := l.tProfile.Crop(*l.offset)
// l.LogDebug("pos:%v, posPoint:%v, offset:%v, cursor:%v", pos, posPoint, l.offset, l.cursor)
if l.tBuffer != nil {
l.tBuffer.Set(text, style)
} else {
l.tBuffer = memphis.NewTextBuffer(text, style, false)
}
l.Unlock()
return
}
func (l *CEntry) refresh() {
if err := l.refreshTextBuffer(); err != nil {
l.LogErr(err)
}
l.updateCursor()
l.Invalidate()
}
func (l *CEntry) resize(data []interface{}, argv ...interface{}) cenums.EventFlag {
alloc := l.GetAllocation()
if !l.IsVisible() || alloc.W <= 0 || alloc.H <= 0 {
l.LogTrace("not visible, zero width or zero height")
return cenums.EVENT_PASS
}
origin := l.GetOrigin()
xPad, _ := l.GetPadding()
_, yAlign := l.GetAlignment()
size := ptypes.NewRectangle(alloc.W, alloc.H)
local := ptypes.MakePoint2I(origin.X+xPad, origin.Y)
size.W = alloc.W - (xPad * 2)
size.H = alloc.H - (xPad * 2)
if size.H < alloc.H {
delta := alloc.H - size.H
local.Y += int(float64(delta) * yAlign)
}
l.Lock()
l.tRegion = ptypes.MakeRegion(local.X, local.Y, size.W, size.H)
l.Unlock()
theme := l.GetThemeRequest()
if err := memphis.FillSurface(l.ObjectID(), theme); err != nil {
l.LogErr(err)
}
if err := memphis.MakeConfigureSurface(l.tid, l.tRegion.Origin(), l.tRegion.Size(), theme.Content.Normal); err != nil {
l.LogErr(err)
} else if err := memphis.FillSurface(l.tid, theme); err != nil {
l.LogErr(err)
}
l.refresh()
return cenums.EVENT_STOP
}
func (l *CEntry) draw(data []interface{}, argv ...interface{}) cenums.EventFlag {
if surface, ok := argv[1].(*memphis.CSurface); ok {
alloc := l.GetAllocation()
if !l.IsVisible() || alloc.W <= 0 || alloc.H <= 0 {
l.LogTrace("not visible, zero width or zero height")
return cenums.EVENT_PASS
}
theme := l.GetThemeRequest()
singleLineMode, lineWrapMode, justify, _ := l.Settings()
surface.Fill(theme)
if tBuffer := l.tBuffer.Clone(); tBuffer != nil {
tBuffer.SetStyle(theme.Content.Normal)
if l.selection != nil {
crop := l.tProfile.GetCropSelect(*l.selection, *l.offset)
tBuffer.Select(crop.Start, crop.End)
}
if tSurface, err := memphis.GetSurface(l.tid); err != nil {
l.LogErr(err)
} else {
tSurface.Fill(theme)
tBuffer.Draw(tSurface, singleLineMode, lineWrapMode, false, justify, cenums.ALIGN_TOP)
if err := surface.CompositeSurface(tSurface); err != nil {
l.LogErr(err)
}
}
}
if debug, _ := l.GetBoolProperty(cdk.PropertyDebug); debug {
surface.DebugBox(paint.ColorSilver, l.ObjectInfo())
}
return cenums.EVENT_STOP
}
return cenums.EVENT_PASS
}
func (l *CEntry) updateSelection(oldPos, newPos int) (note string) {
l.Lock()
profileLen := l.tProfile.Len()
if l.tProfile != nil && profileLen > 0 {
// wasMovingBackwards := l.selectionOldPos > selectionOldPos
isMovingBackwards := oldPos > newPos
if l.selection == nil {
if isMovingBackwards {
l.selectionMovingStart = true
l.selection = ptypes.NewRange(newPos, oldPos-1)
note = fmt.Sprintf("started new selection backwards: %v [%v,%v]", l.selection, oldPos, newPos)
} else {
l.selectionMovingStart = false
l.selection = ptypes.NewRange(oldPos, newPos-1)
note = fmt.Sprintf("started new selection forwards: %v [%v,%v]", l.selection, oldPos, newPos)
}
} else {
if newPos >= profileLen {
newPos = profileLen - 1
}
if l.selectionMovingStart && newPos > l.selection.End {
l.selection.Start = newPos
l.selectionMovingStart = false
} else if !l.selectionMovingStart && newPos < l.selection.Start {
l.selection.End = newPos
l.selectionMovingStart = true
}
if l.selectionMovingStart {
if isMovingBackwards {
l.selection.Start = newPos
note = fmt.Sprintf("moving selection start backwards: %v [%v,%v]", l.selection, oldPos, newPos)
} else {
l.selection.Start = newPos
note = fmt.Sprintf("moving selection start forwards: %v [%v,%v]", l.selection, oldPos, newPos)
}
} else {
if isMovingBackwards {
l.selection.End = newPos
note = fmt.Sprintf("moving selection end backwards: %v [%v,%v]", l.selection, oldPos, newPos)
} else {
l.selection.End = newPos
note = fmt.Sprintf("moving selection end forwards: %v [%v,%v]", l.selection, oldPos, newPos)
}
}
}
} else {
note = fmt.Sprintf("cannot select range of zero-length string")
}
l.Unlock()
l.Invalidate()
return
}
func (l *CEntry) unselectAll() {
if l.selectedAll() {
l.clearSelection()
l.setPosition(0)
}
}
func (l *CEntry) selectAll() {
l.Lock()
end := l.tProfile.Len() - 1
if l.selection == nil {
l.selection = ptypes.NewRange(0, end)
l.LogDebug("new select all (ctrl+a): %v", l.selection)
} else {