# -----------------------------------------------------------------------------
# Menu widget for choosing a volume data set.
#
class Volume_Menu:

  def __init__(self, parent, label = '', command = None, open_button = None):

    import Tkinter
    f = Tkinter.Frame(parent)
    self.frame = f

# chimera.widgets.ModelOptionMenu doesn't work because volume surface model
# created before data region added to volume viewer list.
    
    from CGLtk import Hybrid
    vm = Hybrid.Updating_Option_Menu(f, label, named_volumes)
    vm.frame.grid(row = 0, column = 0, sticky = 'w')
    if command:
      vm.add_callback(command)
    self.volume_menu = vm

    if open_button:
      if not type(open_button) is str:
        open_button = 'Browse...'
      bb = Tkinter.Button(f, text = open_button, command = self.open_volume_cb)
      bb.grid(row = 0, column = 1, sticky = 'w')
      
  # ---------------------------------------------------------------------------
  #
  def data_region(self):

    dr = self.volume_menu.value()
    return dr
    
  # ---------------------------------------------------------------------------
  #
  def open_volume_cb(self):

    def open_volume_file(grid_objects):
      # Add data sets to Volume Viewer list
      from VolumeViewer import volume_dialog
      vv = volume_dialog(create = True)
      for go in grid_objects:
        dr = vv.add_region(go)
      # Update color source menu
      if grid_objects:
        vm = self.volume_menu
        vm.update_menu()
        vm.variable.set(dr.full_name())
      
    from VolumeData.opendialog import select_files
    select_files('Open Volume File', open_volume_file)
    
# -----------------------------------------------------------------------------
#
def volume_models():

  from VolumeViewer import volume_dialog
  vv = volume_dialog()
  if vv == None:
    return []

  surfs = map(lambda dr: dr.surface_model(), vv.data_regions)
  surfs = filter(lambda s: s is not None, surfs)
  print vv.data_regions
  print surfs
  return surfs

# -----------------------------------------------------------------------------
#
def named_volumes():

  import VolumeViewer
  d = VolumeViewer.volume_dialog()
  if d == None:
      return []

  name_vol_pairs = map(lambda dr: (dr.full_name(), dr), d.data_regions)
  name_vol_pairs.sort()
  return name_vol_pairs
