
package ZooZ::TiedVar;
$ZooZ::TiedVar::VERSION = '1.9.1';
use strict;

# The purpose of this is to automatically apply
# any changes to widget options as soon as those
# changes are made.

1;

sub TIESCALAR {
  my ($class, $w, $d, $m, $o, $l, $p) = @_;

  $p ||= [];

  return bless {
		W => $w,  # the widget
		V => $d,  # default value
		M => $m,  # method to use.
		O => $o,  # the option name
		L => $l,  # the label.
		P => $p,  # pre-options
		C => 0,   # whether it changed or not.
	       } => $class;
}

sub FETCH { $_[0]{V} }

sub STORE {
  my ($self, $v) = @_;

  $self->{V} = $v;
  $self->{C} = 1;

  # don't apply it if it's undefined.
  defined $v or return;

  # try to apply it.
  my $m = $self->{M};

  #print "Evaling $m on $self->{W} with @{$self->{P}} and $self->{O} => $v.\n";
  eval {
    $self->{W}->$m(@{$self->{P}}, $self->{O} => $v);
  };

  if ($@) {
    $self->{L}->configure(-fg => 'red')   if $self->{L};
  } else {
    $self->{L}->configure(-fg => 'black') if $self->{L};
  }
}

sub changed { $_[0]{C} }

__END__

=pod

=encoding UTF-8

=head1 NAME

ZooZ::TiedVar

=head1 VERSION

version 1.9.1

=head1 AUTHOR

Ala Qumsieh, faraco <skelic3@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2004-2005, 2018 by Ala Qumsieh, faraco.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
