#!/usr/bin/perl

use lib 'd:/git/tkProg/tklib/Perl/lib';
use lib '/home/share/tkProg/tklib/Perl/lib';

use strict;
use Tk;

use utf8;
use Jcode;
#use encoding 'sjis';

use ProgVars;
use Sci qw($pi $kB $R $NA $c $e $e0 $u0 $me $mp $mn $h $hbar);
use Sci::Material;
use Sci::Optics;
use Crystal::AtomType;

my $pi   = Sci::pi();
my $kB   = Sci::kB();
my $R    = Sci::R();
my $NA   = Sci::NA();
my $c    = Sci::c();
my $e    = Sci::e();
my $e0   = Sci::e0();
my $u0   = Sci::u0();
my $me   = Sci::me();
my $mp   = Sci::mp();
my $mn   = Sci::mn();
my $h    = Sci::h();
my $hbar = Sci::hbar();

use MyTk::MyNoteBook;
use MyTk::MyLabel;
use MyTk::MyEntry;
use MyTk::MyText;
use MyTk::MyButton;

my $Debug = 0;

my $material = new Material;
my $pArgs = &GetArgs(\@ARGV);

my $mw = MainWindow->new();
$mw->title("Semiconductor Parameter Calculator");
if(0) {
	my @Fonts = $mw->fontFamilies();
	for my $s (@Fonts) {
		print "$s\n";
	}
}

my $this = $mw;

my $EntryWidth = 10;
my $T              = 300.0;
my $Dit            = 1.0e12; # cm-2
my $dgate          = 120.0; # nm
my $er             = 4.0;
my ($Cox, $S);
my $Vox            = 10.0;
my ($Qox, $Neox);

my $EffMass         = 0.35;
my $CBMultiplicity  = 1.0;
my $Ne              = 1.0e19; # cm-3
my $EField          = 1.0 / 1.0; # V/cm
my ($Nc, $Dc0, $EF, $dEBM, $Ep, $lDrift, $lDiff);

my $RelaxationTime = 5.0e-15; # s
my ($Mobility, $Conductivity, $vth, $vFE, $lth, $lFE);
my ($vDrift, $DiffusionConstant);

my $Book = $mw->MyNoteBook()->pack(-fill => 'both', -expand => 'yes');
my @Pages;
my $FirstPage;
if($pArgs->{All} or $pArgs->{SS}) {
	$Pages[0] = $Book->add("SS",
					-label => "Subthreshold swing",
					-createcmd => sub {},
					-raisecmd => sub {},
					-state => 'normal',
					-wraplength => 200,
					);
	MakeSSPage(            $Pages[0], $EntryWidth);
	$FirstPage = "SS" if(!$FirstPage);
}
if($pArgs->{All} or $pArgs->{Nc}) {
	$Pages[1] = $Book->add("Nc",           -label => "Nc,BM shift");
	my $Frame = &MakeNcPage($Pages[1], $EntryWidth);
	$this->{EffMassButton} = $Frame->MyButton(
		-text => 'm*:',
		-takefocus => 1,
		-command => [\&ButtonPressed, 'RButtonDown', 'm*'],
		)->grid(-row => 6, -column => 0, -columnspan => 1, -sticky => 'e' );
	$this->{EffMassButton}->{status} = 1;
	$this->{NeButton} = $Frame->MyButton(
		-text => 'Ne:',
		-takefocus => 1,
		-command => [\&ButtonPressed, 'RButtonDown', 'Ne'],
		)->grid(-row => 6, -column => 6, -columnspan => 1, -sticky => 'e' );
	$FirstPage = "Nc" if(!$FirstPage);
}
if($pArgs->{All} or $pArgs->{conductivity}) {
	$Pages[2] = $Book->add("conductivity", -label => "conductivity");
	&MakeConductivityPage(  $Pages[2], $EntryWidth+2);
	$FirstPage = "conductivity" if(!$FirstPage);
}
if($pArgs->{All} or $pArgs->{dispersion}) {
	$Pages[3] = $Book->add("dispersion",   -label => "dispersion");
	&MakeDispersionPage(    $Pages[3], $EntryWidth+2);
	$FirstPage = "dispersion" if(!$FirstPage);
}
if($pArgs->{All} or $pArgs->{atom}) {
	$Pages[4] = $Book->add("atom",         -label => "atom");
	&MakeAtomPage(          $Pages[4], $EntryWidth+2);
	$FirstPage = "atom" if(!$FirstPage);
}
if($pArgs->{All} or $pArgs->{gas}) {
	$Pages[5] = $Book->add("gas",          -label => "gas");
	&MakeGasPage(           $Pages[5], $EntryWidth+2);
	$FirstPage = "gas" if(!$FirstPage);
}
if($pArgs->{All} or $pArgs->{Constants}) {
	$Pages[6] = $Book->add("Constants",    -label => "Constants");
	&MakeConstantsPage(     $Pages[6], $EntryWidth+2);
	$FirstPage = "Constants" if(!$FirstPage);
}
if($pArgs->{All} or $pArgs->{Photon}) {
	$Pages[7] = $Book->add("Photon",       -label => "Photon");
	&MakeUnitConversionPage($Pages[7], 10);
	$FirstPage = "Photon" if(!$FirstPage);
}
if($pArgs->{All} or $pArgs->{Calculator}) {
	$Pages[8] = $Book->add("Calculator",   -label => "Calc");
	&MakeCalculatorPage(    $Pages[8], 50);
	$FirstPage = "Calculator" if(!$FirstPage);
}
$Book->raise($FirstPage) if($FirstPage);
# raised(): 表示されているページを返す
# delete(), pagecget($Page1, -option), pageconfigure(pageName, options)

&UpdateParameters();

MainLoop;
exit;
#=========================================================================================
# Main routine end
#=========================================================================================

#=========================================================================================
# Subroutines
#=========================================================================================
sub GetArgs
{
	my ($pArgs) = @_;
	my %Args;
	if(@$pArgs == 0) {
		$Args{All} = 1;
		return \%Args;
	}

	foreach my $s (@$pArgs) {
		my ($key, $val) = ($s =~ /^([^=]+)=(.*)$/);
		if(!$key) {
			$key = $s;
			$val = 1;
		}
		$Args{$key} = $val;
#print "$key: $val\n";
	}
	return \%Args;
}
	
sub ButtonPressed
{
	my ($event, $type) = @_;
#print "$event $type\n";
	if($type eq 'm*') {
		if($this->{EffMassButton}->{status}) {
			$this->{EffMassButton}->{status} = 0;
			$this->{NeButton}->{status}      = 1;
			$this->{EffMassEntry}->configure(-state => 'disabled');
			$this->{NeEntry}->configure(-state => 'normal');
		}
		else {
			$this->{EffMassButton}->{status} = 1;
			$this->{NeButton}->{status}      = 0;
			$this->{EffMassEntry}->configure(-state => 'normal');
			$this->{NeEntry}->configure(-state => 'disabled');
		}
	}
	elsif($type eq 'Ne') {
		if($this->{NeButton}->{status}) {
			$this->{NeButton}->{status}      = 0;
			$this->{EffMassButton}->{status} = 1;
			$this->{NeEntry}->configure(-state => 'disabled');
			$this->{EffMassEntry}->configure(-state => 'normal');
		}
		else {
			$this->{NeButton}->{status}      = 1;
			$this->{EffMassButton}->{status} = 0;
			$this->{NeEntry}->configure(-state => 'normal');
			$this->{EffMassEntry}->configure(-state => 'disabled');
		}
	}
}

sub UpdateParameters
{
	$Cox  = $material->CalCapacitance($er, 0.01*0.01, $dgate*1.0e-9); # F/cm2
	$Qox  = $Cox * $Vox;
	$Neox = $Qox / $e;

	$S    = $material->CalSubthresholdSwing($T, $er, $dgate*1.0e-9, $Dit*1.0e4);
	$Nc   = $material->Nc($EffMass*$me, $CBMultiplicity, $T, 0) * 1.0e-6;
	$Dc0  = $material->Dc0($EffMass*$me, $CBMultiplicity, 0) * $e**1.5 * 1.0e-6; # cm^-3/eV^1.5
	$EF = $dEBM = $material->BMShift($Ne*1.0e6, $Dc0*1.0e6, 0);
	$Ep   = $material->CalPlasmaFrequency($Ne*1e6, $EffMass) * $h / $e;

#print "tau3=$RelaxationTime  Ne=$Ne  \n";
	$Mobility = $e * $RelaxationTime / $EffMass / $me * 1.0e4; # cm2/Vs
	$Conductivity = $e * $Ne * $Mobility; # S/cm

	$vth                = $material->ElectronThermalVelocity($T, $EffMass*$me, 0);
	$lth                = $vth * $RelaxationTime * 1.0e9; # nm
$EffMass = 1.0e-100 if($EffMass == 0.0);
	$vFE                = sqrt(2.0 * $EF * $e / ($me*$EffMass) ); # m/s
	$lFE                = $vFE * $RelaxationTime * 1.0e9; # nm
	$vDrift             = $Mobility*1.0e-4 * $EField*100.0; # m/s
	$lDrift             = $material->ElectronDriftMeanFreePath($EField*100.0, 1.0, $Mobility*1.0e-4, $RelaxationTime, 0) * 1.0e9; # nm
	$DiffusionConstant  = $material->ElectronDiffusionConstant($T, $Mobility*1.0e-4, 0);
	$lDiff              = $material->ElectronDiffusionLength($DiffusionConstant, $RelaxationTime, 0) * 1.0e9; # nm

	$this->{WLight}             = $this->{hv} * $e * $this->{PhotonFlux}; # W/cm2
	$this->{ULight}             = $this->{WLight} / ($c * 1.0e2); # W/cm3
	$this->{EmaxLight}          = &CalEmaxLight($this->{hv}, $this->{PhotonFlux}); # V/m
	$this->{xmax}               = &CalMaxDisplacementForLorentz(
									$this->{EmaxLight}, $this->{OscillatorStrength},
									$this->{Omega}, $this->{Omega0}, $this->{DampingFactor}) * 1.0e9; # nm

	if($this->{dgateEntry}) {
		$this->{dgateEntry}->SetText($dgate);
		$this->{erEntry}->SetText(   $er);
		$this->{SEntry}->SetText(    $S);
		$this->{CoxEntry}->SetText(  $Cox);
		$this->{DitEntry}->SetText(  $Dit);
	}

	if($this->{EffMassEntry}) {
		$this->{EffMassEntry}->SetText($EffMass);
		$this->{CBMultEntry}->SetText( $CBMultiplicity);
		$this->{NeEntry}->SetText(     $Ne);
		$this->{NcEntry}->SetText(     $Nc);
		$this->{Dc0Entry}->SetText(    $Dc0);
		$this->{dEBMEntry}->SetText(   $dEBM);
		$this->{EpEntry}->SetText(     $Ep);
	}

	if($this->{RelaxationTimeConductivityEntry}) {
		$this->{RelaxationTimeConductivityEntry}->SetText($RelaxationTime);
		$this->{MobilityConductivityEntry}->SetText(      $Mobility);
		$this->{ConductivityConductivityEntry}->SetText(  $Conductivity);
		$this->{vthConductivityEntry}->SetText($vth);
		$this->{vFEConductivityEntry}->SetText($vFE);
		$this->{lthConductivityEntry}->SetText($lth);
		$this->{lFEConductivityEntry}->SetText($lFE);
		$this->{lDriftConductivityEntry}->SetText($lDrift);
		$this->{lDiffConductivityEntry}->SetText( $lDiff);
		$this->{vDriftConductivityEntry}->SetText($vDrift);
		$this->{DiffConstConductivityEntry}->SetText($DiffusionConstant);
	}

	if($this->{IonNameEntry}) {
		my $IonName = $this->{IonName};
		if(!$IonName) {
			$this->{IonName} = AtomType::GetAtomName($this->{AtomicNumber});
		}
		my $CN      = $this->{CoordinationNumberEntry};
		my ($AtomicNumber, $AtomName, $IonCharge) = AtomType::GetAtomInformation($IonName);
		my $IonRadius = AtomType::GetCationRadius($AtomName, $IonCharge, 0);
		$IonCharge = '0' if($IonCharge eq '');
		$IonCharge = '+1'if($IonCharge eq '+');
		$IonCharge = '-1'if($IonCharge eq '-');
		my ($filepath, $AtomJName, $AtomEName,
			$AtomicMass, $FoundYear, $AtomicRadius,
			$MeltingPoint, $BoilingPoint, $Density,
			$HeatCapacity, $IonizationPotential, $ElectronAffinity) = AtomType::GetIonInfFromPeriodicTable2($AtomicNumber);

		my @radius = AtomType::GetIonRadius2($AtomName, $IonCharge, $this->{CoordinationNumber});
		my ($AtmNum, $JName, $EName, $NameOrigin) = AtomType::GetIonInfFromPeriodicTable1($AtomName);

		$this->{AtomicNumber}        = $AtomicNumber;
		$this->{AtomJName}           = $AtomJName;
		$this->{AtomName}            = $AtomName;
		$this->{IonCharge}           = $IonCharge;
		$this->{AtomMass}            = $AtomicMass;
		$this->{IonizationPotential} = $IonizationPotential;
		$this->{ElectronAffinity}    = $ElectronAffinity;
		$this->{IonRadius1}          = $IonRadius;
		my $s = '';
		for(my $i = 0 ; $i < @radius ; $i++) {
			$s .= "$radius[$i], ";
		}
		$this->{IonRadius2}          = $s;
		$this->{AtomicRadius}        = $AtomicRadius;
	}

	if($this->{PhPaEntry}) {
		$this->{PhPa}  = $this->{P} * 1.0e-2;
		$this->{Patm}  = $this->{P} / 1.01325e5;
		$this->{PTorr} = $this->{Patm} * 760;
		$this->{Pmbar} = $this->{PhPa};

		$this->{V} = $this->{n} * $R * $this->{T} / $this->{P};
		my $v = 0.0;
		if($this->{AtomMass}) {
			$v = sqrt(3.0 * $kB * $this->{T} / $mp / $this->{AtomMass});
		}
		$this->{GasVelocity} = $v;
		my $S = $pi * $this->{Rtotal} * $this->{Rtotal} * 1.0e-18; # m2
		$this->{MFPath} = $this->{V} / $this->{n} / $NA / $S; # m
	}

	if($this->{pEntryArray}) {
		for(my $i = 0 ; $i < @{$this->{pEntryArray}} ; $i++) {
			$this->{pEntryArray}->[$i]->Update();
		}
	}
}

