; --------------------------------------------------------------------------------
; @Title: Self-retriggered timer test with dialog box
; @Description: Dialog with timer
; @Keywords: dialog, practice, timer
; @Author: REI
; @Copyright: (C) 1989-2014 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Id: dialog_ontime.cmm 11204 2017-08-10 12:24:05Z smeister $


  ; check if already running then clear and restart
IF WINdow.EXIST(TimerDlg)
  WinCLEAR TimerDlg

; --------------------------------------------------------------------------------
;     Settings
; --------------------------------------------------------------------------------

  ; update time of timer
LOCAL &UpdateTime
&UpdateTime="1.0s"

  ; stop/start handling boolean flag
LOCAL &StopDialog
&StopDialog=(0==1)

; --------------------------------------------------------------------------------
;     dialog definition
; --------------------------------------------------------------------------------
;
WinPOS ,,,,,, TimerDlg
DIALOG
(  ; For activating PRACTICE macro expansion inside a DIALOG definition
   ; two prerequisites have to be fulfilled (ide_ref.pdf):
   ; 1. "(&" instead of "(" must be used
   ; 2. "(&" must begin in the first column of the line

           HEADER "ON TIME demo"

           ; Define width of dialog by printing an empty text: width is 25. units.
	   ;   x  y  w  h
	   POS 0. , 26. 0.
           TEXT ""

           POS 0.5 0. 22. 3.
           BOX "Time"

           POS 1.5 1. 20. 1.
TIME1:     EDIT ""
           (
           )

           POS 0.5 2.75 10.5 1.25
BTN_STOP:  BUTTON "[:brk] Stop" "GOSUB stop_refresh"

           POS 11.5 2.75 11. 1.25
BTN_START: BUTTON "[:go] Start" "GOSUB start_refresh"

           POS 0.5 4.5 14. 1.
TXT_STATE: DYNTEXT "---"

           CLOSE "GOTO end_dialog"
)
;
; --------------------------------------------------------------------------------

begin:
    ; initial update
  GOSUB fnUpdateAllYourStuffHere

    ; Button handling
  DIALOG.DISABLE BTN_START

    ; show new state
  DIALOG.Set TXT_STATE "Timer running"

    ; this starts the update timer
  ON TIME &UpdateTime GOTO fnTimer

halt_dialog:
    STOP

; called via [x] close button or if Escape pressed or if "continue" executed
end_dialog:
  DIALOG.END
  ENDDO

; called via BTN_STOP button
stop_refresh:
    ; clear flag
  &StopDialog=(0==0)
    ; set text in dialog
  DIALOG.Set TXT_STATE "Timer stopping"
    ; toggle buttons
  DIALOG.Disable BTN_STOP
  RETURN

; called via BTN_START button
start_refresh:
    ; set flag
  &StopDialog=(0==1)
    ; continue from where we have stopped
  CONTinue
  RETURN


; --------------------------------------------------------------------------------
; update all variables e.g. read memory or registers
; and set them to the dialog to display
;
fnUpdateAllYourStuffHere:
(
    ; simple set the current time to our edit control
  DIALOG.Set TIME1 Clock.Time()
)
RETURN


; --------------------------------------------------------------------------------
; Self-Retriggering timer to update editfields
; The timer can be stopped and restarted via button
;
fnTimer:

  IF &StopDialog
  (
    DIALOG.Set TXT_STATE "Timer stopped"
    DIALOG.Enable BTN_START
    STOP "Timer stopped"
    ; --- here we will stop ---
    PRINT "Timer started"
      ; set text in dialog
    DIALOG.Set TXT_STATE "Timer running"
      ; toggle buttons
    DIALOG.Enable BTN_STOP
    DIALOG.Disable BTN_START
  )
    ; Update all your variables and set them to the dialog elements
  GOSUB fnUpdateAllYourStuffHere
    ; retrigger ourself
  ON TIME &UpdateTime GOTO fnTimer
  GOTO halt_dialog
