| 1 | # Copyright 2008, 2009, 2010, 2011, 2013, 2014 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::IndicatorComboBox; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Module::Load; | ||
| 22 | use Gtk2; | ||
| 23 | |||
| 24 | use App::Chart; | ||
| 25 | |||
| 26 | use Glib::Object::Subclass | ||
| 27 | 'Gtk2::ComboBox', | ||
| 28 | signals => { changed => \&_do_changed }, | ||
| 29 | properties => [Glib::ParamSpec->string | ||
| 30 | ('key', | ||
| 31 | 'key', | ||
| 32 | 'Blurb.', | ||
| 33 | '', | ||
| 34 | Glib::G_PARAM_READWRITE), | ||
| 35 | |||
| 36 | Glib::ParamSpec->string | ||
| 37 | ('type', | ||
| 38 | 'type', | ||
| 39 | 'Blurb.', | ||
| 40 | '', | ||
| 41 | Glib::G_PARAM_READWRITE), | ||
| 42 | ]; | ||
| 43 | |||
| 44 | Gtk2::Rc->parse_string (<<'HERE'); | ||
| 45 | style "App__Chart__Gtk2__GUI_appears_as_list_style" { | ||
| 46 | GtkComboBox::appears-as-list = 1 | ||
| 47 | } | ||
| 48 | class "App__Chart__Gtk2__IndicatorComboBox" style:gtk "App__Chart__Gtk2__GUI_appears_as_list_style" | ||
| 49 | HERE | ||
| 50 | |||
| 51 | sub INIT_INSTANCE { | ||
| 52 | my ($self) = @_; | ||
| 53 | |||
| 54 | require App::Chart::Gtk2::IndicatorModel; | ||
| 55 | my $model = App::Chart::Gtk2::IndicatorModel->instance; | ||
| 56 | $self->set_model ($model); | ||
| 57 | $self->set (active => 0, | ||
| 58 | add_tearoffs => 1); | ||
| 59 | |||
| 60 | my $renderer = Gtk2::CellRendererText->new; | ||
| 61 | $self->pack_start ($renderer, 0); | ||
| 62 | $self->set_attributes ($renderer, text => $model->{'COL_NAME'}); | ||
| 63 | } | ||
| 64 | |||
| 65 | # 'changed' class closure from Gtk2::ComboBox | ||
| 66 | sub _do_changed { | ||
| 67 | my ($self) = @_; | ||
| 68 | $self->signal_chain_from_overridden; | ||
| 69 | $self->notify ('key'); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub GET_PROPERTY { | ||
| 73 | my ($self, $pspec) = @_; | ||
| 74 | my $pname = $pspec->get_name; | ||
| 75 | if ($pspec->get_name eq 'key') { | ||
| 76 | return $self->get_key; | ||
| 77 | } else { | ||
| 78 | return $self->{$pname}; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | sub SET_PROPERTY { | ||
| 82 | my ($self, $pspec, $newval) = @_; | ||
| 83 | my $pname = $pspec->get_name; | ||
| 84 | if ($pname eq 'key') { | ||
| 85 | $self->set_key ($newval); | ||
| 86 | } elsif ($pname eq 'type') { | ||
| 87 | my $type = $self->{'type'} = $newval; | ||
| 88 | my $key = $self->get_key; | ||
| 89 | $self->set_model (App::Chart::Gtk2::IndicatorModel->by_type($type)); | ||
| 90 | $self->set_key ($key); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | sub get_key { | ||
| 94 | my ($self) = @_; | ||
| 95 | my $iter = $self->get_active_iter || return undef; | ||
| 96 | my $model = $self->get_model; | ||
| 97 | $model->get ($iter, $model->{'COL_KEY'}); | ||
| 98 | } | ||
| 99 | sub set_key { | ||
| 100 | my ($self, $key) = @_; | ||
| 101 | if (! defined $key) { | ||
| 102 | $self->set_active (-1); | ||
| 103 | return; | ||
| 104 | } | ||
| 105 | my $found = 0; | ||
| 106 | my $model = $self->get_model; | ||
| 107 | $model->foreach | ||
| 108 | (sub { | ||
| 109 | my ($model, $path, $iter) = @_; | ||
| 110 | if ($key eq ($model->get($iter, $model->{'COL_KEY'})//'')) { | ||
| 111 | $self->set_active_iter ($iter); | ||
| 112 | $found = 1; | ||
| 113 | return 1; # stop looping | ||
| 114 | } | ||
| 115 | return 0; # continue | ||
| 116 | }); | ||
| 117 | if (! $found) { | ||
| 118 | $self->set_active (-1); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | # return a node name string for the manual, eg. "Simple Moving Average" | ||
| 123 | sub get_manual { | ||
| 124 | my ($self) = @_; | ||
| 125 | my $key = $self->get_key || return undef; | ||
| 126 | $key !~ /::/ || return undef; | ||
| 127 | my $mod = "App::Chart::Series::$key"; | ||
| 128 | eval { Module::Load::load ($mod) } || return undef; | ||
| 129 | my $func = $mod->can('manual') || return undef; | ||
| 130 | return $mod->$func; | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; | ||
| 134 | __END__ |