| 1 | # Brenton Chapin | ||
| 2 | package DBIx::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 | our $VERSION = '0.691'; | ||
| 12 | |||
| 13 | sub temp_generate_taborder | ||
| 14 | { | ||
| 15 | my $tbl = shift; | ||
| 16 | my $mode = shift; | ||
| 17 | my $fl = $DBIx::Perform::GlobalUi->get_field_list; | ||
| 18 | my @tabord; | ||
| 19 | |||
| 20 | $fl->reset; | ||
| 21 | while (my $fo = $fl->iterate_list) { | ||
| 22 | my ($ftag, $ftbl, $fcol) = $fo->get_names; | ||
| 23 | if ($ftbl eq $tbl) { | ||
| 24 | next if !$fo->allows_focus($mode); | ||
| 25 | # next if ($fo->{displayonly}); | ||
| 26 | # next if ($fo->{active_tabcol}); | ||
| 27 | # next if ($fo->{noupdate} && $mode eq 'update'); | ||
| 28 | # next if ($fo->{noentry} && $mode eq 'add'); | ||
| 29 | push @tabord, $ftag; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | return @tabord; | ||
| 33 | } | ||
| 34 | |||
| 35 | # Altered version of execute to let us move backward. | ||
| 36 | sub execute { | ||
| 37 | warn "TRACE: entering Forms::execute\n" if $::TRACE; | ||
| 38 | my $self = shift; | ||
| 39 | my $mwh = shift; | ||
| 40 | my $conf = $self->{CONF}; | ||
| 41 | |||
| 42 | my $GlobalUi = $DBIx::Perform::GlobalUi; | ||
| 43 | |||
| 44 | my $app = $GlobalUi->{app_object}; | ||
| 45 | my $fn = $app->getField('form_name'); | ||
| 46 | my $form = $app->getForm($fn); | ||
| 47 | my $subform = $form->getSubform('DBForm'); | ||
| 48 | my $mode = $subform->getField('editmode'); | ||
| 49 | #warn "mode = :$mode:\n"; | ||
| 50 | my $current_table = $GlobalUi->get_current_table_name; | ||
| 51 | my @taborder = temp_generate_taborder($current_table, $mode); | ||
| 52 | |||
| 53 | # Take an early exit if there's nothing listed in the tab order | ||
| 54 | if (@taborder == 0) { | ||
| 55 | $GlobalUi->change_mode_display($subform, 'perform'); | ||
| 56 | return 0; | ||
| 57 | } | ||
| 58 | warn "taborder = ". join (' ',@taborder). "\n" if $::TRACE; | ||
| 59 | |||
| 60 | my $widgets = $self->{WIDGETS}; | ||
| 61 | my $subforms = $self->{SUBFORMS}; | ||
| 62 | my $focused = $$conf{FOCUSED} = | ||
| 63 | $GlobalUi->{focus} || $taborder[0]; | ||
| 64 | my %taborder = map {($taborder[$_], $_)} (0..$#taborder); # name look-up | ||
| 65 | my ($i, $obj, $key, $oderived, $dwh, $cwh); | ||
| 66 | |||
| 67 | # Create the window if this is not a derived window | ||
| 68 | unless ($$conf{DERIVED}) { | ||
| 69 | $mwh = $self->_formwin; | ||
| 70 | $self->{LEVEL} = 1; | ||
| 71 | |||
| 72 | # Save the old value of DESTROY and temporarily set it to false | ||
| 73 | $oderived = $$conf{DERIVED}; | ||
| 74 | $$conf{DERIVED} = 1; | ||
| 75 | |||
| 76 | # Call ourself again | ||
| 77 | $key = $self->execute($mwh); | ||
| 78 | $self->_relwin(); | ||
| 79 | |||
| 80 | # Restore the DERIVED value and exit | ||
| 81 | $$conf{DERIVED} = $oderived; | ||
| 82 | $self->{LEVEL} = 0; | ||
| 83 | return $key; | ||
| 84 | } | ||
| 85 | |||
| 86 | # Set the EXIT flag to false | ||
| 87 | $$conf{EXIT} = 0; | ||
| 88 | |||
| 89 | # Find the index of the focused widget | ||
| 90 | $i = $taborder{$focused}; | ||
| 91 | |||
| 92 | $self->{MWH} = $mwh; | ||
| 93 | $dwh = $self->_canvas($mwh, $self->_geometry); | ||
| 94 | $self->_init($dwh); | ||
| 95 | $cwh = $self->_canvas($dwh, $self->_cgeometry); | ||
| 96 | $cwh->keypad(1); | ||
| 97 | Curses::define_key("\c[[Z", KEY_STAB); # for some reason not recognized. | ||
| 98 | Curses::timeout(250); # quarter-second wait for ESC sequence. | ||
| 99 | Curses::raw(); # catch ctrl-C | ||
| 100 | |||
| 101 | $self->draw($mwh, 1); # draw form at beginning of execute. | ||
| 102 | |||
| 103 | # Start the loop | ||
| 104 | while (1) { | ||
| 105 | |||
| 106 | $obj = exists $$widgets{$taborder[$i]} ? $$widgets{$taborder[$i]} : | ||
| 107 | $$subforms{$taborder[$i]}; | ||
| 108 | $focused = $$conf{FOCUSED} = $GlobalUi->{focus} | ||
| 109 | = $taborder[$i]; | ||
| 110 | #warn "taborder[$i]=" . $taborder[$i] . "\n"; | ||
| 111 | |||
| 112 | unless (defined $obj) { | ||
| 113 | #change to correct screen | ||
| 114 | my $newscrs = DBIx::Perform::get_screen_from_tag($taborder[$i]); | ||
| 115 | my $newscr = $$newscrs[0]; | ||
| 116 | DBIx::Perform::goto_screen("Run$newscr"); | ||
| 117 | $fn = $app->getField('form_name'); | ||
| 118 | $form = $app->getForm($fn); | ||
| 119 | $subform = $form->getSubform('DBForm'); | ||
| 120 | $form->setField('FOCUSED', 'DBForm'); | ||
| 121 | |||
| 122 | my $table = $GlobalUi->get_current_table_name; | ||
| 123 | |||
| 124 | $subform->setField('TABORDER', \@taborder); | ||
| 125 | $subform->setField('FOCUSED', $taborder[$i]); # first field. | ||
| 126 | $subform->setField('editmode', $mode); | ||
| 127 | return $key; | ||
| 128 | } | ||
| 129 | |||
| 130 | warn "enter call :" . $obj->{OnEnter} . "\n" if $::TRACE; | ||
| 131 | # Call the OnEnter routine if present | ||
| 132 | &{$obj->{OnEnter}}($self) if defined $obj->{OnEnter}; | ||
| 133 | $GlobalUi->{newfocus} = '' if $GlobalUi->{newfocus} eq $taborder[$i]; | ||
| 134 | |||
| 135 | if ($app->{redraw_subform}) { | ||
| 136 | $app->{redraw_subform} = 0; | ||
| 137 | DBIx::Perform::UserInterface::redraw_subform(); | ||
| 138 | } | ||
| 139 | |||
| 140 | # Execute | ||
| 141 | $key = $obj->execute($cwh); | ||
| 142 | |||
| 143 | warn "exit call :" . $obj->{OnExit} . "\n" if $::TRACE; | ||
| 144 | # Call the OnExit routine if present | ||
| 145 | &{$obj->{OnExit}}($self, $key) if defined $obj->{OnExit}; | ||
| 146 | warn "returned from exit call\n" if $::TRACE; | ||
| 147 | $obj->draw($cwh); # un-cursor it. | ||
| 148 | |||
| 149 | if ($app->{redraw_subform}) { | ||
| 150 | $app->{redraw_subform} = 0; | ||
| 151 | DBIx::Perform::UserInterface::redraw_subform(); | ||
| 152 | } | ||
| 153 | |||
| 154 | # default is to move the focus to the next field | ||
| 155 | my $newfocus = $GlobalUi->{newfocus}; | ||
| 156 | $GlobalUi->{newfocus} = ''; | ||
| 157 | if ($newfocus) { | ||
| 158 | # Whoa! Focus warp. | ||
| 159 | $i = $taborder{$newfocus}; | ||
| 160 | $focused = $newfocus; | ||
| 161 | } | ||
| 162 | elsif ( $key ne "\c[" ) # ESC: not default | ||
| 163 | { | ||
| 164 | $i++; | ||
| 165 | } | ||
| 166 | |||
| 167 | # Exit if specified | ||
| 168 | last if $$conf{EXIT}; | ||
| 169 | |||
| 170 | $$conf{DONTSWITCH} = 0; | ||
| 171 | $i = 0 if $i > $#taborder; | ||
| 172 | $i = $#taborder if $i < 0; | ||
| 173 | } | ||
| 174 | |||
| 175 | $cwh->delwin; | ||
| 176 | $dwh->delwin; | ||
| 177 | |||
| 178 | # Reset the EXIT and DESTROY flags | ||
| 179 | $$conf{EXIT} = 0; | ||
| 180 | $$conf{FOCUSED} = $GlobalUi->{focus} = $taborder[$i]; | ||
| 181 | |||
| 182 | warn "TRACE: leaving Forms::execute\n" if $::TRACE; | ||
| 183 | return $key; | ||
| 184 | } | ||
| 185 | |||
| 186 | 1; |