sub EntryFocusedOut {
	my ($obj, $event, $type) = @_;
#print "type: $type\n";
	$T              = eval($T      );
#print "T=$T\n";
	$dgate          = eval($this->{dgateEntry}->GetText()  );
	$Dit            = eval($this->{DitEntry}->GetText()    );
	$er             = eval($this->{erEntry}->GetText()     );
	$Cox            = eval($this->{CoxEntry}->GetText()    );
	$S              = eval($this->{SEntry}->GetText()      );
	$EffMass        = eval($this->{EffMassEntry}->GetText());
	$CBMultiplicity = eval($this->{CBMultEntry}->GetText() );
	$Ne             = eval($this->{NeEntry}->GetText()     );
	$Nc             = eval($this->{NcEntry}->GetText()     );
	$Dc0            = eval($this->{Dc0Entry}->GetText()    );
	$dEBM           = eval($this->{dEBMEntry}->GetText()   );
	$Ep             = eval($this->{EpEntry}->GetText()     );
	$RelaxationTime = eval($this->{RelaxationTimeConductivityEntry}->GetText());
	$Mobility       = eval($this->{MobilityConductivityEntry}->GetText()      );
	$Conductivity   = eval($this->{ConductivityConductivityEntry}->GetText()  );
#print "*C=$Cox m  S=$S m2  d=$dgate m\n";
	if($event eq 'FocusOut') {
		if($type eq 'T') {
			$S     = &CalS($T, $er, $dgate*1.0e-9, $Dit*1.0e4);
			$Nc  = $material->Nc( $EffMass*$me, $CBMultiplicity, $T, 0) * 1.0e-6;
			$Dc0 = $material->Dc0($EffMass*$me, $CBMultiplicity, 0) * $e**1.5 * 1.0e-6; # cm^-3/eV^1.5
		}
		if($type eq 'dgate') {
			$Cox   = &CalC($er, 0.01*0.01, $dgate*1.0e-9); # F/cm2
			$S     = &CalS($T, $er, $dgate*1.0e-9, $Dit*1.0e4);
		}
		if($type eq 'er') {
			$Cox   = &CalC($er, 0.01*0.01, $dgate*1.0e-9); # F/cm2
			$S     = &CalS($T, $er, $dgate*1.0e-9, $Dit*1.0e4);
		}
		if($type eq 'Cox') {
			$dgate = &Caldgate($er, 1.0, $Cox*1.0e4) * 1.0e9;
			$S     = &CalS($T, $er, $dgate*1.0e-9, $Dit*1.0e4);
		}
		if($type eq 'Dit') {
			$S     = &CalS($T, $er, $dgate*1.0e-9, $Dit*1.0e4);
		}
		if($type eq 'S') {
			$Dit = &CalDit($T, $er, 1.0, $dgate*1.0e-9, $S);
		}
		if($type eq 'm*' or $type eq 'M' or $type eq 'Ne') {
			$Nc     = $material->Nc( $EffMass*$me, $CBMultiplicity, $T, 0) * 1.0e-6;
			$Dc0    = $material->Dc0($EffMass*$me, $CBMultiplicity, 0) * $e**1.5 * 1.0e-6; # cm^-3/eV^1.5
			$dEBM   = $material->BMShift($Ne*1.0e6, $Dc0*1.0e6, 0);
			$Ep     = Optics::CalPlasmaFrequency($Ne*1e6, $EffMass) * $h / $e;
#print "Ne=$Ne wp=$Omegap\n";
		}
		if($type eq 'Ep') {
			if($this->{EffMassButton}->{status}) {
				$EffMass = &CalEffectiveMassFromPlasmaFrequency($Ep, $Ne*1e6);
			}
			else {
				$Ne      = &CalNeFromPlasmaFrequency($Ep, $EffMass) * 1e-6;
			}
		}
		if($type eq 'mu') {
			$RelaxationTime = $Mobility*1.0e-4 * $EffMass * $me / $e;
		}
		if($type eq 'sigma') {
			$Ne = $Conductivity / $e / $Mobility;
		}
		if($type eq 'AtomicNumber') {
			$this->{IonName} = AtomType::GetAtomName($this->{AtomicNumber});
		}
		if($type eq 'AtomMass') {
			for(my $i = 1 ; $i < 120 ; $i++) {
				my ($filepath, $AtomJName, $AtomEName,
					$AtomicMass, $FoundYear, $AtomicRadius,
					$MeltingPoint, $BoilingPoint, $Density,
					$HeatCapacity, $IonizationPotential, $ElectronAffinity) = AtomType::GetIonInfFromPeriodicTable2($i);
#print "i=$i  M=$AtomicMass ($this->{AtomMass})\n";
				last if(!$AtomicMass);
				if($AtomicMass > $this->{AtomMass} - 0.5) {
					$this->{IonName} = AtomType::GetAtomName($i);
					last;
				}
			}
		}
		if($type eq 'PhPa') {
			$this->{P} = $this->{PhPa} * 1.0e2;
		}
		if($type eq 'Patm') {
			$this->{P} = $this->{Patm} * 1.013255e5;
		}
		if($type eq 'PTorr') {
			$this->{P} = $this->{PTorr} / 760 * 1.013255e5;
		}
		if($type eq 'Pmbar') {
			$this->{P} = $this->{Pmbar} * 1.0e2;
		}
		if($type eq 'Equation') {
			my $ret = eval($this->{EquationEntry}->GetText());
			$ret = 'Error!!' if(!$ret);
			$this->{AnswerEntry}->SetText($ret);
			return;
		}
	}
	&UpdateParameters();
}

