summaryrefslogtreecommitdiffstatshomepage
path: root/tooltip.go
blob: e16866cf3360113698ca5ba09dbe4522c06bcb3a (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
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
// 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 (
	"syscall"
	"unsafe"

	"github.com/lxn/win"
)

// https://msdn.microsoft.com/en-us/library/windows/desktop/bb760416(v=vs.85).aspx says 80,
// but in reality, that hasn't been enforced for many many Windows versions. So we give it
// 1024 instead.
const maxToolTipTextLen = 1024 // including NUL terminator

type ToolTip struct {
	WindowBase
}

func NewToolTip() (*ToolTip, error) {
	tt, err := newToolTip(0)
	if err != nil {
		return nil, err
	}

	win.SetWindowPos(tt.hWnd, win.HWND_TOPMOST, 0, 0, 0, 0, win.SWP_NOMOVE|win.SWP_NOSIZE|win.SWP_NOACTIVATE)

	return tt, nil
}

func newToolTip(style uint32) (*ToolTip, error) {
	tt := new(ToolTip)

	if err := InitWindow(
		tt,
		nil,
		"tooltips_class32",
		win.WS_DISABLED|win.WS_POPUP|win.TTS_ALWAYSTIP|style,
		0); err != nil {
		return nil, err
	}

	succeeded := false
	defer func() {
		if !succeeded {
			tt.Dispose()
		}
	}()

	tt.SendMessage(win.TTM_SETMAXTIPWIDTH, 0, 300)

	succeeded = true

	return tt, nil
}

func (tt *ToolTip) Title() string {
	var gt win.TTGETTITLE

	buf := make([]uint16, 100)

	gt.DwSize = uint32(unsafe.Sizeof(gt))
	gt.Cch = uint32(len(buf))
	gt.PszTitle = &buf[0]

	tt.SendMessage(win.TTM_GETTITLE, 0, uintptr(unsafe.Pointer(&gt)))

	return syscall.UTF16ToString(buf)
}

func (tt *ToolTip) SetTitle(title string) error {
	return tt.setTitle(title, win.TTI_NONE)
}

func (tt *ToolTip) SetInfoTitle(title string) error {
	return tt.setTitle(title, win.TTI_INFO)
}

func (tt *ToolTip) SetWarningTitle(title string) error {
	return tt.setTitle(title, win.TTI_WARNING)
}

func (tt *ToolTip) SetErrorTitle(title string) error {
	return tt.setTitle(title, win.TTI_ERROR)
}

func (tt *ToolTip) setTitle(title string, icon uintptr) error {
	if len(title) > 99 {
		title = title[:99]
	}

	if win.FALSE == tt.SendMessage(win.TTM_SETTITLE, icon, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title)))) {
		return newError("TTM_SETTITLE failed")
	}

	return nil
}

func (tt *ToolTip) track(tool Widget) error {
	form := tool.Form()
	if form == nil {
		return nil
	}
	// HACK: We may have to delay this until the form is fully up to avoid glitches.
	if !form.AsFormBase().started {
		form.Starting().Once(func() {
			tt.track(tool)
		})
		return nil
	}

	ti := tt.toolInfo(tool.Handle())
	if ti == nil {
		return newError("unknown tool")
	}

	tt.SendMessage(win.TTM_TRACKACTIVATE, 1, uintptr(unsafe.Pointer(ti)))

	b := tool.BoundsPixels()

	p := Point{0, b.Y + b.Height}.toPOINT()
	if form.RightToLeftLayout() {
		p.X = int32(b.X - b.Width/2)
	} else {
		p.X = int32(b.X + b.Width/2)
	}

	win.ClientToScreen(tool.Parent().Handle(), &p)

	tt.SendMessage(win.TTM_TRACKPOSITION, 0, uintptr(win.MAKELONG(uint16(p.X), uint16(p.Y))))

	var insertAfterHWND win.HWND
	if form := tool.Form(); form != nil && win.GetForegroundWindow() == form.Handle() {
		insertAfterHWND = win.HWND_TOP
	} else {
		insertAfterHWND = tool.Handle()
	}
	win.SetWindowPos(tt.hWnd, insertAfterHWND, 0, 0, 0, 0, win.SWP_NOMOVE|win.SWP_NOSIZE|win.SWP_NOACTIVATE)

	return nil
}

