Module TableView Colors (ListViewGadget and ListIconGadget)

Mac OSX specific forum
User avatar
mk-soft
Addict
Addict
Posts: 3110
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Module TableView Colors (ListViewGadget and ListIconGadget)

Post by mk-soft »

A great thanks to Shardik for base of the code :wink:

Update v1.02
- Added #PB_All for column
- Bugfix ListViewGadget

Update v1.03
- Added set color to default and small trick to update tableview

Update v1.04
- Cleanup code

Update v1.4.1
- Bugfix Image

Update v1.4.2
- Bugfix selection

Update v1.4.3
- Bugfix CheckBox

Code: Select all

;-TOP

; Comment   : NSTableView Colors
; Author    : Shardik (Base Of Code)
; Author 2  : mk-soft, Shardik (Bug fixes for items with image)
; Create    : 20.01.2019
; Update    : 10.08.2019
; Version   : v 1.4.3
; OS        : MacOS

; *****************************************************************************

;- Declare Module

DeclareModule TableViewColor
  
  Declare UseTableViewColor()
  Declare SetSelectionColor(Gadget, TextColor, BackColor)
  Declare MySetGadgetItemColor(Gadget, Item, ColorType, Color, Column = 0)
  
  Macro SetGadgetItemColor(Gadget, Item, ColorType, Color, Column = 0)
    MySetGadgetItemColor(Gadget, Item, ColorType, Color, Column)
  EndMacro
  
EndDeclareModule

;- Module

