summaryrefslogtreecommitdiff
path: root/tablemodel.go
blob: 181388e55cc52f69c759f1615bc4e9f6a466e020 (plain)
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
// 24 august 2018

package ui

// #include <stdlib.h>
// #include "ui.h"
// #include "util.h"
// extern int doTableModelNumColumns(uiTableModelHandler *, uiTableModel *);
// extern uiTableValueType doTableModelColumnType(uiTableModelHandler *, uiTableModel *, int);
// extern int doTableModelNumRows(uiTableModelHandler *, uiTableModel *);
// extern uiTableValue *doTableModelCellValue(uiTableModelHandler *mh, uiTableModel *m, int row, int column);
// extern void doTableModelSetCellValue(uiTableModelHandler *, uiTableModel *, int, int, const uiTableValue *);
// static inline uiTableModelHandler *allocTableModelHandler(void)
// {
// 	uiTableModelHandler *mh;
// 
// 	mh = (uiTableModelHandler *) pkguiAlloc(sizeof (uiTableModelHandler));
// 	mh->NumColumns = doTableModelNumColumns;
// 	mh->ColumnType = doTableModelColumnType;
// 	mh->NumRows = doTableModelNumRows;
// 	mh->CellValue = doTableModelCellValue;
// 	mh->SetCellValue = doTableModelSetCellValue;
// 	return mh;
// }
// static inline void freeTableModelHandler(uiTableModelHandler *mh)
// {
// 	free(mh);
// }
import "C"

// TableValue is a type that represents a piece of data that can come
// out of a TableModel.
type TableValue interface {
	toLibui() *C.uiTableValue
}

// TableString is a TableValue that stores a string. TableString is
// used for displaying text in a Table.
type TableString string

func (s TableString) toLibui() *C.uiTableValue {
	cs := C.CString(string(s))
	defer freestr(cs)
	return C.uiNewTableValueString(cs)
}

// TableImage is a TableValue that represents an Image. Ownership
// of the Image is not copied; you must keep it alive alongside the
// TableImage.
type TableImage struct {
	I	*Image
}

func (i TableImage) toLibui() *C.uiTableValue {
	return C.uiNewTableValueImage(i.I.i)
}

// TableInt is a TableValue that stores integers. These are used for
// progressbars. Due to current limitations of libui, they also
// represent checkbox states, via TableFalse and TableTrue.
type TableInt int

// TableFalse and TableTrue are the Boolean constants for TableInt.
const (
	TableFalse TableInt = 0
	TableTrue TableInt = 1
)

func (i TableInt) toLibui() *C.uiTableValue {
	return C.uiNewTableValueInt(C.int(i))
}

// TableColor is a TableValue that represents a color.
type TableColor struct {
	R	float64
	G	float64
	B	float64
	A	float64
}

func (c TableColor) toLibui() *C.uiTableValue {
	return C.uiNewTableValueColor(C.double(c.R), C.double(c.G), C.double(c.B), C.double(c.A))
}

func tableValueFromLibui(value *C.uiTableValue) TableValue {
	if value == nil {
		return nil
	}
	switch C.uiTableValueGetType(value) {
	case C.uiTableValueTypeString:
		cs := C.uiTableValueString(value)
		return TableString(C.GoString(cs))
	case C.uiTableValueTypeImage:
		panic("TODO")
	case C.uiTableValueTypeInt:
		return TableInt(C.uiTableValueInt(value))
	case C.uiTableValueTypeColor:
		panic("TODO")
	}
	panic("unreachable")
}