sub MakeSSPage
{
	my ($page, $EntryWidth) = @_;

	my $Frame  = $page->Frame()->pack(-anchor => 'nw', -fill => 'x');

	$Frame->MyLabel(-text => '')->grid(-row => 0, -column => 0, -columnspan => 1, -sticky => 'e' );
	$this->{TEntry}     = &MakeLabelEntry($Frame, "T",     "T:",         \$T,  "%8.2f", $EntryWidth, "K",      1, 0, "normal");
	$Frame->MyLabel(-text => '')->grid(-row => 2, -column => 0, -columnspan => 1, -sticky => 'e' );

	#$Frame = $mw->Frame()->pack(-anchor => 'nw', -fill => 'x');
	$this->{dgateEntry} = &MakeLabelEntry($Frame, "dgate", "dgate:", \$dgate,  "%8.2f", $EntryWidth, "nm",     3, 0);
	$this->{erEntry}    = &MakeLabelEntry($Frame, "er",    "er:",       \$er,  "%6.2f", $EntryWidth, "",       3, 1);
	$this->{CoxEntry}   = &MakeLabelEntry($Frame, "Cox",   "Cox:",     \$Cox, "%10.4g", $EntryWidth, "F/cm^2", 3, 2);

#	$Frame = $mw->Frame()->pack(-anchor => 'nw', -fill => 'x');
	$this->{DitEntry}   = &MakeLabelEntry($Frame, "Dit",   "Dit:",     \$Dit, "%10.4g", $EntryWidth, "cm^-2eV-1",  4, 1);
	$this->{SEntry}     = &MakeLabelEntry($Frame, "S",     "S:",         \$S,  "%8.3g", $EntryWidth, "V/dec",  4, 2);

	$this->{VoxEntry}   = &MakeLabelEntry($Frame, "Vox",   "Vox:",     \$Vox,  "%8.2f", $EntryWidth, "V",      5, 0);
	$this->{QoxEntry}   = &MakeLabelEntry($Frame, "Qox",   "Qox:",     \$Qox, "%10.4g", $EntryWidth, "C",      5, 1, "disable");
	$this->{NeoxEntry}  = &MakeLabelEntry($Frame, "Neox", "Neox:",    \$Neox, "%10.4g", $EntryWidth,  "",      5, 2, "disable");

	$Frame->MyLabel(-text => '')->grid(-row => 8, -column => 0, -columnspan => 1, -sticky => 'e' );
	return $Frame;
}

sub CalEmaxLight
{
# hv: eV   F: 1/cm2/s, photon flux
	my ($hv, $F) = @_;
	my $W = $hv * $F*1.0e4 * $e / $c; # W/m3
	my $Emax = sqrt(2.0 * $W / ($e0 + 1.0/$u0/$c/$c)); # V/m
	return $Emax;
}

sub CalMaxDisplacementForLorentz
{
# E: V/m
	my ($Emax, $fa, $omega, $omega0, $gamma) = @_;
$me=9.1093897e-31;
#print "me=$me\n";
	my $xmax = sqrt($fa)*$e/$me * $Emax;
	my $omega2 = $omega0*$omega0 - $omega*$omega;
	my $ogamma = $omega*$gamma;
	my $a = sqrt($omega2*$omega2 + $ogamma*$ogamma); # m
	return 0.0 if($a == 0);
	$xmax = $xmax / $a;
	return $xmax;
}

sub MakeAtomPage
{
	my ($page, $EntryWidth) = @_;

	$this->{IonName}             = 'H';
	$this->{CoordinationNumber}  = 6;
	$this->{AtomicNumber}        = 0;
	$this->{AtomJName}           = '';
	$this->{AtomName}            = '';
	$this->{AtomMass}            = 0;
	$this->{IonCharge}           = 0;
	$this->{AtomicRadius}        = 0;
	$this->{IonRadius1}          = 0;
	$this->{IonRadius2}          = 0;
	$this->{IonizationPotential} = 0;
	$this->{ElectronAffinity}    = 0;

	my $Frame  = $page->Frame()->pack(-anchor => 'nw', -fill => 'x');

	$this->{IonNameEntry}      = &MakeLabelEntry($Frame, "Ion",          "Ion:",  \$this->{IonName},      "%s", $EntryWidth,   "", 0, 0, "normal");
	$this->{AtomicNumberEntry} = &MakeLabelEntry($Frame, "AtomicNumber",  "An:",  \$this->{AtomicNumber}, "%d", $EntryWidth,   "", 0, 1, "normal");
	$this->{CoordinationNumberEntry}
					   = &MakeLabelEntry($Frame, "CoordinationNumber", "Cn:",  \$this->{CoordinationNumber}, "%d", $EntryWidth,   "", 0, 2, "normal");

	$this->{AtomJNameEntry}    = &MakeLabelEntry($Frame, "AtomJName",   "JName:", \$this->{AtomJName},    "%s", $EntryWidth,   "", 1, 0, "normal");
#	$this->{AtomJNameEntry}->configure(-font => ["MS UI Cothic", 12, "normal"]);
#	$this->{AtomJNameEntry}->configure(-font => ["Terminal", 12, "normal"]);
	$this->{AtomNameEntry}     = &MakeLabelEntry($Frame, "AtomName",    "Atom:",  \$this->{AtomName},     "%s", $EntryWidth,   "", 1, 1, "normal");
	$this->{AtomMassEntry}     = &MakeLabelEntry($Frame, "AtomMass",       "M:",  \$this->{AtomMass},     "%f", $EntryWidth,   "", 1, 2, "normal");
	$this->{IonChargeEntry}    = &MakeLabelEntry($Frame, "IonCharge",      "Z:",  \$this->{IonCharge},    "%s", $EntryWidth,   "", 1, 3, "normal");

	$this->{AtomicRadiusEntry} = &MakeLabelEntry($Frame, "AtomicRadius", "R(atom):", \$this->{AtomicRadius}, "%f", $EntryWidth, "nm", 2, 0, "normal");
	$this->{IonRadius1Entry}   = &MakeLabelEntry($Frame, "IonRadius",     "R(ion):", \$this->{IonRadius1},  "%f", $EntryWidth, "nm", 2, 1, "normal");
	$this->{IonRadius2Entry}   = &MakeLabelEntry($Frame, "IonRadius",     "R(ion):", \$this->{IonRadius2},  "%s", $EntryWidth, "pm", 2, 2, "normal");

	$this->{IonizationPotentialEntry}
							   = &MakeLabelEntry($Frame, "IonizationPotential", "I:", \$this->{IonizationPotential}, "%f", $EntryWidth, "eV", 3, 0, "normal");
	$this->{ElectronAffinityEntry}
							   = &MakeLabelEntry($Frame, "ElectronAffinity",    "A:", \$this->{ElectronAffinity},    "%f", $EntryWidth, "eV", 3, 1, "normal");
}

