!******************************************************************************!
! Test changing themes at the command and panel level
!
! Arguments: /THEME= switch from test_plot.mm
!
! @bug      2618 PLOT / MWINDOW / THEME: Plot theme should be able to be independent of global theme
! @author:  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)
  switch SHARETHEME  shareTheme GET DEF=false SDEF=TRUE ! if sharing the theme

  call setDatDir

  if interAct then
    set changePlotThemeTitle "Change Top Plot Theme"
    set instructions "You should see a PLOT with the BROWSER theme and a PLOT with the &
                      default theme of ^{ENV.THEME}. \n\n&
                      Click the ^changePlotThemeTitle button. Only the theme on the top PLOT &
                      should change. \n\n&
                      Click the buttons to change the PANEL theme. Only the PANEL theme should change.\n\n&
                      The CHOICE widget should have its own theme, which is set using its choices."
                      
    if shareTheme then
      set changePlotThemeTitle "Change Plot Theme"
      set instructions "You should see two PLOTs, both with the &
                      default theme of ^{ENV.THEME}. \n\n&
                      Click the ^changePlotThemeTitle button. All themes should change except the CHOICE.. \n\n&
                      Click the buttons to change the PANEL theme. All themes should change except the CHOICE.\n\n&
                      The CHOICE widget should have its own theme, which is set using its choices."
    endif

    pipe on
      panel/msgid=MAIN/setup/controls=gc
      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 18 /title

      gcontrol prompt prompt1 "Sample prompt" "Sample text"
      gcontrol CHOICE choice1 "Change this widget's Theme only" "BOW,WOB,GEAR1,GEAR3,OCEAN" "BOW" /wtheme=bow
      
      plot/id=plot1 ^{datDir}.world.prm /theme=browser
      
      plot/id=plot2 ^{datDir}.world.prm 
      
      gcontrol button plotThemeButton ^changePlotThemeTitle "BOW,WOB,GEAR1,GEAR3,OCEAN" "BOW"
      gcontrol button panelThemeButton "Change Panel Theme" "BOW,WOB,GEAR1,GEAR3,OCEAN" "BOW"
      gcontrol line
      gcontrol button refreshPanel "" "Refresh panel"
      
      if shareTheme then
        call updatePanel
      endif
      
    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
    
  elseif msg.name eqs "CHOICE1" then
    set gc.choice1.widgettheme ^msg.data  
    
  elseif msg.name eqs "PLOTTHEMEBUTTON" then
    set reg.plot1.theme ^msg.data
    !invoke ,, reg.plot1.hardRefresh()  ! hard refresh needed to ensure data area is refreshed
    message send plot1 name="RESIZE"
    if shareTheme then
      call updatePanel
      message send plot2 name="RESIZE"  ! resize needed to ensure data area is refreshed
    endif
  
  elseif msg.name eqs "PANELTHEMEBUTTON" then
    message send panel name="THEME" info=0 data="^msg.data"
    call updatePanel
    if shareTheme then
      message send plot1 name="RESIZE"  ! resize needed to ensure data area is refreshed
      message send plot2 name="RESIZE"  ! resize needed to ensure data area is refreshed
    endif
    
  elseif msg.name eqs "REFRESHPANEL" then
    call updatePanel
    
  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

!******************************************************************************!
! 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

!******************************************************************************!
! resize and refresh the control panel to make sure it takes the Theme changes
!******************************************************************************!
procedure updatePanel
  invoke ,, reg.panel.resize()
  !invoke ,, reg.panel.refresh)
  invoke ,, reg.panel.refreshAll()
return
