#! /usr/bin/perl

#Copyright (C) Mario Kemper 2008 <mario.kemper@googlemail.com>  Fri, 12 Sep 2008 00:50:28 +0200

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.

use utf8;
use strict;
use warnings;
use Gtk2 '-init';
use Image::Magick;
use POSIX qw/setlocale/;
use Locale::gettext;
use Glib qw/TRUE FALSE/;
use FindBin '$Bin';    #path where plugin is located
use File::Temp qw/ tempfile tempdir /;

#configure gettext using ENV Variable (setup during gscrot start)
setlocale( LC_MESSAGES, "" );
my $d = Locale::gettext->domain( "gscrot-plugins-bash" );
$d->dir( $ENV{ 'GSCROT_INTL' } );

binmode( STDOUT, ":utf8" );
#gscrot will ask for some infos - no need of changing anything
if ( $ARGV[ 0 ] eq "name" ) {
	print $d->get( "polaroid" );
	exit;
} elsif ( $ARGV[ 0 ] eq "sort" ) {
	print $d->get( "effect" );
	exit;
} elsif ( $ARGV[ 0 ] eq "tip" ) {
	print $d->get(
		"make your screenshot look like a polaroid photo, add a caption, and even rotate it a little"
	);
	exit;
} elsif ( $ARGV[ 0 ] eq "ext" ) {
	print "png";
	exit;
} elsif ( $ARGV[ 0 ] eq "lang" ) {
	print "perl";
	exit;
}

#these variables are passed to the plugin
my $socket_id = $ARGV[ 0 ];
my $filename  = $ARGV[ 1 ];
my $width     = $ARGV[ 2 ];
my $height    = $ARGV[ 3 ];
my $filetype  = $ARGV[ 4 ];

#check convert command
my $use_caption = FALSE;
if ( `convert --help | grep caption` =~ /caption/ ) {
	$use_caption = TRUE;
}

my $plug = Gtk2::Plug->new( $socket_id );
$plug->set_border_width( 10 );

$plug->signal_connect( destroy => sub { Gtk2->main_quit } );

#variables used in this plugin
my $width_preview  = 0;
my $height_preview = 0;

#configure buttons and other needed controls
my $caption_frame = Gtk2::Frame->new( $d->get( "Caption:" ) );

my $caption_label = Gtk2::Label->new( $d->get( "Text:" ) );
my $caption_entry = Gtk2::Entry->new();
$caption_entry->set_text( "$ENV{ 'USER' }" );

my $pointsize_label = Gtk2::Label->new( $d->get( "Font size:" ) );
my $pointsize_sbutton = Gtk2::SpinButton->new_with_range( 1, 200, 1 );
$pointsize_sbutton->set_value( 40 );

my $gravity_label = Gtk2::Label->new( $d->get( "Gravity:" ) );
my $gravity_combo = Gtk2::ComboBox->new_text;
$gravity_combo->insert_text( 0, "NorthWest" );
$gravity_combo->insert_text( 1, "North" );
$gravity_combo->insert_text( 2, "NorthEast" );
$gravity_combo->insert_text( 3, "West" );
$gravity_combo->insert_text( 4, "Center" );
$gravity_combo->insert_text( 5, "East" );
$gravity_combo->insert_text( 6, "SouthWest" );
$gravity_combo->insert_text( 7, "South" );
$gravity_combo->insert_text( 8, "SouthEast" );
$gravity_combo->set_active( 4 );

my $angle_label = Gtk2::Label->new( $d->get( "Rotation:" ) );
my $angle_sbutton = Gtk2::SpinButton->new_with_range( -90, 90, 1 );
$angle_sbutton->set_value( -5 );

my $refresh_btn = Gtk2::Button->new_from_stock( 'gtk-refresh' );
$refresh_btn->signal_connect( 'clicked', \&fct_imagemagick_polaroid,
							  'refresh' );

my $preview =
	Gtk2::Image->new_from_pixbuf(
	   Gtk2::Gdk::Pixbuf->new_from_file_at_scale( $filename, 300, 300, TRUE ) );


my $save_btn = Gtk2::Button->new_from_stock( 'gtk-save' );
$save_btn->signal_connect( 'clicked', \&fct_imagemagick_polaroid, 'save' );

my $cancel_btn = Gtk2::Button->new_from_stock( 'gtk-cancel' );
$cancel_btn->signal_connect( 'clicked' => sub { Gtk2->main_quit }, 'cancel' );

#define the gui layout
my $vbox_param = Gtk2::VBox->new( TRUE,  10 );
my $hbox1      = Gtk2::HBox->new( FALSE, 10 );
my $hbox2      = Gtk2::HBox->new( FALSE, 10 );
my $hbox3      = Gtk2::HBox->new( FALSE, 10 );
my $hbox4      = Gtk2::HBox->new( FALSE, 10 );

