-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.go
1286 lines (1168 loc) · 39.1 KB
/
label.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 (
"regexp"
"strings"
"github.com/gofrs/uuid"
"github.com/go-curses/cdk"
cenums "github.com/go-curses/cdk/lib/enums"
"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 (
TypeLabel cdk.CTypeTag = "ctk-label"
LabelMonoTheme paint.ThemeName = "label-mono"
LabelColorTheme paint.ThemeName = "label-color"
)
var (
rxLabelPlainText = regexp.MustCompile(`(?msi)(_)([A-Za-z0-9])`)
rxLabelMnemonic = regexp.MustCompile(`(?msi)_([A-Za-z0-9])`)
)
func init() {
_ = cdk.TypesManager.AddType(TypeLabel, func() interface{} { return MakeLabel() })
ctkBuilderTranslators[TypeLabel] = 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
}
theme := paint.GetDefaultColorTheme()
theme.Content.Normal = theme.Content.Normal.Dim(false)
theme.Content.Active = theme.Content.Active.Dim(false)
theme.Content.Selected = theme.Content.Selected.Dim(false)
theme.Content.Prelight = theme.Content.Prelight.Dim(false)
theme.Content.Insensitive = theme.Content.Insensitive.Dim(false)
theme.Border.Normal = theme.Border.Normal.Dim(false)
theme.Border.Active = theme.Border.Active.Dim(false)
theme.Border.Selected = theme.Border.Selected.Dim(false)
theme.Border.Prelight = theme.Border.Prelight.Dim(false)
theme.Border.Insensitive = theme.Border.Insensitive.Dim(false)
paint.RegisterTheme(LabelColorTheme, theme)
}
// Label Hierarchy:
// Object
// +- Widget
// +- Misc
// +- Label
// +- AccelLabel
// +- TipsQuery
//
// The Label Widget presents text to the end user.
type Label interface {
Misc
Alignable
Buildable
SetText(text string)
SetAttributes(attrs paint.Style)
SetMarkup(text string) (parseError error)
SetMarkupWithMnemonic(str string) (err error)
SetJustify(justify cenums.Justification)
SetEllipsize(mode bool)
SetWidthChars(nChars int)
SetMaxWidthChars(nChars int)
SetLineWrap(wrap bool)
SetLineWrapMode(wrapMode cenums.WrapMode)
GetMnemonicKeyVal() (value rune)
GetSelectable() (value bool)
GetText() (value string)
SelectRegion(startOffset int, endOffset int)
SetMnemonicWidget(widget Widget)
SetSelectable(setting bool)
SetTextWithMnemonic(str string)
GetAttributes() (value paint.Style)
GetJustify() (value cenums.Justification)
GetEllipsize() (value bool)
GetWidthChars() (value int)
GetMaxWidthChars() (value int)
GetLabel() (value string)
GetLineWrap() (value bool)
GetLineWrapMode() (value cenums.WrapMode)
GetMnemonicWidget() (value Widget)
GetSelectionBounds() (start int, end int, nonEmpty bool)
GetUseMarkup() (value bool)
GetUseUnderline() (value bool)
GetSingleLineMode() (value bool)
SetLabel(str string)
SetUseMarkup(setting bool)
SetUseUnderline(setting bool)
SetSingleLineMode(singleLineMode bool)
GetCurrentUri() (value string)
SetTrackVisitedLinks(trackLinks bool)
GetTrackVisitedLinks() (value bool)
Settings() (singleLineMode bool, lineWrapMode cenums.WrapMode, ellipsize bool, justify cenums.Justification, maxWidthChars int)
GetClearText() (text string)
GetPlainText() (text string)
GetCleanText() (text string)
GetPlainTextInfo() (maxWidth, lineCount int)
GetPlainTextInfoAtWidth(width int) (maxWidth, lineCount int)
}
var _ Label = (*CLabel)(nil)
// The CLabel structure implements the Label 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 Label objects.
type CLabel struct {
CMisc
text string
tid uuid.UUID
tRegion *ptypes.Region
tBuffer memphis.TextBuffer
tbStyle paint.Style
}
// MakeLabel is used by the Buildable system to construct a new Label.
func MakeLabel() Label {
return NewLabel("")
}
// NewLabel is the constructor for new Label instances.
func NewLabel(plain string) Label {
l := new(CLabel)
l.Init()
l.SetText(plain)
return l
}
// NewLabelWithMnemonic creates a new Label, containing the text given. If
// characters in the string are preceded by an underscore, they are underlined.
// If you need a literal underscore character in a label, use '__' (two
// underscores). The first underlined character represents a keyboard
// accelerator called a mnemonic. The mnemonic key can be used to activate
// another widget, chosen automatically, or explicitly using SetMnemonicWidget.
// If SetMnemonicWidget is not called, then the first activatable ancestor of
// the Label will be chosen as the mnemonic widget. For instance, if the label
// is inside a button or menu item, the button or menu item will automatically
// become the mnemonic widget and be activated by the mnemonic.
//
// Parameters:
// label text, with an underscore in front of the mnemonic character
func NewLabelWithMnemonic(label string) (value Label) {
l := new(CLabel)
l.Init()
l.SetTextWithMnemonic(label)
return l
}
// NewLabelWithMarkup creates a new Label, containing the text given and if the
// text contains Tango markup, the rendered text will display accordingly.
func NewLabelWithMarkup(markup string) (label Label, err error) {
label = new(CLabel)
label.Init()
err = label.SetMarkup(markup)
return
}
// Init initializes a Label 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 Label instance. Init is used in the
// NewLabel constructor and only necessary when implementing a derivative
// Label type.
func (l *CLabel) Init() (already bool) {
if l.InitTypeItem(TypeLabel, l) {
return true
}
l.CMisc.Init()
l.flags = enums.NULL_WIDGET_FLAG
l.SetFlags(enums.PARENT_SENSITIVE | enums.APP_PAINTABLE)
theme, _ := paint.GetTheme(LabelColorTheme)
l.SetTheme(theme)
l.text = ""
l.tBuffer = nil
l.tid, _ = uuid.NewV4()
l.tRegion = ptypes.NewRegion(0, 0, 0, 0)
if err := memphis.MakeSurface(l.tid, l.tRegion.Origin(), l.tRegion.Size(), paint.GetDefaultColorStyle()); err != nil {
l.LogErr(err)
}
region := l.GetRegion()
_ = memphis.MakeSurface(l.ObjectID(), region.Origin(), region.Size(), paint.GetDefaultColorStyle())
_ = l.InstallProperty(PropertyAttributes, cdk.StructProperty, true, nil)
_ = l.InstallProperty(PropertyCursorPosition, cdk.IntProperty, false, 0)
_ = l.InstallProperty(PropertyEllipsize, cdk.BoolProperty, true, false)
_ = l.InstallProperty(PropertyJustify, cdk.StructProperty, true, cenums.JUSTIFY_LEFT)
_ = l.InstallProperty(PropertyLabel, cdk.StringProperty, true, "")
_ = l.InstallProperty(PropertyMaxWidthChars, cdk.IntProperty, true, -1)
_ = l.InstallProperty(PropertyMnemonicKeyVal, cdk.IntProperty, false, rune(0))
_ = l.InstallProperty(PropertyMnemonicWidget, cdk.StructProperty, true, nil)
_ = l.InstallProperty(PropertySelectable, cdk.BoolProperty, true, false)
_ = l.InstallProperty(PropertySelectionBound, cdk.IntProperty, false, 0)
_ = l.InstallProperty(PropertySingleLineMode, cdk.BoolProperty, true, false)
_ = l.InstallProperty(PropertyTrackVisitedLinks, cdk.BoolProperty, true, true)
_ = l.InstallProperty(PropertyUseMarkup, cdk.BoolProperty, true, false)
_ = l.InstallProperty(PropertyUseUnderline, 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_WORD)
l.Connect(SignalResize, LabelResizeHandle, l.resize)
l.Connect(SignalDraw, LabelDrawHandle, l.draw)
l.Connect(SignalEnter, LabelEnterHandle, l.enter)
l.Connect(SignalLeave, LabelLeaveHandle, l.leave)
// _ = l.SetBoolProperty(PropertyDebug, true)
return false
}
// Build provides customizations to the Buildable system for Label Widgets.
func (l *CLabel) 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 PropertyLabel:
l.SetLabel(v)
default:
element.ApplyProperty(k, v)
}
}
element.ApplySignals()
return nil
}
// SetText updates the text within the Label 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 *CLabel) SetText(text string) {
if err := l.SetBoolProperty(PropertyUseMarkup, false); err != nil {
l.LogErr(err)
}
l.Lock()
l.text = text
l.Unlock()
l.Invalidate()
}
// SetAttributes updates the attributes property to be the given paint.Style.
//
// Parameters:
// attrs a paint.Style
//
// Locking: write
func (l *CLabel) SetAttributes(attrs paint.Style) {
if err := l.SetStructProperty(PropertyAttributes, attrs); err != nil {
l.LogErr(err)
}
}
// SetMarkup parses text which is marked up with the Tango text markup language,
// setting the Label's text and attribute list based on the parse results.
//
// Parameters:
// text a markup string (see Tango markup format)
//
// Locking: write
func (l *CLabel) SetMarkup(text string) (parseError error) {
l.Lock()
l.text = text
l.Unlock()
if err := l.SetBoolProperty(PropertyUseMarkup, true); err != nil {
l.LogErr(err)
}
// Invalidate will call refreshTextBuffer again, we do this once before to
// see if there's any errors generated because we can't return any errors
// encountered in signal handlers (beyond the logging).
if parseError = l.refreshTextBuffer(); parseError == nil {
l.Invalidate()
}
return
}
// SetMarkupWithMnemonic parses str which is marked up with the Tango text
// markup language, setting the label's text and attribute list based on the
// parse results. If characters in str are preceded by an underscore, they are
// underlined indicating that they represent a keyboard accelerator called a
// mnemonic. The mnemonic key can be used to activate another widget, chosen
// automatically, or explicitly using SetMnemonicWidget.
//
// Parameters:
// str a markup string (see Pango markup format)
//
// Locking: write
func (l *CLabel) SetMarkupWithMnemonic(str string) (err error) {
l.SetUseUnderline(true)
err = l.SetMarkup(str)
return
}
// 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 *CLabel) SetJustify(justify cenums.Justification) {
if err := l.SetStructProperty(PropertyJustify, justify); err != nil {
l.LogErr(err)
}
}
// SetEllipsize updates the mode used to ellipsize (add an ellipsis: "...") to
// the text if there is not enough space to render the entire string.
//
// Parameters:
// mode bool
//
// Locking: write
func (l *CLabel) SetEllipsize(mode bool) {
if err := l.SetBoolProperty(PropertyEllipsize, mode); err != nil {
l.LogErr(err)
}
}
// SetWidthChars updates the desired width in characters of label to nChars.
//
// Parameters:
// nChars the new desired width, in characters.
//
// Locking: write
func (l *CLabel) SetWidthChars(nChars int) {
if err := l.SetIntProperty(PropertyWidthChars, nChars); err != nil {
l.LogErr(err)
}
}
// 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 *CLabel) SetMaxWidthChars(nChars int) {
if err := l.SetIntProperty(PropertyMaxWidthChars, nChars); err != nil {
l.LogErr(err)
}
}
// SetLineWrap updates the line wrapping within the Label 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 *CLabel) SetLineWrap(wrap bool) {
if err := l.SetBoolProperty(PropertyWrap, wrap); err != nil {
l.LogErr(err)
}
}
// 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 *CLabel) SetLineWrapMode(wrapMode cenums.WrapMode) {
if err := l.SetStructProperty(PropertyWrapMode, wrapMode); err != nil {
l.LogErr(err)
}
}
// GetMnemonicKeyVal returns the mnemonic character in the Label text if the
// Label has been set so that it has an mnemonic key. Rhis function
// returns the keyval used for the mnemonic accelerator. If there is no
// mnemonic set up it returns `rune(0)`.
//
// Returns:
// value keyval usable for accelerators
//
// Locking: read
func (l *CLabel) GetMnemonicKeyVal() (value rune) {
if l.GetUseUnderline() {
label := l.GetClearText()
l.RLock()
if rxLabelMnemonic.MatchString(label) {
m := rxLabelMnemonic.FindStringSubmatch(label)
if len(m) > 1 {
l.RUnlock()
return rune(strings.ToLower(m[1])[0])
}
}
l.RUnlock()
}
return
}
// GetSelectable returns the value set by SetSelectable.
//
// Locking: read
func (l *CLabel) 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 *CLabel) GetText() (value string) {
value = l.GetCleanText()
return
}
// 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)
//
// Note that usage of this within CTK is unimplemented at this time
//
// Method stub, unimplemented
func (l *CLabel) SelectRegion(startOffset int, endOffset int) {}
// SetMnemonicWidget updates the mnemonic-widget property with the given Widget.
// If the label has been set so that it has a mnemonic key (using i.e.
// SetMarkupWithMnemonic, SetTextWithMnemonic,
// NewWithMnemonic or the use-underline property) the label can be associated
// with a widget that is the target of the mnemonic. When the label is inside a
// widget (like a Button or a Notebook tab) it is automatically associated with
// the correct widget, but sometimes (i.e. when the target is a Entry next to
// the label) you need to set it explicitly using this function. The target
// widget will be accelerated by emitting the mnemonic-activate signal on it.
// The default handler for this signal will activate the widget if there are no
// mnemonic collisions and toggle focus between the colliding widgets otherwise.
//
// Parameters:
// widget the target Widget.
//
// Note that usage of this within CTK is unimplemented at this time
//
// Locking: write
func (l *CLabel) SetMnemonicWidget(widget Widget) {
if err := l.SetStructProperty(PropertyMnemonicWidget, widget); err != nil {
l.LogErr(err)
} else {
l.Invalidate()
}
}
// SetSelectable updates the selectable property for the Label. Labels 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 *CLabel) SetSelectable(setting bool) {
if err := l.SetBoolProperty(PropertySelectable, setting); err != nil {
l.LogErr(err)
}
}
// SetTextWithMnemonic updates the Label's text from the string str. If
// characters in str are preceded by an underscore, they are underlined
// indicating that they represent a keyboard accelerator called a mnemonic. The
// mnemonic key can be used to activate another widget, chosen automatically, or
// explicitly using SetMnemonicWidget.
//
// Parameters:
// str a string
//
// Locking: write
func (l *CLabel) SetTextWithMnemonic(str string) {
l.SetUseUnderline(true)
l.SetText(str)
}
// 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 Label markup (see SetMarkup).
//
// Locking: read
func (l *CLabel) 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 *CLabel) 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
}
// GetEllipsize returns the ellipsizing state of the label.
// See: SetEllipsize()
//
// Locking: read
func (l *CLabel) GetEllipsize() (value bool) {
var err error
if value, err = l.GetBoolProperty(PropertyEllipsize); err != nil {
l.LogErr(err)
}
return
}
// GetWidthChars retrieves the desired width of label, in characters.
// See: SetWidthChars()
//
// Locking: read
func (l *CLabel) 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 *CLabel) GetMaxWidthChars() (value int) {
var err error
if value, err = l.GetIntProperty(PropertyMaxWidthChars); err != nil {
l.LogErr(err)
}
return
}
// GetLabel returns the text from a label widget including any embedded
// underlines indicating mnemonics and Tango markup.
// See: GetText()
//
// Locking: read
func (l *CLabel) GetLabel() (value string) {
var err error
if value, err = l.GetStringProperty(PropertyLabel); err != nil {
l.LogErr(err)
}
return
}
// GetLineWrap returns whether lines in the label are automatically wrapped.
// See: SetLineWrap()
//
// Locking: read
func (l *CLabel) 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 *CLabel) 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
}
// GetMnemonicWidget retrieves the target of the mnemonic (keyboard shortcut) of
// this Label.
// See: SetMnemonicWidget()
//
// Locking: read
func (l *CLabel) GetMnemonicWidget() (value Widget) {
if v, err := l.GetStructProperty(PropertyMnemonicWidget); err == nil {
value, _ = v.(Widget)
} else {
l.LogErr(err)
}
return
}
// GetSelectionBounds returns the selected range of characters in the label,
// returning TRUE for nonEmpty if there's a selection.
//
// Note that usage of this within CTK is unimplemented at this time
//
// Method stub, unimplemented
func (l *CLabel) GetSelectionBounds() (start int, end int, nonEmpty bool) {
return 0, 0, false
}
// GetUseMarkup returns whether the label's text is interpreted as marked up
// with the Tango text markup language.
// See: SetUseMarkup()
//
// Locking: read
func (l *CLabel) GetUseMarkup() (value bool) {
var err error
if value, err = l.GetBoolProperty(PropertyUseMarkup); err != nil {
l.LogErr(err)
}
return
}
// GetUseUnderline returns whether an embedded underline in the label indicates
// a mnemonic.
// See: SetUseUnderline()
//
// Locking: read
func (l *CLabel) GetUseUnderline() (value bool) {
var err error
if value, err = l.GetBoolProperty(PropertyUseUnderline); err != nil {
l.LogErr(err)
}
return
}
// GetSingleLineMode returns whether the label is in single line mode.
//
// Locking: read
func (l *CLabel) GetSingleLineMode() (value bool) {
var err error
if value, err = l.GetBoolProperty(PropertySingleLineMode); err != nil {
l.LogErr(err)
}
return
}
// SetLabel updates the text of the label. The label is interpreted as including
// embedded underlines and/or Pango markup depending on the values of the
// use-underline and use-markup properties.
//
// Parameters:
// str the new text to set for the label
//
// Locking: write
func (l *CLabel) SetLabel(str string) {
if err := l.SetStringProperty(PropertyLabel, str); err != nil {
l.LogErr(err)
} else {
if l.GetUseMarkup() {
if err := l.SetMarkup(str); err != nil {
l.LogErr(err)
}
} else {
l.SetText(str)
}
}
}
// SetUseMarkup updates whether the text of the label contains markup in Tango's
// text markup language.
// See: SetMarkup()
//
// Parameters:
// setting TRUE if the label's text should be parsed for markup.
//
// Locking: write
func (l *CLabel) SetUseMarkup(setting bool) {
if err := l.SetBoolProperty(PropertyUseMarkup, setting); err != nil {
l.LogErr(err)
} else {
l.Invalidate()
}
}
// SetUseUnderline updates the use-underline property for the Lable. If TRUE, an
// underline in the text indicates the next character should be used for the
// mnemonic accelerator key.
//
// Parameters:
// setting TRUE if underlines in the text indicate mnemonics
//
// Locking: write
func (l *CLabel) SetUseUnderline(setting bool) {
if err := l.SetBoolProperty(PropertyUseUnderline, setting); err != nil {
l.LogErr(err)
} else {
l.Invalidate()
}
}
// 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 *CLabel) SetSingleLineMode(singleLineMode bool) {
if err := l.SetBoolProperty(PropertySingleLineMode, singleLineMode); err != nil {
l.LogErr(err)
} else {
l.Invalidate()
}
}
// GetCurrentUri returns the URI for the currently active link in the label. The
// active link is the one under the mouse pointer or, in a selectable label, the
// link in which the text cursor is currently positioned. This function is
// intended for use in a activate-link handler or for use in a
// query-tooltip handler.
//
// Note that usage of this within CTK is unimplemented at this time
//
// Method stub, unimplemented
func (l *CLabel) GetCurrentUri() (value string) {
return ""
}
// SetTrackVisitedLinks updates whether the label should keep track of clicked
// links (and use a different color for them).
//
// Parameters:
// trackLinks TRUE to track visited links
//
// Note that usage of this within CTK is unimplemented at this time
//
// Locking: write
func (l *CLabel) SetTrackVisitedLinks(trackLinks bool) {
if err := l.SetBoolProperty(PropertyTrackVisitedLinks, trackLinks); err != nil {
l.LogErr(err)
}
}
// GetTrackVisitedLinks returns whether the label is currently keeping track of
// clicked links.
//
// Returns:
// TRUE if clicked links are remembered
//
// Note that usage of this within CTK is unimplemented at this time
//
// Locking: read
func (l *CLabel) GetTrackVisitedLinks() (value bool) {
var err error
if value, err = l.GetBoolProperty(PropertyTrackVisitedLinks); err != nil {
l.LogErr(err)
}
return
}
// Settings is a convenience method to return the interesting settings currently
// configured on the Label instance.
//
// Locking: read
func (l *CLabel) Settings() (singleLineMode bool, lineWrapMode cenums.WrapMode, ellipsize bool, justify cenums.Justification, maxWidthChars int) {
singleLineMode = l.GetSingleLineMode()
lineWrapMode = l.GetLineWrapMode()
ellipsize = l.GetEllipsize()
justify = l.GetJustify()
maxWidthChars = l.GetMaxWidthChars()
return
}
// GetClearText returns the Label's text, stripped of markup.
//
// Locking: read
func (l *CLabel) GetClearText() (text string) {
if l.tBuffer == nil {
return ""
}
singleLineMode, lineWrapMode, ellipsize, justify, maxWidthChars := l.Settings()
l.RLock()
text = l.tBuffer.ClearText(lineWrapMode, ellipsize, justify, maxWidthChars)
if singleLineMode {
if strings.Contains(text, "\n") {
if idx := strings.Index(text, "\n"); idx >= 0 {
text = text[:idx]
}
}
}
l.RUnlock()
return
}
// GetPlainText returns the Label's text, stripped of markup and mnemonics.
//
// Locking: read
func (l *CLabel) GetPlainText() (text string) {
if l.tBuffer == nil {
return ""
}
singleLineMode, lineWrapMode, ellipsize, justify, maxWidthChars := l.Settings()
l.RLock()
text = l.tBuffer.PlainText(lineWrapMode, ellipsize, justify, maxWidthChars)
if singleLineMode {
if strings.Contains(text, "\n") {
if idx := strings.Index(text, "\n"); idx >= 0 {
text = text[:idx]
}
}
}
l.RUnlock()
return
}
// GetCleanText filters the result of GetClearText to strip leading underscores.
//
// Locking: read
func (l *CLabel) GetCleanText() (text string) {
plain := l.GetPlainText()
l.RLock()
text = rxLabelPlainText.ReplaceAllString(plain, "$2")
l.RUnlock()
return
}
// GetPlainTextInfo returns the maximum line width and line count.
//
// Locking: read
func (l *CLabel) GetPlainTextInfo() (maxWidth, lineCount int) {
maxWidthChars := l.GetMaxWidthChars()
maxWidth, lineCount = l.GetPlainTextInfoAtWidth(maxWidthChars)
return
}
// GetPlainTextInfoAtWidth returns the maximum line width and line count, with the given width as an override to the
// value set with SetMaxWidthChars. This is used primarily for pre-rendering stages like Resize to determine the size
// allocations without having to render the actual text with Tango first.
//
// Locking: read
func (l *CLabel) GetPlainTextInfoAtWidth(width int) (maxWidth, lineCount int) {
if l.tBuffer == nil {
_ = l.refreshTextBuffer()
}
_, lineWrapMode, ellipsize, justify, _ := l.Settings()
l.RLock()
maxWidth, lineCount = l.tBuffer.PlainTextInfo(lineWrapMode, ellipsize, justify, width)
l.RUnlock()
return
}
// GetSizeRequest returns the requested size of the Label taking into account
// the label's content and any padding set.
//
// Locking: read
func (l *CLabel) GetSizeRequest() (width, height int) {
size := ptypes.NewRectangle(l.CWidget.GetSizeRequest())
if size.W <= -1 {
if wc := l.GetWidthChars(); wc <= -1 {
if mwc := l.GetMaxWidthChars(); mwc <= -1 {
alloc := l.GetAllocation()
if alloc.W > 0 {
size.W, _ = l.GetPlainTextInfoAtWidth(alloc.W)
} else {
size.W, _ = l.GetPlainTextInfo()
}
} else {
size.W = mwc
}
} else {
size.W = wc
}
}
if size.H <= -1 {
_, size.H = l.GetPlainTextInfoAtWidth(size.W)
}
// add padding
xPadding, yPadding := l.GetPadding()
size.W += xPadding * 2
size.H += yPadding * 2
return size.W, size.H
}
func (l *CLabel) 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 *CLabel) refreshMnemonics() {
if w := l.GetWindow(); w != nil {
if widget := l.GetMnemonicWidget(); widget != nil {
w.RemoveWidgetMnemonics(widget)
} else {
if parent := l.GetParent(); parent != nil {
w.RemoveWidgetMnemonics(parent)
}
}
}
if !GetDefaultSettings().GetEnableMnemonics() {
return
}
if l.GetUseUnderline() {
if w := l.GetWindow(); w != nil {
if keyval := l.GetMnemonicKeyVal(); keyval > 0 {
if widget := l.GetMnemonicWidget(); widget != nil {
w.AddMnemonic(keyval, widget)
} else {
if parent := l.GetParent(); parent != nil {
if pw, ok := parent.Self().(Sensitive); ok && pw.IsSensitive() && pw.IsVisible() {
w.AddMnemonic(keyval, parent)
}
}
}
}
}
}
}
func (l *CLabel) refreshTextBuffer() (err error) {
style := l.GetThemeRequest().Content.Normal
useUnderline := l.GetUseUnderline()
l.Lock()
var markup bool
if markup, err = l.GetBoolProperty(PropertyUseMarkup); err == nil && markup {
var m memphis.Tango