-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlabel_windows.go
86 lines (69 loc) · 2.01 KB
/
label_windows.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
package goey
import (
"syscall"
"unsafe"
"bitbucket.org/rj/goey/base"
win2 "bitbucket.org/rj/goey/internal/syscall"
"github.com/lxn/win"
)
var (
staticClassName []uint16
)
func init() {
staticClassName = []uint16{'S', 'T', 'A', 'T', 'I', 'C', 0}
}
func (w *Label) mount(parent base.Control) (base.Element, error) {
// Create the control
const STYLE = win.WS_CHILD | win.WS_VISIBLE | win.SS_LEFT
hwnd, text, err := createControlWindow(0, &staticClassName[0], w.Text, STYLE, parent.HWnd)
if err != nil {
return nil, err
}
retval := &labelElement{Control: Control{hwnd}, text: text}
win.SetWindowLongPtr(hwnd, win.GWLP_USERDATA, uintptr(unsafe.Pointer(retval)))
return retval, nil
}
type labelElement struct {
Control
text []uint16
}
func (w *labelElement) Props() base.Widget {
return &Label{
Text: w.Control.Text(),
}
}
func (w *labelElement) Layout(bc base.Constraints) base.Size {
width := w.MinIntrinsicWidth(0)
height := w.MinIntrinsicHeight(0)
return bc.Constrain(base.Size{width, height})
}
func (w *labelElement) MinIntrinsicHeight(base.Length) base.Length {
// https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
return 13 * DIP
}
func (w *labelElement) MinIntrinsicWidth(base.Length) base.Length {
width, _ := w.CalcRect(w.text)
return base.FromPixelsX(int(width))
}
func (w *labelElement) SetBounds(bounds base.Rectangle) {
// Because of descenders in text, we may want to increase the height
// of the label.
_, height := w.CalcRect(w.text)
if h := base.FromPixelsY(int(height)); h > bounds.Dy() {
bounds.Max.Y = bounds.Min.Y + h
}
w.Control.SetBounds(bounds)
// Not certain why this is required. However, static controls don't
// repaint when resized. This forces a repaint.
win.InvalidateRect(w.hWnd, nil, true)
}
func (w *labelElement) updateProps(data *Label) error {
text, err := syscall.UTF16FromString(data.Text)
if err != nil {
return err
}
w.text = text
win2.SetWindowText(w.hWnd, &text[0])
// TODO: Update alignment
return nil
}