Module TableViewColor
  
  EnableExplicit
  
  ; Global
  Structure udtColor
    TextColor.i
    BackColor.i
  EndStructure
  
  Structure udtTableViewColor
    GadgetType.i
    IsSelectionColor.i
    SelectionColor.udtColor
    Map Cell.udtColor()
  EndStructure
  
  Global IsTableViewColor = #False
  Global NewMap TableViewColor.udtTableViewColor()
  
  Macro PB(Function)
    Function
  EndMacro
  
  ;} ----------
  
  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, *Selector, *TableView, *Cell,
                                 *Column, Row.i)
    Protected CellFrame.NSRect
    Protected Column.i
    Protected *Column0, *Column1
    Protected Ident.s
    Protected ColumnOffset
    Protected SelectedRow.i
    Protected CellKey.s
    
    Column = CocoaMessage(0, CocoaMessage(0, *TableView, "tableColumns"),
                          "indexOfObject:", *Column)
    *Column0 = CocoaMessage(0, CocoaMessage(0, *TableView, "tableColumns"),
                            "objectAtIndex:", 0)
    
    Ident = PeekS(CocoaMessage(0, CocoaMessage(0, *Column0, "identifier"),
                          "UTF8String"), -1, #PB_UTF8)
    If Ident = "Image"
      ColumnOffset + 1
    ElseIf Ident = "CheckBox"
      ColumnOffset + 1
      *Column1 = CocoaMessage(0, CocoaMessage(0, *TableView, "tableColumns"),
                              "objectAtIndex:", 1)
      Ident = PeekS(CocoaMessage(0, CocoaMessage(0, *Column1, "identifier"),
                                 "UTF8String"), -1, #PB_UTF8)
      If Ident = "Image"
        ColumnOffset + 1
      EndIf
    EndIf
    
    If Column < ColumnOffset
      ProcedureReturn 0
    EndIf
    
    If Not FindMapElement(TableViewColor(), Hex(*TableView))
      ProcedureReturn 0
    EndIf
    
    SelectedRow = CocoaMessage(0, *TableView, "selectedRow")
    
    If SelectedRow = Row And TableViewColor()\IsSelectionColor
      ; ----- Selection color
      If TableViewColor()\GadgetType = #PB_GadgetType_ListIcon
        Column = Val(PeekS(CocoaMessage(0, CocoaMessage(0,
                                                        *Column, "identifier"), "UTF8String"), -1, #PB_UTF8))
        
        Column + ColumnOffset
      EndIf
      
      CocoaMessage(0, *Cell, "setDrawsBackground:", #NO)
      CocoaMessage(0, *Cell, "setTextColor:",
                   NSColor(TableViewColor()\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(TableViewColor()\SelectionColor\BackColor),
                   "setFill")
      CocoaMessage(0, 0, "NSBezierPath fillRect:@", @CellFrame)
    Else
      ; ----- Cell color
      Column - ColumnOffset
      
      CellKey = Str(Row) + "/" + Str(Column)
      
      If FindMapElement(TableViewColor()\Cell(), CellKey)
        If TableViewColor()\GadgetType = #PB_GadgetType_ListIcon
          CocoaMessage(0, *Cell, "setDrawsBackground:", #YES)
          If TableViewColor()\Cell()\TextColor >= 0
            CocoaMessage(0, *Cell, "setTextColor:",
                         NSColor(TableViewColor()\Cell()\TextColor))
          Else
            CocoaMessage(0, *Cell, "setTextColor:",
                         CocoaMessage(0, 0, "NSColor controlTextColor"))
          EndIf
          
          If TableViewColor()\Cell()\BackColor >= 0
            CocoaMessage(0, *Cell, "setBackgroundColor:",
                         NSColor(TableViewColor()\Cell()\BackColor))
          Else
            CocoaMessage(0, *Cell, "setBackgroundColor:",
                         CocoaMessage(0, 0, "NSColor controlBackgroundColor"))
          EndIf
        Else
          CocoaMessage(0, *Cell, "setDrawsBackground:", #NO)
          
          If TableViewColor()\Cell()\TextColor >= 0
            CocoaMessage(0, *Cell, "setTextColor:",
                         NSColor(TableViewColor()\Cell()\TextColor))
          Else
            CocoaMessage(0, *Cell, "setTextColor:",
                         CocoaMessage(0, 0, "NSColor controlTextColor"))
          EndIf
          
          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 + 7
          EndIf
          
          ; ----- Draw cell background
          If TableViewColor()\Cell()\BackColor >= 0
            CocoaMessage(0, NSColor(TableViewColor()\Cell()\BackColor),
                         "setFill")
          Else
            CocoaMessage(0, CocoaMessage(0, 0,
                                         "NSColor controlBackgroundColor"), "setFill")
          EndIf
          
          CocoaMessage(0, 0, "NSBezierPath fillRect:@", @CellFrame)
        EndIf   
      Else
        CocoaMessage(0, *Cell, "setDrawsBackground:", #NO)
        CocoaMessage(0, *Cell, "setTextColor:",
                     CocoaMessage(0, 0, "NSColor controlTextColor"))
      EndIf
    EndIf
  EndProcedure
  
  ; ----------
  
  Procedure UseTableViewColor()
    Protected *AppDelegate = CocoaMessage(0, CocoaMessage(0, 0,
                                                          "NSApplication sharedApplication"), "delegate")
    Protected *DelegateClass = CocoaMessage(0, *AppDelegate, "class")
    Protected *Selector = sel_registerName_("tableView:willDisplayCell:" +
                                            "forTableColumn:row:")
    class_addMethod_(*DelegateClass, *Selector, @CellDisplayCallback(),
                     "v@:@@@@")
    IsTableViewColor = #True
  EndProcedure
  
  ; ----------
  
  Procedure SetSelectionColor(Gadget, TextColor, BackColor)
    Protected Key.s, *AppDelegate
    
    Key = Hex(GadgetID(Gadget))
    
    If Not FindMapElement(TableViewColor(), key)
      AddMapElement(TableViewColor(), Key)
      *AppDelegate = CocoaMessage(0, CocoaMessage(0, 0,
                                                  "NSApplication sharedApplication"), "delegate")
      CocoaMessage(0, GadgetID(Gadget), "setDelegate:", *AppDelegate)
    EndIf
    
    CocoaMessage(0, GadgetID(Gadget), "setSelectionHighlightStyle:", -1)
    TableViewColor()\GadgetType = GadgetType(Gadget)
    TableViewColor()\IsSelectionColor = #True
    TableViewColor()\SelectionColor\TextColor = TextColor
    TableViewColor()\SelectionColor\BackColor = BackColor
  EndProcedure
  
  ; ----------
  
  Procedure MySetGadgetItemColor(Gadget, Item, ColorType, Color, Column = 0)
    Protected Key.s, CellKey.s, *AppDelegate
    Protected GadgetType.i, cnt.i, col.i
    
    GadgetType = GadgetType(Gadget)
    
    If Not IsTableViewColor Or (GadgetType <> #PB_GadgetType_ListIcon And
                                GadgetType <> #PB_GadgetType_ListView)
      ProcedureReturn PB(SetGadgetItemColor)(Gadget, Item, ColorType, Color,
                                             Column)
    EndIf
    
    Key = Hex(GadgetID(Gadget))
    
    If Not FindMapElement(TableViewColor(), Key)
      AddMapElement(TableViewColor(), Key)
      *AppDelegate = CocoaMessage(0, CocoaMessage(0, 0,
                                                  "NSApplication sharedApplication"), "delegate")
      CocoaMessage(0, GadgetID(Gadget), "setDelegate:", *AppDelegate)
    EndIf
    
    TableViewColor()\GadgetType = GadgetType(Gadget)
    
    If Column = #PB_All And GadgetType = #PB_GadgetType_ListIcon
      cnt = CountGadgetItems(Gadget) - 1
      
      For col = 0 To cnt
        CellKey = Str(Item) + "/" + Str(col)
        
        If Not FindMapElement(TableViewColor()\Cell(), CellKey)
          AddMapElement(TableViewColor()\Cell(), CellKey)
          TableViewColor()\Cell()\TextColor = -1
          TableViewColor()\Cell()\BackColor = -1
        EndIf
        
        Select ColorType
          Case #PB_Gadget_FrontColor
            TableViewColor()\Cell()\TextColor = Color
          Case #PB_Gadget_BackColor
            TableViewColor()\Cell()\BackColor = Color
        EndSelect
        
        If TableViewColor()\Cell()\TextColor = -1 And
           TableViewColor()\Cell()\BackColor = -1
          DeleteMapElement(TableViewColor()\Cell())
        EndIf
      Next
    Else
      CellKey = Str(Item) + "/" + Str(Column)
      
      If Not FindMapElement(TableViewColor()\Cell(), CellKey)
        AddMapElement(TableViewColor()\Cell(), CellKey)
        TableViewColor()\Cell()\TextColor = -1
        TableViewColor()\Cell()\BackColor = -1
      EndIf
      
      Select ColorType
        Case #PB_Gadget_FrontColor
          TableViewColor()\Cell()\TextColor = Color
        Case #PB_Gadget_BackColor
          TableViewColor()\Cell()\BackColor = Color
      EndSelect
      If TableViewColor()\Cell()\TextColor = -1 And
         TableViewColor()\Cell()\BackColor = -1
        DeleteMapElement(TableViewColor()\Cell())
      EndIf
    EndIf
    
    CocoaMessage(0, GadgetID(Gadget), "reloadData")
  EndProcedure
  
EndModule

;- End Module

; *****************************************************************************

UseModule TableViewColor

UseTableViewColor()

CreateImage(0,16,16,32, #White)

If StartDrawing(ImageOutput(0))
  Circle(8, 8, 5, #Red)
  StopDrawing()
EndIf

OpenWindow(0, 200, 100, 480, 410,
           "Disable highlight and selection color example")

ListIconGadget(0, 10, 10, WindowWidth(0) - 20, 100, "Name", 110, #PB_ListIcon_CheckBoxes)
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")
SetSelectionColor(0, #Gray, #Green)
SetGadgetItemColor(0, 0, #PB_Gadget_FrontColor, #Red, 0)
SetGadgetItemColor(0, 0, #PB_Gadget_BackColor, #Yellow, 0)

SetGadgetItemColor(0, 1, #PB_Gadget_FrontColor, #Gray, 1)
SetGadgetItemColor(0, 1, #PB_Gadget_BackColor, #Blue, 1)

ListIconGadget(1, 10, 130, WindowWidth(0) - 20, 100, "Name", 110, #PB_ListIcon_CheckBoxes)
AddGadgetColumn(1, 1, "Address", GadgetWidth(0) - GetGadgetItemAttribute(0, 0,
                                                                         #PB_ListIcon_ColumnWidth) - 8)
AddGadgetItem(1, -1, "Harry Rannit" + #LF$ +
                     "12 Parliament Way, Battle Street, By the Bay", ImageID(0))
AddGadgetItem(1, -1, "Ginger Brokeit" + #LF$ +
                     "130 PureBasic Road, BigTown, CodeCity")
AddGadgetItem(1, -1, "Didi Foundit" + #LF$ +
                     "321 Logo Drive, Mouse House, Downtown")
SetSelectionColor(1, #Gray, #Green)
SetGadgetItemColor(1, 0, #PB_Gadget_FrontColor, #Red, 1)
SetGadgetItemColor(1, 0, #PB_Gadget_BackColor, #Yellow, 1)

ListViewGadget(2, 10, 250, WindowWidth(0) - 20, 100)

For i = 1 To 5
  AddGadgetItem (2, -1, "Item " + Str(i) + " of the Listview")
Next

SetSelectionColor(2, #Gray, #Green)
SetGadgetItemColor(2, 1, #PB_Gadget_FrontColor, $2222B2)
SetGadgetItemColor(2, 1, #PB_Gadget_BackColor, $9BD3FF)

ButtonGadget(3, 10, 365, 230, 30, "Remove Color from 2nd ListIcon")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 3
        SetGadgetItemColor(1, 0, #PB_Gadget_FrontColor, #PB_Default, #PB_All)
        SetGadgetItemColor(1, 0, #PB_Gadget_BackColor, #PB_Default, #PB_All)
      EndIf
  EndSelect
ForEver
Last edited by mk-soft on Sat Aug 10, 2019 1:25 pm, edited 4 times in total.
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace
IB-Software
New User
New User
Posts: 7
Joined: Sat Jun 21, 2003 12:11 pm

Re: Module TableView Colors (ListViewGadget and ListIconGadg

Post by IB-Software »

If you display an icon in the ListIconGadget, "SetSelectionColor" and marking a line causes an error message

Code: Select all

[ERROR] Object does not respond to method "setTextColor:"
Example:

Code: Select all

UseModule TableViewColor
  
  UseTableViewColor()
  
  CreateImage(0,16,16,32, #White    )
  If StartDrawing(ImageOutput(0))
    Circle(8,8,5,#Red) 
    StopDrawing()
  EndIf
   
  OpenWindow(0, 200, 100, 480, 280, "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", ImageID(0))
  AddGadgetItem(0, -1, "Ginger Brokeit" + #LF$ + "130 PureBasic Road, BigTown, CodeCity")
  AddGadgetItem(0, -1, "Didi Foundit" + #LF$ + "321 Logo Drive, Mouse House, Downtown")
  SetSelectionColor(0, #Gray, #Green)

  Repeatevent = WaitWindowEvent()
    Until event = #PB_Event_CloseWindow
User avatar
mk-soft
Addict
Addict
Posts: 3110
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module TableView Colors (ListViewGadget and ListIconGadg

Post by mk-soft »

I'll have to beg shardik and wilbert to help me. :)

The module is also under construction.
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace
User avatar
mk-soft
Addict
Addict
Posts: 3110
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module TableView Colors (ListViewGadget and ListIconGadg

Post by mk-soft »

Update v1.4.1
- Bugfix Image

But new version is not ready...
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace
IB-Software
New User
New User
Posts: 7
Joined: Sat Jun 21, 2003 12:11 pm

Re: Module TableView Colors (ListViewGadget and ListIconGadg

Post by IB-Software »

Thank you.
User avatar
Shardik
Addict
Addict
Posts: 1839
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Module TableView Colors (ListViewGadget and ListIconGadg

Post by Shardik »

IB-Software wrote:If you display an icon in the ListIconGadget, "SetSelectionColor" and marking a line causes an error message

Code: Select all

[ERROR] Object does not respond to method "setTextColor:"
Unfortunately mk-soft's module v1.04 - and my example on which his module is based - didn't support images in a ListIcon's first column. Since a first column with an image is internally based on a NSImageCell, it doesn't support the method setTextColor:, hence the error message.

mk-soft's fixed v1.4.1 currently doesn't work correctly with a ListIconGadget without images: when selecting a row the selected area doesn't cover the first column.

This is my try which is based on mk-soft's v1.04 and seemingly works with ListIconGadgets with and without an image column and with a ListViewGadget where PureBasic doesn't support the addition of images. For simplicity it currently doesn't select a first column with an image at all and starts the selection in the second column. I have tested my example on MacOS 10.6.8 'Snow Leopard' with PB 5.62 x86 and on MacOS 10.13.6 'High Sierra' with PB 5.70 x64.

Code: Select all

;-TOP

; Comment   : NSTableView Colors
; Author    : Shardik (Base Of Code)
; Author 2  : mk-soft, Shardik (Bug fixes for items with image)
; OS        : MacOS

; *****************************************************************************

;- Declare Module

DeclareModule TableViewColor
 
  Declare UseTableViewColor()
  Declare SetSelectionColor(Gadget, TextColor, BackColor)
  Declare MySetGadgetItemColor(Gadget, Item, ColorType, Color, Column = 0)
 
  Macro SetGadgetItemColor(Gadget, Item, ColorType, Color, Column = 0)
    MySetGadgetItemColor(Gadget, Item, ColorType, Color, Column)
  EndMacro
 
EndDeclareModule

;- Module

Module TableViewColor
 
  EnableExplicit
 
  ; Global
  Structure udtColor
    TextColor.i
    BackColor.i
  EndStructure
 
  Structure udtTableViewColor
    GadgetType.i
    IsSelectionColor.i
    SelectionColor.udtColor
    Map Cell.udtColor()
  EndStructure
 
  Global IsTableViewColor = #False
  Global NewMap TableViewColor.udtTableViewColor()
 
  Macro PB(Function)
    Function
  EndMacro
 
  ;} ----------
 
  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, *Selector, *TableView, *Cell,
    *Column, Row.i)
    Protected CellFrame.NSRect
    Protected Column.i
    Protected *Column0
    Protected FirstColumnContainsImage.I
    Protected SelectedRow.i
    Protected CellKey.s

    Column = CocoaMessage(0, CocoaMessage(0, *TableView, "tableColumns"),
      "indexOfObject:", *Column)
    *Column0 = CocoaMessage(0, CocoaMessage(0, *TableView, "tableColumns"),
      "objectAtIndex:", 0)

    If PeekS(CocoaMessage(0, CocoaMessage(0, *Column0, "identifier"),
      "UTF8String"), -1, #PB_UTF8) = "Image"
      FirstColumnContainsImage = #True

      If Column = 0
        ProcedureReturn 0
      EndIf
    EndIf

    If Not FindMapElement(TableViewColor(), Hex(*TableView))
      ProcedureReturn 0
    EndIf
   
    SelectedRow = CocoaMessage(0, *TableView, "selectedRow")
   
    If SelectedRow = Row And TableViewColor()\IsSelectionColor
      ; ----- Selection color
      If TableViewColor()\GadgetType = #PB_GadgetType_ListIcon
        Column = Val(PeekS(CocoaMessage(0, CocoaMessage(0,
          *Column, "identifier"), "UTF8String"), -1, #PB_UTF8))

        If FirstColumnContainsImage
          Column + 1
        EndIf
      EndIf

      CocoaMessage(0, *Cell, "setDrawsBackground:", #NO)
      CocoaMessage(0, *Cell, "setTextColor:",
        NSColor(TableViewColor()\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(TableViewColor()\SelectionColor\BackColor),
        "setFill")
      CocoaMessage(0, 0, "NSBezierPath fillRect:@", @CellFrame)
    Else
      ; ----- Cell color
      CellKey = Str(Row) + "/" + Str(Column)

      If FindMapElement(TableViewColor()\Cell(), CellKey)
        If TableViewColor()\GadgetType = #PB_GadgetType_ListIcon
          CocoaMessage(0, *Cell, "setDrawsBackground:", #YES)
          If TableViewColor()\Cell()\TextColor >= 0
            CocoaMessage(0, *Cell, "setTextColor:",
              NSColor(TableViewColor()\Cell()\TextColor))
          Else
            CocoaMessage(0, *Cell, "setTextColor:",
              CocoaMessage(0, 0, "NSColor controlTextColor"))
          EndIf

          If TableViewColor()\Cell()\BackColor >= 0
            CocoaMessage(0, *Cell, "setBackgroundColor:",
              NSColor(TableViewColor()\Cell()\BackColor))
          Else
            CocoaMessage(0, *Cell, "setBackgroundColor:",
              CocoaMessage(0, 0, "NSColor controlBackgroundColor"))
          EndIf
        Else
          CocoaMessage(0, *Cell, "setDrawsBackground:", #NO)

          If TableViewColor()\Cell()\TextColor >= 0
            CocoaMessage(0, *Cell, "setTextColor:",
              NSColor(TableViewColor()\Cell()\TextColor))
          Else
            CocoaMessage(0, *Cell, "setTextColor:",
              CocoaMessage(0, 0, "NSColor controlTextColor"))
          EndIf

          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 + 7
          EndIf

          ; ----- Draw cell background
          If TableViewColor()\Cell()\BackColor >= 0
            CocoaMessage(0, NSColor(TableViewColor()\Cell()\BackColor),
              "setFill")
          Else
            CocoaMessage(0, CocoaMessage(0, 0,
              "NSColor controlBackgroundColor"), "setFill")
          EndIf

          CocoaMessage(0, 0, "NSBezierPath fillRect:@", @CellFrame)
        EndIf   
      Else
        CocoaMessage(0, *Cell, "setDrawsBackground:", #NO)
        CocoaMessage(0, *Cell, "setTextColor:",
          CocoaMessage(0, 0, "NSColor controlTextColor"))
      EndIf
    EndIf
  EndProcedure
 
  ; ----------
 
  Procedure UseTableViewColor()
    Protected *AppDelegate = CocoaMessage(0, CocoaMessage(0, 0,
      "NSApplication sharedApplication"), "delegate")
    Protected *DelegateClass = CocoaMessage(0, *AppDelegate, "class")
    Protected *Selector = sel_registerName_("tableView:willDisplayCell:" +
      "forTableColumn:row:")
    class_addMethod_(*DelegateClass, *Selector, @CellDisplayCallback(),
      "v@:@@@@")
    IsTableViewColor = #True
  EndProcedure
 
  ; ----------
 
  Procedure SetSelectionColor(Gadget, TextColor, BackColor)
    Protected Key.s, *AppDelegate
   
    Key = Hex(GadgetID(Gadget))
   
    If Not FindMapElement(TableViewColor(), key)
      AddMapElement(TableViewColor(), Key)
      *AppDelegate = CocoaMessage(0, CocoaMessage(0, 0,
        "NSApplication sharedApplication"), "delegate")
      CocoaMessage(0, GadgetID(Gadget), "setDelegate:", *AppDelegate)
    EndIf

    CocoaMessage(0, GadgetID(Gadget), "setSelectionHighlightStyle:", -1)
    TableViewColor()\GadgetType = GadgetType(Gadget)
    TableViewColor()\IsSelectionColor = #True
    TableViewColor()\SelectionColor\TextColor = TextColor
    TableViewColor()\SelectionColor\BackColor = BackColor
  EndProcedure
 
  ; ----------
 
  Procedure MySetGadgetItemColor(Gadget, Item, ColorType, Color, Column = 0)
    Protected Key.s, CellKey.s, *AppDelegate
    Protected GadgetType.i, cnt.i, col.i
   
    GadgetType = GadgetType(Gadget)

    If Not IsTableViewColor Or (GadgetType <> #PB_GadgetType_ListIcon And
      GadgetType <> #PB_GadgetType_ListView)
      ProcedureReturn PB(SetGadgetItemColor)(Gadget, Item, ColorType, Color,
        Column)
    EndIf
   
    Key = Hex(GadgetID(Gadget))
   
    If Not FindMapElement(TableViewColor(), Key)
      AddMapElement(TableViewColor(), Key)
      *AppDelegate = CocoaMessage(0, CocoaMessage(0, 0,
        "NSApplication sharedApplication"), "delegate")
      CocoaMessage(0, GadgetID(Gadget), "setDelegate:", *AppDelegate)
    EndIf
   
    TableViewColor()\GadgetType = GadgetType(Gadget)

    If Column = #PB_All And GadgetType = #PB_GadgetType_ListIcon
      cnt = CountGadgetItems(Gadget) - 1

      For col = 0 To cnt
        CellKey = Str(Item) + "/" + Str(col)

        If Not FindMapElement(TableViewColor()\Cell(), CellKey)
          AddMapElement(TableViewColor()\Cell(), CellKey)
          TableViewColor()\Cell()\TextColor = -1
          TableViewColor()\Cell()\BackColor = -1
        EndIf

        Select ColorType
          Case #PB_Gadget_FrontColor
            TableViewColor()\Cell()\TextColor = Color
          Case #PB_Gadget_BackColor
            TableViewColor()\Cell()\BackColor = Color
        EndSelect

        If TableViewColor()\Cell()\TextColor = -1 And
          TableViewColor()\Cell()\BackColor = -1
          DeleteMapElement(TableViewColor()\Cell())
        EndIf
      Next
    Else
      CellKey = Str(Item) + "/" + Str(Column)

      If Not FindMapElement(TableViewColor()\Cell(), CellKey)
        AddMapElement(TableViewColor()\Cell(), CellKey)
        TableViewColor()\Cell()\TextColor = -1
        TableViewColor()\Cell()\BackColor = -1
      EndIf

      Select ColorType
        Case #PB_Gadget_FrontColor
          TableViewColor()\Cell()\TextColor = Color
        Case #PB_Gadget_BackColor
          TableViewColor()\Cell()\BackColor = Color
      EndSelect
      If TableViewColor()\Cell()\TextColor = -1 And
        TableViewColor()\Cell()\BackColor = -1
        DeleteMapElement(TableViewColor()\Cell())
      EndIf
    EndIf

    CocoaMessage(0, GadgetID(Gadget), "reloadData")
  EndProcedure
 
EndModule

;- End Module

; *****************************************************************************

UseModule TableViewColor
 
UseTableViewColor()

CreateImage(0,16,16,32, #White)

If StartDrawing(ImageOutput(0))
  Circle(8, 8, 5, #Red)
  StopDrawing()
EndIf

OpenWindow(0, 200, 100, 480, 410,
  "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")
SetSelectionColor(0, #Gray, #Green)
SetGadgetItemColor(0, 0, #PB_Gadget_FrontColor, #Red, 0)
SetGadgetItemColor(0, 0, #PB_Gadget_BackColor, #Yellow, 0)

ListIconGadget(1, 10, 130, WindowWidth(0) - 20, 100, "Name", 110)
AddGadgetColumn(1, 1, "Address", GadgetWidth(0) - GetGadgetItemAttribute(0, 0,
  #PB_ListIcon_ColumnWidth) - 8)
AddGadgetItem(1, -1, "Harry Rannit" + #LF$ +
  "12 Parliament Way, Battle Street, By the Bay", ImageID(0))
AddGadgetItem(1, -1, "Ginger Brokeit" + #LF$ +
  "130 PureBasic Road, BigTown, CodeCity")
AddGadgetItem(1, -1, "Didi Foundit" + #LF$ +
  "321 Logo Drive, Mouse House, Downtown")
SetSelectionColor(1, #Gray, #Green)
SetGadgetItemColor(1, 0, #PB_Gadget_FrontColor, #Red, 1)
SetGadgetItemColor(1, 0, #PB_Gadget_BackColor, #Yellow, 1)

ListViewGadget(2, 10, 250, WindowWidth(0) - 20, 100)

For i = 1 To 5
  AddGadgetItem (2, -1, "Item " + Str(i) + " of the Listview")
Next

SetSelectionColor(2, #Gray, #Green)
SetGadgetItemColor(2, 1, #PB_Gadget_FrontColor, $2222B2)
SetGadgetItemColor(2, 1, #PB_Gadget_BackColor, $9BD3FF)

ButtonGadget(3, 10, 365, 230, 30, "Remove Color from 2nd ListIcon")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 3
        SetGadgetItemColor(1, 0, #PB_Gadget_FrontColor, #PB_Default, #PB_All)
        SetGadgetItemColor(1, 0, #PB_Gadget_BackColor, #PB_Default, #PB_All)
      EndIf
  EndSelect
ForEver
User avatar
mk-soft
Addict
Addict
Posts: 3110
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module TableView Colors (ListViewGadget and ListIconGadg

Post by mk-soft »

Update v1.4.2
- Bugfix selection

Thanks Shardik :wink:
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace
User avatar
mk-soft
Addict
Addict
Posts: 3110
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module TableView Colors (ListViewGadget and ListIconGadg

Post by mk-soft »

Update v1.4.3
- Bugfix CheckBox

Works now with CheckBoxes :wink:
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace
Lebostein
Enthusiast
Enthusiast
Posts: 669
Joined: Fri Jun 11, 2004 7:07 am

Re: Module TableView Colors (ListViewGadget and ListIconGadget)

Post by Lebostein »

Colors in ListIconGadgets are possible with Mac OS. Why it is not part of PureBasic by default?
Post Reply