Ok, it's just one callback for all the gadgets that want to use the function.
Added a map where the colors are stored.
Except for that it has been extended to change the background color for all Columns.
Code: Select all
EnableExplicit
Structure udtSelectionColor
Type.i
TextColor.i
BackColor.i
EndStructure
Global NewMap SelectionColor.udtSelectionColor()
Procedure SetSelectionColor(Gadget, TextColor, BackColor)
Protected Key.s = Hex(GadgetID(Gadget))
SelectionColor(Key)\Type = GadgetType(Gadget)
SelectionColor(Key)\TextColor = TextColor
SelectionColor(Key)\BackColor = BackColor
EndProcedure
Define AppDelegate = CocoaMessage(0, CocoaMessage(0, 0,
"NSApplication sharedApplication"), "delegate")
Define DelegateClass = CocoaMessage(0, AppDelegate, "class")
Define Selector.I = sel_registerName_("tableView:willDisplayCell:" +
"forTableColumn:row:")
Procedure NSColor(Color)
Protected Alpha.CGFloat = 1
Protected Blue.CGFloat = Blue(Color) / 255
Protected Green.CGFloat = Green(Color) / 255
Protected Red.CGFloat = Red(Color) / 255
ProcedureReturn CocoaMessage(0, 0, "NSColor colorWithDeviceRed:@",
@Red, "green:@", @Green, "blue:@", @Blue, "alpha:@", @Alpha)
EndProcedure
ProcedureC CellDisplayCallback(Object.I, Selector.I, TableView.I, Cell.I, *Column, Row.I)
Protected CellFrame.NSRect
Protected Column.I
Protected SelectedRow.I
If Not FindMapElement(SelectionColor(), Hex(TableView))
ProcedureReturn 0
EndIf
SelectedRow = CocoaMessage(0, TableView, "selectedRow")
If SelectedRow <> Row
CocoaMessage(0, Cell, "setDrawsBackground:", #NO)
CocoaMessage(0, Cell, "setTextColor:", CocoaMessage(0, 0,
"NSColor textColor"))
Else
If SelectionColor()\Type = #PB_GadgetType_ListIcon
;Column = Val(PeekS(CocoaMessage(0, CocoaMessage(0, *Column, "identifier"),
; "UTF8String"), -1, #PB_UTF8))
Column = CocoaMessage(0, CocoaMessage(0, TableView, "tableColumns"), "indexOfObject:", *Column)
EndIf
CocoaMessage(0, Cell,
"setTextColor:", NSColor(SelectionColor()\TextColor))
CocoaMessage(@CellFrame, TableView,
"frameOfCellAtColumn:", Column,
"row:", Row)
; ----- Enlarge cell frame
CellFrame\origin\x - 1
CellFrame\origin\y - 1
CellFrame\size\height + 2
If OSVersion() <= #PB_OS_MacOSX_10_7
CellFrame\size\width + 3
Else
CellFrame\size\width + 8
EndIf
; ----- Draw cell background
CocoaMessage(0, NSColor(SelectionColor()\BackColor), "setFill")
CocoaMessage(0, 0, "NSBezierPath fillRect:@", @CellFrame)
EndIf
EndProcedure
OpenWindow(0, 200, 100, 480, 240, "Disable highlight and selection color example")
ListIconGadget(0, 10, 10, WindowWidth(0) - 20, 100, "Name", 110)
AddGadgetColumn(0, 1, "Address", GadgetWidth(0) - GetGadgetItemAttribute(0, 0, #PB_ListIcon_ColumnWidth) - 8)
AddGadgetItem(0, -1, "Harry Rannit" + #LF$ + "12 Parliament Way, Battle Street, By the Bay")
AddGadgetItem(0, -1, "Ginger Brokeit" + #LF$ + "130 PureBasic Road, BigTown, CodeCity")
AddGadgetItem(0, -1, "Didi Foundit" + #LF$ + "321 Logo Drive, Mouse House, Downtown")
ListViewGadget(1, 10, 120, WindowWidth(0) - 20, 100)
AddGadgetItem(1, -1, "Harry Rannit" + #LF$ + "12 Parliament Way, Battle Street, By the Bay")
AddGadgetItem(1, -1, "Ginger Brokeit" + #LF$ + "130 PureBasic Road, BigTown, CodeCity")
AddGadgetItem(1, -1, "Didi Foundit" + #LF$ + "321 Logo Drive, Mouse House, Downtown")
; ----- Initialize callback
class_addMethod_(DelegateClass, Selector, @CellDisplayCallback(), "v@:@@@@")
; ----- Set delagate
CocoaMessage(0, GadgetID(0), "setDelegate:", AppDelegate)
SetSelectionColor(0, #Black, #Red)
CocoaMessage(0, GadgetID(1), "setDelegate:", AppDelegate)
SetSelectionColor(1, #Gray, #Green)
; ----- Disable default highlighting
CocoaMessage(0, GadgetID(0), "setSelectionHighlightStyle:", -1)
CocoaMessage(0, GadgetID(1), "setSelectionHighlightStyle:", -1)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow