Debian Codegraph @

Tk/Tiler.pm

raw
1 # Copyright (c) 1995-2004 Nick Ing-Simmons. All rights reserved.
2 # This program is free software; you can redistribute it and/or
3 # modify it under the same terms as Perl itself.
4 # An example of a geometry manager "widget" in perl
5 package Tk::Tiler;
6 require Tk;
7 require Tk::Frame;
8
9 use vars qw($VERSION);
10 #$VERSION = sprintf '4.%03d', q$Revision: #12 $ =~ /\D(\d+)\s*$/;
11 $VERSION = '4.013';
12
13 use base qw(Tk::Frame);
14
15 Construct Tk::Widget 'Tiler';
16 sub Tk::Widget::ScrlTiler { shift->Scrolled('Tiler' => @_) }
17
18 sub FocusChildren
19 {
20 return (wantarray) ? () : 0;
21 }
22
23 sub Populate
24 {
25 my ($obj,$args) = @_;
26 $obj->SUPER::Populate($args);
27 $obj->{Slaves} = [];
28 $obj->{LayoutPending} = 0;
29 $obj->{Start} = 0;
30 $obj->{Sw} = 0;
31 $obj->{Sh} = 0;
32 $obj->ConfigSpecs('-takefocus' => ['SELF', 'takeFocus','TakeFocus',1],
33 '-highlightthickness' => ['SELF', 'highlightThickness','HighlightThickness',2],
34 '-yscrollcommand' => ['CALLBACK',undef,undef,undef],
35 '-columns' => ['PASSIVE','columns','Columns',5],
36 '-rows' => ['PASSIVE','rows','Rows',10]
37 );
38 return $obj;
39 }
40
41 sub change_size
42 {
43 my ($w) = shift;
44 my $r = $w->cget('-rows');
45 my $c = $w->cget('-columns');
46 my $bw = $w->cget(-highlightthickness);
47 if (defined $r && defined $c)
48 {
49 $w->GeometryRequest($c*$w->{Sw}+2*$bw,$r*$w->{Sh}+2*$bw);
50 }
51 }
52
53 sub Layout
54 {
55 my $m = shift;
56 my $bw = $m->cget(-highlightthickness);
57 my $why = $m->{LayoutPending};
58 $m->{LayoutPending} = 0;
59 my $W = $m->Width;
60 my $H = $m->Height;
61 my $w = $m->{Sw} || 1; # max width of slave
62 my $h = $m->{Sh} || 1; # max height of slave
63 my $x = $bw;
64 my $y = $bw;
65 my $start = 0;
66 # Set size and position of slaves
67 my $rows = $m->{Rows} = int(($H-2*$bw)/$h) || 1;
68 my $cols = $m->{Cols} = int(($W-2*$bw)/$w) || 1;
69 my $need = $m->{Need} = int( (@{$m->{Slaves}}+$cols-1)/$cols );
70 $m->{Start} = ($need - $rows) if ($m->{Start} + $rows > $need);
71
72 $m->{Start} = 0 if ($m->{Start} < 0);
73 my $row = 0;
74 my @posn = ();
75 my $s;
76 foreach $s (@{$m->{Slaves}})
77 {
78 if ($row < $m->{Start})
79 {
80 $s->UnmapWindow;
81 $x += $w;
82 if ($x+$w+$bw > $W)
83 {
84 $x = $bw;
85 $row++;
86 }
87 }
88 elsif ($y+$h+$bw > $H)
89 {
90 $s->UnmapWindow;
91 $s->ResizeWindow($w,$h) if ($why & 1);
92 }
93 else
94 {
95 push(@posn,[$s,$x,$y]);
96 $x += $w;
97 if ($x+$w+$bw > $W)
98 {
99 $x = $bw;
100 $y += $h;
101 $row++;
102 }
103 }
104 $s->ResizeWindow($w,$h) if ($why & 1);
105 }
106 $row++ if ($x > $bw);
107 if (defined $m->{Prev} && $m->{Prev} > $m->{Start})
108 {
109 @posn = reverse(@posn);
110 }
111 while (@posn)
112 {
113 my $posn = shift(@posn);
114 my ($s,$x,$y) = (@$posn);
115 $s->MoveWindow($x,$y);
116 $s->MapWindow;
117 }
118 $m->{Prev} = $m->{Start};
119 $m->Callback(-yscrollcommand => $m->{Start}/$need,$row/$need) if $need;
120 }
121
122 sub QueueLayout
123 {
124 my ($m,$why) = @_;
125 $m->afterIdle(['Layout',$m]) unless ($m->{LayoutPending});
126 $m->{LayoutPending} |= $why;
127 }
128
129 sub SlaveGeometryRequest
130 {
131 my ($m,$s) = @_;
132 my $sw = $s->ReqWidth;
133 my $sh = $s->ReqHeight;
134 my $sz = 0;
135 if ($sw > $m->{Sw})
136 {
137 $m->{Sw} = $sw;
138 $m->QueueLayout(1);
139 $sz++;
140 }
141 if ($sh > $m->{Sh})
142 {
143 $m->{Sh} = $sh;
144 $m->QueueLayout(1);
145 $sz++;
146 }
147 $m->change_size if ($sz);
148 }
149
150 sub LostSlave
151 {
152 my ($m,$s) = @_;
153 @{$m->{Slaves}} = grep($_ != $s,@{$m->{Slaves}});
154 $m->QueueLayout(2);
155 }
156
157 sub Manage
158 {
159 my $m = shift;
160 my $s;
161 foreach $s (@_)
162 {
163 $m->ManageGeometry($s);
164 push(@{$m->{Slaves}},$s);
165 $m->SlaveGeometryRequest($s);
166 }
167 $m->QueueLayout(2 | 1);
168 }
169
170 sub moveto
171 {
172 my ($m,$frac) = (@_);
173 $m->{Start} = int($m->{Need} * $frac);
174 $m->QueueLayout(4);
175 }
176
177 sub scroll
178 {
179 my ($m,$delta,$type) = @_;
180 $delta *= $m->{Rows}/2 if ($type eq 'pages');
181 $m->{Start} += $delta;
182 $m->QueueLayout(4);
183 }
184
185 sub yview { my $w = shift; my $c = shift; $w->$c(@_) }
186
187 sub FocusIn
188 {
189 my ($w) = @_;
190 # print 'Focus ',$w->PathName,"\n";
191 }
192
193 sub ClassInit
194 {
195 my ($class,$mw) = @_;
196 $mw->bind($class,'<Configure>',['QueueLayout',8]);
197 $mw->bind($class,'<FocusIn>', 'NoOp');
198 $mw->YscrollBind($class);
199 return $class;
200 }
201
202 1;