|
| 1 | +// 26 august 2018 |
| 2 | + |
| 3 | +// +build OMIT |
| 4 | + |
| 5 | +// TODO possible bug in libui: the checkboxes on macOS retain their values when they shouldn't |
| 6 | + |
| 7 | +package main |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + |
| 12 | + "github.com/andlabs/ui" |
| 13 | +) |
| 14 | + |
| 15 | +type modelHandler struct { |
| 16 | + row9Text string |
| 17 | + yellowRow int |
| 18 | + checkStates [15]int |
| 19 | +} |
| 20 | + |
| 21 | +func newModelHandler() *modelHandler { |
| 22 | + m := new(modelHandler) |
| 23 | + m.row9Text = "You can edit this one" |
| 24 | + m.yellowRow = -1 |
| 25 | + return m |
| 26 | +} |
| 27 | + |
| 28 | +func (mh *modelHandler) ColumnTypes(m *ui.TableModel) []ui.TableValue { |
| 29 | + return []ui.TableValue{ |
| 30 | + ui.TableString(""), // column 0 text |
| 31 | + ui.TableString(""), // column 1 text |
| 32 | + ui.TableString(""), // column 2 text |
| 33 | + ui.TableColor{}, // row background color |
| 34 | + ui.TableColor{}, // column 1 text color |
| 35 | + ui.TableImage{}, // column 1 image |
| 36 | + ui.TableString(""), // column 4 button text |
| 37 | + ui.TableInt(0), // column 3 checkbox state |
| 38 | + ui.TableInt(0), // column 5 progress |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (mh *modelHandler) NumRows(m *ui.TableModel) int { |
| 43 | + return 15 |
| 44 | +} |
| 45 | + |
| 46 | +func (mh *modelHandler) CellValue(m *ui.TableModel, row, column int) ui.TableValue { |
| 47 | + if column == 3 { |
| 48 | + if row == mh.yellowRow { |
| 49 | + return ui.TableColor{1, 1, 0, 1} |
| 50 | + } |
| 51 | + if row == 3 { |
| 52 | + return ui.TableColor{1, 0, 0, 1} |
| 53 | + } |
| 54 | + if row == 11 { |
| 55 | + return ui.TableColor{0, 0.5, 1, 0.5} |
| 56 | + } |
| 57 | + return nil |
| 58 | + } |
| 59 | + if column == 4 { |
| 60 | + if (row % 2) == 1 { |
| 61 | + return ui.TableColor{0.5, 0, 0.75, 1} |
| 62 | + } |
| 63 | + return nil |
| 64 | + } |
| 65 | +/* if column == 5 { |
| 66 | + if row < 8 { |
| 67 | + return ui.TableImage{img[0]} |
| 68 | + } |
| 69 | + return ui.TableImage{img[1]} |
| 70 | + } |
| 71 | +*/ if column == 7 { |
| 72 | + return ui.TableInt(mh.checkStates[row]) |
| 73 | + } |
| 74 | + if column == 8 { |
| 75 | + if row == 0 { |
| 76 | + return ui.TableInt(0) |
| 77 | + } |
| 78 | + if row == 13 { |
| 79 | + return ui.TableInt(100) |
| 80 | + } |
| 81 | + if row == 14 { |
| 82 | + return ui.TableInt(-1) |
| 83 | + } |
| 84 | + return ui.TableInt(50) |
| 85 | + } |
| 86 | + switch column { |
| 87 | + case 0: |
| 88 | + return ui.TableString(fmt.Sprintf("Row %d", row)) |
| 89 | + case 2: |
| 90 | + if row == 9 { |
| 91 | + return ui.TableString(mh.row9Text) |
| 92 | + } |
| 93 | + return ui.TableString("Editing this won't change anything") |
| 94 | + case 1: |
| 95 | + return ui.TableString("Colors!") |
| 96 | + case 6: |
| 97 | + return ui.TableString("Make Yellow") |
| 98 | + } |
| 99 | + panic("unreachable") |
| 100 | +} |
| 101 | + |
| 102 | +func (mh *modelHandler) SetCellValue(m *ui.TableModel, row, column int, value ui.TableValue) { |
| 103 | +} |
| 104 | + |
| 105 | +func setupUI() { |
| 106 | + mainwin := ui.NewWindow("libui Control Gallery", 640, 480, true) |
| 107 | + mainwin.OnClosing(func(*ui.Window) bool { |
| 108 | + ui.Quit() |
| 109 | + return true |
| 110 | + }) |
| 111 | + ui.OnShouldQuit(func() bool { |
| 112 | + mainwin.Destroy() |
| 113 | + return true |
| 114 | + }) |
| 115 | + |
| 116 | + mh := newModelHandler() |
| 117 | + model := ui.NewTableModel(mh) |
| 118 | + |
| 119 | + table := ui.NewTable(&ui.TableParams{ |
| 120 | + Model: model, |
| 121 | + RowBackgroundColorModelColumn: 3, |
| 122 | + }) |
| 123 | + mainwin.SetChild(table) |
| 124 | + mainwin.SetMargined(true) |
| 125 | + |
| 126 | + table.AppendTextColumn("Column 1", |
| 127 | + 0, ui.TableModelColumnNeverEditable, nil) |
| 128 | + |
| 129 | +/* table.AppendImageTextColumn("Column 2", |
| 130 | + 5, |
| 131 | + 1, ui.TableModelColumnNeverEditable, &ui.TableTextColumnOptionalParams{ |
| 132 | + ColorModelColumn: 4, |
| 133 | + }); |
| 134 | +*/ table.AppendTextColumn("Editable", |
| 135 | + 2, ui.TableModelColumnAlwaysEditable, nil) |
| 136 | + |
| 137 | + table.AppendCheckboxColumn("Checkboxes", |
| 138 | + 7, ui.TableModelColumnAlwaysEditable) |
| 139 | + table.AppendButtonColumn("Buttons", |
| 140 | + 6, ui.TableModelColumnAlwaysEditable) |
| 141 | + |
| 142 | + table.AppendProgressBarColumn("Progress Bar", |
| 143 | + 8) |
| 144 | + |
| 145 | + mainwin.Show() |
| 146 | +} |
| 147 | + |
| 148 | +func main() { |
| 149 | + ui.Main(setupUI) |
| 150 | +} |
0 commit comments