1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#!/usr/local/bin/perl -w
use Tk;
{
package Packer;
use base qw(Tk::Frame);
Construct Tk::Widget 'Packer';
sub new
{my $class = shift;
my $parent = shift;
my $obj = $parent->Frame('-class' => Packer);
$obj->{Slaves} = [];
$obj->{LayoutPending} = 0;
bless $obj,$class;
}
sub Layout
{
my $m = shift;
$m->{LayoutPending} = 0;
my $w = 0;
my $h = 0;
my $x = 0;
my $y = 0;
my $s;
# find size of largest slave
foreach $s (@{$m->{Slaves}})
{
my $sw = $s->ReqWidth;
my $sh = $s->ReqHeight;
$w = $sw if ($sw > $w);
$h = $sh if ($sh > $h);
}
# Set size and position of slaves
foreach $s (@{$m->{Slaves}})
{
$s->MoveResizeWindow($x,$y,$w,$h);
$s->MapWindow;
$y += $h;
}
# Now ask for enough space
$m->GeometryRequest($w,$y);
}
sub QueueLayout
{
my $m = shift;
$m->DoWhenIdle(['Layout',$m]) unless ($m->{LayoutPending}++);
}
sub SlaveGeometryRequest
{
shift->QueueLayout;
}
sub LostSlave
{
my ($m,$s) = @_;
@{$m->{Slaves}} = grep($_ != $s,@{$m->{Slaves}});
$m->QueueLayout;
}
sub Manage
{
my ($m,$s) = @_;
$m->ManageGeometry($s);
push(@{$m->{Slaves}},$s);
$m->QueueLayout;
}
}
$top = MainWindow->new();
$packer = $top->Packer();
$var = "This can grow";
$one = $top->Label('-text' => 'Label One');
$packer->Manage($one);
$packer->Manage($packer->Label('-textvariable' => \$var));
$packer->Manage($packer->Label('-text' => 'A Very Long Label'));
$packer->pack;
$top->Button('-text' => 'Grow', '-command' => sub { $var .= " more" })->pack;
$top->Button('-text' => 'Loose', '-command' => [ 'pack' , $one])->pack;
$top->Button('-text' => 'Add', '-command' => [
sub
{
my $packer = shift;
$packer->Manage($packer->Label('-text' => 'Added thing'));
}, $packer ])->pack;
$top->Button('-text' => 'Quit', '-command' => [ 'destroy' , $top ])->pack;
MainLoop;
|