Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Mon May 02, 2016 6:54 pm
I have taken my example from above for the ListIconGadget and removed/changed some parts in order to implement SetGadgetItemColor() also for the ListViewGadget. Now it's possible to colorize the text color and background color of single rows in a ListViewGadget. This example is not cross-platform anymore because this feature is not implemented in the PB versions for Linux and Windows!
The code above only supports one single ListViewGadget. If you need to support multiple ListViewGadgets please take a look into this extended example.
I had to modify this example to also work with the new PB 6.00 x64 Beta 7 because Fred modified the ListViewGadget and ListIconGadget internally:
Code: Select all
EnableExplicit
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 i.I
Define Selector.I = sel_registerName("tableView:willDisplayCell:forTableColumn:row:")
Dim ListIconCellColor.Q(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 FrontColor.L
Protected Green.CGFloat
Protected Red.CGFloat
CellColor = ListIconCellColor(Row)
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)
Shared ListIconCellColor.Q()
Protected CellColor.Q
Protected RowCount.I
If ArraySize(ListIconCellColor()) = 0
RowCount = CocoaMessage(0, GadgetID(GadgetID), "numberOfRows")
Dim ListIconCellColor(RowCount - 1)
EndIf
CellColor = ListIconCellColor(Row)
Select ColorType
Case #PB_Gadget_BackColor
CellColor ! (Color & $FFFFFF)
Case #PB_Gadget_FrontColor
CellColor ! ((Color & $FFFFFF) << 32)
EndSelect
ListIconCellColor(Row) = CellColor
EndProcedure
Macro SetGadgetItemColor(GadgetID, Row, ColorType, Color)
SetGadgetItemColorEx(GadgetID, Row, ColorType, Color)
EndMacro
OpenWindow(0, 200, 100, 430, 150,
"Change color of text and background in single rows")
ListViewGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20)
For i = 1 To 10
AddGadgetItem(0, -1, "Line " + Str(i))
Next i
class_addMethod(DelegateClass, Selector, @CellDisplayCallback(), "v@:@@@@")
CocoaMessage(0, GadgetID(0), "setDelegate:", AppDelegate)
; ----- Set background color of line 1 to yellow
SetGadgetItemColor(0, 0, #PB_Gadget_BackColor, $FFFF)
; ----- Set background color of line 5 to X11 color "Aquamarine"
SetGadgetItemColor(0, 4, #PB_Gadget_BackColor, $D4FF7F)
; ----- Set text color of line 3 to blue
SetGadgetItemColor(0, 2, #PB_Gadget_FrontColor, $FF0000)
; ----- Set text color of line 5 to red
SetGadgetItemColor(0, 4, #PB_Gadget_FrontColor, $FF)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
I had to modify this example to also work with the new PB 6.00 x64 Beta 7 because Fred modified the ListViewGadget and ListIconGadget internally:
You find the modified example (working in both PB 6.00 x64 Beta 7 and pre Beta 7) here.Fred wrote:We now use a specialized PBIconTextCell for ListIconGadget and ListViewGadget (in the :dataCellForTableColumn callback). It allows us to properly display an icon in the same column as text for ListIconGadget() instead of having a separate column as before (which was not good looking).