! Test case for problems seen in Wayne's application(s).
!
! @bug 1905 Wayne's app failing to paint due to improper status setting in plot.open
! @bug 2035 time based feature not drawn on PLOT correctly ?
! @author:  Wayne Dengel / Jim Bauer / Neon Ngo
startmacro D:sr[1e4]

  lookandfeel "Gear3" !java
  pipe init

  panel/msgid=main/setup/controls=gc

  timex now currentTime
  waveform/rt _wave{ps=32k} shape=sin form=cf elem=inf amp=32e3 freq=sr/10  delta=1/sr  start=currentTime
  waveform/rt _wave2{ps=32k} shape=triangle form=cf elem=inf amp=32e3 freq=sr/5  delta=1/sr  start=currentTime
  fft/psd/log _wave _fftaudio nfft=1k
  fft/psd/log _wave2 _fftaudio2 nfft=1k
  res swapFile "_fftaudio2"
  status _wave XD=xdelta
  set defInstr "1) Click on the 'LIVE' tab to view the right-side plots.\n&
                => should see 1 large plot on the left\n   &
                   2 plots on the right one above the other\n\n&
                2) on LIVE tab, move mouse over raster PLOT above draw line\n&
                => time SHOULD NOT be zero\n\n&
                3) BEFORE PLOT WRAPS: move mouse to very bottom of plot\n   before axis\n&
                => it SHOULD NOT have a time\n\n&
                4) Zoom in, left mouse click to draw a time based horizontal\n   BLUE line at clicked location\n&
                => line should NOT be a few seconds below clicked location.\n\n&
                5) Click on the 'Swap Raster File' button\n&
                => plot on the right should change\n\n&
                *******************************************\n&
                1 & 2 test BUG 1905 fix, previously plots on the right would be blank or not refreshing.\n&
                3 & 4 test Bug 2035\n\n&
                Note: this is UCL test that is in nxm/ucl/mcr/&
                "

  gcontrol label macro "MACRO"
  gcontrol/tleft menu main "Exit with Status = " "PASS,FAIL" table={ITEMCOLORS={PASS=GREEN,FAIL=RED}}
  gcontrol text idtext "Instructions" "^defInstr" 24 26 /title

  psdpanel/msgid=main/win=win.panel
  plot/msgid=main/id=tpsd1/win=win.pane2 _fftaudio readout=Show|onAxis|onTitle|T|dT view=ixy

  ! Test case for Bug 2035
  call addFeatures

  ! hline feature for regular PLOT
  FEATURE/id=hline2 label=hline2 table={NAME=HLINE2,TYPE=YLINE,T=0,DT=^xdelta,COLOR=YELLOW,ENABLE=1} plot="TPSD1"

  gcontrol label debug "Debug"
    gc button state "Pipe State" "Run,Pause" 1
  gcontrol label label1 "Plot on PsdPanel"
    gcontrol prompt tline  "Time of line"     "" /msg=no /edit=no
    gcontrol tval   tmin  "Box tMin"      0 0 86400 /msg=no /edit=no
    gcontrol tval   tmax  "Box tMax"      0 0 86400 /msg=no /edit=no
    gcontrol button swap  "" "Swap Raster File" ""

  gcontrol label label2 "Plot on Regular Pane"
    ! for line feature on regular plot
    gcontrol prompt tline2 "Time of line"     "" /msg=no /edit=no

  pipe run
  pipe wait
  pipe off
endmacro

procedure processMessage m:msg
  if /debug then say "Got message ^msg data=^msg.data"

  if msg.name eqs "MAIN" then
    assert/text="Plot files tests" msg.data eqss "PASS"
    pipe stop

  elseif msg.name eqs "SWAP" then
    message send id=psdpanel name="SWAPRASTER" data=^swapFile
    if swapFile eqs "_fftaudio2" then
      set swapFile "_fftaudio"
    else
      set swapFile "_fftaudio2"
    endif
    pause 1  ! give time for plot to open and start
    call addFeatures  ! add features back that get blown away with the plot

  elseif msg.name eqs "STATE"
    if msg.data eqs "PAUSE"
      pipe pause
    elseif msg.data eqs "RUN"
      pipe wait
    endif

  elseif msg.fid eqs "RTPSD" then
    ! messages from PLOT on PSD Panel (on JTabbedPane on JPanel)
    if msg.name eqs "DRAGBOX" then
      ! draw box feature on PLOT at specified time
	  result xmin msg.data.xmin
	  result xmax msg.data.xmax
	  result tmin msg.data.tmin
	  result tmax msg.data.tmax
      calc centerx ^xmin ^xmax + 2 /
      calc centert ^tmin ^tmax + 2 /
      set box1.x ^centerx
      set box1.t ^centert
      calc deltax ^xmax ^xmin -
      calc deltat ^tmax ^tmin -
      set box1.dx ^deltax
      set box1.dt ^deltat
      ! only get seconds of day for display on tvals
      timex tmin sod=gc.tmin.value
      timex tmax sod=gc.tmax.value
      if /debug then
        timex tmin
        timex tmax
      endif

    elseif msg.name eqs "MARK" then
      set   hline1.t  ^msg.data.t
      timex hline1.t gc.tline.value /FMT="HMS"
    endif

  elseif msg.fid eqs "TPSD1" then
    ! messages from PLOT on regular PANE
    if msg.name eqs "MARK" then
      set   hline2.t  ^msg.data.t
      timex hline2.t gc.tline2.value /FMT="HMS"
    endif
  endif

return

procedure addFeatures
  ! add box feature to plot for creating a user selectable dragbox on the raster plot
  FEATURE/id=box label=box1 table={NAME=BOX1,TYPE=BOX,COLOR=WHITE,ENABLE=1} plot=rtpsd
  ! add time-based horizontal line feature that will move with left mouse click
  FEATURE/id=hline1 label=hline1 table={NAME=HLINE1,TYPE=YLINE,T=0,DT=^xdelta,COLOR=BLUE,ENABLE=1} plot="RTPSD"


return
