Colouring rows in a ListIconGadget problems

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Colouring rows in a ListIconGadget problems

Post by Fangbeast »

My tired brain is not processing the bleeding obvious somehow.

I have 2 programs, both with 2 ListIconGadgets. Program 1 shows alternate colours for the rows in the ListIconGadget when the option is enabled. Program 2 does not, even though I have compared my logic until I am blue in the face.

On program 2, the first ListIcongadget is showing coloured rows but I cannot seem to get the second to 'take' the colours.

Code: Select all

  ;------------------------------------------------------------------------------------------------
  ; Toggle individual rows to different colours
  ;------------------------------------------------------------------------------------------------
  
  If Toggle\ColourBand = #True            ;: Debug "Colour band is enabled:  " + Str(Toggle\ColourBand)
    If Program\AlternateRow = #True       ;: Debug "Alternate row is:        " + Str(Program\AlternateRow) : Debug "Line number is: "  + Str(LineNumber.i)
      Program\AlternateRow = #False       ;: Debug "Alternate row is:        " + Str(Program\AlternateRow) :  Debug "------------------------------------------------------------"
      SetGadgetItemColor(#Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_BackColor,   $D4D4D4, -1)
      SetGadgetItemColor(#Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_FrontColor,  $000000, -1)
    ElseIf Program\AlternateRow = #False ;: Debug "Alternate row is:        " + Str(Program\AlternateRow)
      Program\AlternateRow = #True        ;: Debug "Alternate row is:        " + Str(Program\AlternateRow) :  Debug "------------------------------------------------------------"
      SetGadgetItemColor(#Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_BackColor,   $C1C1C1, -1)
      SetGadgetItemColor(#Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_FrontColor,  $FFFFFF, -1)
    EndIf
  EndIf
  
The code above is EXACTLY the same (apart from the different listicon) that I can see in program 1 that works. I am sure it is my eyesight playing up again. My right eye has issues with shadows where the contents have broken up a bit so I have troubles focusing.

If anyone has time, I have also archived up the project if there is interest?
Amateur Radio, D-STAR/VK3HAF
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Colouring rows in a ListIconGadget problems

Post by Paul »

Maybe I'm not understanding what you are trying to do because you didn't post a working example but if I plug your logic into two separate ListIconGadgets it seems to work like you want it to...
Are you sure you are specifying which ListIconGadget you want colored?

Code: Select all

  If OpenWindow(0, 0, 0, 600, 300, "SetGadgetItemColor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ListIconGadget(0, 10, 10, 280, 280, "Column 0", 100)
    AddGadgetColumn(0, 1, "Column 1", 100)
    For i = 1 To 10
      AddGadgetItem(0, -1, "Text 1"+Chr(10)+"Text 2")
    Next

    ListIconGadget(1, 300, 10, 280, 280, "Column 0", 100)
    AddGadgetColumn(1, 1, "Column 1", 100)
    For i = 1 To 10
      AddGadgetItem(1, -1, "Text 1"+Chr(10)+"Text 2")
    Next
    
    
Gadget_Keeper_Titles=0
For LineNumber=0 To 10
    If AlternateRow = #True       ;: Debug "Alternate row is:        " + Str(Program\AlternateRow) : Debug "Line number is: "  + Str(LineNumber.i)
      AlternateRow = #False       ;: Debug "Alternate row is:        " + Str(Program\AlternateRow) :  Debug "------------------------------------------------------------"
      SetGadgetItemColor(Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_BackColor,   $D4D4D4)
      SetGadgetItemColor(Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_FrontColor,  $000000)
    ElseIf AlternateRow = #False ;: Debug "Alternate row is:        " + Str(Program\AlternateRow)
      AlternateRow = #True        ;: Debug "Alternate row is:        " + Str(Program\AlternateRow) :  Debug "------------------------------------------------------------"
      SetGadgetItemColor(Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_BackColor,   $C1C1C1)
      SetGadgetItemColor(Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_FrontColor,  $FFFFFF)
    EndIf
Next
    

Gadget_Keeper_Titles=1
For LineNumber=0 To 10
    If AlternateRow = #True       ;: Debug "Alternate row is:        " + Str(Program\AlternateRow) : Debug "Line number is: "  + Str(LineNumber.i)
      AlternateRow = #False       ;: Debug "Alternate row is:        " + Str(Program\AlternateRow) :  Debug "------------------------------------------------------------"
      SetGadgetItemColor(Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_BackColor,   $D4D4D4)
      SetGadgetItemColor(Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_FrontColor,  $000000)
    ElseIf AlternateRow = #False ;: Debug "Alternate row is:        " + Str(Program\AlternateRow)
      AlternateRow = #True        ;: Debug "Alternate row is:        " + Str(Program\AlternateRow) :  Debug "------------------------------------------------------------"
      SetGadgetItemColor(Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_BackColor,   $C1C1C1)
      SetGadgetItemColor(Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_FrontColor,  $FFFFFF)
    EndIf
Next    

    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
Image Image
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Colouring rows in a ListIconGadget problems

Post by Fangbeast »

That's the problem. Your simple example works, the PB manual example works. My MyStuff home inventory colouring works.

My Keeper program doesn't. The module in my Keeper program that display the string of info and colours the bands is functionally and codewise the same as the one in my MyStuff program and I can't see why one works and one doesn't.

Damn and blast my useless eyesight.

I'll see if there is some way of creating a smaller example from the big program. That will take time tough (for me).
Amateur Radio, D-STAR/VK3HAF
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Colouring rows in a ListIconGadget problems

Post by Fangbeast »

This routine gets called each time a string of data is requested from the database.

I'll work up a smaller example on the weekend and I net it works too (Rolls eyes) but the main program wonts.

Code: Select all

; Handle all list display jobs from add, edit, list, import modes etc

Procedure DisplayTitleString(LineNumber.i, DisplayMode.s)

  Select DisplayMode.s

    Case  "AddMode"
    
      ; Add Keeper items to the list
      
      TitleString.s       = Keeper\Title         +  " (" +   Keeper\Attachments  + ")"  + #LF$
      
      CategoryString.s    = Keeper\Category                                             + #LF$
      
     ;InformationString.s = Keeper\Information                                          + #LF$
      
      FavouriteString.s   = Keeper\Favourite                                            + #LF$

      LockedString.s      = Keeper\Locked                                               + #LF$
      
      DeletedString.s     = Keeper\Deleted                                              + #LF$
      
      UpdatedString.s     = Keeper\Updated                                              + #LF$
      
      OwnerString.s       = Keeper\Owner                                                + #LF$
      
      AttachmentString.s  = Keeper\Attachments                                          + #LF$
      
      RecordString.s      = Keeper\Record

      ; 
      
      ListString.s        = TitleString.s   + CategoryString.s    + FavouriteString.s
      
      ListString.s        + LockedString.s  + DeletedString.s     + UpdatedString.s

      ListString.s        + OwnerString.s   + AttachmentString.s  + RecordString.s

      ; 
      
      AddGadgetItem(#Gadget_Keeper_Titles, #AtTheEndOfTheList,  KillQuote(ListString.s))
      
      ; Set a single colum to a different colour

      ;SetGadgetItemColor(#Gadget_Keeper_Titles, #PB_All, #PB_Gadget_FrontColor, $800080, #TitleColumn)
      
      ;SetGadgetItemColor(#Gadget_Keeper_Titles, #PB_All, #PB_Gadget_FrontColor, $800080, #CategoryColumn)
      
      ;SetGadgetItemColor(#Gadget_Keeper_Titles, #PB_All, #PB_Gadget_FrontColor, $800080, #FavouriteColumn)
      
      ;SetGadgetItemColor(#Gadget_Keeper_Titles, #PB_All, #PB_Gadget_FrontColor, $800080, #LockedColumn)
      
      ;SetGadgetItemColor(#Gadget_Keeper_Titles, #PB_All, #PB_Gadget_FrontColor, $800080, #DeletedColumn)
      
      ;SetGadgetItemColor(#Gadget_Keeper_Titles, #PB_All, #PB_Gadget_FrontColor, $800080, #UpdatedColumn)
      
      ;SetGadgetItemColor(#Gadget_Keeper_Titles, #PB_All, #PB_Gadget_FrontColor, $FF0000, #OwnerColumn)
      
      ;SetGadgetItemColor(#Gadget_Keeper_Titles, #PB_All, #PB_Gadget_FrontColor, $800080, #AttachmentsColumn)
      
      ;SetGadgetItemColor(#Gadget_Keeper_Titles, #PB_All, #PB_Gadget_FrontColor, $800080, #RecordColumn)
      
      ; 
      
      ListString.s = #Empty$

      ; Update columns in the list for an edited record
      
    Case  "EditMode"
      
      ; Update the details in the list for the selected record
      
      SetGadgetItemText(#Gadget_Keeper_Titles, LineNumber.i, KillQuote(Keeper\Title) +  " (" +   Keeper\Attachments  + ")", #TitleColumn)
      
      SetGadgetItemText(#Gadget_Keeper_Titles, LineNumber.i, KillQuote(Keeper\Category),                                    #CategoryColumn)
      
     ;SetGadgetItemText(#Gadget_Keeper_Titles, LineNumber.i, KillQuote(Keeper\Information),                                 #InformationColumn)
      
      SetGadgetItemText(#Gadget_Keeper_Titles, LineNumber.i,           Keeper\Favourite,                                    #FavouriteColumn)

      SetGadgetItemText(#Gadget_Keeper_Titles, LineNumber.i,           Keeper\Locked,                                       #LockedColumn)
      
      SetGadgetItemText(#Gadget_Keeper_Titles, LineNumber.i,           Keeper\Deleted,                                      #DeletedColumn)
      
      SetGadgetItemText(#Gadget_Keeper_Titles, LineNumber.i,           Keeper\Updated,                                      #UpdatedColumn)
      
      SetGadgetItemText(#Gadget_Keeper_Titles, LineNumber.i,           Keeper\Owner,                                        #OwnerColumn)
      
      SetGadgetItemText(#Gadget_Keeper_Titles, LineNumber.i,           Keeper\Attachments,                                  #AttachmentsColumn)
      
      SetGadgetItemText(#Gadget_Keeper_Titles, LineNumber.i,           Keeper\Record,                                       #RecordColumn)

      ; 
      
  EndSelect
  
  ; Toggle individual rows to different colours
  
  If Toggle\ColourBand = #True            ;: Debug "Colour band is enabled:  " + Str(Toggle\ColourBand)
    
    If Program\AlternateRow = #True       ;: Debug "Alternate row is:        " + Str(Program\AlternateRow) : Debug "Line number is: "  + Str(LineNumber.i)
      
      Program\AlternateRow = #False       ;: Debug "Alternate row is:        " + Str(Program\AlternateRow) :  Debug "------------------------------------------------------------"
      
      SetGadgetItemColor(#Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_BackColor,   $D4D4D4, -1)
      
      SetGadgetItemColor(#Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_FrontColor,  $000000, -1)
      
    ElseIf Program\AlternateRow = #False ;: Debug "Alternate row is:        " + Str(Program\AlternateRow)
      
      Program\AlternateRow = #True        ;: Debug "Alternate row is:        " + Str(Program\AlternateRow) :  Debug "------------------------------------------------------------"
      
      SetGadgetItemColor(#Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_BackColor,   $C1C1C1, -1)
      
      SetGadgetItemColor(#Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_FrontColor,  $FFFFFF, -1)
      
    EndIf
    
  EndIf
  
  If Keeper\Locked = "1"
    
    SetGadgetItemImage(#Gadget_Keeper_Titles, LineNumber.i, ImageID(#Image_ListLocked))
    
  ElseIf  Keeper\Locked <> "1"
    
    If Keeper\Deleted = "1"
      
      SetGadgetItemData(#Gadget_Keeper_Titles,  LineNumber.i, Val(Keeper\Deleted))
      
      RedrawWindow_(GadgetID(#Gadget_Keeper_Titles), 0, 0, #RDW_INVALIDATE | #RDW_ERASE)
      
      SetGadgetItemImage(#Gadget_Keeper_Titles, LineNumber.i, ImageID(#Image_ListDeleted))
      
    ElseIf Keeper\Deleted <> "1"
      
      If Keeper\Favourite = "1"
        
        SetGadgetItemImage(#Gadget_Keeper_Titles, LineNumber.i, ImageID(#Image_ListFavourite))
        
      ElseIf Keeper\Favourite <> "1"
        
        SetGadgetItemImage(#Gadget_Keeper_Titles, LineNumber.i, ImageID(#Image_ListNormal))
        
      EndIf
      
    EndIf
    
  EndIf
  
EndProcedure

Amateur Radio, D-STAR/VK3HAF
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Colouring rows in a ListIconGadget problems

Post by Fangbeast »

I give up Paul, too hard, thanks for trying to help. Been comparing 2 programs line by line in the procedures (Identical except for variable names) and one works, one doesn't.

Been doing this for 5 hours yesterday, 3 hours today. Time to do something else.
Dishes need washing, loo needs cleaning etc:)
Amateur Radio, D-STAR/VK3HAF
User avatar
blueb
Addict
Addict
Posts: 1044
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Colouring rows in a ListIconGadget problems

Post by blueb »

Paul wrote: ...

Code: Select all

Gadget_Keeper_Titles=1
For LineNumber=0 To 10
    If AlternateRow = #True       ;: Debug "Alternate row is:        " + Str(Program\AlternateRow) : Debug "Line number is: "  + Str(LineNumber.i)
      AlternateRow = #False       ;: Debug "Alternate row is:        " + Str(Program\AlternateRow) :  Debug "------------------------------------------------------------"
      SetGadgetItemColor(Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_BackColor,   $D4D4D4)
      SetGadgetItemColor(Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_FrontColor,  $000000)
    ElseIf AlternateRow = #False ;: Debug "Alternate row is:        " + Str(Program\AlternateRow)
      AlternateRow = #True        ;: Debug "Alternate row is:        " + Str(Program\AlternateRow) :  Debug "------------------------------------------------------------"
      SetGadgetItemColor(Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_BackColor,   $C1C1C1)
      SetGadgetItemColor(Gadget_Keeper_Titles, LineNumber.i, #PB_Gadget_FrontColor,  $FFFFFF)
    EndIf
Next    

    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
Thanks Paul.. nice straightforward example. 8)
- It was too lonely at the top.

System : PB 6.10 LTS (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
kpeters58
Enthusiast
Enthusiast
Posts: 341
Joined: Tue Nov 22, 2011 5:11 pm
Location: Kelowna, BC, Canada

Re: Colouring rows in a ListIconGadget problems

Post by kpeters58 »

This is how I usually do this:

Code: Select all

#STD_GRID    = #PB_ListIcon_GridLines | #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection
#DIALOG      = #PB_Window_ScreenCentered | #PB_Window_TitleBar | #PB_Window_SystemMenu  
#AltRowColor = $FAF0E6 ; light blue

Structure DataRecord
  Field1.s
  Field2.s
  Field3.s
EndStructure
Global NewList DataRecords.DataRecord(), Grid

AddElement(DataRecords()): DataRecords()\Field1 = "Canada":    DataRecords()\Field2 = "Ottawa":    DataRecords()\Field3 = "America": 
AddElement(DataRecords()): DataRecords()\Field1 = "Germany":   DataRecords()\Field2 = "Berlin":    DataRecords()\Field3 = "Europe": 
AddElement(DataRecords()): DataRecords()\Field1 = "Singapore": DataRecords()\Field2 = "Singapore": DataRecords()\Field3 = "Asia": 
AddElement(DataRecords()): DataRecords()\Field1 = "Nepal":     DataRecords()\Field2 = "Katmandu":  DataRecords()\Field3 = "Asia": 
AddElement(DataRecords()): DataRecords()\Field1 = "Sudan":     DataRecords()\Field2 = "Khartoum":  DataRecords()\Field3 = "Africa": 
AddElement(DataRecords()): DataRecords()\Field1 = "Australia": DataRecords()\Field2 = "Canberra":  DataRecords()\Field3 = "Australia": 

Procedure.i iif(Expr.i, TrueVal.i, FalseVal.i)
  If Bool(Expr)
    ProcedureReturn TrueVal
  Else  
    ProcedureReturn FalseVal
  EndIf   
EndProcedure  

Procedure LoadGrid()
  Protected bgcolor = #AltRowColor
    
  ClearGadgetItems(Grid)    
  ForEach DataRecords()
    AddGadgetItem(Grid, -1, DataRecords()\Field1 + #LF$ + DataRecords()\Field2 + #LF$ + DataRecords()\Field3)
    SetGadgetItemColor(Grid, CountGadgetItems(Grid) - 1, #PB_Gadget_BackColor, bgcolor, #PB_All) 
    bgcolor = iif(Bool(bgcolor = #White), #AltRowColor, #White)      
  Next  
EndProcedure  

If OpenWindow(#PB_Any, 0, 0, 800, 600, "Test", #DIALOG)
  Grid = ListIconGadget(#PB_Any, 20, 20, 760, 560, "Country", 250, #STD_GRID)
  AddGadgetColumn(Grid, 2, "Capital",   250) 
  AddGadgetColumn(Grid, 3, "Continent", 255) 
  LoadGrid()
EndIf  

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
 
PB 5.73 on Windows 10 & OS X High Sierra
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Colouring rows in a ListIconGadget problems

Post by Paul »

kpeters58 wrote:This is how I usually do this:

Code: Select all

Procedure LoadGrid()
  Protected bgcolor = #AltRowColor
    
  ClearGadgetItems(Grid)    
  ForEach DataRecords()
    AddGadgetItem(Grid, -1, DataRecords()\Field1 + #LF$ + DataRecords()\Field2 + #LF$ + DataRecords()\Field3)
    SetGadgetItemColor(Grid, CountGadgetItems(Grid) - 1, #PB_Gadget_BackColor, bgcolor, #PB_All) 
    bgcolor = iif(Bool(bgcolor = #White), #AltRowColor, #White)      
  Next  
EndProcedure  
 
Personally I would probably do this...

Code: Select all

Procedure LoadGrid()
  Protected bgcolor = #AltRowColor, row=0
    
  ClearGadgetItems(Grid)    
  ForEach DataRecords()
    AddGadgetItem(Grid, row, DataRecords()\Field1 + #LF$ + DataRecords()\Field2 + #LF$ + DataRecords()\Field3)
    SetGadgetItemColor(Grid, row, #PB_Gadget_BackColor, bgcolor, #PB_All) 
    If Mod(row,2):bgcolor=#AltRowColor:Else:bgcolor=#White:EndIf
    row+1     
  Next  
EndProcedure
Image Image
User avatar
kpeters58
Enthusiast
Enthusiast
Posts: 341
Joined: Tue Nov 22, 2011 5:11 pm
Location: Kelowna, BC, Canada

Re: Colouring rows in a ListIconGadget problems

Post by kpeters58 »

I actually had speed-tested an almost identical version when I wrote mine, as I had thought it to be faster.

I tested it with 10,000 rows and execution times were identical (quite to my surprise - I had thought that
CountGadgetItems(Grid) - 1
would be more expensive), so I went with my current version as it's shorter and uses fewer variables and, to my eyes, is more elegant :)
PB 5.73 on Windows 10 & OS X High Sierra
User avatar
robertfern
User
User
Posts: 23
Joined: Fri Feb 02, 2018 10:33 pm
Location: New Jersey

Re: Colouring rows in a ListIconGadget problems

Post by robertfern »

Just so you know, SetGadgetItemColor() doesn't work on Mac as per PB help.
This function is supported by the following gadgets:

- ListIconGadget()
- TreeGadget()

Note: SetGadgetItemColor() isn't supported on MacOS X with these two gadgets.
Here is a one liner for the Mac to have alternating row colors.

Code: Select all

CocoaMessage(0, GadgetID(Grid), "setUsesAlternatingRowBackgroundColors:", #YES)
It currently doesn't let you set the colors themselves.
I'm looking into a way to do so.

Wish me luck!
Mac OSX Ventura & Windows 10, PB 6.04
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Colouring rows in a ListIconGadget problems

Post by Fangbeast »

Out of the 2 ListIconGadgets in the problem program, the first one is coloured and the second is not. Despite the fact that another program with the same setup and the same colouring code works for both gadgets.

So I think my eyesight has put something somewhere that I can't find it.

I will try the alternate methods shown here on the weekend if I don't fall off the roof (Rust to fix, hehehehe)
Amateur Radio, D-STAR/VK3HAF
Post Reply