Curses-UI

 view release on metacpan or  search on metacpan

lib/Curses/UI/Dialog/Question.pm  view on Meta::CPAN


@ISA = qw(
    Curses::UI::Window
    Curses::UI::Common
);

$VERSION = '1.00';

sub new ()
{
    my $class = shift;

    my %userargs = @_;
    keys_to_lowercase(\%userargs);

    my %args = (
        -border       => 1,
        -question     => '',        # The question to show
        -answer       => '',        # a default answer
        -ipad         => 1, 
	-fg           => -1,
        -bg           => -1,

        %userargs,

        -titleinverse => 1,
        -centered     => 1,
    );

    # Create a new object, but remember the current
    # screen_too_small setting. The width needed for the
    # buttons can only be computed in the second run
    # of focus() and we do not want the first run to
    # set screen_too_small to a true value because
    # of this.
    #
    my $remember = $Curses::UI::screen_too_small;
    my $this = $class->SUPER::new(%args);

    my $q = $this->add('question', 'TextViewer',
        -x => 1, -y => 0,
        -wrapping    => 1,
        -padbottom   => 0,
        -height      => 3,
        -text        => $this->{-question},
        -bg          => $this->{-bg},
        -fg          => $this->{-fg},
        -bbg         => $this->{-bg},
        -bfg         => $this->{-fg},
	-focusable   => 0,
    );    

    my $a = $this->add('answer', 'TextEntry',
               -x => 1, -y    => 3,
               -border => 1,
               -bg   => $this->{-bg},
               -fg   => $this->{-fg},
               -bbg  => $this->{-bg},
               -bfg  => $this->{-fg},
               -text => $this->{-answer});
    # Push the cursor to the end of the line.
    $a->{-pos} = 999;

    # Create a hash with arguments that may be passed to
    # the Buttonbox class.
    my %buttonargs = (
        -buttonalignment => 'right',
    );
    foreach my $arg (qw(-buttons -selected -buttonalignment)) {
        $buttonargs{$arg} = $this->{$arg} if exists $this->{$arg};
    }
    my $b = $this->add(
       'buttons', 'Buttonbox',
       -y => -1,
       -bg          => $this->{-bg},
       -fg          => $this->{-fg},
       -buttons     => [ 'ok', 'cancel' ],

       %buttonargs
    );

    # Let the window in which the buttons are loose focus
    # if a button is pressed, or if enter is hit in the answer box.
    my $pressed = sub {
        my $this = shift;
        my $parent = $this->parent;
        $parent->{-cancelled} = !$this->get;
        $parent->loose_focus();
    };
    $b->set_routine( 'press-button', $pressed );
    $a->set_binding( $pressed, KEY_ENTER());

    # Restore screen_too_small (see above) and
    # start the second layout pass.
    $Curses::UI::screen_too_small = $remember;
    $this->layout;

    # Set the initial focus to the answer box.
    $a->focus;

    return bless $this, $class;
}

# TODO delete_curses_windows
sub layout()
{
    my $this = shift;
    return $this if $Curses::UI::screen_too_small;

    # The maximum available space on the screen.
    my $avail_width = $ENV{COLS};
    my $avail_height = $ENV{LINES};

    # Compute the maximum available space for the message.

    $this->process_padding;

    my $avail_textwidth  = $avail_width;
    $avail_textwidth  -= 2; # border for the textviewer
    $avail_textwidth  -= 2 if $this->{-border};
    $avail_textwidth  -= $this->{-ipadleft} - $this->{-ipadright};



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