lib/Weaving/Tablet/Tk.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« script/snartemo.pl ↑ Summary lib/Weaving/Tablet/Twill.pm »
1 package Weaving::Tablet::Tk;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Weaving::Tablet;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Tk;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 require Tk::HList;
9 require Tk::FileSelect;
10 require Tk::FontDialog;
11 require Tk::ColorEditor;
12 use Data::Dumper;
13 use Moose;
14 use namespace::autoclean;
15 use File::Spec::Functions;
16 use File::Basename;
17  
18 has 'pattern' =>
19 ( isa => 'Weaving::Tablet', is => 'ro', writer => '_set_pattern' );
20 has 'window' =>
21 ( isa => 'Tk::MainWindow', is => 'ro', writer => '_set_window' );
22 has 'canvas' =>
23 ( isa => 'Tk::Frame', is => 'ro', writer => '_set_canvas' );
24 has 'small_font' => (
25 isa => 'Str',
26 is => 'ro',
27 writer => '_set_small_font',
28 default => '-misc-fixed-medium-r-normal--10-100-75-75-c-60-iso8859-1'
29 );
30 has 'row_spacing' => (isa => 'Int', is => 'rw', default => 12);
31 has 'col_spacing' => (isa => 'Int', is => 'rw', default => 9);
32 sub half_col_size { my $self = shift; return int($self->col_spacing/2); }
33 has 'current_row' => (isa => 'Int', is => 'rw', default => 0);
34 has 'current_card' => (isa => 'Int', is => 'rw', default => 0);
35 has 'smooth' => (isa => 'Bool', is => 'rw', default => 0);
36 has 'outline' => (isa => 'Bool', is => 'rw', default => 0);
37 has 'outline_color' => (isa => 'Str', is => 'rw', default => 'black');
38 has 'overlap' => (isa => 'Bool', is => 'rw', default => 0);
39 has 'draw_grid'=> (isa => 'Bool', is => 'rw', default => 0);
40 has 'show_floats' => (isa => 'Bool', is => 'rw', default => 0);
41 has 'float_length' => (isa => 'Int', is => 'rw', default => 3);
42 has 'background_color' => (isa => 'Str', is => 'rw', default => 'white');
43 has 'canvas_tags' => (isa => 'HashRef', is => 'ro', default => sub { {} });
44  
45 our $VERSION;
46 use version; $VERSION = qv('0.9.2');
47  
48 # Preloaded methods go here.
49 my %_windowlist;
50  
51 sub new_pattern
52 {
53 my $this = shift;
54 my %args = @_;
55  
56 my $pattern;
57  
58 if ( exists( $args{file} ) )
59 {
60 $this->window->Busy if ref($this);
61 $pattern = Weaving::Tablet->new( file_name => $args{file} );
62 $this->window->Unbusy if ref($this);
63 }
64 else
65 {
66 $this->window->Busy if ref($this);
67 $pattern = Weaving::Tablet->new(
68 number_of_cards => $args{cards},
69 pattern_length => $args{rows}
70 );
71 $this->window->Unbusy if ref($this);
72 }
73 return unless defined $pattern;
74  
75 delete $args{mw} unless defined $args{mw};
76 my $win = new MainWindow;
77 $_windowlist{MW} = $win unless exists $args{mw};
78 $win->iconname('snartemo');
79 $win->title( $args{file} || "Untitled" );
80  
81 # Make a new ME here!!
82 my $self = $this->new( pattern => $pattern,
83 window => $win,
84 canvas => $win->Scrolled(
85 'Canvas',
86 -background => 'white',
87 -height => 250,
88 -width => 200,
89 )->pack(qw/-expand yes -fill both/),
90 );
91  
92 $_windowlist{$win} = $win;
93  
94 $self->create_menu;
95  
96 $self->draw_pattern;
97 return $self;
98 }
99  
100 sub close_pattern
101 {
102 my $self = shift;
103  
104 if ( $self->pattern->dirty )
105 {
106 my $button = $self->window->Dialog(
107 -text => "Save pattern?",
108 -buttons => [ 'Save', "Don't Save", 'Cancel' ],
109 -default_button => 'Save',
110 )->Show;
111 return if $button eq 'Cancel';
112 if ( $button eq 'Save' )
113 {
114 defined( $self->file_name )
115 ? $self->save_pattern
116 : $self->save_pattern_as;
117 }
118 }
119  
120 delete $_windowlist{ $self->window };
121 $self->window->destroy;
122 }
123  
124 sub create_menu
125 {
126 my $self = shift;
127 my $w = $self->window;
128  
129 my $menuitems = [
130 [
131 Cascade => '~File',
132 -menuitems => [
133 [
134 Button => '~New...',
135 -command => sub { $self->make_new_from_scratch; }
136 ],
137 [
138 Button => '~Open...',
139 -command => sub { $self->make_new_from_file; }
140 ],
141 [
142 Button => 'Close',
143 -command => sub { $self->close_pattern; },
144 ],
145 [ Separator => '' ],
146 [
147 Button => '~Save',
148 -command => sub {
149 defined( $self->file_name )
150 ? $self->save_pattern
151 : $self->save_pattern_as;
152 }
153 ],
154 [
155 Button => 'Save as...',
156 -command => sub { $self->save_pattern_as; }
157 ],
158 [ Separator => '' ],
159 [
160 Button => 'Generate Postscript...',
161 -command => sub { $self->print_pattern_as; }
162 ],
163 [ Separator => '' ],
164 [
165 Button => 'Reload',
166 -command => sub { $self->revert_pattern; }
167 ],
168 [
169 Button => 'Quit',
170 -command => sub {
171 exit;
172 }
173 ],
174 ]
175 ],
176 [
177 Cascade => '~Edit',
178 -menuitems => [
179 [
180 Checkbutton => 'Smooth',
181 -variable => \$self->{smooth},
182 -command => sub {
183 $self->canvas->itemconfigure( 'turn',
184 -smooth => $self->smooth );
185 }
186 ],
187 [
188 Checkbutton => 'Grid',
189 -variable => \$self->{draw_grid},
190 -command => sub {
191 $self->canvas->itemconfigure( 'grid',
192 -outline => $self->draw_grid
193 ? 'black'
194 : undef );
195 }
196 ],
197 [
198 Checkbutton => 'Overlap',
199 -variable => \$self->{overlap},
200 -command => sub {
201 $self->update_pattern;
202 }
203 ],
204 [
205 Checkbutton => 'Outline',
206 -variable => \$self->{outline},
207 -command => sub {
208 $self->update_pattern;
209 }
210 ],
211 [ Separator => '' ],
212 [
213 Button => 'Outline Color',
214 -command => sub { $self->set_outline_color; }
215 ],
216 [
217 Button => 'black outline',
218 -command => sub { $self->outline_color_black; }
219 ],
220 [ Separator => '' ],
221 [
222 Button => 'Background Color',
223 -command => sub { $self->bg_color; }
224 ],
225 [
226 Button => 'white background',
227 -command => sub { $self->bg_color_white; }
228 ],
229 [
230 Button => 'grey background',
231 -command => sub { $self->bg_color_gray; }
232 ],
233 [ Separator => '' ],
234 [
235 Button => 'Set float length...',
236 -command => sub { $self->set_float_length; }
237 ],
238 [
239 Checkbutton => 'Show Floats',
240 -variable => \$self->{show_floats},
241 -command => sub {
242 $self->pattern->float_pattern;
243 $self->update_pattern;
244 }
245 ],
246 [ Separator => '' ],
247 [
248 Button => 'Dump Pattern',
249 -command => sub { $self->dump; }
250 ],
251 ]
252 ],
253 [
254 Cascade => '~Pattern',
255 -menuitems => [
256 [
257 Button => 'Insert row...',
258 -command => sub { $self->insert_row_cmd; }
259 ],
260 [
261 Button => 'Delete row...',
262 -command => sub { $self->delete_row_cmd; }
263 ],
264 [
265 Button => 'Copy row...',
266 -command => sub { $self->duplicate_row_cmd; }
267 ],
268 [ Separator => '' ],
269 [
270 Button => 'Insert card...',
271 -command => sub { $self->insert_card_cmd; }
272 ],
273 [
274 Button => 'Delete card...',
275 -command => sub { $self->delete_card_cmd; }
276 ],
277 [
278 Button => 'Copy card...',
279 -command => sub { $self->duplicate_card_cmd; }
280 ],
281 [ Separator => '' ],
282 [
283 Button => 'Edit colors',
284 -command => sub { $self->edit_palette; }
285 ],
286 [
287 Button => 'Edit threading',
288 -command => sub { $self->edit_threading; }
289 ],
290 [
291 Button => 'Show Twist',
292 -command => sub { $self->show_twist; }
293 ],
294 ]
295 ]
296 ];
297  
298 if ( $Tk::VERSION >= 800 )
299 {
300 my $menubar = $w->Menu( -menuitems => $menuitems );
301 $w->configure( -menu => $menubar );
302 }
303 else
304 {
305 $w->Menubutton(
306 -text => 'Pseudo menubar',
307 -menuitems => $menuitems,
308 )->pack;
309 }
310 }
311  
312 sub file_name
313 {
314 my $self = shift;
315 return $self->pattern->file_name;
316 }
317  
318 sub dump
319 {
320 my $self = shift;
321 print $self->pattern->dump_pattern;
322 foreach my $k ( sort keys %$self )
323 {
324 next unless ref( $self->{$k} );
325 print "self->$k is $self->{$k}\n";
326 }
327 }
328  
329 sub show_twist
330 {
331 my $self = shift;
332 my $twist = $self->pattern->print_twist;
333 print $twist;
334 }
335  
336 sub set_outline_color
337 {
338 my $self = shift;
339 my $bg_color = $self->color_dlog->Show(
340 -title => 'Outline Color',
341 -initialcolor => $self->outline_color
342 );
343 return unless defined $bg_color;
344 $self->outline_color($bg_color);
345 $self->outline(1);
346 $self->canvas->itemconfigure( 'turn', -outline => $self->outline_color );
347 }
348  
349 sub outline_color_black
350 {
351 my $self = shift;
352 $self->outline_color('black');
353 $self->outline(1);
354 $self->canvas->itemconfigure( 'turn', -outline => $self->outline_color );
355 }
356  
357 sub bg_color
358 {
359 my $self = shift;
360 my $bg_color = $self->color_dlog->Show(
361 -title => 'Background Color',
362 -initialcolor => $self->background_color
363 );
364 return unless defined $bg_color;
365 $self->background_color($bg_color);
366 $self->canvas->configure( -background => $self->background_color );
367 }
368  
369 sub bg_color_white
370 {
371 my $self = shift;
372 $self->background_color('white');
373 $self->canvas->configure( -background => $self->background_color );
374 }
375  
376 sub bg_color_gray
377 {
378 my $self = shift;
379 $self->background_color('gray');
380 $self->canvas->configure( -background => $self->background_color );
381 }
382  
383 sub set_float_length
384 {
385 my $self = shift;
386 my $float_length = $self->float_length_dlog( $self->float_length );
387 return unless defined $float_length;
388 $self->float_length($float_length);
389 $self->update_pattern if $self->show_floats;
390 }
391  
392 sub set_current_row
393 {
394 my $self = shift;
395 my $canvas = $self->canvas;
396 my $r = shift;
397  
398 do
399 {
400 foreach my $t ( $self->gettags('current') )
401 {
402 $r = unpack( 'x3a*', $t ) if $t =~ /^row\d+$/;
403 }
404 } unless defined $r;
405 return unless defined $r;
406 $r %= $self->pattern->number_of_rows;
407 $canvas->move( 'currentrow', 0,
408 ( $self->current_row - $r ) * $self->row_spacing );
409 $self->current_row($r);
410 }
411  
412 sub set_current_card
413 {
414 my $self = shift;
415 my $canvas = $self->canvas;
416 my $c = shift;
417  
418 do
419 {
420 foreach my $t ( $self->gettags('current') )
421 {
422 $c = unpack( 'x4a*', $t ) if $t =~ /^card\d+$/;
423 }
424 } unless defined $c;
425 return unless defined $c;
426 $c %= $self->pattern->number_of_cards;
427 $canvas->move( 'currentcard',
428 ( $c - $self->current_card ) * $self->col_spacing, 0 );
429 $self->current_card($c);
430 }
431  
432 sub draw_pattern
433 {
434 shift->_draw_pattern( @_, 'draw' );
435 }
436  
437 sub update_pattern
438 {
439 my $pattern = shift;
440 my @cardlist = @_;
441 @cardlist or @cardlist = ( 0 .. $pattern->pattern->number_of_cards - 1 );
442 $pattern->pattern->float_pattern(@cardlist) if $pattern->show_floats;
443 $pattern->_draw_pattern( 'update', @cardlist );
444 }
445  
446 sub create
447 {
448 my $self = shift;
449 my $type = shift;
450 my @args;
451 while (@_)
452 {
453 last if $_[0] =~ /^-[a-z]/;
454 push @args, shift;
455 }
456 my %args = @_;
457 my $canvas = $self->canvas;
458 my $tags = $args{-tags};
459 delete $args{-tags};
460 my $id = $canvas->create($type, @args, %args);
461 my $idtag = "id$id";
462 $canvas->addtag($idtag, 'withtag', $id);
463 for my $addtag (qw/turn grid SZ start rownumber/)
464 {
465 for my $tag (@$tags)
466 {
467 next unless $tag eq $addtag;
468 $canvas->addtag($tag, 'withtag', $id);
469 last;
470 }
471 }
472 for my $tag ($idtag, @$tags)
473 {
474 push @{$self->canvas_tags->{$tag}}, $id;
475 push @{$self->canvas_tags->{$idtag}}, $tag;
476 }
477 }
478  
479 sub delete
480 {
481 my $self = shift;
482 my @tags = @_;
483 my $canvas = $self->canvas;
484 for my $tag (@tags)
485 {
486 next unless exists $self->canvas_tags->{$tag};
487 $canvas->delete(@{$self->canvas_tags->{$tag}});
488 }
489 }
490  
491 sub itemconfigure
492 {
493 my $self = shift;
494 my ($tag, %args) = @_;
495 my $canvas = $self->canvas;
496 return unless exists $self->canvas_tags->{$tag};
497 for my $id (@{$self->canvas_tags->{$tag}})
498 {
499 $canvas->itemconfigure($id, %args);
500 }
501 }
502  
503 sub _draw_pattern
504 {
505 my $self = shift;
506 my ($action, @cardlist) = @_;
507 my $pattern = $self->pattern;
508 my $canvas = $self->canvas;
509 @cardlist or @cardlist = ( 0 .. $pattern->number_of_cards - 1 );
510  
511 {
512 my @cardlist = map { ref($_) ? $_->[0] : $_; } @cardlist;
513 $pattern->twist_pattern(@cardlist);
514 $pattern->color_pattern(@cardlist);
515 }
516  
517 my ( $c8, $r8, $offset, $rs );
518 do
519 {
520 $self->delete('rownumber');
521 foreach my $row ( 0 .. $pattern->number_of_rows - 1 )
522 {
523 $r8 = ( -$row ) * $self->row_spacing - 8;
524 $self->create(
525 'text',
526 38, $r8,
527 -text => $row + 1,
528 -font => $self->small_font,
529 -anchor => 'e',
530 -tags => [ 'rownumber', "row$row", "rownumber$row" ],
531 );
532 }
533 } if $action eq 'draw';
534 $rs = $self->row_spacing;
535 my $poly_ht = $self->overlap ? $rs : $rs / 2;
536 my $half_col_size = $self->half_col_size;
537 foreach my $card (@cardlist)
538 {
539 my $startrow = 0;
540 ( $card, $startrow ) = @$card if ref($card);
541 $self->window->update;
542 $c8 = $card * $self->col_spacing + 8 + 48;
543 foreach my $row ( $startrow .. $pattern->number_of_rows - 1 )
544 {
545 # $self->_draw_pick($card, $row);
546 $r8 = ( -$row ) * $rs - 8;
547 $offset =
548 ( index( '\|/', $pattern->card_turns($card)->[$row] ) - 1 ) *
549 $half_col_size;
550 $offset *= $pattern->cards->[$card]->SZ eq 'S' ? 1 : -1;
551 $self->delete( "turn$card" . "x$row" );
552 $self->create(
553 'polygon',
554 _poly_coords( $self, $r8, $c8, $offset, $poly_ht ),
555 -fill => $pattern->color_table->[
556 $pattern->threading(
557 $card, $pattern->color( $card, $row )
558 )],
559 -outline => $self->outline ? $self->outline_color : undef,
560 -width => 0.5,
561 -tags =>
562 [ 'turn', "row$row", "card$card", "turn$card" . "x$row" ],
563 );
564 $self->delete( "grid$card" . "x$row" );
565 $self->create(
566 'rectangle',
567 $c8 - $half_col_size - 1, $r8 + $rs / 2,
568 $c8 + $half_col_size, $r8 - $rs / 2,
569 -fill => undef,
570 -outline => $self->draw_grid ? 'black' : undef,
571 -tags =>
572 [ 'grid', "row$row", "card$card", "grid$card" . "x$row" ],
573 );
574 }
575 $self->delete("float$card");
576 if ( $self->show_floats )
577 {
578 foreach my $float ( @{ $pattern->{floats}[$card] } ) ##FIXME
579 {
580  
581 # add check against float_length here...
582 next
583 unless ( $float->[1] - $float->[0] ) + 1 >=
584 $self->float_length;
585 $self->create(
586 'rectangle',
587 $c8 - $half_col_size - 1,
588 ( -$float->[0] ) * $rs - 8 + $rs / 2,
589 $c8 + $half_col_size,
590 ( -$float->[1] ) * $rs - 8 - $rs / 2,
591 -fill => undef,
592 -width => 2,
593 -outline => 'black',
594 -tags => [ 'float', "card$card", "float$card" ],
595 );
596 }
597 }
598  
599 if ( $action eq 'draw' )
600 {
601 $self->delete("SZ$card");
602 $self->create(
603 'text',
604 $c8, 24,
605 -anchor => 'center',
606 -text => $pattern->SZ($card),
607 -tags => [ 'SZ', "card$card", "SZ$card" ]
608 );
609 $self->delete("starthole$card");
610 $self->create(
611 'text',
612 $c8, 36,
613 -anchor => 'center',
614 -text => chr( ord('A') + $pattern->start($card) ),
615 -tags =>
616 [ 'starthole', 'start', "card$card", "starthole$card" ],
617 );
618 foreach my $hole ( 0 .. 3 )
619 {
620 $self->delete( "start$card" . "hole$hole" );
621 $self->create(
622 'rectangle',
623 $c8 - 3,
624 42 + 12 * $hole,
625 $c8 + 3,
626 52 + 12 * $hole,
627 -fill => $pattern->color_table->[
628 $pattern->threading( $card, $hole )
629 ],
630 -tags =>
631 [ 'start', "card$card", "start$card" . "hole$hole" ],
632 );
633 }
634 }
635 elsif ( $action eq 'update' )
636 {
637 $self->itemconfigure( "SZ$card", -text => $pattern->SZ($card) );
638 $self->itemconfigure( "starthole$card",
639 -text => chr( ord('A') + $pattern->start($card) ) );
640 foreach my $hole ( 0 .. 3 )
641 {
642 $self->itemconfigure(
643 "start$card" . "hole$hole",
644 -fill => $pattern->color_table->[
645 $pattern->threading( $card, $hole )
646 ]
647 );
648 }
649 }
650 }
651  
652 if ( $action eq 'draw' )
653 {
654 $self->delete('holelabel');
655 foreach my $hole ( 0 .. 3 )
656 {
657 $self->create(
658 'text',
659 46, 47 + 12 * $hole,
660 -text => chr( ord('A') + $hole ),
661 -tags => [ 'holelabel', "holelabel$hole" ],
662 );
663 }
664 }
665  
666 do
667 {
668 $canvas->delete('currentrow');
669 $canvas->delete('currentcard');
670 $canvas->create(
671 'rectangle',
672 40, -$self->current_row * $self->row_spacing - 10,
673 50, -$self->current_row * $self->row_spacing - 6,
674 -fill => 'black',
675 -tags => ['currentrow'],
676 );
677 $canvas->create(
678 'rectangle',
679 $self->current_card * $self->col_spacing + 8 + 48 - 2, 4,
680 $self->current_card * $self->col_spacing + 8 + 48 + 2, 16,
681 -fill => 'black',
682 -tags => ['currentcard'],
683 );
684  
685 $canvas->bind( 'turn', '<1>', sub { $self->flip_turn; } );
686 $canvas->bind( 'grid', '<1>', sub { $self->flip_turn; } );
687 $canvas->bind( 'turn', '<Shift-1>', sub { $self->null_turn; } );
688 $canvas->bind( 'grid', '<Shift-1>', sub { $self->null_turn; } );
689 $canvas->bind( 'SZ', '<1>', sub { $self->flip_card; } );
690 $canvas->bind( 'start', '<1>', sub { $self->rotate_start(1); } );
691 $canvas->bind( 'start', '<Shift-1>',
692 sub { $self->rotate_start(-1); } );
693 $canvas->bind( 'start', '<2>', sub { $self->edit_threading; } );
694 $canvas->bind( 'start', '<3>', sub { $self->set_current_card; } );
695 $canvas->bind( 'rownumber', '<1>', sub { $self->set_current_row; } );
696 $canvas->CanvasBind( '<Delete>', sub { $self->delete_cmd; } );
697 $canvas->CanvasBind( '<Insert>', sub { $self->insert_cmd; } );
698 } if $action eq 'draw';
699  
700 $canvas->configure( -scrollregion => [ $canvas->bbox("all") ] );
701 }
702  
703 sub _draw_pick
704 {
705 my $self = shift;
706 my ($card, $row) = @_;
707 my $pattern = $self->pattern;
708 my $canvas = $self->canvas;
709 my $rs = $self->row_spacing;
710 my $r8 = ( -$row ) * $rs - 8;
711 my $c8 = $card * $self->col_spacing + 8 + 48;
712 my $poly_ht = $self->overlap ? $rs : $rs / 2;
713 my $half_col_size = $self->half_col_size;
714 if (!$self->gettags("turn${card}x${row}"))
715 {
716 for my $pick (-1 .. 1)
717 {
718 my $offset = $pick * $half_col_size * ($pattern->cards->[$card]->SZ eq 'S' ? 1 : -1);
719 my $turn_tag = (qw/null fwd back/)[$pick];
720 $self->create(
721 'polygon',
722 _poly_coords( $self, $r8, $c8, $offset, $poly_ht ),
723 -fill => $pattern->color_table->[
724 $pattern->threading($card, $pattern->color( $card, $row ))],
725 -state => 'hidden',
726 -tags =>
727 [ 'turn', "row$row", "card$card", "turn$card" . "x$row" , "turn${card}x${row}$turn_tag"],
728 );
729 }
730 $self->create(
731 'rectangle',
732 $c8 - $half_col_size - 1, $r8 + $rs / 2,
733 $c8 + $half_col_size, $r8 - $rs / 2,
734 -fill => undef,
735 -outline => $self->draw_grid ? 'black' : undef,
736 -tags =>
737 [ 'grid', "row$row", "card$card", "grid$card" . "x$row" ],
738 );
739 }
740 my $pick = index( '\|/', $pattern->card_turns($card)->[$row] ) - 1;
741 $pick *= ($pattern->cards->[$card]->SZ eq 'S' ? 1 : -1);
742 my $turn_tag = (qw/null fwd back/)[$pick];
743 $self->itemconfigure("turn${card}x${row}fwd", -state => 'hidden');
744 $self->itemconfigure("turn${card}x${row}back", -state => 'hidden');
745 $self->itemconfigure("turn${card}x${row}null", -state => 'hidden');
746 $self->itemconfigure("turn${card}x${row}$turn_tag",
747 -state => 'normal',
748 -fill => $pattern->color_table->[
749 $pattern->threading($card, $pattern->color( $card, $row ))],);
750 }
751  
752 sub _poly_coords
753 {
754 my $pattern = shift;
755 my ( $r8, $c8, $offset, $poly_ht ) = @_;
756 if ( $offset != 0 )
757 {
758 return $pattern->overlap
759 ? (
760 $c8 - $offset,
761 $r8 + $poly_ht - 1,
762 $c8 - $offset,
763 $r8,
764 $c8 + $offset,
765 $r8 - $poly_ht + 1,
766 $c8 + $offset,
767 $r8,
768 $c8 - $offset,
769 $r8 + $poly_ht - 1,
770 -smooth => $pattern->smooth,
771 )
772 : (
773 $c8 - $offset,
774 $r8 + $poly_ht - 1,
775 $c8 - $offset,
776 $r8 + $poly_ht / 2,
777 $c8 + $offset / 2,
778 $r8 - $poly_ht + 1,
779 $c8 + $offset,
780 $r8 - $poly_ht + 1,
781 $c8 + $offset,
782 $r8 - $poly_ht / 2,
783 $c8 - $offset / 2,
784 $r8 + $poly_ht - 1,
785 $c8 - $offset,
786 $r8 + $poly_ht - 1,
787 -smooth => $pattern->smooth,
788 );
789 }
790 else
791 {
792 my $offset = $pattern->half_col_size / 2;
793 return (
794 $c8 - $offset,
795 $r8 + $poly_ht - 1,
796 $c8 - $offset,
797 $r8 - $poly_ht + 1,
798 $c8 + $offset,
799 $r8 - $poly_ht + 1,
800 $c8 + $offset,
801 $r8 + $poly_ht + 1,
802 $c8 - $offset,
803 $r8 + $poly_ht - 1,
804 -smooth => $pattern->smooth,
805 );
806 }
807 }
808  
809 sub delete_cmd
810 {
811 my $self = shift;
812  
813 my $action = $self->delete_dlog->Show;
814  
815 $action eq 'Row' and $self->delete_row_cmd;
816 $action eq 'Card' and $self->delete_card_cmd;
817 }
818  
819 sub insert_cmd
820 {
821 my $self = shift;
822  
823 my $action = $self->insert_dlog->Show;
824  
825 $action eq 'Row' and $self->insert_row_cmd;
826 $action eq 'Card' and $self->insert_card_cmd;
827 }
828  
829 sub make_new_from_scratch
830 {
831 my $self = shift;
832  
833 my $d = $self->window->DialogBox(
834 -title => "New pattern...",
835 -buttons => [qw/OK Cancel/],
836 -default_button => 'OK'
837 );
838  
839 my ( $cards, $rows ) = ( 10, 10 );
840 my $msg = '';
841  
842 $d->add(
843 'LabEntry',
844 -label => 'Number of Cards',
845 -labelPack => [qw/-side left -anchor w/],
846 -textvariable => \$cards,
847 )->pack(qw/-side top -anchor w/);
848 $d->add(
849 'LabEntry',
850 -label => 'Number of turns',
851 -labelPack => [qw/-side left -anchor w/],
852 -textvariable => \$rows,
853 )->pack(qw/-side top -anchor w/);
854 $d->add(
855 'Label',
856 -textvariable => \$msg,
857 -width => 50,
858 -height => 2,
859 -justify => 'left',
860 )->pack(qw/-side left -anchor w/);
861  
862 LOOP:
863 {
864 return if $d->Show eq 'Cancel';
865 $msg = '';
866 $msg = 'Number of Cards must be > 0' unless $cards > 0;
867 $msg = 'Number of turns must be > 0' unless $rows > 0;
868 redo LOOP unless $msg eq '';
869 }
870 return
871 if $self->new_pattern(
872 mw => $self->window,
873 cards => $cards,
874 rows => $rows
875 );
876 $self->window->Dialog( -text => 'Could not create new pattern' )->Show;
877 return;
878 }
879  
880 sub revert_pattern
881 {
882 my $self = shift;
883  
884 return unless defined $self->pattern->file_name;
885 return unless -r $self->pattern->file_name;
886  
887 return
888 if $self->window->Dialog(
889 -text => "Revert to saved version (discard changes)?",
890 -buttons => [qw/OK Cancel/],
891 -default_button => 'OK'
892 ) eq 'Cancel';
893
894 my $new_pattern = $self->pattern->new(file_name => $self->pattern->file_name);
895 my $old_canvas = $self->canvas;
896 my $new_canvas = $self->window->Scrolled(
897 'Canvas',
898 -background => 'white',
899 -height => $old_canvas->height,
900 -width => $old_canvas->width,
901 )->pack(qw/-expand yes -fill both/),
902 $old_canvas->packForget;
903 $old_canvas->destroy;
904 $self->_set_canvas($new_canvas);
905 $new_pattern->load_pattern;
906 $self->draw_pattern;
907 }
908  
909 sub make_new_from_file
910 {
911 my $self = shift;
912 my $fs =
913 $self->fs_dlog( { -filelabel => 'Load pattern from...', -create => 0 } );
914 my $file = $fs->Show;
915 return if $file eq '';
916 return if $self->new_pattern( mw => $self->window, file => $file );
917 $self->window->Dialog( -text => "Could not load file $file" )->Show;
918 return;
919 }
920  
921 sub save_pattern
922 {
923 my $self = shift;
924 open my $pat, '>', $self->pattern->file_name or return;
925 print $pat $self->pattern->dump_pattern;
926 close $pat;
927 return 1;
928 }
929  
930 sub save_pattern_as
931 {
932 my $self = shift;
933 my $fs =
934 $self->fs_dlog( { -filelabel => 'Save pattern as...', -create => 1 } );
935 my $file = $fs->Show;
936 return if ! defined $file;
937 return if $file eq '';
938 $self->pattern->file_name($file);
939 return if $self->save_pattern;
940 $self->window->Dialog( -text => "Could not save file as $file" )->Show;
941 return;
942 }
943  
944 sub print_pattern_as
945 {
946 my $self = shift;
947 my $fs =
948 $self->fs_dlog( { -filelabel => 'Save postscript as...', -create => 1 } );
949 my $file = $fs->Show;
950 return if ! defined $file;
951 return if $file eq '';
952 my @bbox = $self->canvas->bbox('all');
953 my $width = $bbox[2]-$bbox[0];
954 my $height = $bbox[3]-$bbox[1];
955 my $pagewidth = $width/90 . 'i';
956 my $height_per_page = $height > 850 ? 800 : 850;
957 my $page_overlap = $height > 850 ? 50 : 0;
958 if ($width > 720)
959 {
960 $pagewidth = '8i';
961 $height_per_page *= $width/720;
962 $height_per_page = int($height_per_page);
963 }
964 my $pages = int($height/$height_per_page)+1;
965 # now
966 my $page = 1;
967 my $y = $bbox[3] - ($height_per_page + $page_overlap);
968 while ($page <= $pages)
969 {
970 my $y = $bbox[3] - ($height_per_page*$page + $page_overlap);
971 $y = $bbox[1] if $y < $bbox[1];
972 use autodie;
973 my $psfile = _add_page_to_file_name($file, $page);
974 print "Printing page $page to $psfile\n";
975 $self->canvas->postscript(
976 -file => $psfile,
977 -colormode => 'color',
978 -x => $bbox[0],
979 -y => $y,
980 -width => $width,
981 -height => $height_per_page + $page_overlap,
982 -pagewidth => $pagewidth,
983 );
984 $page++;
985 }
986 return;
987 }
988  
989 sub _add_page_to_file_name
990 {
991 my ($path, $number) = @_;
992 my ($volume, $directories, $file) = File::Spec->splitpath( $path );
993 my($filename, undef, $suffix) = fileparse($file, qr/\.[^.]*/);
994 $filename .= $number;
995 return catfile($volume, $directories, $filename.$suffix) if $volume;
996 return catfile($directories, $filename.$suffix);
997 }
998  
999 sub insert_row_cmd
1000 {
1001 my $self = shift;
1002 my $dlog = $self->insert_row_dlog;
1003 $dlog->{where_row} = $self->current_row + 1;
1004 $dlog->{dlog}->deiconify;
1005 $dlog->{dlog}->raise;
1006 }
1007  
1008 sub delete_row_cmd
1009 {
1010 my $self = shift;
1011 my $dlog = $self->delete_row_dlog;
1012 $dlog->{where_row} = $self->current_row + 1;
1013 $dlog->{dlog}->deiconify;
1014 $dlog->{dlog}->raise;
1015 }
1016  
1017 sub duplicate_row_cmd
1018 {
1019 my $self = shift;
1020 my $dlog = $self->duplicate_row_dlog;
1021 $dlog->{from_row} = $self->current_row + 1;
1022 $dlog->{after_row} = $self->current_row + 1;
1023 $dlog->{dlog}->deiconify;
1024 $dlog->{dlog}->raise;
1025 }
1026  
1027 sub insert_card_cmd
1028 {
1029 my $self = shift;
1030 my $dlog = $self->insert_card_dlog;
1031 $dlog->{where_card} = $self->current_card + 1;
1032 $self->refresh_color_list( $dlog->{palette} );
1033 $self->refresh_card_threading( $dlog->{card_canvas} );
1034 $dlog->{threading} = [ $self->threading( $self->current_card ) ];
1035 $dlog->{turns}->delete( '1.0', 'end' );
1036 $dlog->{turns}->insert( '1.0',
1037 join( "", reverse $self->card_turns( $self->current_card ) ) );
1038 $dlog->{dlog}->deiconify;
1039 $dlog->{dlog}->raise;
1040 }
1041  
1042 sub delete_card_cmd
1043 {
1044 my $self = shift;
1045 my $dlog = $self->delete_card_dlog;
1046 $dlog->{where_card} = $self->current_card + 1;
1047 $dlog->{dlog}->deiconify;
1048 $dlog->{dlog}->raise;
1049 }
1050  
1051 sub duplicate_card_cmd
1052 {
1053 my $self = shift;
1054 my $dlog = $self->duplicate_card_dlog;
1055 $dlog->{from_card} = $self->current_card + 1;
1056 $dlog->{after_card} = $self->current_card + 1;
1057 $dlog->{dlog}->deiconify;
1058 $dlog->{dlog}->raise;
1059 }
1060  
1061 sub edit_palette
1062 {
1063 my $pattern = shift;
1064 my $canvas = $pattern->canvas;
1065 my $palette = $pattern->palette_dlog;
1066 $pattern->refresh_color_list( $palette->{h} );
1067 $palette->{dlog}->deiconify;
1068 $palette->{dlog}->raise;
1069 }
1070  
1071 sub get_new_color
1072 {
1073 my $pattern = shift;
1074 my $h = shift;
1075  
1076 my $title = "Select new color to add to palette";
1077 my $color = $pattern->color_dlog->Show( -title => $title, -parent => $h );
1078 return unless defined $color;
1079 $pattern->color_table( undef, $color );
1080 my $c = @{ $pattern->{color_table} } - 1;
1081 $h->add( $c, -text => $c );
1082 $h->itemCreate( $c, 1, -text => $color );
1083 }
1084  
1085 sub refresh_color_list
1086 {
1087 my $self = shift;
1088 my $h = shift;
1089 $h->delete('all');
1090 foreach my $c ( 0 .. @{$self->pattern->color_table} - 1 )
1091 {
1092 $h->add( $c, -text => $c );
1093 $h->itemCreate( $c, 1, -text => $self->pattern->color_table->[$c] );
1094 }
1095 }
1096  
1097 sub refresh_card_threading
1098 {
1099 my $self = shift;
1100 my $pattern = $self->pattern;
1101 my $card_canvas = shift;
1102 my $threading = shift
1103 || $pattern->threading( $self->current_card );
1104  
1105 foreach my $h ( 0 .. 3 )
1106 {
1107 $card_canvas->itemconfigure( "hole$h",
1108 -fill => $pattern->color_table->[$threading->[$h]], );
1109 }
1110 }
1111  
1112 sub edit_threading
1113 {
1114 my $self = shift;
1115 my $pattern = $self->pattern;
1116 my $canvas = $self->canvas;
1117 $self->set_current_card;
1118 $self->current_card(0) unless $self->current_card;
1119  
1120 my $list = $self->threading_dlog->{h};
1121 my $t = $self->threading_dlog->{dlog};
1122 my $card_canvas = $self->threading_dlog->{card_canvas};
1123  
1124 $self->threading_dlog->{start} =
1125 $pattern->start( $self->current_card );
1126 $self->refresh_color_list($list);
1127 $self->refresh_card_threading($card_canvas);
1128 $t->deiconify;
1129 $t->raise;
1130 }
1131  
1132 sub gettags
1133 {
1134 my $self = shift;
1135 my $tag = shift;
1136 if ($tag eq 'current')
1137 {
1138 for my $tag ($self->canvas->gettags('current'))
1139 {
1140 next unless $tag =~ /^id\d+/;
1141 return @{$self->canvas_tags->{$tag}};
1142 }
1143 return;
1144 }
1145 }
1146 sub flip_turn
1147 {
1148 my $pattern = shift;
1149 my $canvas = $pattern->canvas;
1150  
1151 my ( $r, $c );
1152 foreach my $t ( $pattern->gettags('current') )
1153 {
1154 $r = unpack( 'x3a*', $t ) if $t =~ /^row\d+$/;
1155 $c = unpack( 'x4a*', $t ) if $t =~ /^card\d+$/;
1156 }
1157 return unless defined $r;
1158 return unless defined $c;
1159  
1160 $pattern->pattern->turns( $c, $r, $pattern->pattern->turns( $c, $r ) eq '/' ? '\\' : '/' );
1161 $pattern->set_current_row($r);
1162 $pattern->set_current_card($c);
1163 $pattern->update_pattern( [ $c, $r ] );
1164 }
1165  
1166 sub null_turn
1167 {
1168 my $pattern = shift;
1169 my $canvas = $pattern->canvas;
1170  
1171 my ( $r, $c );
1172 foreach my $t ( $pattern->gettags('current') )
1173 {
1174 $r = unpack( 'x3a*', $t ) if $t =~ /^row\d+$/;
1175 $c = unpack( 'x4a*', $t ) if $t =~ /^card\d+$/;
1176 }
1177 return unless defined $r;
1178 return unless defined $c;
1179  
1180 $pattern->pattern->turns( $c, $r, '|' );
1181 $pattern->set_current_row($r);
1182 $pattern->set_current_card($c);
1183 $pattern->update_pattern( [ $c, $r ] );
1184 }
1185  
1186 sub flip_card
1187 {
1188 my $pattern = shift;
1189 my $canvas = $pattern->canvas;
1190  
1191 my $c;
1192 foreach my $t ( $pattern->gettags('current') )
1193 {
1194 $c = unpack( 'x4a*', $t ) if $t =~ /^card\d+$/;
1195 }
1196 return unless defined $c;
1197 $pattern->pattern->SZ($c, $pattern->pattern->SZ($c) eq 'S' ? 'Z' : 'S');
1198 $pattern->set_current_card($c);
1199 $pattern->update_pattern($c);
1200 }
1201  
1202 sub rotate_start
1203 {
1204 my $pattern = shift;
1205 my $dir = shift || 1;
1206 my $canvas = $pattern->canvas;
1207  
1208 my $c;
1209 foreach my $t ( $pattern->gettags('current') )
1210 {
1211 $c = unpack( 'x4a*', $t ) if $t =~ /^card\d+$/;
1212 }
1213 return unless defined $c;
1214  
1215 $pattern->pattern->start( $c, ( $pattern->pattern->start($c) + $dir ) % 4 );
1216 $pattern->set_current_card($c);
1217 $pattern->update_pattern($c);
1218 }
1219  
1220 sub palette_dlog
1221 {
1222 my $self = shift;
1223 unless ( defined $self->{_palette_dlog} )
1224 {
1225 my $palette = $self->window->Toplevel;
1226 $palette->title('Pattern palette editor');
1227 $palette->Button(
1228 -text => 'Close',
1229 -command => [ withdraw => $palette ],
1230 )->pack(qw/-side bottom/);
1231 my $h =
1232 $self->_palette(
1233 $palette->Frame->pack(qw/-side top -fill both -expand y/) );
1234  
1235 $self->{_palette_dlog} = { dlog => $palette, h => $h };
1236 }
1237 $self->{_palette_dlog};
1238 }
1239  
1240 sub _palette # (frame, card_canvas, \current_hole, \threading, update)
1241  
1242 # frame only required
1243 {
1244 my $self = shift;
1245 my $palette = shift;
1246 my $card_canvas = shift;
1247 my $current_hole = shift;
1248 my $threading = shift;
1249 my $update = shift || 0;
1250  
1251 my $h = $palette->Scrolled(
1252 'HList',
1253 -width => 40,
1254 -height => 10,
1255 -header => 1,
1256 -drawbranch => 0,
1257 -columns => 2,
1258 -scrollbars => 'osoe',
1259 )->pack(qw/-side top -fill both -expand y/);
1260 $palette->Button(
1261 -text => 'Add from picker',
1262 -command => sub {
1263 my $title = "Select new color to add to palette";
1264 my $color = $self->color_dlog->Show( -title => $title );
1265 return unless defined $color;
1266 $self->pattern->color_table( undef, $color );
1267 $self->refresh_color_list($h);
1268 },
1269 )->pack(qw/-side top/);
1270 $palette->Button(
1271 -text => 'Add name',
1272 -command => sub {
1273 my $d = $palette->DialogBox(
1274 -title => 'Add Color by Name',
1275 -buttons => [qw/OK Cancel/],
1276 -default_button => 'OK',
1277 );
1278 my $cname;
1279 $d->add(
1280 'LabEntry',
1281 -label => 'Color',
1282 -labelPack => [qw/-side left/],
1283 -width => 30,
1284 -textvariable => \$cname,
1285 )->pack;
1286 return if $d->Show eq 'Cancel';
1287 return if $cname eq '';
1288 $self->pattern->color_table( undef, $cname );
1289 $self->refresh_color_list($h);
1290 },
1291 )->pack(qw/-side top/);
1292  
1293 $h->header( create => 0, -text => 'Number' );
1294 $h->header( create => 1, -text => 'Color' );
1295 $h->columnWidth( 0, -char => 7 );
1296 $h->columnWidth( 1, -char => 33 );
1297  
1298 $h->configure(
1299 -command => sub {
1300 my $s = shift;
1301 my $title = "Old color $s (" . $self->color_table($s) . ")";
1302 my $color = $self->color_dlog->Show(
1303 -title => $title,
1304 -initialcolor => $self->color_table($s),
1305 );
1306 return unless defined $color;
1307 $self->pattern->color_table( $s, $color );
1308 $h->itemConfigure( $s, 1, -text => $color );
1309 $self->update_pattern;
1310 $self->refresh_card_threading($card_canvas) if $update;
1311 },
1312 );
1313 $h->configure(
1314 -browsecmd => sub {
1315 $self->pattern->threading( $self->current_card, $$current_hole, shift );
1316 $card_canvas->itemconfigure(
1317 "hole$$current_hole",
1318 -fill => $self->pattern->color_table->[
1319 $self->pattern->threading( $self->current_card, $$current_hole)],
1320 );
1321 $card_canvas->update;
1322 $self->pattern->color_pattern( $self->current_card );
1323 $self->update_pattern( $self->current_card );
1324 },
1325 ) if $update eq 'old';
1326 $h->configure(
1327 -browsecmd => sub {
1328 $$threading[$$current_hole] = shift;
1329 $card_canvas->itemconfigure( "hole$$current_hole",
1330 -fill => $self->pattern->color_table( $$threading[$$current_hole] ) );
1331  
1332 },
1333 ) if $update eq 'new';
1334 $h->bind(
1335 "<3>",
1336 sub {
1337 my $w = shift;
1338 my $d = $h->DialogBox(
1339 -title => "Edit Color Name",
1340 -buttons => [qw/OK Cancel/],
1341 -default_button => 'OK',
1342 );
1343 my $old;
1344 my $new;
1345 $d->add(
1346 'LabEntry',
1347 -textvariable => \$old,
1348 -label => 'Old name',
1349 -labelPack => [qw/-side left -anchor w/],
1350 -state => 'disabled',
1351 -width => 30,
1352 )->pack(qw/-side top -anchor w/);
1353 $d->add(
1354 'LabEntry',
1355 -textvariable => \$new,
1356 -label => 'New name',
1357 -labelPack => [qw/-side left -anchor w/],
1358 -width => 30,
1359 )->pack(qw/-side top -anchor w/);
1360 my $y = $w->XEvent->y;
1361 my $s = $h->nearest($y);
1362 $old = $new = $self->color_table($s);
1363 return if $d->Show eq 'Cancel';
1364 print "change $old ($s at $y) to $new\n";
1365 return if $old eq $new;
1366 return if $new eq '';
1367 $self->pattern->color_table( $s, $new );
1368 $h->itemConfigure( $s, 1, -text => $new );
1369 $self->update_pattern;
1370 $self->refresh_card_threading($card_canvas) if $update;
1371 }
1372 );
1373 $h;
1374 }
1375  
1376 sub threading_dlog
1377 {
1378 my $self = shift;
1379 unless ( defined $self->{_threading_dlog} )
1380 {
1381 my $t_info = {
1382 current_hole => 0,
1383 start => 0
1384 };
1385  
1386 my $t = $self->window->Toplevel;
1387 $t->title('Edit card threading');
1388 $t->Button(
1389 -text => 'Close',
1390 -command => [ withdraw => $t ],
1391 )->pack(qw/-side bottom/);
1392 my $f1 = $t->Frame->pack(qw/-side left/);
1393 $f1->Label( -textvariable => \$self->{current_card} )
1394 ->pack(qw/-side top/);
1395 my $f2 =
1396 $t->Frame( -label => "Palette", )
1397 ->pack(qw/-side right -expand y -fill both/);
1398 my $card_canvas = $self->_card_canvas( $f1, \$t_info->{current_hole},
1399 \$t_info->{start}, 1, )->pack(qw/-side top/);
1400  
1401 # put up the palette
1402 my $h = $self->_palette( $f2, $card_canvas, \$t_info->{current_hole},
1403 undef, 'old' );
1404  
1405 $f1->Button(
1406 -text => 'Prev',
1407 -command => sub {
1408 $self->set_current_card( $self->current_card - 1 );
1409 $t_info->{start} = $self->pattern->start( $self->current_card );
1410 $self->refresh_card_threading($card_canvas);
1411 },
1412 )->pack(qw/-side left/);
1413 $f1->Button(
1414 -text => 'Next',
1415 -command => sub {
1416 $self->set_current_card( $self->current_card + 1 );
1417 $t_info->{start} = $self->pattern->start( $self->current_card );
1418 $self->refresh_card_threading($card_canvas);
1419 },
1420 )->pack(qw/-side right/);
1421 $t_info->{dlog} = $t;
1422 $t_info->{h} = $h;
1423 $t_info->{card_canvas} = $card_canvas;
1424 $self->{_threading_dlog} = $t_info;
1425 }
1426 $self->{_threading_dlog};
1427 }
1428  
1429 sub fs_dlog
1430 {
1431 my $self = shift;
1432 my $configs = shift;
1433 unless ( defined $self->{_fs_dlog} )
1434 {
1435 $self->{_fs_dlog} = $self->window->FileSelect;
1436 }
1437 $self->{_fs_dlog}->configure(%$configs);
1438 $self->{_fs_dlog};
1439 }
1440  
1441 sub color_dlog
1442 {
1443 my $self = shift;
1444 unless ( defined $self->{_color_dlog} )
1445 {
1446 $self->{_color_dlog} = $self->window->ColorDialog;
1447 }
1448 $self->{_color_dlog};
1449 }
1450  
1451 sub font_dlog
1452 {
1453 my $self = shift;
1454 unless ( defined $self->{_font_dlog} )
1455 {
1456 $self->{_font_dlog} = $self->window->FontDialog(
1457 -nicefont => 0,
1458 -fixedfont => 1,
1459 );
1460 }
1461 $self->{_font_dlog};
1462 }
1463  
1464 sub insert_row_dlog
1465 {
1466 my $self = shift;
1467 unless ( defined $self->{_insert_row_dlog} )
1468 {
1469 my $d_info = {
1470 dlog => undef,
1471 where => 'current',
1472 where_row => undef,
1473 turns => undef,
1474 };
1475 my $d = $self->window->Toplevel;
1476 $d->title('Insert Row of Turns');
1477 $d->Radiobutton(
1478 -text => 'Beginning (bottom)',
1479 -variable => \$d_info->{where},
1480 -value => 'bottom',
1481 )->pack(qw/-side top -anchor w/);
1482 my $f1 = $d->Frame->pack(qw/-side top -anchor w/);
1483 $f1->Radiobutton(
1484 -text => 'After row',
1485 -variable => \$d_info->{where},
1486 -value => 'current',
1487 )->pack(qw/-side left/);
1488 $f1->Entry(
1489 -textvariable => \$d_info->{where_row},
1490 -width => 10,
1491 )->pack(qw/-side left/);
1492 $d->Radiobutton(
1493 -text => 'End (top)',
1494 -variable => \$d_info->{where},
1495 -value => 'end',
1496 )->pack(qw/-side top -anchor w/);
1497  
1498 my $f2 = $d->Frame->pack(qw/-side top -anchor w/);
1499 $f2->Label( -text => 'Turns', )->pack(qw/-side left -anchor n/);
1500 $f2->Scrolled(
1501 'Entry',
1502 -scrollbars => 's',
1503 -width => 50,
1504 -textvariable => \$d_info->{turns},
1505 )->pack(qw/-side left -anchor n/);
1506  
1507 $d->Button(
1508 -text => 'Done',
1509 -command => sub {
1510 my $after_row =
1511 $d_info->{where} eq 'bottom' ? 0
1512 : $d_info->{where} eq 'current' ? $d_info->{where_row}
1513 : $self->pattern->number_of_rows;
1514 if ( $d_info->{where} eq 'current'
1515 and ( $after_row < 0 or $after_row > $self->pattern->number_of_rows )
1516 )
1517 {
1518 $d->Dialog( -text =>
1519 'After must be between 0 and the number of turns' )
1520 ->Show;
1521 return;
1522 }
1523 my @turns = split( //, $d_info->{turns} );
1524 if ( grep !/^[\/\\|]$/, @turns )
1525 {
1526 $d->Dialog( -text => 'Turns may only be /, | or \\' )->Show;
1527 return;
1528 }
1529 print "insert_row(", $after_row, ", '",
1530 $d_info->{turns} || '', "')\n";
1531 $self->pattern->insert_pick( [$after_row-1, $d_info->{turns} || ''] );
1532 $self->draw_pattern;
1533 $d->withdraw;
1534 },
1535 )->pack(qw/-side left/);
1536 $d->Button(
1537 -text => 'Stay',
1538 -command => sub {
1539 my $after_row =
1540 $d_info->{where} eq 'bottom' ? 0
1541 : $d_info->{where} eq 'current' ? $d_info->{where_row}
1542 : $self->pattern->number_of_rows;
1543 if ( $d_info->{where} eq 'current'
1544 and ( $after_row < 0 or $after_row > $self->pattern->number_of_rows )
1545 )
1546 {
1547 $d->Dialog( -text =>
1548 'After must be between 0 and the number of turns' )
1549 ->Show;
1550 return;
1551 }
1552 my @turns = split( //, $d_info->{turns} );
1553 if ( grep !/^[\/\\|]$/, @turns )
1554 {
1555 $d->Dialog( -text => 'Turns may only be /, | or \\' )->Show;
1556 return;
1557 }
1558 print "insert_row(", $after_row, ", '",
1559 $d_info->{turns} || '', "')\n";
1560 $self->pattern->insert_pick( [$after_row-1, $d_info->{turns} || ''] );
1561 $self->draw_pattern;
1562 },
1563 )->pack(qw/-side left/);
1564  
1565 $d->Button(
1566 -text => 'Cancel',
1567 -command => sub {
1568 $d->withdraw;
1569 },
1570 )->pack(qw/-side right/);
1571  
1572 $d_info->{dlog} = $d;
1573 $self->{_insert_row_dlog} = $d_info;
1574 }
1575  
1576 $self->{_insert_row_dlog};
1577 }
1578  
1579 sub float_length_dlog
1580 {
1581 my $self = shift;
1582 my $old_float_length = shift;
1583 unless ( defined $self->{_float_length_dlog} )
1584 {
1585 my $d_info = {
1586 dlog => undef,
1587 float_length => '',
1588 };
1589 my $d = $self->window->DialogBox(
1590 -title => 'Shortest float to mark',
1591 -buttons => [qw/OK Cancel/],
1592 );
1593 $d->add( 'Entry', -textvariable => \$d_info->{float_length} )->pack;
1594 $d_info->{dlog} = $d;
1595 $self->{_float_length_dlog} = $d_info;
1596 }
1597 $self->{_float_length_dlog}->{float_length} = $old_float_length;
1598 my $action = $self->{_float_length_dlog}->{dlog}->Show;
1599 return $action eq 'OK'
1600 ? $self->{_float_length_dlog}->{float_length}
1601 : $old_float_length;
1602 }
1603  
1604 sub delete_row_dlog
1605 {
1606 my $self = shift;
1607 unless ( defined $self->{_delete_row_dlog} )
1608 {
1609 my $d_info = {
1610 dlog => undef,
1611 where => 'current',
1612 where_row => undef,
1613 };
1614 my $d = $self->window->Toplevel;
1615 $d->title('delete Row of Turns');
1616 $d->Radiobutton(
1617 -text => 'Beginning (bottom)',
1618 -variable => \$d_info->{where},
1619 -value => 'bottom',
1620 )->pack(qw/-side top -anchor w/);
1621 my $f1 = $d->Frame->pack(qw/-side top -anchor w/);
1622 $f1->Radiobutton(
1623 -text => 'Row',
1624 -variable => \$d_info->{where},
1625 -value => 'current',
1626 )->pack(qw/-side left/);
1627 $f1->Entry(
1628 -textvariable => \$d_info->{where_row},
1629 -width => 10,
1630 )->pack(qw/-side left/);
1631 $d->Radiobutton(
1632 -text => 'End (top)',
1633 -variable => \$d_info->{where},
1634 -value => 'end',
1635 )->pack(qw/-side top -anchor w/);
1636  
1637 $d->Button(
1638 -text => 'OK',
1639 -command => sub {
1640 my $after_row =
1641 $d_info->{where} eq 'bottom' ? 1
1642 : $d_info->{where} eq 'current' ? $d_info->{where_row}
1643 : $self->pattern->number_of_rows;
1644 if ( $d_info->{where} eq 'current'
1645 and ( $after_row < 0 or $after_row > $self->pattern->number_of_rows )
1646 )
1647 {
1648 $d->Dialog( -text =>
1649 'Row must be between 1 and the number of turns' )
1650 ->Show;
1651 return;
1652 }
1653 print "delete_row($after_row-1)\n";
1654 $self->canvas->delete( "row" . ( $self->pattern->number_of_rows - 1 ) );
1655 $self->pattern->delete_row( $after_row - 1 );
1656 $self->current_row( $self->pattern->number_of_rows - 1 )
1657 if $self->current_row >= $self->pattern->number_of_rows;
1658 $self->draw_pattern;
1659 $d->withdraw;
1660 },
1661 )->pack(qw/-side left/);
1662  
1663 $d->Button(
1664 -text => 'Cancel',
1665 -command => sub {
1666 $d->withdraw;
1667 },
1668 )->pack(qw/-side right/);
1669  
1670 $d_info->{dlog} = $d;
1671 $self->{_delete_row_dlog} = $d_info;
1672 }
1673  
1674 $self->{_delete_row_dlog};
1675 }
1676  
1677 sub duplicate_row_dlog
1678 {
1679 my $self = shift;
1680 unless ( defined $self->{_duplicate_row_dlog} )
1681 {
1682 my $d_info = {
1683 dlog => undef,
1684 from => 'current',
1685 from_row => undef,
1686 after => 'current',
1687 after_row => undef,
1688 };
1689 my $d = $self->window->Toplevel;
1690 $d->title('duplicate a Row of Turns');
1691 my $f_bottom = $d->Frame->pack(qw/-side bottom -fill x -expand y/);
1692 my $f_left = $d->Frame( -label => 'From' )->pack(qw/-side left/);
1693 my $f_right = $d->Frame( -label => 'After' )->pack(qw/-side right/);
1694 $f_left->Radiobutton(
1695 -text => 'Beginning (bottom)',
1696 -variable => \$d_info->{from},
1697 -value => 'bottom',
1698 )->pack(qw/-side top -anchor w/);
1699 my $f1 = $f_left->Frame->pack(qw/-side top -anchor w/);
1700 $f1->Radiobutton(
1701 -text => 'Row',
1702 -variable => \$d_info->{from},
1703 -value => 'current',
1704 )->pack(qw/-side left/);
1705 $f1->Entry(
1706 -textvariable => \$d_info->{from_row},
1707 -width => 10,
1708 )->pack(qw/-side left/);
1709 $f_left->Radiobutton(
1710 -text => 'End (top)',
1711 -variable => \$d_info->{from},
1712 -value => 'end',
1713 )->pack(qw/-side top -anchor w/);
1714  
1715 $f_right->Radiobutton(
1716 -text => 'Beginning (bottom)',
1717 -variable => \$d_info->{from},
1718 -value => 'bottom',
1719 )->pack(qw/-side top -anchor w/);
1720 my $f2 = $f_right->Frame->pack(qw/-side top -anchor w/);
1721 $f2->Radiobutton(
1722 -text => 'Row',
1723 -variable => \$d_info->{from},
1724 -value => 'current',
1725 )->pack(qw/-side left/);
1726 $f2->Entry(
1727 -textvariable => \$d_info->{from_row},
1728 -width => 10,
1729 )->pack(qw/-side left/);
1730 $f_right->Radiobutton(
1731 -text => 'End (top)',
1732 -variable => \$d_info->{from},
1733 -value => 'end',
1734 )->pack(qw/-side top -anchor w/);
1735  
1736 $f_bottom->Button(
1737 -text => 'Done',
1738 -command => sub {
1739 my $from_row =
1740 $d_info->{from} eq 'bottom' ? 1
1741 : $d_info->{from} eq 'current' ? $d_info->{from_row}
1742 : $self->pattern->number_of_rows;
1743 if ( $d_info->{from} eq 'current'
1744 and ( $from_row < 0 or $from_row > $self->pattern->number_of_rows ) )
1745 {
1746 $d->Dialog( -text =>
1747 '"From" row must be between 1 and the number of turns'
1748 )->Show;
1749 return;
1750 }
1751 my $after_row =
1752 $d_info->{after} eq 'bottom' ? 0
1753 : $d_info->{after} eq 'current' ? $d_info->{after_row}
1754 : $self->pattern->number_of_rows;
1755 if ( $d_info->{after} eq 'current'
1756 and ( $after_row < 0 or $after_row > $self->pattern->number_of_rows )
1757 )
1758 {
1759 $d->Dialog( -text =>
1760 '"To" row must be between 0 and the number of turns' )
1761 ->Show;
1762 return;
1763 }
1764 print "duplicate_row($from_row-1, $after_row)\n";
1765 $self->duplicate_row( $from_row - 1, $after_row );
1766 $self->draw_pattern;
1767 $d->withdraw;
1768 },
1769 )->pack(qw/-side left/);
1770 $f_bottom->Button(
1771 -text => 'Stay',
1772 -command => sub {
1773 my $from_row =
1774 $d_info->{from} eq 'bottom' ? 1
1775 : $d_info->{from} eq 'current' ? $d_info->{from_row}
1776 : $self->pattern->number_of_rows;
1777 if ( $d_info->{from} eq 'current'
1778 and ( $from_row < 0 or $from_row > $self->pattern->number_of_rows ) )
1779 {
1780 $d->Dialog( -text =>
1781 '"From" row must be between 1 and the number of turns'
1782 )->Show;
1783 return;
1784 }
1785 my $after_row =
1786 $d_info->{after} eq 'bottom' ? 0
1787 : $d_info->{after} eq 'current' ? $d_info->{after_row}
1788 : $self->pattern->number_of_rows;
1789 if ( $d_info->{after} eq 'current'
1790 and ( $after_row < 0 or $after_row > $self->pattern->number_of_rows )
1791 )
1792 {
1793 $d->Dialog( -text =>
1794 '"To" row must be between 0 and the number of turns' )
1795 ->Show;
1796 return;
1797 }
1798 print "duplicate_row($from_row-1, $after_row)\n";
1799 $self->duplicate_row( $from_row - 1, $after_row );
1800 $self->draw_pattern;
1801 },
1802 )->pack(qw/-side left/);
1803  
1804 $f_bottom->Button(
1805 -text => 'Cancel',
1806 -command => sub {
1807 $d->withdraw;
1808 },
1809 )->pack(qw/-side right/);
1810  
1811 $d_info->{dlog} = $d;
1812 $self->{_duplicate_row_dlog} = $d_info;
1813 }
1814  
1815 $self->{_duplicate_row_dlog};
1816 }
1817  
1818 sub insert_dlog
1819 {
1820 my $self = shift;
1821 unless ( defined $self->{_insert_dlog} )
1822 {
1823 $self->{_insert_dlog} = $self->window->Dialog(
1824 -text => 'Insert new card or row?',
1825 -default_button => 'Row',
1826 -buttons => [qw/Row Card Cancel/],
1827 );
1828 }
1829 $self->{_insert_dlog};
1830 }
1831  
1832 sub delete_dlog
1833 {
1834 my $self = shift;
1835 unless ( defined $self->{_delete_dlog} )
1836 {
1837 $self->{_delete_dlog} = $self->window->Dialog(
1838 -text => 'Delete card or row?',
1839 -default_button => 'Row',
1840 -buttons => [qw/Row Card Cancel/],
1841 );
1842 }
1843 $self->{_delete_dlog};
1844 }
1845  
1846 sub insert_card_dlog
1847 {
1848 my $self = shift;
1849 unless ( defined $self->{_insert_card_dlog} )
1850 {
1851 my $d_info = {
1852 dlog => $self->window->Toplevel,
1853 palette => undef,
1854 card_canvas => undef,
1855 where => 'current',
1856 where_card => $self->current_card,
1857 SZ => 'S',
1858 start => 0,
1859 turns => undef,
1860 threading => [ 0, 1, 2, 3 ],
1861 current_hole => 0,
1862 };
1863 my $d = $d_info->{dlog};
1864 $d->title('Insert New Card');
1865 my $f3 = $d->Frame->pack(qw/-side bottom -fill x -expand y/);
1866 $f3->Button(
1867 -text => 'Insert and Close',
1868 -command => sub {
1869 my $after_card =
1870 $d_info->{where} eq 'bottom' ? 0
1871 : $d_info->{where} eq 'current' ? $d_info->{where_card}
1872 : $self->pattern->number_of_cards;
1873 if (
1874 $d_info->{where} eq 'current'
1875 and ( $after_card < 0
1876 or $after_card > $self->pattern->number_of_cards )
1877 )
1878 {
1879 $d->Dialog( -text =>
1880 '"To" card must be between 0 and the number of turns'
1881 )->Show;
1882 return;
1883 }
1884  
1885 my $turns = $d_info->{turns}->get( '1.0', 'end' );
1886 chomp $turns;
1887 print "unreversed turns is :$turns:\n";
1888  
1889 # any other data checks, like turns...
1890 # FIXME
1891 $self->insert_card( $after_card,
1892 join( "", reverse split( //, $turns ) ) );
1893 $self->SZ( $after_card, $d_info->{SZ} );
1894 $self->threading( $after_card, $d_info->{threading} );
1895 $self->start( $after_card, $d_info->{start} );
1896 $self->draw_pattern;
1897 $d->withdraw;
1898 },
1899 )->pack(qw/-side left/);
1900 $f3->Button(
1901 -text => 'Insert and Stay',
1902 -command => sub {
1903 my $after_card =
1904 $d_info->{where} eq 'bottom' ? 0
1905 : $d_info->{where} eq 'current' ? $d_info->{where_card}
1906 : $self->pattern->number_of_cards;
1907 if (
1908 $d_info->{where} eq 'current'
1909 and ( $after_card < 0
1910 or $after_card > $self->pattern->number_of_cards )
1911 )
1912 {
1913 $d->Dialog( -text =>
1914 '"To" card must be between 0 and the number of turns'
1915 )->Show;
1916 return;
1917 }
1918  
1919 my $turns = $d_info->{turns}->get( '1.0', 'end' );
1920 chomp $turns;
1921 print "unreversed turns is :$turns:\n";
1922  
1923 # any other data checks, like turns...
1924 # FIXME
1925 $self->insert_card( $after_card,
1926 join( "", reverse split( //, $turns ) ) );
1927 $self->SZ( $after_card, $d_info->{SZ} );
1928 $self->threading( $after_card, $d_info->{threading} );
1929 $self->start( $after_card, $d_info->{start} );
1930 $self->draw_pattern;
1931 },
1932 )->pack(qw/-side left/);
1933 $f3->Button(
1934 -text => 'Cancel',
1935 -command => sub {
1936 $d->withdraw;
1937 },
1938 )->pack(qw/-side right/);
1939 my $f1r =
1940 $d->Frame( -label => 'Turns' )
1941 ->pack(qw/-side right -fill y -expand y/);
1942 my $turns = $f1r->Scrolled(
1943 'Text',
1944 -scrollbars => 'e',
1945 -height => 40,
1946 -width => 1,
1947 -wrap => 'char',
1948 )->pack(qw/-side top -expand 0 -fill none/);
1949 $turns->delete( '1.0', 'end' );
1950 $turns->insert( '1.0',
1951 join( "", reverse $self->card_turns( $self->current_card ) ) );
1952 my $f1l = $d->Frame( -borderwidth => 1 )->pack(qw/-side top -anchor w/);
1953 my $f2l = $d->Frame( -borderwidth => 1 )->pack(qw/-side top -anchor w/);
1954 my $f3l = $d->Frame( -borderwidth => 1 )->pack(qw/-side top -anchor w/);
1955 my $f4l = $d->Frame->pack(qw/-side top -anchor w/);
1956  
1957 # where to insert - in f1r
1958 $f1l->Radiobutton(
1959 -text => 'Beginning (left)',
1960 -variable => \$d_info->{where},
1961 -value => 'bottom',
1962 )->pack(qw/-side top -anchor w/);
1963 my $f1l1 = $f1l->Frame->pack(qw/-side top -anchor w/);
1964 $f1l1->Radiobutton(
1965 -text => 'After card',
1966 -variable => \$d_info->{where},
1967 -value => 'current',
1968 )->pack(qw/-side left/);
1969 $f1l1->Entry(
1970 -textvariable => \$d_info->{where_card},
1971 -width => 10,
1972 )->pack(qw/-side left/);
1973 $f1l->Radiobutton(
1974 -text => 'End (right)',
1975 -variable => \$d_info->{where},
1976 -value => 'end',
1977 )->pack(qw/-side top -anchor w/);
1978  
1979 # threading direction - f2l
1980 $f2l->Radiobutton(
1981 -text => 'Threaded S',
1982 -variable => \$d_info->{SZ},
1983 -value => 'S',
1984 )->pack(qw/-side top -anchor w/);
1985 $f2l->Radiobutton(
1986 -text => 'Threaded Z',
1987 -variable => \$d_info->{SZ},
1988 -value => 'Z',
1989 )->pack(qw/-side top -anchor w/);
1990  
1991 # start hole and threading - f3l
1992 my $card_canvas = $self->_card_canvas( $f3l, \$d_info->{current_hole},
1993 \$d_info->{start}, 0 )->pack(qw/-side top/);
1994  
1995 # put up the palette - f4l
1996 my $h = $self->_palette( $f4l, $card_canvas, \$d_info->{current_hole},
1997 $d_info->{threading}, 'new' );
1998  
1999 $d_info->{turns} = $turns;
2000 $d_info->{card_canvas} = $card_canvas;
2001 $d_info->{palette} = $h;
2002 $self->{_insert_card_dlog} = $d_info;
2003 }
2004 $self->{_insert_card_dlog};
2005 }
2006  
2007 sub _card_canvas # (window, \current_hole, \start_hole, update_pattern?)
2008 {
2009 my $self = shift;
2010 my $win = shift;
2011 my $current_hole = shift;
2012 my $start = shift;
2013 my $update = shift || 0;
2014  
2015 my $card_canvas = $win->Canvas(
2016 -height => 200,
2017 -width => 200,
2018 -background => 'white',
2019 );
2020  
2021 # focus areas
2022 $card_canvas->createRectangle(
2023 10, 10, 90, 90,
2024 -fill => 'white',
2025 -outline => 'white',
2026 -tags => [ 'frect', 'focusrect0', 'A' ],
2027 );
2028 $card_canvas->createRectangle(
2029 110, 10, 190, 90,
2030 -fill => 'white',
2031 -outline => 'white',
2032 -tags => [ 'frect', 'focusrect1', 'B' ],
2033 );
2034 $card_canvas->createRectangle(
2035 110, 110, 190, 190,
2036 -fill => 'white',
2037 -outline => 'white',
2038 -tags => [ 'frect', 'focusrect2', 'C' ],
2039 );
2040 $card_canvas->createRectangle(
2041 10, 110, 90, 190,
2042 -fill => 'white',
2043 -outline => 'white',
2044 -tags => [ 'frect', 'focusrect3', 'D' ],
2045 );
2046  
2047 # the hole labels
2048 $card_canvas->createText(
2049 45, 30,
2050 -text => 'A',
2051 -anchor => 's',
2052 -tags => ['A'],
2053 );
2054 $card_canvas->createText(
2055 155, 30,
2056 -text => 'B',
2057 -anchor => 's',
2058 -tags => ['B'],
2059 );
2060 $card_canvas->createText(
2061 155, 140,
2062 -text => 'C',
2063 -anchor => 's',
2064 -tags => ['C'],
2065 );
2066 $card_canvas->createText(
2067 45, 140,
2068 -text => 'D',
2069 -anchor => 's',
2070 -tags => ['D'],
2071 );
2072  
2073 # the holes
2074 $card_canvas->createOval(
2075 30, 30, 60, 60,
2076 -fill => 'white',
2077 -outline => 'black',
2078 -tags => [ 'hole', 'A', 'hole0' ],
2079 );
2080 $card_canvas->createOval(
2081 140, 30, 170, 60,
2082 -fill => 'white',
2083 -outline => 'black',
2084 -tags => [ 'hole', 'B', 'hole1' ],
2085 );
2086 $card_canvas->createOval(
2087 140, 140, 170, 170,
2088 -fill => 'white',
2089 -outline => 'black',
2090 -tags => [ 'hole', 'C', 'hole2' ],
2091 );
2092 $card_canvas->createOval(
2093 30, 140, 60, 170,
2094 -fill => 'white',
2095 -outline => 'black',
2096 -tags => [ 'hole', 'D', 'hole3' ],
2097 );
2098  
2099 # current hole indicator
2100 my $change_focus = sub {
2101 foreach my $h ( 0 .. 3 )
2102 {
2103 $card_canvas->itemconfigure(
2104 "focusrect$h",
2105 -fill => 'white',
2106 -outline => 'white'
2107 );
2108 }
2109 $card_canvas->itemconfigure(
2110 "focusrect$$current_hole",
2111 -fill => 'gold',
2112 -outline => 'black'
2113 );
2114 };
2115  
2116 # show the current hole
2117 &$change_focus;
2118  
2119 foreach my $h ( 0 .. 3 )
2120 {
2121 $card_canvas->bind(
2122 chr( ord('A') + $h ),
2123 '<1>',
2124 sub {
2125 $$current_hole = $h;
2126 &$change_focus;
2127 }
2128 );
2129 }
2130  
2131 # start hole indicators
2132 $card_canvas->createText(
2133 100, 100,
2134 -text => 'Start hole',
2135 -anchor => 'center',
2136 );
2137 my $start_sub = $update
2138 ? sub {
2139 $self->start( $self->current_card, $$start );
2140 $self->update_pattern( $self->current_card );
2141 }
2142 : sub { };
2143  
2144 foreach my $coords ( [ 0, 80, 80 ], [ 1, 120, 80 ], [ 2, 120, 120 ],
2145 [ 3, 80, 120 ] )
2146 {
2147 $card_canvas->createWindow(
2148 $$coords[1],
2149 $$coords[2],
2150 -anchor => 'center',
2151 -window => $card_canvas->Radiobutton(
2152 -text => '',
2153 -value => $$coords[0],
2154 -command => $start_sub,
2155 -variable => $start,
2156 ),
2157 );
2158 }
2159  
2160 $card_canvas;
2161 }
2162  
2163 sub delete_card_dlog
2164 {
2165 my $self = shift;
2166 unless ( defined $self->{_delete_card_dlog} )
2167 {
2168 my $d_info = {
2169 dlog => undef,
2170 where => 'current',
2171 where_card => undef,
2172 };
2173 my $d = $self->window->Toplevel;
2174 $d->title('delete Card');
2175 $d->Radiobutton(
2176 -text => 'Beginning (left)',
2177 -variable => \$d_info->{where},
2178 -value => 'bottom',
2179 )->pack(qw/-side top -anchor w/);
2180 my $f1 = $d->Frame->pack(qw/-side top -anchor w/);
2181 $f1->Radiobutton(
2182 -text => 'card',
2183 -variable => \$d_info->{where},
2184 -value => 'current',
2185 )->pack(qw/-side left/);
2186 $f1->Entry(
2187 -textvariable => \$d_info->{where_card},
2188 -width => 10,
2189 )->pack(qw/-side left/);
2190 $d->Radiobutton(
2191 -text => 'End (right)',
2192 -variable => \$d_info->{where},
2193 -value => 'end',
2194 )->pack(qw/-side top -anchor w/);
2195  
2196 $d->Button(
2197 -text => 'OK',
2198 -command => sub {
2199 my $after_card =
2200 $d_info->{where} eq 'bottom' ? 1
2201 : $d_info->{where} eq 'current' ? $d_info->{where_card}
2202 : $self->pattern->number_of_cards;
2203 if (
2204 $d_info->{where} eq 'current'
2205 and ( $after_card < 0
2206 or $after_card > $self->pattern->number_of_cards )
2207 )
2208 {
2209 $d->Dialog( -text =>
2210 'card must be between 1 and the number of turns' )
2211 ->Show;
2212 return;
2213 }
2214 print "delete_card($after_card-1)\n";
2215 $self->canvas->delete(
2216 "card" . ( $self->pattern->number_of_cards - 1 ) );
2217 $self->pattern->delete_card( $after_card - 1 );
2218 $self->current_card( $self->pattern->number_of_cards - 1 )
2219 if $self->current_card >= $self->pattern->number_of_cards;
2220 $self->draw_pattern;
2221 $d->withdraw;
2222 },
2223 )->pack(qw/-side left/);
2224  
2225 $d->Button(
2226 -text => 'Cancel',
2227 -command => sub {
2228 $d->withdraw;
2229 },
2230 )->pack(qw/-side right/);
2231  
2232 $d_info->{dlog} = $d;
2233 $self->{_delete_card_dlog} = $d_info;
2234 }
2235  
2236 $self->{_delete_card_dlog};
2237 }
2238  
2239 sub duplicate_card_dlog
2240 {
2241 my $self = shift;
2242 unless ( defined $self->{_duplicate_card_dlog} )
2243 {
2244 my $d_info = {
2245 dlog => undef,
2246 from => 'current',
2247 from_card => undef,
2248 after => 'current',
2249 after_card => undef,
2250 };
2251 my $d = $self->window->Toplevel;
2252 $d->title('duplicate a card');
2253 my $f_bottom = $d->Frame->pack(qw/-side bottom -fill x -expand y/);
2254 my $f_left = $d->Frame( -label => 'From' )->pack(qw/-side left/);
2255 my $f_right = $d->Frame( -label => 'After' )->pack(qw/-side right/);
2256 $f_left->Radiobutton(
2257 -text => 'Beginning (left)',
2258 -variable => \$d_info->{from},
2259 -value => 'bottom',
2260 )->pack(qw/-side top -anchor w/);
2261 my $f1 = $f_left->Frame->pack(qw/-side top -anchor w/);
2262 $f1->Radiobutton(
2263 -text => 'card',
2264 -variable => \$d_info->{from},
2265 -value => 'current',
2266 )->pack(qw/-side left/);
2267 $f1->Entry(
2268 -textvariable => \$d_info->{from_card},
2269 -width => 10,
2270 )->pack(qw/-side left/);
2271 $f_left->Radiobutton(
2272 -text => 'End (right)',
2273 -variable => \$d_info->{from},
2274 -value => 'end',
2275 )->pack(qw/-side top -anchor w/);
2276  
2277 $f_right->Radiobutton(
2278 -text => 'Beginning (left)',
2279 -variable => \$d_info->{from},
2280 -value => 'bottom',
2281 )->pack(qw/-side top -anchor w/);
2282 my $f2 = $f_right->Frame->pack(qw/-side top -anchor w/);
2283 $f2->Radiobutton(
2284 -text => 'card',
2285 -variable => \$d_info->{from},
2286 -value => 'current',
2287 )->pack(qw/-side left/);
2288 $f2->Entry(
2289 -textvariable => \$d_info->{from_card},
2290 -width => 10,
2291 )->pack(qw/-side left/);
2292 $f_right->Radiobutton(
2293 -text => 'End (right)',
2294 -variable => \$d_info->{from},
2295 -value => 'end',
2296 )->pack(qw/-side top -anchor w/);
2297  
2298 $f_bottom->Button(
2299 -text => 'Done',
2300 -command => sub {
2301 my $from_card =
2302 $d_info->{from} eq 'bottom' ? 1
2303 : $d_info->{from} eq 'current' ? $d_info->{from_card}
2304 : $self->pattern->number_of_cards;
2305 if ( $d_info->{from} eq 'current'
2306 and
2307 ( $from_card < 0 or $from_card > $self->pattern->number_of_cards ) )
2308 {
2309 $d->Dialog( -text =>
2310 '"From" card must be between 1 and the number of turns'
2311 )->Show;
2312 return;
2313 }
2314 my $after_card =
2315 $d_info->{after} eq 'bottom' ? 0
2316 : $d_info->{after} eq 'current' ? $d_info->{after_card}
2317 : $self->pattern->number_of_cards;
2318 if (
2319 $d_info->{after} eq 'current'
2320 and ( $after_card < 0
2321 or $after_card > $self->pattern->number_of_cards )
2322 )
2323 {
2324 $d->Dialog( -text =>
2325 '"To" card must be between 0 and the number of turns'
2326 )->Show;
2327 return;
2328 }
2329 print "duplicate_card($from_card-1, $after_card)\n";
2330 $self->duplicate_card( $from_card - 1, $after_card );
2331 $self->draw_pattern;
2332 $d->withdraw;
2333 },
2334 )->pack(qw/-side left/);
2335 $f_bottom->Button(
2336 -text => 'Stay',
2337 -command => sub {
2338 my $from_card =
2339 $d_info->{from} eq 'bottom' ? 1
2340 : $d_info->{from} eq 'current' ? $d_info->{from_card}
2341 : $self->pattern->number_of_cards;
2342 if ( $d_info->{from} eq 'current'
2343 and
2344 ( $from_card < 0 or $from_card > $self->pattern->number_of_cards ) )
2345 {
2346 $d->Dialog( -text =>
2347 '"From" card must be between 1 and the number of turns'
2348 )->Show;
2349 return;
2350 }
2351 my $after_card =
2352 $d_info->{after} eq 'bottom' ? 0
2353 : $d_info->{after} eq 'current' ? $d_info->{after_card}
2354 : $self->pattern->number_of_cards;
2355 if (
2356 $d_info->{after} eq 'current'
2357 and ( $after_card < 0
2358 or $after_card > $self->pattern->number_of_cards )
2359 )
2360 {
2361 $d->Dialog( -text =>
2362 '"To" card must be between 0 and the number of turns'
2363 )->Show;
2364 return;
2365 }
2366 print "duplicate_card($from_card-1, $after_card)\n";
2367 $self->duplicate_card( $from_card - 1, $after_card );
2368 $self->draw_pattern;
2369 },
2370 )->pack(qw/-side left/);
2371  
2372 $f_bottom->Button(
2373 -text => 'Cancel',
2374 -command => sub {
2375 $d->withdraw;
2376 },
2377 )->pack(qw/-side right/);
2378  
2379 $d_info->{dlog} = $d;
2380 $self->{_duplicate_card_dlog} = $d_info;
2381 }
2382  
2383 $self->{_duplicate_card_dlog};
2384 }
2385  
2386 #sub AUTOLOAD
2387 #{
2388 # print "AUTOLOAD: ", join("\n", $AUTOLOAD, @_, '');
2389 #}
2390  
2391 # Autoload methods go after =cut, and are processed by the autosplit program.
2392  
2393 1;
2394 __END__
« script/snartemo.pl ↑ Summary lib/Weaving/Tablet/Twill.pm »