!******************************************************************************!
! Test for Line and Raster plot coordinated zoom and pan
!
! @bug      NXM-3537 Plot does not zoom correctly
! @since    3.9.3
! @author:  Jeanette McCall
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)

  if interAct then
    set instructions "Zooming in on the LINE plot\n&
                      should trigger ZOOMX on RASTER plot"
    set format sf
    set sr 1e4
    set over 0
    set sfreq sr/10
    set chirpamt sfreq/4
    set amp 1e4
    set noiseamp 1e2
    set fsize 512
    set numelem 10e10
    set firstClick true
    set throttle realtime
    invoke tcur nxm.sys.lib.Time.current()

    set this.timer(0) 1
    pipe on
      panel/msgid=MAIN/setup/controls=gc
      gcontrol label macro "Test MACRO"
      gcontrol menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft

      gcontrol text  idtext "Instructions" instructions 24 8 /title

      panel/panes=winl/setup=winl
      panel/id=panel3/panes=winr/setup=winr

      ! Create chirped waveform
      waveform/id=wavec/throttle=^throttle/tl=4k _wavechirp format elem=numelem cos freq=sfreq amp=amp delta=1/sr chirp=^chirpamt start=^tcur

      ! take fft of the wave
      fft/dbf/id=signal _wavechirp _signal fsize over=over

      ! line plot
      plot/id=line/win=winl.panel/msgid=main/nice=2/xmult=M _signal{xu=FREQ,YU=ENERGY} type=line axis="TFRAME" &
                        readout="+EXTPREC" scale="AutoMin|AutoMax|MinRange|MaxRange" minrange=60 maxrange=256
      set reg.line.xmult M

      ! raster plot
      plot/id=raster/win=winr.panel/msgid=main/nice=2/xmult=M/lpb=1 _signal{xu=FREQ,YU=TIMECODE,YD=0.25} type=raster &
                        readout="+Show|+onTitle|+X|+DX|+T|+DT|+EXTPREC" view=IYX axis=BFRAME
      set reg.raster.xmult M

      ! TODO: other GConrols, Graphical Commands, Commands, etc. (otherwise remove this line)
    pipe off

  endif
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  elseif msg.name eqs "TIMER" then
    set this.timer(0) 0
    !say "message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
    res pane reg.winl.line
    invoke ,, pane.attach(reg.LINE,"LINE")
    res pane reg.winr.raster
    invoke ,, pane.attach(reg.RASTER,"RASTER")
  elseif msg.fid eqs "LINE"
    call handlePlots "RASTER"
  elseif msg.fid eqs "RASTER"
    call handlePlots "LINE"
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return

!******************************************************************************!
! handle interaction between plots
!******************************************************************************!
procedure handlePlots s:plot_other
  if msg.name eqs "ZOOM"
    set msg.name "ZOOMX"
    message pass ^plot_other msg
  elseif msg.name eqs "UNZOOM"
    message pass ^plot_other msg
  elseif msg.name eqs "PANX"
    message pass ^plot_other msg
  elseif msg.name eqs "PANXY"
    message prune ,, msg
    set msg.name "PANX"
    message pass ^plot_other msg
  endif

return
