Help with gadget extra storage space.

Windows specific forum
Pud
User
User
Posts: 29
Joined: Mon Dec 28, 2009 2:23 am

Help with gadget extra storage space.

Post 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.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Help with gadget extra storage space.

Post by ts-soft »

You can store many of infos in GadgetData :wink:

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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Help with gadget extra storage space.

Post by IdeasVacuum »

I don't know the answer to your question, but you should use SetWindowLongPtr_, GetWindowLongPtr_, SetClassLongPtr_
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Bisonte
Addict
Addict
Posts: 1320
Joined: Tue Oct 09, 2007 2:15 am

Re: Help with gadget extra storage space.

Post 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) 
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Pud
User
User
Posts: 29
Joined: Mon Dec 28, 2009 2:23 am

Re: Help with gadget extra storage space.

Post 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.
Pud
User
User
Posts: 29
Joined: Mon Dec 28, 2009 2:23 am

Re: Help with gadget extra storage space.

Post 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!!
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Help with gadget extra storage space.

Post by ts-soft »

But you have to RemoveProp_() before FreeGadget!
Better simple use SetGadgetData with pointer to a structured var Or use a Map
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
skywalk
Addict
Addict
Posts: 4242
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Help with gadget extra storage space.

Post 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. :idea:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Fred
Administrator
Administrator
Posts: 18360
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Help with gadget extra storage space.

Post by Fred »

You should really use SetGadgetData() here.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Help with gadget extra storage space.

Post 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.
BERESHEIT
Post Reply