/
tableview.go
187 lines (148 loc) · 3.51 KB
/
tableview.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2011 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"math/rand"
"sort"
"strings"
"time"
)
import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)
type Foo struct {
Index int
Bar string
Baz float64
Quux time.Time
checked bool
}
type FooModel struct {
walk.TableModelBase
walk.SorterBase
sortColumn int
sortOrder walk.SortOrder
evenBitmap *walk.Bitmap
oddIcon *walk.Icon
items []*Foo
}
func NewFooModel() *FooModel {
m := new(FooModel)
m.evenBitmap, _ = walk.NewBitmapFromFile("../img/open.png")
m.oddIcon, _ = walk.NewIconFromFile("../img/x.ico")
m.ResetRows()
return m
}
// Called by the TableView from SetModel and every time the model publishes a
// RowsReset event.
func (m *FooModel) RowCount() int {
return len(m.items)
}
// Called by the TableView when it needs the text to display for a given cell.
func (m *FooModel) Value(row, col int) interface{} {
item := m.items[row]
switch col {
case 0:
return item.Index
case 1:
return item.Bar
case 2:
return item.Baz
case 3:
return item.Quux
}
panic("unexpected col")
}
// Called by the TableView to retrieve if a given row is checked.
func (m *FooModel) Checked(row int) bool {
return m.items[row].checked
}
// Called by the TableView when the user toggled the check box of a given row.
func (m *FooModel) SetChecked(row int, checked bool) error {
m.items[row].checked = checked
return nil
}
// Called by the TableView to sort the model.
func (m *FooModel) Sort(col int, order walk.SortOrder) error {
m.sortColumn, m.sortOrder = col, order
sort.Sort(m)
return m.SorterBase.Sort(col, order)
}
func (m *FooModel) Len() int {
return len(m.items)
}
func (m *FooModel) Less(i, j int) bool {
a, b := m.items[i], m.items[j]
c := func(ls bool) bool {
if m.sortOrder == walk.SortAscending {
return ls
}
return !ls
}
switch m.sortColumn {
case 0:
return c(a.Index < b.Index)
case 1:
return c(a.Bar < b.Bar)
case 2:
return c(a.Baz < b.Baz)
case 3:
return c(a.Quux.Before(b.Quux))
}
panic("unreachable")
}
func (m *FooModel) Swap(i, j int) {
m.items[i], m.items[j] = m.items[j], m.items[i]
}
// Called by the TableView to retrieve an item image.
func (m *FooModel) Image(row int) interface{} {
if m.items[row].Index%2 == 0 {
return m.evenBitmap
}
return m.oddIcon
}
func (m *FooModel) ResetRows() {
// Create some random data.
m.items = make([]*Foo, rand.Intn(50000))
now := time.Now()
for i := range m.items {
m.items[i] = &Foo{
Index: i,
Bar: strings.Repeat("*", rand.Intn(5)+1),
Baz: rand.Float64() * 1000,
Quux: time.Unix(rand.Int63n(now.Unix()), 0),
}
}
// Notify TableView and other interested parties about the reset.
m.PublishRowsReset()
m.Sort(m.sortColumn, m.sortOrder)
}
func main() {
rand.Seed(time.Now().UnixNano())
model := NewFooModel()
MainWindow{
Title: "Walk TableView Example",
Size: Size{800, 600},
Layout: VBox{MarginsZero: true},
Children: []Widget{
PushButton{
Text: "Reset Rows",
OnClicked: model.ResetRows,
},
TableView{
AlternatingRowBGColor: walk.RGB(255, 255, 224),
CheckBoxes: true,
ColumnsOrderable: true,
Columns: []TableViewColumn{
{Title: "#"},
{Title: "Bar"},
{Title: "Baz", Format: "%.2f", Alignment: AlignFar},
{Title: "Quux", Format: "2006-01-02 15:04:05", Width: 150},
},
Model: model,
},
},
}.Run()
}