#!/usr/bin/perl -w
######################################################################
# img.pl
######################################################################
# Perl Power! - Michael Schilli 1998
######################################################################

use Tk;
use Fs;

my $top = MainWindow->new();
                                        # initialize file selector
my $fs = Fs->new($top, \&load_image, "Select an image");

                                        # menu bar and pulldowns
$menubar   = $top->Frame(-relief => 'raised', -bd => 2); 
$menu_file = $menubar->Menubutton(-text => "File", 
                                  -underline => 0); 
$menu_file->command(-label => "Load", 
		    -command => sub { $fs->start("."); });
$menu_file->command(-label => "Exit", 
                    -command => sub { exit 0 });

$canvas  = $top->Canvas();
$photo   = $top->Photo();
$canvas->create('image', 0, 0, -image => $photo, 
                               -anchor => 'nw');

my $yscrollbar = $top->Scrollbar(-command => 
                                 ['yview', $canvas],
			         -orient => 'vertical');
my $xscrollbar = $top->Scrollbar(-command => 
                                 ['xview', $canvas], 
			         -orient => 'horizontal');

$canvas->configure(-xscrollcommand => ['set', $xscrollbar],
		   -yscrollcommand => ['set', $yscrollbar]);

$menubar->pack(-expand => 'yes', -fill => 'x', -anchor => 'n');
$menu_file->pack(-anchor => 'w');

$yscrollbar->pack(-side => 'right', -fill => 'y');
$xscrollbar->pack(-side => 'bottom', -fill => 'x');

$canvas->pack(-expand => 'yes', -fill => 'both', 
              -anchor => 's');

MainLoop;

######################################################################
# load_image callback function
######################################################################
sub load_image {
    my $file = shift;

    $photo->configure(-file => $file);

    my $newwidth  = $photo->width;
    my $newheight = $photo->height;

    $canvas->configure(-scrollregion => 
                       [0, 0, $newwidth, $newheight],
		       -width => $newwidth, 
		       -height => $newheight);
}

