File Coverage

blib/lib/Gtk2/Ex/ActionTooltips.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             # Copyright 2008, 2009, 2010, 2011, 2012 Kevin Ryde
2              
3             # This file is part of Gtk2-Ex-WidgetBits.
4             #
5             # Gtk2-Ex-WidgetBits is free software; you can redistribute it and/or
6             # modify it under the terms of the GNU General Public License as published
7             # by the Free Software Foundation; either version 3, or (at your option) any
8             # later version.
9             #
10             # Gtk2-Ex-WidgetBits is distributed in the hope that it will be useful,
11             # but WITHOUT ANY WARRANTY; without even the implied warranty of
12             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13             # Public License for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with Gtk2-Ex-WidgetBits. If not, see .
17              
18             package Gtk2::Ex::ActionTooltips;
19 2     2   30601 use 5.008;
  2         7  
  2         81  
20 2     2   11 use strict;
  2         3  
  2         67  
21 2     2   11 use warnings;
  2         8  
  2         59  
22 2     2   8 use Carp;
  2         11  
  2         164  
23 2     2   2176 use Gtk2 1.160; # for $widget->set_tooltip_text new in Gtk2 1.152
  0            
  0            
24              
25             our $VERSION = 48;
26              
27             use Exporter;
28             our @ISA = ('Exporter');
29             our @EXPORT_OK = qw(group_tooltips_to_menuitems
30             action_tooltips_to_menuitems_dynamic);
31             our %EXPORT_TAGS = (all => \@EXPORT_OK);
32              
33             # uncomment this to run the ### lines
34             #use Smart::Comments;
35              
36             # Cribs:
37             #
38             # $action->get_proxies can't be used within a 'connect-proxy' handler until
39             # Gtk2-Perl 1.220 (due to floating ref handling problems). That means not
40             # using group_tooltips_to_menuitems() within connect-proxy until that
41             # version, though doing so from there would be pretty unusual anyway.
42             #
43             # $action->get_tooltip isn't wrapped (gtk_action_get_tooltip()) as of
44             # Gtk2-Perl 1.220, hence use of $action->get('tooltip').
45             # $widget->set_tooltip_text is wrapped in 1.160, and will want at least that
46             # or newer for bug fixes, so may as well use that method instead of the
47             # 'tooltip-text' property.
48             #
49              
50             my $connect_hook_id;
51             sub _ensure_connect_hook {
52             $connect_hook_id ||= Gtk2::ActionGroup->signal_add_emission_hook
53             (connect_proxy => \&_do_connect_proxy);
54             }
55              
56             sub group_tooltips_to_menuitems {
57             Gtk2::Widget->can('set_tooltip_text') || return; # only in Gtk 2.12 up
58              
59             foreach my $actiongroup (@_) {
60             $actiongroup->{(__PACKAGE__)} = undef;
61             _ensure_connect_hook ();
62              
63             foreach my $action ($actiongroup->list_actions) {
64             _do_action_tooltip ($action);
65             }
66             }
67             }
68              
69             sub action_tooltips_to_menuitems_dynamic {
70             Gtk2::Widget->can('set_tooltip_text') || return; # only in Gtk 2.12 up
71              
72             foreach my $action (@_) {
73             $action->{(__PACKAGE__)} = undef;
74             _ensure_connect_hook ();
75              
76             $action->signal_connect ('notify::tooltip' => \&_do_action_tooltip);
77             _do_action_tooltip ($action);
78             }
79             }
80              
81             # Gtk2::ActionGroup 'connect-proxy' emission hook handler
82             sub _do_connect_proxy {
83             my ($invocation_hint, $parameters) = @_;
84             my ($actiongroup, $action, $widget) = @$parameters;
85             ### dynamic connect: ("@{[$action->get_name]} $action onto $widget")
86              
87             if ((exists $actiongroup->{(__PACKAGE__)}
88             || exists $action->{(__PACKAGE__)})
89             && $widget->isa('Gtk2::MenuItem')) {
90             $widget->set_tooltip_text ($action->get('tooltip'));
91             }
92             return 1; # stay connected
93             }
94              
95             # Gtk2::Action 'notify::tooltip' signal handler, and called directly
96             sub _do_action_tooltip {
97             my ($action) = @_;
98             my $tip = $action->get('tooltip');
99             ### tooltip: ["@{[$action->get_name]} $action, tip ", $tip]
100              
101             foreach my $widget ($action->get_proxies) {
102             ### proxy: $widget
103             if ($widget->isa('Gtk2::MenuItem')) {
104             $widget->set_tooltip_text ($tip);
105             }
106             }
107             }
108              
109             1;
110             __END__