File: xlib_demo

package info (click to toggle)
perl-tk 1:800.025-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 18,444 kB
  • ctags: 19,081
  • sloc: ansic: 206,740; perl: 40,187; makefile: 4,371; sh: 2,373; yacc: 762
file content (86 lines) | stat: -rwxr-xr-x 1,694 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/local/bin/perl -w

use Tk;

use Tk::Xlib;

$top = MainWindow->new();

# Check out some of Xlib stuff
$screen = $top->Screen;
$sw     = $screen->WidthOfScreen;
$sh     = $screen->HeightOfScreen;
$name   = $top->Display->DisplayString;
print "$name is [$sw,$sh] pixels\n";

sub DoKey
{my $w = shift;
 my $e = $w->XEvent;
 my $msg = "key : " . $e->A;
 my $dpy = $w->Display;
 my $id  = $w->WindowId;
 unless (defined $w->{GC})
  {
   $w->{GC} = GC->new($dpy,$id,
                     Foreground => 5, 
                     Font => $dpy->XLoadFont("-*-Courier-Medium-R-Normal--*-240-*-*-*-*-*-*"));
  }
 my $gc  = $w->{GC};
 $dpy->XDrawString($id,$gc,$e->x,$e->y,$msg);
}

$top->bind("<Any-KeyPress>",\&DoKey);

$top->bind('<ButtonPress-1>',[\&DrawLine,1]);
$top->bind('<ButtonRelease-1>',[\&DrawLine,0]);
$top->bind('<B1-Motion>',[\&DrawLine,1]);

$top->bind('<ButtonPress-2>',[\&DrawWin,1]);
$top->bind('<ButtonRelease-2>',[\&DrawWin,0]);
$top->bind('<B2-Motion>',[\&DrawWin,1]);

sub DrawLine
{
 my $w = shift;
 my $draw = shift;
 $w->tmpLine(@{delete $w->{'tmpLine'}},6) if (exists $w->{'tmpLine'});
 if ($draw)
  {
   my $e  = $w->XEvent;
   my $x  = $e->x;
   my $y1 = 0;
   my $y2 = $y1 + $w->Height;
   $w->{'tmpLine'} = [$x,$y1,$x,$y2];
   $w->tmpLine(@{$w->{'tmpLine'}},7);
  }
}

sub DrawWin
{
 my $w = shift;
 my $draw = shift;
 my $l = delete $w->{'tmpWin'};
 if ($draw)
  {
   my $e = $w->XEvent;
   unless (defined $l)
    {
     $l = $w->Toplevel(-background => 'black');
     $l->overrideredirect(1);
    }
   $l->MoveResizeWindow($e->X, $w->rooty, 1, $w->Height);
   $w->{'tmpWin'} = $l;
  }
 else
  {
   if (defined $l)
    {
     $l->destroy;
    }
  }
}


MainLoop();