Debian Codegraph @

DragDrop/selection

raw
1 #!/usr/bin/perl -w
2 use Tk;
3 use Encode qw(decode);
4 use Encode::Guess;
5 use strict;
6
7 $Tk::FontRank = \&Ranker;
8
9 my $mw = MainWindow->new();
10 my $seln;
11
12 my $tb = $mw->Frame->pack(-fill => 'x');
13
14 my $lb = $mw->Scrolled('Listbox',-width => 40, -exportselection => 0);
15
16 my $entry = $mw->Entry->pack(-fill => 'x');
17
18 $tb->Button('-text' => "Primary Targets", '-command' => [\&ShowTargets,$lb,'PRIMARY'])->pack('-side'=>'left');
19 $tb->Button('-text' => "Clipboard Targets", '-command' => [\&ShowTargets,$lb,'CLIPBOARD'])->pack('-side'=>'left');
20 $tb->Label('-textvariable' => \$seln)->pack('-side'=>'left');
21 $tb->Button('-text' => "Quit", '-command' => ['destroy',$mw])->pack('-side'=>'right');
22
23 $lb->packAdjust(-expand => 1, -fill => 'both');
24 my $tx = $mw->Scrolled('ROText',-width => 40, -exportselection => 0, -wrap => 'none')->pack(-expand => 1, -fill => 'both');
25
26 $lb->bind('<ButtonRelease-1>',[\&GetSelected,$tx]);
27
28 ShowTargets($lb,'PRIMARY');
29
30 MainLoop;
31
32 sub GetSelected
33 {
34 my ($lb,$tx) = @_;
35 my $name = $lb->getSelected;
36 if (defined $name)
37 {
38 $tx->insert('end',"----- $name -----\n");
39 my @targ = $lb->SelectionGet('-selection'=>$seln,$name);
40 if ($name eq 'text/html')
41 {
42 my $targ = join("", @targ);
43 $targ = decode("Guess", $targ);
44 $tx->insert('end', $targ);
45 }
46 else
47 {
48 foreach (@targ)
49 {
50 $tx->insert('end',"$_\n");
51 }
52 }
53 $tx->see('end');
54 }
55 }
56
57 sub ShowTargets
58 {
59 my $lb = shift;
60 $seln = shift;
61 my $own = $lb->SelectionExists('-selection'=>$seln);
62 if ($own)
63 {
64 printf "owner of $seln is %x\n",$own;
65 $lb->delete(0,'end');
66 eval {
67 my @targ = $lb->SelectionGet('-selection'=>$seln,'TARGETS');
68 $lb->insert('end',@targ);
69 foreach (@targ)
70 {
71 if (/FILE_NAME/)
72 {
73 print $lb->SelectionGet('-selection'=>$seln,'FILE_NAME'),"\n";
74 }
75 }
76 };
77 }
78 }
79
80 sub Ranker
81 {
82 my ($cost,$ch,$want,$got) = @_;
83 return $cost if $ch == 32;
84 if ($cost == 0)
85 {
86 printf "U+%04x for ",$ch;
87 print $want->family," use ",$got->Xname,"\n";
88 }
89 elsif ($cost == -1)
90 {
91 printf "U+%04x $ch not in ",$ch;
92 print $got->Xname,"\n";
93 }
94 else
95 {
96 return (1 << 30) if ($got->size && $want->size != $got->size);
97 if ($got->encoding eq 'iso10646-1')
98 {
99 $cost = ($cost >> 3) || 1;
100 }
101 printf "%08x for U+%04x ",$cost,$ch;
102 print $want->family,' got ',$got->family,'/',$got->encoding,"\n";
103 }
104 return $cost;
105 }
106