!******************************************************************************!
! Test the plotting of Shape (.shp) files (LayerShape).
!
! Author:  J. Hallam / J. Bauer
!******************************************************************************!
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

    set defaultInstructions &
        "See a plot of the world with the nations of the world.\n&
        - initial color of points in northern US should be RED\n\n&
         Adjust the controls:\n&
         - border color\n&
         - coloring mode\n&
         - contrast\n&
         - opaqueness / alpha (transparency)\n\n&
         Pass the mouse over the different nations\n&
         -> name of the nation is displayed in the OnAxis readout.\n\n&
         ****************************************\n&
         Note: Opaqueness is just Alpha in percentage form.&
         "

    pipe init

      panel/msgid=MAIN/setup/controls=gc
      plot/id=idplot/READOUT="+OnAxis" ^{datDir}.world.shp{LAYER={LABEL=NAME,OPAQ=50}}|&
                                   nxm.sys.test.points.shp{LAYER={COLORTABLE={POINT=RED}}} view=latlon

      set shapeLayer reg.idplot.layers.world   ! Handle to world Shape file layer
      set pointsLayer reg.idplot.layers.points ! Handle to points Shape file layer

      invoke coloringModes shapeLayer.coloringModes

      set coloringMode shapeLayer.coloringMode
      set tab shapeLayer.getColorTable
      set borderColor "^tab.border"

      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" "^defaultInstructions" 40 15 /title

      gcontrol LABEL optionsLabel "Options"
      gcontrol menu borderColor "" "BorderColor"  0 /tright /nodepress
      gcontrol/nc=3 button coloringMode "Coloring Mode" "^coloringModes"
      ! slider FLAG serves to test Bug 1498
      gcontrol lval opaqueNess   "OpaqueNess " 50 0 100 1 /tleft /slider
      gcontrol lval alpha        "Alpha      " 127 0 255 1 /tleft /slider
      gcontrol lval contrast     "Contrast   " 50 0 100 1 /tleft /slider
      gcontrol lval brightness   "Brightness " 50 0 100 1 /tleft /slider
      gcontrol LABEL pointsOptionsLabel "Points Shape Options"
      gcontrol choice colorPoints "US Points Color" "RED,BLUE,CYAN,GREEN" RED /tleft
    pipe off
  endif

  ! Additional interactive tests, only run with /BASELINE
  if interAct and baseline then
    %test_plot_layershape_border ! Bug 1439
  endif
endmacro

!******************************************************************************!
procedure processMessage m:msg

!say "msg.name ^msg.name, data ^msg.data"

call setInstructions "^defaultInstructions"

if msg.name eqs "MAIN" then
  assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
  pipe stop
elseif msg.name EQS "OPAQUENESS" then
  set shapeLayer.opaqueness ^msg.data
  set  gc.alpha.value shapeLayer.alpha
elseif msg.name EQS "CONTRAST" then
  set shapeLayer.contrast ^msg.data
elseif msg.name EQS "BRIGHTNESS" then
  set shapeLayer.brightness ^msg.data
elseif msg.name EQS "ALPHA" then
  set shapeLayer.alpha ^msg.data
  set gc.opaqueness.value ^shapeLayer.opaqueness
elseif msg.name EQS "COLORINGMODE" then
  set shapeLayer.coloringMode ^msg.data
  if msg.data EQS "ONSELECT" then
    invoke ,, shapeLayer.setSelectedValue(CODE,US,TRUE)
    call setInstructions &
      "Should see a plot of the world with the nations of the world. All nations &
       should be colored black, except the US which should be colored blue."
  endif

elseif msg.name EQS "COLORPOINTS" then
  set pointsLayer.color ^msg.data

elseif msg.name EQS "BORDERCOLOR" then
  popup/blocking=MACRO/title="Set ^attribute" result=borderColor FUNC=COLOR DEFAULTS=^borderColor
  set shapeLayer.colorTable {border=^borderColor}

elseif msg.data EQS "SHAPE-OPAQ" then
  call test_plot_shape_opaq

elseif msg.data EQS "SHAPE-ONSELECT" then
  call test_plot_shape_onselect
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 open
  ! Automatically called
return

!******************************************************************************!
procedure newplot s:plotstring s:instructions
  message FUNC=send ID=IDPLOT NAME=CLOSEFILE DATA="*"
  set reg.idplot.axis   "DEF"
  foreach item intable testfiles
   if testfiles.^item nFEXISTS then
    warning "Could not generate/find ^testfiles.^item"
    return
   endif
  endfor
  call setInstructions instructions
  message FUNC=send ID=IDPLOT NAME=OPENFILE DATA=plotstring
  set reg.idplot.xunits "DEFAULT"
  set reg.idplot.yunits "DEFAULT"
return

!******************************************************************************!
procedure test_plot_none
  result testfiles {}
  call newplot "" "Should see a blank screen"
return


!******************************************************************************!
procedure test_plot_shape
  result testfiles {}
  result testfiles.world   "^{datDir}.world.shp{LAYER={LABEL=NAME}}"

  call setInstructions
    "Should see a plot of the world with the nations of the world colored. &
     As the mouse passes over the different nations the name of the nation &
     should be displayed in the OnAxis readout."
  message send idplot,,view,,latlon
return


!******************************************************************************!
procedure test_plot_shape_onselect
  result testfiles {}
  result testfiles.world   "^{datDir}.world.shp{LAYER=&
                                        {LABEL="NAME",COLORINGMODE="OnSelect"}}"


  message send idplot,,view,,latlon
  pause 3
  invoke ,, reg.idplot.layers.world.setSelectedValue(CODE,US,TRUE)
return


!******************************************************************************!
procedure setInstructions s:msgString
  set reg.panel.controls.idtext.value msgString
return
