use strict;
use warnings;
use Tk;

my $mw;
my $win;
my $lab;


sub openWin {
	if( $win ) {
		$win->deiconify;
		$win->wm('state', 'normal');
	} else {
		eval { $win->destroy } if $win;
		$win = $mw->Toplevel;
		$win->Label(-text => "This is the window being manipulated")
			->pack(-fill => 'both', -expand => 1);
		$lab->configure(-text => "The window object is:\n$win");
	}
}


sub closeWin {
	return unless $win;
	$win->destroy;
	$lab->configure(-text => '');
	undef $win;
}


sub minimizeWin {
	return unless $win;
	$win->iconify;
}


sub maximizeWin {
	return unless $win;
	$win->wm('state', 'zoomed');
	eval { $win->wmAttribute(-zoomed => 1) }; 
}


sub moveWin {
	return unless $win;
	my ($x, $y) = $win->geometry() =~ /\+(\d+)\+(\d+)\z/ or die;
	$_ += 10 for $x, $y;
	$win->geometry("+$x+$y");
}


sub resizeWin {
	return unless $win;
	my ($w, $h) = $win->geometry() =~ /^(\d+)x(\d+)/ or die;
	$_ += 10 for $w, $h;
	$win->geometry($w . "x" . $h);
}

$mw = MainWindow->new;
for my $label0 ($mw->Label(-text => 'Window handle:')) {
	$lab = $mw->Label(-text => '');
	$label0->grid($lab);
}

my @binit = ('Open/Restore' => \&openWin, Close => \&closeWin,
	Minimize => \&minimizeWin, Maximize => \&maximizeWin,
	Move => \&moveWin, Resize => \&resizeWin);

while( my ($text, $callback) = splice @binit, 0, 2 ) {
	$mw->Button(-text => $text, -command => $callback)->grid('-');
}

MainLoop();

__END__