| 1 | # Brenton Chapin, Martin Baer, et. al. | ||
| 2 | package DBIx::Perform::Widgets::ButtonSet; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use base qw(Curses::Widgets::ButtonSet); | ||
| 6 | use Curses; # for KEY_foo constants. | ||
| 7 | use Curses::Widgets; | ||
| 8 | use base 'Exporter'; | ||
| 9 | |||
| 10 | our $VERSION = '0.692'; | ||
| 11 | |||
| 12 | sub _conf { | ||
| 13 | my $self = shift; | ||
| 14 | my %stuff = @_; | ||
| 15 | $stuff{TABORDER} = "\n" | ||
| 16 | unless exists $stuff{TABORDER}; | ||
| 17 | $stuff{ACTIVATEKEY} = "\n"; | ||
| 18 | $self->SUPER::_conf(%stuff); | ||
| 19 | } | ||
| 20 | |||
| 21 | # Process input a keystroke at a time. | ||
| 22 | # Usage: $self->input_key($key); | ||
| 23 | sub input_key { | ||
| 24 | my $self = shift; | ||
| 25 | my $in = shift; | ||
| 26 | my $conf = $self->{CONF}; | ||
| 27 | my ( $value, $hz ) = @$conf{qw(VALUE HORIZONTAL)}; | ||
| 28 | my @labels = @{ $$conf{LABELS} }; | ||
| 29 | my $num = scalar @labels; # 9 or 5 | ||
| 30 | |||
| 31 | # handle single letter shortcuts in the labels | ||
| 32 | my @tmpl1 = @labels; | ||
| 33 | my %char1 = map { ( lc( substr( $tmpl1[$_], 0, 1 ) ), $_ ) } 0 .. $#labels; | ||
| 34 | my $ival = $char1{ lc($in) }; | ||
| 35 | |||
| 36 | if ( defined($ival) ) { # handle shortcut character | ||
| 37 | $value = $ival; | ||
| 38 | $$conf{EXIT} = 1; | ||
| 39 | } | ||
| 40 | elsif ($hz) { | ||
| 41 | if ( $in eq KEY_RIGHT || $in eq ' ' ) { | ||
| 42 | ++$value; | ||
| 43 | $value = 0 if $value >= $num; | ||
| 44 | } | ||
| 45 | elsif ( $in eq KEY_LEFT ) { | ||
| 46 | --$value; | ||
| 47 | $value = ( $num - 1 ) if $value < 0; | ||
| 48 | } | ||
| 49 | elsif ( $in eq KEY_DOWN ) { | ||
| 50 | $value = $$conf{LINEEND}; | ||
| 51 | $value = 0 if $value >= $num; | ||
| 52 | } | ||
| 53 | elsif ( $in eq KEY_UP ) { | ||
| 54 | $value = $$conf{LINEBEGIN}; | ||
| 55 | } | ||
| 56 | elsif ( $in !~ /[\d\cw]/ ) { | ||
| 57 | beep; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | if ( $in eq KEY_UP ) { | ||
| 62 | --$value; | ||
| 63 | $value = ( $num - 1 ) if $value == -1; | ||
| 64 | } | ||
| 65 | elsif ( $in eq KEY_DOWN or $in eq ' ' ) { | ||
| 66 | ++$value; | ||
| 67 | $value = 0 if $value == $num; | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | beep; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | $$conf{VALUE} = $value; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub execute { | ||
| 77 | my $self = shift; | ||
| 78 | my $mwh = shift; | ||
| 79 | my $conf = $self->{CONF}; | ||
| 80 | my $func = $$conf{'INPUTFUNC'} || \&scankey; | ||
| 81 | my $regex = $$conf{'FOCUSSWITCH'}; | ||
| 82 | my $key; | ||
| 83 | |||
| 84 | $self->draw( $mwh, 1 ); | ||
| 85 | |||
| 86 | while (1) { | ||
| 87 | $key = &$func($mwh); | ||
| 88 | |||
| 89 | # change focus | ||
| 90 | if ( defined $key ) { | ||
| 91 | if ( defined $regex ) { | ||
| 92 | return $key | ||
| 93 | if ( $key =~ /^[$regex]/ | ||
| 94 | || ( $regex =~ /\t/ && $key eq KEY_STAB ) ); | ||
| 95 | } | ||
| 96 | $self->input_key($key); | ||
| 97 | } | ||
| 98 | |||
| 99 | return $key if ( ( $key eq KEY_RIGHT ) || ( $key eq KEY_LEFT ) | ||
| 100 | || $key eq ' ' ); | ||
| 101 | return $key if ( $key =~ /[\d\cw]/ ); | ||
| 102 | |||
| 103 | $self->draw( $mwh, 1 ); | ||
| 104 | if ( $conf->{EXIT} ) { | ||
| 105 | $conf->{EXIT} = undef; | ||
| 106 | return $conf->{ACTIVATEKEY}; # pretend we got the "go" key. | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | sub _content { | ||
| 112 | } | ||
| 113 | |||
| 114 | sub _cursor { | ||
| 115 | my $self = shift; | ||
| 116 | my $dwh = shift; | ||
| 117 | my $conf = $self->{CONF}; | ||
| 118 | my @labels = @{ $$conf{LABELS} }; | ||
| 119 | my ( $length, $hz ) = @$conf{qw(LENGTH HORIZONTAL)}; | ||
| 120 | my ( $y, $x ) = ( 0, 0 ); | ||
| 121 | my ($offset); | ||
| 122 | my $i; | ||
| 123 | my $bute; # button end -- last column occupied by button | ||
| 124 | my $b_line = ""; | ||
| 125 | my ( $lw, $lh ); #line width, height | ||
| 126 | |||
| 127 | getmaxyx( $stdscr, $lh, $lw ); | ||
| 128 | |||
| 129 | # Enforce a sane cursor position | ||
| 130 | if ( $$conf{VALUE} >= @labels ) { | ||
| 131 | $$conf{VALUE} = @labels - 1; | ||
| 132 | } | ||
| 133 | elsif ( $$conf{VALUE} < 0 ) { | ||
| 134 | $$conf{VALUE} = 0; | ||
| 135 | } | ||
| 136 | |||
| 137 | # Calculate the cell offset | ||
| 138 | $offset = $$conf{BORDER} ? 1 : ( $$conf{PADDING} ? $$conf{PADDING} : 0 ); | ||
| 139 | |||
| 140 | # Set the coordinates | ||
| 141 | if ($hz) { | ||
| 142 | if ( $$conf{BORDER} ) { | ||
| 143 | $offset = | ||
| 144 | $$conf{VALUE} | ||
| 145 | ? $$conf{VALUE} * $length + $$conf{VALUE} * $offset | ||
| 146 | : 0; | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | $length = length( @labels[ $$conf{VALUE} ] ) + 2; | ||
| 150 | $i = 0; | ||
| 151 | my $lb = 0; | ||
| 152 | $bute = $offset; | ||
| 153 | foreach (@labels) { | ||
| 154 | $bute += length($_) + 2; | ||
| 155 | if ( $bute > $lw - 4 - $$conf{X} ) { | ||
| 156 | if ( $i > $$conf{VALUE} ) { | ||
| 157 | if ( $i < @labels ) { | ||
| 158 | $b_line .= " ..."; | ||
| 159 | } | ||
| 160 | last; | ||
| 161 | } | ||
| 162 | $bute = 7 + length($_); | ||
| 163 | $offset = 5; | ||
| 164 | $b_line = " ... "; | ||
| 165 | $$conf{LINEBEGIN} = $lb; | ||
| 166 | $lb = $i; | ||
| 167 | } | ||
| 168 | $i++; | ||
| 169 | if ( $i <= $$conf{VALUE} ) { | ||
| 170 | $offset = $bute; | ||
| 171 | } | ||
| 172 | $b_line .= ' ' . $_ . ' '; | ||
| 173 | } | ||
| 174 | $$conf{LINEEND} = $i; | ||
| 175 | #FIX: next line is a hack to get around having to run through the entire | ||
| 176 | # list to figure out what LINEBEGIN should really be. Works as long | ||
| 177 | # as the menu <= 2 lines. | ||
| 178 | $$conf{LINEBEGIN} = $i if !$lb; | ||
| 179 | $dwh->addstr( 0, 0, $b_line ); | ||
| 180 | } | ||
| 181 | $x = $offset; | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | $offset = $$conf{VALUE} ? $$conf{VALUE} + $$conf{VALUE} * $offset : 0; | ||
| 185 | $y = $offset; | ||
| 186 | } | ||
| 187 | |||
| 188 | # Display the cursor | ||
| 189 | $dwh->chgat( $y, $x, $length, A_STANDOUT, | ||
| 190 | select_colour( @$conf{qw(FOREGROUND BACKGROUND)} ), 0 ); | ||
| 191 | |||
| 192 | # Restore the default settings | ||
| 193 | $self->_restore($dwh); | ||
| 194 | } | ||
| 195 | |||
| 196 | 1; |