!******************************************************************************!
! Adaptation of test_plot_many that tests the usage of PX, PX2, and NX autogrid options
!
! @issue    NXM-4029 PANEL AUTOGRID needs to support uneven layouts (Jeff)
! @since    4.1.3
! @author:  Jeanette McCall
!******************************************************************************!
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)
  switch "SRATE"       sr       GET DEF=8000
  switch "SFREQ"       sfreq    GET DEF=sr/10
  switch "PLOTTYPE"    plottype GET DEF="LINE"
  switch "PLOTFS"      fs4plot  GET DEF=128

  res numPlots  15
  res num1stRow 10
  res num2ndRow 5
  res numRows   2
  res D:percents1stRow (0.075,0.125,0.075,0.125,0.075,0.125,0.075,0.125,0.075,0.125,0.075,0.125)
  res D:percents2ndRow   (0.22,0.22,0.12,0.22,0.22)
  res maxPipeReaders 31

  if interAct then
    set instructions "You should see:\n\n&
        1st row: ^num1stRow plots : alternating skinny / wide\n\n&
        2nd row: ^num2ndRow plots : all plots same width except skinny middle plot"

    pipe on
      panel/msgid=MAIN/controls=gc/setup/grid={nx=^num1stRow,nx2=^num2ndRow,ny=^numRows,name=GP,title="plot",px=^percents1stRow,px2=^percents2ndRow}
      gcontrol label macro "Test MACRO"
      gcontrol menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft

      gcontrol text  idtext "Instructions" instructions 24 8 /title

      calc maxOpenTime 0.05 numPlots *
      timer set
      do ii 1 numPlots
        calc/type=L jj ii maxPipeReaders 1 + / 1 +
        if reg.wave^{jj} nrexists then
          waveform/id=wave^jj/throttle=real _wave^{jj}{maxOutlets=^maxPipeReaders} SI infinity white freq=sfreq amp=1000 delta=1/sr
        endif
        plot/nice/id=GP^{ii} _wave^{jj}{fs=^fs4plot} type=plottype axis=frame
      enddo
      timer elapse elapseTime
      assert/text="^reg.main.name: performance of opening many primitives (^elapseTime sec)" elapseTime <= maxOpenTime
    pipe off

  endif
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the 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
  else
    say "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
