Page 1 of 1

Autosizing columns within ListIconGadgets (Windows only)

Posted: Fri Oct 21, 2022 4:20 am
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).

Re: Autosizing columns within ListIconGadgets (Windows only)

Posted: Fri Oct 21, 2022 11:15 am
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?

Re: Autosizing columns within ListIconGadgets (Windows only)

Posted: Fri Oct 21, 2022 1:08 pm
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.