sub MakeGasPage
{
	my ($page, $EntryWidth) = @_;

	$this->{IonName}             = 'H';
	$this->{AtomicNumber}        = 0;
	$this->{AtomMass}            = 0;

	$this->{T}            = 300.0; # K
	$this->{P}            = 1.01325e5; # N/m2
	$this->{PhPa}         = 0;     # hPa
	$this->{PTorr}        = 0;     # Torr
	$this->{Patm}         = 0;     # atm
	$this->{Pmbar}        = 0;     # mbar
	$this->{n}            = 1.0;   # mol
	$this->{V}            = 0;
	$this->{Rtotal}       = 0.2;   # nm
	$this->{GasVelocity}  = 0.0;   # m/s
	$this->{MFPath}       = 0.0;   # m

	my $Frame  = $page->Frame()->pack(-anchor => 'nw', -fill => 'x');

	$this->{IonNameEntry}      = &MakeLabelEntry($Frame, "Ion",          "Ion:", \$this->{IonName},       "%s", $EntryWidth,   "", 0, 0, "normal");
	$this->{AtomicNumberEntry} = &MakeLabelEntry($Frame, "AtomicNumber",  "An:",  \$this->{AtomicNumber}, "%d", $EntryWidth,   "", 0, 1, "normal");
	$this->{AtomMassEntry}     = &MakeLabelEntry($Frame, "AtomMass",       "M:",  \$this->{AtomMass},     "%f", $EntryWidth,   "", 0, 2, "normal");

	$this->{TEntry}     = &MakeLabelEntry($Frame, "T",       "T:",  \$this->{T},     "%12.6g", $EntryWidth,   "K",    1, 0, "normal");
	$this->{PEntry}     = &MakeLabelEntry($Frame, "P",       "P:",  \$this->{P},     "%12.6g", $EntryWidth,   "N/m2", 1, 1, "normal");
	$this->{PhPaEntry}  = &MakeLabelEntry($Frame, "PhPa",    "P:",  \$this->{PhPa},  "%12.6g", $EntryWidth,   "hPa",  1, 2, "normal");
	$this->{PatmEntry}  = &MakeLabelEntry($Frame, "Patm",    "P:",  \$this->{Patm},  "%12.6g", $EntryWidth,   "atm",  1, 3, "normal");
	$this->{PmbarEntry} = &MakeLabelEntry($Frame, "Pmbar",   "P:",  \$this->{Pmbar}, "%12.6g", $EntryWidth,   "mbar", 2, 0, "normal");
	$this->{PTorrEntry} = &MakeLabelEntry($Frame, "PTorr",   "P:",  \$this->{PTorr}, "%12.6g", $EntryWidth,   "Torr", 2, 1, "normal");

	$this->{nEntry}     = &MakeLabelEntry($Frame, "n",       "n:",  \$this->{n}, "%12.4g", $EntryWidth,   "mol",  3, 0, "normal");
	$this->{VEntry}     = &MakeLabelEntry($Frame, "V",       "V:",  \$this->{V}, "%12.4g", $EntryWidth,   "m3",   3, 1, "normal");


	$this->{RtotalEntry}      = &MakeLabelEntry($Frame, "Rtotal", "Rtot:",  \$this->{Rtotal},          "%f", $EntryWidth,    "nm", 4, 0, "normal");
	$this->{GasVelocityEntry} = &MakeLabelEntry($Frame, "v",         "v:",  \$this->{GasVelocity}, "%12.4g", $EntryWidth,   "m/s", 4, 1, "normal");
	$this->{MFPathEntry}      = &MakeLabelEntry($Frame, "MFPath",  "MFP:",  \$this->{MFPath},      "%12.4g", $EntryWidth,     "m", 4, 2, "normal");
}

