| 1 | # Copyright 2008, 2009, 2010, 2011, 2012, 2013 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::LineStyleComboBox; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Gtk2; | ||
| 22 | use Locale::TextDomain ('App-Chart'); | ||
| 23 | |||
| 24 | use Glib::Object::Subclass | ||
| 25 | 'Gtk2::ComboBox', | ||
| 26 | signals => { changed => \&_do_changed }, | ||
| 27 | properties => [Glib::ParamSpec->string | ||
| 28 | ('linestyle', | ||
| 29 | 'linestyle', | ||
| 30 | 'Blurb.', | ||
| 31 | '', | ||
| 32 | Glib::G_PARAM_READWRITE), | ||
| 33 | ]; | ||
| 34 | |||
| 35 | use constant { COL_KEY => 0, | ||
| 36 | COL_NAME => 1 }; | ||
| 37 | |||
| 38 | use constant::defer _model => sub { | ||
| 39 | ### LineStyleComboBox _model() | ||
| 40 | my $model = Gtk2::ListStore->new ('Glib::String', 'Glib::String'); | ||
| 41 | |||
| 42 | foreach my $elem ([ '', __('Default LineStyle') ], | ||
| 43 | [ 'Candles', __('Candles') ], | ||
| 44 | [ 'OHLC', __('OHLC') ], | ||
| 45 | [ 'Points', __('Points') ], | ||
| 46 | [ 'Line', __('Line') ], | ||
| 47 | [ 'HighLow', __('High/Low') ], | ||
| 48 | [ 'Bars', __('Bars') ], | ||
| 49 | [ 'Stops', __('Stops') ], | ||
| 50 | [ 'None', __('None') ], | ||
| 51 | ) { | ||
| 52 | my ($package, $name) = @$elem; | ||
| 53 | $model->set ($model->append, COL_KEY, $package, COL_NAME, $name); | ||
| 54 | } | ||
| 55 | |||
| 56 | # plus anything extra | ||
| 57 | require Module::Find; | ||
| 58 | require Gtk2::Ex::TreeModelBits; | ||
| 59 | my %extra; | ||
| 60 | # hash slice, everything on disk | ||
| 61 | @extra{map {s/App::Chart::Gtk2::LineStyle:://;$_} | ||
| 62 | Module::Find::findsubmod('App::Chart::Gtk2::LineStyle')} = (); | ||
| 63 | ### extra on disk: \%extra | ||
| 64 | # hash slice, less already known | ||
| 65 | delete @extra{Gtk2::Ex::TreeModelBits::column_contents($model,COL_KEY)}; | ||
| 66 | |||
| 67 | ### extra: sort keys %extra | ||
| 68 | foreach my $linestyle (sort keys %extra) { | ||
| 69 | $model->set ($model->append, COL_KEY, $linestyle, COL_NAME, $linestyle); | ||
| 70 | } | ||
| 71 | |||
| 72 | return $model; | ||
| 73 | }; | ||
| 74 | |||
| 75 | sub INIT_INSTANCE { | ||
| 76 | my ($self) = @_; | ||
| 77 | $self->set_model (_model()); | ||
| 78 | my $renderer = Gtk2::CellRendererText->new; | ||
| 79 | $self->pack_start ($renderer, 1); | ||
| 80 | $self->set_attributes ($renderer, text => COL_NAME); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _do_changed { | ||
| 84 | my ($self) = @_; | ||
| 85 | $self->signal_chain_from_overridden; | ||
| 86 | $self->notify ('linestyle'); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub GET_PROPERTY { | ||
| 90 | my ($self, $pspec) = @_; | ||
| 91 | if ($pspec->get_name eq 'linestyle') { | ||
| 92 | my $iter = $self->get_active_iter; | ||
| 93 | return $self->get_model->get ($iter, COL_KEY); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | sub SET_PROPERTY { | ||
| 97 | my ($self, $pspec, $newval) = @_; | ||
| 98 | if ($pspec->get_name eq 'linestyle') { | ||
| 99 | my $linestyle = $newval; | ||
| 100 | if (! defined $linestyle) { | ||
| 101 | $self->set_active (-1); | ||
| 102 | } else { | ||
| 103 | my $found; | ||
| 104 | $self->get_model->foreach | ||
| 105 | (sub { | ||
| 106 | my ($model, $path, $iter) = @_; | ||
| 107 | if ($model->get($iter, COL_KEY) eq $linestyle) { | ||
| 108 | $self->set_active_iter ($iter); | ||
| 109 | $found = 1; | ||
| 110 | return 1; # stop looping | ||
| 111 | } | ||
| 112 | return 0; # continue | ||
| 113 | }); | ||
| 114 | if (! $found) { | ||
| 115 | Glib->warning | ||
| 116 | (undef, "LineStyleComboBox: no such line style \"$linestyle\""); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | 1; | ||
| 123 | __END__ |