Page 1 of 1

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

Posted: Sun May 12, 2013 2:40 am
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:

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

Posted: Sun May 12, 2013 3:55 pm
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