TimeSpin Gadget - Is there a better/easier way

Just starting out? Need help? Post your questions and find answers here.
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Post by Foz »

Enjoy! :D

Code: Select all

EnableExplicit

;{- private variables
Global __TimerSpinGadget_TotalNumber.l
Global Dim __TimerSpinGadget_spnControl.l(3, 0)

Global Dim __TimerSpinGadget_SP_Max(3)
__TimerSpinGadget_SP_Max(0) = 23
__TimerSpinGadget_SP_Max(1) = 59
__TimerSpinGadget_SP_Max(2) = 59
__TimerSpinGadget_SP_Max(3) = 99
;}

;{- private procedures
Procedure __TimerSpinGadget_Update(pTimerSpinControlID.l, pSpinGadgetID.l)
  Protected Change.L
  Protected Value.W = GetGadgetState(__TimerSpinGadget_spnControl(pTimerSpinControlID, pSpinGadgetID))
  
  If Value<0
    Value + (__TimerSpinGadget_SP_Max(pTimerSpinControlID) + 1) : Change = -1
  EndIf
  
  Protected MaxValue.l = GetGadgetAttribute(__TimerSpinGadget_spnControl(pTimerSpinControlID, pSpinGadgetID), #PB_Spin_Maximum)-1
  
  If Value>__TimerSpinGadget_SP_Max(pTimerSpinControlID)
    Value-(__TimerSpinGadget_SP_Max(pTimerSpinControlID) + 1) : Change = 1
  EndIf
  
  SetGadgetState(__TimerSpinGadget_spnControl(pTimerSpinControlID, pSpinGadgetID), Value)
  SetGadgetText(__TimerSpinGadget_spnControl(pTimerSpinControlID, pSpinGadgetID), RSet(Str(Value), 2, "0") )
  
  If Change And pTimerSpinControlID>0
    SetGadgetState(__TimerSpinGadget_spnControl(pTimerSpinControlID-1, pSpinGadgetID), GetGadgetState(__TimerSpinGadget_spnControl(pTimerSpinControlID-1, pSpinGadgetID)) + Change)
    __TimerSpinGadget_Update(pTimerSpinControlID-1, pSpinGadgetID)
  EndIf
EndProcedure
;}
;{ public procedures
Procedure TimerSpinGadget_SetNumberOfInstances(pCount.l)
  __TimerSpinGadget_TotalNumber = pCount-1
  
  ReDim __TimerSpinGadget_spnControl(3, __TimerSpinGadget_TotalNumber)
  
EndProcedure

Procedure TimerSpinGadget_New(pGadgetArea.l, pOffsetX.l, pOffsetY.l, pInstanceNumber.l = 0)
  UseGadgetList(pGadgetArea)
  
  Protected i.l
  For i = 0 To 3
    __TimerSpinGadget_spnControl(i, pInstanceNumber) = SpinGadget(#PB_Any, pOffsetX + 32*(i), pOffsetY, 30, 20, -1, __TimerSpinGadget_SP_Max(i) + 1, #PB_Spin_ReadOnly | #PB_Spin_Numeric)
    SetGadgetState(__TimerSpinGadget_spnControl(i, pInstanceNumber), 0)
    SetGadgetText(__TimerSpinGadget_spnControl(i, pInstanceNumber), "00")
  Next
EndProcedure

Procedure TimerSpinGadget_Release()
  Protected i.l, j.l
  For j = 0 To __TimerSpinGadget_TotalNumber
    For i = 0 To 3
      FreeGadget(__TimerSpinGadget_spnControl(i, j))
    Next
  Next
EndProcedure

Procedure TimerSpinGadget_Event(Event.l, WindowID.l, GadgetID.l, EventType.l)
  If event = #PB_Event_Gadget
    Protected i.l, j.l
    For j = 0 To __TimerSpinGadget_TotalNumber
      For i = 0 To 3
        If __TimerSpinGadget_spnControl(i, j) = GadgetID
          __TimerSpinGadget_Update(i, j)
        EndIf
      Next
    Next
  EndIf
EndProcedure

Procedure.s TimerSpinGadget_GetTime(pInstanceNumber.l)
  Protected rv.s = ""
  rv + GetGadgetText(__TimerSpinGadget_spnControl(0, pInstanceNumber)) + ":"
  rv + GetGadgetText(__TimerSpinGadget_spnControl(1, pInstanceNumber)) + ":"
  rv + GetGadgetText(__TimerSpinGadget_spnControl(2, pInstanceNumber)) + ":"
  rv + GetGadgetText(__TimerSpinGadget_spnControl(3, pInstanceNumber))
  
  ProcedureReturn rv
EndProcedure
;}

; This section can be in another file, just include the code above as an XInclude
CompilerIf #PB_Compiler_Debugger
  OpenWindow(0, 216, 0, 300, 150, "New window ( 0 )", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_WindowCentered)
  
  TimerSpinGadget_SetNumberOfInstances(4)
  TimerSpinGadget_New(WindowID(0), 10, 10, 0)
  TimerSpinGadget_New(WindowID(0), 150, 10, 1)
  TimerSpinGadget_New(WindowID(0), 10, 50, 2)
  TimerSpinGadget_New(WindowID(0), 150, 50, 3)
  
  Define btnListValues.l = ButtonGadget(#PB_Any, 80, 90, 100, 24, "Display Values")
  
  Define Event
  Define WindowID
  Define GadgetID
  Define EventType
  
  Repeat ; Start of the event loop
    
    Event = WaitWindowEvent() ; This line waits until an event is received from Windows
    WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures
    GadgetID = EventGadget() ; Is it a gadget event?
    EventType = EventType() ; The event type
    
    TimerSpinGadget_Event(Event, WindowID, GadgetID, EventType)
    
    If Event = #PB_Event_Gadget
      If GadgetID = btnListValues
        Define display.s = ""
        display + TimerSpinGadget_GetTime(0) + Chr(13) + Chr(10)
        display + TimerSpinGadget_GetTime(1) + Chr(13) + Chr(10)
        display + TimerSpinGadget_GetTime(2) + Chr(13) + Chr(10)
        display + TimerSpinGadget_GetTime(3)
        
        MessageRequester("", display)
        
        GadgetID = 0
      EndIf
    EndIf
  
Until Event = #PB_Event_CloseWindow
TimerSpinGadget_Release()

CompilerEndIf
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Post by Perkin »

@Foz - Brilliant, :D works well, and each is a set, Thanks a lot for this.

I re-read tthe topic you posted earlier, still confuses me :? , I'll keep trying till I understand it, just out of sheer bloody-mindedness :lol:

Thanks
:D Perkin :D
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Post by Perkin »

I've made a couple of changes to Foz's code

Ive Changed

'TimerSpinGadget_GetTime' --> 'TimerSpinGadget_GetTimeS'

Added

'__TimerSpinGadget_SP_Mult(3)' (Array of multipliers to get millis)
'TimerSpinGadget_GetTimeL' (Returns milliseconds - removes need for conversion routine)

Here's the code added :-

Code: Select all

Global Dim __TimerSpinGadget_SP_Mult(3) 
__TimerSpinGadget_SP_Mult(0) = 3600000 
__TimerSpinGadget_SP_Mult(1) = 60000 
__TimerSpinGadget_SP_Mult(2) = 1000 
__TimerSpinGadget_SP_Mult(3) = 10 

; -Return value in millis
Procedure.l TimerSpinGadget_GetTimeL(pInstanceNumber.l) 
  Protected rv.l = 0 
  rv + GetGadgetState(__TimerSpinGadget_spnControl(0, pInstanceNumber)) * __TimerSpinGadget_SP_Mult(0)
  rv + GetGadgetState(__TimerSpinGadget_spnControl(1, pInstanceNumber)) * __TimerSpinGadget_SP_Mult(1) 
  rv + GetGadgetState(__TimerSpinGadget_spnControl(2, pInstanceNumber)) * __TimerSpinGadget_SP_Mult(2)
  rv + GetGadgetState(__TimerSpinGadget_spnControl(3, pInstanceNumber)) * __TimerSpinGadget_SP_Mult(3)
  
  ProcedureReturn rv 
EndProcedure 
I also added a few lines to the display reslts event, they now read (with the other four lines included)

Code: Select all

        display + TimerSpinGadget_GetTimeS(0) + Chr(13) + Chr(10) 
        display + TimerSpinGadget_GetTimeS(1) + Chr(13) + Chr(10) 
        display + TimerSpinGadget_GetTimeS(2) + Chr(13) + Chr(10) 
        display + TimerSpinGadget_GetTimeS(3)  + Chr(13) + Chr(10)
        display + Str(TimerSpinGadget_GetTimeL(0)) + Chr(13) + Chr(10) 
        display + Str(TimerSpinGadget_GetTimeL(1)) + Chr(13) + Chr(10) 
        display + Str(TimerSpinGadget_GetTimeL(2)) + Chr(13) + Chr(10) 
        display + Str(TimerSpinGadget_GetTimeL(3))
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Post by Foz »

Community code - building on the shoulders of others :)
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Post by Perkin »

Foz wrote
Community code - building on the shoulders of others
Building on the shoulders of the few (Foz, srod, gnozal to name just 3, I know there's more :) )
%101010 = $2A = 42
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Post by Foz »

Well, you brought the idea.
Ollivier brought a trimmed version.
I separated it into a "user control"
You have extended it.

That's four steps and 3 users to get it to where it is now :)
User avatar
dhouston
Enthusiast
Enthusiast
Posts: 430
Joined: Tue Aug 21, 2007 2:44 pm
Location: USA (Cincinnati)
Contact:

Post by dhouston »

Perkin wrote:dhouston - saw that topic whilst searching for help over this, it looked to have been answered, maybe re-requesting help again - bump the topic.
I was referring to http://www.purebasic.fr/english/viewtopic.php?t=29700 which has gone unanswered.
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Post by Foz »

Perhaps you can adapt my example here to suit your needs?
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

2 dhouston

Yes, and sorry for such this absence : sometimes it's not possible to answer... November, it wasn't a good period for me...

2 foz

Intersted version. Thanks !
User avatar
dhouston
Enthusiast
Enthusiast
Posts: 430
Joined: Tue Aug 21, 2007 2:44 pm
Location: USA (Cincinnati)
Contact:

Post by dhouston »

Foz wrote:Perhaps you can adapt my example here to suit your needs?
I've just used multiple SpinGadgets (two each for Latitude, Longitude & Timezone). While inelegant and ugly, it is cross-platform.
Post Reply