Adjust ListIcon column width and window size dynamically

Share your advanced PureBasic knowledge/code with the community.
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Adjust ListIcon column width and window size dynamically

Post by Shardik »

I had the need to always make visible the whole content of a cell in a ListIconGadget and to adjust the width of a column when displaying a new entry that doesn't fit completely into that column. Of course the width of the ListIconGadget and the width of the window have to be adjusted respectively. Even the centered placement of a button is taken care of. This code is improving on a previous example of srod and myself:
http://www.purebasic.fr/english/viewtop ... 75&start=4
http://www.purebasic.fr/english/viewtop ... 75&start=8

Code: Select all

Procedure.L GetTextWidth(Text.S) 
  Protected TextWidth.L 

  If StartDrawing(WindowOutput(0)) 
    FontID = GetGadgetFont(0) 
    DrawingFont(FontID) 
    TextWidth = TextWidth(Text) 
    StopDrawing() 
  EndIf 
  
  ProcedureReturn TextWidth 
EndProcedure 


#WindowHeight = 110 

Dim ColumnWidth.L(2) 

ColumnWidthIncreased.L 
ColumnWidthSum.l 
i.L 
TextWidth.L 
WindowWidth.L

WindowWidth = 239
ExamineDesktops()

If OpenWindow(0, 100, 100, WindowWidth, #WindowHeight, "Adjust column width", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  If CreateGadgetList(WindowID(0)) 
    If LoadFont(0, "Arial", 10, #PB_Font_Bold) 
      SetGadgetFont(#PB_Default, FontID(0)) 
    EndIf 

    ListIconGadget(0, 5, 5, WindowWidth - 10, #WindowHeight - 35, "Column 1", 75, #PB_ListIcon_AlwaysShowSelection) 
    AddGadgetColumn(0, 1, "Column 2", 75) 
    AddGadgetColumn(0, 2, "Column 3", 75) 
    ButtonGadget(1, (WindowWidth - 130) / 2, #WindowHeight - 25, 130, 20, "Generate new row") 

    For i = 0 To 2 
      ColumnWidth(i) = SendMessage_(GadgetID(0), #LVM_GETCOLUMNWIDTH, i, 0) 
    Next i 

    Repeat 
      WindowEvent = WaitWindowEvent() 

      If WindowEvent = #PB_Event_Gadget 
        If EventGadget() = 1 
          AddGadgetItem(0, -1, ReplaceString(Space(Random(11) + 1), " ", "+") + #LF$ + ReplaceString(Space(Random(11) + 1), " ", "+") + #LF$ + ReplaceString(Space(Random(11) + 1), " ", "+")) 

          ColumnWidthIncreased = #False 

          For i = 0 To 2 
            TextWidth = GetTextWidth(GetGadgetItemText(0, CountGadgetItems(0) - 1, i)) 

            If TextWidth + 12 > ColumnWidth(i) 
              ColumnWidth(i) = TextWidth + 12 
              SendMessage_(GadgetID(0), #LVM_SETCOLUMNWIDTH, i, ColumnWidth(i)) 
              ColumnWidthIncreased = #True 
            EndIf 
          Next i 

          If ColumnWidthIncreased 
            ColumnWidthSum = 0 

            For i = 0 To 2 
              ColumnWidthSum = ColumnWidthSum + ColumnWidth(i) 
            Next i 

            WindowWidth = ColumnWidthSum + 14
            ResizeGadget(0, #PB_Ignore, #PB_Ignore, ColumnWidthSum + 4, #PB_Ignore) 
            ResizeGadget(1, (ColumnWidthSum + 14 - 130) / 2, #PB_Ignore, 130, #PB_Ignore) 
            ResizeWindow(0, (DesktopWidth(0) - WindowWidth) / 2, #PB_Ignore, WindowWidth, #PB_Ignore) 
          EndIf 

          If CountGadgetItems(0) = 3 
            DisableGadget(1, #True) 
          EndIf 
        EndIf 
      EndIf 
    Until WindowEvent = #PB_Event_CloseWindow 
  EndIf 
EndIf
Added: When resizing the window, it is again placed in a centered position on the desktop.