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

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

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

Post by Flype »

As those functions exists for gadgets, it may be useful for window ?
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User_Russian
Addict
Addict
Posts: 1516
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

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

Post by User_Russian »

+100000000
Why have not yet been implemented? What is the difficulty?
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

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

Post by Joakim Christiansen »

Would be nice indeed!
Meanwhile:

Code: Select all

Global NewMap windowData()
windowData(Str(#window)) = value ;store
Debug windowData(Str(#window))  ;read
I like logic, hence I dislike humans but love computers.
cxAlex
User
User
Posts: 88
Joined: Fri Oct 24, 2008 11:29 pm
Location: Austria
Contact:

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

Post 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)
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

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

Post 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.
User_Russian
Addict
Addict
Posts: 1516
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

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

Post 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
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

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

Post 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.
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.)
User_Russian
Addict
Addict
Posts: 1516
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

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

Post by User_Russian »

Bisonte wrote:On Windows you can use for a single value
This, I know, but I need cross-platform variant.
Fred
Administrator
Administrator
Posts: 18153
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post by Fred »

What's wrong with the map method ?
User_Russian
Addict
Addict
Posts: 1516
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

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

Post 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.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

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

Post 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
User avatar
NicTheQuick
Addict
Addict
Posts: 1503
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

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

Post by NicTheQuick »

I still would prefer a native method for this.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Post Reply