! Test Layer2D for making time-based feature boxes in a raster plot
!
! Author  : Neon Ngo / J Bauer
! Version : $Id: test_plot_layer2d_pipes.mm,v 1.2 2013/03/08 21:11:26 jwb Exp $
! Bug     : 2338, 2340
!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE

  if interAct then
    set instructions "The entire plot of apenny should appear. &
                      SHIFT-drag a box in the lower portion of the plot window using the right mouse button. &
                      A box should appear at the mouse location."

    pipe init
      panel/msgid=MAIN/setup/controls=gc
      noop apenny _apen

      gcontrol label  macro   "MACRO"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}
      gcontrol text idtext "Instructions" "^instructions" 24 10 /title

      gcontrol tval   tcurr "setTimeAt" 0 0 86400 /msg=no /edit=no
      gcontrol tval   tmin  "tMin"      0 0 86400 /msg=no /edit=no
      gcontrol tval   tmax  "tMax"      0 0 86400 /msg=no /edit=no

      gcontrol label  debug "Debug" Open
      gcontrol choice shape "Shape   " reg.wave.shapeList Cosine

      plot/id=idplot/msgid=MAIN/READOUT=Show|OnAxis|T|dT _apen /lps=128 /lpb=64

      ! 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=idplot
    pipe off
  endif
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.fid eqs "IDPLOT"  then
    if msg.name eqs "DRAGBOX" then
      result xmin msg.data.xmin
      result xmax msg.data.xmax
      result tmin msg.data.tmin
      result tmax msg.data.tmax
      if tmax lt tmin then
        set d:tmp tmin
        set tmin ^tmax
        set tmax ^tmp
      endif
      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
      timex tmin sod=gc.tmin.value
      timex tmax sod=gc.tmax.value
    endif

  else
    say "Unhandled message name=^msg.name info=^msg.info fid=^msg.fid data=^msg.data"
  endif

return
