forked from skelterjohn/go.uik
/
label.go
72 lines (56 loc) · 1.23 KB
/
label.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
package uik
import (
"code.google.com/p/draw2d/draw2d"
"image/color"
)
type Label struct {
Block
Text string
TextCh chan string
FontSize float64
FontSizeCh chan float64
}
func NewLabel(text string) (l *Label) {
l = new(Label)
l.MakeChannels()
l.Size = Coord{100, 50}
l.TextCh = make(chan string)
l.FontSizeCh = make(chan float64)
go l.handleEvents()
l.TextCh <- text
l.FontSizeCh <- 12
l.Paint = func(gc draw2d.GraphicContext) {
l.draw(gc)
}
return
}
func (l *Label) draw(gc draw2d.GraphicContext) {
gc.SetStrokeColor(color.Black)
font := draw2d.GetFont(gc.GetFontData())
bounds := font.Bounds()
height := float64(bounds.YMax - bounds.YMin)*l.FontSize/float64(font.UnitsPerEm())
offset := l.Size.Y - (l.Size.Y - height) / 2
safeRect(gc, Coord{0, 0}, l.Size)
gc.FillStroke()
gc.Translate(10, offset)
gc.SetFontSize(l.FontSize)
gc.FillString(l.Text)
}
func (l *Label) handleEvents() {
for {
select {
case l.Text = <-l.TextCh:
if l.Parent != nil {
l.Parent.Redraw <- l.BoundsInParent()
}
case l.FontSize = <-l.FontSizeCh:
if l.Parent != nil {
l.Parent.Redraw <- l.BoundsInParent()
}
case <-l.Draw:
bgc := l.PrepareBuffer()
l.doPaint(bgc)
l.ParentDrawBuffer <- l.Buffer
}
}
}