!******************************************************************************!
! Tests for the PLOT legend
!
! @bug 1839: plotting setup file (.tbl) with multiple file=
!
! @Author:  J. Hallam / Neon Ngo
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)

  if interAct then
    call setDatDir
    if sysDatFull then
      set instructions "Legend in BOTTOM Right corner of plot with:\n                 &
                            GRID\n                 &
                            WORLD\n                 &
                            CITIES\n\n&
                        1) Click the buttons to HIDE,SHOW CITIES while the &
                        legend is visible: the check boxes in the legend &
                        should match the current state.\n\n &
                        2) Use the BR/TR/BL/TL buttons to move the LEGEND &
                        around the corners of the PANEL.\n&
                        * Legend should stay within the boundaries of plot area.\n\n&
                        3) Add/Remove the ELLIPSES:\n&
                        * Legend should change to match ellipses layer on plot."
      pipe on
        panel/msgid=MAIN/setup/controls=gc
        ! Test for Bug 1839: changed world.prm|cities.prm to use setup=plot_setup.tbl
        plot/id=idplot/legend={CORNER=BR} setup=nxm.sys.test.plot_setup.tbl

        gcontrol label MainLab "MACRO"
        gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
        set gc.main.itemColors {PASS=GREEN,FAIL=RED}
        gcontrol text idtext "Instructions" "^instructions" 24 20 /title
        gcontrol button toggleLegend "Legend" "HIDE,SHOW" "SHOW" /tleft
        gcontrol button legendPos "Legend" "BR,TR,BL,TL" "BR" /tleft
        gcontrol button showCities   "Cities" "HIDE,SHOW" "SHOW" /tleft
        gcontrol button Ellipses  "Ellipses" "ADD,REMOVE,HIDE,SHOW" "" /NC=2
      pipe off
    endif

    %test_plot_legend_1_color ! NXM-3574
  endif
endmacro

!**************************************************************************************************!
procedure processMessage m:msg
!say "^reg.main.name: msg.name ^msg.name, 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 "SHOWCITIES" then
  if msg.data EQS "HIDE" then
    set reg.IDPLOT.layers.cities.enable -GLOBAL
  else
    set reg.IDPLOT.layers.cities.enable +GLOBAL
  endif
elseif msg.name eqs "ELLIPSES" then
  if msg.data EQS "ADD" then
    if reg.IDPLOT.layers.ellipses NREXISTS then
      mess FUNC=SEND ID=IDPLOT NAME=OPENFILE DATA=ellipseflags5000.prm{TAG=ELLIPSES}
    else
      beep 1
      warning "^reg.main.name: ELLIPSES have already been added to the PLOT"
    endif
  elseif msg.data EQS "REMOVE" then
    if reg.IDPLOT.layers.ellipses REXISTS then
      mess FUNC=SEND ID=IDPLOT NAME=CLOSEFILE DATA=ELLIPSES
    else
      beep 1
      warning "^reg.main.name: ELLIPSES have already been REMOVED the PLOT"
    endif
  elseif msg.data EQS "HIDE" then
    if reg.IDPLOT.layers.ellipses REXISTS then
      set reg.IDPLOT.layers.ellipses.enable -GLOBAL
    endif
  else
    if reg.IDPLOT.layers.ellipses REXISTS then
      set reg.IDPLOT.layers.ellipses.enable +GLOBAL
    endif
  endif

elseif msg.name eqs "TOGGLELEGEND" then
  if msg.data EQS "HIDE" then
    invoke ,, reg.IDPLOT.hideLegend()
  else
    invoke ,, reg.IDPLOT.showLegend("BR")
  endif

elseif msg.name eqs "LEGENDPOS" then
  invoke ,, reg.IDPLOT.showLegend("^msg.data")

else
  ! do nothing
  !say "^reg.main.name: Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
endif

return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  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
