| 1 | # Brenton Chapin, Martin Baer et. al ... | ||
| 2 | package DBIx::Perform::Widgets::TextField; | ||
| 3 | |||
| 4 | use base 'Curses::Widgets::TextField'; | ||
| 5 | |||
| 6 | use Curses; | ||
| 7 | use Curses::Widgets; | ||
| 8 | |||
| 9 | use constant 'KEY_DEL' => '330'; # dunno why not in Curses. | ||
| 10 | |||
| 11 | use 5.6.0; | ||
| 12 | |||
| 13 | our $VERSION = '0.695'; | ||
| 14 | |||
| 15 | Curses::define_key( "\c[[Z", KEY_STAB ); # for some reason not recognized. | ||
| 16 | |||
| 17 | our $OVERWRITE = 1; | ||
| 18 | |||
| 19 | our $KEY_ESC = "\c["; | ||
| 20 | our $KEY_RET = "\n"; | ||
| 21 | our $KEY_TAB = "\t"; | ||
| 22 | |||
| 23 | # focus switch table isn't working | ||
| 24 | sub is_FSKEY { | ||
| 25 | my $self = shift; | ||
| 26 | my $in = shift; | ||
| 27 | |||
| 28 | return undef | ||
| 29 | if ( $in ne $KEY_TAB | ||
| 30 | && $in ne $KEY_RET | ||
| 31 | && $in ne $KEY_ESC | ||
| 32 | && $in ne KEY_UP | ||
| 33 | && $in ne KEY_DOWN | ||
| 34 | && $in ne KEY_LEFT | ||
| 35 | && $in ne KEY_RIGHT | ||
| 36 | && $in ne KEY_BACKSPACE | ||
| 37 | && $in ne "\cH" ); | ||
| 38 | |||
| 39 | return 1; | ||
| 40 | } | ||
| 41 | |||
| 42 | # Validates and initialises a new TextField object. | ||
| 43 | # Overriding Curses::Widgets::TextField::_conf | ||
| 44 | sub _conf { | ||
| 45 | |||
| 46 | my $self = shift; | ||
| 47 | my %conf = ( | ||
| 48 | NAME => undef, | ||
| 49 | CAPTION => undef, | ||
| 50 | COLUMNS => 10, | ||
| 51 | LINES => 1, | ||
| 52 | MAXLENGTH => 255, | ||
| 53 | MASK => undef, | ||
| 54 | VALUE => '', | ||
| 55 | INPUTFUNC => \&scankey, | ||
| 56 | BORDER => 1, | ||
| 57 | FOCUSSWITCH => "\t\n", | ||
| 58 | CURSORPOS => 0, | ||
| 59 | TEXTSTART => 0, | ||
| 60 | PASSWORD => 0, | ||
| 61 | READONLY => 0, | ||
| 62 | @_ | ||
| 63 | ); | ||
| 64 | my @required = qw(X Y); | ||
| 65 | my $err = 0; | ||
| 66 | |||
| 67 | # Check for required arguments | ||
| 68 | foreach (@required) { $err = 1 unless exists $conf{$_} } | ||
| 69 | |||
| 70 | # Make sure no errors are returned by the parent method | ||
| 71 | $err = 1 unless $self->SUPER::_conf(%conf); | ||
| 72 | |||
| 73 | return $err == 0 ? 1 : 0; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub input_key { | ||
| 77 | |||
| 78 | # Process input a keystroke at a time. | ||
| 79 | |||
| 80 | my $self = shift; | ||
| 81 | my $in = shift; | ||
| 82 | my $conf = $self->{CONF}; | ||
| 83 | my $mask = $$conf{MASK}; | ||
| 84 | my $tag = $$conf{NAME}; | ||
| 85 | |||
| 86 | my $GlobalUi = $DBIx::Perform::GlobalUi; | ||
| 87 | my $table = $GlobalUi->get_current_table_name; | ||
| 88 | my $fl = $GlobalUi->get_field_list; | ||
| 89 | my $fo = $fl->get_field_object( $table, $tag ); | ||
| 90 | my $form = $GlobalUi->get_current_form; | ||
| 91 | my $subform = $form->getSubform('DBForm'); | ||
| 92 | my $mode = $subform->getField('editmode'); | ||
| 93 | |||
| 94 | $fo->{size} = $$conf{COLUMNS} | ||
| 95 | if !defined $fo->{subscript_ceiling} | ||
| 96 | || !defined $fo->{format} | ||
| 97 | || !defined $fo->{picture}; | ||
| 98 | |||
| 99 | my ($shift) = | ||
| 100 | grep { $$conf{attrs}{$_} } qw(UPSHIFT DOWNSHIFT); | ||
| 101 | my ( $value, $pos, $ro ) = @$conf{qw(VALUE CURSORPOS READONLY)}; | ||
| 102 | |||
| 103 | $max = 80; | ||
| 104 | $max = $fo->{size} if $mode ne 'query'; | ||
| 105 | |||
| 106 | if (defined $fo->{picture}) { | ||
| 107 | my ($pic, $rc) = $fo->do_picture(''); | ||
| 108 | $value .= substr($pic, length($value)); | ||
| 109 | } | ||
| 110 | |||
| 111 | my @string = split( //, $value ); | ||
| 112 | |||
| 113 | # Process special keys | ||
| 114 | if ( $in eq "\cX" ) { # ctrl-x = delete char forward | ||
| 115 | return if $ro; | ||
| 116 | my ($val2, $rc) = $fo->do_picture($value, 1); | ||
| 117 | my $len = length($val2); | ||
| 118 | my $pos2 = $pos; | ||
| 119 | if (defined $fo->{picture}) { | ||
| 120 | my $partpic = substr($fo->{picture}, 0, $pos); | ||
| 121 | $pos2 = $partpic =~ tr/[AX#]//; | ||
| 122 | } | ||
| 123 | @string = split( //, $val2 ); | ||
| 124 | if ( $pos2 < $len && $len > 0 ) { | ||
| 125 | splice( @string, $pos2, 1 ); | ||
| 126 | $val2 = join '', @string; | ||
| 127 | $fo->set_value($val2); | ||
| 128 | ($value, $rc) = $fo->do_picture($val2); | ||
| 129 | } | ||
| 130 | else { | ||
| 131 | beep; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | elsif ( $in eq KEY_UP || $in eq KEY_DOWN ) { | ||
| 135 | $$conf{'EXIT'} = 1; | ||
| 136 | } | ||
| 137 | elsif ( $in eq KEY_RIGHT ) { | ||
| 138 | do { | ||
| 139 | if ( $pos >= length($value) || $pos >= $max-1) { | ||
| 140 | $pos = 0; | ||
| 141 | $$conf{'EXIT'} = 1; | ||
| 142 | } | ||
| 143 | $pos++; | ||
| 144 | } while ($fo->is_picture_char($pos)); | ||
| 145 | } | ||
| 146 | elsif ( $in eq KEY_LEFT or $in eq KEY_BACKSPACE or $in eq "\cH" ) { | ||
| 147 | do { | ||
| 148 | if ( --$pos < 0 ) { | ||
| 149 | $pos = 0; | ||
| 150 | $$conf{'EXIT'} = 1; | ||
| 151 | } | ||
| 152 | } while ($fo->is_picture_char($pos)); | ||
| 153 | } | ||
| 154 | elsif ( $in eq KEY_HOME ) { | ||
| 155 | $pos = 0; | ||
| 156 | while ($fo->is_picture_char($pos)) { $pos++; } | ||
| 157 | } | ||
| 158 | elsif ( $in eq KEY_END ) { | ||
| 159 | $pos = length($value); | ||
| 160 | while ($fo->is_picture_char($pos)) { $pos--; } | ||
| 161 | } | ||
| 162 | elsif ( $in eq "\cD" ) { # clear to end of field | ||
| 163 | splice( @string, $pos, $#string - $pos + 1 ); | ||
| 164 | $value = join( '', @string ); | ||
| 165 | my ($val2, $rc) = $fo->do_picture($value, 1); | ||
| 166 | $fo->set_value($val2); | ||
| 167 | } | ||
| 168 | elsif ( $in eq "\cA" ) { | ||
| 169 | $OVERWRITE = !$OVERWRITE; | ||
| 170 | } | ||
| 171 | else # Process the other keys | ||
| 172 | { | ||
| 173 | return if $ro || $in !~ /^[[:print:]]$/; | ||
| 174 | return if defined $self->is_FSKEY($in); | ||
| 175 | |||
| 176 | # Exit if it's a non-printing character | ||
| 177 | unless ( $in =~ /^[\w\W]$/ ) { | ||
| 178 | $pos = 0; | ||
| 179 | return; | ||
| 180 | } | ||
| 181 | |||
| 182 | if ($$conf{FIRSTKEY} && $fo->is_any_numeric_db_type) { | ||
| 183 | my $pos2 = $pos; | ||
| 184 | if (defined $fo->{picture}) { | ||
| 185 | my $partpic = substr($fo->{picture}, 0, $pos); | ||
| 186 | $pos2 = $partpic =~ tr/[AX#]//; | ||
| 187 | } | ||
| 188 | $value = substr($value, 0, $pos2); | ||
| 189 | $fo->set_value($value); | ||
| 190 | } | ||
| 191 | $$conf{FIRSTKEY} = 0; | ||
| 192 | |||
| 193 | # UPSHIFT / DOWNSHIFT attributes | ||
| 194 | $in = $fo->handle_shift_attributes($in); | ||
| 195 | |||
| 196 | # PICTURE attribute | ||
| 197 | # this is executed here and in onExit (below) | ||
| 198 | my $rc; | ||
| 199 | ( $in, $pos, $rc ) = $fo->handle_picture_attribute( $in, $pos ); | ||
| 200 | if ( $rc != 0 ) { beep; return; } | ||
| 201 | |||
| 202 | # Append to the end if the cursor's at the end | ||
| 203 | my $autonext = $fo->{autonext}; | ||
| 204 | my $vlen = length($value); | ||
| 205 | if ( $pos == $vlen ) { | ||
| 206 | |||
| 207 | # Reject if we're already at the max length | ||
| 208 | if ( $vlen >= $max && !defined $autonext ) { | ||
| 209 | beep; | ||
| 210 | return; | ||
| 211 | } | ||
| 212 | $value .= $in; | ||
| 213 | } | ||
| 214 | elsif ($OVERWRITE) { | ||
| 215 | splice( @string, $pos, 1, $in ); | ||
| 216 | $value = join( '', @string ); | ||
| 217 | } | ||
| 218 | else { #if ( $pos > 0 ) { | ||
| 219 | my ($val2, $rc) = $fo->do_picture($value, 1); | ||
| 220 | @string = split( //, $val2 ); | ||
| 221 | my $pos2 = $pos; | ||
| 222 | if (defined $fo->{picture}) { | ||
| 223 | my $partpic = substr($fo->{picture}, 0, $pos); | ||
| 224 | $pos2 = $partpic =~ tr/[AX#]//; | ||
| 225 | } | ||
| 226 | @string = | ||
| 227 | ( @string[ 0 .. ( $pos2 - 1 ) ], $in, | ||
| 228 | @string[ $pos2 .. $#string ] ); | ||
| 229 | $val2 = join( '', @string ); | ||
| 230 | ($value, $rc) = $fo->do_picture($val2); | ||
| 231 | } | ||
| 232 | my ($val2, $rc) = $fo->do_picture($value, 1); | ||
| 233 | $fo->set_value($val2); | ||
| 234 | |||
| 235 | # Increment the cursor's position | ||
| 236 | #++$pos; | ||
| 237 | my $len = length($value); | ||
| 238 | |||
| 239 | if ( $len >= $max && defined $autonext ) { | ||
| 240 | @$conf{qw(VALUE CURSORPOS)} = ( $value, $pos ); | ||
| 241 | $$conf{'EXIT'} = 1; | ||
| 242 | beep; | ||
| 243 | return; | ||
| 244 | } | ||
| 245 | do { | ||
| 246 | ++$pos; | ||
| 247 | } while ($fo->is_picture_char( $pos )); | ||
| 248 | } | ||
| 249 | |||
| 250 | # Save the changes | ||
| 251 | @$conf{qw(VALUE CURSORPOS)} = ( $value, $pos ); | ||
| 252 | } | ||
| 253 | |||
| 254 | # Overriding Curses::Widgets::execute | ||
| 255 | sub execute { | ||
| 256 | my $self = shift; | ||
| 257 | my $mwh = shift; | ||
| 258 | my $conf = $self->{CONF}; | ||
| 259 | my $func = $$conf{'INPUTFUNC'} || \&scankey; | ||
| 260 | my $fskeys = $$conf{'FOCUSSWITCH'}; | ||
| 261 | my $mkeys = $$conf{'FOCUSSWITCH_MACROKEYS'}; | ||
| 262 | my $key; | ||
| 263 | |||
| 264 | my ( $val, $pos, $rc ); | ||
| 265 | |||
| 266 | warn "TRACE: entering Curses::Widgets::execute\n" if $::TRACE; | ||
| 267 | warn Data::Dumper->Dump( [$self], ['widget_obj'] ) if $::TRACE_DATA; | ||
| 268 | |||
| 269 | $mkeys = [$mkeys] if ( defined($mkeys) && ref($mkeys) ne 'ARRAY' ); | ||
| 270 | my $regex = | ||
| 271 | $mkeys | ||
| 272 | ? ( "([$fskeys]|" . join( '|', @$mkeys ) . ")" ) | ||
| 273 | : "[$fskeys]"; | ||
| 274 | |||
| 275 | $self->draw( $mwh, 1 ); | ||
| 276 | |||
| 277 | $$conf{FIRSTKEY} = 1; | ||
| 278 | |||
| 279 | $key = "\t"; #default keypress, for AUTONEXT | ||
| 280 | while (1) { | ||
| 281 | last if $$conf{'EXIT'}; # ADDED | ||
| 282 | $key = &$func($mwh); | ||
| 283 | if ( defined $key ) { | ||
| 284 | undef $rc; | ||
| 285 | |||
| 286 | # replace with KEY_FSTAB when available | ||
| 287 | if ( defined $self->is_FSKEY($key) ) { | ||
| 288 | $rc = $self->_onExit; | ||
| 289 | } | ||
| 290 | else { $rc = 0; } | ||
| 291 | if ( defined $regex ) { | ||
| 292 | last | ||
| 293 | if ( | ||
| 294 | ( $key =~ /^$regex/ && $rc == 0 ) | ||
| 295 | || ( $fskeys =~ /\t/ | ||
| 296 | && $key eq KEY_STAB | ||
| 297 | && defined $self->is_FSKEY($key) | ||
| 298 | && $rc == 0 ) | ||
| 299 | ); | ||
| 300 | } | ||
| 301 | $self->input_key($key); | ||
| 302 | } | ||
| 303 | my $value = $$conf{VALUE}; | ||
| 304 | if (length($value) > $$conf{COLUMNS}) { | ||
| 305 | $$conf{'EXIT'} = 1; | ||
| 306 | last; | ||
| 307 | } | ||
| 308 | $self->draw( $mwh, 1 ); | ||
| 309 | } | ||
| 310 | |||
| 311 | # $$conf{CURSORPOS} = 0; # removed so 'overflow' field can get cursor pos | ||
| 312 | warn "TRACE: leaving Curses::Widgets::execute\n" if $::TRACE; | ||
| 313 | return $key; | ||
| 314 | } | ||
| 315 | |||
| 316 | sub _content { | ||
| 317 | my $self = shift; | ||
| 318 | my $dwh = shift; | ||
| 319 | my $cursor = shift; | ||
| 320 | my $conf = $self->{CONF}; | ||
| 321 | my $fo = $$conf{FieldObj}; | ||
| 322 | my ( $pos, $ts, $value, $border, $col ) = | ||
| 323 | @$conf{qw(CURSORPOS TEXTSTART VALUE BORDER COLUMNS)}; | ||
| 324 | |||
| 325 | my $seg; | ||
| 326 | |||
| 327 | # Trim the value if it exceeds the maximum length | ||
| 328 | $value = substr( $value, 0, $$conf{MAXLENGTH} ) if $$conf{MAXLENGTH}; | ||
| 329 | |||
| 330 | # Adjust the cursor position and text start if it's out of whack | ||
| 331 | if ( $pos > length($value)+1 ) { | ||
| 332 | $pos = length($value)+1; | ||
| 333 | } | ||
| 334 | elsif ( $pos < 0 ) { | ||
| 335 | $pos = 0; | ||
| 336 | } | ||
| 337 | if ( $pos > $ts + $$conf{COLUMNS} - 1 ) { | ||
| 338 | $ts = $pos - $$conf{COLUMNS}; | ||
| 339 | } | ||
| 340 | elsif ( $pos < $ts ) { | ||
| 341 | $ts = $pos; | ||
| 342 | } | ||
| 343 | $ts = 0 if $ts < 0; | ||
| 344 | |||
| 345 | # Write the widget value (adjusting for horizontal scrolling) | ||
| 346 | $seg = substr( $value, $ts, $$conf{COLUMNS} ); | ||
| 347 | $seg = '*' x length($seg) if $$conf{PASSWORD}; | ||
| 348 | # $seg .= ' ' x ( $$conf{COLUMNS} - length($seg) ); | ||
| 349 | $dwh->addstr( 0, 0, $seg ); | ||
| 350 | $dwh->attroff(A_BOLD); | ||
| 351 | |||
| 352 | # Save the textstart, cursorpos, and value in case it was tweaked | ||
| 353 | @$conf{qw(TEXTSTART CURSORPOS VALUE)} = ( $ts, $pos, $value ); | ||
| 354 | |||
| 355 | } | ||
| 356 | |||
| 357 | sub _cursor { | ||
| 358 | my $self = shift; | ||
| 359 | my $dwh = shift; | ||
| 360 | my $conf = $self->{CONF}; | ||
| 361 | |||
| 362 | # Display the cursor | ||
| 363 | my $cpos = $$conf{CURSORPOS} - $$conf{TEXTSTART}; | ||
| 364 | my $attr = A_STANDOUT; | ||
| 365 | if ( $cpos > $$conf{COLUMNS} ) { | ||
| 366 | $cpos--; | ||
| 367 | $attr = A_REVERSE; | ||
| 368 | } | ||
| 369 | $dwh->chgat( 0, $cpos, 1, $attr, | ||
| 370 | select_colour( @$conf{qw(FOREGROUND BACKGROUND)} ), 0 ) | ||
| 371 | unless $$conf{READONLY}; | ||
| 372 | |||
| 373 | # Restore the default settings | ||
| 374 | $self->_restore($dwh); | ||
| 375 | } | ||
| 376 | |||
| 377 | # This is new - handles runtime behavior of text fields | ||
| 378 | sub _onExit { | ||
| 379 | $self = shift; | ||
| 380 | |||
| 381 | my $conf = $self->{CONF}; | ||
| 382 | my $tag = $$conf{NAME}; | ||
| 383 | my $rc = undef; | ||
| 384 | my $GlobalUi = $DBIx::Perform::GlobalUi; | ||
| 385 | my $table = $GlobalUi->get_current_table_name; | ||
| 386 | my $fl = $GlobalUi->get_field_list; | ||
| 387 | my $fo = $fl->get_field_object( $table, $tag ); | ||
| 388 | my $form = $GlobalUi->get_current_form; | ||
| 389 | my $subform = $form->getSubform('DBForm'); | ||
| 390 | my $mode = $subform->getField('editmode'); | ||
| 391 | |||
| 392 | return unless defined $fo; | ||
| 393 | my ( $value, $pos ) = @$conf{qw(VALUE CURSORPOS )}; | ||
| 394 | ($value, $rc) = $fo->do_picture($value, 1) if $mode !~ /^q/i; | ||
| 395 | $value = $fo->get_value if defined $self->{value}; | ||
| 396 | |||
| 397 | return ( $value, $pos, 0 ) | ||
| 398 | if length($value) == 0; | ||
| 399 | |||
| 400 | # db field may need numeric values | ||
| 401 | # my $need_number = $fo->is_any_numeric_db_type; | ||
| 402 | # my $is_number = $fo->is_number($value); | ||
| 403 | |||
| 404 | # if ( !$is_number && $need_number ) { | ||
| 405 | # $GlobalUi->display_error('er11d'); | ||
| 406 | # return ( $value, $pos, -1 ); | ||
| 407 | # } | ||
| 408 | |||
| 409 | ($value, $rc) = $fo->format_value_for_display( $value ) if $mode !~ /^q/i; | ||
| 410 | $GlobalUi->set_screen_value( $tag, $value); | ||
| 411 | return $rc; | ||
| 412 | } | ||
| 413 | |||
| 414 | 1; | ||
| 415 |