Page 1 of 1

Need some advice (array pointer in structure)

Posted: Thu Mar 18, 2010 1:31 pm
by LuaDev
Hi, im making a ComboBox plugin for another application and i want to add some advanced options but iv hit a bit of a wall

i using a ownerdrawn api combo, iv added icons, text and back color for default and selection, now i want to add alternate icons and item colors for each item, but im struggling to figure out how to store the information

for the basic operation of the icons i have used the item data and for the colors i have attached props to the combos parent container, this all works fine

now i want to add colors and a alternate icon for the selection of each individual item, i have the basic setup in place but i see a huge memory problem

i am using the below structure, but i need the arrays to be dynamic, a combo with only a handful of items is going to use far to much memory (i dont know to much about this, correct me if i am wrong) and if 5/6 combos are added with only a few items in each, the memory over usage is some thing i just cant live with

Code: Select all

Structure COMBOINFO

  ; base info
  GadgetID.l
  X.l
  Y.l
  Width.l
  Height.l

  ; item text
  ItemDefText.s[100]
  ItemSelText.s[100]

  ; item icons
  ItemDefIcon.l[100]
  ItemSelIcon.l[100]
  
  ; item text color
  ItemDefTextColor.l[100]
  ItemSelTextColor.l[100]
  
  ; item back color
  ItemDefBackColor.l[100]
  ItemSelBackColor.l[100]

EndStructure

Global NewList Combo.COMBOIMNFO()
i have searched the forum, and found some complex solutions i could probably edit to suit my needs, but before i do, i would like some input from the PB gurus that could maybe point me to a simple solution

any advice or tips would be greatly appreciated, and thanks for your time

Re: Need some advice (array pointer in structure)

Posted: Thu Mar 18, 2010 2:34 pm
by srod
When using SetGadgetItemData() to store icon info for individual items, why not store a pointer to a structure which you create with AllocateMemory() (or store in an array) instead? This way you can store a whole heap of information for each item as shown with the following quick hack :

Code: Select all

Structure myComboItemInfo
  sometext$
  aNumber.i
EndStructure

If OpenWindow(0, 0, 0, 270, 140, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ComboBoxGadget(0, 10, 10, 250, 21)
  For a = 0 To 5
    AddGadgetItem(0, a,"ComboBox item " + Str(a))
    *ptr.myComboItemInfo = AllocateMemory(SizeOf(myComboItemInfo))
    *ptr\sometext$ = "Structure text for item " + Str(a) + "."
    SetGadgetItemData(0, a, *ptr)
  Next
  SetGadgetState(0, 0)
  ButtonGadget(1, 10, 100, 80, 20, "CLICK!")
  Repeat
    eventID = WaitWindowEvent()
    Select eventID
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            ;Retrieve the structure for the selected item.
              item = GetGadgetState(0)
              *ptr = GetGadgetItemData(0, item)
            ;Display the someText$ field.
              Debug *ptr\sometext$
        EndSelect 
    EndSelect
  Until eventID = #PB_Event_CloseWindow
EndIf
Whenever you delete an item, make sure you retrieve the item's structure pointer with GetGadgetItemData() and then use ClearStructure() to clear the memory of dynamic strings etc. before then using FreeMemory().

Re: Need some advice (array pointer in structure)

Posted: Thu Mar 18, 2010 3:24 pm
by LuaDev
that's the one, i was thinking im missing something obvious, i got the basic functionally in before i decided to add the extra stuff and automatically thought of a structure+array

well, i'll be busy rewriting this now, thanks for the help srod