App-InvestSim

 view release on metacpan or  search on metacpan

lib/App/InvestSim/GUI.pm  view on Meta::CPAN

package App::InvestSim::GUI;

use 5.022;
use strict;
use warnings;

use App::InvestSim::Config ':all';
use App::InvestSim::Finance;
use App::InvestSim::LiteGUI ':all';
use App::InvestSim::Values ':all';
use CLDR::Number;
use File::Spec::Functions;
use Text::Wrap ();
use Tkx;

# All entry aligned in a column should have the same width (in number of
# characters), for simplicity we are using the same width everywhere.
use constant ENTRY_WIDTH => 10;

my $cldr = CLDR::Number->new(locale => 'fr-FR');
my $cldr_currency = $cldr->currency_formatter(currency_code => 'EUR', maximum_fraction_digits => 0);
my $cldr_percent = $cldr->percent_formatter(minimum_fraction_digits => 1);
my $cldr_decimal = $cldr->decimal_formatter();

sub format_euro {
  my ($val) = @_;
  $val = 0 unless $val;  # Cover the case where $val is undef or '' (also 0).
  return $cldr_currency->format($val);
}

sub format_percent {
  my ($val) = @_;
  $val = 0 unless $val;  # Cover the case where $val is undef or '' (also 0).
  return $cldr_percent->format($val / 100);
}

sub format_year {
  my ($val) = @_;
  $val = 0 unless $val;  # Cover the case where $val is undef or '' (also 0).
  return '1 an' if $val == 1;
  return "${val} ans";
}

sub format_surface {
  my ($val) = @_;
  $val = 0 unless $val;  # Cover the case where $val is undef or '' (also 0).
  return $cldr_decimal->format($val).' m²';
}

# Force-refresh the content of all entry field.
my @all_refresh_actions; # a list of sub-reference to execute
sub refresh_all_fields {
  Tkx::focus('.');
  map { $_->() } @all_refresh_actions;
  calculate_all();
}

# Callback called when one of our validated fields gets the focus.
# widget is the widget itself (the object, not the Tk path), $var is a scalar
# reference to the variable holding the un-decorated value. $right_justified is
# true if the field should be right justified when not edited.
# This replaces the beautified content of the entry, with the raw content for
# editing.
sub focus_in_field {
  my ($widget, $var, $right_justified) = @_;
  $widget->m_delete(0, 'end');
  $widget->m_insert(0, $$var);
  $widget->m_configure(-justify => 'left') if $right_justified;
  $widget->m_configure(-validate => 'key');
}

# Callback called when one of our validated fields lose the focus. Arguments are
# the same as for focus_in_field, with the addition of $refresh which is a method
# called to format the raw value into a better looking string.
# This replaces the raw content of the entry with the beautified text.
sub focus_out_field {
  my ($widget, $var, $right_justified, $refresh) = @_;
  $widget->m_configure(-validate => 'none');
  # Todo: check here that the widget is in a valid state before proceeding.
  $$var = $widget->m_get() =~ s/,/./r;



( run in 2.079 seconds using v1.01-cache-2.11-cpan-ceb78f64989 )