diff options
author | Alexander Neumann <alexander.neumann@picos-software.com> | 2019-02-25 16:04:11 +0100 |
---|---|---|
committer | Alexander Neumann <alexander.neumann@picos-software.com> | 2019-02-25 16:04:11 +0100 |
commit | 42e908c801230a12dc8c99894afee20374187984 (patch) | |
tree | b9d844b410c3e3cd135b0969967158a718cdfe02 | |
parent | TableView: Some small fixes (diff) | |
download | wireguard-windows-42e908c801230a12dc8c99894afee20374187984.tar.xz wireguard-windows-42e908c801230a12dc8c99894afee20374187984.zip |
TableView: Add support for custom column formatting functions
-rw-r--r-- | declarative/tableviewcolumn.go | 2 | ||||
-rw-r--r-- | tableview.go | 68 | ||||
-rw-r--r-- | tableviewcolumn.go | 11 |
3 files changed, 49 insertions, 32 deletions
diff --git a/declarative/tableviewcolumn.go b/declarative/tableviewcolumn.go index 12f5e1e4..27212b5b 100644 --- a/declarative/tableviewcolumn.go +++ b/declarative/tableviewcolumn.go @@ -31,6 +31,7 @@ type TableViewColumn struct { Frozen bool StyleCell func(style *walk.CellStyle) LessFunc func(i, j int) bool + FormatFunc func(value interface{}) string } func (tvc TableViewColumn) Create(tv *walk.TableView) error { @@ -62,6 +63,7 @@ func (tvc TableViewColumn) Create(tv *walk.TableView) error { return err } w.SetLessFunc(tvc.LessFunc) + w.SetFormatFunc(tvc.FormatFunc) return tv.Columns().Add(w) } diff --git a/tableview.go b/tableview.go index dd1845c9..113c00e8 100644 --- a/tableview.go +++ b/tableview.go @@ -1704,44 +1704,48 @@ func (tv *TableView) lvWndProc(origWndProcPtr uintptr, hwnd win.HWND, msg uint32 } if di.Item.Mask&win.LVIF_TEXT > 0 { + value := tv.model.Value(row, col) var text string - switch val := tv.model.Value(row, col).(type) { - case string: - text = val - - case float32: - prec := tv.columns.items[col].precision - if prec == 0 { - prec = 2 - } - text = FormatFloatGrouped(float64(val), prec) + if format := tv.columns.items[col].formatFunc; format != nil { + text = format(value) + } else { + switch val := value.(type) { + case string: + text = val + + case float32: + prec := tv.columns.items[col].precision + if prec == 0 { + prec = 2 + } + text = FormatFloatGrouped(float64(val), prec) - case float64: - prec := tv.columns.items[col].precision - if prec == 0 { - prec = 2 - } - text = FormatFloatGrouped(val, prec) + case float64: + prec := tv.columns.items[col].precision + if prec == 0 { + prec = 2 + } + text = FormatFloatGrouped(val, prec) - case time.Time: - if val.Year() > 1601 { - text = val.Format(tv.columns.items[col].format) - } + case time.Time: + if val.Year() > 1601 { + text = val.Format(tv.columns.items[col].format) + } - case bool: - if val { - text = checkmark - } + case bool: + if val { + text = checkmark + } - case *big.Rat: - prec := tv.columns.items[col].precision - if prec == 0 { - prec = 2 - } - text = formatBigRatGrouped(val, prec) + case *big.Rat: + prec := tv.columns.items[col].precision + if prec == 0 { + prec = 2 + } + text = formatBigRatGrouped(val, prec) - default: - text = fmt.Sprintf(tv.columns.items[col].format, val) + default: + text = fmt.Sprintf(tv.columns.items[col].format, val) } utf16 := syscall.StringToUTF16(text) diff --git a/tableviewcolumn.go b/tableviewcolumn.go index e66c59ac..f8aa4f40 100644 --- a/tableviewcolumn.go +++ b/tableviewcolumn.go @@ -25,6 +25,7 @@ type TableViewColumn struct { titleOverride string width int lessFunc func(i, j int) bool + formatFunc func(value interface{}) string visible bool frozen bool } @@ -332,6 +333,16 @@ func (tvc *TableViewColumn) SetLessFunc(lessFunc func(i, j int) bool) { tvc.lessFunc = lessFunc } +// FormatFunc returns the custom format func of this TableViewColumn. +func (tvc *TableViewColumn) FormatFunc() func(value interface{}) string { + return tvc.formatFunc +} + +// FormatFunc sets the custom format func of this TableViewColumn. +func (tvc *TableViewColumn) SetFormatFunc(formatFunc func(value interface{}) string) { + tvc.formatFunc = formatFunc +} + func (tvc *TableViewColumn) indexInListView() int32 { if tvc.tv == nil { return -1 |