| 1 | $Tk::MARC::Editor::VERSION = '1.0'; | ||
| 2 | |||
| 3 | package Tk::MARC::Editor; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Tk::MARC::Editor - a free-form editor for MARC::Record objects | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Tk; | ||
| 12 | use Tk::MARC::Editor; | ||
| 13 | |||
| 14 | # Get a MARC::Record to play with | ||
| 15 | use MARC::File::USMARC; | ||
| 16 | my $file = MARC::File::USMARC->in( "path/to/marc/record/file.mrc" ); | ||
| 17 | my $marc = $file->next(); | ||
| 18 | $file->close(); | ||
| 19 | undef $file; | ||
| 20 | |||
| 21 | my $mw = MainWindow->new; | ||
| 22 | $mw->title("A free-form MARC editor"); | ||
| 23 | |||
| 24 | my $FRAME = $mw->Frame()->pack(-side => 'top'); | ||
| 25 | |||
| 26 | # Create the editor, passing in the record to edit | ||
| 27 | my $ed = $FRAME->Editor(-record => $marc, | ||
| 28 | -background => 'white' | ||
| 29 | )->pack(-side => 'top'); | ||
| 30 | |||
| 31 | # Or better yet, a scrollable editor | ||
| 32 | my $ed = $FRAME->Scrolled('Editor', | ||
| 33 | -scrollbars => 'e', | ||
| 34 | -record => $marc, | ||
| 35 | -background => 'white', | ||
| 36 | )->pack(-side => 'top'); | ||
| 37 | |||
| 38 | # Create a space to write information to | ||
| 39 | my $ln = $FRAME->Text(-background => 'lightgray', -height => 10)->pack(-side => 'top'); | ||
| 40 | |||
| 41 | # ...and some buttons to do neat things | ||
| 42 | my $b1 = $mw->Button( -text => "Get MARC", | ||
| 43 | -command => sub { my $marc = $ed->Contents(); | ||
| 44 | print $marc->as_usmarc(); | ||
| 45 | } | ||
| 46 | )->pack(-side => 'left'); | ||
| 47 | my $b2 = $mw->Button( -text => "Lint", | ||
| 48 | -command => sub { my $s = $ed->Lint(); | ||
| 49 | $ln->Contents( $s ); | ||
| 50 | } | ||
| 51 | )->pack(-side => 'left'); | ||
| 52 | my $b3 = $mw->Button( -text => "Errorchecks", | ||
| 53 | -command => sub { my $s = $ed->Errorchecks(); | ||
| 54 | $ln->Contents( $s ); | ||
| 55 | } | ||
| 56 | )->pack(-side => 'left'); | ||
| 57 | |||
| 58 | # This one shows that the leader gets updated properly: | ||
| 59 | my $b4 = $mw->Button( -text => "Get MARC and reload it", | ||
| 60 | -command => sub { my $marc = $ed->Contents(); | ||
| 61 | $ed->Contents( $marc ); | ||
| 62 | } | ||
| 63 | )->pack(-side => 'left'); | ||
| 64 | |||
| 65 | MainLoop; | ||
| 66 | |||
| 67 | =head1 DESCRIPTION | ||
| 68 | |||
| 69 | Module for editing MARC records as text. It is derived from | ||
| 70 | Tk::Text; the Contents method is overriden to handle MARC::Record | ||
| 71 | objects. | ||
| 72 | |||
| 73 | Right-clicking on the editor will bring up a menu which lets you | ||
| 74 | add fields/subfields (and tells you what they are). | ||
| 75 | |||
| 76 | You can, of course, simply use the [Enter] key to open up a new | ||
| 77 | line, and type in your new field from scratch. When the entry | ||
| 78 | cursor moves off that line, the color formatting is applied. | ||
| 79 | |||
| 80 | You can use the mouse or keyboard to select and delete text as | ||
| 81 | if this were any other text editor (in fact, you can do almost | ||
| 82 | anything that you could do with C<Tk::Text>). | ||
| 83 | |||
| 84 | Only certain elements of the leader are editable (the rest, such | ||
| 85 | as 'record length', are handled internally by C<MARC::Record>. | ||
| 86 | |||
| 87 | There are a couple of example programs in the C<pl> directory. | ||
| 88 | |||
| 89 | =head1 WIDGET-SPECIFIC OPTIONS | ||
| 90 | |||
| 91 | -record => I<marc> | ||
| 92 | |||
| 93 | =over | ||
| 94 | |||
| 95 | Specify the C<MARC::Record> object to edit. Required. You can | ||
| 96 | change the record being edited later using the C<Configure( $record )> | ||
| 97 | method. | ||
| 98 | |||
| 99 | =back | ||
| 100 | |||
| 101 | =head1 INSERT VS. OVERSTRIKE MODE | ||
| 102 | |||
| 103 | =over | ||
| 104 | |||
| 105 | All areas (field designation, indicators, subfield designation, | ||
| 106 | fixed-field data, etc) will always be in Overstrike mode. | ||
| 107 | |||
| 108 | Pressing the I<Insert> key while in a data region (for example, | ||
| 109 | while in the actual words of the title) will toggle between | ||
| 110 | insert and overstrike, I<but only for the data regions>. | ||
| 111 | |||
| 112 | If you are in Insert mode in a data region, any other region | ||
| 113 | is still in Overstrike mode. When you return to a data region, | ||
| 114 | you will be returned to Insert mode (that is, it remembers what | ||
| 115 | mode you were using the last time you were in a data region). | ||
| 116 | |||
| 117 | =back | ||
| 118 | |||
| 119 | =head1 COLOR | ||
| 120 | |||
| 121 | You can completely configure the colour scheme by specifying | ||
| 122 | any combination of the following switches: | ||
| 123 | |||
| 124 | -fieldfg -fieldbg | ||
| 125 | -ind1fg -ind1bg | ||
| 126 | -ind2fg -ind2bg | ||
| 127 | -subfieldfg -subfieldbg | ||
| 128 | -datafg -databg | ||
| 129 | -fixedfg -fixedbg | ||
| 130 | -leaderfg -leaderbg | ||
| 131 | -leadereditfg -leadereditbg | ||
| 132 | |||
| 133 | So, for example, to create the editor to look like one | ||
| 134 | of the old amber-on-black terminals (remember those?), | ||
| 135 | you could do something like this: | ||
| 136 | |||
| 137 | my $ed = $FRAME->Scrolled('Editor', | ||
| 138 | -scrollbars => 'e', | ||
| 139 | -record => $marc, | ||
| 140 | -background => 'black', | ||
| 141 | -leaderbg => 'yellow', | ||
| 142 | -leaderfg => 'black', | ||
| 143 | -fieldbg => 'black', | ||
| 144 | -fieldfg => 'yellow', | ||
| 145 | -ind1bg => 'black', | ||
| 146 | -ind1fg => 'yellow', | ||
| 147 | -ind2bg => 'black', | ||
| 148 | -ind2fg => 'yellow', | ||
| 149 | -subfieldbg => 'yellow', | ||
| 150 | -subfieldfg => 'black', | ||
| 151 | -databg => 'black', | ||
| 152 | -datafg => 'yellow', | ||
| 153 | -fixedbg => 'black', | ||
| 154 | -fixedfg => 'yellow', | ||
| 155 | )->pack(-side => 'top'); | ||
| 156 | |||
| 157 | You can, of course, always use the configure( -attribute => value ); form | ||
| 158 | to deal with colors individually. For example, $ed->configure( -leaderbg => 'darkgreen' ); | ||
| 159 | |||
| 160 | You can also use the C<ColorScheme> method to get or set all the colors at once. | ||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | # Revision history for Tk::MARC::Editor | ||
| 165 | # ------------------------------------- | ||
| 166 | # 1.0 2006.02.24 | ||
| 167 | # - renamed module from Tk::MARC_Editor to Tk::MARC::Editor on the | ||
| 168 | # advice of the kind folks at modules [at] perl.org | ||
| 169 | # 0.9 2006.01.06 | ||
| 170 | # - file dialog problems in the sample app under windows | ||
| 171 | # - shift key acts as a delete key | ||
| 172 | # 0.8 2006.01.04 | ||
| 173 | # - added ColorScheme method to get or set all colors | ||
| 174 | # as a unit. | ||
| 175 | # 0.7 2006.01.04 | ||
| 176 | # - handle configure and cget in the Tk way (using ConfigSpecs) | ||
| 177 | # 0.6 2006.01.04 | ||
| 178 | # - fixed leader bug (allow re-editing of editable fields) | ||
| 179 | # - proper positioning of insertion cursor re: leaderedit tag | ||
| 180 | # 0.5 2005.12.29 | ||
| 181 | # - fixed leader bug (leader was being returned as a 26-character string!) | ||
| 182 | # 0.4 2005.12.22 | ||
| 183 | # - proper event bindings | ||
| 184 | # - specify colors | ||
| 185 | # - POD | ||
| 186 | # 0.3 2005.12.21 | ||
| 187 | # - better leader handling :-) | ||
| 188 | # 0.2 2005.12.20 | ||
| 189 | # - leader handling | ||
| 190 | # 0.1 2005.12.19 | ||
| 191 | # - original version | ||
| 192 | |||
| 193 | use Tk qw(Ev); | ||
| 194 | use Tk::widgets qw(Text); | ||
| 195 | use base qw/Tk::Derived Tk::Text/; | ||
| 196 | use Carp; | ||
| 197 | use MARC::Record; | ||
| 198 | use MARC::Descriptions; | ||
| 199 | use MARC::Lint; | ||
| 200 | use MARC::Errorchecks; | ||
| 201 | use Data::Dumper; | ||
| 202 | use strict; | ||
| 203 | |||
| 204 | Construct Tk::Widget 'Editor'; | ||
| 205 | |||
| 206 | our ( | ||
| 207 | $TD, | ||
| 208 | %pos, | ||
| 209 | %ldrpos, | ||
| 210 | ); | ||
| 211 | |||
| 212 | sub ClassInit { | ||
| 213 | my ($class, $mw) = @_; | ||
| 214 | $TD = MARC::Descriptions->new; | ||
| 215 | %pos = ( field => { start => 0, length => 3 }, | ||
| 216 | ind1 => { start => 4, length => 1 }, | ||
| 217 | ind2 => { start => 5, length => 1 }, | ||
| 218 | subfield => { start => 8, length => 1 }, | ||
| 219 | data => { start => 9, length => undef }, | ||
| 220 | ); | ||
| 221 | %ldrpos = ( "A Logical record length" => { start => 0, length => 5, editable => 0 }, | ||
| 222 | "B Record status" => { start => 5, length => 1, editable => 1 }, | ||
| 223 | "C Type of record" => { start => 6, length => 1, editable => 1 }, | ||
| 224 | "D Bibliographic level" => { start => 7, length => 1, editable => 1 }, | ||
| 225 | "E Type of control" => { start => 8, length => 1, editable => 1 }, | ||
| 226 | "F Character coding scheme" => { start => 9, length => 1, editable => 0 }, | ||
| 227 | "G Indicator count" => { start => 10, length => 1, editable => 0 }, | ||
| 228 | "H Subfield code count" => { start => 11, length => 1, editable => 0 }, | ||
| 229 | "I Base address of data" => { start => 12, length => 5, editable => 0 }, | ||
| 230 | "J Encoding level" => { start => 17, length => 1, editable => 1 }, | ||
| 231 | "K Descriptive cataloging form" => { start => 18, length => 1, editable => 1 }, | ||
| 232 | "L Linked record requirement" => { start => 19, length => 1, editable => 1 }, | ||
| 233 | "M Entry map" => { start => 20, length => 4, editable => 0 }, | ||
| 234 | ); | ||
| 235 | |||
| 236 | $class->SUPER::ClassInit($mw); | ||
| 237 | } | ||
| 238 | |||
| 239 | sub Populate { | ||
| 240 | my ($self, $args) = @_; | ||
| 241 | |||
| 242 | my $record = delete $args->{'-record'}; | ||
| 243 | croak "Missing -record" unless $record; | ||
| 244 | croak "Not a MARC::Record" unless (ref($record) eq "MARC::Record"); | ||
| 245 | |||
| 246 | $self->ConfigSpecs('-fieldfg' => ['METHOD',undef,undef,'darkgreen']); | ||
| 247 | $self->ConfigSpecs('-fieldbg' => ['METHOD',undef,undef,undef]); | ||
| 248 | $self->ConfigSpecs('-ind1fg' => ['METHOD',undef,undef,'yellow']); | ||
| 249 | $self->ConfigSpecs('-ind1bg' => ['METHOD',undef,undef,'darkslategrey']); | ||
| 250 | $self->ConfigSpecs('-ind2fg' => ['METHOD',undef,undef,'yellow']); | ||
| 251 | $self->ConfigSpecs('-ind2bg' => ['METHOD',undef,undef,'darkslategrey']); | ||
| 252 | $self->ConfigSpecs('-subfieldfg' => ['METHOD',undef,undef,'darkgoldenrod']); | ||
| 253 | $self->ConfigSpecs('-subfieldbg' => ['METHOD',undef,undef,undef]); | ||
| 254 | $self->ConfigSpecs('-datafg' => ['METHOD',undef,undef,'blue']); | ||
| 255 | $self->ConfigSpecs('-databg' => ['METHOD',undef,undef,undef]); | ||
| 256 | $self->ConfigSpecs('-fixedfg' => ['METHOD',undef,undef,'black']); | ||
| 257 | $self->ConfigSpecs('-fixedbg' => ['METHOD',undef,undef,undef]); | ||
| 258 | $self->ConfigSpecs('-leaderfg' => ['METHOD',undef,undef,'black']); | ||
| 259 | $self->ConfigSpecs('-leaderbg' => ['METHOD',undef,undef,undef]); | ||
| 260 | $self->ConfigSpecs('-leadereditfg' => ['METHOD',undef,undef,'blanchedalmond']); | ||
| 261 | $self->ConfigSpecs('-leadereditbg' => ['METHOD',undef,undef,'white']); | ||
| 262 | |||
| 263 | $self->SUPER::Populate($args); | ||
| 264 | $self-> bindtags([$self, ref($self), $self->toplevel, 'all']); # take first crack at events | ||
| 265 | $self->_bind_for_record(); | ||
| 266 | $self->OverstrikeMode(1); | ||
| 267 | $self->menu( $self->configure_menu( $self->Menu() )); | ||
| 268 | |||
| 269 | # Set up some default colors | ||
| 270 | my %color = ( "field" => { fg => 'darkgreen', bg => undef }, | ||
| 271 | "ind1" => { fg => 'yellow', bg => 'darkslategrey' }, | ||
| 272 | "ind2" => { fg => 'yellow', bg => 'darkslategrey' }, | ||
| 273 | "subfield" => { fg => 'darkgoldenrod', bg => undef }, | ||
| 274 | "data" => { fg => 'blue', bg => undef }, | ||
| 275 | "fixed" => { fg => 'black', bg => undef }, | ||
| 276 | "leader" => { fg => 'black', bg => 'blanchedalmond' }, | ||
| 277 | "leaderedit" => { fg => 'black', bg => 'white' }, | ||
| 278 | ); | ||
| 279 | $self->{colors} = \%color; | ||
| 280 | foreach my $key (sort keys %color) { | ||
| 281 | foreach my $elem (sort keys %{ $color{$key} }) { | ||
| 282 | $color{$key}{$elem} = delete $args->{"-$key$elem"} if ($args->{"-$key$elem"}); | ||
| 283 | } | ||
| 284 | } | ||
| 285 | foreach my $key (sort keys %color) { | ||
| 286 | $self->tagConfigure($key, | ||
| 287 | 'foreground' => $color{$key}{fg}, | ||
| 288 | 'background' => $color{$key}{bg}, | ||
| 289 | ); | ||
| 290 | } | ||
| 291 | $self->tagConfigure('leaderedit', 'borderwidth' => 1, 'relief' => 'groove'); | ||
| 292 | |||
| 293 | |||
| 294 | # If the user clicks anywhere in the leader, move to the first editable leader field | ||
| 295 | # (and set up for leader-bindings rather than field-bindings) | ||
| 296 | $self->tagBind('leader', '<ButtonRelease>', sub { $self->markSet('insert', | ||
| 297 | #$self->index('leaderedit.first + 1 chars') | ||
| 298 | $self->index('leaderedit.first') | ||
| 299 | ); | ||
| 300 | $self->_bind_for_leader(); | ||
| 301 | $self->OverstrikeMode(1); | ||
| 302 | Tk->break; | ||
| 303 | }); | ||
| 304 | $self->tagBind('field', '<ButtonRelease>', sub { $self->_bind_for_record(); $self->OverstrikeMode(1); }); | ||
| 305 | $self->tagBind('ind1', '<ButtonRelease>', sub { $self->_bind_for_record(); $self->OverstrikeMode(1); }); | ||
| 306 | $self->tagBind('ind2', '<ButtonRelease>', sub { $self->_bind_for_record(); $self->OverstrikeMode(1); }); | ||
| 307 | $self->tagBind('subfield', '<ButtonRelease>', sub { $self->_bind_for_record(); $self->OverstrikeMode(1); }); | ||
| 308 | $self->tagBind('data', '<ButtonRelease>', sub { $self->_bind_for_record(); | ||
| 309 | # set the overstrike mode for data to whatever it | ||
| 310 | # was the last time the user was in data. | ||
| 311 | $self->OverstrikeMode( $self->{dataOverstrikeMode} ); | ||
| 312 | }); | ||
| 313 | $self->tagBind('fixed', '<ButtonRelease>', sub { $self->_bind_for_record(); $self->OverstrikeMode(1); }); | ||
| 314 | |||
| 315 | $self->{menuSubfields} = ""; | ||
| 316 | $self->{dataOverstrikeMode} = 1; | ||
| 317 | $self->Contents( $record ); | ||
| 318 | } | ||
| 319 | |||
| 320 | |||
| 321 | =head1 METHODS | ||
| 322 | |||
| 323 | =head2 Lint() | ||
| 324 | |||
| 325 | =over | ||
| 326 | |||
| 327 | Returns the result of using C<MARC::Lint> on the record being edited, | ||
| 328 | as a string. | ||
| 329 | |||
| 330 | =back | ||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | sub Lint { | ||
| 335 | my $self = shift; | ||
| 336 | |||
| 337 | my $marc = $self->Editor_get(); | ||
| 338 | my $lint = new MARC::Lint; | ||
| 339 | $lint->check_record( $marc ); | ||
| 340 | |||
| 341 | my $s; | ||
| 342 | |||
| 343 | # Print the errors that were found | ||
| 344 | $s .= join( "\n", $lint->warnings ) . "\n"; | ||
| 345 | |||
| 346 | return $s; | ||
| 347 | } | ||
| 348 | |||
| 349 | =head2 Errorchecks() | ||
| 350 | |||
| 351 | =over | ||
| 352 | |||
| 353 | Returns the result of using C<MARC::Errorchecks> on the record being edited, | ||
| 354 | as a string. | ||
| 355 | |||
| 356 | =back | ||
| 357 | |||
| 358 | =cut | ||
| 359 | |||
| 360 | sub Errorchecks { | ||
| 361 | my $self = shift; | ||
| 362 | |||
| 363 | my $marc = $self->Editor_get(); | ||
| 364 | my $s = join( "\n", @{ MARC::Errorchecks::check_all_subs($marc) }) . "\n"; | ||
| 365 | |||
| 366 | return $s; | ||
| 367 | } | ||
| 368 | |||
| 369 | =head2 Contents( I<marc> ) | ||
| 370 | |||
| 371 | =over | ||
| 372 | |||
| 373 | Load the given C<MARC::Record> object into the editor. | ||
| 374 | |||
| 375 | When invoked without a parameter, gets the contents of the editor | ||
| 376 | converted into a C<MARC::Record> object. | ||
| 377 | |||
| 378 | =back | ||
| 379 | |||
| 380 | =cut | ||
| 381 | |||
| 382 | sub Contents { | ||
| 383 | my $self = shift; | ||
| 384 | my ($marc) = @_; | ||
| 385 | |||
| 386 | if ($marc) { | ||
| 387 | return $self->Editor_load( $marc ); | ||
| 388 | } else { | ||
| 389 | return $self->Editor_get(); | ||
| 390 | } | ||
| 391 | } | ||
| 392 | |||
| 393 | =head2 ColorScheme( I<href> ) | ||
| 394 | |||
| 395 | =over | ||
| 396 | |||
| 397 | Set or retrieve the current color scheme. | ||
| 398 | |||
| 399 | When invoked without a parameter, gets the color scheme - | ||
| 400 | a hash of the following form: | ||
| 401 | |||
| 402 | %color = ( "field" => { fg => 'darkgreen', bg => undef }, | ||
| 403 | "ind1" => { fg => 'yellow', bg => 'darkslategrey' }, | ||
| 404 | "ind2" => { fg => 'yellow', bg => 'darkslategrey' }, | ||
| 405 | "subfield" => { fg => 'darkgoldenrod', bg => undef }, | ||
| 406 | "data" => { fg => 'blue', bg => undef }, | ||
| 407 | "fixed" => { fg => 'black', bg => undef }, | ||
| 408 | "leader" => { fg => 'black', bg => 'blanchedalmond' }, | ||
| 409 | "leaderedit" => { fg => 'black', bg => 'white' }, | ||
| 410 | "background" => 'white', | ||
| 411 | ); | ||
| 412 | |||
| 413 | Note that you can always use the C<configure( -attribute => value );> form | ||
| 414 | to deal with colors individually. For example, C<$ed->configure( -leaderbg => 'darkgreen' );> | ||
| 415 | |||
| 416 | =back | ||
| 417 | |||
| 418 | =cut | ||
| 419 | |||
| 420 | sub ColorScheme { | ||
| 421 | my $self = shift; | ||
| 422 | my $href = shift; | ||
| 423 | |||
| 424 | if ($href) { | ||
| 425 | foreach my $key (qw/field ind1 ind2 subfield data fixed leader leaderedit/) { | ||
| 426 | $self->tagConfigure($key, 'foreground' => $href->{$key}{fg}) if ($href->{$key}{fg}); | ||
| 427 | if ($href->{$key}{bg}) { | ||
| 428 | $self->tagConfigure($key, 'background' => $href->{$key}{bg}); | ||
| 429 | } else { | ||
| 430 | $self->tagConfigure($key, 'background' => $href->{background}) if ($href->{background}); | ||
| 431 | } | ||
| 432 | } | ||
| 433 | $self->configure(-background => $href->{background}) if ($href->{background}); | ||
| 434 | } else { | ||
| 435 | foreach my $key (qw/field ind1 ind2 subfield data fixed leader leaderedit/) { | ||
| 436 | $href->{$key}{fg} = $self->tagCget($key, '-foreground'); | ||
| 437 | $href->{$key}{bg} = $self->tagCget($key, '-background'); | ||
| 438 | } | ||
| 439 | $href->{background} = $self->cget('-background'); | ||
| 440 | } | ||
| 441 | return $href; | ||
| 442 | } | ||
| 443 | |||
| 444 | sub _build_leader { | ||
| 445 | my $self = shift; | ||
| 446 | my $line = shift; | ||
| 447 | $line =~ s/^LDR (.*)/$1/; | ||
| 448 | $self->insert('end', "LDR ", 'leader'); | ||
| 449 | foreach my $key (sort keys %ldrpos) { | ||
| 450 | my $s = substr($key, 2) . "["; | ||
| 451 | $self->insert('end', $s, 'leader'); | ||
| 452 | $s = substr($line, $ldrpos{$key}{start}, $ldrpos{$key}{length}); | ||
| 453 | if ($ldrpos{$key}{editable}) { | ||
| 454 | $self->insert('end', $s, 'leaderedit'); | ||
| 455 | } else { | ||
| 456 | $self->insert('end', $s, 'leader'); | ||
| 457 | } | ||
| 458 | $s = "] "; | ||
| 459 | $self->insert('end', $s, 'leader'); | ||
| 460 | } | ||
| 461 | $self->insert('end', "\n", 'leader'); | ||
| 462 | } | ||
| 463 | |||
| 464 | sub _extract_leader { | ||
| 465 | my $self = shift; | ||
| 466 | my $line = shift; | ||
| 467 | my $leader; | ||
| 468 | |||
| 469 | my @elements = split /\]/, $line; | ||
| 470 | foreach my $elem (@elements) { | ||
| 471 | $elem =~ s/^.*\[(.+)/$1/; | ||
| 472 | $leader .= $elem; | ||
| 473 | } | ||
| 474 | $leader = substr($leader,0,24); | ||
| 475 | return $leader; | ||
| 476 | } | ||
| 477 | |||
| 478 | sub Editor_load { | ||
| 479 | my $self = shift; | ||
| 480 | my ($marc) = @_; | ||
| 481 | |||
| 482 | croak "Not a MARC::Record" unless (ref($marc) eq "MARC::Record"); | ||
| 483 | # $self->SUPER::Contents( $marc->as_formatted ); | ||
| 484 | $self->delete('1.0','end'); | ||
| 485 | |||
| 486 | my $s = $marc->as_formatted(); | ||
| 487 | my @lines = split /\n/, $s; | ||
| 488 | |||
| 489 | foreach my $line (@lines) { | ||
| 490 | if ($line =~ /^LDR /) { | ||
| 491 | $self->_build_leader($line); | ||
| 492 | } else { | ||
| 493 | |||
| 494 | $self->insert('end', $line); | ||
| 495 | $self->markSet('insert','end'); | ||
| 496 | $self->_apply_tags(); | ||
| 497 | $self->insert('end', "\n"); | ||
| 498 | } | ||
| 499 | } | ||
| 500 | #print STDERR $self->GetTextTaggedWith('data'); | ||
| 501 | $self->markSet('insert', $self->index('2.0')); | ||
| 502 | } | ||
| 503 | |||
| 504 | sub Editor_get { | ||
| 505 | my $self = shift; | ||
| 506 | my $marc = new MARC::Record; | ||
| 507 | |||
| 508 | my $s = $self->SUPER::Contents(); | ||
| 509 | my @lines = split /\n/, $s; | ||
| 510 | my $fld; | ||
| 511 | my $current_field = ""; | ||
| 512 | my $ind1; | ||
| 513 | my $ind2; | ||
| 514 | my @sfld_data = (); | ||
| 515 | my $leader; | ||
| 516 | |||
| 517 | my $marc_field_as_text = ""; | ||
| 518 | foreach my $line (@lines) { | ||
| 519 | |||
| 520 | my $t_field = substr $line, $pos{field}->{start}, $pos{field}->{length}; | ||
| 521 | my $t_leader; | ||
| 522 | my $t_ind1; | ||
| 523 | my $t_ind2; | ||
| 524 | my $t_subfield; | ||
| 525 | my $t_data; | ||
| 526 | |||
| 527 | if ($t_field =~ /^LDR$/) { | ||
| 528 | # leader | ||
| 529 | $t_leader = $self->_extract_leader($line); | ||
| 530 | #print STDERR "Line : [" . $line . "]\n"; | ||
| 531 | #print STDERR "Leader: [" . $t_leader . "]\n"; | ||
| 532 | #print STDERR "The leader is [" . length($t_leader) . "] characters long\n\n"; | ||
| 533 | $leader = $self->_extract_leader($line); | ||
| 534 | } elsif (($t_field =~ /^\w\w\w/) && ($t_field lt "010")) { | ||
| 535 | # fixed field | ||
| 536 | $t_data = $line; | ||
| 537 | $t_data =~ s/^\w\w\w\s+(\w.*)/$1/; | ||
| 538 | $fld = MARC::Field->new($t_field, $t_data); | ||
| 539 | $marc->append_fields($fld); | ||
| 540 | $fld = undef; | ||
| 541 | } elsif ($t_field =~ /^\w\w\w/) { | ||
| 542 | # finish previous field | ||
| 543 | if ($current_field) { | ||
| 544 | $fld = MARC::Field->new($current_field, $ind1, $ind2, @sfld_data); | ||
| 545 | $marc->append_fields($fld); | ||
| 546 | $fld = undef; | ||
| 547 | @sfld_data = (); | ||
| 548 | } | ||
| 549 | |||
| 550 | # new MARC field | ||
| 551 | $current_field = $t_field; | ||
| 552 | $ind1 = substr $line, $pos{ind1}{start}, $pos{ind1}{length}; | ||
| 553 | $ind2 = substr $line, $pos{ind2}{start}, $pos{ind2}{length}; | ||
| 554 | $t_subfield = substr $line, $pos{subfield}{start}, $pos{subfield}{length}; | ||
| 555 | $t_data = substr $line, $pos{data}{start}; | ||
| 556 | push @sfld_data, ( $t_subfield => $t_data ); | ||
| 557 | } else { | ||
| 558 | # MARC field continuation | ||
| 559 | $t_subfield = substr $line, $pos{subfield}{start}, $pos{subfield}{length}; | ||
| 560 | $t_data = substr $line, $pos{data}{start}; | ||
| 561 | push @sfld_data, ( $t_subfield => $t_data ); | ||
| 562 | } | ||
| 563 | } | ||
| 564 | |||
| 565 | # Don't forget the last field! | ||
| 566 | if ($current_field) { | ||
| 567 | $fld = MARC::Field->new($current_field, $ind1, $ind2, @sfld_data); | ||
| 568 | $marc->append_fields($fld); | ||
| 569 | } | ||
| 570 | |||
| 571 | # now update the leader bits (well, the updatable bits, anyway) | ||
| 572 | $self->_update_leader($leader, $marc); | ||
| 573 | |||
| 574 | #print STDERR $marc->as_usmarc(); | ||
| 575 | #print STDERR "\n" . $marc->as_formatted() . "\n"; | ||
| 576 | |||
| 577 | return $marc; | ||
| 578 | } | ||
| 579 | |||
| 580 | sub _update_leader { | ||
| 581 | my $self = shift; | ||
| 582 | my ($leader, $marc) = @_; | ||
| 583 | |||
| 584 | my $new_leader = $leader; | ||
| 585 | my $blob = $marc->as_usmarc(); | ||
| 586 | # $new_leader = $self->_update_leader_element("A Logical record length", $new_leader, $marc); | ||
| 587 | foreach my $key (sort keys %ldrpos) { | ||
| 588 | # if it is a non-editable leader field, replace the text version with the | ||
| 589 | # 'real' version from the marc blob | ||
| 590 | if (!($ldrpos{$key}{editable})) { | ||
| 591 | $new_leader = $self->_update_leader_element($key, $new_leader, $blob); | ||
| 592 | } | ||
| 593 | } | ||
| 594 | #print STDERR "New leader: [$new_leader]\n"; | ||
| 595 | $marc->leader($new_leader); | ||
| 596 | } | ||
| 597 | |||
| 598 | # Extract a value from the leader in a MARC blob, and put it into a string | ||
| 599 | # in place of the existing value in that string. | ||
| 600 | sub _update_leader_element { | ||
| 601 | my $self = shift; | ||
| 602 | my ($key, $leader, $blob) = @_; | ||
| 603 | |||
| 604 | my $oldval = substr($leader, | ||
| 605 | $ldrpos{$key}{start}, | ||
| 606 | $ldrpos{$key}{length}, | ||
| 607 | substr($blob, | ||
| 608 | $ldrpos{$key}{start}, | ||
| 609 | $ldrpos{$key}{length} | ||
| 610 | ) | ||
| 611 | ); | ||
| 612 | return $leader; | ||
| 613 | } | ||
| 614 | |||
| 615 | sub _find_index_range { | ||
| 616 | my $self = shift; | ||
| 617 | my ($tag, $ref_iStart, $ref_iEnd) = @_; | ||
| 618 | |||
| 619 | my $i1; my $i2; | ||
| 620 | $i1 = "insert linestart + " . $pos{$tag}{start} . " chars"; | ||
| 621 | if (defined $pos{$tag}{length}) { | ||
| 622 | $i2 = "insert linestart + " . ($pos{$tag}{start} + $pos{$tag}{length}) . " chars"; | ||
| 623 | } else { | ||
| 624 | $i2 = "insert lineend"; | ||
| 625 | } | ||
| 626 | |||
| 627 | $$ref_iStart = $self->index($i1); | ||
| 628 | $$ref_iEnd = $self->index($i2); | ||
| 629 | } | ||
| 630 | |||
| 631 | sub _apply_tags { | ||
| 632 | my $self = shift; | ||
| 633 | my $index_insert = $self->index('insert'); | ||
| 634 | my $index_start = $self->index('insert linestart'); | ||
| 635 | my $index_end = $self->index('insert lineend'); | ||
| 636 | |||
| 637 | my $iStart; | ||
| 638 | my $iEnd; | ||
| 639 | |||
| 640 | my @tags = qw( field ind1 ind2 subfield data ); | ||
| 641 | foreach my $tag (@tags) { | ||
| 642 | $self->_find_index_range($tag, \$iStart, \$iEnd); # find proper range | ||
| 643 | $self->tagRemove($tag,$index_start,$index_end); # remove from line | ||
| 644 | my $mfld = $self->get($iStart, $iEnd); | ||
| 645 | if ($mfld =~ /\S/) { # if there's any text, | ||
| 646 | $self->tagAdd($tag, $iStart, $iEnd); # add to proper range | ||
| 647 | if (($tag eq 'field') && ($mfld lt "010")) { # fixed field | ||
| 648 | $self->tagAdd('fixed', | ||
| 649 | $self->index("$iEnd + 5 chars"), | ||
| 650 | $self->index("$iEnd lineend") | ||
| 651 | ); | ||
| 652 | last; # and bail out of the tag processing | ||
| 653 | } | ||
| 654 | } | ||
| 655 | } | ||
| 656 | } | ||
| 657 | |||
| 658 | sub _add_field { | ||
| 659 | my $self = shift; | ||
| 660 | my $field_to_add = shift; | ||
| 661 | |||
| 662 | my $field_num = $field_to_add; | ||
| 663 | $field_num =~ s/^\[(.*)\].*$/$1/; | ||
| 664 | my $description = $field_to_add; | ||
| 665 | $description =~ s/^\[.*\]\s+(.*)$/$1/; | ||
| 666 | |||
| 667 | #print STDERR "Add field [$field_num]\n"; | ||
| 668 | my $marc = $self->Editor_get(); | ||
| 669 | my $field; | ||
| 670 | if ($field_num lt "010") { | ||
| 671 | $field = MARC::Field->new( $field_num, $description ); | ||
| 672 | } else { | ||
| 673 | $field = MARC::Field->new( $field_num, ' ',' ', 'a' => $description ); | ||
| 674 | } | ||
| 675 | $marc->insert_grouped_field( $field ); | ||
| 676 | $self->Editor_load( $marc ); | ||
| 677 | $self->markSet('insert',$self->search(-backwards, | ||
| 678 | -regexp, | ||
| 679 | "^$field_num", | ||
| 680 | $self->index('end') | ||
| 681 | ) | ||
| 682 | ); | ||
| 683 | $self->see($self->index('insert')); | ||
| 684 | } | ||
| 685 | |||
| 686 | sub _add_subfield { | ||
| 687 | my $self = shift; | ||
| 688 | my ($field_num, $subfield, $description, $idxFieldTag) = @_; | ||
| 689 | |||
| 690 | # print STDERR "_add_subfield: Field [$field_num], Subfield [$subfield], Description [$description]\n"; | ||
| 691 | my ($index, $junk) = $self->tagNextrange('field', $idxFieldTag); # this gets the current field | ||
| 692 | ($index, $junk) = $self->tagNextrange('field', "$index + 4 chars"); # this gets the next one, if there is one. | ||
| 693 | if ($index) { | ||
| 694 | $self->markSet('insert', "$index - 1 lines"); | ||
| 695 | $self->markSet('insert', "insert lineend"); | ||
| 696 | } else { # we're dealing with the last field | ||
| 697 | $self->markSet('insert', 'end'); | ||
| 698 | } | ||
| 699 | $self->Insert("\n _$subfield$description"); # and stuff the text in | ||
| 700 | $self->_apply_tags(); | ||
| 701 | $self->{menuSubfields} = undef; | ||
| 702 | } | ||
| 703 | |||
| 704 | sub check_for_type { | ||
| 705 | my $self = shift; | ||
| 706 | my ($type, $index) = @_; | ||
| 707 | my $retval = 0; | ||
| 708 | my @applied_tags = $self->tagNames($index); | ||
| 709 | foreach my $applied (@applied_tags) { | ||
| 710 | next unless ($applied eq $type); | ||
| 711 | $retval = 1; | ||
| 712 | last; | ||
| 713 | } | ||
| 714 | return $retval; | ||
| 715 | } | ||
| 716 | |||
| 717 | |||
| 718 | #----------------------------------------------------------------------------------- | ||
| 719 | # Bindings (subroutines are named by tagname_Event) | ||
| 720 | #----------------------------------------------------------------------------------- | ||
| 721 | sub _bind_for_leader { | ||
| 722 | my $self = shift; | ||
| 723 | |||
| 724 | $self->bind('<Up>' => ""); | ||
| 725 | $self->bind('<Down>' => ""); | ||
| 726 | $self->bind('<Down>' => \&keydown ); | ||
| 727 | $self->bind('<Left>' => ""); | ||
| 728 | $self->bind('<Left>' => \&leader_keyleft); | ||
| 729 | $self->bind('<Right>' => ""); | ||
| 730 | $self->bind('<Right>' => \&leader_keyright); | ||
| 731 | $self->bind('<Insert>' => ""); | ||
| 732 | $self->bind('<Tab>' => \&leader_keyright); | ||
| 733 | $self->bind('<Shift-Tab>' => \&leader_keyleft); | ||
| 734 | $self->bind('<Home>' => \&leader_home); | ||
| 735 | $self->bind('<End>' => \&leader_end); | ||
| 736 | $self->bind('<Delete>' => \&leader_delete); | ||
| 737 | $self->bind('<space>' => \&leader_delete); | ||
| 738 | $self->bind('<KeyPress>' => [\&leader_keypress, Ev('K'), Ev('k') ]); | ||
| 739 | } | ||
| 740 | |||
| 741 | sub _bind_for_record { | ||
| 742 | my $self = shift; | ||
| 743 | |||
| 744 | $self->bind('<Up>' => \&keyup ); | ||
| 745 | $self->bind('<Down>' => \&keydown ); | ||
| 746 | $self->bind('<Left>' => ""); | ||
| 747 | $self->bind('<Left>' => \&keyleft ); | ||
| 748 | $self->bind('<Right>' => ""); | ||
| 749 | $self->bind('<Right>' => \&keyright ); | ||
| 750 | $self->bind('<Insert>' => \&keyinsert ); | ||
| 751 | $self->bind('<Tab>' => ""); | ||
| 752 | $self->bind('<Shift-Tab>' => ""); | ||
| 753 | $self->bind('<Home>' => ""); | ||
| 754 | $self->bind('<End>' => ""); | ||
| 755 | $self->bind('<Delete>' => ""); | ||
| 756 | $self->bind('<space>' => ""); | ||
| 757 | # $self->bind('<KeyPress>' => ""); | ||
| 758 | $self->bind('<KeyPress>' => [\&record_keypress, Ev('K'), Ev('k') ]); | ||
| 759 | } | ||
| 760 | |||
| 761 | sub leader_home { | ||
| 762 | my $self = shift; | ||
| 763 | $self->markSet('insert', $self->index('leaderedit.first')); | ||
| 764 | Tk->break; | ||
| 765 | } | ||
| 766 | |||
| 767 | sub leader_end { | ||
| 768 | my $self = shift; | ||
| 769 | $self->markSet('insert', $self->index('leaderedit.last - 1 chars')); | ||
| 770 | # $self->markSet('insert', $self->index('leaderedit.last')); | ||
| 771 | Tk->break; | ||
| 772 | } | ||
| 773 | |||
| 774 | sub leader_delete { | ||
| 775 | my $self = shift; | ||
| 776 | $self->delete( 'insert' ); | ||
| 777 | $self->insert( 'insert', ' ', 'leaderedit' ); | ||
| 778 | $self->markSet( 'insert', $self->index('insert') . " - 1 chars"); | ||
| 779 | Tk->break; | ||
| 780 | } | ||
| 781 | |||
| 782 | sub leader_keypress { | ||
| 783 | my $self = shift; | ||
| 784 | my ($keysym, $keycode) = @_; | ||
| 785 | |||
| 786 | # The only editable fields are single-character. | ||
| 787 | |||
| 788 | if ($keysym =~ /^\w$/) { | ||
| 789 | $self->delete( 'insert' ); | ||
| 790 | $self->insert( 'insert', $keysym, 'leaderedit' ); | ||
| 791 | $self->markSet( 'insert', $self->index('insert') . " - 1 chars"); | ||
| 792 | $self->leader_keyright(); | ||
| 793 | |||
| 794 | # } else { | ||
| 795 | # print STDERR "keypress: [" . $keysym . "]"; | ||
| 796 | # print STDERR " in " . $self->tagNames('insert') . "\n"; | ||
| 797 | |||
| 798 | } | ||
| 799 | Tk->break; | ||
| 800 | } | ||
| 801 | |||
| 802 | sub leader_keyright { | ||
| 803 | my $self = shift; | ||
| 804 | my ($next_editable, $junk) = $self->tagNextrange('leaderedit', | ||
| 805 | $self->index('insert') . " + 1 chars", | ||
| 806 | $self->index('insert lineend')); | ||
| 807 | $self->markSet('insert', $next_editable) if $next_editable; | ||
| 808 | $self->leader_end() unless $next_editable; | ||
| 809 | Tk->break; | ||
| 810 | } | ||
| 811 | |||
| 812 | sub leader_keyleft { | ||
| 813 | my $self = shift; | ||
| 814 | my ($prev_editable, $junk) = $self->tagPrevrange('leaderedit', | ||
| 815 | $self->index('insert'), | ||
| 816 | $self->index('insert linestart')); | ||
| 817 | $self->markSet('insert', $prev_editable) if $prev_editable; | ||
| 818 | $self->leader_home() unless $prev_editable; | ||
| 819 | Tk->break; | ||
| 820 | } | ||
| 821 | |||
| 822 | sub keyup { | ||
| 823 | my $self = shift; | ||
| 824 | $self->_apply_tags(); | ||
| 825 | $self->markSet('insert', $self->index('insert - 1 lines')); | ||
| 826 | |||
| 827 | if ($self->check_for_type('data','insert')) { | ||
| 828 | $self->OverstrikeMode( $self->{dataOverstrikeMode} ); | ||
| 829 | } else { | ||
| 830 | $self->OverstrikeMode(1); | ||
| 831 | } | ||
| 832 | |||
| 833 | if ($self->check_for_type('leader','insert')) { | ||
| 834 | $self->_bind_for_leader(); | ||
| 835 | # Move to the first editable leader field. | ||
| 836 | $self->markSet('insert', $self->index('leaderedit.first')); | ||
| 837 | } | ||
| 838 | $self->see('insert'); | ||
| 839 | Tk->break; | ||
| 840 | } | ||
| 841 | |||
| 842 | sub keydown { | ||
| 843 | my $self = shift; | ||
| 844 | $self->_apply_tags() unless ($self->check_for_type('leaderedit','insert')); | ||
| 845 | $self->markSet('insert', $self->index('insert + 1 lines')); | ||
| 846 | |||
| 847 | # $self->OverstrikeMode(1) unless ($self->check_for_type('data','insert')); | ||
| 848 | if ($self->check_for_type('data','insert')) { | ||
| 849 | $self->OverstrikeMode( $self->{dataOverstrikeMode} ); | ||
| 850 | } else { | ||
| 851 | $self->OverstrikeMode(1); | ||
| 852 | } | ||
| 853 | |||
| 854 | if (!($self->check_for_type('leader','insert'))) { | ||
| 855 | $self->_bind_for_record(); | ||
| 856 | } | ||
| 857 | $self->see('insert'); | ||
| 858 | Tk->break; | ||
| 859 | } | ||
| 860 | |||
| 861 | sub keyleft { | ||
| 862 | my $self = shift; | ||
| 863 | $self->_apply_tags(); | ||
| 864 | $self->markSet('insert', $self->index('insert - 1 chars')); | ||
| 865 | |||
| 866 | # $self->OverstrikeMode(1) unless ($self->check_for_type('data','insert')); | ||
| 867 | if ($self->check_for_type('data','insert')) { | ||
| 868 | $self->OverstrikeMode( $self->{dataOverstrikeMode} ); | ||
| 869 | } else { | ||
| 870 | $self->OverstrikeMode(1); | ||
| 871 | } | ||
| 872 | |||
| 873 | if ($self->check_for_type('leader','insert')) { | ||
| 874 | $self->_bind_for_leader(); | ||
| 875 | } | ||
| 876 | $self->see('insert'); | ||
| 877 | Tk->break; | ||
| 878 | } | ||
| 879 | |||
| 880 | sub keyright { | ||
| 881 | my $self = shift; | ||
| 882 | $self->_apply_tags(); | ||
| 883 | $self->markSet('insert', $self->index('insert + 1 chars')); | ||
| 884 | |||
| 885 | # $self->OverstrikeMode(1) unless ($self->check_for_type('data','insert')); | ||
| 886 | if ($self->check_for_type('data','insert')) { | ||
| 887 | $self->OverstrikeMode( $self->{dataOverstrikeMode} ); | ||
| 888 | } else { | ||
| 889 | $self->OverstrikeMode(1); | ||
| 890 | } | ||
| 891 | |||
| 892 | if (!($self->check_for_type('leader','insert'))) { | ||
| 893 | $self->_bind_for_record(); | ||
| 894 | } | ||
| 895 | $self->see('insert'); | ||
| 896 | Tk->break; | ||
| 897 | } | ||
| 898 | |||
| 899 | sub keyinsert { | ||
| 900 | my $self = shift; | ||
| 901 | # Can only go into Insert mode in 'data' tag | ||
| 902 | if ($self->check_for_type('data','insert')) { | ||
| 903 | if ( $self->{dataOverstrikeMode} ) { | ||
| 904 | $self->{dataOverstrikeMode} = 0; | ||
| 905 | } else { | ||
| 906 | $self->{dataOverstrikeMode} = 1; | ||
| 907 | } | ||
| 908 | $self->OverstrikeMode($self->{dataOverstrikeMode}); | ||
| 909 | } else { | ||
| 910 | $self->OverstrikeMode(1); | ||
| 911 | } | ||
| 912 | Tk->break; | ||
| 913 | } | ||
| 914 | |||
| 915 | sub record_keypress { | ||
| 916 | my $self = shift; | ||
| 917 | my ($keysym, $keycode) = @_; | ||
| 918 | |||
| 919 | # print STDERR "keypress: [" . $keysym . "]"; | ||
| 920 | # print STDERR " in " . $self->tagNames('insert') . "\n"; | ||
| 921 | |||
| 922 | # Handle the Shift-acts-as-delete problem. | ||
| 923 | if ($keysym =~ /^Shift_L$/) { | ||
| 924 | Tk->break; | ||
| 925 | } | ||
| 926 | } | ||
| 927 | |||
| 928 | |||
| 929 | #----------------------------------------------------------------------------------- | ||
| 930 | # "Menu" configuration (MARC field and subfield names) | ||
| 931 | #----------------------------------------------------------------------------------- | ||
| 932 | sub configure_menu { | ||
| 933 | my $self = shift; | ||
| 934 | my $menu = shift; | ||
| 935 | |||
| 936 | my $mFieldList = $menu->Menu(); | ||
| 937 | |||
| 938 | my $submenu_0XX = $menu->Menu(); | ||
| 939 | $self->_add_submenu_items($submenu_0XX,"010","099"); | ||
| 940 | $mFieldList->add("cascade", -label => "0XX", -menu => $submenu_0XX); | ||
| 941 | |||
| 942 | my $submenu_1XX = $menu->Menu(); | ||
| 943 | $self->_add_submenu_items($submenu_1XX,"100","199"); | ||
| 944 | $mFieldList->add("cascade", -label => "1XX", -menu => $submenu_1XX); | ||
| 945 | |||
| 946 | my $submenu_2XX = $menu->Menu(); | ||
| 947 | $self->_add_submenu_items($submenu_2XX,"200","299"); | ||
| 948 | $mFieldList->add("cascade", -label => "2XX", -menu => $submenu_2XX); | ||
| 949 | |||
| 950 | my $submenu_3XX = $menu->Menu(); | ||
| 951 | $self->_add_submenu_items($submenu_3XX,"300","399"); | ||
| 952 | $mFieldList->add("cascade", -label => "3XX", -menu => $submenu_3XX); | ||
| 953 | |||
| 954 | my $submenu_4XX = $menu->Menu(); | ||
| 955 | $self->_add_submenu_items($submenu_4XX,"400","499"); | ||
| 956 | $mFieldList->add("cascade", -label => "4XX", -menu => $submenu_4XX); | ||
| 957 | |||
| 958 | my $submenu_5XX = $menu->Menu(); | ||
| 959 | $self->_add_submenu_items($submenu_5XX,"500","599"); | ||
| 960 | $mFieldList->add("cascade", -label => "5XX", -menu => $submenu_5XX); | ||
| 961 | |||
| 962 | my $submenu_6XX = $menu->Menu(); | ||
| 963 | $self->_add_submenu_items($submenu_6XX,"600","699"); | ||
| 964 | $mFieldList->add("cascade", -label => "6XX", -menu => $submenu_6XX); | ||
| 965 | |||
| 966 | my $submenu_7XX = $menu->Menu(); | ||
| 967 | $self->_add_submenu_items($submenu_7XX,"700","799"); | ||
| 968 | $mFieldList->add("cascade", -label => "7XX", -menu => $submenu_7XX); | ||
| 969 | |||
| 970 | my $submenu_8XX = $menu->Menu(); | ||
| 971 | $self->_add_submenu_items($submenu_8XX,"800","899"); | ||
| 972 | $mFieldList->add("cascade", -label => "8XX", -menu => $submenu_8XX); | ||
| 973 | |||
| 974 | my $submenu_9XX = $menu->Menu(); | ||
| 975 | $self->_add_submenu_items($submenu_9XX,"900","999"); | ||
| 976 | $mFieldList->add("cascade", -label => "9XX", -menu => $submenu_9XX); | ||
| 977 | |||
| 978 | $menu->add("cascade", | ||
| 979 | -label => 'Fields Reference', | ||
| 980 | -menu => $mFieldList, | ||
| 981 | ); | ||
| 982 | |||
| 983 | |||
| 984 | |||
| 985 | $menu->add("command", | ||
| 986 | -label => 'Subfields Reference', | ||
| 987 | -command => sub {my $idxFieldTag; | ||
| 988 | my $junk; | ||
| 989 | if ($self->index('insert') == $self->index('insert linestart')) { | ||
| 990 | $idxFieldTag = $self->index('insert'); | ||
| 991 | } else { | ||
| 992 | ($idxFieldTag, $junk) = $self->tagPrevrange('field', | ||
| 993 | $self->index('insert')); | ||
| 994 | } | ||
| 995 | my $field = $self->get($idxFieldTag, | ||
| 996 | "$idxFieldTag + 3 chars" | ||
| 997 | ); | ||
| 998 | |||
| 999 | #print STDERR "Invoking popup for field [$field]\n"; | ||
| 1000 | |||
| 1001 | $self->{menuSubfields} = $menu->Menu(); | ||
| 1002 | my $href = $TD->get($field, "subfields"); | ||
| 1003 | #print Dumper($href); | ||
| 1004 | |||
| 1005 | # MARC::Descriptions is going through some changes to support localization. | ||
| 1006 | # Check if the version we have supports different languages or not. | ||
| 1007 | my ($k,@otherstuff) = keys %$href; # get a single key to test against | ||
| 1008 | |||
| 1009 | if (ref($href->{$k}{description}) eq "HASH") { | ||
| 1010 | if (exists $href->{$k}{description}{eng}) { | ||
| 1011 | foreach my $subfield (sort keys %$href) { | ||
| 1012 | my $s = "[" . $subfield . "] " . $href->{$subfield}{description}{eng}; | ||
| 1013 | #print STDERR "$s\n"; | ||
| 1014 | $self->{menuSubfields}->add('command', | ||
| 1015 | -label => $s, | ||
| 1016 | -command => sub { | ||
| 1017 | $self->_add_subfield($field, | ||
| 1018 | $subfield, | ||
| 1019 | $href->{$subfield}{description}{eng}, | ||
| 1020 | $idxFieldTag, | ||
| 1021 | ); | ||
| 1022 | } | ||
| 1023 | ); | ||
| 1024 | } | ||
| 1025 | } | ||
| 1026 | } else { | ||
| 1027 | foreach my $subfield (sort keys %$href) { | ||
| 1028 | my $s = "[" . $subfield . "] " . $href->{$subfield}{description}; | ||
| 1029 | #print STDERR "$s\n"; | ||
| 1030 | $self->{menuSubfields}->add('command', | ||
| 1031 | -label => $s, | ||
| 1032 | -command => sub { | ||
| 1033 | $self->_add_subfield($field, | ||
| 1034 | $subfield, | ||
| 1035 | $href->{$subfield}{description}, | ||
| 1036 | $idxFieldTag, | ||
| 1037 | ); | ||
| 1038 | } | ||
| 1039 | ); | ||
| 1040 | } | ||
| 1041 | } | ||
| 1042 | $self->{menuSubfields}->Popup(qw/-popover cursor/); | ||
| 1043 | #print STDERR "Done popup\n"; | ||
| 1044 | } | ||
| 1045 | ); | ||
| 1046 | |||
| 1047 | return $menu; | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | |||
| 1051 | sub _add_submenu_items { | ||
| 1052 | my $self = shift; | ||
| 1053 | my ($sm, $first, $last) = @_; | ||
| 1054 | |||
| 1055 | my @submenu_items = (); | ||
| 1056 | |||
| 1057 | for (my $i = $first; $i <= $last; $i++) { | ||
| 1058 | my $tag = sprintf("%03s",$i); | ||
| 1059 | my $s = $TD->get($tag,"description"); | ||
| 1060 | if ($s) { | ||
| 1061 | push @submenu_items, "[$tag] $s"; | ||
| 1062 | } | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | foreach my $item (@submenu_items) { | ||
| 1066 | $sm->add('command', | ||
| 1067 | -label => $item, | ||
| 1068 | -command => sub { | ||
| 1069 | #my $field = $item; | ||
| 1070 | #$self->_add_field($field); | ||
| 1071 | $self->_add_field($item); | ||
| 1072 | } | ||
| 1073 | ); | ||
| 1074 | } | ||
| 1075 | } | ||
| 1076 | |||
| 1077 | #-------------------------------------------------------------------- | ||
| 1078 | # ConfigSpecs methods | ||
| 1079 | #-------------------------------------------------------------------- | ||
| 1080 | sub fieldfg { | ||
| 1081 | my ($self, $value) = @_; | ||
| 1082 | $self->tagConfigure('field','foreground' => $value) if (@_ > 1); | ||
| 1083 | # return $self->cget( '-fieldfg' ); | ||
| 1084 | } | ||
| 1085 | sub fieldbg { | ||
| 1086 | my ($self, $value) = @_; | ||
| 1087 | $self->tagConfigure('field','background' => $value) if (@_ > 1); | ||
| 1088 | # return $self->cget( '-fieldbg' ); | ||
| 1089 | } | ||
| 1090 | sub ind1fg { | ||
| 1091 | my ($self, $value) = @_; | ||
| 1092 | $self->tagConfigure('ind1','foreground' => $value) if (@_ > 1); | ||
| 1093 | # return $self->cget( '-ind1fg' ); | ||
| 1094 | } | ||
| 1095 | sub ind1bg { | ||
| 1096 | my ($self, $value) = @_; | ||
| 1097 | $self->tagConfigure('ind1','background' => $value) if (@_ > 1); | ||
| 1098 | # return $self->cget( '-ind1bg' ); | ||
| 1099 | } | ||
| 1100 | sub ind2fg { | ||
| 1101 | my ($self, $value) = @_; | ||
| 1102 | $self->tagConfigure('ind2','foreground' => $value) if (@_ > 1); | ||
| 1103 | # return $self->cget( '-ind2fg' ); | ||
| 1104 | } | ||
| 1105 | sub ind2bg { | ||
| 1106 | my ($self, $value) = @_; | ||
| 1107 | $self->tagConfigure('ind2','background' => $value) if (@_ > 1); | ||
| 1108 | # return $self->cget( '-ind2bg' ); | ||
| 1109 | } | ||
| 1110 | sub subfieldfg { | ||
| 1111 | my ($self, $value) = @_; | ||
| 1112 | $self->tagConfigure('subfield','foreground' => $value) if (@_ > 1); | ||
| 1113 | # return $self->cget( '-subfieldfg' ); | ||
| 1114 | } | ||
| 1115 | sub subfieldbg { | ||
| 1116 | my ($self, $value) = @_; | ||
| 1117 | $self->tagConfigure('subfield','background' => $value) if (@_ > 1); | ||
| 1118 | # return $self->cget( '-subfieldbg' ); | ||
| 1119 | } | ||
| 1120 | sub datafg { | ||
| 1121 | my ($self, $value) = @_; | ||
| 1122 | $self->tagConfigure('data','foreground' => $value) if (@_ > 1); | ||
| 1123 | # return $self->cget( '-datafg' ); | ||
| 1124 | } | ||
| 1125 | sub databg { | ||
| 1126 | my ($self, $value) = @_; | ||
| 1127 | $self->tagConfigure('data','background' => $value) if (@_ > 1); | ||
| 1128 | # return $self->cget( '-databg' ); | ||
| 1129 | } | ||
| 1130 | sub fixedfg { | ||
| 1131 | my ($self, $value) = @_; | ||
| 1132 | $self->tagConfigure('fixed','foreground' => $value) if (@_ > 1); | ||
| 1133 | # return $self->cget( '-fixedfg' ); | ||
| 1134 | } | ||
| 1135 | sub fixedbg { | ||
| 1136 | my ($self, $value) = @_; | ||
| 1137 | $self->tagConfigure('fixed','background' => $value) if (@_ > 1); | ||
| 1138 | # return $self->cget( '-fixedbg' ); | ||
| 1139 | } | ||
| 1140 | sub leaderfg { | ||
| 1141 | my ($self, $value) = @_; | ||
| 1142 | $self->tagConfigure('leader','foreground' => $value) if (@_ > 1); | ||
| 1143 | # return $self->cget( '-leaderfg' ); | ||
| 1144 | } | ||
| 1145 | sub leaderbg { | ||
| 1146 | my ($self, $value) = @_; | ||
| 1147 | $self->tagConfigure('leader','background' => $value) if (@_ > 1); | ||
| 1148 | # return $self->cget( '-leaderbg' ); | ||
| 1149 | } | ||
| 1150 | sub leadereditfg { | ||
| 1151 | my ($self, $value) = @_; | ||
| 1152 | $self->tagConfigure('leaderedit','foreground' => $value) if (@_ > 1); | ||
| 1153 | # return $self->cget( '-leadereditfg' ); | ||
| 1154 | } | ||
| 1155 | sub leadereditbg { | ||
| 1156 | my ($self, $value) = @_; | ||
| 1157 | $self->tagConfigure('leaderedit','background' => $value) if (@_ > 1); | ||
| 1158 | # return $self->cget( '-leadereditbg' ); | ||
| 1159 | } | ||
| 1160 | |||
| 1161 | =head1 REQUIREMENTS | ||
| 1162 | |||
| 1163 | Tk::MARC::Editor requires MARC::Record, MARC::Descriptions, | ||
| 1164 | MARC::Lint, and MARC::Errorchecks | ||
| 1165 | |||
| 1166 | =head1 SEE ALSO | ||
| 1167 | |||
| 1168 | =over | ||
| 1169 | |||
| 1170 | =item * perl4lib (L<http://perl4lib.perl.org>) | ||
| 1171 | |||
| 1172 | A mailing list devoted to the use of Perl in libraries. | ||
| 1173 | |||
| 1174 | =item * Library of Congress MARC pages (L<http://www.loc.gov/marc/>) | ||
| 1175 | |||
| 1176 | The definative source for all things MARC. | ||
| 1177 | |||
| 1178 | =back | ||
| 1179 | |||
| 1180 | =head1 LICENSE | ||
| 1181 | |||
| 1182 | This code may be distributed under the same terms as Perl itself. | ||
| 1183 | |||
| 1184 | =head1 AUTHOR | ||
| 1185 | |||
| 1186 | David Christensen, C<< <David dot A dot Christensen at gmail dot com> >> | ||
| 1187 | |||
| 1188 | =cut | ||
| 1189 | |||
| 1190 | 1; |