$Tk::LabelWgs::VERSION = 1.0;

package Tk::LabelWgs;

use base qw(Tk::LabelFmt);

use strict;

Construct Tk::Widget 'LabelWgs';

sub ClassInit {
    my ($class, $mw) = @_;
    $class->Tk::LabelFmt::ClassInit($mw);
}

sub Populate {
    my ($w, $args) = @_;
    $w->Tk::LabelFmt::Populate($args);
    $w->ConfigSpecs(
		    -undefstring =>  [qw/PASSIVE undefstring UndefString/, "-- --.--'?"],
		    -formatcmd =>    [qw/CALLBACK formatCmd FormatCmd/, \&FormatCmdWgs],
		    -axis =>         [qw/METHOD axis Axis/, "alt"],
		    -compass =>      [qw/PASSIVE compass Compass/, ['N', 'S', 'E', 'W']],
		    -format  =>      [qw/PASSIVE format Format/, "%10.3f"],
		    -bases  =>       [qw/PASSIVE format Format/, [1, 60, 100] ],
		    );
}

my %AXIES = ('lat' => 0, 'lon' => 1, 'alt' => 2);

sub axis {
    my $w = shift;
    return $w->{'axis'} if (!@_); # This is a cget.
    my $axis = shift;
    if (!exists $AXIES{$axis}) {
	$w->BackTrace("Invalid wgs type specified: '$axis'");
    } else {
	$w->{'axis'} = $axis;
    }
}

sub FormatCmdWgs {
    my ($w, $dwgs) = @_;
    return $w->cget('-undefstring') if (!defined $dwgs);

    my $axis = $w->cget('-axis');
    return sprintf($w->cget('-format'), $dwgs) if ($axis eq 'alt');
    
    my $bases = $w->cget('-bases');

    my $compass = $w->cget('-compass');
    my $hemi = $compass->[$AXIES{$axis}*2+($dwgs < 0)];
    $dwgs = abs($dwgs);
    my $deg = int $dwgs * $bases->[0];
    $dwgs = ($dwgs - $deg) * $bases->[1];
    my $min = int $dwgs;
    my $hmin = int (($dwgs - $min) * $bases->[2]);
    return sprintf("%02ld %02ld.%02ld' %s", $deg, $min, $hmin, $hemi);
}
#sub Configured { Tk::LabelFmt::Configured(@_); }

1;
__END__

=head1 NAME
Tk::LabelWgs - Print variables in wgs84 format.

=head1 SYNOPSIS

S<    >I<$lo> = I<$parent>-E<gt>B<LabelWgs>(I<-option> =E<gt> I<value>, ... );

=head1 DESCRIPTION

This widget is derived from LabelFmt and contains additional options for formatting wgs
latitude, longitude, and altitude values. The same options that are abailable for 
LabelFmt are also available here.

=over 4

=item B<Label options>

LabelTimer takes all valid options of Tk::Label;

=item B<LabelFmt options>

LabelTimer takes all valid options of Tk::LabelFmt;

=item B<-axis>

Must be one of 'lat', 'lon', or 'alt'. Default is 'alt'.

=item B<-compass>

A reference to an array of values to be displayed for north, south, east and west.
Default is ['N','S','E','W'].

=back

=head1 METHODS

None.

=head1 ADVERTISED WIDGETS

None.

=head1 EXAMPLES

my @position = (0,0,0);

I<$lo> = I<$mw>-E<gt>B<LabelWgs>(-axis=>'lat', -textvariable =E<gt> \$position[0]);

I<$lo> = I<$mw>-E<gt>B<LabelWgs>(-axis=>'lon', -textvariable =E<gt> \$position[1]);

I<$lo> = I<$mw>-E<gt>B<LabelWgs>(-axis=>'alt', -textvariable =E<gt> \$position[2]);

@position = (1.0,2.0,3.0);

=head1 AUTHOR

viosca@imageman.com

This program is free software; you can redistribute it andor modify it and/ormodify it 
under the same terms as Perl itself.

=head1 SEE ALSO

L<Tk::LabelFmt|Tk::LabelFmt>
L<Tk::LabelTimer|Tk::LabelTimer>

=cut
