Named Properties for Gadgets like SetProp/GetProp (All OS)

Share your advanced PureBasic knowledge/code with the community.
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Named Properties for Gadgets like SetProp/GetProp (All OS)

Post by mk-soft »

Hello, had too much time.

With windows there is the API SetProg and GetProp.
But this works better for all OS also with maps very well and need only some macros.

Update v1.03
- Change FreeProperties(...) - #PB_All deleted all properties of not exists gadgets
- Added ClearProperties() - Clear all properties

Code: Select all

;-TOP

; Named Properties for Gadgets by mk-soft, v1.03, 04.07.2020

Structure udtPropertyValue
  VarType.i
  StructureUnion
    iVal.i
    fltVal.f
    dblVal.d
  EndStructureUnion
  sVal.s
EndStructure

Structure udtProperty
  Map Value.udtPropertyValue()
EndStructure

Global NewMap MapProperty.udtProperty()

Macro SetPropertyInteger(_Gadget_, _Name_, _Value_)
  MapProperty(Str(_Gadget_))\Value(_Name_)\VarType = #PB_Integer
  MapProperty()\Value()\iVal = _Value_
EndMacro

Macro SetPropertyFloat(_Gadget_, _Name_, _Value_)
  MapProperty(Str(_Gadget_))\Value(_Name_)\VarType = #PB_Float
  MapProperty()\Value()\fltVal = _Value_
EndMacro

Macro SetPropertyDouble(_Gadget_, _Name_, _Value_)
  MapProperty(Str(_Gadget_))\Value(_Name_)\VarType = #PB_Double
  MapProperty()\Value()\dblVal = _Value_
EndMacro

Macro SetPropertyString(_Gadget_, _Name_, _Value_)
  MapProperty(Str(_Gadget_))\Value(_Name_)\VarType = #PB_String
  MapProperty()\Value()\sVal = _Value_
EndMacro

; ----

Macro GetPropertyInteger(_Gadget_, _Name_)
  MapProperty(Str(_Gadget_))\Value(_Name_)\iVal
EndMacro

Macro GetPropertyFloat(_Gadget_, _Name_)
  MapProperty(Str(_Gadget_))\Value(_Name_)\fltVal
EndMacro

Macro GetPropertyDouble(_Gadget_, _Name_)
  MapProperty(Str(_Gadget_))\Value(_Name_)\dblVal
EndMacro

Macro GetPropertyString(_Gadget_, _Name_)
  MapProperty(Str(_Gadget_))\Value(_Name_)\sVal
EndMacro

; ----

Macro GetPropertyVarType(_Gadget_, _Name_)
  MapProperty(Str(_Gadget_))\Value(_Name_)\VarType
EndMacro

Macro Properties(_Gadget_)
  MapProperty(Str(_Gadget_))
EndMacro

Macro FreeProperties(_Gadget_) ; <- #PB_All deleted all properties of not exists gadgets
  If _Gadget_ = #PB_All
    ForEach MapProperty()
      If Not IsGadget(Val(MapKey(MapProperty())))
        DeleteMapElement(MapProperty())
      EndIf
    Next
  Else
    DeleteMapElement(MapProperty(), Str(_Gadget_))
  EndIf
EndMacro

Macro ClearProperties() ; Clear all properties
  ClearMap(MapProperty())
EndMacro

; ****

CompilerIf #PB_Compiler_IsMainFile
  Debug "Set Properties"
  SetPropertyString(1, "Text", "Hello World")  
  SetPropertyInteger(1, "Year", 2020)  
  Debug "----"
  
  Debug "Get Properties"
  Debug "Text = " + GetPropertyString(1, "Text")
  Debug "Year = " + GetPropertyInteger(1, "Year")
  Debug "----"
  
  Debug "VarType of Year"
  r1 = GetPropertyVarType(1, "Year")
  Select r1
    Case 0 : Debug "Property not exists"
    Case #PB_Integer : Debug "- Integer"
    Case #PB_Float   : Debug "- Float"
    Case #PB_Double  : Debug "- Double"
    Case #PB_String  : Debug "- String"
  EndSelect
  Debug "----"
  
  Debug "VarType of Text"
  r1 = GetPropertyVarType(1, "Text")
  Select r1
    Case 0 : Debug "Property not exists"
    Case #PB_Integer : Debug "- Integer"
    Case #PB_Float   : Debug "- Float"
    Case #PB_Double  : Debug "- Double"
    Case #PB_String  : Debug "- String"
  EndSelect
  Debug "----"
  
  Debug "VarType of XYZ"
  r1 = GetPropertyVarType(1, "XYZ")
  Select r1
    Case 0 : Debug "Property not exists"
    Case #PB_Integer : Debug "- Integer"
    Case #PB_Float   : Debug "- Float"
    Case #PB_Double  : Debug "- Double"
    Case #PB_String  : Debug "- String"
  EndSelect
  Debug "----"
  
  ; FreeProperties(1)
  
  Debug "List of Properties"
  ForEach Properties(1)\Value()
    Debug MapKey(Properties(1)\Value())
  Next
  Debug "---"
  
