c....	spurion_structmenu.f
c	Routines for the structure intensity command's sub-menu

c=============================================================================

      subroutine load_structmenuitems()
       
      implicit none
      include 'io_files.inc'
      include 'spur_structmenu.inc'

c...	passed variables 
      

      structmenuitem(sbit_bragg) = schar_bragg
      structmenuitem(sbit_num) = schar_num
      structmenuitem(sbit_color) = schar_color
      structmenuitem(sbit_size) = schar_size
      structmenuitem(sbit_uniform) = schar_uniform
      structmenuitem(sbit_label) = schar_label
      structmenuitem(sbit_absences) = schar_absences
      structmenuitem(sbit_logsort) = schar_logsort
      structmenuitem(sbit_tolsort) = schar_tolsort
      structmenuitem(sbit_basesort) = schar_basesort

c...	standard menu items
      structmenuitem(sbit_help) = schar_help
      structmenuitem(sbit_quit) = schar_quit
      
      return
      end

c=============================================================================

      subroutine load_structmenu()
       
      implicit none
      include 'io_files.inc'
      include 'spur_structmenu.inc'

c...	passed variables 
      
      structmenu(sbit_bragg) = 
     & 'Toggle display Bragg intensities' 
      structmenu(sbit_num) = 
     & 'Toggle rep. intensities by circles/numbers' 
      structmenu(sbit_color) = 
     & 'Toggle rep. intensities by color/grey scale' 
      structmenu(sbit_size) = 
     & 'Rel. circle size' 
      structmenu(sbit_uniform) = 
     & 'Toggle circle size uniform or vary with Sf^2' 
      structmenu(sbit_label) = 
     & 'Toggle show/hide sorting labels'
      structmenu(sbit_absences) = 
     & 'Toggle display/hide sysmetatic absences' 

      structmenu(sbit_logsort) = 
     & 'Toggle  bunching sort by S^2= base^n or +- tol' 
      structmenu(sbit_tolsort) = 
     & 'Select sorting tolerance (+- tol)' 
      structmenu(sbit_basesort) = 
     & 'Select sorting base: (base^(n-1) < S^2 < base^n )' 

c...	Standard menu items
      structmenu(sbit_help) = 
     & 'HELP!!'
      structmenu(sbit_quit) =
     & 'to quit'

      
      return
      end

c=============================================================================
      subroutine load_structitemvalue(relcircsize, sorttol, base)
       

      implicit none
      include 'io_files.inc'
      include 'spur_structmenu.inc'
      include 'spur_buf.inc'

c...	functions
      logical bit_on

c...	passed variables
      real	relcircsize		!! in other parts of code = c.
      real	sorttol			!! sorting tolerance (linear scale)
      real	base			!! sorting base (log scale)
       
c...    local variables
      integer i
      
      do i = 1, len_structmenu 
        structitemvalue(i) = ''
      enddo
      
      
       
      if (bit_on(current_structop, sbit_bragg)) then
        write (structitemvalue(sbit_bragg), *) 'Sf^2 not shown'
      else
        write (structitemvalue(sbit_bragg), *) 'Sf^2 shown'
      endif
      
      if (bit_on(current_structop, sbit_num)) then
        write (structitemvalue(sbit_num), *) 'numbers'
      else
        write (structitemvalue(sbit_num), *) 'circles'
      endif
      
      if (bit_on(current_structop, sbit_color)) then
        write (structitemvalue(sbit_color), *) 'color'
      else
        write (structitemvalue(sbit_color), *) 'grey'
      endif
      
      write (structitemvalue(sbit_size), *) abs(relcircsize)

      if (relcircsize .ge. 0.0) then
        write (structitemvalue(sbit_uniform), *) 'uniform'
      else
        write (structitemvalue(sbit_uniform), *) 'varying'
      endif
 
c      if (relcircsize .ge. 0.0) then
c        write (structitemvalue(sbit_size), 90) relcircsize, ' uniform'
c      else
c        write (structitemvalue(sbit_size), 90) 
c     &		abs(relcircsize), ' varying'
c      endif

 90    format( f10.3, a10) 
 
      if (bit_on(current_structop, sbit_label)) then
        write (structitemvalue(sbit_label), *) 'no labels'
      else
        write (structitemvalue(sbit_label), *) 'ABC..cba labels'
      endif
      
      if (bit_on(current_structop, sbit_absences)) then
        write (structitemvalue(sbit_absences), *) 'display absences'
      else
        write (structitemvalue(sbit_absences), *) 'hide absences'
      endif
      
      if (bit_on(current_structop, sbit_logsort)) then
        write (structitemvalue(sbit_logsort), *) 'sort by S^2= base^n'
      else
        write (structitemvalue(sbit_logsort), *) 'sort by +- tol'
      endif
      
      if (bit_on(current_structop, sbit_logsort)) then
        write (structitemvalue(sbit_tolsort), *) ' '
      else
        write (structitemvalue(sbit_tolsort), 90) sorttol, ' = tol'
      endif

      if (bit_on(current_structop, sbit_logsort)) then
        write (structitemvalue(sbit_basesort), 90) base, ' =base'
      else
        write (structitemvalue(sbit_basesort), *) ' '
      endif

      return
      end

c==========================================================================
      subroutine cmd_structmenu(relcircsize, sorttol, base, 
     &	      struct, gray, rainbow, plotunique, sortlogscale,
     &		hide_absences)
     
      implicit none
      include 'arraysize.inc'
      include 'io_files.inc'
      include 'spur_structmenu.inc'

