Gtk2-Ex-WidgetBits
view release on metacpan or search on metacpan
lib/Gtk2/Ex/EntryBits.pm view on Meta::CPAN
my %direction_to_offset = (up => 1,
down => -1);
sub scroll_number_handler {
my ($entry, $event) = @_;
### EntryBits scroll_number() ...
if (my $num_increment = $direction_to_offset{$event->direction}) {
if ($event->state & 'control-mask') {
$num_increment *= 10;
}
if (defined (my $pos = x_to_text_index($entry,$event->x))) {
my $text = $entry->get_text;
my $text_at = substr($text,$pos);
if ($text_at =~ /^(\d+)/) {
my $num_len = length($1);
my $text_before = substr($text,0,$pos);
$text_before =~ /(\d*)$/;
$pos -= length($1);
$num_len += length($1);
my $old_len = length($text);
my $num = substr($text, $pos, $num_len);
$text = substr($text, 0, $pos)
. max(0, $num+$num_increment)
. substr($text, $pos+$num_len);
my $cursor_position = $entry->get_position;
$entry->set_text ($text);
$entry->set_position ($cursor_position
+ length($text)-$old_len);
$entry->activate;
return 1; # Gtk2::EVENT_STOP
}
}
}
return 0; # Gtk2::EVENT_PROPAGATE
}
1;
__END__
=for stopwords Ryde Gtk2-Ex-WidgetBits
=head1 NAME
Gtk2::Ex::EntryBits -- misc functions for Gtk2::Entry widgets
=head1 SYNOPSIS
use Gtk2::Ex::EntryBits;
=head1 FUNCTIONS
=over 4
=item C<< Gtk2::Ex::EntryBits::select_region_noclip ($entry, $start, $end) >>
Select text from C<$start> to C<$end> like C<< $entry->select_region >>, but
don't put it on the clipboard. This is a good way to let the user type over
previous text, without upsetting any cut and paste in progress.
This is implemented with a nasty hack temporarily pretending C<$entry> is
unrealized.
=item C<< $pos = Gtk2::Ex::EntryBits::x_to_text_index ($entry, $x) >>
Convert from C<$x> pixel in C<$entry> widget coordinates to a character
index into the text string in the entry.
If C<$x> is past the beginning of the text the return is 0. If C<$x> is
past the end of the text the return is length(text). Any xalign or user
scrolling is accounted for so the character position is as the text appears
on screen.
This is implemented by pango layout offset as described in the GtkEntry
C<gtk_entry_get_layout_offsets()> documentation.
=item C<< $event_ret = Gtk2::Ex::EntryBits::scroll_number_handler ($entry, $event) >>
This function can be used as a handler for the C<scroll-event> signal of a
C<Gtk2::Entry> to have a mouse scroll increment or decrement numbers within
the entry text.
+-----------------+
| abc,123+def*56 |
+-----------------+
^ ^
scroll with mouse over a number to increment/decrement
If C<$event-E<gt>direction()> is "up" or "down" and the mouse
C<$event-E<gt>x()> position is over a number then increment or decrement
that number by 1, or if the control key (C<$event-E<gt>state>) is held down
then by 10.
The return value is C<Gtk2::EVENT_STOP> if a number is scrolled, or
C<Gtk2::EVENT_PROPAGATE> if not (either "left" or "right" scrolls or not
over a number).
=back
=head1 EXPORTS
Nothing is exported by default, but the functions can be requested in usual
C<Exporter> style,
use Gtk2::Ex::EntryBits 'x_to_text_index';
$pos = x_to_text_index ($entry, $x);
The names are probably a bit too generic to want to import most of the time,
but might suit an Entry subclass or something only Entry related.
There's no C<:all> tag since this module is meant as a grab-bag of functions
and to import as-yet unknown things would be asking for name clashes.
=head1 SEE ALSO
L<Gtk2::Entry>, L<Gtk2::Editable>
=head1 HOME PAGE
( run in 4.337 seconds using v1.01-cache-2.11-cpan-2398b32b56e )