App-Chart

 view release on metacpan or  search on metacpan

maybe/SymbolHistory.pm  view on Meta::CPAN


  if (DEBUG) { print "SymbolHistory forward to $symbol $symlist\n"; }
  $self->goto ($symbol, $symlist);
  return ($self->{'current_symbol'},
          $self->{'current_symlist'});
}

# set the 'forward-action' and 'back-action' objects sensitive or not
# according to there being something to go forward or back to
sub _update_actions {
  my ($self) = @_;
  if (my $action = $self->{'forward_action'}) {
    my $model = $self->{'forward_model'};
    $action->set_sensitive ($model->get_iter_first);
  }
  if (my $action = $self->{'back_action'}) {
    my $model = $self->{'back_model'};
    $action->set_sensitive ($model->get_iter_first);
  }
}

# enforce MAX_HISTORY on the given liststore model
# if it's too big then remove elements from the end
sub _limit {
  my ($model) = @_;
  my $len = $model->iter_n_children (undef);
  for (my $pos = $len - 1; $pos >= MAX_HISTORY; $pos--) {
    $model->remove ($model->iter_nth_child (undef, $pos));
  }
}

# 'delete-symbol' notify handler
sub _do_delete_symbol {
  my ($self, $symbol) = @_;
  foreach my $model ($self->{'back_model'}, $self->{'forward_model'}) {
    require Gtk2::Ex::TreeModelBits;
    Gtk2::Ex::TreeModelBits::remove_matching_rows
        ($model, sub {
           my ($model, $iter) = @_;
           return ($model->get_value ($iter, COL_SYMBOL) eq $symbol);
         });
  }
}

#------------------------------------------------------------------------------
# menu

sub back_menu {
  my ($self) = @_;
  return _make_menu ($self, 'back_menu', 'back_model',
                     __('Chart: Back'));
}
sub forward_menu {
  my ($self) = @_;
  return _make_menu ($self, 'forward_menu', 'forward_model',
                     __('Chart: Forward'));
}
sub _make_menu {
  my ($self, $menu_key, $model_key, $tearoff_title) = @_;
  return ($self->{$menu_key} ||= do {
    require Gtk2::Ex::MenuView;
    require Gtk2::Ex::Dashes::MenuItem;
    my $menuview = Gtk2::Ex::MenuView->new
      (model          => $self->{$model_key},
#        tearoff_items  => 'top',
#        tearoff_title  => $tearoff_title,
#        tearoff_type   => 'Gtk2::Ex::Dashes::MenuItem',
      );
    $menuview->signal_connect
      (item_create_or_update => \&_do_item_create_or_update);
    $menuview->signal_connect
      (activate => \&_do_menu_activate, App::Chart::Glib::Ex::MoreUtils::ref_weak($self));
#     $menuview->signal_connect
#       (tearoff => \&_do_menu_tearoff, App::Chart::Glib::Ex::MoreUtils::ref_weak($self));
    $menuview;
  });
}

# MenuView 'tearoff'
# sub _do_menu_tearoff {
#   my ($menuview, $ref_weak_self) = @_;
#   my $self = $$ref_weak_self || return;
#   require App::Chart::BrowseHistoryDialog;
#   my $dialog = App::Chart::BrowseHistoryDialog->new
#     (back_model    => $self->{'back_model'},
#      forward_model => $self->{'forward_model'});
#   $dialog->present;
# }

# MenuView 'item-create-or-update'
sub _do_item_create_or_update {
  my ($menuview, $item, $model, $path, $iter) = @_;
  $item ||= Gtk2::MenuItem->new_with_label ('');
  $item->show;
  my $symbol = $model->get_value ($iter, COL_SYMBOL);
  require App::Chart::Database;
  my $name = App::Chart::Database->symbol_name ($symbol);
  $item->get_child->set_text ($symbol . ($name ? ' - ' . $name : ''));
  return $item;
}

sub _do_menu_activate {
  my ($menuview, $model, $path, $iter, $ref_weak_self) = @_;
  my $self = $$ref_weak_self || return;
  my ($pos) = $path->get_indices;
  if ($model == $self->{'back_model'}) {
    foreach (0 .. $pos) { $self->back; }
  } else {
    foreach (0 .. $pos) { $self->forward; }
  }
  $self->signal_emit ('menu_activate',
                      $self->{'current_symbol'},
                      $self->{'current_symlist'})
}

1;
__END__

=for stopwords symlist undef

=head1 NAME

App::Chart::SymbolHistory -- previously visited symbols

=head1 SYNOPSIS

 use App::Chart::SymbolHistory;
 my $history = App::Chart::SymbolHistory->new;

=head1 OBJECT HIERARCHY

C<App::Chart::SymbolHistory> is a subclass of C<Glib::Object>.

    Glib::Object
      App::Chart::SymbolHistory

=head1 DESCRIPTION

A C<App::Chart::SymbolHistory> object records a history of visited symbol and
symlist, allowing navigation back or forward in the list.

=head1 FUNCTIONS

=over 4

=item C<< App::Chart::SymbolHistory->new >>

Create and return a new symbol history object.

=cut

=item C<< $history->goto ($symbol, $symlist) >>

Add symbol+symlist to C<$history> as the currently viewed position.  If this
is different than previously noted then that previous symbol+symlist is
added to the "back" list.

=item C<< $history->back() >>

=item C<< $history->forward() >>

Go back or forward in C<$history>.  The return is values C<($symbol,
$symlist)> which is where to go to, or C<(undef,undef)> if nothing further
to go to.

=item C<< $history->back_menu >>

=item C<< $history->forward_menu >>

Return a C<Gtk2::Menu> of symbols to go back or forward to.

=back

=head1 PROPERTIES

=over 4

=item C<back-action> (C<Gtk2::Action>, default undef)

=item C<forward-action> (C<Gtk2::Action>, default undef)

Action objects to be set sensitive or insensitive according to whether
there's anything to go back or forward to.

=back

=head1 SIGNALS

=over 4

=item C<menu-activate> (parameters: history, symbol, symlist)

Emitted when an item symbol+symlist is selected from the back or forward
menus (as created by the C<back_menu> etc functions above).

=back

=head1 SEE ALSO

L<Gtk2::Ex::MenuView>

=cut



( run in 0.882 second using v1.01-cache-2.11-cpan-7fcb06a456a )