| 1 | # Copyright 2008, 2009, 2010, 2011 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::Gtk2::Smarker; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use Glib; | ||
| 23 | use Gtk2; | ||
| 24 | |||
| 25 | use App::Chart::Gtk2::Ex::ListModelPos; | ||
| 26 | |||
| 27 | |||
| 28 | use App::Chart::Gtk2::SymlistListModel; | ||
| 29 | my $lists = App::Chart::Gtk2::SymlistListModel->instance; | ||
| 30 | |||
| 31 | sub new { | ||
| 32 | my ($class, $symbol, $symlist) = @_; | ||
| 33 | my $listpos = App::Chart::Gtk2::Ex::ListModelPos->new (model => $lists); | ||
| 34 | my $self = bless { lists_listpos => $listpos }, $class; | ||
| 35 | $self->goto ($symbol, $symlist); | ||
| 36 | return $self; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub goto { | ||
| 40 | my ($self, $symbol, $symlist) = @_; | ||
| 41 | ### Smarker goto: $symbol | ||
| 42 | ### symlist: defined $symlist && $symlist->key | ||
| 43 | |||
| 44 | if ($symlist) { | ||
| 45 | $self->{'lists_listpos'}->goto ($lists->key_to_pos ($symlist->key)); | ||
| 46 | my $pos = $symlist->find_symbol_pos ($symbol); | ||
| 47 | $self->{'symlist_listpos'} | ||
| 48 | = App::Chart::Gtk2::Ex::ListModelPos->new (model => $symlist, | ||
| 49 | type => 'at', | ||
| 50 | index => $pos); | ||
| 51 | } else { | ||
| 52 | # FIXME: find first symlist containing symbol | ||
| 53 | # | ||
| 54 | $self->{'lists_listpos'}->goto (0, 'before'); | ||
| 55 | $self->{'symlist_listpos'} = undef; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | sub symlist { | ||
| 60 | my ($self) = @_; | ||
| 61 | my $listpos = $self->{'lists_listpos'} || return undef; | ||
| 62 | my $iter = $listpos->iter || return undef; | ||
| 63 | my $key = $lists->get_value ($iter, 0); | ||
| 64 | return App::Chart::Gtk2::Symlist->new_from_key ($key); | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | sub _condition { | ||
| 69 | my ($self, $condition) = @_; | ||
| 70 | if (! defined $condition) { return \&Glib::TRUE; } | ||
| 71 | if (ref($condition)) { return $condition; } | ||
| 72 | return $self->can ("_cond_$condition") | ||
| 73 | || croak "No such condition $condition"; | ||
| 74 | } | ||
| 75 | |||
| 76 | # 'database' condition means symbol must exist in database (as opposed to | ||
| 77 | # just being in a symlist to see a quote in the watchlists) | ||
| 78 | sub _cond_database { | ||
| 79 | my ($self, $symlist, $symbol) = @_; | ||
| 80 | require App::Chart::Database; | ||
| 81 | return App::Chart::Database->symbol_exists ($symbol); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub next { | ||
| 85 | my ($self, $condition) = @_; | ||
| 86 | $condition = _condition ($self, $condition); | ||
| 87 | |||
| 88 | for (;;) { | ||
| 89 | if (my $listpos = $self->{'symlist_listpos'}) { | ||
| 90 | if (my $iter = $listpos->next_iter) { | ||
| 91 | my $symlist = $listpos->model; | ||
| 92 | my $symbol = $symlist->get_value ($iter, 0); | ||
| 93 | $condition->($self, $symlist, $symbol) or next; | ||
| 94 | return ($symbol, $symlist); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | ### prev list ... | ||
| 99 | my $listpos = $self->{'lists_listpos'}; | ||
| 100 | my $iter = $listpos->next_iter || return; | ||
| 101 | |||
| 102 | my $key = $lists->get_value ($iter, $lists->COL_KEY); | ||
| 103 | ### $key | ||
| 104 | my $symlist = App::Chart::Gtk2::Symlist->new_from_key ($key); | ||
| 105 | $self->{'symlist_listpos'} | ||
| 106 | = App::Chart::Gtk2::Ex::ListModelPos->new (model => $symlist, | ||
| 107 | type => 'start'); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | sub prev { | ||
| 112 | my ($self, $condition) = @_; | ||
| 113 | $condition = _condition ($self, $condition); | ||
| 114 | |||
| 115 | for (;;) { | ||
| 116 | if (my $listpos = $self->{'symlist_listpos'}) { | ||
| 117 | if (my $iter = $listpos->prev_iter) { | ||
| 118 | my $symlist = $listpos->model; | ||
| 119 | my $symbol = $symlist->get_value ($iter, 0); | ||
| 120 | $condition->($self, $symlist, $symbol) or next; | ||
| 121 | return ($symbol, $symlist); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | my $listpos = $self->{'lists_listpos'}; | ||
| 126 | my $iter = $listpos->prev_iter || return; | ||
| 127 | |||
| 128 | my $key = $lists->get_value ($iter, $lists->COL_KEY); | ||
| 129 | my $symlist = App::Chart::Gtk2::Symlist->new_from_key ($key); | ||
| 130 | $self->{'symlist_listpos'} | ||
| 131 | = App::Chart::Gtk2::Ex::ListModelPos->new (model => $symlist, | ||
| 132 | type => 'end'); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | 1; | ||
| 137 | __END__ |