| 1 | # Copyright 2008, 2009, 2010, 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::SubprocessStopMenu; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use Glib::Ex::SignalIds; | ||
| 23 | use Gtk2; | ||
| 24 | use Scalar::Util; | ||
| 25 | # use Locale::TextDomain ('App-Chart'); | ||
| 26 | |||
| 27 | use App::Chart::Glib::Ex::MoreUtils; | ||
| 28 | use App::Chart; | ||
| 29 | use App::Chart::Gtk2::Subprocess; | ||
| 30 | |||
| 31 | # uncomment this to run the ### lines | ||
| 32 | #use Smart::Comments; | ||
| 33 | |||
| 34 | use Glib::Object::Subclass | ||
| 35 | 'Gtk2::Menu', | ||
| 36 | properties => [ Glib::ParamSpec->object | ||
| 37 | ('subprocess', | ||
| 38 | 'subprocess', | ||
| 39 | 'App::Chart::Gtk2::Subprocess object to act on, or undef.', | ||
| 40 | 'App::Chart::Gtk2::Subprocess', | ||
| 41 | Glib::G_PARAM_READWRITE), | ||
| 42 | ]; | ||
| 43 | |||
| 44 | sub INIT_INSTANCE { | ||
| 45 | my ($self) = @_; | ||
| 46 | |||
| 47 | # avoid selecting Stop too easily | ||
| 48 | $self->append (Gtk2::SeparatorMenuItem->new); | ||
| 49 | |||
| 50 | { | ||
| 51 | my $item = $self->{'stop'} | ||
| 52 | = Gtk2::ImageMenuItem->new_from_stock ('gtk-stop'); | ||
| 53 | $self->append ($item); | ||
| 54 | $item->signal_connect (activate => \&_do_stop); | ||
| 55 | $item->show; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | sub SET_PROPERTY { | ||
| 60 | my ($self, $pspec, $newval) = @_; | ||
| 61 | my $pname = $pspec->get_name; | ||
| 62 | $self->{$pname} = $newval; # per default GET_PROPERTY | ||
| 63 | |||
| 64 | if ($pname eq 'subprocess') { | ||
| 65 | my $subprocess = $newval; | ||
| 66 | $self->{'subprocess_ids'} = $subprocess && Glib::Ex::SignalIds->new | ||
| 67 | ($subprocess, | ||
| 68 | $subprocess->signal_connect ('notify::status', | ||
| 69 | \&_do_subprocess_status_change, | ||
| 70 | App::Chart::Glib::Ex::MoreUtils::ref_weak($self))); | ||
| 71 | _update_sensitive ($self); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | # 'activate' from Stop item | ||
| 76 | sub _do_stop { | ||
| 77 | my ($item) = @_; | ||
| 78 | my $self = $item->get_ancestor(__PACKAGE__) || return; | ||
| 79 | ### SubprocessStopMenu stop: "$self" | ||
| 80 | ### subprocess: "@{[$self->{'subprocess'}//'undef']}" | ||
| 81 | if (my $subprocess = $self->{'subprocess'}) { | ||
| 82 | if (my $job = $subprocess->get('job')) { | ||
| 83 | $job->stop; | ||
| 84 | } else { | ||
| 85 | $subprocess->stop; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | # 'notify::status' from $self->{'subprocess'}, if any | ||
| 91 | sub _do_subprocess_status_change { | ||
| 92 | my ($subprocess, $pspec, $ref_weak_self) = @_; | ||
| 93 | my $self = $$ref_weak_self || return; | ||
| 94 | _update_sensitive ($self); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _update_sensitive { | ||
| 98 | my ($self) = @_; | ||
| 99 | my $subprocess = $self->{'subprocess'}; | ||
| 100 | |||
| 101 | my $stop = $self->{'stop'}; | ||
| 102 | $stop->set_sensitive ($subprocess && defined ($subprocess->pid)); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub popup_from_treeview { | ||
| 106 | my ($self, $event, $treeview) = @_; | ||
| 107 | ref $self or $self = $self->new; # object or class method | ||
| 108 | |||
| 109 | my ($path) = $treeview->get_path_at_pos ($event->x, $event->y); | ||
| 110 | if (! $path) { return; } | ||
| 111 | |||
| 112 | my $model = $treeview->get_model; # $App::Chart::Gtk2::Subprocess::store | ||
| 113 | my $iter = $model->get_iter ($path); | ||
| 114 | my $subprocess = $model->get_value ($iter, 0); | ||
| 115 | $self->set (subprocess => $subprocess); | ||
| 116 | $self->set_screen ($treeview->get_screen); | ||
| 117 | $self->popup (undef, undef, undef, undef, $event->button, $event->time); | ||
| 118 | } | ||
| 119 | |||
| 120 | 1; | ||
| 121 | __END__ | ||
| 122 | |||
| 123 | =for stopwords Subprocess undef subprocess | ||
| 124 | |||
| 125 | =head1 NAME | ||
| 126 | |||
| 127 | App::Chart::Gtk2::SubprocessStopMenu -- menu to stop or delete a Subprocess | ||
| 128 | |||
| 129 | =for test_synopsis my ($event, $treeview) | ||
| 130 | |||
| 131 | =head1 SYNOPSIS | ||
| 132 | |||
| 133 | use App::Chart::Gtk2::SubprocessStopMenu; | ||
| 134 | my $menu = App::Chart::Gtk2::SubprocessStopMenu->new; | ||
| 135 | $menu->popup_from_treeview ($event, $treeview); | ||
| 136 | |||
| 137 | =head1 WIDGET HIERARCHY | ||
| 138 | |||
| 139 | C<App::Chart::Gtk2::SubprocessStopMenu> is a subclass of C<Gtk::Menu>, | ||
| 140 | |||
| 141 | Gtk2::Widget | ||
| 142 | Gtk2::Container | ||
| 143 | Gtk2::MenuShell | ||
| 144 | Gtk2::Menu | ||
| 145 | App::Chart::Gtk2::SubprocessStopMenu | ||
| 146 | |||
| 147 | =head1 DESCRIPTION | ||
| 148 | |||
| 149 | C<App::Chart::Gtk2::SubprocessStopMenu> displays a little menu to stop a given | ||
| 150 | C<App::Chart::Gtk2::Subprocess>. | ||
| 151 | |||
| 152 | +--------+ | ||
| 153 | +--------+ | ||
| 154 | | Stop | | ||
| 155 | +--------+ | ||
| 156 | |||
| 157 | This is used by C<App::Chart::Gtk2::DownloadDialog> and has been split out | ||
| 158 | to reduce the amount of code there. | ||
| 159 | |||
| 160 | =head1 FUNCTIONS | ||
| 161 | |||
| 162 | =over 4 | ||
| 163 | |||
| 164 | =item C<< App::Chart::Gtk2::SubprocessStopMenu->new (key=>value,...) >> | ||
| 165 | |||
| 166 | Create and return a new C<App::Chart::Gtk2::SubprocessStopMenu> object. Optional | ||
| 167 | key/value pairs set initial properties as per C<< Glib::Object->new >>. | ||
| 168 | |||
| 169 | =back | ||
| 170 | |||
| 171 | =head1 PROPERTIES | ||
| 172 | |||
| 173 | =over 4 | ||
| 174 | |||
| 175 | =item C<subprocess> (C<App::Chart::Gtk2::Subprocess> object, default undef) | ||
| 176 | |||
| 177 | The subprocess to act on in the menu. Normally this is set at the time the | ||
| 178 | menu is popped up. Changing it while popped up works, but might confuse the | ||
| 179 | user. | ||
| 180 | |||
| 181 | =back | ||
| 182 | |||
| 183 | =head1 SEE ALSO | ||
| 184 | |||
| 185 | L<App::Chart::Gtk2::Subprocess>, L<Gtk2::Menu>, L<App::Chart::Gtk2::DownloadDialog> | ||
| 186 | |||
| 187 | =cut |