| 1 | # Copyright 2007, 2008, 2009, 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::Symlist::Alphabetical; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | |||
| 22 | use App::Chart::Gtk2::Symlist; | ||
| 23 | use Glib::Object::Subclass | ||
| 24 | 'App::Chart::Gtk2::Symlist'; | ||
| 25 | |||
| 26 | use constant DEBUG => 0; | ||
| 27 | |||
| 28 | # sub can_edit { return 0; } | ||
| 29 | sub can_destroy { return 0; } | ||
| 30 | |||
| 31 | # override to always be alphabetical, no matter what $pos requested | ||
| 32 | sub insert_symbol_at_pos { | ||
| 33 | my ($self, $symbol, $pos, $note) = @_; | ||
| 34 | insert_symbol ($self, $symbol, $note); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub insert_symbol { | ||
| 38 | my ($self, $symbol, $note) = @_; | ||
| 39 | if (DEBUG) { print "Alphabetical ",$self->{'key'}, | ||
| 40 | " insert_symbol $symbol\n"; } | ||
| 41 | |||
| 42 | my $hash = $self->hash; | ||
| 43 | if (exists $hash->{$symbol}) { | ||
| 44 | if (DEBUG >= 2) { print " already in list\n"; } | ||
| 45 | return; | ||
| 46 | } | ||
| 47 | |||
| 48 | my $found = 0; | ||
| 49 | my $pos; | ||
| 50 | $self->foreach (sub { | ||
| 51 | my ($self, $path, $iter) = @_; | ||
| 52 | my $this_symbol = $self->get_value($iter,0); | ||
| 53 | if (DEBUG) { print " looking at ",$path->to_string, | ||
| 54 | " $this_symbol\n";} | ||
| 55 | if ($this_symbol eq $symbol) { | ||
| 56 | # already in the list, do nothing | ||
| 57 | if (DEBUG) { print " already in list\n"; } | ||
| 58 | $found = 1; | ||
| 59 | return 1; # stop iterating | ||
| 60 | } | ||
| 61 | |||
| 62 | if (App::Chart::symbol_cmp ($this_symbol, $symbol) > 0) { | ||
| 63 | if (DEBUG) { print " stop '$this_symbol' gt new '$symbol'\n";} | ||
| 64 | ($pos) = $path->get_indices; | ||
| 65 | return 1; # stop iterating | ||
| 66 | } | ||
| 67 | |||
| 68 | return 0; # keep iterating | ||
| 69 | }); | ||
| 70 | if ($found) { return; } | ||
| 71 | $pos //= $self->length; | ||
| 72 | |||
| 73 | if (DEBUG) { print " insert at $pos\n"; } | ||
| 74 | $self->insert_with_values ($pos, 0=>$symbol, 1=>$note); | ||
| 75 | } | ||
| 76 | |||
| 77 | 1; | ||
| 78 | __END__ |