Page 1 of 1

SetWindowData(#Window,Value) And GetWindowData(#Window)

Posted: Wed Feb 08, 2006 10:47 am
by Flype
As those functions exists for gadgets, it may be useful for window ?

Re: SetWindowData(#Window,Value) And GetWindowData(#Window)

Posted: Wed Mar 13, 2013 10:44 am
by User_Russian
+100000000
Why have not yet been implemented? What is the difficulty?

Re: SetWindowData(#Window,Value) And GetWindowData(#Window)

Posted: Wed Mar 13, 2013 11:14 pm
by Joakim Christiansen
Would be nice indeed!
Meanwhile:

Code: Select all

Global NewMap windowData()
windowData(Str(#window)) = value ;store
Debug windowData(Str(#window))  ;read

Re: SetWindowData(#Window,Value) And GetWindowData(#Window)

Posted: Thu Mar 14, 2013 12:51 am
by cxAlex
Or this way, you can include it into your Ssurce and forget it, If it's implemented natively then the native procedures are used.

Code: Select all

CompilerIf Not Defined(GetWindowData, #PB_Function)
  Global NewMap __WindowData()

  Procedure GetWindowData(_Window)
    ProcedureReturn __WindowData(Str(_Window))
  EndProcedure

  Procedure SetWindowData(_Window, _Data)
    __WindowData(Str(_Window)) = (_Data)
  EndProcedure
  
CompilerEndIf

; Test

SetWindowData(1, 20)
Debug GetWindowData(1)

Re: SetWindowData(#Window,Value) And GetWindowData(#Window)

Posted: Mon Jul 22, 2013 3:57 pm
by Justin
Any news on this? It will be implemented in the new version? It would be very useful to encapsulate code.
No api it has to be CP.

Re: SetWindowData(#Window,Value) And GetWindowData(#Window)

Posted: Tue Sep 10, 2013 1:45 pm
by User_Russian
Functions would be very useful.
An example of their use.

Code: Select all

Structure Gadgets
  Button_1.i
  Button_2.i
EndStructure

Procedure OpenWin(ID, x=0, y=0, w=100, h=100, t.s="", f=0)
  Protected *Gadget.Gadgets
  
  *Gadget=AllocateMemory(SizeOf(Gadgets))
  If *Gadget
    If OpenWindow(ID, x, y, w, h, t, f)
      *Gadget\Button_1 = ButtonGadget(#PB_Any, 20, 10, 80, 24, "Button_1")
      *Gadget\Button_2 = ButtonGadget(#PB_Any, 20, 50, 80, 24, "Button_2")
      SetWindowData(ID, *Gadget)
    Else
      FreeMemory(*Gadget)
    EndIf
  EndIf
  
EndProcedure

Procedure EventWin(Event, Window)
  Protected *Gadget.Gadgets
  
  If Event = #PB_Event_Gadget
    
    *Gadget = GetWindowData(Window)
    If *Gadget
      Select EventGadget()
        Case *Gadget\Button_1
          MessageRequester("", "Button_1; WinID "+Window)
        Case *Gadget\Button_2
          MessageRequester("", "Button_2; WinID "+Window)
      EndSelect
    EndIf
    
  ElseIf Event = #PB_Event_CloseWindow
    *Gadget = GetWindowData(Window)
    If *Gadget
      FreeMemory(*Gadget) : SetWindowData(0, 0)
    EndIf
    CloseWindow(Window)
  EndIf
  
EndProcedure

OpenWin(0)
OpenWin(1)
OpenWin(2)
OpenWin(3)

Repeat
  Event = WaitWindowEvent()
  Window= EventWindow()
  
  EventWin(Event, Window)
  
Until Event = #PB_Event_CloseWindow And Window=0

Re: SetWindowData(#Window,Value) And GetWindowData(#Window)

Posted: Tue Sep 10, 2013 2:09 pm
by Bisonte
On Windows you can use for a single value

Code: Select all

SetWindowLongPtr_(WindowID(#Window), #GWLP_USERDATA, Value)
Value = GetWindowLongPtr_(WindowID(#Window), #GWLP_USERDATA)
or for more than one

Code: Select all

SetProp_(WindowID(#Window), "MyValue", Value)
GetProp_(WindowID(#Window), "MyValue")
RemoveProp_(WindowID(#Window), "MyValue")
But thats only windows. A little cross solution for that, is a map to handle it like this

Code: Select all

DeclareModule Common_GUI
  
  ; --- Crossplatform Property Handling like Windows OS 
  ; --- SetProp_(), GetProp_(), RemoveProp_()
  
  Declare SetProp(ID, Property.s, Value)          ; Set a property to an ID
  Declare GetProp(ID, Property.s)                 ; Get a property from an ID
  Declare DelProp(ID, Property.s, Flag = #False)  ; If Flag = #True then Remove all properties of this object
  
EndDeclareModule
Module        Common_GUI
    
  Structure struct_CG_Gadget_Properties
    Map Property.i()
  EndStructure
  
  Global NewMap CG_Gadget_Properties.struct_CG_Gadget_Properties()
  
  Procedure SetProp(ID, Property.s, Value)
    
    Protected Result = #False
    
    CG_Gadget_Properties(Str(ID))\Property(Property) = Value
    Result = #True  
        
    ProcedureReturn Result
    
  EndProcedure
  Procedure GetProp(ID, Property.s)
    
    Protected Value = #Null
    
    If FindMapElement(CG_Gadget_Properties(), Str(ID))
      If FindMapElement(CG_Gadget_Properties(Str(ID))\Property(), Property)
        Value = CG_Gadget_Properties(Str(ID))\Property(Property)
      EndIf
    EndIf
    
    ProcedureReturn Value
    
  EndProcedure
  Procedure DelProp(ID, Property.s, Flag = #False)
    
    Protected Result = #False
    
    If FindMapElement(CG_Gadget_Properties(), Str(ID))
      
      If Flag = #False
        If FindMapElement(CG_Gadget_Properties(Str(ID))\Property(), Property)
          DeleteMapElement(CG_Gadget_Properties(Str(ID))\Property(), Property)
          Result = #True  
        EndIf
      Else
        DeleteMapElement(CG_Gadget_Properties(), Str(ID))
        Result = #True  
      EndIf    
      
    EndIf
    
    ProcedureReturn Result
    
  EndProcedure
  
EndModule
UseModule     Common_GUI
But a native solution is the best... so I agree this wish.

Re: SetWindowData(#Window,Value) And GetWindowData(#Window)

Posted: Tue Sep 10, 2013 2:19 pm
by User_Russian
Bisonte wrote:On Windows you can use for a single value
This, I know, but I need cross-platform variant.

Re: SetWindowData(#Window,Value) And GetWindowData(#Window)

Posted: Tue Sep 10, 2013 5:29 pm
by Fred
What's wrong with the map method ?

Re: SetWindowData(#Window,Value) And GetWindowData(#Window)

Posted: Tue Sep 10, 2013 6:07 pm
by User_Russian
Fred wrote:What's wrong with the map method ?
The windows are in different modules, and use of map, will create additional difficulties.

Re: SetWindowData(#Window,Value) And GetWindowData(#Window)

Posted: Tue Sep 10, 2013 7:10 pm
by Danilo
User_Russian wrote:
Fred wrote:What's wrong with the map method ?
The windows are in different modules, and use of map, will create additional difficulties.
Put it in a helper module, so you can use it in all other modules? (base by cxAlex)

Code: Select all

DeclareModule WindowHelper
    EnableExplicit
    Declare GetWindowData(window, index=0)
    Declare SetWindowData(window, value, index=0)
EndDeclareModule

Module WindowHelper
    
    Global NewMap windowData.i()
    
    Procedure GetWindowData(window, index=0)
        ProcedureReturn windowData(Str(window)+":"+Str(index))
    EndProcedure
    
    Procedure SetWindowData(window, value, index=0)
        windowData(Str(window)+":"+Str(index)) = value
    EndProcedure
    
EndModule



WindowHelper::SetWindowData(1, 12)
WindowHelper::SetWindowData(2, 30)

Debug WindowHelper::GetWindowData(1)
Debug WindowHelper::GetWindowData(2)

For index = 1 To 10
    WindowHelper::SetWindowData(1, index+100, index)
Next

For index = 1 To 10
    Debug WindowHelper::GetWindowData(1, index)
Next

Re: SetWindowData(#Window,Value) And GetWindowData(#Window)

Posted: Sun Jan 26, 2014 8:08 pm
by NicTheQuick
I still would prefer a native method for this.