| 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__ |