#
#  Menu.pm
#  --generate choice menus from file
#

package Mchange::Menu;

use CSL::Gtk;
use Tie::RefHash;
use Tie::IxHash;


sub new {

  my $class = shift;

  my $self = { };

  my %params = @_;
  
  my $file = $params{file};
  $file = "!!CONFIGREGEX!!/" . $file unless -e $file;
      
  my $menu = $params{menu};

  open( INPUT, "<$file" )
    or die "Couldn't open file $file\n";
  
  $self->{'file'} = $file;
  
  #make references to the menu items we are inserting
  tie %$self, "Tie::RefHash";
  #make foreach( keys ) return the values in insertion order
  tie %$self, "Tie::IxHash";

  while ( my $line = <INPUT> ) {
    chomp $line;
    next unless $line;
    next if $line =~ /^#/;

    my @toks = split /=/, $line;
    
    my $add = new Gtk::MenuItem( $toks[0] );
    $add->show;
    $menu->append( $add );

    #get rid of angry whitespace
    $toks[1] =~ s/\s+$//;
    
    $self->{$add} = $toks[1];
  }

  close INPUT;
  
  return bless $self, $class;
  
}



1;
