forked from czkoko/ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
table.go
228 lines (193 loc) · 7.67 KB
/
table.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// 26 august 2018
package ui
import (
"unsafe"
)
// #include "pkgui.h"
import "C"
// TableModelColumnNeverEditable and
// TableModelColumnAlwaysEditable are the value of an editable
// model column parameter to one of the Table create column
// functions; if used, that jparticular Table colum is not editable
// by the user and always editable by the user, respectively.
const (
TableModelColumnNeverEditable = -1
TableModelColumnAlwaysEditable = -2
)
// TableTextColumnOptionalParams are the optional parameters
// that control the appearance of the text column of a Table.
type TableTextColumnOptionalParams struct {
// ColorModelColumn is the model column containing the
// text color of this Table column's text, or -1 to use the
// default color.
//
// If CellValue for this column for any cell returns nil, that
// cell will also use the default text color.
ColorModelColumn int
}
func (p *TableTextColumnOptionalParams) toLibui() *C.uiTableTextColumnOptionalParams {
if p == nil {
return nil
}
cp := C.pkguiAllocTableTextColumnOptionalParams()
cp.ColorModelColumn = C.int(p.ColorModelColumn)
return cp
}
// TableParams defines the parameters passed to NewTable.
type TableParams struct {
// Model is the TableModel to use for this uiTable.
// This parameter cannot be nil.
Model *TableModel
// RowBackgroundColorModelColumn is a model column
// number that defines the background color used for the
// entire row in the Table, or -1 to use the default color for
// all rows.
//
// If CellValue for this column for any row returns NULL, that
// row will also use the default background color.
RowBackgroundColorModelColumn int
}
func (p *TableParams) toLibui() *C.uiTableParams {
cp := C.pkguiAllocTableParams()
cp.Model = p.Model.m
cp.RowBackgroundColorModelColumn = C.int(p.RowBackgroundColorModelColumn)
return cp
}
// Table is a Control that shows tabular data, allowing users to
// manipulate rows of such data at a time.
type Table struct {
ControlBase
t *C.uiTable
onClicked func(*Table, int)
onDoubleClicked func(*Table, int)
onHeaderClicked func(*Table, int)
}
// NewTable creates a new Table with the specified parameters.
func NewTable(p *TableParams) *Table {
t := new(Table)
cp := p.toLibui()
t.t = C.uiNewTable(cp)
C.pkguiTableRowOnClicked(t.t)
C.pkguiTableRowOnDoubleClicked(t.t)
C.pkguiTableHeaderOnClicked(t.t)
C.pkguiFreeTableParams(cp)
t.ControlBase = NewControlBase(t, uintptr(unsafe.Pointer(t.t)))
return t
}
// AppendTextColumn appends a text column to t. name is
// displayed in the table header. textModelColumn is where the text
// comes from. If a row is editable according to
// textEditableModelColumn, SetCellValue is called with
// textModelColumn as the column.
func (t *Table) AppendTextColumn(name string, textModelColumn int, textEditableModelColumn int, textParams *TableTextColumnOptionalParams) {
cname := C.CString(name)
defer freestr(cname)
cp := textParams.toLibui()
defer C.pkguiFreeTableTextColumnOptionalParams(cp)
C.uiTableAppendTextColumn(t.t, cname, C.int(textModelColumn), C.int(textEditableModelColumn), cp)
}
// AppendImageColumn appends an image column to t.
// Images are drawn at icon size, appropriate to the pixel density
// of the screen showing the Table.
func (t *Table) AppendImageColumn(name string, imageModelColumn int) {
cname := C.CString(name)
defer freestr(cname)
C.uiTableAppendImageColumn(t.t, cname, C.int(imageModelColumn))
}
// AppendImageTextColumn appends a column to t that
// shows both an image and text.
func (t *Table) AppendImageTextColumn(name string, imageModelColumn int, textModelColumn int, textEditableModelColumn int, textParams *TableTextColumnOptionalParams) {
cname := C.CString(name)
defer freestr(cname)
cp := textParams.toLibui()
defer C.pkguiFreeTableTextColumnOptionalParams(cp)
C.uiTableAppendImageTextColumn(t.t, cname, C.int(imageModelColumn), C.int(textModelColumn), C.int(textEditableModelColumn), cp)
}
// AppendCheckboxColumn appends a column to t that
// contains a checkbox that the user can interact with (assuming the
// checkbox is editable). SetCellValue will be called with
// checkboxModelColumn as the column in this case.
func (t *Table) AppendCheckboxColumn(name string, checkboxModelColumn int, checkboxEditableModelColumn int) {
cname := C.CString(name)
defer freestr(cname)
C.uiTableAppendCheckboxColumn(t.t, cname, C.int(checkboxModelColumn), C.int(checkboxEditableModelColumn))
}
// AppendCheckboxTextColumn appends a column to t
// that contains both a checkbox and text.
func (t *Table) AppendCheckboxTextColumn(name string, checkboxModelColumn int, checkboxEditableModelColumn int, textModelColumn int, textEditableModelColumn int, textParams *TableTextColumnOptionalParams) {
cname := C.CString(name)
defer freestr(cname)
cp := textParams.toLibui()
defer C.pkguiFreeTableTextColumnOptionalParams(cp)
C.uiTableAppendCheckboxTextColumn(t.t, cname, C.int(checkboxModelColumn), C.int(checkboxEditableModelColumn), C.int(textModelColumn), C.int(textEditableModelColumn), cp)
}
// AppendProgressBarColumn appends a column to t
// that displays a progress bar. These columns work like
// ProgressBar: a cell value of 0..100 displays that percentage, and
// a cell value of -1 displays an indeterminate progress bar.
func (t *Table) AppendProgressBarColumn(name string, progressModelColumn int) {
cname := C.CString(name)
defer freestr(cname)
C.uiTableAppendProgressBarColumn(t.t, cname, C.int(progressModelColumn))
}
// AppendButtonColumn appends a column to t
// that shows a button that the user can click on. When the user
// does click on the button, SetCellValue is called with a nil
// value and buttonModelColumn as the column.
// CellValue on buttonModelColumn should return the text to show
// in the button.
func (t *Table) AppendButtonColumn(name string, buttonModelColumn int, buttonClickableModelColumn int) {
cname := C.CString(name)
defer freestr(cname)
C.uiTableAppendButtonColumn(t.t, cname, C.int(buttonModelColumn), C.int(buttonClickableModelColumn))
}
// HeaderVisible returns whether the Table's header is visible.
func (t *Table) HeaderVisible() bool {
return tobool(C.uiTableHeaderVisible(t.t))
}
func (t *Table) SetHeaderVisible(visible bool) {
C.uiTableHeaderSetVisible(t.t, frombool(visible))
}
func (t *Table) HeaderSortIndicator(column int) uint {
return uint(C.uiTableHeaderSortIndicator(t.t, C.int(column)))
}
// 0: none, 1: ascending, 2: decending
func (t *Table) SetHeaderSortIndicator(column int, indicator uint) {
C.uiTableHeaderSetSortIndicator(t.t, C.int(column), C.uint(indicator))
}
func (t *Table) ColumnWidth(column int) int {
return int(C.uiTableColumnWidth(t.t, C.int(column)))
}
func (t *Table) SetColumnWidth(column int, w int) {
C.uiTableColumnSetWidth(t.t, C.int(column), C.int(w))
}
func (t *Table) OnRowClicked(f func(*Table, int)) {
t.onClicked = f
}
func (t *Table) OnRowDoubleClicked(f func(*Table, int)) {
t.onDoubleClicked = f
}
func (t *Table) OnHeaderClicked(f func(*Table, int)) {
t.onHeaderClicked = f
}
//export pkguiDoTableOnRowClicked
func pkguiDoTableOnRowClicked(cc *C.uiTable, row C.int, data unsafe.Pointer) {
c := ControlFromLibui(uintptr(unsafe.Pointer(cc))).(*Table)
if c.onClicked != nil {
c.onClicked(c, int(row))
}
}
//export pkguiDoTableOnRowDoubleClicked
func pkguiDoTableOnRowDoubleClicked(cc *C.uiTable, row C.int, data unsafe.Pointer) {
c := ControlFromLibui(uintptr(unsafe.Pointer(cc))).(*Table)
if c.onDoubleClicked != nil {
c.onDoubleClicked(c, int(row))
}
}
//export pkguiDoTableOnHeaderClicked
func pkguiDoTableOnHeaderClicked(cc *C.uiTable, row C.int, data unsafe.Pointer) {
c := ControlFromLibui(uintptr(unsafe.Pointer(cc))).(*Table)
if c.onHeaderClicked != nil {
c.onHeaderClicked(c, int(row))
}
}