#!/usr/bin/perl -w

# include perl packages
use strict;
use warnings;
use diagnostics;

use Tk;
use Tk::FileSelect;
use File::Basename;

my $xnview_slideshow_header = 
'# Slide Show Sequence
Loop = 1
FullScreen = 1
TitleBar = 0
View = 1
CenterWindow = 0
ReadErrors = 1
HideMouse = 1
RandomOrder = 0
ShowFilename = 0
BackgroundColor = 0';


my $nr = @ARGV;

if ($nr < 1) {
  print "Error: $0 called with just $nr arguments\n";
  usage();
  exit();
}


my $dir = $ARGV[0];
if (-d $dir) {
  print "Error: first argument ($dir) is a valid directory.\nThis Mapivi Plug-In is developed for Mapivi version >= 0.7.5\n";
  usage();
  exit();
}

my $top = MainWindow->new;
$top->title("MaPiVi Plugin file list");

my $file = glob("~/tmp/filelist");

labeledEntryButton($top,"top",24,"path/name of file list","Set",\$file);

my $f1 = $top->Frame()->pack(-anchor => 'w');
my $separator = "\n";
$f1->Label(-text => "separator")->pack(-side => 'left');
$f1->Optionmenu(-options => [ ['newline' => "\n"],['newline and quotas' => "\"\n\""],['comma', ','],['comma and space', ', '],['comma and newline', ",\n"]], -variable => \$separator, -textvariable => \$separator)->pack(-side => 'left');

my $write_xnview_slideshow = 0;
$top->Checkbutton(-variable => \$write_xnview_slideshow,
				  -text => "Write XnView slide show file (*.sld)",
				  -command => sub {
					  # change to separator needed from XnView
					  $separator = "\"\n\"";
					  # add right file extension *.sld
					  $file .= '.sld' if ( $file !~ m/.*\.sld/);
				  }
				  )->pack(-anchor => 'w');

my $f2 = $top->Frame()->pack(-anchor => 'w');
$f2->Button(-text => "export file list", -command => \&export)->pack(-side => 'left');
$f2->Button(-text => "exit plugin", -command => \&exit)->pack(-side => 'left');

$top->MainLoop;


sub export {
  if (-f $file) {
	my $rc = $top->messageBox(-icon  => 'warning', -message => "file $file exist. Ok to overwrite?",
					 -title => "Plugin",   -type    => "OKCancel");
	return if ($rc !~ m/Ok/i);
  }

  if (!open(FILE, ">$file")) {
	print "could not open $file for write access!: $!\n";
	return;
  }

  if ($write_xnview_slideshow) {
	  print FILE "$xnview_slideshow_header\n";
  }

  print FILE '"' if ($separator eq "\"\n\"");

  for (0 .. $nr-1) {
	$separator = "\"\n"  if (($_ == $nr-1) and ($separator eq "\"\n\""));
	print FILE $ARGV[$_].$separator;
  }

  close FILE;

  print "Plugin finished successfully!\n";
  exit();
}

##############################################################
# usage
##############################################################
sub usage {
  my $prog = basename($0);
  print "\nUsage: $prog file1 [file2] [file3] [...]\n\n";
  print "This is a example plugin for mapivi (see http://mapivi.de.vu)\n";
  print "It will just write a file list (the selected pictures from mapivi)\n";
  print "Author:  Martin Herrmann <martin-herrmann\@gmx.de>\n";
  print "License: GNU General Public License, version 2\n";
}


##############################################################
# labeledEntryButton - build a frame containing a labeled entry
#                      and a button with a file selector
##############################################################
sub labeledEntryButton {

  # input values
  my ($parentWidget, $position, $width, $label, $buttext, $varRef, $dir) = @_;
  my $frame = labeledEntry($parentWidget, $position, $width, $label, $varRef);
  setFileButton($frame,"right",$buttext,$label,$varRef, $dir);
  return $frame;
}

##############################################################
# labeledEntry - build a frame containing a labeled entry
##############################################################
sub labeledEntry {

  # input values
  my ($parentWidget, $position, $width, $label, $varRef) = @_;

  my $frame =
	$parentWidget->Frame(-relief=>"groove", -bd => 2)->pack(-side => $position, -fill => "x", -padx => 3, -pady => 3);

  $frame->Label(-text   => $label,
				-width  => $width,
				-anchor => "w",
			   )->pack(-side => "left", -padx => 3);

  my $entry =
	$frame->Entry(-textvariable => $varRef,
				  -width        => $width,
				 )->pack(-side => "left", -fill => "x", -expand => "1", -padx => 1);
  $entry->xview("end");
  $entry->icursor("end");

  return $frame;
}

##############################################################
# setFileButton - open a file selector and set file name
##############################################################
sub setFileButton {

  # input values
  my ($parentWidget, $position, $butlabel, $fileselLabel, $varRef, $dir) = @_;
  # $dir is optional, if defined and true a dir will be selected instead of a file

  $parentWidget->Button(-text => $butlabel,
						-command => sub {
						  my $fileSelect = $top->FileSelect(-title => $fileselLabel,
															-directory => dirname($$varRef),
															-width => 30, -height => 30);
						  my $file = $fileSelect->Show;
						  if (defined $file and $file ne "") {
							if (-f $file) {
							  $$varRef = $file;
							}
						  }
						},
					   )->pack(-side => $position);
}

# Tell Emacs that this is really a perl script
# Local Variables:
# mode:perl
# End:
