!******************************************************************************!
!  Macro to implement box drawing feature using shift-drag.
!  Test case also shows use of CONTRAST option in PLOT (NXM-1337) - updated to use
!  GEAR2 theme to better see BLACK background when CONTAST MODE is on vs
!  theme's default bgcolor.
!
!  Arguments: None
!
!  Issue:   NXM-1281 XBC: need to map ID of 1 to MAIN
!  Issue:   NXM-1337 Plotter bug fixes
!  Author:  JC4 / N Ngo / J Bauer
!******************************************************************************!
startmacro/theme=gear2
  switch INTERACTIVE  interAct     GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch COREGRAPHICS coreGraphics GET DEF=FALSE SDEF=TRUE  ! for a quick check of graphics - more extreme tests - since 3.8.1

  call setDatDir

  call test_min_max  ! NXM-1333 (Add methods for setting bounds of box)

  if interAct or coreGraphics then
    call set_instr
    pipe init
      panel/setup/controls=gc
      ! msgid=1 added in 3.3.2 for NXM-1281
      plot/id=plot1/msgid=1/win=pane1 ^{datDir}.world.prm                            ! start a plot
      FEATURE label=box1 table={NAME=BOX1,TYPE=BOX,COLOR=WHITE,ENABLE=1} plot=plot1  ! add the feature
      invoke bgcolor nxm.sys.libg.MColor.toString(reg.plot1.theme.cbg)

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

      gcontrol choice plotView "Plot View" "LATLON,GEODETIC" "LATLON" /tleft
      gcontrol Button plotCONT "" "TOGGLE CONTRAST MODE" ""
    pipe off
  endif
endmacro

!******************************************************************************!
procedure processMessage
  !say "Msg ^msg.name = ^msg.data from ^msg.fid"

  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  elseif msg.fid eqs "PLOT1"  then
    if msg.name eqs "DRAGBOX" then
      result xmin msg.data.xmin
      result xmax msg.data.xmax
      result ymin msg.data.ymin
      result ymax msg.data.ymax
! INDEX ADDRESSING OF THE DRAGBOX/ZOOM MESSAGES HAS BEEN DEPRECATED AS OF NXM 2.3.0.
! ONLY DONE HERE TO MAKE SURE IT IS BACKWARD COMPATIBLE
      INFO "^reg.main.name: checking deprecated DRAGBOX fields (for backward compatibility)"
      if reg.plot1.view EQSS "GEO" then
        assert/text="^reg.main.name: DRAGBOX: ^{reg.plot1.view} mode, field 4 = XMIN " msg.data(4) EQ msg.data.xmin
        assert/text="^reg.main.name: DRAGBOX: ^{reg.plot1.view} mode, field 5 = XMAX " msg.data(5) EQ msg.data.xmax
        assert/text="^reg.main.name: DRAGBOX: ^{reg.plot1.view} mode, field 6 = YMAX " msg.data(6) EQ msg.data.ymax
        assert/text="^reg.main.name: DRAGBOX: ^{reg.plot1.view} mode, field 7 = YMIN " msg.data(7) EQ msg.data.ymin
      else
        assert/text="^reg.main.name: DRAGBOX: ^{reg.plot1.view} mode, field 0 = XMIN " msg.data(0) EQ msg.data.xmin
        assert/text="^reg.main.name: DRAGBOX: ^{reg.plot1.view} mode, field 1 = XMAX " msg.data(1) EQ msg.data.xmax
        assert/text="^reg.main.name: DRAGBOX: ^{reg.plot1.view} mode, field 2 = YMIN " msg.data(2) EQ msg.data.ymin
        assert/text="^reg.main.name: DRAGBOX: ^{reg.plot1.view} mode, field 3 = YMAX " msg.data(3) EQ msg.data.ymax
        assert/text="^reg.main.name: DRAGBOX: ^{reg.plot1.view} mode, field 4 = TMIN " msg.data(4) EQ msg.data.tmin
        assert/text="^reg.main.name: DRAGBOX: ^{reg.plot1.view} mode, field 5 = TMAX " msg.data(5) EQ msg.data.tmax
      endif
      ! since 3.7.1, using method call to set box location from msg.data table
      set box1.setBoundsFromTable(msg.data)
    elseif msg.name eqs "UNZOOM" then
      ! verify previous UNZOOM message data with XMIN,XMAX,YMIN,YMAX data for NXM-826
      invoke refData=plot1.MP.getXY
      assert/text="^reg.main.name: UNZOOM: XMIN = refData[0] " msg.data.xmin EQ refData(0)
      assert/text="^reg.main.name: UNZOOM: XMAX = refData[1] " msg.data.xmax EQ refData(1)
      assert/text="^reg.main.name: UNZOOM: YMIN = refData[2] " msg.data.ymin EQ refData(2)
      assert/text="^reg.main.name: UNZOOM: YMAX = refData[3] " msg.data.ymax EQ refData(3)
      INFO "^reg.main.name: checking deprecated UNZOOM fields (for backward compatibility)"
      assert/text="^reg.main.name: UNZOOM: field 0 = XMIN " msg.data(0) EQ msg.data.xmin
      assert/text="^reg.main.name: UNZOOM: field 1 = XMAX " msg.data(1) EQ msg.data.xmax
      assert/text="^reg.main.name: UNZOOM: field 2 = YMIN " msg.data(2) EQ msg.data.ymin
      assert/text="^reg.main.name: UNZOOM: field 3 = YMAX " msg.data(3) EQ msg.data.ymax
    endif
  elseif msg.name EQS "PLOTVIEW"
    set box1.dx 0
    set box1.dy 0
    set reg.plot1.view "^msg.data"
  elseif msg.name EQSS "PLOTCONT" then
    ! toggle PLOT contrast
    set reg.plot1.options "~CONTRAST"
    invoke ,, reg.plot1.refresh
    ! TODO: better way to toggle PLOT's contrast (0=toggle, 1=on, -1=off)
    ! set reg.plot1.contrast 0
  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

