Page 1 of 1

Colorize text in ListViewGadget

Posted: Mon May 09, 2022 9:29 pm
by WilliamL
I've used this code in my program and with 6.0 Beta 7-M1 it no longer works.

https://www.purebasic.fr/english/viewto ... 70#p487570

The text no longer shows.

It doesn't work in 6.0 Beta 7x64 either

Re: [6.0B7M1] Colorize text in ListViewGadget

Posted: Tue May 10, 2022 9:54 am
by Fred
We changed the way yo render a cell to be better looking, so you need to adapt your OS code to match it unfortunately.

Re: [6.0B7M1] Colorize text in ListViewGadget

Posted: Tue May 10, 2022 4:46 pm
by WilliamL
Fred, thanks for the reply.

Maybe this thread should be moved to the Mac OSX forum so more people can see it and, maybe, come up with a solution.

See example by Shardik that works below...

Re: [6.0B7M1] Colorize text in ListViewGadget

Posted: Tue May 10, 2022 6:06 pm
by Shardik
WilliamL wrote: Mon May 09, 2022 9:29 pm It doesn't work in 6.0 Beta 7x64 either
I can confirm that my example linked above doesn't work anymore correctly (text isn't displayed) with PB 6.00 x64 Beta 7 on MacOS Catalina, Big Sur and Monterey (tested in Light and Dark Mode). The same example still works like a charm with PB 6.00 x64 Beta 6 and PB 5.73 x64 (in Light Mode, for Dark Mode the chosen colors still should be adjusted).

Re: [6.0B7M1] Colorize text in ListViewGadget

Posted: Tue May 10, 2022 6:32 pm
by mk-soft
It would be nice if we knew how the cell is rendered now.

Re: [6.0B7M1] Colorize text in ListViewGadget

Posted: Wed May 11, 2022 3:15 pm
by Fred
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).

Re: [6.0B7M1] Colorize text in ListViewGadget

Posted: Wed May 11, 2022 11:01 pm
by mk-soft
Thus, many of our codes for NSTableView no longer work :(

Link: https://www.purebasic.fr/english/viewtopic.php?t=77111
Link: https://www.purebasic.fr/english/viewto ... 25#p448525

and more

Re: [6.0B7M1] Colorize text in ListViewGadget

Posted: Thu May 12, 2022 3:59 pm
by deseven
This is an insanely big issue for me and surely for other people who used any customization on ListIcon and ListView gadgets on macOS. Even the most basic things stopped working now. Can anyone please explain how to work with those gadgets now? Just a general approach would be extremely useful to figure out the rest.

Re: [6.0B7M1] Colorize text in ListViewGadget

Posted: Thu May 12, 2022 6:11 pm
by Shardik
WilliamL wrote: Mon May 09, 2022 9:29 pm I've used this code in my program and with 6.0 Beta 7-M1 it no longer works.

https://www.purebasic.fr/english/viewto ... 70#p487570

The text no longer shows.

It doesn't work in 6.0 Beta 7x64 either

I have patched my CellDisplayCallback() to work with PB 6.00 x64 Beta 7 in Light Mode (for Dark Mode you should still adapt the colors). It's also working in pre Beta 7 and PB 5.73. The trick is to not only set the front and back color but also to display the text in each call of the CellDisplayCallback (which wasn't necessary in pre Beta 7):

Code: Select all

EnableExplicit

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 ListViewCellColor.Q(0)

ProcedureC CellDisplayCallback(Object.I, Selector.I, TableView.I, Cell.I,
  *Column, Row.I)
  Shared ListViewCellColor.Q()

  Protected Alpha.CGFloat
  Protected BackColor.L
  Protected Blue.CGFloat
  Protected CellColor.Q
  Protected CellText.S
  Protected FrontColor.L
  Protected Green.CGFloat
  Protected Red.CGFloat

  CellColor = ListViewCellColor(Row)

  If CellColor = 0
    CocoaMessage(0, Cell, "setDrawsBackground:", #NO)
    CocoaMessage(0, Cell, 
      "setTextColor:", CocoaMessage(0, 0, "NSColor textColor"))
  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 textColor"))
    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

  CellText = GetGadgetItemText(CocoaMessage(0, TableView, "tag"), Row, 0)
  CocoaMessage(0, Cell, "setStringValue:$", @CellText)
EndProcedure

Procedure SetGadgetItemColorEx(GadgetID.I, Row.I, ColorType.I, Color.I)
  Shared ListViewCellColor.Q()

  Protected CellColor.Q
  Protected RowCount.I

  If ArraySize(ListViewCellColor()) = 0
    RowCount = CocoaMessage(0, GadgetID(GadgetID), "numberOfRows")
    Dim ListViewCellColor(RowCount - 1)
  EndIf

  CellColor = ListViewCellColor(Row)

  Select ColorType
    Case #PB_Gadget_BackColor
      CellColor ! (Color & $FFFFFF)
    Case #PB_Gadget_FrontColor
      CellColor ! ((Color & $FFFFFF) << 32)
  EndSelect

  ListViewCellColor(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

Re: [6.0B7M1] Colorize text in ListViewGadget

Posted: Thu May 12, 2022 6:31 pm
by mk-soft
You can get the gadget number inside callback with

Code: Select all

Gadget = CocoaMessage(0, TableView, "tag")
;)

Re: [6.0B7M1] Colorize text in ListViewGadget

Posted: Thu May 12, 2022 6:50 pm
by Shardik
mk-soft wrote: Thu May 12, 2022 6:31 pm You can get the gadget number inside callback with

Code: Select all

Gadget = CocoaMessage(0, TableView, "tag")
;)
You are right, I always forget that... :wink:
I have also removed the variable Column.I from the callback because it's not needed for the ListViewGadget: the Column is always 0.

Re: [6.0B7M1] Colorize text in ListViewGadget

Posted: Thu May 12, 2022 8:00 pm
by WilliamL
Thanks Shardik,

That works for me. (whew!)

:D

Re: [6.0B7M1] Colorize text in ListViewGadget

Posted: Fri May 27, 2022 5:57 am
by Lebostein
Fred wrote: Tue May 10, 2022 9:54 am We changed the way yo render a cell to be better looking, so you need to adapt your OS code to match it unfortunately.
Is it now possible to color the Gadget with PB commands like Windows?

Re: [6.0B7M1] Colorize text in ListViewGadget

Posted: Fri May 27, 2022 8:45 am
by Lebostein
Fred wrote: Tue May 10, 2022 9:54 am We changed the way yo render a cell to be better looking, so you need to adapt your OS code to match it unfortunately.
No better looking. It looks worse. The horizontal distance between the grid and the text is almost zero now. The text looks pushed to the edge. The horizontal distance between the grid and the header title text seems unchanged:

before:
Image

now (Typesetters would shake their heads):
Image

Re: Colorize text in ListViewGadget

Posted: Fri May 27, 2022 11:23 am
by Thorsten1867