Tk-MenuDialog

 view release on metacpan or  search on metacpan

lib/Tk/MenuDialog.pm  view on Meta::CPAN


=head2 show()

=over 2

=item B<Description>

Show the dialog as a new MainWindow.

The function will return if the users cancels the dialog or clicks a button

=item B<Parameters>

NONE

=item B<Return>

UNDEF when canceled, or the hash reference associated with the button clicked.

=back

=cut

##----------------------------------------------------------------------------
sub show
{
  my $self   = shift;
  my $test   = shift;
  my $win;    ## Window widget
  my $result; ## Variable used to capture the result
  my $buttons = [];

  ## Create as a new MainWindow
  $win = MainWindow->new(-title => $self->title);
  
  ## Hide the window
  $win->withdraw;
  
  ## Do not allow user to resize
  $win->resizable(0,0);

  ## Now use the grid geometry manager to layout everything
  $self->_grid_row(0);
  
  ## Insert spacer (if needed)
  $self->_insert_spacer($win);
  
  my $first;
  ## Now add the itmes
  my $number = 0;
  foreach my $item (@{$self->items})
  {
    ## See if the widget was created
    if (my $widget = $self->_build_button($item, $win, $number))
    {
      ## Place the widget
      $widget->grid(
        -row        => $self->_next_row,
        -rowspan    => 1,
        -column     => 1,
        -columnspan => 1,
        -sticky     => qq{nsew},
      );
      
      ## See if button should be disabled
      $widget->configure(-state => qq{disabled}) if ($item->{disabled});

      ## See if this is our first non-disabled field
      $first = $widget if (!$first && !$item->{disabled});
    }
    $number++;
    
    ## Insert spacer (if needed)
    $self->_insert_spacer($win);
  }
  
  $self->_watch_variable(\$result);
  
  ## Setup any keyboard bindings
  $self->_set_key_bindings($win);
  
  ## Calculate the geometry
  $self->_calc_geometry($win);

  ## Display the window
  $win->deiconify;
  
  ## Detect user closing the window
  $win->protocol('WM_DELETE_WINDOW' =>
    sub
    {
      return unless ($self->can_cancel);
      $result = -1;
    });

  ## See if we are testing
  if ($test)
  {
    ## Make sure the string is the correct format
    if ($test =~ /TEST:\s+(-?\d+)/x)
    {
      ## < 0  means CANCEL
      ## >= 0 means select item indicated
      $test = $1;
      
      ## Set a callback to close the window
      $win->after(1500, sub {$result = $test;});
    }
  }

  ## Set the focus to the item
  $first->focus() if ($first);

  ## Wait for variable to change
  $win->waitVariable(\$result);

  ## Hide the window
  $win->withdraw();

  ## See if we have a result
  if (defined($result))



( run in 0.523 second using v1.01-cache-2.11-cpan-39bf76dae61 )