! Test race condition when drawing feature and unzooming at the same time.
!
! @Author:  Neon Ngo / J Bauer
! @Bug:     2113
!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE

  call setDatDir

  if interAct then
    set instructions "1) Press the buttons to zoom in and then zoom out.\n&
                      2) Do this MULTIPLE times.\n&
                      3) There should be a feature BOX where the zoom was\n   (around Lincoln's eye)\n\n&
                      ************************************\n&
                      Note: Prior to Bug 2113 fix the plot would sometimes not zoom out."

    pipe init
      panel/msgid=MAIN/setup/controls=gc

      plot/id=idplot/msgid=MAIN ^{datDir}.apenny.prm

      ! 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

      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 button zoomin "" "ZOOM In"
      gcontrol button zoomout "" "ZOOM Out"

    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.name eqs "ZOOMIN" then
    message send ID=idplot NAME="ZOOM" DATA={X1=76,X2=86,Y1=70,Y2=80}

  elseif msg.name eqs "ZOOMOUT" then
    set box1.x 81
    set box1.y 75
    set box1.dx 10
    set box1.dy 10
    ! need to use INVOKE here because the delay in MESSAGE does not show the issue
    invoke ,, reg.idplot.unzoom()
   ! message send ID=idplot NAME="UNZOOM"

  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
    endif
  else
    say "Unhandled message name=^msg.name info=^msg.info 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
