Debian Codegraph @

examples/xlib_demo

raw
1 #!/usr/local/bin/perl -w
2
3 use Tk;
4
5 use Tk::Xlib;
6
7 $top = MainWindow->new();
8
9 # Check out some of Xlib stuff
10 $screen = $top->Screen;
11 $sw = $screen->WidthOfScreen;
12 $sh = $screen->HeightOfScreen;
13 $name = $top->Display->DisplayString;
14 print "$name is [$sw,$sh] pixels\n";
15
16 sub DoKey
17 {my $w = shift;
18 my $e = $w->XEvent;
19 my $msg = "key : " . $e->A;
20 my $dpy = $w->Display;
21 my $id = $w->WindowId;
22 unless (defined $w->{GC})
23 {
24 $w->{GC} = GC->new($dpy,$id,
25 Foreground => 5,
26 Font => $dpy->XLoadFont("-*-Courier-Medium-R-Normal--*-240-*-*-*-*-*-*"));
27 }
28 my $gc = $w->{GC};
29 $dpy->XDrawString($id,$gc,$e->x,$e->y,$msg);
30 }
31
32 $top->bind("<Any-KeyPress>",\&DoKey);
33
34 $top->bind('<ButtonPress-1>',[\&DrawLine,1]);
35 $top->bind('<ButtonRelease-1>',[\&DrawLine,0]);
36 $top->bind('<B1-Motion>',[\&DrawLine,1]);
37
38 $top->bind('<ButtonPress-2>',[\&DrawWin,1]);
39 $top->bind('<ButtonRelease-2>',[\&DrawWin,0]);
40 $top->bind('<B2-Motion>',[\&DrawWin,1]);
41
42 # XXX Does not work.
43 sub DrawLine
44 {
45 my $w = shift;
46 my $draw = shift;
47 $w->tmpLine(@{delete $w->{'tmpLine'}},6) if (exists $w->{'tmpLine'});
48 if ($draw)
49 {
50 my $e = $w->XEvent;
51 my $x = $e->x;
52 my $y1 = 0;
53 my $y2 = $y1 + $w->Height;
54 $w->{'tmpLine'} = [$x,$y1,$x,$y2];
55 $w->tmpLine(@{$w->{'tmpLine'}},7);
56 }
57 }
58
59 sub DrawWin
60 {
61 my $w = shift;
62 my $draw = shift;
63 my $l = delete $w->{'tmpWin'};
64 if ($draw)
65 {
66 my $e = $w->XEvent;
67 unless (defined $l)
68 {
69 $l = $w->Toplevel(-background => 'black');
70 $l->overrideredirect(1);
71 }
72 $l->MoveResizeWindow($e->X, $w->rooty, 1, $w->Height);
73 $w->{'tmpWin'} = $l;
74 }
75 else
76 {
77 if (defined $l)
78 {
79 $l->destroy;
80 }
81 }
82 }
83
84
85 MainLoop();
86
87