Skip to content

add support of row's color and backgroundcolor #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions tableview.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ const (
tableViewSelectedIndexesChangedTimerId
)

// TableRow defines the color and font of each row
type TableRow struct {
TextColor Color
BackgroundColor Color
}

// TableView is a model based widget for record centric, tabular data.
//
// TableView is implemented as a virtual mode list view to support quite large
Expand Down Expand Up @@ -68,6 +74,7 @@ type TableView struct {
delayedCurrentIndexChangedCanceled bool
sortedColumnIndex int
sortOrder SortOrder
rows map[int]*TableRow
}

// NewTableView creates and returns a *TableView as child of the specified
Expand All @@ -85,6 +92,7 @@ func NewTableViewWithStyle(parent Container, style uint32) (*TableView, error) {
filePath2IconIndex: make(map[string]int32),
selectedIndexes: NewIndexList(nil),
}
tv.rows = make(map[int]*TableRow)

tv.columns = newTableViewColumnList(tv)

Expand Down Expand Up @@ -146,6 +154,28 @@ func NewTableViewWithStyle(parent Container, style uint32) (*TableView, error) {
return tv, nil
}

// SetRowTextColor set the row's text color
func (tv *TableView) SetRowTextColor(index int, c Color) {
r, ok := tv.rows[index]
if !ok {
tv.rows[index] = &TableRow{TextColor: c, BackgroundColor: RGB(255, 255, 255)}
return
}
r.TextColor = c
return
}

// SetRowBackgroundColor set the row's background color
func (tv *TableView) SetRowBackgroundColor(index int, c Color) {
r, ok := tv.rows[index]
if !ok {
tv.rows[index] = &TableRow{TextColor: RGB(0, 0, 0), BackgroundColor: c}
return
}
r.BackgroundColor = c
return
}

// Dispose releases the operating system resources, associated with the
// *TableView.
func (tv *TableView) Dispose() {
Expand Down Expand Up @@ -1265,8 +1295,9 @@ func (tv *TableView) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr)
}

case win.NM_CUSTOMDRAW:
nmlvcd := (*win.NMLVCUSTOMDRAW)(unsafe.Pointer(lParam))
v, ok := tv.rows[int(nmlvcd.Nmcd.DwItemSpec)]
if tv.alternatingRowBGColor != defaultTVRowBGColor {
nmlvcd := (*win.NMLVCUSTOMDRAW)(unsafe.Pointer(lParam))

switch nmlvcd.Nmcd.DwDrawStage {
case win.CDDS_PREPAINT:
Expand All @@ -1283,8 +1314,9 @@ func (tv *TableView) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr)
}*/
nmlvcd.ClrTextBk = win.COLORREF(tv.alternatingRowBGColor)
}

return win.CDRF_NOTIFYSUBITEMDRAW
if !ok {
return win.CDRF_NOTIFYSUBITEMDRAW
}

case win.CDDS_ITEMPREPAINT | win.CDDS_SUBITEM:
if nmlvcd.Nmcd.DwItemSpec%2 == 1 &&
Expand All @@ -1295,13 +1327,25 @@ func (tv *TableView) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr)

nmlvcd.ClrText = white
}

if !ok {
return win.CDRF_NEWFONT
}
}
}
if ok {
switch nmlvcd.Nmcd.DwDrawStage {
case win.CDDS_PREPAINT:
return win.CDRF_NOTIFYITEMDRAW
case win.CDDS_ITEMPREPAINT:
nmlvcd.ClrTextBk = win.COLORREF(v.BackgroundColor)
return win.CDRF_NOTIFYSUBITEMDRAW
case win.CDDS_ITEMPREPAINT | win.CDDS_SUBITEM:
nmlvcd.ClrText = win.COLORREF(v.TextColor)
return win.CDRF_NEWFONT
}
return win.CDRF_DODEFAULT
}

return win.CDRF_DODEFAULT

case win.LVN_COLUMNCLICK:
nmlv := (*win.NMLISTVIEW)(unsafe.Pointer(lParam))

Expand Down