!******************************************************************************!
! Tests for NXM-1333 (Add methods for setting bounds of box)
procedure test_min_max
  new nxm.sys.libg.Feature feature

  set t:xyTests   {}
  set t:xyTests.1 {X=-10,DX=10,Y=-10,DY=10,T=-1,DT=-1}
  set t:xyTests.2 {X=100,DX=42,Y=-10,DY=10,T=-1,DT=-1}
  set t:xyTests.3 {X=-10,DX=10,Y=100,DY=42,T=-1,DT=-1}
  set t:xyTests.4 {X=-10,DX=10,Y=-10,DY=10,T=20,DT=20}
  set t:xyTests.5 {X=100,DX=00,Y=100,DY=00,T=20,DT=00}

  foreach test inTable/v xyTests
    calc xMin test.x test.dx 2 / -
    calc xMax test.x test.dx 2 / +
    calc yMin test.y test.dy 2 / -
    calc yMax test.y test.dy 2 / +

    invoke ,, feature.xMinMax(xMin,xMax)
    invoke ,, feature.yMinMax(yMin,yMax)

    ! changed in 3.3.2 to only display the assert if it FAILs
    res t:boxtests {TEXT="Testing box min and max"}
    assert/group=boxtests/text="^reg.main.name: xMin = ^xMin"    feature.xMin eqt xMin
    assert/group=boxtests/text="^reg.main.name: xMax = ^xMax"    feature.xMax eqt xMax
    assert/group=boxtests/text="^reg.main.name:    x = ^test.x"  feature.x    eqt test.x
    assert/group=boxtests/text="^reg.main.name:   dx = ^test.dx" feature.dx   eqt test.dx
    assert/group=boxtests/text="^reg.main.name: yMax = ^yMax"    feature.yMax eqt yMax
    assert/group=boxtests/text="^reg.main.name: yMin = ^yMin"    feature.yMin eqt yMin
    assert/group=boxtests/text="^reg.main.name:    y = ^test.y"  feature.y    eqt test.y
    assert/group=boxtests/text="^reg.main.name:   dy = ^test.dy" feature.dy   eqt test.dy

    if test.dt gt 0 then
      calc tMin test.t test.dt 2 / -
      calc tMax test.t test.dt 2 / +
      invoke ,, feature.tMinMax(tMin,tMax)

      assert/group=boxtests/text="^reg.main.name: tMin = ^tMin"    feature.tMin eqt tMin
      assert/group=boxtests/text="^reg.main.name: tMax = ^tMax"    feature.tMax eqt tMax
      assert/group=boxtests/text="^reg.main.name:    t = ^test.t"  feature.t    eqt test.t
      assert/group=boxtests/text="^reg.main.name:   dt = ^test.dt" feature.dt   eqt test.dt

    endif
  endfor
  assert/group=boxtests/summary

return

procedure set_instr

 set instr "(1) SHIFT-drag a box in plot window using left mouse button. &
         A box should appear. \n\n&
         (2) Zoom in by dragging a box in plot, then UNZOOM with right click. \n\n&
         (3) Change the view to GEODETIC and do the same. \n\n&
         (4) Check terminal output for any FAILED test. &
         Ignore any DEPRECATE DragBox messages.\n\n&
         (5) NXM-1337 - Click the TOGGLE CONTRAST MODE button. \n\n&
         (6) The plot should alternate between theme's background color  &
         and a BLACK background (CONTRAST mode on)."

return
