!******************************************************************************!
! This tests the functionality of a collapsable menubar at the top of a panel.
! WARNING: the MMP on this file NEEDS TO REMAIN ALMOST EXACTLY AS IT IS to be
! a faithful enough replica of Jeff's SNAPPER mmp. Create a new separate test case,
! if any significant modifications are needed.
!
!
! Arguments: None
!
! @bug      NXM-3324, NXM-3404 Jeff's panel menubar
! @since    3.9.0
! @author:  Jeanette McCall and Kevin Esterheld
!******************************************************************************!
startmacro S:throttle["REAL"]
  switch INTERACTIVE  interAct     GET DEF=TRUE  SDEF=TRUE
  switch BASELINE     baseline     GET DEF=FALSE SDEF=TRUE
  switch COREGRAPHICS coreGraphics GET DEF=FALSE SDEF=TRUE
  res oldLookAndFeel env.theme
  lookandfeel gear4
  env get IOOptions origIOOptions
  !env set IOOptions +FixPipeRestartTimeLines - no longer needed in 3.9.4
  call setDatDir

  if interAct OR coregraphics then
    set instructions "This demonstrates the functionality of MENUBAR.\n\n&
                      The GLabel groups are initially displayed on a menu bar across the top.\n\n&
                      1) Click individual tabs to see the GControl menus drop down.\n\n&
                      2) With at least one menu bar open, use the scroll bar in the lower left &
                       to scroll left to right though the menu tabs\n\n&
                      3) Now expand the collapsed section / pane that normally holds the GControls.&
                         The menu bar should disappear and the GControls should move to that pane.\n\n&
                      4) Expand one of the GLabels and drag off one of the controls over a plot.\n\n&
                      5) Collapse the GContol pane again and all the controls should move back &
                         to the menu bar, except the detached control (which should not move).\n\n&
                      6) Draw a dragbox in any plot with the RMB, then release the RMB. &
                         Nothing should print out in the on-axis readout since the on-axis readout is turned off.\n\n&
                      7) 3 rows of the list should render at all times."

    set format sf
    set sr 1e4
    set over 0
    set sfreq sr/10
    set chirpamt sfreq/4
    set amp 1e4
    set noiseamp 1e2
    set fsize 512
    set numelem 10e10
    set firstClick true

    invoke tcur nxm.sys.lib.Time.current()

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

      ! Create waveforms with adjustable frequency (2nd one has frequency chirped each TL)
      waveform/id=wave/throttle=^throttle _wave format elem=numelem cos freq=sfreq amp=amp delta=1/sr start=^tcur
      waveform/id=wavec/throttle=^throttle/tl=4k _wavechirp format elem=numelem cos freq=sfreq amp=amp delta=1/sr chirp=^chirpamt start=^tcur
      ! Create ffts of the 2 waveforms (frame size can be adjusted using gcontrol)
      fft/dbf/id=fft _wave _fft{form=sf} fsize over=over
      fft/dbf/id=fftchirp _wavechirp _fftchirp fsize over=over
      ! plot the ffts
      plot/id=plotfft/msgid=MAIN/READOUT=Show|OnTitle|T|dT _fft view=iXY 
      plot/id=plotfftchirp/msgid=MAIN/READOUT=Show|OnTitle|T|dT _fftchirp view=iXY
      plot/id=plotfftcopy/msgid=MAIN/READOUT=Show|OnTitle|T|dT _fft view=iXY 
      plot/id=plotfftchirpcopy/msgid=MAIN/READOUT=Show|OnTitle|T|dT _fftchirp view=iXY
      list2/id=list ^{datDir}.coneflags5000.prm

      ! add box feature to plot for creating a user selectable dragbox on the raster plot
      FEATURE/id=box1 label=box1 table={NAME=BOX1,TYPE=BOX,COLOR=WHITE,ENABLE=1} plot=idplotfft
      FEATURE/id=box2 label=box2 table={NAME=BOX2,TYPE=BOX,COLOR=WHITE,ENABLE=1} plot=idplotfft2

      ! add a logger to match Jeff's test
      msglog/id=logger/msgid=main/wrap/file=logFile
      res INFO 0
      message send logger ,, LOG ,INFO, "Open the INSTRUCTIONS tab to see what to do"

      gcontrol label  macro   "INSTRUCTIONS"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}
      gcontrol text idtext "Instructions" "^instructions" 30 35 /title

      gcontrol label  plot  "PLOT/TIMELINE"
      gcontrol choice sfreq "Set SFREQ (multiplier):" ".25,.5,1,2,3,4" "1" /tleft
      gcontrol choice framesz "Set FFT FRAME Size:" "256,512,1k" "512" /tleft
      gcontrol tval   timeAtPos1 "Time at 1st click" 0 0 -1 /flags="NoMsg|NoEdit"
      gcontrol tval   timeAtPos2 "Time at 2nd click" 0 0 -1 /flags="NoMsg|NoEdit"
      gcontrol tval   tcurr "startTimeAt" tcur 0 -1 /msg=no /edit=no
      gcontrol tval   tmin "box tMin" 0 0 -1 /msg=no /edit=no

      gcontrol tval   tmax "box tMax" 0 0 -1 /msg=no /edit=no
      gcontrol label  tab3   "TAB3"
      gcontrol menu clickThree "Click Here" "TAB,THREE" /tleft
      gcontrol text idtextt3 "Instructions" "TAB3: see Instructions tab text" 24 4 /title

      gcontrol label  tab4   "TAB4"
      gcontrol menu clickFour "Click Here" "TAB,FOUR" /tleft
      gcontrol text idtextt4 "Instructions" "TAB4: see Instructions tab text" 24 4 /title

      gcontrol label  tab5   "TAB5"
      gcontrol menu clickFive "Click Here" "TAB,FIVE" /tleft
      gcontrol text idtextt5 "Instructions" "TAB5: see Instructions tab text" 24 4 /title
    pipe off

    %test_panel_menubar_tsetup
    %test_panel_menubar_save_setup
  endif

  env set IOOptions origIOOptions
  lookandfeel oldLookAndFeel
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 "DRAGBOX"  then
    if msg.fid eqss "IDPLOTFFT" then
      result tmin msg.data.tmin
      result tmax msg.data.tmax
      timex tmin d:gc.tmin.value
      timex tmax d:gc.tmax.value
      if msg.fid eqs "IDPLOTFFT2" then
        ! second fft from signal with noise added in 3.7.3
        set box2.setBoundsFromTable(msg.data)
      else
        set box1.setBoundsFromTable(msg.data)
      endif
    endif
  elseif msg.name eqs "SFREQ" then
    ! set frequwency of both waveforms
    calc newfreq sfreq ^msg.data *
    set reg.wave.freq  ^newfreq
    set reg.wavec.freq  ^newfreq
    set reg.wave.state "RESTART"
    set reg.wavec.state "RESTART"
  elseif msg.name eqs "FRAMESZ" then ! since 3.7.2 for NXM-3040
    ! set framse size of both ffts
    res fsize msg.data
    set reg.fft.nfft fsize
    set reg.fft2.nfft fsize
  elseif msg.name eqs "MARK"
    ! set the "Time At # Click" gcontrols
    if firstClick then
      res gc.timeAtPos1.value msg.data.time
      res firstClick false
    else
      res gc.timeAtPos2.value msg.data.time
      res firstClick true
    endif
  elseif msg.name eqs "UNZOOM"
    if msg.fid eqs "IDPLOTFFT" then
      invoke reg.idplotfft.refresh()
    elseif msg.fid eqs "IDPLOTNOOP" then
      invoke reg.idplotnoop.refresh()
    endif
  elseif msg.name eqs "ZOOM"
  elseif msg.name eqss "CLICK"
    res display msg.data
    if display eqs ""
      res display msg.name
    endif
    popup/blocking=none popup OK "You clicked ^display" "INFO"
  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
