!******************************************************************************!
! Test keeping two plots in sync with zooms via percent zooms
!
! @bug      2722 PLOT: ZOOM(IN/OUT) messages cannot sync with (UN)ZOOM messages when linking plots
! @author:  J Bauer
! @version: $Id: test_plot_zoom_linked.mm,v 1.2 2013/05/31 10:55:12 jwb Exp $
!******************************************************************************!
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 "You should see a line plot and a raster plot.\n\n&
                      Move the mouse over the line plot and the freq for both plots &
                      should follow the mouse.\n\n&
                      Use the mouse wheel or '+' key to zoom in on the line plot. The raster should also zoom in.\n\n&
                      Use the mouse wheel or '-' key to zoom out on the raster plot. The line plot should match."

    set sr     1e5
    set sfreq  sr/10

    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 16 /title

        gcontrol choice zoomPercent "ZOOM Percentage" "10,50,90" "50"

      waveform _wave sf 10e10 sin freq=sfreq amp=1e4 delta=1/sr /throttle=real
      fft _wave _waveb{form=sb} nfft=1k navg=1 over=0 /dbf
      plot/id=plot1/nice/mousewheelzoom/usemouseposition/msgid=1 _waveb{cl=8} cnt=motion type=line y1=-100 y2=80
      plot/id=plot2/nice/mousewheelzoom/usemouseposition/msgid=1 _waveb cnt=click z1=-100 z2=80 /lpb=16


    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 "ZOOMPERCENT" then
    calc percent msg.data 100 /
    set reg.plot1.zoomPercentage percent
    set reg.plot2.zoomPercentage percent

  elseif msg.fid eqss "PLOT" then
    res fid msg.fid
    if fid eqs "PLOT1"
      res toid "PLOT2"
    else
      res toid "PLOT1"
    endif

    if msg.name eqs "MARK" and fid eqs "PLOT1" then
      set reg.waveform.freq msg.data.x
    elseif msg.name eqs "ZOOM" then
      res msg.name "ZOOMX"
      message pass ^toid msg
    elseif msg.name eqss "ZOOMIN" then
      message pass ^toid msg
    elseif msg.name eqss "ZOOMOUT" then
      message pass ^toid msg
    elseif msg.name eqs "UNZOOM" then
      message pass ^toid msg
    elseif "PANX" subs msg.name then
      res msg.name "PANX"
      message pass ^toid msg
    endif

  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
