Autosizing columns within ListIconGadgets (Windows only)

Share your advanced PureBasic knowledge/code with the community.
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Autosizing columns within ListIconGadgets (Windows only)

Post by jacdelad »

This code autosizes the columns of a ListIconGadget. The remaining space will be attached to the column in the second parameter (-1 or don't define it for the last column to be used).

Code: Select all

CompilerIf #PB_Compiler_OS=#PB_OS_Windows
  Procedure AutoSizeLIG(Gadget.i,Column.l=-1);Return Values: 1=OK,0=Error
    If IsGadget(Gadget)
      Protected ColumnCount.l=GetGadgetAttribute(Gadget,#PB_ListIcon_ColumnCount),Counter.l,TempWidth.l
      If ColumnCount<=Column Or Column<-1
        ProcedureReturn 0
      Else
        If Column=-1
          Column+ColumnCount
        EndIf
        LockWindowUpdate_(GadgetID(Gadget))
        For Counter=0 To ColumnCount-2
          SendMessage_(GadgetID(Gadget),#LVM_SETCOLUMNWIDTH,Counter,#LVSCW_AUTOSIZE)
        Next
        SendMessage_(GadgetID(Gadget),#LVM_SETCOLUMNWIDTH,ColumnCount-1,#LVSCW_AUTOSIZE_USEHEADER)
        TempWidth=GetGadgetItemAttribute(Gadget,0,#PB_ListIcon_ColumnWidth,ColumnCount-1)
        SendMessage_(GadgetID(Gadget),#LVM_SETCOLUMNWIDTH,ColumnCount-1,#LVSCW_AUTOSIZE)
        SetGadgetItemAttribute(Gadget,0,#PB_ListIcon_ColumnWidth,GetGadgetItemAttribute(Gadget,0,#PB_ListIcon_ColumnWidth,Column)+TempWidth-GetGadgetItemAttribute(Gadget,0,#PB_ListIcon_ColumnWidth,ColumnCount-1),Column)
        LockWindowUpdate_(0)
        ProcedureReturn 1
      EndIf
    EndIf
  EndProcedure
CompilerElse
  CompilerError "Only for Windows!"
CompilerEndIf

CompilerIf #PB_Compiler_IsMainFile
  OpenWindow(0,0,0,800,540,"AutoSizeLIG",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  ListIconGadget(0,0,0,800,500,"First",100,#PB_ListIcon_FullRowSelect|#PB_ListIcon_GridLines|#PB_ListIcon_AlwaysShowSelection)
  AddGadgetColumn(0,1,"Second",100)
  AddGadgetColumn(0,2,"Third",100)
  AddGadgetColumn(0,3,"Fourth",100)
  AddGadgetColumn(0,4,"Fifth",100)
  AddGadgetColumn(0,5,"Sixth",100)
  ButtonGadget(1,80,510,80,20,"Go!")
  SpinGadget(2,10,510,60,20,0,5,#PB_Spin_Numeric|#PB_Spin_ReadOnly)
  SetGadgetText(2,"3")
  
  AddGadgetItem(0,-1,"abcdefg"+#LF$+"abcde"+#LF$+"a"+#LF$+"abcdefghijk"+#LF$+"abcdef"+#LF$+"abcdefgh")
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            Select EventType()
              Case #PB_EventType_LeftClick
                AutoSizeLIG(0,Val(GetGadgetText(2)))
            EndSelect
        EndSelect
    EndSelect
  ForEver
CompilerEndIf
Let me know if there's something to improve (like also making sure the headers are fully visible).
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Axolotl
Addict
Addict
Posts: 838
Joined: Wed Dec 31, 2008 3:36 pm

Re: Autosizing columns within ListIconGadgets (Windows only)

Post by Axolotl »

Thanks for sharing.

I used the API functions for a while now.
Out of curiosity:
Why do you use .l instead of .i for your variable declaration ?
Wouldn't it be better to use an intermediate variable hGad = GadgetID(Gadget) instead of calling GadgetID(Gadget) all the time?
For example, do these things affect the speed of execution?
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Autosizing columns within ListIconGadgets (Windows only)

Post by jacdelad »

Hi Axolotl,
I used .l for some variables, because 4 billion columns should be enough (even on x64-systems).
I don't know how much performance GadgetID() needs, but yes, storing it once and reusing it could be a small improvement.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply