[Module] GadgetDataExModule.pbi

Share your advanced PureBasic knowledge/code with the community.
User avatar
Thorsten1867
Addict
Addict
Posts: 1366
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

[Module] GadgetDataExModule.pbi

Post by Thorsten1867 »

Extended Gadget Data

Unlike SetGadgetData/GetGadgetData, GadgetDataEx allows you to save multiple data under a specific name (Key$) with the gadget. The different data can be retrieved by name.

Code: Select all

;/ === GadgetDataExModule.pbi [PB 5.6x] ===
;/ Extended Gadget Data
;/ October 2017 by Thorsten1867

  ; Gadget::SetData()        - Saves the specified value with the specified gadget (similar to: SetGadgetData())
  ; Gadget::GetData()        - Returns the data value previously stored for this gadget  (similar to: GetGadgetData())
  ; Gadget::SetIntegerData() - Stores an integer number under the name (Key$) for this gadget
  ; Gadget::GetIntegerData() - Returns the integer number previously stored under the name (Key$) for this gadget
  ; Gadget::SetFloatData()   - Stores a floating point number under the name (Key$) for this gadget (max. 10 digits)
  ; Gadget::GetFloatData()   - Returns the floating point number previously stored under the name (Key$) for this gadget
  ; Gadget::SetStringData()  - Stores a string under the name (Key$) for this gadget
  ; Gadget::GetStringData()  - Returns the string previously stored under the name (Key$) for this gadget
  ; Gadget::FreeData()       - Deletes the stored data for this gadget

DeclareModule Gadget
  Declare SetData(GadgetID.i, Value.i)
  Declare.i GetData(GadgetID.i)
  Declare SetIntegerData(GadgetID.i, Key$, Value.i)
  Declare.i GetIntegerData(GadgetID.i, Key$)
  Declare SetFloatData(GadgetID.i, Key$, Value.f)
  Declare.f GetFloatData(GadgetID.i, Key$)
  Declare SetStringData(GadgetID.i, Key$, Value$)
  Declare.s GetStringData(GadgetID.i, Key$)
  Declare FreeData(GadgetID.i)
EndDeclareModule

Module Gadget
  
  ;{ ----- Definitions -----
  Structure ExDataStructure
    Value.i
    Map String.s()
  EndStructure
  
  Global NewMap ExDG.ExDataStructure()
  ;} -----------------------
  
  Procedure SetData(GadgetID.i, Value.i)
    Define GId$ = Str(GadgetID)
    
    If FindMapElement(ExDG(), GId$)
      ExDG()\Value = Value
    Else
      AddMapElement(ExDG(), GId$)
      ExDG()\Value = Value
    EndIf
 
  EndProcedure
  
  Procedure.i GetData(GadgetID.i)
    ProcedureReturn ExDG(Str(GadgetID))\Value
  EndProcedure  
  
  
  Procedure SetIntegerData(GadgetID.i, Key$, Value.i)
    Define GId$ = Str(GadgetID)
    
    If FindMapElement(ExDG(GId$)\String(), Key$)
      ExDG(GId$)\String() = Str(Value)
    Else
      AddMapElement(ExDG(GId$)\String(), Key$)
      ExDG(GId$)\String() = Str(Value)
    EndIf
    
  EndProcedure
  
  Procedure.i GetIntegerData(GadgetID.i, Key$)
    ProcedureReturn Val(ExDG(Str(GadgetID))\String(Key$))
  EndProcedure 
  
  
  Procedure SetFloatData(GadgetID.i, Key$, Value.f)
    Define GId$ = Str(GadgetID)
    
    If FindMapElement(ExDG(GId$)\String(), Key$)
      ExDG(GId$)\String() = StrF(Value)
    Else
      AddMapElement(ExDG(GId$)\String(), Key$)
      ExDG(GId$)\String() = StrF(Value)
    EndIf
    
  EndProcedure
  
  Procedure.f GetFloatData(GadgetID.i, Key$)
    ProcedureReturn ValF(ExDG(Str(GadgetID))\String(Key$))
  EndProcedure 
  
  
  Procedure SetStringData(GadgetID.i, Key$, Value$)
    Define GId$ = Str(GadgetID)
    
    If FindMapElement(ExDG(GId$)\String(), Key$)
      ExDG(GId$)\String() = Value$
    Else
      AddMapElement(ExDG(GId$)\String(), Key$)
      ExDG(GId$)\String() = Value$
    EndIf
    
  EndProcedure
  
  Procedure.s GetStringData(GadgetID.i, Key$)
    ProcedureReturn ExDG(Str(GadgetID))\String(Key$)
  EndProcedure  
  
  
  Procedure FreeData(GadgetID.i)
    DeleteMapElement(ExDG(), Str(GadgetID))
  EndProcedure
  
EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  #Window = 0
  #ComboBox = 1
  #Button   = 2
  
  If OpenWindow(#Window, 200, 200, 120, 70, "Test Extended Gadget Data")
    ComboBoxGadget(#ComboBox, 10, 10, 110, 20)
    AddGadgetItem(#ComboBox, -1, "Apple")
    AddGadgetItem(#ComboBox, -1, "Pear")
    AddGadgetItem(#ComboBox, -1, "Banana")
    ButtonGadget(#Button, 35, 35, 60, 25, "Apply")
  EndIf
  SetGadgetState(#ComboBox, 0)
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #Button
            Debug  "Last: " + Gadget::GetStringData(#ComboBox, "Fruit")
            Debug "Current: " + GetGadgetText(#ComboBox)
            Gadget::SetData(#ComboBox, GetGadgetState(#ComboBox))
            Gadget::SetStringData(#ComboBox, "Fruit", GetGadgetText(#ComboBox))
          Case #ComboBox
            If EventType() = #PB_EventType_Change
              Debug "--> " + GetGadgetState(#ComboBox)
            EndIf
        EndSelect
    EndSelect
   
  ForEver
  
  Debug "============================="
  Debug "Last Combobox state: "+Gadget::GetData(#ComboBox)
  Debug "Last Selection: "+Gadget::GetStringData(#ComboBox, "Fruit")
  Debug "=============================" 
  
  Gadget::FreeData(#ComboBox)
  
CompilerEndIf
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5349
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: [Module] GadgetDataExModule.pbi

Post by Kwai chang caine »

Very nice idea !!!! only number is not always enough for enter DATA in gadget 8)
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
Thorsten1867
Addict
Addict
Posts: 1366
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Re: [Module] GadgetDataExModule.pbi

Post by Thorsten1867 »

Last edited by Thorsten1867 on Tue Nov 07, 2017 11:28 am, edited 1 time in total.
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2057
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: [Module] GadgetDataExModule.pbi

Post by Andre »

Nice and useful, thank you! :D

Btw. your last 'update' posting doesn't belong to this code, or? :wink:
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Post Reply