Debian Codegraph @

examples/tie_basic

raw
1 #!/usr/local/bin/perl
2
3 # XXX Seems not to work, exits without an error message
4
5 # use Memory (2440);
6
7 # BEGIN { $ENV{'PERL_DL_NONLAZY'} = 1 }
8
9 use Carp;
10
11 $SIG{__DIE__} = sub { print "die ",shift,"\n" };
12
13
14 use Tk qw(exit);
15 # use Tk::Xrm;
16
17 use Tk::widgets qw(Button Label Menu Photo Optionmenu Pixmap);
18 use Tk::widgets qw(ErrorDialog Checkbutton Radiobutton Entry Message Listbox);
19
20
21 use Config;
22
23 sub showbinding
24 {
25 my $w = shift;
26 foreach ($w->bind())
27 {
28 print "$w $_ = ",$w->bind($_),"\n";
29 }
30 }
31
32 sub test_back_trace
33 {
34 my ($w,$count) = @_;
35 if ($count)
36 {
37 test_back_trace($w,$count-1);
38 }
39 else
40 {
41 $w->BackTrace("Moan");
42 }
43 }
44
45 sub back_trace
46 {
47 my @info;
48 my $i = 0;
49 while (@info = caller($i))
50 {
51 print "$i: ",join(' ',@info),"\n";
52 $i++;
53 }
54 }
55
56 sub send_test
57 {
58 my ($w) = @_;
59 my $result = eval { $w->send('basic_demo',"Hi there") };
60 die "Send failed '$@'" if ($@);
61 print "Send returned '$result'\n";
62 }
63
64 sub entry
65 {
66 my $top = shift;
67 my $scroll = shift;
68 my $f = ($scroll) ? $top->Frame() : $top;
69 my $e = $f->Entry("-relief","ridge",@_);
70 $e->pack("-side"=>"top","-fill"=>"x","-expand"=>"yes");
71 $e->bind("<Any-Enter>", sub { shift->focus() });
72 $e->bind("<Any-FocusIn>", sub { shift->configure("-relief" => "sunken") });
73 $e->bind("<Any-FocusOut>", sub { shift->configure("-relief" => "ridge" ) });
74 if ($scroll)
75 {
76 my $es = $f->Scrollbar("-orient"=>"horizontal","-command",["view",$e]);
77 $e->configure("-scrollcommand",["set",$es]);
78 $es->pack("-side"=>"bottom","-fill"=>"x");
79 $f->pack("-side","top");
80 }
81 return $e;
82 }
83
84 sub listbox
85 {
86 my $top = shift;
87 my $l = $top->ScrlListbox("-selectmode"=>"extended",-label => 'Listbox');
88 foreach (@_)
89 {
90 $l->insert("end", "item" . $_);
91 }
92 $l->pack("-side","top","-fill","both","-expand","yes");
93 return $l;
94 }
95
96 sub button
97 {
98 return shift->Button(@_)->pack(-side => 'left');
99 }
100
101 sub checkbutton
102 {
103 my $top = shift;
104 my $b = $top->Checkbutton(@_);
105 $b->pack(-side => 'left');
106 return $b;
107 }
108
109 sub radiobutton
110 {
111 my $top = shift;
112 my $b = $top->Radiobutton(@_);
113 $b->pack(-side => 'left');
114 return $b;
115 }
116
117 sub label
118 {
119 my $top = shift;
120 my $w = $top->Label((@_) ? @_ : ('-text' => "A Label") );
121 $w->pack("-side"=>"bottom","-fill"=>"x");
122 return $w;
123 }
124
125 sub message
126 {
127 my $top = shift;
128 my $w = $top->Message("-text","Press keys in blue square");
129 configure $w "-foreground","blue";
130 $w->pack("-fill"=>"x");
131 return $w;
132 }
133
134 sub keys
135 {my $top = shift;
136 my $w = $top->Frame("-width",40, "-height",40,"-relief" => "ridge",
137 "-background" => "blue", "-border" => 7,
138 "-takefocus" => 1);
139 $w->bind("<Enter>", "focus");
140 $w->bind("<FocusIn>", NoOp);
141 $w->bind("<Any-Escape>", sub { shift->toplevel->destroy()});
142 $w->bind("<Any-KeyPress>",
143 sub { my $w = shift;
144 my $e = $w->XEvent;
145 # print "Key(",join(',',@_),")\n";
146 my $K = $e->K();
147 my $A = $e->A();
148 print "press $K $A\n"
149 });
150 $w->pack();
151 return $w;
152 }
153
154 sub menubar
155 {my $top = shift;
156 require Tk::Menubar;
157 my $menu = $top->Menubar;
158 my $file = $menu->Menubutton("-text" => "File","-underline" => 0, -bg => 'ivory',
159 -tearoff => 0
160 );
161 $file->command("-label","Save","-command" => sub { print "Save\n" },"-underline" => 0);
162 $file->command("-label","Delete","-command" => sub { print "Delete\n" },"-underline" => 0);
163 $file->separator;
164 $file->command("-label","Quit","-command" => sub { $top->destroy },"-underline" => 0);
165 $file->pack("-side" => "left");
166
167 $file = $menu->Menubutton(-text => "Options", -underline => 0, -font => 'fixed');
168
169 $file->checkbutton('-label' => 'Strict ~Motif', '-variable' => 'Tk::strictMotif');
170
171 $file->separator;
172 $file->checkbutton('-label' => 'Oil checked', '-variable' => 'main::oil');
173 $file->checkbutton('-label' => 'Water checked', '-variable' => \$Water);
174 $file->cget('-menu')->bind('<ButtonRelease-1>',sub {print "Hey\n";});
175 $file->command("-label","Interps", "-underline" => 0,
176 "-command" => sub { print 'Interps(',join(',',$file->interps),")\n" } );
177 $file->pack("-side" => "left");
178
179 $file = $menu->Menubutton(-text => "Exceptions", -underline => 0);
180
181 $file->command("-label","Exit","-command" => sub { exit 0 },"-underline" => 0);
182 $file->command("-label","Close","-command" => sub { close Gibberish },"-underline" => 0);
183 $file->command("-label","Moan","-command" => [ \&test_back_trace, $file, 3 ],"-underline" => 0);
184 $file->command("-label","Trace","-command" => \&back_trace,"-underline" => 0);
185 $file->command("-label","Send","-command" => [\&send_test,$top] );
186
187 $file->pack("-side" => "left");
188
189 my $help = $menu->Menubutton(-text => 'Help', -underline => 0,
190 -menuitems => [[ Command => '~Versions', -command => [\&ShowVersion, $top ]]]
191 );
192 $help->pack(-side => 'right');
193 return $menu;
194 }
195
196 sub entry_check
197 {
198 my ($i);
199 for ($i = 0; $i < $entry->width; $i++)
200 {
201 my $cur = $entry->index('@'.$i);
202 print "$i => $cur\n";
203 }
204 }
205
206 sub popup
207 {my $w = shift;
208 my $top = $w->MainWindow;
209 $w->{Cursor} = $top->cget("-cursor");
210 $top->configure("-cursor"=>"watch");
211 print "Event ",$Tk::event,"\n";
212 if (defined $e)
213 {
214 $w->wm("geometry",$e->X . "x" . $e->Y);
215 }
216 $w->deiconify;
217 $w->update;
218 $w->grab;
219 }
220
221 sub popdown
222 {
223 my $w = shift;
224 my $top = $w->MainWindow;
225 $top->configure("-cursor"=> $w->{Cursor});
226 $w->grab("release");
227 $w->withdraw;
228 }
229
230
231 sub dialogue
232 {my $top = shift;
233 my $t = $top->Toplevel("-class","Dialogue",-screen => $ENV{'DISPLAY'});
234 my $l = &label($t,@_);
235 my $b = &button($t,"-text","OK","-command"=>[ \&popdown ,$t]);
236 $t->wm("group",$top);
237 $t->wm("transient",$top);
238 $t->wm("withdraw");
239 $t->wm("minsize",0,0);
240 $l->pack("-side"=> "top", "-expand" => 1, "-fill" => "both");
241 $b->pack("-side"=> "bottom");
242 $t->wm("protocol","WM_DELETE_WINDOW",[\&popdown,$t]);
243 return $t;
244 }
245
246 $top = MainWindow->new();
247
248 $con = $top->Scrolled('Text', -height => 6)->pack(-side => 'bottom', -fill => 'x');
249
250 tie *STDOUT,Tk::Text,$con->Subwidget('text');
251 tie *STDERR,Tk::Text,$con->Subwidget('text');
252
253 $top->optionAdd($top->Name.'*Dialog*Background', 'yellow');
254
255 $Tk::_mw_ = $top;
256
257 $top->bind('MainWindow','<Map>',
258 sub {
259 printf "Mapped %.3g Sec. after bootstrap (u=%.3g s=%.3g)\n",Tk::Time_So_Far,times;
260 printf("%gK\n",Memory->used/1024) if (defined &Memory::used);
261 system("/bin/ps","-o",'vsz,osz,rss,pmem,time','-p',$$) if ($^O eq 'solaris');
262 }
263 );
264
265 $camel = $top->Photo('-file' => Tk->findINC("Xcamel.gif"));
266
267 # print "camel is ",$camel->width," wide\n";
268
269 if (@ARGV)
270 {
271 $top->CmdLine;
272 print "ARGV now ",join(' ',@ARGV),"\n"
273 }
274
275 sub ShowVersion
276 {my ($top) = @_;
277 my $d = $top->Dialog(-title => 'Versions',
278 -popover => $top,
279 -image => $camel,
280 -text =>
281 "Core Tk version : $Tk::version
282 Tk patchLevel : $Tk::patchLevel
283 library : $Tk::library
284 perl/Tk Version : $Tk::VERSION\n",-justify => 'center');
285 $d->Show;
286 }
287
288
289 $fred = "Initial Text";
290 $oil = 1;
291 my $bl = $top->Balloon;
292
293 &menubar($top);
294
295 my $bf = $top->Frame->pack(-fill => 'x');
296
297 $bf->Optionmenu(-textvariable => \$option,
298 -options => [qw(one two three four)],
299 -command => sub { print "Optionmenu set to: ",shift,"\n"}
300 )->pack(-side => 'right');
301
302 my $be = $bf->BrowseEntry(-variable => \$option )->pack(-side => 'right');
303 $be->insert('end',qw(one two three four));
304
305 $QuitPB = $top->Pixmap('-file' => Tk->findINC("demos/images/QuitPB.xpm"));
306 $QuitPBa = $top->Pixmap('-file' => Tk->findINC("demos/images/QuitPBa.xpm"));
307
308 my $q = &button($bf,"-image",$QuitPB,"-activeimage",$QuitPBa,
309 "-command", sub { $top->destroy });
310 $bl->attach($q, -msg => "Exit Application");
311
312 &button($bf,"-text","Push me","-command",
313 [ sub {print "push(",join(',',@_),")\n"}, 1, Two , "iii" ]);
314 &checkbutton($bf,"-text","oil","-variable",\$oil);
315 &checkbutton($bf,"-text","water","-variable",main::Water);
316 &radiobutton($bf,"-text","one","-variable",\$option,"-value"=>"one");
317 &radiobutton($bf,"-text","two","-variable",\$option,"-value"=>"two");
318 &radiobutton($bf,"-text","three","-variable",\$option,"-value"=>"three");
319 &button($bf,"-text","State","-command",
320 sub {print "water=$main::Water oil=$oil option=$option\n"});
321 &button($bf,"-text","Chop","-command", sub { chop($fred) });
322
323 $main::Water = 1;
324
325
326 $entry = &entry($top,0,"-width","30","-textvariable",\$fred);
327 &message($top);
328 &keys($top);
329 &label($top,"-bitmap"=>"info");
330
331
332 $l = &listbox($top,1..25);
333 &button($top,"-bitmap",'@'.Tk->findINC("demos/images/c.icon"),"-command", [ sub { shift->SetList(<*.c>) }, $l ] );
334
335 $pic = $top->Photo("-file" => Tk->findINC("demos/images/earthris.gif"));
336
337 $d = dialogue($top, "-image" => $pic );
338
339 $p = &button($top,"-text" => "popup picture", "-command" => [ \&popup , $d ]);
340 $p = &button($top,"-text" => "bisque", "-command" => [ 'bisque', $top ]);
341 $p = &button($top,"-text" => "Busy",
342 "-command" => sub { $top->Busy; $top->after(5000); $top->Unbusy });
343
344 # Check that we have not broken array context again...
345
346 # print join(',',$top->children),"\n";
347
348 $top->Icon(-image => $camel);
349
350 $top->property("set",MYPROP,AREA,32,[1,2,3,4,5]);
351
352 $top->update("idletasks");
353
354 if (open(Gibberish,"</dev/tty"))
355 {
356 $top->fileevent(Gibberish,'readable',[sub { my ($fh) = @_; print "stdin:",scalar <$fh> },\*Gibberish]);
357 }
358 else
359 {
360 warn "Cannot open /dev/tty:$!";
361 }
362
363 # print "imageNames(",join(',',$top->imageNames),")\n";
364 # print "imageTypes(",join(',',$top->imageTypes),")\n";
365
366 Tk::MainLoop();
367
368