sub MakeDispersionPage
{
	my ($page, $EntryWidth) = @_;

	$this->{hv}                 = 3.0; # eV
	$this->{PhotonFlux}         = 1.0e15; # 1/cm2/s
	$this->{WLight}             = $this->{hv} * $e * $this->{PhotonFlux}; # W/cm2
	$this->{ULight}             = $this->{WLight} / ($c * 1.0e2); # W/cm3
	$this->{EmaxLight}          = &CalEmaxLight($this->{hv}, $this->{PhotonFlux}); # V/m

	$this->{OscillatorStrength} = 1.0;
	$this->{Omega}              = 1.0e15; # s-1
	$this->{Omega0}             = 2.0e15; # s-1
	$this->{DampingFactor}      = 1.0e14; # s-1
	$this->{xmax}               = &CalMaxDisplacementForLorentz(
									$this->{EmaxLight}, $this->{OscillatorStrength},
									$this->{Omega}, $this->{Omega0}, $this->{DampingFactor}) * 1.0e9; # nm
#print "Emax=$this->{EmaxLight} V/m\n";
#print "xmax=$this->{xmax} nm\n";

	my $Frame  = $page->Frame()->pack(-anchor => 'nw', -fill => 'x');

	$this->{hvEntry}     = &MakeLabelEntry($Frame, "hv",     "hv:",     \$this->{hv},                 "%8.4f", $EntryWidth, "eV",      1, 0, "normal");
	$this->{FEntry}      = &MakeLabelEntry($Frame, "F",      "F:",      \$this->{PhotonFlux},        "%12.4g", $EntryWidth, "cm-2s-1", 1, 1, "normal");
	$this->{WEntry}      = &MakeLabelEntry($Frame, "W",      "W:",      \$this->{WLight},            "%12.4g", $EntryWidth, "W/cm2",   2, 0, "normal");
	$this->{UEntry}      = &MakeLabelEntry($Frame, "U",      "U:",      \$this->{ULight},            "%12.4g", $EntryWidth, "W/cm3",   2, 1, "normal");
	$this->{EmaxEntry}   = &MakeLabelEntry($Frame, "Emax",   "Emax:",   \$this->{EmaxLight},         "%12.4g", $EntryWidth, "V/m",     2, 2, "disabled");

	$this->{OmegaEntry}  = &MakeLabelEntry($Frame, "omega",  "omega:",  \$this->{Omega},              "%8.4g", $EntryWidth, "s-1",     3, 0, "normal");
	$this->{Omega0Entry} = &MakeLabelEntry($Frame, "omega0", "omega0:", \$this->{Omega0},             "%8.4g", $EntryWidth, "s-1",     3, 1, "normal");
	$this->{GammaEntry}  = &MakeLabelEntry($Frame, "gamma",  "gamma:",  \$this->{DampingFactor},     "%12.4g", $EntryWidth, "s-1",     3, 2, "normal");
	$this->{faEntry}     = &MakeLabelEntry($Frame, "fa",     "fa:",     \$this->{OscillatorStrength}, "%8.4g", $EntryWidth, "",        4, 0, "normal");
	$this->{xmaxEntry}   = &MakeLabelEntry($Frame, "xmax",   "xmax:",   \$this->{xmax},               "%8.4g", $EntryWidth, "nm",      4, 1, "disabled");

	return $Frame;
}

sub MakeConductivityPage
{
	my ($page, $EntryWidth) = @_;

	my $Frame = $page->Frame()->pack(-anchor => 'nw', -fill => 'x');
	$Frame->MyLabel(-text => '')->grid(-row => 5, -column => 0, -columnspan => 1, -sticky => 'e' );

	$this->{EffMassConductivityEntry}        = &MakeLabelEntry($Frame, "m*",    "m*:",       \$EffMass,         "%8.3f", $EntryWidth, "me0",    6, 0);
	$this->{RelaxationTimeConductivityEntry} = &MakeLabelEntry($Frame, "tau",   "tau:",      \$RelaxationTime, "%10.4g", $EntryWidth, "s",      6, 1);
	$this->{MobilityConductivityEntry}       = &MakeLabelEntry($Frame, "mu",    "Mobility:", \$Mobility,       "%10.4g", $EntryWidth, "cm2/Vs", 6, 2);
	$this->{NeConductivityEntry}             = &MakeLabelEntry($Frame, "Ne",    "Ne:",       \$Ne,             "%10.4g", $EntryWidth, "cm-3",   7, 0);
	$this->{ConductivityConductivityEntry}   = &MakeLabelEntry($Frame, "sigma", "sigma:",    \$Conductivity,   "%10.4g", $EntryWidth, "S/cm",   7, 1);
	return $Frame;
}

sub MakeNcPage
{
	my ($page, $EntryWidth) = @_;

	my $Frame = $page->Frame()->pack(-anchor => 'nw', -fill => 'x');
	$this->{TEntry}     = &MakeLabelEntry($Frame, "T",     "T:",         \$T,  "%8.2f", $EntryWidth, "K",      1, 0, "normal");

	$this->{EffMassEntry}                    = &MakeLabelEntry($Frame, "m*",    "m*:",         \$EffMass,  "%8.3f", $EntryWidth, "me0",  6, 0);
	$this->{CBMultEntry}                     = &MakeLabelEntry($Frame, "M",     "M:",   \$CBMultiplicity,  "%6.2f", $EntryWidth, "",     6, 1);
	$this->{NeEntry}                         = &MakeLabelEntry($Frame, "Ne",    "Ne:",              \$Ne, "%10.4g", $EntryWidth, "cm-3", 6, 2, "disabled");
	$this->{NcEntry}                         = &MakeLabelEntry($Frame, "Nc",    "Nc:",              \$Nc, "%10.4g", $EntryWidth, "cm-3", 7, 0, "disabled");
	$this->{Dc0Entry}                        = &MakeLabelEntry($Frame, "Dc0",   "D(E)=",           \$Dc0, "%10.4g", $EntryWidth, "E^0.5 /cm^3/eV", 7, 1, "disabled");
	$this->{dEBMEntry}                       = &MakeLabelEntry($Frame, "dEBM",  "dE(BM):",        \$dEBM, "%10.4g", $EntryWidth, "eV",   8, 0, "disabled");
	$this->{EpEntry}                         = &MakeLabelEntry($Frame, "Ep",    "Ep:",              \$Ep, "%10.4g", $EntryWidth, "eV",   8, 1);
	$this->{EFieldEntry}                     = &MakeLabelEntry($Frame, "EField", "E:",          \$EField, "%10.4g", $EntryWidth, "V/cm", 8, 2);

	$this->{RelaxationTimeConductivityEntry} = &MakeLabelEntry($Frame, "tau",   "tau:", \$RelaxationTime, "%10.4g", $EntryWidth, "s",      9, 0);
	$this->{MobilityConductivityEntry}       = &MakeLabelEntry($Frame, "mu",    "Mobility:",  \$Mobility, "%10.4g", $EntryWidth, "cm2/Vs", 9, 1);
	$this->{ConductivityConductivityEntry}   = &MakeLabelEntry($Frame, "sigma", "sigma:", \$Conductivity, "%10.4g", $EntryWidth, "S/cm",   9, 2);

	$this->{vthConductivityEntry}            = &MakeLabelEntry($Frame, "vth",   "vth:",            \$vth, "%10.4g", $EntryWidth, "m/s",  10, 0, "disabled");
	$this->{lthConductivityEntry}            = &MakeLabelEntry($Frame, "lth",   "lth:",            \$lth, "%10.4g", $EntryWidth,  "nm",  10, 1, "disabled");
	$this->{vFEConductivityEntry}            = &MakeLabelEntry($Frame, "vFE",   "vFE:",            \$vFE, "%10.4g", $EntryWidth, "m/s",  11, 0, "disabled");
	$this->{lFEConductivityEntry}            = &MakeLabelEntry($Frame, "lFE",   "lFE:",            \$lFE, "%10.4g", $EntryWidth,  "nm",  11, 1, "disabled");

	$this->{vDriftConductivityEntry}         = &MakeLabelEntry($Frame, "vDrift",    "vDrift:",  \$vDrift, "%10.4g", $EntryWidth,  "m/s", 12, 0, "disabled");
	$this->{lDriftConductivityEntry}         = &MakeLabelEntry($Frame, "lDrift",    "lDrift:",  \$lDrift, "%10.4g", $EntryWidth,  "nm",  12, 1, "disabled");
	$this->{DiffConstConductivityEntry}      = &MakeLabelEntry($Frame, "DiffConst", "D:", \$DiffusionConstant, "%10.4g", $EntryWidth,  "m2/s", 13, 0, "disabled");
	$this->{lDiffConductivityEntry}          = &MakeLabelEntry($Frame, "lDiff",     "lDiff:",    \$lDiff, "%10.4g", $EntryWidth,  "nm",  13, 1, "disabled");

	return $Frame;
}

