// based off andlabs/ui/examples/table.go package main import "fmt" import "log" // import "os" import "github.com/andlabs/ui" import _ "github.com/andlabs/ui/winmanifest" import "github.com/davecgh/go-spew/spew" var img [2]*ui.Image type cellData struct { index int value ui.TableValue name string // what type of cell is this? event func() // what function to call if there is an event on this } // hmm. will this stand the test of time? type rowData struct { name string // what kind of row is this? status string // status of the row? /* // These may or may not be implementable click func() // what function to call if the user clicks on it doubleclick func() // what function to call if the user double clicks on it */ cells [20]cellData } type modelHandler struct { name string // I forgot where this goes rowcount int // This is the number of 'rows' which really means data elements not what the human sees rowBGcolor int // This is what index stores the row BG color (should always be 0) rows []rowData // This is all the table data by row generatedColumnTypes []ui.TableValue // generate this dynamically libUIevent func(*modelHandler, *ui.TableModel, int, int, ui.TableValue) } func initValues(mh *modelHandler) { img[0] = ui.NewImage(16, 16) img[1] = ui.NewImage(16, 16) mh.generatedColumnTypes = append(mh.generatedColumnTypes, ui.TableColor{}) mh.generatedColumnTypes = append(mh.generatedColumnTypes, ui.TableString("")) mh.generatedColumnTypes = append(mh.generatedColumnTypes, ui.TableColor{}) mh.generatedColumnTypes = append(mh.generatedColumnTypes, ui.TableString("test")) mh.generatedColumnTypes = append(mh.generatedColumnTypes, ui.TableString("")) mh.generatedColumnTypes = append(mh.generatedColumnTypes, ui.TableColor{}) for i := 0; i < mh.rowcount; i++ { log.Println("i=",i) // alternate background of each row light and dark if (i % 2) == 1 { mh.rows[i].cells[0].value = ui.TableColor{0.5, 0.5, 0.5, .7} mh.rows[i].cells[0].name = "BG" } else { mh.rows[i].cells[0].value = ui.TableColor{0.1, 0.1, 0.1, .1} mh.rows[i].cells[0].name = "BG" } // text for Column 0 mh.rows[i].cells[1].value = ui.TableString(fmt.Sprintf("fun %d", i)) mh.rows[i].cells[1].name = "EDIT" // text color for Column 0 mh.rows[i].cells[2].value = ui.TableColor{0.9, 0, 0, 1} mh.rows[i].cells[2].name = "COLOR" // set the button text for Column 1 mh.rows[i].cells[3].value = ui.TableString(fmt.Sprintf("awesome %d", i)) mh.rows[i].cells[3].name = "BUTTON" // text for Column 2 mh.rows[i].cells[4].value = ui.TableString(fmt.Sprintf("color %d", i)) mh.rows[i].cells[4].name = "EDIT" // text color for Column 2 mh.rows[i].cells[5].value = ui.TableColor{0.9, 0, 0, 1} mh.rows[i].cells[5].name = "COLOR" // text for Column 3 mh.rows[i].cells[6].value = ui.TableString(fmt.Sprintf("imgcolor %d", i)) // image for Column 3 mh.rows[i].cells[7].value = ui.TableImage{img[0]} mh.rows[i].cells[7].name = "IMAGE" // text color for Column 3 mh.rows[i].cells[8].value = ui.TableColor{0.9, 0, 0, 1} mh.rows[i].cells[8].name = "COLOR" } } func initColorColumn(mh *modelHandler, stringID int, colorID int) { mh.generatedColumnTypes = append(mh.generatedColumnTypes, ui.TableString("")) mh.generatedColumnTypes = append(mh.generatedColumnTypes, ui.TableColor{}) for i := 0; i < mh.rowcount; i++ { log.Println("i=",i) // text for Column ? mh.rows[i].cells[stringID].value = ui.TableString(fmt.Sprintf("append %d", i)) mh.rows[i].cells[stringID].name = "EDIT" // text color for Column ? mh.rows[i].cells[colorID].value = ui.TableColor{0.0, 0.9, 0.4, 1} mh.rows[i].cells[colorID].name = "COLOR" } // mh.rowcount = mh.rowcount + 2 } func appendColorColumn(mh *modelHandler, table *ui.Table, stringID int, colorID int) { log.Println("mh.rows=", mh.rows) textid := mh.rowcount + 1 log.Println("textid=", textid) colorid := mh.rowcount + 2 log.Println("colorid=", colorid) // text color doesn't work like this? table.AppendTextColumn("testappend", stringID, ui.TableModelColumnAlwaysEditable, &ui.TableTextColumnOptionalParams{ ColorModelColumn: colorID, }); // os.Exit(-1) } func newModelHandler(rowcount int) *modelHandler { mh := new(modelHandler) mh.rowBGcolor = 0 // this is the weird exception. Just always have this as 0 mh.rowcount = rowcount // This is the standard callback function from libUI when the user does something mh.libUIevent = defaultSetCellValue mh.rows = make([]rowData, mh.rowcount) initValues(mh) initColorColumn(mh, 6, 7) log.Println("Called newModelHandler() with mh=", mh) spew.Dump(mh) return mh } func makeTable(name string, rowcount int, row1Name string) (*ui.Table, *modelHandler, *ui.TableModel) { mh := newModelHandler(rowcount) mh.name = name model := ui.NewTableModel(mh) table := ui.NewTable( &ui.TableParams{ Model: model, RowBackgroundColorModelColumn: mh.rowBGcolor, }) table.AppendTextColumn(row1Name, 1, ui.TableModelColumnAlwaysEditable, nil) table.AppendButtonColumn("Details", 3, ui.TableModelColumnAlwaysEditable) // text color doesn't work like this? table.AppendTextColumn("testcolor", 4, ui.TableModelColumnAlwaysEditable, &ui.TableTextColumnOptionalParams{ ColorModelColumn: 5, }); appendColorColumn(mh, table, 6, 7) /* table.AppendImageTextColumn("testimagecolor", 6, 7, ui.TableModelColumnNeverEditable, &ui.TableTextColumnOptionalParams{ ColorModelColumn: 8, }); */ return table, mh, model } func defaultSetCellValue(mh *modelHandler, m *ui.TableModel, row, column int, value ui.TableValue) { if (mh.rows[row].cells[column].name == "EDIT") { mh.rows[row].cells[column].value = value } if (mh.rows[row].cells[column].name == "BUTTON") { log.Println("FOUND THE BUTTON!!!!!!! Button was pressed START", row, column) } return }