Skip to content

Commit 85940da

Browse files
committed
And FINALLY ported Table. Now to write an example program.
1 parent 3c4b7e2 commit 85940da

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

pkgui.c

+20
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,23 @@ const uiTableModelHandler pkguiTableModelHandler = {
238238
.CellValue = pkguiDoTableModelCellValue,
239239
.SetCellValue = realDoTableModelSetCellValue,
240240
};
241+
242+
uiTableTextColumnOptionalParams *pkguiAllocTableTextColumnOptionalParams(void)
243+
{
244+
return (uiTableTextColumnOptionalParams *) pkguiAlloc(sizeof (uiTableTextColumnOptionalParams));
245+
}
246+
247+
void pkguiFreeTableTextColumnOptionalParams(uiTableTextColumnOptionalParams *p)
248+
{
249+
free(p);
250+
}
251+
252+
uiTableParams *pkguiAllocTableParams(void)
253+
{
254+
return (uiTableParams *) pkguiAlloc(sizeof (uiTableParams));
255+
}
256+
257+
void pkguiFreeTableParams(uiTableParams *p)
258+
{
259+
free(p);
260+
}

pkgui.h

+6
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,10 @@ extern void pkguiFreeAreaHandler(uiAreaHandler *ah);
9292
// tablemodel.go
9393
extern const uiTableModelHandler pkguiTableModelHandler;
9494

95+
// table.go
96+
extern uiTableTextColumnOptionalParams *pkguiAllocTableTextColumnOptionalParams(void);
97+
extern void pkguiFreeTableTextColumnOptionalParams(uiTableTextColumnOptionalParams *p);
98+
extern uiTableParams *pkguiAllocTableParams(void);
99+
extern void pkguiFreeTableParams(uiTableParams *p);
100+
95101
#endif

