ListboxTV and CellBackgroundPaint

I’m trying to get alternate row colors in a ListboxTV, but g is always nil and raises a NilObjectException

CellBackgroundPaint (g As Graphics, row As Integer, column As Integer, textFrame as NSRectMBS, controlView as NSViewMBS) As Boolean
If g = Nil Then
  System.DebugLog("g is nil")
  Return False
End If

If Me.ListIndex = row Then
  // let Xojo draw selection
Else
  
  If row Mod 2 = 0 Then
    g.ForeColor = ColorGroup.NamedColor("unemphasizedSelectedContentBackgroundColor")
    
  Else
    g.ForeColor = ColorGroup.NamedColor("alternatingContentBackgroundColors")
  End If
  
  g.FillRect 0, 0, g.Width, g.Height
End If

How do you invoke the code (event)?

It’s a CellBackgroundPaint event, like a Listbox.CellBackgroundPaint or Listbox.PaintCellBackground event. You don’t need to invoke it.

As far as I remember both CellBackGroundPaint and CellTextPaint don’t work for ListboxTV. I wanted to replace all my listboxes with ListboxTV.

Christian told me to use NSGraphicsMBS for drawing icons into a listbox:

  dim g2 as new NSGraphicsMBS
  g2.drawPicture(database_export_light, 0, 0, 20, 20, 0, 0, database_export_light.Width, database_export_light.Height, 1, 1)

But this didn’t quite work either. At some point I gave up.

Yeah, I just found the note about cellbackgroundpaint. I like being able to do individual row heights with this, and I found how to do alternating row colors.

But I still need a custom background color for some rows, an warning icons drawn in front of text.

I may have to splurge and get PiDog’s DataView instead.

CellTextPaint works fine…

different color on some rows
Screenshot 5

alternate row color
Screenshot 3

there’s a problem with “quickviewtemplate” drawing while zooming, but I hope I will fix it
Screenshot 4

OP wanted to use NSTableView and not the listbox. For NSTableView CellTextPaint does NOT work.

it’s not the xojo’s native listbox. it is based on “ListBoxTV TableView” example

Then post your code.

Here’s the project

on cellTextPaint, 5th line of code should be modified to avoid different colors on selected rows:

//different color on every third row
if row mod 3 = 0 and not me.Selected(row) then

Add

nsi.setSize 24, 24
nsi.isFlipped = true
nsg.drawPicture(nsi.CopyPictureWithMask

before drawing…

and change operation to compositeCopy

nsg.drawPicture(… NSGraphicsMBS.NSCompositeSourceOver, 1)

change it to NSGraphicsMBS.NSCompositeCopy

It might be a macOS Monterey thing - but I have a gap in the colors on the left side. Tags also don’t change color until you click on them. I also get a nilobjectexception in celltextpaint when closing the window.

Screen Shot 2022-06-27 at 6.32.30 AM

I’m sorry, but I’m on High Sierra. I’ve tested the project on Catalina and it works fine.
I should buy an external SSD and install Monterey to do some testing, but…

for that gap, let’s try to change this

//different color on every third row
if row mod 3 = 0 and not me.Selected(row) then
nsg.setFillColor if (me.Selected(row), NSColorMBS.selectedMenuItemColor, NSColorMBS.colorWithDeviceRGB(0.910, 0.951, 1))
dim frameRect as new NSRectMBS
frameRect.X = textFrame.X
frameRect.Y = textFrame.Y
frameRect.Width = textFrame.Width + 10
frameRect.Height = textFrame.Height
nsg.fillRect frameRect
end

into this…

//different color on every third row
if row mod 3 = 0 and not me.Selected(row) then
nsg.setFillColor if (me.Selected(row), NSColorMBS.selectedMenuItemColor, NSColorMBS.colorWithDeviceRGB(0.910, 0.951, 1))
nsg.fillRect textFrame
end


I’ve increased the width of the frameRect to get rid of the gap at the end of the row.
Maybe that affects the behaviour on Monterey…
so try nsg.fillRect textFrame instead of enlarged rectangle.

or try to adjust the rectangle…

if SystemInformationMBS.isMonterey(true) then

dim frameRect as new NSRectMBS
frameRect.X = textFrame.X - 10
frameRect.Y = textFrame.Y - 10
frameRect.Width = textFrame.Width + 10
frameRect.Height = textFrame.Height
nsg.fillRect frameRect

I don’t know what to say until I can test it… :frowning:

Screenshot 1

It’s still pretty wonky for me. If all I need is a table with alternating row colors and individually height rows, this is great, but difference color backgrounds, text and icons added start being more hassle than it’s worth.

With Dataview, it’s going to be pretty easy to switch over my current custom listbox.