Colorize text in ListViewGadget

Mac OSX specific forum
WilliamL
Addict
Addict
Posts: 1215
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Colorize text in ListViewGadget

Post 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
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [6.0B7M1] Colorize text in ListViewGadget

Post 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.
WilliamL
Addict
Addict
Posts: 1215
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: [6.0B7M1] Colorize text in ListViewGadget

Post 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...
Last edited by WilliamL on Fri May 13, 2022 3:05 pm, edited 1 time in total.
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [6.0B7M1] Colorize text in ListViewGadget

Post 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).
Last edited by Shardik on Wed May 11, 2022 12:20 pm, edited 1 time in total.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [6.0B7M1] Colorize text in ListViewGadget

Post by mk-soft »

It would be nice if we knew how the cell is rendered now.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [6.0B7M1] Colorize text in ListViewGadget

Post 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).
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [6.0B7M1] Colorize text in ListViewGadget

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
deseven
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: [6.0B7M1] Colorize text in ListViewGadget

Post 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.
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [6.0B7M1] Colorize text in ListViewGadget

Post 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
Last edited by Shardik on Thu May 12, 2022 6:55 pm, edited 2 times in total.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [6.0B7M1] Colorize text in ListViewGadget

Post by mk-soft »

You can get the gadget number inside callback with

Code: Select all

Gadget = CocoaMessage(0, TableView, "tag")
;)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [6.0B7M1] Colorize text in ListViewGadget

Post 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.
WilliamL
Addict
Addict
Posts: 1215
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: [6.0B7M1] Colorize text in ListViewGadget

Post by WilliamL »

Thanks Shardik,

That works for me. (whew!)

:D
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Re: [6.0B7M1] Colorize text in ListViewGadget

Post 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?
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Re: [6.0B7M1] Colorize text in ListViewGadget

Post 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
User avatar
Thorsten1867
Addict
Addict
Posts: 1366
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Re: Colorize text in ListViewGadget

Post by Thorsten1867 »

Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
Post Reply