!
! Test case for enforcing data draw ordering in Layer2D
!
! @bug      2035
! @author:  J Bauer
! @version: $Id: test_plot_layer2d_ordered.mm,v 1.4 2015/07/03 13:19:26 jtm Exp $
!
startmacro D:sr[1e4]

  lookandfeel "Gear4"
  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
  fft/psd/log _wave _AHfftaudio nfft=1k
  noop _AHfftaudio _AHfftaudio2
  status _wave XD=xdelta
  set defInstr "Click on each Tab and mouse over the beginning of each plot. The plots in each tab &
                 should be identical to each other."


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

  plot/msgid=main/id=plot1/forceorder _AHfftaudio readout=Show|onAxis|onTitle|T|dT
  plot/msgid=main/id=plot2/forceorder _AHfftaudio2 readout=Show|onAxis|onTitle|T|dT ! view=ixy

  ! Test case for Bug 2035
  ! 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=plot1
  ! 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=plot1

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

  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 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="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop

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

  elseif msg.fid eqs "PLOT1" 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 "PLOT2" 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
