Cross-platform Properties 1.1(Surrogate for SetGadgetData())

Share your advanced PureBasic knowledge/code with the community.
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: Cross-platform Properties (Surrogate for SetGadgetData()

Post by said »

Interesting! Thanks for sharing, however i would suggest:

1. Since 'Name' is user-input, better secure it with either UCase/LCase, otherwise

Code: Select all

SetProp(#PB_GadgetType_Canvas, 0, "MyData", 0)
and 
SetProp(#PB_GadgetType_Canvas, 0, "myData", 0)

will have different entries in the map!
2. for simplicity (as an option) you could combine the 3 maps in one and use one key that's made of the 3 parts:
Type+ID+Ucase(Name)

Thanks again! I like the idea as this will release GadgetData unique value! I am going to use it :lol:
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: Cross-platform Properties (Surrogate for SetGadgetData()

Post by Bisonte »

In windows all objects (windows,gadgets,statusbar,toolbar) , are windows (or the os handle them like this),
so their os-id's are unique.
I don't know if linux and macos have the same behaviour like windows, but if so, this little piece of code make sense ;)
The syntax is like the WinAPI functions. And FreeMap() is since PB4.51, so a little check is integrated...

Code: Select all

CompilerIf #PB_Compiler_Version => 451
  Procedure InitProps(UPPERCASE = #False) ; < for you, said ;)
    Global NewMap _CrossPlatformProperties_()
    _CrossPlatformProperties_("UPPERCASE") = UPPERCASE
  EndProcedure
  Procedure SetProp(hWnd, Property$, Value)
    
    Protected Key.s, Result = #False
    
    If hWnd
      Key = Str(hWnd) + "_" + Property$
      If _CrossPlatformProperties_("UPPERCASE")
        Key = UCase(Key)
      EndIf
      _CrossPlatformProperties_(Key) = Value
      Result = #True
    EndIf
    
    ProcedureReturn Result
    
  EndProcedure
  Procedure GetProp(hWnd, Property$)
    
    Protected Key.s, Result = #False
    
    If hWnd
      Key = Str(hWnd) + "_" + Property$
      If _CrossPlatformProperties_("UPPERCASE")
        Key = UCase(Key)
      EndIf
      If FindMapElement(_CrossPlatformProperties_(), Key) 
        Result = _CrossPlatformProperties_(Key)
      EndIf
    EndIf
    
    ProcedureReturn Result  
    
  EndProcedure
  Procedure RemoveProp(hWnd, Property$)
    
    Protected Key.s, Result = #False
    
    If hWnd
      Key = Str(hWnd) + "_" + Property$
      If _CrossPlatformProperties_("UPPERCASE")
        Key = UCase(Key)
      EndIf
      If FindMapElement(_CrossPlatformProperties_(), Key) 
        DeleteMapElement(_CrossPlatformProperties_(), Key)
        Result = #True
      EndIf
    EndIf
    
    ProcedureReturn Result  
    
  EndProcedure
  Procedure ClearAllProps()
    
    ClearMap(_CrossPlatformProperties_())
    FreeMap(_CrossPlatformProperties_())
    
  EndProcedure
CompilerEndIf
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
Post Reply