#!/usr/bin/perl

use lib 'D:/Programs/Perl/lib';

use strict;
use Tk;

use Sci::Science;
use Sci::Optics;

my $pi = Sci::pi();

my $mw = MainWindow->new();
$mw->title("Unit conversion");

my $EntryWidth = 10;

&MakeUnitConversionPage($mw, $EntryWidth);

MainLoop;
exit;

sub MakeUnitConversionPage
{
	my ($mw, $EntryWidth) = @_;

	my @pEntryString;
	my @Frames;
	for(my $i = 0 ; $i < 8 ; $i++) {
		my $anchor     = 'e';
		my $justify    = 'center';
		my $state      = 'disabled';
		my $background = 'yellow';

		my @EntryString = ('eV', 'nm', 'Hz', 'Omega /s', '/cm', 's');
		$pEntryString[$i] = \@EntryString;
		my $E  = 0.5 * $i;
		$E  = Utils::DelSpace(sprintf("%6.3f",  $E));
		$E = 1.0 if($i == 0.0);
		my $wl = Utils::DelSpace(sprintf("%8.2f",  Optics::eVTonm($E)));
		my $f  = Utils::DelSpace(sprintf("%8.4g",  Optics::eVToHz($E)));
		my $w  = Utils::DelSpace(sprintf("%8.4g",  2.0 * $pi * $f));
		my $k  = Utils::DelSpace(sprintf("%8.0f",  Optics::eVToKiser($E)));
		my $t  = Utils::DelSpace(sprintf("%12.4g", 1.0 / $f));
		if($i > 0) {
			$anchor     = 'w';
			$justify    = 'left';
			$state      = 'normal';
			$background = 'white';
			$EntryString[0] = $E;
			$EntryString[1] = $wl;
			$EntryString[2] = $f;
			$EntryString[3] = $w;
			$EntryString[4] = $k;
			$EntryString[5] = $t;
		}

		$Frames[$i] = $mw->Frame()->pack(-side => 'top', -expand => 'yes', -fill => 'both');

		my $ent = $Frames[$i]->Entry(
			-state        => $state,
			-justify      => $justify,
			-width        => $EntryWidth,
			-background   => $background,
			-textvariable => \$EntryString[0],
			)->pack(-side => 'left');
		$mw->bind($ent, '<FocusIn>',  [\&UnitConvEntryFocusedOut, 'FocusIn',  \@pEntryString, $i, 'eV']);
		$mw->bind($ent, '<FocusOut>', [\&UnitConvEntryFocusedOut, 'FocusOut', \@pEntryString, $i, 'eV']);

		$ent = $Frames[$i]->Entry(
			-state        => $state,
			-justify      => $justify,
			-width        => $EntryWidth,
			-background   => $background,
			-textvariable => \$EntryString[1],
			)->pack(-side => 'left');
		$mw->bind($ent, '<FocusIn>',  [\&UnitConvEntryFocusedOut, 'FocusIn',  \@pEntryString, $i, 'nm']);
		$mw->bind($ent, '<FocusOut>', [\&UnitConvEntryFocusedOut, 'FocusOut', \@pEntryString, $i, 'nm']);

		$ent = $Frames[$i]->Entry(
			-state        => $state,
			-justify      => $justify,
			-width        => $EntryWidth,
			-background   => $background,
			-textvariable => \$EntryString[2],
			)->pack(-side => 'left');
		$mw->bind($ent, '<FocusIn>',  [\&UnitConvEntryFocusedOut, 'FocusIn',  \@pEntryString, $i, 'Hz']);
		$mw->bind($ent, '<FocusOut>', [\&UnitConvEntryFocusedOut, 'FocusOut', \@pEntryString, $i, 'Hz']);

		$ent = $Frames[$i]->Entry(
			-state        => $state,
			-justify      => $justify,
			-width        => $EntryWidth,
			-background   => $background,
			-textvariable => \$EntryString[3],
			)->pack(-side => 'left');
		$mw->bind($ent, '<FocusIn>',  [\&UnitConvEntryFocusedOut, 'FocusIn',  \@pEntryString, $i, '/s']);
		$mw->bind($ent, '<FocusOut>', [\&UnitConvEntryFocusedOut, 'FocusOut', \@pEntryString, $i, '/s']);

		$ent = $Frames[$i]->Entry(
			-state        => $state,
			-justify      => $justify,
			-width        => $EntryWidth,
			-background   => $background,
			-textvariable => \$EntryString[4],
			)->pack(-side => 'left');
		$mw->bind($ent, '<FocusIn>',  [\&UnitConvEntryFocusedOut, 'FocusIn',  \@pEntryString, $i, '/cm']);
		$mw->bind($ent, '<FocusOut>', [\&UnitConvEntryFocusedOut, 'FocusOut', \@pEntryString, $i, '/cm']);

		$ent = $Frames[$i]->Entry(
			-state        => $state,
			-justify      => $justify,
			-width        => $EntryWidth,
			-background   => $background,
			-textvariable => \$EntryString[5],
			)->pack(-side => 'left');
		$mw->bind($ent, '<FocusIn>',  [\&UnitConvEntryFocusedOut, 'FocusIn',  \@pEntryString, $i, 's']);
		$mw->bind($ent, '<FocusOut>', [\&UnitConvEntryFocusedOut, 'FocusOut', \@pEntryString, $i, 's']);
	}
}