CompilerEndIf
Last edited by mk-soft on Mon Jul 06, 2020 6:54 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Denis
Enthusiast
Enthusiast
Posts: 704
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Re: Named Properties for Gadgets like SetProp/GetProp (All O

Post by Denis »

Hi mk-soft,

Very interesting and clean code.

I will use it ASAP.
Thanks
A+
Denis
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Named Properties for Gadgets like SetProp/GetProp (All O

Post by Kwai chang caine »

Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
ebs
Enthusiast
Enthusiast
Posts: 530
Joined: Fri Apr 25, 2003 11:08 pm

Re: Named Properties for Gadgets like SetProp/GetProp (All O

Post by ebs »

Hi mk-soft,

That is really clever! (and useful)

Can I make one suggestion? Change the FreeProperties macro to this:

Code: Select all

#ALL = -1
Macro FreeProperties(_Gadget_)
  If _Gadget_ = #ALL
    ClearMap(MapProperty())
  Else
    DeleteMapElement(MapProperty(), Str(_Gadget_))
  EndIf
EndMacro
That way, if you want to clear all properties:

Code: Select all

FreeProperties(#ALL)
Regards,
Eric
Joris
Addict
Addict
Posts: 885
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: Named Properties for Gadgets like SetProp/GetProp (All O

Post by Joris »

I don't understand what this is ment for. Do you have a example that shows something more clear ?
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Named Properties for Gadgets like SetProp/GetProp (All O

Post by mk-soft »

Maybe delete only no exists gadgets

Code: Select all

Macro FreeProperties(_Gadget_) ; <- #PB_All deleted all not exists Gadgets
  If _Gadget_ = #PB_All
    ForEach MapProperty()
      If Not IsGadget(Val(MapKey(MapProperty())))
        DeleteMapElement(MapProperty())
      EndIf
    Next
  Else
    DeleteMapElement(MapProperty(), Str(_Gadget_))
  EndIf
EndMacro
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Named Properties for Gadgets like SetProp/GetProp (All O

Post by Josh »

mk-soft wrote:Maybe delete only no exists gadgets
Why don't store the data in a structure with Set-/GetWindowData, Set-/GetGadgetData?
sorry for my bad english
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Named Properties for Gadgets like SetProp/GetProp (All O

Post by mk-soft »

Joris wrote:I don't understand what this is ment for. Do you have a example that shows something more clear ?
Stupid example ...

Code: Select all

;-TOP

IncludeFile "NamedProperties.pb"

Enumeration Gadgets
  #String
  #ButtonRestore
EndEnumeration

Procedure Main()
  If OpenWindow(0, 100, 100, 400, 300, "Window", #PB_Window_SystemMenu)
    StringGadget(#String, 10, 10, 380, 25, "First Text")
    ButtonGadget(#ButtonRestore, 10, 50, 120, 30, "Restore")
    
    SetActiveGadget(#String)
    
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_Menu
          Select EventMenu()
              CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
              Case #PB_Menu_Quit
                Break
              CompilerEndIf
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #String
              Select EventType()
                Case #PB_EventType_Focus
                  SetPropertyString(#String, "FocusText", GetGadgetText(#String))
                  SetPropertyInteger(#String, "FocusDate", Date())
                  
              EndSelect
              
            Case #ButtonRestore
              SetGadgetText(#String, GetPropertyString(#String, "FocusText"))
              Debug "Restore from Date " + FormatDate("%mm/%dd/%yyyy %hh:%ii:%ss", GetPropertyInteger(#String, "FocusDate"))
              
          EndSelect
          
        Case #PB_Event_CloseWindow
          Break
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Named Properties for Gadgets like SetProp/GetProp (All O

Post by mk-soft »

Josh wrote:
mk-soft wrote:Maybe delete only no exists gadgets
Why don't store the data in a structure with Set-/GetWindowData, Set-/GetGadgetData?
1.
First you must get the pointer to the structured memory and create the structured memory when you use it for the first time.
2.
When closing windows or removing gadgets, you must ensure that the memory on the structure is freed. Otherwise you will get a memory leak, because then you will not be able to access the old data.
3.
Not for PB_Any
The data for the gadget is still available after the rebuild, or you can assign the data before.

[Edit]
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Named Properties for Gadgets like SetProp/GetProp (All O

Post by mk-soft »

Update v1.03
- Change FreeProperties(...) - #PB_All deleted all properties of not exists gadgets
- Added ClearProperties() - Clear all properties

It would be great if Fred would allow us to use the internal NumericMap functions.
:wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Named Properties for Gadgets like SetProp/GetProp (All O

Post by Josh »

mk-soft wrote: 1.
First you must get the pointer to the structured memory and create the structured memory when you use it for the first time.
2.
When closing windows or removing gadgets, you must ensure that the memory on the structure is freed. Otherwise you will get a memory leak, because then you will not be able to access the old data.
3.
Not for PB_Any
The data for the gadget is still available after the rebuild, or you can assign the data before.
  1. One line of code. In most cases where I have to give data to a gadget, I have already stored it in a structure anyway.
  2. Yes, I have to free the allocated memory, that's what a programmer does. But this is no different with your system, otherwise you will have a problem if you create a new gadget with the same gadget number.
  3. I don't see an advantage here, but there is a danger. See point 2.
It doesn't matter, anyone can do it however they want. But I prefer to use the things that are natively available for this.
sorry for my bad english
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Named Properties for Gadgets like SetProp/GetProp (All O

Post by mk-soft »

This is only about the alternative Windows API function SetProp and GetPro for all OS.

It should only show that it is very easy to solve with maps and few macros.

Everybody can do it the way he wants ...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Named Properties for Gadgets like SetProp/GetProp (All OS)

Post by Rinzwind »

With windows there is the API SetProg and GetProp.
Mind you, that Windows API is questionably slow, way, way slower than using a PB map for similar functionality.
Post Reply