#!/opt/bin/perl

use Gtk2 -init;
use Gtk2::Gdk::Keysyms;

use Gtk2::CV::ImageWindow;
use Gtk2::CV::Schnauzer;

use Gtk2::CV;

Gtk2::Rc->parse (Gtk2::CV::find_rcfile "gtkrc");

my $viewer;
my $schnauzer;

sub new_schnauzer {
   my $w = new Gtk2::Window;
   $w->add (my $s = new Gtk2::CV::Schnauzer);

   $s->signal_connect (activate => sub { $viewer->load_image ($_[1]) });
   $s->signal_connect_after (key_press_event => \&std_keys);

   $s;
}

$schnauzer = new_schnauzer;

my $mainwin = $schnauzer->get_toplevel;

sub std_keys {
   my $key = $_[1]->keyval;
   my $state = $_[1]->state;

   my $ctrl = grep $_ eq "control-mask", @$state;

   if ($key == $Gtk2::Gdk::Keysyms{q}) {
      main_quit Gtk2;
   } elsif ($ctrl && $key == $Gtk2::Gdk::Keysyms{v}) {
      my $s = new_schnauzer;
      $s->set_dir (".");
      $s->get_toplevel->show_all
   } else {
      #$mainwin->show_all;
      $schnauzer->handle_key ($key, $state);
   }

   1;
}

$viewer = new Gtk2::CV::ImageWindow;

$viewer->set_title ("CV");

$viewer->signal_connect (key_press_event => \&std_keys);
$viewer->signal_connect (delete_event => sub { main_quit Gtk2 });

$viewer->signal_connect (button3_press_event => sub {
   $mainwin->visible
      ? $mainwin->hide
      : $mainwin->show_all;
});

if (@ARGV) {
   $schnauzer->set_paths (\@ARGV);
   $schnauzer->handle_key ($Gtk2::Gdk::Keysyms{space}, []);
} else {
   $schnauzer->set_dir (".");
   $mainwin->show_all;
}

$viewer->show_all;

main Gtk2;

=head1 NAME

cv - a fast gtk+ image viewer modeled after xv

=head1 SYNOPSIS

   cv [file...]

=head1 DESCRIPTION

None yet.

=head2 THE IMAGE WINDOW

You can use the following keys in the image window:

 q       quit the program
 <       half the image size
 >       double the image size
 ,       shrink the image by 10%
 .       enlarge the image by 10%
 n       reset to normal size
 m       maximize to screensize
 M       maxime to screensize, respecting image aspect
 u       uncrop
 r       set scaling mode to 'nearest' (fastest)
 s       set scaling mode to 'bilinear' (default)
 S       set scaling mode to 'hyper' (slowest)
 t       rotate clockwise 90°
 T       rotate counterclockwise°
 ctrl-v  open a new visual schnauzer window for the current dir

The following keys are redirected to the default visual schnauzer window:

 space     next image
 backspace last image

=head2 THE VISUAL SCHNAUZER

You can use the following keys in the schnauzer window:

 space       move to and display next image
 backspace   move to and display previous image
 return      display selected picture

 cursor keys move selection
 page-up     move one page up
 page-down   move one page down
 home        move to first file
 end         move to last file

 ctrl-g      generate icons for the selected files
 ctrl-d      delete selected files WITHOUT ASKING AGAIN

=head1 BUGS/TODO

 rotate on disk
 print
 lots of ui issues
 save(?)
 preferences
 ctrl-u in schnauzer
 shift-cursor in schnauzer

=head1 AUTHOR

Marc Lehmann <cv@plan9.de>.

=cut

