| 1 | # Tk::ToolBar - a draggable ToolBar widget | ||
| 2 | |||
| 3 | #!perl -w | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Tk; | ||
| 7 | use Tk::ToolBar; | ||
| 8 | #use Tk::LabOptionmenu; | ||
| 9 | |||
| 10 | my $mw = MainWindow->new; | ||
| 11 | my $tb = $mw->ToolBar; | ||
| 12 | |||
| 13 | my $cv = $mw->Canvas(qw/-bg black/)->pack(qw/-fill both -expand 1/); | ||
| 14 | $cv->createPolygon(0, 0, 50, 100, 100, 0, 0, 50, 100, 50, 0, 0, | ||
| 15 | -fill => 'white'); | ||
| 16 | |||
| 17 | $mw->Button(-text => 'See built-in images', | ||
| 18 | -command => \&seeBuiltIns, | ||
| 19 | )->pack; | ||
| 20 | |||
| 21 | for my $img (qw/filenew22 fileopen22 filesave22/) { | ||
| 22 | $tb->ToolButton(-image => $img, | ||
| 23 | -tip => $img); | ||
| 24 | } | ||
| 25 | |||
| 26 | $tb->separator; | ||
| 27 | |||
| 28 | for my $img (qw/editcopy22 editcut22 editpaste22 editdelete22/) { | ||
| 29 | $tb->ToolButton(-image => $img, | ||
| 30 | -tip => $img); | ||
| 31 | } | ||
| 32 | |||
| 33 | $tb->separator; | ||
| 34 | |||
| 35 | $tb->ToolButton(-image => 'actexit22', | ||
| 36 | -tip => 'Quit', | ||
| 37 | -command => [$mw, 'destroy']); | ||
| 38 | |||
| 39 | MainLoop; | ||
| 40 | |||
| 41 | sub seeBuiltIns { | ||
| 42 | #my @files = qw/tkIcons tkIcons.crystal tkIcons.klassic/; | ||
| 43 | my @files = qw/tkIcons/; | ||
| 44 | |||
| 45 | my $t = $mw->Toplevel; | ||
| 46 | $t->title("Tk::ToolBar's built-in images"); | ||
| 47 | |||
| 48 | my $g = $t->Frame->pack(qw/-side top -fill x/); | ||
| 49 | $t->Label(-text => 'Click on icon to see its name')->pack(qw/-side top -expand 1/); | ||
| 50 | |||
| 51 | my %frame; | ||
| 52 | $frame{$_} = $t->Frame for @files; | ||
| 53 | |||
| 54 | my ($sel, $cur); | ||
| 55 | |||
| 56 | $g->Button(-text => 'Close Window', | ||
| 57 | -command => [$t => 'destroy'], | ||
| 58 | )->pack(qw/-side left -expand 1/); | ||
| 59 | |||
| 60 | $g->Label(-text => 'Icon Style')->pack(qw/-side left/); | ||
| 61 | $g->Optionmenu( | ||
| 62 | #-label => 'Icon Style', | ||
| 63 | -options => \@files, | ||
| 64 | -variable => \$sel, | ||
| 65 | -command => sub { | ||
| 66 | $frame{$cur}->packForget if $cur; | ||
| 67 | $frame{$sel}->pack(qw/-side bottom/); | ||
| 68 | $cur = $sel; | ||
| 69 | })->pack(qw/-side left/); | ||
| 70 | |||
| 71 | for my $ff (@files) { | ||
| 72 | my $frame = $frame{$ff}; | ||
| 73 | my $file = Tk->findINC("ToolBar/$ff"); | ||
| 74 | die "ERROR: Can't find tkIcons!\n" unless defined $file; | ||
| 75 | |||
| 76 | open my $fh, $file or die $!; | ||
| 77 | |||
| 78 | my %icons; | ||
| 79 | |||
| 80 | while (<$fh>) { | ||
| 81 | chomp; | ||
| 82 | my ($n, $d) = (split /:/)[0, 4]; | ||
| 83 | |||
| 84 | $icons{$n} = $mw->Photo("$ff.$n", -data => $d); | ||
| 85 | } | ||
| 86 | |||
| 87 | close $fh; | ||
| 88 | |||
| 89 | my $selected = 'None'; | ||
| 90 | $frame->Label(-textvariable => \$selected)->pack(qw/-side top/); | ||
| 91 | |||
| 92 | my $f = $frame->Frame->pack(qw/-fill both -expand 1/); | ||
| 93 | |||
| 94 | my $r = my $c = 0; | ||
| 95 | my %labels; | ||
| 96 | |||
| 97 | for my $n (sort keys %icons) { | ||
| 98 | my $l = $f->Label( | ||
| 99 | -image => "$ff.$n", | ||
| 100 | )->grid(-column => $c, | ||
| 101 | -row => $r, | ||
| 102 | ); | ||
| 103 | |||
| 104 | $labels{$n} = $l; | ||
| 105 | |||
| 106 | $l->bind('<1>' => sub { | ||
| 107 | if ($selected ne 'None') { | ||
| 108 | $labels{$selected}->configure(-borderwidth => 0, -bg => Tk::ACTIVE_BG);#defaultthing); | ||
| 109 | } | ||
| 110 | |||
| 111 | $selected = $n; | ||
| 112 | $l->configure(-borderwidth => 2, -bg => 'white'); | ||
| 113 | }); | ||
| 114 | |||
| 115 | $c++; | ||
| 116 | if ($c == 20) { | ||
| 117 | $r++; | ||
| 118 | $c = 0; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | } | ||
| 122 | } |