-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparagraph_windows.go
115 lines (96 loc) · 2.99 KB
/
paragraph_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
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
package goey
import (
"syscall"
"unsafe"
"bitbucket.org/rj/goey/base"
win2 "bitbucket.org/rj/goey/internal/syscall"
"github.com/lxn/win"
)
func (w *P) calcStyle() uint32 {
style := uint32(win.WS_CHILD | win.WS_VISIBLE | win.SS_LEFT)
if w.Align == JustifyCenter {
style = style | win.SS_CENTER
} else if w.Align == JustifyRight {
style = style | win.SS_RIGHT
} else if w.Align == JustifyFull {
style = style | win.SS_RIGHTJUST
}
return style
}
func (w *P) mount(parent base.Control) (base.Element, error) {
// Create the control.
hwnd, text, err := createControlWindow(0, &staticClassName[0], w.Text, w.calcStyle(), parent.HWnd)
if err != nil {
return nil, err
}
retval := ¶graphElement{Control: Control{hwnd}, text: text}
win.SetWindowLongPtr(hwnd, win.GWLP_USERDATA, uintptr(unsafe.Pointer(retval)))
return retval, nil
}
type paragraphElement struct {
Control
text []uint16
}
func (w *paragraphElement) measureReflowLimits() {
hwnd := w.hWnd
hdc := win.GetDC(hwnd)
if hMessageFont != 0 {
win.SelectObject(hdc, win.HGDIOBJ(hMessageFont))
}
// Calculate the width of a single 'm' (find the em width)
rect := win.RECT{0, 0, 0x7fffffff, 0x7fffffff}
caption := [10]uint16{'m', 'm', 'm', 'm', 'm', 'm', 'm', 'm', 'm', 'm'}
win.DrawTextEx(hdc, &caption[0], 10, &rect, win.DT_CALCRECT, nil)
win.ReleaseDC(hwnd, hdc)
paragraphMaxWidth = base.FromPixelsX(int(rect.Right)) * 8
}
func (w *paragraphElement) Props() base.Widget {
align := JustifyLeft
if style := win.GetWindowLong(w.hWnd, win.GWL_STYLE); style&win.SS_CENTER == win.SS_CENTER {
align = JustifyCenter
} else if style&win.SS_RIGHT == win.SS_RIGHT {
align = JustifyRight
} else if style&win.SS_RIGHTJUST == win.SS_RIGHTJUST {
align = JustifyFull
}
return &P{
Text: w.Control.Text(),
Align: align,
}
}
func (w *paragraphElement) MinIntrinsicHeight(width base.Length) base.Length {
if width == base.Inf {
width = w.maxReflowWidth()
}
hdc := win.GetDC(w.hWnd)
if hMessageFont != 0 {
win.SelectObject(hdc, win.HGDIOBJ(hMessageFont))
}
rect := win.RECT{0, 0, int32(width.PixelsX()), 0x7fffffff}
win.DrawTextEx(hdc, &w.text[0], int32(len(w.text)), &rect, win.DT_CALCRECT|win.DT_WORDBREAK, nil)
win.ReleaseDC(w.hWnd, hdc)
return base.FromPixelsY(int(rect.Bottom))
}
func (w *paragraphElement) MinIntrinsicWidth(height base.Length) base.Length {
if height != base.Inf {
panic("not implemented")
}
width, _ := w.CalcRect(w.text)
return min(base.FromPixelsX(int(width)), w.minReflowWidth())
}
func (w *paragraphElement) SetBounds(bounds base.Rectangle) {
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 *paragraphElement) updateProps(data *P) error {
text, err := syscall.UTF16FromString(data.Text)
if err != nil {
return err
}
w.text = text
win2.SetWindowText(w.hWnd, &text[0])
win.SetWindowLongPtr(w.hWnd, win.GWL_STYLE, uintptr(data.calcStyle()))
return nil
}