Detecting ListIconGadget column width adjustment

Just starting out? Need help? Post your questions and find answers here.
USCode
Addict
Addict
Posts: 923
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

Detecting ListIconGadget column width adjustment

Post by USCode »

As far as I can tell there doesn't appear to be any cross-platform PB events that fire when the user adjusts the width of a column in the ListIconGadget.

I'd like to save the new width in my database immediately after the user finishes adjusting each individual column so I can display the same width later. Does anyone have any suggestions?

You WinAPI gurus must have a solution at least for Windows? :wink:

Thanks!
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Detecting ListIconGadget column width adjustment

Post by srod »

The following will inform you (windows only) when the user has finished dragging a column divider. You will need to add additional code for trapping a double-click resize etc. and this involves performing some hit-testing.

Code: Select all

Global gOldProc

Procedure.i listiconProc(hWnd, uMsg, wParam, lParam)
  Protected result, *nmHEADER.HD_NOTIFY
  result = CallWindowProc_(gOldProc, hWnd, uMsg, wParam, lParam)
  Select uMsg
    Case #WM_NOTIFY
      *nmHEADER = lParam 
      Select *nmHEADER\hdr\code
        Case #HDN_ENDTRACK, #HDN_ENDTRACKW
          Debug "Resized item " + Str(*nmHEADER\iItem) + ", width = " + Str(GetGadgetItemAttribute(0, -1, #PB_ListIcon_ColumnWidth, *nmHEADER\iItem))
      EndSelect
  EndSelect
  ProcedureReturn result
EndProcedure

If OpenWindow(0, 100, 100, 300, 100, "ListIcon Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  hWnd = ListIconGadget(0, 5, 5, 290, 90, "Name", 100, #PB_ListIcon_FullRowSelect|#PB_ListIcon_AlwaysShowSelection)
  gOldProc = SetWindowLongPtr_(hWnd, #GWL_WNDPROC, @listIconProc())
  AddGadgetColumn(0, 1, "Address", 250)
  AddGadgetItem(0, -1, "Harry Rannit"+Chr(10)+"12 Parliament Way, Battle Street, By the Bay")
  AddGadgetItem(0, -1, "Ginger Brokeit"+Chr(10)+"130 PureBasic Road, BigTown, CodeCity")
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf
I may look like a mule, but I'm not a complete ass.
USCode
Addict
Addict
Posts: 923
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

Re: Detecting ListIconGadget column width adjustment

Post by USCode »

Thanks srod! I appreciate it.

I may end up using that code and tracking down code for the other platforms as an almost-ideal solution, with the ideal solution of PB itself providing notification in a cross-platform way.

I'd even settle for PB providing an event that *any* column has changed in width, if not a specific one, then I could just loop through and save all the widths.
Alternatively, I may just end up re-saving all the widths every time the user interacts with that listicongadget, though that is not my first choice.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Detecting ListIconGadget column width adjustment

Post by Shardik »

Is it really necessary to detect all column width adjustments on the fly and save
the new values immediately? It should be sufficient to intercept the closing of the
window or application and then read all column widths (even crossplatform) with

Code: Select all

GetGadgetItemAttribute(#Gadget, 0, #PB_ListIcon_ColumnWidth, Column)
to save them for the next start of your application. With the corresponding
SetGadgetItemAttribute() it is possible to display the columns with their previous
widths during the next run.
USCode
Addict
Addict
Posts: 923
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

Re: Detecting ListIconGadget column width adjustment

Post by USCode »

Thanks Shardik. Yep, I may have to change my design to do as you suggest as my current design doesn't lend itself to doing it that way easily.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Detecting ListIconGadget column width adjustment

Post by Shardik »

USCode wrote:You WinAPI gurus must have a solution at least for Windows?
Unfortunately I am no WinAPI guru nor a Linux or MacOS guru but I am trying my
best while developing my first major app for all 3 platforms simultaneously. :wink:
Nevertheless I was able to find a MacOS X API solution utilizing my first stab at a
DataBrowser callback (tested with PB 4.51 and Snow Leopard):

Code: Select all

EnableExplicit

ImportC ""
  DisposeDataBrowserItemNotificationUPP(*DataBrowserItemNotificationCallback)
  InitDataBrowserCallbacks(*DataBrowserCallbacks)
  NewDataBrowserItemNotificationUPP(*DataBrowserItemNotificationCallback)
  SetDataBrowserCallbacks(Control.I, *DataBrowserCallbacks)
EndImport

#kDataBrowserLatestCallbacks = 0
#kDataBrowserUserStateChanged = 13
#noErr = 0

#NumColumns = 2

Structure DataBrowserCallbacks
  Version.I
  DataBrowserItemDataUPP.I
  DataBrowserItemCompareUPP.I
  DataBrowserItemNotificationUPP.I
  DataBrowserAddDragItemUPP.I
  DataBrowserAcceptDragUPP.I
  DataBrowserReceiveDragUPP.I
  DataBrowserPostProcessDragUPP.I
  DataBrowserItemHelpContentUPP.I
  DataBrowserGetContextualMenuUPP.I
  DataBrowserSelectContextualMenuUPP.I
EndStructure

Define DataBrowserCallbacks.DataBrowserCallbacks
Define *DataBrowserItemNotificationUPP
Define i.I

Dim ColumnWidth.I(#NumColumns - 1)

Procedure DataBrowserItemNotificationCallback(Control.I, ItemID.I, ItemNotification.I)
  Shared ColumnWidth.I()

  Protected i.I
  Protected NewColumnWidth.I

  If ItemNotification = #kDataBrowserUserStateChanged
    For i = 0 To #NumColumns - 1
      NewColumnWidth = GetGadgetItemAttribute(0, 0, #PB_ListIcon_ColumnWidth, i)

      If NewColumnWidth <> ColumnWidth(i)
        Debug "Column width of column " + Str(i) + " changed from " + Str(ColumnWidth(i)) + " to " + Str(NewColumnWidth)
        ColumnWidth(i) = NewColumnWidth
      EndIf
    Next i
  EndIf
EndProcedure

OpenWindow(0, 100, 100, 420, 90, "ListIcon Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ListIconGadget(0, 5, 5, 410, 80, "Name", 110, #PB_ListIcon_FullRowSelect|#PB_ListIcon_AlwaysShowSelection)
AddGadgetColumn(0, 1, "Address", 279)
AddGadgetItem(0, -1, "Harry Rannit"+Chr(10)+"12 Parliament Way, Battle Street, By the Bay")
AddGadgetItem(0, -1, "Ginger Brokeit"+Chr(10)+"130 PureBasic Road, BigTown, CodeCity")

For i = 0 To #NumColumns - 1
  ColumnWidth(i) = GetGadgetItemAttribute(0, 0, #PB_ListIcon_ColumnWidth, i)
Next i

DataBrowserCallbacks\version = #kDataBrowserLatestCallbacks

If InitDataBrowserCallbacks(@DataBrowserCallbacks) = #noErr
  *DataBrowserItemNotificationUPP = NewDataBrowserItemNotificationUPP(@DataBrowserItemNotificationCallback())
  DataBrowserCallbacks\DataBrowserItemNotificationUPP = *DataBrowserItemNotificationUPP

  While WindowEvent()
  Wend

  If SetDataBrowserCallbacks(GadgetID(0), @DataBrowserCallbacks) = #noErr
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break
      EndSelect
    ForEver

    DisposeDataBrowserItemNotificationUPP(*DataBrowserItemNotificationUPP)
  EndIf
EndIf
Post Reply