Debian Codegraph @

examples/clip_bug

raw
1 #!/usr/local/bin/perl -w
2 # Show a Tk canvas bug:
3 # when scrolling a canvas, clipping for embedded windows
4 # uses the embedded windows parent, not the canvas.
5 #
6 # Note also that embedded windows do not behave properly
7 # when covered by other canvas items: they stay on top.
8
9
10 use strict;
11 use Tk;
12
13 my $display = new MainWindow;
14 $display->minsize(100, 100);
15
16 # Quit button
17 my $quit = $display->Button(-text => 'Quit', -command => sub {exit;});
18 $quit->pack();
19
20 # Canvas and all
21 my $canvas_frame = $display->Frame;
22 $canvas_frame->pack(-expand => 'yes', -fill => 'both');
23 my $canvas = $canvas_frame->Canvas(-relief => 'sunken', -bd => 2);
24 my $vscroll = $canvas_frame->Scrollbar(-command => ['yview', $canvas]);
25 my $hscroll = $canvas_frame->Scrollbar(-command => ['xview', $canvas],
26 -orient => 'horiz');
27 $canvas->configure(-xscrollcommand => ['set', $hscroll],
28 -yscrollcommand => ['set', $vscroll]);
29 $vscroll->pack(-side => 'right', -fill => 'y');
30 $hscroll->pack(-side => 'bottom', -fill => 'x');
31 $canvas->pack(-expand => 'yes', -fill => 'both');
32 $canvas->configure(-scrollregion => ['0', '0', '10c', '10c']);
33
34 # Add a few items to scroll
35 add_item($display, $canvas, 1);
36 add_item($display, $canvas, 5);
37 add_item($display, $canvas, 9);
38 MainLoop;
39
40
41 # Add a box with a check-button beside
42 # Parameters:
43 # - Reference to window
44 # - Reference to canvas
45 # - Y-pos
46 sub add_item
47 {
48 my ($d, $c, $y) = @_;
49
50 # This button is owned by the display, not the canvas
51 my $button1 = $c->Checkbutton();
52 $c->create(('window', '1c', "$y" . 'c'),
53 -anchor => 'w', -window => $button1);
54
55 # Rectangle
56 my $yp = $y + 2;
57 $c->create(('rectangle', '1.2c', "$y" . 'c', '4c', "$yp" . 'c'),
58 -fill => 'SkyBlue2');
59
60 # This button is owned by the canvas, and doesn't go wrong
61 my $button2 = $c->Checkbutton();
62 $c->create(('window', '4.2c', "$y" . 'c'),
63 -anchor => 'w', -window => $button2);
64 }