Page 1 of 1
Help with gadget extra storage space.
Posted: Tue Feb 12, 2013 11:40 pm
by Pud
Isn’t it infuriating when you have x,000s of lines of working code but can’t get a few lines to work at all, no matter how many times you look at them!
I just need to store some information with a canvas gadget, a bit more then is possible with SetGadgetData.
The following is basically what I am trying to do.
SetClassLong_(GadgetID(canvisgadget),#GCL_CBWNDEXTRA,4)
SetWindowLong_(GadgetID(canvisgadget),0,99)
Debug GetWindowLong_(GadgetID(canvisgadget),0)
Does a canvas gadget use this storage space for it’s own purpose?
Thanks in anticipation.
Re: Help with gadget extra storage space.
Posted: Wed Feb 13, 2013 12:05 am
by ts-soft
You can store many of infos in GadgetData
Code: Select all
Structure mycanvasdata
a.i
b.i
c.s
d.d
; ...
EndStructure
Global *cdata.mycanvasdata = AllocateMemory(SizeOf(mycanvasdata))
*cdata\a = 100
*cdata\b = 200
*cdata\c = "Hello World"
*cdata\d = #PI
If OpenWindow(0, 0, 0, 220, 220, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(0, 10, 10, 200, 160)
SetGadgetData(0, *cdata)
ButtonGadget(1, 65, 180, 100, 20, "show canvasdata")
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Gadget And EventGadget() = 0
If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
If StartDrawing(CanvasOutput(0))
x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
Circle(x, y, 10, RGB(Random(255), Random(255), Random(255)))
StopDrawing()
EndIf
EndIf
EndIf
If Event = #PB_Event_Gadget And EventGadget() = 1
*a.mycanvasdata = GetGadgetData(0)
Debug *a\a
Debug *a\b
Debug *a\c
Debug *a\d
EndIf
Until Event = #PB_Event_CloseWindow
EndIf
The only limit is the memory of your computer.
Greetings - Thomas
Re: Help with gadget extra storage space.
Posted: Wed Feb 13, 2013 12:07 am
by IdeasVacuum
I don't know the answer to your question, but you should use SetWindowLongPtr_, GetWindowLongPtr_, SetClassLongPtr_
Re: Help with gadget extra storage space.
Posted: Wed Feb 13, 2013 12:23 am
by Bisonte
or you can use
Code: Select all
; Set Data
SetProp_(GadgetID(GadgetNo), Keyname.s, Value)
;Get Data
Value = GetProp_(GadgetID(GadgetNo), KeyName.s)
So you can use different values with different keynames...
Code: Select all
SetProp_(GadgetID(#Canvas), "MouseX", Value)
Re: Help with gadget extra storage space.
Posted: Wed Feb 13, 2013 12:28 am
by Pud
Thanks for the replies.
The canvas gadgets (and there could be many) are allocated dynamically so I don’t want to have to maintain a separate array to store the associated data. Storing the data with the gadget is the neatest solution, but only if I can get it to work!
Any other ideas?
Regards.
Re: Help with gadget extra storage space.
Posted: Wed Feb 13, 2013 12:34 am
by Pud
Bisonte wrote:or you can use
Code: Select all
; Set Data
SetProp_(GadgetID(GadgetNo), Keyname.s, Value)
;Get Data
Value = GetProp_(GadgetID(GadgetNo), KeyName.s)
So you can use different values with different keynames...
Code: Select all
SetProp_(GadgetID(#Canvas), "MouseX", Value)
Ya that worked!!!!
Cheers!!
Re: Help with gadget extra storage space.
Posted: Wed Feb 13, 2013 1:02 am
by ts-soft
But you have to RemoveProp_() before FreeGadget!
Better simple use SetGadgetData with pointer to a structured var Or use a Map
Re: Help with gadget extra storage space.
Posted: Wed Feb 13, 2013 1:21 am
by skywalk
If you already create your gadgets dynamically, just link your data to any gadgetid's you like and don't bother with setproperties or setgadgetdata() stuff.

Re: Help with gadget extra storage space.
Posted: Wed Feb 13, 2013 8:30 am
by Fred
You should really use SetGadgetData() here.
Re: Help with gadget extra storage space.
Posted: Wed Feb 13, 2013 3:50 pm
by netmaestro
SetGadgetData() with a structured pointer to allocated memory, I fully recommend the approach posted by ts-soft. SetProp_() / RemoveProp_() is best handled in a callback and there's just no need for this. Also, maybe you don't plan to make it crossplatform at all right now, but you might change your mind next year and then all the Prop stuff would have to be completely rewritten. The ts-soft code is good to go.