sub MakeConstantsPage
{
	my ($page, $EntryWidth) = @_;

	my $Frame = $page->Frame()->pack(-anchor => 'nw', -fill => 'x');

	my $c  = 2.99792458e8; # m/s";
	my $mp = 1.6726231e-27; # kg";
	my $mn = 1.67495e-27; # kg";
	my $u0 = 4*3.14*1e-7; # . "Ns<sup>2</sup>C<sup>-2</sup>";
	my $a0 = 5.29177e-11; # m";
	my $NA = 6.0221367e23; # mol<sup>-1</sup>";
	my $basee = 2.71828183;
	my $R = 8.31451; # J/K/mol";
	my $F = 96485.3; # C/mol";
	my $g = 9.81; # m/s2";
	my $HartreeToeV = 27.1481; # eV";
	my $RyToeV = $HartreeToeV / 2.0;
	my $G = 6.67259e-11; # Nm2/kg2 
	my $Debye = 3.33564e-30; # Cm

	&MakeLabelEntry($Frame, "pi",   "pi:",         \$pi,  "%g", $EntryWidth, "",        0, 0);
	&MakeLabelEntry($Frame, "e(b)", "e(base):", \$basee,  "%g", $EntryWidth, "",        0, 1);

	&MakeLabelEntry($Frame, "c",    "c:",           \$c,  "%g", $EntryWidth, "m/s",     1, 0);
	&MakeLabelEntry($Frame, "e0",   "e0:",         \$e0,  "%g", $EntryWidth, "C2/N/m2", 1, 1);

	&MakeLabelEntry($Frame, "e",    "e:",           \$e,  "%g", $EntryWidth, "C",       2, 0);
	&MakeLabelEntry($Frame, "u0",   "u0:",         \$u0,  "%g", $EntryWidth, "Ns2/C2",  2, 1);

	&MakeLabelEntry($Frame, "h",    "h:",           \$h,  "%g", $EntryWidth, "Js",      3, 0);
	&MakeLabelEntry($Frame, "hbar", "hbar:",     \$hbar,  "%g", $EntryWidth, "Js",      3, 1);

	&MakeLabelEntry($Frame, "kB",   "kB:",         \$kB,  "%g", $EntryWidth, "J/K",     4, 0);
	&MakeLabelEntry($Frame, "R",    "R:",           \$R,  "%g", $EntryWidth, "J/K/mol", 4, 1);
	&MakeLabelEntry($Frame, "F",    "F:",           \$F,  "%g", $EntryWidth, "C/mol",   4, 2);

	&MakeLabelEntry($Frame, "NA",   "NA:",         \$NA,  "%g", $EntryWidth, "/mol",    5, 0);
	&MakeLabelEntry($Frame, "g",    "g:",           \$g,  "%g", $EntryWidth, "m/s^2",   5, 1);
	&MakeLabelEntry($Frame, "G",    "G:",           \$G,  "%g", $EntryWidth, "Nm2/kg2", 5, 2);

	&MakeLabelEntry($Frame, "a0",   "a0:",          \$a0, "%g", $EntryWidth, "m",       6, 0);
	&MakeLabelEntry($Frame, "Hr",   "Hr:", \$HartreeToeV, "%g", $EntryWidth, "eV",      6, 1);
	&MakeLabelEntry($Frame, "Ry",   "Ry:",      \$RyToeV, "%g", $EntryWidth, "eV",      6, 2);

	&MakeLabelEntry($Frame, "Debye","Debye:",    \$Debye, "%g", $EntryWidth, "Cm",      7, 1);

	&MakeLabelEntry($Frame, "me",   "me:",          \$me, "%g", $EntryWidth, "kg",      8, 0);
	&MakeLabelEntry($Frame, "mp",   "mp:",          \$mp, "%g", $EntryWidth, "kg",      8, 1);
	&MakeLabelEntry($Frame, "mn",   "mn:",          \$mn, "%g", $EntryWidth, "kg",      8, 2);
	return $Frame;
}

sub MakeCalculatorPage
{
	my ($page, $EntryWidth) = @_;
	$EntryWidth = 50 if($EntryWidth);
	my $Frame = $page->Frame()->pack(-anchor => 'nw', -fill => 'x');
	my $Equation = '$kB * $T / $e';
	$this->{EquationEntry} = &MakeLabelEntry($Frame, "Equation", "Eq:", \$Equation,  "%s", $EntryWidth, "", 1, 0, "normal");
	my $Answer = eval($Equation);
	$this->{AnswerEntry}   = &MakeLabelEntry($Frame, "Answer",   "Ans:", \$Answer,   "%s", $EntryWidth, "", 2, 0, "normal");
if(0) {
	$Frame->MyLabel(-text => 'Eq:')->grid(-row => 3, -column => 0, -columnspan => 1, -sticky => 'e' );
	$this->{EquationText}  = $Frame->MyText(
		-width        => 50,
		-height       => 3,
		-text         => \$Equation,
		-state        => 'normal',
		)->grid(-row => 3, -column => 1, -columnspan => 1, -sticky => 'e' );
	$Frame->MyLabel(-text => 'Ans')->grid(-row => 4, -column => 0, -columnspan => 1, -sticky => 'e' );
	$this->{AnswerText}  = $Frame->MyText(
		-width        => 50,
		-height       => 3,
		-text         => \$Answer,
		-state        => 'normal',
		)->grid(-row => 4, -column => 1, -columnspan => 1, -sticky => 'e' );
}

	return $Frame;
}

