-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathimgbutton.go
95 lines (73 loc) · 1.72 KB
/
imgbutton.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
package x11ui
import (
"image"
"github.com/BurntSushi/xgbutil/xgraphics"
"github.com/llgcode/draw2d/draw2dimg"
"log"
)
type ImgButton struct {
*Widget
// Custom properties
text string
lastx, lasty float64
cursor float64
line float64
fname string
}
func NewImgButton(title string, p *Window, dims ...int) *ImgButton {
if p == nil {
log.Fatal("Cannot Create Widget without Application")
}
tbox := new(ImgButton)
tbox.Widget = WidgetFactory(p, dims...)
tbox.fname = "hg.png"
tbox.init()
// tbox.Create(p, dims...)
// tbox.loadTheme()
// pbar.SetValue(0.5)
return tbox
}
func (t *ImgButton) registerHandlers() {
// xevent.KeyPressFun(t.keybHandler).Connect(t.xu, t.xwin.Id)
}
func (i *ImgButton) SetPicture(fname string) {
i.fname = fname
i.addPicture()
i.updateCanvas()
}
func min(x, y int) int {
if x < y {
return x
} else {
return y
}
}
func (t *ImgButton) addPicture() {
img, err := draw2dimg.LoadFromPngFile(t.fname)
if err != nil {
deBug("Background Image", err)
return
}
irect := image.Rectangle{image.Point{0, 0}, image.Point{t.Width(), t.Height()}}
inset := irect.Inset(2)
log.Println(irect, inset)
mx := min(inset.Dx(), inset.Dy())
simg := xgraphics.Scale(img, mx, mx)
log.Print(inset, irect)
// si := t.canvas.SubImage(inset).(*xgraphics.Image)
// xg := xgraphics.NewConvert(t.xu, )
// xg.XDraw()
// xg.XPaintRects(t.xwin.Id, inset)
si := t.canvas.SubImage(inset).(*xgraphics.Image)
xgraphics.Blend(si, simg, image.Point{0, 0})
// si.CreatePixmap()
// si.XDraw()
// si.XPaint(t.xwin.Id)
t.canvas.XSurfaceSet(t.xwin.Id)
t.updateCanvas()
}
func (t *ImgButton) init() {
// t.drawBorder(StateNormal)
// t.updateCanvas()
t.registerHandlers()
}