table.go

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// 26 august 2018
2+
3+
package ui
4+
5+
import (
6+
"unsafe"
7+
)
8+
9+
// #include "pkgui.h"
10+
import "C"
11+
12+
// TableModelColumnNeverEditable and
13+
// TableModelColumnAlwaysEditable are the value of an editable
14+
// model column parameter to one of the Table create column
15+
// functions; if used, that jparticular Table colum is not editable
16+
// by the user and always editable by the user, respectively.
17+
const (
18+
TableModelColumnNeverEditable = -1
19+
TableModelColumnAlwaysEditable = -2
20+
)
21+
22+
// TableTextColumnOptionalParams are the optional parameters
23+
// that control the appearance of the text column of a Table.
24+
type TableTextColumnOptionalParams struct {
25+
// ColorModelColumn is the model column containing the
26+
// text color of this Table column's text, or -1 to use the
27+
// default color.
28+
//
29+
// If CellValue for this column for any cell returns nil, that
30+
// cell will also use the default text color.
31+
ColorModelColumn int
32+
}
33+
34+
func (p *TableTextColumnOptionalParams) toLibui() *C.uiTableTextColumnOptionalParams {
35+
if p == nil {
36+
return nil
37+
}
38+
cp := C.pkguiAllocTableTextColumnOptionalParams()
39+
cp.ColorModelColumn = C.int(p.ColorModelColumn)
40+
return cp
41+
}
42+
43+
// TableParams defines the parameters passed to NewTable.
44+
type TableParams struct {
45+
// Model is the TableModel to use for this uiTable.
46+
// This parameter cannot be nil.
47+
Model *TableModel
48+
49+
// RowBackgroundColorModelColumn is a model column
50+
// number that defines the background color used for the
51+
// entire row in the Table, or -1 to use the default color for
52+
// all rows.
53+
//
54+
// If CellValue for this column for any row returns NULL, that
55+
// row will also use the default background color.
56+
RowBackgroundColorModelColumn int
57+
}
58+
59+
func (p *TableParams) toLibui() *C.uiTableParams {
60+
cp := C.pkguiAllocTableParams()
61+
cp.Model = p.Model.m
62+
cp.RowBackgroundColorModelColumn = C.int(p.RowBackgroundColorModelColumn)
63+
return cp
64+
}
65+
66+
// Table is a Control that shows tabular data, allowing users to
67+
// manipulate rows of such data at a time.
68+
type Table struct {
69+
ControlBase
70+
t *C.uiTable
71+
}
72+
73+
// NewTable creates a new Table with the specified parameters.
74+
func NewTable(p *TableParams) *Table {
75+
t := new(Table)
76+
77+
cp := p.toLibui()
78+
t.t = C.uiNewTable(cp)
79+
C.pkguiFreeTableParams(cp)
80+
81+
t.ControlBase = NewControlBase(t, uintptr(unsafe.Pointer(t.t)))
82+
return t
83+
}
84+
85+
// AppendTextColumn appends a text column to t. name is
86+
// displayed in the table header. textModelColumn is where the text
87+
// comes from. If a row is editable according to
88+
// textEditableModelColumn, SetCellValue is called with
89+
// textModelColumn as the column.
90+
func (t *Table) AppendTextColumn(name string, textModelColumn int, textEditableModelColumn int, textParams *TableTextColumnOptionalParams) {
91+
cname := C.CString(name)
92+
defer freestr(cname)
93+
cp := textParams.toLibui()
94+
defer C.pkguiFreeTableTextColumnOptionalParams(cp)
95+
C.uiTableAppendTextColumn(t.t, cname, C.int(textModelColumn), C.int(textEditableModelColumn), cp)
96+
}
97+
98+
// AppendImageColumn appends an image column to t.
99+
// Images are drawn at icon size, appropriate to the pixel density
100+
// of the screen showing the Table.
101+
func (t *Table) AppendImageColumn(name string, imageModelColumn int) {
102+
cname := C.CString(name)
103+
defer freestr(cname)
104+
C.uiTableAppendImageColumn(t.t, cname, C.int(imageModelColumn))
105+
}
106+
107+
// AppendImageTextColumn appends a column to t that
108+
// shows both an image and text.
109+
func (t *Table) AppendImageTextColumn(name string, imageModelColumn int, textModelColumn int, textEditableModelColumn int, textParams *TableTextColumnOptionalParams) {
110+
cname := C.CString(name)
111+
defer freestr(cname)
112+
cp := textParams.toLibui()
113+
defer C.pkguiFreeTableTextColumnOptionalParams(cp)
114+
C.uiTableAppendImageTextColumn(t.t, cname, C.int(imageModelColumn), C.int(textModelColumn), C.int(textEditableModelColumn), cp)
115+
}
116+
117+
// AppendCheckboxColumn appends a column to t that
118+
// contains a checkbox that the user can interact with (assuming the
119+
// checkbox is editable). SetCellValue will be called with
120+
// checkboxModelColumn as the column in this case.
121+
func (t *Table) AppendCheckboxColumn(name string, checkboxModelColumn int, checkboxEditableModelColumn int) {
122+
cname := C.CString(name)
123+
defer freestr(cname)
124+
C.uiTableAppendCheckboxColumn(t.t, cname, C.int(checkboxModelColumn), C.int(checkboxEditableModelColumn))
125+
}
126+
127+
// AppendCheckboxTextColumn appends a column to t
128+
// that contains both a checkbox and text.
129+
func (t *Table) AppendCheckboxTextColumn(name string, checkboxModelColumn int, checkboxEditableModelColumn int, textModelColumn int, textEditableModelColumn int, textParams *TableTextColumnOptionalParams) {
130+
cname := C.CString(name)
131+
defer freestr(cname)
132+
cp := textParams.toLibui()
133+
defer C.pkguiFreeTableTextColumnOptionalParams(cp)
134+
C.uiTableAppendCheckboxTextColumn(t.t, cname, C.int(checkboxModelColumn), C.int(checkboxEditableModelColumn), C.int(textModelColumn), C.int(textEditableModelColumn), cp)
135+
}
136+
137+
// AppendProgressBarColumn appends a column to t
138+
// that displays a progress bar. These columns work like
139+
// ProgressBar: a cell value of 0..100 displays that percentage, and
140+
// a cell value of -1 displays an indeterminate progress bar.
141+
func (t *Table) AppendProgressBarColumn(name string, progressModelColumn int) {
142+
cname := C.CString(name)
143+
defer freestr(cname)
144+
C.uiTableAppendProgressBarColumn(t.t, cname, C.int(progressModelColumn))
145+
}
146+
147+
// AppendButtonColumn appends a column to t
148+
// that shows a button that the user can click on. When the user
149+
// does click on the button, SetCellValue is called with a nil
150+
// value and buttonModelColumn as the column.
151+
// CellValue on buttonModelColumn should return the text to show
152+
// in the button.
153+
func (t *Table) AppendButtonColumn(name string, buttonModelColumn int, buttonClickableModelColumn int) {
154+
cname := C.CString(name)
155+
defer freestr(cname)
156+
C.uiTableAppendButtonColumn(t.t, cname, C.int(buttonModelColumn), C.int(buttonClickableModelColumn))
157+
}

0 commit comments

Comments
 (0)