| 1 | |||
| 2 | package DBIx::Informix::Perform::Forms; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | |||
| 6 | use base qw(Curses::Forms); | ||
| 7 | |||
| 8 | use Curses; # for KEY_STAB | ||
| 9 | use Carp; | ||
| 10 | |||
| 11 | # Altered version of execute to let us move backward. | ||
| 12 | sub execute { | ||
| 13 | my $self = shift; | ||
| 14 | my $mwh = shift; | ||
| 15 | my $conf = $self->{CONF}; | ||
| 16 | my @taborder = @{$$conf{TABORDER}}; | ||
| 17 | my $widgets = $self->{WIDGETS}; | ||
| 18 | my $subforms = $self->{SUBFORMS}; | ||
| 19 | my $focused = $$conf{FOCUSED} || $taborder[0] || undef; | ||
| 20 | my %taborder = map {($taborder[$_], $_)} (0..$#taborder); # name look-up | ||
| 21 | my ($i, $obj, $key, $oderived, $dwh, $cwh); | ||
| 22 | $$conf{'REDRAW'} = 0; | ||
| 23 | |||
| 24 | # Create the window if this is not a derived window | ||
| 25 | unless ($$conf{DERIVED}) { | ||
| 26 | $mwh = $self->_formwin; | ||
| 27 | $self->{LEVEL} = 1; | ||
| 28 | |||
| 29 | # Save the old value of DESTORY and temporarily set it to false | ||
| 30 | $oderived = $$conf{DERIVED}; | ||
| 31 | $$conf{DERIVED} = 1; | ||
| 32 | |||
| 33 | # Call ourself again | ||
| 34 | $key = $self->execute($mwh); | ||
| 35 | $self->_relwin(); | ||
| 36 | |||
| 37 | # Restore the DERIVED value and exit | ||
| 38 | $$conf{DERIVED} = $oderived; | ||
| 39 | $self->{LEVEL} = 0; | ||
| 40 | return $key; | ||
| 41 | } | ||
| 42 | |||
| 43 | # Take an early exit if there's nothing listed in the tab order | ||
| 44 | unless ($#taborder > -1) { | ||
| 45 | carp ref($self), ": Must have a widget to give focus to!"; | ||
| 46 | return 0; | ||
| 47 | } | ||
| 48 | |||
| 49 | # Set the EXIT flag to false | ||
| 50 | $$conf{EXIT} = 0; | ||
| 51 | |||
| 52 | # Find the index of the focused widget | ||
| 53 | # $i = 0; | ||
| 54 | # until ($i > $#taborder || $focused eq $taborder[$i]) { | ||
| 55 | # ++$i; | ||
| 56 | # } | ||
| 57 | # $i = 0 if $i > $#taborder; | ||
| 58 | $i = $taborder{$focused}; | ||
| 59 | |||
| 60 | $self->{MWH} = $mwh; | ||
| 61 | $dwh = $self->_canvas($mwh, $self->_geometry); | ||
| 62 | $self->_init($dwh); | ||
| 63 | $cwh = $self->_canvas($dwh, $self->_cgeometry); | ||
| 64 | $cwh->keypad(1); | ||
| 65 | Curses::define_key("\c[[Z", KEY_STAB); # for some reason not recognized. | ||
| 66 | Curses::timeout(250); # quarter-second wait for ESC sequence. | ||
| 67 | Curses::raw(); # catch ctrl-C | ||
| 68 | |||
| 69 | $self->draw($mwh, 1); # draw form at beginning of execute. | ||
| 70 | |||
| 71 | # Start the loop | ||
| 72 | while (1) { | ||
| 73 | |||
| 74 | $obj = exists $$widgets{$taborder[$i]} ? $$widgets{$taborder[$i]} : | ||
| 75 | $$subforms{$taborder[$i]}; | ||
| 76 | $focused = $$conf{FOCUSED} = $taborder[$i]; | ||
| 77 | |||
| 78 | unless (defined $obj) { | ||
| 79 | carp ref($self), ": No such form or widget to focus!"; | ||
| 80 | return 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | # Draw the form if needed | ||
| 84 | if ($$conf{REDRAW}){ | ||
| 85 | $self->draw($mwh, 1); | ||
| 86 | $$conf{REDRAW} = 0; | ||
| 87 | } | ||
| 88 | |||
| 89 | # Call the OnEnter routine if present | ||
| 90 | &{$obj->{OnEnter}}($self) if defined $obj->{OnEnter}; | ||
| 91 | { | ||
| 92 | my $newfocus = $$conf{FOCUSED}; | ||
| 93 | if ($newfocus ne $focused) { | ||
| 94 | # Whoa! Focus warp. DON'T EXECUTE THE FIELD. | ||
| 95 | $i = $taborder{$newfocus}; | ||
| 96 | $focused = $newfocus; | ||
| 97 | next; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | # Execute | ||
| 102 | $key = $obj->execute($cwh); | ||
| 103 | # $self->draw($mwh); | ||
| 104 | |||
| 105 | # Call the OnExit routine if present | ||
| 106 | &{$obj->{OnExit}}($self, $key) if defined $obj->{OnExit}; | ||
| 107 | $obj->draw($cwh); # un-cursor it. | ||
| 108 | |||
| 109 | # Exit if specified | ||
| 110 | last if $$conf{EXIT}; | ||
| 111 | |||
| 112 | { | ||
| 113 | my $newfocus = $$conf{FOCUSED}; | ||
| 114 | if ($newfocus ne $focused) { | ||
| 115 | # Whoa! Focus warp. | ||
| 116 | $i = $taborder{$newfocus}; | ||
| 117 | $focused = $newfocus; | ||
| 118 | } | ||
| 119 | # Otherwise, move the focus where appropriate | ||
| 120 | elsif (!$$conf{DONTSWITCH}) { | ||
| 121 | $i += $key eq KEY_STAB ? -1 : 1; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | $$conf{DONTSWITCH} = 0; | ||
| 125 | $i = 0 if $i > $#taborder; | ||
| 126 | $i = $#taborder if $i < 0; | ||
| 127 | |||
| 128 | # Exit if this is the last widget on a subform | ||
| 129 | # NOT IN PERFORM, WE DON'T. | ||
| 130 | # last if ($$conf{SUBFORM} && $focused eq $taborder[$#taborder]); | ||
| 131 | } | ||
| 132 | |||
| 133 | $cwh->delwin; | ||
| 134 | $dwh->delwin; | ||
| 135 | |||
| 136 | # Reset the EXIT and DESTROY flags | ||
| 137 | $$conf{EXIT} = 0; | ||
| 138 | $$conf{FOCUSED} = $taborder[$i]; | ||
| 139 | |||
| 140 | return $key; | ||
| 141 | } | ||
| 142 | |||
| 143 | 1; |