| 1 | # Copyright 2007, 2008, 2009, 2010, 2011, 2016 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify | ||
| 6 | # it under the terms of the GNU General Public License as published by the | ||
| 7 | # Free Software Foundation; either version 3, or (at your option) any later | ||
| 8 | # version. | ||
| 9 | # | ||
| 10 | # Chart is distributed in the hope that it will be useful, but | ||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 13 | # for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | package App::Chart::Gtk2::Ex::TreePath::Circular; | ||
| 19 | use 5.008; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use Carp; | ||
| 23 | use List::Util qw(min max); | ||
| 24 | use Gtk2; | ||
| 25 | use App::Chart::Gtk2::Ex::TreePath::Subclass; | ||
| 26 | our @ISA = ('App::Chart::Gtk2::Ex::TreePath::Subclass', 'Gtk2::TreePath'); | ||
| 27 | |||
| 28 | use constant DEBUG => 0; | ||
| 29 | |||
| 30 | sub goto_index { | ||
| 31 | my ($self, $index) = @_; | ||
| 32 | $self->up; | ||
| 33 | $self->append_index ($index || 0); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub next { | ||
| 37 | my ($self, $model) = @_; | ||
| 38 | if (! $model) { croak "TreePath::Circular next needs the tree model"; } | ||
| 39 | $self->SUPER::next; | ||
| 40 | my ($cur_index) = $self->get_indices; | ||
| 41 | my $rows = $model->iter_n_children (undef); | ||
| 42 | if ($cur_index >= $rows) { | ||
| 43 | $self->goto_index (0); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | sub prev { | ||
| 48 | my ($self, $model) = @_; | ||
| 49 | if (! $model) { croak "TreePath::Circular next needs the tree model"; } | ||
| 50 | if (! $self->SUPER::prev) { | ||
| 51 | # was at position 0, wrap to end | ||
| 52 | $self->goto_index (max (0, $model->iter_n_children (undef) - 1)); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | sub row_inserted { | ||
| 57 | my ($self, $model, $ins_path, $ins_iter) = @_; | ||
| 58 | my ($cur_index) = $self->get_indices; | ||
| 59 | my ($ins_index) = $ins_path->get_indices; | ||
| 60 | # if inserted before current then advance | ||
| 61 | if ($ins_index < $cur_index) { $self->next ($model); } | ||
| 62 | if (DEBUG) { print "ins to ", $self->to_string, "\n"; } | ||
| 63 | } | ||
| 64 | |||
| 65 | sub row_deleted { | ||
| 66 | my ($self, $model, $del_path) = @_; | ||
| 67 | my ($del_index) = $del_path->get_indices; | ||
| 68 | my ($cur_index) = $self->get_indices; | ||
| 69 | # if deleted before current then decrement | ||
| 70 | if ($del_index < $cur_index) { $self->prev ($model); } | ||
| 71 | if (DEBUG) { print "del to ", $self->to_string, "\n"; } | ||
| 72 | } | ||
| 73 | |||
| 74 | sub rows_reordered { | ||
| 75 | my ($self, $model, $reordered_path, $reordered_iter, $aref) = @_; | ||
| 76 | my ($cur_index) = $self->get_indices; | ||
| 77 | # follow to new index | ||
| 78 | $self->goto_index ($aref->[$cur_index]); | ||
| 79 | if (DEBUG) { print "reorder to ", $self->to_string, "\n"; } | ||
| 80 | } | ||
| 81 | |||
| 82 | 1; | ||
| 83 | __END__ |