<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># ----------------------------------------------------------------------
# Curses::UI::Dialog::Question
#
# (c) 2001-2002 by Luke Closs. All rights reserved.
# This file is part of Curses::UI. Curses::UI is free software.
# You can redistribute it and/or modify it under the same terms
# as perl itself.
# 
# This was mostly copied from Curses::UI::Dialog::Basic
#
# Currently maintained by Marcus Thiesen
# e-mail: marcus@cpan.thiesenweb.de
# ----------------------------------------------------------------------

package Curses::UI::Dialog::Question;

use strict;
use Curses qw(KEY_ENTER);
use Curses::UI::Common;
use Curses::UI::Window;

use vars qw(
    $VERSION
    @ISA
);

@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       =&gt; 1,
        -question     =&gt; '',        # The question to show
        -answer       =&gt; '',        # a default answer
        -ipad         =&gt; 1, 
	-fg           =&gt; -1,
        -bg           =&gt; -1,

        %userargs,

        -titleinverse =&gt; 1,
        -centered     =&gt; 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-&gt;SUPER::new(%args);

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

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

    # Create a hash with arguments that may be passed to
    # the Buttonbox class.
    my %buttonargs = (
        -buttonalignment =&gt; 'right',
    );
    foreach my $arg (qw(-buttons -selected -buttonalignment)) {
        $buttonargs{$arg} = $this-&gt;{$arg} if exists $this-&gt;{$arg};
    }
    my $b = $this-&gt;add(
       'buttons', 'Buttonbox',
       -y =&gt; -1,
       -bg          =&gt; $this-&gt;{-bg},
       -fg          =&gt; $this-&gt;{-fg},
       -buttons     =&gt; [ '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-&gt;parent;
        $parent-&gt;{-cancelled} = !$this-&gt;get;
        $parent-&gt;loose_focus();
    };
    $b-&gt;set_routine( 'press-button', $pressed );
    $a-&gt;set_binding( $pressed, KEY_ENTER());

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

    # Set the initial focus to the answer box.
    $a-&gt;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-&gt;process_padding;

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

    my $avail_textheight = $avail_height;
    $avail_textheight -= 2; # border for the textviewer
    $avail_textheight -= 3; # for answer box
    $avail_textheight -= 2; # empty line and line of buttons
    $avail_textheight -= 2 if $this-&gt;{-border};
    $avail_textheight -= $this-&gt;{-ipadtop} - $this-&gt;{-ipadbottom};

    # Break up the message in separate lines if neccessary.
    my @lines = ();
    foreach (split (/\n/,  $this-&gt;{-question})) {
        push @lines, @{text_wrap($_, $avail_textwidth)};
    }

    # Compute the longest line in the message.
    my $longest_line = 0;
    foreach (@lines) {
        $longest_line = length($_)
            if (length($_) &gt; $longest_line);
    }

    # Compute the width of the buttons (if the buttons
    # object is available. This is not the case just after
    # new() calls SUPER::new()).
    my $buttons = $this-&gt;getobj('buttons');
    my $button_width = 0;
    if (defined $buttons) {
        $button_width = $buttons-&gt;compute_buttonwidth;
    }

    # Decide what is the longest line.
    $longest_line = $button_width if $longest_line &lt; $button_width;

    # Check if there is enough space to show the widget.
    if ($avail_textheight &lt; 1 or $avail_textwidth &lt; $longest_line) {
        $Curses::UI::screen_too_small = 1;
        return $this;
    }

    # Compute the size of the widget.

    my $w = $longest_line;
    $w += 2; # border of textviewer
    $w += 2; # extra width for preventing wrapping of text
    $w += 2 if $this-&gt;{-border};
    $w += $this-&gt;{-ipadleft} + $this-&gt;{-ipadright};

    my $h = @lines;
    $h += 2; # empty line + line of buttons
    $h += 3; # for textentry widget
    $h += 2; # border of textviewer
    $h += 2 if $this-&gt;{-border};
    $h += $this-&gt;{-ipadtop} + $this-&gt;{-ipadbottom};

    $this-&gt;{-width} = $w;
    $this-&gt;{-height} = $h;

    $this-&gt;SUPER::layout;

    return $this;
}

sub get()
{
    my $this = shift;
    return undef if $this-&gt;{-cancelled};
    $this-&gt;getobj('answer')-&gt;get;
}

1;



=head1 NAME

Curses::UI::Dialog::Question - Pose a simple question to the user


=head1 CLASS HIERARCHY

 Curses::UI::Widget
    |
    +----Curses::UI::Container
            |
            +----Curses::UI::Window
                    |
                    +----Curses::UI::Dialog::Question


=head1 SYNOPSIS

    use Curses::UI;
    my $cui = new Curses::UI;
    my $win = $cui-&gt;add('window_id', 'Window');

    # The hard way.
    # -------------
    my $dialog = $win-&gt;add(
        'mydialog', 'Dialog::Question',
        -question   =&gt; 'How super awesome are you?'
    );
    $dialog-&gt;modalfocus;
    $win-&gt;delete('mydialog');

    # The easy way (see Curses::UI documentation).
    # --------------------------------------------
    my $value = $cui-&gt;question(-question =&gt; 'How super awesome are you?');

    # or even
    my $awesomeness = $cui-&gt;question('How super awesome are you?');




=head1 DESCRIPTION

Curses::UI::Dialog::Question is a basic question dialog. This type of
dialog has a message on it, a TextEntry answer box, and one or more 
buttons. It can be used to have a user enter some answer in response
to a question.

See exampes/demo-widgets in the distribution for a short demo.



=head1 OPTIONS

=over 4

=item * B&lt;-title&gt; &lt; TEXT &gt;

Set the title of the dialog window to TEXT.

=item * B&lt;-question&gt; &lt; TEXT &gt;

This option sets the question to show to TEXT. The text may
contain newline (\n) characters.

=item * B&lt;-buttons&gt; &lt; ARRAYREF &gt;

=item * B&lt;-selected&gt; &lt; INDEX &gt;

=item * B&lt;-buttonalignment&gt; &lt; VALUE &gt;

These options sets the buttons that have to be used. For an
explanation of these options, see the
L&lt;Curses::UI::Buttonbox|Curses::UI::Buttonbox&gt; documentation.

=back




=head1 METHODS

=over 4

=item * B&lt;new&gt; ( HASH )

=item * B&lt;layout&gt; ( )

=item * B&lt;draw&gt; ( BOOLEAN )

=item * B&lt;focus&gt; ( )

These are standard methods. See L&lt;Curses::UI::Container|Curses::UI::Container&gt;
for an explanation of these.

=item * B&lt;get&gt; ( )

This method will call B&lt;get&gt; on the TextEntry object of the dialog
and return its returnvalue. See L&lt;Curses::UI::TextEntry&gt; for more 
information on this.  If the cancel button was pressed, the return 
value will be undef.

=back




=head1 SEE ALSO

L&lt;Curses::UI|Curses::UI&gt;,
L&lt;Curses::UI::Container|Curses::UI::Container&gt;,
L&lt;Curses::UI::Buttonbox|Curses::UI::Buttonbox&gt;




=head1 AUTHOR

Copyright (c) 2004 Luke Closs &lt;lukec@activestate.com&gt;. All rights reserved.

Maintained by Marcus Thiesen (marcus@cpan.thiesenweb.de)


This package is free software and is provided "as is" without express
or implied warranty. It may be used, redistributed and/or modified
under the same terms as perl itself.

</pre></body></html>