sub UnitConvEntryFocusedOut {
	my ($obj, $event, $ppEntryString, $idx, $unit) = @_;
	return if($idx == 0);

	my $pVars = $ppEntryString->[$idx];
	if($event eq 'FocusOut') {
		if($unit eq 'eV') {
			my $E  = $pVars->[0];
			my $wl = Utils::DelSpace(sprintf("%8.2f", Optics::eVTonm($E)));
			my $f  = Utils::DelSpace(sprintf("%8.4g", Optics::eVToHz($E)));
			my $w  = Utils::DelSpace(sprintf("%8.4g", 2.0 * $pi * $f));
			my $k  = Utils::DelSpace(sprintf("%8.0f", Optics::eVToKiser($E)));
			my $t  = Utils::DelSpace(sprintf("%12.4g", 1.0 / $f));
			$pVars->[1] = $wl;
			$pVars->[2] = $f;
			$pVars->[3] = $w;
			$pVars->[4] = $k;
			$pVars->[5] = $t;
		}
		if($unit eq 'nm') {
			my $wl = $pVars->[1];
			my $E  = Utils::DelSpace(sprintf("%6.3f", Optics::nmToeV($wl)));
			my $f  = Utils::DelSpace(sprintf("%8.4g", Optics::eVToHz($E)));
			my $w  = Utils::DelSpace(sprintf("%8.4g", 2.0 * $pi * $f));
			my $k  = Utils::DelSpace(sprintf("%8.0f", Optics::eVToKiser($E)));
			my $t  = Utils::DelSpace(sprintf("%12.4g", 1.0 / $f));
			$pVars->[0] = $E;
			$pVars->[2] = $f;
			$pVars->[3] = $w;
			$pVars->[4] = $k;
			$pVars->[5] = $t;
		}
		if($unit eq 'Hz') {
			my $f  = $pVars->[2];
			my $E  = Utils::DelSpace(sprintf("%6.3f", Optics::HzToeV($f)));
			my $wl = Utils::DelSpace(sprintf("%8.2f", Optics::eVTonm($E)));
			my $w  = Utils::DelSpace(sprintf("%8.4g", 2.0 * $pi * $f));
			my $k  = Utils::DelSpace(sprintf("%8.0f", Optics::eVToKiser($E)));
			my $t  = Utils::DelSpace(sprintf("%12.4g", 1.0 / $f));
			$pVars->[0] = $E;
			$pVars->[1] = $wl;
			$pVars->[3] = $w;
			$pVars->[4] = $k;
			$pVars->[5] = $t;
		}
		if($unit eq '/s') {
			my $w  = $pVars->[3];
			my $f  = Utils::DelSpace(sprintf("%8.4g", $w / 2.0 / $pi));
			my $E  = Utils::DelSpace(sprintf("%6.3f", Optics::HzToeV($f)));
			my $wl = Utils::DelSpace(sprintf("%8.2f", Optics::eVTonm($E)));
			my $k  = Utils::DelSpace(sprintf("%8.0f", Optics::eVToKiser($E)));
			my $t  = Utils::DelSpace(sprintf("%12.4g", 1.0 / $f));
			$pVars->[0] = $E;
			$pVars->[1] = $wl;
			$pVars->[2] = $f;
			$pVars->[4] = $k;
			$pVars->[5] = $t;
		}
		if($unit eq '/cm') {
			my $k  = $pVars->[4];
			my $wl = Utils::DelSpace(sprintf("%8.2f", 1.0 / $k * 1.0e7));
			my $E  = Utils::DelSpace(sprintf("%6.3f", Optics::nmToeV($wl)));
			my $f  = Utils::DelSpace(sprintf("%8.4g", Optics::eVToHz($E)));
			my $w  = Utils::DelSpace(sprintf("%8.4g", 2.0 * $pi * $f));
			my $t  = Utils::DelSpace(sprintf("%12.4g", 1.0 / $f));
			$pVars->[0] = $E;
			$pVars->[1] = $wl;
			$pVars->[2] = $f;
			$pVars->[3] = $w;
			$pVars->[5] = $t;
		}
		if($unit eq 's') {
			my $t  = $pVars->[5];
			my $f  = Utils::DelSpace(sprintf("%8.4g", 1.0 / $t));
			my $E  = Utils::DelSpace(sprintf("%6.3f", Optics::HzToeV($f)));
			my $wl = Utils::DelSpace(sprintf("%8.2f", Optics::eVTonm($E)));
			my $w  = Utils::DelSpace(sprintf("%8.4g", 2.0 * $pi * $f));
			my $k  = Utils::DelSpace(sprintf("%8.0f", Optics::eVToKiser($E)));
			$pVars->[0] = $E;
			$pVars->[1] = $wl;
			$pVars->[2] = $f;
			$pVars->[3] = $w;
			$pVars->[4] = $k;
		}
	}
}