| 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__ | ||
| 84 | |||
| 85 | =head1 NAME | ||
| 86 | |||
| 87 | App::Chart::Gtk2::Ex::TreePath::Circular -- managed path position with wraparound | ||
| 88 | |||
| 89 | =for test_synopsis my ($model) | ||
| 90 | |||
| 91 | =head1 SYNOPSIS | ||
| 92 | |||
| 93 | use App::Chart::Gtk2::Ex::TreePath::Circular; | ||
| 94 | my $path = My::TreePath::Circular->new; | ||
| 95 | |||
| 96 | $path->next ($model); | ||
| 97 | $path->prev ($model); | ||
| 98 | |||
| 99 | =head1 CLASS HIERARCHY | ||
| 100 | |||
| 101 | C<App::Chart::Gtk2::Ex::TreePath::Circular> is a Perl subclass of C<Gtk2::TreePath>, | ||
| 102 | |||
| 103 | Glib::Boxed | ||
| 104 | Gtk2::TreePath | ||
| 105 | App::Chart::Gtk2::Ex::TreePath::Circular | ||
| 106 | |||
| 107 | =head1 DESCRIPTION | ||
| 108 | |||
| 109 | C<App::Chart::Gtk2::Ex::TreePath::Circular> is a version of C<Gtk2::TreePath> designed | ||
| 110 | to maintain a position in the model's row data and to wrap round from the | ||
| 111 | end back to the beginning of the rows when necessary. It's used for | ||
| 112 | instance by C<Gtk2::Ex::TickerView> to maintain the current item position in | ||
| 113 | that display. | ||
| 114 | |||
| 115 | =head1 FUNCTIONS | ||
| 116 | |||
| 117 | =over 4 | ||
| 118 | |||
| 119 | =item C<< App::Chart::Gtk2::Ex::TreePath::Circular->new >> | ||
| 120 | |||
| 121 | Create and return a new path object. | ||
| 122 | |||
| 123 | =item C<< App::Chart::Gtk2::Ex::TreePath::Circular->new_first >> | ||
| 124 | |||
| 125 | Create and return a new path object positioned at index 0, ie. the start of | ||
| 126 | any model's data. | ||
| 127 | |||
| 128 | =item C<< App::Chart::Gtk2::Ex::TreePath::Circular->new_from_indices ($index) >> | ||
| 129 | |||
| 130 | Create and return a new path object positioned at the given index. Index 0 | ||
| 131 | is the first in any model. | ||
| 132 | |||
| 133 | =item C<< App::Chart::Gtk2::Ex::TreePath::Circular->new_from_string ($str) >> | ||
| 134 | |||
| 135 | Create and return a new path object positioned at an index given by a string. | ||
| 136 | |||
| 137 | =item C<< $circpath->next ($model) >> | ||
| 138 | |||
| 139 | =item C<< $circpath->prev ($model) >> | ||
| 140 | |||
| 141 | Step C<$circpath> to the next or previous row in the given C<$model>. At | ||
| 142 | the end of the rows C<next> wraps around to the start again, and conversely | ||
| 143 | at the start C<prev> wraps around to the end. | ||
| 144 | |||
| 145 | =item C<< $circpath->row_inserted ($model, $path, $iter) >> | ||
| 146 | |||
| 147 | =item C<< $circpath->row_deleted ($model, $path) >> | ||
| 148 | |||
| 149 | =item C<< $circpath->rows_reordered ($model, $path, $iter, $aref) >> | ||
| 150 | |||
| 151 | Adjust C<$circpath> to maintain it's current position in the presence of the | ||
| 152 | given changes to C<$model>. These functions can be called from the | ||
| 153 | respective model signal, the parameters are the same as to those signals. | ||
| 154 | |||
| 155 | =back | ||
| 156 | |||
| 157 | =head1 SEE ALSO | ||
| 158 | |||
| 159 | L<Gtk2::TreePath> | ||
| 160 | |||
| 161 | =cut |