Page 1 of 1

More GadgetItemData

Posted: Sun Dec 03, 2006 4:38 pm
by wayne-c
Hi all,

I needed a way to store additional data to each item in a ListIconGadget, but I wanted to avoid a separate LinkedList or any "hidden" columns. So here's what I did.

I don't know if this code is correct (mem leaks or so) but for the moment it seems to run fine here.

Probably it is helpful to somebody.

Code: Select all

Structure LISTICON_ITEMDATA_EX
  DateModified.l
  Extension.s
  DummyVariable1.l
  DummyVariable2.l
EndStructure

Procedure RemoveGadgetItemEx(Gadget, Position)
  Protected lParam.l= 0
  lParam= GetGadgetItemData(Gadget, Position)
  If lParam
    FreeMemory(lParam)
  EndIf
  RemoveGadgetItem(Gadget, Position)
EndProcedure

Procedure ClearGadgetItemListEx(Gadget)
  Protected Position.l= 0
  For Position= CountGadgetItems(Gadget) - 1 To 0 Step -1
    RemoveGadgetItemEx(Gadget, Position)
  Next
EndProcedure

Procedure AddGadgetItemEx(Text$, DateModified, Extension$, Dummy1, Dummy2)
  Protected n= CountGadgetItems(1)
  AddGadgetItem(1, n, Text$)
  hMem= AllocateMemory(SizeOf(LISTICON_ITEMDATA_EX)) ; Memory is freed when the item is removed
    *lii.LISTICON_ITEMDATA_EX= hMem
    *lii\DateModified= DateModified
    *lii\Extension= Extension$
    *lii\DummyVariable1= Dummy1
    *lii\DummyVariable2= Dummy2
  SetGadgetItemData(1, n, hMem)
EndProcedure

If OpenWindow(0, 0, 0, 300, 300, "MoreGadgetItemData", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If CreateGadgetList(WindowID(0))
    StringGadget(2, 10, 10, 135, 20, "", #PB_String_ReadOnly)
    StringGadget(3, 10, 35, 135, 20, "", #PB_String_ReadOnly)
    StringGadget(4, 155, 10, 135, 20, "", #PB_String_ReadOnly)
    StringGadget(5, 155, 35, 135, 20, "", #PB_String_ReadOnly)
    ListIconGadget(1, 10, 70, 280, 220, "Name", 200)
  EndIf
  
  AddGadgetItemEx("Hello World", Date() - 2800000, "Word Document", 123, 45)
  AddGadgetItemEx("Hello Moon", Date(), "Excel Workbook", 678, 90)
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            If EventType() = #PB_EventType_Change
              idx.l= GetGadgetState(1)
              If idx > -1
                lParam= GetGadgetItemData(1, idx)
                If lParam
                  *lii.LISTICON_ITEMDATA_EX= lParam
                  SetGadgetText(2, *lii\Extension)
                  SetGadgetText(3, FormatDate("%yyyy.%mm.%dd %hh:%ii:%ss", *lii\DateModified))
                  SetGadgetText(4, Str(*lii\DummyVariable1))
                  SetGadgetText(5, Str(*lii\DummyVariable2))
                EndIf
              EndIf
            EndIf
        EndSelect
    EndSelect
  ForEver
  
  ClearGadgetItemListEx(1)
  
  CloseWindow(0)
EndIf
End

Posted: Mon Dec 04, 2006 9:03 am
by dige
Very interesting!
@PB-Team: would be nice to know, if its a useable way, isnt it?

Posted: Mon Dec 04, 2006 11:46 am
by Dare
dige wrote:Very interesting!
@PB-Team: would be nice to know, if its a useable way, isnt it?
I'm pretty sure it is.

(I hope it is, I use the same approach in two apps. Looks like a better organised approach by wayne-c though).

Posted: Mon Dec 04, 2006 3:04 pm
by rsts
I like it. :D

Many thanks for sharing it with us.

cheers

Posted: Mon Dec 04, 2006 4:07 pm
by freak
dige wrote:Very interesting!
@PB-Team: would be nice to know, if its a useable way, isnt it?
Of course. This is what the command was meant for.
You can do the same with SetGadgetData(). Just make sure your
structure is freed when the gadgets are freed.

Posted: Mon Dec 04, 2006 7:43 pm
by utopiomania
Perfect timing for a small project I'm working on right now. Thanks!