More GadgetItemData

Share your advanced PureBasic knowledge/code with the community.
wayne-c
Enthusiast
Enthusiast
Posts: 337
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

More GadgetItemData

Post 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
As you walk on by, Will you call my name? Or will you walk away?
dige
Addict
Addict
Posts: 1412
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

Very interesting!
@PB-Team: would be nice to know, if its a useable way, isnt it?
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post 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).
Dare2 cut down to size
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

I like it. :D

Many thanks for sharing it with us.

cheers
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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.
quidquid Latine dictum sit altum videtur
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

Perfect timing for a small project I'm working on right now. Thanks!
Post Reply