c...	functions
      logical bit_on

c...	passed variables
      real	relcircsize
      real 	sorttol
      real	base
      logical	struct, gray, rainbow, plotunique, sortlogscale
      logical	hide_absences
c...	local variables
      character*5 key
cUnused      integer i
      character*72 aline
      integer newbit, bit_value

      aline = 'Modify display of Bragg intensities'

cNo!      current_structop = 0	! is reset in main(). Not here.
      add_structop = 0
      call set_bit(current_structop,sbit_quit,0) 
      call set_bit(current_structop,sbit_help,0) 

      do while (.not. bit_on(current_structop,sbit_quit)) 
        call load_structitemvalue(relcircsize, sorttol, base)
c02Mar        call load_mod_itemvalue(ec, es, hc, hs, fe, npts, mon)

        call display_submenu(aline, key, current_structop, add_structop,
     &	      len_structmenu, structmenu, structitemvalue, 
     &		structmenuitem, .false.)

	 call which_bit(add_structop,newbit,1)
 220	 if ((newbit .eq. 0) .or. (newbit .gt. len_structmenu)) then
	   write (*,*) 'cmd_structmenu: newbit is out of range ', newbit
	   goto 555
c	 else
c	   write (*,'(a,a)') structmenu(newbit), ' Enter value(s):'
c	   write (*,'(a,a,$)') structmenu(newbit), ' Enter value(s):'
	 endif
	 call toggle_bit(current_structop, newbit, bit_value)

         if (bit_on(add_structop,sbit_bragg) ) then
	   struct = .not. struct
c	   call toggle_bit(current_structop, sbit_bragg, bit_value)
	   
         else if (bit_on(add_structop,sbit_num) ) then
	   gray = .not. gray		!! gray = .true. use greyscale.
	   				!! gray = .false. use numbers
c	  call toggle_bit(current_structop, sbit_num, bit_value)

         else if (bit_on(add_structop,sbit_color) ) then
           rainbow= .not. rainbow	!! color vs. greyscale
c	   call toggle_bit(current_structop, sbit_color, bit_value)

         else if (bit_on(add_structop,sbit_size) ) then
 230	  write (*,'(a,a,$)')  structmenu(sbit_size), ': '
c 230           call display_submenu(aline, key, 
c     &		current_structop, add_structop, len_structmenu, 
c     &		structmenu, structitemvalue, structmenuitem)
	   read(*,*,err=230) relcircsize
	   if (relcircsize .lt. 0.0) goto 230
c	   if (relcircsize .lt. 0.0) then
c	     write (*,*) 'cmd_structmenu: ', 
c     &	     	'rel. circle size must be positive'
c     	     goto 230
c	   endif
c	   call set_bit(current_structop,sbit_size,1)	!! Not toggled

         else if (bit_on(add_structop,sbit_uniform) ) then
	    relcircsize = -1.0 * relcircsize
c	    call toggle_bit(current_structop, sbit_uniform, bit_value)

c         else if (bit_on(add_structop,sbit_size) ) then
c           call display_submenu(aline, key, 
c     &		current_structop, add_structop, len_structmenu, 
c     &		structmenu, structitemvalue, structmenuitem)
c	   read(*,*,err=220) relcircsize

         else if (bit_on(add_structop,sbit_label) ) then
 	   plotunique = .not. plotunique
c	   call toggle_bit(current_structop, sbit_label, bit_value)

         else if (bit_on(add_structop,sbit_absences) ) then
 	   hide_absences = .not. hide_absences
c	   call toggle_bit(current_structop, sbit_absences, bit_value)

         else if (bit_on(add_structop,sbit_logsort) ) then
	   sortlogscale = .not. sortlogscale
c	   call toggle_bit(current_structop, sbit_logsort, bit_value)

         else if (bit_on(add_structop,sbit_tolsort) ) then
	   if (sortlogscale) then
	      write (*,*) 'cmd_structuremenu: Tolerance won''t show'
	   endif
 240  	   write (*,'(a,a,$)')  structmenu(sbit_tolsort), ': '
c           call display_submenu(aline, key, 
c     &		current_structop, add_structop, len_structmenu, 
c     &		structmenu, structitemvalue, structmenuitem)
	   read(*,*,err=240) sorttol
	   if (sorttol .lt. 0.0) goto 240
	   call set_bit(current_structop,sbit_tolsort,1) !! Not toggled directly.

         else if (bit_on(add_structop,sbit_basesort) ) then
	   if (.not. sortlogscale) then
	      write (*,*) 'cmd_structuremenu: Base won''t show'
	   endif
 250  	   write (*,'(a,a,$)')  structmenu(sbit_basesort), ': '
c           call display_submenu(aline, key, 
c     &		current_structop, add_structop, len_structmenu, 
c     &		structmenu, structitemvalue, structmenuitem)
	   read(*,*,err=250) base
	   if (base .lt. 0.0) goto 240
	   call set_bit(current_structop,sbit_basesort,1) !! Not toggled directly.

  
         else if (bit_on(add_structop, sbit_help) ) then 
	   call set_bit(current_structop,sbit_help,1) 
	   call help('', 'spurion', 
     &	   'spurion MainMenu sm_StrucFactorMenu')
         else if (bit_on(add_structop, sbit_quit) ) then 
	   call set_bit(current_structop,sbit_quit,1) 
	 else
	   write (*,*) 'cmd_modify: Unrecognized command: "', key, '"'
         endif


 555    continue
      enddo
      
      return
      end


