| 1 | package DBIx::Perform::Widgets::TextField; | ||
| 2 | |||
| 3 | use base 'Curses::Widgets::TextField'; | ||
| 4 | |||
| 5 | use Curses; | ||
| 6 | use Curses::Widgets; | ||
| 7 | |||
| 8 | use constant 'KEY_DEL' => '330'; # dunno why not in Curses. | ||
| 9 | |||
| 10 | use 5.6.0; | ||
| 11 | |||
| 12 | our $OVERWRITE = 1; | ||
| 13 | |||
| 14 | sub input_key { | ||
| 15 | # Process input a keystroke at a time. | ||
| 16 | # | ||
| 17 | # Usage: $self->input_key($key); | ||
| 18 | |||
| 19 | my $self = shift; | ||
| 20 | my $in = shift; | ||
| 21 | my $conf = $self->{CONF}; | ||
| 22 | my $mask = $$conf{MASK}; | ||
| 23 | my ($shift) = grep { $$conf{attrs}{$_} } qw(UPSHIFT DOWNSHIFT); | ||
| 24 | my ($value, $pos, $max, $ro) = | ||
| 25 | @$conf{qw(VALUE CURSORPOS MAXLENGTH READONLY)}; | ||
| 26 | my @string = split(//, $value); | ||
| 27 | |||
| 28 | # Process special keys | ||
| 29 | if ($in eq "\cX") { # ctrl-x = delete char forward | ||
| 30 | return if $ro; | ||
| 31 | if ($pos < length($value)) { | ||
| 32 | splice(@string, $pos, 1); | ||
| 33 | $value = join '', @string; | ||
| 34 | } else { | ||
| 35 | beep; | ||
| 36 | } | ||
| 37 | } elsif ($in eq KEY_RIGHT) { | ||
| 38 | $pos < length($value) ? ++$pos : beep; | ||
| 39 | } elsif ($in eq KEY_LEFT or $in eq KEY_BACKSPACE or $in eq "\cH") { | ||
| 40 | $pos > 0 ? --$pos : beep; | ||
| 41 | } elsif ($in eq KEY_HOME) { | ||
| 42 | $pos = 0; | ||
| 43 | } elsif ($in eq KEY_END) { | ||
| 44 | $pos = length($value); | ||
| 45 | } elsif ($in eq "\cD") { # clear to end of field | ||
| 46 | splice(@string, $pos, $#string-$pos+1); | ||
| 47 | $value = join('', @string); | ||
| 48 | } elsif ($in eq "\cU") { # clear to beginning (not part of Perform) | ||
| 49 | $value = ""; | ||
| 50 | # Process other keys | ||
| 51 | } else { | ||
| 52 | |||
| 53 | return if $ro || $in !~ /^[[:print:]]$/; | ||
| 54 | |||
| 55 | # Exit if it's a non-printing character | ||
| 56 | return unless $in =~ /^[\w\W]$/; | ||
| 57 | |||
| 58 | $in = uc($in) | ||
| 59 | if $shift eq 'UPSHIFT'; | ||
| 60 | $in = lc($in) | ||
| 61 | if $shift eq 'DOWNSHIFT'; | ||
| 62 | |||
| 63 | # Append to the end if the cursor's at the end | ||
| 64 | if ($pos == length($value)) { | ||
| 65 | # Reject if we're already at the max length | ||
| 66 | if (defined $max && length($value) == $max) { | ||
| 67 | beep; | ||
| 68 | return; | ||
| 69 | } | ||
| 70 | $value .= $in; | ||
| 71 | |||
| 72 | # Insert/replace the character at the cursor's position | ||
| 73 | } elsif ($OVERWRITE) { | ||
| 74 | splice(@string, $pos, 1, $in); | ||
| 75 | $value = join('', @string); | ||
| 76 | } elsif ($pos > 0) { | ||
| 77 | @string = (@string[0..($pos - 1)], $in, @string[$pos..$#string]); | ||
| 78 | $value = join('', @string); | ||
| 79 | |||
| 80 | # Insert the character at the beginning of the string | ||
| 81 | } else { | ||
| 82 | $value = "$in$value"; | ||
| 83 | } | ||
| 84 | |||
| 85 | # Increment the cursor's position | ||
| 86 | ++$pos; | ||
| 87 | |||
| 88 | # If just filled up and AUTONEXT is on, exit. | ||
| 89 | $$conf{'EXIT'} = 1 # requires change to execute | ||
| 90 | if (defined $max && length($value) == $max && | ||
| 91 | $$conf{'AUTONEXT'}); | ||
| 92 | } | ||
| 93 | |||
| 94 | # Save the changes | ||
| 95 | @$conf{qw(VALUE CURSORPOS)} = ($value, $pos); | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | # Overriding Curses::Widgets::execute | ||
| 100 | sub execute { | ||
| 101 | my $self = shift; | ||
| 102 | my $mwh = shift; | ||
| 103 | my $conf = $self->{CONF}; | ||
| 104 | my $func = $$conf{'INPUTFUNC'} || \&scankey; | ||
| 105 | my $fskeys = $$conf{'FOCUSSWITCH'}; | ||
| 106 | my $mkeys = $$conf{'FOCUSSWITCH_MACROKEYS'}; | ||
| 107 | my $key; | ||
| 108 | |||
| 109 | $mkeys = [$mkeys] if (defined($mkeys) && ref($mkeys) ne 'ARRAY'); | ||
| 110 | my $regex = $mkeys ? ("([$fskeys]|" . join ('|', @$mkeys) . ")") | ||
| 111 | : "[$fskeys]"; | ||
| 112 | |||
| 113 | $self->draw($mwh, 1); | ||
| 114 | |||
| 115 | while (1) { | ||
| 116 | $key = &$func($mwh); | ||
| 117 | if (defined $key) { | ||
| 118 | if (defined $regex) { | ||
| 119 | return $key if ($key =~ /^$regex/ || ($fskeys =~ /\t/ && | ||
| 120 | $key eq KEY_STAB)); | ||
| 121 | } | ||
| 122 | if ($key eq "\cA") { | ||
| 123 | $OVERWRITE = !$OVERWRITE; | ||
| 124 | #print STDERR "OVERWRITE = '$OVERWRITE'\n"; | ||
| 125 | return $key; | ||
| 126 | } | ||
| 127 | $self->input_key($key); | ||
| 128 | } | ||
| 129 | $self->draw($mwh, 1); | ||
| 130 | last if $$conf{'EXIT'}; # ADDED | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | # Modify this to handle the pos-after-at-last-char case. | ||
| 136 | sub _content { | ||
| 137 | my $self = shift; | ||
| 138 | my $dwh = shift; | ||
| 139 | my $cursor = shift; | ||
| 140 | my $conf = $self->{CONF}; | ||
| 141 | my ($pos, $ts, $value, $border, $col) = | ||
| 142 | @$conf{qw(CURSORPOS TEXTSTART VALUE BORDER COLUMNS)}; | ||
| 143 | my $seg; | ||
| 144 | |||
| 145 | # Trim the value if it exceeds the maximum length | ||
| 146 | $value = substr($value, 0, $$conf{MAXLENGTH}) if $$conf{MAXLENGTH}; | ||
| 147 | |||
| 148 | # Turn on underlining (terminal-dependent) if no border is used | ||
| 149 | $dwh->attron(A_UNDERLINE) unless $border; | ||
| 150 | |||
| 151 | # Adjust the cursor position and text start if it's out of whack | ||
| 152 | if ($pos > length($value)) { | ||
| 153 | $pos = length($value); | ||
| 154 | } elsif ($pos < 0) { | ||
| 155 | $pos = 0; | ||
| 156 | } | ||
| 157 | if ($pos > $ts + $$conf{COLUMNS} - 1) { | ||
| 158 | # $ts = $pos + 1 - $$conf{COLUMNS}; | ||
| 159 | $ts = $pos - $$conf{COLUMNS}; | ||
| 160 | } elsif ($pos < $ts) { | ||
| 161 | $ts = $pos; | ||
| 162 | } | ||
| 163 | $ts = 0 if $ts < 0; | ||
| 164 | |||
| 165 | # Write the widget value (adjusting for horizontal scrolling) | ||
| 166 | $seg = substr($value, $ts, $$conf{COLUMNS}); | ||
| 167 | $seg = '*' x length($seg) if $$conf{PASSWORD}; | ||
| 168 | $seg .= ' ' x ($$conf{COLUMNS} - length($seg)); | ||
| 169 | $dwh->addstr(0, 0, $seg); | ||
| 170 | $dwh->attroff(A_BOLD); | ||
| 171 | |||
| 172 | # Underline the field if no border is used | ||
| 173 | $dwh->chgat(0, 0, $col, A_UNDERLINE, | ||
| 174 | select_colour(@$conf{qw(FOREGROUND BACKGROUND)}), 0) unless $border; | ||
| 175 | |||
| 176 | # Save the textstart, cursorpos, and value in case it was tweaked | ||
| 177 | @$conf{qw(TEXTSTART CURSORPOS VALUE)} = ($ts, $pos, $value); | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | sub _cursor { | ||
| 182 | my $self = shift; | ||
| 183 | my $dwh = shift; | ||
| 184 | my $conf = $self->{CONF}; | ||
| 185 | |||
| 186 | |||
| 187 | # Display the cursor | ||
| 188 | my $cpos = $$conf{CURSORPOS} - $$conf{TEXTSTART}; | ||
| 189 | my $attr = A_STANDOUT; | ||
| 190 | if ($cpos >= $$conf{COLUMNS}) { | ||
| 191 | $cpos--; | ||
| 192 | $attr = A_REVERSE; | ||
| 193 | } | ||
| 194 | $dwh->chgat(0, $cpos, 1, $attr, | ||
| 195 | select_colour(@$conf{qw(FOREGROUND BACKGROUND)}), 0) | ||
| 196 | unless $$conf{READONLY}; | ||
| 197 | |||
| 198 | # Restore the default settings | ||
| 199 | $self->_restore($dwh); | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | 1; | ||
| 204 |