| 1 | # Copyright 2007, 2008, 2009, 2010, 2011, 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::WeblinkMenu; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Glib; | ||
| 22 | use Gtk2; | ||
| 23 | use Locale::TextDomain ('App-Chart'); | ||
| 24 | |||
| 25 | use Glib::Ex::ObjectBits 'set_property_maybe';; | ||
| 26 | use Glib::Ex::ConnectProperties 13; # v.13 for model-rows | ||
| 27 | use Gtk2::Ex::MenuView; | ||
| 28 | |||
| 29 | use App::Chart::Weblink; | ||
| 30 | |||
| 31 | # uncomment this to run the ### lines | ||
| 32 | #use Smart::Comments; | ||
| 33 | |||
| 34 | use Glib::Object::Subclass | ||
| 35 | 'Gtk2::Ex::MenuView', | ||
| 36 | signals => { activate => \&_do_activate, | ||
| 37 | item_create_or_update => \&_do_item_create_or_update, | ||
| 38 | notify => \&_do_notify }, | ||
| 39 | properties => [Glib::ParamSpec->string | ||
| 40 | ('symbol', | ||
| 41 | __('Symbol'), | ||
| 42 | 'The stock or commodity symbol to display weblinks for.', | ||
| 43 | '', # default | ||
| 44 | Glib::G_PARAM_READWRITE)]; | ||
| 45 | |||
| 46 | |||
| 47 | sub INIT_INSTANCE { | ||
| 48 | my ($self) = @_; | ||
| 49 | ### WeblinkMenu INIT_INSTANCE() | ||
| 50 | $self->set (model => Gtk2::ListStore->new ('Glib::Scalar')); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub SET_PROPERTY { | ||
| 54 | my ($self, $pspec, $newval) = @_; | ||
| 55 | my $pname = $pspec->get_name; | ||
| 56 | ### WeblinkMenu SET_PROPERTY(): $pname | ||
| 57 | $self->{$pname} = $newval; # per default GET_PROPERTY | ||
| 58 | |||
| 59 | # $pname eq 'symbol' | ||
| 60 | _update_model_contents ($self); | ||
| 61 | _update_parent_sensitive ($self); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _do_notify { | ||
| 65 | my ($self, $pspec) = @_; | ||
| 66 | ### WeblinkMenu _do_notify: $pspec->get_name | ||
| 67 | if ($pspec->get_name eq 'attach_widget') { | ||
| 68 | my $parent = $self->get_attach_widget; | ||
| 69 | ### attached: "$parent" | ||
| 70 | ### model: $self->get('model') | ||
| 71 | $self->{'connp'} = Glib::Ex::ConnectProperties->dynamic | ||
| 72 | ([$self->get('model'), 'model-rows#not-empty'], | ||
| 73 | [$parent, 'sensitive']); | ||
| 74 | } | ||
| 75 | # return shift->signal_chain_from_overridden(@_); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _update_model_contents { | ||
| 79 | my ($self) = @_; | ||
| 80 | ### WeblinkMenu _update_model_contents() | ||
| 81 | my $model = $self->get('model'); | ||
| 82 | my $symbol = $self->get('symbol'); | ||
| 83 | my $i = 0; | ||
| 84 | if (defined $symbol) { | ||
| 85 | foreach my $weblink (App::Chart::Weblink->links_for_symbol ($symbol)) { | ||
| 86 | my $iter = $model->iter_nth_child(undef,$i++) || $model->append; | ||
| 87 | $model->set ($iter, 0 => $weblink); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | while (my $iter = $model->iter_nth_child(undef,$i)) { | ||
| 91 | $model->remove ($iter); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | sub _update_parent_sensitive { | ||
| 95 | my ($self) = @_; | ||
| 96 | return; | ||
| 97 | |||
| 98 | ### WeblinkMenu _update_parent_sensitive: $self->get_attach_widget | ||
| 99 | my $parent_item = $self->get_attach_widget || return; | ||
| 100 | my $model = $self->get('model'); | ||
| 101 | $parent_item->set_sensitive ($model->get_iter_first); | ||
| 102 | } | ||
| 103 | |||
| 104 | # 'item-create-or-update' class closure handler | ||
| 105 | sub _do_item_create_or_update { | ||
| 106 | my ($self, $item, $model, $path, $iter) = @_; | ||
| 107 | my $weblink = $model->get_value ($iter, 0); | ||
| 108 | if (! $item) { | ||
| 109 | $item = Gtk2::MenuItem->new_with_label (''); | ||
| 110 | $item->show; | ||
| 111 | } | ||
| 112 | $item->get_child->set_text_with_mnemonic ($weblink->name); | ||
| 113 | set_property_maybe | ||
| 114 | ($item, | ||
| 115 | sensitive => $weblink->sensitive($self->get('symbol')), | ||
| 116 | # tooltip-markup new in 2.12 | ||
| 117 | tooltip_markup => Glib::Markup::escape_text($weblink->{'desc'} || '')); | ||
| 118 | return $item; | ||
| 119 | } | ||
| 120 | |||
| 121 | # 'activate' class closure | ||
| 122 | sub _do_activate { | ||
| 123 | my ($self, $item, $model, $path, $iter) = @_; | ||
| 124 | my $weblink = $model->get_value ($iter, 0); | ||
| 125 | my $symbol = $self->get('symbol'); | ||
| 126 | $weblink->open ($symbol); | ||
| 127 | } | ||
| 128 | |||
| 129 | 1; | ||
| 130 | __END__ | ||
| 131 | |||
| 132 | =for stopwords weblinks weblink WeblinkMenu | ||
| 133 | |||
| 134 | =head1 NAME | ||
| 135 | |||
| 136 | App::Chart::Gtk2::WeblinkMenu -- menu of weblinks for stock symbol | ||
| 137 | |||
| 138 | =head1 SYNOPSIS | ||
| 139 | |||
| 140 | use App::Chart::Gtk2::WeblinkMenu; | ||
| 141 | my $menu = App::Chart::Gtk2::WeblinkMenu->new; | ||
| 142 | |||
| 143 | =head1 DESCRIPTION | ||
| 144 | |||
| 145 | A App::Chart::Gtk2::WeblinkMenu widget displays a menu of the weblinks (see | ||
| 146 | L<App::Chart::Weblink>) for a given stock symbol. Clicking on one of the | ||
| 147 | menu items opens the corresponding weblink in a browser as per | ||
| 148 | C<< $weblink->open >>. | ||
| 149 | |||
| 150 | +---------------------------+ | ||
| 151 | | FOOEX Company Information | | ||
| 152 | | Google Stock Page | | ||
| 153 | | Yahoo Stock Page | | ||
| 154 | +---------------------------+ | ||
| 155 | |||
| 156 | Any parent menu item holding the WeblinkMenu is set sensitive or insensitive | ||
| 157 | according to whether there's weblinks for the given symbol, including | ||
| 158 | insensitive when the symbol is the empty string. | ||
| 159 | |||
| 160 | =head1 WIDGET HIERARCHY | ||
| 161 | |||
| 162 | C<App::Chart::Gtk2::WeblinkMenu> is a subclass of C<Gtk2::Menu>, | ||
| 163 | |||
| 164 | Gtk2::Widget | ||
| 165 | Gtk2::Container | ||
| 166 | Gtk2::MenuShell | ||
| 167 | Gtk2::Menu | ||
| 168 | App::Chart::Gtk2::WeblinkMenu | ||
| 169 | |||
| 170 | =head1 FUNCTIONS | ||
| 171 | |||
| 172 | =over 4 | ||
| 173 | |||
| 174 | =item C<< App::Chart::Gtk2::WeblinkMenu->new (key=>value,...) >> | ||
| 175 | |||
| 176 | Create and return a new C<App::Chart::Gtk2::WeblinkMenu> object. Optional | ||
| 177 | key/value pairs can be given to set initial properties as per | ||
| 178 | C<< Glib::Object->new >>. | ||
| 179 | |||
| 180 | =back | ||
| 181 | |||
| 182 | =head1 PROPERTIES | ||
| 183 | |||
| 184 | =over 4 | ||
| 185 | |||
| 186 | =item C<symbol> (string, default empty "") | ||
| 187 | |||
| 188 | The stock or commodity symbol (a string) to display links for. This can be | ||
| 189 | an empty string "" for no symbol (and hence no links showing). | ||
| 190 | |||
| 191 | =back | ||
| 192 | |||
| 193 | =head1 SEE ALSO | ||
| 194 | |||
| 195 | L<App::Chart::Weblink>, L<Gtk2::Menu>, L<Gtk2::MenuItem> | ||
| 196 | |||
| 197 | =cut |