/
tableview.go
148 lines (113 loc) · 2.89 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
// 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"
"strings"
"time"
)
import (
"github.com/lxn/walk"
)
type Foo struct {
Bar string
Baz float64
Quux time.Time
checked bool
}
type FooModel struct {
walk.TableModelBase
items []*Foo
}
// Make sure we implement all required interfaces.
var _ walk.TableModel = &FooModel{}
var _ walk.ItemChecker = &FooModel{}
func NewFooModel() *FooModel {
m := new(FooModel)
m.ResetRows()
return m
}
// Called by the TableView from SetModel to retrieve column information.
func (m *FooModel) Columns() []walk.TableColumn {
return []walk.TableColumn{
{Title: "#"},
{Title: "Bar"},
{Title: "Baz", Format: "%.2f", Alignment: walk.AlignFar},
{Title: "Quux", Format: "2006-01-02 15:04:05", Width: 150},
}
}
// 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 row
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
}
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{
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()
}
type MainWindow struct {
*walk.MainWindow
model *FooModel
}
func main() {
walk.Initialize(walk.InitParams{PanicOnError: true})
defer walk.Shutdown()
rand.Seed(time.Now().UnixNano())
mainWnd, _ := walk.NewMainWindow()
mw := &MainWindow{
MainWindow: mainWnd,
model: NewFooModel(),
}
mw.SetLayout(walk.NewVBoxLayout())
mw.SetTitle("Walk TableView Example")
resetRowsButton, _ := walk.NewPushButton(mw)
resetRowsButton.SetText("Reset Rows")
resetRowsButton.Clicked().Attach(func() {
// Get some fresh data.
mw.model.ResetRows()
})
tableView, _ := walk.NewTableView(mw)
// Everybody loves check boxes.
tableView.SetCheckBoxes(true)
// Don't forget to set the model.
tableView.SetModel(mw.model)
mw.SetMinMaxSize(walk.Size{320, 240}, walk.Size{})
mw.SetSize(walk.Size{800, 600})
mw.Show()
mw.Run()
}