; --------------------------------------------------------------------------------
; @Title: Example for dialog with automatic update timer
; @Description:
;   An internal update timer is started within a dialog which
;   does some automated update within the dialog
;   see line 108 for UPDATE dialog command
;   Main working routine is at label "fnHandleUpdate"
;   This script uses the debugger command B::Var to create and handle an array of
;   strings representing user defined icons for continuous looping purpose.
; @Keywords: practice, dialog, update, var
; @Author: SME
; @Copyright: (C) 1989-2017 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Id: dialog_update.cmm 11204 2017-08-10 12:24:05Z smeister $

; --------------------------------------------------------------------------------
; Checks IF dialog already running then clear and restart
; (This demo cannot be started twice)
; --------------------------------------------------------------------------------

IF WINdow.EXIST(UpdateDlg)
   WinCLEAR UpdateDlg

;
; Global variables needed
;

; State of the statemaschine
LOCAL &UpdateState
&UpdateState="init"

; Progressbar percentage display
LOCAL &Progress
&Progress=0.

; Index and icons to display the spinning wheel
LOCAL &WheelIndex
&WheelIndex=0
B::Var.NEW char[12][128] \spinning_wheel
B::Var.Assign \spinning_wheel[0]="[=AVjT7ATDt0F40L3lq1z$l$Uz$UKIEl$1tE0x1QV38XV2@20X0800d00z3jSCyhRFh085]"
B::Var.Assign \spinning_wheel[1]="[=AVjT7ATDt0l70z0lk0zgl$Uz$UKIt$3xU0z33tV34KdVH0M2Z088111082V40R1G4H61e]"
B::Var.Assign \spinning_wheel[2]="[=AVjT7AzDt0l70z3l$1z$l$Uz$UKIdz3dU0t33uV34GdVH0F2V00811128HVp1R1KZn4hH]"
B::Var.Assign \spinning_wheel[3]="[=AVjT7AzDt0l70z3l$1z$l$Uz$UKIElq1NC041vV34GF1510G040000J1$iSCyhRJhdK2]"
B::Var.Assign \spinning_wheel[4]="[=AVjT7AzDt0l70z3l$1z$l$Uz$UKItk2xA0z03uV34GdVH0F2V008111882Z4lS1GaH6hH]"
B::Var.Assign \spinning_wheel[5]="[=AVjT7ATDt0l70z3l$1z$l$Uz$UKItk2xA0z03tV34GdVH0F2V008111882Z4lS1G5H6hH]"
B::Var.Assign \spinning_wheel[6]="[=AVjT7ATDt0l70z3l$1z$l$Uz$UKIElq1NC041wV34GF1510G040000J1$iSCyhRFhdK2]"
B::Var.Assign \spinning_wheel[7]="[=AVjT7ATDt0l70z3l$1z$l$Uz$UKIdz3dU0t33tV34GdVH0F2V00811128HVp1R1K4n4hH]"
B::Var.Assign \spinning_wheel[8]="[=AVjT7AzDt0l70z0lk0zgl$Uz$UKIt$3xU0z33uV34KdVH0M2Z088111082V40R1GZH61e]"
B::Var.Assign \spinning_wheel[9]="[=AVjT7AzDt0F40L3lq1z$l$Uz$UKIEl$1tE0x1PV38XV2@20X0800d00z3jSCyhRJh085]"
B::Var.Assign \spinning_wheel[10]="[=AVjT7AzDt0l70t3Vz1f$l$Uz$UKIt$3xU0z33uV3JGdVH7FHV028111082V40R1GZn41e]"
B::Var.Assign \spinning_wheel[11]="[=AVjT7ATDt0l70t3Vz1f$l$Uz$UKIt$3xU0z33tV3JGdVH7FHV028111082V40R1G4n41e]"


; --------------------------------------------------------------------------------
; Dialog Definition
;
WinPOS ,,,,,, UpdateDlg
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
             ; here we do not need macro expansion

             HEADER  "UPDATE Demo"

             ; Define width of dialog by printing an empty text: width is 25. units.
             ;       x  y  w  h
             POS     0. , 25. 0.
             TEXT    ""
             ;
             ; Update routine running state display
             ;
             POS     0.5 0. 22. 3.
             BOX     "Update Timer"
             POS     0.75 1. 2.5 1.
