!------------------------------------------------------------------------------!
! Demonstrate Message func=SEND vs func=SENDX, SENDW, and invokes.
!
! Author:  J. Bauer
!------------------------------------------------------------------------------!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE

  call setDatDir

  set def_instructions &
      "Should see a blank plot.\n\n&
      1) Click the OPEN FILE MSG buttons below\n\n&
      2) Note the axis ranges\n   &
         (on plot or below)\n\n&
      Clicking on the SEND button causes the plot to &
      have different axis values because of the delay in the SEND function."

  set daFile "^{datDir}.fftdemo.prm"

  if interAct then
    pipe init

    panel/msgid=MAIN/setup/controls=gc
    plot/id=idplot/READOUT="+OnAxis"

    gcontrol label mainLabel "Main"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}
    gcontrol text idtext "Instructions" "^def_instructions" 24 12 /title

    gcontrol button doOpenFile "OPEN FILE MSG: " "DIRECT,SEND,SEND W/ PAUSE,SENDX,SENDW" none /nc=2
    gcontrol prompt outerXMin  "Outer X Min" ""
    gcontrol prompt outerXMax  "Outer X Max" ""
    gcontrol prompt innerXMin  "View X Min" ""
    gcontrol prompt innerXMax  "View X Max" ""
    gcontrol prompt origXDiff  "Original View X2-X1" ""
    gcontrol prompt newXDiff   "Updated View X2-X1" ""
    gcontrol prompt xstart     "X Start:" ""

    pipe off

  endif

endmacro

!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name:  User indicated" msg.data eqss "PASS"
    pipe stop

  elseif msg.name EQS "DOOPENFILE" then
    call clearPrompts
    set reg.idplot.axis "TFRAME"
    call setOrigDiff
    if msg.data eqs "DIRECT" then
      invoke ,, reg.idplot.closeFile("*")
      invoke ,, reg.idplot.openFile("^daFile")
    elseif msg.data eqs "SEND W/ PAUSE"
      message FUNC=send ID=IDPLOT NAME=OPENFILE INFO=-1 DATA="^daFile"
      pause .1
    else
      message FUNC=^msg.data ID=IDPLOT NAME=OPENFILE INFO=-1 DATA="^daFile"
    endif

    set reg.idplot.xs 2e6
    set reg.idplot.rxd 2500

    pause .1
    call setNewDiff
    call updatePrompts
  else
    ! do nothing
    say "^reg.main.name: Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return

!******************************************************************************!
procedure clearPrompts
  set gc.outerXMin.value ""
  set gc.outerXMax.value ""
  set gc.innerXMin.value ""
  set gc.innerXMax.value ""
  set gc.xstart.value    ""
return

!******************************************************************************!
procedure setOrigDiff
  set origX1 reg.idplot.XMin
  set origX2 reg.idplot.XMax
  calc origDiff origX2 origX1 -
return

!******************************************************************************!
procedure setNewDiff
  set newX1 reg.idplot.XMin
  set newX2 reg.idplot.XMax
  calc newDiff newX2 newX1 -
return

!******************************************************************************!
procedure updatePrompts
  set gc.outerXMin.value reg.idplot.outerXMin
  set gc.outerXMax.value reg.idplot.outerXMax
  set gc.innerXMin.value reg.idplot.XMin
  set gc.innerXMax.value reg.idplot.XMax
  set gc.origXDiff.value origDiff
  set gc.newXDiff.value  newDiff
  set gc.xstart.value    reg.idplot.XStart
return
