| 1 | package Tk::HyperlinkButton; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.016003; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Browser::Open qw/ open_browser /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 0 | ▶ | use Tk; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use Tk::widgets qw/ Button /; | ||
| 10 | use base qw/ Tk::Derived Tk::Button /; | ||
| 11 | |||
| 12 | Construct Tk::Widget 'HyperlinkButton'; | ||
| 13 | |||
| 14 | our $VERSION = '0.02'; | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | Tk::HyperlinkButton - Create a clickable hyperlink button to open a web browser | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | use Tk::HyperlinkButton; | ||
| 23 | |||
| 24 | my $mw = MainWindow->new(); | ||
| 25 | |||
| 26 | my $link_text = 'metacpan.org'; | ||
| 27 | my $link_target = 'http://www.metacpan.org'; | ||
| 28 | my $link_callback = sub{ print "your callback here\n"; }; | ||
| 29 | |||
| 30 | my $hyperlink_widget = $mw->HyperlinkButton( | ||
| 31 | -text => $link_text, | ||
| 32 | -target => $link_target, | ||
| 33 | -command => $link_callback, | ||
| 34 | ); | ||
| 35 | |||
| 36 | $hyperlink_widget->pack; | ||
| 37 | |||
| 38 | $mw->MainLoop; | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | Tk::Hyperlink is an adjusted L<Tk::Button> widget to display a hyperlink. | ||
| 43 | The hyperlink will be displayed with blue foreground color by default and highlighted on mouse over. | ||
| 44 | The look and feel resembles a Label (no borders by default). | ||
| 45 | |||
| 46 | A custom callback can be provided the same way as for other buttons (using the option C<-command>). | ||
| 47 | The callback will be executed when the hyperlink is clicked. | ||
| 48 | The default callback will open the system's default web browser and navigate to the URL provided by the option C<target>. | ||
| 49 | If C<target> is not defined, the URL will be what you define by the option C<-text>. | ||
| 50 | |||
| 51 | The widget is indented to only represent the hyperlink. | ||
| 52 | The intended use case is actually to have some kind of L<Tk::Label>-like widget that is a clickable hyperlink. | ||
| 53 | |||
| 54 | Text in front or after the hyperlink should be created with other means (e.g. L<Tk::Label> or L<Tk::Text>). | ||
| 55 | |||
| 56 | =head1 WIDGET-SPECIFIC OPTIONS | ||
| 57 | |||
| 58 | =head2 target | ||
| 59 | |||
| 60 | Sets the address for the hyperlink. If target is not given, C<-text> will be used as target automatically. | ||
| 61 | |||
| 62 | Using C<-text> and C<-target> in combination, you can get a clickable hyperlink that open a web browser with the address defined in C<-target>, but that displays a different text as defined in C<-text>. | ||
| 63 | Basically, that's what you have in HTML as well. | ||
| 64 | |||
| 65 | =head2 command | ||
| 66 | |||
| 67 | A callback that will be executed when the hyperlink is clicked. Optional. | ||
| 68 | By default, clicking the hyperlink will open the system's default browser and navigate to the URL (cf. C<-target>). | ||
| 69 | |||
| 70 | =head1 WIDGET METHODS | ||
| 71 | |||
| 72 | The HyperlinkButton method creates a widget object. | ||
| 73 | This object supports the C<configure> and C<cget> methods described in L<Tk::options> which can be used to enquire and modify the options described above. | ||
| 74 | The widget also inherits all the methods provided by the generic L<Tk::Widget> class. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | ##=head2 Populate( %args ) | ||
| 79 | ## | ||
| 80 | ##Handles the custom widget attributes and defaults (e.g. blue font color). | ||
| 81 | ## | ||
| 82 | ##Creates the bindings to change the mouse cursor and relief (raised/flat). | ||
| 83 | ##Bindings are created as instance bindings, because we do not want to overwrite the L<Tk::Button> class bindings. | ||
| 84 | ## | ||
| 85 | ##=cut | ||
| 86 | |||
| 87 | sub Populate { | ||
| 88 | my( $self, $args ) = @_; | ||
| 89 | |||
| 90 | my $link_text = $args->{'-text'}; | ||
| 91 | |||
| 92 | my $target = delete $args->{'-target'}; | ||
| 93 | unless( defined $target ) { | ||
| 94 | $target = $link_text; | ||
| 95 | } | ||
| 96 | |||
| 97 | my $link_callback = $args->{'-command'}; | ||
| 98 | |||
| 99 | # only use default behavior if no custom command is provided | ||
| 100 | unless( defined $link_callback ) { | ||
| 101 | $link_callback = sub{ $self->open_link_in_browser(); }; | ||
| 102 | $args->{'-command'} = $link_callback; | ||
| 103 | } | ||
| 104 | |||
| 105 | $self->SUPER::Populate( $args ); | ||
| 106 | |||
| 107 | my %defaults = ( | ||
| 108 | -relief => 'flat', | ||
| 109 | -foreground => 'blue', | ||
| 110 | -overrelief => 'raised', | ||
| 111 | -activeforeground => 'blue', | ||
| 112 | -borderwidth => 1, | ||
| 113 | ); | ||
| 114 | |||
| 115 | foreach my $attr_name ( keys %defaults ) { | ||
| 116 | if( !exists $args->{$attr_name} or !defined $args->{$attr_name} ) { | ||
| 117 | $args->{$attr_name} = $defaults{$attr_name}; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | $self->ConfigSpecs( | ||
| 122 | 'DEFAULT' => [$self], | ||
| 123 | '-target' => [ 'PASSIVE', 'target', 'Target', $target ], | ||
| 124 | ); | ||
| 125 | |||
| 126 | $self->Delegates( | ||
| 127 | 'DEFAULT' => $self, | ||
| 128 | ); | ||
| 129 | |||
| 130 | $self->bind('<Any-Enter>' => sub{ | ||
| 131 | highlight_link_cursor($_[0], 'raised', 'hand2'); | ||
| 132 | return; | ||
| 133 | }); | ||
| 134 | |||
| 135 | $self->bind('<Any-Leave>' => sub{ | ||
| 136 | highlight_link_cursor($_[0], 'flat', 'xterm'); | ||
| 137 | return; | ||
| 138 | }); | ||
| 139 | |||
| 140 | return; | ||
| 141 | } # /Populate | ||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | ##=head2 highlight_link_cursor($cursor) | ||
| 147 | ## | ||
| 148 | ##Changes the cursor when the mouse hovers over the hyperlink. | ||
| 149 | ##On hover, y default, the cursor C<hand2> will be used. It will be reset to the cursor C<xterm>. | ||
| 150 | ## | ||
| 151 | ##=cut | ||
| 152 | |||
| 153 | sub highlight_link_cursor { | ||
| 154 | my ($w, $relief, $cursor) = @_; | ||
| 155 | $w->configure(-relief => $relief) if $relief; | ||
| 156 | $w->configure(-cursor => $cursor) if $cursor; | ||
| 157 | } # /highlight_link_cursor | ||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | =head2 open_link_in_browser() | ||
| 163 | |||
| 164 | Open a browser and navigate to the hyperlink target (cf. C<-target>). | ||
| 165 | Uses L<Browser::Open> for starting the browser. | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | sub open_link_in_browser { | ||
| 170 | my $self = shift; | ||
| 171 | my $target = $self->cget('-target'); | ||
| 172 | |||
| 173 | ## debug: | ||
| 174 | #print "target: $target\n"; | ||
| 175 | #print "command used to start the browser: " . Browser::Open::open_browser_cmd() . "\n"; | ||
| 176 | |||
| 177 | my $ok = open_browser($target); | ||
| 178 | # ! defined($ok): no recognized command found | ||
| 179 | # $ok == 0: command found and executed | ||
| 180 | # $ok != 0: command found, error while executing | ||
| 181 | return; | ||
| 182 | } # /open_link_in_browser | ||
| 183 | |||
| 184 | |||
| 185 | |||
| 186 | =head1 BINDINGS | ||
| 187 | |||
| 188 | When a new hyperlink is created, it has default instance event bindings to the following events: | ||
| 189 | |||
| 190 | =over | ||
| 191 | |||
| 192 | =item * C<Any-Enter>: Will highlight the hyperlink similar to what you see in a web browser. Also changes the cursor. | ||
| 193 | |||
| 194 | =item * C<Any-Leave>: Will reset the highlightning or appearance changes done by the other events. | ||
| 195 | |||
| 196 | =back | ||
| 197 | |||
| 198 | This widget is intended to be interactive. | ||
| 199 | |||
| 200 | =head1 TODO | ||
| 201 | |||
| 202 | * Describe how to embed hyperlinks in Tk::Text / Tk::ROText. | ||
| 203 | * configuration options for font and colors | ||
| 204 | |||
| 205 | =head1 SEE ALSO | ||
| 206 | |||
| 207 | L<Tk::Button>, L<Browser::Open> | ||
| 208 | |||
| 209 | Code is inspired by L<https://www.perlmonks.org/?node_id=667664>, downloaded 2019-11-01. | ||
| 210 | |||
| 211 | =head1 AUTHOR | ||
| 212 | |||
| 213 | Alexander Becker, E<lt>asb@cpan.orgE<gt> | ||
| 214 | |||
| 215 | =head1 COPYRIGHT AND LICENSE | ||
| 216 | |||
| 217 | Copyright (C) 2019 by Alexander Becker | ||
| 218 | |||
| 219 | This library is free software; you can redistribute it and/or modify | ||
| 220 | it under the same terms as Perl itself, either Perl version 5.28.0 or, | ||
| 221 | at your option, any later version of Perl 5 you may have available. | ||
| 222 | |||
| 223 | =head1 KEYWORDS | ||
| 224 | |||
| 225 | button, widget, hyperlink, link, web link | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | 1; # /Tk::HyperlinkButton | ||