| 1 | # Copyright 2007, 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::SymlistRadioMenu; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use Gtk2; | ||
| 23 | |||
| 24 | use App::Chart::Gtk2::Symlist; | ||
| 25 | use App::Chart::Gtk2::SymlistListModel (qw(COL_KEY COL_NAME)); | ||
| 26 | |||
| 27 | # uncomment this to run the ### lines | ||
| 28 | #use Smart::Comments; | ||
| 29 | |||
| 30 | use Gtk2::Ex::MenuView; | ||
| 31 | use Glib::Object::Subclass | ||
| 32 | 'Gtk2::Ex::MenuView', | ||
| 33 | signals => { activate => \&_do_activate, | ||
| 34 | item_create_or_update => \&_do_item_create_or_update, | ||
| 35 | }, | ||
| 36 | properties => [ Glib::ParamSpec->object | ||
| 37 | ('symlist', | ||
| 38 | 'symlist', | ||
| 39 | 'App::Chart::Gtk2::Symlist object selected, or undef.', | ||
| 40 | 'App::Chart::Gtk2::Symlist', | ||
| 41 | Glib::G_PARAM_READWRITE), | ||
| 42 | ]; | ||
| 43 | |||
| 44 | sub INIT_INSTANCE { | ||
| 45 | my ($self) = @_; | ||
| 46 | |||
| 47 | my $model = App::Chart::Gtk2::SymlistListModel->instance; | ||
| 48 | |||
| 49 | # hack for extra constructed lists ... | ||
| 50 | # | ||
| 51 | my @symlists = grep {$_->isa('App::Chart::Gtk2::Symlist::Join')} | ||
| 52 | values %App::Chart::Gtk2::Symlist::instances; | ||
| 53 | if (@symlists) { | ||
| 54 | @symlists = sort {$a->key cmp $b->key} @symlists; | ||
| 55 | require Gtk2::Ex::TreeModelBits; | ||
| 56 | my $store = Gtk2::ListStore->new | ||
| 57 | (Gtk2::Ex::TreeModelBits::all_column_types ($model)); | ||
| 58 | my $pos = 0; | ||
| 59 | foreach my $symlist (@symlists) { | ||
| 60 | ### extra: COL_KEY.' '.COL_NAME.' '.$symlist->key | ||
| 61 | $store->insert_with_values ($pos++, | ||
| 62 | COL_KEY, $symlist->key, | ||
| 63 | COL_NAME, $symlist->name); | ||
| 64 | } | ||
| 65 | require Gtk2::Ex::ListModelConcat; | ||
| 66 | $model = Gtk2::Ex::ListModelConcat->new (models => [ $store, $model ]); | ||
| 67 | } | ||
| 68 | |||
| 69 | $self->set (model => $model); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub SET_PROPERTY { | ||
| 73 | my ($self, $pspec, $newval) = @_; | ||
| 74 | my $pname = $pspec->get_name; | ||
| 75 | if ($pspec->get_name eq 'symlist') { | ||
| 76 | $self->set_symlist ($newval); | ||
| 77 | } else { | ||
| 78 | $self->{$pname} = $newval; # per default GET_PROPERTY | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | sub _do_item_create_or_update { | ||
| 83 | my ($self, $item, $model, $path, $iter) = @_; | ||
| 84 | ### SymlistRadioMenu _do_item_create_or_update: $item | ||
| 85 | if (! $item) { | ||
| 86 | my $group = ($self->get_children)[0]; | ||
| 87 | $item = Gtk2::RadioMenuItem->new_with_label($group,''); | ||
| 88 | } | ||
| 89 | ### label: $item->get_child | ||
| 90 | $item->get_child->set_text ($model->get_value ($iter, COL_NAME)); | ||
| 91 | return $item; | ||
| 92 | } | ||
| 93 | |||
| 94 | # 'activate' signal handler on items | ||
| 95 | sub _do_activate { | ||
| 96 | my ($self, $item, $model, $path, $iter) = @_; | ||
| 97 | if ($item->get_active) { | ||
| 98 | my $key = $model->get_value ($iter, COL_KEY); | ||
| 99 | $self->{'symlist'} = App::Chart::Gtk2::Symlist->new_from_key ($key); | ||
| 100 | $self->notify ('symlist'); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | sub set_symlist { | ||
| 105 | my ($self, $symlist) = @_; | ||
| 106 | ### SymlistRadioMenu set_symlist: $self->get_name." to ".($symlist||'[none]') | ||
| 107 | if (($self->{'symlist'}||0) == ($symlist||0)) { return; } | ||
| 108 | |||
| 109 | my $key = $symlist->key; | ||
| 110 | my $model = $self->get('model'); | ||
| 111 | my $pos = liststore_find_pos | ||
| 112 | ($model, sub { my ($model, $path, $iter) = @_; | ||
| 113 | return ($model->get_value($iter, COL_KEY) eq $key); }); | ||
| 114 | if (! defined $pos) { | ||
| 115 | croak "SymlistRadioMenu: no such list for set_symlist: $key"; | ||
| 116 | } | ||
| 117 | |||
| 118 | $self->{'symlist'} = undef; | ||
| 119 | require Glib::Ex::FreezeNotify; | ||
| 120 | { my $freezer = Glib::Ex::FreezeNotify->new ($self); | ||
| 121 | if (my $item = $self->item_at_indices ($pos)) { | ||
| 122 | $item->set_active (1); | ||
| 123 | } | ||
| 124 | $self->notify ('symlist'); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | sub liststore_find_pos { | ||
| 129 | my ($store, $subr) = @_; | ||
| 130 | my $pos; | ||
| 131 | $store->foreach (sub { | ||
| 132 | my ($store, $path, $iter) = @_; | ||
| 133 | if ($subr->($store, $path, $iter)) { | ||
| 134 | ($pos) = $path->get_indices; | ||
| 135 | return 1; # stop; | ||
| 136 | } | ||
| 137 | return 0; # continue; | ||
| 138 | }); | ||
| 139 | return $pos; | ||
| 140 | } | ||
| 141 | |||
| 142 | 1; | ||
| 143 | __END__ | ||
| 144 | |||
| 145 | =for stopwords symlists undef symlist programmatically | ||
| 146 | |||
| 147 | =head1 NAME | ||
| 148 | |||
| 149 | App::Chart::Gtk2::SymlistRadioMenu -- radio menu of symbol lists | ||
| 150 | |||
| 151 | =head1 SYNOPSIS | ||
| 152 | |||
| 153 | use App::Chart::Gtk2::SymlistRadioMenu; | ||
| 154 | my $menu = App::Chart::Gtk2::SymlistRadioMenu->new; | ||
| 155 | |||
| 156 | =head1 WIDGET HIERARCHY | ||
| 157 | |||
| 158 | C<App::Chart::Gtk2::SymlistRadioMenu> is a subclass of C<Gtk2::Ex::MenuView>, | ||
| 159 | |||
| 160 | Gtk2::Widget | ||
| 161 | Gtk2::Container | ||
| 162 | Gtk2::MenuShell | ||
| 163 | Gtk2::Menu | ||
| 164 | Gtk2::Ex::MenuView | ||
| 165 | App::Chart::Gtk2::SymlistRadioMenu | ||
| 166 | |||
| 167 | =head1 DESCRIPTION | ||
| 168 | |||
| 169 | A C<App::Chart::Gtk2::SymlistRadioMenu> displays a menu of the available symlists | ||
| 170 | with C<Gtk2::RadioMenuItem>s, allowing the user to select one of them. The | ||
| 171 | currently selected list is in the C<symlist> property. | ||
| 172 | |||
| 173 | +--------------+ | ||
| 174 | | * Alerts | | ||
| 175 | +--------------+ | ||
| 176 | | Favourites | | ||
| 177 | +--------------+ | ||
| 178 | | All | | ||
| 179 | +--------------+ | ||
| 180 | | Historical | | ||
| 181 | +--------------+ | ||
| 182 | |||
| 183 | =head1 FUNCTIONS | ||
| 184 | |||
| 185 | =over 4 | ||
| 186 | |||
| 187 | =item C<< App::Chart::Gtk2::SymlistRadioMenu->new (key=>value,...) >> | ||
| 188 | |||
| 189 | Create and return a new C<App::Chart::Gtk2::SymlistRadioMenu> object. Optional | ||
| 190 | key/value pairs set initial properties as per C<< Glib::Object->new >>. | ||
| 191 | |||
| 192 | =back | ||
| 193 | |||
| 194 | =head1 PROPERTIES | ||
| 195 | |||
| 196 | =over 4 | ||
| 197 | |||
| 198 | =item C<symlist> (C<App::Chart::Gtk2::Symlist>, default undef) | ||
| 199 | |||
| 200 | The currently selected symlist in the menu radio buttons, or undef for none | ||
| 201 | selected. This changes when the user selects a new list, and it can also be | ||
| 202 | set programmatically. | ||
| 203 | |||
| 204 | =back | ||
| 205 | |||
| 206 | =head1 SEE ALSO | ||
| 207 | |||
| 208 | L<App::Chart::Gtk2::Symlist>, L<Gtk2::Menu>, L<Gtk2::RadioMenuItem> | ||
| 209 | |||
| 210 | =cut |