func (tt *ToolTip) untrack(tool Widget) error {
	ti := tt.toolInfo(tool.Handle())
	if ti == nil {
		return newError("unknown tool")
	}

	tt.SendMessage(win.TTM_TRACKACTIVATE, 0, uintptr(unsafe.Pointer(ti)))

	return nil
}

func (tt *ToolTip) AddTool(tool Widget) error {
	return tt.addTool(tt.hwndForTool(tool), false)
}

func (tt *ToolTip) addTrackedTool(tool Widget) error {
	return tt.addTool(tt.hwndForTool(tool), true)
}

func (tt *ToolTip) addTool(hwnd win.HWND, track bool) error {
	if hwnd == 0 {
		return nil
	}

	var ti win.TOOLINFO
	ti.CbSize = uint32(unsafe.Sizeof(ti))
	ti.Hwnd = hwnd
	ti.UFlags = win.TTF_IDISHWND
	if track {
		ti.UFlags |= win.TTF_TRACK
	} else {
		ti.UFlags |= win.TTF_SUBCLASS
	}
	ti.UId = uintptr(hwnd)

	if win.FALSE == tt.SendMessage(win.TTM_ADDTOOL, 0, uintptr(unsafe.Pointer(&ti))) {
		return newError("TTM_ADDTOOL failed")
	}

	return nil
}

func (tt *ToolTip) RemoveTool(tool Widget) error {
	return tt.removeTool(tt.hwndForTool(tool))
}

func (tt *ToolTip) removeTool(hwnd win.HWND) error {
	var ti win.TOOLINFO
	ti.CbSize = uint32(unsafe.Sizeof(ti))
	ti.Hwnd = hwnd
	ti.UId = uintptr(hwnd)

	tt.SendMessage(win.TTM_DELTOOL, 0, uintptr(unsafe.Pointer(&ti)))

	return nil
}

func (tt *ToolTip) Text(tool Widget) string {
	return tt.text(tt.hwndForTool(tool))
}

func (tt *ToolTip) text(hwnd win.HWND) string {
	ti := tt.toolInfo(hwnd)
	if ti == nil {
		return ""
	}

	return win.UTF16PtrToString(ti.LpszText)
}

func (tt *ToolTip) SetText(tool Widget, text string) error {
	return tt.setText(tt.hwndForTool(tool), text)
}

func (tt *ToolTip) setText(hwnd win.HWND, text string) error {
	ti := tt.toolInfo(hwnd)
	if ti == nil {
		return newError("unknown tool")
	}

	n := 0
	for i, r := range text {
		if r < 0x10000 {
			n++
		} else {
			n += 2 // surrogate pair
		}
		if n >= maxToolTipTextLen {
			text = text[:i]
			break
		}
	}

	ti.LpszText = syscall.StringToUTF16Ptr(text)

	tt.SendMessage(win.TTM_SETTOOLINFO, 0, uintptr(unsafe.Pointer(ti)))

	return nil
}

func (tt *ToolTip) toolInfo(hwnd win.HWND) *win.TOOLINFO {
	var ti win.TOOLINFO
	var buf [maxToolTipTextLen]uint16

	ti.CbSize = uint32(unsafe.Sizeof(ti))
	ti.Hwnd = hwnd
	ti.UId = uintptr(hwnd)
	ti.LpszText = &buf[0]

	if win.FALSE == tt.SendMessage(win.TTM_GETTOOLINFO, 0, uintptr(unsafe.Pointer(&ti))) {
		return nil
	}

	return &ti
}

func (*ToolTip) hwndForTool(tool Widget) win.HWND {
	if hftt, ok := tool.(interface{ handleForToolTip() win.HWND }); ok {
		return hftt.handleForToolTip()
	}

	return tool.Handle()
}