/
gadget.go
131 lines (103 loc) · 2 KB
/
gadget.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package windigo
import "sync"
type Gadget interface {
Manage(Object)
}
type GadgetType struct {
TopLeft
WidthHeight
MinSize WidthHeight
Color
Gravity GravityType
Elastic ElasticType
BackingStore []Cell
// comm[n].yin and comm[n].yang channels for communication with
// parent window(comm[0]) and subordinate widgets(n > 0).
Children []Object
Comm []IChing
managed bool
wg sync.WaitGroup
Parent Container
}
func NewGadget(r *Region, fg, bg Attribute) *GadgetType {
g := new(GadgetType)
g.X = r.X
g.Y = r.Y
g.W = r.W
g.H = r.H
g.Fg = fg
g.Bg = bg
// Gravity and Elastic will default to GravityNone and ElasticNone
// managed will default to false and Parent, Children and Comm nil.
return g
}
func (g *GadgetType) Loc() (int, int) {
return g.X, g.Y
}
func (g *GadgetType) SetLoc(x, y int) {
g.X = x
g.Y = y
}
func (g *GadgetType) Size() (int, int) {
return g.W, g.H
}
func (g *GadgetType) SetSize(width, height int) {
g.W = width
g.H = height
}
func (g *GadgetType) Colors() (Attribute, Attribute) {
return g.Fg, g.Bg
}
func (g *GadgetType) SetColors(fg, bg Attribute) {
g.Fg = fg
g.Bg = bg
}
func (g *GadgetType) Ancestor() Container {
return g.Parent
}
func (g *GadgetType) SetAncestor(p Container) {
g.Parent = p
}
func (g *GadgetType) Init() error {
g.wg.Add(1)
go g.EventMgr()
return nil
}
func (g *GadgetType) AddComm(c IChing) {
g.Comm = append(g.Comm, c)
}
func (g *GadgetType) GetComm() []IChing {
return g.Comm
}
func (g *GadgetType) AddChild(o Object) {
g.Children = append(g.Children, o)
}
func (g *GadgetType) Managed() bool {
return g.managed
}
func (g *GadgetType) SetManaged() {
g.managed = true
}
func (g *GadgetType) Manage(o Object) error {
Manage(g, o)
o.Init()
Flush()
return nil
}
func (g *GadgetType) Clear() {
Fill(g, ' ')
}
func (g *GadgetType) Refresh() {
g.Clear()
for _, w := range g.Children {
w.Refresh()
}
}
func (g *GadgetType) EventMgr() {
EventMgr(g)
p := g.Ancestor()
p.Done()
}
func (g *GadgetType) Done() {
g.wg.Done()
}