WHEELY:      DYNAMIC "[:empty]"
             POS     4. 1. 16. 1.
             TEXT    "running"
             ;
             ; Display Progress
             ; box, text, bar, start and stop
             ;
             POS     0.5 2. 22. 5.
             BOX     "Progress Bar"

             POS     1.25 3. 18. 1.
PROGTEXT:    DYNTEXT "0%"

             POS     1.25 4. 20. 0.75
PROGBAR:     BAR

             POS     1.25 5. 9.5 1.25
BTN_START:   BUTTON  "[:go] Start"  "&UpdateState=""start"""

             POS     11.5 5. 9.75 1.25
BTN_STOP:    BUTTON  "[:brk] Stop"  "&UpdateState=""stop"""
             ;
             ; handle [X] button and Escape
             ;
             CLOSE   "&UpdateState=""exit"""
             ;
             ; This starts the update timer and call fnHandleUpdate every 100ms
             ; Currently we cannot trigger faster than 100ms cause internal
             ; mainloop prohibits this
             ;
             UPDATE  "GOTO fnHandleUpdate"  100ms
)
;
; --------------------------------------------------------------------------------

halt_dialog:
             STOP
             ; IF something fails we can single step to here and cleanup
             &UpdateState="exit"
             DIALOG.END
             END


; --------------------------------------------------------------------------------
; Spin the wheel
; --------------------------------------------------------------------------------

fnSpinWheel:
(
             B::DIALOG.Set  WHEELY  Var.STRing(\spinning_wheel[&WheelIndex])
             &WheelIndex=&WheelIndex+1.
             IF (&WheelIndex>11.)
                 &WheelIndex=0.
             RETURN
)


; --------------------------------------------------------------------------------
; The update handler code
;
; Realized through a little statemaschine
;   wait  : waiting for commands from user e.g. start/stop progressbar
;   init  : first time intialization then GOTO state "wait"
;   start : start progressbar update then GOTO state "run"
;   stop  : stop  progressbar update then GOTO state "wait"
;   run   : do update progressbar and stay here
;
; --------------------------------------------------------------------------------

fnHandleUpdate:
(
             ; animate wheel to show running timer routine
             GOSUB fnSpinWheel

             IF      "&UpdateState"=="wait"
             (
                     ; just sit and wait until started
                     GOTO fnDone
             )
             ELSE IF "&UpdateState"=="init"
             (
                     ; initialize then wait until restarted

                     ; preset buttons
                     DIALOG.Enable  BTN_START
                     DIALOG.Disable BTN_STOP
                     ; start counting on update
                     &UpdateState="wait"
                     GOTO fnDone
             )
             ELSE IF "&UpdateState"=="start"
             (
                     ; start updateing then run

                     ; toggle buttons
                     DIALOG.Disable BTN_START
                     DIALOG.Enable  BTN_STOP
                     ; start counting on update
                     &UpdateState="run"
                     GOTO fnDone
             )
             ELSE IF "&UpdateState"=="stop"
             (
                     ; stop updating then wait

                     ; toggle buttons
                     DIALOG.Enable  BTN_START
                     DIALOG.Disable BTN_STOP
                     ; next state
                     &UpdateState="wait"
                     GOTO fnDone
             )
             ELSE IF "&UpdateState"=="run"
             (
                     ; update progressbar and stay here

                     LOCAL &tmp

                     &Progress=&Progress+5.
                     IF (&Progress>100.)
                         &Progress=0.

                     &tmp=FORMAT.DECIMAL(1,&Progress)
                     ; set progress-text
                     DIALOG.Set  PROGTEXT  "&tmp"+"%"
                     ; set progress-bar
                     DIALOG.Set  PROGBAR  &Progress
                     GOTO fnDone
             )
             ELSE IF "&UpdateState"=="exit"
             (
                     DIALOG.END
                     ENDDO
             )
fnDone:
             STOP
)
DIALOG.END

ENDDO

; --------------------------------------------------------------------------------
