SetWindowData(#Window,Value) And GetWindowData(#Window)
Posted: Wed Feb 08, 2006 10:47 am
As those functions exists for gadgets, it may be useful for window ?
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
Global NewMap windowData()
windowData(Str(#window)) = value ;store
Debug windowData(Str(#window)) ;read
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)
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
Code: Select all
SetWindowLongPtr_(WindowID(#Window), #GWLP_USERDATA, Value)
Value = GetWindowLongPtr_(WindowID(#Window), #GWLP_USERDATA)
Code: Select all
SetProp_(WindowID(#Window), "MyValue", Value)
GetProp_(WindowID(#Window), "MyValue")
RemoveProp_(WindowID(#Window), "MyValue")
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
This, I know, but I need cross-platform variant.Bisonte wrote:On Windows you can use for a single value
The windows are in different modules, and use of map, will create additional difficulties.Fred wrote:What's wrong with the map method ?
Put it in a helper module, so you can use it in all other modules? (base by cxAlex)User_Russian wrote:The windows are in different modules, and use of map, will create additional difficulties.Fred wrote:What's wrong with the map method ?
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