summaryrefslogtreecommitdiffstatshomepage
path: root/label.go
blob: 259447392017c522d0d64c2db051915d48fd78ab (plain) (blame)
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
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

package walk

type Label struct {
	static
	textChangedPublisher EventPublisher
}

func NewLabel(parent Container) (*Label, error) {
	return NewLabelWithStyle(parent, 0)
}

func NewLabelWithStyle(parent Container, style uint32) (*Label, error) {
	l := new(Label)

	if err := l.init(l, parent); err != nil {
		return nil, err
	}

	l.SetTextAlignment(AlignNear)

	l.MustRegisterProperty("Text", NewProperty(
		func() interface{} {
			return l.Text()
		},
		func(v interface{}) error {
			return l.SetText(assertStringOr(v, ""))
		},
		l.textChangedPublisher.Event()))

	return l, nil
}

func (l *Label) asStatic() *static {
	return &l.static
}

func (l *Label) LayoutFlags() LayoutFlags {
	if l.TextAlignment() == AlignNear {
		return GrowableVert
	}

	return GrowableHorz | GrowableVert
}

func (l *Label) TextAlignment() Alignment1D {
	return l.textAlignment1D()
}

func (l *Label) SetTextAlignment(alignment Alignment1D) error {
	if alignment == AlignDefault {
		alignment = AlignNear
	}

	return l.setTextAlignment1D(alignment)
}

func (l *Label) Text() string {
	return l.text()
}

func (l *Label) SetText(text string) error {
	if changed, err := l.setText(text); err != nil {
		return err
	} else if !changed {
		return nil
	}

	l.textChangedPublisher.Publish()

	return nil
}