my $hbox_row1 = Gtk2::HBox->new( TRUE, 10 );
my $hbox_row2 = Gtk2::HBox->new( TRUE, 10 );

my $hbox_btn  = Gtk2::HBox->new( TRUE,  10 );
my $vbox_btn  = Gtk2::VBox->new( FALSE, 10 );
my $vbox_main = Gtk2::VBox->new( FALSE, 10 );

#packing
if ( $use_caption ) {
	$hbox1->pack_start( $caption_label, FALSE, TRUE, 5 );
	$hbox1->pack_start( $caption_entry, TRUE,  TRUE, 5 );

	$hbox2->pack_start( $gravity_label, FALSE, TRUE, 5 );
	$hbox2->pack_start( $gravity_combo, TRUE,  TRUE, 5 );

	$hbox3->pack_start( $pointsize_label,   FALSE, TRUE, 5 );
	$hbox3->pack_start( $pointsize_sbutton, TRUE,  TRUE, 5 );

	$hbox4->pack_start( $angle_label,   FALSE, TRUE, 5 );
	$hbox4->pack_start( $angle_sbutton, TRUE,  TRUE, 5 );

	$hbox_row1->pack_start_defaults( $hbox1 );
	$hbox_row1->pack_start_defaults( $hbox2 );

	$hbox_row2->pack_start_defaults( $hbox3 );
	$hbox_row2->pack_start_defaults( $hbox4 );

	$vbox_param->pack_start( $hbox_row1, TRUE, TRUE, 5 );

} else {
	$hbox4->pack_start( $angle_label,   FALSE, TRUE, 5 );
	$hbox4->pack_start( $angle_sbutton, TRUE,  TRUE, 5 );

	$hbox_row2->pack_start_defaults( $hbox4 );
}


$vbox_param->pack_start( $hbox_row2, TRUE, TRUE, 5 );

$vbox_main->pack_start( $vbox_param, FALSE, TRUE, 5 );

$vbox_main->pack_start( $preview,     TRUE, TRUE, 5 );
$vbox_main->pack_start( $refresh_btn, TRUE, TRUE, 5 );

$hbox_btn->pack_start( $cancel_btn, TRUE, TRUE, 5 );
$hbox_btn->pack_start( $save_btn,   TRUE, TRUE, 5 );
$vbox_main->pack_start( $hbox_btn,  TRUE, TRUE, 5 );

$plug->add( $vbox_main );

$plug->show_all;

#generate first preview
&fct_imagemagick_polaroid( undef, 'refresh' );

#lets'start
Gtk2->main;

####define your functions here


sub fct_imagemagick_polaroid {
	my ( $widget, $data ) = @_;

	my $image = Image::Magick->new;

	if ( $data eq 'save' ) {

		my ( $tmpfh, $tmpfilename ) = tempfile();
		my $convert_output = undef;
		if ( $use_caption ) {
			$convert_output =
				system(   "convert -caption '"
						. $caption_entry->get_text
						. "' '$filename' -pointsize "
						. $pointsize_sbutton->get_value
						. " -gravity "
						. $gravity_combo->get_active_text
						. "  -bordercolor snow "
						. " -background black "
						. " -polaroid "
						. $angle_sbutton->get_value
						. " $tmpfilename" );
		} else {
			$convert_output = system( "
				  convert '$filename' -bordercolor white -border 6 -bordercolor grey60 -border 1 -background none -rotate "
					. $angle_sbutton->get_value
					. " -background  black  \\( +clone -shadow 60x4+4+4 \\) +swap -background none -flatten $tmpfilename"
			);
		}
		$image->ReadImage( $tmpfilename );
		$image->WriteImage( filename => $filename );

		Gtk2->main_quit;
		return TRUE;
	} else {

		my ( $tmpfh, $tmpfilename ) = tempfile();
		my $convert_output = undef;
		if ( $use_caption ) {
			$convert_output =
				system(   "convert -caption '"
						. $caption_entry->get_text
						. "' '$filename' -pointsize "
						. $pointsize_sbutton->get_value
						. " -gravity "
						. $gravity_combo->get_active_text
						. "  -bordercolor snow "
						. " -background black "
						. " -polaroid "
						. $angle_sbutton->get_value
						. " $tmpfilename" );
		} else {
			$convert_output = system( "
				  convert '$filename' -bordercolor white -border 6 -bordercolor grey60 -border 1 -background none -rotate "
					. $angle_sbutton->get_value
					. " -background  black  \\( +clone -shadow 60x4+4+4 \\) +swap -background none -flatten $tmpfilename"
			);
		}
		$preview->set_from_pixbuf(
								   Gtk2::Gdk::Pixbuf->new_from_file_at_scale(
													$tmpfilename, 300, 300, TRUE
																			)
								 );

		return TRUE;
	}
}
####define your functions here

1;

