Listbox Highlight

They do actually… This is the code on a listbox subclassI use to get it… written long before dark mode and I am not sure where I originally got the declares (I am not good with declares!)

[code]Public Shared Function BackgroundColor(isSelected as Boolean, Active as Boolean) as Color

If Not isSelected then Return ListBackgroundColor ’ another method … usually returns white

Dim BkgColor as Color
#if TargetCocoa
Declare Function NSClassFromString Lib “Foundation” ( name As CFStringRef ) As Ptr
Declare Function alternateSelectedControlColor Lib “AppKit” Selector “alternateSelectedControlColor” ( obj As Ptr ) As Ptr
Declare Function secondarySelectedControlColor Lib “AppKit” Selector “secondarySelectedControlColor” ( obj As Ptr ) As Ptr
Declare Function colorUsingColorSpaceName Lib “AppKit” Selector “colorUsingColorSpaceName:” (obj As Ptr, name As Ptr) As Ptr
#If Target32Bit
Declare Function redComponent Lib “AppKit” Selector “redComponent” (obj As Ptr) As Single
Declare Function greenComponent Lib “AppKit” Selector “greenComponent” (obj As Ptr) As Single
Declare Function blueComponent Lib “AppKit” Selector “blueComponent” (obj As Ptr) As Single
#else
Declare Function redComponent Lib “AppKit” Selector “redComponent” (obj As Ptr) As Double
Declare Function greenComponent Lib “AppKit” Selector “greenComponent” (obj As Ptr) As Double
Declare Function blueComponent Lib “AppKit” Selector “blueComponent” (obj As Ptr) As Double
#Endif

Static NSColor As Ptr = NSClassFromString("NSColor")
Static NSCalibratedRGBColorSpace As Ptr
If NSCalibratedRGBColorSpace = Nil Then
  declare function CFBundleGetBundleWithIdentifier lib "CoreFoundation.framework" (bundleID as CFStringRef) as Ptr
  Dim BundlePtr as ptr = CFBundleGetBundleWithIdentifier("com.apple.AppKit")
  
  soft declare function CFRetain lib "CoreFoundation.framework" (cf as Ptr) as ptr
  Call CFRetain(BundlePtr)
  
  declare function CFBundleGetDataPointerForName lib "CoreFoundation.framework" (bundle as Ptr, symbolName as CFStringRef) as Ptr
  NSCalibratedRGBColorSpace =  CFBundleGetDataPointerForName(BundlePtr,"NSCalibratedRGBColorSpace").Ptr(0)
  
  soft declare sub CFRelease lib "CoreFoundation.framework" (cf as Ptr)
  
  CFRelease(BundlePtr)
  
End If

Dim theColor As Ptr
If Active Then
  theColor = alternateSelectedControlColor(NSColor)
Else
  theColor = secondarySelectedControlColor(NSColor)
End If

Dim rgbColor As Ptr = colorUsingColorSpaceName(theColor, NSCalibratedRGBColorSpace)
If rgbColor <> Nil Then
  Dim red As Integer = redComponent(rgbColor) * 255.0
  Dim green As Integer = greenComponent(rgbColor) * 255.0
  Dim blue As Integer = blueComponent(rgbColor) * 255.0
  BkgColor = RGB(red, green, blue)
Else
  BkgColor = HighlightColor
End If

#else
BkgColor = HighlightColor

#ElseIf TargetMacOS

Declare function GetThemeBrushAsColor lib "Carbon" (inBrush as Integer, _
inDepth as Integer, _
inColorDev as Integer, _
outColor as ptr) as Integer

dim oc as MemoryBlock = new MemoryBlock(6)
dim res as Integer

If Active Then
  If GetThemeBrushAsColor(-5, 32, 1, oc) <> 0 Then
    BkgColor =  HighlightColor
  else
    BkgColor = RGB(oc.byte(0), oc.byte(2), oc.byte(4))
  end if
Else
  If GetThemeBrushAsColor(-4, 32, 1, oc) <> 0 Then
    BkgColor = DarkBevelColor
  else
    BkgColor =  RGB(oc.byte(0), oc.byte(2), oc.byte(4))
  End if
End if

#Elseif TargetWin32
If Active then
BkgColor = HighlightColor
Else
dim res as memoryblock
Declare Function GetSysColor Lib “user32” (nIndex As integer) As integer
res = newmemoryBlock(4)
res.long(0) = getsyscolor(0)

  BkgColor = rgb(res.byte(0),res.byte(1),res.byte(2))
End If

#else

if Active Then
  BkgColor =  HighlightColor
Else
  BkgColor = FillColor
end if

#endif

Return BkgColor
End Function
[/code]

isn’t what the OP wants simply

dim c as color=HIGHLIGHTCOLOR // which on macOS is &CA4CCFE (when set to default)

in darkmode that changed to &c304f78

[quote=414719:@Dave S]isn’t what the OP wants simply

dim c as color=HIGHLIGHTCOLOR // which on macOS is &CA4CCFE (when set to default)

in darkmode that changed to &c304f78[/quote]

The color the listbox uses to highlight a selected row is not REALBasic.HighlightColor on a Mac so if you want to match that, you need to use declares…

As I have not installed Mojave, I don’t know if those declares deal with dark mode.

  • karen

http://documentation.xojo.com/index.php/HighlightColor

I’ve noticed that there is something strange in the MacOS system highlight color, as it pertains to list items. I have my system highlight color set to a pale yellow, and that is the color that I get when editing text fields (in XOJO and in other apps), but in XOJO listboxes the highlight color is darkened to an ugly dark greenish gold. I get exactly the same dark highlight color in the Finder when I click on a file in list view mode, and and the same color again in other list views in other apps. So, it seems that it’s the OS that’s doing something to the system highlight color, making it darker when highlighting list items.

As previously suggested, using the cellbackgroundpaint event is an easy way to set the background color to something more palatable. Just one or two lines of code, and you’re done.

the only problem I see with setting the color I want with a constant, is that if the user changes the highlight color in the system preferences, from light blue to light yellow like Robert, he will have my dark blue color when he selects something in a listbox …

you got them from Joe Ranieri … a good source ?
https://forum.xojo.com/conversation/post/160871

@Karen Atkocius : also what is ListBackgroundColor ?

I was thinking about that too. I suspect that a person could come up with a function that would take the darkened system highlight color and lighten it up again.

For teh Mac I don’t have the Cocoa declare for that … only the old carbon one, which if it still works at all, would not work for Dark Mode.

Really the Listbox should have shared methods on it to retinue current Highlight, background and Text coior for both selected and unselected rows…

Given that Xojo Inc obviously has code to get the colors as they use them, I have never understood why the never exposed them to us, given the extensive customization the Xojo Listbox allows.

At the very LEAST when a cell is painted the graphics.ForeColor for g passed in should already be set to the background fill color BEFORE cellbackground paint is called, analogous to how CellTextPaint works.

  • Karen