summaryrefslogtreecommitdiffstatshomepage
path: root/linklabel.go
blob: d959e092d7d01eaa5593db17917addcca50880fd (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
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
// Copyright 2017 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 (
	"syscall"
	"unsafe"

	"github.com/lxn/win"
)

type LinkLabel struct {
	WidgetBase
	textChangedPublisher   EventPublisher
	linkActivatedPublisher LinkLabelLinkEventPublisher
}

func NewLinkLabel(parent Container) (*LinkLabel, error) {
	ll := new(LinkLabel)

	if err := InitWidget(
		ll,
		parent,
		"SysLink",
		win.WS_TABSTOP|win.WS_VISIBLE,
		0); err != nil {
		return nil, err
	}

	ll.SetBackground(nullBrushSingleton)

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

	return ll, nil
}

func (ll *LinkLabel) MinSizeHint() Size {
	var s win.SIZE

	ll.SendMessage(win.LM_GETIDEALSIZE, uintptr(ll.maxSize.Width), uintptr(unsafe.Pointer(&s)))

	return Size{int(s.CX), int(s.CY)}
}

func (ll *LinkLabel) SizeHint() Size {
	return ll.MinSizeHint()
}

func (ll *LinkLabel) Text() string {
	return ll.text()
}

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

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

	return ll.updateParentLayout()
}

func (ll *LinkLabel) LinkActivated() *LinkLabelLinkEvent {
	return ll.linkActivatedPublisher.Event()
}

func (ll *LinkLabel) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
	switch msg {
	case win.WM_NOTIFY:
		nml := (*win.NMLINK)(unsafe.Pointer(lParam))

		switch nml.Hdr.Code {
		case win.NM_CLICK, win.NM_RETURN:
			link := &LinkLabelLink{
				ll:    ll,
				index: int(nml.Item.ILink),
				id:    syscall.UTF16ToString(nml.Item.SzID[:]),
				url:   syscall.UTF16ToString(nml.Item.SzUrl[:]),
			}

			ll.linkActivatedPublisher.Publish(link)
		}

	case win.WM_KILLFOCUS:
		ll.ensureStyleBits(win.WS_TABSTOP, true)

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

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

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

type LinkLabelLinkEventHandler func(link *LinkLabelLink)

type LinkLabelLinkEvent struct {
	handlers []LinkLabelLinkEventHandler
}

func (e *LinkLabelLinkEvent) Attach(handler LinkLabelLinkEventHandler) int {
	for i, h := range e.handlers {
		if h == nil {
			e.handlers[i] = handler
			return i
		}
	}

	e.handlers = append(e.handlers, handler)
	return len(e.handlers) - 1
}

func (e *LinkLabelLinkEvent) Detach(handle int) {
	e.handlers[handle] = nil
}

type LinkLabelLinkEventPublisher struct {
	event LinkLabelLinkEvent
}

func (p *LinkLabelLinkEventPublisher) Event() *LinkLabelLinkEvent {
	return &p.event
}

func (p *LinkLabelLinkEventPublisher) Publish(link *LinkLabelLink) {
	for _, handler := range p.event.handlers {
		if handler != nil {
			handler(link)
		}
	}
}

type LinkLabelLink struct {
	ll    *LinkLabel
	index int
	id    string
	url   string
}

func (lll *LinkLabelLink) Index() int {
	return lll.index
}

func (lll *LinkLabelLink) Id() string {
	return lll.id
}

func (lll *LinkLabelLink) URL() string {
	return lll.url
}

func (lll *LinkLabelLink) Enabled() (bool, error) {
	return lll.hasState(win.LIS_ENABLED)
}

func (lll *LinkLabelLink) SetEnabled(enabled bool) error {
	return lll.setState(win.LIS_ENABLED, enabled)
}

func (lll *LinkLabelLink) Focused() (bool, error) {
	return lll.hasState(win.LIS_FOCUSED)
}

func (lll *LinkLabelLink) SetFocused(focused bool) error {
	return lll.setState(win.LIS_FOCUSED, focused)
}

func (lll *LinkLabelLink) Visited() (bool, error) {
	return lll.hasState(win.LIS_VISITED)
}

func (lll *LinkLabelLink) SetVisited(visited bool) error {
	return lll.setState(win.LIS_VISITED, visited)
}

func (lll *LinkLabelLink) hasState(state uint32) (bool, error) {
	li := win.LITEM{
		ILink:     int32(lll.index),
		Mask:      win.LIF_ITEMINDEX | win.LIF_STATE,
		StateMask: state,
	}

	if win.TRUE != lll.ll.SendMessage(win.LM_GETITEM, 0, uintptr(unsafe.Pointer(&li))) {
		return false, newError("LM_GETITEM")
	}

	return li.State&state == state, nil
}

func (lll *LinkLabelLink) setState(state uint32, set bool) error {
	li := win.LITEM{
		Mask:      win.LIF_STATE,
		StateMask: state,
	}

	if set {
		li.State = state
	}

	li.Mask |= win.LIF_ITEMINDEX
	li.ILink = int32(lll.index)

	if win.TRUE != lll.ll.SendMessage(win.LM_SETITEM, 0, uintptr(unsafe.Pointer(&li))) {
		return newError("LM_SETITEM")
	}

	return nil
}