| 1 | # Copyright 2010, 2011 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Gtk2-Ex-History. | ||
| 4 | # | ||
| 5 | # Gtk2-Ex-History is free software; you can redistribute it and/or modify it | ||
| 6 | # under the terms of the GNU General Public License as published by the Free | ||
| 7 | # Software Foundation; either version 3, or (at your option) any later | ||
| 8 | # version. | ||
| 9 | # | ||
| 10 | # Gtk2-Ex-History is distributed in the hope that it will be useful, but | ||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 13 | # for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with Gtk2-Ex-History. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | package Gtk2::Ex::History::Action; | ||
| 19 | use 5.008; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use Scalar::Util; | ||
| 23 | |||
| 24 | use Gtk2 1.220; # for Gtk2::EVENT_PROPAGATE | ||
| 25 | use Gtk2::Ex::History; | ||
| 26 | use Glib::Ex::ConnectProperties 13; # v.13 for model-rows | ||
| 27 | |||
| 28 | use Locale::TextDomain ('Gtk2-Ex-History'); | ||
| 29 | use Locale::Messages; | ||
| 30 | BEGIN { | ||
| 31 | Locale::Messages::bind_textdomain_codeset ('Gtk2-Ex-History','UTF-8'); | ||
| 32 | Locale::Messages::bind_textdomain_filter ('Gtk2-Ex-History', | ||
| 33 | \&Locale::Messages::turn_utf_8_on); | ||
| 34 | } | ||
| 35 | |||
| 36 | our $VERSION = 8; | ||
| 37 | |||
| 38 | # uncomment this to run the ### lines | ||
| 39 | #use Smart::Comments; | ||
| 40 | |||
| 41 | use Glib::Object::Subclass | ||
| 42 | 'Gtk2::Action', | ||
| 43 | signals => { activate => \&_do_activate }, | ||
| 44 | properties => [ Glib::ParamSpec->object | ||
| 45 | ('history', | ||
| 46 | __('History object'), | ||
| 47 | 'The history object to act on.', | ||
| 48 | 'Gtk2::Ex::History', | ||
| 49 | Glib::G_PARAM_READWRITE), | ||
| 50 | |||
| 51 | Glib::ParamSpec->enum | ||
| 52 | ('way', | ||
| 53 | 'Which way', | ||
| 54 | 'Which way to go in the history when activated, either back or forward.', | ||
| 55 | 'Gtk2::Ex::History::Way', | ||
| 56 | 'back', | ||
| 57 | Glib::G_PARAM_READWRITE), | ||
| 58 | ]; | ||
| 59 | |||
| 60 | my $connect_hook_id; | ||
| 61 | my $disconnect_hook_id; | ||
| 62 | |||
| 63 | sub INIT_INSTANCE { | ||
| 64 | my ($self) = @_; | ||
| 65 | ### History-Action INIT_INSTANCE() | ||
| 66 | |||
| 67 | $connect_hook_id ||= Gtk2::ActionGroup->signal_add_emission_hook | ||
| 68 | (connect_proxy => \&_do_connect_proxy); | ||
| 69 | $disconnect_hook_id ||= Gtk2::ActionGroup->signal_add_emission_hook | ||
| 70 | (disconnect_proxy => \&_do_disconnect_proxy); | ||
| 71 | |||
| 72 | _update ($self); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub SET_PROPERTY { | ||
| 76 | my ($self, $pspec, $newval) = @_; | ||
| 77 | my $pname = $pspec->get_name; | ||
| 78 | ### History-Action SET_PROPERTY(): $pname | ||
| 79 | |||
| 80 | $self->{$pname} = $newval; # per default GET_PROPERTY | ||
| 81 | _update ($self); | ||
| 82 | } | ||
| 83 | |||
| 84 | # Gtk2::ActionGroup 'connect-proxy' emission hook handler | ||
| 85 | sub _do_connect_proxy { | ||
| 86 | my ($invocation_hint, $parameters) = @_; | ||
| 87 | my ($actiongroup, $self, $toolitem) = @$parameters; | ||
| 88 | ### History-Action _do_connect_proxy(): "@{[$self->get_name]} $self onto $toolitem" | ||
| 89 | ### child: $toolitem->get_child | ||
| 90 | |||
| 91 | my $button; | ||
| 92 | if ($self->isa(__PACKAGE__) | ||
| 93 | && $toolitem->isa('Gtk2::ToolItem') | ||
| 94 | && ($button = $toolitem->get_child) | ||
| 95 | && $button->isa('Gtk2::Button')) { | ||
| 96 | |||
| 97 | # must have 'button-release-mask' or the menu doesn't pop down on button | ||
| 98 | # release, for some reason | ||
| 99 | require Gtk2::Ex::WidgetEvents; | ||
| 100 | $toolitem->{(__PACKAGE__)}->{'wevents'} | ||
| 101 | = Gtk2::Ex::WidgetEvents->new ($button, ['button-release-mask']); | ||
| 102 | |||
| 103 | require Glib::Ex::SignalIds; | ||
| 104 | Scalar::Util::weaken (my $weak_self = $self); | ||
| 105 | $toolitem->{(__PACKAGE__)}->{'ids'} = Glib::Ex::SignalIds->new | ||
| 106 | ($button, | ||
| 107 | $button->signal_connect | ||
| 108 | (button_press_event => \&_do_button_press_event, \$weak_self)); | ||
| 109 | ### ids: $toolitem->{(__PACKAGE__)}->{'ids'} | ||
| 110 | } | ||
| 111 | return 1; # keep emission hook | ||
| 112 | } | ||
| 113 | |||
| 114 | # Gtk2::ActionGroup 'disconnect-proxy' emission hook handler | ||
| 115 | sub _do_disconnect_proxy { | ||
| 116 | my ($invocation_hint, $parameters) = @_; | ||
| 117 | my ($actiongroup, $action, $widget) = @$parameters; | ||
| 118 | ### History-Action _do_disconnect_proxy(): "@{[$action->get_name]} $action from $widget" | ||
| 119 | |||
| 120 | delete $widget->{(__PACKAGE__)}; | ||
| 121 | return 1; # keep emission hook | ||
| 122 | } | ||
| 123 | |||
| 124 | # 'button-press-event' handler on a toolitem button child | ||
| 125 | sub _do_button_press_event { | ||
| 126 | my ($button, $event, $ref_weak_self) = @_; | ||
| 127 | ### History-Action _do_button_press_event(): $event->button | ||
| 128 | my $self = $$ref_weak_self || return; | ||
| 129 | if ($event->button == 3 && (my $history = $self->{'history'})) { | ||
| 130 | require Gtk2::Ex::History::Menu; | ||
| 131 | Gtk2::Ex::History::Menu->new_popup (history => $history, | ||
| 132 | way => $self->get('way'), | ||
| 133 | event => $event); | ||
| 134 | } | ||
| 135 | return Gtk2::EVENT_PROPAGATE; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _update { | ||
| 139 | my ($self) = @_; | ||
| 140 | my $way = $self->get('way'); | ||
| 141 | |||
| 142 | $self->set (stock_id => "gtk-go-$way", | ||
| 143 | tooltip => ($way eq 'back' | ||
| 144 | ? __('Go back.') | ||
| 145 | : __('Go forward.'))); | ||
| 146 | |||
| 147 | my $history = $self->{'history'}; | ||
| 148 | $self->{'connp'} = $history && Glib::Ex::ConnectProperties->dynamic | ||
| 149 | ([$history->model($way), 'model-rows#not-empty'], | ||
| 150 | [$self, 'sensitive']); | ||
| 151 | } | ||
| 152 | |||
| 153 | sub _do_activate { | ||
| 154 | my ($self) = @_; | ||
| 155 | my $history = $self->{'history'} || return; | ||
| 156 | my $way = $self->get('way'); | ||
| 157 | $history->$way; | ||
| 158 | } | ||
| 159 | |||
| 160 | 1; | ||
| 161 | __END__ |