Simple PB Decimal SpinGadget()

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Simple PB Decimal SpinGadget()

Post by RASHAD »

Hi

Code: Select all

LoadFont(0,"Arial",12)
  
Procedure Dec_Spin(Gadget,dec)
    result.f = GetGadgetState(Gadget)/Pow(10,dec)
    SetGadgetText(Gadget, StrF(result,dec))
    Debug ValF(GetGadgetText(Gadget))
EndProcedure  
  
If OpenWindow(0, 0, 0, 265, 70, "SpinGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  SpinGadget(0, 10, 10, 75, 25, 0, 1000,#PB_Spin_Numeric)
  SetGadgetState (0, 0) : SetGadgetText(0, "0.0") 
  SetGadgetFont(0,FontID(0))
  SetGadgetColor(0,#PB_Gadget_FrontColor,#Red)
  
  SpinGadget(1, 95, 10, 75, 25, 0, 10000,#PB_Spin_Numeric)
  SetGadgetState (1, 250) : SetGadgetText(1, "2.50") 
  SetGadgetFont(1,FontID(0))
  SetGadgetColor(1,#PB_Gadget_FrontColor,#Green)
  
  SpinGadget(2, 180, 10, 75, 25, 0, 100000,#PB_Spin_Numeric)
  SetGadgetState (2, 50000) : SetGadgetText(2, "50.000") 
  SetGadgetFont(2,FontID(0))
  SetGadgetColor(2,#PB_Gadget_FrontColor,#Blue)  
 
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
             Quit = 1
             
    Case #PB_Event_Gadget
          Select EventGadget()
               Case 0
                      Dec_Spin(0,1)
                      
               Case 1                   
               	    Dec_Spin(1,2)
               				
               Case 2
                      Dec_Spin(2,3)
  				EndSelect
  EndSelect
Until Quit = 1
EndIf
Egypt my love
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Simple PB Decimal SpinGadget()

Post by davido »

@RASHAD

Very nice, thank you for sharing. :D
DE AA EB
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Simple PB Decimal SpinGadget()

Post by IdeasVacuum »

A thing of beauty 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Simple PB Decimal SpinGadget()

Post by Demivec »

very nice! :)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Simple PB Decimal SpinGadget()

Post by RASHAD »

Thanks
I was expecting no or bad reaction
Well,with the IT fellows you never know :mrgreen:
Egypt my love
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: Simple PB Decimal SpinGadget()

Post by said »

RASHAD wrote:Thanks
I was expecting no or bad reaction
Well,with the IT fellows you never know :mrgreen:
:) :)

RASHAD, you are a valuable contributor to this forum, you know that. Looks like sometimes we (PB users) tend to be quite lazy even in saying a nice/free thank you for sharing :mrgreen:
PB forums remain a nice place due to guys like you and many many others (from whom i keep learning every day)

Now, if you are really after some 'bad reaction' i can offer one :? :?

Code: Select all

The SpinGadget as written will behave as ReadOnly though the flag is not set
Here is variation that fixes this, hope you dont mind (and thanks for sharing):

Code: Select all

LoadFont(0,"Arial",12)
 
Procedure Dec_Spin(Gadget,dec)
    result.f = GetGadgetState(Gadget)/Pow(10,dec)
    SetGadgetText(Gadget, StrF(result,dec))
    Debug ValF(GetGadgetText(Gadget))
EndProcedure 
Procedure Dec_Spin_new(Gadget,dec,evTyp)
    If evTyp = #PB_EventType_Change
        SetGadgetState(Gadget, ValF(GetGadgetText(Gadget)) * Pow(10,dec))
    Else
        result.f = GetGadgetState(Gadget)/Pow(10,dec)
        SetGadgetText(Gadget, StrF(result,dec))
    EndIf
    Debug ValF(GetGadgetText(Gadget))
EndProcedure 
 
If OpenWindow(0, 0, 0, 265, 70, "SpinGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  SpinGadget(0, 10, 10, 75, 25, 0, 1000);,#PB_Spin_Numeric)
  SetGadgetState (0, 0) : SetGadgetText(0, "0.0")
  SetGadgetFont(0,FontID(0))
  SetGadgetColor(0,#PB_Gadget_FrontColor,#Red)
 
  SpinGadget(1, 95, 10, 75, 25, 0, 10000);,#PB_Spin_Numeric)
  SetGadgetState (1, 250) : SetGadgetText(1, "2.50")
  SetGadgetFont(1,FontID(0))
  SetGadgetColor(1,#PB_Gadget_FrontColor,#Green)
 
  SpinGadget(2, 180, 10, 75, 25, 0, 100000);,#PB_Spin_Numeric)
  SetGadgetState (2, 50000) : SetGadgetText(2, "50.000")
  SetGadgetFont(2,FontID(0))
  SetGadgetColor(2,#PB_Gadget_FrontColor,#Blue) 
 
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
             Quit = 1
             
    Case #PB_Event_Gadget
          Select EventGadget()
               Case 0
                      ;Dec_Spin(0,1)
                      Dec_Spin_new(0,1,EventType())
               Case 1                   
                      ;Dec_Spin(1,2)
                      Dec_Spin_new(1,2,EventType())
               Case 2
                      ;Dec_Spin(2,3)
                      Dec_Spin_new(2,3,EventType())
              EndSelect
  EndSelect
Until Quit = 1
EndIf
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Simple PB Decimal SpinGadget()

Post by RASHAD »

Hi said
Your version is not a Decimal SpinGadget() :)
Egypt my love
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: Simple PB Decimal SpinGadget()

Post by said »

Rashad, that's your idea! I was pointing out a non expected behavior, You want me to do the whole thing, fine:

Code: Select all

LoadFont(0,"Arial",12)

Procedure.s keepOnlyDigit(wrd.s)
    Protected *c.character, ret.s
    
    *c = @wrd
    While *c\c
        If (*c\c < '0' Or *c\c > '9') And *c\c <> '.'
            *c\c = '0'
        EndIf
        ret + Chr(*c\c)
        *c + SizeOf(character)
    Wend
    ProcedureReturn ret
EndProcedure

Procedure Dec_Spin(Gadget,dec)
    result.f = GetGadgetState(Gadget)/Pow(10,dec)
    SetGadgetText(Gadget, StrF(result,dec))
    Debug ValF(GetGadgetText(Gadget))
EndProcedure 
Procedure Dec_Spin_new(Gadget,dec,evTyp)
    If evTyp = #PB_EventType_Change
        SetGadgetState(Gadget, ValF(keepOnlyDigit(GetGadgetText(Gadget))) * Pow(10,dec))
        result.f = GetGadgetState(Gadget)/Pow(10,dec)
        SetGadgetText(Gadget, StrF(result,dec))
    Else
        result.f = GetGadgetState(Gadget)/Pow(10,dec)
        SetGadgetText(Gadget, StrF(result,dec))
    EndIf
    Debug ValF(GetGadgetText(Gadget))
EndProcedure 
 
If OpenWindow(0, 0, 0, 265, 70, "SpinGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  SpinGadget(0, 10, 10, 75, 25, 0, 1000);,#PB_Spin_Numeric)
  SetGadgetState (0, 0) : SetGadgetText(0, "0.0")
  SetGadgetFont(0,FontID(0))
  SetGadgetColor(0,#PB_Gadget_FrontColor,#Red)
 
  SpinGadget(1, 95, 10, 75, 25, 0, 10000);,#PB_Spin_Numeric)
  SetGadgetState (1, 250) : SetGadgetText(1, "2.50")
  SetGadgetFont(1,FontID(0))
  SetGadgetColor(1,#PB_Gadget_FrontColor,#Green)
 
  SpinGadget(2, 180, 10, 75, 25, 0, 100000);,#PB_Spin_Numeric)
  SetGadgetState (2, 50000) : SetGadgetText(2, "50.000")
  SetGadgetFont(2,FontID(0))
  SetGadgetColor(2,#PB_Gadget_FrontColor,#Blue) 
 
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
             Quit = 1
             
    Case #PB_Event_Gadget
          Select EventGadget()
               Case 0
                      ;Dec_Spin(0,1)
                      Dec_Spin_new(0,1,EventType())
               Case 1                   
                      ;Dec_Spin(1,2)
                      Dec_Spin_new(1,2,EventType())
               Case 2
                      ;Dec_Spin(2,3)
                      Dec_Spin_new(2,3,EventType())
              EndSelect
  EndSelect
Until Quit = 1
EndIf
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Simple PB Decimal SpinGadget()

Post by RASHAD »

It is a simple you made it out of the range :P
Try to enter 6.34 for exam using your last code
PB 5.22 x86 win 8.1 x64
Just add #PB_Spin_ReadOnly
- Only digits are allowed
- No direct edit

Code: Select all

LoadFont(0,"Arial",12)
  
Procedure Dec_Spin(Gadget,dec)
    result.f = GetGadgetState(Gadget)/Pow(10,dec)
    SetGadgetText(Gadget, StrF(result,dec))
    Debug ValF(GetGadgetText(Gadget))
EndProcedure  
  
If OpenWindow(0, 0, 0, 265, 70, "SpinGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  SpinGadget(0, 10, 10, 75, 25, 0, 1000,#PB_Spin_Numeric|#PB_Spin_ReadOnly)
  SetGadgetState (0, 0) : SetGadgetText(0, "0.0") 
  SetGadgetFont(0,FontID(0))
  SetGadgetColor(0,#PB_Gadget_FrontColor,#Red)
  
  SpinGadget(1, 95, 10, 75, 25, 0, 10000,#PB_Spin_Numeric|#PB_Spin_ReadOnly)
  SetGadgetState (1, 250) : SetGadgetText(1, "2.50") 
  SetGadgetFont(1,FontID(0))
  SetGadgetColor(1,#PB_Gadget_FrontColor,#Green)
  
  SpinGadget(2, 180, 10, 75, 25, 0, 100000,#PB_Spin_Numeric|#PB_Spin_ReadOnly)
  SetGadgetState (2, 50000) : SetGadgetText(2, "50.000") 
  SetGadgetFont(2,FontID(0))
  SetGadgetColor(2,#PB_Gadget_FrontColor,#Blue)  
 
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
             Quit = 1
             
    Case #PB_Event_Gadget
          Select EventGadget()
               Case 0
                      Dec_Spin(0,1)
                      
               Case 1                   
               				Dec_Spin(1,2)
               				
               Case 2
                      Dec_Spin(2,3)
  				EndSelect
  EndSelect
Until Quit = 1
EndIf

Egypt my love
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Simple PB Decimal SpinGadget()

Post by Danilo »

Shows wrong values when I keep holding down the arrow buttons for automatic increment/decrement. Value 2.5 becomes 250.
I tested on Mac OS X after removing the WinAPI constants (#Red, #Green, #Blue).
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Simple PB Decimal SpinGadget()

Post by RASHAD »

Hi Danilo
It works fine with me PB 5.22 x86 - win 8.1 x64
Can not reproduce your criticism
Danilo you keep changing your post while I am posting mine :P
With MAC you drive do what it needs to be correct and post it for the forum members

Edit :See if next works fine with MAC

Code: Select all

Procedure Dec_Spin(Gadget,dec)
    result.f = GetGadgetState(Gadget)/Pow(10,dec)
    If result >= GetGadgetAttribute(Gadget, #PB_Spin_Minimum)/Pow(10,dec) And result <= GetGadgetAttribute(Gadget, #PB_Spin_Maximum)/Pow(10,dec)
       SetGadgetText(Gadget, StrF(result,dec))
    EndIf
    Debug ValF(GetGadgetText(Gadget))
EndProcedure

Egypt my love
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Simple PB Decimal SpinGadget()

Post by Danilo »

The problem on Mac OS X was the flag #PB_Spin_Numeric. It did not allow the Dot character within the gadget text.

This works on Mac OS X and also updates correctly while holding down the up/down arrows:

Code: Select all

LoadFont(0,"Arial",12)
  
Procedure Dec_Spin(Gadget,dec)
    result.f = GetGadgetState(Gadget)/Pow(10,dec)
    SetGadgetText(Gadget, StrF(result,dec))
EndProcedure

Procedure DecSpinEvents_0() : Dec_Spin(0,1) : EndProcedure
Procedure DecSpinEvents_1() : Dec_Spin(1,2) : EndProcedure
Procedure DecSpinEvents_2() : Dec_Spin(2,3) : EndProcedure

If OpenWindow(0, 0, 0, 265, 70, "SpinGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  SpinGadget(0, 10, 10, 75, 25, 0, 1000,#PB_Spin_ReadOnly)
  SetGadgetState (0, 0) : SetGadgetText(0, "0.0")
  SetGadgetFont(0,FontID(0))
  SetGadgetColor(0,#PB_Gadget_FrontColor,RGB(255,0,0))
  
  SpinGadget(1, 95, 10, 75, 25, 0, 10000,#PB_Spin_ReadOnly)
  SetGadgetState (1, 250) : SetGadgetText(1, "2.50")
  SetGadgetFont(1,FontID(0))
  SetGadgetColor(1,#PB_Gadget_FrontColor,RGB(0,255,0))
  
  SpinGadget(2, 180, 10, 75, 25, 0, 100000,#PB_Spin_ReadOnly)
  SetGadgetState (2, 50000) : SetGadgetText(2, "50.000")
  SetGadgetFont(2,FontID(0))
  SetGadgetColor(2,#PB_Gadget_FrontColor,RGB(0,0,255))  
  
  BindGadgetEvent(0,@DecSpinEvents_0())
  BindGadgetEvent(1,@DecSpinEvents_1())
  BindGadgetEvent(2,@DecSpinEvents_2())
  
  Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: Simple PB Decimal SpinGadget()

Post by said »

RASHAD wrote:It is a simple you made it out of the range :P
Try to enter 6.34 for exam using your last code
PB 5.22 x86 win 8.1 x64
Just add #PB_Spin_ReadOnly
- Only digits are allowed
- No direct edit
You're right, my trick is not working fine! You win 8) :)

So, now this is decimal readonly SpinGadget() ... that's fair
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Simple PB Decimal SpinGadget()

Post by TI-994A »

said wrote:...my trick is not working fine! You win 8) :)
Hi said. It's all good code, and we're all learning. Great contribution! :D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply