File: insert-text-test.pl

package info (click to toggle)
libgtk2-perl 2%3A1.244-1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,524 kB
  • sloc: perl: 21,568; ansic: 122; makefile: 6
file content (54 lines) | stat: -rw-r--r-- 1,400 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
=doc

Use the insert-text signal to trap and possibly mangle text as the user types,
but before it is committed and displayed.

=cut

use Gtk2 -init;
use Data::Dumper;


$dialog = Gtk2::Dialog->new;
$dialog->add_button ('gtk-close' => 'close');

$entry = Gtk2::Entry->new;
$entry->set_activates_default (1);
$entry->signal_connect ('insert-text' => sub {
			my ($widget, $string, $len, $position) = @_;
			if ($string eq '-') {
				$_[1] = '_';
				$_[3]--;
			} elsif ($string eq 'ee') {
				$_[1] = ' <whee> ';
			} elsif ($string eq '#') {
				# just can't insert these.
				$entry->signal_stop_emission_by_name
							('insert-text');
				warn "NOSOUPFORYOU!!\n";
			}
			() # this callback must return either 2 or 0 items.
		});

$dialog->vbox->pack_start ($entry, 0, 0, 0);
$entry->show;

$label = Gtk2::Label->new;
$label->set_markup ('using a handler for insert-text, this entry
turns dashes into underscores (moved back
one space), turns the string "ee" into
something else (you\'ll have to paste it),
and won\'t let you type a #.');
$dialog->vbox->pack_start ($label, 1,1, 0);
$label->show;


# while we're testing custom marshallers, connect to dialog's response
# signal, which also has a custom marshaller.  if the blue smoke gets
# out, then something i broken deep inside.
$dialog->signal_connect (response => sub {
	print Dumper(\@_);
	Gtk2->main_quit;
});
$dialog->show;
Gtk2->main;