| 1 | # Copyright 2007, 2008, 2009, 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::Menu; | ||
| 19 | 1 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 0 | ▶ | use Gtk2 1.220; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 23 | use Scalar::Util; | ||
| 24 | |||
| 25 | use Glib::Ex::ObjectBits; | ||
| 26 | use Gtk2::Ex::History; | ||
| 27 | use Gtk2::Ex::MenuView; | ||
| 28 | |||
| 29 | use Locale::TextDomain ('Gtk2-Ex-History'); | ||
| 30 | use Locale::Messages; | ||
| 31 | BEGIN { | ||
| 32 | Locale::Messages::bind_textdomain_codeset ('Gtk2-Ex-History','UTF-8'); | ||
| 33 | Locale::Messages::bind_textdomain_filter ('Gtk2-Ex-History', | ||
| 34 | \&Locale::Messages::turn_utf_8_on); | ||
| 35 | } | ||
| 36 | use Gtk2::Ex::Dashes::MenuItem; | ||
| 37 | |||
| 38 | # uncomment this to run the ### lines | ||
| 39 | #use Smart::Comments; | ||
| 40 | |||
| 41 | our $VERSION = 8; | ||
| 42 | |||
| 43 | use Glib::Object::Subclass | ||
| 44 | 'Gtk2::Ex::MenuView', | ||
| 45 | signals => { item_create_or_update => \&_do_item_create_or_update, | ||
| 46 | activate => \&_do_activate, | ||
| 47 | }, | ||
| 48 | properties => [ Glib::ParamSpec->object | ||
| 49 | ('history', | ||
| 50 | __('History object'), | ||
| 51 | 'The history object to present and act on.', | ||
| 52 | 'Gtk2::Ex::History', | ||
| 53 | Glib::G_PARAM_READWRITE), | ||
| 54 | |||
| 55 | Glib::ParamSpec->enum | ||
| 56 | ('way', | ||
| 57 | 'Which way', | ||
| 58 | 'Which way of the history to present, either back or forward.', | ||
| 59 | 'Gtk2::Ex::History::Way', | ||
| 60 | 'back', | ||
| 61 | Glib::G_PARAM_READWRITE), | ||
| 62 | ]; | ||
| 63 | |||
| 64 | |||
| 65 | sub INIT_INSTANCE { | ||
| 66 | my ($self) = @_; | ||
| 67 | |||
| 68 | my $dashesitem = Gtk2::Ex::Dashes::MenuItem->new (visible => 1); | ||
| 69 | $dashesitem->signal_connect (activate => \&_do_dashesitem_activate); | ||
| 70 | $self->prepend ($dashesitem); | ||
| 71 | Glib::Ex::ObjectBits::set_property_maybe | ||
| 72 | ($dashesitem, | ||
| 73 | # tooltip-text new in Gtk 2.12 | ||
| 74 | tooltip_text => __('Open the back/forward history dialog')); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub SET_PROPERTY { | ||
| 78 | my ($self, $pspec, $newval) = @_; | ||
| 79 | my $pname = $pspec->get_name; | ||
| 80 | $self->{$pname} = $newval; # per default GET_PROPERTY | ||
| 81 | |||
| 82 | if ($pname eq 'history' || $pname eq 'way') { | ||
| 83 | my $history = $self->{'history'}; | ||
| 84 | $self->set (model => $history && $history->model($self->get('way'))); | ||
| 85 | #### History-Menu model: $self->get('model') | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | # 'activate' signal handler on Dashes::MenuItem tearoff | ||
| 90 | sub _do_dashesitem_activate { | ||
| 91 | my ($dashesitem) = @_; | ||
| 92 | my $self = $dashesitem->get_parent || return; # if orphaned somehow | ||
| 93 | require Gtk2::Ex::History::Dialog; | ||
| 94 | Gtk2::Ex::History::Dialog->popup ($self->{'history'}, $self); | ||
| 95 | } | ||
| 96 | |||
| 97 | # 'item-create-or-update' class closure handler | ||
| 98 | sub _do_item_create_or_update { | ||
| 99 | my ($self, $item, $model, $path, $iter) = @_; | ||
| 100 | #### History-Menu _do_item_create_or_update(): $path->to_string | ||
| 101 | |||
| 102 | $item ||= Gtk2::MenuItem->new_with_label (''); | ||
| 103 | my $place = $model->get ($iter, 0); | ||
| 104 | if (my $history = $self->{'history'}) { | ||
| 105 | # should always have the history obj when still have model ... | ||
| 106 | $item->get_child->set_use_markup ($history->get('use-markup')); | ||
| 107 | $place = $history->signal_emit ('place-to-text', $place); | ||
| 108 | } | ||
| 109 | $item->get_child->set_text ($place); | ||
| 110 | |||
| 111 | return $item; | ||
| 112 | } | ||
| 113 | |||
| 114 | # 'activate' class closure handler | ||
| 115 | sub _do_activate { | ||
| 116 | my ($self, $item, $model, $path, $iter) = @_; | ||
| 117 | my $history = $self->{'history'} || return; | ||
| 118 | my $way = $self->get('way'); | ||
| 119 | my $n = ($path->get_indices)[0]; | ||
| 120 | $history->$way ($n+1); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub new_popup { | ||
| 124 | my ($class, %options) = @_; | ||
| 125 | ### History-Menu new_popup() | ||
| 126 | my $event = delete $options{'event'}; | ||
| 127 | my $self = $class->new (%options); | ||
| 128 | |||
| 129 | my $button = 0; | ||
| 130 | my $time = 0; | ||
| 131 | if ($event) { | ||
| 132 | if ($event->can('button')) { | ||
| 133 | $button = $event->button; | ||
| 134 | } | ||
| 135 | if ($event->can('time')) { | ||
| 136 | $time = $event->time; | ||
| 137 | } | ||
| 138 | if (my $window = $event->window) { | ||
| 139 | $self->set_screen ($window->get_screen); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | ### screen: $self->get_screen->make_display_name | ||
| 143 | ### $button | ||
| 144 | ### $time | ||
| 145 | $self->popup (undef, undef, undef, undef, $button, $time); | ||
| 146 | return $self; | ||
| 147 | } | ||
| 148 | |||
| 149 | 1; | ||
| 150 | __END__ | ||