| 1 | ##============================================================================== | ||
| 2 | ## Tk::autobind - automatically bind a widget to an ALT-key | ||
| 3 | ##============================================================================== | ||
| 4 | ## Copyright 2001-2003 Kevin Michael Vail. All rights reserved. | ||
| 5 | ## This program is free software; you can redistribute it and/or modify it | ||
| 6 | ## under the same terms as Perl itself. | ||
| 7 | ##============================================================================== | ||
| 8 | ## $Id: autobind.pm,v 1.2 2003/06/17 01:23:39 kevin Exp $ | ||
| 9 | ##============================================================================== | ||
| 10 | require 5.005; | ||
| 11 | |||
| 12 | package Tk::autobind; | ||
| 13 | use Tk; | ||
| 14 | use strict; | ||
| 15 | use vars qw($VERSION); | ||
| 16 | ($VERSION) = q$Revision: 1.2 $ =~ /^Revision:\s+(\S+)/ or $VERSION = "0.0"; | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | Tk::autobind - automatically bind a widget to an ALT-key | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | C<< use Tk::autobind; >> | ||
| 25 | |||
| 26 | C<< I<$widget>->autobind(I<callback>); >> | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | C<Tk::autobind> offers a convenient way to set up a form and have ALT-key | ||
| 31 | bindings for the widgets on that form. All you have to do is call C<autobind> | ||
| 32 | after you create a widget. If the widget has an C<-underline> configuration | ||
| 33 | option set to a value greater than or equal to 0, its ALT-key binding is the | ||
| 34 | key at that location in the widget's C<-text> configuration option. | ||
| 35 | |||
| 36 | For example, if you have a Checkbutton with the following C<-text>: | ||
| 37 | |||
| 38 | Automatically fix | ||
| 39 | |||
| 40 | and its C<-underline> value is set to 0, then if the user presses ALT-A while | ||
| 41 | the focus is in the widget's main window, the checkbutton widget will be | ||
| 42 | invoked. | ||
| 43 | |||
| 44 | The binding that is generated is essentially | ||
| 45 | |||
| 46 | C<< $widget->toplevel->bind('<Alt-Key-I<x>>', $callback); >> | ||
| 47 | |||
| 48 | =head1 METHODS | ||
| 49 | |||
| 50 | =over 4 | ||
| 51 | |||
| 52 | =item I<$widget>->autobind(I<callback>); | ||
| 53 | |||
| 54 | Adds the binding given above. If I<callback> is specified, it must be one of | ||
| 55 | the forms of a valid Tk callback (see L<Tk::callbacks>). If it is omitted, a | ||
| 56 | closure is generated and used: | ||
| 57 | |||
| 58 | sub { $widget->Invoke } | ||
| 59 | |||
| 60 | If I<callback> is explicitly set to an empty string, the binding is removed. | ||
| 61 | |||
| 62 | This method always returns I<$widget> to allow method chaining. For example, | ||
| 63 | you can stick B<autobind> before the call to B<pack>: | ||
| 64 | |||
| 65 | my $checkbutton = $mw->Checkbutton(Name => 'cb1')->autobind->pack; | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | ##============================================================================== | ||
| 70 | ## autobind | ||
| 71 | ##============================================================================== | ||
| 72 | sub Tk::Widget::autobind { | ||
| 73 | my ($widget, $callback) = @_; | ||
| 74 | my $underline; | ||
| 75 | $underline = eval { $widget->cget('-underline') }; | ||
| 76 | if (!$@ && defined $underline && $underline >= 0) { | ||
| 77 | my $key; | ||
| 78 | $key = eval { substr($widget->cget('-text'), $underline, 1) }; | ||
| 79 | if (!$@ && defined $key) { | ||
| 80 | $callback = sub { $widget->Invoke; } unless defined $callback; | ||
| 81 | $widget->toplevel->bind("<Alt-Key-\L$key>", $callback); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | return $widget; | ||
| 85 | } | ||
| 86 | |||
| 87 | =pod | ||
| 88 | |||
| 89 | =back | ||
| 90 | |||
| 91 | =head1 SEE ALSO | ||
| 92 | |||
| 93 | L<Tk::bind> | ||
| 94 | |||
| 95 | =head1 AUTHOR | ||
| 96 | |||
| 97 | Kevin Michael Vail F<< <kevin@vaildc.net> >> | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | 1; | ||
| 102 | |||
| 103 | ##============================================================================== | ||
| 104 | ## $Log: autobind.pm,v $ | ||
| 105 | ## Revision 1.2 2003/06/17 01:23:39 kevin | ||
| 106 | ## Gracefully handle things when applied to a widget that doesn't | ||
| 107 | ## _have_ an -underline or a -text config option. | ||
| 108 | ## | ||
| 109 | ## Revision 1.1 2001/09/01 02:36:35 kevin | ||
| 110 | ## Allow this to work for dialogs, too. | ||
| 111 | ## | ||
| 112 | ## Revision 1.0 2001-08-24 11:22:14-04 kevin | ||
| 113 | ## Initial revision | ||
| 114 | ##============================================================================== |