summaryrefslogtreecommitdiffstatshomepage
path: root/label.go
blob: 00de0f4a9d3822bd594203af0bf80d91f307c74d (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// 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

import (
	"github.com/lxn/win"
)

type Label struct {
	WidgetBase
	textChangedPublisher EventPublisher
	textColor            Color
}

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

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

	if err := InitWidget(
		l,
		parent,
		"STATIC",
		win.WS_VISIBLE|win.SS_CENTERIMAGE|style,
		0); err != nil {
		return nil, err
	}

	l.SetBackground(nullBrushSingleton)

	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 (*Label) LayoutFlags() LayoutFlags {
	return GrowableVert | GrowableHorz
}

func (l *Label) MinSizeHint() Size {
	return l.calculateTextSize()
}

func (l *Label) SizeHint() Size {
	return l.MinSizeHint()
}

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

func (l *Label) SetText(value string) error {
	if value == l.Text() {
		return nil
	}

	if err := l.setText(value); err != nil {
		return err
	}

	return l.updateParentLayout()
}

func (l *Label) TextColor() Color {
	return l.textColor
}

func (l *Label) SetTextColor(c Color) {
	l.textColor = c

	l.Invalidate()
}

func (l *Label) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
	switch msg {
	case win.WM_NCHITTEST:
		return win.HTCLIENT

	case win.WM_SETTEXT:
		l.textChangedPublisher.Publish()

	case win.WM_SIZE, win.WM_SIZING:
		l.Invalidate()
	}

	return l.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}