Debian Codegraph @

DragDrop/site_test

raw
1 #!/usr/local/bin/perl -w
2 use Tk;
3 use strict;
4 use Tk::DropSite;
5 use vars qw($kind $STRING $FILE_NAME);
6
7 sub MAX_LEN () { 48 }
8
9 BEGIN
10 {
11 $kind = ($^O eq 'MSWin32' or ($^O eq 'cygwin' and $Tk::platform eq 'MSWin32'))?
12 ['Win32'] : ['Sun','XDND']
13 }
14 use Tk::DropSite @$kind;
15 use Tk::DragDrop @$kind;
16 use Tk::Menubar;
17
18 $SIG{__WARN__} = sub {
19 local ($_) = @_;
20 warn "$_";
21 if (/Malformed UTF-8 character|Attempt to free unreferenced|Use of uninitialized/)
22 {
23 Tk::abort();
24 }
25 };
26
27 sub enter_leave
28 {
29 my ($ds,$flag) = @_;
30 $ds->configure(-relief => ($flag) ? 'sunken' : 'ridge');
31 return 1;
32 }
33
34 my $top = MainWindow->new();
35 my $mb = $top->Menubar;
36 $mb->Menubutton(-text => '~File', -menuitems => [
37 [ Button => 'E~xit', -command => [ destroy => $top]],
38 ]);
39
40 my $but = $top->Frame->pack;
41
42 my $lb = $top->Scrolled('Listbox',-width => 40)->pack(-side => 'bottom');
43
44 my $filename = "/tmp/SomethingSilly";
45 my $uri = "file://localhost/tmp/SomethingSilly";
46
47 open(FOO,">$filename");
48 print FOO "Hi There\n";
49 close(FOO);
50
51 my $src = $top->Frame->pack;
52 foreach my $k ('Local',@$kind,'Any')
53 {
54 my $thing = $src->Message('-text' => "$k Source")->pack(-side => 'left');
55 my @list;
56 @list = (-sitetypes => $k) unless $k eq 'Any';
57 $thing->DragDrop(-event => '<B1-Motion>', @list,
58 -handlers => [
59 [-type => 'text/plain',[\&string_handler,"This is text/plain"]],
60 [-type => 'text/uri-list',[\&string_handler,$uri]],
61 ]);
62 }
63
64
65 my $dst = $top->Frame->pack;
66 foreach my $k ('Local',@$kind,'Any')
67 {
68 my @list;
69 @list = (-droptypes => $k) unless $k eq 'Any';
70 my $sun = $dst->Message('-text' => "$k Drops here", '-relief' => 'ridge' )->pack(-side => 'left');
71 $sun->DropSite(-dropcommand => [\&ShowTargets,$lb],
72 -entercommand => [\&enter_leave,$sun],
73 @list);
74 }
75
76 my $str = $top->Frame->pack(-fill => 'x');
77 foreach my $targ (qw(STRING FILE_NAME))
78 {
79 no strict 'refs';
80 my $l = $str->Label(-anchor => 'e', -justify => 'right', -text => $targ);
81 my $v = $str->Label(-anchor => 'w', -justify => 'left', -relief => 'groove',
82 -textvariable => \${$targ});
83 Tk::grid($l,$v,-sticky => 'ew');
84 }
85 $str->gridColumnconfigure(0,-weight => 0);
86 $str->gridColumnconfigure(1,-weight => 1);
87
88 sub string_handler
89 {
90 my ($text,$offset,$max) = @_;
91 return substr($text,$offset,$max);
92 }
93
94 MainLoop;
95
96 use Data::Dumper;
97
98 sub ShowTargets
99 {
100 my ($lb,$seln,$action,$targ,$x,$y) = @_;
101 my $own = $lb->SelectionExists('-selection'=>$seln);
102 unless ($targ && @$targ)
103 {
104 Tk::catch { $targ = [$lb->SelectionGet('-selection'=>$seln,'TARGETS')] };
105 }
106 print "Action = $action\n" if $action;
107 $lb->delete(0,'end');
108 $lb->insert('end',@$targ);
109 $STRING = $FILE_NAME = '';
110 foreach (@$targ)
111 {
112 if (m#^(FILE_NAME|text/uri-list)#)
113 {
114 Tk::catch { $FILE_NAME = $lb->SelectionGet('-selection'=>$seln,$_) };
115 }
116 elsif (/_SUN/ && !/(END|ACK|DONE|YIELD)/)
117 {
118 my @info;
119 Tk::catch { @info = $lb->SelectionGet('-selection'=>$seln,$_) };
120 if ($@)
121 {
122 print "Cannot get $_\n";
123 }
124 else
125 {
126 print "$_:",Dumper(\@info);
127 }
128 }
129 elsif (m#^((UTF8_)?STRING$|text/)#)
130 {
131 my $string;
132 Tk::catch { $string = $lb->SelectionGet('-selection'=>$seln,$_) };
133 if ($@)
134 {
135 print "Cannot get $_:$@\n";
136 }
137 else
138 {
139 $STRING = $string;
140 $string =~ s/([^\x20-\x7e\xa0-\xff])/sprintf("\\x{%x}",ord($1))/ge;
141 if (length($string) > MAX_LEN())
142 {
143 substr($string,MAX_LEN()-3) = "..."
144 }
145 print "$_:$string\n";
146 }
147 }
148 }
149 }
150
151