sub MakeLabelEntry
{
	my ($Frame, $name, $label1, $pVariable, $format, $EntryWidth, $label2, $row, $column, $state) = @_;
	$state = 'normal' if(!defined $state);
#print "Frame=$Frame  name=$name,$label1,$format,$label2\n";
	$$pVariable = Utils::DelSpace(sprintf($format, $$pVariable));
	$Frame->MyLabel(-text => $label1)->grid(-row => $row, -column => 3*$column+0, -columnspan => 1, -sticky => 'e' );
	my $ent  = $Frame->MyEntry(
		-name         => $name,
		-width        => $EntryWidth,
#		-text         => $val,
		-textvariable => $pVariable,
		-state        => $state,
		-format       => $format,
		)->grid(-row => $row, -column => 3*$column+1, -columnspan => 1, -sticky => 'e' );
	$Frame->MyLabel(-text => $label2)->grid(-row => $row, -column => 3*$column+2, -columnspan => 1, -sticky => 'w' );
	$ent->bind('<FocusOut>', [\&EntryFocusedOut, 'FocusOut', $name]);

	$this->{pEntryArray} = [] if(!$this->{pEntryArray});
	push(@{$this->{pEntryArray}}, $ent);
	return $ent;
}

sub CalEffectiveMassFromPlasmaFrequency
{
# Ne: m-3, Ep: eV
	my ($Ep, $Ne) = @_;
	my $k = 2.0 * $pi * $Ep * $e / $h;
	$k = $k * $k;
	my $m = $Ne * $e*$e / $me / $e0 / $k;
	return $m; # in $me
}

sub CalNeFromPlasmaFrequency
{
# m: in $me, $Ep: eV
	my ($Ep, $m) = @_;

	my $k = 2.0 * $pi * $Ep * $e / $h;
	$k = $k * $k;
	my $Ne = $k / ($e*$e / $m / $me / $e0);
	return $Ne; # m-3
}

sub CalC
{
	my ($er, $S, $dgate) = @_; # m
	my $C = $er * $e0 * $S / $dgate; # F/m2
#print "C=$C m  S=$S m2  d=$dgate m\n";
	return $C;
}

sub Caldgate
{
	my ($er, $S, $C) = @_; # m
	return $er * $e0 * $S / $C;
}

sub CalS
{
	my ($T, $er, $dgate, $Dit) = @_; # m
	my $C = &CalC($er, 1.0*1.0, $dgate); # F/m2
#print "C=$C F/m2\n";
#print "eDit/C=", $e * $Dit / $C, "\n";	
#print "e0=$e0\n";
	return $kB * $T * log(10.0) * (1.0 + $e * $Dit / $C) / $e;
}

sub CalDit
{
	my ($T, $er, $S, $dgate, $SS) = @_; # m
	return ($Cox / $e) * ($SS * $e / ($kB * $T * log(10.0)) - 1.0);
}

sub MakeUnitConversionPage
{
	my ($Frame, $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)));
#print "f=$f E=$E wl=$wl\n";
		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] = $Frame->Frame()->pack(-side => 'top', -expand => 'yes', -fill => 'both');
		$Frames[$i] = $Frame->Frame()->pack(-side => 'top');

		my $ent = $Frames[$i]->Entry(
			-state        => $state,
			-justify      => $justify,
			-width        => $EntryWidth,
			-background   => $background,
			-textvariable => \$EntryString[0],
			)->pack(-side => 'left');
		$Frame->bind($ent, '<FocusIn>',  [\&UnitConvEntryFocusedOut, 'FocusIn',  \@pEntryString, $i, 'eV']);
		$Frame->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');
		$Frame->bind($ent, '<FocusIn>',  [\&UnitConvEntryFocusedOut, 'FocusIn',  \@pEntryString, $i, 'nm']);
		$Frame->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');
		$Frame->bind($ent, '<FocusIn>',  [\&UnitConvEntryFocusedOut, 'FocusIn',  \@pEntryString, $i, 'Hz']);
		$Frame->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');
		$Frame->bind($ent, '<FocusIn>',  [\&UnitConvEntryFocusedOut, 'FocusIn',  \@pEntryString, $i, '/s']);
		$Frame->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');
		$Frame->bind($ent, '<FocusIn>',  [\&UnitConvEntryFocusedOut, 'FocusIn',  \@pEntryString, $i, '/cm']);
		$Frame->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');
		$Frame->bind($ent, '<FocusIn>',  [\&UnitConvEntryFocusedOut, 'FocusIn',  \@pEntryString, $i, 's']);
		$Frame->bind($ent, '<FocusOut>', [\&UnitConvEntryFocusedOut, 'FocusOut', \@pEntryString, $i, 's']);
	}
	return $Frame;
}

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

	my $pVars = $ppEntryString->[$idx];
	if($event eq 'FocusOut') {
		my $E  = $pVars->[0];
		if($unit eq 'nm') {
			my $wl = $pVars->[1];
			$E  = Optics::nmToeV($wl);
		}
		if($unit eq 'Hz') {
			my $f  = $pVars->[2];
			$E  = Optics::HzToeV($f);
		}
		if($unit eq '/s') {
			my $w  = $pVars->[3];
			$E  = Optics::HzToeV($w / 2.0 / $pi)
		}
		if($unit eq '/cm') {
			my $k  = $pVars->[4];
			$E  = Optics::nmToeV(1.0 / $k * 1.0e7);
		}
		if($unit eq 's') {
			my $t  = $pVars->[5];
			$E  = Optics::HzToeV(1.0 / $t);
		}
		my $wl = Optics::eVTonm($E);
		my $f  = Optics::eVToHz($E);
		my $w  = 2.0 * $pi * $f;
		my $k  = Optics::eVToKiser($E);
		my $t  = 1.0 / $f;

		$pVars->[0] = Utils::DelSpace(sprintf("%6.3f", Optics::HzToeV($f)));
		$pVars->[1] = Utils::DelSpace(sprintf("%8.2f", Optics::eVTonm($E)));
		$pVars->[2] = Utils::DelSpace(sprintf("%8.4g", 1.0 / $t));
		$pVars->[3] = Utils::DelSpace(sprintf("%8.4g", 2.0 * $pi * $f));
		$pVars->[4] = Utils::DelSpace(sprintf("%8.0f", Optics::eVToKiser($E)));
		$pVars->[5] = Utils::DelSpace(sprintf("%12.4g", 1.0 / $f));
	}

}
