Since I recently needed to colorize single cells in a ListIconGadget, I have programmed a workaround for the SetGadgetItemColor() function which is still not implemented for MacOS X. The example below is
: only on MacOS X a macro will be defined which calls the procedure SetGadgetItemColorEx(), so that it's possible now to use SetGadgetItemColor() also on MacOS X...
to establish a callback which is called for every cell of a ListIconGadget before the drawing of a cell begins. I use the quad array ListIconCellColor.Q(Row.I, Column.I) to store any changed colors: the left Long part of a quad color value contains the front color and the right Long part contains the back color.
I have tested my code example successfully on MacOS X 10.6.8 "Snow Leopard" and MacOS X 10.11.4 "El Capitan" with PB 5.42 x86 and x64 and in both ASCII and Unicode mode and on Windows 7 x64.
Code: Select all
EnableExplicit
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
ImportC ""
class_addMethod(Class.I, Selector.I, *Callback, Types.P-ASCII)
sel_registerName(MethodName.P-ASCII)
EndImport
Define AppDelegate.I = CocoaMessage(0, CocoaMessage(0, 0,
"NSApplication sharedApplication"), "delegate")
Define DelegateClass.I = CocoaMessage(0, AppDelegate, "class")
Define Selector.I = sel_registerName("tableView:willDisplayCell:forTableColumn:row:")
Dim ListIconCellColor.Q(0, 0)
ProcedureC CellDisplayCallback(Object.I, Selector.I, TableView.I, Cell.I,
*Column, Row.I)
Shared ListIconCellColor.Q()
Protected Alpha.CGFloat
Protected BackColor.L
Protected Blue.CGFloat
Protected CellColor.Q
Protected Column.I
Protected FrontColor.L
Protected Green.CGFloat
Protected Red.CGFloat
Column = Val(PeekS(CocoaMessage(0, CocoaMessage(0, *Column, "identifier"),
"UTF8String"), -1, #PB_UTF8))
CellColor = ListIconCellColor(Row, Column)
If CellColor = 0
CocoaMessage(0, Cell, "setDrawsBackground:", #NO)
CocoaMessage(0, Cell,
"setTextColor:", CocoaMessage(0, 0, "NSColor blackColor"))
Else
BackColor = CellColor & $FFFFFF
If BackColor = 0
CocoaMessage(0, Cell, "setDrawsBackground:", #NO)
Else
CocoaMessage(0, Cell, "setDrawsBackground:", #YES)
Alpha = 1
Red = Red(BackColor) / 255
Green = Green(BackColor) / 255
Blue = Blue(BackColor) / 255
CocoaMessage(0, Cell, "setBackgroundColor:", CocoaMessage(0, 0,
"NSColor colorWithDeviceRed:@", @Red,
"green:@", @Green,
"blue:@", @Blue,
"alpha:@", @Alpha))
EndIf
FrontColor = (CellColor >> 32) & $FFFFFF
If FrontColor = 0
CocoaMessage(0, Cell,
"setTextColor:", CocoaMessage(0, 0, "NSColor blackColor"))
Else
Alpha = 1
Red = Red(FrontColor) / 255
Green = Green(FrontColor) / 255
Blue = Blue(FrontColor) / 255
CocoaMessage(0, Cell, "setTextColor:", CocoaMessage(0, 0,
"NSColor colorWithDeviceRed:@", @Red,
"green:@", @Green,
"blue:@", @Blue,
"alpha:@", @Alpha))
EndIf
EndIf
EndProcedure
Procedure SetGadgetItemColorEx(GadgetID.I, Row.I, ColorType.I, Color.I,
Column.I)
Shared ListIconCellColor.Q()
Protected CellColor.Q
Protected ColumnCount.I
Protected RowCount.I
If ArraySize(ListIconCellColor()) = 0
ColumnCount = CocoaMessage(0, GadgetID(GadgetID), "numberOfColumns")
RowCount = CocoaMessage(0, GadgetID(GadgetID), "numberOfRows")
Dim ListIconCellColor(RowCount - 1, ColumnCount - 1)
EndIf
CellColor = ListIconCellColor(Row, Column)
Select ColorType
Case #PB_Gadget_BackColor
CellColor ! (Color & $FFFFFF)
Case #PB_Gadget_FrontColor
CellColor ! ((Color & $FFFFFF) << 32)
EndSelect
ListIconCellColor(Row, Column) = CellColor
EndProcedure
CompilerEndIf
OpenWindow(0, 200, 100, 430, 101,
"Change color of text and background in single cells")
ListIconGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20, "Name",
100, #PB_ListIcon_GridLines)
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")
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
Macro SetGadgetItemColor(GadgetID, Row, ColorType, Color, Column)
SetGadgetItemColorEx(GadgetID, Row, ColorType, Color, Column)
EndMacro
class_addMethod(DelegateClass, Selector, @CellDisplayCallback(), "v@:@@@@")
CocoaMessage(0, GadgetID(0), "setDelegate:", AppDelegate)
CompilerEndIf
; ----- Set background color of cell 2,0 to yellow
SetGadgetItemColor(0, 2, #PB_Gadget_BackColor, $FFFF, 0)
; ----- Set background color of cell 1,1 to X11 color "Aquamarine"
SetGadgetItemColor(0, 1, #PB_Gadget_BackColor, $D4FF7F, 1)
; ----- Set text color of cell 0,0 to blue
SetGadgetItemColor(0, 0, #PB_Gadget_FrontColor, $FF0000, 0)
; ----- Set text color of cell 1,1 to red
SetGadgetItemColor(0, 1, #PB_Gadget_FrontColor, $FF, 1)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
The code above only supports one single ListIconGadget. If you need to support multiple ListIconGadgets please take a look into
extended example.